Skip to content

cuda: guard the iq4_nl dequantize row kernel against short rows - #29683

Merged
JohannesGaessler merged 1 commit into
ggml-org:masterfrom
yeahdongcn:xd/cuda-iq4-nl-short-rows
Sep 30, 2026
Merged

JohannesGaessler merged 1 commit into
ggml-org:masterfrom
yeahdongcn:xd/cuda-iq4-nl-short-rows

Conversation

@yeahdongcn

@yeahdongcn yeahdongcn commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

dequantize_block_iq4_nl writes QK_K values per block and is never told how many values are left. Callers pass the total number of elements as k — ggml_cuda_mul_mat_cublas_impl and the flash-attention K/V conversion pass ggml_nelements(...), conv2d/conv3d the padded element count — and that number only has to be a multiple of QK4_NL, while dequantize_row_iq4_nl_cuda launches ceil(k / QK_K) blocks that each write QK_K values. When k is not a multiple of QK_K, the last block reads and writes past the end of the row and of the output buffer.

Skip the sub-blocks that start at or past k; for k a multiple of QK_K the check never fires. This is the same nb32 guard that dequantize_block_q4_0 and dequantize_block_q4_1 already use in this file.

convert.cu has no backend guards and all three of ggml_get_to_fp16_cuda, ggml_get_to_bf16_cuda and ggml_get_to_fp32_cuda return this kernel, so it applies to CUDA, HIP and MUSA alike. Split out of #29508, where it was carried over from #29292, because the fix is not MUSA specific.

Testing Done

k = 192 is a multiple of QK4_NL but not of QK_K. Calling the dequantization entry point directly on a buffer filled with a known pattern, and counting the values it overwrites past k:

build bytes written past k, CUDA (H20-3e) bytes written past k, MUSA (S5000)
master (19e28a2) 128 128
this PR 0 0
[iq4nl-canary] k=192 valid_written=192 bytes_written_past_k=128 canary=896 bytes   # master
[iq4nl-canary] k=192 valid_written=192 bytes_written_past_k=0   canary=896 bytes   # this PR

The values written for the valid elements are identical in both cases, and the iq4_nl cases of test-backend-ops -o MUL_MAT, including k = 192, pass before and after the change, so this is an out of bounds read and write rather than a wrong result. NVIDIA H20-3e with CUDA 12.8 (sm_90); MTT S5000 with MUSA SDK 5.2.0.

The measurement is a debug-only hook in a scratch copy of master that calls ggml_get_to_fp16_cuda(GGML_TYPE_IQ4_NL); it is not part of this PR.

Additional information

Requirements

dequantize_block_iq4_nl writes QK_K values per block, but a row can be shorter than that (an IQ4_NL row is only guaranteed to be a multiple of QK4_NL). Threads whose 32-value sub-block starts at or past k currently read and write past the end of the row. Skip those sub-blocks; for rows that are a multiple of QK_K the check never fires.
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Sep 30, 2026
@yeahdongcn
yeahdongcn marked this pull request as ready for review September 30, 2026 02:18
@yeahdongcn
yeahdongcn requested a review from a team as a code owner September 30, 2026 02:18
@CISC
CISC removed the request for review from slaren September 30, 2026 07:09
@JohannesGaessler
JohannesGaessler merged commit f872b59 into ggml-org:master Sep 30, 2026
11 of 12 checks passed
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 4, 2026
…-org#29683)

dequantize_block_iq4_nl writes QK_K values per block, but a row can be shorter than that (an IQ4_NL row is only guaranteed to be a multiple of QK4_NL). Threads whose 32-value sub-block starts at or past k currently read and write past the end of the row. Skip those sub-blocks; for rows that are a multiple of QK_K the check never fires.

(cherry picked from commit f872b59)
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 21, 2026
…t rows

The plain dequantize_block_iq4_nl and dequantize_block_mxfp4 write
QK_K values per group of 32 threads, but the count is only a multiple
of 32. Threads whose 32-value block starts at or past the end read and
wrote past the buffers. Skip those blocks, as the q4_0 kernel does.
Same fix as CUDA ggml-org#29683 for iq4_nl; mxfp4 has the same layout.

Reached by MUL_MAT with a weight that is not reordered (more than 8
columns) and rows * cols not a multiple of 256.

MUL_MAT with q4_0 / iq4_nl / mxfp4, 32 columns, k = 2080 and 32 / 33 /
39 rows (0, 1 and 7 blocks in the last group): 9/9. MUL_MAT 1522/1522.

Assisted-by: Claude Opus 5.5 (1M context) <[email protected]>
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 22, 2026
…t rows

The plain dequantize_block_iq4_nl and dequantize_block_mxfp4 write
QK_K values per group of 32 threads, but the count is only a multiple
of 32. Threads whose 32-value block starts at or past the end read and
wrote past the buffers. Skip those blocks, as the q4_0 kernel does.
Same fix as CUDA ggml-org#29683 for iq4_nl; mxfp4 has the same layout.

Reached by MUL_MAT with a weight that is not reordered (more than 8
columns) and rows * cols not a multiple of 256.

MUL_MAT with q4_0 / iq4_nl / mxfp4, 32 columns, k = 2080 and 32 / 33 /
39 rows (0, 1 and 7 blocks in the last group): 9/9. MUL_MAT 1522/1522.

Assisted-by: Claude Opus 5.5 (1M context) <[email protected]>
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 29, 2026
…t rows

The plain dequantize_block_iq4_nl and dequantize_block_mxfp4 write
QK_K values per group of 32 threads, but the count is only a multiple
of 32. Threads whose 32-value block starts at or past the end read and
wrote past the buffers. Skip those blocks, as the q4_0 kernel does.
Same fix as CUDA ggml-org#29683 for iq4_nl; mxfp4 has the same layout.

Reached by MUL_MAT with a weight that is not reordered (more than 8
columns) and rows * cols not a multiple of 256.

MUL_MAT with q4_0 / iq4_nl / mxfp4, 32 columns, k = 2080 and 32 / 33 /
39 rows (0, 1 and 7 blocks in the last group): 9/9. MUL_MAT 1522/1522.

Assisted-by: Claude Opus 5.5 (1M context) <[email protected]>
iki007 added a commit to iki007/llama.cpp that referenced this pull request Nov 4, 2026
…t rows

The plain dequantize_block_iq4_nl and dequantize_block_mxfp4 write
QK_K values per group of 32 threads, but the count is only a multiple
of 32. Threads whose 32-value block starts at or past the end read and
wrote past the buffers. Skip those blocks, as the q4_0 kernel does.
Same fix as CUDA ggml-org#29683 for iq4_nl; mxfp4 has the same layout.

Reached by MUL_MAT with a weight that is not reordered (more than 8
columns) and rows * cols not a multiple of 256.

MUL_MAT with q4_0 / iq4_nl / mxfp4, 32 columns, k = 2080 and 32 / 33 /
39 rows (0, 1 and 7 blocks in the last group): 9/9. MUL_MAT 1522/1522.

Assisted-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants