test_math_op_patch.py 16.9 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
import paddle.fluid as 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 31 32
    def test_add_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = a + 10
33 34 35 36
        ab = fluid.layers.concat(input=[a, b], axis=1)
        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 51 52 53 54
    def test_radd_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = 10 + a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
55
        a_np = np.random.random(size=[10, 1]).astype('float32')
56 57 58
        b_np = exe.run(
            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 63 64 65 66
    def test_sub_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        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()
Y
Yang Yu 已提交
74 75 76 77 78
    def test_radd_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        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 87 88 89 90
    def test_mul_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        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 99 100 101 102
    def test_rmul_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        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 111 112 113 114
    def test_div_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        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 123 124 125 126
    def test_rdiv_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        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 136 137 138 139 140
    def test_div_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        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 152 153 154 155 156
    def test_mul_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        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 168 169 170 171 172
    def test_add_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        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 184 185 186 187 188
    def test_sub_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        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 200
    @prog_scope()
    def test_integer_div(self):
        a = fluid.layers.data(name="a", shape=[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 214 215
    @prog_scope()
    def test_equal(self):
        a = fluid.layers.data(name="a", shape=[1], dtype='float32')
        b = fluid.layers.data(name="b", shape=[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 234 235 236 237 238
        self.assertEqual(c.dtype, fluid.core.VarDesc.VarType.BOOL)

    @prog_scope()
    def test_equal_and_cond(self):
        a = fluid.layers.data(name="a", shape=[1], dtype='float32')
        b = fluid.layers.data(name="b", shape=[1], dtype='float32')

        one = fluid.layers.ones(shape=[1], dtype='int32')
        zero = fluid.layers.zeros(shape=[1], dtype='int32')
239
        cond = one == zero
240 241 242 243
        c = fluid.layers.cond(cond, lambda: a + b, lambda: a - b)

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

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

254 255 256 257 258 259
    @prog_scope()
    def test_neg(self):
        a = fluid.layers.data(name="a", shape=[10, 1])
        b = -a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
260
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float32')
261

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

267 268 269 270 271 272
    @prog_scope()
    def test_astype(self):
        a = fluid.layers.data(name="a", shape=[10, 1])
        b = a.astype('float32')
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
273
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float64')
274

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

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

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

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

    @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

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

    @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

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

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

Y
Yang Yu 已提交
384

385 386 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
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 已提交
487 488
if __name__ == '__main__':
    unittest.main()