test_var_base.py 63.4 KB
Newer Older
L
Leo Chen 已提交
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 copy
L
Leo Chen 已提交
16
import unittest
17

18 19
import numpy as np

20
import paddle
21
import paddle.nn.functional as F
22 23
from paddle import base
from paddle.base import core
L
Leo Chen 已提交
24 25 26 27 28 29 30 31


class TestVarBase(unittest.TestCase):
    def setUp(self):
        self.shape = [512, 1234]
        self.dtype = np.float32
        self.array = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)

32
    def test_to_tensor(self):
33
        def check_with_place(place):
34
            with base.dygraph.guard():
35
                paddle.set_default_dtype('float32')
36
                # set_default_dtype should not take effect on int
37
                x = paddle.to_tensor(1, place=place, stop_gradient=False)
38
                np.testing.assert_array_equal(x.numpy(), [1])
39 40
                self.assertNotEqual(x.dtype, core.VarDesc.VarType.FP32)

41 42 43
                y = paddle.to_tensor(2, place=x.place)
                self.assertEqual(str(x.place), str(y.place))

44
                # set_default_dtype should not take effect on numpy
45 46 47 48 49 50 51 52
                x = paddle.to_tensor(
                    np.array([1.2]).astype('float16'),
                    place=place,
                    stop_gradient=False,
                )
                np.testing.assert_array_equal(
                    x.numpy(), np.array([1.2], 'float16')
                )
53 54
                self.assertEqual(x.dtype, core.VarDesc.VarType.FP16)

55 56 57 58
                # set_default_dtype take effect on int
                x = paddle.to_tensor(1, place=place)
                self.assertTrue(x.dtype, core.VarDesc.VarType.INT64)

59
                # set_default_dtype take effect on float
60
                x = paddle.to_tensor(1.2, place=place, stop_gradient=False)
61 62 63
                np.testing.assert_array_equal(
                    x.numpy(), np.array([1.2]).astype('float32')
                )
64
                self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
Z
Zhou Wei 已提交
65
                clone_x = x.clone()
66 67 68
                np.testing.assert_array_equal(
                    clone_x.numpy(), np.array([1.2]).astype('float32')
                )
Z
Zhou Wei 已提交
69 70 71
                self.assertEqual(clone_x.dtype, core.VarDesc.VarType.FP32)
                y = clone_x**2
                y.backward()
72 73 74
                np.testing.assert_array_equal(
                    x.grad.numpy(), np.array([2.4]).astype('float32')
                )
75
                y = x.cpu()
76
                self.assertEqual(y.place.__repr__(), "Place(cpu)")
77 78
                if core.is_compiled_with_cuda():
                    y = x.pin_memory()
79
                    self.assertEqual(y.place.__repr__(), "Place(gpu_pinned)")
80
                    y = x.cuda()
81
                    self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
82
                    y = x.cuda(None)
83
                    self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
84
                    y = x.cuda(device_id=0)
85
                    self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
86
                    y = x.cuda(blocking=False)
87
                    self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
88
                    y = x.cuda(blocking=True)
89
                    self.assertEqual(y.place.__repr__(), "Place(gpu:0)")
90 91
                    with self.assertRaises(ValueError):
                        y = x.cuda("test")
92

93 94 95 96 97
                # support 'dtype' is core.VarType
                x = paddle.rand((2, 2))
                y = paddle.to_tensor([2, 2], dtype=x.dtype)
                self.assertEqual(y.dtype, core.VarDesc.VarType.FP32)

98
                # set_default_dtype take effect on complex
99
                x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
100
                np.testing.assert_array_equal(x.numpy(), [1 + 2j])
C
chentianyu03 已提交
101
                self.assertEqual(x.dtype, core.VarDesc.VarType.COMPLEX64)
102 103 104

                paddle.set_default_dtype('float64')
                x = paddle.to_tensor(1.2, place=place, stop_gradient=False)
105
                np.testing.assert_array_equal(x.numpy(), [1.2])
106 107 108
                self.assertEqual(x.dtype, core.VarDesc.VarType.FP64)

                x = paddle.to_tensor(1 + 2j, place=place, stop_gradient=False)
109
                np.testing.assert_array_equal(x.numpy(), [1 + 2j])
C
chentianyu03 已提交
110
                self.assertEqual(x.dtype, core.VarDesc.VarType.COMPLEX128)
111

112 113 114
                x = paddle.to_tensor(
                    1, dtype='float32', place=place, stop_gradient=False
                )
115
                np.testing.assert_array_equal(x.numpy(), [1.0])
116
                self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
117
                self.assertEqual(x.shape, [])
118 119 120
                self.assertEqual(x.stop_gradient, False)
                self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)

121 122 123 124 125 126
                x = paddle.to_tensor(
                    (1, 2), dtype='float32', place=place, stop_gradient=False
                )
                x = paddle.to_tensor(
                    [1, 2], dtype='float32', place=place, stop_gradient=False
                )
127
                np.testing.assert_array_equal(x.numpy(), [1.0, 2.0])
128
                self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
129
                self.assertIsNone(x.grad)
130 131 132 133
                self.assertEqual(x.shape, [2])
                self.assertEqual(x.stop_gradient, False)
                self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)

134 135 136 137 138 139
                x = paddle.to_tensor(
                    self.array,
                    dtype='float32',
                    place=place,
                    stop_gradient=False,
                )
140
                np.testing.assert_array_equal(x.numpy(), self.array)
141 142 143 144 145 146 147
                self.assertEqual(x.dtype, core.VarDesc.VarType.FP32)
                self.assertEqual(x.shape, self.shape)
                self.assertEqual(x.stop_gradient, False)
                self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)

                y = paddle.to_tensor(x)
                y = paddle.to_tensor(y, dtype='float64', place=place)
148
                np.testing.assert_array_equal(y.numpy(), self.array)
149 150 151 152 153
                self.assertEqual(y.dtype, core.VarDesc.VarType.FP64)
                self.assertEqual(y.shape, self.shape)
                self.assertEqual(y.stop_gradient, True)
                self.assertEqual(y.type, core.VarDesc.VarType.LOD_TENSOR)
                z = x + y
154
                np.testing.assert_array_equal(z.numpy(), 2 * self.array)
155

156 157 158
                x = paddle.to_tensor(
                    [1 + 2j, 1 - 2j], dtype='complex64', place=place
                )
159
                y = paddle.to_tensor(x)
160
                np.testing.assert_array_equal(x.numpy(), [1 + 2j, 1 - 2j])
C
chentianyu03 已提交
161
                self.assertEqual(y.dtype, core.VarDesc.VarType.COMPLEX64)
162 163
                self.assertEqual(y.shape, [2])

164 165 166 167 168
                paddle.set_default_dtype('float32')
                x = paddle.randn([3, 4])
                x_array = np.array(x)
                self.assertEqual(x_array.shape, x.numpy().shape)
                self.assertEqual(x_array.dtype, x.numpy().dtype)
169
                np.testing.assert_array_equal(x_array, x.numpy())
170

171
                x = paddle.to_tensor(1.0, place=place)
172 173 174 175 176 177 178
                self.assertEqual(x.item(), 1.0)
                self.assertTrue(isinstance(x.item(), float))

                x = paddle.randn([3, 2, 2])
                self.assertTrue(isinstance(x.item(5), float))
                self.assertTrue(isinstance(x.item(1, 0, 1), float))
                self.assertEqual(x.item(5), x.item(1, 0, 1))
179 180 181
                np.testing.assert_array_equal(
                    x.item(1, 0, 1), x.numpy().item(1, 0, 1)
                )
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

                x = paddle.to_tensor([[1.111111, 2.222222, 3.333333]])
                self.assertEqual(x.item(0, 2), x.item(2))
                self.assertAlmostEqual(x.item(2), 3.333333)
                self.assertTrue(isinstance(x.item(0, 2), float))

                x = paddle.to_tensor(1.0, dtype='float64')
                self.assertEqual(x.item(), 1.0)
                self.assertTrue(isinstance(x.item(), float))

                x = paddle.to_tensor(1.0, dtype='float16')
                self.assertEqual(x.item(), 1.0)
                self.assertTrue(isinstance(x.item(), float))

                x = paddle.to_tensor(1, dtype='uint8')
                self.assertEqual(x.item(), 1)
                self.assertTrue(isinstance(x.item(), int))

                x = paddle.to_tensor(1, dtype='int8')
                self.assertEqual(x.item(), 1)
                self.assertTrue(isinstance(x.item(), int))

                x = paddle.to_tensor(1, dtype='int16')
                self.assertEqual(x.item(), 1)
                self.assertTrue(isinstance(x.item(), int))

                x = paddle.to_tensor(1, dtype='int32')
                self.assertEqual(x.item(), 1)
                self.assertTrue(isinstance(x.item(), int))

                x = paddle.to_tensor(1, dtype='int64')
                self.assertEqual(x.item(), 1)
T
tianshuo78520a 已提交
214
                self.assertTrue(isinstance(x.item(), int))
215 216 217 218 219 220 221 222 223

                x = paddle.to_tensor(True)
                self.assertEqual(x.item(), True)
                self.assertTrue(isinstance(x.item(), bool))

                x = paddle.to_tensor(1 + 1j)
                self.assertEqual(x.item(), 1 + 1j)
                self.assertTrue(isinstance(x.item(), complex))

224 225 226 227 228
                # empty tensor
                x = paddle.to_tensor([])
                self.assertEqual(x.shape, [0])
                expected_result = np.array([], dtype='float32')
                self.assertEqual(x.numpy().shape, expected_result.shape)
229
                np.testing.assert_array_equal(x.numpy(), expected_result)
230

231 232
                numpy_array = np.random.randn(3, 4)
                # covert core.LoDTensor to paddle.Tensor
233 234
                lod_tensor = paddle.base.core.LoDTensor()
                place = paddle.base.framework._current_expected_place()
235 236
                lod_tensor.set(numpy_array, place)
                x = paddle.to_tensor(lod_tensor)
237
                np.testing.assert_array_equal(x.numpy(), numpy_array)
238 239 240 241 242 243
                self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)
                self.assertEqual(str(x.place), str(place))

                # covert core.Tensor to paddle.Tensor
                x = paddle.to_tensor(numpy_array)
                dlpack = x.value().get_tensor()._to_dlpack()
244
                tensor_from_dlpack = paddle.base.core.from_dlpack(dlpack)
245
                x = paddle.to_tensor(tensor_from_dlpack)
246
                np.testing.assert_array_equal(x.numpy(), numpy_array)
247 248
                self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR)

249
                # test dtype=bfloat16
250 251 252
                x = paddle.to_tensor(-1e6, dtype=paddle.bfloat16)
                self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
                self.assertTrue(x == -999424.0)
253 254
                self.assertTrue(x.item() == -999424.0)
                self.assertTrue(isinstance(x.item(), float))
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

                x = paddle.to_tensor([-1e6, -1e6, -1e6], dtype='bfloat16')
                self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
                self.assertTrue(x[0] == -999424.0)
                self.assertTrue(x[1] == -999424.0)
                self.assertTrue(x[2] == -999424.0)

                x = paddle.to_tensor(
                    -1e6, dtype=paddle.bfloat16, stop_gradient=False
                )
                self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
                self.assertTrue(x == -999424.0)
                y = x * x
                y.backward()
                self.assertTrue(x.grad == -999424.0 * 2)

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                # test default_type=bfloat16
                paddle.set_default_dtype('bfloat16')
                x = paddle.to_tensor(-1e6)
                self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
                self.assertTrue(x == -999424.0)
                self.assertTrue(x.item() == -999424.0)
                self.assertTrue(isinstance(x.item(), float))

                x = paddle.to_tensor([-1e6, -1e6, -1e6])
                self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
                self.assertTrue(x[0] == -999424.0)
                self.assertTrue(x[1] == -999424.0)
                self.assertTrue(x[2] == -999424.0)

                x = paddle.to_tensor(-1e6, stop_gradient=False)
                self.assertEqual(x.dtype, core.VarDesc.VarType.BF16)
                self.assertTrue(x == -999424.0)
                y = x * x
                y.backward()
                self.assertTrue(x.grad == -999424.0 * 2)
                paddle.set_default_dtype('float32')

293 294 295 296 297 298 299 300
                with self.assertRaises(ValueError):
                    paddle.randn([3, 2, 2]).item()
                with self.assertRaises(ValueError):
                    paddle.randn([3, 2, 2]).item(18)
                with self.assertRaises(ValueError):
                    paddle.randn([3, 2, 2]).item(1, 2)
                with self.assertRaises(ValueError):
                    paddle.randn([3, 2, 2]).item(2, 1, 2)
301 302 303 304 305 306 307 308 309 310 311
                with self.assertRaises(TypeError):
                    paddle.to_tensor('test')
                with self.assertRaises(TypeError):
                    paddle.to_tensor(1, dtype='test')
                with self.assertRaises(ValueError):
                    paddle.to_tensor([[1], [2, 3]])
                with self.assertRaises(ValueError):
                    paddle.to_tensor([[1], [2, 3]], place='test')
                with self.assertRaises(ValueError):
                    paddle.to_tensor([[1], [2, 3]], place=1)

312 313
        check_with_place(core.CPUPlace())
        check_with_place("cpu")
314
        if core.is_compiled_with_cuda():
315 316 317 318
            check_with_place(core.CUDAPinnedPlace())
            check_with_place("gpu_pinned")
            check_with_place(core.CUDAPlace(0))
            check_with_place("gpu:0")
319

320
    def test_to_tensor_not_change_input_stop_gradient(self):
321
        with paddle.base.dygraph.guard(core.CPUPlace()):
322 323 324 325 326 327
            a = paddle.zeros([1024])
            a.stop_gradient = False
            b = paddle.to_tensor(a)
            self.assertEqual(a.stop_gradient, False)
            self.assertEqual(b.stop_gradient, True)

328
    def test_to_tensor_change_place(self):
329 330
        if core.is_compiled_with_cuda():
            a_np = np.random.rand(1024, 1024)
331
            with paddle.base.dygraph.guard(core.CPUPlace()):
332 333
                a = paddle.to_tensor(a_np, place=paddle.CUDAPinnedPlace())
                a = paddle.to_tensor(a)
334
                self.assertEqual(a.place.__repr__(), "Place(cpu)")
335

336
            with paddle.base.dygraph.guard(core.CUDAPlace(0)):
337 338
                a = paddle.to_tensor(a_np, place=paddle.CUDAPinnedPlace())
                a = paddle.to_tensor(a)
339
                self.assertEqual(a.place.__repr__(), "Place(gpu:0)")
340

341
            with paddle.base.dygraph.guard(core.CUDAPlace(0)):
342 343
                a = paddle.to_tensor(a_np, place=paddle.CPUPlace())
                a = paddle.to_tensor(a, place=paddle.CUDAPinnedPlace())
344
                self.assertEqual(a.place.__repr__(), "Place(gpu_pinned)")
345

346
    def test_to_tensor_with_lodtensor(self):
347 348
        if core.is_compiled_with_cuda():
            a_np = np.random.rand(1024, 1024)
349
            with paddle.base.dygraph.guard(core.CPUPlace()):
350 351 352
                lod_tensor = core.LoDTensor()
                lod_tensor.set(a_np, core.CPUPlace())
                a = paddle.to_tensor(lod_tensor)
353
                np.testing.assert_array_equal(a_np, a.numpy())
354

355
            with paddle.base.dygraph.guard(core.CUDAPlace(0)):
356 357
                lod_tensor = core.LoDTensor()
                lod_tensor.set(a_np, core.CUDAPlace(0))
358
                a = paddle.to_tensor(lod_tensor, place=core.CPUPlace())
359
                np.testing.assert_array_equal(a_np, a.numpy())
360
                self.assertTrue(a.place.__repr__(), "Place(cpu)")
361

362
    def test_to_variable(self):
363 364
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array, name="abc")
365
            np.testing.assert_array_equal(var.numpy(), self.array)
L
Leo Chen 已提交
366 367 368 369 370 371 372
            self.assertEqual(var.name, 'abc')
            # default value
            self.assertEqual(var.persistable, False)
            self.assertEqual(var.stop_gradient, True)
            self.assertEqual(var.shape, self.shape)
            self.assertEqual(var.dtype, core.VarDesc.VarType.FP32)
            self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR)
373 374
            # The type of input must be 'ndarray' or 'Variable', it will raise TypeError
            with self.assertRaises(TypeError):
375
                var = base.dygraph.to_variable("test", name="abc")
376 377
            # test to_variable of LayerObjectHelper(LayerHelperBase)
            with self.assertRaises(TypeError):
378
                linear = paddle.nn.Linear(32, 64)
379
                var = linear._helper.to_variable("test", name="abc")
L
Leo Chen 已提交
380

381
    def test_list_to_variable(self):
382
        with base.dygraph.guard():
383
            array = [[[1, 2], [1, 2], [1.0, 2]], [[1, 2], [1, 2], [1, 2]]]
384
            var = base.dygraph.to_variable(array, dtype='int32')
385
            np.testing.assert_array_equal(var.numpy(), array)
386 387 388 389
            self.assertEqual(var.shape, [2, 3, 2])
            self.assertEqual(var.dtype, core.VarDesc.VarType.INT32)
            self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR)

390
    def test_tuple_to_variable(self):
391
        with base.dygraph.guard():
392
            array = (((1, 2), (1, 2), (1, 2)), ((1, 2), (1, 2), (1, 2)))
393
            var = base.dygraph.to_variable(array, dtype='float32')
394
            np.testing.assert_array_equal(var.numpy(), array)
395 396 397 398
            self.assertEqual(var.shape, [2, 3, 2])
            self.assertEqual(var.dtype, core.VarDesc.VarType.FP32)
            self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR)

399
    def test_tensor_to_variable(self):
400 401 402 403
        with base.dygraph.guard():
            t = base.Tensor()
            t.set(np.random.random((1024, 1024)), base.CPUPlace())
            var = base.dygraph.to_variable(t)
404
            np.testing.assert_array_equal(t, var.numpy())
405

406
    def test_leaf_tensor(self):
407
        with base.dygraph.guard():
408 409 410 411 412
            x = paddle.to_tensor(np.random.uniform(-1, 1, size=[10, 10]))
            self.assertTrue(x.is_leaf)
            y = x + 1
            self.assertTrue(y.is_leaf)

413 414 415
            x = paddle.to_tensor(
                np.random.uniform(-1, 1, size=[10, 10]), stop_gradient=False
            )
416 417 418 419 420
            self.assertTrue(x.is_leaf)
            y = x + 1
            self.assertFalse(y.is_leaf)

            linear = paddle.nn.Linear(10, 10)
421 422 423 424
            input = paddle.to_tensor(
                np.random.uniform(-1, 1, size=[10, 10]).astype('float32'),
                stop_gradient=False,
            )
425 426 427 428 429 430 431
            self.assertTrue(input.is_leaf)

            out = linear(input)
            self.assertTrue(linear.weight.is_leaf)
            self.assertTrue(linear.bias.is_leaf)
            self.assertFalse(out.is_leaf)

432
    def test_detach(self):
433
        with base.dygraph.guard():
434
            x = paddle.to_tensor([1.0], dtype="float64", stop_gradient=False)
Z
Zhou Wei 已提交
435 436 437
            detach_x = x.detach()
            self.assertTrue(detach_x.stop_gradient, True)

438 439 440
            cmp_float = (
                np.allclose if core.is_compiled_with_rocm() else np.array_equal
            )
Z
Zhou Wei 已提交
441
            detach_x[:] = 10.0
Z
zhulei 已提交
442
            self.assertTrue(cmp_float(x.numpy(), [10.0]))
Z
Zhou Wei 已提交
443 444 445

            y = x**2
            y.backward()
Z
zhulei 已提交
446
            self.assertTrue(cmp_float(x.grad.numpy(), [20.0]))
447
            self.assertIsNone(detach_x.grad)
Z
Zhou Wei 已提交
448

449 450 451
            detach_x.stop_gradient = (
                False  # Set stop_gradient to be False, supported auto-grad
            )
Z
Zhou Wei 已提交
452 453
            z = 3 * detach_x**2
            z.backward()
Z
zhulei 已提交
454 455
            self.assertTrue(cmp_float(x.grad.numpy(), [20.0]))
            self.assertTrue(cmp_float(detach_x.grad.numpy(), [60.0]))
456

457 458 459 460 461
            with self.assertRaises(ValueError):
                detach_x[:] = 5.0

            detach_x.stop_gradient = True

Z
Zhou Wei 已提交
462
            # Due to sharing of data with origin Tensor, There are some unsafe operations:
463 464 465 466
            with self.assertRaises(RuntimeError):
                y = 2**x
                detach_x[:] = 5.0
                y.backward()
Z
Zhou Wei 已提交
467

468
    def test_write_property(self):
469 470
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
L
Leo Chen 已提交
471

472
            self.assertEqual(var.name, 'generated_tensor_0')
L
Leo Chen 已提交
473 474 475 476 477 478 479 480 481 482 483
            var.name = 'test'
            self.assertEqual(var.name, 'test')

            self.assertEqual(var.persistable, False)
            var.persistable = True
            self.assertEqual(var.persistable, True)

            self.assertEqual(var.stop_gradient, True)
            var.stop_gradient = False
            self.assertEqual(var.stop_gradient, False)

484
    def test_deep_copy(self):
485
        with base.dygraph.guard():
姜永久 已提交
486
            empty_var = core.eager.Tensor()
487
            empty_var_copy = copy.deepcopy(empty_var)
488 489 490
            self.assertEqual(
                empty_var.stop_gradient, empty_var_copy.stop_gradient
            )
491 492 493 494
            self.assertEqual(empty_var.persistable, empty_var_copy.persistable)
            self.assertEqual(empty_var.type, empty_var_copy.type)
            self.assertEqual(empty_var.dtype, empty_var_copy.dtype)

495 496
            x = paddle.to_tensor([2.0], stop_gradient=False)
            y = paddle.to_tensor([3.0], stop_gradient=False)
497 498 499 500 501 502 503 504 505
            z = x * y
            memo = {}
            x_copy = copy.deepcopy(x, memo)
            y_copy = copy.deepcopy(y, memo)

            self.assertEqual(x_copy.stop_gradient, y_copy.stop_gradient)
            self.assertEqual(x_copy.persistable, y_copy.persistable)
            self.assertEqual(x_copy.type, y_copy.type)
            self.assertEqual(x_copy.dtype, y_copy.dtype)
506 507
            np.testing.assert_array_equal(x.numpy(), x_copy.numpy())
            np.testing.assert_array_equal(y.numpy(), y_copy.numpy())
508 509

            self.assertNotEqual(id(x), id(x_copy))
510
            np.testing.assert_array_equal(x.numpy(), [2.0])
511

512
            with self.assertRaises(ValueError):
513
                x_copy[:] = 5.0
514

515 516 517 518 519 520 521 522 523
            with self.assertRaises(RuntimeError):
                copy.deepcopy(z)

            x_copy2 = copy.deepcopy(x, memo)
            y_copy2 = copy.deepcopy(y, memo)
            self.assertEqual(id(x_copy), id(x_copy2))
            self.assertEqual(id(y_copy), id(y_copy2))

            # test copy selected rows
姜永久 已提交
524 525 526 527 528 529 530
            x = core.eager.Tensor(
                core.VarDesc.VarType.FP32,
                [3, 100],
                "selected_rows",
                core.VarDesc.VarType.SELECTED_ROWS,
                True,
            )
531

532
            selected_rows = x.value().get_selected_rows()
533 534 535
            selected_rows.get_tensor().set(
                np.random.rand(3, 100), core.CPUPlace()
            )
536 537 538 539 540 541 542 543 544 545
            selected_rows.set_height(10)
            selected_rows.set_rows([3, 5, 7])
            x_copy = copy.deepcopy(x)

            self.assertEqual(x_copy.stop_gradient, x.stop_gradient)
            self.assertEqual(x_copy.persistable, x.persistable)
            self.assertEqual(x_copy.type, x.type)
            self.assertEqual(x_copy.dtype, x.dtype)

            copy_selected_rows = x_copy.value().get_selected_rows()
546 547 548
            self.assertEqual(
                copy_selected_rows.height(), selected_rows.height()
            )
549
            self.assertEqual(copy_selected_rows.rows(), selected_rows.rows())
550 551
            np.testing.assert_array_equal(
                np.array(copy_selected_rows.get_tensor()),
552 553
                np.array(selected_rows.get_tensor()),
            )
554

L
Leo Chen 已提交
555
    # test some patched methods
556
    def test_set_value(self):
557 558
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
L
Leo Chen 已提交
559 560 561 562 563
            tmp1 = np.random.uniform(0.1, 1, [2, 2, 3]).astype(self.dtype)
            self.assertRaises(AssertionError, var.set_value, tmp1)

            tmp2 = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
            var.set_value(tmp2)
564
            np.testing.assert_array_equal(var.numpy(), tmp2)
L
Leo Chen 已提交
565

566
    def test_to_string(self):
567 568
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
569
            self.assertTrue(isinstance(str(var), str))
L
Leo Chen 已提交
570

571
    def test_element_size(self):
572
        with base.dygraph.guard():
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
            x = paddle.to_tensor(1, dtype='bool')
            self.assertEqual(x.element_size(), 1)

            x = paddle.to_tensor(1, dtype='float16')
            self.assertEqual(x.element_size(), 2)

            x = paddle.to_tensor(1, dtype='float32')
            self.assertEqual(x.element_size(), 4)

            x = paddle.to_tensor(1, dtype='float64')
            self.assertEqual(x.element_size(), 8)

            x = paddle.to_tensor(1, dtype='int8')
            self.assertEqual(x.element_size(), 1)

            x = paddle.to_tensor(1, dtype='int16')
            self.assertEqual(x.element_size(), 2)

            x = paddle.to_tensor(1, dtype='int32')
            self.assertEqual(x.element_size(), 4)

            x = paddle.to_tensor(1, dtype='int64')
            self.assertEqual(x.element_size(), 8)

            x = paddle.to_tensor(1, dtype='uint8')
            self.assertEqual(x.element_size(), 1)

            x = paddle.to_tensor(1, dtype='complex64')
            self.assertEqual(x.element_size(), 8)

            x = paddle.to_tensor(1, dtype='complex128')
            self.assertEqual(x.element_size(), 16)

606
    def test_backward(self):
607 608
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
L
Leo Chen 已提交
609
            var.stop_gradient = False
610
            loss = F.relu(var)
L
Leo Chen 已提交
611 612 613 614
            loss.backward()
            grad_var = var._grad_ivar()
            self.assertEqual(grad_var.shape, self.shape)

615
    def test_gradient(self):
616 617
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
L
Leo Chen 已提交
618
            var.stop_gradient = False
619
            loss = F.relu(var)
L
Leo Chen 已提交
620 621 622 623
            loss.backward()
            grad_var = var.gradient()
            self.assertEqual(grad_var.shape, self.array.shape)

624
    def test_block(self):
625 626
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
627
            self.assertEqual(
628
                var.block, base.default_main_program().global_block()
629
            )
L
Leo Chen 已提交
630

631
    def _test_slice(self):
632
        w = base.dygraph.to_variable(
633 634
            np.random.random((784, 100, 100)).astype('float64')
        )
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

        for i in range(3):
            nw = w[i]
            self.assertEqual((100, 100), tuple(nw.shape))

        nw = w[:]
        self.assertEqual((784, 100, 100), tuple(nw.shape))

        nw = w[:, :]
        self.assertEqual((784, 100, 100), tuple(nw.shape))

        nw = w[:, :, -1]
        self.assertEqual((784, 100), tuple(nw.shape))

        nw = w[1, 1, 1]

651
        self.assertEqual(len(nw.shape), 0)
652 653 654 655

        nw = w[:, :, :-1]
        self.assertEqual((784, 100, 99), tuple(nw.shape))

656 657 658 659 660 661 662
        tensor_array = np.array(
            [
                [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                [[19, 20, 21], [22, 23, 24], [25, 26, 27]],
            ]
        ).astype('float32')
663
        var = base.dygraph.to_variable(tensor_array)
664 665 666 667 668
        var1 = var[0, 1, 1]
        var2 = var[1:]
        var3 = var[0:1]
        var4 = var[::-1]
        var5 = var[1, 1:, 1:]
669
        var_reshape = paddle.reshape(var, [3, -1, 3])
670 671 672 673 674 675 676 677 678 679
        var6 = var_reshape[:, :, -1]
        var7 = var[:, :, :-1]
        var8 = var[:1, :1, :1]
        var9 = var[:-1, :-1, :-1]
        var10 = var[::-1, :1, :-1]
        var11 = var[:-1, ::-1, -1:]
        var12 = var[1:2, 2:, ::-1]
        var13 = var[2:10, 2:, -2:-1]
        var14 = var[1:-1, 0:2, ::-1]
        var15 = var[::-1, ::-1, ::-1]
680
        var16 = var[-4:4]
681 682
        var17 = var[:, 0, 0:0]
        var18 = var[:, 1:1:2]
683 684

        vars = [
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
            var,
            var1,
            var2,
            var3,
            var4,
            var5,
            var6,
            var7,
            var8,
            var9,
            var10,
            var11,
            var12,
            var13,
            var14,
            var15,
            var16,
            var17,
            var18,
704 705 706
        ]
        local_out = [var.numpy() for var in vars]

707 708 709 710 711 712
        np.testing.assert_array_equal(local_out[1], tensor_array[0, 1, 1:2])
        np.testing.assert_array_equal(local_out[2], tensor_array[1:])
        np.testing.assert_array_equal(local_out[3], tensor_array[0:1])
        np.testing.assert_array_equal(local_out[4], tensor_array[::-1])
        np.testing.assert_array_equal(local_out[5], tensor_array[1, 1:, 1:])
        np.testing.assert_array_equal(
713 714
            local_out[6], tensor_array.reshape((3, -1, 3))[:, :, -1]
        )
715 716 717
        np.testing.assert_array_equal(local_out[7], tensor_array[:, :, :-1])
        np.testing.assert_array_equal(local_out[8], tensor_array[:1, :1, :1])
        np.testing.assert_array_equal(local_out[9], tensor_array[:-1, :-1, :-1])
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
        np.testing.assert_array_equal(
            local_out[10], tensor_array[::-1, :1, :-1]
        )
        np.testing.assert_array_equal(
            local_out[11], tensor_array[:-1, ::-1, -1:]
        )
        np.testing.assert_array_equal(
            local_out[12], tensor_array[1:2, 2:, ::-1]
        )
        np.testing.assert_array_equal(
            local_out[13], tensor_array[2:10, 2:, -2:-1]
        )
        np.testing.assert_array_equal(
            local_out[14], tensor_array[1:-1, 0:2, ::-1]
        )
        np.testing.assert_array_equal(
            local_out[15], tensor_array[::-1, ::-1, ::-1]
        )
736 737 738
        np.testing.assert_array_equal(local_out[16], tensor_array[-4:4])
        np.testing.assert_array_equal(local_out[17], tensor_array[:, 0, 0:0])
        np.testing.assert_array_equal(local_out[18], tensor_array[:, 1:1:2])
739

740
    def _test_slice_for_tensor_attr(self):
741 742 743 744 745 746 747
        tensor_array = np.array(
            [
                [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                [[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                [[19, 20, 21], [22, 23, 24], [25, 26, 27]],
            ]
        ).astype('float32')
748 749 750

        var = paddle.to_tensor(tensor_array)

751 752 753 754
        one = paddle.ones(shape=[], dtype="int32")
        two = paddle.full(shape=[], fill_value=2, dtype="int32")
        negative_one = paddle.full(shape=[], fill_value=-1, dtype="int32")
        four = paddle.full(shape=[], fill_value=4, dtype="int32")
755

756
        var = base.dygraph.to_variable(tensor_array)
757 758 759 760 761
        var1 = var[0, one, one]
        var2 = var[one:]
        var3 = var[0:one]
        var4 = var[::negative_one]
        var5 = var[one, one:, one:]
762
        var_reshape = paddle.reshape(var, [3, negative_one, 3])
763 764 765 766 767 768 769 770 771 772 773 774 775
        var6 = var_reshape[:, :, negative_one]
        var7 = var[:, :, :negative_one]
        var8 = var[:one, :one, :1]
        var9 = var[:-1, :negative_one, :negative_one]
        var10 = var[::negative_one, :one, :negative_one]
        var11 = var[:negative_one, ::-1, negative_one:]
        var12 = var[one:2, 2:, ::negative_one]
        var13 = var[two:10, 2:, -2:negative_one]
        var14 = var[1:negative_one, 0:2, ::negative_one]
        var15 = var[::negative_one, ::-1, ::negative_one]
        var16 = var[-4:4]

        vars = [
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
            var,
            var1,
            var2,
            var3,
            var4,
            var5,
            var6,
            var7,
            var8,
            var9,
            var10,
            var11,
            var12,
            var13,
            var14,
            var15,
            var16,
793 794 795
        ]
        local_out = [var.numpy() for var in vars]

796 797 798 799 800 801
        np.testing.assert_array_equal(local_out[1], tensor_array[0, 1, 1:2])
        np.testing.assert_array_equal(local_out[2], tensor_array[1:])
        np.testing.assert_array_equal(local_out[3], tensor_array[0:1])
        np.testing.assert_array_equal(local_out[4], tensor_array[::-1])
        np.testing.assert_array_equal(local_out[5], tensor_array[1, 1:, 1:])
        np.testing.assert_array_equal(
802 803
            local_out[6], tensor_array.reshape((3, -1, 3))[:, :, -1]
        )
804 805 806
        np.testing.assert_array_equal(local_out[7], tensor_array[:, :, :-1])
        np.testing.assert_array_equal(local_out[8], tensor_array[:1, :1, :1])
        np.testing.assert_array_equal(local_out[9], tensor_array[:-1, :-1, :-1])
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
        np.testing.assert_array_equal(
            local_out[10], tensor_array[::-1, :1, :-1]
        )
        np.testing.assert_array_equal(
            local_out[11], tensor_array[:-1, ::-1, -1:]
        )
        np.testing.assert_array_equal(
            local_out[12], tensor_array[1:2, 2:, ::-1]
        )
        np.testing.assert_array_equal(
            local_out[13], tensor_array[2:10, 2:, -2:-1]
        )
        np.testing.assert_array_equal(
            local_out[14], tensor_array[1:-1, 0:2, ::-1]
        )
        np.testing.assert_array_equal(
            local_out[15], tensor_array[::-1, ::-1, ::-1]
        )
825
        np.testing.assert_array_equal(local_out[16], tensor_array[-4:4])
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    def _test_for_getitem_ellipsis_index(self):
        shape = (64, 3, 5, 256)
        np_fp32_value = np.random.random(shape).astype('float32')
        np_int_value = np.random.randint(1, 100, shape)

        var_fp32 = paddle.to_tensor(np_fp32_value)
        var_int = paddle.to_tensor(np_int_value)

        def assert_getitem_ellipsis_index(var_tensor, var_np):
            var = [
                var_tensor[..., 0].numpy(),
                var_tensor[..., 1, 0].numpy(),
                var_tensor[0, ..., 1, 0].numpy(),
                var_tensor[1, ..., 1].numpy(),
                var_tensor[2, ...].numpy(),
                var_tensor[2, 0, ...].numpy(),
                var_tensor[2, 0, 1, ...].numpy(),
                var_tensor[...].numpy(),
                var_tensor[:, ..., 100].numpy(),
            ]

848 849 850 851 852 853 854 855 856
            np.testing.assert_array_equal(var[0], var_np[..., 0])
            np.testing.assert_array_equal(var[1], var_np[..., 1, 0])
            np.testing.assert_array_equal(var[2], var_np[0, ..., 1, 0])
            np.testing.assert_array_equal(var[3], var_np[1, ..., 1])
            np.testing.assert_array_equal(var[4], var_np[2, ...])
            np.testing.assert_array_equal(var[5], var_np[2, 0, ...])
            np.testing.assert_array_equal(var[6], var_np[2, 0, 1, ...])
            np.testing.assert_array_equal(var[7], var_np[...])
            np.testing.assert_array_equal(var[8], var_np[:, ..., 100])
857 858 859 860 861 862 863

        var_fp32 = paddle.to_tensor(np_fp32_value)
        var_int = paddle.to_tensor(np_int_value)

        assert_getitem_ellipsis_index(var_fp32, np_fp32_value)
        assert_getitem_ellipsis_index(var_int, np_int_value)

864 865
        # test 1 dim tensor
        var_one_dim = paddle.to_tensor([1, 2, 3, 4])
866 867 868
        np.testing.assert_array_equal(
            var_one_dim[..., 0].numpy(), np.array([1])
        )
869

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
    def _test_none_index(self):
        shape = (8, 64, 5, 256)
        np_value = np.random.random(shape).astype('float32')
        var_tensor = paddle.to_tensor(np_value)

        var = [
            var_tensor[1, 0, None].numpy(),
            var_tensor[None, ..., 1, 0].numpy(),
            var_tensor[:, :, :, None].numpy(),
            var_tensor[1, ..., 1, None].numpy(),
            var_tensor[2, ..., None, None].numpy(),
            var_tensor[None, 2, 0, ...].numpy(),
            var_tensor[None, 2, None, 1].numpy(),
            var_tensor[None].numpy(),
            var_tensor[0, 0, None, 0, 0, None].numpy(),
885
            var_tensor[None, None, 0, ..., None].numpy(),
886
            var_tensor[..., None, :, None].numpy(),
887 888 889
            var_tensor[0, 1:10:2, None, None, ...].numpy(),
        ]

890 891 892 893 894 895 896 897 898
        np.testing.assert_array_equal(var[0], np_value[1, 0, None])
        np.testing.assert_array_equal(var[1], np_value[None, ..., 1, 0])
        np.testing.assert_array_equal(var[2], np_value[:, :, :, None])
        np.testing.assert_array_equal(var[3], np_value[1, ..., 1, None])
        np.testing.assert_array_equal(var[4], np_value[2, ..., None, None])
        np.testing.assert_array_equal(var[5], np_value[None, 2, 0, ...])
        np.testing.assert_array_equal(var[6], np_value[None, 2, None, 1])
        np.testing.assert_array_equal(var[7], np_value[None])
        np.testing.assert_array_equal(var[8], np_value[0, 0, None, 0, 0, None])
899 900 901
        np.testing.assert_array_equal(
            var[9], np_value[None, None, 0, ..., None]
        )
902
        np.testing.assert_array_equal(var[10], np_value[..., None, :, None])
903

904 905
        # TODO(zyfncg) there is a bug of dimensions when slice step > 1 and
        #              indexs has int type
906
        # self.assertTrue(
907
        #     np.array_equal(var[11], np_value[0, 1:10:2, None, None, ...]))
908

Z
zyfncg 已提交
909 910 911 912
    def _test_bool_index(self):
        shape = (4, 2, 5, 64)
        np_value = np.random.random(shape).astype('float32')
        var_tensor = paddle.to_tensor(np_value)
913 914 915 916 917 918 919 920 921 922
        index = [
            [True, True, True, True],
            [True, False, True, True],
            [True, False, False, True],
            [False, 0, 1, True, True],
            [False, False, False, False],
        ]
        index2d = np.array(
            [[True, True], [False, False], [True, False], [True, True]]
        )
Z
zyfncg 已提交
923 924
        tensor_index = paddle.to_tensor(index2d)
        var = [
925 926 927 928
            var_tensor[index[0]].numpy(),
            var_tensor[index[1]].numpy(),
            var_tensor[index[2]].numpy(),
            var_tensor[index[3]].numpy(),
Z
zyfncg 已提交
929 930
            var_tensor[paddle.to_tensor(index[0])].numpy(),
            var_tensor[tensor_index].numpy(),
931
            var_tensor[paddle.to_tensor(index[4])].numpy(),
Z
zyfncg 已提交
932
        ]
933 934 935 936 937 938 939
        np.testing.assert_array_equal(var[0], np_value[index[0]])
        np.testing.assert_array_equal(var[1], np_value[index[1]])
        np.testing.assert_array_equal(var[2], np_value[index[2]])
        np.testing.assert_array_equal(var[3], np_value[index[3]])
        np.testing.assert_array_equal(var[4], np_value[index[0]])
        np.testing.assert_array_equal(var[5], np_value[index2d])
        np.testing.assert_array_equal(var[6], np_value[index[4]])
940 941 942 943 944 945
        np.testing.assert_array_equal(
            var_tensor[var_tensor > 0.67], np_value[np_value > 0.67]
        )
        np.testing.assert_array_equal(
            var_tensor[var_tensor < 0.55], np_value[np_value < 0.55]
        )
Z
zyfncg 已提交
946

947
        with self.assertRaises(IndexError):
Z
zyfncg 已提交
948
            var_tensor[[True, False]]
949
        with self.assertRaises(IndexError):
Z
zyfncg 已提交
950 951 952 953
            var_tensor[[True, False, False, False, False]]
        with self.assertRaises(IndexError):
            var_tensor[paddle.to_tensor([[True, False, False, False]])]

954 955 956 957 958 959
    def _test_scalar_bool_index(self):
        shape = (1, 2, 5, 64)
        np_value = np.random.random(shape).astype('float32')
        var_tensor = paddle.to_tensor(np_value)
        index = [True]
        tensor_index = paddle.to_tensor(index)
960 961 962
        var = [
            var_tensor[tensor_index].numpy(),
        ]
963
        np.testing.assert_array_equal(var[0], np_value[index])
964

H
hong 已提交
965 966
    def _test_for_var(self):
        np_value = np.random.random((30, 100, 100)).astype('float32')
967
        w = base.dygraph.to_variable(np_value)
H
hong 已提交
968 969

        for i, e in enumerate(w):
970
            np.testing.assert_array_equal(e.numpy(), np_value[i])
H
hong 已提交
971

972 973 974
    def _test_numpy_index(self):
        array = np.arange(120).reshape([4, 5, 6])
        t = paddle.to_tensor(array)
975 976
        np.testing.assert_array_equal(t[np.longlong(0)].numpy(), array[0])
        np.testing.assert_array_equal(
977 978 979
            t[np.longlong(0) : np.longlong(4) : np.longlong(2)].numpy(),
            array[0:4:2],
        )
980 981
        np.testing.assert_array_equal(t[np.int64(0)].numpy(), array[0])
        np.testing.assert_array_equal(
982 983
            t[np.int32(1) : np.int32(4) : np.int32(2)].numpy(), array[1:4:2]
        )
984
        np.testing.assert_array_equal(
985 986
            t[np.int16(0) : np.int16(4) : np.int16(2)].numpy(), array[0:4:2]
        )
987 988 989 990 991 992

    def _test_list_index(self):
        # case1:
        array = np.arange(120).reshape([6, 5, 4])
        x = paddle.to_tensor(array)
        py_idx = [[0, 2, 0, 1, 3], [0, 0, 1, 2, 0]]
J
JYChen 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

        # note(chenjianye):
        # Non-tuple sequence for multidimensional indexing is supported in numpy < 1.23.
        # For List case, the outermost `[]` will be treated as tuple `()` in version less than 1.23,
        # which is used to wrap index elements for multiple axes.
        # And from 1.23, this will be treat as a whole and only works on one axis.
        #
        # e.g. x[[[0],[1]]] == x[([0],[1])] == x[[0],[1]] (in version < 1.23)
        #      x[[[0],[1]]] == x[array([[0],[1]])] (in version >= 1.23)
        #
        # Here, we just modify the code to remove the impact of numpy version changes,
        # changing x[[[0],[1]]] to x[tuple([[0],[1]])] == x[([0],[1])] == x[[0],[1]].
        # Whether the paddle behavior in this case will change is still up for debate.
1006
        idx = [paddle.to_tensor(py_idx[0]), paddle.to_tensor(py_idx[1])]
J
JYChen 已提交
1007 1008
        np.testing.assert_array_equal(x[idx].numpy(), array[tuple(py_idx)])
        np.testing.assert_array_equal(x[py_idx].numpy(), array[tuple(py_idx)])
1009 1010
        # case2:
        tensor_x = paddle.to_tensor(
1011 1012
            np.zeros(12).reshape(2, 6).astype(np.float32)
        )
1013 1014
        tensor_y1 = paddle.zeros([1], dtype='int32') + 2
        tensor_y2 = paddle.zeros([1], dtype='int32') + 5
1015 1016
        tensor_x[:, tensor_y1:tensor_y2] = 42
        res = tensor_x.numpy()
1017 1018 1019 1020 1021 1022
        exp = np.array(
            [
                [0.0, 0.0, 42.0, 42.0, 42.0, 0.0],
                [0.0, 0.0, 42.0, 42.0, 42.0, 0.0],
            ]
        )
1023
        np.testing.assert_array_equal(res, exp)
1024

W
WeiXin 已提交
1025 1026 1027
        # case3:
        row = np.array([0, 1, 2])
        col = np.array([2, 1, 3])
1028
        np.testing.assert_array_equal(array[row, col], x[row, col].numpy())
W
WeiXin 已提交
1029

1030
    def test_slice(self):
1031
        with base.dygraph.guard():
1032
            self._test_slice()
1033
            self._test_slice_for_tensor_attr()
H
hong 已提交
1034
            self._test_for_var()
1035
            self._test_for_getitem_ellipsis_index()
1036
            self._test_none_index()
Z
zyfncg 已提交
1037
            self._test_bool_index()
1038
            self._test_scalar_bool_index()
1039 1040
            self._test_numpy_index()
            self._test_list_index()
1041

1042
            var = base.dygraph.to_variable(self.array)
1043 1044
            np.testing.assert_array_equal(var[1, :].numpy(), self.array[1, :])
            np.testing.assert_array_equal(var[::-1].numpy(), self.array[::-1])
L
Leo Chen 已提交
1045

H
hong 已提交
1046 1047 1048
            with self.assertRaises(IndexError):
                y = var[self.shape[0]]

1049 1050 1051
            with self.assertRaises(IndexError):
                y = var[0 - self.shape[0] - 1]

W
WeiXin 已提交
1052 1053 1054 1055
            with self.assertRaises(IndexError):
                mask = np.array([1, 0, 1, 0], dtype=bool)
                var[paddle.to_tensor([0, 1]), mask]

1056
    def test_var_base_to_np(self):
1057 1058
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
W
wanghuancoder 已提交
1059
            np.testing.assert_array_equal(var.numpy(), var.numpy(False))
L
Leo Chen 已提交
1060

1061
    def test_var_base_as_np(self):
1062 1063
        with base.dygraph.guard():
            var = base.dygraph.to_variable(self.array)
1064
            np.testing.assert_array_equal(var.numpy(), np.array(var))
1065 1066 1067
            np.testing.assert_array_equal(
                var.numpy(), np.array(var, dtype=np.float32)
            )
1068

1069
    def test_if(self):
1070 1071 1072
        with base.dygraph.guard():
            var1 = base.dygraph.to_variable(np.array([[[0]]]))
            var2 = base.dygraph.to_variable(np.array([[[1]]]))
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

            var1_bool = False
            var2_bool = False

            if var1:
                var1_bool = True

            if var2:
                var2_bool = True

1083 1084 1085 1086
            assert not var1_bool, "if var1 should be false"
            assert var2_bool, "if var2 should be true"
            assert not bool(var1), "bool(var1) is False"
            assert bool(var2), "bool(var2) is True"
1087

1088
    def test_to_static_var(self):
1089
        with base.dygraph.guard():
W
wanghuancoder 已提交
1090
            # Convert Tensor into Variable or Parameter
1091
            var_base = base.dygraph.to_variable(self.array, name="var_base_1")
1092 1093 1094
            static_var = var_base._to_static_var()
            self._assert_to_static(var_base, static_var)

1095
            var_base = base.dygraph.to_variable(self.array, name="var_base_2")
1096 1097 1098
            static_param = var_base._to_static_var(to_parameter=True)
            self._assert_to_static(var_base, static_param, True)

W
wanghuancoder 已提交
1099
            # Convert EagerParamBase into Parameter
1100
            fc = paddle.nn.Linear(
1101 1102
                10,
                20,
1103
                weight_attr=paddle.ParamAttr(
1104 1105
                    learning_rate=0.001,
                    do_model_average=True,
1106
                    regularizer=paddle.regularizer.L1Decay(),
1107 1108
                ),
            )
1109 1110 1111 1112 1113 1114
            weight = fc.parameters()[0]
            static_param = weight._to_static_var()
            self._assert_to_static(weight, static_param, True)

    def _assert_to_static(self, var_base, static_var, is_param=False):
        if is_param:
1115
            self.assertTrue(isinstance(static_var, base.framework.Parameter))
1116
            self.assertTrue(static_var.persistable, True)
1117
            if isinstance(var_base, base.framework.EagerParamBase):
1118
                for attr in ['trainable', 'is_distributed', 'do_model_average']:
1119 1120 1121
                    self.assertEqual(
                        getattr(var_base, attr), getattr(static_var, attr)
                    )
1122

1123 1124 1125
                self.assertEqual(
                    static_var.optimize_attr['learning_rate'], 0.001
                )
1126
                self.assertTrue(
1127
                    isinstance(
1128
                        static_var.regularizer, paddle.regularizer.L1Decay
1129 1130
                    )
                )
1131
        else:
1132
            self.assertTrue(isinstance(static_var, base.framework.Variable))
1133 1134 1135 1136 1137 1138 1139

        attr_keys = ['block', 'dtype', 'type', 'name']
        for attr in attr_keys:
            self.assertEqual(getattr(var_base, attr), getattr(static_var, attr))

        self.assertListEqual(list(var_base.shape), list(static_var.shape))

1140
    def test_tensor_str(self):
Z
Zhou Wei 已提交
1141
        paddle.enable_static()
1142
        paddle.disable_static(paddle.CPUPlace())
C
cnn 已提交
1143
        paddle.seed(10)
1144 1145 1146 1147
        a = paddle.rand([10, 20])
        paddle.set_printoptions(4, 100, 3)
        a_str = str(a)

1148
        expected = '''Tensor(shape=[10, 20], dtype=float32, place=Place(cpu), stop_gradient=True,
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
       [[0.2727, 0.5489, 0.8655, ..., 0.2916, 0.8525, 0.9000],
        [0.3806, 0.8996, 0.0928, ..., 0.9535, 0.8378, 0.6409],
        [0.1484, 0.4038, 0.8294, ..., 0.0148, 0.6520, 0.4250],
        ...,
        [0.3426, 0.1909, 0.7240, ..., 0.4218, 0.2676, 0.5679],
        [0.5561, 0.2081, 0.0676, ..., 0.9778, 0.3302, 0.9559],
        [0.2665, 0.8483, 0.5389, ..., 0.4956, 0.6862, 0.9178]])'''

        self.assertEqual(a_str, expected)

1159
    def test_tensor_str2(self):
1160 1161 1162 1163
        paddle.disable_static(paddle.CPUPlace())
        a = paddle.to_tensor([[1.5111111, 1.0], [0, 0]])
        a_str = str(a)

1164
        expected = '''Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
1165 1166 1167 1168 1169
       [[1.5111, 1.    ],
        [0.    , 0.    ]])'''

        self.assertEqual(a_str, expected)

1170
    def test_tensor_str3(self):
1171 1172 1173 1174
        paddle.disable_static(paddle.CPUPlace())
        a = paddle.to_tensor([[-1.5111111, 1.0], [0, -0.5]])
        a_str = str(a)

1175
        expected = '''Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
1176 1177 1178 1179 1180
       [[-1.5111,  1.    ],
        [ 0.    , -0.5000]])'''

        self.assertEqual(a_str, expected)

1181
    def test_tensor_str_scaler(self):
1182 1183 1184 1185
        paddle.disable_static(paddle.CPUPlace())
        a = paddle.to_tensor(np.array(False))
        a_str = str(a)

1186
        expected = '''Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True,
1187 1188 1189 1190
       False)'''

        self.assertEqual(a_str, expected)

1191
    def test_tensor_str_shape_with_zero(self):
1192 1193
        paddle.disable_static(paddle.CPUPlace())
        x = paddle.ones((10, 10))
1194
        y = paddle.nonzero(x == 0)
1195 1196
        a_str = str(y)

1197
        expected = '''Tensor(shape=[0, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
1198 1199 1200 1201
       [])'''

        self.assertEqual(a_str, expected)

1202
    def test_tensor_str_linewidth(self):
1203 1204 1205
        paddle.disable_static(paddle.CPUPlace())
        paddle.seed(2021)
        x = paddle.rand([128])
1206 1207 1208
        paddle.set_printoptions(
            precision=4, threshold=1000, edgeitems=3, linewidth=80
        )
1209 1210
        a_str = str(x)

1211
        expected = '''Tensor(shape=[128], dtype=float32, place=Place(cpu), stop_gradient=True,
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
       [0.3759, 0.0278, 0.2489, 0.3110, 0.9105, 0.7381, 0.1905, 0.4726, 0.2435,
        0.9142, 0.3367, 0.7243, 0.7664, 0.9915, 0.2921, 0.1363, 0.8096, 0.2915,
        0.9564, 0.9972, 0.2573, 0.2597, 0.3429, 0.2484, 0.9579, 0.7003, 0.4126,
        0.4274, 0.0074, 0.9686, 0.9910, 0.0144, 0.6564, 0.2932, 0.7114, 0.9301,
        0.6421, 0.0538, 0.1273, 0.5771, 0.9336, 0.6416, 0.1832, 0.9311, 0.7702,
        0.7474, 0.4479, 0.3382, 0.5579, 0.0444, 0.9802, 0.9874, 0.3038, 0.5640,
        0.2408, 0.5489, 0.8866, 0.1006, 0.5881, 0.7560, 0.7928, 0.8604, 0.4670,
        0.9285, 0.1482, 0.4541, 0.1307, 0.6221, 0.4902, 0.1147, 0.4415, 0.2987,
        0.7276, 0.2077, 0.7551, 0.9652, 0.4369, 0.2282, 0.0047, 0.2934, 0.4308,
        0.4190, 0.1442, 0.3650, 0.3056, 0.6535, 0.1211, 0.8721, 0.7408, 0.4220,
        0.5937, 0.3123, 0.9198, 0.0275, 0.5338, 0.4622, 0.7521, 0.3609, 0.4703,
        0.1736, 0.8976, 0.7616, 0.3756, 0.2416, 0.2907, 0.3246, 0.4305, 0.5717,
        0.0735, 0.0361, 0.5534, 0.4399, 0.9260, 0.6525, 0.3064, 0.4573, 0.9210,
        0.8269, 0.2424, 0.7494, 0.8945, 0.7098, 0.8078, 0.4707, 0.5715, 0.7232,
        0.4678, 0.5047])'''

        self.assertEqual(a_str, expected)

1230
    def test_tensor_str_linewidth2(self):
1231 1232 1233 1234 1235 1236
        paddle.disable_static(paddle.CPUPlace())
        paddle.seed(2021)
        x = paddle.rand([128])
        paddle.set_printoptions(precision=4, linewidth=160, sci_mode=True)
        a_str = str(x)

1237
        expected = '''Tensor(shape=[128], dtype=float32, place=Place(cpu), stop_gradient=True,
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
       [3.7587e-01, 2.7798e-02, 2.4891e-01, 3.1097e-01, 9.1053e-01, 7.3811e-01, 1.9045e-01, 4.7258e-01, 2.4354e-01, 9.1415e-01, 3.3666e-01, 7.2428e-01,
        7.6640e-01, 9.9146e-01, 2.9215e-01, 1.3625e-01, 8.0957e-01, 2.9153e-01, 9.5642e-01, 9.9718e-01, 2.5732e-01, 2.5973e-01, 3.4292e-01, 2.4841e-01,
        9.5794e-01, 7.0029e-01, 4.1260e-01, 4.2737e-01, 7.3788e-03, 9.6863e-01, 9.9102e-01, 1.4416e-02, 6.5640e-01, 2.9318e-01, 7.1136e-01, 9.3008e-01,
        6.4209e-01, 5.3849e-02, 1.2730e-01, 5.7712e-01, 9.3359e-01, 6.4155e-01, 1.8320e-01, 9.3110e-01, 7.7021e-01, 7.4736e-01, 4.4793e-01, 3.3817e-01,
        5.5794e-01, 4.4412e-02, 9.8023e-01, 9.8735e-01, 3.0376e-01, 5.6397e-01, 2.4082e-01, 5.4893e-01, 8.8659e-01, 1.0065e-01, 5.8812e-01, 7.5600e-01,
        7.9280e-01, 8.6041e-01, 4.6701e-01, 9.2852e-01, 1.4821e-01, 4.5410e-01, 1.3074e-01, 6.2210e-01, 4.9024e-01, 1.1466e-01, 4.4154e-01, 2.9868e-01,
        7.2758e-01, 2.0766e-01, 7.5508e-01, 9.6522e-01, 4.3688e-01, 2.2823e-01, 4.7394e-03, 2.9342e-01, 4.3083e-01, 4.1902e-01, 1.4416e-01, 3.6500e-01,
        3.0560e-01, 6.5350e-01, 1.2115e-01, 8.7206e-01, 7.4081e-01, 4.2203e-01, 5.9372e-01, 3.1230e-01, 9.1979e-01, 2.7486e-02, 5.3383e-01, 4.6224e-01,
        7.5211e-01, 3.6094e-01, 4.7034e-01, 1.7355e-01, 8.9763e-01, 7.6165e-01, 3.7557e-01, 2.4157e-01, 2.9074e-01, 3.2458e-01, 4.3049e-01, 5.7171e-01,
        7.3509e-02, 3.6087e-02, 5.5341e-01, 4.3993e-01, 9.2601e-01, 6.5248e-01, 3.0640e-01, 4.5727e-01, 9.2104e-01, 8.2688e-01, 2.4243e-01, 7.4937e-01,
        8.9448e-01, 7.0981e-01, 8.0783e-01, 4.7065e-01, 5.7154e-01, 7.2319e-01, 4.6777e-01, 5.0465e-01])'''

        self.assertEqual(a_str, expected)

1252
    def test_tensor_str_bf16(self):
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
        paddle.disable_static(paddle.CPUPlace())
        a = paddle.to_tensor([[1.5, 1.0], [0, 0]])
        a = paddle.cast(a, dtype=core.VarDesc.VarType.BF16)
        paddle.set_printoptions(precision=4)
        a_str = str(a)

        expected = '''Tensor(shape=[2, 2], dtype=bfloat16, place=Place(cpu), stop_gradient=True,
       [[1.5000, 1.    ],
        [0.    , 0.    ]])'''

        self.assertEqual(a_str, expected)

1265
    def test_print_tensor_dtype(self):
L
Leo Chen 已提交
1266 1267 1268 1269 1270 1271 1272
        paddle.disable_static(paddle.CPUPlace())
        a = paddle.rand([1])
        a_str = str(a.dtype)

        expected = 'paddle.float32'

        self.assertEqual(a_str, expected)
1273

L
Leo Chen 已提交
1274

1275
class TestVarBaseSetitem(unittest.TestCase):
1276
    def func_setUp(self):
1277 1278 1279
        self.set_dtype()
        self.tensor_x = paddle.to_tensor(np.ones((4, 2, 3)).astype(self.dtype))
        self.np_value = np.random.random((2, 3)).astype(self.dtype)
1280 1281
        self.tensor_value = paddle.to_tensor(self.np_value)

1282 1283 1284
    def set_dtype(self):
        self.dtype = "int32"

1285
    def _test(self, value):
1286
        id_origin = id(self.tensor_x)
1287
        self.tensor_x[0] = value
1288
        if isinstance(value, (int, float)):
1289
            result = np.zeros((2, 3)).astype(self.dtype) + value
1290 1291 1292 1293

        else:
            result = self.np_value

1294
        np.testing.assert_array_equal(self.tensor_x[0].numpy(), result)
1295 1296 1297
        self.assertEqual(id_origin, id(self.tensor_x))

        self.tensor_x[1:2] = value
1298
        np.testing.assert_array_equal(self.tensor_x[1].numpy(), result)
1299 1300 1301
        self.assertEqual(id_origin, id(self.tensor_x))

        self.tensor_x[...] = value
1302
        np.testing.assert_array_equal(self.tensor_x[3].numpy(), result)
1303 1304
        self.assertEqual(id_origin, id(self.tensor_x))

W
wanghuancoder 已提交
1305
    def func_test_value_tensor(self):
1306 1307
        self._test(self.tensor_value)

W
wanghuancoder 已提交
1308
    def test_value_tensor(self):
1309
        self.func_setUp()
W
wanghuancoder 已提交
1310 1311 1312
        self.func_test_value_tensor()

    def func_test_value_numpy(self):
1313 1314
        self._test(self.np_value)

W
wanghuancoder 已提交
1315
    def test_value_numpy(self):
1316
        self.func_setUp()
W
wanghuancoder 已提交
1317 1318 1319
        self.func_test_value_numpy()

    def func_test_value_int(self):
1320 1321
        self._test(10)

W
wanghuancoder 已提交
1322
    def test_value_int(self):
1323
        self.func_setUp()
W
wanghuancoder 已提交
1324 1325
        self.func_test_value_int()

1326 1327 1328 1329 1330 1331 1332 1333 1334 1335

class TestVarBaseSetitemInt64(TestVarBaseSetitem):
    def set_dtype(self):
        self.dtype = "int64"


class TestVarBaseSetitemFp32(TestVarBaseSetitem):
    def set_dtype(self):
        self.dtype = "float32"

1336
    def func_test_value_float(self):
1337 1338 1339
        paddle.disable_static()
        self._test(3.3)

1340 1341 1342 1343
    def test_value_float(self):
        self.func_setUp()
        self.func_test_value_float()

1344

1345 1346 1347 1348 1349
class TestVarBaseSetitemFp64(TestVarBaseSetitem):
    def set_dtype(self):
        self.dtype = "float64"


1350
class TestVarBaseSetitemBoolIndex(unittest.TestCase):
1351
    def func_setUp(self):
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
        paddle.disable_static()
        self.set_dtype()
        self.set_input()

    def set_input(self):
        self.tensor_x = paddle.to_tensor(np.ones((4, 2, 3)).astype(self.dtype))
        self.np_value = np.random.random((2, 3)).astype(self.dtype)
        self.tensor_value = paddle.to_tensor(self.np_value)

    def set_dtype(self):
        self.dtype = "int32"

    def _test(self, value):
        paddle.disable_static()

        id_origin = id(self.tensor_x)
        index_1 = paddle.to_tensor(np.array([True, False, False, False]))
        self.tensor_x[index_1] = value

1371
        if isinstance(value, (int, float)):
1372 1373 1374 1375 1376
            result = np.zeros((2, 3)).astype(self.dtype) + value

        else:
            result = self.np_value

1377
        np.testing.assert_array_equal(self.tensor_x[0].numpy(), result)
1378 1379 1380 1381
        self.assertEqual(id_origin, id(self.tensor_x))

        index_2 = paddle.to_tensor(np.array([False, True, False, False]))
        self.tensor_x[index_2] = value
1382
        np.testing.assert_array_equal(self.tensor_x[1].numpy(), result)
1383 1384 1385 1386
        self.assertEqual(id_origin, id(self.tensor_x))

        index_3 = paddle.to_tensor(np.array([True, True, True, True]))
        self.tensor_x[index_3] = value
1387
        np.testing.assert_array_equal(self.tensor_x[3].numpy(), result)
1388 1389
        self.assertEqual(id_origin, id(self.tensor_x))

1390
    def func_test_value_tensor(self):
1391 1392 1393
        paddle.disable_static()
        self._test(self.tensor_value)

1394 1395 1396 1397 1398
    def test_value_tensor(self):
        self.func_setUp()
        self.func_test_value_tensor()

    def func_test_value_numpy(self):
1399 1400 1401
        paddle.disable_static()
        self._test(self.np_value)

1402 1403 1404 1405 1406
    def test_value_numpy(self):
        self.func_setUp()
        self.func_test_value_numpy()

    def func_test_value_int(self):
1407 1408 1409
        paddle.disable_static()
        self._test(10)

1410 1411 1412 1413
    def test_value_int(self):
        self.func_setUp()
        self.func_test_value_int()

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429

class TestVarBaseSetitemBoolScalarIndex(unittest.TestCase):
    def set_input(self):
        self.tensor_x = paddle.to_tensor(np.ones((1, 2, 3)).astype(self.dtype))
        self.np_value = np.random.random((2, 3)).astype(self.dtype)
        self.tensor_value = paddle.to_tensor(self.np_value)

    def _test(self, value):
        paddle.disable_static()
        self.assertEqual(self.tensor_x.inplace_version, 0)

        id_origin = id(self.tensor_x)
        index = paddle.to_tensor(np.array([True]))
        self.tensor_x[index] = value
        self.assertEqual(self.tensor_x.inplace_version, 1)

1430
        if isinstance(value, (int, float)):
1431 1432 1433 1434 1435
            result = np.zeros((2, 3)).astype(self.dtype) + value

        else:
            result = self.np_value

1436
        np.testing.assert_array_equal(self.tensor_x[0].numpy(), result)
1437 1438 1439
        self.assertEqual(id_origin, id(self.tensor_x))


1440
class TestVarBaseInplaceVersion(unittest.TestCase):
1441
    def test_setitem(self):
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
        paddle.disable_static()

        var = paddle.ones(shape=[4, 2, 3], dtype="float32")
        self.assertEqual(var.inplace_version, 0)

        var[1] = 1
        self.assertEqual(var.inplace_version, 1)

        var[1:2] = 1
        self.assertEqual(var.inplace_version, 2)

1453
    def test_bump_inplace_version(self):
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
        paddle.disable_static()
        var = paddle.ones(shape=[4, 2, 3], dtype="float32")
        self.assertEqual(var.inplace_version, 0)

        var._bump_inplace_version()
        self.assertEqual(var.inplace_version, 1)

        var._bump_inplace_version()
        self.assertEqual(var.inplace_version, 2)


1465
class TestVarBaseSlice(unittest.TestCase):
1466
    def test_slice(self):
1467 1468 1469 1470 1471 1472 1473 1474 1475
        paddle.disable_static()
        np_x = np.random.random((3, 8, 8))
        x = paddle.to_tensor(np_x, dtype="float64")
        actual_x = x._slice(0, 1)
        actual_x = paddle.to_tensor(actual_x)
        self.assertEqual(actual_x.numpy().all(), np_x[0:1].all())


class TestVarBaseClear(unittest.TestCase):
1476
    def test_clear(self):
1477 1478 1479 1480 1481 1482 1483 1484
        paddle.disable_static()
        np_x = np.random.random((3, 8, 8))
        x = paddle.to_tensor(np_x, dtype="float64")
        x._clear()
        self.assertEqual(str(x), "Tensor(Not initialized)")


class TestVarBaseOffset(unittest.TestCase):
1485
    def test_offset(self):
1486 1487 1488 1489 1490 1491 1492 1493 1494
        paddle.disable_static()
        np_x = np.random.random((3, 8, 8))
        x = paddle.to_tensor(np_x, dtype="float64")
        expected_offset = 0
        actual_x = x._slice(expected_offset, 1)
        actual_x = paddle.to_tensor(actual_x)
        self.assertEqual(actual_x._offset(), expected_offset)


1495
class TestVarBaseShareBufferTo(unittest.TestCase):
1496
    def test_share_buffer_To(self):
1497
        paddle.disable_static()
1498 1499 1500
        np_src = np.random.random((3, 8, 8))
        src = paddle.to_tensor(np_src, dtype="float64")
        # empty_var
姜永久 已提交
1501
        dst = core.eager.Tensor()
1502 1503
        src._share_buffer_to(dst)
        self.assertEqual(src._is_shared_buffer_with(dst), True)
1504 1505 1506


class TestVarBaseTo(unittest.TestCase):
1507
    def func_setUp(self):
1508 1509 1510 1511
        paddle.disable_static()
        self.np_x = np.random.random((3, 8, 8))
        self.x = paddle.to_tensor(self.np_x, dtype="float32")

1512
    def func_test_to_api(self):
1513
        x_double = self.x._to(dtype='double')
1514
        self.assertEqual(x_double.dtype, paddle.base.core.VarDesc.VarType.FP64)
1515
        np.testing.assert_allclose(self.np_x, x_double, rtol=1e-05)
1516 1517

        x_ = self.x._to()
1518
        self.assertEqual(self.x.dtype, paddle.base.core.VarDesc.VarType.FP64)
1519
        np.testing.assert_allclose(self.np_x, x_, rtol=1e-05)
1520

1521
        if paddle.base.is_compiled_with_cuda():
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
            x_gpu = self.x._to(device=paddle.CUDAPlace(0))
            self.assertTrue(x_gpu.place.is_gpu_place())
            self.assertEqual(x_gpu.place.gpu_device_id(), 0)

            x_gpu0 = self.x._to(device='gpu:0')
            self.assertTrue(x_gpu0.place.is_gpu_place())
            self.assertEqual(x_gpu0.place.gpu_device_id(), 0)

            x_gpu1 = self.x._to(device='gpu:0', dtype="float64")
            self.assertTrue(x_gpu1.place.is_gpu_place())
            self.assertEqual(x_gpu1.place.gpu_device_id(), 0)
1533
            self.assertEqual(
1534
                x_gpu1.dtype, paddle.base.core.VarDesc.VarType.FP64
1535
            )
1536 1537 1538 1539

            x_gpu2 = self.x._to(device='gpu:0', dtype="float16")
            self.assertTrue(x_gpu2.place.is_gpu_place())
            self.assertEqual(x_gpu2.place.gpu_device_id(), 0)
1540
            self.assertEqual(
1541
                x_gpu2.dtype, paddle.base.core.VarDesc.VarType.FP16
1542
            )
1543 1544 1545 1546 1547 1548 1549 1550 1551

        x_cpu = self.x._to(device=paddle.CPUPlace())
        self.assertTrue(x_cpu.place.is_cpu_place())

        x_cpu0 = self.x._to(device='cpu')
        self.assertTrue(x_cpu0.place.is_cpu_place())

        x_cpu1 = self.x._to(device=paddle.CPUPlace(), dtype="float64")
        self.assertTrue(x_cpu1.place.is_cpu_place())
1552
        self.assertEqual(x_cpu1.dtype, paddle.base.core.VarDesc.VarType.FP64)
1553 1554 1555

        x_cpu2 = self.x._to(device='cpu', dtype="float16")
        self.assertTrue(x_cpu2.place.is_cpu_place())
1556
        self.assertEqual(x_cpu2.dtype, paddle.base.core.VarDesc.VarType.FP16)
1557 1558 1559 1560

        self.assertRaises(ValueError, self.x._to, device=1)
        self.assertRaises(AssertionError, self.x._to, blocking=1)

1561 1562 1563 1564
    def test_to_api(self):
        self.func_setUp()
        self.func_test_to_api()

1565 1566

class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase):
1567
    def test_varbase_init(self):
1568
        paddle.disable_static()
1569
        t = base.Tensor()
1570
        np_x = np.random.random((3, 8, 8))
1571
        t.set(np_x, base.CPUPlace())
1572

1573
        if paddle.base.is_compiled_with_cuda():
1574
            device = paddle.CUDAPlace(0)
1575
            tmp = base.core.eager.Tensor(t, device)
1576 1577 1578 1579
            self.assertTrue(tmp.place.is_gpu_place())
            self.assertEqual(tmp.numpy().all(), np_x.all())

        device = paddle.CPUPlace()
1580
        tmp = base.core.eager.Tensor(t, device)
1581 1582 1583 1584
        self.assertEqual(tmp.numpy().all(), np_x.all())


class TestVarBaseNumel(unittest.TestCase):
1585
    def test_numel_normal(self):
1586 1587 1588 1589 1590 1591 1592
        paddle.disable_static()
        np_x = np.random.random((3, 8, 8))
        x = paddle.to_tensor(np_x, dtype="float64")
        x_actual_numel = x._numel()
        x_expected_numel = np.product((3, 8, 8))
        self.assertEqual(x_actual_numel, x_expected_numel)

1593
    def test_numel_without_holder(self):
1594
        paddle.disable_static()
姜永久 已提交
1595
        x_without_holder = core.eager.Tensor()
1596 1597 1598
        x_actual_numel = x_without_holder._numel()
        self.assertEqual(x_actual_numel, 0)

1599 1600

class TestVarBaseCopyGradientFrom(unittest.TestCase):
1601
    def test_copy_gradient_from(self):
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
        paddle.disable_static()
        np_x = np.random.random((2, 2))
        np_y = np.random.random((2, 2))
        x = paddle.to_tensor(np_x, dtype="float64", stop_gradient=False)
        y = paddle.to_tensor(np_y, dtype="float64")
        out = x + x
        out.backward()
        x._copy_gradient_from(y)
        self.assertEqual(x.grad.numpy().all(), np_y.all())


1613 1614
class TestEagerTensorGradNameValue(unittest.TestCase):
    def test_eager_tensor_grad_name_value(self):
1615 1616 1617 1618 1619 1620 1621 1622
        a_np = np.array([2, 3]).astype('float32')
        a = paddle.to_tensor(a_np)
        a.stop_gradient = False
        b = a**2
        self.assertIsNone(a._grad_value())
        b.backward()
        # Note, for new dygraph, there are no generated grad name, so we skip the name check.
        self.assertIsNotNone(a._grad_value())
1623 1624


L
Leo Chen 已提交
1625 1626
if __name__ == '__main__':
    unittest.main()