Skip to content

Validate GatherBlockQuantized CUDA indices bounds - #31645

Open
apsonawane wants to merge 1 commit into
mainfrom
msrc/cuda-gatherblockquantized-indices-bounds-fix
Open

Validate GatherBlockQuantized CUDA indices bounds#31645
apsonawane wants to merge 1 commit 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 on lines +33 to +38
for (size_t i = 0; i < indices_size; ++i) {
const int64_t indices_val = static_cast<int64_t>(indices_host[i]);
ORT_RETURN_IF_NOT(indices_val >= -gather_axis_dim && indices_val < gather_axis_dim,
"indices element out of data bounds, idx=", indices_val,
" must be within the inclusive range [", -gather_axis_dim, ",", gather_axis_dim - 1, "]");
}
Comment on lines +22 to +31
if (indices->Location().device.Type() == OrtDevice::CPU) {
const Tind* indices_ptr = indices->Data<Tind>();
for (size_t i = 0; i < indices_size; ++i) {
indices_host[i] = indices_ptr[i];
}
} else {
CUDA_RETURN_IF_ERROR(cudaMemcpyAsync(indices_host.data(), indices->Data<Tind>(), indices_size * sizeof(Tind),
cudaMemcpyDefault, stream));
CUDA_RETURN_IF_ERROR(cudaStreamSynchronize(stream));
}
Comment on lines +538 to +542
TEST(GatherBlockQuantizedOpTest, InvalidIndices_Cuda) {
Test_InvalidIndices_WithZeroPoints<UInt4x2, float, int32_t>();
Test_InvalidIndices_WithZeroPoints<UInt4x2, float, int64_t>();
Test_InvalidIndices_WithZeroPoints<uint8_t, float, int32_t>();
}
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