AI State of the Art
SOTA/Inference & Optimization/31 min
Section 123

Medusa: multi-head speculative decoding

Listen as a podcast →

The starting point, in one line

Inference with a large language model (LLM) is limited by memory bandwidth: at every token, all the weights have to be re-read from memory, and the chip waits. The full argument is in Quantization: the concepts; the Medusa authors restate it almost word for word in their opening paragraph. The whole "speculative" family of techniques amounts to getting several tokens out of a single re-read of the weights.

What Medusa changes relative to speculative decoding

Speculative decoding describes the two-model arrangement: a small draft model proposes, the large one verifies. You therefore have to obtain that small model, train it or find it ready-made, hold it in memory alongside the large one, and live with the fact that it "thinks" somewhat differently.

Medusa removes the second model. You graft extra outputs onto the large model itself: the original head predicts the next token, and new heads predict the one after that, and the one after that. The model becomes its own proposer.

Classic speculative decodingMedusa
Number of modelstwo, loaded togetherone
Where proposals come froma complete small model, run several times in sequencegrafted heads, run once in parallel
What must be trainednothing, if a small model of the same family already existsthe heads, for every model and every version
Do proposals condition each other?yes, the small model sees what it just proposedno, each head guesses blind

That last row is what drives the difference in behaviour, and we come back to it below: it is Medusa's structural weakness.

Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads (Cai et al., 2024)
The idea in one sentence

Instead of producing one token per traversal of the model, you add K heads that guess the next K tokens in parallel; a tree-shaped verification mechanism then sorts through the candidate sequences in a single extra traversal.

How a Medusa head is built

A standard Transformer has one output: it takes the last internal representation computed for the current token (a vector of numbers, written hh, of dimension dd (for instance d=4096d = 4096 for 7-billion-parameter models of the Llama family)) and turns it into a probability for every word in the vocabulary.

Medusa attaches extra heads to that same vector hh:

  • head 0 (the original one) → the token at position t+1t+1
  • Medusa head 1 → the token at position t+2t+2
  • Medusa head 2 → the token at position t+3t+3
  • … and so on up to head K

Each Medusa head is deliberately simple: a single linear layer of size d×dd \times d, followed by an activation function (the authors use SiLU, a smooth curve that squashes negative values), plus a residual connection, meaning hh is added back to the result, so the head can start from the original information rather than reconstruct it. Then comes a projection to the vocabulary, of size d×Vd \times V where VV is the vocabulary size.

The picture: several scouts stationed at the same spot, each assigned to look one step further down the road.

Diagram of how Medusa works. On the left, the frozen original model: embedding, Transformer layers, then the standard language head. The final hidden representation feeds both that standard head and three separately trained Medusa heads. Each head produces its three most likely tokens: It, I, As for the standard head; is, apostrophe, the for Medusa head 1; difficult, is, apostrophe for head 2; not, difficult, a for head 3. These predictions form a list of candidate sequences, only one of which (It is difficult) is validated and kept as the step's output.
The whole arrangement in one image. The original model (blue) stays frozen; Medusa heads (red) are grafted onto the same hidden layer. Each proposes its best tokens, candidate sequences are drawn from them, and a single verification pass keeps the longest one that holds up.Source : Together AI blog

What the heads cost in memory

Let us do the arithmetic in the open, with the values used by the authors: d=4096d = 4096 and a vocabulary of V=32,000V = 32{,}000 words.

  • the d×dd \times d layer: 4096×4096≈174096 \times 4096 \approx 17 million parameters;
  • the projection to the vocabulary, d×Vd \times V: 4096×32,000≈1314096 \times 32{,}000 \approx 131 million;
  • total per head: roughly 148 million parameters.

The authors use 5 heads. That adds roughly 740 million parameters, about 10 % of a 7-billion-parameter model. In FP16 (2 bytes per parameter) that is around 1.5 GB of extra memory.

Medusa is not free in memory

You often read that Medusa "saves the memory of the draft model". That needs qualifying: 740 million parameters is the order of magnitude of a small draft model. The real benefit lies elsewhere, in not having to obtain or pre-train a second model. The paper cites a competing approach whose draft-model pre-training consumed 275 A100 GPU-hours; Medusa heads train in a few hours on a single card.

Tree verification

The combinatorial problem

Each head proposes not one token but its few best candidates. If each of the 5 heads proposes 5, the number of possible continuations is 555^5, that is 3,125 sequences. Checking them one by one is out of the question: you would have swapped one bottleneck for another.

Candidate tree produced by Medusa from the token Once. The root Once branches into about ten possible continuations (upon, ertain, course, comma, a, once, there, the, was...), each branching again over two further levels, for a total of more than a hundred nodes. One path is drawn in red through the tree: Once then upon then a then comma, the branch finally accepted.
What the combinatorial explosion actually looks like: each level of the tree is one Medusa head, each branch one candidate token. The red path is the only sequence finally accepted, all the rest of the computation is thrown away, which is why verifying everything in a single pass matters.Source : Medusa paper (Cai et al., 2024), via ML Scrapbook

The trick: a tree-shaped attention mask

You arrange the candidates as a tree rather than a list, and rig the attention mechanism to check every branch at once.

A reminder of the mechanism: in a Transformer, each token "looks at" preceding tokens, and an attention mask is the grid saying, for each token, which ones it is allowed to look at. Normally the mask is triangular: token 5 sees tokens 1 through 4.

Medusa replaces that triangular mask with a tree-shaped one: each candidate sees only its ancestors in the tree, never neighbouring branches. You can then lay all the candidates from all the branches end to end in a single input sequence: the mask keeps them from contaminating each other. One traversal of the model suffices to evaluate all 3,125 continuations.

You then keep the longest branch whose every token passes the acceptance test.

Illustration of Medusa's tree attention mask. On the left, a small tree: the root splits into two head-1 candidates (It and I), each splitting into three head-2 candidates (is, apostrophe, the). On the right, the corresponding eight-by-eight attention matrix of queries against keys, with a check mark in each permitted cell: each row activates only the column of its own token and those of its ancestors in the tree, never those of sibling branches.
The mechanism that makes single-pass verification possible. On the left the candidate tree, on the right the attention mask that encodes it: check marks show who is allowed to look at whom. Each token sees only its ancestors, which lets all the branches sit side by side in the same sequence without interfering.Source : Medusa paper (Cai et al., 2024), via Aman's AI Journal

This is not free either: lengthening the input sequence with a hundred-odd candidates costs computation, and the authors measure that overhead. They find that a well-chosen sparse tree of 64 nodes beats a dense tree of 256 nodes: a few good candidates beat many bad ones.

Training the heads: two recipes

Medusa-1, the original model stays frozen

Only the heads learn; the main model's weights do not budge. The loss adds up each head's prediction error:

L=∑kλk⋅CE(headk(h))withλk=0.8 k\mathcal{L} = \sum_{k} \lambda_k \cdot \text{CE}\big(\text{head}_k(h)\big) \qquad \text{with} \qquad \lambda_k = 0.8^{\,k}

The symbols, one at a time. L\mathcal{L} is the loss, the number training tries to make as small as possible. kk numbers the heads (0 for the original head, 1 for the first Medusa head, and so on). CE\text{CE} is cross-entropy, the usual error measure for a word prediction: it grows the lower the probability the model assigned to the word that actually appeared. hh is the internal representation all the heads hang off. λk\lambda_k is a weight that doses each head's importance.

In one sentence: ask every head to guess its token, and count the errors of the further-looking heads for less. Where does the 0.8 k0.8^{\,k} come from? Exactly that dosing: head 1 counts for 0.8, head 2 for 0.64, head 5 for 0.33. It is the authors' choice, not a mathematical necessity: guessing five tokens ahead is very hard, and letting that head weigh as much as the others would drag the whole training down.

What it costs, per the authors: about 5 hours for Medusa-1 on Vicuna-7B, on a single NVIDIA A100 PCIe card, using 60,000 conversations from the public ShareGPT dataset. The main model can even be loaded in quantized form, since it is only used to produce the internal representations.

Medusa-2, train the model too

You tune the heads and the main model together, so the internal representation better suits all the heads. The speed gain is larger, but it takes a cautious recipe (a loss combined with the original task, adjustment via LoRA rather than a full retrain) or you damage the model's capabilities. The authors in fact start from a model already trained under Medusa-1 before moving to Medusa-2.

Acceptance: Medusa is not always exact

An important point, and one usually glossed over. Speculative decoding rests on a strong guarantee: the output is identical to what the large model alone would produce. Medusa can offer the same guarantee (just sort the candidates with the same acceptance-rejection test) and the authors present Medusa-1 as acceleration without loss of generation quality.

But they also propose, and recommend in practice, a different filter they call typical acceptance: instead of the exact statistical test, you accept any candidate whose probability the main model judges "reasonable" against an adjustable threshold. It is faster, especially when generation is highly random, but it no longer preserves the output distribution. The EAGLE authors say so explicitly: Medusa's non-greedy generation does not guarantee losslessness.

The question to ask

Faced with an implementation advertising "Medusa", the right question is not "how many times faster?" but "which acceptance scheme?". With the exact rejection test, the output is the original model's. With typical acceptance, it is no longer quite that, the gap is reputed to be small, but it exists, and a threshold controls it.

Measured results

The authors define three distinct quantities that must not be conflated:

  • the acceleration rate: the average number of tokens produced per decoding step. It is 1.0 for an ordinary model;
  • the overhead: the ratio between the duration of a step with Medusa and one without. It is necessarily above 1, since the input sequence is lengthened with candidates;
  • the actual speedup, which is the quotient of the two: acceleration rate ÷ overhead.

On Vicuna-7B and Vicuna-13B, against the reference Hugging Face implementation, with 5 heads:

ModelMedusa-1Medusa-2
Vicuna-7B2.18×2.83×
Vicuna-13B2.33×2.83×

And the breakdown for the Medusa-2 models, with quality measured on MT-Bench (a set of open-ended questions whose answers are scored 0 to 10 by a judge model; the gap against the original model is in parentheses):

ModelAcceleration rateOverheadMT-Bench quality
Vicuna-7B3.471.226.18 (+0.01)
Zephyr-7B3.141.187.25 (−0.07)
Vicuna-13B3.511.236.43 (−0.14)
Vicuna-33B3.011.277.18 (+0.05)

Source: Cai et al., 2024, figure 4 and table 1. Conditions not to lose sight of: one request at a time, 5 heads, an unoptimized Hugging Face baseline.

Two readings. First, the overhead column explains why the actual speedup is well below the acceleration rate: you gain 3.5 tokens per step, but each step costs 22 % more. Second, speed does not rise with model size: Vicuna-33B has the worst acceleration rate in the table (3.01) and the highest overhead (1.27). The authors attribute this to a mismatch between the data that trained that model and the data they used for its heads.

The gain also depends heavily on the task: across MT-Bench categories, Medusa-2 on Vicuna-7B reaches 3.29× on coding (highly predictable text, where the heads guess well) against considerably less on open-ended writing.

The limits, unvarnished

The heads guess blind

This is the structural weakness. Each head predicts its token without seeing what the other heads predicted. The head aiming at t+3t+3 has no idea which token the t+1t+1 head chose. It is like asking someone for the third word of a sentence without telling them the first two. The inevitable result: the further ahead a head aims, the less reliable it is. A real draft model, by contrast, re-reads itself as it goes.

The heads are welded to their model

The heads must be trained for each model, and retrained after every change to that model. This is not a universal add-on.

Fine-tuning invalidates the heads

If you adjust the main model (via LoRA or otherwise), the distribution of internal representations shifts, and the Medusa heads no longer predict correctly from them. They have to be retrained. That is a dependency worth documenting in any deployment pipeline.

Slotting it into a server is not immediate

The authors note it themselves in a footnote: Medusa works in principle with batching, but integrating it into a serving engine such as vLLM takes additional engineering work. The reason makes sense: the tree mask and the variable length of candidates complicate attention-cache management, which is precisely what those engines optimize most carefully.

What came next: EAGLE

EAGLE (Extrapolation Algorithm for Greater Language-model Efficiency) fixes exactly Medusa's weakness.

  • Instead of independent heads, EAGLE runs a small autoregressive module on the model's internal representations (those of the second-to-last layer, which carry more information than the output probabilities).
  • Each prediction conditions the next: to guess t+3t+3, the module knows what was proposed for t+2t+2. The authors identify that missing information as what was capping accuracy.
  • EAGLE-2 adjusts the candidate tree on the fly according to the model's confidence: it speculates more when sure, less when hesitant.

On LLaMA2-Chat 70B, the EAGLE authors report a 2.7× to 3.5× speedup while preserving the distribution of the generated text.

EAGLE: Speculative Sampling Requires Rethinking Feature Uncertainty (Li et al., 2024)

And multi-token prediction?

Do not confuse Medusa with multi-token prediction, covered in Multi-token prediction. Both add heads, but not at the same moment and not for the same purpose:

  • Medusa grafts heads after the fact, onto an already-trained model, purely to speed up inference;
  • multi-token prediction asks the model to aim several tokens ahead from pre-training onwards, primarily so that it learns better, with faster inference only a side benefit.

What to take away

Medusa illustrates a clear direction: enrich the model rather than multiply models. You avoid having to obtain a draft model, you avoid the behavioural mismatch between two models, and head training is measured in hours on a single card. In exchange: about 10 % more parameters, heads to retrain for every model version, and an acceptance scheme that, in its recommended variant, no longer guarantees identical output. EAGLE takes up the idea while curing the heads' blindness, and is today the most effective variant of this family.

Check your understanding0 / 5

1. You often read that Medusa saves the memory of the draft model. Is that actually true?

2. Why is a Medusa head aiming three tokens ahead less reliable than a genuine draft model aiming at the same position?

3. Five heads each proposing five candidates yield 3,125 possible continuations. How does Medusa check them without swapping one bottleneck for another?

4. An implementation advertises Medusa acceleration with no quality loss. What question should you ask before believing it?

5. Your team fine-tunes a Medusa-equipped model with LoRA. What happens to the acceleration setup?