Repository navigation
CUDA mmvq: guard the last row block by the rows of dst, so a MUL_MAT_ID tail at one token is computed once - #11
Conversation
…n stride, so a MUL_MAT_ID tail is computed once mul_mat_vec_q guards its last block of rows with `row0 + i < stride_col_dst`. For a plain MUL_MAT that stride is the row count of a contiguous dst, so the guard holds. On the MUL_MAT_ID path with one token (ncols_dst == 1 and ids), the kernel is launched with stride_col_dst = dst->nb[2], which is nrows * n_used, so a block that straddles the last row (m not a multiple of the rows per block, which the small-K sizing raises to several) passes rows past the end: it reads beyond its expert's rows and writes the results into the next expert's first rows, racing that expert's own block. test-backend-ops MUL_MAT_ID with n_mats 8, n_used 6, m 66, n 1, k 768 fails on stock b11803 kernels (q4_K ERR 0.051, iq2_xs 0.096, q8_0 0.022, iq2_xxs 1.0, changing run to run) and passes with the row count as the guard; the case is added to the eval set. The models served here have expert rows that are multiples of four, so they were not affected.
There was a problem hiding this comment.
🟢 Approved
The focused bounds correction is consistently propagated through the affected launch paths, with regression coverage and reported passing CUDA suites.
No open issues 🎉
What changed in this PR
This PR fixes the CUDA quantized matrix-vector tail bound so single-token MUL_MAT_ID operations cannot write excess rows into another expert's output.
Changes:
- Pass an explicit destination row count through all affected kernel launch paths.
- Use that count instead of the column stride for tail guards.
- Add the 66-row regression case across five data types.
| File | Description |
|---|---|
| tests/test-backend-ops.cpp | Adds single-token expert tail regression cases. |
| ggml/src/ggml-cuda/mmvq.cu | Propagates the row count and corrects tail bounds. |
Review effort: Balanced
|
The Order: merge #9 first; once it lands I will merge |
What this changes
mul_mat_vec_q(ggml/src/ggml-cuda/mmvq.cu) guards its last block of rows withrow0 + i < stride_col_dst. For a plain MUL_MAT that stride is the row count of a contiguous dst, so the guard holds. On the MUL_MAT_ID path with one token (ncols_dst == 1andids),ggml_cuda_mul_mat_vec_qlaunches the kernel withstride_col_dst = dst->nb[2] / ts_dst, which isnrows * n_used(the stride between tokens), so a block that straddles the last row passes rows past the end: it reads beyond its expert's rows and writes those results into the next expert's first rows, racing that expert's own block, which is why the error changes from run to run. The straddle happens whenevermis not a multiple of the block's rows, and the small-K sizing (upstreamccb87fa3e, kept by #5) raises the rows per block to the warp count for one token, so K = 768 at m = 66 reaches it. Themul_mat_vec_q_moekernel used forncols_dst > 1already guards withnrows_x; f16 does not take this kernel.The kernel now takes the row count of dst (
nrows_dst) and guards with it; the launch sites passnrows_x. The shape is added totest-backend-ops' eval set (n_mats 8, n_used 6, m 66, n 1, k 768) for q4_K, iq2_xs, f16, q8_0 and iq2_xxs.This is upstream's defect, present in stock b11803 (below); it is fixed here because this fork ships the kernel. The models served from this fork (DeepSeek-V4.1, GLM-5.3, Qwen3.8) have expert rows that are multiples of four (
n_embd2560 or 5120 on the down projections), so they never reached the tail; any model whose expert output or intermediate size is not a multiple of the rows per block would, at one token. The fork's issue tracker is disabled, so this PR is the report.Evidence (DGX Spark GB10, CUDA arch 121, 2026-10-26)
Before, stock kernels —
1082d1983(tagb11803-spiceai.2: upstream b11803 plus the Cargo packaging and the load-check throw, no kernel change), the case added to the eval loop,./build/bin/test-backend-ops test -b CUDA0 -o MUL_MAT_ID:Before, this fork —
bd3dc081through the mmvq variant harness'sMUL_MAT_ID_TAILprobe (same shape),GGML_CUDA_MMVQ_IQ3_SMALL_K=0:After — this branch, same node, same command:
bec252fd8built on the same node (cmake --build build -j 20 --target test-backend-ops, 46 s incremental),./build/bin/test-backend-ops test -b CUDA0 -o MUL_MAT_ID:and the whole MUL_MAT suite for regressions,
-o MUL_MAT:2445/2445 tests passed, Backend CUDA0: OK. (The variant harness's owntest-backend-opsbinary links its kernels statically, so it cannot be pointed at the fixed library; the eval cases above are the same shape.)What is not covered
Metal, Vulkan and the CPU backend do not run this kernel; the change is CUDA-only. No model served from the fork reaches the tail (above), so no decode measurement changes; the test is the regression guard.