diff --git a/paddle/phi/kernels/funcs/pooling.h b/paddle/phi/kernels/funcs/pooling.h index c0741672a458e..f1656c8a177a7 100644 --- a/paddle/phi/kernels/funcs/pooling.h +++ b/paddle/phi/kernels/funcs/pooling.h @@ -371,6 +371,13 @@ inline int PoolOutputSize(int input_size, int padding_2, int stride, bool ceil_mode) { + PADDLE_ENFORCE_NE( + stride, + 0, + phi::errors::InvalidArgument( + "The stride of PoolOutputSize shall not be 0, but received %d.", + stride)); + int output_size; if (!ceil_mode) { output_size = diff --git a/python/paddle/fluid/tests/unittests/test_pool1d_api.py b/python/paddle/fluid/tests/unittests/test_pool1d_api.py index 3816822e8f3ec..e671235869663 100644 --- a/python/paddle/fluid/tests/unittests/test_pool1d_api.py +++ b/python/paddle/fluid/tests/unittests/test_pool1d_api.py @@ -429,6 +429,16 @@ def run_zero_stride(): self.assertRaises(ValueError, run_zero_stride) + def run_zero_tuple_stride(): + with fluid.dygraph.guard(): + array = np.array([1], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(array, [1, 1, 1]), dtype='float32' + ) + out = F.max_pool1d(x, 1, stride=(0)) + + self.assertRaises(ValueError, run_zero_tuple_stride) + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_pool2d_api.py b/python/paddle/fluid/tests/unittests/test_pool2d_api.py index c55ea337c41b7..fcdec610a480e 100644 --- a/python/paddle/fluid/tests/unittests/test_pool2d_api.py +++ b/python/paddle/fluid/tests/unittests/test_pool2d_api.py @@ -609,6 +609,18 @@ def run_zero_stride(): self.assertRaises(ValueError, run_zero_stride) + def run_zero_tuple_stride(): + with fluid.dygraph.guard(): + array = np.array([1], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(array, [1, 1, 1, 1]), dtype='float32' + ) + out = max_pool2d( + x, 1, stride=(0, 0), return_mask=False, data_format='NHWC' + ) + + self.assertRaises(ValueError, run_zero_tuple_stride) + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_pool3d_api.py b/python/paddle/fluid/tests/unittests/test_pool3d_api.py index 30a03ab220bcc..e1d7543e7bce0 100644 --- a/python/paddle/fluid/tests/unittests/test_pool3d_api.py +++ b/python/paddle/fluid/tests/unittests/test_pool3d_api.py @@ -575,6 +575,16 @@ def run_zero_stride(): self.assertRaises(ValueError, run_zero_stride) + def run_zero_tuple_stride(): + with fluid.dygraph.guard(): + array = np.array([1], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(array, [1, 1, 1, 1, 1]), dtype='float32' + ) + out = max_pool3d(x, 1, stride=(0, 0, 0), ceil_mode=False) + + self.assertRaises(ValueError, run_zero_tuple_stride) + if __name__ == '__main__': unittest.main()