Skip to content
,

llama.cpp MoE Offload Guide: n-cpu-moe, cpu-moe and Override Tensor

7–10 minutes
Woman in a brick-walled loft holding a tablet with a MOE OFFLOAD card, hook big models small GPU

Mixture-of-experts (MoE) models are the reason a 16 GB or 24 GB GPU can now run models with 100 billion parameters or more. The trick is to keep the small, always-used parts of the model on the GPU and push the huge expert weights into system RAM. llama.cpp has dedicated flags for exactly this.

This guide explains how MoE offloading works and how to use --cpu-moe, --n-cpu-moe, -ot (override tensor) and the newer --moe-cache-mib option. It also covers what the automatic --fit setting already does for you, and how to tune for the best tokens per second. Every flag here is checked against llama.cpp’s current source.

TL;DR

  • Easiest: just run the model. --fit is on by default and moves MoE expert tensors to system RAM when the model doesn’t fit.
  • All experts on CPU: -cmoe / --cpu-moe. Everything else stays on the GPU.
  • Fine control: -ncmoe N / --n-cpu-moe N keeps the experts of the first N layers on the CPU. Lower N until VRAM is nearly full for the best speed.
  • Full control: -ot "regex=CPU" places any tensors you match.
  • New: --moe-cache-mib N gives CPU-kept experts a GPU cache (default 0, off).

New to llama.cpp? Start with how to install llama.cpp and my llama-server guide.

Why MoE Models Offload So Well

A dense model uses every weight for every token. An MoE model splits each layer’s feed-forward block into many “experts”, and a router picks only a few of them per token. A model may have 100B+ total parameters but only a small fraction active for each token.

That gives a natural split:

  • Attention, embeddings, norms and shared weights: used for every token, fairly small. Keep these on the GPU.
  • Expert weights: the bulk of the file, but each token touches only a few. Put these in system RAM, where the CPU computes the handful that are active.

Because the CPU only reads the active experts for each token, generation stays usable even though most of the model sits in RAM. Compare that with offloading whole layers of a dense model, where the CPU must process every weight of those layers for every token.

In llama.cpp the expert tensors are named like blk.12.ffn_up_exps, ffn_down_exps and ffn_gate_exps. The built-in pattern used by the MoE flags is \.ffn_(up|down|gate|gate_up)_(ch|)exps.

What --fit Already Does

Recent llama.cpp builds have -fit / --fit, and it defaults to on. It adjusts any settings you didn’t set yourself so the model fits in your device memory.

For MoE models, the fitting code in common/fit.cpp handles experts specially. When the full model won’t fit, it moves MoE expert tensors to system memory first and keeps the rest on the GPU.

It also leaves a safety margin per GPU (--fit-target, default 1024 MiB), and won’t shrink the context below --fit-ctx (default 4096).

So on a current build, this often just works:

llama-server -m model-Q4_K_M.gguf -c 32768

Use the manual flags below when you want to squeeze out more speed, reserve VRAM for something else, or run an older build.

Note that --fit only adjusts settings you left unset, so passing -ngl, -ot or the MoE flags takes over from it for those settings. Add --fit off if you want full manual control.

--cpu-moe: All Experts on the CPU

llama-server -m model.gguf -ngl 99 --cpu-moe -c 32768

-cmoe / --cpu-moe keeps all MoE expert weights in system RAM. With -ngl 99 (or -ngl all) everything else goes on the GPU.

This uses the least VRAM, which leaves room for a long context. It’s also the slowest MoE option, because every expert runs on the CPU. Use it as your starting point, then move experts back to the GPU with --n-cpu-moe.

--n-cpu-moe: The Sweet Spot

llama-server -m model.gguf -ngl 99 --n-cpu-moe 30 -c 32768

-ncmoe N / --n-cpu-moe N keeps the experts of the first N layers on the CPU and the rest on the GPU. If a model has 48 layers, --n-cpu-moe 48 equals --cpu-moe, and --n-cpu-moe 0 puts all experts on the GPU.

Every layer of experts you move to the GPU makes generation faster, until VRAM runs out. The best setting is the lowest N that still loads with your chosen context size and leaves a little headroom.

There’s also -ncffn N / --n-cpu-ffn N, which does the same for the dense feed-forward weights of normal (non-MoE) models.

-ot / --override-tensor: Full Control

-ot takes <tensor name pattern>=<buffer type> pairs, separated by commas. The pattern is a regular expression matched against tensor names. Buffer types are names like CPU, CUDA0 or CUDA1.

All experts to the CPU (same as --cpu-moe):

-ot "exps=CPU"

Experts of layers 20-39 only:

-ot "blk\.(2[0-9]|3[0-9])\.ffn_.*_exps=CPU"

Only the up and down experts, keeping gate experts on the GPU:

-ot "ffn_(up|down)_exps=CPU"

On two GPUs, you can pin tensors to a specific card, for example "blk\.([0-9]|1[0-9])\.ffn_.*_exps=CUDA0". Most people never need -ot now that --n-cpu-moe exists, but it’s still the tool for unusual layouts. On Windows PowerShell, keep the pattern in quotes.

--moe-cache-mib: GPU Cache for CPU Experts

A newer pair of options adds a middle ground:

  • --moe-cache-mib N: a GPU cache, in MiB, for the MoE experts kept on the CPU. Default 0 (disabled). With several GPUs it’s split among them like the layers.
  • --moe-cache-cpu-share F: once the cache is full, the share of cache misses (0 to 1) that the CPU computes from its own copy instead of uploading the expert to the GPU. Default 0.00.

The idea is that some experts are picked far more often than others. Caching the busy ones on the GPU can speed things up without moving every expert there. Try something like --n-cpu-moe 40 --moe-cache-mib 4096 and compare speeds. Since this is new, results vary a lot between models and hardware.

A Tuning Recipe

  1. Pick your context size first (for example -c 32768). The KV cache needs VRAM too.
  2. Start safe: -ngl 99 --cpu-moe. Check it loads and note the speed.
  3. Switch to --n-cpu-moe with N equal to the layer count (shown in the load log), then lower N in steps of 4-8.
  4. Watch VRAM in Task Manager or nvidia-smi. Stop when about 0.5-1 GB is left.
  5. Benchmark with llama-bench. It accepts a comma-separated list for -ncmoe, so one run can compare several values: llama-bench -m model.gguf -ngl 99 -ncmoe 24,28,32,36.
  6. Optional: quantize the KV cache (-ctk q8_0 -ctv q8_0) to free VRAM for more expert layers.

Prompt processing (reading long inputs) and generation speed react differently. More experts on the GPU mostly helps generation. A larger batch size (-ub) can help prompt processing when experts are on the CPU.

RAM, CPU and Hardware Tips

  • Enough RAM: the expert weights must fit in system RAM with room to spare. A 60 GB GGUF needs well over 64 GB of total RAM plus VRAM in practice.
  • RAM speed matters most: expert computation on the CPU is limited by memory bandwidth. Dual-channel DDR5 is much better than DDR4, and more channels help further.
  • Threads: set -t to your physical core count (not threads) as a starting point.
  • Quant choice: a smaller quant shrinks the experts the CPU must read. See GGUF quantization explained.
  • Don’t let it swap: if the OS starts paging to disk, speed collapses. Close other apps or pick a smaller quant.

For other ways to run models bigger than your GPU, see running models larger than VRAM.

Ollama and LM Studio

Ollama and LM Studio use llama.cpp’s ggml code underneath but decide the GPU split for you, and they don’t expose every llama.cpp flag. If you want exact control with --n-cpu-moe or -ot, run llama-server directly and point your apps at its OpenAI-compatible API.

My Ollama environment variables guide lists what you can tune in Ollama.

Troubleshooting

Out of memory while loading

Raise --n-cpu-moe, lower the context, or quantize the KV cache. Remember the context needs VRAM too.

Very slow generation

Too many experts on the CPU, slow RAM, or swapping. Lower N if VRAM allows, check RAM use, and set -t to your physical cores.

“-ot” seems to do nothing

The pattern doesn’t match any tensor names. Check the names in the load log and make sure the quotes survived your shell.

The flags are not recognized

Your build is too old. Update to a recent release; see install llama.cpp.

It ignores my GPU completely

You’re running a CPU-only build, or -ngl 0 is set. The startup log warns when no usable GPU is found.

FAQ

What does --n-cpu-moe do in llama.cpp?

It keeps the MoE expert weights of the first N layers in system RAM and runs them on the CPU, while everything else stays on the GPU.

What’s the difference between --cpu-moe and --n-cpu-moe?

--cpu-moe puts all expert weights on the CPU. --n-cpu-moe N does it only for the first N layers, so you can fill your VRAM with the rest.

Do I still need these flags with --fit?

Often not. --fit is on by default and moves experts to RAM when needed. Manual flags help you tune for more speed.

Does MoE offloading work for dense models?

No. Dense models have no expert tensors. Use -ngl or --n-cpu-ffn for those.

How much RAM do I need for a big MoE model?

Enough to hold the GGUF file minus what fits in VRAM, plus headroom for the OS. Check the file size of your chosen quant.

Is -ot the same as --override-tensor?

Yes. -ot is the short form.

Can I use these flags with llama-cli and llama-bench?

llama-cli and llama-server share all of them. llama-bench supports -ncmoe and -ot (it separates -ot pairs with semicolons) but not --cpu-moe.

Sources

Free weekly email

Get new AI guides every week

One short weekly email with our newest local AI, ComfyUI and AI tool guides. No spam, no pop-ups, just the good stuff.

Free. Unsubscribe anytime in one click. We only use your email to send this newsletter. See our privacy policy.

Woman in a brick-walled loft holding a tablet with a MOE OFFLOAD card, hook big models small GPU