Skip to content

Commit de3c067

Browse files
authored
fix gpu outofrange (#29238) (#29348)
* fix gpu emb out of range Change-Id: I5794ac73bd634d5ea069a6fbbd914274b6d6b7bf * fix doc Change-Id: I5a3350b2930a9ab2f52116c192b087307faf8fdf
1 parent 07a7cd4 commit de3c067

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

paddle/fluid/operators/lookup_table_v2_op.cu

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ __global__ void LookupTableV2(T *output, const T *table, const int64_t *ids,
3131

3232
while (idy < K) {
3333
int64_t id = ids[idy];
34-
PADDLE_ENFORCE(
35-
id >= 0,
36-
"Variable value (input) of OP(fluid.layers.embedding) "
37-
"expected >= 0 and < %ld, but got %ld. Please check input value.",
38-
N, id);
39-
PADDLE_ENFORCE(
40-
id < N,
41-
"Variable value (input) of OP(fluid.layers.embedding) "
42-
"expected >= 0 and < %ld, but got %ld. Please check input value.",
43-
N, id);
4434
T *out = output + idy * D;
4535
const T *tab = table + id * D;
4636
for (int i = idx; i < D; i += BlockDimX) {
@@ -66,16 +56,6 @@ __global__ void LookupTableV2Grad(T *table, const T *output, const int64_t *ids,
6656

6757
while (idy < K) {
6858
int64_t id = ids[idy];
69-
PADDLE_ENFORCE(
70-
id >= 0,
71-
"Variable value (input) of OP(fluid.layers.embedding) "
72-
"expected >= 0 and < %ld, but got %ld. Please check input value.",
73-
N, id);
74-
PADDLE_ENFORCE(
75-
id < N,
76-
"Variable value (input) of OP(fluid.layers.embedding) "
77-
"expected >= 0 and < %ld, but got %ld. Please check input value.",
78-
N, id);
7959
const T *out = output + idy * D;
8060
T *tab = table + id * D;
8161
for (int i = idx; i < D; i += BlockDimX) {
@@ -127,6 +107,21 @@ class LookupTableV2CUDAKernel : public framework::OpKernel<T> {
127107
ids_p = ids_t->data<int64_t>();
128108
}
129109

110+
for (int64_t i = 0; i < K; ++i) {
111+
PADDLE_ENFORCE_GE(
112+
ids[i], 0,
113+
platform::errors::InvalidArgument(
114+
"Variable value (input) of OP(paddle.nn.embedding) "
115+
"expected >= 0 and < %ld, but got %ld. Please check input value.",
116+
N, ids[i]));
117+
PADDLE_ENFORCE_LT(
118+
ids[i], N,
119+
platform::errors::InvalidArgument(
120+
"Variable value (input) of OP(paddle.nn.embedding) "
121+
"expected >= 0 and < %ld, but got %ld. Please check input value.",
122+
N, ids[i]));
123+
}
124+
130125
auto *table = table_t->data<T>();
131126
auto *output = output_t->mutable_data<T>(context.GetPlace());
132127

python/paddle/fluid/input.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,7 @@ def embedding(input,
197197
indicates the size of the dictionary of embeddings and the size of each embedding vector respectively.
198198
is_sparse(bool): The flag indicating whether to use sparse update. This parameter only
199199
affects the performance of the backwards gradient update. It is recommended to set
200-
True because sparse update is faster. But some optimizer does not support sparse update,
201-
such as :ref:`api_fluid_optimizer_AdadeltaOptimizer` , :ref:`api_fluid_optimizer_AdamaxOptimizer` ,
202-
:ref:`api_fluid_optimizer_DecayedAdagradOptimizer` , :ref:`api_fluid_optimizer_FtrlOptimizer` ,
203-
:ref:`api_fluid_optimizer_LambOptimizer` and :ref:`api_fluid_optimizer_LarsMomentumOptimizer` .
200+
True because sparse update is faster. But some optimizer does not support sparse update
204201
In these case, is_sparse must be False. Default: False.
205202
is_distributed(bool): Whether to store the embedding matrix in a distributed manner. Only used
206203
in multi-machine distributed CPU training. Default: False.
@@ -210,11 +207,10 @@ def embedding(input,
210207
encounters :math:`padding\_idx` in id. And the padding data will not be updated while training.
211208
If set None, it makes no effect to output. Default: None.
212209
param_attr(ParamAttr): To specify the weight parameter property. Default: None, which means the
213-
default weight parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr` . In addition,
210+
default weight parameter property is used. In addition,
214211
user-defined or pre-trained word vectors can be loaded with the :attr:`param_attr` parameter.
215212
The local word vector needs to be transformed into numpy format, and the shape of local word
216-
vector should be consistent with :attr:`size` . Then :ref:`api_fluid_initializer_NumpyArrayInitializer`
217-
is used to load custom or pre-trained word vectors. See code example 2 for details.
213+
vector should be consistent with :attr:`size` .
218214
dtype(str|core.VarDesc.VarType): It refers to the data type of output Tensor.
219215
It must be float32 or float64. Default: float32.
220216
@@ -267,9 +263,7 @@ def embedding(input,
267263
268264
import paddle
269265
import numpy as np
270-
271-
paddle.disable_static()
272-
266+
273267
x_data = np.arange(3, 6).reshape((3, 1)).astype(np.int64)
274268
275269
# x is a Tensor.

python/paddle/nn/functional/input.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,25 @@ def embedding(x, weight, padding_idx=None, sparse=False, name=None):
168168
169169
.. code-block:: python
170170
171+
import numpy as np
171172
import paddle
172173
import paddle.nn as nn
173174
174-
weight = prog.global_block().create_parameter(
175-
attr=self._param_attr,
176-
shape=param_shape,
177-
dtype=self._dtype,
178-
default_initializer=Constant(1.0))
175+
x0 = np.arange(3, 6).reshape((3, 1)).astype(np.int64)
176+
w0 = np.full(shape=(10, 3), fill_value=2).astype(np.float32)
179177
180-
prog = paddle.static.Program()
178+
# x.data = [[3], [4], [5]]
179+
# x.shape = [3, 1]
180+
x = paddle.to_tensor(x0, stop_gradient=False)
181181
182-
weight = prog.global_block().create_parameter(
183-
(128, 100), dtype="float32", default_initializer=Constant(1.0))
182+
# w.data = [[2. 2. 2.] ... [2. 2. 2.]]
183+
# w.shape = [10, 3]
184+
w = paddle.to_tensor(w0, stop_gradient=False)
184185
185-
label = paddle.static.data(
186-
name="label",
187-
shape=[4],
188-
append_batch_size=False,
189-
dtype="int64")
190-
191-
emb = nn.embedding(
192-
x=label, weight=weight, sparse=True, name="embedding")
186+
# emb.data = [[[2., 2., 2.]], [[2., 2., 2.]], [[2., 2., 2.]]]
187+
# emb.shape = [3, 1, 3]
188+
emb = nn.functional.embedding(
189+
x=x, weight=w, sparse=True, name="embedding")
193190
194191
"""
195192
padding_idx = -1 if padding_idx is None else padding_idx if padding_idx >= 0 else (

python/paddle/nn/layer/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ class Embedding(layers.Layer):
12161216
12171217
x_data = np.arange(3, 6).reshape((3, 1)).astype(np.int64)
12181218
y_data = np.arange(6, 12).reshape((3, 2)).astype(np.float32)
1219-
paddle.disable_static(paddle.CPUPlace())
1219+
12201220
x = paddle.to_tensor(x_data, stop_gradient=False)
12211221
y = paddle.to_tensor(y_data, stop_gradient=False)
12221222

0 commit comments

Comments
 (0)