Repository navigation
CUDA: bound the partial last row block of mul_mat_vec_q by the matrix rows - #511
danielhanchen wants to merge 3 commits into
Conversation
… 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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…rd line stays as upstream
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Summary
The unslothai/llama.cpp prebuilt is an aged upstream
b####tag plus the pins listed inscripts/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: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_qandmul_mat_vec_q_moeread 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
test-backend-ops -b CUDA0 -o MUL_MAT_ID, b12966 + the new test cases only (base)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 src1llama_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 pinsmerge_checksclean;pin_contract: all pins intact once #491 is listed at its current heade6e34ffd3c2(the copy listed it at131d93897cd, whose comment-trimmed line arrives through #505 / #512, a listing artifact). CUDAmmvq.cu/mmvf.cu/mmid-gemm.cu/fusion.cucompile on the merged tree (sm_75 + sm_100a)