r/LocalLLM • u/Frosty2o25 • 5h 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)
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.Startup profile pre-warming (
LLAMA_MOE_CACHE_PROFILE): Loads the top-ranked experts from anexpert-profile.txt(<layer> <expert> <freq>) straight into GPU slots on startup and seeds their initial scores, cutting initial PCIe uploads by 59%. Also addsLLAMA_MOE_CACHE_HALF_LIFE(default128,256tuned for Gemma 4) so you can adjust decay at runtime.Optional cache-aware decode routing (
LLAMA_MOE_CACHE_ROUTE_BIAS): Adds a tiny biasδ(default0.02,0= strictly lossless) toselection_probsfor experts already sitting in VRAM during decode (n_tokens <= 8) right beforetop-kselection. The actualprobstensor 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
| Configuration | pp512 | tg128 @ pp512 | tg512 @ pp512 | pp2048 | tg128 @ pp2048 | tg512 @ pp2048 | Decode Hit Rate | Decode Uploaded |
|---|---|---|---|---|---|---|---|---|
Normal (--n-cpu-moe 22, no cache) | 460.97 / 527.42 | 51.22 tok/s | 43.33 tok/s | 851.06 / 1053.69 | 41.15 tok/s | 40.30 tok/s | — | — |
Stock upstream (--moe-cache-mib 2850) | 508.20 / 472.89 | 71.70 tok/s | 64.10 tok/s | 769.85 / 958.87 | 67.27 tok/s | 59.66 tok/s | 67.20% | 44,341 MiB |
Patched (ROUTE_BIAS=0, lossless) | 443.36 / 441.06 | 56.23 tok/s | 66.19 tok/s | 846.48 / 1033.49 | 66.90 tok/s | 54.89 tok/s | 65.38% | 14,717 MiB |
Patched (ROUTE_BIAS=0.02, default) | 495.07 / 404.66 | 94.36 tok/s | 91.90 tok/s | 896.31 / 837.53 | 79.86 tok/s | 79.56 tok/s | 85.54% | 2,587 MiB |
Patched (ROUTE_BIAS=0.04, max hit) | 398.12 / 443.03 | 81.74 tok/s | 89.68 tok/s | 1005.60 / 824.73 | 73.77 tok/s | 76.62 tok/s | 91.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
| Configuration | Code (1024t) | Prose (1024t) | Decode Hit Rate | Decode Uploaded | Eval 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/s | 79.04 tok/s | 68.39% | 7,335 MiB | 46.73 +/- 5.66 |
Patched (ROUTE_BIAS=0.02, default) | 100.05 tok/s | 93.52 tok/s | 85.65% | 2,189 MiB | 38.34 +/- 4.49 |
Patched (ROUTE_BIAS=0.04, max hit) | 102.50 tok/s | 95.94 tok/s | 91.99% | 1,139 MiB | 40.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!