Repository navigation
perf(deepseek_v41): run resident experts first in sorted prefill routes - #4959
Conversation
478befa to
a98a9e1
Compare
A sorted call over more experts than slots used to evict every resident
expert before reaching it: each chunk protected only its own experts, so
chunk 1 (lowest ids) replaced all slots and the experts a chunk later
needed had to be read again. A prefill forward therefore read every
expert it touched, about 384 per layer at 8K tokens, no matter what was
resident.
_sorted now runs one group with the resident experts first (no reads,
while the first miss group is read ahead), then the misses in groups of
at most `capacity` experts in ascending id order. Outputs are put back in
the routes' sorted order. Routing and arithmetic are unchanged; on CPU the
outputs are bitwise identical to the previous chunking.
Measured on M4 Max 128 GB, DeepSeek-V4.1-Flash-1017-oQ2.5e, 131/384
experts resident, agent pattern (9K fresh + 14K and 10K prefix-cache
suffixes), 3-4 interleaved blocks per arm:
hits per full forward 1836 -> 4038, expert reads 108 -> 79 GB
full 8K forward 36.9 -> 32.0 s, short ~1K forward 18.3 -> 13.8 s
TTFT per round S1 56.7 -> 46.8 s, S2 72.5 -> 62.9 s,
S3 78.5 -> 62.7 s (sum 207.8 -> 172.4 s, -17%)
decode ms/cycle unchanged
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
a98a9e1 to
5419431
Compare
|
Rebased onto CI: One note for local runs with the 0.7.3rc1 app bundle, in case it shows up elsewhere: that bundle vendors an mlx-vlm 0.7.10 wheel, which has no |
Measured: what the resident-first ordering does under real expert offloadI benchmarked this on an offloaded DeepSeek-V4.1 Flash checkpoint, where every miss is an actual disk read — that is the case this change targets, and it shows up directly in the server's offload counters. Setup
Method
100k prompt (13 forwards), one run per arm
25k prompt, two runs per arm (fresh prompt each, values are means)
How I read it
Caveats
Happy to re-run at another depth or residency fraction if a specific number would help the review — the harness is a single script. |
jundot
left a comment
There was a problem hiding this comment.
With id-order chunks the first chunk evicted every resident expert before the loop reached it, so each full prefill forward re-read everything it touched. Running the resident group first keeps those slots, and the new test shows it directly (on the old chunking it fails with the resident experts read again, here it passes). The read drop in your 100k run lines up with the share of slots that stay resident.
Thanks for the per-forward counter breakdown too. LGTM, merging.
#4951 has landed as 9aa3f28, so this is now a single commit on
main:perf(deepseek_v41): run resident experts first in sorted prefill routesis the whole diff.Content unchanged, only the base moved — the resident-first change can be reviewed on its own.
Problem
OffloadedExpert._sortedcut the expert-sorted prefill routes into chunks of up tocapacitydistinct experts and calledensure_idsper chunk.ensure_idsprotects only the experts of the current call, so on a sweep over more experts than slots, chunk 1 (lowest ids) evicted every resident expert, chunk 2 evicted chunk 1, and so on. By the time a chunk reached an expert that had been resident when the call started, it had already been evicted. Every prefill forward therefore read every expert it touched, regardless of what was resident (the existing test even asserted this: "sorted routes over more experts than slots thrash by construction").With V4.1-Flash and 131/384 experts resident, an 8K-token chunk touches ~364 experts per layer in the lower 20 layers, so each forward read ~108 GB from SSD.
Change
_sortednow builds expert groups with_groups:capacitydistinct experts per group (as before).Each group gathers the routes of its experts (positions stay ascending, so the group's ids stay sorted), runs as one
sorted_indices=Truecall, and the outputs are put back in the routes' sorted order, whichcombine_sorted_expertsexpects. Routing, arithmetic, read-ahead window and eviction policy are unchanged. On CPU the outputs are bitwise identical to the previous chunking; with no resident experts (first sweep) the groups are exactly the previous chunks.Measurements
M4 Max 128 GB,
DeepSeek-V4.1-Flash-1017-oQ2.5e-mtp, MoE offload 131/384 resident (fraction 0.34), prefill step 8192, CED prefill on, temperature 0. Agent-like pattern per block: fresh load → 3.4K warm-up → S1 9.2K fresh prompt → S2 +14.4K suffix (prefix-cache hit) → S4 19-token follow-up → S3 +10.2K suffix, 300 tokens each, real texts with a nonce. Arms interleaved (A R R A, A R RP RP R A), 3–4 blocks per arm; per-forward counters from instrumented_ExpertSlots.Things I tried alongside, for reference (not in this PR):
Tests
test_sorted_routes_chunk_on_expert_boundaries: the second sweep now runs the resident pair first — 6 reads instead of 8, same outputs.test_resident_first_groups_restore_the_sorted_route_order(3 seeds × 4 sweeps): random routes and weights, outputs allclose to the resident reference, experts resident before a call are never read by it, read-ahead drained.tests/test_deepseek_v41_moe_offload.py tests/test_deepseek_v41_offload.py tests/test_moe_expert_offload.py→ 136 passed, 3 skipped, 4 deselected. The 13 failures of the same run are allModuleNotFoundError: No module named 'mlx_vlm.models.d1_lfm3_vl': my local 0.7.3rc1 bundle vendors an mlx-vlm 0.7.10 wheel without that submodule, which exists from v0.7.11 (138d761) — the versionmainpins. Unrelated to this diff.🤖 Generated with Claude Code