test_elementwise_mul_op.py 8.0 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
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 32 33 34 35 36 37
        self.dtype = np.float32
        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 84 85
class TestElementwiseMulOp_scalar(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
86
            'X': np.random.rand(10, 3, 4).astype(np.float32),
87 88 89
            'Y': np.random.rand(1).astype(np.float32)
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
90
        self.init_kernel_type()
91 92


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


G
gongweibao 已提交
104
class TestElementwiseMulOp_broadcast_0(ElementwiseMulOp):
105
    def init_input_output(self):
106
        self.x = np.random.rand(2, 13, 4).astype(self.dtype)
107 108
        self.y = np.random.rand(2).astype(self.dtype)
        self.out = self.x * self.y.reshape(2, 1, 1)
109

110 111
    def init_axis(self):
        self.axis = 0
112 113


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

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


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

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


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

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


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


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


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


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


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


class TestElementwiseMulOp_xsize_lessthan_ysize(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
            'X': np.random.rand(4, 5).astype(np.float64),
            'Y': np.random.rand(2, 3, 4, 5).astype(np.float64)
        }

        self.attrs = {'axis': 2}

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


225
class TestElementwiseMulOpError(unittest.TestCase):
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    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)


242 243
if __name__ == '__main__':
    unittest.main()