Skip to content

Guard GridSample CUDA coordinate conversion - #29581

Merged
Gopalakrishnan Nallasamy (GopalakrishnanN) merged 4 commits into
mainfrom
GopalakrishnanN/gridsample-int64-hardening
Jul 9, 2026
Merged

Guard GridSample CUDA coordinate conversion#29581
Gopalakrishnan Nallasamy (GopalakrishnanN) merged 4 commits into
mainfrom
GopalakrishnanN/gridsample-int64-hardening

Conversation

@GopalakrishnanN

@GopalakrishnanN Gopalakrishnan Nallasamy (GopalakrishnanN) commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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, GsReflect performs the isfinite check 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 CPU GsReflect (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*
    • 180 tests passed
  • .\.venv\Scripts\clang-format.exe on touched C++ files

Note: 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_t index 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 new GridSampleCudaHardeningTest cases run on CPU and CUDA (and CUDA-NHWC when ENABLE_CUDA_NHWC_OPS is enabled); the CUDA kernel is registered for float only, so double is exercised through the shared templated CPU path.

Gopalakrishnan Nallasamy 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 GridSample kernels to ensure float→int64 conversions are well-defined for NaN/Inf/extreme values.
  • Widened intermediate index types to int64_t where 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.

Comment thread onnxruntime/core/providers/cuda/tensor/grid_sample_impl.cu Outdated
Gopalakrishnan Nallasamy 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

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.

4 participants