Skip to content

Validate GatherBlockQuantized CUDA indices bounds - #31645

Merged
Akshay Sonawane (apsonawane) merged 4 commits into
mainfrom
msrc/cuda-gatherblockquantized-indices-bounds-fix
Aug 11, 2026
Merged

Validate GatherBlockQuantized CUDA indices bounds#31645
Akshay Sonawane (apsonawane) merged 4 commits into
mainfrom
msrc/cuda-gatherblockquantized-indices-bounds-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request adds validation for index ranges in the CUDA implementation of the GatherBlockQuantized operator to prevent out-of-bounds memory access. It also introduces new unit tests to ensure the correctness of this validation, including cases with negative indices. The most important changes are grouped below:

CUDA Index Range Validation

  • Added the ValidateIndicesRangeForCuda template function in gather_block_quantized.cc to check that all indices are within the valid range for the gather axis, supporting both CPU and CUDA memory. This prevents invalid memory access during CUDA kernel execution.
  • Integrated index validation into the GatherBlockQuantized::ComputeInternal method, ensuring validation is performed before computation proceeds.

Unit Test Enhancements

  • Added the Test_NegativeInvalidIndices_WithZeroPoints test helper to cover cases with negative out-of-bounds indices.
  • Introduced new CUDA-specific tests (InvalidIndices_Cuda and NegativeInvalidIndices_Cuda) to verify that invalid indices (including negative values) are correctly detected and handled on CUDA devices.

Add CPU-equivalent indices range validation for CUDA path, including GPU-resident indices copied to host, to prevent out-of-range device reads.

Add CUDA regression tests for out-of-range and negative indices.

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 aims to prevent out-of-bounds memory access in the CUDA implementation of the contrib GatherBlockQuantized op by validating index ranges before launching the CUDA kernel, and adds unit tests for invalid indices (including negative invalid indices).

Changes:

  • Added a ValidateIndicesRangeForCuda<Tind> helper in the CUDA kernel implementation and invoked it from ComputeInternal.
  • Added new test helper for negative invalid indices and introduced CUDA-guarded test cases for invalid indices.

Reviewed changes

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

File Description
onnxruntime/contrib_ops/cuda/quantization/gather_block_quantized.cc Adds host-side (CPU/GPU input aware) indices range validation and calls it before kernel launch.
onnxruntime/test/contrib_ops/gather_block_quantized_op_test.cc Adds negative-invalid-index test helper and CUDA-guarded test cases for invalid indices.
Suppressed comments (1)

onnxruntime/test/contrib_ops/gather_block_quantized_op_test.cc:548

  • Same as InvalidIndices_Cuda: this test may fall back to CPU EP (and still pass) when CUDA isn't available, so it doesn't guarantee coverage of the CUDA validation path. Add a CUDA availability skip (or force CUDA EP only).
TEST(GatherBlockQuantizedOpTest, NegativeInvalidIndices_Cuda) {
  Test_NegativeInvalidIndices_WithZeroPoints<UInt4x2, float, int32_t>();
  Test_NegativeInvalidIndices_WithZeroPoints<UInt4x2, float, int64_t>();
  Test_NegativeInvalidIndices_WithZeroPoints<uint8_t, float, int32_t>();
}

Comment thread onnxruntime/contrib_ops/cuda/quantization/gather_block_quantized.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/quantization/gather_block_quantized.cc Outdated
Comment thread onnxruntime/test/contrib_ops/gather_block_quantized_op_test.cc Outdated
Fixes the remaining PR #31645 comments:
- Normalize negative indices in the CUDA kernel before address arithmetic so valid
  negative gather indices follow CPU semantics instead of producing OOB access.
- Use explicit cudaMemcpyDeviceToHost in the CUDA validation helper and skip the
  validation path for empty indices tensors.
- Make the CUDA-specific tests explicitly skip when CUDA is unavailable so they
  cannot silently fall back to CPU execution.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread onnxruntime/contrib_ops/cuda/quantization/gather_block_quantized.cc Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants