Repository navigation
CUDA: keep the input of a fused mul_mat_vec_f + ADD allocated until the ADD - #509
danielhanchen wants to merge 2 commits into
Conversation
…he ADD The "mul_mat + add" fusion runs a one-column F32 / F16 / BF16 / F8_E4M3 product and its ADD (ADD_ID) as one mul_mat_vec_f launch with x_bias. Every block reads the whole src1 vector while other blocks already write output rows. Unfused, src1 is dead after the product, so the allocator may place the ADD output over it; in the fused kernel that is a write-while-read race and 16-bit model decode is not repeatable from run to run. Keep src1, the bias and (for MUL_MAT_ID) the ids allocated until the ADD, through the allocation dependencies the other fused kernels already use. The fusion still runs; only the placement of the ADD output changes. GGML_CUDA_MMVF_ADD_ALIAS=1 restores the previous placement.
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6a4df1f41e
ℹ️ 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".
| const ggml_type t1 = mm->src[1]->type; | ||
| if ((t0 != GGML_TYPE_F32 && t0 != GGML_TYPE_F16 && t0 != GGML_TYPE_BF16 && t0 != GGML_TYPE_F8_E4M3) || | ||
| (t1 != GGML_TYPE_F32 && t1 != GGML_TYPE_BF16) || | ||
| (mm->op == GGML_OP_MUL_MAT ? mm->ne[1] != 1 : mm->ne[2] != 1)) { |
There was a problem hiding this comment.
Keep allocation dependencies independent of batch size
For floating-point models with adjacent matmul/bias nodes, this one-column condition adds allocation-dependency nodes during single-token decoding but omits them during multi-token processing. ggml_backend_sched_split_graph materializes these dependencies as extra graph nodes, and ggml_gallocr_needs_realloc rejects a reservation whenever the node count changes. Consequently, switching between these batch sizes forces a new allocation plan and, with multiple scheduler buffers, synchronization of all backends in ggml_backend_sched_alloc_splits, defeating the prompt-sized reservation in llama_context::reserve. This introduces repeated stalls when workloads alternate between single-token and multi-token batches. Add the dependencies independently of column count, leaving the one-column restriction in the execution-time fusion predicate.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e9c430f: the deps no longer depend on the column count, so pp and tg graphs keep the same node count; the one-column restriction stays only in the fusion predicate.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs 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". |
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
The CUDA "mul_mat + add" fusion runs a one-column F32 / F16 / BF16 / F8_E4M3 product and the following ADD (ADD_ID) as one
mul_mat_vec_flaunch withx_bias. Every block reads the whole src1 vector while other blocks already write their output rows. Unfused, src1 is dead after the product, so the graph allocator may place the ADD output over it. In the fused kernel that is a write-while-read race: blocks read src1 values that other blocks have already overwritten. Upstream'sggml_cuda_fusion_add_alloc_depskeeps the inputs alive for the MMQ GLU and top-k MoE fusions, not for this one.The fix
fusion.cu,ggml_cuda_fusion_add_alloc_deps: for every 16/32-bit or F8 product followed by its ADD / ADD_ID, src1, the bias and (MUL_MAT_ID) the ids get an allocation dependency on the ADD. The condition is a superset of the fusion's, so every pair the fusion takes is covered. The fusion still runs; only the placement of the ADD output changes.GGML_CUDA_MMVF_ADD_ALIAS=1restores the previous placement.Evidence
fusion.cuat e9c430ffusion.cu)test-backend-ops -b CUDA0ADD 180/180, ADD_ID 37/37, MUL_MAT 2845/2845; greedy md5 identical to b12966 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 (2 runs each); with the fix vsGGML_CUDA_MMVF_ADD_ALIAS=1, 3 runs each, md5 stable and equal on Qwen3-0.6B BF16, Llama-3.2-1B F16, gemma-3-1b BF16, tiny Qwen3-MoE F16 and OLMoE (the B200 did not reproduce the alias, so this row shows no regression only)GGML_CUDA_MMVF_ADD_ALIAS=1, and 5 launches of the pinllama_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)Not proven