Add --moe-mode sweep-prefill: streamed expert supply for CPU mul_mat_id#4
Open
Zimo41650079726sub wants to merge 2 commits into
Open
Add --moe-mode sweep-prefill: streamed expert supply for CPU mul_mat_id#4Zimo41650079726sub wants to merge 2 commits into
Zimo41650079726sub wants to merge 2 commits into
Conversation
--moe-mode sweep-prefill turns prefill expert reads into a physical-order streaming sweep. The CPU mul_mat_id kernel already iterates experts in ascending id order over contiguous mmap slices, so the math is untouched; eligible expert tensors (verified to lie inside the file-backed mapping) are marked at load via their unused leaf op_params, and thread 0 of the kernel adds WILLNEED readahead for the next used slices plus a lagged DONTNEED discard behind, keeping the page-cache footprint at a few slices. Decode ubatches stay below --moe-sweep-min-tokens (default 32) and use the regular path. GGML_SWEEP_STATS=1 logs per-node sweep statistics. Greedy output is byte-identical to the stock golden run.
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.
Motivation
When a MoE model is much larger than RAM and served from NVMe via mmap (CPU backend), prefill touches nearly every expert: on DeepSeek-V4-Flash 284B we measured 44–100% of the 256 experts per layer used within a single 512-token ubatch. The default demand-paging path reads them through page faults in routing order, which looks random to the kernel, so readahead never engages and prefill becomes fault-latency bound.
On systems with a GPU backend, expert upload overlaps with GPU compute. On CPU-only hosts (this work was done on Snapdragon X Elite under WSL2) compute and I/O share the same cores, so there is no natural overlap — the only leverage left is telling the kernel what to read next.
Mechanism
The CPU
mul_mat_idkernel already iterates experts in ascending id order over contiguous mmap slices, so expert access during one node evaluation is a physical-order sweep. This PR exploits that:op_params— the compute graph is unchanged.madvise(WILLNEED)for the next 2 used expert slices ahead of the loop, and a laggedmadvise(DONTNEED)2 slices behind, keeping the page-cache footprint at a few slices.--moe-sweep-min-tokens(default 32) take the regular path, so decode behavior is untouched.GGML_SWEEP_STATS=1logs per-node sweep statistics.The math is untouched; only page-cache population timing changes.
Correctness
qwen35moearch): greedy output byte-identical to stock, in both unconstrained and 8 GB-cgroup conditions (sweep engagement confirmed viaGGML_SWEEP_STATS; loader registered 120 expert tensors).Results
Snapdragon X Elite (12-core Oryon, WSL2 Ubuntu, NVMe ~7 GB/s seq), cold page cache:
When the model fits in RAM (warm cache, no cgroup) sweep-prefill is slightly slower (52.8 vs 56.1 tok/s on the same Qwen3.5 setup) because
DONTNEEDevicts cache that would have been reused — this is why the mode is opt-in rather than default.Scope and limitations
ggml-cpu); opt-in via--moe-mode sweep-prefill.WILLNEEDis a no-op andDONTNEEDwould evict useful cache — hence opt-in, not default.Notes from the road not taken
We tried the textbook interventions first; all were neutral or harmful in this regime:
fadvise(DONTNEED)after reads destroyed the kernel's (correct) caching decisions,--no-mmaptraded thrash for slower steady state, and O_DIRECT is incompatible with letting the page cache act as a second-level expert cache. The kernel LRU behaves well here — the single gap is that it cannot predict expert access order, which this PR fills with a two-slice-ahead hint.🤖 Generated with Claude Code