Sub topic 14
Mixture-of-experts models: big in memory, lighter per token
This chapter assumes you can already estimate weight memory from parameters and bits per weight, and that you know token generation is mostly limited by memory bandwidth. It also assumes you have seen the KV cache formula. Here you will see how mixture-of-experts (MoE) models break the usual link between file size and speed.
Dense models versus mixture-of-experts
A dense model uses every parameter for every token. If a dense model has 8 billion parameters, each generated token passes through all 8 billion.
A mixture-of-experts model replaces some layers with a set of parallel sub-networks called experts. A small router network looks at each token and picks a few experts to run. The other experts stay idle for that token.
This gives two parameter counts:
- Total parameters: every weight in the file, including all experts. This sets the file size and the memory you need.
- Active parameters: the weights actually used for one token. This sets how much computation a token needs.
Model names often show both numbers. A name such as "30B-A3B" means about 30 billion total parameters and about 3 billion active per token. Names vary by publisher, so check the model card for the real values.
Not every weight is an expert. Attention layers, embeddings and the router are shared, and every token uses them. So active parameters are the shared weights plus the few chosen experts.
What this means for memory
The router chooses experts per token, and the next token may need different ones. Across a long prompt or a long answer, almost every expert gets used at some point. So an inference engine cannot drop the idle experts. All of them must stay in memory, or be readable quickly.
Memory for weights therefore follows total parameters. Use the same formula as before:
def weights_gib(params_billion, bits_per_weight):
return params_billion * 1e9 * bits_per_weight / 8 / 2**30
total_b = 30.0 # total parameters, in billions
active_b = 3.0 # active parameters per token, in billions
bits = 4.85 # roughly a Q4_K_M file
print(f"memory for weights: {weights_gib(total_b, bits):.1f} GiB")
print(f"weights read per token (approx.): {weights_gib(active_b, bits):.1f} GiB")This prints about 16.9 GiB for the file and about 1.7 GiB read per token. A 30B MoE needs the memory of a 30B dense model. It does not fit in 8 GiB of VRAM at 4-bit, however light its per-token work is.
The KV cache does not shrink either. It depends on layers, KV heads, head size and context length. Those come from the attention part of the model, which every token uses. Experts do not change the KV cache formula.
What this means for speed
Generation speed is limited by how many bytes must be read for each token. For a dense model, that is about the whole file. For an MoE model, it is about the active weights only, so the bandwidth ceiling rises:
def decode_ceiling_tokens_per_s(bandwidth_gb_s, bytes_read_gb):
return bandwidth_gb_s / bytes_read_gb
bandwidth = 100 # GB/s, for example dual-channel system RAM at the high end
dense_file_gb = 18.2 # dense 30B at about 4.85 bits per weight
moe_active_gb = 1.8 # roughly the active part of a 30B-A3B model
print("dense ceiling:", round(decode_ceiling_tokens_per_s(bandwidth, dense_file_gb), 1))
print("MoE ceiling: ", round(decode_ceiling_tokens_per_s(bandwidth, moe_active_gb), 1))The dense ceiling is about 5.5 tokens per second. The MoE ceiling is about 55. The bandwidth is the same and the stored parameter count is the same. Only the bytes read per token changed.
Why MoE suits CPU and unified memory setups
Many consumer machines have a lot of slow memory and a little fast memory. A desktop might have 64 GiB of system RAM at about 60 to 100 GB/s and a GPU with 12 GiB of VRAM. A dense 30B model runs slowly from system RAM, because each token reads the whole file at that bandwidth.
An MoE model changes the trade. System RAM gives you room for a large total size. The small active set keeps per-token reads low, so the slow tier hurts less. The same logic applies to unified memory on Apple silicon and similar designs. There you have a large pool, and MoE lets you use big models without the speed of a big dense one.
Some llama.cpp builds can place the shared layers on the GPU and keep the expert weights in system RAM. The shared layers are used for every token and are small, so they gain the most from fast VRAM. The experts are large and each is used rarely, so they tolerate slower memory better. Check the options of your build, because flag names have changed between versions.
Gotchas and failure modes
- Total parameters still decide whether the model loads. Do not pick a model by its active count alone.
- The ceiling is looser than for dense models. Routing adds work, and different tokens need different experts, so cache behaviour and memory access are less regular. Real speed is often a smaller fraction of the ceiling than for a dense model. Measure it.
- Prompt processing behaves differently. When many tokens are processed in a batch, they collectively touch most experts. Prefill reads close to the whole model and does compute for many tokens at once. The per-token advantage mostly shows up in generation.
- Quantisation applies to experts as well. A 4-bit MoE file is about total parameters times 4.85 bits, and small experts may be more sensitive to aggressive quantisation than a large dense model. Test quality on your own tasks before going below 4 bits.
- Parameter counts in names are rounded. Use the file size on disk as your real figure for weight memory.
Applying it to a hardware choice
Suppose you have 32 GiB of unified memory and want responsive chat. A dense 32B model at 4.85 bits needs about 18 GiB of weights, plus a KV cache and the operating system. It fits, but each token reads about 18 GB, so a machine with 100 GB/s tops out near 5 tokens per second. A 30B-A3B MoE model has a similar file size and fits equally well, but reads about 10 times less per token. You would expect much faster generation.
If you have a 12 GiB GPU and 64 GiB of system RAM, the dense model must be split between them and runs at a speed set mostly by the system RAM part. The MoE model, with its shared layers on the GPU and its experts in RAM, can reach a useful speed.
Dense models can still win on quality per total parameter. If a dense model and an MoE model of similar file size both fit, benchmark both for speed and test both on your own prompts.
Exercise
A 100B-total, 10B-active MoE model is stored at 4.85 bits per weight. Your system RAM bandwidth is 80 GB/s. Roughly how much memory do the weights need, and what is the generation speed ceiling? Would a 24 GiB GPU alone hold it?
Show answer
Weights: 100e9 × 4.85 / 8 ≈ 60.6 GB, about 56 GiB. Bytes read per token use the active parameters: 10e9 × 4.85 / 8 ≈ 6.1 GB. Ceiling ≈ 80 / 6.1 ≈ 13 tokens per second. A 24 GiB GPU cannot hold 56 GiB of weights, so it would need to be combined with system RAM or a machine with a larger memory pool. Real speed will fall below 13 tokens per second.
Exercise
Why does an MoE model not need a smaller KV cache than a dense model with the same attention shape?
Show answer
The KV cache depends on the number of layers, KV heads, head size, context length and bytes per value. These belong to the attention layers, which every token uses. Experts are in the feed-forward part of the model, so they do not enter the KV cache formula.