test_elementwise_mul_op.py 15.6 KB
Newer Older
1
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
9 10 11 12 13
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14

15
import unittest
16

17
import numpy as np
18
from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci
19

20
import paddle
21
import paddle.fluid.core as core
22 23


G
gongweibao 已提交
24
class ElementwiseMulOp(OpTest):
25 26 27
    def init_kernel_type(self):
        self.use_mkldnn = False

28 29
    def setUp(self):
        self.op_type = "elementwise_mul"
30 31
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
32
        self.dtype = np.float64
33 34 35 36 37
        self.axis = -1
        self.init_dtype()
        self.init_input_output()
        self.init_kernel_type()
        self.init_axis()
38
        self.if_enable_cinn()
39

40
        self.inputs = {
41
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
42
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
43
        }
44 45
        self.outputs = {'Out': self.out}
        self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
46 47

    def test_check_output(self):
48
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
49
        self.check_output(check_dygraph=(not self.use_mkldnn))
50 51

    def test_check_grad_normal(self):
52
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
53 54 55 56 57 58
        self.check_grad(
            ['X', 'Y'],
            'Out',
            check_dygraph=(not self.use_mkldnn),
            check_prim=True,
        )
59 60

    def test_check_grad_ingore_x(self):
61
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
62 63 64 65
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
66
            check_dygraph=(not self.use_mkldnn),
67
            check_prim=True,
68
        )
69 70

    def test_check_grad_ingore_y(self):
71
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
72 73 74 75
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
76
            check_dygraph=(not self.use_mkldnn),
77
            check_prim=True,
78
        )
79

80 81 82 83 84 85 86 87 88 89 90
    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        self.out = np.multiply(self.x, self.y)

    def init_dtype(self):
        pass

    def init_axis(self):
        pass

91
    def if_enable_cinn(self):
92 93
        pass

94

95 96 97 98 99 100
class TestElementwiseMulOp_ZeroDim1(ElementwiseMulOp):
    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, []).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, []).astype(self.dtype)
        self.out = np.multiply(self.x, self.y)

101
    def if_enable_cinn(self):
102 103
        self.enable_cinn = False

104 105 106 107 108 109 110

class TestElementwiseMulOp_ZeroDim2(ElementwiseMulOp):
    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, []).astype(self.dtype)
        self.out = np.multiply(self.x, self.y)

111
    def if_enable_cinn(self):
112 113
        self.enable_cinn = False

114 115 116 117 118 119 120

class TestElementwiseMulOp_ZeroDim3(ElementwiseMulOp):
    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, []).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        self.out = np.multiply(self.x, self.y)

121
    def if_enable_cinn(self):
122 123
        self.enable_cinn = False

124

125 126 127
class TestBF16ElementwiseMulOp(OpTest):
    def setUp(self):
        self.op_type = "elementwise_mul"
128 129
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
130 131 132 133 134 135 136 137 138
        self.dtype = np.uint16

        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(np.float32)
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype(np.float32)
        self.out = np.multiply(self.x, self.y)

        self.axis = -1

        self.inputs = {
139 140 141 142 143 144
            'X': OpTest.np_dtype_to_fluid_dtype(
                convert_float_to_uint16(self.x)
            ),
            'Y': OpTest.np_dtype_to_fluid_dtype(
                convert_float_to_uint16(self.y)
            ),
145 146 147
        }
        self.outputs = {'Out': convert_float_to_uint16(self.out)}
        self.attrs = {'axis': self.axis, 'use_mkldnn': False}
148
        self.if_enable_cinn()
149 150 151 152 153

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
154
        self.check_grad(['X', 'Y'], 'Out', check_prim=True)
155 156

    def test_check_grad_ingore_x(self):
157
        self.check_grad(['Y'], 'Out', no_grad_set=set("X"), check_prim=True)
158 159

    def test_check_grad_ingore_y(self):
160 161
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'), check_prim=True)

162
    def if_enable_cinn(self):
163
        self.enable_cinn = False
164 165


166
@skip_check_grad_ci(
167 168
    reason="[skip shape check] Use y_shape(1) to test broadcast."
)
169 170 171
class TestElementwiseMulOp_scalar(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
172 173
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
174
        self.inputs = {
175
            'X': np.random.rand(10, 3, 4).astype(np.float64),
176
            'Y': np.random.rand(1).astype(np.float64),
177 178
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
179
        self.init_kernel_type()
180 181


G
gongweibao 已提交
182
class TestElementwiseMulOp_Vector(ElementwiseMulOp):
183 184
    def setUp(self):
        self.op_type = "elementwise_mul"
185 186
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
187
        self.inputs = {
188 189
            'X': np.random.random((100,)).astype("float64"),
            'Y': np.random.random((100,)).astype("float64"),
190 191
        }
        self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])}
192
        self.init_kernel_type()
193 194


195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
class ElementwiseMulOp_broadcast(OpTest):
    def init_kernel_type(self):
        self.use_mkldnn = False

    def setUp(self):
        self.op_type = "elementwise_mul"
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
        self.init_dtype()
        self.init_kernel_type()
        self.init_axis()
        self.init_input_attr_output()
        self.if_check_prim()
        self.if_check_dygraph()

    def test_check_output(self):
        self.check_output(
            check_dygraph=self.check_dygraph, check_prim=self.check_prim
        )

    def test_check_grad_normal(self):
        self.check_grad(
            ['X', 'Y'],
            'Out',
            check_dygraph=self.check_dygraph,
            check_prim=self.check_prim,
        )

    def test_check_grad_ingore_x(self):
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            check_dygraph=self.check_dygraph,
            check_prim=self.check_prim,
        )

    def test_check_grad_ingore_y(self):
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            check_dygraph=self.check_dygraph,
            check_prim=self.check_prim,
        )

    def init_input_attr_output(self):
        self.x = np.random.uniform(0.1, 1, [13, 17, 1]).astype(self.dtype)
        self.y = np.random.uniform(0.1, 1, [17, 17]).astype(self.dtype)
        self.out = np.multiply(self.x, self.y)
        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
        }
        self.outputs = {'Out': self.out}
        self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}

    def init_dtype(self):
        self.dtype = np.float64

    def init_axis(self):
        self.axis = -1

    def if_check_prim(self):
        self.check_prim = self.axis == -1

    def if_check_dygraph(self):
        self.check_dygraph = (not self.use_mkldnn) and (self.axis == -1)


class TestElementwiseMulOp_broadcast_0(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
267 268 269
        self.x = np.random.rand(100, 2, 3).astype(self.dtype)
        self.y = np.random.rand(100).astype(self.dtype)
        self.out = self.x * self.y.reshape(100, 1, 1)
270 271 272 273 274 275
        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
        }
        self.outputs = {'Out': self.out}
        self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
276

277 278
    def init_axis(self):
        self.axis = 0
279 280


281 282
class TestElementwiseMulOp_broadcast_1(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
283
        self.inputs = {
284
            'X': np.random.rand(2, 100, 3).astype(np.float64),
285
            'Y': np.random.rand(100).astype(np.float64),
286 287
        }

288
        self.attrs = {'axis': self.axis}
289
        self.outputs = {
290
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 100, 1)
291
        }
292 293 294

    def init_axis(self):
        self.axis = 1
295 296


297 298
class TestElementwiseMulOp_broadcast_2(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
299
        self.inputs = {
300
            'X': np.random.rand(2, 3, 100).astype(np.float64),
301
            'Y': np.random.rand(100).astype(np.float64),
302
        }
303
        self.attrs = {'axis': self.axis}
304
        self.outputs = {
305
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 1, 100)
306 307 308
        }


309 310
class TestElementwiseMulOp_broadcast_3(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
311
        self.inputs = {
312
            'X': np.random.rand(2, 10, 12, 3).astype(np.float64),
313
            'Y': np.random.rand(10, 12).astype(np.float64),
314 315
        }

316
        self.attrs = {'axis': self.axis}
317
        self.outputs = {
318
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 10, 12, 1)
319
        }
320 321 322

    def init_axis(self):
        self.axis = 1
323 324


325 326
class TestElementwiseMulOp_broadcast_4(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
327
        self.inputs = {
328
            'X': np.random.rand(10, 2, 11).astype(np.float64),
329
            'Y': np.random.rand(10, 1, 11).astype(np.float64),
330
        }
331
        self.attrs = {'axis': self.axis}
332 333 334
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}


335 336
class TestElementwiseMulOp_broadcast_5(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
337
        self.inputs = {
338
            'X': np.random.rand(10, 4, 2, 3).astype(np.float64),
339
            'Y': np.random.rand(10, 4, 1, 3).astype(np.float64),
340
        }
341
        self.attrs = {'axis': self.axis}
342 343 344
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}


345 346 347
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
W
Wu Yi 已提交
348 349 350 351
class TestElementwiseMulOpFp16(ElementwiseMulOp):
    def init_dtype(self):
        self.dtype = np.float16

352
    def if_enable_cinn(self):
353
        pass
354

W
Wu Yi 已提交
355

356 357 358
class TestElementwiseMulOp_commonuse_1(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
359 360
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
361
        self.inputs = {
362
            'X': np.random.rand(2, 3, 100).astype(np.float64),
363
            'Y': np.random.rand(1, 1, 100).astype(np.float64),
364 365
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
366
        self.init_kernel_type()
367 368 369 370 371


class TestElementwiseMulOp_commonuse_2(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
372 373
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
374
        self.inputs = {
375
            'X': np.random.rand(30, 3, 1, 5).astype(np.float64),
376
            'Y': np.random.rand(30, 1, 4, 1).astype(np.float64),
377 378
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
379
        self.init_kernel_type()
380 381 382 383 384


class TestElementwiseMulOp_xsize_lessthan_ysize(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
385 386
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
387
        self.inputs = {
388
            'X': np.random.rand(10, 10).astype(np.float64),
389
            'Y': np.random.rand(2, 2, 10, 10).astype(np.float64),
390 391 392 393 394
        }

        self.attrs = {'axis': 2}

        self.outputs = {
395
            'Out': self.inputs['X'].reshape(1, 1, 10, 10) * self.inputs['Y']
396
        }
397
        self.init_kernel_type()
398 399


400 401 402
class TestComplexElementwiseMulOp(OpTest):
    def setUp(self):
        self.op_type = "elementwise_mul"
403
        self.python_api = paddle.multiply
404 405 406 407 408 409
        self.init_base_dtype()
        self.init_input_output()
        self.init_grad_input_output()

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
410
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
411 412 413 414 415 416 417 418
        }
        self.attrs = {'axis': -1, 'use_mkldnn': False}
        self.outputs = {'Out': self.out}

    def init_base_dtype(self):
        self.dtype = np.float64

    def init_input_output(self):
419 420 421 422 423 424
        self.x = np.random.random((2, 3, 4, 5)).astype(
            self.dtype
        ) + 1j * np.random.random((2, 3, 4, 5)).astype(self.dtype)
        self.y = np.random.random((2, 3, 4, 5)).astype(
            self.dtype
        ) + 1j * np.random.random((2, 3, 4, 5)).astype(self.dtype)
425 426 427
        self.out = self.x * self.y

    def init_grad_input_output(self):
428 429 430
        self.grad_out = np.ones((2, 3, 4, 5), self.dtype) + 1j * np.ones(
            (2, 3, 4, 5), self.dtype
        )
431 432 433 434 435 436 437
        self.grad_x = self.grad_out * np.conj(self.y)
        self.grad_y = self.grad_out * np.conj(self.x)

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
438 439 440 441 442 443
        self.check_grad(
            ['X', 'Y'],
            'Out',
            user_defined_grads=[self.grad_x, self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
444 445

    def test_check_grad_ingore_x(self):
446 447 448 449 450 451 452
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            user_defined_grads=[self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
453 454

    def test_check_grad_ingore_y(self):
455 456 457 458 459 460 461
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            user_defined_grads=[self.grad_x],
            user_defined_grad_outputs=[self.grad_out],
        )
462 463


C
chentianyu03 已提交
464 465 466
class TestRealComplexElementwiseMulOp(TestComplexElementwiseMulOp):
    def init_input_output(self):
        self.x = np.random.random((2, 3, 4, 5)).astype(self.dtype)
467 468 469
        self.y = np.random.random((2, 3, 4, 5)).astype(
            self.dtype
        ) + 1j * np.random.random((2, 3, 4, 5)).astype(self.dtype)
C
chentianyu03 已提交
470 471 472
        self.out = self.x * self.y

    def init_grad_input_output(self):
473 474 475
        self.grad_out = np.ones((2, 3, 4, 5), self.dtype) + 1j * np.ones(
            (2, 3, 4, 5), self.dtype
        )
C
chentianyu03 已提交
476 477 478 479
        self.grad_x = np.real(self.grad_out * np.conj(self.y))
        self.grad_y = self.grad_out * np.conj(self.x)


480
class TestElementwiseMulop(unittest.TestCase):
481
    def test_dygraph_mul(self):
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        paddle.disable_static()

        np_a = np.random.random((2, 3, 4)).astype(np.float32)
        np_b = np.random.random((2, 3, 4)).astype(np.float32)

        tensor_a = paddle.to_tensor(np_a, dtype="float32")
        tensor_b = paddle.to_tensor(np_b, dtype="float32")

        # normal case: nparray * tenor
        expect_out = np_a * np_b
        actual_out = np_a * tensor_b
        np.testing.assert_allclose(actual_out, expect_out)

        # normal case: tensor * nparray
        actual_out = tensor_a * np_b
        np.testing.assert_allclose(actual_out, expect_out)

        paddle.enable_static()


502
if __name__ == '__main__':
503
    paddle.enable_static()
504
    unittest.main()