TL;DR
Large language models write text one token at a time, and each token needs a full pass over the model's weights, so the hardware sits mostly idle. Medusa adds a few extra output heads to the same model. Each head guesses a token further ahead, the guesses are arranged into a tree of candidate continuations, and the model checks all of them in one pass, keeping the longest correct run. The paper reports speedups of 2.18x to 2.83x on Vicuna-7B and 2.33x to 2.83x on Vicuna-13B on the MT-Bench benchmark, with output quality kept close to the original model.
Builds on: Speculative Decoding, KV Cache · Leads to: none yet in this graph
Why it mattered
The method is simple to add to an existing model: it trains a small set of heads and leaves the rest of the system alone. The authors state that libraries such as TensorRT and Hugging Face TGI added support for these ideas following the paper, and that the ideas extend to larger batch sizes. The paper's own experiments use batch size 1, which is the case of a model running on one user's machine, and its evaluation is on chat models such as Vicuna and Zephyr. The practical change is that a team serving a chat model can speed it up by training heads on top of it, instead of finding, training and hosting a second small model that agrees with the first.
The problem
Autoregressive decoding generates token only after token is known. Each step therefore moves the full set of model parameters from high-bandwidth memory into the processor's cache to produce a single token. The paper describes this as memory-bandwidth-bound: the arithmetic units are underused because moving data is the bottleneck. The idea behind this is covered in GPU Memory Hierarchy.
Speculative decoding is the known fix. A small draft model proposes several tokens, and the big model checks them in one pass. The paper names two costs. Existing draft models are usually pre-trained separately, which can take substantial compute (one cited example used 275 A100 GPU hours), and a separately trained draft can drift in distribution from the target, so it proposes continuations the target rejects. Medusa keeps the propose-then-check pattern but takes the draft from the model itself.
How it works
Take one decoding step with two heads, K = 2, and a tree that takes the top 2 guesses from the first head and the top 3 from the second.
- Backbone. The original model runs on the text so far and produces its last hidden state for the newest position. The normal head turns into a probability for the next token. Check: this is what the model already did.
- Medusa heads. Two extra heads take the same . Head 1 predicts the token at position , and head 2 predicts the token at position . Each head gives a distribution over the vocabulary. Check: each head outputs one probability list, and each list sums to 1.
- Top guesses. Head 1 offers its 2 most likely tokens. Head 2 offers its 3 most likely tokens. Check: 2 times 3 gives 6 full candidate continuations. Candidates that share a first token share a branch of the tree.
- Tree attention. The 6 candidates form a tree with 8 new nodes (2 at the first level, then 6 at the second). The model processes all 8 nodes in one forward pass, using an attention mask so each node sees only its own ancestors. Check: one pass handles 8 tokens of work, instead of 1 token per pass.
- Verify. The original model's probabilities judge each candidate. The longest candidate prefix that is accepted becomes the output of this step. Check: if the first guess is accepted and the second is not, the step adds 2 tokens instead of 1.
- Next step. The model's output at the accepted position supplies the new hidden state, so the heads can start again.
flowchart LR H[Hidden state h_t] --> B[Original head: next token] H --> M1[Medusa head 1: top 2] H --> M2[Medusa head 2: top 3] M1 --> T[Tree of 6 candidates] M2 --> T T --> A[One tree-attention pass] A --> V[Accept longest valid prefix]
Worked examples
Use heads with , , . How many new tokens does the tree hold?
- Level 1: nodes.
- Level 2: nodes.
- Level 3: nodes.
- Total: new tokens, all verified in one pass.
The number of leaves (full candidates) is 8, one per path from the root to level 3.
The paper defines the acceptance rate as the average number of tokens produced per decoding step, and overhead as the per-step time of Medusa divided by the time of ordinary decoding. Speedup is acceptance rate divided by overhead (Appendix B.1). Table 1 gives Vicuna-7B with Medusa-2 an acceptance rate of 3.47 and an overhead of 1.22.
- Divide the acceptance rate by the overhead: .
- Compute: , and , so the quotient is about 2.84.
- The paper reports a speedup of 2.83 for the same model.
The two agree to within rounding, so the relation holds as a rough check. Quote the reported speedup when you cite the paper.
Table 4 reports tokens per second on the AlpacaEval dataset. For Vicuna-7b, the base speed is 37.07 tokens per second and the Medusa speed is 106.76 tokens per second.
- Divide the Medusa speed by the base speed: .
- Compute: , so the ratio is 2.88.
- The table reports a speedup of 2.88. For Vicuna-33b the same division gives , matching its reported speedup.
Try it: the Vicuna-13b row has a base speed of 29.01 and a Medusa speed of 91.54 in Table 4. Is the Vicuna-13b speedup in the table 3.16? Answer: , which rounds to 3.16. The table's speedup for Vicuna-13b is 3.16, so yes.
The math
Two training variants appear below. Medusa-1 trains only the heads on a frozen backbone, so the backbone's output is unchanged and the speedup is lossless. Medusa-2 trains the heads and the backbone together, which gives more accurate heads and a higher speedup, but needs a training recipe that keeps the backbone's own abilities intact.
Write for the last hidden state at position , with dimension . The original model's head gives . Medusa adds heads, and head predicts the token positions ahead of the original head's position.
Step 2 in symbols: the Medusa head
Each head is one feed-forward layer with a residual connection, followed by the output projection and a softmax. SiLU is a smooth activation function:
Here is a matrix and is a matrix, where is the vocabulary size. The paper initialises to match the original head and to zero, so at the start every Medusa head predicts what the original model predicts.
Step 3 in symbols: the candidate tree
If head keeps its top tokens, the tree has nodes at level . The total number of new tokens is
With , and : level 1 has 2 nodes and level 2 has nodes, so the total is , matching the walkthrough.
Step 5 in symbols: typical acceptance
The paper also gives a looser acceptance rule than rejection sampling. A candidate token at a position is accepted when
where is the Entropy of the original model's distribution at that position, is a fixed threshold and scales with entropy. When the distribution is spread out (high entropy), more continuations count as reasonable.
Training loss for Medusa-1
Head is trained with cross-entropy against the true token , with a weight . The paper sets :
Later heads are harder to predict, so their weight is smaller.
Results
Main speedups. On Vicuna-7B, Medusa-1 gives a 2.18x wall-time speedup and Medusa-2 gives 2.83x. On Vicuna-13B, Medusa-1 gives 2.33x and Medusa-2 gives 2.83x (Section 3.1). The paper says the coding category of MT-Bench speeds up by 3.29x and the extraction category by 3.62x, both for Medusa-2 on Vicuna-7B.
Quality. Table 2 reports MT-Bench quality, a set of chat prompts whose answers GPT-4 scores from 0 to 10. The baseline scores 6.17, Medusa-1 scores 6.23 and Medusa-2 scores 6.18. Direct fine-tuning of the full model with the heads scores 5.925, which is lower, so the two-stage training is needed. Table 1 gives quality changes for the self-distillation models: Vicuna-7B +0.01, Zephyr-7B -0.07, Vicuna-13B -0.14, Vicuna-33B +0.05.
Breakdown. Table 3 gives one rough path from the naive approach to the final method on Vicuna-7B: Medusa-1 heads without tree attention about 1.5x, adding tree attention about 1.9x, an optimised tree about 2.2x, and training with Medusa-2 about 2.8x.
Limits. The main experiments use batch size 1. The paper's simulations (Appendix G) suggest speedup drops for batch sizes above 32, because the linear layers become compute-bound, but this is a model and not a measurement. Quality for Vicuna-13B drops by 0.14 with self-distillation, and the 33B model shows a lower acceptance rate, which the authors attribute to a mismatch between their hidden training data and the self-distillation data. The abstract's range of 2.3x to 3.6x for Medusa-2 is wider than the 2.3x to 2.8x in the introduction; the table values above are the ones to quote.
Reading guide
- Section 1 (Introduction): the memory-bandwidth problem and why draft models are hard to use. Needs Speculative Decoding.
- Section 2.1 (Medusa heads and tree attention): the head formula and the candidate tree. Needs Softmax and Attention Mechanism.
- Section 2.2 (Training): the cross-entropy loss and the two training stages. Needs Cross-Entropy Loss, and Low-Rank Adaptation (LoRA) or QLoRA for the parameter-efficient setting.
- Section 2.3 (Typical acceptance and self-distillation): needs Entropy and Knowledge Distillation.
- Section 3 (Experiments): the speedup tables. Read after Section 2.
- Appendix G: the hardware model. Needs GPU Memory Hierarchy; read it last.
Check yourself
QuestionWith 3 heads and top-2 choices at every level, how many candidate continuations (leaves) does the tree have?Show answer
Each level multiplies by 2, so the leaves are at level 3: candidates.
QuestionVicuna-7B with Medusa-2 has an acceptance rate of 3.47 and an overhead of 1.22. What speedup does the simple relation predict, and what does the paper report?Show answer
. The paper reports 2.83, so the relation holds to within rounding.
QuestionWhy does Medusa use a small set of heads on the backbone instead of a separate draft model?Show answer
A separate draft model must be found or pre-trained, which costs compute and can differ in distribution from the target. Heads on the target's own hidden state share its distribution, add only one layer each, and need no second model to serve.
QuestionWhy does the typical acceptance rule accept more candidates when the entropy of the original distribution is high?Show answer
The threshold falls as entropy rises, so a candidate with lower probability can still pass. High entropy means many continuations are reasonable, which the rule allows.
Sources
- Medusa paper, arXiv: https://arxiv.org/abs/2401.10774
- DOI for the same paper: https://doi.org/10.48550/arXiv.2401.10774
- Speculative sampling, the draft-model method Medusa compares against: https://arxiv.org/abs/2302.01318