diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index 61bdbdafa812c06f80c0c7958df2bf30af495141..3e7e49fcf45d5631ac3550ff5e707c61b00898f4 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 8e62d040041702e28386ed4d751722d8c3f81635..a2f0bfa6515b6b3c3e284572237709dbf105f578 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():