Validate GridSample input spatial dimensions are non-empty#29255
Open
titaiwangms wants to merge 4 commits into
Open
Validate GridSample input spatial dimensions are non-empty#29255titaiwangms wants to merge 4 commits into
titaiwangms wants to merge 4 commits into
Conversation
…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>
Contributor
There was a problem hiding this comment.
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::Computeto 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). |
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>
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
GridSamplesizes 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
GridSample::Compute: reject any zero-size input spatial dimension with a descriptive error.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.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
GridSampleinputs. Covers both the CPU and CUDA execution providers; no behavior change for valid inputs.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com