↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Several tokens per forward

A language model decodes one token per forward pass, and for a single request each pass is limited by memory, not arithmetic: to produce one token, Vicuna-7B reads all 13.5 GB of its float16 weights. Checking several candidate tokens in the same pass costs little more than checking one. Medusa exploits this. It adds a few small heads to the model that guess the tokens two, three, four and more places ahead from the same hidden state, arranges their guesses in a tree, and lets the model check the whole tree in one forward. With greedy acceptance the output is exactly the model's own greedy answer, only produced in fewer passes.

The page records FasterDecoding's five Medusa heads on Vicuna-7B v1.3 answering one chat question greedily, in float16 on the Mac's GPU, and shows every step: the heads' guesses, the candidate tree, its attention mask and the verdicts.

The Medusa heads

Let be Vicuna's last hidden state at the newest position, after the final norm. Its own LM head gives the next token, . Head is one residual feed-forward block followed by its own copy of an LM head, and predicts the token places further on:

The Medusa heads panel lists the top 10 of Vicuna's LM head and of each head at the current step, and draws each head's residual branch next to . No head sees another's guess: head 2's list is the same whichever token head 1 picks, so the heads guess a whole continuation at once and cannot keep it consistent. The five heads add 739 M parameters, stored in bfloat16. In Medusa-1, the version recorded here, they are trained on top of a frozen Vicuna, so the base model and its answers are unchanged.

A tree of candidates

Taking only each head's top guess would be fragile: one wrong head breaks the chain. Medusa instead combines several top candidates per head into a tree. The root is Vicuna's own next token; its children are head 1's candidates, their children head 2's, and so on. The tree's shape is fixed in advance and chosen to cover the combinations most likely to be accepted. This page uses Medusa's vicuna_7b_stage2 tree: 64 tokens, the root and 63 candidates, four levels deep, so head 5 is never checked.

The Candidate tree panel shows that tree at each step, a column per depth, each node marked with the head and rank it came from and coloured by its verdict.

Tree attention

All 64 tokens go through Vicuna in one forward. Node sits at position prefix , and an attention mask lets it see the cached prefix, its own ancestors and itself, and nothing else:

With this mask every root-to-leaf path is scored exactly as if it were the only sequence in the batch, so one forward yields Vicuna's next-token distribution after every node of the tree. The Tree attention panel shades the mask with the attention weights recorded in block 1, averaged over its 32 heads; every weight outside the mask is exactly zero in the recording.

Verification

With greedy decoding, a candidate at depth is accepted when it equals Vicuna's argmax after its parent, and a path is kept up to its first mismatch. Medusa takes the path with the longest accepted prefix, emits the root and the accepted drafts, and uses Vicuna's argmax after the last accepted token as the next step's root, so every forward adds at least one token. The Verify panel shows Vicuna's argmax along the best path; the histogram under it counts how many drafts each of the tree's 42 root-to-leaf paths would have kept.

For sampling, the paper replaces exact matching with a typical acceptance rule that accepts candidates the model finds likely enough; that is faster but no longer reproduces the base model's distribution exactly. The page uses greedy acceptance only.

What it gains on this page

The 75-token answer takes 31 Vicuna forwards after the prompt, 2.42 tokens per forward, where plain greedy decoding takes 75; every token equals greedy's. Four steps kept no draft, 14 kept one, 9 kept two, 4 kept three, and none kept four; the Decoding steps strip shows the count at every step.

Each forward now reads 1.5 GB of head weights on top of Vicuna's 13.5 GB and processes 64 tokens instead of one. Measured on this Mac, a tree step took 355 ms on average against 183 ms for one greedy token, 6.82 against 5.47 tokens per second, but the Mac paged 9.4 GB in while these were timed, which slows greedy's many forwards most; the counted forwards are the fairer comparison. On GPUs, the paper reports over 2.2× speed-up for Medusa-1 and 2.3–3.6× for Medusa-2, which fine-tunes the backbone together with the heads.

Where it came from

Stern, Shazeer and Uszkoreit's blockwise parallel decoding (2018) already added heads that predict several future tokens and kept the longest prefix the model agreed with. Speculative decoding (Leviathan et al.; Chen et al., 2023) used a separate small draft model and a rejection-sampling rule that keeps the target's distribution exactly; the Draft tab shows it. SpecInfer (Miao et al.) verified a tree of drafts with tree attention. Medusa (Cai et al., 2024) put these together without a second model. The EAGLE tab replaces the independent heads with a small model that drafts autoregressively from Vicuna's features, on the same prompt; the Lookahead tab drafts with no extra weights at all.

Papers

  1. Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads Tianle Cai, Yuhong Li, Zhengyang Geng, Hongwu Peng, Jason D. Lee, Deming Chen, Tri Dao · ICML 2024 The method on this page: residual heads on the last hidden state, a fixed candidate tree, tree attention, and Medusa-1 and Medusa-2 training.
  2. Blockwise Parallel Decoding for Deep Autoregressive Models Mitchell Stern, Noam Shazeer, Jakob Uszkoreit · NeurIPS 2018 Extra output heads predict several future tokens and the model keeps the longest prefix it agrees with: Medusa's idea, without a tree.
  3. Fast Inference from Transformers via Speculative Decoding Yaniv Leviathan, Matan Kalman, Yossi Matias · ICML 2023 Draft cheaply, verify in one forward of the large model, and accept with a rule that leaves the output distribution unchanged.
  4. SpecInfer: Accelerating Generative Large Language Model Serving with Tree-based Speculative Inference and Verification Xupeng Miao et al. · ASPLOS 2024 Verifies a whole tree of draft tokens in one forward with a tree-shaped attention mask, as this page's tree attention does.
  5. Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena Lianmin Zheng et al. · NeurIPS Datasets and Benchmarks 2023 The paper Vicuna's model card cites for Vicuna, the LLaMA chat model fine-tuned on ShareGPT conversations that Medusa's heads are trained on here.
  6. Unlocking Efficiency in Large Language Model Inference: A Comprehensive Survey of Speculative Decoding Heming Xia et al. · Findings of ACL 2024 A survey of drafting and verification strategies, placing Medusa, EAGLE and lookahead decoding side by side.

See also