Article

Speculative Decoding and Medusa

#LLM-inference
On this page 9

Accelerating LLM Inference with Speculative Decoding: Notes on Medusa

This post briefly introduces speculative decoding and records what I learned while studying the Medusa method.

Speculative Decoding

Speculative decoding uses a lightweight draft model to generate multiple tokens at once, then asks the original LLM to verify them.

Suppose we generate kk tokens at a time. The cost of the process is approximately

cost=decodingdraft×k+forwardllmcost = decoding_{draft} \times k + forward_{llm}

I use forward rather than decoding for the verification stage because verification can exploit the parallelism of a forward pass, much like training does.

To obtain a speedup, we therefore need:

m×decodingllm>decodingdraft×k+forwardllmm \times decoding_{llm} > decoding_{draft} \times k + forward_{llm}

Here, mm is the number of tokens among the kk candidates that are ultimately accepted. In other words, generating those mm tokens directly with the large model must take longer than drafting and verifying the candidate sequence.

This exposes the two central challenges of speculative decoding:

  • Reduce the overhead.
  • Increase the verification acceptance rate.

The key is therefore to find a suitable draft model.

In general, a smaller draft model introduces less overhead but produces worse predictions, reducing the acceptance rate. A larger draft model may predict more accurately, but its overhead can erase the performance gain.

Finding the right draft model can consequently be expensive. A smaller model from the same family is often a sensible choice, but models that are already in the 7B/13B range may not have a substantially smaller family member. Pretraining a new small model is costly, and without access to the original model’s training distribution it is difficult to match its token probability distribution closely enough to achieve a high acceptance rate.

Moreover, basic speculative decoding samples at the token level: the draft model generates tokens, and the target model verifies them. The lightweight model’s limited accuracy directly constrains the draft-token acceptance rate.


Medusa

One of the largest difficulties in conventional speculative decoding is obtaining a separate small draft model.

An alternative is to reuse the original LLM’s weights and train a small set of additional parameters. These incremental parameters can be viewed as corrections built on top of the original model and can predict draft tokens. This is what Medusa does: it uses multiple LM heads to predict tokens at different future positions.

1. Pipeline

Medusa pipeline

As shown above, different heads use the last hidden state to generate tokens for different future positions.

Define the next token as token 11. At position ii, the number of candidate tokens is a hyperparameter. The method selects the top-sitop\text{-}s_i entries from that head’s logits, producing sis_i candidates. Together, these candidates form a tree.

The original LLM then verifies paths through this candidate tree and emits the accepted output.

2. Medusa Heads

Medusa uses a single-layer feed-forward network with a residual connection for each head:

pt(k)=softmax(W2(k)⋅SiLU(W1(k)⋅ht)+ht),where W2(k)∈Rd×V,  W1(k)∈Rd×dp^{(k)}_t = \text{softmax} \left( W^{(k)}_2 \cdot \text{SiLU} \left( W^{(k)}_1 \cdot h_t \right) + h_t \right), \quad \text{where } W^{(k)}_2 \in \mathbb{R}^{d \times V}, \; W^{(k)}_1 \in \mathbb{R}^{d \times d}

Here, pt(k)p_t^{(k)} is the token distribution predicted by head kk, while pt(0)p_t^{(0)} denotes the probability distribution produced by the model’s own LM head.

3. Tree Attention

Suppose a single speculative step generates candidates for the next KK positions. Because every position is predicted in parallel, the number of possible paths is the Cartesian product

∏i=1Ksi\prod_{i=1}^{K} s_i

That is, a node at position ii may select any of the si+1s_{i+1} candidates at the next position as its child.

At depth ii, the tree contains ∏j=1isj\prod_{j=1}^{i} s_j nodes. A single speculative step must therefore verify ∏i=1Ksi\prod_{i=1}^{K} s_i paths containing a total of

∑i=1K∏j=1isj\sum_{i=1}^{K} \prod_{j=1}^{i} s_j

tokens. This grows explosively. With K=4K=4 and top-8top\text{-}8, there are 4,096 paths and 4,680 tokens in total.

Serial verification would require ∏i=1Ksi\prod_{i=1}^{K} s_i forward passes before selecting the best path, which is far worse than the KK calls required by ordinary decoding. Medusa therefore needs a way to verify all paths in parallel.

The two-dimensional tree must be compressed into a one-dimensional sequence that still preserves the dependency relationships among nodes. Medusa accomplishes this with Tree Attention. Inspired by a causal mask, it applies a mask that prevents tokens on different paths from attending to one another.

Tree-attention mask

In the figure, checked cells indicate attention relationships that remain unmasked.

After computing QTKQ^{T}K, we obtain a matrix whose rows and columns represent all candidate tokens. The attention output is a hidden_state tensor of size

(∑i=1K∏j=1isj)×hidden-dimension\left(\sum_{i=1}^{K} \prod_{j=1}^{i} s_j\right) \times hidden\text{-}dimension

Passing these hidden states through the original LM head produces logit distributions. The LLM then applies its own sampling method, whether greedy decoding or sampling: the last hidden state passes through the LM head, softmax produces a probability distribution, and one token is sampled. Medusa checks whether this token exists among its first-level candidates. If it does, verification continues along the corresponding branch at the next level. This process repeats until it finds a valid path or reaches a level with no matching candidate.

4. Optimized Tree Construction

To reduce verification overhead, Medusa optimizes the verification sequence by pruning low-acceptance-probability paths from the draft tree in advance.

The paper uses a greedy algorithm. Given a fixed total number of tree nodes, it adds tokens one by one, always choosing the token with the largest contribution to the expected number of accepted tokens. The process is as follows:

  1. Choose a calibration dataset and calculate the accuracy of the ii-th ranked predicted token from head kk. This should be empirical accuracy: the number of correct predictions divided by the total number of samples. Denote it by ak(i)a_k^{(i)}.

  2. After obtaining each node’s accuracy, assume that these accuracies are independent. For a path composed of candidates [i1,i2,…,ik][i_1,i_2,\ldots,i_k], the estimated path probability is the product ∏j=1kajij\prod_{j=1}^{k}a_j^{i_j}.

  3. Define a set II whose elements correspond to every node in the tree, not only its leaves. Each element is the path [i1,i2,…,im][i_1,i_2,\ldots,i_m] leading to that node. If the node is at depth mm and the total number of accepted tokens is LL, then

P(L≥m)=∑[i1,i2,…,im]∈I∏j=1majijP(L \geq m)=\sum_{[i_1,i_2,\ldots,i_m]\in I}\prod_{j=1}^{m}a_j^{i_j}

  1. The expected number of accepted tokens is

E[L]=∑i=1KP(L≥i)=∑[i1,i2,…,ik]∈I∏j=1kajij,k∈[1,2,…,K]E[L]=\sum_{i=1}^{K}P(L\geq i)=\sum_{[i_1,i_2,\ldots,i_k]\in I}\prod_{j=1}^{k}a_j^{i_j}, \quad k \in [1,2,\ldots,K]

With the node count fixed, maximizing the expectation means selecting high-accuracy nodes. The resulting pruning procedure can produce a 64-node sparse tree like the one below.

A 64-node sparse tree

5. Training

The paper reports that tuning the LLM alongside the Medusa heads performs better than training the heads alone.

1) Training the Heads Alone (Frozen Backbone)

The loss is computed with cross-entropy:

Lk=−log⁡ptk(yt+k+1)\mathcal{L}_{k} = - \log p_t^{k}(y_{t+k+1})

Here, ptk(y)p_t^{k}(y) is the probability that head kk assigns to the correct token yy.

As kk increases, Lk\mathcal{L}_{k} also tends to increase. Because all heads generate in parallel, tokens farther into the future are harder to predict. Medusa therefore introduces an additional weight λk\lambda_k to balance the loss across heads:

LMEDUSA−1=∑k=1K−λklog⁡pt(k)(yt+k+1)\mathcal{L}_{MEDUSA-1} = \sum_{k=1}^{K} - \lambda_{k} \log p_t^{(k)}(y_{t+k+1})

The paper uses 0.8k0.8^k for λk\lambda_k.

2) Joint Training

  • Combined loss: Add the LLM loss and introduce a weight λ0\lambda_0 to balance it against the Medusa loss:

    LMEDUSA−2=LLM+λ0LMEDUSA−1\mathcal{L}_{MEDUSA-2} = \mathcal{L}_{LM} + \lambda_{0} \mathcal{L}_{MEDUSA-1}

  • Different learning rates: Because the LLM is already trained, its learning rate should be much smaller than the learning rate used for the heads.

  • Head warm-up: The heads have a large loss at the beginning of training. Training them alone for a short warm-up period prevents their large gradients from disrupting backpropagation through the LLM.

6. Summary

Medusa is a highly practical method. Its heads are extremely lightweight, yet they can produce a meaningful speedup.

Acceleration results

The draft tokens are still generated in parallel and cannot use information from earlier drafted tokens. I therefore expect its acceptance rate to be lower than that of EAGLE, which I plan to study next.