Run a mixture-of-experts model larger than your VRAM by keeping its experts in system RAM
At the end of this you will have a mixture-of-experts (MoE) model running in llama.cpp on a machine whose GPU is too small to hold it, with the parts used on every token on the card and the bulk of the weights in system RAM. It is for anyone with one consumer card, or one older data-centre card, and a lot of RAM.
Requirements: One NVIDIA GPU of any size · plenty of system RAM · Linux · llama.cpp built with CUDA, a current release with --cpu-moe, --n-cpu-moe and --load-mode · a GGUF file of an MoE model
Requirements
- One NVIDIA card. More VRAM means more expert layers on the card.
- System RAM larger than the part of the model that will not fit on the card, plus room for the operating system.
- llama.cpp built with CUDA for your card (see our build tutorial).
- A GGUF file of an MoE model.
- Patience at start-up: a large file takes minutes to load.
Steps
1. Understand why MoE models tolerate this
In an MoE model each layer holds many expert blocks, and a router picks only a few of them for each token. The attention weights and any shared expert are used for every token; each routed expert is used only some of the time. Keep the always-used parts on the GPU and the experts in RAM, and the GPU does the work that happens on every token while the CPU reads only the few experts each token chooses. Speed then depends mostly on your memory bandwidth.
2. Put every expert in RAM first
Start with the simplest form. -ngl all puts every layer on the GPU, and --cpu-moe then keeps all MoE expert weights on the CPU.
llama-server -m <models-dir>/model.gguf -ngl all --cpu-moe -c 32768
If this starts and leaves VRAM free, use that room in step 3.
3. Move experts back onto the GPU, layer by layer
--n-cpu-moe N keeps the expert weights of only the first N layers on the CPU, and the rest stay on the GPU with their layers. Start N at the model's layer count (the same as --cpu-moe) and lower it a few layers at a time until VRAM is nearly full.
llama-server -m <models-dir>/model.gguf -ngl all --n-cpu-moe 30 -c 32768
There is no formula for N. Restart after each change and stop one step before the load fails or the context no longer fits. Leave room for the KV cache, which grows with -c. The flag came in with pull request 15077, which says it adds tensor overrides for you, and that overrides you pass with -ot take priority.
4. Use a tensor override for finer control
-ot (--override-tensor) takes a pattern matched against tensor names and a buffer type. You write it by hand for every layer's experts:
llama-server -m <models-dir>/model.gguf -ngl all \
-ot "\.ffn_(up|down|gate)_exps=CPU" -c 32768
Use this for a split the convenience flags cannot express, such as keeping only the down-projection experts in RAM. Check your model's tensor names first, because they vary between architectures.
5. Optionally let the GPU cache experts
--moe-cache-mib N sets a GPU cache in MiB for the experts kept on the CPU. The default is 0, which turns it off.
llama-server -m <models-dir>/model.gguf -ngl all --cpu-moe --moe-cache-mib 8192 -c 32768
Size it from whatever VRAM is left after step 3.
6. Choose a load mode
With experts in RAM, how the file is loaded matters more. --load-mode mlock forces the system to keep the model in RAM rather than swapping or compressing it. NetworkCoder's video on llama.cpp 1.0 reported prompt reading nearly doubling with a pinned load on a machine that kept experts in RAM beside a 12 GB card, as noted in our tuning test. Test that claim on your machine.
llama-server -m <models-dir>/model.gguf -ngl all --n-cpu-moe 30 --load-mode mlock -c 32768
Check it worked
At start-up the server log states how many layers went to the GPU and how large the model buffer is on each device. Look for a line containing offloaded with a count of layers, and lines ending in model buffer size = ... MiB, one for each CUDA device and one for the CPU. With experts in RAM, the CPU buffer should hold most of the file and each CUDA buffer much less. To see each tensor's placement, start with -v; the loader then names each overridden tensor and the buffer it went to.
Then check nvidia-smi for VRAM use and send one long request. The server's timing lines report prompt reading (prompt eval time) and generation (eval time) in tokens per second. Compare both numbers each time you lower N.
Common problems
- Out of memory on the GPU at start: raise N in
--n-cpu-moe, or shorten-c. - The model loads but is slow and the system swaps: the experts do not fit in RAM. Close other programs, use a smaller quant, or add RAM.
- An
-otpattern does nothing: the tensor names in your model differ. Run with-vand read the names. - Loading takes far longer than reading the file once: with the default memory-mapped load and too little free RAM, parts of the file are read from disk again. Free RAM or try
--load-mode noneormlock.
Our recommendation
We'd start with --cpu-moe to prove the model runs, then lower --n-cpu-moe until the card is nearly full, because every expert layer moved to the GPU is one fewer the CPU must read. We'd use -ot only for a split the convenience flags cannot express. We'd test --load-mode mlock and the expert cache on our own prompts before believing any speed-up, and judge success by the server's own timing lines, not by a single number from someone else's hardware.