Skip to content

Commit

Permalink
test=develop, bug fix for embeddings padding (PaddlePaddle#28708)
Browse files Browse the repository at this point in the history
* test=develop, bug fix for embeddings padding

* fix raise Value for Embedding

Change-Id: I6d343fceee369a5796ad59cca5c91fdd15429125

Co-authored-by: seiriosPlus <[email protected]>
  • Loading branch information
123malin and seiriosPlus authored Nov 20, 2020
1 parent 655d5eb commit 9066828
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
13 changes: 7 additions & 6 deletions python/paddle/nn/functional/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ def embedding(x, weight, padding_idx=None, sparse=False, name=None):
x=label, weight=weight, sparse=True, name="embedding")
"""
padding_idx = -1 if padding_idx is None else padding_idx if padding_idx >= 0 else (
weight.shape[0] + padding_idx)

if padding_idx >= weight.shape[0] or padding_idx < -weight.shape[0]:
raise ValueError("padding_idx must be within [-{}, {})".format(
weight.shape[0], weight.shape[0]))

if in_dygraph_mode():
return core.ops.lookup_table_v2(
weight, x, 'is_sparse', sparse, 'is_distributed', False,
Expand All @@ -206,12 +213,6 @@ def embedding(x, weight, padding_idx=None, sparse=False, name=None):
remote_prefetch = sparse and (not is_distributed)

tmp = helper.create_variable_for_type_inference(dtype)
padding_idx = -1 if padding_idx is None else padding_idx if padding_idx >= 0 else (
weight.shape[0] + padding_idx)

if padding_idx >= weight.shape[0] or padding_idx < -weight.shape[0]:
raise ValueError("padding_idx must be within [-{}, {})".format(
weight.shape[0], weight.shape[0]))

helper.append_op(
type='lookup_table_v2',
Expand Down
8 changes: 5 additions & 3 deletions python/paddle/nn/layer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,16 +1103,18 @@ def __init__(self,
self._embedding_dim = embedding_dim
self._sparse = sparse
self._is_distributed = False
self._padding_idx = -1 if padding_idx is None else padding_idx if padding_idx >= 0 else (
num_embeddings + padding_idx)
self._padding_idx = padding_idx

if self._num_embeddings <= 0:
raise ValueError("num_embeddings must be gather than 0")

if self._embedding_dim <= 0:
raise ValueError("embedding_dim must be gather than 0")

if self._padding_idx >= num_embeddings or self._padding_idx < -num_embeddings:
padding_idx = -1 if padding_idx is None else padding_idx if padding_idx >= 0 else (
num_embeddings + padding_idx)

if padding_idx >= num_embeddings or padding_idx < -num_embeddings:
raise ValueError("padding_idx must be within [-{}, {})".format(
num_embeddings, num_embeddings))

Expand Down

0 comments on commit 9066828

Please sign in to comment.