Skip to content
IntermediateEdge / Local 3 min readPublished

Run 30B–120B MoE Models on 12–24 GB GPUs with --n-cpu-moe

Keep every attention layer and KV cache on the GPU with -ngl 99 while moving only inactive routed experts to DDR5 RAM via --n-cpu-moe or --cpu-moe.

Written against

llama.cpp b8000+ · CUDA / ROCm / Vulkan · dual-channel DDR5 · GGUF Q4_K_M

Not re-run since it was last edited — treat the commands as a starting point, not a tested recipe.

llama.cppMoE--n-cpu-moe-ot16GB VRAM24GB VRAM
01

Why whole-layer offload (-ngl) wastes MoE speed

In a Mixture-of-Experts checkpoint, routed feed-forward experts hold 85–95% of total weight bytes, yet each generated token activates only a small fraction (for example, 3B active out of 30B total in Qwen3 30B-A3B). Lowering -ngl pushes entire transformer blocks — including dense attention projections and their KV cache — onto the CPU, forcing PCIe transfers and slow CPU attention on every layer. Passing -ngl 99 together with --n-cpu-moe N keeps all attention layers, shared experts, embeddings, and the full KV cache on the GPU, moving only the routed expert tensor (ffn_.*_exps) of N layers to system RAM.

02

Size the split before launching

Qwen3 30B-A3B at Q4_K_M with an 8K context needs about 20.2 GB in full GPU memory — comfortable on 24 GB cards (140 of the 177 models in this index fit 24 GB comfortably at 4K), but about 5.7 GB over a 16 GB card once Windows reserves 1.5 GB. Offloading 18 of its 48 MoE layers via --n-cpu-moe 18 moves roughly 6.4 GB of routed experts to DDR5 RAM while keeping every attention head and the 8K KV cache on an RTX 4060 Ti 16GB. Likewise, GLM-4.5 Air at Q4_K_M with an 8K context needs about 68.7 GB total; on a single 24 GB card with 96 GB DDR5, --n-cpu-moe 36 keeps attention on the GPU and streams only the active experts per token from RAM.

bash
# 1) Partial expert offload: Qwen3 30B-A3B Q4_K_M on a 16 GB GPU + 32 GB RAM
./build/bin/llama-server \
  -m ./models/Qwen3-30B-A3B-Q4_K_M.gguf \
  -ngl 99 --n-cpu-moe 18 -c 8192 --flash-attn on \
  --host 127.0.0.1 --port 8080

# 2) Move ALL routed experts to CPU RAM (--cpu-moe) for 100B+ MoE on 16–24 GB VRAM
./build/bin/llama-server \
  -m ./models/GLM-4.5-Air-Q4_K_M-00001-of-00002.gguf \
  -ngl 99 --cpu-moe -c 16384 --flash-attn on \
  --host 127.0.0.1 --port 8080
03

Equivalent -ot regex override for older builds or custom layer ranges

Under the hood, --cpu-moe is shorthand for -ot ".ffn_.*_exps.=CPU", and --n-cpu-moe N maps the first N expert blocks to CPU. If you want to pin specific layer ranges across heterogeneous GPUs and CPU RAM (for example, layers 0–19 on CUDA0, 20–34 on CUDA1, and 35–47 on CPU), pass explicit -ot rules evaluated from first to last.

bash
# Explicit tensor-override equivalent:
./build/bin/llama-server \
  -m ./models/Qwen3-Coder-30B-A3B-Instruct-Q4_K_M.gguf \
  -ngl 99 -ot "blk\.(3[0-9]|4[0-7])\.ffn_.*_exps\..*=CPU" \
  -c 16384 --flash-attn on --host 127.0.0.1 --port 8080
04

Tuning CPU threads and DDR5 bandwidth

When experts execute on the CPU, token generation is paced by system RAM bandwidth (about 75–90 GB/s on dual-channel DDR5-5600/6000) divided by the active expert bytes per token — not total file size. Set -t to physical performance cores (not hyperthreads) so memory controllers saturate without cache contention, and enable --flash-attn on so GPU-resident attention stays fast at 16K–64K context.

Common questions

Does Ollama support --n-cpu-moe?

Ollama manages layer placement automatically and does not expose llama.cpp’s --n-cpu-moe or -ot flags in Modelfile parameters. Use llama-server directly when an MoE model exceeds your VRAM.

How do I get the exact --n-cpu-moe count for my card?

Open the VRAM Calculator in Model → GPU mode, pick the MoE model and your GPU, and enter your free VRAM if other apps are open. When the model exceeds your card, the Run Plan automatically computes the exact --n-cpu-moe N flag and system RAM required.

What this guide uses

Format
GGUF

Next steps

See everything that fits 🟢 RTX 4060 Ti 16GReverse lookup — 16GB at 8192 context, ranked by quality

Models covered in this guide

Did this actually run?

Copying a command is not the same as it working, so this is the only place the site asks. Nothing is collected beyond the answer itself.

Related guides

Deployment guides are educational. Each model is subject to its own license — read the official Hugging Face model card before downloading or deploying.