Speculative Decoding

A small model guesses the next few tokens. The big model checks all of them in one pass. On T5-XXL this ran 3.4× faster, and the output did not change.

Pick a draft model, then move k, the number of guesses per round. Watch both decoders spend the same time and count the tokens each one writes. Then we build, piece by piece, the idle GPU, the keep rule, the speedup formula, and the methods that draft without a second model.

You need what a probability is and the idea that a language model writes one token at a time. We build the rest from zero.

Built on Leviathan, Kalman and Matias, Fast Inference from Transformers via Speculative Decoding (ICML 2023), and Chen et al., Accelerating Large Language Model Decoding with Speculative Sampling (2023). Part of the Inference Engineering family with Serving Engines and Cost Modeling.

Race two decoders

7

The acceptance rates (0.75, 0.62, 0.71) and draft costs (0.02, 0.02, 0.11 of a target pass) are Leviathan et al.'s English to German runs with T5-XXL as the target (Table 4). Which guesses pass in each round is a random draw.

Chapter 0

The Idle GPU

Find out why one decode step leaves almost all of the chip's arithmetic unused

A chat model streams a reply to one user. You watch the GPU. Its arithmetic units sit almost idle, step after step. Nothing is broken. This is how decoding works.

To write one token, the GPU must read every weight of the model from its memory. Take a 7-billion-parameter model in 16-bit numbers on one A100 GPU. The weights fill 14 GB. The A100 reads about 2 TB per second. So one step costs a fixed time before any multiply starts:

14 GB ÷ 2 TB/s = 7 ms per token, or about 143 tokens per second.

The arithmetic is small. One token needs about 2 operations per weight, so 14 billion operations. The A100 can do 312 trillion per second. That work takes 0.045 ms of the 7 ms. The chip uses about 0.64% of its arithmetic. A step like this is memory-bound: the time to move bytes sets its speed, not the time to compute.

Fill the idle pass

Yellow is the fixed time to read the weights. Blue is the time to compute. Drag the number of token positions one pass must check.

5

A100 80GB rated figures (about 2 TB/s, 312 dense 16-bit TFLOPS) and the 2-operations-per-weight rule. The model ignores the attention and KV-cache reads, so it is a best case.

One pass reads the weights once, for any number of positions. So how many positions fit before compute becomes the slow part? Call the chip's peak arithmetic rate F, its memory speed BW, and the bytes per weight b. Call the number of positions that fit in the memory time N*.

Say it aloud

N-star equals F times b, divided by two times BW.

N*
N-star the idle-compute budget: how many token positions one pass can compute in the time it spends reading weights.
F
F the chip's peak arithmetic rate, in operations per second.
b
b bytes per weight: 2 for 16-bit numbers.
BW
BW memory bandwidth, in bytes per second.
2
two operations per weight per token: one multiply, one add.
In plain words

Divide the arithmetic rate F by the memory speed BW. Multiply by the bytes b each weight costs to read. Divide by the 2 operations each weight costs per position. That is N*.

With numbers

A100: F = 312 × 1012, b = 2, BW = 2 × 1012. N* = (312 × 1012 × 2) / (2 × 2 × 1012) = 156 positions. One pass can check 156 positions for the price of one.

The idea of this lesson: guess the next few tokens cheaply, then check all the guesses in one pass of the big model. The check costs about one step. Each right guess is a token for free.

Failure. The symptom is a speedup that vanishes under load. Measure the positions per pass across the whole batch. If they near N*, the pass is no longer free. Chapter 9 gives the fix.

  1. Draft, then verify: one round of guess and check, and the bonus token.
  2. The keep rule: when to keep a guess, and what to draw when you reject it.
  3. Why the output is exact: the proof, and the shortcut that breaks it.
  4. Tokens per round: the expected count from one acceptance rate.
  5. Speedup and break-even: the hero's formula, and the best k.
  6. Choosing a draft: a small model, prompt lookup, or the target's own early layers.
  7. Medusa and EAGLE: drafting inside the target, and tree attention.
  8. Lookahead decoding: no draft at all, only parallel guesses.
  9. Speculation meets the batch: when other users take the idle budget.
  10. Field guide: numbers, code, checklist and links.
One decode step for one user uses under 1% of the A100's arithmetic. Why does a chip with twice the TFLOPS, but the same memory bandwidth, not make the step faster?

Chapter 1

Draft, Then Verify

Run one round: a cheap model guesses k tokens, the big model checks them all at once

Two models share the work. The target model is the big model whose output you want. The draft model is a much smaller model that guesses ahead. One round goes like this:

  1. The draft writes k tokens, one at a time. It is small, so this is cheap.
  2. The target runs once over the prefix plus all k guesses.
  3. Going left to right, the target keeps each guess or rejects it. The first rejection ends the round.
  4. The target adds one token of its own. After a rejection, it is a correction at that place. If the target kept every guess, it is a bonus token after the last one.

Why can one pass check k guesses? A transformer computes a next-token distribution at every position of its input, in parallel. Feed it the prefix plus k guesses and it returns k + 1 distributions. Each one is what the target would say after that much text.

Run a round

Purple boxes are the draft's guesses. Teal is a guess the target keeps, red a guess it rejects, warm the target's own token. Press Run a round.

0.70
4

Illustrative: each guess passes with the same chance α, independent of the others. Real acceptance varies token by token.

Worked round, k = 4. The draft guesses "sat by the cold". The target keeps "sat", "by" and "the", and rejects "cold". It writes its own token, "warm", at that place. The round adds 4 tokens for one target pass. If it had kept all 4 guesses, it would add a fifth, the bonus.

Data flow in the target pass: the input is the token ids, shape [k + 1]. The output is logits, shape [k + 1, V], where V is the vocabulary size. Row i is the target's distribution after the prefix plus i guesses.

Failure. Symptom: rounds add one token most of the time. Measure the share of rounds that end on the first guess. If it is high, the draft disagrees with the target too often. Fix it with a better draft (Chapter 6) or a smaller k (Chapter 5).

The draft proposes 4 tokens and the target keeps all 4. How many tokens does the round add?

Chapter 2

The Keep Rule

Decide when to keep a guess, and what to draw when you reject one

Most servers sample: they draw the next token at random from the model's distribution. So "the token the target would pick" is not one fixed token. We need a rule that keeps the target's odds intact.

Take a vocabulary of three tokens, A, B and C. At this place the target gives each token a probability. Call the target's probability of token x p(x). The draft gives its own probability, q(x). The draft has drawn token x. Call the chance we keep it a(x).

Say it aloud

a of x equals the smaller of one and p of x over q of x.

a(x)
a of x the chance we keep the drafted token x.
min
the smaller of pick the lower of the two values, so a chance never passes 1.
p(x)
p of x the target's probability of token x here.
q(x)
q of x the draft's probability of token x here.
In plain words

If the target likes x at least as much as the draft (p(x) ≥ q(x)), keep it always. If the draft likes x more, keep it with chance p(x) / q(x).

With numbers

With made-up numbers: p = (0.5, 0.3, 0.2), q = (0.4, 0.4, 0.2). a(A) = min(1, 0.5 / 0.4) = 1. a(B) = min(1, 0.3 / 0.4) = 0.75. a(C) = min(1, 0.2 / 0.2) = 1. Only B, which the draft likes too much, is ever rejected.

A rejection means the draft gave too few draws to some token. We redraw from the target's leftover: the probability the target has and the draft did not cover. Call the redraw distribution p′(x), and the total leftover Z.

Say it aloud

p-prime of x equals the larger of zero and p of x minus q of x, divided by Z.

p′(x)
p-prime of x the chance of drawing x after a rejection.
max(0, …)
the larger of zero and a negative leftover counts as zero.
p(x)
p of x the target's probability of x.
q(x)
q of x the draft's probability of x.
Z
Z the sum of the leftovers over all tokens, so the p-primes add to 1.
In plain words

For each token, subtract q(x) from p(x). Set any negative result to zero. Divide each leftover by their sum Z. Draw from that.

With numbers

Same numbers: leftovers 0.5 − 0.4 = 0.1, 0.3 − 0.4 → 0, 0.2 − 0.2 = 0. Z = 0.1. So p′ = (1, 0, 0): a rejected B always becomes A.

Run the keep rule

Left: target p in blue, draft q in purple, with each token's keep chance. Right: what comes out, teal from kept guesses, warm from redraws. Pick a draft, then draw.

The three-token p and q are toy numbers. The rule is the one in Leviathan et al. (Section 2.3) and Chen et al. (Algorithm 2).

Failure. Symptom: outputs from the speculative server read different from the plain server at the same temperature. Measure token frequencies from both on the same prompts. If they differ, the code skips the redraw or redraws from p instead of p′. Fix the rejection branch.

Target p(B) = 0.3, draft q(B) = 0.4. The draft drew B. What is the chance the target keeps it?

Chapter 3

Why the Output Is Exact

Prove that the keep rule gives the target's distribution, and see the shortcut that breaks it

The device in Chapter 2 converged to p. Now prove it holds for every p and q. First, one number that matters for the rest of the lesson. For each token, the smaller of p(x) and q(x) is the probability the two models agree on. Call the sum of these overlaps α, the acceptance rate.

Say it aloud

alpha equals the sum over x of the smaller of p of x and q of x.

α
alpha the chance that one drafted token is kept, averaged over what the draft might draw. From 0 to 1.
Σx
the sum over x add one term for each token in the vocabulary.
min
the smaller of the overlap of the two models at token x.
p(x)
p of x the target's probability of x.
q(x)
q of x the draft's probability of x.
In plain words

For each token, take the lower of p(x) and q(x). Add them up. The total area the two distributions share is α.

With numbers

p = (0.5, 0.3, 0.2), q = (0.4, 0.4, 0.2): α = 0.4 + 0.3 + 0.2 = 0.9. The target keeps nine draws in ten. The total leftover Z from Chapter 2 is 1 − α = 0.1.

A token x can come out two ways. The draft drew x and we kept it: chance q(x) × a(x) = min(p(x), q(x)). Or the draft drew something we rejected, chance 1 − α, and the redraw gave x, chance p′(x). Call the chance x comes out P(out = x).

Say it aloud

P of out equals x equals the smaller of p of x and q of x, plus one minus alpha times p-prime of x, and that equals p of x.

P(out = x)
P of out equals x the chance the round emits token x at this place.
min(p(x), q(x))
the smaller of p of x and q of x the chance the draft drew x and we kept it.
1 − α
one minus alpha the chance of a rejection.
p′(x)
p-prime of x the redraw's chance of x: max(0, p(x) − q(x)) / (1 − α).
p(x)
p of x the target's own probability: the goal.
In plain words

The 1 − α cancels the Z inside p′(x), leaving max(0, p(x) − q(x)). Add min(p(x), q(x)). If p(x) is larger, you get q(x) + p(x) − q(x). If q(x) is larger, you get p(x) + 0. Both are p(x).

With numbers

Token A: 0.4 + 0.1 × 1 = 0.5. Token B: 0.3 + 0.1 × 0 = 0.3. Token C: 0.2 + 0 = 0.2. The output is (0.5, 0.3, 0.2), which is p exactly, though q differs at A and B.

A round of k guesses repeats this check at each place, given the tokens kept so far. Each step is an exact draw from the target, so the whole kept sequence is too. Leviathan et al. prove this in their Appendix A.1.

Break the rule

Left: teal is the overlap α, warm the leftover the redraw fills, red the draft's excess, which the rule rejects. Right: what each rule outputs, against p. Pick a rule.

Exact probabilities, not samples. "Greedy match" keeps a guess only if it is the target's top token and otherwise emits the top token.

Failure. The greedy-match shortcut is the common bug. Symptom: answers lose variety, even at temperature 1. Measure the distinct outputs over repeated samples of one prompt. Fix: use the keep rule whenever the server samples.

The target samples at temperature 1 with p = (0.5, 0.3, 0.2). A server keeps a guess only if it equals the target's top token, and otherwise emits the top token. What comes out?

Chapter 4

Tokens per Round

Count how many tokens one target pass gives on average, from one acceptance rate

A round with k guesses gives between 1 and k + 1 tokens. How many on average? Assume each guess is kept with the same chance α, independent of the others. This is the simplifying assumption in Leviathan et al.

The round reaches guess number i only if the first i guesses were all kept. That has chance α multiplied by itself i times. Each guess reached and kept adds one token, and the target always adds one more. Call the average tokens per round E and the number of guesses k.

E = 1 + α + α2 + … + αk

Multiply that sum by α and subtract. Every middle term cancels, leaving a short closed form.

Say it aloud

E equals one minus alpha to the k plus one, over one minus alpha.

E
E the average number of tokens one round adds.
αk+1
alpha to the k plus one α multiplied by itself k + 1 times; k is the number of guesses per round.
α
alpha the chance each guess is kept, from 0 to just under 1.
In plain words

Raise α to the power k + 1. Subtract that from 1. Divide by 1 − α. As k grows, E climbs toward 1 / (1 − α) and stops.

With numbers

With made-up numbers: α = 0.7, k = 4. α5 = 0.168. E = (1 − 0.168) / 0.3 = 2.77 tokens per round. The ceiling is 1 / 0.3 = 3.33 for any k.

Count tokens per round

Bars are how many rounds gave 1, 2, …, k + 1 tokens. Dots are what the formula predicts. Set α and k, then run.

0.70
4

Simulated with independent guesses. Real guesses are not independent, which is why the formula is an estimate.

Read the bars at α = 0.7, k = 4. The tallest bar is 1 token: 30% of rounds fail on the first guess. The last bar, 5 tokens, holds 24%: rounds that kept all four guesses and got the bonus. The shape is a capped geometric: each bar is α times the one before, and the last bar collects the rest.

Failure. Symptom: measured tokens per round sit well below the formula. Measure α at each position in the round. If later guesses pass less often, the independence assumption fails. Use the per-position rates in place of one α.

With α = 0.5, what is the most tokens per round you can expect on average, however large you make k?

Chapter 5

Speedup and Break-Even

Divide tokens per round by the cost of a round, and find the best number of guesses

Tokens per round is half of the answer. A round also costs more than a plain step, because the draft runs k times first. Measure every time in units of one target pass. Call the time of one draft step c, the cost ratio. A round costs 1 + k × c target passes. Call the speedup over plain decoding S.

Say it aloud

S equals E over one plus k times c.

S
S the speedup: tokens per second with speculation, divided by tokens per second without.
E
E average tokens per round, (1 − αk+1) / (1 − α) from Chapter 4.
k
k guesses per round.
c
c the time of one draft step as a fraction of one target pass.
In plain words

Work out E from α and k. Work out the round's cost: one target pass plus k draft steps of c each. Divide E by that cost. Above 1 is a win; below 1 is a slowdown.

With numbers

Leviathan et al., T5-small drafting for T5-XXL: α = 0.75, c = 0.02, k = 7. E = (1 − 0.758) / 0.25 = 3.60. Cost = 1 + 7 × 0.02 = 1.14. S = 3.60 / 1.14 = 3.16. The paper predicted 3.2× and measured 3.4× (Table 4).

The formula is Theorem 3.8 of Leviathan et al. More guesses raise E, but E stops near 1 / (1 − α) while the cost keeps growing by c per guess. So S has a peak.

Find the best k

The curve is the speedup S at each k. The dashed line is 1, plain decoding; red below it is a slowdown. Move α and c.

0.70
0.10

Theorem 3.8 of Leviathan et al. under the same independence assumption. The default c = 0.10 is a draft about one tenth the target's size.

αE at k = 4S at k = 4, c = 0.1Best kS at best k
0.31.431.0211.18
0.51.941.3821.46
0.72.771.9841.98
0.94.102.9310 or more3.43

Failure. A weak draft with a large k is slower than no draft. With α = 0.15, k = 8 and c = 0.1: E = 1.18, cost = 1.8, S = 0.65. Measure α on real traffic before you set k. If α drops, lower k or turn speculation off.

α = 0.5 and c = 0.1. Why does k = 16 give a lower speedup than k = 2?

Chapter 6

Choosing a Draft

Compare three places to get guesses, and match each one to the traffic it suits

The draft sets two numbers in the speedup formula: α, how often its guesses pass, and c, what each guess costs. You cannot move one without the other. A bigger draft guesses better and costs more.

Draft sourceHow it guessesCost cWhere it wins
Small modelA model of the same family, 10 to 100 times smaller, runs ahead0.02 to 0.1Any traffic; needs a second model to host and keep in step
Prompt lookupFind the last few tokens earlier in the prompt and copy what followednear 0Code edits, summaries and RAG answers that quote their input
Skip layersThe target itself with some layers skipped drafts for the full target0.2 to 0.5No second model at all; smaller gains

Leviathan et al. measured α across T5, LaMDA and a small GPT. Drafts about two orders of magnitude smaller than the target gave α between 0.5 and 0.9 (Section 4.2). Chen et al. reported 2 to 2.5× on Chinchilla 70B with a small draft model. Prompt lookup (Saxena, 2023) uses no model: it matches text. Skipping layers is the idea of Draft & Verify (Zhang et al., 2023), which reports up to 1.99× on LLaMA-2. The cost column gives rough ranges, not measurements.

Match the draft to the traffic

Each bar is a draft source's speedup at k = 4, from the formula in Chapter 5. Pick the traffic.

Illustrative α and c for each pair, chosen to show the pattern. Measure your own α before you choose.

Worked number, code edits: prompt lookup with α = 0.82 and c = 0.002. E = (1 − 0.825) / 0.18 = 3.50, cost = 1.008, S = 3.47. On fiction the same lookup finds few matches. With α = 0.05, S = 1.04: almost nothing, but almost no loss, because the guesses were free.

Failure. Symptom: latency creeps up over months with no code change. Measure α per traffic type every week. A small draft trained against an old target checkpoint drifts away from the new one. Fix: retrain the draft with each target release.

Why can prompt lookup never slow decoding much, even when its guesses rarely pass?

Chapter 7

Medusa and EAGLE: Drafting Inside the Target

Draft from the target's own hidden state, and check a whole tree of guesses in one pass

A second model is a second thing to host, train and keep in step. Medusa and EAGLE remove it. They read the target's last hidden state, the vector it computes just before its output head, and guess from that.

Medusa (Cai et al., 2024) adds K extra heads on that hidden state. Head 1 guesses the token two places ahead, head 2 three places ahead, and so on. Each head is one small block: a d × d layer with a skip connection, then a d × V output layer. That output layer starts as a copy of the target's own head. For a 7B model (d = 4,096, V = 32,000) that is about 148 million weights per head.

Each head offers its top s guesses, so the guesses form a tree. To check every branch in one pass, the target uses tree attention. A mask lets each node see only the nodes on its own path back to the root.

Read the tree mask

Left: a tree of 2 heads with 2 guesses each. Right: the attention mask, one row per node. Tap a node, or press Next node.

Toy words. The masking rule is the one in the Medusa paper: attention applies only to a token's predecessors.

A full tree grows fast: s + s2 + … + sK nodes. With 4 heads and 4 guesses each, that is 340 positions, over the 156 budget from Chapter 0. Medusa keeps a sparse tree of 64 nodes, depth 4, chosen offline. In the paper it beat a dense tree of 256 nodes.

Medusa reports over 2.2× with a frozen target (Medusa-1) and 2.3 to 3.6× when the target is fine-tuned with the heads (Medusa-2). Its default typical acceptance keeps a guess if the target gives it enough probability. That is faster but not exact. The keep rule of Chapter 2 is the exact option.

EAGLE versionWhat it changesReported speedup
EAGLE (Li et al., 2024)One decoder layer predicts the next hidden state from hidden states plus tokens one step ahead; the target's frozen head turns it into a token. 0.24B weights for a 7B target.2.7 to 3.5× on LLaMA2-Chat 70B, output distribution kept
EAGLE-2Grows the tree per context, using the draft's own confidence as a guide3.05 to 4.26×, 20 to 40% over EAGLE
EAGLE-3Predicts tokens directly and mixes hidden states from several target layersUp to 6.5×, about 1.4× over EAGLE-2

Failure. Symptom: Medusa answers differ in tone from the target's samples. Measure output statistics with typical acceptance on and off. If they differ, switch to the exact keep rule where the output distribution matters.

In tree attention, which positions can the node "sat", on the branch root → "dog" → "sat", attend to?

Chapter 8

Lookahead Decoding

Guess a window of future tokens, fix them in parallel, and keep the phrases that appear

What if there is no draft at all? Fill a window of W future places with guesses. Run the target once over all of them. Each place gets a new token, computed from the guesses to its left. Repeat. This is Jacobi iteration, an old way to solve many equations in parallel.

One place is always right after a pass: the first, because the target has confirmed the text before it. A later place becomes right once every place before it is right. So plain Jacobi often fixes one token per pass, no better than normal decoding.

Lookahead decoding (Fu, Bailis, Stoica and Zhang, 2024) adds a pool. As the window changes, short phrases (n-grams) appear in it. The method stores them. Later, when the text reaches a phrase's first token, it offers the phrase as guesses and checks it in the same pass. Correct phrases fix several places at once.

Fix the window

Each box is a place in the window. Teal is fixed for good, purple is still a guess. Press One pass. Turn the pool on and compare the passes needed.

A toy with a greedy target that always continues one fixed sentence. The real method runs a 2-D window and a separate check branch in one pass.

Worked number: a window of 8 places, filled in 3 passes, gives 8 tokens for 3 target passes, so 8 / 3 = 2.67 tokens per pass. Each pass computes more positions than a plain step, so the gain in time is less than 2.67 when the budget is tight.

The paper reports up to 1.8× on MT-bench and up to 4× on code completion when it spreads the work across several GPUs. The output matches the target's greedy decoding.

Failure. Symptom: lookahead gives nothing on short, varied replies. Measure the pool's hit rate. If it is near zero, the text has few repeated phrases. Expect gains on long outputs and code, not on short chat.

Why does plain Jacobi iteration often fix only one token per pass on a language model?

Chapter 9

Speculation Meets the Batch

Share the idle budget between real users and guesses, and throttle speculation as the batch fills

A server rarely serves one user. Continuous batching puts many users' tokens into the same pass (see Serving Engines). Each user in the batch spends part of the same idle budget N* from Chapter 0. Speculation spends it too.

Call the number of users in the batch B. If each one checks k guesses, each needs k + 1 positions per pass. The pass stays near its memory time only while the total fits in N*.

Say it aloud

B times k plus one is at most N-star.

B
B the number of users decoding in the same pass.
k
k guesses each user checks per round; k + 1 positions with the target's own token.
≤
is at most the condition for the pass to stay memory-bound.
N*
N-star the idle-compute budget: 156 on the A100 example.
In plain words

Multiply the users B by the positions each needs, k + 1. Compare with N*. Below it, guesses are nearly free. Above it, the pass slows down for every user.

With numbers

B = 20, k = 4: 20 × 5 = 100 ≤ 156, so speculation is nearly free. B = 60, k = 4: 300 positions, almost twice the budget. The largest batch that fits k = 4 is 156 / 5 = 31 users.

Share the budget

The bar is positions per pass: blue for the users' own tokens, purple for guesses. The yellow line is N* = 156. Move B and k.

20
4

Illustrative: α = 0.7 and c = 0.1 for every user, the 7 ms A100 step, and no KV-cache reads. Real batches also read each user's cache, so the budget is smaller.

The fix is a rule on the scheduler: read B every step and set k from it. With many free positions, speculate with a large k. Near the line, use a small k or only free drafts such as prompt lookup. Far past the line, turn speculation off. EAGLE-3 reports a 1.38× throughput gain at batch size 64 in SGLang, so the line depends on the method and the cost of its guesses.

Failure. Symptom: p99 latency rises for all users at peak hours after the team turns speculation on. Measure B × (k + 1) against N* over the day. If peaks cross it, throttle k by B.

A server runs B = 40 users. Each speculates with k = 4. With N* = 156, what happens?

Chapter 10

Field Guide

Keep the terms, numbers, code and checks in one place

A decode step reads all the weights to make one token, so its arithmetic sits idle. Speculative decoding fills that idle arithmetic with guesses. The keep rule makes the output exact. Two numbers, α and c, decide the speedup, and the batch decides how much budget is left.

TermMeaningNumber to remember
Memory-bound stepTime set by reading weights, not by arithmetic7B on an A100: 7 ms, 0.64% of compute used
N*Positions one pass can compute for the price of one156 on the A100 in 16-bit
Keep ruleKeep x with chance min(1, p(x) / q(x)); else redraw from the leftoverp = (0.5, 0.3, 0.2), q = (0.4, 0.4, 0.2): keep B 75%
αChance a guess is kept: the overlap Σ min(p, q)0.5 to 0.9 for drafts 100× smaller (Leviathan et al.)
ETokens per round: (1 − αk+1) / (1 − α)α = 0.7, k = 4: 2.77
SSpeedup: E / (1 + k c)T5-small for T5-XXL: 3.2× predicted, 3.4× measured
MedusaExtra heads on the target, a 64-node tree, typical acceptance2.2× (Medusa-1), 2.3 to 3.6× (Medusa-2)
EAGLE familyA one-layer draft on the target's hidden states2.7 to 3.5×; EAGLE-2 up to 4.26×; EAGLE-3 up to 6.5×
LookaheadJacobi window plus an n-gram pool; no draftUp to 1.8× on MT-bench
Batch budgetB × (k + 1) ≤ N*k = 4 fits up to 31 users

The core, as code

pythonimport numpy as np

def speculative_round(prefix, draft, target, k, rng):
    # 1. draft k tokens, one at a time; keep each draft distribution q_i (shape [V])
    guesses, qs = [], []
    for _ in range(k):
        q = draft(prefix + guesses)
        guesses.append(rng.choice(len(q), p=q)); qs.append(q)
    # 2. ONE target pass over prefix + guesses: k + 1 distributions, shape [k + 1, V]
    ps = target(prefix, guesses)
    out = []
    for i, x in enumerate(guesses):
        p, q = ps[i], qs[i]
        if rng.random() < min(1.0, p[x] / q[x]):     # the keep rule
            out.append(x)
        else:
            left = np.maximum(p - q, 0)                 # the leftover
            out.append(rng.choice(len(p), p=left / left.sum()))
            return out                                   # correction ends the round
    out.append(rng.choice(len(ps[k]), p=ps[k]))       # all kept: the bonus token
    return out

def speedup(alpha, k, c):
    E = (1 - alpha ** (k + 1)) / (1 - alpha)
    return E / (1 + k * c)                         # speedup(0.75, 7, 0.02) = 3.16

Before you turn it on

  1. Measure α on your own traffic, per traffic type. Never take it from a table.
  2. Pick k from the formula for that α and c. Recheck when α moves.
  3. Use the exact keep rule if outputs must match the target's samples.
  4. Log tokens per round and the share of rounds that end on the first guess.
  5. Throttle k by batch size so that B × (k + 1) stays under N*.
  6. Retrain any learned draft (small model, Medusa heads, EAGLE layer) with each target release.

Keep going

Sources

  1. Leviathan, Kalman, Matias. "Fast Inference from Transformers via Speculative Decoding." ICML 2023. arXiv:2211.17192
  2. Chen, Borgeaud, Irving, Lespiau, Sifre, Jumper. "Accelerating Large Language Model Decoding with Speculative Sampling." 2023. arXiv:2302.01318
  3. Cai et al. "Medusa: Simple LLM Inference Acceleration Framework with Multiple Decoding Heads." 2024. arXiv:2401.10774
  4. Li, Wei, Zhang, Zhang. "EAGLE: Speculative Sampling Requires Rethinking Feature Uncertainty." 2024. arXiv:2401.15077; EAGLE-2 arXiv:2406.16858; EAGLE-3 arXiv:2503.01840
  5. Fu, Bailis, Stoica, Zhang. "Break the Sequential Dependency of LLM Inference Using Lookahead Decoding." 2024. arXiv:2402.02057
  6. Zhang et al. "Draft & Verify: Lossless Large Language Model Acceleration via Self-Speculative Decoding." 2023. arXiv:2309.08168
  7. Saxena. Prompt lookup decoding. 2023. GitHub
The takeaway. Guess cheaply, check in one pass, keep with chance min(1, p / q). The speed comes from α and c; the correctness comes from the rule.

Now press Present or Teach and explain from memory: why the step is memory-bound, the keep rule and its proof, and the speedup formula. Then go back to the race and predict the speedup before you move k.

Speculative Decoding
Leviathan et al. 2023 · Chen et al. 2023 · Back to Gleams