Repository navigation
cuda: guard the iq4_nl dequantize row kernel against short rows - #29683
Merged
JohannesGaessler merged 1 commit intoSep 30, 2026
Merged
Conversation
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.
yeahdongcn
marked this pull request as ready for review
September 30, 2026 02:18
am17an
approved these changes
Sep 30, 2026
JohannesGaessler
approved these changes
Sep 30, 2026
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]>
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.
Overview
dequantize_block_iq4_nlwritesQK_Kvalues per block and is never told how many values are left. Callers pass the total number of elements ask—ggml_cuda_mul_mat_cublas_impland the flash-attention K/V conversion passggml_nelements(...),conv2d/conv3dthe padded element count — and that number only has to be a multiple ofQK4_NL, whiledequantize_row_iq4_nl_cudalaunchesceil(k / QK_K)blocks that each writeQK_Kvalues. Whenkis not a multiple ofQK_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; forka multiple ofQK_Kthe check never fires. This is the samenb32guard thatdequantize_block_q4_0anddequantize_block_q4_1already use in this file.convert.cuhas no backend guards and all three ofggml_get_to_fp16_cuda,ggml_get_to_bf16_cudaandggml_get_to_fp32_cudareturn 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 = 192is a multiple ofQK4_NLbut not ofQK_K. Calling the dequantization entry point directly on a buffer filled with a known pattern, and counting the values it overwrites pastk:k, CUDA (H20-3e)k, MUSA (S5000)The values written for the valid elements are identical in both cases, and the
iq4_nlcases oftest-backend-ops -o MUL_MAT, includingk = 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