Skip to content

perf(moe): read missing experts in parallel with pread before the slot writes - #3589

Merged
jundot merged 2 commits into
jundot:mainfrom
beaglemoo:perf/moe-offload-parallel-fetch
Sep 18, 2026
Merged

jundot merged 2 commits into
jundot:mainfrom
beaglemoo:perf/moe-offload-parallel-fetch

Conversation

@beaglemoo

Copy link
Copy Markdown
Contributor

Follow-up to #2595 / #986. Every expert cache miss in moe_expert_offload.py was 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 with os.pread (one read-only fd per shard; the np.memmap store is gone). The second pass is the original serial install loop, taking bytes from the pipeline instead of reading them itself. Every mutation of slot_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) and OMLX_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:

residency case before after
25% 3461-token TTFT 183 s 18.7 s
25% decode after that prompt 4.4 tok/s 17.7 tok/s
25% 122-token warm decode 2.3 tok/s 16.2 tok/s
68.8% 3461-token TTFT 30.1 s 4.5 s
68.8% decode after that prompt 13.6 tok/s 19.7 tok/s
68.8% 122-token warm decode 23.9 tok/s 22.4 tok/s

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.py 36 fast and 3 slow passed, and again with OMLX_MOE_OFFLOAD_IO_WORKERS=1. New TestParallelFetch covers 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.

…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.

@jundot jundot left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

@jundot
jundot merged commit bdd1317 into jundot:main Sep 18, 2026
0 of 3 checks passed
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.

2 participants