Repository navigation
perf(qwen35): gate needsMoeFlush on ExpertStreamingConfig.isEnabled — full-RAM 35B-A3B path 3.4× - #34
Conversation
There was a problem hiding this comment.
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
needsMoeFlushflag combining “this layer is MoE” +ExpertStreamingConfig.shared.isEnabled. - Gates the pre-MLP and post-MLP
eval(...)+Stream.gpu.synchronize()flush blocks onneedsMoeFlush. - 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.
| let needsMoeFlush = (self.mlp is Qwen35SparseMoeBlock) | ||
| && ExpertStreamingConfig.shared.isEnabled |
There was a problem hiding this comment.
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.
| 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 |
|
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. Merging — thank you! 🎉 |
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 inQwen35DecoderLayer.callAsFunctiononExpertStreamingConfig.shared.isEnabled. The flushes are necessary on the SSD-streaming code path (give the SSDload_synca 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-4biton M1 Ultra 64 GB, full-GPU, no--stream-experts, no--draft-model,--repeat-penalty 1.1,temperature: 0.6: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
needsMoeFlushflag combining the existingQwen35SparseMoeBlockcheck withExpertStreamingConfig.shared.isEnabled, then gates both the pre-MLP and post-MLP flush blocks on that flag.SSD-streaming retains its
load_syncisolation guarantee (viaExpertStreamingConfig.shared.isEnabled == truewhen--stream-expertsactivates the streamer); full-RAM path becomes lazy-eval-only across the layer chain so MLX can batch kernel dispatches.Hardware / repro
Qwen3.6-35B-A3B-UD-MLX-4bit(Unsloth Dynamic 4-bit, 256 routed + 1 shared expert, top-8 routing)SharpAI/SwiftLMissue Sync upstream ml-explore/mlx-swift-lm main (48114ee) #84.Test plan
Qwen3.6-35B-A3B-UD-MLX-4bit(M1 Ultra 64 GB).Qwen3.5-122B-A10B-4bitwith--stream-experts—needsMoeFlushstill fires when streaming is enabled, preserves the FLUSH-LOAD-EXECUTE invariant.Companion PR
SharpAI/SwiftLMPR with the prompt-cache bleed fixes: SharpAI/SwiftLM#85