Guard GridSample CUDA coordinate conversion - #29581
Merged
Gopalakrishnan Nallasamy (GopalakrishnanN) merged 4 commits intoJul 9, 2026
Merged
Guard GridSample CUDA coordinate conversion#29581Gopalakrishnan Nallasamy (GopalakrishnanN) merged 4 commits into
Gopalakrishnan Nallasamy (GopalakrishnanN) merged 4 commits into
Conversation
added 2 commits
July 6, 2026 15:38
…ge coords Port the CPU GridSample hardening (PR #28302) to the CUDA kernels: guard GsReflect against non-finite input and non-positive range and use an int64_t wrap count; add IsSafeForInt64Conversion and sanitize denormalized coordinates before integer casts; clamp reflected indices in PixelAtGrid and PixelAtGrid3D; widen bilinear/nearest index locals to int64_t. Add float-only CPU+CUDA regression tests (GridSampleCudaHardeningTest) covering nearest/bilinear/cubic reflection, zeros sanitization, the dim==1 zero-range guard, and the 3D paths. CoreML and WebGPU are excluded because their integer-conversion semantics differ.
Gopalakrishnan Nallasamy (GopalakrishnanN)
requested review from
Copilot,
Hariharan Seshadri (hariharans29) and
Tianlei Wu (tianleiwu)
July 6, 2026 23:30
Copilot started reviewing on behalf of
Gopalakrishnan Nallasamy (GopalakrishnanN)
July 6, 2026 23:30
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the CUDA implementation of the ONNX GridSample operator against non-finite and extreme grid coordinates, aligning CUDA behavior with existing CPU float-grid handling and preventing undefined behavior during float→integer conversions.
Changes:
- Added device-side guards and coordinate sanitization in CUDA
GridSamplekernels to ensure float→int64 conversions are well-defined for NaN/Inf/extreme values. - Widened intermediate index types to
int64_twhere needed and clamped reflected indices before sampling to avoid out-of-range accesses. - Added regression tests that run on CPU and (when enabled) CUDA EPs to cover these adversarial coordinate cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
onnxruntime/test/providers/cpu/tensor/grid_sample_test_custom.cc |
Adds CUDA-focused hardening regression tests and a CPU+CUDA EP runner helper (excluding CoreML/WebGPU due to differing semantics). |
onnxruntime/core/providers/cuda/tensor/grid_sample_impl.cu |
Sanitizes unsafe coordinates, switches key index computations to int64_t, guards reflection math, and clamps reflected indices before sampling. |
added 2 commits
July 7, 2026 11:37
Addresses PR review feedback: reorder GsReflect so x_max - x_min is only computed after the isfinite(fx) early-return, avoiding an unnecessary subtraction for non-finite coordinates. Behavior is unchanged.
Mirror the CUDA GsReflect reorder so the CPU and CUDA implementations stay in lockstep: compute x_max - x_min only after the isfinite(fx) early-return. Behavior is unchanged.
Gopalakrishnan Nallasamy (GopalakrishnanN)
requested review from
Copilot and
Xavier Dupré (xadupre)
July 7, 2026 20:47
Copilot started reviewing on behalf of
Gopalakrishnan Nallasamy (GopalakrishnanN)
July 7, 2026 20:47
View session
Justin Chu (justinchuby)
approved these changes
Jul 8, 2026
Gopalakrishnan Nallasamy (GopalakrishnanN)
merged commit Jul 9, 2026
f631f0d
into
main
92 checks passed
Gopalakrishnan Nallasamy (GopalakrishnanN)
deleted the
GopalakrishnanN/gridsample-int64-hardening
branch
July 9, 2026 01:07
This was referenced Aug 12, 2026
Closed
This was referenced Aug 17, 2026
Open
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
Guards the CUDA GridSample coordinate conversion paths for non-finite and extreme grid values, matching the existing CPU behavior for float grids. The CUDA path now sanitizes coordinates before integer conversion, uses wider intermediate indices where needed, and clamps reflected indices before sampling.
Per review feedback,
GsReflectperforms theisfinitecheck before computing the reflection range (x_max - x_min), so a non-finite coordinate returns early without the extra subtraction. The same reorder is applied to the CPUGsReflect(core/providers/cpu/tensor/grid_sample.cc) to keep the CPU and CUDA implementations in lockstep; behavior is unchanged.Tests
.\.venv\Scripts\python.exe tools\ci_build\build.py --config RelWithDebInfo --build --parallel --target onnxruntime_provider_test --build_dir build\Windows.\build\Windows\RelWithDebInfo\RelWithDebInfo\onnxruntime_provider_test.exe --gtest_filter=*Grid*.\.venv\Scripts\clang-format.exeon touched C++ filesNote: the local build is CPU-only (
onnxruntime_USE_CUDA=OFF), so CUDA compilation/runtime coverage will come from CUDA CI.Test coverage
The hardening is applied at shared choke-points: coordinate sanitization runs before interpolation-mode dispatch, and the
int64_tindex widening and reflected-index clamps are single shared branches. So one representative case per {mode, padding, dimensionality} exercises the hardened path rather than the full cross-product. Regression tests use constant-valued images, so the expected output is well-defined regardless of which (now sanitized/clamped) index each adversarial coordinate resolves to; this also sidesteps a pre-existing CPU-vs-CUDA reflected-index difference (double-reflect + round-half-to-even). The newGridSampleCudaHardeningTestcases run on CPU and CUDA (and CUDA-NHWC whenENABLE_CUDA_NHWC_OPSis enabled); the CUDA kernel is registered forfloatonly, sodoubleis exercised through the shared templated CPU path.