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

[BugFix] Add boundary safety check for grid_sample_kernel #62891

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 4 additions & 7 deletions paddle/phi/kernels/gpu/grid_sample_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,13 @@ ComputePositionsWithMask(T coord,
coord = ClipIndexesWithMask(coord, size, &grad_clip);
*grad_in = (*grad_in) * grad_clip;
} else if (padding_mode == PaddingMode::reflect) {
if (align_corners) {
coord = ReflectIndexesWithMask(coord, 0, 2 * (size - 1), &grad_refl);
} else {
coord = ReflectIndexesWithMask(coord, -1, 2 * size - 1, &grad_refl);
}
coord = align_corners
? ReflectIndexesWithMask(coord, 0, 2 * (size - 1), &grad_refl)
: ReflectIndexesWithMask(coord, -1, 2 * size - 1, &grad_refl);
coord = ClipIndexesWithMask(coord, size, &grad_clip);
*grad_in = (*grad_in) * grad_refl * grad_clip;
}

return coord;
return SafeDownGradeToIntRange(coord);
}

template <typename T>
Expand Down
28 changes: 9 additions & 19 deletions paddle/phi/kernels/gpu/grid_sample_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ template <typename T>
static __forceinline__ __device__ T Unnormalize(T coord,
int size,
bool align_corners) {
if (align_corners) {
return ((coord + 1.f) / 2) * (size - 1);
} else {
return ((coord + 1.f) * size - 1) / 2;
}
return align_corners ? ((coord + 1.f) / 2) * (size - 1)
: ((coord + 1.f) * size - 1) / 2;
}

template <typename T>
static __forceinline__ __device__ T ClipIndexes(T in, int max_value) {
return min(static_cast<T>(max_value), max(in, static_cast<T>(0)));
return min(static_cast<T>(max_value - 1), max(in, static_cast<T>(0)));
}

template <typename T>
Expand All @@ -51,11 +48,7 @@ static __forceinline__ __device__ T ReflectIndexes(T in,
in = fabs(in - min);
T extra = fmod(in, span);
int flips = static_cast<int>(floor(in / span));
if (flips % 2 == 0) {
return extra + min;
} else {
return span - extra + min;
}
return (flips & 1) ? span - extra + min : extra + min; // cond ? odd : even
}

template <typename T>
Expand All @@ -65,16 +58,13 @@ static __forceinline__ __device__ T ComputePositions(T coord,
bool align_corners) {
coord = Unnormalize<T>(coord, size, align_corners);
if (padding_mode == PaddingMode::border) {
coord = ClipIndexes(coord, size - 1);
coord = ClipIndexes(coord, size);
} else if (padding_mode == PaddingMode::reflect) {
if (align_corners) {
coord = ReflectIndexes(coord, 0, 2 * (size - 1));
} else {
coord = ReflectIndexes(coord, -1, 2 * size - 1);
}
coord = ClipIndexes(coord, size - 1);
coord = align_corners ? ReflectIndexes(coord, 0, 2 * (size - 1))
: ReflectIndexes(coord, -1, 2 * size - 1);
coord = ClipIndexes(coord, size);
}
return coord;
return SafeDownGradeToIntRange(coord);
}

template <typename T>
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/gpu/grid_sample_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@

#pragma once

#include <limits.h>

namespace phi {

enum class Mode {
bilinear,
nearest,
};

template <typename T>
__forceinline__ __device__ T SafeDownGradeToIntRange(T x) {
bool unsafe_cond =
x > INT_MAX - 1 || x < INT_MIN || !::isfinite(static_cast<double>(x));
return unsafe_cond ? static_cast<T>(-100.0) : x;
}

enum class PaddingMode { zeros, border, reflect };

static __forceinline__ __device__ bool InBounds(int h, int w, int H, int W) {
Expand Down