Skip to content

qwen4exp: enable rms_norm + mul fusion - #28896

Merged
am17an merged 2 commits into
masterfrom
qwen4exp-hc-normfix
Sep 14, 2026
Merged

am17an merged 2 commits into
masterfrom
qwen4exp-hc-normfix

Conversation

@am17an

@am17an am17an commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Overview

refactor the graph to enable rms_norm + mul fusion. I see about +3% PP because of this.

Additional information

Requirements

@am17an
am17an requested a review from CISC as a code owner September 14, 2026 12:03
@github-actions github-actions Bot added the model Model specific label Sep 14, 2026
Comment thread src/models/qwen4exp.cpp Outdated
ggml_tensor * xn = ggml_rms_norm(ctx0, x, hparams.f_norm_rms_eps);
// the gamma view enters the graph first, so rms_norm and mul stay adjacent for backend fusion
ggml_tensor * w_norm_hc = ggml_reshape_2d(ctx0, w_norm, n_embd, hc);
ggml_build_forward_expand(gf, w_norm_hc);

@ggerganov ggerganov Sep 14, 2026 •

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.

Is the ggml_build_forward_expand(gf, w_norm_hc) call actually needed?

Edit: nvm, it's needed

Comment thread src/models/qwen4exp.cpp Outdated
// the converter folded each gamma to (1 + w)
ggml_tensor * xn = ggml_rms_norm(ctx0, x, hparams.f_norm_rms_eps);
// the gamma view enters the graph first, so rms_norm and mul stay adjacent for backend fusion
ggml_tensor * w_norm_hc = ggml_reshape_2d(ctx0, w_norm, n_embd, hc);

@ggerganov ggerganov Sep 14, 2026 •

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.

You should be able to avoid both of these ggml_reshape_2d calls by loading the weight directly as a 2D tensor via the TENSOR_ALLOW_RESHAPE flag. See how we do it here:

// for wo_a, the shape in the file is (n_head * n_embd_head / o_groups, o_lora_rank*o_groups)
// so we reshape here, to avoid reshaping the tensor in the graph
layer.wo_a = create_tensor(tn(LLM_TENSOR_ATTN_OUT_A, "weight", i), {n_head * n_embd_head / o_groups, o_lora_rank, o_groups}, flags | TENSOR_ALLOW_RESHAPE);

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.

I already forgot about this feature. :)

@am17an
am17an merged commit 41abbfd into master Sep 14, 2026
24 of 27 checks passed
@am17an
am17an deleted the qwen4exp-hc-normfix branch September 14, 2026 14:16
@CISC

CISC commented Sep 15, 2026

Copy link
Copy Markdown
Member

meh pushed a commit to meh/llama.cpp that referenced this pull request Sep 15, 2026
* qwen4exp: enable rms_norm + mul fusion

* use TENSOR_ALLOW_RESHAPE
liquidspikes added a commit to liquidspikes/llama.cpp that referenced this pull request Sep 16, 2026
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
* qwen4exp: enable rms_norm + mul fusion

* use TENSOR_ALLOW_RESHAPE
adderek pushed a commit to adderek/llama.cpp that referenced this pull request Sep 20, 2026
* qwen4exp: enable rms_norm + mul fusion

* use TENSOR_ALLOW_RESHAPE

(cherry picked from commit 41abbfd)
adderek pushed a commit to adderek/llama.cpp that referenced this pull request Sep 20, 2026
Cherry-picked from unslothai/llama.cpp topic branches onto d0e7ec2, then
merged here. The case for taking these is CORRECTNESS, not speed — the two
performance commits in the set touch the GPU path and nothing in the
verification below measured throughput.

  82b4198  fix llm_graph_input_ple reuse
  e339f5d  model: qwen4exp: reduce number of graph splits (ggml-org#27880)
  6f06845  qwen4exp: fix QSA correctness defects and harden metadata loading
  f46d73a  qwen4exp: trim comments
  4594877  qwen4exp: enable rms_norm + mul fusion (ggml-org#28896)

Three conflicts were resolved by hand and are worth knowing about:

  llama-memory-hybrid-idx.cpp  BOTH sides kept. Upstream adds
      hparams_idx.rope_type = LLAMA_ROPE_TYPE_NONE (cached indexer keys are
      raw; rotation happens after pooling at read time, so a K-shift must not
      rotate them). Our no_turbo() guard stays: the indexer keys are read back
      with GET_ROWS, TurboQuant stores them WHT-rotated and has no get_rows,
      so that one cache falls back to f16. Independent fixes at the same spot,
      not alternatives.
  qwen4exp.cpp metadata guards  took upstream. GGML_ASSERT(dsv4_hc_mult > 0)
      becomes a throw with a readable message plus a > 1 check — transformers,
      vLLM and SGLang all reject hc_count <= 1.
  build_ple -> build_inp_ple   took the upstream refactor. Our GGML_UNUSED(inp)
      was dropped: inp is genuinely used in the new build_ple, via
      build_conv_state_at.

Verified on this box, 2026-09-20:
  - compiles and links, zero warnings under -Wall -Wextra -Wpedantic
  - loads Qwen3.8-Flash-Next UD-IQ4_XS (88 GB, the only qwen4exp weights here)
    CPU-only and generates correctly
  - byte-identical output against the pre-merge build (d0e7ec2) at temp 0
    seed 42, on a short prompt AND on a ~2515-token prompt split into ~5
    ubatches, which is where the PLE predecessor history can actually diverge
  - needle recall intact in the long run (a broken n-gram hash fails retrieval
    first, so this is the check that matters)

NOT verified: anything on a GPU. No HIP build was made, so the two perf
commits are unmeasured and the preserved no_turbo() guard ran with f16 KV,
where it had nothing to convert. Its real test is --cache-type-k turbo4 on a
card.

Deliberately not taken: dbac7c4 (superseded one day later by b356fa2 /
ggml-org#28040, which replaces the whole hash-map rebuild with a logarithmic lookup
and carries a measured +4.9% tg at 71k ctx) and ca14269 (the tip of a
7-commit NextN/MTP chain that also needs draft weights we do not have).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
roverdrubber added a commit to roverdrubber/llama.cpp that referenced this pull request Oct 15, 2026
The branch is far behind upstream master and the parts that a card other than
this one needs had drifted. This ports the non-backend half of that sync,
content-matched against upstream rather than replayed by hash: the parser and
JSON schema chain (ggml-org#27764, ggml-org#28736, ggml-org#28742, which adds common/parsers/,
common/json-schema.* and tests/test-json-schema.cpp and retires the three
example scripts it replaces), the chat, grammar, jinja and server fixes
(ggml-org#28302, ggml-org#28469, ggml-org#26885, ggml-org#28279, ggml-org#28511, ggml-org#28620, ggml-org#28817, ggml-org#28749), the
qwen4exp RMSNorm and multiply fusion (ggml-org#28896, hand-ported because the norm
tensors here load with a different shape), the GDN q/k l2 norm as
flash-linear-attention defines it (ggml-org#28068), the ggml_prec_set_acc API that
replaces ggml_mul_mat_set_prec and ggml_flash_attn_ext_set_prec at every call
site, and the scheduler fixes for zero-sized expert ids (ggml-org#28739) and split
inputs (ggml-org#28387).

The GPU backend half of the same sync lands in the following two commits; this
one alone does not build the CUDA or HIP backend, because their call sites move
to the new precision API there.

test-chat, test-json-schema, test-json-schema-to-grammar,
test-grammar-integration and test-grammar-parser pass.

Assisted-by: Claude Opus 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants