From 3ab6faa8b311fdfcd9458641cd30eb8faf8379d8 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 1 Feb 2023 17:08:09 +0800 Subject: [PATCH] Fix div 0 error of case11: paddle.nn.functional.max_pool1d/max_pool2d/max_pool3d (#50010) * add stride check for MaxPool * add unittests --- paddle/fluid/operators/pool_with_index_op.cc | 5 +++++ paddle/phi/kernels/funcs/pooling.h | 5 +++++ .../fluid/tests/unittests/test_pool1d_api.py | 14 +++++++++++++- .../fluid/tests/unittests/test_pool2d_api.py | 12 ++++++++++++ .../fluid/tests/unittests/test_pool3d_api.py | 12 ++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/operators/pool_with_index_op.cc b/paddle/fluid/operators/pool_with_index_op.cc index 74b98069bf..79262db30f 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 17b87a0e17..c0741672a4 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 2c191bf489..3816822e8f 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 44ef18605e..c55ea337c4 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 961f0b5c56..30a03ab220 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() -- GitLab