Repository navigation
perf(moe): chunk over-capacity prefill on expert boundaries - #3654
Merged
jundot merged 1 commit intoSep 14, 2026
Merged
Conversation
The common adapter halved the token axis until each chunk's working set fit, so an expert routed to by several chunks was fetched again in every one of them, with LRU evicting it in between: 64,369 fetches per 585-token prefill at 12.5% residency on gemma-4-26b-a4b-it-4bit. Sort the routes by expert, cut chunks holding every route of up to `capacity` distinct experts (the DeepSeek V4.1 adapter's sorted-prefill shape), make each chunk's experts resident through the existing ExpertCache.ensure, run the chunk with one expert index per route under the kernel the resident model would choose for the whole call, and restore route order once. Each expert is installed at most once per call. Measured (benchmarks/moe_offload_prefill_bench.py, single runs, warm request): TTFT 16.60 s -> 0.97 s at 12.5%, 8.87 s -> 0.85 s at 25%, 2.38 s -> 0.69 s at 50%; decode untouched. Numerical contract unchanged in kind: bit-exact below the sort threshold, rounding-scale above it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
jundot
approved these changes
Sep 14, 2026
jundot
left a comment
Owner
There was a problem hiding this comment.
Thanks for the implementation. Grouping each expert's routes together removes the repeated loads while evaluating each chunk before its cache slots are reused. I checked the 585- and 2,048-token Gemma paths at 12.5% residency; both matched the previous implementation's 32 greedy output tokens, with substantially lower TTFT. LGTM, I'll merge this.
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 (expert offload primitive). Brings the common adapter's over-capacity prefill to the shape the DeepSeek V4.1 adapter already uses (#3574 / #3628).
Problem
OffloadSwitchGLU.__call__handled a prefill that routes to more distinct experts than the cache holds by halving the token axis until every chunk's working set fit, then running the chunks in order. An expert routed to by several chunks was fetched again in every one of them, with LRU evicting it in between. Ongemma-4-26b-a4b-it-4bita 585-token prompt at 12.5% residency caused 64,369 expert fetches per prefill for 3,840 layer-experts in the model, anddocs/MoE_Expert_Offload.mdlisted prefill as "the clearest follow-up".Change
Over-capacity prefill is chunked on expert boundaries: the routes are sorted by expert on the host, cut into chunks holding every route of up to
capacitydistinct experts, each chunk's experts made resident through the existingExpertCache.ensure, the chunk run with one expert index per route, and the outputs put back in route order once at the end. Each expert is installed at most once per call by construction. The kernel choice mirrors the stockSwitchGLU's rule for the call as a whole (sorted at or above the 64-index threshold, unsorted below), so every chunk runs the kernel the resident model would have used; each chunk is evaluated before the next is built, which bounds the prefill transient as before.Decode and within-capacity calls are untouched (same fast path). The change is confined to
OffloadSwitchGLU;ExpertCacheand the store are not modified, so it composes with #3589 (parallel pread installs) rather than overlapping it.Numerical contract
Unchanged in kind: bit-exact where the kernel path is identical (below the sort threshold both sides run the unsorted kernel; the existing
test_chunked_prefill_bit_exact_below_sort_thresholdnow exercises the new path and still passes), rounding-scale where it is not. Two tests added: every distinct expert is fetched exactly once in an over-capacity prefill (fetch spy), and route order is restored with rounding-scale agreement to the resident model above the threshold.Measured
benchmarks/moe_offload_prefill_bench.py(new, loads throughlm_load_compat+apply_moe_expert_offload+materialize_lazy_state, no scheduler),mlx-community/gemma-4-26b-a4b-it-4bit, M5 Max 128 GB, 585-token prompt, 32 decode tokens, single runs. Before =ea974149, after = this branch. Warm = second identical request; cold = first request after load (page cache cold, pays the initial fill).The before numbers reproduce the doc's existing table (18.1 s / 9.6 s / 2.9 s TTFT at 12.5 / 25 / 50%). Decode differences are single-run noise; the decode path is not touched.
Tests
pytest tests/test_moe_expert_offload.py tests/test_moe_offload_compat.py: 46 passed (3 slow deselected).ruffclean on the changed files; the new benchmark isblack-formatted; existing files were not reformatted.🤖 Generated with Claude Code