Skip to content

CUDA: keep the input of a fused mul_mat_vec_f + ADD allocated until the ADD - #509

Open
danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-mmvf-add-alias
Open

danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-mmvf-add-alias

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

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_f launch with x_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's ggml_cuda_fusion_add_alloc_deps keeps 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.
  • The dependencies do not depend on the column count (the one-column limit stays in the fusion predicate only). Adding them for decode alone would give the prompt and decode graphs different node counts, so every switch between the two would force a scheduler reallocation and backend sync. The cost is that during prompt processing src1 stays allocated until the ADD, the very next node.
  • Kill switch: GGML_CUDA_MMVF_ADD_ALIAS=1 restores the previous placement.

Evidence

Check Result
Our fork, same fix OLMoE BF16 and Qwen3.6-35B-A3B BF16 decode was not repeatable launch to launch before the fix and is after; quantized md5 unchanged
fusion.cu at e9c430f compiles with nvcc 12.8 (sm_90); not run on a GPU
Pin-set tree repro not reproduced: no 16-bit model gave launch-to-launch differences on the pin-set tree on B200 or RTX 5090 without this pin either (see the rows below), so the rows show no regression, not the fix
B200 (sm_100a), b12966 vs + this pin (B200 build of 6a4df1f; head e9c430f adds the deps for every column count, 2 lines in fusion.cu) test-backend-ops -b CUDA0 ADD 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 vs GGML_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)
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
RTX 5090 (sm_120a), pin-set tree vs + this pin: greedy md5, base, GGML_CUDA_MMVF_ADD_ALIAS=1, and 5 launches of the pin one output per model across all arms and launches: Qwen3-0.6B BF16, Llama-3.2-1B F16, gemma-3-1b BF16, granite-4.0-h-tiny BF16, Qwen3-30B-A3B Q4_K_XL
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)

Not proven

  • The alias itself was not reproduced on the pin-set tree (B200, RTX 5090); the fix is carried from the fork, where OLMoE BF16 and Qwen3.6-35B-A3B BF16 decode differed launch to launch before it.
  • HIP and other backends are untouched by this change.

…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.
@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-06T08:41:55.251768Z e9c430f 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread ggml/src/ggml-cuda/fusion.cu Outdated
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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@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. More of your lovely PRs please.

Reviewed commit: e9c430f6e6

ℹ️ 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