llama.cpp MoE expert cache
by ggml · Technique · Text & code
A llama.cpp option that keeps the most used experts of mixture-of-experts models in graphics memory so big models run faster on small gpus.
Can nearly triple MoE speed on small GPUs, but only with tuned settings
Latest · Nov 24Pull request 29887, merged Oct 7, added a GPU expert cache to llama.cpp; its author saw 38.8 to 67.8 tok/s on an RTX 5090. Off by default
Who says what
Opinion has settledauto_ai_techlab, gucchinworks and the llama.cpp PR authors, who measured it on their own GPUs
Tuned, it is a large win: 38.8 to 67.8 tok/s on an RTX 5090 in the PR, 2.86x over no cache at 2048 MiB with --load-mode none, and 1.36x over layer-wise offload with -ub 32. At defaults it can lose: small mmap-loaded caches ran 20-40 tok/s against 59.4 uncached, a 12 GB card had no room at ubatch 512, and looped layers' weights were counted three times.
From Junk GPU Lab on Nov 16 to auto_ai_techlab on Nov 24, every tester found the same thing: it pays off only once loading mode, batch size and cache size are set by hand.
What would settle it. Merging a fix for the triple-counting bug, still open as issue #32744, and defaults that don't need --load-mode none and -ub 32.
Seen running on
| Machine | Speed | How | Seen by |
|---|---|---|---|
| 2 runs on 16 GB | |||
| llama.cpp | |||
| 1 run on 32 GB | |||
| 67.8 tok/s | |||
Evidence
- Nov 24llama.cpp 1.0 added an MoE expert cache: experts stay on the CPU and only frequently used ones sit in a GPU cache.Hands-on test
auto_ai_techlab - Nov 24With default mmap loading, 256–1024 MiB expert caches were slower than no cache (--cpu-moe, 59.4 tok/s): 20.2, 26.2, 39.7 tok/s.Hands-on test
auto_ai_techlab - Nov 24With default mmap loading, a 2048 MiB expert cache passed the no-cache run: 78.5 vs 59.4 tok/s (1.32x).Hands-on test
auto_ai_techlab - Nov 24Adding --load-mode none, as a server log warning advised, made small caches beat no cache: 1.15x at 256 MiB, 1.87x at 1024 MiB, 2.86x at 2048 MiB.Hands-on test
auto_ai_techlab - Nov 24With the expert math done on the GPU (cache 512/2048 MiB, 1024 MiB plus none), 20/20 answers matched all-GPU text exactly; CPU compute matched 0/20.Hands-on test
auto_ai_techlab
Show all 14
- Nov 24--moe-cache-cpu-share at 1024 MiB cache gave 47.1 tok/s at 0.5 and 64.0 at 1, using default mmap loading.Hands-on test
auto_ai_techlab - Nov 22At the default ubatch of 512 the compute buffer takes about 9.9GB with experts on the host, leaving a 12GB GPU no room for the cache; the tuner script stopped.
- Nov 22The cache only works for batches of 32 tokens or fewer; with -ub 32 the compute buffer shrinks to about 0.7GB and about 7GB can go to the cache.
- Nov 22Unmodified cache (7296 MiB, pinned host memory, -ub 32) gave 28.31 tok/s with CPU share 0.95, 1.36x layer-wise offload in the same run; mmap with no share gave 20.13.
- Nov 22The cache counted the looped layers' weights three times (28188 MiB vs 15066 MiB in the file) and kept separate slot tables per pass, so reused experts took extra slots.
- Nov 22The cache tuner script does not stop the server on Windows because it sends SIGINT, and its predicted 38.0 tok/s measured 26.60 because its test output was repetitive.
- Nov 22A separate llama.cpp issue (#32744, Linux, 24GB GPU) found the same triple-counting cause and proposed a similar fix in a fork; the issue was still open.
- Nov 18Pull request 30966, merged Nov 7, lets llama.cpp run cache-missed experts on the CPU: 24 to 47.6 tok/s with a ~6 GB cache, but 65.5 without versus 54.1 with it at ~19 GB cacheReportedllama.cpp PRvia
Агенты Романа (Roman's Agents) - Nov 16With the new MoE cache on DeepSeek V4 Flash, 16 GB cache gave 10 tok/s and 22 GB gave 11; replayed routing shows 48 GB would hit 99%.Hands-on test
Junk GPU Lab
Everything said about llama.cpp MoE expert cache, on the wire