test_math_op_patch_var_base.py 24.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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
import inspect
16
import unittest
17 18 19

import numpy as np

20
import paddle
21
from paddle import fluid
22 23 24 25


class TestMathOpPatchesVarBase(unittest.TestCase):
    def setUp(self):
26
        self.shape = [10, 1024]
27 28
        self.dtype = np.float32

姜永久 已提交
29
    def test_add(self):
30 31 32 33 34 35
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a + b
36
            np.testing.assert_array_equal(res.numpy(), a_np + b_np)
37

姜永久 已提交
38
    def test_sub(self):
39 40 41 42 43 44
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a - b
45
            np.testing.assert_array_equal(res.numpy(), a_np - b_np)
46

姜永久 已提交
47
    def test_mul(self):
48 49 50 51 52 53
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a * b
54
            np.testing.assert_array_equal(res.numpy(), a_np * b_np)
55

姜永久 已提交
56
    def test_div(self):
57 58 59 60 61 62
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a / b
63
            # NOTE: Not sure why array_equal fails on windows, allclose is acceptable
64
            np.testing.assert_allclose(res.numpy(), a_np / b_np, rtol=1e-05)
65

姜永久 已提交
66
    def test_add_scalar(self):
67 68 69 70 71
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = 0.1
            res = a + b
72
            np.testing.assert_array_equal(res.numpy(), a_np + b)
73

姜永久 已提交
74
    def test_add_scalar_reverse(self):
75 76 77 78 79
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = 0.1
            res = b + a
80
            np.testing.assert_array_equal(res.numpy(), b + a_np)
81

姜永久 已提交
82
    def test_sub_scalar(self):
83 84 85 86 87
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = 0.1
            res = a - b
88
            np.testing.assert_array_equal(res.numpy(), a_np - b)
89

姜永久 已提交
90
    def test_sub_scalar_reverse(self):
91 92 93 94 95
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = 0.1
            res = b - a
96
            np.testing.assert_array_equal(res.numpy(), b - a_np)
97

姜永久 已提交
98
    def test_mul_scalar(self):
99 100 101 102 103
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = 0.1
            res = a * b
104
            np.testing.assert_array_equal(res.numpy(), a_np * b)
105 106

    # div_scalar, not equal
姜永久 已提交
107
    def test_div_scalar(self):
108 109 110 111 112
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = 0.1
            res = a / b
113
            np.testing.assert_allclose(res.numpy(), a_np / b, rtol=1e-05)
114 115

    # pow of float type, not equal
姜永久 已提交
116
    def test_pow(self):
117 118 119 120 121 122
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a**b
123
            np.testing.assert_allclose(res.numpy(), a_np**b_np, rtol=1e-05)
124

姜永久 已提交
125
    def test_floor_div(self):
126 127 128 129 130 131
        a_np = np.random.randint(1, 100, size=self.shape)
        b_np = np.random.randint(1, 100, size=self.shape)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a // b
132
            np.testing.assert_array_equal(res.numpy(), a_np // b_np)
133

姜永久 已提交
134
    def test_mod(self):
135 136 137 138 139 140
        a_np = np.random.randint(1, 100, size=self.shape)
        b_np = np.random.randint(1, 100, size=self.shape)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a % b
141
            np.testing.assert_array_equal(res.numpy(), a_np % b_np)
142

143
    # for bitwise and/or/xor/not
姜永久 已提交
144
    def test_bitwise(self):
145 146 147 148 149 150 151 152 153
        paddle.disable_static()

        x_np = np.random.randint(-100, 100, [2, 3, 5])
        y_np = np.random.randint(-100, 100, [2, 3, 5])
        x = paddle.to_tensor(x_np)
        y = paddle.to_tensor(y_np)

        out_np = x_np & y_np
        out = x & y
154
        np.testing.assert_array_equal(out.numpy(), out_np)
155 156 157

        out_np = x_np | y_np
        out = x | y
158
        np.testing.assert_array_equal(out.numpy(), out_np)
159 160 161

        out_np = x_np ^ y_np
        out = x ^ y
162
        np.testing.assert_array_equal(out.numpy(), out_np)
163 164 165

        out_np = ~x_np
        out = ~x
166
        np.testing.assert_array_equal(out.numpy(), out_np)
167

168
    # for logical compare
姜永久 已提交
169
    def test_equal(self):
170 171 172 173 174 175 176
        a_np = np.asarray([1, 2, 3, 4, 5])
        b_np = np.asarray([1, 2, 3, 4, 5])
        c_np = np.asarray([1, 2, 2, 4, 5])
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            c = fluid.dygraph.to_variable(c_np)
177 178
            res1 = a == b
            res2 = a == c
179 180
            np.testing.assert_array_equal(res1.numpy(), a_np == b_np)
            np.testing.assert_array_equal(res2.numpy(), a_np == c_np)
181

姜永久 已提交
182
    def test_not_equal(self):
183 184 185 186 187 188 189
        a_np = np.asarray([1, 2, 3, 4, 5])
        b_np = np.asarray([1, 2, 3, 4, 5])
        c_np = np.asarray([1, 2, 2, 4, 5])
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            c = fluid.dygraph.to_variable(c_np)
190 191
            res1 = a != b
            res2 = a != c
192 193
            np.testing.assert_array_equal(res1.numpy(), a_np != b_np)
            np.testing.assert_array_equal(res2.numpy(), a_np != c_np)
194

姜永久 已提交
195
    def test_less_than(self):
196 197 198 199 200
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
201
            res = a < b
202
            np.testing.assert_array_equal(res.numpy(), a_np < b_np)
203

姜永久 已提交
204
    def test_less_equal(self):
205 206 207 208 209
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
210
            res = a <= b
211
            np.testing.assert_array_equal(res.numpy(), a_np <= b_np)
212

姜永久 已提交
213
    def test_greater_than(self):
214 215 216 217 218
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
219
            res = a > b
220
            np.testing.assert_array_equal(res.numpy(), a_np > b_np)
221

姜永久 已提交
222
    def test_greater_equal(self):
223 224 225 226 227
        a_np = np.random.random(self.shape).astype(self.dtype)
        b_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
228
            res = a >= b
229
            np.testing.assert_array_equal(res.numpy(), a_np >= b_np)
230

姜永久 已提交
231
    def test_neg(self):
232 233 234 235
        a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            res = -a
236
            np.testing.assert_array_equal(res.numpy(), -a_np)
237

姜永久 已提交
238
    def test_float_int_long(self):
239 240 241 242
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(np.array([100.1]))
            self.assertTrue(float(a) == 100.1)
            self.assertTrue(int(a) == 100)
T
tianshuo78520a 已提交
243
            self.assertTrue(int(a) == 100)
244

姜永久 已提交
245
    def test_len(self):
246 247 248 249 250
        a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            self.assertTrue(len(a) == 10)

姜永久 已提交
251
    def test_index(self):
252 253 254 255 256 257 258 259 260 261 262
        with fluid.dygraph.guard():
            var1 = fluid.dygraph.to_variable(np.array([2]))
            i_tmp = 0
            for i in range(var1):
                self.assertTrue(i == i_tmp)
                i_tmp = i_tmp + 1
            list1 = [1, 2, 3, 4, 5]
            self.assertTrue(list1[var1] == 3)
            str1 = "just test"
            self.assertTrue(str1[var1] == 's')

姜永久 已提交
263
    def test_np_left_mul(self):
H
hong 已提交
264 265
        with fluid.dygraph.guard():
            t = np.sqrt(2.0 * np.pi)
266
            x = paddle.ones((2, 2), dtype="float32")
H
hong 已提交
267 268
            y = t * x

269 270 271 272 273 274
            np.testing.assert_allclose(
                y.numpy(),
                t * np.ones((2, 2), dtype='float32'),
                rtol=1e-05,
                atol=0.0,
            )
H
hong 已提交
275

姜永久 已提交
276
    def test_add_different_dtype(self):
277 278 279 280 281 282
        a_np = np.random.random(self.shape).astype(np.float32)
        b_np = np.random.random(self.shape).astype(np.float16)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)
            res = a + b
283
            np.testing.assert_array_equal(res.numpy(), a_np + b_np)
284

姜永久 已提交
285
    def test_floordiv_different_dtype(self):
286 287 288 289 290 291
        a_np = np.full(self.shape, 10, np.int64)
        b_np = np.full(self.shape, 2, np.int32)
        with fluid.dygraph.guard():
            a = paddle.to_tensor(a_np)
            b = paddle.to_tensor(b_np)
            res = a // b
292
            np.testing.assert_array_equal(res.numpy(), a_np // b_np)
293

姜永久 已提交
294
    def test_astype(self):
295 296 297 298 299 300 301 302 303 304
        a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            res1 = a.astype(np.float16)
            res2 = a.astype('float16')
            res3 = a.astype(fluid.core.VarDesc.VarType.FP16)

            self.assertEqual(res1.dtype, res2.dtype)
            self.assertEqual(res1.dtype, res3.dtype)

305 306
            np.testing.assert_array_equal(res1.numpy(), res2.numpy())
            np.testing.assert_array_equal(res1.numpy(), res3.numpy())
307

姜永久 已提交
308
    def test_conpare_op_broadcast(self):
309 310 311 312 313 314 315
        a_np = np.random.uniform(-1, 1, [10, 1, 10]).astype(self.dtype)
        b_np = np.random.uniform(-1, 1, [1, 1, 10]).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
            b = fluid.dygraph.to_variable(b_np)

            self.assertEqual((a != b).dtype, fluid.core.VarDesc.VarType.BOOL)
316
            np.testing.assert_array_equal((a != b).numpy(), a_np != b_np)
317

姜永久 已提交
318
    def test_tensor_patch_method(self):
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        paddle.disable_static()
        x_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype)
        y_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype)
        z_np = np.random.uniform(-1, 1, [6, 9]).astype(self.dtype)

        x = paddle.to_tensor(x_np)
        y = paddle.to_tensor(y_np)
        z = paddle.to_tensor(z_np)

        a = paddle.to_tensor([[1, 1], [2, 2], [3, 3]])
        b = paddle.to_tensor([[1, 1], [2, 2], [3, 3]])

        # 1. Unary operation for Tensor
        self.assertEqual(x.dim(), 2)
        self.assertEqual(x.ndimension(), 2)
        self.assertEqual(x.ndim, 2)
335 336
        self.assertEqual(x.size, 6)
        self.assertEqual(x.numel(), 6)
337 338 339 340
        np.testing.assert_array_equal(x.exp().numpy(), paddle.exp(x).numpy())
        np.testing.assert_array_equal(x.tanh().numpy(), paddle.tanh(x).numpy())
        np.testing.assert_array_equal(x.atan().numpy(), paddle.atan(x).numpy())
        np.testing.assert_array_equal(x.abs().numpy(), paddle.abs(x).numpy())
341
        m = x.abs()
342
        np.testing.assert_array_equal(m.sqrt().numpy(), paddle.sqrt(m).numpy())
343 344 345
        np.testing.assert_array_equal(
            m.rsqrt().numpy(), paddle.rsqrt(m).numpy()
        )
346
        np.testing.assert_array_equal(x.ceil().numpy(), paddle.ceil(x).numpy())
347 348 349
        np.testing.assert_array_equal(
            x.floor().numpy(), paddle.floor(x).numpy()
        )
350 351 352 353 354 355
        np.testing.assert_array_equal(x.cos().numpy(), paddle.cos(x).numpy())
        np.testing.assert_array_equal(x.acos().numpy(), paddle.acos(x).numpy())
        np.testing.assert_array_equal(x.asin().numpy(), paddle.asin(x).numpy())
        np.testing.assert_array_equal(x.sin().numpy(), paddle.sin(x).numpy())
        np.testing.assert_array_equal(x.sinh().numpy(), paddle.sinh(x).numpy())
        np.testing.assert_array_equal(x.cosh().numpy(), paddle.cosh(x).numpy())
356 357 358 359 360 361 362 363 364
        np.testing.assert_array_equal(
            x.round().numpy(), paddle.round(x).numpy()
        )
        np.testing.assert_array_equal(
            x.reciprocal().numpy(), paddle.reciprocal(x).numpy()
        )
        np.testing.assert_array_equal(
            x.square().numpy(), paddle.square(x).numpy()
        )
365 366
        np.testing.assert_array_equal(x.rank().numpy(), paddle.rank(x).numpy())
        np.testing.assert_array_equal(x[0].t().numpy(), paddle.t(x[0]).numpy())
367 368 369
        np.testing.assert_array_equal(
            x.asinh().numpy(), paddle.asinh(x).numpy()
        )
370
        # acosh(x) = nan, need to change input
X
xiaoting 已提交
371 372
        t_np = np.random.uniform(1, 2, [2, 3]).astype(self.dtype)
        t = paddle.to_tensor(t_np)
373 374 375 376 377 378 379 380 381 382 383 384 385
        np.testing.assert_array_equal(
            t.acosh().numpy(), paddle.acosh(t).numpy()
        )
        np.testing.assert_array_equal(
            x.atanh().numpy(), paddle.atanh(x).numpy()
        )
        d = paddle.to_tensor(
            [
                [1.2285208, 1.3491015, 1.4899898],
                [1.30058, 1.0688717, 1.4928783],
                [1.0958099, 1.3724753, 1.8926544],
            ]
        )
386
        d = d.matmul(d.t())
387 388
        # ROCM not support cholesky
        if not fluid.core.is_compiled_with_rocm():
389 390 391
            np.testing.assert_array_equal(
                d.cholesky().numpy(), paddle.cholesky(d).numpy()
            )
392 393

        np.testing.assert_array_equal(
394 395 396 397 398
            x.is_empty().numpy(), paddle.is_empty(x).numpy()
        )
        np.testing.assert_array_equal(
            x.isfinite().numpy(), paddle.isfinite(x).numpy()
        )
399
        np.testing.assert_array_equal(
400 401
            x.cast('int32').numpy(), paddle.cast(x, 'int32').numpy()
        )
402
        np.testing.assert_array_equal(
403 404 405 406 407 408 409 410
            x.expand([3, 2, 3]).numpy(), paddle.expand(x, [3, 2, 3]).numpy()
        )
        np.testing.assert_array_equal(
            x.tile([2, 2]).numpy(), paddle.tile(x, [2, 2]).numpy()
        )
        np.testing.assert_array_equal(
            x.flatten().numpy(), paddle.flatten(x).numpy()
        )
411
        index = paddle.to_tensor([0, 1])
412
        np.testing.assert_array_equal(
413 414
            x.gather(index).numpy(), paddle.gather(x, index).numpy()
        )
415
        index = paddle.to_tensor([[0, 1], [1, 2]])
416
        np.testing.assert_array_equal(
417 418
            x.gather_nd(index).numpy(), paddle.gather_nd(x, index).numpy()
        )
419
        np.testing.assert_array_equal(
420 421
            x.reverse([0, 1]).numpy(), paddle.reverse(x, [0, 1]).numpy()
        )
422
        np.testing.assert_array_equal(
423 424
            a.reshape([3, 2]).numpy(), paddle.reshape(a, [3, 2]).numpy()
        )
425 426
        np.testing.assert_array_equal(
            x.slice([0, 1], [0, 0], [1, 2]).numpy(),
427 428
            paddle.slice(x, [0, 1], [0, 0], [1, 2]).numpy(),
        )
429
        np.testing.assert_array_equal(
430 431
            x.split(2)[0].numpy(), paddle.split(x, 2)[0].numpy()
        )
432
        m = paddle.to_tensor(
433 434
            np.random.uniform(-1, 1, [1, 6, 1, 1]).astype(self.dtype)
        )
435
        np.testing.assert_array_equal(
436 437
            m.squeeze([]).numpy(), paddle.squeeze(m, []).numpy()
        )
438
        np.testing.assert_array_equal(
439 440
            m.squeeze([1, 2]).numpy(), paddle.squeeze(m, [1, 2]).numpy()
        )
441
        m = paddle.to_tensor([2, 3, 3, 1, 5, 3], 'float32')
442 443 444
        np.testing.assert_array_equal(
            m.unique()[0].numpy(), paddle.unique(m)[0].numpy()
        )
445 446
        np.testing.assert_array_equal(
            m.unique(return_counts=True)[1],
447 448
            paddle.unique(m, return_counts=True)[1],
        )
449 450 451 452
        np.testing.assert_array_equal(x.flip([0]), paddle.flip(x, [0]))
        np.testing.assert_array_equal(x.unbind(0), paddle.unbind(x, 0))
        np.testing.assert_array_equal(x.roll(1), paddle.roll(x, 1))
        np.testing.assert_array_equal(x.cumsum(1), paddle.cumsum(x, 1))
453
        m = paddle.to_tensor(1)
454
        np.testing.assert_array_equal(m.increment(), paddle.increment(m))
455
        m = x.abs()
456 457 458
        np.testing.assert_array_equal(m.log(), paddle.log(m))
        np.testing.assert_array_equal(x.pow(2), paddle.pow(x, 2))
        np.testing.assert_array_equal(x.reciprocal(), paddle.reciprocal(x))
459 460

        # 2. Binary operation
461
        np.testing.assert_array_equal(
462 463
            x.divide(y).numpy(), paddle.divide(x, y).numpy()
        )
464 465
        np.testing.assert_array_equal(
            x.matmul(y, True, False).numpy(),
466 467
            paddle.matmul(x, y, True, False).numpy(),
        )
468 469
        np.testing.assert_array_equal(
            x.norm(p='fro', axis=[0, 1]).numpy(),
470 471
            paddle.norm(x, p='fro', axis=[0, 1]).numpy(),
        )
472
        np.testing.assert_array_equal(
473 474
            x.dist(y).numpy(), paddle.dist(x, y).numpy()
        )
475
        np.testing.assert_array_equal(
476 477
            x.cross(y).numpy(), paddle.cross(x, y).numpy()
        )
478 479
        m = x.expand([2, 2, 3])
        n = y.expand([2, 2, 3]).transpose([0, 2, 1])
480
        np.testing.assert_array_equal(
481 482
            m.bmm(n).numpy(), paddle.bmm(m, n).numpy()
        )
483
        np.testing.assert_array_equal(
484 485
            x.histogram(5, -1, 1).numpy(), paddle.histogram(x, 5, -1, 1).numpy()
        )
486
        np.testing.assert_array_equal(
487 488
            x.equal(y).numpy(), paddle.equal(x, y).numpy()
        )
489
        np.testing.assert_array_equal(
490 491
            x.greater_equal(y).numpy(), paddle.greater_equal(x, y).numpy()
        )
492
        np.testing.assert_array_equal(
493 494
            x.greater_than(y).numpy(), paddle.greater_than(x, y).numpy()
        )
495
        np.testing.assert_array_equal(
496 497
            x.less_equal(y).numpy(), paddle.less_equal(x, y).numpy()
        )
498
        np.testing.assert_array_equal(
499 500
            x.less_than(y).numpy(), paddle.less_than(x, y).numpy()
        )
501
        np.testing.assert_array_equal(
502 503
            x.not_equal(y).numpy(), paddle.not_equal(x, y).numpy()
        )
504
        np.testing.assert_array_equal(
505 506
            x.equal_all(y).numpy(), paddle.equal_all(x, y).numpy()
        )
507
        np.testing.assert_array_equal(
508 509
            x.allclose(y).numpy(), paddle.allclose(x, y).numpy()
        )
510
        m = x.expand([2, 2, 3])
511
        np.testing.assert_array_equal(
512 513
            x.expand_as(m).numpy(), paddle.expand_as(x, m).numpy()
        )
514
        index = paddle.to_tensor([2, 1, 0])
515
        np.testing.assert_array_equal(
516 517
            a.scatter(index, b).numpy(), paddle.scatter(a, index, b).numpy()
        )
518 519 520 521

        # 3. Bool tensor operation
        x = paddle.to_tensor([[True, False], [True, False]])
        y = paddle.to_tensor([[False, False], [False, True]])
522
        np.testing.assert_array_equal(
523 524
            x.logical_and(y).numpy(), paddle.logical_and(x, y).numpy()
        )
525
        np.testing.assert_array_equal(
526 527
            x.logical_not(y).numpy(), paddle.logical_not(x, y).numpy()
        )
528
        np.testing.assert_array_equal(
529 530
            x.logical_or(y).numpy(), paddle.logical_or(x, y).numpy()
        )
531
        np.testing.assert_array_equal(
532 533
            x.logical_xor(y).numpy(), paddle.logical_xor(x, y).numpy()
        )
534
        np.testing.assert_array_equal(
535 536
            x.logical_and(y).numpy(), paddle.logical_and(x, y).numpy()
        )
537 538
        a = paddle.to_tensor([[1, 2], [3, 4]])
        b = paddle.to_tensor([[4, 3], [2, 1]])
539
        np.testing.assert_array_equal(
540 541
            x.where(a, b).numpy(), paddle.where(x, a, b).numpy()
        )
542

543 544 545 546
        x_np = np.random.randn(3, 6, 9, 7)
        x = paddle.to_tensor(x_np)
        x_T = x.T
        self.assertTrue(x_T.shape, [7, 9, 6, 3])
547
        np.testing.assert_array_equal(x_T.numpy(), x_np.T)
548

549 550 551 552 553 554
        self.assertTrue(inspect.ismethod(a.dot))
        self.assertTrue(inspect.ismethod(a.logsumexp))
        self.assertTrue(inspect.ismethod(a.multiplex))
        self.assertTrue(inspect.ismethod(a.prod))
        self.assertTrue(inspect.ismethod(a.scale))
        self.assertTrue(inspect.ismethod(a.stanh))
S
Steffy-zxf 已提交
555
        self.assertTrue(inspect.ismethod(a.add_n))
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
        self.assertTrue(inspect.ismethod(a.max))
        self.assertTrue(inspect.ismethod(a.maximum))
        self.assertTrue(inspect.ismethod(a.min))
        self.assertTrue(inspect.ismethod(a.minimum))
        self.assertTrue(inspect.ismethod(a.floor_divide))
        self.assertTrue(inspect.ismethod(a.remainder))
        self.assertTrue(inspect.ismethod(a.floor_mod))
        self.assertTrue(inspect.ismethod(a.multiply))
        self.assertTrue(inspect.ismethod(a.logsumexp))
        self.assertTrue(inspect.ismethod(a.inverse))
        self.assertTrue(inspect.ismethod(a.log1p))
        self.assertTrue(inspect.ismethod(a.erf))
        self.assertTrue(inspect.ismethod(a.addmm))
        self.assertTrue(inspect.ismethod(a.clip))
        self.assertTrue(inspect.ismethod(a.trace))
        self.assertTrue(inspect.ismethod(a.kron))
        self.assertTrue(inspect.ismethod(a.isinf))
        self.assertTrue(inspect.ismethod(a.isnan))
        self.assertTrue(inspect.ismethod(a.concat))
        self.assertTrue(inspect.ismethod(a.broadcast_to))
        self.assertTrue(inspect.ismethod(a.scatter_nd_add))
        self.assertTrue(inspect.ismethod(a.scatter_nd))
        self.assertTrue(inspect.ismethod(a.shard_index))
        self.assertTrue(inspect.ismethod(a.chunk))
        self.assertTrue(inspect.ismethod(a.stack))
        self.assertTrue(inspect.ismethod(a.strided_slice))
        self.assertTrue(inspect.ismethod(a.unsqueeze))
        self.assertTrue(inspect.ismethod(a.unstack))
        self.assertTrue(inspect.ismethod(a.argmax))
        self.assertTrue(inspect.ismethod(a.argmin))
        self.assertTrue(inspect.ismethod(a.argsort))
        self.assertTrue(inspect.ismethod(a.masked_select))
        self.assertTrue(inspect.ismethod(a.topk))
        self.assertTrue(inspect.ismethod(a.index_select))
        self.assertTrue(inspect.ismethod(a.nonzero))
        self.assertTrue(inspect.ismethod(a.sort))
        self.assertTrue(inspect.ismethod(a.index_sample))
        self.assertTrue(inspect.ismethod(a.mean))
        self.assertTrue(inspect.ismethod(a.std))
        self.assertTrue(inspect.ismethod(a.numel))
596

姜永久 已提交
597
    def test_complex_scalar(self):
598 599 600
        a_np = np.random.random(self.shape).astype(self.dtype)
        with fluid.dygraph.guard():
            a = fluid.dygraph.to_variable(a_np)
601
            res = 1j * a
602
            np.testing.assert_array_equal(res.numpy(), 1j * a_np)
603

姜永久 已提交
604
    def test_matmul(self):
605 606 607 608 609 610 611 612 613 614
        x_np = np.random.uniform(-1, 1, [2, 3]).astype(self.dtype)
        y_np = np.random.uniform(-1, 1, [3, 2]).astype(self.dtype)
        except_out = x_np @ y_np

        with fluid.dygraph.guard():
            x = paddle.to_tensor(x_np)
            y = paddle.to_tensor(y_np)
            out = x @ y
            np.testing.assert_allclose(out.numpy(), except_out, atol=1e-03)

615 616 617

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