未验证 提交 3cf50f91 编写于 作者: R RedContritio 提交者: GitHub

Fix 空指针 (Null pointer) of case8: paddle.slice (#49979)

* add check for input of slice

* add unittest
上级 1346cd35
......@@ -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<int64_t> starts = starts_arr.GetData();
std::vector<int64_t> ends = ends_arr.GetData();
......
......@@ -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"
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册