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 edge case in GridSample border padding mode #10628

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions onnxruntime/contrib_ops/cpu/grid_sample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ Status GridSample<T>::Compute(OpKernelContext* context) const {
}

if (x < x_min || x > x_max || y < y_min || y > y_max) { // out of bound
if (padding_mode_ == Border) {
// use original border in both align_corner cases
x = std::clamp(x, static_cast<T>(0), static_cast<T>(W_in - 1));
y = std::clamp(y, static_cast<T>(0), static_cast<T>(H_in - 1));
} else if (padding_mode_ == Reflection) {
if (padding_mode_ == Reflection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's better to add some comments to explain why we only need the Reflection padding_mode_ here..

x = GsReflect(x, x_min, x_max);
y = GsReflect(y, y_min, y_max);
}
Expand Down
5 changes: 1 addition & 4 deletions onnxruntime/contrib_ops/cuda/grid_sample_impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ __global__ void _GridSampleKernel(
float border[] = {x_min, y_min, x_max, y_max}; // l-t-r-b
if (grid_x_imgSpace < x_min || grid_x_imgSpace > x_max ||
grid_y_imgSpace < y_min || grid_y_imgSpace > y_max) { // out of bound
if (padding_mode == 1) { // border
grid_x_imgSpace = max(0.0f, min(grid_x_imgSpace, W_in - 1.0f));
grid_y_imgSpace = max(0.0f, min(grid_y_imgSpace, H_in - 1.0f));
} else if (padding_mode == 2) { // reflection
if (padding_mode == 2) { // reflection
grid_x_imgSpace = GsReflect(grid_x_imgSpace, x_min, x_max);
grid_y_imgSpace = GsReflect(grid_y_imgSpace, y_min, y_max);
}
Expand Down
23 changes: 23 additions & 0 deletions onnxruntime/test/contrib_ops/gridsample_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ TEST(GridsampleContribOpTest, gridsample_mode_bicubic) {
test.Run();
}

TEST(GridsampleContribOpTest, gridsample_mode_bicubic_border) {
OpTester test("GridSample", 1, kMSDomain);
test.AddInput<float>("X", {1, 1, 3, 2}, {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f});
test.AddInput<float>("Grid", {1, 4, 4, 2},
{-1.0000f, -1.0000f, -0.5000f, -0.5000f,
-0.2000f, -0.2000f, 0.0000f, 0.0000f,
0.0000f, 0.0000f, -0.2000f, -0.2000f,
0.5000f, 0.5000f, 1.0000f, 1.0000f,
-1.100f, 0.4000f, -0.400f, -1.100f,
1.100f, -0.400f, 0.400f, 1.100f,
-10.000f, 0.000f, 10.000f, 0.000f,
0.000f, -10.000f, 0.000f, 10.000f});
test.AddAttribute("mode", "bicubic");
test.AddAttribute("padding_mode", "border");
test.AddOutput<float>("Y", {1, 1, 4, 4},
{-0.2812f, 0.3828f, 1.5005f, 2.5000f,
2.5000f, 1.5005f, 4.6172f, 5.2812f,
3.2960f, -0.0374f, 1.7040f, 5.0374f,
2.0000f, 3.0000f, 0.5000f, 4.5000f});

test.Run();
}

} // namespace test
} // namespace onnxruntime