Skip to content

Validate GridSample input spatial dimensions are non-empty#29255

Open
titaiwangms wants to merge 4 commits into
microsoft:mainfrom
titaiwangms:validate-gridsample-spatial-dims
Open

Validate GridSample input spatial dimensions are non-empty#29255
titaiwangms wants to merge 4 commits into
microsoft:mainfrom
titaiwangms:validate-gridsample-spatial-dims

Conversation

@titaiwangms

Copy link
Copy Markdown
Contributor

Description

GridSample sizes its output from the grid, so an input with a zero-size spatial dimension skips the empty-output early return and reaches interpolation with an invalid spatial extent. This change validates that every input spatial dimension is non-empty in both the CPU and CUDA kernels, returning a clear error for such malformed inputs.

Changes

  • CPU GridSample::Compute: reject any zero-size input spatial dimension with a descriptive error.
  • CUDA GridSample::ComputeInternal: same validation, layout-aware for NCHW and NHWC, covering both 4-D (H, W) and 5-D (D, H, W) inputs. Messages are consistent across both EPs.
  • Add an expect-failure test (zero-size spatial dim, padding_mode=border) scoped to the CPU and CUDA execution providers, which are the kernels that carry the guard.

Motivation

Improves input validation and error diagnostics for malformed GridSample inputs. Covers both the CPU and CUDA execution providers; no behavior change for valid inputs.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

titaiwangms and others added 3 commits June 25, 2026 01:35
…UDA kernels)

Add ORT_RETURN_IF_NOT guards that every input spatial dimension is greater
than zero in GridSample::Compute (CPU) and GridSample::ComputeInternal
(CUDA), covering 2D and 3D-spatial inputs in both NCHW and NHWC layouts.
The output is sized by the grid, so a zero-size input spatial dimension
would otherwise skip the empty-output early return and reach interpolation
with an invalid extent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
GridSample sizes its output from the grid, so a zero-size input spatial
dimension skips the empty-output early return and reaches the
clamp/reflect index computation with an invalid extent. Add an
ORT_RETURN_IF_NOT check that every input spatial dimension is greater
than zero in both the CPU Compute and the CUDA ComputeInternal (covering
2D and 3D-spatial inputs, NCHW and NHWC layouts), with a consistent
diagnostic message. Add an expect-failure OpTester case that fans across
the registered CPU and CUDA execution providers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Agent-signed-off: Developer (cc69a935) [claude-opus-4.8 via copilot]
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… EPs

The zero-spatial-dimension validation guard currently lives only in the
CPU and CUDA GridSample kernels. GetExecutionProviders() also adds the
WebGPU and CoreML kernels on builds that enable them, and those separate
kernels do not carry the guard, so the kExpectFailure substring
assertion would falsely fail there. Add a CPU+CUDA-only provider helper
and use it for the expect-failure case; positive tests are unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Agent-signed-off: Developer (cc69a935) [claude-opus-4.8 via copilot]
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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 tightens input validation for the GridSample operator by rejecting malformed inputs where any input spatial dimension is zero, which previously could bypass the “empty output” early return (since output shape is derived from the grid) and reach interpolation with invalid spatial extents.

Changes:

  • Add input-shape validation in CPU GridSample::Compute to reject any zero-size spatial dimension.
  • Add equivalent validation in CUDA GridSample::ComputeInternal, handling both NCHW and NHWC layouts for 4-D and 5-D inputs.
  • Add an expect-failure unit test for the zero-spatial-dimension case, scoped to CPU and CUDA EPs.

Reviewed changes

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

File Description
onnxruntime/core/providers/cpu/tensor/grid_sample.cc Adds runtime validation to reject zero-size spatial dimensions before sampling/interpolation.
onnxruntime/core/providers/cuda/tensor/grid_sample.cc Adds the same validation for CUDA (layout-aware for NCHW/NHWC, 4-D/5-D).
onnxruntime/test/providers/cpu/tensor/grid_sample_test_custom.cc Adds an expect-failure test and helpers to run it only on EPs that implement the guard (CPU/CUDA).

Comment thread onnxruntime/core/providers/cpu/tensor/grid_sample.cc
Comment thread onnxruntime/core/providers/cuda/tensor/grid_sample.cc
Comment thread onnxruntime/test/providers/cpu/tensor/grid_sample_test_custom.cc
@titaiwangms titaiwangms marked this pull request as draft June 25, 2026 16:29
Extend the GridSample input-validation coverage with a 5-D case whose input
has a zero-size spatial dimension, complementing the existing 4-D case. This
exercises the 5-D branch of the non-empty-spatial-dimension check (and the
CUDA NCHW/NHWC layouts). The run is restricted to the CPU and CUDA execution
providers via GetCpuAndCudaExecutionProviders(), matching the 4-D case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@titaiwangms titaiwangms marked this pull request as ready for review June 25, 2026 17:33
@titaiwangms titaiwangms enabled auto-merge (squash) June 25, 2026 22:08
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