From 4e1357af368cccb3449dc14c09a2da24ccf5bf2b Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sat, 21 Jan 2023 17:32:23 +0800 Subject: [PATCH 1/2] [Divide by 0 Error] add normalize check --- .../unittests/test_nn_functional_norm_op.py | 37 +++++++++++++++++++ python/paddle/nn/functional/norm.py | 4 ++ 2 files changed, 41 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py diff --git a/python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py b/python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py new file mode 100644 index 0000000000000..b9309a83b52dc --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py @@ -0,0 +1,37 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np + +import paddle + + +class TestSparseEmbeddingAPIError(unittest.TestCase): + def test_errors(self): + with paddle.fluid.dygraph.guard(): + # The size of input in sparse_embedding should not be 0. + def test_0_size(): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(array, [1, 1, 0]), dtype='float32' + ) + paddle.nn.functional.normalize(x) + + self.assertRaises(ValueError, test_0_size) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/nn/functional/norm.py b/python/paddle/nn/functional/norm.py index 87723f6bb8f58..80dedc3be8035 100644 --- a/python/paddle/nn/functional/norm.py +++ b/python/paddle/nn/functional/norm.py @@ -77,6 +77,10 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): # [[0. , 0.24253564, 0.37139067], # [1. , 0.97014254, 0.92847669]]) """ + + if x.size == 0: + raise ValueError("input size should not be 0") + if in_dygraph_mode(): eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype) out = _C_ops.p_norm(x, float(p), axis, epsilon, True, False) From 0bfe83b554733fcf8f65a891e24b1e483050a7af Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 1 Feb 2023 00:02:38 +0800 Subject: [PATCH 2/2] [Divide by 0 Error] normalize check migrate to c++ --- .../unittests/test_nn_functional_norm_op.py | 37 ------------------- .../fluid/tests/unittests/test_normalize.py | 12 ++++++ python/paddle/nn/functional/norm.py | 3 -- 3 files changed, 12 insertions(+), 40 deletions(-) delete mode 100644 python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py diff --git a/python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py b/python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py deleted file mode 100644 index b9309a83b52dc..0000000000000 --- a/python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np - -import paddle - - -class TestSparseEmbeddingAPIError(unittest.TestCase): - def test_errors(self): - with paddle.fluid.dygraph.guard(): - # The size of input in sparse_embedding should not be 0. - def test_0_size(): - array = np.array([], dtype=np.float32) - x = paddle.to_tensor( - np.reshape(array, [1, 1, 0]), dtype='float32' - ) - paddle.nn.functional.normalize(x) - - self.assertRaises(ValueError, test_0_size) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_normalize.py b/python/paddle/fluid/tests/unittests/test_normalize.py index 908360d29e692..a986dd90415d5 100644 --- a/python/paddle/fluid/tests/unittests/test_normalize.py +++ b/python/paddle/fluid/tests/unittests/test_normalize.py @@ -97,6 +97,18 @@ def test_gpu(self): with fluid.program_guard(fluid.Program()): self.run_static(use_gpu=True) + def test_errors(self): + with fluid.dygraph.guard(): + # The size of input in Normalize should not be 0. + def test_0_size(): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(array, [1, 1, 0]), dtype='float32' + ) + paddle.nn.functional.normalize(x) + + self.assertRaises(ValueError, test_0_size) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/nn/functional/norm.py b/python/paddle/nn/functional/norm.py index 80dedc3be8035..0c194d45ed8d3 100644 --- a/python/paddle/nn/functional/norm.py +++ b/python/paddle/nn/functional/norm.py @@ -78,9 +78,6 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): # [1. , 0.97014254, 0.92847669]]) """ - if x.size == 0: - raise ValueError("input size should not be 0") - if in_dygraph_mode(): eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype) out = _C_ops.p_norm(x, float(p), axis, epsilon, True, False)