Sub topic 14
Mixture-of-experts models: big in memory, lighter per token
This chapter assumes you can estimate weight memory from parameter count and bits per weight, and that you know token generation is usually limited by memory bandwidth. A mixture-of-experts (MoE) model changes how those two facts combine.
What a mixture-of-experts model is
In a dense model, every parameter is used for every token. In an MoE model, each transformer layer holds many separate feed-forward blocks called experts. A small component called the router picks a few experts for each token. The other experts in that layer sit idle for that token.
Two numbers describe an MoE model:
- Total parameters: every weight in the file, including all experts.
- Active parameters: the weights actually used to produce one token.
For example, a model named "30B-A3B" has about 30 billion total parameters and about 3 billion active per token.
Memory follows total, speed follows active
The router can choose different experts for each token, so every expert must be available at any moment. That means all weights must sit in fast memory. The memory budget depends on total parameters.
Each generated token reads only the selected experts, plus the shared parts such as attention. The bandwidth cost per token depends on active parameters. The ceiling from the earlier chapter still applies, but you replace the model file size with the bytes read per token.
A worked comparison
The script below compares a dense 8B model with a 30B-A3B MoE model, both at about 4.85 bits per weight, on a machine with 100 GB/s of memory bandwidth. It treats the active share as bytes read per token, which is a simplification because shared layers and routing add some extra reads.
def weights_gib(params_billion, bits_per_weight):
return params_billion * 1e9 * bits_per_weight / 8 / 2**30
def ceiling_tokens_per_s(bandwidth_gb_s, active_params_billion, bits_per_weight):
bytes_per_token_gb = active_params_billion * bits_per_weight / 8
return bandwidth_gb_s / bytes_per_token_gb
bits = 4.85
bandwidth = 100 # GB/s
models = [
("dense 8B", 8.0, 8.0),
("MoE 30B-A3B", 30.0, 3.0),
]
for name, total, active in models:
mem = weights_gib(total, bits)
speed = ceiling_tokens_per_s(bandwidth, active, bits)
print(f"{name}: weights {mem:.1f} GiB, ceiling {speed:.0f} tokens/s")The MoE model needs about 17 GiB for weights, more than three times the dense model's 4.5 GiB. Its ceiling is roughly 55 tokens per second against about 21 for the dense model. You pay in memory and gain in speed.
When MoE helps and when it does not
MoE suits machines with a lot of memory but modest bandwidth, such as a desktop with plenty of system RAM or a unified-memory laptop. It lets a large model run at a usable speed.
It helps less in these cases:
- The weights do not fit in VRAM, so some experts sit in slower memory.
- Prompt processing handles many tokens at once. Different tokens pick different experts, so a long prompt touches most of them.
- You need the most compact file. A dense model of equal quality is usually smaller, though not necessarily as fast.
Common errors
- Sizing memory from the active count. A 30B-A3B model needs memory for 30B, not 3B.
- Applying the speed ceiling to total size. Use the active size for generation.
- Forgetting the KV cache. Context length still adds memory in the same way as for dense models.
Exercise
An MoE model has 47B total parameters and 13B active per token, stored at 4.85 bits per weight. Roughly how much memory do the weights need, and what is the bandwidth-based generation ceiling on a machine with 200 GB/s?
Show answer
Weights: 47e9 × 4.85 / 8 ≈ 28.5 GB, about 26.5 GiB. Bytes read per token: 13e9 × 4.85 / 8 ≈ 7.9 GB. Ceiling: 200 / 7.9 ≈ 25 tokens per second. Memory is sized by total parameters, speed by active parameters.