Skip to content

perf(loadtime): split big-MoE quantize + per-layer asyncEval - #216

Merged
ekryski merged 1 commit into
ekryski:alphafrom
TheTom:pr/alpha-loadtime-hardening
May 14, 2026
Merged

ekryski merged 1 commit into
ekryski:alphafrom
TheTom:pr/alpha-loadtime-hardening

Conversation

@TheTom

@TheTom TheTom commented May 14, 2026

Copy link
Copy Markdown

Summary

Two changes that prevent Metal's GPU command-buffer watchdog (~2s) from killing model load + first-forward on very large MoE models — surfaced while bringing up DeepSeek-V4-Flash (43 layers × 256 experts, 90 GB on disk).

`Load.swift` — `eval(model.parameters())` after `quantize(model:)`

`quantize(model:)` walks the module tree and emits a fresh-quantize op per `Quantizable` leaf. For DeepSeek-V4 (43 layers × 256 experts × {gate,up,down} SwitchLinear ≈ 33K modules) the lazy graph fuses these into a single Metal command buffer that overruns the watchdog at first forward.

Splitting with one `eval(model.parameters())` after the `quantize()` walk forces eager materialization of each module's quant op separately. Cheap on small models (one eval over a graph that was about to evaluate anyway). Decisive on big MoE.

`DeepseekV4.swift` — per-layer `asyncEval` during prefill

Same watchdog symptom on the forward path. Per-layer `asyncEval` splits the 43-layer graph at layer boundaries so the GPU pipeline stays fed without any single command buffer exceeding the watchdog. Mirrors the pattern already in `Qwen35TextModelInner` and `NemotronHBackbone`.

Test plan

  • No-op on small quantized models (Qwen3-0.6B-4bit, Llama-3.2-3B-4bit) — load time unchanged, decode tok/s unchanged
  • DeepSeek-V4-Flash-2bit-DQ end-to-end coherency — deferred (model is 90 GB and triggers OS-level memory pressure on local test boxes; needs the bigger M-series rig)

Notes

Both changes are defensive: they take a code path that was already used for Qwen35/NemotronH on the forward and extend it to the load + DeepseekV4 forward.

🤖 Generated with Claude Code

Two changes that prevent Metal's GPU command-buffer watchdog (~2s)
from killing model load + first-forward on very large MoE models.

**Load.swift — eval(model.parameters()) after quantize**

`quantize(model:)` walks the module tree and emits a fresh-quantize
op per Quantizable leaf. For DeepSeek-V4 (43 layers × 256 experts ×
{gate,up,down} SwitchLinear ≈ 33K modules) the lazy graph fuses
these into a single Metal command buffer that overruns the watchdog
at first-forward.

Splitting with one `eval(model.parameters())` after the quantize()
walk forces eager materialization of each module's quant op
separately. Cheap on small models (one cheap eval over a graph that
was about to evaluate anyway). Decisive on big MoE.

**DeepseekV4.swift — per-layer asyncEval during prefill**

Same watchdog symptom on the forward path. Per-layer asyncEval
splits the 43-layer graph at layer boundaries so the GPU pipeline
stays fed without any single command buffer exceeding the watchdog.
Mirrors the pattern already used in Qwen35TextModelInner and
NemotronHBackbone.
@ekryski ekryski added the performance Performance improvement (auto-applied from perf: prefix) label May 14, 2026
@ekryski

ekryski commented May 14, 2026

Copy link
Copy Markdown
Owner

Nice catch on the watchdog timeout — the per-layer asyncEval extension to DeepseekV4 mirrors the existing Qwen35/NemotronH pattern cleanly, and the post-quantize eval(model.parameters()) is exactly the right kind of one-line defensive fix that prevents future big-MoE bring-ups from hitting the same wall. Thanks Tom!

@ekryski
ekryski merged commit 3cf175d into ekryski:alpha May 14, 2026
4 of 5 checks passed
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
Integrate an isolated non-serving model correction with its regression assertions. Preserve concurrent main changes and promote the resolved recording checks to ordinary assertions. No deployment or serving dependency update.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance improvement (auto-applied from perf: prefix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants