7 Inference and Serving 8 / 8
  1. In this chapter
  2. Your first inference service
  3. Decoding and Sampling
  4. KV Cache and Inference Performance
  5. Prefix Caching and PD Disaggregation
  6. Model Quantization
  7. LLM Inference Serving
  8. Continuous Batching and PagedAttention
  9. Speculative Decoding
All chapters →

Chapter 7 · Inference and Serving 7.8

Speculative Decoding: Draft with a Small Model, Verify with a Large One

Speculative decoding lets a small, fast draft model guess the next few tokens and the large model verify them in one parallel forward pass, with an accept-and-correct rule that keeps the output distribution unchanged. This page derives the expected speedup and lets you tune it interactively.

Intermediate 15 min read Updated Companion code ↓

After this page you will be able to

  • Explain why decoding is slow and how guess, verify and correct speeds it up.
  • Explain why the accept-and-resample rule leaves the output distribution unchanged, and check it in code.
  • Derive the expected yield and speedup, and use the interactive lab to see how acceptance rate and draft length move them.
  • Say where drafts come from and when speculative decoding does not pay off.
On this page
  1. Why decoding is slow, and why it can be sped up
  2. The core idea: guess, verify, correct
  3. Why the output distribution does not change
  4. How much faster: expected output and speedup
  5. Hands-on: the speculative decoding lab
  6. Where the draft comes from
  7. When it does not pay off
  8. Hands-on code: verify “the distribution is unchanged” and the expected output
  9. Summary and exercises
  10. Quick self-check
  11. Further reading
  12. FAQ

Speculative decoding lets a small, fast draft model guess the next γ\gamma tokens, then lets the large model verify them in a single parallel forward pass. An “accept with a probability, resample from a corrected distribution on rejection” rule guarantees that the output distribution is exactly the same as using the large model alone. The speedup is lossless: each round yields at least 1 token, and with luck γ+1\gamma+1. The actual gain is set by two quantities, the probability α\alpha that a draft token is accepted and the time ratio cc of the draft model to the large model.

Why decoding is slow, and why it can be sped up

Autoregressive decoding produces one token at a time, and the next step has to wait for the previous one. At small batch sizes the bottleneck is not compute but moving all the weights from GPU memory into the compute units: every generated token reads the whole model once.

Take a 7B-parameter BF16 model decoding on an A100 80GB SXM (batch size 1, ignoring the reads of attention and the KV cache and kernel overheads):

QuantityValueHow it is obtained
BF16 weight size14 GB7×1097\times10^{9} parameters × 2 bytes
Memory bandwidth2,039 GB/sNVIDIA A100 datasheet (80GB SXM)
BF16 dense compute312 TFLOPSNVIDIA A100 datasheet
Time to read the weights once6.87 ms, an upper bound of about 146 tokens/s14 GB ÷ 2,039 GB/s
Compute time for one token0.045 ms2×7×1092\times7\times10^{9} FLOP ÷ 312 TFLOPS
Compute time for 5 positions at once0.22 ms5 × 0.045 ms

Reading the weights takes 6.87 ms while the compute for one token takes only 0.045 ms, a gap of about 153 times (the ratio of compute to bandwidth is 312×1012/2039×109≈153312\times10^{12}/2039\times10^{9}\approx153 FLOP/byte). Since the weights have to be read anyway, letting the large model process γ+1\gamma+1 positions on that same pass is almost free. Speculative decoding finds positions worth processing together: a cheap draft model guesses a string of candidates and the large model verifies them in one go. The “ratio of compute to bandwidth” here is the ridge point of the roofline model, derived in GPUs and the Roofline Model.

The core idea: guess, verify, correct

One round of speculative decoding goes like this:

  1. Guess: the draft model autoregressively proposes γ\gamma candidate tokens x1,…,xγx_1,\dots,x_\gamma and records the probability qiq_i it gave to each.
  2. Verify: the large model concatenates the prefix with the γ\gamma candidates and runs one forward pass, which yields the distributions p1,…,pγ+1p_1,\dots,p_{\gamma+1} at γ+1\gamma+1 positions at once.
  3. Accept or reject: check from left to right. Candidate xix_i is accepted with probability min⁡(1, pi(xi)/qi(xi))\min(1,\,p_i(x_i)/q_i(x_i)); at the first rejection, stop, and discard all later candidates.
  4. Correct: at the rejected position, resample a token from the corrected distribution (next section). If all γ\gamma candidates are accepted, sample one extra “bonus” token from the distribution pγ+1p_{\gamma+1} at position γ+1\gamma+1.
One round of speculative decoding: draft, parallel verification and output The draft model proposes 4 candidates x1 to x4 one after another. One forward pass of the target model checks 5 positions in parallel: the first two candidates are accepted, the third is rejected, and the fourth and fifth positions are void. The round outputs x1, x2 and x3 prime, which is resampled from the corrected distribution at the third position. ① Draft model small and fast, guesses γ=4 ② Target model one pass checks 5 positions ③ Output: 3 tokens the rejected slot is resampled x₁ x₂ x₃ x₄ ✓ Accept min(1, p₁/q₁) ✓ Accept min(1, p₂/q₂) ✗ Reject min(1, p₃/q₃) Void x₃ was rejected Void bonus unused x₁ x₂ x₃′ resampled The rejected slot is redrawn from norm(max(0, p − q)); the overall output matches the target model
One round of speculative decoding: the draft proposes 4 tokens one by one, the target model checks 5 positions in a single forward pass, accepts the first two and corrects the third, so the round outputs 3 tokens.

Why the output distribution does not change

Look at a single position. Let the target model’s distribution be pp and the draft model’s be qq. A draft token x∼qx\sim q is accepted with probability min⁡(p(x)/q(x),1)\min(p(x)/q(x),1), and on rejection a token is drawn from the corrected distribution:

p′(x)=max⁡(0,  p(x)−q(x))∑ymax⁡(0,  p(y)−q(y))p'(x)=\frac{\max\big(0,\;p(x)-q(x)\big)}{\sum_{y}\max\big(0,\;p(y)-q(y)\big)}

Write β=∑xmin⁡(p(x),q(x))\beta=\sum_x\min\big(p(x),q(x)\big). It is both the total probability that a draft token is accepted and equal to 1−TV(p,q)1-\mathrm{TV}(p,q), where TV\mathrm{TV} is the total variation distance. The denominator is exactly 1−β1-\beta. The probability of outputting xx is therefore

Pr⁡[x]=q(x)min⁡ ⁣(1,p(x)q(x))⏟proposed and accepted+(1−β) p′(x)⏟filled in after a rejection=min⁡(p(x),q(x))+(p(x)−min⁡(p(x),q(x)))=p(x)\Pr[x]=\underbrace{q(x)\min\!\Big(1,\frac{p(x)}{q(x)}\Big)}_{\text{proposed and accepted}}+\underbrace{(1-\beta)\,p'(x)}_{\text{filled in after a rejection}} =\min\big(p(x),q(x)\big)+\big(p(x)-\min(p(x),q(x))\big)=p(x)

The accepted part contributes min⁡(p,q)\min(p,q), and the gap p−min⁡(p,q)p-\min(p,q) is filled in exactly by the corrected distribution; together they give pp. If a rejection sampled directly from pp instead, high-probability tokens would be counted twice and the distribution would be skewed. Later positions are used only when all earlier ones were accepted, so the per-position result extends to the whole sequence (Leviathan et al., 2023, Appendix A.1).

How much faster: expected output and speedup

Let the probability that each draft token is accepted be α\alpha (assuming the positions are independent and identically distributed). The number of tokens KK produced in a round is “the number of consecutive acceptances (at most γ\gamma) plus 1”:

Pr⁡[K=k]=αk−1(1−α)(k≤γ),Pr⁡[K=γ+1]=αγ\Pr[K=k]=\alpha^{k-1}(1-\alpha)\quad(k\le\gamma),\qquad \Pr[K=\gamma+1]=\alpha^{\gamma} E[K]=1−αγ+11−α\mathbb E[K]=\frac{1-\alpha^{\gamma+1}}{1-\alpha}

One round costs γ\gamma draft steps plus 1 verification by the target model. Let one draft step take cc times as long as one target step (cc is called the draft cost ratio). A round then takes γc+1\gamma c+1, and the speedup over ordinary decoding is

speedup=1−αγ+1(1−α)(γc+1)\text{speedup}=\frac{1-\alpha^{\gamma+1}}{(1-\alpha)(\gamma c+1)}

This is Theorem 3.8 of Leviathan et al. Letting c→0c\to0 and γ→∞\gamma\to\infty, the speedup tends to 1/(1−α)1/(1-\alpha). Table 1 of the paper gives several numbers for c=0c=0, and the widget below reproduces them, for example 6.86x for α=0.9,γ=10\alpha=0.9,\gamma=10.

Hands-on: the speculative decoding lab

Drag the acceptance rate α\alpha, the draft length γ\gamma and the draft cost ratio cc. The top chart shows the speedup as a function of γ\gamma, and the bottom chart shows the distribution of tokens produced per round, where the dots are the frequencies from simulating 2000 rounds with the same rule.

  1. A longer draft is not always better. With the defaults (α=0.8\alpha=0.8, c=0.05c=0.05), γ=4\gamma=4 gives a 2.80x speedup. The optimum is γ=8\gamma=8 at 3.09x, and stretching to γ=16\gamma=16 drops it to 2.72x, because later draft tokens are less and less likely to be accepted yet still cost draft time.
  2. Even c=0c=0 has a ceiling. For α=0.8\alpha=0.8 the limit is 1/(1−0.8)=51/(1-0.8)=5x. With γ=4\gamma=4 you only get 3.36x, and you need γ=32\gamma=32 to approach 5.0x.
  3. The acceptance rate matters more than the draft length. Keep c=0.05c=0.05 and γ=4\gamma=4 and lower α\alpha from 0.8 to 0.6: the speedup falls from 2.80x to 1.92x, and the number of positions the target model has to process per kept token rises from 1.49x to 2.17x. Rejected drafts burn compute for nothing.
  4. An expensive draft eats the gain. At α=0.8\alpha=0.8, raising cc from 0.05 to 0.3 shrinks the best γ\gamma from 8 to 3 and leaves only a 1.55x speedup.
  5. It can be slower. With α=0.3\alpha=0.3, c=0.3c=0.3 and γ=4\gamma=4 the speedup is 0.65x, slower than not using it at all. For γ=1\gamma=1 the speedup is (1+α)/(1+c)(1+\alpha)/(1+c), so it pays off only if α>c\alpha>c.

Where the draft comes from

ApproachSource of the draftCharacteristics
Separate small modelA smaller model of the same family with the same tokenizerPlug and play, no training needed, but one more model to host. Leviathan et al. report that a draft about two orders of magnitude smaller than the target usually works best
N-gram lookupA statistical language model; evaluating it is a table lookup, so c≈0c\approx0Lower α\alpha: with a bigram model on T5-XXL English-to-German translation they got α≈0.2\alpha\approx0.2
Extra decoding heads (Medusa)Several heads on top of the target model predict multiple future tokens in parallelNo separate draft model, and tree attention verifies several candidates at once; the heads need training
Feature-level drafting (EAGLE)Autoregression on second-to-top-layer features predicts the draftReports a 2.7x to 3.5x latency speedup on LLaMA2-Chat 70B
Multi-token prediction module (MTP in DeepSeek-V3)The MTP module that exists from training is reused at inferenceReports an acceptance rate of 85% to 90% for the second token across topics, and a 1.8x higher decoding throughput

The DeepSeek-V3 numbers can be cross-checked with the formula above. Predicting one extra token beyond the next amounts to γ=1\gamma=1, and a round yields 1+α1+\alpha tokens in expectation. For α=0.85\alpha=0.85 to 0.90 that is 1.85 to 1.90, and the reported 1.8x is slightly below it. The gap can be read as the overhead of the draft module and verification: treating 1.8x as (1+α)/(1+c)(1+\alpha)/(1+c) gives cc of about 0.03 to 0.06.

When it does not pay off

SituationReasonQuantity involved
Low acceptance rate (open-ended writing, a draft model that is too weak)Most candidates are rejected, so a round yields barely more than 1 tokensmall α\alpha
The draft model is not cheap enoughEvery round spends an extra γc\gamma clarge cc
Large batch, decoding already compute-boundVerifying γ+1\gamma+1 positions is no longer free. In the A100 example above, compute is only saturated at a batch of about 150, after which the benefit shrinks quicklya round no longer costs 1
Tight memoryThe draft model and its KV cache must be hosted as well—

Hands-on code: verify “the distribution is unchanged” and the expected output

The snippet below uses a toy distribution over 4 tokens to check two things: whether the frequencies produced by the accept-and-correct rule equal pp, and whether the average number of tokens per round matches the formula:

import numpy as np

rng = np.random.default_rng(0)
p = np.array([0.50, 0.25, 0.15, 0.10])  # target model's distribution at one position
q = np.array([0.25, 0.25, 0.25, 0.25])  # draft model's distribution at the same position
residual = np.maximum(p - q, 0)
residual /= residual.sum()               # corrected distribution norm(max(0, p - q))
alpha = np.minimum(p, q).sum()           # probability that one draft token is accepted


def one_position(n):
    x = rng.choice(4, size=n, p=q)                       # the draft model proposes
    accept = rng.random(n) < np.minimum(1, p[x] / q[x])  # accept with probability min(1, p/q)
    out = np.where(accept, x, rng.choice(4, size=n, p=residual))  # on rejection, resample from the corrected distribution
    return out, accept.mean()


out, rate = one_position(1_000_000)
print("output frequencies :", np.round(np.bincount(out, minlength=4) / len(out), 3))
print("target distribution:", p)
print(f"acceptance rate {rate:.3f}, theory sum(min(p, q)) = {alpha:.2f}")

gamma, rounds = 4, 200_000
x = rng.choice(4, size=(rounds, gamma), p=q)
accept = rng.random((rounds, gamma)) < np.minimum(1, p[x] / q[x])
n_accepted = np.where(accept.all(axis=1), gamma, accept.argmin(axis=1))  # number accepted before the first rejection
print(f"{np.mean(n_accepted + 1):.3f} tokens per round, formula (1-a^(g+1))/(1-a) = {(1 - alpha ** (gamma + 1)) / (1 - alpha):.3f}")

Output:

output frequencies : [0.5   0.25  0.149 0.1  ]
target distribution: [0.5  0.25 0.15 0.1 ]
acceptance rate 0.750, theory sum(min(p, q)) = 0.75
3.054 tokens per round, formula (1-a^(g+1))/(1-a) = 3.051

The output frequencies agree with pp to three decimals, and the simulated 3.054 tokens per round differs from the formula’s 3.051 only by sampling error.

Summary and exercises

Key points:

  1. Small-batch decoding is limited by memory bandwidth, so letting the large model process a few more positions barely adds any time.
  2. Speculative decoding has a draft model guess γ\gamma tokens, lets the large model verify them in one forward pass, accepts each with probability min⁡(1,p/q)\min(1,p/q) and resamples from the corrected distribution on rejection. The output distribution is exactly the same as with the large model alone.
  3. The expected output per round is (1−αγ+1)/(1−α)(1-\alpha^{\gamma+1})/(1-\alpha) tokens, and the speedup divides that by γc+1\gamma c+1. A low acceptance rate, an expensive draft or a large batch shrinks the gain, even to below 1.

Quick self-check

1. Why does speculative decoding not lower the output quality?

  • Because the draft model is as good as the large model
  • Because draft tokens are accepted with probability min(1, p/q) and rejected ones are resampled from the corrected distribution norm(max(0, p−q)), so the output distribution at each position is exactly the target model’s
  • Because the large model manually fixes all of the draft model’s mistakes
  • Because it is only used with greedy decoding

The accepted part contributes min(p, q), and the gap p − min(p, q) is filled in by the corrected distribution; together they are exactly p. This holds at any sampling temperature, and greedy decoding is just a special case.

2. With acceptance rate α = 0.8 and draft length γ = 4, how many tokens does a round produce in expectation?

  • 2.0
  • 3.36
  • 4.0
  • 5.0

The expectation is (1 − 0.8⁵) / (1 − 0.8) = (1 − 0.32768) / 0.2 ≈ 3.36. The upper limit is γ + 1 = 5, which requires every candidate to be accepted.

3. Which of the following situations noticeably shrink the benefit of speculative decoding? (select all that apply)

  • The draft model’s predictions disagree with the target model’s, so the acceptance rate is low
  • The batch is large and decoding is already close to the compute limit
  • The draft model is not cheap enough relative to the target model, so c is large
  • The generated text is long

The first three correspond to a small α, verification no longer being free, and a large c. The longer the generation, the smaller the boundary effect of “each round calls the large model at least once” in the formula, so length is not a disadvantage.

4. Where should the token at a rejected position be sampled from?

  • Directly from the target model’s distribution p
  • From the corrected distribution norm(max(0, p − q))
  • The draft model’s second most likely token
  • Let the draft model sample again

The acceptance step has already contributed min(p, q), so only p − min(p, q) is left to fill in. Sampling directly from p would count high-probability tokens twice and change the distribution.

Exercises:

  1. For α=0.9,γ=2,c=0\alpha=0.9,\gamma=2,c=0, how many tokens does a round produce in expectation, and what is the speedup?
  2. In the lab set α=0.8,c=0.3\alpha=0.8,c=0.3. What is the best γ\gamma, and why is it much smaller than at c=0.05c=0.05?
  3. The target distribution is p=(0.5,0.25,0.15,0.1)p=(0.5,0.25,0.15,0.1) and the draft distribution qq is uniform. Find the acceptance rate α\alpha, then the expected number of tokens per round for γ=4\gamma=4.
Reference answers
  1. (1−0.93)/(1−0.9)=0.271/0.1=2.71(1-0.9^{3})/(1-0.9)=0.271/0.1=2.71 tokens. With c=0c=0 a round takes 1, so the speedup is also 2.71x, which agrees with Table 1 of the paper.
  2. The best γ=3\gamma=3, with a speedup of about 1.55x. Once draft steps get expensive, every extra guessed token costs another c=0.3c=0.3 target steps while the chance that later candidates are accepted keeps decaying as αk\alpha^k, so it is better to stop early.
  3. α=∑xmin⁡(p,q)=0.25+0.25+0.15+0.10=0.75\alpha=\sum_x\min(p,q)=0.25+0.25+0.15+0.10=0.75. The expected output per round is (1−0.755)/(1−0.75)≈3.05(1-0.75^{5})/(1-0.75)\approx3.05 tokens, which matches the 3.051 from the code above.

Further reading

FAQ

Does speculative decoding change the model's output?
No. It accepts a draft token with probability min(1, p/q) and, on rejection, samples from a corrected distribution, which makes the output distribution at every position exactly equal to the target model's distribution p. The speedup is lossless, and with greedy decoding you get the target model's own greedy sequence (up to floating-point error). It changes the speed, not the quality.
How should I choose the draft model?
The most common choice is a small model from the same family with the same tokenizer. Leviathan et al. report that a draft model about two orders of magnitude smaller than the target usually works best, because the acceptance rate has to be balanced against the cost of drafting. If you do not want to maintain a separate draft model, you can use Medusa's extra decoding heads, EAGLE's feature-level drafting, or, as in DeepSeek-V3, reuse the multi-token prediction (MTP) module that already exists from training.
How much faster does speculative decoding make generation?
It depends on the acceptance rate, the drafting cost, the hardware and the batch size, so there is no fixed number. Published reports include 2x to 3x on T5-XXL (Leviathan et al.), 2x to 2.5x on Chinchilla 70B (Chen et al.), a 2.7x to 3.5x latency speedup for EAGLE on LLaMA2-Chat 70B, and a 1.8x higher decoding throughput from MTP in DeepSeek-V3.
Is speculative decoding still useful at large batch sizes?
The benefit shrinks. Speculative decoding exploits the fact that at small batch sizes decoding is limited by memory bandwidth and compute is idle. Once the batch is large enough to saturate compute (for a 7B model on an A100, roughly a batch of 150), verifying γ+1 positions is no longer free, and rejected drafts waste compute.
Can speculative decoding be combined with KV cache and quantization?
Yes, they stack. The KV cache means the target model only processes the new positions when verifying, and quantization means each step reads fewer weight bytes; both lower the cost of one target step. Speculative decoding reduces the number of serial target-model calls. The draft model can be quantized too, which lowers the draft cost ratio c.