Skip to content

model : build the qwen4exp shared expert before the routed experts - #1

Draft
anothergasteizone wants to merge 1 commit into
masterfrom
qwen4exp-shared-expert-first
Draft

anothergasteizone wants to merge 1 commit into
masterfrom
qwen4exp-shared-expert-first

Conversation

@anothergasteizone

@anothergasteizone anothergasteizone commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Overview

Build the qwen4exp shared expert before the routed experts, so that the final moe_out + shexp add lands right after the expert sum in the graph. The add then joins the existing add chain (one fused MULTI_ADD on Vulkan) instead of running as its own dispatch.

build_moe_ffn expands the routed experts into the graph, so the shared expert used to come after the expert sum and split the add chain. Expanding the shared expert first only changes the node order. The result is bit-identical on every backend.

Additional information

Measured on an AMD Radeon AI PRO R9700 (RDNA4, 32 GB) used as an eGPU over Thunderbolt 3, where the cost per dispatch is high. Model: Qwen4-35B-A3B UD-Q4_K_XL. On its own the speedup is below the run-to-run noise here (about 2%); it adds up with other dispatch reductions.

Qwen4-35B-A3B, R9700 eGPU before this PR
Dispatches per decode token 1886 1847 (-39)
Standalone ADD per decode token 100 61 (MULTI_ADD now sums 9 inputs: 8 experts + shared expert)
KL divergence vs before (prefill / decode) - 0.000000 / 0.000000
Same top p vs before - 100%
temp 0 output - byte-identical

"before" is a branch that already carries ggml-org#33710 (hc_post fusion fix) and ggml-org#33286.

It is part of a set of changes measured together with ggml-org#33710 and ggml-org#33286, and combines with ggml-org#33412 (generated epilogue fusion, R9700 eGPU numbers in ggml-org#33412 (comment)), where the shared expert is then summed in the same fused add. All together, decode goes from 80.7 to 93.4 t/s (+16%) on this setup:

Qwen4-35B-A3B, R9700 eGPU (t/s) tg128 @ d0 tg128 @ d8192 pp2048
master 80.7 76.5 3365
ggml-org#33710 + this PR + ggml-org#33286 83.2 (+3.1%) 78.8 (+3.0%) 3455
master + ggml-org#33412 90.8 (+12.5%) 88.3 (+15.4%) 3522
all of the above + ggml-org#33412 93.4 (+15.7%) 89.7 (+17.3%) 3690

qwen3next, qwen35moe and qwen2moe build the shared expert the same way; I can follow up there if this is wanted.

Setup, commands and measurement notes

Hardware: AMD Radeon AI PRO R9700 (RDNA4, GFX1201, 32 GB) as an eGPU over Thunderbolt 3, RADV (Mesa 26.3.1), Ryzen 7 7840HS, Ubuntu 26.04. Model: Qwen4-35B-A3B UD-Q4_K_XL (unsloth), -ngl 99 -ot per_layer_token_embd=CPU -fa 1 -ctk q8_0 -ctv q8_0 -ub 1024 -b 4096.

Dispatch counts from GGML_VK_PERF_LOGGER. KL divergence: llama-perplexity --kl-divergence, 4x512 tokens (prefill, -ub 512) and 2x512 tokens (decode, -ub 1).

llama-bench: 5 rounds x 3 reps per row; the first two rows and the last two were measured in separate sessions, with about 2% drift between sessions. Run-to-run noise is about 2% for tg and about 5% for pp2048, so the prefill column is not conclusive.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code helped find this from profiling, write the change, run the validation and write this description. I reviewed the change and the results.

Written with Claude Code (Claude Opus 5.5)

build_moe_ffn expands the routed experts into the graph, so the shared
expert came after the expert sum and the final moe_out + shexp add ran
as its own node. Expanding the shared expert first puts that add right
after the expert sum, so it joins the same add chain (one fused
MULTI_ADD on Vulkan). Only the node order changes; the result is
bit-identical.

Assisted-by: Claude Code (Claude Opus 5.5)
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