test_elementwise_mul_op.py 18.2 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
from paddle.fluid import 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.public_python_api = paddle.multiply
33
        self.dtype = np.float64
34 35 36 37 38
        self.axis = -1
        self.init_dtype()
        self.init_input_output()
        self.init_kernel_type()
        self.init_axis()
39
        self.if_enable_cinn()
40

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

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

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

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

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

81 82 83 84 85 86 87 88 89 90 91
    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

92
    def if_enable_cinn(self):
93 94
        pass

95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
class TestComplexElementwiseMulOpWithCheckGrad(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.python_api = paddle.multiply
        self.public_python_api = paddle.multiply
        self.dtype = np.complex128
        self.axis = -1
        self.init_dtype()
        self.init_input_output()
        self.init_kernel_type()
        self.init_axis()
        self.if_enable_cinn()

        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}

    def init_input_output(self):
        self.x = np.array([3 + 4j, 1 + 2j]).astype(self.dtype)
        self.y = np.array([3 + 4j, 5 + 6j]).astype(self.dtype)
        self.out = np.multiply(self.x, self.y)

    def if_enable_cinn(self):
        self.enable_cinn = False

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

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

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


145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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)


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)


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)


166 167 168
class TestBF16ElementwiseMulOp(OpTest):
    def setUp(self):
        self.op_type = "elementwise_mul"
169 170
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
171
        self.public_python_api = paddle.multiply
172 173 174 175 176 177 178 179 180
        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 = {
181 182 183 184 185 186
            '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)
            ),
187 188 189
        }
        self.outputs = {'Out': convert_float_to_uint16(self.out)}
        self.attrs = {'axis': self.axis, 'use_mkldnn': False}
190
        self.if_enable_cinn()
191 192 193 194 195

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
196
        self.check_grad(['X', 'Y'], 'Out', check_prim=True)
197 198

    def test_check_grad_ingore_x(self):
199 200 201 202 203 204
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            check_prim=True,
        )
205 206

    def test_check_grad_ingore_y(self):
207 208 209 210 211 212
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            check_prim=True,
        )
213

214
    def if_enable_cinn(self):
215
        self.enable_cinn = False
216 217


218
@skip_check_grad_ci(
219 220
    reason="[skip shape check] Use y_shape(1) to test broadcast."
)
221 222 223
class TestElementwiseMulOp_scalar(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
224 225
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
226
        self.public_python_api = paddle.multiply
227
        self.inputs = {
228
            'X': np.random.rand(10, 3, 4).astype(np.float64),
229
            'Y': np.random.rand(1).astype(np.float64),
230 231
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
232
        self.init_kernel_type()
233 234


G
gongweibao 已提交
235
class TestElementwiseMulOp_Vector(ElementwiseMulOp):
236 237
    def setUp(self):
        self.op_type = "elementwise_mul"
238 239
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
240
        self.public_python_api = paddle.multiply
241
        self.inputs = {
242 243
            'X': np.random.random((100,)).astype("float64"),
            'Y': np.random.random((100,)).astype("float64"),
244 245
        }
        self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])}
246
        self.init_kernel_type()
247 248


249 250 251 252 253 254 255 256
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
257
        self.public_python_api = paddle.multiply
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        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):
322 323 324
        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)
325 326 327 328 329 330
        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}
331

332 333
    def init_axis(self):
        self.axis = 0
334 335


336 337
class TestElementwiseMulOp_broadcast_1(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
338
        self.inputs = {
339
            'X': np.random.rand(2, 100, 3).astype(np.float64),
340
            'Y': np.random.rand(100).astype(np.float64),
341 342
        }

343
        self.attrs = {'axis': self.axis}
344
        self.outputs = {
345
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 100, 1)
346
        }
347 348 349

    def init_axis(self):
        self.axis = 1
350 351


352 353
class TestElementwiseMulOp_broadcast_2(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
354
        self.inputs = {
355
            'X': np.random.rand(2, 3, 100).astype(np.float64),
356
            'Y': np.random.rand(100).astype(np.float64),
357
        }
358
        self.attrs = {'axis': self.axis}
359
        self.outputs = {
360
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 1, 100)
361 362 363
        }


364 365
class TestElementwiseMulOp_broadcast_3(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
366
        self.inputs = {
367
            'X': np.random.rand(2, 10, 12, 3).astype(np.float64),
368
            'Y': np.random.rand(10, 12).astype(np.float64),
369 370
        }

371
        self.attrs = {'axis': self.axis}
372
        self.outputs = {
373
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 10, 12, 1)
374
        }
375 376 377

    def init_axis(self):
        self.axis = 1
378 379


380 381
class TestElementwiseMulOp_broadcast_4(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
382
        self.inputs = {
383
            'X': np.random.rand(10, 2, 11).astype(np.float64),
384
            'Y': np.random.rand(10, 1, 11).astype(np.float64),
385
        }
386
        self.attrs = {'axis': self.axis}
387 388 389
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}


390 391
class TestElementwiseMulOp_broadcast_5(ElementwiseMulOp_broadcast):
    def init_input_attr_output(self):
392
        self.inputs = {
393
            'X': np.random.rand(10, 4, 2, 3).astype(np.float64),
394
            'Y': np.random.rand(10, 4, 1, 3).astype(np.float64),
395
        }
396
        self.attrs = {'axis': self.axis}
397 398 399
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}


400 401 402
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
W
Wu Yi 已提交
403 404 405 406
class TestElementwiseMulOpFp16(ElementwiseMulOp):
    def init_dtype(self):
        self.dtype = np.float16

407
    def if_enable_cinn(self):
408
        pass
409

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    def test_check_output(self):
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_output(check_dygraph=(not self.use_mkldnn))

    def test_check_grad_normal(self):
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_grad(
            ['X', 'Y'],
            'Out',
            check_dygraph=(not self.use_mkldnn),
            check_prim=True,
        )

    def test_check_grad_ingore_x(self):
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            check_dygraph=(not self.use_mkldnn),
            check_prim=True,
        )

    def test_check_grad_ingore_y(self):
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            check_dygraph=(not self.use_mkldnn),
            check_prim=True,
        )

W
Wu Yi 已提交
443

444 445 446
class TestElementwiseMulOp_commonuse_1(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
447 448
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
449
        self.public_python_api = paddle.multiply
450
        self.inputs = {
451
            'X': np.random.rand(2, 3, 100).astype(np.float64),
452
            'Y': np.random.rand(1, 1, 100).astype(np.float64),
453 454
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
455
        self.init_kernel_type()
456 457 458 459 460


class TestElementwiseMulOp_commonuse_2(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
461 462
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
463
        self.public_python_api = paddle.multiply
464
        self.inputs = {
465
            'X': np.random.rand(30, 3, 1, 5).astype(np.float64),
466
            'Y': np.random.rand(30, 1, 4, 1).astype(np.float64),
467 468
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
469
        self.init_kernel_type()
470 471 472 473 474


class TestElementwiseMulOp_xsize_lessthan_ysize(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
475 476
        self.prim_op_type = "prim"
        self.python_api = paddle.multiply
477
        self.public_python_api = paddle.multiply
478
        self.inputs = {
479
            'X': np.random.rand(10, 10).astype(np.float64),
480
            'Y': np.random.rand(2, 2, 10, 10).astype(np.float64),
481 482 483 484 485
        }

        self.attrs = {'axis': 2}

        self.outputs = {
486
            'Out': self.inputs['X'].reshape(1, 1, 10, 10) * self.inputs['Y']
487
        }
488
        self.init_kernel_type()
489 490


491 492 493
class TestComplexElementwiseMulOp(OpTest):
    def setUp(self):
        self.op_type = "elementwise_mul"
494
        self.python_api = paddle.multiply
495 496 497 498 499 500
        self.init_base_dtype()
        self.init_input_output()
        self.init_grad_input_output()

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
501
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
502 503 504 505 506 507 508 509
        }
        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):
510 511 512 513 514 515
        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)
516 517 518
        self.out = self.x * self.y

    def init_grad_input_output(self):
519 520 521
        self.grad_out = np.ones((2, 3, 4, 5), self.dtype) + 1j * np.ones(
            (2, 3, 4, 5), self.dtype
        )
522 523 524 525 526 527 528
        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):
529 530 531 532 533 534
        self.check_grad(
            ['X', 'Y'],
            'Out',
            user_defined_grads=[self.grad_x, self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
535 536

    def test_check_grad_ingore_x(self):
537 538 539 540 541 542 543
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            user_defined_grads=[self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
544 545

    def test_check_grad_ingore_y(self):
546 547 548 549 550 551 552
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            user_defined_grads=[self.grad_x],
            user_defined_grad_outputs=[self.grad_out],
        )
553 554


C
chentianyu03 已提交
555 556 557
class TestRealComplexElementwiseMulOp(TestComplexElementwiseMulOp):
    def init_input_output(self):
        self.x = np.random.random((2, 3, 4, 5)).astype(self.dtype)
558 559 560
        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 已提交
561 562 563
        self.out = self.x * self.y

    def init_grad_input_output(self):
564 565 566
        self.grad_out = np.ones((2, 3, 4, 5), self.dtype) + 1j * np.ones(
            (2, 3, 4, 5), self.dtype
        )
C
chentianyu03 已提交
567 568 569 570
        self.grad_x = np.real(self.grad_out * np.conj(self.y))
        self.grad_y = self.grad_out * np.conj(self.x)


571
class TestElementwiseMulop(unittest.TestCase):
572
    def test_dygraph_mul(self):
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
        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()


593
if __name__ == '__main__':
594
    paddle.enable_static()
595
    unittest.main()