test_math_op_patch.py 17.1 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2
#
Y
Yang Yu 已提交
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
6
#
Y
Yang Yu 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
Y
Yang Yu 已提交
9 10 11 12 13 14 15
# 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.

import unittest
16 17 18

import numpy
import numpy as np
19
from decorator_helper import prog_scope
20

21
import paddle
22
from paddle import fluid
Y
Yang Yu 已提交
23 24 25


class TestMathOpPatches(unittest.TestCase):
26 27 28
    def setUp(self):
        paddle.enable_static()

29
    @prog_scope()
Y
Yang Yu 已提交
30
    def test_add_scalar(self):
G
GGBond8488 已提交
31
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
32
        b = a + 10
33
        ab = paddle.concat([a, b], axis=1)
34 35 36
        c = ab + 10
        d = ab + a
        # e = a + ab
Y
Yang Yu 已提交
37 38
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
39
        a_np = np.random.random(size=[10, 1]).astype('float32')
40 41 42
        b_np, c_np, d_np = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b, c, d]
        )
43
        np.testing.assert_allclose(a_np + 10, b_np, rtol=1e-05)
44
        ab_np = np.concatenate([a_np, b_np], axis=1)
45
        np.testing.assert_allclose(ab_np + 10, c_np, rtol=1e-05)
46
        d_expected = ab_np + np.concatenate([a_np, a_np], axis=1)
47
        np.testing.assert_allclose(d_expected, d_np, rtol=1e-05)
Y
Yang Yu 已提交
48

49
    @prog_scope()
Y
Yang Yu 已提交
50
    def test_radd_scalar(self):
G
GGBond8488 已提交
51
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
52 53 54
        b = 10 + a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
55
        a_np = np.random.random(size=[10, 1]).astype('float32')
56
        (b_np,) = exe.run(
57 58
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
59
        np.testing.assert_allclose(a_np + 10, b_np, rtol=1e-05)
Y
Yang Yu 已提交
60

61
    @prog_scope()
Y
Yang Yu 已提交
62
    def test_sub_scalar(self):
G
GGBond8488 已提交
63
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
64 65 66
        b = a - 10
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
67
        a_np = np.random.random(size=[10, 1]).astype('float32')
68 69 70
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
71
        np.testing.assert_allclose(a_np - 10, b_np, rtol=1e-05)
Y
Yang Yu 已提交
72

73
    @prog_scope()
74
    def test_rsub_scalar(self):
G
GGBond8488 已提交
75
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
76 77 78
        b = 10 - a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
79
        a_np = np.random.random(size=[10, 1]).astype('float32')
80 81 82
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
83
        np.testing.assert_allclose(10 - a_np, b_np, rtol=1e-05)
Y
Yang Yu 已提交
84

85
    @prog_scope()
Y
Yang Yu 已提交
86
    def test_mul_scalar(self):
G
GGBond8488 已提交
87
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
88 89 90
        b = a * 10
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
91
        a_np = np.random.random(size=[10, 1]).astype('float32')
92 93 94
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
95
        np.testing.assert_allclose(a_np * 10, b_np, rtol=1e-05)
Y
Yang Yu 已提交
96

97
    @prog_scope()
Y
Yang Yu 已提交
98
    def test_rmul_scalar(self):
G
GGBond8488 已提交
99
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
100 101 102
        b = 10 * a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
103
        a_np = np.random.random(size=[10, 1]).astype('float32')
104 105 106
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
107
        np.testing.assert_allclose(10 * a_np, b_np, rtol=1e-05)
Y
Yang Yu 已提交
108

109
    @prog_scope()
Y
Yang Yu 已提交
110
    def test_div_scalar(self):
G
GGBond8488 已提交
111
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
112 113 114
        b = a / 10
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
115
        a_np = np.random.random(size=[10, 1]).astype('float32')
116 117 118
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
119
        np.testing.assert_allclose(a_np / 10, b_np, rtol=1e-05)
Y
Yang Yu 已提交
120

121
    @prog_scope()
Y
Yang Yu 已提交
122
    def test_rdiv_scalar(self):
G
GGBond8488 已提交
123
        a = paddle.static.data(name="a", shape=[-1, 1])
Y
Yang Yu 已提交
124 125 126
        b = 10 / a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
127
        a_np = np.random.random(size=[10, 1]).astype('float32') + 1e-2
Y
Yang Yu 已提交
128

129 130 131
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
132
        np.testing.assert_allclose(10 / a_np, b_np, rtol=1e-05)
Y
Yang Yu 已提交
133

134
    @prog_scope()
Y
Yang Yu 已提交
135
    def test_div_two_tensor(self):
G
GGBond8488 已提交
136 137
        a = paddle.static.data(name="a", shape=[-1, 1])
        b = paddle.static.data(name="b", shape=[-1, 1])
Y
Yang Yu 已提交
138 139 140
        c = a / b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
141 142
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32') + 1e-2
143 144 145 146 147
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, 'b': b_np},
            fetch_list=[c],
        )
148
        np.testing.assert_allclose(a_np / b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
149

150
    @prog_scope()
Y
Yang Yu 已提交
151
    def test_mul_two_tensor(self):
G
GGBond8488 已提交
152 153
        a = paddle.static.data(name="a", shape=[-1, 1])
        b = paddle.static.data(name="b", shape=[-1, 1])
Y
Yang Yu 已提交
154 155 156
        c = a * b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
157 158
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32')
159 160 161 162 163
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, 'b': b_np},
            fetch_list=[c],
        )
164
        np.testing.assert_allclose(a_np * b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
165

166
    @prog_scope()
Y
Yang Yu 已提交
167
    def test_add_two_tensor(self):
G
GGBond8488 已提交
168 169
        a = paddle.static.data(name="a", shape=[-1, 1])
        b = paddle.static.data(name="b", shape=[-1, 1])
Y
Yang Yu 已提交
170 171 172
        c = a + b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
173 174
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32')
175 176 177 178 179
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, 'b': b_np},
            fetch_list=[c],
        )
180
        np.testing.assert_allclose(a_np + b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
181

182
    @prog_scope()
Y
Yang Yu 已提交
183
    def test_sub_two_tensor(self):
G
GGBond8488 已提交
184 185
        a = paddle.static.data(name="a", shape=[-1, 1])
        b = paddle.static.data(name="b", shape=[-1, 1])
Y
Yang Yu 已提交
186 187 188
        c = a - b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
189 190
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32')
191 192 193 194 195
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, 'b': b_np},
            fetch_list=[c],
        )
196
        np.testing.assert_allclose(a_np - b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
197

198 199
    @prog_scope()
    def test_integer_div(self):
G
GGBond8488 已提交
200
        a = paddle.static.data(name="a", shape=[-1, 1], dtype='int64')
S
ShenLiang 已提交
201
        b = a / 7
202 203
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
204
        a_np = np.array([3, 4, 10, 14, 9, 18]).astype('int64')
205 206 207
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
S
ShenLiang 已提交
208

209
        b_np_actual = (a_np / 7).astype('float32')
210
        np.testing.assert_allclose(b_np, b_np_actual, rtol=1e-05)
211

212 213
    @prog_scope()
    def test_equal(self):
G
GGBond8488 已提交
214 215
        a = paddle.static.data(name="a", shape=[-1, 1], dtype='float32')
        b = paddle.static.data(name="b", shape=[-1, 1], dtype='float32')
216
        c = a == b
217 218 219

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
220 221
        a_np = np.array([3, 4, 10, 14, 9, 18]).astype('float32')
        b_np = np.array([3, 4, 11, 15, 8, 18]).astype('float32')
222

223 224 225 226 227
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, "b": b_np},
            fetch_list=[c],
        )
228

229
        np.testing.assert_array_equal(c_np, a_np == b_np)
230 231 232 233
        self.assertEqual(c.dtype, fluid.core.VarDesc.VarType.BOOL)

    @prog_scope()
    def test_equal_and_cond(self):
G
GGBond8488 已提交
234 235 236 237
        a = paddle.static.data(name="a", shape=[-1, 1], dtype='float32')
        a.desc.set_need_check_feed(False)
        b = paddle.static.data(name="b", shape=[-1, 1], dtype='float32')
        b.desc.set_need_check_feed(False)
238
        one = paddle.ones(shape=[1], dtype='int32')
239
        zero = paddle.zeros(shape=[1], dtype='int32')
240
        cond = one == zero
241
        c = paddle.static.nn.cond(cond, lambda: a + b, lambda: a - b)
242 243 244

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
G
GGBond8488 已提交
245 246
        a_np = np.array([3, 4, 10, 14, 9, 18]).astype('float32')
        b_np = np.array([3, 4, 11, 15, 8, 18]).astype('float32')
247 248 249 250 251
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, "b": b_np},
            fetch_list=[c],
        )
252

253
        np.testing.assert_array_equal(c_np, a_np - b_np)
254

255 256
    @prog_scope()
    def test_neg(self):
G
GGBond8488 已提交
257 258
        a = paddle.static.data(name="a", shape=[-1, 10, 1], dtype='float32')
        a.desc.set_need_check_feed(False)
259 260 261
        b = -a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
262
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float32')
263

264 265 266
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
267
        np.testing.assert_allclose(-a_np, b_np, rtol=1e-05)
268

269 270
    @prog_scope()
    def test_astype(self):
G
GGBond8488 已提交
271 272
        a = paddle.static.data(name="a", shape=[-1, 10, 1])
        a.desc.set_need_check_feed(False)
273 274 275
        b = a.astype('float32')
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
276
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float64')
277

278 279 280
        (b_np,) = exe.run(
            fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]
        )
281
        np.testing.assert_allclose(a_np.astype('float32'), b_np, rtol=1e-05)
282

283 284 285 286 287 288 289 290 291 292
    def test_bitwise_and(self):
        x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        out_np = x_np & y_np

        x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32")
        y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32")
        z = x & y

        exe = fluid.Executor()
293 294 295 296 297
        out = exe.run(
            fluid.default_main_program(),
            feed={"x": x_np, "y": y_np},
            fetch_list=[z],
        )
298
        np.testing.assert_array_equal(out[0], out_np)
299 300 301 302 303 304 305 306 307 308 309 310

    @prog_scope()
    def test_bitwise_or(self):
        x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        out_np = x_np | y_np

        x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32")
        y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32")
        z = x | y

        exe = fluid.Executor()
311 312 313 314 315
        out = exe.run(
            fluid.default_main_program(),
            feed={"x": x_np, "y": y_np},
            fetch_list=[z],
        )
316
        np.testing.assert_array_equal(out[0], out_np)
317 318 319 320 321 322 323 324 325 326 327 328

    @prog_scope()
    def test_bitwise_xor(self):
        x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        y_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        out_np = x_np ^ y_np

        x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32")
        y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32")
        z = x ^ y

        exe = fluid.Executor()
329 330 331 332 333
        out = exe.run(
            fluid.default_main_program(),
            feed={"x": x_np, "y": y_np},
            fetch_list=[z],
        )
334
        np.testing.assert_array_equal(out[0], out_np)
335 336 337 338 339 340 341 342 343

    @prog_scope()
    def test_bitwise_not(self):
        x_np = np.random.randint(-100, 100, [2, 3, 5]).astype("int32")
        out_np = ~x_np

        x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32")
        z = ~x

344
        exe = fluid.Executor()
345 346 347
        out = exe.run(
            fluid.default_main_program(), feed={"x": x_np}, fetch_list=[z]
        )
348
        np.testing.assert_array_equal(out[0], out_np)
349 350 351 352 353 354 355 356 357

    @prog_scope()
    def test_T(self):
        x_np = np.random.randint(-100, 100, [2, 8, 5, 3]).astype("int32")
        out_np = x_np.T

        x = paddle.static.data(name="x", shape=[2, 8, 5, 3], dtype="int32")
        z = x.T

358
        exe = fluid.Executor()
359 360 361
        out = exe.run(
            fluid.default_main_program(), feed={"x": x_np}, fetch_list=[z]
        )
362
        np.testing.assert_array_equal(out[0], out_np)
363

364 365 366 367 368 369 370 371 372 373 374
    @prog_scope()
    def test_ndim(self):
        a = paddle.static.data(name="a", shape=[10, 1])
        self.assertEqual(a.dim(), 2)
        self.assertEqual(a.ndimension(), 2)
        self.assertEqual(a.ndim, 2)

    @prog_scope()
    def test_matmul(self):
        a = paddle.static.data(name='a', shape=[2, 3], dtype='float32')
        b = paddle.static.data(name='b', shape=[3, 5], dtype='float32')
375
        c = a @ b  # __matmul__
376 377
        a_np = np.random.uniform(-1, 1, size=[2, 3]).astype('float32')
        b_np = np.random.uniform(-1, 1, size=[3, 5]).astype('float32')
378 379
        place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)
380 381 382 383 384
        (c_np,) = exe.run(
            paddle.static.default_main_program(),
            feed={"a": a_np, "b": b_np},
            fetch_list=[c],
        )
385
        np.testing.assert_allclose(a_np @ b_np, c_np, rtol=1e-05)
386

Y
Yang Yu 已提交
387

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
class TestDygraphMathOpPatches(unittest.TestCase):
    def init_data(self):
        self.np_a = np.random.random((2, 3, 4)).astype(np.float32)
        self.np_b = np.random.random((2, 3, 4)).astype(np.float32)
        self.np_a[np.abs(self.np_a) < 0.0005] = 0.002
        self.np_b[np.abs(self.np_b) < 0.0005] = 0.002

        self.tensor_a = paddle.to_tensor(self.np_a, dtype="float32")
        self.tensor_b = paddle.to_tensor(self.np_b, dtype="float32")

    def test_dygraph_greater_than(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor > nparray
        expect_out = self.np_a > self.np_b
        actual_out = self.tensor_a > self.np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()

    def test_dygraph_greater_equal(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor >= nparray
        expect_out = self.np_a >= self.np_b
        actual_out = self.tensor_a >= self.np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()

    def test_dygraph_reminder(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor % nparray
        expect_out = self.np_a % self.np_b
        actual_out = self.tensor_a % self.np_b
        np.testing.assert_allclose(actual_out, expect_out, rtol=1e-7, atol=1e-7)
        paddle.enable_static()

    def test_dygraph_less_than(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor < nparray
        expect_out = self.np_a < self.np_b
        actual_out = self.tensor_a < self.np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()

    def test_dygraph_less_equal(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor <= nparray
        expect_out = self.np_a <= self.np_b
        actual_out = self.tensor_a <= self.np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()

    def test_dygraph_floor_divide(self):
        paddle.disable_static()
        np_a = np.random.random((2, 3, 4)).astype(np.int32)
        np_b = np.random.random((2, 3, 4)).astype(np.int32)
        np_b[np.abs(np_b) < 1] = 2
        # normal case: tenor // nparray
        tensor_a = paddle.to_tensor(np_a, dtype="int32")
        tensor_b = paddle.to_tensor(np_b, dtype="int32")
        expect_out = np_a // np_b
        actual_out = tensor_a // np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()

    def test_dygraph_elementwise_pow(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor ** nparray
        expect_out = self.np_a**self.np_b
        actual_out = self.tensor_a**self.np_b
        np.testing.assert_allclose(actual_out, expect_out, rtol=1e-7, atol=1e-7)

        # normal case: nparray ** tensor
        expect_out = self.np_a**self.np_b
        actual_out = self.np_a**self.tensor_b
        np.testing.assert_allclose(actual_out, expect_out, rtol=1e-7, atol=1e-7)

        paddle.enable_static()

    def test_dygraph_not_equal(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor != nparray
        expect_out = self.np_a != self.np_b
        actual_out = self.tensor_a != self.np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()

    def test_dygraph_equal(self):
        paddle.disable_static()
        self.init_data()
        # normal case: tenor == nparray
        expect_out = self.np_a == self.np_b
        actual_out = self.tensor_a == self.np_b
        np.testing.assert_equal(actual_out, expect_out)
        paddle.enable_static()


Y
Yang Yu 已提交
490 491
if __name__ == '__main__':
    unittest.main()