Skip to content

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

Merged
lukekim merged 1 commit into
spiceai-b11803from
lukim/b11803-mmvq-id-row-guard
Oct 26, 2026
Merged

lukekim merged 1 commit into
spiceai-b11803from
lukim/b11803-mmvq-id-row-guard

Conversation

@lukekim

@lukekim lukekim commented Oct 26, 2026

Copy link
Copy Markdown

What this changes

mul_mat_vec_q (ggml/src/ggml-cuda/mmvq.cu) 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), ggml_cuda_mul_mat_vec_q launches the kernel with stride_col_dst = dst->nb[2] / ts_dst, which is nrows * 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 whenever m is not a multiple of the block's rows, and the small-K sizing (upstream ccb87fa3e, kept by #5) raises the rows per block to the warp count for one token, so K = 768 at m = 66 reaches it. The mul_mat_vec_q_moe kernel used for ncols_dst > 1 already guards with nrows_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 pass nrows_x. The shape is added to test-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_embd 2560 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 (tag b11803-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:

[MUL_MAT_ID] ERR = 0.051317265 > 0.000500000   MUL_MAT_ID(type_a=q4_K,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): FAIL
[MUL_MAT_ID] ERR = 0.095931864 > 0.000500000   MUL_MAT_ID(type_a=iq2_xs,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): FAIL
  MUL_MAT_ID(type_a=f16,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): OK
  932/934 tests passed
  Backend CUDA0: FAIL

Before, this fork — bd3dc081 through the mmvq variant harness's MUL_MAT_ID_TAIL probe (same shape), GGML_CUDA_MMVQ_IQ3_SMALL_K=0:

[MUL_MAT_ID] ERR = 0.022110915 > 0.000500000   MUL_MAT_ID_TAIL(type_a=q8_0,...,m=66,n=1,k=768,...): FAIL
[MUL_MAT_ID] ERR = 1.000241086 > 0.000500000   MUL_MAT_ID_TAIL(type_a=iq2_xxs,...,m=66,n=1,k=768,...): FAIL
  0/2 tests passed

After — this branch, same node, same command:

bec252fd8 built 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:

  MUL_MAT_ID(type_a=q4_K,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): OK
  MUL_MAT_ID(type_a=iq2_xs,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): OK
  MUL_MAT_ID(type_a=f16,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): OK
  MUL_MAT_ID(type_a=q8_0,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): OK
  MUL_MAT_ID(type_a=iq2_xxs,type_b=f32,n_mats=8,n_used=6,b=0,m=66,n=1,k=768,amax=1.000000,m_v=0,ids_offset=0): OK
  936/936 tests passed
  Backend CUDA0: OK

and the whole MUL_MAT suite for regressions, -o MUL_MAT: 2445/2445 tests passed, Backend CUDA0: OK. (The variant harness's own test-backend-ops binary 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.

…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.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 09:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

The ubuntu job's Tests (Backend sampling) step failed on this head (run 40298255813), and it is the base's known regression, not this change: the server aborted with common/sampling.cpp:629: GGML_ASSERT(!gsmpl->grmr && "using grammar in combination with backend sampling is not supported") failed, taking down unit/test_chat_completion.py::test_completion_with_response_format[response_format0-6-"42"] and unit/test_speculative.py::test_synth_ignores_target_tokens (RemoteDisconnected), 2 failed, 353 passed — the same assert and the same two tests as #7's run 40271195149 on 046d98ce5, which spiceai-b11803 (59d512374) carries and #9 fixes. This branch changes ggml/src/ggml-cuda/mmvq.cu and tests/test-backend-ops.cpp only, and the server tests run on a CPU build that does not compile the CUDA kernel. Every other hosted check on this head passes (arm64 Linux, Windows arm64, webgpu ×2; the x64 and remaining Windows legs are still pending).

Order: merge #9 first; once it lands I will merge spiceai-b11803 into this branch so this job re-runs green here before it is merged.

@lukekim
lukekim merged commit 6d9e9a8 into spiceai-b11803 Oct 26, 2026
15 of 25 checks passed
lukekim added a commit that referenced this pull request Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants