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 p_norm gpu nan bug while divide zero #41804

Merged
merged 1 commit into from
Apr 15, 2022
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
12 changes: 7 additions & 5 deletions paddle/phi/kernels/gpu/p_norm_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ struct AbsMaxAndMinGradFunctor {

template <typename T>
struct PNormGradFunctor {
HOSTDEVICE explicit inline PNormGradFunctor(float porder) {
HOSTDEVICE explicit inline PNormGradFunctor(float porder, float eps) {
this->porder = static_cast<T>(porder - 1.);
this->eps = static_cast<T>(eps);
}
template <typename Context,
typename X,
Expand All @@ -58,11 +59,12 @@ struct PNormGradFunctor {
DY* dy,
const Dim& dim,
int size) {
dx->device(place) = (*x).abs().pow(this->porder) * (*x).sign() *
dy->broadcast(dim) *
(*y).pow(-this->porder).broadcast(dim);
dx->device(place) =
(*x).abs().pow(this->porder) * (*x).sign() * dy->broadcast(dim) *
(*y + y->constant(eps)).pow(-this->porder).broadcast(dim);
}
T porder;
T eps;
};

template <typename T, typename Context>
Expand Down Expand Up @@ -96,7 +98,7 @@ void PNormGradKernel(const Context& dev_ctx,
dev_ctx, in_x, in_norm, in_norm_dy, out_dx, functor, dims, reduce_all);

} else {
auto functor = PNormGradFunctor<T>(porder);
auto functor = PNormGradFunctor<T>(porder, epsilon);
funcs::LaunchReduceGradKernel<Context, T, PNormGradFunctor<T>>(
dev_ctx, in_x, in_norm, in_norm_dy, out_dx, functor, dims, reduce_all);
}
Expand Down