Skip to content

perf(qwen35): skip the per-layer MoE GPU flush on single-token decode - #77

Merged
solderzzc merged 2 commits into
SharpAI:mainfrom
CodeAndCanvas728:perf/qwen35-skip-moe-flush-on-decode
Oct 6, 2026
Merged

solderzzc merged 2 commits into
SharpAI:mainfrom
CodeAndCanvas728:perf/qwen35-skip-moe-flush-on-decode

Conversation

@CodeAndCanvas728

Copy link
Copy Markdown

What

In Qwen35DecoderLayer.callAsFunction, the "flush-load-execute" syncs (eval + Stream.gpu.synchronize() before and after each MoE layer, only with SSD expert streaming) now run only for prefill (x.dim(1) > 1). Single-token decode skips them. One condition, no other change.

Why

With --stream-experts the text-only (LLM) Qwen3.5 path syncs the GPU twice per MoE layer, about 80 hard syncs per decoded token on a 40-layer model. The VLM implementation of the same model has no such flush and streams fine. This is why a vision-capable checkpoint loaded with --no-vision (SwiftLM#206, needed for the prompt cache) decoded about 19% slower than the same weights loaded as a VLM.

Measurements

Rig: 16 GB M2 Air, 35B-A3B hybrid MoE (30 GatedDeltaNet + 10 attention layers, 256 experts top-8, mixed 4/5/8-bit affine), --stream-experts, greedy, 588-token code prompt, 100 new tokens, iogpu.wired_limit_mb=13500. Same binary for every arm (an env switch selected the behaviour), alternating order, 3 repetitions per arm, 3 requests each.

Arm Decode tok/s (mean)
VLM load (no flush in this path) 5.5
text-only, flush always (before) 4.4
text-only, no flush at all 5.3
text-only, flush for prefill only (this PR) 5.4

At a ~8k-token prompt (--prefill-size 2048): 5.9 and 5.1 tok/s against 4.8 and 4.0 before. Generated text was identical in every run, there were no GPU timeouts, and TTFT was unchanged (prefill still flushes).

Not covered

  • The flush was added for models that swap under memory pressure (the comment mentions a 122B model on 64 GB). I only tested a 35B on 16 GB with swap below 14 GB. If decode on a heavily swapping setup needs the isolation, say so and I will gate this behind a flag instead.
  • Mistral4.swift and SwitchLayers.swift also call synchronize; I did not touch them.

CodeAndCanvas728 and others added 2 commits October 6, 2026 16:54
With --stream-experts the text-only Qwen3.5 decoder layer evaluates and
synchronizes the GPU before and after every MoE layer (about 80 syncs per
token). Keep that for prefill, where it isolates the expert loads, but skip
it when decoding one token: the VLM implementation of the same model never
flushed.

Measured on a 16 GB M2 with a 35B-A3B 4/5/8-bit checkpoint, ABAB, 3 reps per
arm: decode 4.4 -> 5.4 tok/s (+22%); at an 8k-token prompt 4.8/4.0 -> 5.9/5.1.
Generated text identical, no GPU timeouts, TTFT unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…t, fix comment

Move the per-layer MoE flush decision into a pure static function so it can be
unit tested without weights or a GPU. MLX_MOE_DECODE_FLUSH=1 restores the old
flush on single-token decode, in case a heavily swapping setup needs it. The
comment now describes the actual ordering invariant (SwitchGLU's per-layer router
readback plus the token dependency chain) instead of the stale "VITAL" text.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@solderzzc

Copy link
Copy Markdown
Member

Thanks @CodeAndCanvas728, this is a clean, well-measured change.

I went through the paths and also ran a differential test on a small synthetic Qwen3.5 MoE with --stream-experts. For single-token decode, SwitchGLU's per-layer router-index readback plus the token dependency chain already order the expert reads, so the two extra syncs look redundant. Prefill and speculative/MTP verify keep them, which is the right call. Output was byte-identical to base across repeated runs, under CPU load and under Metal API/shader validation. The tiny model says nothing about the 35B speedup, so your numbers are the evidence there.

To save you a round trip, I pushed one small follow-up commit on top of yours (a plain push, nothing rewritten): 44ae062

  • the gate is now a small static function, Qwen35DecoderLayer.needsMoeFlush(...), with four unit tests
  • MLX_MOE_DECODE_FLUSH=1 restores the old flush on decode, since nobody has tried a 122B model that is actually swapping
  • the comment now describes the ordering invariant instead of the old "VITAL" text

Please take a look and revert anything you don't like. Nice work.

@solderzzc
solderzzc merged commit a8c85d5 into SharpAI:main Oct 6, 2026
7 checks passed
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.

3 participants