Repository navigation
common : add --moe-cache-mib auto to size the MoE expert cache from the fit - #251
danielhanchen wants to merge 18 commits into
Conversation
Assisted-by: Claude
With --moe-cache-mib auto, --fit sizes the MoE cache when the experts do not fit in device memory: the memory left after the dense weights, the context and the compute buffers goes to the cache, or with --moe-cache-static F a fraction F of it to the experts of whole layers kept in device memory (first layers first). The result is measured again with the cache allocated and the cache shrinks by any overshoot. If the cache would be smaller than n_expert_used + 1 slots per group of cached layers, fit falls back to whole layers without a cache. Only for a MoE model on a single GPU, otherwise no cache is used. llama-fit-params prints the chosen --moe-cache-mib. (cherry picked from commit 0c0cd60)
- roll back a partially applied MoE cache on errors during the fit - reduce the static layers and re-measure if they overshoot - re-measure up to 4 times while shrinking the cache - require a cache of at least twice the minimum, no cache if no layer is in host memory - do not cache the NextN/MTP layers in the main context (cherry picked from commit e5b686c)
… reject a non-numeric --moe-cache-static A context that shares the main model (MTP) runs on its weights wherever the fit places them, so its memory was measured once with its own params and went stale when the fit moved experts to the host. Measure it with the main model params and again whenever their placement changes. std::stof accepted NaN and trailing junk for --moe-cache-static. (cherry picked from commit 82aa472)
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
ggml-org#29887 and ggml-org#30112 (the MoE cache over multiple GPUs) are now upstream, so the cache files take upstream's version. --moe-cache-mib auto keeps its NextN/MTP layer filter and min_size beside it, as an MTP constructor overload and a separate check, so every upstream line stays as it is.
Drop --moe-cache-static: every measurement found the full cache best or within 5% once experts spill, so the static share and its layer bookkeeping go, along with llama_model_n_bytes_exps. Drop the NextN/MTP layer filter and the MTP constructor overload, which leaves upstream's cache files untouched apart from the minimum size the fitter needs. common_fit_params takes a bool instead of a struct.
…at merge past b11453, drop unslothai#247, pin unslothai#251 Upstream K2 Horizon (ggml-org#29535), the glm5-next gather removal (ggml-org#30042) and the GLM5-Next MTP graph (ggml-org#29928) broke the Inkling, unslothai#243 and unslothai#241 pins. Each branch now carries a merge of upstream master. unslothai#247 is in every tag from b11452 on. unslothai#251 adds --moe-cache-mib auto on top of ggml-org#29887.
…29887 and ggml-org#30112 merged ggml-org#29887 (the MoE expert cache) merged on 10-07 and ggml-org#30112 (the cache over multiple GPUs) on 10-08. On b11496 and later the old Inkling head refused in six files and unslothai#251, which carried its own copy of ggml-org#29887, refused in eight. Both branches now merge upstream master c811cb8. unslothai#251 keeps upstream's cache lines intact, so the merged tree holds both pins in full.
unslothai#251 drops --moe-cache-static and the NextN/MTP cache filter, leaving the auto sizing and the shared-MTP re-measure (+165/-17 against upstream).
unsloth: repin Inkling, unslothai#241 and unslothai#243 past b11453, drop unslothai#247, pin --moe-cache-mib auto (unslothai#251)
dmds_full can be measured at the native context while the fit has already reduced the context, so the difference of the totals counted the KV cache as expert weights and picked the cache where the layer split keeps most experts resident. Also log the last measured cache size on failure.
…PU device The fit writes its expert placement into params, but the cache size it chose stayed in the context params, so a reload from params (the server waking from sleep) kept the experts in host memory without a cache. The minimum cache size is 0 unless the device is a GPU the cache can use, and --moe-cache-mib rejects values with trailing characters.
pr-set : repin unslothai#251 for b12191
pr-set : repin unslothai#251 and unslothai#332 for b12233 (ggml-org#31185)
|
Before: After, when the only override is Code: before, fit.cpp#L475-L480, after, fit.cpp#L516-L523, sizing loop, #L382-L406.
On a Radeon 8060S (gfx1151, 128 GB, Linux, HIP 7.16), same flags, head -> fix: gpt-oss-120b 21.0 -> 38.0, Qwen3-30B-A3B 42.3 -> 66.1, gpt-oss-20b 31.3 -> 56.2 tok/s. Same on CUDA with the models and method from the PR table: L4 24 GB (sm_89),
The first two columns are head; the fix gives the same numbers there (within run-to-run noise). With the fix, Same models and method on a Radeon 8060S (Strix Halo, gfx1151, 128 GB unified memory, Linux, HIP 7.16), no budget emulated (the whole model fits in device-visible memory), decode tok/s, median of 3:
GLM-5.3-Flash with The greedy 96-token answer of every cache cell is byte-identical to the full-GPU run. A Radeon 8060S (gfx1151, Linux, HIP 7.16) shows the same: granite-3.1-3b-a800m 86.1 -> 138.1 tok/s, output identical in all 8 cells. Explicit sizes, plain Same models and method on an R9700 32 GB (gfx1201, Windows 11, ROCm 7.1), fix at
The fix matches plain auto in every row (5938 / 2047 MiB caches). Unlike the L4, at a 5.8 GiB budget plain One more thing from these runs, in the auto design itself and present on the head too: auto accepts any cache of at least 2x the one-token minimum, but a cache that holds too little of the experts decodes slower than no cache. Sweeping
Break-even is about 37% coverage for OLMoE and 53% for Qwen1.5-MoE, both near a 65% hit rate. I'd have auto skip the cache below about half the host expert size (or below a projected ~65% hit rate) rather than at 2x the one-token minimum. Multi-GPU: with two R9700s, |
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
LeoBorcherding
left a comment
There was a problem hiding this comment.
Codex: clean on 827478f5a here and on the fork mirror, and clean on f06be43af (head plus the override guard) on the mirror. Four findings came up over the mirror rounds:
common/fit.cpp:240(minimum footprint measured without the fitted placement) andtools/ui/src/lib/utils/compaction.ts:56: not from this PR. Both came in verbatim with the ggml-org merge (ggml-org#21231, ggml-org#27401).common/fit.cpp:807, auto never sizes a cache when the placement already fits (--cpu-moewith the dense weights in VRAM): real, and still on the head. On an R9700 with an IQ1_M Qwen3.8-Flash-Next coder GGUF,-cmoe --fit on --moe-cache-mib autoprojects 4626 of 32460 MiB, returnsno changes needed, and runs with no cache and no message, so about 27.8 GB of VRAM sits unused. With an explicit--moe-cache-mib 16000, the same command gets a 15995 MiB cache and decodes at 24.8 tok/s instead of 15.8.common/fit.cpp:408, scope the guard to expert overrides: not narrowed. With a non-expert-ot(-ot token_embd=CPU), auto either returns early with no cache or aborts the fit atfit.cpp:607, so narrowing the guard brings the silent case back.
f06be43af makes the fit.cpp:807 case say what happened (--moe-cache-mib auto places the tensors itself, drop --cpu-moe/-ot or give a size). It still doesn't size the cache. The useful behaviour for -cmoe + auto is to size the cache from the VRAM the early return leaves free, and that's the bigger win for the people who actually use -cmoe. I'd take f06be43af here as the floor and do the sizing either in this PR or right after it.
Ran: the three cells above on head vs f06be43af (R9700, Windows 11, ROCm 7.1). Explicit sizes are unchanged. Not run: multi-GPU (auto is single-GPU only by its own check), CUDA, Linux ROCm.
…timings (source line moved off the axes)
|
The fix for the @danielhanchen #379 pins this PR at |
…ache-mib auto minimum The minimum cache size weighted each group by the bytes of every layer that reads it, so a looped model (Inkling 2, core layers read 3 times) was sized for a visit-weighted split. The MoE cache splits its budget by the expert weights, each counted once, so a core-heavy group could get fewer than n_expert_used slots at that minimum. Layers that read the same weights (or whole views of them) now count once here too, unless LLAMA_MOE_CACHE_DEDUPE=0.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 038adc6eab
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // --moe-cache-mib auto keeps the layer split instead of a MoE cache when the split would hold at least this share of the | ||
| // expert weights in device memory (calibrated in the measurements on the pull request) |
There was a problem hiding this comment.
Keep explanatory comment sentences on a single line
The new comment splits a sentence across two lines, contrary to the explicit rule in AGENTS.md against splitting sentences or hard-wrapping comments. Keep this sentence on one line and apply the same correction to the other newly wrapped explanatory comments in this change.
AGENTS.md reference: AGENTS.md:L84-L84
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 79629e9: each of those comments is now one sentence per line (the auto sizing threshold, the shared-context note, the cache vs layer split note, the params reload note and the dedupe note). Comment-only change.
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |


Adds
--moe-cache-mib autoon top of the MoE expert cache that is now upstream: ggml-org#29887 (GPU LRU cache for MoE experts kept in host memory, merged 10-07) and ggml-org#30112 (the same cache over multiple GPUs, merged 10-08). Withauto, the fitter sizes the cache itself instead of the user guessing a MiB number.What it does
--moe-cache-mib auto(or-1) only acts when--fitruns on a single GPU, the model has routed experts, and the user did not set-ngl,-tsor-ot:global_surplus_cpu_moe). Withauto, every routed expert goes to host memory and that whole surplus becomes the cache. In other words: free VRAM minus the fit margin minus everything that is not a routed expert (attention, shared experts, dense FFN, output, KV, compute buffers, a draft model).n_expert_used + 1slots for every layout group), fit takes its normal path with no cache.The decision is logged in one line:
Other details:
autopicks.--load-mode none. The cache copies misses from host memory, and pinned memory is 2.5x to 8x faster to copy from than pageable mmap. With mmap, the cache was slower than no cache in our runs.Measurements
All runs used
--load-mode none -ub 2048 -c 32768, single GPU, smallest Unsloth dynamic quants. Values are decode tok/s / prompt tok/s at 2048 tokens. Budgets below the card size were emulated with--fit-target. Eachautoarm ran twice, and the two runs agree within 0.5%.--fit on(no cache)--fit on --moe-cache-mib autoWith MTP (
--spec-type draft-mtp), the shared MTP context is measured too. GLM-5.3-Flash at a 40 GiB cap keeps the same draft acceptance and output text with the cache on.Why all experts on the CPU instead of keeping some layers resident. At a fixed expert budget we swept the cache share from 0 to 100%, with the rest as whole resident expert layers. That covered 4 models and 8 GPU/host setups (T4, L4, A100 40/80GB, RTX PRO 6000; 4 to 48 vCPUs). Whenever experts spill, a cache covering the whole budget was the best or within about 5% of the best, so there is no knob for the split. Its decode gain over resident layers only:
When
autokeeps the layer split insteadautonow first estimates the sharerof the expert weights that the normal layer split would keep in device memory, from the same budget the cache would get:r = (target - non-expert memory) / expert memory. Ifr >= 0.80, the split is kept and no cache is used:Why 0.80. A sweep on Kaggle T4 (4 vCPU, default 2 threads; nightly b11541, all routed experts in host RAM in every
autorun,--load-mode none -ub 2048 -c 8192, 2082-token prompt then 512 decoded tokens, 3 interleaved runs per arm, same-host A/A within 3% decode and 4% prompt):autovs splitautovs splitautodecode and prompt speed are flat inr, while the split keeps improving, so the prompt cost grows withr.r0.95. With an 8k prompt and 128 tokens out, they break even near 0.74 (Qwen3.6) and 0.83 (gemma-4).rof about 0.82-0.86 (40 of 49 layers' experts resident, a 20214 MiB cache) decoded about 20% slower withauto.restimate matches the fitter: on gemma-4 with a 10240 MiB cap it reports 85%, and the split it then picks keeps 85% of the expert layers.0.80 keeps the cache wherever it wins decode by 1.5x or more on small hosts, and keeps the split in the range where long prompts and stronger CPUs already favour it.
Tests
llama-fit-params --moe-cache-mib autoon all four models with a capped free-memory report. They are sane, the fallbacks trigger when the dense part does not fit, and the measured device total lands within 1 MiB of the target.llama-serverlaunches stayed inside their budget and answered requests, with cache hit rates around 69%.auto,llama-completion --temp 0 -n 64gives the same tokens as the whole model on the GPU (Qwen3.6, gemma-4).autokeeps the same text and the same draft acceptance as withoutauto, 0.80 / 0.71 (cache 10129 MiB);Branch layout
The head is upstream master at
c811cb8f0(which carries ggml-org#29887 and ggml-org#30112) plus this change, joined by a merge commit so the branch history is not rewritten. The base branchbase/ggml-org-30112is exactlyc811cb8f0, so the diff shows only this change.Kept small on purpose (+165/-17, 9 files):
common/fit.cpp:src/llama-moe-cache.cpp:llama_model_moe_cache_min_size, the floor the fitter needs. It is the only addition to upstream's cache files.--moe-cache-mibacceptsauto, andllama-fit-paramsprints the size it picked.autoacts on a single GPU only. Multiple GPUs log a warning and fall back to the normal fit; sizing over the per-device split ggml-org#30112 introduced is a follow-up.Supersedes #250.