From 75fac7227db4a7e0b9e43b8cd941c92733c4156a Mon Sep 17 00:00:00 2001 From: phlrain Date: Wed, 28 May 2025 14:56:06 +0800 Subject: [PATCH 1/3] fix matmul grad 0 size bug --- paddle/phi/kernels/gpu/elementwise_grad.h | 32 ++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/paddle/phi/kernels/gpu/elementwise_grad.h b/paddle/phi/kernels/gpu/elementwise_grad.h index 5bd695ac8c124e..fa6d639f41eeb8 100644 --- a/paddle/phi/kernels/gpu/elementwise_grad.h +++ b/paddle/phi/kernels/gpu/elementwise_grad.h @@ -340,7 +340,17 @@ void ElementwiseDivGrad(const GPUContext &dev_ctx, DenseTensor *dy, int axis = -1) { const auto place = dev_ctx.GetPlace(); - if (dx != nullptr && dy != nullptr) { + if (dx->numel() == 0) { + dev_ctx.Alloc(dx); + } + + if (dy->numel() == 0) { + dev_ctx.Alloc(dy); + } + + bool need_dx = (dx != nullptr) && (dy->numel() != 0); + bool need_dy = (dy != nullptr) && (dy->numel() != 0); + if (need_dx && need_dy) { std::vector ins = {&dout, &out, &y}; GetGradXAndYOut(dev_ctx, place, @@ -350,11 +360,11 @@ void ElementwiseDivGrad(const GPUContext &dev_ctx, dx, dy, funcs::DivGradXYFunctor()); - } else if (dx != nullptr && dy == nullptr) { + } else if (need_dx) { std::vector ins = {&dout, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dx, funcs::DivGradXFunctor()); - } else if (dy != nullptr && dx == nullptr) { + } else if (need_dy) { std::vector ins = {&dout, &out, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dy, funcs::DivGradYFunctor()); @@ -377,7 +387,17 @@ void ElementwiseMulGrad(const GPUContext &dev_ctx, int axis) { const auto place = dev_ctx.GetPlace(); - if (dx != nullptr && dy != nullptr) { + if (dx->numel() == 0) { + dev_ctx.Alloc(dx); + } + + if (dy->numel() == 0) { + dev_ctx.Alloc(dy); + } + + bool need_dx = (dx != nullptr) && (dy->numel() != 0); + bool need_dy = (dy != nullptr) && (dy->numel() != 0); + if (need_dy && need_dy) { std::vector ins = {&dout, &y, &x}; GetGradXAndYOut(dev_ctx, place, @@ -387,11 +407,11 @@ void ElementwiseMulGrad(const GPUContext &dev_ctx, dx, dy, funcs::MultiplyGradXYFunctor()); - } else if (dx != nullptr && dy == nullptr) { + } else if (need_dx) { std::vector ins = {&dout, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dx, funcs::MultiplyGradFunctor()); - } else if (dx == nullptr && dy != nullptr) { + } else if (need_dy) { std::vector ins = {&dout, &x}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dy, funcs::MultiplyGradFunctor()); From a4018b56ec8156a6cfcca365b500f91b524b0658 Mon Sep 17 00:00:00 2001 From: phlrain Date: Wed, 28 May 2025 23:13:54 +0800 Subject: [PATCH 2/3] fix bug --- paddle/phi/kernels/gpu/elementwise_grad.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/paddle/phi/kernels/gpu/elementwise_grad.h b/paddle/phi/kernels/gpu/elementwise_grad.h index fa6d639f41eeb8..0b45012f78fecb 100644 --- a/paddle/phi/kernels/gpu/elementwise_grad.h +++ b/paddle/phi/kernels/gpu/elementwise_grad.h @@ -340,15 +340,15 @@ void ElementwiseDivGrad(const GPUContext &dev_ctx, DenseTensor *dy, int axis = -1) { const auto place = dev_ctx.GetPlace(); - if (dx->numel() == 0) { + if (dx != nullptr && dx->numel() == 0) { dev_ctx.Alloc(dx); } - if (dy->numel() == 0) { + if (dy != nullptr && dy->numel() == 0) { dev_ctx.Alloc(dy); } - bool need_dx = (dx != nullptr) && (dy->numel() != 0); + bool need_dx = (dx != nullptr) && (dx->numel() != 0); bool need_dy = (dy != nullptr) && (dy->numel() != 0); if (need_dx && need_dy) { std::vector ins = {&dout, &out, &y}; @@ -387,15 +387,15 @@ void ElementwiseMulGrad(const GPUContext &dev_ctx, int axis) { const auto place = dev_ctx.GetPlace(); - if (dx->numel() == 0) { + if (dx != nullptr && dx->numel() == 0) { dev_ctx.Alloc(dx); } - if (dy->numel() == 0) { + if (dy != nullptr && dy->numel() == 0) { dev_ctx.Alloc(dy); } - bool need_dx = (dx != nullptr) && (dy->numel() != 0); + bool need_dx = (dx != nullptr) && (dx->numel() != 0); bool need_dy = (dy != nullptr) && (dy->numel() != 0); if (need_dy && need_dy) { std::vector ins = {&dout, &y, &x}; From 4cf5a4b68fc5511ea37b880a62f4966c53235af5 Mon Sep 17 00:00:00 2001 From: phlrain Date: Thu, 29 May 2025 17:16:50 +0800 Subject: [PATCH 3/3] fix bug --- paddle/phi/kernels/gpu/elementwise_grad.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/kernels/gpu/elementwise_grad.h b/paddle/phi/kernels/gpu/elementwise_grad.h index 0b45012f78fecb..961af8b96738a3 100644 --- a/paddle/phi/kernels/gpu/elementwise_grad.h +++ b/paddle/phi/kernels/gpu/elementwise_grad.h @@ -397,7 +397,7 @@ void ElementwiseMulGrad(const GPUContext &dev_ctx, bool need_dx = (dx != nullptr) && (dx->numel() != 0); bool need_dy = (dy != nullptr) && (dy->numel() != 0); - if (need_dy && need_dy) { + if (need_dx && need_dy) { std::vector ins = {&dout, &y, &x}; GetGradXAndYOut(dev_ctx, place,