From e9869a9f26ceccfaaa771de3108eb2012dcbe4f7 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Sun, 22 Jan 2023 16:19:50 +0000 Subject: [PATCH 1/2] add stride check for MaxPool --- paddle/fluid/operators/pool_with_index_op.cc | 5 +++++ paddle/phi/kernels/funcs/pooling.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/paddle/fluid/operators/pool_with_index_op.cc b/paddle/fluid/operators/pool_with_index_op.cc index 74b98069bf647..79262db30fafb 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 17b87a0e17d51..c0741672a458e 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; } From 27894e73abb5a0d94fb5693cc5a273018edb09de Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 31 Jan 2023 14:06:11 +0000 Subject: [PATCH 2/2] add unittests --- .../fluid/tests/unittests/test_pool1d_api.py | 14 +++++++++++++- .../fluid/tests/unittests/test_pool2d_api.py | 12 ++++++++++++ .../fluid/tests/unittests/test_pool3d_api.py | 12 ++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_pool1d_api.py b/python/paddle/fluid/tests/unittests/test_pool1d_api.py index 2c191bf4892b7..3816822e8f3ec 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 @@ def test_pool1d(self): 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 @@ def run_stride_out_of_range(): 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 44ef18605ed2a..c55ea337c41b7 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 @@ def run_stride_out_of_range(): 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 961f0b5c569f0..30a03ab220bcc 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 @@ def run_size_out_of_range(): 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()