Skip to content

Search wahloon.net

Search models, news, guides and FAQs.

All guides
Guides#moe#llama-cpp#vram#offload#guide#local

Running MoE models bigger than your VRAM

Why a 150 GiB mixture-of-experts model can run on a 96 GB GPU, how llama.cpp offloads experts, and how to tune --n-cpu-moe without guessing.

8 min readMarkdown
On this page

A 150 GiB Q4 file does not fit on a 96 GB GPU. It can still run there.

Hy4-flash's abliterated Q4_K_M is 150.1 GiB. Its author reports running it with about 96 GiB on the GPU and the rest in system RAM. That is not a trick or a compressed cache. It is the difference between a dense model and a mixture-of-experts model: the file is large, but each token only reads a small part of it.

This guide explains where the weights live, which llama.cpp flags move them, and how to size a machine without guessing.

Total parameters are not the memory you need at once

Every weight still has to be somewhere — VRAM, system RAM, or disk. MoE does not make the file smaller. It makes the working set per token smaller.

A model with 512 routed experts and 16 active per token reads 16 experts in each layer, not all 512. The rest stay resident but idle. If the idle experts are in system RAM, the GPU does not need to hold them.

ModelTotalActiveQ4 fileFirst realistic fit
Qwen3.6-35B-A3B35B~3B~21 GB24 GB GPU
gpt-oss-120b117B5.1B~63 GB48 GB GPU + 32 GB RAM
Hy4-flash~220B12B150.1 GiB96 GB GPU + 64 GB RAM
ERNIE 5.5 Flash280B14B170.3 GiB (NVFP4)2×96 GB GPUs

The file sizes are quant-dependent; the fit column is a starting point, not a guarantee. Context length and the runtime's buffers change the answer. To check a specific build against your own machine, use the hardware fit page.

What has to be on the GPU

Not every tensor is a routed expert. The parts that run for every token should stay on the GPU:

  • Attention weights and the KV cache.
  • Shared experts, which are active on every token.
  • The router, which decides which experts to use.
  • Embeddings, norms and lm_head.
  • Dense FFN layers, if the model has any.

The routed experts are the bulk, and they are the part you can move to the CPU. In llama.cpp, the expert tensors are the ffn_*_exps tensors in each block. Everything else in the block is a normal tensor and stays where -ngl puts it.

Some models add special tables. Hy4-flash has a 63.5B Engram table, about 59 GiB in the GGUF. It is only row-gathered, so it can live in system RAM without being touched as a matrix. That is why a 150 GiB file can run with 96 GiB of VRAM: the GPU holds the backbone and the experts, and the Engram table stays in RAM.

The flags that matter

llama.cpp has three ways to move expert weights, plus a cache for the ones you leave on the CPU.

FlagWhat it doesUse it when
-ngl 99Offload all layers to the GPUAlways, then override
--n-cpu-moe NKeep the MoE weights of the first N layers in CPUVRAM is short
--cpu-moeKeep all MoE weights in CPUVRAM is very short
-ot PATTERN=CPUOverride specific tensor patternsFine control, special tables
--moe-cache-mib NGPU cache for CPU-resident expertsRecent builds; repeated experts
-c NContext sizeBudget the KV cache
-ctk q8_0 -ctv q8_0Quantize the KV cacheLong context, short VRAM
-fa onFlash attentionUsually faster and smaller
--parallel 1One server slotSingle user; avoids KV multiplication

--n-cpu-moe N keeps the first N layers' expert tensors on the CPU. It is a prefix, not a budget. If you want the last layers on the CPU instead, use -ot with a block range.

--moe-cache-mib is newer. It caches recently used CPU-resident experts in VRAM, which helps when routing is stable. Check llama-server --help before relying on it.

A worked example: Hy4-flash on 96 GB

The author's Q4_K_M run kept the Engram table in RAM and the rest on the GPU. In llama.cpp, that is an explicit tensor override:

llama-server \
  -m Hy4-flash-abliterated-Q4_K_M-00001-of-00002.gguf \
  -ngl 99 \
  -ot "per_layer_token_embd=CPU" \
  -c 16384 \
  -fa on \
  --jinja \
  --host 127.0.0.1 \
  --port 8080

The -00001-of-00002.gguf file is the entry point; llama.cpp loads the second shard automatically.

On a 48 GB GPU, the same file needs expert offload. Start with a prefix and sweep it:

llama-server \
  -m Hy4-flash-abliterated-Q4_K_M-00001-of-00002.gguf \
  -ngl 99 \
  -ot "per_layer_token_embd=CPU" \
  --n-cpu-moe 24 \
  -c 8192 \
  -fa on \
  --jinja

24 is a starting point, not a recommendation. The right number depends on your card, the context, and how much RAM you have. Measure it.

Sizing by machine

SetupWhat fitsReality check
16 GB VRAM, 32 GB RAM35B-A3B Q4 with most experts on CPUSlow; keep context at 4K–8K
24 GB VRAM, 64 GB RAM35B-A3B Q4 fully on GPU; 120B Q4 with heavy offload120B is a stretch
32 GB VRAM, 96 GB RAM120B Q4 with offload; Hy4 IQ4_XS with most experts on CPUHy4 will be slow
48 GB VRAM, 128 GB RAMHy4 IQ4_XS with partial offload; Hy4 Q4 with heavy offloadMeasure before committing
96 GB VRAM, 64 GB RAMHy4 Q4 with the Engram table in RAMThe author's setup
2×96 GB VRAMERNIE 5.5 Flash NVFP4 at TP=2vLLM, not llama.cpp

Two rules keep the table honest:

  1. The file must fit in VRAM plus RAM. If it does not, the OS pages from disk and throughput collapses.
  2. Leave headroom. Compute buffers, the KV cache and fragmentation need 1–2 GB. A machine that is 100% full at load will fail at 16K context.

Why it can still be slow

Moving experts to the CPU is not free. Three costs show up:

  • CPU matmuls. The experts that are not on the GPU are computed on the CPU. That is bandwidth-bound and much slower than a GPU matmul.
  • PCIe traffic. If the runtime copies experts to the GPU instead of computing on the CPU, each token pays a transfer. --moe-cache-mib reduces repeat transfers but cannot remove the first one.
  • Prefill. Prompt processing touches many tokens and many experts. With experts on the CPU, prefill can be far slower than decode. A model that feels fine at 20 tokens/s can take a long time to read a 32K prompt.

Measure both numbers: prompt-processing speed and generation speed. A single tokens/s figure hides the cost that matters for long documents.

Multi-GPU

llama.cpp splits across GPUs with --split-mode:

  • layer (default) pipelines layers across GPUs. It is the simplest and works over PCIe.
  • row splits tensors by rows. It can use more bandwidth and benefits from NVLink.
  • tensor splits weights and KV across GPUs and is marked experimental.

--tensor-split 3,1 gives the first GPU three times the share. Use it when the GPUs have different memory.

For NVFP4 checkpoints, the runtime is usually vLLM rather than llama.cpp. There, the equivalent knob is --tensor-parallel-size, often with --enable-expert-parallel. The ERNIE release uses TP=2 or TP=4; check the model card for the exact command.

Measure, don't guess

llama-bench accepts the same offload flags:

llama-bench \
  -m Hy4-flash-abliterated-Q4_K_M-00001-of-00002.gguf \
  -ngl 99 \
  -ncmoe 24 \
  -p 512 \
  -n 128

Sweep -ncmoe in steps and record both pp512 and tg128. The best value is the smallest prefix that keeps you under the VRAM limit with the context you actually use. Larger values load too, but they move work to the CPU for no reason.

Watch memory while it runs:

  • NVIDIA: nvidia-smi or nvtop.
  • Apple Silicon: Activity Monitor's Memory Pressure graph, not the raw used-memory number.
  • AMD: rocm-smi.

If the process starts swapping, reduce context, drop to a smaller quant, or move experts back to the GPU if VRAM allows. If it is merely slow, move fewer experts to the CPU and try again.

Caveats

  • Not all MoEs are the same. Shared experts, dense layers and special tables change the ratio of “must be on GPU” to “can be on CPU.”
  • Quant quality still matters. A Q4 file that fits is better than a Q2 file that fits easily. Do not use offload as an excuse to drop two quant levels.
  • Context is part of the budget. A 32K context can cost several GB of KV cache. Quantized KV helps, but it is not free.
  • Vendor fit claims are optimistic. “Runs on 24 GB” often means a short context, a small quant, and no other GPU work. Measure your own workload.
  • The model card is the source of truth for flags. Architectures like Hy4-flash need a recent llama.cpp build; older binaries can load a file and produce nonsense.

Next steps

11 minGuide

How to check a GGUF before you load it

A practical pre-flight for downloaded GGUF files: read the chat template, verify the hash, find bytes after the last tensor, and keep the runtime away from your secrets.

#gguf#security#safety
Read