Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Divide by 0 Error] add normalize check #49977

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions python/paddle/fluid/tests/unittests/test_nn_functional_norm_op.py
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个单测不需要新建文件。放到python/paddle/fluid/tests/unittests/test_normalize.py下即可。

4 changes: 4 additions & 0 deletions python/paddle/nn/functional/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个判断应放在C++中进行。如果放在python端,在动态图下会影响到API性能~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看报错也在PNormKernel内所以跟#49966 放在一起

--------------------------------------
C++ Traceback (most recent call last):
--------------------------------------
0   p_norm_ad_func(paddle::experimental::Tensor const&, float, int, float, bool, bool)
1   paddle::experimental::p_norm(paddle::experimental::Tensor const&, float, int, float, bool, bool)
2   void phi::PNormKernel<float, phi::CPUContext>(phi::CPUContext const&, phi::DenseTensor const&, float, int, float, bool, bool, phi::DenseTensor*)
3   Eigen::internal::TensorExecutor<Eigen::TensorAssignOp<Eigen::TensorReshapingOp<Eigen::DSizes<int, 2> const, Eigen::TensorMap<Eigen::Tensor<float, 1, 1, long>, 0, Eigen::MakePointer> >, Eigen::TensorCwiseUnaryOp<Eigen::internal::bind2nd_op<Eigen::internal::scalar_pow_op<float, float> >, Eigen::TensorReductionOp<Eigen::internal::SumReducer<float>, Eigen::DSizes<int, 1> const, Eigen::TensorCwiseUnaryOp<Eigen::internal::bind2nd_op<Eigen::internal::scalar_pow_op<float, float> >, Eigen::TensorCwiseUnaryOp<Eigen::internal::scalar_abs_op<float const>, Eigen::TensorReshapingOp<Eigen::DSizes<int, 3> const, Eigen::TensorMap<Eigen::Tensor<float const, 1, 1, long>, 0, Eigen::MakePointer> const> const> const> const, Eigen::MakePointer> const> const> const, Eigen::DefaultDevice, true, (Eigen::internal::TiledEvaluation)0>::run(Eigen::TensorAssignOp<Eigen::TensorReshapingOp<Eigen::DSizes<int, 2> const, Eigen::TensorMap<Eigen::Tensor<float, 1, 1, long>, 0, Eigen::MakePointer> >, Eigen::TensorCwiseUnaryOp<Eigen::internal::bind2nd_op<Eigen::internal::scalar_pow_op<float, float> >, Eigen::TensorReductionOp<Eigen::internal::SumReducer<float>, Eigen::DSizes<int, 1> const, Eigen::TensorCwiseUnaryOp<Eigen::internal::bind2nd_op<Eigen::internal::scalar_pow_op<float, float> >, Eigen::TensorCwiseUnaryOp<Eigen::internal::scalar_abs_op<float const>, Eigen::TensorReshapingOp<Eigen::DSizes<int, 3> const, Eigen::TensorMap<Eigen::Tensor<float const, 1, 1, long>, 0, Eigen::MakePointer> const> const> const> const, Eigen::MakePointer> const> const> const&, Eigen::DefaultDevice const&)

----------------------
Error Message Summary:
----------------------
FatalError: `Erroneous arithmetic operation` is detected by the operating system.
  [TimeInfo: *** Aborted at 1675180076 (unix time) try "date -d @1675180076" if you are using GNU date ***]
  [SignalInfo: *** SIGFPE (@0x7f16555110ef) received by PID 26694 (TID 0x7f165b2e9740) from PID 1431376111 ***]

Floating point exception (core dumped)

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)
Expand Down