test_elementwise_add_op.py 6.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.
G
gongweibao 已提交
14 15
import unittest
import numpy as np
K
Kexin Zhao 已提交
16
import paddle.fluid.core as core
G
gongweibao 已提交
17 18 19
from op_test import OpTest


K
Kexin Zhao 已提交
20
class TestElementwiseAddOp(OpTest):
G
gongweibao 已提交
21 22
    def setUp(self):
        self.op_type = "elementwise_add"
K
Kexin Zhao 已提交
23 24 25 26 27
        self.dtype = np.float32
        init_dtype()

        x = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
        y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype)
G
gongweibao 已提交
28
        self.inputs = {
K
Kexin Zhao 已提交
29 30
            'X': OpTest.np_dtype_to_fluid_dtype(x),
            'Y': OpTest.np_dtype_to_fluid_dtype(y)
G
gongweibao 已提交
31
        }
K
Kexin Zhao 已提交
32
        self.outputs = {'Out': np.add(x, y)}
G
gongweibao 已提交
33 34 35 36 37

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
K
Kexin Zhao 已提交
38 39
        if self.dtype == np.float16:
            return
G
gongweibao 已提交
40 41 42
        self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.005)

    def test_check_grad_ingore_x(self):
K
Kexin Zhao 已提交
43 44
        if self.dtype == np.float16:
            return
G
gongweibao 已提交
45 46 47 48
        self.check_grad(
            ['Y'], 'Out', max_relative_error=0.005, no_grad_set=set("X"))

    def test_check_grad_ingore_y(self):
K
Kexin Zhao 已提交
49 50
        if self.dtype == np.float16:
            return
G
gongweibao 已提交
51 52 53
        self.check_grad(
            ['X'], 'Out', max_relative_error=0.005, no_grad_set=set('Y'))

K
Kexin Zhao 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67
    def init_dtype():
        pass


class TestFP16ElementwiseAddOp(TestElementwiseAddOp):
    def init_dtype():
        self.dtype = np.float16

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            place = core.CUDAPlace(0)
            if core.is_float16_supported(place):
                self.check_output_with_place(place, atol=1e-3)

G
gongweibao 已提交
68

K
Kexin Zhao 已提交
69
class TestElementwiseAddOp_scalar(TestElementwiseAddOp):
70 71 72 73 74 75 76 77 78
    def setUp(self):
        self.op_type = "elementwise_add"
        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']}


K
Kexin Zhao 已提交
79
class TestElementwiseAddOp_scalar2(TestElementwiseAddOp):
80 81 82 83 84 85 86 87 88
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 3, 4).astype(np.float32),
            'Y': np.random.rand(1, 1).astype(np.float32)
        }
        self.outputs = {'Out': self.inputs['X'] + self.inputs['Y']}


K
Kexin Zhao 已提交
89
class TestElementwiseAddOp_Vector(TestElementwiseAddOp):
G
gongweibao 已提交
90 91 92 93 94 95 96 97 98
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.random((32, )).astype("float32"),
            'Y': np.random.random((32, )).astype("float32")
        }
        self.outputs = {'Out': np.add(self.inputs['X'], self.inputs['Y'])}


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

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


K
Kexin Zhao 已提交
113
class TestElementwiseAddOp_broadcast_1(TestElementwiseAddOp):
G
gongweibao 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 3, 4).astype(np.float32),
            'Y': np.random.rand(3).astype(np.float32)
        }

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


K
Kexin Zhao 已提交
127
class TestElementwiseAddOp_broadcast_2(TestElementwiseAddOp):
G
gongweibao 已提交
128 129 130 131 132 133 134 135 136 137 138 139
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 3, 4).astype(np.float32),
            'Y': np.random.rand(4).astype(np.float32)
        }

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


K
Kexin Zhao 已提交
140
class TestElementwiseAddOp_broadcast_3(TestElementwiseAddOp):
G
gongweibao 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 3, 4, 5).astype(np.float32),
            'Y': np.random.rand(3, 4).astype(np.float32)
        }

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


K
Kexin Zhao 已提交
154
class TestElementwiseAddOp_broadcast_4(TestElementwiseAddOp):
155 156 157 158 159 160 161 162 163 164 165 166 167
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 3, 4, 5).astype(np.float32),
            'Y': np.random.rand(2, 1).astype(np.float32)
        }

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


K
Kexin Zhao 已提交
168
class TestElementwiseAddOp_rowwise_add_0(TestElementwiseAddOp):
Q
qijun 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 3, 4).astype(np.float32),
            'Y': np.random.rand(3, 4).astype(np.float32)
        }

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


K
Kexin Zhao 已提交
182
class TestElementwiseAddOp_rowwise_add_1(TestElementwiseAddOp):
Q
qijun 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195
    def setUp(self):
        self.op_type = "elementwise_add"
        self.inputs = {
            'X': np.random.rand(2, 1).astype(np.float32),
            'Y': np.random.rand(1).astype(np.float32)
        }

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


G
gongweibao 已提交
196 197
if __name__ == '__main__':
    unittest.main()