Validate MatMulNBits 8-bit g_idx bounds on CUDA - #31643
Conversation
Add missing rid bounds assert/clamp in Dequantize8BitsKernelReOrder and validate non-CPU g_idx tensors on host before launch. Includes CUDA regression tests for out-of-range and negative g_idx values.
There was a problem hiding this comment.
Pull request overview
This PR hardens the CUDA MatMulNBits (8-bit blockwise) path by validating the g_idx/group_index input to prevent out-of-bounds indexing into per-block scales/zero_points, and adds negative tests to ensure invalid indices fail with a clear error.
Changes:
- Added host-side
group_indexrange validation for CUDA tensors inMatMulNBits<T>::ComputeInternal()(pre-kernel launch). - Added device-side assertion + clamping in the reorder dequantization CUDA kernel as defense-in-depth.
- Added CUDA tests that expect failures for negative and out-of-range
g_idxvalues.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| onnxruntime/test/contrib_ops/matmul_8bits_test.cc | Adds CUDA tests asserting invalid g_idx causes operator failure. |
| onnxruntime/contrib_ops/cuda/quantization/matmul_nbits.cc | Adds CUDA-side group_index validation before launching dequant/GEMM. |
| onnxruntime/contrib_ops/cuda/quantization/dequantize_blockwise_8bits.cu | Adds kernel-level assert/clamp around rid (group index) usage. |
Add CUDA graph capture detection to the group_index validation helper so we fail fast with a clear ORT status instead of performing a host round-trip and stream synchronization during capture. Also switch the device-to-host copy to an explicit cudaMemcpyDeviceToHost for clarity. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tianlei Wu (tianleiwu)
left a comment
There was a problem hiding this comment.
Requesting changes because the current validation path makes g_idx models incompatible with CUDA graph capture and adds a blocking device-to-host validation round trip to the inference hot path. Details inline.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tianlei Wu (tianleiwu)
left a comment
There was a problem hiding this comment.
Thanks for moving g_idx validation out of the inference hot path; the rebuilt CUDA provider tests run both new invalid-range cases successfully. Two initialization-path regressions remain: unchecked attribute arithmetic can fault before normal input validation, and valid initializer-based models fail whenever prepacking is disabled. Details inline.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Major review finding
The validation result is cached for the lifetime of the kernel, but
Please validate every non-prepacked runtime |
Tianlei Wu (tianleiwu)
left a comment
There was a problem hiding this comment.
One CUDA test-isolation issue remains. The implementation now addresses the earlier bounds, initialization, and graph-capture concerns, but the new regression tests do not prove that the CUDA kernel owns the node. Details inline.
This pull request improves the robustness and reliability of the CUDA implementation for blockwise 8-bit quantized matrix multiplication in ONNX Runtime. The main focus is on validating the group index (
g_idx) input to prevent out-of-bounds memory access, and on adding tests to verify error handling for invalid group indices.Validation and Error Handling Improvements:
ValidateGroupIndexRangeForCudainmatmul_nbits.ccto check that all values in thegroup_indextensor are within the valid range[0, k_blocks), returning an error if any value is out of bounds. This is called before launching the CUDA kernel. [1] [2]Dequantize8BitsKernelReOrder, added an assertion and clamping to ensure thatrid(the group index) is within the valid range, further protecting against invalid memory access.Testing Enhancements:
matmul_8bits_test.ccto verify that the operator fails as expected wheng_idxcontains out-of-range or negative values, ensuring the new validation logic is exercised.Miscellaneous:
<vector>header to support host-side validation logic.These changes collectively improve the safety and reliability of quantized matrix multiplication on CUDA by proactively catching invalid input and providing clear error messages.