llama.cpp: 80 GB MoE models on 32 GB of VRAM

My homelab box formula has two MSI RTX 5060 Ti 16 GB, an i7-14700 and 64 GB of DDR5. That is 32 GB of VRAM (but as two separate 16 GB pools without peer-to-peer access), and the interesting open models no longer fit into it: they are mixtures of experts (MoE) of 80 GiB and more. A video by kaiexplainsyt listed five llama.cpp settings for exactly this case, and llama.cpp had just reached version 1.0. So I built it from source and measured the five settings one at a time, on two models, against what llama.cpp does without any of them.

In short

  • llama.cpp’s default is already good. Its --fit (on by default) keeps every layer on the GPUs and moves only experts to RAM. That generates 2.3× faster than the classic “as many layers as fit” offload, on both models.
  • The MoE cache is the biggest win for generation. It keeps the most used experts on the GPUs. Its tuner script recommends a size that is about half of what fits on two GPUs; twice its size was 13–14 % faster.
  • Bigger batches (-b 4096 -ub 4096) are the biggest win for prompts: 2.5× on Qwen, 1.8× on DeepSeek.
  • DirectIO (-lm dio) helps when the experts fit in RAM, and then the cache should do all the work on the GPUs (CPU share 0), not what the tuner chose.
  • Qwen3.8-Flash-Next: 341 / 38.8 → 803 / 59.5 tokens/s (prompt / generation). DeepSeek V4 Flash, whose experts do not fit in RAM: 146 / 14.5 → 244 / 20.4 tokens/s.

The setup

GPUs 2× MSI RTX 5060 Ti 16 GB, both on CPU lanes (Gen5 x8), no P2P
CPU / RAM i7-14700, 64 GB DDR5-6000 (62 GiB usable)
llama.cpp v1.0.0 (build 12359), built from the tag with CUDA 13.0 for sm_120, NCCL 2.28.9
Driver NVIDIA 580.178.04 (open modules)
Model 1 Qwen3.8-Flash-Next, IQ3_XXS, 79.1 GiB: 125B parameters, 6B active, 512 experts
Model 2 DeepSeek-V4-Flash-0731, UD-IQ1_M, 80.9 GiB: 284B parameters, 13B active, 256 experts

Both models are about the same size on disk, but they are built very differently, and that turned out to be the whole story. Qwen’s 79 GiB contain 27 GiB of n-gram embeddings that llama.cpp reads lazily from disk, row by row, so only about 52 GiB really need memory: 50 GiB of experts and 2.3 GiB of everything else. DeepSeek has no such part: about 74 GiB of experts and 10 GiB of everything else, more than VRAM and RAM can hold together without the disk.

DeepSeek at 1.75 bits is not about quality. I picked the largest quant that fits at all, and this is a speed comparison only.

How I measured

  • One script per step (run-step.sh): checks that nothing else uses the GPUs, drops the page cache, starts a fresh llama-server, measures the load time, then sends one warm-up and five measured requests (ten for the final step).
  • Every request is the same: a 4260-token prompt (Qwen; 4188 for DeepSeek’s tokenizer), then exactly 256 generated tokens, temperature 0, no prompt caching.
  • Fixed for every run: one slot, 8 threads, 8192 tokens of context, the server’s RAM prompt cache off (it takes up to 8 GiB and would serve a repeated prompt from memory).
  • Recorded: prompt and generation tokens/s, VRAM per GPU, RAM, GPU and CPU power, and from the last runs on, how much is read from the NVMe.
  • Cumulative: each step keeps the previous ones, so each number is what that setting adds on top.

The baselines: llama.cpp already does the obvious thing well

Baseline Qwen prompt / gen DeepSeek prompt / gen
0a: as many whole layers as fit (-fit off -ngl N) 334.6 / 16.8 131.8 / 6.4
0b: llama.cpp’s default (--fit) 340.9 / 38.8 145.7 / 14.5

(tokens/s, mean of 5 requests; the spread was ±0.1–0.4 for generation.)

Without any flags, llama.cpp 1.0 checks free VRAM before loading and decides the split itself. It puts all layers on the GPUs and moves only the experts of layers that do not fit to RAM. The classic approach, -ngl N, puts whole layers (attention included) on the CPU instead. Same VRAM, 2.3× the generation speed on both models. So the bar for the five settings is not “naive”, it is --fit.

Step 1: all experts to RAM (-cmoe)

On its own this is a step backwards: Qwen drops to 24.4 tokens/s, the GPUs hold only 2 GiB each, and prompt processing falls 40 %. With all experts in RAM, every 512-token micro-batch of the prompt copies nearly all of them to the GPUs.

But it frees about 25 GiB of VRAM for the next step.

Step 2: the MoE cache

--moe-cache-mib N keeps copies of the most used experts on the GPUs; --moe-cache-cpu-share F lets the CPU compute part of the cache misses instead of uploading them. llama.cpp ships a tuner, scripts/moe-cache-tune.py: it starts the server six times, fits the machine’s upload and CPU speeds, and recommends both values. For Qwen it measured uploads at 12.8 GiB/s and the CPU at 46.7 GiB/s, and recommended 13440 MiB at share 0.8.

Qwen Cache Generation Hit rate
0b --fit – 38.8 –
2: the tuner’s recommendation 13.1 GiB 48.1 85 %
2b: twice that 25.0 GiB 54.4 94 %

Here is the first real finding. The cache is split across both GPUs, but the tuner sizes it from the free memory of the first GPU only. On two cards it recommends about half of what fits, and with the other half, Qwen was 13 % faster. DeepSeek showed the same: 18.7 tokens/s with the tuner’s size, 21.4 with twice that. I used the doubled cache for the following steps.

Step 3: DirectIO (-lm dio)

By default llama.cpp maps the model file into memory, so the experts in RAM are page cache. DirectIO reads them into pinned memory instead, which the GPUs can copy from at full speed.

Qwen Load Prompt Generation RAM left
2b (mmap) 16.4 s 258.0 54.4 59 GiB
3 (-lm dio) 26.1 s 320.2 57.8 8 GiB

Prompts +24 %, generation +6 %. Loading takes 10 seconds longer, but the first finished answer arrives at the same time: with mmap, the experts are read from the NVMe during the first request instead. The price is RAM: pinned memory is really taken, where mmap’d pages count as available. Qwen fits with 8 GiB to spare. DeepSeek cannot use it at all: it would have to pin 74 GiB in 62 GiB.

Step 4: bigger batches (-b 4096 -ub 4096)

Step 1 showed why prompts got slower: every micro-batch copies the experts it uses. At the default 512 tokens, a 4260-token prompt is nine micro-batches and nine rounds of copies; at 4096, it is two. The catch: the bigger compute buffer needs about 0.9 GiB more on GPU0, which was full. So the cache had to shrink by 2 GiB, and a control run measured what that alone costs.

Qwen Cache Batch Prompt Generation
4c (control) 23 GiB 512 317.6 56.9
4 23 GiB 4096 804.3 58.0

2.5× faster prompts, and the smaller cache cost nothing measurable. Generation does not change: it runs one token at a time and never uses big batches. According to the server’s own statistics, the prompts copied 81 GiB of experts from the cache instead of 280 GiB.

Step 5, and the tuner’s other blind spot

The final configuration with ten requests reproduced step 4 (806.0 / 57.6). Then I tried one thing the documentation hints at: with pinned memory, “the CPU share often does not help”. The tuner had chosen 0.8, but it tuned with mmap.

Qwen, 10 requests Generation, all Requests 4–10 Hit rate CPU
CPU share 0.8 (the tuner’s) 57.6 57.7 94.5 % 56 W
CPU share 0 59.5 60.9 96.5 % 41 W

With share 0, every miss is copied into the cache, so the cache fills better; with 0.8, most misses are computed on the CPU and never enter it. Share 0 starts slower and is 5 % faster from the fourth request on, with 15 W less on the CPU. The tuner’s answer was right for the setup it measured, and steps 2 and 3 should be tuned together.

Qwen, best configuration: -cmoe --moe-cache-mib 23552 --moe-cache-cpu-share 0 -lm dio -b 4096 -ub 4096: prompts 2.4× and generation +53 % over llama.cpp’s default, with the same VRAM and the same load time.

DeepSeek: when the experts do not fit in RAM

The cache holds copies; every expert still lives in RAM too. DeepSeek’s 74 GiB of experts cannot all be there, and pinning (step 3) is out. But with mmap the three can work as tiers: the most used experts in the GPU cache (17–20 GiB), the next ones in the page cache (about 55 GiB), the rarest read from the NVMe.

DeepSeek Prompt Generation
0b --fit 145.7 14.5
1 -cmoe 93.7 10.7
2 tuner (CPU share 0.9) 112.6 18.7
2b twice the cache 126.4 21.4
4 -ub 4096 230.5 19.9
5 (10 requests) 244.4 20.4

It works: prompts +68 % and generation +41 % over the default. Two things differ from Qwen. The CPU share matters a lot here (the tuner measured share 0 at 11.1 and 0.95 at 15.9 tokens/s), because uploading an expert that may first have to come from disk is expensive. And the NVMe never rests: with a new counter in the last runs, both batch sizes read about 10 GiB from the NVMe per request. For DeepSeek on this machine, the limit is not a flag but 64 GB of RAM.

All settings together

Qwen3.8-Flash-Next DeepSeek V4 Flash
llama.cpp’s default (--fit), prompt / generation 340.9 / 38.8 145.7 / 14.5
All settings, prompt / generation 803.1 / 59.5 244.4 / 20.4
Gain prompt 2.4×, generation +53 % prompt +68 %, generation +41 %

(tokens/s; Qwen with step 5’s CPU share 0; DeepSeek without DirectIO, because its experts do not fit in RAM.)

What went wrong on the way

  • The first build failed while downloading llama-server’s web UI. My build script cloned with --depth 1; llama.cpp counts commits for its build number, got 1, asked Hugging Face for UI version b1 and failed on the newer latest it fell back to. A clone with full history (--filter=tree:0) fixed it.
  • Installing CUDA through Ansible sat silent for ten minutes while NVIDIA’s server slowed to 50 KB/s on the last package. I stopped it and installed by hand, line by line; the role now only configures the repository and checks what is installed.
  • Two samplers kept running after a failed run and wrote into the next runs’ files. The cleanup ran under set -e, so its first failing kill (of a server that had already exited) ended it before the samplers were stopped.

What I did not measure

Quality, especially DeepSeek at 1.75 bits. Long contexts and parallel requests (everything here is one request with an 8K context). Other batch sizes than 512 and 4096, other thread counts, tensor split (NCCL 2.28.9 is untested on these cards). And a machine with more RAM, which is what DeepSeek is really asking for.

Conclusion

For MoE models that do not fit into VRAM, llama.cpp 1.0 already does the right thing by default; nobody should still be tuning -ngl by hand for them. On top of that, the MoE cache and bigger batches are worth real speed: on Qwen3.8-Flash-Next, 60 instead of 39 tokens/s and prompts 2.4× faster, on two consumer cards. Two caveats from the measurements: on two GPUs, give the cache about twice what the tuner says, and tune the CPU share after deciding on the load mode, not before. Next: the same two models, prompts and scripts on Strata, a framework built to run big models on small hardware, and on other local inference engines, all compared with these llama.cpp numbers.