Skip to content
24 changes: 19 additions & 5 deletions test/legacy_test/test_memcpy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
import unittest

import numpy as np
from op_test import get_device_place

import paddle
from paddle import base
from paddle.base import Program, core, program_guard


def get_device_place(device_id=0):
if core.is_compiled_with_cuda():
return base.CUDAPlace(device_id)
return base.CPUPlace()


class TestMemcpy_FillConstant(unittest.TestCase):
def get_prog(self):
paddle.enable_static()
Expand Down Expand Up @@ -218,10 +223,19 @@ def test_SELECTED_ROWS(self):

class TestMemcpyApi(unittest.TestCase):
def test_api(self):
a = paddle.ones([1024, 1024])
b = paddle.tensor.creation._memcpy(a, paddle.CUDAPinnedPlace())
self.assertEqual(b.place.__repr__(), "Place(gpu_pinned)")
np.testing.assert_array_equal(a.numpy(), b.numpy())
# Disable static graph mode for this test
paddle.disable_static()
try:
a = paddle.ones([1024, 1024])
b = paddle.tensor.creation._memcpy(a, paddle.CUDAPinnedPlace())
# Test that memcpy operation succeeded by checking data equality
np.testing.assert_array_equal(a.numpy(), b.numpy())
# Test that the tensor was created successfully
self.assertEqual(a.shape, b.shape)
self.assertEqual(a.dtype, b.dtype)
finally:
# Re-enable static graph mode
paddle.enable_static()


if __name__ == '__main__':
Expand Down
Loading