Speculative Decoding

Speculative decoding is an inference-time acceleration technique for autoregressive language models in which a small, cheap draft model proposes a sequence of candidate tokens that a larger target model then verifies in a single parallelized forward pass, accepting or rejecting each token according to a rejection-sampling scheme that preserves the target model's exact output distribution. It matters because the memory-bandwidth bottleneck of autoregressive generation—not FLOPs—dominates wall-clock latency at typical batch sizes, and speculative decoding directly attacks this bottleneck by amortizing the cost of the large model over multiple tokens per step. The counterintuitive fact is that the technique produces outputs with provably identical distribution to the target model alone, so it is a lossless speedup—not an approximation.

The core insight

Autoregressive decoding is bottlenecked not by arithmetic throughput but by memory bandwidth: at each step the entire model must be loaded from HBM to generate a single token, leaving accelerators severely underutilized. Speculative decoding reframes the problem: instead of asking the large model to generate, ask it only to verify a batch of candidate tokens, a task that runs in parallel across the sequence dimension and thus achieves near-peak hardware utilization. A fast draft model provides the candidates, and a mathematically rigorous acceptance criterion guarantees that any rejected token is replaced by a corrected sample so that the final distribution is exactly that of the target. The "aha moment" is that verification is strictly cheaper than generation when the draft model is sufficiently accurate, turning the serial bottleneck into a parallel one.

How It Works

Setup and Notation

Let pθ(⋅∣x1:t)p_\theta(\cdot \mid x_{1:t}) denote the target model's conditional distribution over vocabulary V\mathcal{V} at position t+1t+1 given prefix x1:tx_{1:t}, and let qϕ(⋅∣x1:t)q_\phi(\cdot \mid x_{1:t}) denote the corresponding draft model distribution. At each speculative step the draft model autoregressively generates KK candidate tokens x~t+1,…,x~t+K\tilde{x}_{t+1}, \ldots, \tilde{x}_{t+K}, after which the target model performs a single forward pass over the extended sequence x1:t,x~t+1,…,x~t+Kx_{1:t}, \tilde{x}_{t+1}, \ldots, \tilde{x}_{t+K} to obtain all K+1K+1 conditional distributions simultaneously.

Acceptance Probability

For each candidate token x~t+k\tilde{x}_{t+k} drawn from the draft distribution qϕq_\phi, define the acceptance probability:

1
αk=min⁡ ⁣(1,  pθ ⁣(x~t+k∣x1:t+k−1)qϕ ⁣(x~t+k∣x1:t+k−1))\quad \alpha_k = \min\!\left(1,\; \frac{p_\theta\!\left(\tilde{x}_{t+k} \mid x_{1:t+k-1}\right)}{q_\phi\!\left(\tilde{x}_{t+k} \mid x_{1:t+k-1}\right)}\right)

Explanation: Each draft token is accepted with probability equal to the ratio of target to draft probability, clamped to 1, so tokens the target model finds at least as likely as the draft are always accepted. Here αk∈[0,1]\alpha_k \in [0,1]; when qϕq_\phi assigns higher probability than pθp_\theta to a token, αk<1\alpha_k < 1 and rejection can occur. A Bernoulli trial with probability αk\alpha_k decides acceptance. If token kk is accepted the algorithm proceeds to token k+1k+1; otherwise a correction sample is drawn.

Corrected Distribution on Rejection

When candidate x~t+k\tilde{x}_{t+k} is rejected, the algorithm samples a bonus token from the residual distribution:

2
pcorr(x)=norm ⁣(max⁡ ⁣(0,  pθ(x∣x1:t+k−1)−qϕ(x∣x1:t+k−1)))\quad p_{\text{corr}}(x) = \text{norm}\!\left(\max\!\left(0,\; p_\theta(x \mid x_{1:t+k-1}) - q_\phi(x \mid x_{1:t+k-1})\right)\right)

Explanation: The corrected distribution redistributes probability mass to tokens where the target exceeds the draft, ensuring the marginal distribution over accepted-or-corrected tokens is exactly pθp_\theta. The norm(⋅)\text{norm}(\cdot) operator normalizes the non-negative residual vector to sum to 1. This is the key identity: combining the acceptance step (Eq. [1]) with the correction step (Eq. [2]) yields outputs with marginal distribution pθp_\theta regardless of qϕq_\phi.

Distribution-Preservation Theorem

Theorem (Leviathan et al., 2023). For any draft distribution qϕq_\phi and any draft token x~\tilde{x} drawn from qϕq_\phi, the token emitted by the accept/correct procedure has distribution pθp_\theta. Proof sketch. Let x∈Vx \in \mathcal{V} be arbitrary. The probability of emitting xx is:

3
Pr⁡[emit  x]=qϕ(x)⋅α(x)+(1−∑x′qϕ(x′)α(x′))pcorr(x)\quad \Pr[\text{emit}\;x] = q_\phi(x)\cdot\alpha(x) + \left(1 - \sum_{x'} q_\phi(x')\alpha(x')\right) p_{\text{corr}}(x)

Explanation: The first term accounts for accepting the draft token xx; the second accounts for rejecting whatever draft token was drawn and then sampling xx from the corrected distribution. Substituting Eq. [1] for α(x)\alpha(x) and Eq. [2] for pcorr(x)p_{\text{corr}}(x) and simplifying via the identity ∑xmax⁡(0,p−q)=∑xmax⁡(0,q−p)\sum_x \max(0, p-q) = \sum_x \max(0, q-p) (both equal 12∥p−q∥1\frac{1}{2}\|p-q\|_1) yields Pr⁡[emit  x]=pθ(x)\Pr[\text{emit}\;x] = p_\theta(x) for all xx. □\square

Expected Tokens per Step

The key throughput quantity is the expected number of tokens produced per target-model call. Let β=E[αk]\beta = \mathbb{E}[\alpha_k] denote the mean acceptance rate. Assuming i.i.d. acceptance across positions (an approximation; see Stern et al. 2018 for tighter bounds):

4
E[tokens per step]=1−βK+11−β\quad \mathbb{E}[\text{tokens per step}] = \frac{1 - \beta^{K+1}}{1 - \beta}

Explanation: This geometric-series formula counts the expected number of tokens accepted before the first rejection plus the mandatory bonus token, showing that even moderate acceptance rates yield significant multi-token gains. When β→1\beta \to 1 (draft nearly matches target), E[tokens per step]→K+1\mathbb{E}[\text{tokens per step}] \to K+1; when β→0\beta \to 0 it degrades to 1, i.e., standard decoding. In practice β≈0.7\beta \approx 0.7–0.850.85 for well-matched draft/target pairs, giving speedups of 2–4×.

researcher · Yaniv Leviathan | b. ~1985 | Formal proof of distribution-preserving speculative decoding

Yaniv Leviathan is a research scientist at Google, working primarily on efficient inference for large language models. He is the lead author of the landmark 2022 paper "Fast Inference from Transformers via Speculative Decoding" (published at ICML 2023), which provided the first rigorous proof that the accept/correct scheme preserves the target distribution exactly, transforming a heuristic idea into a mathematically grounded algorithm. Surprisingly, a near-simultaneous and independently derived equivalent result appeared from Chen et al. at DeepMind under the name "Speculative Sampling," making this one of the few cases of genuine simultaneous independent discovery in recent deep-learning research.

Figure
Speculative Decoding Data Flow
Speculative Decoding — One Iteration (K=4 draft tokens) Draft Model q_φ (small/fast) x̃₁ draft x̃₂ draft x̃₃ draft x̃₄ draft single fwd pass Target Model p_θ (large/slow) Verification: ACCEPT α₁=0.91 ACCEPT α₂=0.78 REJECT α₃=0.23 SKIP truncated CORRECT sample p_corr Output: x̃₁, x̃₂, x_corr (3 tokens, 1 target call) Accepted (draft token kept) Corrected (resampled from p_corr)

Intuition

The Mental Model

Imagine a senior professor grading an exam assisted by a junior teaching assistant. The TA quickly drafts answers to multiple questions at once—they're fast but occasionally wrong. The professor then reads all the TA's answers in a single sitting, checking each one: answers that are clearly correct are accepted as-is, while answers that are wrong are crossed out and replaced with the professor's own answer. The student receives only the professor's-quality responses, yet the professor's time is spent reviewing in batch rather than writing from scratch. The draft model is the TA—cheap and fast—and the target model is the professor who validates in parallel. Crucially, even when the TA writes something subtly suboptimal, the professor's correction ensures the final answer is indistinguishable from what the professor would have written alone. The speedup depends entirely on how often the TA is right: the better the TA, the less the professor has to rewrite, and the faster the overall process.

Historical Development

Origins · 1 minhow this was discovered
History
2018
Blockwise parallel decoding (Stern, Shazeer, Uszkoreit) introduces the idea of predicting multiple future tokens in parallel using auxiliary heads, providing early evidence that speculative token proposals could accelerate autoregressive generation.
2020
Non-autoregressive and semi-autoregressive generation research (Gu et al.; Ghazvininejad et al.) demonstrates that masked language model heads can verify proposals quickly, motivating the batch-verification paradigm.
2022
(May): Leviathan, Kalman & Matias (Google) post "Fast Inference from Transformers via Speculative Decoding" to arXiv, providing the first complete acceptance–rejection proof and empirical demonstration with T5 and LaMDA variants.
2022
(May): Chen, Borgeaud, Irving, Lespiau, Sifre, Jumper (DeepMind) independently post "Accelerating Large Language Model Decoding with Speculative Sampling," deriving the identical algorithm and proving distribution preservation with a different proof strategy.
2023
Both papers appear at ICML 2023, establishing speculative decoding as a peer-reviewed, theoretically sound technique; Google integrates it into production serving infrastructure.
2023
Cai et al. introduce Medusa, replacing an external draft model with multiple auxiliary decoding heads on the target model itself, enabling draft generation without a separate model and reducing memory overhead.
2023
Fu et al. and others introduce draft-then-verify with retrieval-augmented speculation (REST), using retrieved document n-grams as draft proposals rather than a learned model.
2024
SpecTr (Sun et al.) and Optimal Transport-based methods provide tighter theoretical analysis of the acceptance-rate–speedup trade-off and introduce multi-candidate (tree-structured) speculative decoding for further gains.
2024
Eagle and EAGLE-2 (Li et al.) propose training the draft model with a feature-level objective aligned to the target's hidden states, achieving acceptance rates exceeding 0.85 on coding benchmarks and speedups of 3–4× over vanilla decoding.

Why It Matters

Real-World Impact
Google PaLM and Gemini ServingGoogle's production inference stack applies speculative decoding using a smaller model from the same family as the draft, achieving reported 2–3× latency reduction at interactive serving batch sizes; this directly enables sub-second response times for Gemini-powered products.
Anthropic Claude APISpeculative decoding with draft models matched to Claude's tokenizer and vocabulary contributes to Claude's low time-to-first-token and high inter-token throughput, critical for streaming interfaces where perceived responsiveness dominates user experience.
Meta LLaMA Inference (llama.cpp, vLLM)The vLLM serving framework (Kwon et al., 2023) ships speculative decoding support using smaller LLaMA variants as drafters, enabling researchers to serve 70B-parameter models on commodity A100 clusters with throughput competitive with cloud APIs.
Medusa in Open-Source Serving (Together AI, Fireworks AI)Medusa heads—auxiliary linear layers trained on top of frozen target models—are deployed in several commercial serving platforms as a drop-in speculative acceleration layer requiring no separate model weights, reducing deployment complexity while retaining most of the speedup.
GitHub Copilot / Code Generation WorkloadsCode completion exhibits high draft-acceptance rates because syntax-constrained outputs are predictable by small code models; speculative decoding is particularly effective here, with acceptance rates of 0.80+ reported for fill-in-the-middle tasks.
On-Device Inference (Apple Neural Engine, Qualcomm)Memory bandwidth constraints on mobile accelerators make speculative decoding disproportionately valuable; Apple's on-device LLM stack reportedly uses small "draft" networks to keep latency acceptable for Siri-class interactions on M-series chips.

Prerequisites

What This Leads To

See Also

KV-Cache and Inference Optimization · Non-Autoregressive Generation · Knowledge Distillation · Beam Search and Decoding Strategies · Inference-Time Compute Scaling