diff --git a/paddle/fluid/operators/pool_with_index_op.cc b/paddle/fluid/operators/pool_with_index_op.cc index 74b98069bf647fa153978790cf8934fc5968074c..79262db30fafbaf5fd3accc098b0938bb7ec14e4 100644 --- a/paddle/fluid/operators/pool_with_index_op.cc +++ b/paddle/fluid/operators/pool_with_index_op.cc @@ -27,6 +27,11 @@ inline int MaxPoolOutputSize(int input_size, int filter_size, int padding, int stride) { + PADDLE_ENFORCE_NE( + stride, + 0, + phi::errors::InvalidArgument( + "The stride of MaxPool shall not be 0, but received %d.", stride)); int output_size = (input_size - filter_size + 2 * padding) / stride + 1; return output_size; } diff --git a/paddle/phi/kernels/funcs/pooling.h b/paddle/phi/kernels/funcs/pooling.h index 17b87a0e17d5127bb8d125cf489f3efdfbfb00cc..c0741672a458e1876ee2e0cec85018d742a1bb07 100644 --- a/paddle/phi/kernels/funcs/pooling.h +++ b/paddle/phi/kernels/funcs/pooling.h @@ -402,6 +402,11 @@ inline int MaxPoolOutputSize(int input_size, int filter_size, int padding, int stride) { + PADDLE_ENFORCE_NE( + stride, + 0, + phi::errors::InvalidArgument( + "The stride of MaxPool shall not be 0, but received %d.", stride)); int output_size = (input_size - filter_size + 2 * padding) / stride + 1; return 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 2c191bf4892b774065b1b390841bac009016b2df..3816822e8f3ec979bc01fd3d71b88968142d057c 100644 --- a/python/paddle/fluid/tests/unittests/test_pool1d_api.py +++ b/python/paddle/fluid/tests/unittests/test_pool1d_api.py @@ -274,7 +274,7 @@ class TestPool1D_API(unittest.TestCase): self.check_max_dygraph_return_index_results(place) -class TestPool2DError_API(unittest.TestCase): +class TestPool1DError_API(unittest.TestCase): def test_error_api(self): def run1(): with fluid.dygraph.guard(): @@ -417,6 +417,18 @@ class TestPool2DError_API(unittest.TestCase): self.assertRaises(ValueError, run_stride_out_of_range) + def run_zero_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, padding=1, return_mask=True, ceil_mode=True + ) + + self.assertRaises(ValueError, run_zero_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 44ef18605ed2aed1f9d48a37d77b51e109cb0095..c55ea337c41b7fc45d42124792397d7915189bc6 100644 --- a/python/paddle/fluid/tests/unittests/test_pool2d_api.py +++ b/python/paddle/fluid/tests/unittests/test_pool2d_api.py @@ -597,6 +597,18 @@ class TestPool2DError_API(unittest.TestCase): self.assertRaises(ValueError, run_stride_out_of_range) + def run_zero_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, padding=1, return_mask=True, ceil_mode=True + ) + + self.assertRaises(ValueError, run_zero_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 961f0b5c569f043f42705dd10719838229c89e02..30a03ab220bcc6b4b596e4c1eb4986f3ecae8cb3 100644 --- a/python/paddle/fluid/tests/unittests/test_pool3d_api.py +++ b/python/paddle/fluid/tests/unittests/test_pool3d_api.py @@ -563,6 +563,18 @@ class TestPool3DError_API(unittest.TestCase): self.assertRaises(ValueError, run_size_out_of_range) + def run_zero_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, padding=1, return_mask=True, ceil_mode=True + ) + + self.assertRaises(ValueError, run_zero_stride) + if __name__ == '__main__': unittest.main()