[CUDA] Fix CUDA GridSample reflection for degenerate dimensions#27639
Open
[CUDA] Fix CUDA GridSample reflection for degenerate dimensions#27639
Conversation
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
onnxruntime/core/providers/cuda/tensor/grid_sample_impl.cuonnxruntime/core/providers/cuda/tensor/grid_sample.ccmodeattribute read from the CUDA kernel constructor.Regression tests
onnxruntime/test/providers/cpu/tensor/grid_sample_test_custom.inc1x1/1x1x1spatial shapes.Testing
onnxruntime_provider_testwith CUDA support:cmake --build build/cuda/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_provider_test -- -j96build/cuda/RelWithDebInfo/onnxruntime_provider_test --gtest_filter="*degenerate*"build/cuda/RelWithDebInfo/onnxruntime_provider_test --gtest_filter="*GridSample*"104GridSample 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
+1index back into the only valid element, covering the path that was previously unsafe.