From 3cf50f91472544b7560241b1a2ee9d9155175997 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 1 Feb 2023 17:11:01 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20=E7=A9=BA=E6=8C=87=E9=92=88=20(Null=20poi?= =?UTF-8?q?nter)=20of=20case8:=20paddle.slice=20(#49979)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add check for input of slice * add unittest --- paddle/phi/infermeta/unary.cc | 15 +++++++++++++ .../fluid/tests/unittests/test_slice_op.py | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 2b35545db1c..f2fcb316208 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -3395,6 +3395,21 @@ void SliceRawInferMeta(const MetaTensor& input, } } + PADDLE_ENFORCE_EQ( + axes.size(), + starts_arr.size(), + phi::errors::InvalidArgument( + "The length of axes (%d) and length of starts (%d) should be same.", + axes.size(), + starts_arr.size())); + PADDLE_ENFORCE_EQ( + axes.size(), + ends_arr.size(), + phi::errors::InvalidArgument( + "The length of axes (%d) and length of ends (%d) should be same.", + axes.size(), + ends_arr.size())); + // 2.1 Check attrs. std::vector starts = starts_arr.GetData(); std::vector ends = ends_arr.GetData(); diff --git a/python/paddle/fluid/tests/unittests/test_slice_op.py b/python/paddle/fluid/tests/unittests/test_slice_op.py index 19aa669badf..157818e7943 100644 --- a/python/paddle/fluid/tests/unittests/test_slice_op.py +++ b/python/paddle/fluid/tests/unittests/test_slice_op.py @@ -852,6 +852,27 @@ class TestInferShape(unittest.TestCase): paddle.slice(x, 0, starts, ends) +class TestSliceOpError(unittest.TestCase): + def test_dismatch_shape(self): + with fluid.dygraph.guard(): + with self.assertRaises(ValueError): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor(np.reshape(array, [0]), dtype='float32') + paddle.slice(x, axes=[0], starts=[], ends=[]) + + with self.assertRaises(ValueError): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor(np.reshape(array, [0]), dtype='float32') + paddle.slice(x, axes=[0], starts=[0], ends=[]) + + # if shape match, pass + array = np.array([], dtype=np.float32) + x = paddle.to_tensor(np.reshape(array, [0]), dtype='float32') + out = paddle.slice(x, axes=[0], starts=[0], ends=[0]) + self.assertEqual(out.numel(), 0) + # self.assertEqual(out.shape) + + @unittest.skipIf( not core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) -- GitLab