From e63fb1e608a71803c859ddf602a880f96276af04 Mon Sep 17 00:00:00 2001 From: Zhang Zheng <32410583+ZzSean@users.noreply.github.com> Date: Mon, 8 May 2023 17:09:36 +0800 Subject: [PATCH] [Cherry-Pick] Fix the calculation of y_grad in divide_backward (#53584) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cherry-Pick: #53582 修改内容:在除法out = x / y中,将y的反向公式由dy = -dout * out / y 改为 dy = -dout * ((x / y) / y) 修改原因:使用result作为反向的输入,在低精度的时候本身cast之后就会存在一些精度损失,所以重新计算后才是更准确的结果 修改影响:此改动可以使结果更精确且对性能影响忽略不计 --- paddle/phi/kernels/funcs/elementwise_functor.h | 8 ++++---- paddle/phi/kernels/gpu/elementwise_divide_grad_kernel.cu | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/paddle/phi/kernels/funcs/elementwise_functor.h b/paddle/phi/kernels/funcs/elementwise_functor.h index 508240a9916..84938597e82 100644 --- a/paddle/phi/kernels/funcs/elementwise_functor.h +++ b/paddle/phi/kernels/funcs/elementwise_functor.h @@ -117,7 +117,7 @@ struct DivGradXYFunctor { // dy = - dout * out / y phi::Array outs; outs[0] = a / c; - outs[1] = -a * b / c; + outs[1] = -a * ((b / c) / c); return outs; } }; @@ -130,7 +130,7 @@ struct DivGradXYFunctor, ComplexType> { const ComplexType c) { phi::Array, 2> outs; ComplexType c_conj(c.real, -c.imag); - ComplexType out_div_c_conj((b / c).real, -(b / c).imag); + ComplexType out_div_c_conj(((b / c) / c).real, -((b / c) / c).imag); outs[0] = a / c_conj; outs[1] = -a * out_div_c_conj; return outs; @@ -157,7 +157,7 @@ struct DivGradXFunctor> { template struct DivGradYFunctor { inline HOSTDEVICE T operator()(const T a, const T b, const T c) const { - return -a * b / c; + return -a * ((b / c) / c); } }; @@ -167,7 +167,7 @@ struct DivGradYFunctor> { inline HOSTDEVICE ComplexType operator()(const ComplexType a, const ComplexType b, const ComplexType c) const { - ComplexType out_div_c_conj((b / c).real, -(b / c).imag); + ComplexType out_div_c_conj(((b / c) / c).real, -((b / c) / c).imag); return -a * out_div_c_conj; } }; diff --git a/paddle/phi/kernels/gpu/elementwise_divide_grad_kernel.cu b/paddle/phi/kernels/gpu/elementwise_divide_grad_kernel.cu index 57bf6da4060..d63841f17e6 100644 --- a/paddle/phi/kernels/gpu/elementwise_divide_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/elementwise_divide_grad_kernel.cu @@ -36,7 +36,7 @@ void DivideGradKernel(const Context& dev_ctx, DenseTensor* dy) { const auto place = dev_ctx.GetPlace(); if (dx != nullptr && dy != nullptr) { - std::vector ins = {&dout, &out, &y}; + std::vector ins = {&dout, &x, &y}; GetGradXAndYOut( dev_ctx, place, @@ -51,7 +51,7 @@ void DivideGradKernel(const Context& dev_ctx, GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dx, funcs::DivGradXFunctor()); } else if (dy != nullptr && dx == nullptr) { - std::vector ins = {&dout, &out, &y}; + std::vector ins = {&dout, &x, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dy, funcs::DivGradYFunctor()); } -- GitLab