From d772166cd57227fddf1c53defe2e9d3c8fcb0349 Mon Sep 17 00:00:00 2001 From: Siming Dai <908660116@qq.com> Date: Thu, 22 Sep 2022 14:23:59 +0800 Subject: [PATCH] Fix UVA Tensor (#46371) --- paddle/fluid/pybind/tensor_py.h | 9 +++++---- .../paddle/fluid/tests/unittests/test_tensor_uva.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/pybind/tensor_py.h b/paddle/fluid/pybind/tensor_py.h index 4b01f2b568b..f8e2d14a798 100644 --- a/paddle/fluid/pybind/tensor_py.h +++ b/paddle/fluid/pybind/tensor_py.h @@ -608,9 +608,10 @@ void SetStringTensorFromPyArray(phi::StringTensor *self, } template -void SetUVATensorFromPyArrayImpl(framework::LoDTensor *self_tensor, - const py::array_t &array, - int device_id) { +void SetUVATensorFromPyArrayImpl( + framework::LoDTensor *self_tensor, + const py::array_t &array, + int device_id) { #if defined(PADDLE_WITH_CUDA) VLOG(4) << "Running in SetUVATensorFromPyArrayImpl."; std::vector dims; @@ -647,7 +648,7 @@ void SetUVATensorFromPyArrayImpl(framework::LoDTensor *self_tensor, template void SetUVATensorFromPyArray( const std::shared_ptr &self, - const py::array_t &array, + const py::array_t &array, int device_id) { #if defined(PADDLE_WITH_CUDA) VLOG(4) << "Running in SetUVATensorFromPyArray for VarBase."; diff --git a/python/paddle/fluid/tests/unittests/test_tensor_uva.py b/python/paddle/fluid/tests/unittests/test_tensor_uva.py index 8133ce50567..1261cf41b79 100644 --- a/python/paddle/fluid/tests/unittests/test_tensor_uva.py +++ b/python/paddle/fluid/tests/unittests/test_tensor_uva.py @@ -57,6 +57,17 @@ class TestUVATensorFromNumpy(unittest.TestCase): np.testing.assert_allclose(tensor.numpy(), data, rtol=1e-05) np.testing.assert_allclose(tensor2.numpy(), data, rtol=1e-05) + def test_uva_tensor_corectness(self): + if paddle.fluid.core.is_compiled_with_cuda(): + a = np.arange(0, 100, dtype="int32") + a = a.reshape([10, 10]) + slice_a = a[:, 5] + tensor1 = paddle.to_tensor(slice_a) + tensor2 = core.eager.to_uva_tensor(slice_a) + np.testing.assert_allclose(tensor1.numpy(), + tensor2.numpy(), + rtol=1e-05) + def test_uva_tensor_creation(self): with _test_eager_guard(): self.func_uva_tensor_creation() -- GitLab