Skip to content

Commit eb6d339

Browse files
authored
【cherry-pick】fix decay param and overflow in match_matrix (#22107)
* fix decay param in DecayAdagrad test=develop (#22026) * fix integer overflow in match_matrix (#22036) * fix integer overflow in match_matrix test=develop * fix integer overflow in match_matrix test=develop * fix typo test=develop
1 parent 9b64d63 commit eb6d339

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

paddle/fluid/operators/match_matrix_tensor_op.cc

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ void MatchMatrixTensorOP::InferShape(framework::InferShapeContext* ctx) const {
5656
PADDLE_ENFORCE_EQ(w_dims[2], y_dims[1],
5757
"W 's shape must satisfy: W[2] = Y[1]");
5858

59-
int out_dim_0 = -1;
60-
int tmp_dim_0 = -1;
59+
int64_t out_dim_0 = -1;
60+
int64_t tmp_dim_0 = -1;
6161
if (ctx->IsRuntime()) {
6262
framework::Variable* x_var =
6363
boost::get<framework::Variable*>(ctx->GetInputVarPtrs("X")[0]);
@@ -86,8 +86,8 @@ void MatchMatrixTensorOP::InferShape(framework::InferShapeContext* ctx) const {
8686

8787
out_dim_0 = 0;
8888
for (size_t i = 1; i < x_lod_0.size(); i++) {
89-
int x_len = x_lod_0[i] - x_lod_0[i - 1];
90-
int y_len = y_lod_0[i] - y_lod_0[i - 1];
89+
int64_t x_len = x_lod_0[i] - x_lod_0[i - 1];
90+
int64_t y_len = y_lod_0[i] - y_lod_0[i - 1];
9191
out_dim_0 += (x_len * y_len);
9292
}
9393
out_dim_0 *= dim_t;
@@ -173,17 +173,17 @@ class CPUMatchMatrixTensorOPKernel : public framework::OpKernel<T> {
173173
auto* tmp = ctx.Output<LoDTensor>("Tmp");
174174

175175
int dim_t = ctx.Attr<int>("dim_t");
176-
int dim_in = x->dims()[1];
176+
int64_t dim_in = x->dims()[1];
177177

178178
const auto& offset_l = x->lod()[0];
179179
const auto& offset_r = y->lod()[0];
180180

181181
std::vector<size_t> top_offset;
182-
int top_size = 0;
182+
size_t top_size = 0;
183183
top_offset.push_back(top_size);
184184
for (size_t b = 0; b < x->lod()[0].size() - 1; b++) {
185-
int len_l = offset_l[b + 1] - offset_l[b];
186-
int len_r = offset_r[b + 1] - offset_r[b];
185+
size_t len_l = offset_l[b + 1] - offset_l[b];
186+
size_t len_r = offset_r[b + 1] - offset_r[b];
187187
top_size += dim_t * len_l * len_r;
188188
top_offset.push_back(top_size);
189189
}
@@ -204,8 +204,8 @@ class CPUMatchMatrixTensorOPKernel : public framework::OpKernel<T> {
204204

205205
for (size_t b = 0; b < x->lod()[0].size() - 1; b++) {
206206
for (int t = 0; t < dim_t; t++) {
207-
int len_l = offset_l[b + 1] - offset_l[b];
208-
int len_r = offset_r[b + 1] - offset_r[b];
207+
size_t len_l = offset_l[b + 1] - offset_l[b];
208+
size_t len_r = offset_r[b + 1] - offset_r[b];
209209
auto* top_data = out_data + top_offset[b] + t * len_l * len_r;
210210
const auto* l_t_data =
211211
bottom_l_trans_data + offset_l[b] * dim_t * dim_in + t * dim_in;
@@ -234,16 +234,16 @@ class CPUMatchMatrixTensorOPGradKernel : public framework::OpKernel<T> {
234234
auto* tmp = ctx.Input<LoDTensor>("Tmp");
235235

236236
int dim_t = ctx.Attr<int>("dim_t");
237-
int dim_in = x->dims()[1];
237+
int64_t dim_in = x->dims()[1];
238238

239239
const auto& offset_l = x->lod()[0];
240240
const auto& offset_r = y->lod()[0];
241-
std::vector<int> top_offset;
242-
int top_size = 0;
241+
std::vector<size_t> top_offset;
242+
size_t top_size = 0;
243243
top_offset.push_back(top_size);
244244
for (size_t b = 0; b < x->lod()[0].size() - 1; b++) {
245-
int len_l = offset_l[b + 1] - offset_l[b];
246-
int len_r = offset_r[b + 1] - offset_r[b];
245+
size_t len_l = offset_l[b + 1] - offset_l[b];
246+
size_t len_r = offset_r[b + 1] - offset_r[b];
247247
top_size += dim_t * len_l * len_r;
248248
top_offset.push_back(top_size);
249249
}
@@ -270,11 +270,11 @@ class CPUMatchMatrixTensorOPGradKernel : public framework::OpKernel<T> {
270270

271271
for (size_t b = 0; b < x->lod()[0].size() - 1; b++) {
272272
for (int t = 0; t < dim_t; t++) {
273-
int len_l = offset_l[b + 1] - offset_l[b];
274-
int len_r = offset_r[b + 1] - offset_r[b];
273+
size_t len_l = offset_l[b + 1] - offset_l[b];
274+
size_t len_r = offset_r[b + 1] - offset_r[b];
275275

276-
for (int i = 0; i < len_l; i++) {
277-
for (int j = 0; j < len_r; j++) {
276+
for (size_t i = 0; i < len_l; i++) {
277+
for (size_t j = 0; j < len_r; j++) {
278278
auto diff =
279279
top_diff[top_offset[b] + t * len_l * len_r + i * len_r + j];
280280
auto* l_trans_data = bottom_l_trans_data +
@@ -324,11 +324,7 @@ REGISTER_OPERATOR(match_matrix_tensor_grad, ops::MatchMatrixTensorOpGrad);
324324
REGISTER_OP_CPU_KERNEL(match_matrix_tensor,
325325
ops::CPUMatchMatrixTensorOPKernel<
326326
paddle::platform::CPUDeviceContext, float>);
327-
// ops::CPUMatchMatrixTensorOPKernel<paddle::platform::CPUDeviceContext,
328-
// double>
329327

330328
REGISTER_OP_CPU_KERNEL(match_matrix_tensor_grad,
331329
ops::CPUMatchMatrixTensorOPGradKernel<
332330
paddle::platform::CPUDeviceContext, float>);
333-
// ops::CPUMatchMatrixTensorOPGradKernel<paddle::platform::CPUDeviceContext,
334-
// double>

python/paddle/fluid/optimizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,8 @@ def _append_optimize_op(self, block, param_and_grad):
20602060
},
20612061
outputs={"ParamOut": param_and_grad[0],
20622062
"MomentOut": moment_acc},
2063-
attrs={"epsilon": self._epsilon},
2063+
attrs={"epsilon": self._epsilon,
2064+
"decay": self._decay},
20642065
stop_gradient=True)
20652066

20662067
return decayed_adagrad_op

0 commit comments

Comments
 (0)