Search

Search papers and lessons

Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads

Tianle Cai, Yuhong Li, Zhengyang Geng et al. · 2024

Medusa speeds up LLM text generation about 2 to 3 times by adding several extra prediction heads to one model, so it guesses several future tokens per step and checks them in one pass, with no separate draft model.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Probability
  • Neural network basics
  • Linear algebra
  • Architectures
  • ReadReady to readNot yet

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 t+1t+1 only after token tt 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.

  1. Backbone. The original model runs on the text so far and produces its last hidden state hth_t for the newest position. The normal head turns hth_t into a probability for the next token. Check: this is what the model already did.
  2. Medusa heads. Two extra heads take the same hth_t. Head 1 predicts the token at position t+2t+2, and head 2 predicts the token at position t+3t+3. Each head gives a distribution over the vocabulary. Check: each head outputs one probability list, and each list sums to 1.
  3. 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.
  4. 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.
  5. 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.
  6. 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

Example
1. Tiny: counting tree nodes

Use K=3K = 3 heads with s1=2s_1 = 2, s2=2s_2 = 2, s3=2s_3 = 2. How many new tokens does the tree hold?

  1. Level 1: s1=2s_1 = 2 nodes.
  2. Level 2: s1×s2=2×2=4s_1 \times s_2 = 2 \times 2 = 4 nodes.
  3. Level 3: s1×s2×s3=2×2×2=8s_1 \times s_2 \times s_3 = 2 \times 2 \times 2 = 8 nodes.
  4. Total: 2+4+8=142 + 4 + 8 = 14 new tokens, all verified in one pass.

The number of leaves (full candidates) is 8, one per path from the root to level 3.

Example
2. Standard: speedup equals acceptance rate over overhead

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.

  1. Divide the acceptance rate by the overhead: 3.47/1.223.47 / 1.22.
  2. Compute: 1.22×2.84=3.46481.22 \times 2.84 = 3.4648, and 3.47−3.4648=0.00523.47 - 3.4648 = 0.0052, so the quotient is about 2.84.
  3. 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.

Example
3. Realistic: AlpacaEval throughput (Table 4)

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.

  1. Divide the Medusa speed by the base speed: 106.76/37.07106.76 / 37.07.
  2. Compute: 37.07×2.88=106.7637.07 \times 2.88 = 106.76, so the ratio is 2.88.
  3. The table reports a speedup of 2.88. For Vicuna-33b the same division gives 40.43/17.87=2.2640.43 / 17.87 = 2.26, 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: 91.54/29.01=3.15691.54 / 29.01 = 3.156, 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 hth_t for the last hidden state at position tt, with dimension dd. The original model's head gives pt(0)p_t^{(0)}. Medusa adds KK heads, and head kk predicts the token k+1k+1 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:

pt(k)=softmax(W2(k)(SiLU(W1(k)ht)+ht))p_t^{(k)} = \text{softmax}\left(W_2^{(k)}\left(\text{SiLU}\left(W_1^{(k)} h_t\right) + h_t\right)\right)

Here W1(k)W_1^{(k)} is a d×dd \times d matrix and W2(k)W_2^{(k)} is a d×Vd \times V matrix, where VV is the vocabulary size. The paper initialises W2(k)W_2^{(k)} to match the original head and W1(k)W_1^{(k)} to zero, so at the start every Medusa head predicts what the original model predicts.

Step 3 in symbols: the candidate tree

If head kk keeps its top sks_k tokens, the tree has ∏i=1ksi\prod_{i=1}^{k} s_i nodes at level kk. The total number of new tokens is

∑k=1K∏i=1ksi\sum_{k=1}^{K} \prod_{i=1}^{k} s_i

With K=2K = 2, s1=2s_1 = 2 and s2=3s_2 = 3: level 1 has 2 nodes and level 2 has 2×3=62 \times 3 = 6 nodes, so the total is 2+6=82 + 6 = 8, matching the walkthrough.

Step 5 in symbols: typical acceptance

The paper also gives a looser acceptance rule than rejection sampling. A candidate token xx at a position is accepted when

poriginal(x∣context)>min⁡(ϵ, δexp⁡(−H(poriginal(⋅∣context))))p_{\text{original}}(x \mid \text{context}) > \min\left(\epsilon,\ \delta \exp\left(-H(p_{\text{original}}(\cdot \mid \text{context}))\right)\right)

where HH is the Entropy of the original model's distribution at that position, ϵ\epsilon is a fixed threshold and δ\delta scales with entropy. When the distribution is spread out (high entropy), more continuations count as reasonable.

Training loss for Medusa-1

Head kk is trained with cross-entropy against the true token yt+k+1y_{t+k+1}, with a weight λk\lambda_k. The paper sets λk=0.8k\lambda_k = 0.8^k:

LMedusa-1=∑k=1K−λklog⁡pt(k)(yt+k+1)\mathcal{L}_{\text{Medusa-1}} = \sum_{k=1}^{K} -\lambda_k \log p_t^{(k)}\left(y_{t+k+1}\right)

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

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: 2×2×2=82 \times 2 \times 2 = 8 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

3.47/1.22≈2.843.47 / 1.22 \approx 2.84. 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 δexp⁡(−H)\delta \exp(-H) 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

Read the full paper

Tianle Cai, Yuhong Li, Zhengyang Geng et al., 2024. arXiv:2401.10774