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.

mixed

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 settled

auto_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

MachineSpeedHowSeen by
2 runs on 16 GB
RTX 5070 Ti
and 2 more builds in the same post
no speed givenllama.cppauto_ai_techlabJapanese · Nov 24
Tesla P100no speed givenJunk GPU Lab · Nov 16
1 run on 32 GB: 67.8 tok/s
RTX 509067.8 tok/sАгенты Романа (Roman's Agents)Russian · Nov 18

Evidence

  1. Nov 24
    llama.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 testauto_ai_techlabJapanese
  2. Nov 24
    With 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 testauto_ai_techlabJapanese
  3. Nov 24
    With default mmap loading, a 2048 MiB expert cache passed the no-cache run: 78.5 vs 59.4 tok/s (1.32x).
    Hands-on testauto_ai_techlabJapanese
  4. Nov 24
    Adding --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 testauto_ai_techlabJapanese
  5. Nov 24
    With 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 testauto_ai_techlabJapanese
Show all 14
  1. 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 testauto_ai_techlabJapanese
  2. Nov 22
    At 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.
  3. Nov 22
    The 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.
  4. Nov 22
    Unmodified 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.
  5. Nov 22
    The 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.
  6. Nov 22
    The 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.
  7. Nov 22
    A 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.
  8. Nov 18
    Pull 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 cache
  9. Nov 16
    With 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 testJunk GPU Lab

Everything said about llama.cpp MoE expert cache, on the wire

Alternatives

llama.cpp MoE expert cache slot sharing patchAt 7296 MiB cache, slot sharing raised cache hit rate from 81.54% to 94.77% and generation from 27.52 to 39.35 tok/s (1.43x) on Inkling 2 Mini.
Works with
Made for llama.cpp MoE expert cache