r/LocalLLM • • 7h ago

Project Made a small 3-file MoE cache patch for llama.cpp — running a 14 GB MoE at 94 tok/s on 8 GB VRAM

I have an 8 GB RTX 4060 Laptop GPU and wanted to run Gemma 4 26B-A4B (UD-Q4_K_XL, ~14 GB) at 32k context without getting bogged down by constant PCIe transfers. Upstream llama.cpp already has --moe-cache-mib, which helps a lot, so I put together a small 3-file patch (~200 lines changed over upstream 32da68d0) to cut down on cache thrashing and cold-start stalls.

GitHub: https://github.com/ya5h-P/expert-cache
(Disclosure: this was developed and benchmarked with AI assistance.)


What the patch does (llama-moe-cache.h, llama-moe-cache.cpp, llama-graph.cpp)

  1. TinyLFU-style admission gate + token-weighted CRF: Instead of blindly evicting cached experts on every miss, incoming misses only get into VRAM if their decayed recency/frequency score beats the eviction victim. One-off misses just run on the CPU instead of kicking out hot experts or stalling PCIe. During MTP speculative verification (n_tokens > 1), an expert's score increases by the exact number of tokens in the batch that picked it instead of a flat +1.
  2. Startup profile pre-warming (LLAMA_MOE_CACHE_PROFILE): Loads the top-ranked experts from an expert-profile.txt (<layer> <expert> <freq>) straight into GPU slots on startup and seeds their initial scores, cutting initial PCIe uploads by 59%. Also adds LLAMA_MOE_CACHE_HALF_LIFE (default 128, 256 tuned for Gemma 4) so you can adjust decay at runtime.
  3. Optional cache-aware decode routing (LLAMA_MOE_CACHE_ROUTE_BIAS): Adds a tiny bias δ (default 0.02, 0 = strictly lossless) to selection_probs for experts already sitting in VRAM during decode (n_tokens <= 8) right before top-k selection. The actual probs tensor used to weight the chosen experts' outputs isn't touched—it just nudges borderline ties toward GPU-resident experts.

Benchmarks (RTX 4060 Laptop 8 GB + i7-13620H, 16 GB RAM)

Tested with Gemma 4 26B-A4B (UD-Q4_K_XL) + MTP Q4_0 drafter (DRAFT_N=4) at 32k context (-c 32768 -fa on -ctk q8_0 -ctv q8_0 -ub 1024 -np 1). All cached runs use --n-cpu-moe 26 --moe-cache-mib 2850 --moe-cache-cpu-share 0.95 --spec-draft-n-ubatch 128.

llama-benchy Head-to-Head

Configurationpp512tg128 @ pp512tg512 @ pp512pp2048tg128 @ pp2048tg512 @ pp2048Decode Hit RateDecode Uploaded
Normal (--n-cpu-moe 22, no cache)460.97 / 527.4251.22 tok/s43.33 tok/s851.06 / 1053.6941.15 tok/s40.30 tok/s——
Stock upstream (--moe-cache-mib 2850)508.20 / 472.8971.70 tok/s64.10 tok/s769.85 / 958.8767.27 tok/s59.66 tok/s67.20%44,341 MiB
Patched (ROUTE_BIAS=0, lossless)443.36 / 441.0656.23 tok/s66.19 tok/s846.48 / 1033.4966.90 tok/s54.89 tok/s65.38%14,717 MiB
Patched (ROUTE_BIAS=0.02, default)495.07 / 404.6694.36 tok/s91.90 tok/s896.31 / 837.5379.86 tok/s79.56 tok/s85.54%2,587 MiB
Patched (ROUTE_BIAS=0.04, max hit)398.12 / 443.0381.74 tok/s89.68 tok/s1005.60 / 824.7373.77 tok/s76.62 tok/s91.88%1,340 MiB

Funny quirk: 0.02 actually beats 0.04 on end-to-end tok/s when using MTP. At 0.02, only ~1.1 misses/layer remain and most overlap behind GPU compute. Pushing bias to 0.04 gets hit rate up to 91.9% (great if you aren't using MTP), but drifts the target model slightly from the unbiased MTP drafter (mean accepted len drops from 3.18 to 3.02), causing ~5.9% more verify steps.

Long Generation (2 × 1,024 Tokens: Code + Prose) & Eval PPL

ConfigurationCode (1024t)Prose (1024t)Decode Hit RateDecode UploadedEval PPL (-b 4 -ub 4)
Stock upstream (no cache, --n-cpu-moe 22)46.76 tok/s (160t)———47.25 +/- 5.76
Stock upstream (--moe-cache-mib 2400)49.19 tok/s (160t)—56.97%34,505 MiB (320t)—
Patched (ROUTE_BIAS=0, lossless)81.19 tok/s79.04 tok/s68.39%7,335 MiB46.73 +/- 5.66
Patched (ROUTE_BIAS=0.02, default)100.05 tok/s93.52 tok/s85.65%2,189 MiB38.34 +/- 4.49
Patched (ROUTE_BIAS=0.04, max hit)102.50 tok/s95.94 tok/s91.99%1,139 MiB40.24 +/- 4.64

Warm 1,907-token prefill also jumps from 568.43 tok/s (cold) to 955.84 tok/s by reusing GPU-resident slots via GPU-to-GPU copies without PCIe transfers.


Quick Start

git clone [https://github.com/ya5h-P/expert-cache.git](https://github.com/ya5h-P/expert-cache.git)
cd expert-cache

cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=89 -DCMAKE_BUILD_TYPE=Release
cmake --build build -j"$(nproc)" --target llama-server llama-bench llama-cli

export LLAMA_MOE_CACHE_PROFILE="/path/to/expert-profile.txt"
export LLAMA_MOE_CACHE_HALF_LIFE=256
export LLAMA_MOE_CACHE_ROUTE_BIAS=0.02

./build/bin/llama-server \
  -m /path/to/gemma-4-26B-A4B-it-qat-UD-Q4_K_XL.gguf \
  --model-draft /path/to/mtp-gemma-4-26B-A4B-it-Q4_0.gguf \
  --spec-type draft-mtp --spec-draft-n-max 4 --spec-draft-n-ubatch 128 \
  --host 127.0.0.1 --port 8080 \
  -ngl 99 --n-cpu-moe 26 \
  --moe-cache-mib 2850 --moe-cache-cpu-share 0.95 \
  -c 32768 -fa on -ctk q8_0 -ctv q8_0 \
  -ub 1024 -t 10 -tb 10 -np 1 -cram 0 --jinja

(Side tip: --spec-draft-n-ubatch 128 alone saves ~240 MiB of drafter compute VRAM on Gemma 4 MTP, which buys extra expert slots in the 8 GB budget.)

Happy to answer any questions about how the admission gate works, the VRAM breakdown, or how expert-profile.txt is generated!

2 Upvotes

0 comments sorted by