Skip to content

qwen4exp : fuse the PLE grouped RMSNorm with its scale - #40

Merged
routhjim merged 1 commit into
mainfrom
pr/qwen4exp-ple-fusion
Sep 21, 2026
Merged

routhjim merged 1 commit into
mainfrom
pr/qwen4exp-ple-fusion

Conversation

@routhjim

Copy link
Copy Markdown
Owner

build_ple's grouped_norm reshaped between ggml_rms_norm and ggml_mul, which splits
the pair into two dispatches — the backend emits a fused RMS_NORM_MUL only when the two
are adjacent nodes. Moving the mul up to sit directly on the norm, and viewing the
[hc_dim] gamma as [n_embd, hc] so it broadcasts over the token axis, fuses them with
the math unchanged.

 auto grouped_norm = [&](ggml_tensor * x, ggml_tensor * w) {
     ggml_tensor * t = ggml_reshape_3d(ctx0, x, n_embd, hc, n_tokens);
-    t = ggml_rms_norm(ctx0, t, hparams.f_norm_rms_eps);
-    t = ggml_reshape_2d(ctx0, t, hc_dim, n_tokens);
-    t = ggml_mul(ctx0, t, w);
-    return ggml_reshape_3d(ctx0, t, n_embd, hc, n_tokens);
+    return ggml_mul(ctx0, ggml_rms_norm(ctx0, t, hparams.f_norm_rms_eps),
+                    ggml_reshape_2d(ctx0, w, n_embd, hc));
 };

The lambda already returned [n_embd, hc, n_tokens], so the trailing reshape was
redundant; all three call sites (key, query, conv) are unaffected.

Context

This is the same defect fixed in build_hc_mix (dc44c05) — grouped_norm was the
remaining unfused site, and it runs three times per PLE layer.

Found via upstream ggml-org/llama.cpp#28896,
which fixes both sites and reports +3% PP for the pair. We already carry the
build_hc_mix half, so only the PLE half was missing here. Upstream reshapes the gammas at
load time (TENSOR_ALLOW_RESHAPE); this keeps our graph-side reshape for consistency with
build_hc_mix.

Honest scope

Not yet measured on this tree. The gain here is the remainder of upstream's 3%, not
all of it. Vulkan does implement the fusion (pipeline_rms_norm_mul_f32), so it applies to
our backend. It should help decode as well as prefill: these tensors are tiny at batch 1–4
and dispatch-bound (our XTX profiling puts ~26% of time in tiny-op dispatch), and the
sibling build_hc_mix bug was originally spotted in a decode profile
(RMS_NORM dst(f32 2560,4) running unfused).

⚠️ The next upstream sync will conflict in qwen4exp.cpp: we and upstream fixed both
build_hc_mix and this block differently on the same lines.

🤖 Generated with Claude Code

https://claude.ai/code/session_019F3dWuvm3aaKMwJTiq3YQi

build_ple's grouped_norm reshaped between ggml_rms_norm and ggml_mul, which splits
the pair into two dispatches: the backend emits a fused RMS_NORM_MUL only when the
two are adjacent nodes. Moving the mul up to sit directly on the norm -- and viewing
the [hc_dim] gamma as [n_embd, hc] so it broadcasts over the token axis -- fuses them
with the math unchanged. The lambda already returned [n_embd, hc, n_tokens], so the
trailing reshape was redundant and all three call sites are unaffected.

This is the same block that was fixed in build_hc_mix (dc44c05); grouped_norm is
the remaining unfused site, and it runs three times per PLE layer (key, query, conv).

Found via upstream ggml-org/llama.cpp#28896, which fixes both sites and reports +3% PP
for the pair. We already carry the build_hc_mix half, so only the PLE half was missing.
Upstream reshapes the gammas at load time (TENSOR_ALLOW_RESHAPE); this keeps our
graph-side reshape for consistency with build_hc_mix.

NOT yet measured on this tree -- the change is a strictly-fewer-dispatches refactor
with identical math, and the gain here is the remainder of upstream's 3%, not all of it.
Vulkan does implement the fusion (pipeline_rms_norm_mul_f32), so it applies to our
backend; expect it to help decode as well as prefill, since these ops are tiny and
dispatch-bound at batch 1-4.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019F3dWuvm3aaKMwJTiq3YQi
@routhjim
routhjim merged commit 23a7f12 into main Sep 21, 2026
@routhjim
routhjim deleted the pr/qwen4exp-ple-fusion branch September 21, 2026 23:18
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