Validate GatherBlockQuantized CUDA indices bounds - #31645
Open
apsonawane wants to merge 1 commit into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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 fromComputeInternal. - 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>(); | ||
| } |
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.
This pull request adds validation for index ranges in the CUDA implementation of the
GatherBlockQuantizedoperator 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
ValidateIndicesRangeForCudatemplate function ingather_block_quantized.ccto 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.GatherBlockQuantized::ComputeInternalmethod, ensuring validation is performed before computation proceeds.Unit Test Enhancements
Test_NegativeInvalidIndices_WithZeroPointstest helper to cover cases with negative out-of-bounds indices.InvalidIndices_CudaandNegativeInvalidIndices_Cuda) to verify that invalid indices (including negative values) are correctly detected and handled on CUDA devices.