Model Efficiency & Scaling2023intermediate11 min read
Fast Inference from Transformers via Speculative Decoding
تسريع الاستدلال في المُحوِّلات باستخدام فك الترميز التخميني
Leviathan, Y. · Kalman, M. · Matias, Y. — ICML
The problem
from large autoregressive Transformers is slow because decoding K tokens requires K sequential forward passes — each depending on the output of the previous one. The is typically , not arithmetic: the model's weights must be loaded from memory for every single . Existing acceleration methods like , quantization, or early exits usually require retraining, architecture changes, or alter the output distribution. There was no general way to speed up inference while keeping outputs identical and models unchanged.
The contribution
: a lossless inference acceleration algorithm that uses a small "draft" model to speculatively generate γ tokens, then verifies all of them in a single parallel forward pass of the large "target" model. A novel sampling method — speculative sampling — guarantees the output distribution is identical to the target model alone. No retraining, no architecture changes. Demonstrated 2–3× wall-time speedup on T5-XXL (11B) for translation and summarization with identical outputs.
The impact
Speculative decoding has become a standard inference-time optimization in production LLM serving systems. It inspired a family of follow-up techniques (EAGLE, Medusa, SpecInfer, staged speculative decoding) and is now built into frameworks like vLLM, TensorRT-LLM, and HuggingFace TGI. The core insight — that verification is cheaper than generation because it can be parallelized — fundamentally changed how the industry thinks about .
Imagine a court stenographer and a senior judge. The stenographer types blazingly fast but sometimes makes mistakes. The judge reads slowly but is always right. In the standard system, the judge dictates one word at a time — painfully slow. With speculative decoding, the stenographer types ahead by five words, and the judge reads all five at once: "Correct, correct, correct, wrong — here's the right word." One glance replaced four dictation steps. The transcript is exactly as if the judge dictated every word, but the courtroom finishes three times sooner.
The bottleneck: why autoregressive decoding is slow
A large autoregressive generates text one token at a time. To produce K tokens, the model must perform K sequential forward passes — each one loading the model's billions of parameters from memory, computing over the full context, and outputting a single token. The next pass cannot start until the previous token is generated.
The critical insight is that these forward passes are almost never compute-bound. Modern GPUs have enormous arithmetic , but they sit idle waiting for memory reads. This is the memory bottleneck: the time to decode a token is dominated by the time to load the model weights from high-bandwidth memory (HBM), not by the matrix multiplications themselves. The arithmetic units have spare capacity — and speculative decoding exploits exactly that spare capacity.
The core idea: draft, verify, accept
Speculative decoding has three steps per iteration. First, a small, fast draft model (Mq) generates γ candidate tokens autoregressively — this is cheap because the draft model is typically 100× smaller than the target. Second, the large target model (Mp) evaluates all γ candidates plus one additional position in a single forward pass — this is the parallel verification step. Because the target model processes all positions at once (like an ), this single pass costs roughly the same as generating one token in the standard method. Third, we walk through the draft tokens one by one: accept each one whose probability under Mp is high enough relative to its probability under Mq, reject at the first failure, and sample a correction from the target model's adjusted distribution.
The result: each iteration produces between 1 and γ+1 tokens using only one serial run of the expensive target model. In the best case, all γ drafts are accepted plus one bonus token — a (γ+1)× reduction in the number of target-model calls.
Speculative sampling: preserving the distribution exactly
The mathematical heart of the paper is the speculative sampling method. The goal is to sample a token from the target distribution p(x) while using the draft distribution q(x) to speed things up — without changing the output distribution at all.
The rule is elegant. Sample a token x from q(x). Draw a uniform random number r. If q(x) ≤ p(x), the draft model underestimated this token — the target model would have been even more likely to produce it — so accept unconditionally. If q(x) > p(x), the draft model overestimated — accept with probability p(x)/q(x), and if rejected, resample from an adjusted distribution p′(x) = normalize(max(0, p(x) − q(x))).
The key theorem proves that tokens sampled this way are distributed identically to sampling from p(x) directly. This is what makes speculative decoding lossless: the output text has exactly the same distribution as if you had run the target model alone, token by token.
How many tokens do we gain? The acceptance rate α
The key quantity governing speedup is α, the expected acceptance rate — the probability that a draft token passes the verification criterion. α measures how well the draft model Mq approximates the target model Mp at each step.
The paper shows that α has a clean formula: α = Σ_x min(p(x), q(x)) = 1 − D_LK(p,q), where D_LK is a symmetric divergence measuring the overlap between the two distributions. When p and q are identical, α = 1 and every draft is accepted. When they have disjoint support, α = 0.
Assuming acceptance decisions are roughly independent (the i.i.d. simplification), the expected number of tokens per iteration follows a capped geometric distribution. The speedup is then governed by two factors: α (how good the draft model is) and c (how expensive the draft model is relative to the target). When the draft model is tiny (c ≈ 0), the speedup approaches 1/(1−α). Even a modest α of 0.7 gives roughly 3× fewer target-model calls.
Choosing the draft model and γ
The speculative sampling guarantee holds for any draft model — even a random token generator or a simple bigram model. The output distribution is always identical to the target. What changes is how fast you get there: a better draft model means higher α, which means more accepted tokens per iteration.
In practice, the authors found that draft models about two orders of magnitude smaller than the target work best. For T5-XXL (11B parameters), T5-Small (77M) gave the highest speedup — it's fast enough that its cost coefficient c is near zero, yet it's good enough to achieve α values between 0.53 and 0.75 depending on the task.
The optimal γ can be found numerically given α and c. Higher α supports larger γ before diminishing returns set in. With very low c (tiny draft model), optimal γ can be large. The paper also notes that an "oracle" that dynamically adjusts γ based on predicted acceptance could yield up to ~60% further improvement — a direction explored by later work.
The idea in code
Simplified to show the idea — not the real implementation.
import numpy as np
def speculative_decode_step(target_model, draft_model, prefix, gamma=5):
"""One iteration: draft γ tokens, verify in parallel, accept or correct."""
# Step 1: Draft model generates γ candidate tokens autoregressively
draft_tokens = []
draft_probs = []
current = prefix
for _ in range(gamma):
q = draft_model.get_distribution(current) # q(x | prefix)
token = np.random.choice(len(q), p=q) # sample from draft
draft_tokens.append(token)
draft_probs.append(q)
current = current + [token]
# Step 2: Target model evaluates ALL positions in ONE forward pass
# This is the key: verification is parallel, not sequential!
target_probs = target_model.get_distributions_parallel(
prefix, draft_tokens
) # returns p(x) for each position
# Step 3: Accept or reject, left to right
accepted = []
for i in range(gamma):
p = target_probs[i] # target's distribution at position i
q = draft_probs[i] # draft's distribution at position i
x = draft_tokens[i] # the draft's proposed token
# Acceptance criterion: accept if r < p(x)/q(x)
if np.random.random() < min(1.0, p[x] / q[x]):
accepted.append(x) # keep the draft token
else:
# Reject: sample correction from adjusted distribution
adjusted = np.maximum(0, p - q)
adjusted /= adjusted.sum()
correction = np.random.choice(len(adjusted), p=adjusted)
accepted.append(correction)
break # stop at first rejection
# If all accepted, sample one bonus token from the last target dist
if len(accepted) == gamma:
bonus = np.random.choice(len(target_probs[gamma]),
p=target_probs[gamma])
accepted.append(bonus)
return prefix + accepted # 1 to γ+1 new tokensEmpirical results: 2–3× speedup with identical outputs
The authors tested speculative decoding on T5-XXL (11B parameters) on two tasks — English→German translation (WMT) and text summarization (CNN/DailyMail) — with various draft models. Using T5-Small (77M) as the draft model on a single TPU-v4:
- Translation with (T=0): 3.4× speedup, α = 0.75
- Translation with sampling (T=1): 2.6× speedup, α = 0.62
- Summarization with greedy decoding (T=0): 3.1× speedup, α = 0.65
- Summarization with sampling (T=1): 2.3× speedup, α = 0.53
Greedy decoding consistently yields higher α and greater speedup because the distributions are sharper — a concentrated distribution is easier for the draft model to match. All speedups came with provably identical outputs: not approximately the same, but drawn from the exact same distribution.
Compute trade-offs: latency vs. arithmetic operations
Speculative decoding trades arithmetic operations for latency. Each iteration runs the target model on γ+1 positions in parallel — when drafts are rejected, those extra computations are "wasted." The total arithmetic operations may increase by 1.1–1.6× depending on α and γ.
But this trade-off is favorable because inference is memory-bound, not compute-bound. The GPU's arithmetic units have spare cycles anyway. What matters is how many times you must load the model weights from memory — and speculative decoding reduces that count dramatically (by the speedup factor). Each weight read now produces multiple tokens instead of one.
Crucially, the memory access pattern improves: the target model's weights and are read once per iteration rather than once per token. This is why the method works best when memory bandwidth is the bottleneck — which is almost always the case for large model inference with small batch sizes.
What speculative decoding unlocked
2022
Speculative Decoding (this paper)
Introduced the draft-verify-accept framework with lossless output guarantee. 2–3× speedup on T5-XXL without retraining.
2023
Speculative Sampling (DeepMind)
Independent concurrent work by Chen et al. confirming 2–2.5× speedup on Chinchilla 70B with the same theoretical framework.
2023
SpecInfer & Medusa
Extended speculation to tree-structured drafts, verifying multiple candidate sequences in one pass for higher acceptance rates.
2024
EAGLE & EAGLE-2
Learned draft heads that predict feature representations rather than tokens, achieving 3–5× speedup with dynamic draft trees.
2024
Framework Integration
vLLM, TensorRT-LLM, and HuggingFace TGI all shipped built-in speculative decoding support, making it a production standard.
The deepest impact of speculative decoding is the paradigm shift it introduced: the realization that verification is fundamentally cheaper than generation in autoregressive models. Generation must be sequential — each token depends on the previous one. But verification is parallel — given a proposed sequence, the model can check all positions at once. This asymmetry is now exploited across the entire LLM serving stack, from custom hardware schedulers to inference APIs, and has become one of the most important optimization primitives in the era of large language model deployment.
CitationLeviathan, Kalman, Matias. Fast Inference from Transformers via Speculative Decoding. ICML, 2023.
Terms in this paper
- Speculative Decodingفك الترميز التخميني
- Autoregressive Modelالنموذج التوليدي التراجعي
- Autoregressive Generationالتوليد الارتجاعي
- Acceptance Probabilityاحتمال القبول
- Rejection Samplingالترشيح بالرفض
- Inference Latencyتأخير الاستدلال
- Memory Bandwidthنطاق الذاكرة
- KV Cacheذاكرة المفاتيح والقيم
- Throughputمعدل التدفق والإنتاجية
- Distillationالتقطير
- Greedy Decodingفك الترميز الجشع
- Decoder-Only Modelنموذج فكّ الترميز فقط
- parallelismالمعالجة المتوازية
- Wall-Clock Speedupتسريع الزمن الفعلي
- Student Modelنموذج الطالب