From 75d306ffeb1e5a990d9974f862622ef86df0aceb Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Mon, 8 Aug 2022 11:03:37 +0800 Subject: [PATCH] [Eager] fix to_uva_tensor without specific gpu number (#44937) * [Eager] fix to_uva_tensor without specific gpu number * Update test_tensor_uva.py update test case --- paddle/fluid/pybind/eager_functions.cc | 11 +++++++---- .../paddle/fluid/tests/unittests/test_tensor_uva.py | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index 61bdbdafa8..3e7e49fcf4 100644 --- a/paddle/fluid/pybind/eager_functions.cc +++ b/paddle/fluid/pybind/eager_functions.cc @@ -870,10 +870,13 @@ static PyObject* eager_api_to_uva_tensor(PyObject* self, PyObject* obj = PyTuple_GET_ITEM(args, 0); auto array = py::cast(py::handle(obj)); - int device_id = 0; - PyObject* Py_device_id = PyTuple_GET_ITEM(args, 1); - if (Py_device_id) { - device_id = CastPyArg2AttrLong(Py_device_id, 1); + Py_ssize_t args_num = PyTuple_Size(args); + int64_t device_id = 0; + if (args_num > 1) { + PyObject* Py_device_id = PyTuple_GET_ITEM(args, 1); + if (Py_device_id) { + device_id = CastPyArg2AttrLong(Py_device_id, 1); + } } if (py::isinstance>(array)) { diff --git a/python/paddle/fluid/tests/unittests/test_tensor_uva.py b/python/paddle/fluid/tests/unittests/test_tensor_uva.py index 8e62d04004..a2f0bfa651 100644 --- a/python/paddle/fluid/tests/unittests/test_tensor_uva.py +++ b/python/paddle/fluid/tests/unittests/test_tensor_uva.py @@ -47,10 +47,15 @@ class TestUVATensorFromNumpy(unittest.TestCase): data = np.random.randint(10, size=[4, 5]).astype(dtype) if _in_legacy_dygraph(): tensor = paddle.fluid.core.to_uva_tensor(data, 0) + tensor2 = paddle.fluid.core.to_uva_tensor(data) else: tensor = core.eager.to_uva_tensor(data, 0) + tensor2 = core.eager.to_uva_tensor(data) + self.assertTrue(tensor.place.is_gpu_place()) + self.assertTrue(tensor2.place.is_gpu_place()) self.assertTrue(np.allclose(tensor.numpy(), data)) + self.assertTrue(np.allclose(tensor2.numpy(), data)) def test_uva_tensor_creation(self): with _test_eager_guard(): -- GitLab