test_math_op_patch.py 14.3 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
# 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.

15
from __future__ import print_function, division
16

Y
Yang Yu 已提交
17
import unittest
18
from decorator_helper import prog_scope
19
import paddle
20
import paddle.fluid as fluid
Y
Yang Yu 已提交
21
import numpy
22
import numpy as np
Y
Yang Yu 已提交
23 24 25


class TestMathOpPatches(unittest.TestCase):
26

27 28 29
    def setUp(self):
        paddle.enable_static()

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

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

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

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

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

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

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

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

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

135
    @prog_scope()
Y
Yang Yu 已提交
136 137 138 139 140 141
    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)
142 143
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32') + 1e-2
144 145 146 147 148 149 150
        c_np, = exe.run(fluid.default_main_program(),
                        feed={
                            "a": a_np,
                            'b': b_np
                        },
                        fetch_list=[c])
        np.testing.assert_allclose(a_np / b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
151

152
    @prog_scope()
Y
Yang Yu 已提交
153 154 155 156 157 158
    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)
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 166 167
        c_np, = exe.run(fluid.default_main_program(),
                        feed={
                            "a": a_np,
                            'b': b_np
                        },
                        fetch_list=[c])
        np.testing.assert_allclose(a_np * b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
168

169
    @prog_scope()
Y
Yang Yu 已提交
170 171 172 173 174 175
    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)
176 177
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32')
178 179 180 181 182 183 184
        c_np, = exe.run(fluid.default_main_program(),
                        feed={
                            "a": a_np,
                            'b': b_np
                        },
                        fetch_list=[c])
        np.testing.assert_allclose(a_np + b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
185

186
    @prog_scope()
Y
Yang Yu 已提交
187 188 189 190 191 192
    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)
193 194
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32')
195 196 197 198 199 200 201
        c_np, = exe.run(fluid.default_main_program(),
                        feed={
                            "a": a_np,
                            'b': b_np
                        },
                        fetch_list=[c])
        np.testing.assert_allclose(a_np - b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
202

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

214
        b_np_actual = (a_np / 7).astype('float32')
215
        np.testing.assert_allclose(b_np, b_np_actual, rtol=1e-05)
216

217 218 219 220 221 222 223 224
    @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')
        c = (a == b)

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

        c_np, = exe.run(fluid.default_main_program(),
229 230 231 232
                        feed={
                            "a": a_np,
                            "b": b_np
                        },
233 234
                        fetch_list=[c])

235
        np.testing.assert_array_equal(c_np, a_np == b_np)
236 237 238 239 240 241 242 243 244 245 246 247 248 249
        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')
        cond = (one == zero)
        c = fluid.layers.cond(cond, lambda: a + b, lambda: a - b)

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
250 251
        a_np = np.array([3, 4, 10, 14, 9, 18]).astype('float')
        b_np = np.array([3, 4, 11, 15, 8, 18]).astype('float')
252
        c_np, = exe.run(fluid.default_main_program(),
253 254 255 256
                        feed={
                            "a": a_np,
                            "b": b_np
                        },
257 258
                        fetch_list=[c])

259
        np.testing.assert_array_equal(c_np, a_np - b_np)
260

261 262 263 264 265 266
    @prog_scope()
    def test_neg(self):
        a = fluid.layers.data(name="a", shape=[10, 1])
        b = -a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
267
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float32')
268

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

274 275 276 277 278 279
    @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)
280
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float64')
281

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

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

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

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

    @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

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

    @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

365 366 367 368
        exe = fluid.Executor()
        out = exe.run(fluid.default_main_program(),
                      feed={"x": x_np},
                      fetch_list=[z])
369
        np.testing.assert_array_equal(out[0], out_np)
370

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

Y
Yang Yu 已提交
395 396 397

if __name__ == '__main__':
    unittest.main()