test_elementwise_pow_op.py 9.7 KB
Newer Older
1
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Q
Qiao Longfei 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# 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

Q
Qiao Longfei 已提交
15
import unittest
16

Q
Qiao Longfei 已提交
17
import numpy as np
W
wanghuancoder 已提交
18
from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci
19

20
import paddle
21
from paddle import fluid
Q
Qiao Longfei 已提交
22 23


24 25 26 27 28 29
def pow_grad(x, y, dout):
    dx = dout * y * np.power(x, (y - 1))
    dy = dout * np.log(x) * np.power(x, y)
    return dx, dy


Q
Qiao Longfei 已提交
30 31 32
class TestElementwisePowOp(OpTest):
    def setUp(self):
        self.op_type = "elementwise_pow"
33
        self.python_api = paddle.pow
34
        self.public_python_api = paddle.pow
35
        self.prim_op_type = "prim"
Q
Qiao Longfei 已提交
36
        self.inputs = {
37
            'X': np.random.uniform(1, 2, [20, 5]).astype("float64"),
38
            'Y': np.random.uniform(1, 2, [20, 5]).astype("float64"),
Q
Qiao Longfei 已提交
39 40 41 42
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}

    def test_check_output(self):
43
        if hasattr(self, 'attrs'):
W
wanghuancoder 已提交
44
            self.check_output(check_dygraph=False)
45
        else:
W
wanghuancoder 已提交
46
            self.check_output()
Q
Qiao Longfei 已提交
47

48
    def test_check_grad_normal(self):
49
        if hasattr(self, 'attrs'):
50
            self.check_grad(
W
wanghuancoder 已提交
51
                ['X', 'Y'], 'Out', check_prim=True, check_dygraph=False
52
            )
53
        else:
W
wanghuancoder 已提交
54
            self.check_grad(['X', 'Y'], 'Out', check_prim=True)
55

Q
Qiao Longfei 已提交
56

57 58 59 60
class TestElementwisePowOp_ZeroDim1(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
        self.python_api = paddle.pow
61
        self.public_python_api = paddle.pow
62 63 64
        self.enable_cinn = False
        self.prim_op_type = "prim"

65 66 67 68 69 70 71 72 73 74 75
        self.inputs = {
            'X': np.random.uniform(1, 2, []).astype("float64"),
            'Y': np.random.uniform(1, 2, []).astype("float64"),
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


class TestElementwisePowOp_ZeroDim2(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
        self.python_api = paddle.pow
76
        self.public_python_api = paddle.pow
77 78 79
        self.enable_cinn = False
        self.prim_op_type = "prim"

80 81 82 83 84 85 86 87 88 89 90
        self.inputs = {
            'X': np.random.uniform(1, 2, [20, 5]).astype("float64"),
            'Y': np.random.uniform(1, 2, []).astype("float64"),
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


class TestElementwisePowOp_ZeroDim3(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
        self.python_api = paddle.pow
91
        self.public_python_api = paddle.pow
92 93 94
        self.enable_cinn = False
        self.prim_op_type = "prim"

95 96 97 98 99 100 101
        self.inputs = {
            'X': np.random.uniform(1, 2, []).astype("float64"),
            'Y': np.random.uniform(1, 2, [20, 5]).astype("float64"),
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


102 103 104
class TestElementwisePowOp_big_shape_1(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
105
        self.python_api = paddle.pow
106
        self.public_python_api = paddle.pow
107 108
        self.prim_op_type = "prim"

109
        self.inputs = {
110
            'X': np.random.uniform(1, 2, [10, 10]).astype("float64"),
111
            'Y': np.random.uniform(0.1, 1, [10, 10]).astype("float64"),
112 113 114 115 116 117 118
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


class TestElementwisePowOp_big_shape_2(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
119
        self.python_api = paddle.pow
120
        self.public_python_api = paddle.pow
121 122
        self.prim_op_type = "prim"

123
        self.inputs = {
124
            'X': np.random.uniform(1, 2, [10, 10]).astype("float64"),
125
            'Y': np.random.uniform(0.2, 2, [10, 10]).astype("float64"),
126 127 128 129
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


130
@skip_check_grad_ci(
131 132
    reason="[skip shape check] Use y_shape(1) to test broadcast."
)
Q
Qiao Longfei 已提交
133 134 135
class TestElementwisePowOp_scalar(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
136
        self.python_api = paddle.pow
137
        self.public_python_api = paddle.pow
138 139
        self.prim_op_type = "prim"

Q
Qiao Longfei 已提交
140
        self.inputs = {
141
            'X': np.random.uniform(0.1, 1, [3, 3, 4]).astype(np.float64),
142
            'Y': np.random.uniform(0.1, 1, [1]).astype(np.float64),
143 144 145 146 147 148 149
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


class TestElementwisePowOp_tensor(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
150
        self.python_api = paddle.pow
151
        self.public_python_api = paddle.pow
152 153
        self.prim_op_type = "prim"

154
        self.inputs = {
155
            'X': np.random.uniform(0.1, 1, [100]).astype("float64"),
156
            'Y': np.random.uniform(1, 3, [100]).astype("float64"),
157 158 159 160 161 162 163
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


class TestElementwisePowOp_broadcast_0(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
164
        self.python_api = paddle.pow
165
        self.public_python_api = paddle.pow
166 167
        self.prim_op_type = "prim"

168
        self.inputs = {
169
            'X': np.random.uniform(0.1, 1, [2, 1, 100]).astype("float64"),
170
            'Y': np.random.uniform(0.1, 1, [100]).astype("float64"),
Q
Qiao Longfei 已提交
171 172 173 174
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


175 176 177
class TestElementwisePowOp_broadcast_4(TestElementwisePowOp):
    def setUp(self):
        self.op_type = "elementwise_pow"
178
        self.python_api = paddle.pow
179
        self.public_python_api = paddle.pow
180 181
        self.prim_op_type = "prim"

182
        self.inputs = {
183
            'X': np.random.uniform(0.1, 1, [2, 10, 3, 5]).astype("float64"),
184
            'Y': np.random.uniform(0.1, 1, [2, 10, 1, 5]).astype("float64"),
185 186 187 188
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}


189 190 191
class TestElementwisePowOpInt(OpTest):
    def setUp(self):
        self.op_type = "elementwise_pow"
192
        self.python_api = paddle.pow
193

194 195 196 197
        self.inputs = {'X': np.asarray([1, 3, 6]), 'Y': np.asarray([1, 1, 1])}
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}

    def test_check_output(self):
198
        if hasattr(self, 'attrs'):
W
wanghuancoder 已提交
199
            self.check_output(check_dygraph=False)
200
        else:
W
wanghuancoder 已提交
201
            self.check_output()
202 203 204 205 206 207 208


class TestElementwisePowGradOpInt(unittest.TestCase):
    def setUp(self):
        self.x = np.asarray([1, 3, 6])
        self.y = np.asarray([1, 1, 1])
        self.res = self.x**self.y
209

210 211 212
        # dout = 1
        self.grad_res = np.asarray([1, 1, 1])
        # dx = dout * y * pow(x, y-1)
213 214 215
        self.grad_x = (
            self.grad_res * self.y * (self.x ** (self.y - 1)).astype("int")
        )
216
        # dy = dout * log(x) * pow(x, y)
217 218 219
        self.grad_y = (
            self.grad_res * np.log(self.x) * (self.x**self.y)
        ).astype("int")
220 221 222 223 224 225 226 227 228 229 230 231

    def test_grad(self):
        places = [fluid.CPUPlace()]
        if fluid.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            with fluid.dygraph.guard(place):
                x = fluid.dygraph.to_variable(self.x, zero_copy=False)
                y = fluid.dygraph.to_variable(self.y, zero_copy=False)
                x.stop_gradient = False
                y.stop_gradient = False
                res = x**y
232
                res.retain_grads()
233
                res.backward()
234 235 236
                np.testing.assert_array_equal(res.gradient(), self.grad_res)
                np.testing.assert_array_equal(x.gradient(), self.grad_x)
                np.testing.assert_array_equal(y.gradient(), self.grad_y)
237 238


239 240 241
class TestElementwisePowOpFP16(OpTest):
    def setUp(self):
        self.op_type = "elementwise_pow"
242
        self.dtype = np.float16
243
        self.python_api = paddle.pow
244
        self.public_python_api = paddle.pow
245 246
        self.prim_op_type = "prim"

247 248
        self.inputs = {
            'X': np.random.uniform(1, 2, [20, 5]).astype("float16"),
249
            'Y': np.random.uniform(1, 2, [20, 5]).astype("float16"),
250 251 252 253 254
        }
        self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])}

    def test_check_output(self):
        if hasattr(self, 'attrs'):
W
wanghuancoder 已提交
255
            self.check_output(check_dygraph=False)
256
        else:
W
wanghuancoder 已提交
257
            self.check_output()
258 259

    def test_check_grad(self):
260 261 262 263 264 265
        self.check_grad(
            ['X', 'Y'],
            'Out',
            user_defined_grads=pow_grad(
                self.inputs['X'], self.inputs['Y'], 1 / self.inputs['X'].size
            ),
266
            check_prim=True,
267
        )
268 269


270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
class TestElementwisePowBF16Op(OpTest):
    def setUp(self):
        self.op_type = "elementwise_pow"
        self.dtype = np.uint16
        self.python_api = paddle.pow

        x = np.random.uniform(0, 1, [20, 5]).astype(np.float32)
        y = np.random.uniform(0, 1, [20, 5]).astype(np.float32)
        out = np.power(x, y)
        self.inputs = {
            'X': convert_float_to_uint16(x),
            'Y': convert_float_to_uint16(y),
        }
        self.outputs = {'Out': convert_float_to_uint16(out)}

    def test_check_output(self):
        if hasattr(self, 'attrs'):
W
wanghuancoder 已提交
287
            self.check_output()
288
        else:
W
wanghuancoder 已提交
289
            self.check_output()
290 291

    def test_check_grad(self):
W
wanghuancoder 已提交
292
        self.check_grad(['X', 'Y'], 'Out')
293 294


Q
Qiao Longfei 已提交
295 296
if __name__ == '__main__':
    unittest.main()