test_math_op_patch.py 17.2 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):
L
Leo Chen 已提交
26
    @classmethod
27
    def setUp(self):
L
Leo Chen 已提交
28
        np.random.seed(1024)
29 30
        paddle.enable_static()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @prog_scope()
    def test_equal_and_cond(self):
G
GGBond8488 已提交
236 237 238 239
        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)
240
        one = paddle.ones(shape=[1], dtype='int32')
241
        zero = paddle.zeros(shape=[1], dtype='int32')
242
        cond = one == zero
243
        c = paddle.static.nn.cond(cond, lambda: a + b, lambda: a - b)
244 245 246

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

255
        np.testing.assert_array_equal(c_np, a_np - b_np)
256

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

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

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

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

285 286 287 288 289 290 291 292 293 294
    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()
295 296 297 298 299
        out = exe.run(
            fluid.default_main_program(),
            feed={"x": x_np, "y": y_np},
            fetch_list=[z],
        )
300
        np.testing.assert_array_equal(out[0], out_np)
301 302 303 304 305 306 307 308 309 310 311 312

    @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()
313 314 315 316 317
        out = exe.run(
            fluid.default_main_program(),
            feed={"x": x_np, "y": y_np},
            fetch_list=[z],
        )
318
        np.testing.assert_array_equal(out[0], out_np)
319 320 321 322 323 324 325 326 327 328 329 330

    @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()
331 332 333 334 335
        out = exe.run(
            fluid.default_main_program(),
            feed={"x": x_np, "y": y_np},
            fetch_list=[z],
        )
336
        np.testing.assert_array_equal(out[0], out_np)
337 338 339 340 341 342 343 344 345

    @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

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

    @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

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

366 367 368 369 370 371 372 373 374 375 376
    @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')
377
        c = a @ b  # __matmul__
378 379
        a_np = np.random.uniform(-1, 1, size=[2, 3]).astype('float32')
        b_np = np.random.uniform(-1, 1, size=[3, 5]).astype('float32')
380 381
        place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)
382 383 384 385 386
        (c_np,) = exe.run(
            paddle.static.default_main_program(),
            feed={"a": a_np, "b": b_np},
            fetch_list=[c],
        )
387
        np.testing.assert_allclose(a_np @ b_np, c_np, rtol=1e-05)
388

Y
Yang Yu 已提交
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 490 491
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 已提交
492 493
if __name__ == '__main__':
    unittest.main()