Skip to content

perf(qwen35): gate needsMoeFlush on ExpertStreamingConfig.isEnabled — full-RAM 35B-A3B path 3.4× - #34

Merged
solderzzc merged 1 commit into
SharpAI:mainfrom
ericjlake:pr/qwen35-needsmoeflush-gate
Apr 26, 2026
Merged

solderzzc merged 1 commit into
SharpAI:mainfrom
ericjlake:pr/qwen35-needsmoeflush-gate

Conversation

@ericjlake

@ericjlake ericjlake commented Apr 26, 2026 •

Copy link
Copy Markdown

Companion PR to SharpAI/SwiftLM #84 / SharpAI/SwiftLM#85.

This is the headline perf change from issue #84: gates the per-MoE-layer eval + Stream.gpu.synchronize() blocks in Qwen35DecoderLayer.callAsFunction on ExpertStreamingConfig.shared.isEnabled. The flushes are necessary on the SSD-streaming code path (give the SSD load_sync a fresh, empty Metal command buffer so the expert GEMM doesn't queue behind unrelated GPU work — the FLUSH-LOAD-EXECUTE comment in the file documents the rationale) but pure overhead on the full-RAM path: 32 layers × 2 syncs per layer = 64 hard CPU↔GPU syncs per token, capping GPU utilization well below 100% and serializing kernel launches that MLX would otherwise pipeline.

Result

Qwen3.6-35B-A3B-UD-MLX-4bit on M1 Ultra 64 GB, full-GPU, no --stream-experts, no --draft-model, --repeat-penalty 1.1, temperature: 0.6:

Prompt Tokens TTFT Gen tok/s (before) Gen tok/s (after)
Short (~126 tok) 126 0.56s 19.2 63.12
Medium (~400 tok) 400 0.38s 18.1 63.98
Long (~800 tok) 800 0.16s 18.3 63.24

3.4× steady-state speedup. No errors, no warnings. Memory and swap behavior unchanged (still fully resident).

Diff

One file, +20/-11. The change introduces a local needsMoeFlush flag combining the existing Qwen35SparseMoeBlock check with ExpertStreamingConfig.shared.isEnabled, then gates both the pre-MLP and post-MLP flush blocks on that flag.

// Was: fired unconditionally on every Qwen35SparseMoeBlock layer.
// Now:
let needsMoeFlush = (self.mlp is Qwen35SparseMoeBlock)
    && ExpertStreamingConfig.shared.isEnabled

if needsMoeFlush {
    if let cacheState = cache {
        eval([h] + cacheState.innerState())
    } else {
        eval(h)
    }
    Stream.gpu.synchronize()
}

let mlpOutput = (self.mlp as! UnaryLayer)(postAttentionLayerNorm(h))
let finalH = h + mlpOutput

if needsMoeFlush {
    eval(finalH)
    Stream.gpu.synchronize()
}

SSD-streaming retains its load_sync isolation guarantee (via ExpertStreamingConfig.shared.isEnabled == true when --stream-experts activates the streamer); full-RAM path becomes lazy-eval-only across the layer chain so MLX can batch kernel dispatches.

Hardware / repro

Test plan

  • Full-RAM path: 3.4× speedup verified on Qwen3.6-35B-A3B-UD-MLX-4bit (M1 Ultra 64 GB).
  • SSD-streaming path: bench unchanged on Qwen3.5-122B-A10B-4bit with --stream-experts — needsMoeFlush still fires when streaming is enabled, preserves the FLUSH-LOAD-EXECUTE invariant.
  • No errors or warnings in the server log during steady-state runs.

Companion PR

SharpAI/SwiftLM PR with the prompt-cache bleed fixes: SharpAI/SwiftLM#85

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes Qwen3.5/3.6 MoE decoding performance by avoiding per-layer GPU flush/synchronize overhead when expert SSD-streaming is not active (full-RAM path), while preserving the flush isolation behavior required for SSD streaming.

Changes:

  • Introduces a local needsMoeFlush flag combining “this layer is MoE” + ExpertStreamingConfig.shared.isEnabled.
  • Gates the pre-MLP and post-MLP eval(...) + Stream.gpu.synchronize() flush blocks on needsMoeFlush.
  • Updates the in-code rationale/commentary to describe why flushing is only required during SSD expert streaming.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +562 to +563
let needsMoeFlush = (self.mlp is Qwen35SparseMoeBlock)
&& ExpertStreamingConfig.shared.isEnabled

Copilot AI Apr 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needsMoeFlush is gated on ExpertStreamingConfig.shared.isEnabled, but earlier in this same callAsFunction the SSD/prefault path is still gated on ProcessInfo.processInfo.environment["EXPERIMENTAL_SSD_STREAM"] (line ~525). This creates two independent “streaming enabled” switches, so enabling expert streaming via ExpertStreamingConfig.activate(...) (the intended replacement for the env var) won’t trigger the prefault watchdog-bypass behavior. Consider switching the prefault gate to ExpertStreamingConfig.shared.isEnabled (or otherwise centralizing the condition) so all SSD-streaming behaviors are enabled consistently.

Suggested change
let needsMoeFlush = (self.mlp is Qwen35SparseMoeBlock)
&& ExpertStreamingConfig.shared.isEnabled
let isExpertStreamingEnabled =
ExpertStreamingConfig.shared.isEnabled
|| ProcessInfo.processInfo.environment["EXPERIMENTAL_SSD_STREAM"] != nil
let needsMoeFlush = (self.mlp is Qwen35SparseMoeBlock)
&& isExpertStreamingEnabled

Copilot uses AI. Check for mistakes.
@solderzzc

Copy link
Copy Markdown
Member

Thanks Eric — this is a clean, well-documented fix. The 3.4× number speaks for itself.

Re: Copilot's observation about the dual gate (env var at line ~525 vs. ExpertStreamingConfig.shared.isEnabled here) — good catch from the bot. The EXPERIMENTAL_SSD_STREAM env var in the prefault block is a legacy gate from earlier development; the canonical path is ExpertStreamingConfig.shared.isEnabled which is what --stream-experts sets. In practice they're always in sync, but we should migrate the prefault block to use the same API as a follow-up cleanup. Not something to hold this PR on.

Merging — thank you! 🎉

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