test_elementwise_mul_op.py 8.1 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

from __future__ import print_function
16 17
import unittest
import numpy as np
18
from op_test import OpTest, skip_check_grad_ci
19 20
import paddle.fluid.core as core
from paddle.fluid.op import Operator
21 22
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
23 24


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

29 30
    def setUp(self):
        self.op_type = "elementwise_mul"
31
        self.dtype = np.float64
32 33 34 35 36 37
        self.axis = -1
        self.init_dtype()
        self.init_input_output()
        self.init_kernel_type()
        self.init_axis()

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

    def test_check_output(self):
46 47
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_output(check_dygraph=(self.use_mkldnn == False))
48 49

    def test_check_grad_normal(self):
50 51 52
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_grad(
            ['X', 'Y'], 'Out', check_dygraph=(self.use_mkldnn == False))
53 54

    def test_check_grad_ingore_x(self):
55 56 57 58 59 60
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            check_dygraph=(self.use_mkldnn == False))
61 62

    def test_check_grad_ingore_y(self):
63 64 65 66 67 68
        # TODO(wangzhongpu): support mkldnn op in dygraph mode
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            check_dygraph=(self.use_mkldnn == False))
69

70 71 72 73 74 75 76 77 78 79 80
    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

81

82 83
@skip_check_grad_ci(
    reason="[skip shape check] Use y_shape(1) to test broadcast.")
84 85 86 87
class TestElementwiseMulOp_scalar(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
88 89
            'X': np.random.rand(10, 3, 4).astype(np.float64),
            'Y': np.random.rand(1).astype(np.float64)
90 91
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
92
        self.init_kernel_type()
93 94


G
gongweibao 已提交
95
class TestElementwiseMulOp_Vector(ElementwiseMulOp):
96 97 98
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
99 100
            'X': np.random.random((100, )).astype("float64"),
            'Y': np.random.random((100, )).astype("float64")
101 102
        }
        self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])}
103
        self.init_kernel_type()
104 105


G
gongweibao 已提交
106
class TestElementwiseMulOp_broadcast_0(ElementwiseMulOp):
107
    def init_input_output(self):
108 109 110
        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)
111

112 113
    def init_axis(self):
        self.axis = 0
114 115


G
gongweibao 已提交
116
class TestElementwiseMulOp_broadcast_1(ElementwiseMulOp):
117 118 119
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
120 121
            'X': np.random.rand(2, 100, 3).astype(np.float64),
            'Y': np.random.rand(100).astype(np.float64)
122 123 124 125
        }

        self.attrs = {'axis': 1}
        self.outputs = {
126
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 100, 1)
127
        }
128
        self.init_kernel_type()
129 130


G
gongweibao 已提交
131
class TestElementwiseMulOp_broadcast_2(ElementwiseMulOp):
132 133 134
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
135 136
            'X': np.random.rand(2, 3, 100).astype(np.float64),
            'Y': np.random.rand(100).astype(np.float64)
137 138 139
        }

        self.outputs = {
140
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 1, 100)
141
        }
142
        self.init_kernel_type()
143 144


G
gongweibao 已提交
145
class TestElementwiseMulOp_broadcast_3(ElementwiseMulOp):
146 147 148
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
149 150
            'X': np.random.rand(2, 10, 12, 3).astype(np.float64),
            'Y': np.random.rand(10, 12).astype(np.float64)
151 152 153 154
        }

        self.attrs = {'axis': 1}
        self.outputs = {
155
            'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 10, 12, 1)
156
        }
157
        self.init_kernel_type()
158 159


160 161 162 163
class TestElementwiseMulOp_broadcast_4(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
164 165
            'X': np.random.rand(10, 2, 11).astype(np.float64),
            'Y': np.random.rand(10, 1, 11).astype(np.float64)
166 167
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
168
        self.init_kernel_type()
169 170 171 172 173 174


class TestElementwiseMulOp_broadcast_5(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
175 176
            'X': np.random.rand(10, 4, 2, 3).astype(np.float64),
            'Y': np.random.rand(10, 4, 1, 3).astype(np.float64)
177 178
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
179
        self.init_kernel_type()
180 181


182 183
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
W
Wu Yi 已提交
184 185 186 187 188
class TestElementwiseMulOpFp16(ElementwiseMulOp):
    def init_dtype(self):
        self.dtype = np.float16


189 190 191 192
class TestElementwiseMulOp_commonuse_1(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
193 194
            'X': np.random.rand(2, 3, 100).astype(np.float64),
            'Y': np.random.rand(1, 1, 100).astype(np.float64)
195 196
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
197
        self.init_kernel_type()
198 199 200 201 202 203


class TestElementwiseMulOp_commonuse_2(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
204 205
            'X': np.random.rand(30, 3, 1, 5).astype(np.float64),
            'Y': np.random.rand(30, 1, 4, 1).astype(np.float64)
206 207
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
208
        self.init_kernel_type()
209 210 211 212 213 214


class TestElementwiseMulOp_xsize_lessthan_ysize(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
215 216
            'X': np.random.rand(10, 10).astype(np.float64),
            'Y': np.random.rand(2, 2, 10, 10).astype(np.float64)
217 218 219 220 221
        }

        self.attrs = {'axis': 2}

        self.outputs = {
222
            'Out': self.inputs['X'].reshape(1, 1, 10, 10) * self.inputs['Y']
223
        }
224
        self.init_kernel_type()
225 226


227
class TestElementwiseMulOpError(unittest.TestCase):
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    def test_errors(self):
        with program_guard(Program(), Program()):
            # the input of elementwise_mul must be Variable.
            x1 = fluid.create_lod_tensor(
                np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace())
            y1 = fluid.create_lod_tensor(
                np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace())
            self.assertRaises(TypeError, fluid.layers.elementwise_mul, x1, y1)

            # the input dtype of elementwise_mul must be float16 or float32 or float64 or int32 or int64
            # float16 only can be set on GPU place
            x2 = fluid.layers.data(name='x2', shape=[3, 4, 5, 6], dtype="uint8")
            y2 = fluid.layers.data(name='y2', shape=[3, 4, 5, 6], dtype="uint8")
            self.assertRaises(TypeError, fluid.layers.elementwise_mul, x2, y2)


244 245
if __name__ == '__main__':
    unittest.main()