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

fix the div 0 error of sparse_embedding #49948

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions python/paddle/fluid/contrib/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ def sparse_embedding(
'paddle.static.nn.sparse_embedding',
)

if input.size == 0:
raise ValueError("input size should not be 0")

Comment on lines +790 to +792
Copy link
Contributor

Choose a reason for hiding this comment

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

这个报错是哪里报错的?可以在C++端做检查吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Traceback (most recent call last):
  File "bug.py", line 6, in <module>
    paddle.static.nn.sparse_embedding(input, [2097152, 2097152, 2097152, 2097152], padding_idx=2097152)
  File "/home/work/wangying/paddlepaddle/pdpd_fuzz_protobuf/pdpd-env/clang_pd_fuzz_env/lib/python3.8/site-packages/paddle/fluid/contrib/layers/nn.py", line 1205, in sparse_embedding
    w = helper.create_parameter(
  File "/home/work/wangying/paddlepaddle/pdpd_fuzz_protobuf/pdpd-env/clang_pd_fuzz_env/lib/python3.8/site-packages/paddle/fluid/layer_helper_base.py", line 432, in create_parameter
    return self.main_program.global_block().create_parameter(
  File "/home/work/wangying/paddlepaddle/pdpd_fuzz_protobuf/pdpd-env/clang_pd_fuzz_env/lib/python3.8/site-packages/paddle/fluid/framework.py", line 3963, in create_parameter
    initializer(param, self)
  File "/home/work/wangying/paddlepaddle/pdpd_fuzz_protobuf/pdpd-env/clang_pd_fuzz_env/lib/python3.8/site-packages/paddle/fluid/initializer.py", line 72, in __call__
    return self.forward(param, block)
  File "/home/work/wangying/paddlepaddle/pdpd_fuzz_protobuf/pdpd-env/clang_pd_fuzz_env/lib/python3.8/site-packages/paddle/fluid/initializer.py", line 712, in forward
    limit = math.sqrt(6.0 / float(fan_in + fan_out))
ZeroDivisionError: float division by zero

求指路一下c++端文件~

Copy link
Contributor

Choose a reason for hiding this comment

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

啊,抱歉,我看了一下报错。这个是python端的错误。只能加在python端。

w = helper.create_parameter(
attr=helper.param_attr,
shape=size,
Expand Down
37 changes: 37 additions & 0 deletions python/paddle/fluid/tests/unittests/test_sparse_embedding_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 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():
input = paddle.to_tensor([], dtype='int64')
paddle.static.nn.sparse_embedding(
input,
[2097152, 2097152, 2097152, 2097152],
padding_idx=2097152,
)

self.assertRaises(ValueError, test_0_size)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()