Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix atan2 grad when dx or dy is nullptr #56067

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions paddle/phi/kernels/impl/atan2_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ struct Atan2GradFunctor {
float x1 = static_cast<float>(x1_[idx]);
float x2 = static_cast<float>(x2_[idx]);
float x = x1 * x1 + x2 * x2;
dx1_[idx] = static_cast<T>(static_cast<float>(dout_[idx]) * x2 / x);
dx2_[idx] = static_cast<T>(-static_cast<float>(dout_[idx]) * x1 / x);
if (dx1_) {
dx1_[idx] = static_cast<T>(static_cast<float>(dout_[idx]) * x2 / x);
}
if (dx2_) {
dx2_[idx] = static_cast<T>(-static_cast<float>(dout_[idx]) * x1 / x);
}
}

const T* x1_;
Expand All @@ -56,8 +60,12 @@ struct Atan2GradFunctor<double> {

HOSTDEVICE void operator()(int64_t idx) const {
auto x = x1_[idx] * x1_[idx] + x2_[idx] * x2_[idx];
dx1_[idx] = dout_[idx] * x2_[idx] / x;
dx2_[idx] = -dout_[idx] * x1_[idx] / x;
if (dx1_) {
dx1_[idx] = dout_[idx] * x2_[idx] / x;
}
if (dx2_) {
dx2_[idx] = -dout_[idx] * x1_[idx] / x;
}
}

const double* x1_;
Expand All @@ -81,9 +89,11 @@ void Atan2GradKernel(const Context& ctx,
auto out_grad_data = out_grad.data<T>();

auto* x_grad_data =
ctx.template Alloc<T>(x_grad, size_t(x.numel() * sizeof(T)));
x_grad ? ctx.template Alloc<T>(x_grad, size_t(x.numel() * sizeof(T)))
: nullptr;
auto* y_grad_data =
ctx.template Alloc<T>(y_grad, size_t(y.numel() * sizeof(T)));
y_grad ? ctx.template Alloc<T>(y_grad, size_t(y.numel() * sizeof(T)))
: nullptr;

phi::funcs::ForRange<Context> for_range(ctx, numel);
phi::Atan2GradFunctor<T> functor(
Expand Down