Skip to content

CUDA: bound the partial last row block of mul_mat_vec_q by the matrix rows - #511

Open
danielhanchen wants to merge 3 commits into
base/upstream-9a811bab3from
pin-mmvq-rowbound
Open

danielhanchen wants to merge 3 commits into
base/upstream-9a811bab3from
pin-mmvq-rowbound

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This pin is one commit on top of upstream b12966 (9a811bab3c4) and needs no other pin. Once upstream has an equivalent change, the pin is dropped.

The problem

With small K the mat-vec launch puts several rows in one block (rows_per_cuda_block = warps). When the row count is not a multiple of that:

  • single-column MUL_MAT_ID guards its stores with stride_col_dst, which is the token stride for MUL_MAT_ID, not the expert's rows. The last row block of a slot writes rows past the expert into the next slot's first rows, which that slot's own block also writes: the result depends on which block writes last, and the last slot of the last token writes past dst;
  • mul_mat_vec_q and mul_mat_vec_q_moe read whole rows past the end of the matrix (only the stores are guarded), past the weight buffer for its last matrix.

Real models have row counts that are multiples of 4, so this is latent there; odd shapes (66 / 70 rows) hit it.

The fix

  • mmvq.cu: the store bound is the slot stride (stride_channel_dst) for MUL_MAT_ID at one column and the column stride as before otherwise. Rows of the last block past the last row read the last row again; their sums are not stored, and the stored rows read what they read before, so every in-range output keeps its bytes. The fused bias loads use the same row bound as the stores.
  • tests/test-backend-ops.cpp: MUL_MAT_ID cases with 66 / 70 expert rows at K 1024 / 2048 (Q4_0, IQ4_XS, Q4_K, Q8_0; 1 / 2 / 4 tokens).

Evidence

Check Result
B200 (sm_100a) test-backend-ops -b CUDA0 -o MUL_MAT_ID, b12966 + the new test cases only (base) 1128 / 1135: 7 of the new cases fail, every one with the harness's sentinel mismatch (writes past dst) and wrong rows, one with NaN (iq4_xs, 40 experts, 70 rows, K 2048: CUDA0=nan CPU=7.34): q4_0 / iq4_xs / q4_K at 66 rows, 1 token, broadcast and per-slot src1
B200 same, with this pin (build before the fused-bias bound commit) MUL_MAT_ID 1135/1135, MUL_MAT 2845/2845
RTX 5090 (sm_120a), pin-set tree vs + this pin MUL_MAT_ID 1298/1298 -> 1334/1334, MUL_MAT 3123/3123 both
Greedy md5, B200, b12966 vs + this pin (same build) identical on all 8 of Qwen3-0.6B BF16 / Q4_K_M, Llama-3.2-1B F16, gemma-3-1b BF16 / Q4_K_M, granite-4.0-h-tiny Q4_K_M, Qwen3-30B-A3B UD-Q2_K_XL, tiny Qwen3-MoE F16
No-break md5 on the pin-set tree (5090): Qwen4-35B-A3B, Qwen3.8-Flash-Next, Inkling-2-Mini, Qwen3.8-27B, 2 prompts each identical to the pin-set tree; greedy md5 also identical on Qwen3-0.6B Q4_0, Qwen3-30B-A3B Q4_K_XL, gemma-3-1b Q4_0, granite-4.0-h-tiny Q4_K_M
Pin set sim (llama_pinset.py sim, private copy, b13023, 12-06): master pr-set + open candidates #485-#503 (without #495) + #505 + #508-#513 + #516 at their current heads, 74 pins every pin merges (git or additive); merge_checks clean; pin_contract: all pins intact once #491 is listed at its current head e6e34ffd3c2 (the copy listed it at 131d93897cd, whose comment-trimmed line arrives through #505 / #512, a listing artifact). CUDA mmvq.cu / mmvf.cu / mmid-gemm.cu / fusion.cu compile on the merged tree (sm_75 + sm_100a)

… rows

With small K (rows per block = warps) and rows that are not a multiple of
the rows per block, the last row block of a matrix:

- for a single-column MUL_MAT_ID, guarded its stores with the token stride
  instead of the expert's rows, so it wrote rows past the expert into the
  next slot's first rows, which that slot's own block also writes: the
  output depended on which block wrote last, and the last slot of the last
  token wrote past dst;
- in mul_mat_vec_q and mul_mat_vec_q_moe, read whole rows past the end of
  the matrix (only the stores were guarded), past the weight buffer for
  its last matrix.

The store bound is now the slot stride (the rows of dst) for MUL_MAT_ID at
one column, the column stride as before otherwise. The rows of the last
block past the last row read the last row again; their sums are not
stored and the stored rows read what they read before, so every output
keeps its bytes. test-backend-ops: MUL_MAT_ID cases with 66 / 70 rows at
K 1024 / 2048.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T09:00:49.011100Z 310e3c5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Dec 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: e66befc8c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 310e3c530e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

1 participant