AI / ML Theory
Chapter 10 · 24 min

10.4 — Speculative decoding

Goal: Derive speculative decoding from a draft–target rejection-sampling argument; prove that the resulting distribution equals the target's distribution exactly; analyse the expected wall-clock speedup and when speculative decoding helps vs. hurts.

1. Why this session exists

Decoding from a 7070B-parameter target model is slow: each token costs one full forward pass, memory-bound at batch 11 (Cap 10.3 §3). The compute is spent moving the weights through HBM; the actual multiply-add work is cheap relative to the bandwidth bill.

A simple observation: at typical batch sizes, the GPU is idle most of the time. If we could verify multiple candidate tokens in one forward pass — and most of them turn out to be the same tokens the target would have produced — we'd amortise the per-step weight-read cost across multiple emitted tokens.

This is speculative decoding (Leviathan et al., 2023, arXiv:2211.17192[1]; also Chen et al., 2023[2], arXiv:2302.01318). The protocol:

  1. A cheap draft model πdraft\pi_\text{draft} proposes kk tokens autoregressively.
  2. The expensive target model πtarget\pi_\text{target} runs one forward pass on the kk-token continuation, producing kk next-token distributions in parallel.
  3. A rejection-sampling rule accepts a prefix of the draft, then samples one extra token from a corrected distribution at the rejection point.

The miraculous property: the marginal distribution of the accepted output is exactly πtarget\pi_\text{target}. No quality loss. The only thing that changes is wall-clock — typically 22–3×3\times faster decode at no accuracy cost.

This session derives the algorithm, proves the correctness, and characterises when speculative decoding wins.


2. Setup

Let:

  • πtarget(⋅∣x,y<t)\pi_\text{target}(\cdot \mid x, y_{<t}) be the target model's next-token distribution at step tt. Expensive to compute.
  • πdraft(⋅∣x,y<t)\pi_\text{draft}(\cdot \mid x, y_{<t}) be the draft model's distribution. Cheap (e.g., a 11B model proposing for a 7070B target).
  • kk the speculation length (typical k=4k = 4–88).

The goal is to produce a stream y1,y2,…y_1, y_2, \ldots whose joint distribution equals ∏tπtarget(yt∣x,y<t)\prod_t \pi_\text{target}(y_t \mid x, y_{<t}), but with fewer wall-clock target-model invocations per emitted token.


3. The algorithm

SpeculativeDecode(x):
    t = 1
    while not done:
        # Draft phase: cheap autoregressive sampling
        draft tokens ỹ_{t}, ỹ_{t+1}, ..., ỹ_{t+k-1}
        store draft probabilities p̃_j(ỹ_{t+j}) = π_draft(ỹ_{t+j} | ..., ỹ_{<t+j})

        # Verification phase: one target forward over k tokens
        target distributions q_j(·) = π_target(· | x, y_{<t}, ỹ_{t..t+j-1})
            for j = 0, 1, ..., k

        # Acceptance loop
        for j = 0, 1, ..., k-1:
            r ~ Uniform(0, 1)
            if r < min(1, q_j(ỹ_{t+j}) / p̃_j(ỹ_{t+j})):
                accept: y_{t+j} = ỹ_{t+j}
            else:
                # rejection: resample from corrected distribution
                q̄(v) ∝ max(0, q_j(v) - p̃_j(v))
                y_{t+j} ~ q̄
                t = t + j + 1; goto outer while
        # All k drafts accepted
        # Bonus token: sample y_{t+k} from q_k (free — already computed)
        y_{t+k} ~ q_k
        t = t + k + 1

In words: walk down the draft, accept each token with probability min⁡(1,q/p~)\min(1, q/p̃). On the first rejection, resample from the residual distribution (q−p~)+/Z(q - p̃)_+ / Z. If all kk are accepted, take one bonus token from the next target distribution (which we already computed for free during verification).


4. Correctness: the rejection-sampling argument

The key claim:

  Pr⁡[Yt+j=v]  =  qj(v)for every accepted (or resampled) token.  \boxed{\;\Pr[Y_{t+j} = v] \;=\; q_j(v) \quad \text{for every accepted (or resampled) token.}\;}

I.e., the distribution of the emitted token at each step is exactly the target's distribution qjq_j, regardless of the draft.

4.1 Proof for a single step

Fix jj and write p=p~jp = p̃_j, q=qjq = q_j for brevity, Y~∼p\tilde{Y} \sim p for the draft sample.

There are three ways the output YY takes value vv:

Case A. The draft proposes vv (Y~=v\tilde{Y} = v) and it is accepted (r<min⁡(1,q(v)/p(v))r < \min(1, q(v)/p(v))). Probability:

p(v)⋅min⁡ ⁣(1,q(v)p(v))  =  min⁡(p(v),q(v)).p(v) \cdot \min\!\left(1, \frac{q(v)}{p(v)}\right) \;=\; \min(p(v), q(v)).

Case B. The draft proposes some v′≠vv' \ne v (Y~=v′\tilde{Y} = v') and it is rejected, after which we resample Y=vY = v from qˉ(⋅)∝(q−p)+\bar{q}(\cdot) \propto (q - p)_+.

The probability that any draft is rejected is

Pr⁡[reject]  =  ∑v′p(v′)⋅[1−min⁡ ⁣(1,q(v′)p(v′))]  =  ∑v′max⁡(0,p(v′)−q(v′))  =  12∥p−q∥1.\Pr[\text{reject}] \;=\; \sum_{v'} p(v') \cdot \left[1 - \min\!\left(1, \frac{q(v')}{p(v')}\right)\right] \;=\; \sum_{v'} \max(0, p(v') - q(v')) \;=\; \tfrac{1}{2}\|p - q\|_1.

(Using ∑v′min⁡(p,q)+∑v′max⁡(0,p−q)=∑v′p=1\sum_{v'} \min(p, q) + \sum_{v'} \max(0, p - q) = \sum_{v'} p = 1.)

The corrected distribution is qˉ(v)=max⁡(0,q(v)−p(v))/Z\bar{q}(v) = \max(0, q(v) - p(v)) / Z with Z=∑vmax⁡(0,q(v)−p(v))=12∥p−q∥1Z = \sum_{v} \max(0, q(v) - p(v)) = \tfrac{1}{2}\|p - q\|_1. So

Pr⁡[reject and resample v]  =  12∥p−q∥1⋅max⁡(0,q(v)−p(v))12∥p−q∥1  =  max⁡(0,q(v)−p(v)).\Pr[\text{reject and resample } v] \;=\; \tfrac{1}{2}\|p - q\|_1 \cdot \frac{\max(0, q(v) - p(v))}{\tfrac{1}{2}\|p - q\|_1} \;=\; \max(0, q(v) - p(v)).

Case C. The draft proposes vv and it is rejected. Doesn't contribute to Y=vY = v (rejection always leads to resampling from qˉ\bar{q}, which would be a different draw).

Sum. Adding Case A and Case B:

Pr⁡[Y=v]  =  min⁡(p(v),q(v))+max⁡(0,q(v)−p(v))  =  q(v).\Pr[Y = v] \;=\; \min(p(v), q(v)) + \max(0, q(v) - p(v)) \;=\; q(v).

(The last equality is the elementary identity min⁡(a,b)+max⁡(0,b−a)=b\min(a, b) + \max(0, b - a) = b for non-negative a,ba, b.) □\square

So a single rejection-sampling step yields a sample from qq — exactly, not approximately — regardless of pp. Iterating over jj gives the multi-step claim.

4.2 What if all kk drafts are accepted?

After accepting all kk tokens, we have the target distributions q0,q1,…,qkq_0, q_1, \ldots, q_k computed (one verification forward). We never sampled yt+ky_{t+k} from qkq_k. The "bonus token" step samples yt+k∼qky_{t+k} \sim q_k — this is an additional free token from the target distribution. So the all-accepted case emits k+1k+1 tokens for the price of one target forward pass + kk draft forward passes.


5. The expected speedup

Let α\alpha be the per-token acceptance probability — the probability that any given draft token is accepted:

α  =  EY~∼p ⁣[min⁡ ⁣(1,q(Y~)p(Y~))].\alpha \;=\; \mathbb{E}_{\tilde{Y} \sim p}\!\left[ \min\!\left(1, \frac{q(\tilde{Y})}{p(\tilde{Y})}\right) \right].

A useful identity: α=1−12∥p−q∥1\alpha = 1 - \tfrac{1}{2} \|p - q\|_1. The more pp and qq agree, the higher α\alpha.

5.1 Expected accepted tokens per round

In a round of kk drafts, the number of consecutive acceptances before the first rejection is a (truncated) geometric random variable. Its expectation:

E[accepted]  =  1−αk+11−α.\mathbb{E}[\text{accepted}] \;=\; \frac{1 - \alpha^{k+1}}{1 - \alpha}.

(Sum of 1+α+α2+⋯+αk1 + \alpha + \alpha^2 + \cdots + \alpha^k.) Plus one rejection-resampled token in the partial-rejection case, or one bonus token in the all-accepted case. Net:

  E[emitted per round]  =  1−αk+11−α  \boxed{\;\mathbb{E}[\text{emitted per round}] \;=\; \frac{1 - \alpha^{k+1}}{1 - \alpha}\;}

5.2 Cost per round

One round costs:

  • kk draft forwards: k⋅cdraftk \cdot c_\text{draft}
  • 11 target forward over kk tokens (the verification): ctarget(k)≈ctargetc_\text{target}^{(k)} \approx c_\text{target} (the target forward over kk tokens is roughly the cost of one target forward — the prefill cost scales with kk but is bounded by memory traffic, not compute, at small kk).

So

  speedup  =  E[emitted per round]1+k⋅cdraft/ctarget  =  (1−αk+1)/(1−α)1+k⋅cdraft/ctarget  \boxed{\;\text{speedup} \;=\; \frac{\mathbb{E}[\text{emitted per round}]}{1 + k \cdot c_\text{draft}/c_\text{target}} \;=\; \frac{(1 - \alpha^{k+1}) / (1 - \alpha)}{1 + k \cdot c_\text{draft}/c_\text{target}}\;}

5.3 Worked numbers

Llama-3 70B target with Llama-3 8B draft. Approximate cdraft/ctarget≈1/9c_\text{draft}/c_\text{target} \approx 1/9. For an aligned chat workload, α≈0.7\alpha \approx 0.7–0.80.8 empirically.

At α=0.75\alpha = 0.75, k=5k = 5:

  • Numerator: (1−0.756)/(1−0.75)=(1−0.178)/0.25=3.29(1 - 0.75^6)/(1 - 0.75) = (1 - 0.178)/0.25 = 3.29
  • Denominator: 1+5/9=1.5561 + 5/9 = 1.556
  • Speedup: 3.29/1.556≈2.1×3.29 / 1.556 \approx 2.1\times.

A factor of ∼2\sim 2 wall-clock speedup with zero quality loss is the typical operational gain. Speedups of 33–4×4\times are achievable with strong draft models or simple tasks (high α\alpha).

5.4 The acceptance-rate ceiling

α≤1−12∥p−q∥1≤1\alpha \le 1 - \tfrac{1}{2}\|p - q\|_1 \le 1. Equality α=1\alpha = 1 requires p=qp = q — i.e., draft = target. So speculative decoding's ceiling speedup is (k+1)/(1+k⋅cdraft/ctarget)(k+1)/(1 + k \cdot c_\text{draft}/c_\text{target}). With k=5k = 5 and cdraft/ctarget=1/9c_\text{draft}/c_\text{target} = 1/9: ceiling ≈3.86×\approx 3.86\times.

In practice, the per-token acceptance rate α\alpha depends on:

  • How similar the draft and target are. Same family, same training data, smaller size: high α\alpha.
  • The distribution sharpness — sharp distributions (TT small) are easier to predict, α\alpha goes up.
  • The task — code and structured output have low entropy and high α\alpha; open-ended chat has lower α\alpha.

6. Drafting strategies

The draft model can be:

(a) A smaller model from the same family. Llama-3 70B target + Llama-3 8B draft. Trained on the same data, similar tokeniser, similar distribution. α\alpha is typically high. The most common production setup.

(b) A self-distilled draft. Distil the target down to a ∼1%\sim 1\%-size draft. Highest α\alpha but requires the extra training run.

(c) Medusa heads (Cai et al., 2024, arXiv:2401.10774)[3]. Instead of a separate draft model, attach extra prediction heads to the target model that predict yt+1,yt+2,…y_{t+1}, y_{t+2}, \ldots in one pass. Zero extra model weights to load; lower α\alpha than a dedicated draft (the heads are shallow) but cdraft≈0c_\text{draft} \approx 0, so the speedup arithmetic still favours it.

(d) EAGLE / EAGLE-2 (Li et al., 2024, arXiv:2401.15077)[4]. A small autoregressive draft head conditioned on the target's hidden states. Higher α\alpha than Medusa; small constant draft cost. Currently (2026) the best published wall-clock results for general LLM inference.

(e) Tree-based / Lookahead decoding. Generalisations that propose a tree of possible continuations and verify the whole tree in parallel, accepting the deepest matching branch. Higher per-round emit at higher per-round verify cost; complex to implement.


7. Implementation details

Three details that catch implementers:

7.1 Parallel target verification

The target model's kk-token forward pass uses causal attention: position jj attends only to positions ≤j\le j. The KV cache for the prefix y<ty_{<t} is reused; the new keys/values for y~t,…,y~t+k−1\tilde{y}_t, \ldots, \tilde{y}_{t+k-1} are computed once and tentatively appended. On a rejection at position jj, the tentative cache entries from position j+1j+1 onwards are discarded — the cache after a round is truncated to whatever was actually accepted.

This is the single largest source of bugs in speculative-decode implementations. The cache must support truncation in O(1)O(1).

7.2 Temperature handling

Both πdraft\pi_\text{draft} and πtarget\pi_\text{target} should be evaluated at the same temperature — otherwise the acceptance rule samples from the wrong qq. The straightforward implementation evaluates both at the user-facing TT. Top-kk and top-pp truncation are applied to both in the same way.

(There is a variant — typical acceptance — where the acceptance rule accepts any draft token whose probability under qq exceeds a threshold rather than the strict ratio. This loosens the rule, raises α\alpha, but slightly miscalibrates the output. Used in some production stacks for ∼10%\sim 10\% extra speedup.)

7.3 Greedy speculative decoding

At T=0T = 0 (greedy), the algorithm simplifies dramatically: accept y~j\tilde{y}_j iff y~j=arg⁡max⁡vqj(v)\tilde{y}_j = \arg\max_v q_j(v). No randomness. The acceptance rate α\alpha is the probability that draft argmax matches target argmax, position by position. This is often very high (α≈0.85\alpha \approx 0.85–0.950.95 for similar models) — speculative decoding shines brightest at low temperature.


8. When speculative decoding doesn't help

Speculative decoding hurts when:

  • Draft cost is high relative to target. If cdraft/ctarget>∼0.3c_\text{draft}/c_\text{target} > \sim 0.3, the speculation overhead exceeds the savings even at high α\alpha.
  • Acceptance rate is low. Mismatched tokenisers, different model families, or tasks where the draft is consistently wrong. α<0.4\alpha < 0.4 typically means net loss.
  • Batch size is large. At batch B≫32B \gg 32, decode is compute-bound (Cap 10.3 §5.1), and adding speculation work makes throughput worse. Speculative decoding is primarily a latency optimisation for small-batch / single-stream serving. At high-throughput batched serving, continuous batching (10.8) is the better tool.

The clean rule: speculative decoding for latency-sensitive single-stream inference; continuous batching for throughput.


9. Connection to "test-time scaling"

Speculative decoding is a compute-saving technique that gives back wall-clock at no quality cost. Test-time scaling (10.7) is the opposite — spending more inference compute for higher quality.

Both can compose: a reasoning model uses test-time scaling to extend its chain-of-thought, and speculative decoding to make each token of that chain cheaper. The two operate at orthogonal layers (reasoning loop vs. per-token decode).


10. What we keep going forward

Three things.

  1. The protocol. Draft proposes kk, target verifies in parallel, rejection-sampling rule accepts a prefix. The acceptance distribution is exactly the target's — proved by the min⁡(p,q)+max⁡(0,q−p)=q\min(p, q) + \max(0, q - p) = q identity.
  2. The speedup formula. E[emitted]/E[cost]=(1−αk+1)/(1−α)1+k⋅cdraft/ctarget\mathbb{E}[\text{emitted}]/\mathbb{E}[\text{cost}] = \frac{(1 - \alpha^{k+1})/(1 - \alpha)}{1 + k \cdot c_\text{draft}/c_\text{target}}. Empirically ∼2\sim 2–3×3\times for chat workloads at α≈0.7\alpha \approx 0.7–0.80.8.
  3. Where it wins and where it doesn't. Small-batch latency-sensitive inference: yes, almost always. High-throughput batched serving: no, use continuous batching instead.

Cap 10.5 next: FlashAttention. With the KV-cache and speculative-decode mechanics in hand, we look at the attention kernel itself — and how an I/O-aware reformulation makes exact attention ∼2\sim 2–4×4\times faster on modern GPUs.

Self-test

(a) Write the speculative-decoding algorithm in pseudocode. Identify exactly where the one target-model forward pass happens.

(b) Prove that Pr⁡[Y=v]=q(v)\Pr[Y = v] = q(v) for a single accepted-or-resampled step. Use the identity min⁡(a,b)+max⁡(0,b−a)=b\min(a,b) + \max(0, b-a) = b.

(c) State the expected-speedup formula. Compute the speedup for α=0.7\alpha = 0.7, k=4k = 4, cdraft/ctarget=0.1c_\text{draft}/c_\text{target} = 0.1.

(d) Why does α=1−12∥p−q∥1\alpha = 1 - \tfrac12 \|p - q\|_1? Sketch the derivation from α=EY~∼p[min⁡(1,q(Y~)/p(Y~))]\alpha = \mathbb{E}_{\tilde Y \sim p}[\min(1, q(\tilde Y)/p(\tilde Y))].

(e) A team adds speculative decoding to their throughput-optimised serving stack (batch 128, continuous batching). They observe no speedup — sometimes a slowdown. Why?

(f) Greedy speculative decoding: state the simplified acceptance rule and why it almost always achieves higher α\alpha than T>0T > 0 speculative decoding.


References

  1. [1]

    Leviathan, Y., Kalman, M., & Matias, Y. (2023). "Fast inference from transformers via speculative decoding". arXiv:2211.17192 — Speculative decoding protocol and rejection-sampling correctness (10.4).

    ↩
  2. [2]

    Chen, C., Borgeaud, S., Irving, G., et al. (2023). "Accelerating large language model decoding with speculative sampling". arXiv:2302.01318 — Parallel discovery of the same technique (10.4).

    ↩
  3. [3]

    Cai, T., Li, Y., Geng, Z., et al. (2024). "Medusa: Simple LLM inference acceleration framework with multiple decoding heads". arXiv:2401.10774 — Medusa head-based drafting (10.4).

    ↩
  4. [4]

    Li, Y., Wei, F., Zhang, C., & Zhang, H. (2024). "EAGLE: Speculative sampling requires rethinking feature uncertainty". arXiv:2401.15077 — EAGLE draft architecture (10.4).

    ↩