未验证 提交 08ad72d0 编写于 作者: 乔龙飞 Qiao Longfei 提交者: GitHub

Merge pull request #15679 from jacquesqiao/update-lookup_table_grad-padding-index

lookup_table_grad kernel should consider padding_idx test=develop
...@@ -129,6 +129,7 @@ class LookupTableGradKernel : public framework::OpKernel<T> { ...@@ -129,6 +129,7 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
"must be either LoDTensor or SelectedRows"); "must be either LoDTensor or SelectedRows");
} }
int64_t padding_idx = context.Attr<int64_t>("padding_idx");
bool is_sparse = context.Attr<bool>("is_sparse"); bool is_sparse = context.Attr<bool>("is_sparse");
// Since paddings are not trainable and fixed in forward, the gradient of // Since paddings are not trainable and fixed in forward, the gradient of
// paddings makes no sense and we don't deal with it in backward. // paddings makes no sense and we don't deal with it in backward.
...@@ -187,6 +188,10 @@ class LookupTableGradKernel : public framework::OpKernel<T> { ...@@ -187,6 +188,10 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
memset(d_table_data, 0, d_table->numel() * sizeof(T)); memset(d_table_data, 0, d_table->numel() * sizeof(T));
for (int64_t i = 0; i < ids->numel(); ++i) { for (int64_t i = 0; i < ids->numel(); ++i) {
if (padding_idx != kNoPadding && ids_data[i] == padding_idx) {
// the gradient of padding_idx should be 0, already done by memset, so
// do nothing.
} else {
PADDLE_ENFORCE_LT(ids_data[i], N); PADDLE_ENFORCE_LT(ids_data[i], N);
PADDLE_ENFORCE_GE(ids_data[i], 0); PADDLE_ENFORCE_GE(ids_data[i], 0);
for (int j = 0; j < D; ++j) { for (int j = 0; j < D; ++j) {
...@@ -195,6 +200,7 @@ class LookupTableGradKernel : public framework::OpKernel<T> { ...@@ -195,6 +200,7 @@ class LookupTableGradKernel : public framework::OpKernel<T> {
} }
} }
} }
}
}; };
} // namespace operators } // namespace operators
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册