Repository navigation
CUDA: qwen4exp hyper-connection mixer in one cooperative launch at decode - #504
danielhanchen wants to merge 2 commits into
Conversation
…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.
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: 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".
| 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)) { |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
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 inscripts/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 upstreammaster. 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 gatedDSV4_HC_PRE. The block's gatedDSV4_HC_POST(SIGMOID, SCALE, DSV4_HC_POST) runs right before the next mixer. All of these are launch-bound at one token.The fix
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 withcp.asyncwhile 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 gatedDSV4_HC_POSTfeeds 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_fusetries the mixer first, at anRMS_NORMor at theSIGMOIDof 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 theDSV4_HC_PRE, so no fused output is placed on them.GGML_CUDA_HC_MIX=1forces it on (NVIDIA Ampere or newer),GGML_CUDA_HC_MIX=0turns it off,GGML_CUDA_HC_MIX_POST=0keeps 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_FUSEDbuilds one mixer asbuild_hc_mixdoes, 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_mixalready 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.llama_pinset.py simwith the master pr-set plus this pin)pin_contract: all intact;merge_checks: clean. This pin is a plain merge (no additive resolution)src/llama-model-loader.cpp); without #495, all 67 pins merge,pin_contract: all intact,merge_checks: clean-DGGML_CUDA=OFF) and CUDA build (sm_80 / sm_89 / sm_100) of the merged all-candidates treehc-mix.cufor sm_75 / sm_80 / sm_89 / sm_100 / sm_120hc-mix.cugit merge-treewith upstreammaster(2ed8d0971bd)test-backend-ops -o HC_MIX_FUSED -b CUDA0on the pin branchGGML_CUDA_HC_MIX=1 GGML_CUDA_HC_MIX_TOKENS=4, 10 / 10 withGGML_CUDA_HC_MIX_POST=0as well, 10 / 10 withGGML_CUDA_HC_MIX=0. The engagement lines (mixer and hc_post fold) appear only when forced onB200 on the pin-set tree (master
pr-set.jsonat b13023, A) against A plus this pin (B), both built for sm_100. Correctness only:test-backend-ops, full suite, CUDA0HC_MIX_FUSEDcases); B withGGML_CUDA_HC_MIX=124421 / 24421--cpu-moe),llama-perplexity -ub 1, 2 chunks, B default and B withGGML_CUDA_HC_MIX=0vs AGGML_CUDA_HC_MIX=1vs A withGGML_CUDA_DISABLE_MMVQ_F32_ACT=1(q8_1 activations on both sides)GGML_CUDA_DISABLE_MMVQ_F32_ACT=1vs A defaultGGML_CUDA_HC_MIX=0: md568a50ec2c16b. B forced on gives5a5df4d6e09e(the #448 difference above) and prints both engagement lines with-vEngagement 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, printshc mix: hyper-connection mixer in one launch (1 tokens, 148 blocks, 45856 B shared)andhc 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 withGGML_CUDA_HC_MIX=0. All four arms give the same md5 for every model:add9f113bf3131bf30d1c667f2d275e552f4cf431c9f005a76c42267efa015c2f5e6a040--cpu-moe)afabba3dee00--cpu-moe)f03bdfef4879On 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,-von every run,llama-perplexity -c 512 -b 512 -ub 1, 2 chunks, built fromdb5055c1c8e(the head adds only the matcher refusal for scaled matmuls, which Qwen4 does not have):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 orGGML_CUDA_HC_MIX=0runs.13fdff8ff6c1for A, B and B with the kill switch.On the pin-set tree (upstream b13023 + the master
pr-set.jsonpins,A) againstAplus this pin (B), rented GPUs, Qwen4-35B-A3B UD-Q4_K_XL:Aarms 0.07% / 0.14%;BwithGGML_CUDA_HC_MIX=0is -0.03% / -0.05% vsA-ub 1,BvsAA,Band the kill switch-ot exps=CPUon this cardAvsBNot proven
GGML_CUDA_HC_MIX_TOKENS=1); on the fork, 3-token fused batches were slower.