Advanced

Beam search, speculative decoding & Medusa

Beam search explores likely sequences for quality, while speculative and multi-head decoding seek exact or controlled speedups by proposing several future tokens.

Updated

1

Concept

Autoregressive generation exposes a serial dependency: token t+1t+1 cannot be finalized until token tt is known. Several methods spend extra parallel work around this chain, but they optimize different things. Beam search explores sequences to improve a search objective. Speculative decoding and Medusa propose future tokens so the expensive model may validate several positions in one pass.

Greedy decoding keeps one partial sequence. Beam search keeps bb candidates. At each step it expands each beam with possible next tokens, adds log-probabilities to cumulative scores, and retains the best candidates according to a scoring rule. Length normalization and stopping rules are essential because raw summed log-probability favors shorter sequences. Beams can share KV-cache prefixes and branch through copy-on-write metadata.

Beam search is useful when a concentrated output and sequence-level likelihood matter, such as some constrained transduction tasks. For open-ended dialogue, several beams often collapse into near-duplicates or favor bland, high-probability text. Increasing beam width raises model work and cache use. It is not a general inference acceleration technique.

Speculative decoding starts with a smaller or cheaper draft model. The draft autoregressively proposes a block of future tokens. The larger target model evaluates those proposed positions together, exploiting parallelism similar to a short prefill. An acceptance procedure compares draft and target probabilities. Accepted tokens advance the sequence; after the first rejection, dependent draft suffix tokens are discarded and a corrected token is sampled.

With the proper acceptance and correction rule, speculative sampling preserves the target model’s output distribution. This is stronger than merely checking whether the target’s argmax equals the draft token. Exactness refers to the mathematical sampler under matching numerical and implementation assumptions, not bit-identical output across different kernels.

Speed depends on acceptance rate and relative cost. A draft that is too weak proposes many rejected tokens. A draft that is too expensive saves little. Longer proposal blocks offer more potential progress but waste more work after early rejection. The best configuration depends on prompts, decoding settings, batch size, hardware, and load.

Medusa avoids a separate full draft model by attaching multiple lightweight decoding heads to the base model. Heads predict tokens at several future offsets, creating a tree of candidate continuations. A verification pass checks the tree with the base model and accepts a valid path. Training or adapting these heads must match the target checkpoint; changing the base can invalidate their proposal quality.

Tree attention and cache bookkeeping are the quiet complexity. Candidate branches share a prefix but differ later. Masks must prevent one branch from reading another. Accepted states must be committed in order; rejected branches must release memory. A bug here can produce plausible but incorrect tokens, so distributional tests against baseline decoding matter.

Constrained decoding can combine with these methods, but proposal and verification must obey the same grammar state. Tool calls and stop sequences also change acceptance boundaries. Streaming may arrive in bursts when several tokens are accepted, so clients must not assume one network event per model step.

The durable distinction is purpose. Beam search pays for multiple hypotheses to choose a preferred sequence. Speculation pays a cheap proposer to expose parallel work while the target remains authoritative. Medusa moves proposal heads into the target architecture. All three branch into possible futures; only their objective, acceptance contract, and cost decide whether that branching helps.

2

Explain it like I am five

Three delivery teams handle uncertain roads differently. Beam search keeps several complete routes alive and discards weak ones as the trip grows. Speculative decoding sends a bicycle scout ahead, then lets the authoritative truck validate several turns at once and roll back after the first bad turn. Medusa mounts several route-prediction heads on the truck itself, proposing a small tree of future turns for one verification pass.

3

Teach it back

Contrast beam search's objective with speculative decoding's objective, then explain how rejection preserves the target distribution in exact speculative sampling.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

Beam search maintains several high-scoring partial sequences to find a sequence with strong cumulative probability; it spends extra compute for search quality and is not primarily a latency trick. Speculative decoding uses a cheaper draft model to propose multiple tokens, then evaluates them in parallel with the target model. An acceptance rule keeps proposals consistent with the target probabilities and samples a corrected token when a proposal is rejected, so the output distribution can match ordinary target-model sampling.

4

Check your understanding

1. What happens after the target rejects a speculative token?
Answer and explanation

Later draft tokens from that branch are discarded and generation resumes from a corrected token — Tokens after the first rejected proposal depend on a prefix the target did not accept and therefore cannot remain.

2. What does Medusa add to a language model?
Answer and explanation

Multiple decoding heads that propose future tokens for tree verification — Medusa attaches heads that predict several future positions, then verifies candidate branches with the base model.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Yaniv Leviathan, Matan Kalman, and Yossi Matias (2022). Fast Inference from Transformers via Speculative Decoding.
  2. Tianle Cai et al. (2024). Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads.