Skip to content

[CUDA] Fix CUDA GridSample reflection for degenerate dimensions#27639

Open
tianleiwu wants to merge 3 commits intomainfrom
tlwu/cuda_3d_grid_sample_refine
Open

[CUDA] Fix CUDA GridSample reflection for degenerate dimensions#27639
tianleiwu wants to merge 3 commits intomainfrom
tlwu/cuda_3d_grid_sample_refine

Conversation

@tianleiwu
Copy link
Contributor

Description

This change hardens the CUDA GridSample reflection path when one or more sampled spatial dimensions have size 1. The CUDA kernels now avoid invalid reflection math for degenerate dimensions, and the test suite adds targeted 4D and 5D regression coverage so the failure mode is exercised on CUDA providers.

Summary of Changes

CUDA GridSample kernel updates

File Change
onnxruntime/core/providers/cuda/tensor/grid_sample_impl.cu Guard reflection handling for size-1 dimensions in 2D and 3D sampling helpers, clamp reflected indices into valid ranges, and clean up helper/kernel signatures.
onnxruntime/core/providers/cuda/tensor/grid_sample.cc Remove an unused mode attribute read from the CUDA kernel constructor.

Regression tests

File Change
onnxruntime/test/providers/cpu/tensor/grid_sample_test_custom.inc Run the custom opset-20 boundary tests on all execution providers and add new reflection regression tests for 4D opset-20 and 5D opset-22 inputs with degenerate 1x1 / 1x1x1 spatial shapes.

Testing

  • Rebuilt onnxruntime_provider_test with CUDA support:
    cmake --build build/cuda/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_provider_test -- -j96
  • Verified the new regression cases:
    build/cuda/RelWithDebInfo/onnxruntime_provider_test --gtest_filter="*degenerate*"
  • Ran the full GridSample provider suite:
    build/cuda/RelWithDebInfo/onnxruntime_provider_test --gtest_filter="*GridSample*"
  • Local result: 104 GridSample tests passed.

Motivation and Context

Reviewer feedback called out that the CUDA GridSample fix needed direct regression coverage. The added tests intentionally use degenerate spatial dimensions under reflection padding so bilinear/trilinear neighbor fetches must reflect the out-of-bounds +1 index back into the only valid element, covering the path that was previously unsafe.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens CUDA GridSample’s reflection padding behavior when one or more spatial input dimensions are degenerate (size 1), preventing invalid reflection math and adding regression tests that exercise the CUDA path for both 2D (4D tensors) and 3D (5D tensors) sampling.

Changes:

  • Add explicit handling for size-1 dimensions in CUDA GridSample reflection padding (2D + 3D helpers), including clamping reflected indices into valid ranges.
  • Minor CUDA kernel signature cleanup (__restrict__ pointers) and remove an unused attribute read in the CUDA kernel constructor.
  • Add targeted regression tests for degenerate spatial shapes under reflection padding (4D opset-20 and 5D opset-22), and run the existing custom boundary tests across the standard EP set used by the GridSample test suite.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
onnxruntime/core/providers/cuda/tensor/grid_sample_impl.cu Guards reflection for degenerate spatial dimensions to avoid division-by-zero in GsReflect and clamps indices post-reflection.
onnxruntime/core/providers/cuda/tensor/grid_sample.cc Removes an unused pre-read of the mode attribute; constructor still correctly parses mode by opset.
onnxruntime/test/providers/cpu/tensor/grid_sample_test_custom.inc Adds CUDA-relevant regression coverage for reflection + degenerate spatial dims and aligns custom tests with the suite’s GetExecutionProviders() helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants