Skip to content

Commit 3a73d34

Browse files
authored
fix the div 0 error of sparse_embedding (#49948)
* fix the div 0 error of sparse_embedding * add unittest
1 parent 776021c commit 3a73d34

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

python/paddle/fluid/contrib/layers/nn.py

+3
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,9 @@ def sparse_embedding(
793793
'paddle.static.nn.sparse_embedding',
794794
)
795795

796+
if input.size == 0:
797+
raise ValueError("input size should not be 0")
798+
796799
w = helper.create_parameter(
797800
attr=helper.param_attr,
798801
shape=size,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
import paddle
18+
19+
20+
class TestSparseEmbeddingAPIError(unittest.TestCase):
21+
def test_errors(self):
22+
with paddle.fluid.dygraph.guard():
23+
# The size of input in sparse_embedding should not be 0.
24+
def test_0_size():
25+
input = paddle.to_tensor([], dtype='int64')
26+
paddle.static.nn.sparse_embedding(
27+
input,
28+
[2097152, 2097152, 2097152, 2097152],
29+
padding_idx=2097152,
30+
)
31+
32+
self.assertRaises(ValueError, test_0_size)
33+
34+
35+
if __name__ == '__main__':
36+
paddle.enable_static()
37+
unittest.main()

0 commit comments

Comments
 (0)