Skip to content

CUDA: qwen4exp hyper-connection mixer in one cooperative launch at decode - #504

Open
danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-hc-mix
Open

danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-hc-mix

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Interaction with pin #448 (resolved, no code change): on a B200 with the mixer forced on (GGML_CUDA_HC_MIX=1), Qwen4 decode KLD against the pin-set tree was 0.0180 (top-1 95.3%). That is #448's own activation difference, not this pin: #448 (active on cc >= 10.0 only) runs the Q8_0 batch-1 mat-vecs on f32 activations, while this kernel rounds the hc activations to q8_1 as the Ampere / Ada mat-vecs do. The base with #448 switched off (GGML_CUDA_DISABLE_MMVQ_F32_ACT=1) differs from the default base by the same amount (KLD 0.01875, top-1 94.9%, same PPL shift), and the forced-on mixer scored against that base gives KLD 0 (max 4.9e-5, the floor) with top-1 100%. No default configuration combines the two: this mixer is off by default on Hopper and Blackwell, and #448 is inactive on Ampere / Ada. An A100 rerun of the KLD and greedy arms with the engagement line printed in the same run is pending.

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), so it needs no other pin. It also merges cleanly with current upstream master. Once upstream has an equivalent change, the pin is dropped.

The problem

Qwen4-35B-A3B (qwen4exp) runs a hyper-connection mixer twice per layer (80 per decoded token). With the merged [hc_down | hc_inject] Q8_0 weight the converter writes, each mixer is a chain of small kernels at decode: grouped RMS norm and its weight, the down product (260 rows, K = 8192), the 1/hc scale, SiLU over the hc_down rows, the up product (8192 rows, K = 256) and the gated DSV4_HC_PRE. The block's gated DSV4_HC_POST (SIGMOID, SCALE, DSV4_HC_POST) runs right before the next mixer. All of these are launch-bound at one token.

The fix

  • New hc-mix.cu: one cooperative kernel computes the whole mixer for 1 to 4 tokens. Each block stages its slice of both weights in shared memory with cp.async while the norm runs, computes its down rows, then one grid barrier, then the up rows and the gated mix for its 32-column range. The inject rows are written, already scaled, into the SCALE output where the block's hc_post gate reads them. When the previous block's gated DSV4_HC_POST feeds the mixer, it is computed in the same launch (its output is written after the grid barrier, since it may alias the residual).
  • fusion.cu: ggml_cuda_try_fuse tries the mixer first, at an RMS_NORM or at the SIGMOID of the hc_post gate. The matcher checks the exact chain shape (n_embd 2048, hc 4, hc_down 256 rows, Q8_0 weights in a plain CUDA buffer), that the launch writes every value a later node reads (the gate and SiLU outputs have only the fused reader, the SCALE output is read elsewhere only through the inject rows), and that no output overlaps an input.
  • graph_optimize: the launch inputs (x, and with the hc_post fold its block output, residual and gate input) are kept allocated until the DSV4_HC_PRE, so no fused output is placed on them.
  • Default on for NVIDIA Ampere / Ada with at least 96 SMs (A100, RTX 4090, L40S). Off on Hopper / Blackwell, where programmatic dependent launch already overlaps the separate kernels, and below 96 SMs. GGML_CUDA_HC_MIX=1 forces it on (NVIDIA Ampere or newer), GGML_CUDA_HC_MIX=0 turns it off, GGML_CUDA_HC_MIX_POST=0 keeps the hc_post separate, GGML_CUDA_HC_MIX_TOKENS (default 1) raises the token limit. HIP and MUSA compile it out.
  • test-backend-ops: HC_MIX_FUSED builds one mixer as build_hc_mix does, 1 to 5 tokens, with and without the hc_post in front.

The output is not bit-identical by design: the f32 summation order inside the two dot products differs from the separate mat-vec kernels. The activations are quantized to q8_1 with the same arithmetic as quantize_q8_1, and the elementwise math is the same.

No model code changes: the matcher reads the graph build_hc_mix already builds from the merged weight.

Evidence

All B200 (sm_100) runs check correctness only; none of them are timing numbers. The mixer is default off on Blackwell, so the B200 runs force it on with GGML_CUDA_HC_MIX=1.

Check Result
Pin set at b13023 (llama_pinset.py sim with the master pr-set plus this pin) all 51 pins merge; pin_contract: all intact; merge_checks: clean. This pin is a plain merge (no additive resolution)
Pin set at b13023 with every open candidate pin (#485 to #503) plus this pin this pin merges and is intact. #495 is refused on its own (src/llama-model-loader.cpp); without #495, all 67 pins merge, pin_contract: all intact, merge_checks: clean
Pin set at b13024 (newest window tag) with every open candidate this pin merges and is intact; #251, #268 and #495 are refused there, independent of this pin (none of their files are touched here)
Interaction with #485 (prescaled hc_post gate) and #240 (qwen4exp shared MTP heads) both merge cleanly with this pin and stay intact. With the mixer on, the hc_post chain #485 fuses is taken into the mixer launch first; with it off, #485 runs unchanged
CPU build (-DGGML_CUDA=OFF) and CUDA build (sm_80 / sm_89 / sm_100) of the merged all-candidates tree both build
nvcc 13.1 compile of hc-mix.cu for sm_75 / sm_80 / sm_89 / sm_100 / sm_120 0 diagnostics in hc-mix.cu
git merge-tree with upstream master (2ed8d0971bd) clean
B200, test-backend-ops -o HC_MIX_FUSED -b CUDA0 on the pin branch 10 / 10 with GGML_CUDA_HC_MIX=1 GGML_CUDA_HC_MIX_TOKENS=4, 10 / 10 with GGML_CUDA_HC_MIX_POST=0 as well, 10 / 10 with GGML_CUDA_HC_MIX=0. The engagement lines (mixer and hc_post fold) appear only when forced on

B200 on the pin-set tree (master pr-set.json at b13023, A) against A plus this pin (B), both built for sm_100. Correctness only:

Check Result
test-backend-ops, full suite, CUDA0 A 24411 / 24411; B 24421 / 24421 (the 10 new HC_MIX_FUSED cases); B with GGML_CUDA_HC_MIX=1 24421 / 24421
Qwen4-35B-A3B UD-Q4_K_XL (--cpu-moe), llama-perplexity -ub 1, 2 chunks, B default and B with GGML_CUDA_HC_MIX=0 vs A KLD 0, top-1 100% (the mixer is off on sm_100 by default, so the graph is the same as A)
Same, B with GGML_CUDA_HC_MIX=1 vs A with GGML_CUDA_DISABLE_MMVQ_F32_ACT=1 (q8_1 activations on both sides) mean KLD 0.000000, max 4.9e-5 (the A/A floor), top-1 100%
Same, A with GGML_CUDA_DISABLE_MMVQ_F32_ACT=1 vs A default mean KLD 0.01875, top-1 94.9%: the f32-activation path of #448, which the q8_1 mixer does not take. B forced on vs A default is 0.0180 / 95.3%
Greedy 128 tokens A, B default and B with GGML_CUDA_HC_MIX=0: md5 68a50ec2c16b. B forced on gives 5a5df4d6e09e (the #448 difference above) and prints both engagement lines with -v

Engagement and the KLD floor in the same run: B with GGML_CUDA_HC_MIX=1 GGML_CUDA_DISABLE_MMVQ_F32_ACT=1, llama-perplexity -ub 1 -v, scored against A with the same #448 switch, prints hc mix: hyper-connection mixer in one launch (1 tokens, 148 blocks, 45856 B shared) and hc mix: the previous block's hc_post computed in the mixer launch; mean KLD 0.000000 +/- 0.000001, max 4.9e-5, same top 100%.

No other model changes (B200, greedy 64 tokens, same prompt and seed). Arms: A, B default, B forced on (GGML_CUDA_HC_MIX=1, so the matcher sees every graph) and B with GGML_CUDA_HC_MIX=0. All four arms give the same md5 for every model:

Model md5
Qwen3-0.6B Q4_K_M add9f113bf31
granite-3.1-1b-a400m (MoE) Q4_K_M 31bf30d1c667
gemma-3-1b Q4_K_M f2d275e552f4
granite-4.0-h-tiny (hybrid) Q4_K_M cf431c9f005a
Inkling-2-Mini UD-IQ1_S 76c42267efa0
Qwen3.8-27B UD-IQ2_XXS 15c2f5e6a040
Qwen3.8-Flash-Next UD-IQ1_S (--cpu-moe) afabba3dee00
Qwen4-35B-A3B UD-Q4_K_XL (--cpu-moe) f03bdfef4879

On Qwen4 the forced-on text also matches over these 64 tokens; over 128 tokens it diverges by the #448 difference above.

A100 80GB (sm_80, the mixer on by default), Qwen4-35B-A3B UD-Q4_K_XL fully on the GPU, -fa on, -v on every run, llama-perplexity -c 512 -b 512 -ub 1, 2 chunks, built from db5055c1c8e (the head adds only the matcher refusal for scaled matmuls, which Qwen4 does not have):

  • The line hc mix: hyper-connection mixer in one launch (1 tokens, 108 blocks, 54560 B shared) appears in the B default runs and in none of the A or GGML_CUDA_HC_MIX=0 runs.
  • KLD vs A: B default mean 0.000000 +/- 0.000001, max 5.3e-5, same top 100%; B with the kill switch gives the same numbers (the floor).
  • Greedy 128 tokens: md5 13fdff8ff6c1 for A, B and B with the kill switch.

On the pin-set tree (upstream b13023 + the master pr-set.json pins, A) against A plus this pin (B), rented GPUs, Qwen4-35B-A3B UD-Q4_K_XL:

Check Result
A100 80GB PCIe, fully on GPU, 4 rotated rounds, decode tok/s tg128 112.12 -> 123.50 (+10.15%), tg64 at depth 8192 107.18 -> 118.39 (+10.46%). Spread between the two A arms 0.07% / 0.14%; B with GGML_CUDA_HC_MIX=0 is -0.03% / -0.05% vs A
A100, decode KLD at -ub 1, B vs A mean 0, max 5.3e-5 (the same as the kill-switch arm, the floor), top-1 100%; greedy md5 identical for A, B and the kill switch
RTX 4090, KLD and greedy md5 pass. Speed inconclusive: the model needed -ot exps=CPU on this card
No-break greedy md5, A vs B identical on Qwen3-0.6B, gemma-3-1b, granite-4.0-h-tiny, Inkling-2-Mini, Qwen3.8-27B and Qwen3.8-Flash-Next

Not proven

  • RTX 4090 speed on the pin-set tree (the 4090 run needed experts on the CPU). The engagement log line is printed only at higher verbosity; the kill switch removing the +10% on the A100 shows the path runs.
  • Hopper and Blackwell keep the mixer off by default, and so do GPUs below 96 SMs: on the fork it measured slower there (RTX 5090 -3.1%, RTX 3090 -0.3%).
  • Multi-token batches (MTP verify) stay unfused by default (GGML_CUDA_HC_MIX_TOKENS=1); on the fork, 3-token fused batches were slower.
  • HIP and MUSA were not compiled here; the kernel and launch are compiled out there and the matcher returns 0 before any HIP-specific code.
  • Only the Qwen4-35B-A3B shape is supported (n_embd 2048, 4 streams, 256 hc_down rows, Q8_0 hc weights). Other qwen4exp sizes or hc weight types run unfused.

…code

The decode-step mixer of a merged [hc_down | hc_inject] Q8_0 weight (RMS_NORM, MUL, MUL_MAT, SCALE, SILU, MUL_MAT, gated DSV4_HC_PRE, plus the previous block's gated DSV4_HC_POST) runs as one cooperative kernel. Default on for NVIDIA Ampere / Ada with at least 96 SMs; GGML_CUDA_HC_MIX=0 off, =1 forced on; GGML_CUDA_HC_MIX_POST=0 keeps the hc_post separate.
@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:11:54.129078Z b129c8f 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: db5055c1c8

ℹ️ 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/hc-mix.cu Outdated
Comment on lines +585 to +586
if (up->op != GGML_OP_MUL_MAT || up->src[1] != si || up->type != GGML_TYPE_F32 || !hcm_weight_ok(w_up, cuda_ctx->device) ||
w_up->ne[0] != n_lo || w_up->ne[1] != n_embd*hc || !ggml_is_contiguous(up) || (up->flags & GGML_TENSOR_FLAG_OUTPUT)) {

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 Reject scaled matmuls before fusing the mixer

If either mixer matmul is constructed with ggml_mul_mat_ext() and a non-unit weight scale, this matcher still accepts it: the operation remains GGML_OP_MUL_MAT, and the Q8_0 weight passes hcm_weight_ok(). The fused kernel reads only the weights and activations, ignoring the scale in src[2]. The unfused CUDA dispatch applies that scale through ggml_cuda_op_mul_mat_scale(), so enabling fusion silently changes the inject weights or gate values and therefore the model output. Reject scaled matmuls for both down and up unless their scale semantics are implemented in the fused kernel.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in b129c8f: the matcher now leaves both matmuls unfused when they carry a weight or input scale (build_lora_mm passes them through ggml_mul_mat_ext).

@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. Already looking forward to the next diff.

Reviewed commit: b129c8fa91

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

2 participants