Repository navigation
perf(moe): read missing experts in parallel with pread before the slot writes - #3589
Merged
Merged
Conversation
…t writes Every expert cache miss was a synchronous memmap copy on the compute thread, one projection tensor at a time. On Qwen3.8-Flash-Next (48 layers x 512 experts, ~1.84 MB per expert) a 3461-token prompt spent 30 s in TTFT at 68.8% residency and 183 s at 25%: almost all of it waiting on serial reads that have no dependency on each other. CheckpointExpertStore now opens one read-only descriptor per shard up front and splits a fetch into a plan, a pure-bytes read (os.pread, so it carries its own offset and needs no lock) and the host-side conversion. ExpertCache.ensure classifies a call's misses without touching any cache state, starts their reads on a shared pool, and then runs the same serial install loop as before, taking bytes from the pipeline instead of reading them itself. Invariants preserved: slot_of insertion order, eviction victims, the hit/miss counters, the per-slot assignment order and the bf16 uint16 transport are all unchanged, and every mutation still happens on the calling thread in the serial order - the pool only changes when the bytes arrive. An expert unseated by eviction between the two passes falls back to a serial read. OMLX_MOE_OFFLOAD_IO_WORKERS=1 (or anything unparseable) keeps the old path and starts no threads; OMLX_MOE_OFFLOAD_IO_BATCH bounds the experts in flight, and with them the host memory the pipeline holds ahead of the writes. Concurrent ensure() on one cache stays unsupported.
This was referenced Sep 12, 2026
jundot
approved these changes
Sep 18, 2026
jundot
left a comment
Owner
There was a problem hiding this comment.
The parallel reads keep slot updates and LRU ordering on the calling thread, and the results in #3686 support landing this before fetch/compute overlap. I'll merge this and immediately follow up on the read-failure cache state, the IO_BATCH=1 refill case, and the existing prefill test's fetch spy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #2595 / #986. Every expert cache miss in
moe_expert_offload.pywas a serial, synchronous memmap copy on the compute thread, one projection tensor at a time. On a large-expert-count checkpoint that makes prefill and low-residency decode I/O-latency bound rather than bandwidth bound.ExpertCache.ensure()now runs two passes. The first classifies the call's misses without touching cache state and starts their reads on a bounded thread pool withos.pread(one read-only fd per shard; thenp.memmapstore is gone). The second pass is the original serial install loop, taking bytes from the pipeline instead of reading them itself. Every mutation ofslot_of,free,map, the counters and the resident slots still happens on the calling thread in the serial order, so LRU victims, hit/miss counts, resident bytes and the numerical contract are identical to the serial path.OMLX_MOE_OFFLOAD_IO_WORKERS(default 12; 1 or less selects the serial path and creates no pool) andOMLX_MOE_OFFLOAD_IO_BATCH(default 4x workers) bound the threads and the in-flight payload bytes. Docs updated.Measured on an M5 Pro 64 GB with
Vontra/Qwen3.8-Flash-Next-MLX-oQ2-MTP(48 layers x 512 experts, 10 active, about 1.84 MB per expert), PLE table mmapped, MTP off, greedy, same prompts before and after:Generated text was identical to the serial baseline in every case. Loaded size was unchanged (33.9 GB at 68.8 percent).
Tests:
tests/test_moe_expert_offload.py36 fast and 3 slow passed, and again withOMLX_MOE_OFFLOAD_IO_WORKERS=1. NewTestParallelFetchcovers parallel-vs-serial slot and output equality, LRU order and counters, degenerate env values, concurrent store reads, and the in-flight payload bound. The pre-existing ruff findings on untouched lines of these two files are left alone.The natural next step, kept out of this PR, is issuing the next prefill chunk's reads while the current chunk computes in
OffloadSwitchGLU.__call__; the distinct set per chunk is already known up front.