diff --git a/paddle/fluid/pybind/eager.cc b/paddle/fluid/pybind/eager.cc index fa66e55e9c53a1aaaf7d1016bdf1a9ecfda6342f..8695928205bb0fabdf45e314eb6cae9391844962 100644 --- a/paddle/fluid/pybind/eager.cc +++ b/paddle/fluid/pybind/eager.cc @@ -62,7 +62,7 @@ void EmptyTensorInitializer(TensorObject* self, const std::string& name, bool persistable = false, int stop_gradient = -1, framework::proto::VarType::Type dtype = paddle::framework::proto::VarType::FP32, - const std::vector& dims = {}, + const std::vector& dims = {0}, framework::proto::VarType::Type var_type = paddle::framework::proto::VarType::LOD_TENSOR) { auto ddims = phi::make_ddim(dims); @@ -75,7 +75,7 @@ void EmptyTensorInitializer(TensorObject* self, const std::string& name, if (var_type == paddle::framework::proto::VarType::LOD_TENSOR) { // TODO(jiabin): Maybe support LOD later std::shared_ptr dense_tensor = nullptr; - if (dims.empty()) { + if (dims.size() == 1 && dims[0] == 0) { std::shared_ptr allocation_ptr = nullptr; dense_tensor = std::make_shared( allocation_ptr, diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 17908e80de4192d647f98269669c30624d6d4b96..ca757fac9a6e3d92b5af89252347da2dc7c03f61 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -195,6 +195,17 @@ static PyObject* tensor_method_numpy(TensorObject* self, PyObject* args, nullptr); if (!self->tensor.impl()->initialized()) { + if (tensor_dims.size() == 0) { + py_dims[0] = 0; + py_strides[0] = 0; + PyObject* array = api.PyArray_NewFromDescr_( + api.PyArray_Type_, api.PyArray_DescrFromType_(numpy_dtype), 1, + py_dims, py_strides, nullptr, + pybind11::detail::npy_api::NPY_ARRAY_ALIGNED_ | + pybind11::detail::npy_api::NPY_ARRAY_WRITEABLE_, + nullptr); + return array; + } return array; } diff --git a/paddle/phi/api/lib/tensor.cc b/paddle/phi/api/lib/tensor.cc index 67c1b711fc9977e334fe4c720bb7e931f4f7839c..f1aa48a2a4d977a9041016bfcfcddd387df43618 100644 --- a/paddle/phi/api/lib/tensor.cc +++ b/paddle/phi/api/lib/tensor.cc @@ -114,9 +114,6 @@ phi::DDim Tensor::dims() const { return impl_->dims(); } std::vector Tensor::shape() const { auto dims = impl_->dims(); - if (dims.size() == 1 && dims.at(0) == 0) { - return {}; - } return phi::vectorize(dims); } diff --git a/python/paddle/fluid/tests/unittests/test_egr_python_api.py b/python/paddle/fluid/tests/unittests/test_egr_python_api.py index 7e78b223b3f6acb57d2d55fc4fb41695439ac930..600a49b2332beed17954803e87cf57e6cd0bdbfc 100644 --- a/python/paddle/fluid/tests/unittests/test_egr_python_api.py +++ b/python/paddle/fluid/tests/unittests/test_egr_python_api.py @@ -114,7 +114,7 @@ class EagerVariablePropertiesAndMethodsTestCase(unittest.TestCase): egr_tensor = core.eager.Tensor() self.assertEqual(egr_tensor.persistable, False) self.assertTrue("generated" in egr_tensor.name) - self.assertEqual(egr_tensor.shape, []) + self.assertEqual(egr_tensor.shape, [0]) self.assertEqual(egr_tensor.dtype, core.VarDesc.VarType.FP32) self.assertEqual(egr_tensor.stop_gradient, True) diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 724a71ebe3dda5adcb989372924051ae09da73e5..2729aabf604bbed3827f834ed9e7a86e03d96530 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -214,6 +214,13 @@ class TestVarBase(unittest.TestCase): self.assertEqual(x.item(), 1 + 1j) self.assertTrue(isinstance(x.item(), complex)) + # 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) + self.assertTrue(np.array_equal(x.numpy(), expected_result)) + numpy_array = np.random.randn(3, 4) # covert core.LoDTensor to paddle.Tensor lod_tensor = paddle.fluid.core.LoDTensor()