Repository navigation
qwen4exp : fuse the PLE grouped RMSNorm with its scale - #40
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
build_ple'sgrouped_normreshaped betweenggml_rms_normandggml_mul, which splitsthe pair into two dispatches — the backend emits a fused
RMS_NORM_MULonly when the twoare 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 withthe 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 wasredundant; all three call sites (key, query, conv) are unaffected.
Context
This is the same defect fixed in
build_hc_mix(dc44c05) —grouped_normwas theremaining 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_mixhalf, so only the PLE half was missing here. Upstream reshapes the gammas atload time (
TENSOR_ALLOW_RESHAPE); this keeps our graph-side reshape for consistency withbuild_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 toour 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_mixbug was originally spotted in a decode profile(
RMS_NORM dst(f32 2560,4)running unfused).qwen4exp.cpp: we and upstream fixed bothbuild_hc_mixand this block differently on the same lines.🤖 Generated with Claude Code
https://claude.ai/code/session_019F3dWuvm3aaKMwJTiq3YQi