Start here: what one token costs

A plain ("dense") transformer produces each new token with one full forward pass: every layer, every weight, in order. Then it appends that token and does it all again. Tokens come out strictly one after another.

The surprising part is that the bottleneck is usually not the math. Generating for one user, the GPU spends most of its time reading the weights out of memory:

8B params × 2 bytes (bf16) = 16 GB read per token
16 GB ÷ ~1 TB/s memory bandwidth ≈ 16 ms per token → at most ~60 tokens/s

So there are three ways to spend less per token, and each technique below takes one of them:

Idea: many MLPs per layer, and each token only uses a few

Recall that the MLP is where most parameters, and most stored knowledge, live. MoE replaces the single MLP in each layer with E separate MLPs ("experts"), plus a tiny router: one small matrix that scores every expert for the current token. Only the top k experts run, and their outputs are blended by those scores. Attention stays dense.

text experts E top-k router
tokens sent to each expert (dashed = perfectly even share)

Click a row to see that token's blend. Router weights are hand-set to be readable (illustrative). In trained models, experts specialize less cleanly than this, and more by syntax and token type than by topic.

The math

Load-balancing term added to the training loss (Switch Transformer), where fi is the fraction of tokens sent to expert i and Pi is its mean router probability:

It is smallest when the load is even. Without it, the router "collapses": a few experts get all the tokens, train more, get picked more, and the rest die. Toggle router → no balancing above to see this. DeepSeek-V3 instead nudges a per-expert bias up or down to even out load.

Stored vs. used

ModelExperts (active)TotalActive
Mixtral 8×7B8 (2)47B13B
DeepSeek-V3256 + 1 shared (8)671B37B
Kimi K2384 + 1 shared (8)~1T32B

Knowledge capacity scales with total parameters. Cost per token scales with active parameters.

The catch

  • Memory, not compute: all experts must be loaded, because any token might need any of them. DeepSeek-V3 is cheap to run per token but still needs ~700 GB of weights resident, spread over many GPUs.
  • Communication: with experts on different GPUs, tokens are shuffled between devices every layer. That's where MoE serving engineering goes.
  • Training is fussier: load balancing and router instability need care, and the top-k choice isn't differentiable. Gradients flow through the gate weights gi of the chosen experts.
  • Why it wins anyway: at a fixed compute budget, MoE models reach lower loss than dense ones. More knowledge per FLOP is why most frontier open models are now MoE.

Idea: easy tokens don't need every layer

The residual stream holds the model's current best guess, and it is refined layer by layer. You can read that guess at any layer by applying the final norm and the output head early. This is called the logit lens. After "The capital of", the token "of" is obvious by layer 2. "Seine" needs the deep layers that recall facts. Early exit stops a token as soon as its guess is confident enough.

exit when confidence ≥ (1.00 = never exit early = the normal model)
top guess = final answer (shade = confidence) top guess is wrong at this layer exit layer skipped

Each row predicts the next word of "The capital of France is Paris, and its river is the Seine." The layer-by-layer confidences are illustrative, shaped like real logit-lens measurements, not taken from a trained model. Drag the threshold down: compute drops, then wrong early guesses start to slip through, starting with "of → the" and "and → the".

The math

hℓ is the residual stream after layer ℓ, and WU is the output head. Emit argmax pℓ* and skip layers ℓ*+1…L. Real systems (e.g. CALM, 2022) train the model so intermediate layers give good predictions, and calibrate τ so outputs stay close to the full model's.

The catch

  • The KV-cache hole: if token 5 exits at layer 4, layers 5–12 never compute its K and V. Token 6 then attends to nothing for token 5 in those layers. Fixes include copying the exited state upward or filling it in later, and all cost something.
  • Batching kills it: GPUs serve many users in lockstep. If one token in the batch needs all 12 layers, the batch waits anyway.
  • Confidently wrong: early layers often favor generic words ("the"). A fixed threshold trades accuracy for speed.
  • Where it lives on: rarely shipped as-is. Its ideas survive in Mixture-of-Depths (tokens routed around blocks) and LayerSkip (2024), which uses early layers as a built-in draft model for speculative decoding (next tab).

Side by side

SkipsSavesCostsSame output?
Mixture of expertsmost MLP weights, per token (width)compute per token; more knowledge per FLOPmemory for all experts, routing, cross-GPU trafficno: it is a different model, trained that way
Early exitlater layers, for easy tokens (depth)compute and latency on easy tokensKV-cache holes, poor batching, some errorsapproximately
Speculative decodingsequential big-model passes (time)latency, often 2–3×draft model, wasted guessesyes, exactly the same distribution

They stack: a production system can serve an MoE model with speculative decoding and quantized weights all at once.

Idea: guess cheaply, check expensively, but check many at once

Remember the cost is reading the weights. One big-model pass over 5 tokens reads the weights once, so it costs about the same as a pass over 1 token. So a small draft model guesses the next γ tokens one by one (cheap). Then the big target model scores all γ guesses in a single parallel pass, exactly like training-time teacher forcing. It keeps the longest acceptable prefix, and a clever accept/reject rule makes the output distribution exactly the big model's. Nothing is approximated.

draft length γ draft quality
Press "Run one round".
draft token accepted rejected → resampled from the target bonus token (all drafts accepted)

Both models are toy word-level models built from a small set of sentences. The target is a bigram model; the draft is the target blurred toward uniform by the "draft quality" knob. The accept/reject algorithm is the real one (Leviathan et al. and Chen et al., 2023).

The rule that makes it exact

For each drafted token x ~ q (draft), accept with probability

At the first rejection, resample from the leftover mass where the target wanted more than the draft gave, and stop:

Why it equals p: tokens that are accepted contribute min(p,q). Rejections happen with probability 1−β (β = Σ min(p,q)) and the resample refills exactly (p−q)+:

Check it: 20,000 trials

after the word
target p (what we want)speculative outputdraft q

Sampling is exact even when the draft is bad: a bad draft only costs speed. Set draft quality to 0 and run again.

In practice

  • Typical win is 2–3× lower latency on predictable text: code, boilerplate, and quoting the prompt back.
  • Draft options: a small model of the same family; extra prediction heads on the big model (Medusa, EAGLE); the big model's own early layers (LayerSkip); or plain copying of n-grams from the prompt (prompt lookup).
  • The catch: wasted draft work when guesses miss, extra memory for the draft, and less benefit at large batch sizes, where the GPU is already busy with arithmetic.
  • It is standard in serving stacks (vLLM, TensorRT-LLM, llama.cpp) and widely used by API providers.