From 0eba90803228d6b2fc0764cc9077a4c57dd4bf19 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 20 Jan 2023 20:40:18 +0800 Subject: [PATCH 1/3] [Divide by 0 Error] add norm check --- .../tests/unittests/test_linalg_norm_op.py | 34 +++++++++++++++++++ python/paddle/tensor/linalg.py | 3 ++ 2 files changed, 37 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_linalg_norm_op.py diff --git a/python/paddle/fluid/tests/unittests/test_linalg_norm_op.py b/python/paddle/fluid/tests/unittests/test_linalg_norm_op.py new file mode 100644 index 0000000000000..77d3583e9f1cb --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_linalg_norm_op.py @@ -0,0 +1,34 @@ +# 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 TestNormAPIError(unittest.TestCase): + def test_errors(self): + # 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, [0, 0]), dtype='float32') + paddle.linalg.norm(x, axis=0) + + self.assertRaises(ValueError, test_0_size) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 4cce1b01968a1..4be2d11bca8cc 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -529,6 +529,9 @@ def p_matrix_norm(input, porder=1.0, axis=axis, keepdim=False, name=None): ) return out + if x.size == 0: + raise ValueError("input size should not be 0") + if axis is None and p is not None: if isinstance(p, str): if p == "fro": From b955a2d8f55692cbe4ccd4d68be20aaeba043ca3 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sat, 21 Jan 2023 00:12:09 +0800 Subject: [PATCH 2/3] [Divide by 0 Error] fix x AttributeError --- python/paddle/tensor/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 4be2d11bca8cc..6fbc3427216e0 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -529,7 +529,7 @@ def p_matrix_norm(input, porder=1.0, axis=axis, keepdim=False, name=None): ) return out - if x.size == 0: + if not isinstance(x, str) and not isinstance(x, int) and x.size == 0: raise ValueError("input size should not be 0") if axis is None and p is not None: From 8b4d74ff5e7b4b8a37eb4562a90b313f858c531e Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Tue, 31 Jan 2023 23:17:24 +0800 Subject: [PATCH 3/3] [Divide by 0 Error] norm check migrate to c++ --- paddle/phi/kernels/cpu/p_norm_kernel.cc | 7 ++++ paddle/phi/kernels/gpu/p_norm_kernel.cu | 7 ++++ paddle/phi/kernels/xpu/p_norm_kernel.cc | 8 +++++ .../tests/unittests/test_linalg_norm_op.py | 34 ------------------- .../fluid/tests/unittests/test_norm_all.py | 9 +++++ python/paddle/tensor/linalg.py | 3 -- 6 files changed, 31 insertions(+), 37 deletions(-) delete mode 100644 python/paddle/fluid/tests/unittests/test_linalg_norm_op.py diff --git a/paddle/phi/kernels/cpu/p_norm_kernel.cc b/paddle/phi/kernels/cpu/p_norm_kernel.cc index 597939953b277..bb33b8a397e02 100644 --- a/paddle/phi/kernels/cpu/p_norm_kernel.cc +++ b/paddle/phi/kernels/cpu/p_norm_kernel.cc @@ -61,6 +61,13 @@ void PNormKernel(const Context& dev_ctx, int pre, n, post; GetDims(xdim, axis, &pre, &n, &post, asvector); + for (int i = 0; i < xdim.size(); i++) { + PADDLE_ENFORCE_LT(0, + xdim[i], + errors::InvalidArgument( + "The dims of Input(X) should be greater than 0.")); + } + auto* place = dev_ctx.eigen_device(); Eigen::DSizes shape(pre, n, post); diff --git a/paddle/phi/kernels/gpu/p_norm_kernel.cu b/paddle/phi/kernels/gpu/p_norm_kernel.cu index c7a6261ce381e..fb869a00d9c50 100644 --- a/paddle/phi/kernels/gpu/p_norm_kernel.cu +++ b/paddle/phi/kernels/gpu/p_norm_kernel.cu @@ -105,6 +105,13 @@ void PNormKernel(const Context& dev_ctx, std::vector reduce_axis = funcs::details::GetReduceDim(axis_dims, xdim.size(), asvector); + for (int i = 0; i < xdim.size(); i++) { + PADDLE_ENFORCE_LT(0, + xdim[i], + errors::InvalidArgument( + "The dims of Input(X) should be greater than 0.")); + } + using MT = typename dtype::MPTypeTrait::Type; if (porder == 0) { phi::funcs::ReduceKernel>( diff --git a/paddle/phi/kernels/xpu/p_norm_kernel.cc b/paddle/phi/kernels/xpu/p_norm_kernel.cc index 7ef72c61ad3aa..60abc59517b78 100644 --- a/paddle/phi/kernels/xpu/p_norm_kernel.cc +++ b/paddle/phi/kernels/xpu/p_norm_kernel.cc @@ -55,6 +55,14 @@ void PNormKernel(const Context& dev_ctx, int n = 1; int t = 1; GetDims(xdim, axis, &m, &t, &n, asvector); + + for (int i = 0; i < xdim.size(); i++) { + PADDLE_ENFORCE_LT(0, + xdim[i], + errors::InvalidArgument( + "The dims of Input(X) should be greater than 0.")); + } + x_dim.push_back(m); x_dim.push_back(t); x_dim.push_back(n); diff --git a/python/paddle/fluid/tests/unittests/test_linalg_norm_op.py b/python/paddle/fluid/tests/unittests/test_linalg_norm_op.py deleted file mode 100644 index 77d3583e9f1cb..0000000000000 --- a/python/paddle/fluid/tests/unittests/test_linalg_norm_op.py +++ /dev/null @@ -1,34 +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 TestNormAPIError(unittest.TestCase): - def test_errors(self): - # 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, [0, 0]), dtype='float32') - paddle.linalg.norm(x, axis=0) - - self.assertRaises(ValueError, test_0_size) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_norm_all.py b/python/paddle/fluid/tests/unittests/test_norm_all.py index d70d0dd9f065d..beff458bd1b70 100644 --- a/python/paddle/fluid/tests/unittests/test_norm_all.py +++ b/python/paddle/fluid/tests/unittests/test_norm_all.py @@ -655,6 +655,15 @@ def err_dtype(p, shape_x, xdtype, out=None): ValueError, paddle.norm, data, p='unspport', axis=[-3, -2, -1] ) + with fluid.dygraph.guard(): + # The size of input in Norm should not be 0. + def test_0_size(): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor(np.reshape(array, [0, 0]), dtype='float32') + paddle.linalg.norm(x, axis=0) + + self.assertRaises(ValueError, test_0_size) + if __name__ == '__main__': paddle.enable_static() diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 6fbc3427216e0..4cce1b01968a1 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -529,9 +529,6 @@ def p_matrix_norm(input, porder=1.0, axis=axis, keepdim=False, name=None): ) return out - if not isinstance(x, str) and not isinstance(x, int) and x.size == 0: - raise ValueError("input size should not be 0") - if axis is None and p is not None: if isinstance(p, str): if p == "fro":