Skip to content

Commit

Permalink
[Divide by 0 Error] add normalize check (#49977)
Browse files Browse the repository at this point in the history
* [Divide by 0 Error] add normalize check

* [Divide by 0 Error] normalize check migrate to c++
  • Loading branch information
gouzil authored Feb 3, 2023
1 parent 08e7259 commit 620ce8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/paddle/fluid/tests/unittests/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions python/paddle/nn/functional/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None):
# [[0. , 0.24253564, 0.37139067],
# [1. , 0.97014254, 0.92847669]])
"""

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

0 comments on commit 620ce8b

Please sign in to comment.