test_math_op_patch.py 13.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
from decorator_helper import prog_scope
17
import paddle
18
import paddle.fluid as fluid
Y
Yang Yu 已提交
19
import numpy
20
import numpy as np
Y
Yang Yu 已提交
21 22 23


class TestMathOpPatches(unittest.TestCase):
24 25 26
    def setUp(self):
        paddle.enable_static()

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

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

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

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

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

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

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

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

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

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

148
    @prog_scope()
Y
Yang Yu 已提交
149 150 151 152 153 154
    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)
155 156
        a_np = np.random.random(size=[10, 1]).astype('float32')
        b_np = np.random.random(size=[10, 1]).astype('float32')
157 158 159 160 161
        (c_np,) = exe.run(
            fluid.default_main_program(),
            feed={"a": a_np, 'b': b_np},
            fetch_list=[c],
        )
162
        np.testing.assert_allclose(a_np * b_np, c_np, rtol=1e-05)
Y
Yang Yu 已提交
163

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

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

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

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

210 211 212 213
    @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')
214
        c = a == b
215 216 217

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

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

227
        np.testing.assert_array_equal(c_np, a_np == b_np)
228 229 230 231 232 233 234 235 236
        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')
237
        cond = one == zero
238 239 240 241
        c = fluid.layers.cond(cond, lambda: a + b, lambda: a - b)

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

250
        np.testing.assert_array_equal(c_np, a_np - b_np)
251

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

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

265 266 267 268 269 270
    @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)
271
        a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float64')
272

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

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

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

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

    @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

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

    @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

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

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

Y
Yang Yu 已提交
382 383 384

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