Inference: Computation Reduction
By Ludovico Bessi, Senior ML Engineer at Google ยท Updated
Spending less compute on easy tokens and easy requests: early exiting, cascades and routing.
The Idea
A transformer spends the same amount of compute on every token: every layer, every time. But not every token is equally hard. The next token after "The United States of" is obvious; the next step in a proof is not. Not every request is equally hard either: "what is 2+2" does not need your largest model.
Computation reduction methods make cost adaptive: easy work exits early or goes to a cheaper model, and hard work gets the full model. There are two levels:
- Within a model, per token: early exiting.
- Across models, per request: cascades and routing.
Early Exiting
How it works
Attach a prediction head (or reuse the model's LM head) after some intermediate layers. At each exit point, estimate whether the model is already confident about the next token. If it is, stop and emit the token without running the remaining layers.
Confidence measures
- Softmax confidence: the gap between the top two probabilities at the intermediate head.
- Hidden-state saturation: if the hidden state barely changes between consecutive layers, more layers are unlikely to help.
- A learned classifier that predicts whether exiting now would change the final answer.
CALM
Confident Adaptive Language Modeling (CALM; Schuster et al., 2022) made early exit work for autoregressive generation. Two contributions matter:
- Calibrated thresholds. CALM sets the exit thresholds using a statistical procedure so that, with high probability, the early-exit output stays within a chosen tolerance of the full model's output. Quality loss becomes a knob rather than an accident.
- Handling skipped layers. When a token exits at layer 8, later tokens still need keys and values from layers 9 and up for that token. CALM propagates the exited token's last computed hidden state to the skipped layers to fill the KV cache.
CALM reported potential speedups of up to 3ร while provably maintaining output quality.
LayerSkip and self-speculation
LayerSkip (Elhoushi et al., 2024) trains the model with layer dropout (higher rates for later layers) and an early-exit loss, so that early layers produce usable predictions. At inference, the early layers act as a draft model and the full model verifies โ self-speculative decoding. Because the draft is part of the same network, it reuses the same weights and KV cache, and the verification keeps the output identical to the full model.
Practical challenges
- Batching: in a batch, different tokens want to exit at different layers. The batch must wait for the slowest token unless the engine regroups work, which erodes the gains at high load.
- KV cache consistency: skipped layers still need KV entries, as in CALM.
- Training: most off-the-shelf models were not trained for early exits, and their intermediate layers predict poorly without fine-tuning.
Early exiting shines in latency-sensitive, low-batch settings, which is why the self-speculative form, which avoids quality risk, has become the more popular variant.
Cascade Inference
A cascade sends each request to the cheapest model first and escalates only when needed.
request โ small model โ confident? โ answer
โ not confident โ medium model โ confident? โ answer
โ not confident โ large model โ answer
Deciding when to escalate
- Model confidence: token-level probabilities, or the model's own verbalized confidence.
- A scorer: a small model trained to predict whether an answer is acceptable. FrugalGPT (Chen et al., 2023) used a learned scorer to decide whether to accept a cheaper LLM's answer, and reported matching the best individual LLM (GPT-4) with up to 98% cost reduction, or 4% higher accuracy than GPT-4 at the same cost.
- Self-consistency: sample several answers from the small model; if they agree, accept.
- Task signals: for code, does it compile and pass tests? For extraction, does it validate against a schema?
Costs
Cascades add latency for escalated requests, since they pay for the small model's attempt first. They work best when most traffic is easy and the escalation check is cheap and reliable.
A different "cascade": shared-prefix attention
In serving engines such as FlashInfer, cascade inference also names a kernel-level technique: when many requests share a long prefix, attention is split into a shared part (all requests' queries against the prefix KV, computed once with good memory reuse) and a unique part (each request against its own suffix). The two partial results are merged with the online-softmax rule. This reduces memory traffic for batches with common prefixes, complementing prefix caching.
Routing
A router decides up front which model handles a request, with no escalation loop. It might be a small classifier trained on which model wins for which kind of query (RouteLLM is one open framework), or simple rules based on task type, user tier or prompt length.
Routing has lower latency than a cascade, since there is only one model call, but it must predict difficulty before seeing any answer, so it is less accurate.
Mixture of Experts as Built-In Computation Reduction
A Mixture-of-Experts model applies routing inside each layer: each token goes to a few of many expert FFNs. A model like Mixtral 8x7B has about 47B parameters in total but uses about 13B per token, so FLOPs per token resemble a 13B dense model. The memory footprint remains that of the full model, so MoE reduces compute, not memory. See LLM Architecture.
When to Use What
| Situation | Technique |
|---|---|
| Single-user, latency-critical, can fine-tune | Early exit / LayerSkip self-speculation |
| Mixed easy/hard traffic, cost-sensitive | Cascade with a cheap reliable check |
| Clear request categories, latency-sensitive | Router |
| Many requests share a long prefix | Shared-prefix (cascade) attention + prefix caching |
| Building or choosing a model | MoE for compute efficiency |
The highest-leverage version for most teams is simple: route easy traffic to a smaller model. Distilled small models are often good enough for most requests, and the cost difference is an order of magnitude. See the LLM Inference Cost Optimization playbook.
Key Takeaways
- Compute should match difficulty: easy tokens and easy requests should cost less.
- Early exiting stops at intermediate layers when confident; CALM calibrates thresholds and fills skipped layers' KV entries.
- LayerSkip turns early exits into self-speculative decoding with identical output.
- Cascades try cheap models first and escalate; FrugalGPT showed large cost savings with a learned acceptance scorer.
- Routers pick one model up front; MoE routes inside the model, cutting FLOPs but not memory.