test_elementwise_mul_op.py 7.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 46 47 48

    def test_check_output(self):
        self.check_output()

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

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

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

57 58 59 60 61 62 63 64 65 66 67
    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

68

69 70 71 72 73 74 75 76 77 78
class TestElementwiseMulOp_scalar(ElementwiseMulOp):
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
            'X': np.random.rand(2, 3, 4).astype(np.float32),
            'Y': np.random.rand(1).astype(np.float32)
        }
        self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}


G
gongweibao 已提交
79
class TestElementwiseMulOp_Vector(ElementwiseMulOp):
80 81 82
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
83 84
            'X': np.random.random((32, )).astype("float64"),
            'Y': np.random.random((32, )).astype("float64")
85 86 87 88
        }
        self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])}


G
gongweibao 已提交
89
class TestElementwiseMulOp_broadcast_0(ElementwiseMulOp):
90 91 92 93
    def init_input_output(self):
        self.x = np.random.rand(2, 3, 4).astype(self.dtype)
        self.y = np.random.rand(2).astype(self.dtype)
        self.out = self.x * self.y.reshape(2, 1, 1)
94

95 96
    def init_axis(self):
        self.axis = 0
97 98


G
gongweibao 已提交
99
class TestElementwiseMulOp_broadcast_1(ElementwiseMulOp):
100 101 102
    def setUp(self):
        self.op_type = "elementwise_mul"
        self.inputs = {
103 104
            'X': np.random.rand(2, 3, 4).astype(np.float64),
            'Y': np.random.rand(3).astype(np.float64)
105 106 107 108 109 110 111 112
        }

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


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

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


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

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


140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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']}


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']}


W
Wu Yi 已提交
160 161 162 163 164
class TestElementwiseMulOpFp16(ElementwiseMulOp):
    def init_dtype(self):
        self.dtype = np.float16


165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
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']}


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']}


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']
        }


200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
class TestElementwiseMulOpError(OpTest):
    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)


217 218
if __name__ == '__main__':
    unittest.main()