Skip to content

Validate MatMulNBits 8-bit g_idx bounds on CUDA - #31643

Open
apsonawane wants to merge 3 commits into
mainfrom
msrc/matmulnbits8-gidx-bounds-fix
Open

Validate MatMulNBits 8-bit g_idx bounds on CUDA#31643
apsonawane wants to merge 3 commits into
mainfrom
msrc/matmulnbits8-gidx-bounds-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

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:

  • Added a new function ValidateGroupIndexRangeForCuda in matmul_nbits.cc to check that all values in the group_index tensor 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]
  • In the CUDA kernel Dequantize8BitsKernelReOrder, added an assertion and clamping to ensure that rid (the group index) is within the valid range, further protecting against invalid memory access.

Testing Enhancements:

  • Added two new tests in matmul_8bits_test.cc to verify that the operator fails as expected when g_idx contains out-of-range or negative values, ensuring the new validation logic is exercised.

Miscellaneous:

  • Included the <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.

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.
@apsonawane
apsonawane requested review from tianleiwu and a lite review from Copilot August 4, 2026 22:05

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 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_index range validation for CUDA tensors in MatMulNBits<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_idx values.

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.

Comment thread onnxruntime/contrib_ops/cuda/quantization/matmul_nbits.cc
@apsonawane
apsonawane enabled auto-merge (squash) August 5, 2026 17:41
apsonawane and others added 2 commits August 5, 2026 10:41
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>
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