diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 0e8bf1d0f886195852ac4d1ce02e57b90e57a9cc..94756a41f850369c7d062cf828ded3d66f5bb099 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -759,8 +759,10 @@ static PyObject* tensor__getitem_index_not_tensor(TensorObject* self, decrease_axis, none_axes, infer_flags, list_select_idxs; // if index is a list, list_select_flag will be true bool list_select_flag = false; + // Note(0x45f): Using defined() instead of initialized() + // to support slice tensor which shape like [0, 0, 0]. PADDLE_ENFORCE_EQ( - self->tensor.initialized(), + self->tensor.defined(), true, platform::errors::InvalidArgument( "tensor %s has not been initialized, we can only slice initialized " diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py index 3a7a1dc1b0eaefc41a5aa23e2f4b992ef8ee39df..6d8f0b14407847b96bdb749bd4905c2976483886 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py @@ -284,5 +284,23 @@ class TestPaddleStridedSlice(unittest.TestCase): np.testing.assert_array_equal(sl.numpy(), array_slice) +def slice_zero_shape_tensor(x): + y = x[1:2] + return y + + +class TestSliceZeroShapeTensor(unittest.TestCase): + + def test_slice(self): + paddle.disable_static() + x = paddle.ones([0, 0, 0, 0]) + y = slice_zero_shape_tensor(x) + np.testing.assert_equal(y.shape, [0, 0, 0, 0]) + + static_func = paddle.jit.to_static(slice_zero_shape_tensor) + y = static_func(x) + np.testing.assert_equal(y.shape, [0, 0, 0, 0]) + + if __name__ == '__main__': unittest.main()