Speculative Decoding: Lossless 2–3× Faster LLM Inference via Draft & Verify
How Speculative Decoding breaks the single-token autoregressive memory-bandwidth barrier for 2×–3× faster generation with 100% mathematically IDENTICAL outputs: Draft-K → Parallel Verify, Modified Rejection Sampling, and Medusa / EAGLE.
Speculative Decoding uses a tiny fast Draft model to propose K tokens ahead, then lets the large Target model verify all K tokens in ONE parallel forward pass with lossless rejection sampling.
Autoregressive decoding underutilizes GPU compute on 1 token at a time; verifying 5 draft tokens in parallel delivers 2×–3× faster latency with mathematically identical output quality.
Speculative Decoding is the closest thing to a free lunch in LLM systems — cutting single-user latency by 2×–3× while guaranteeing the exact same probability distribution as the full 70B+ model!
In coding assistants (Cursor, Copilot) and RAG summarization where acceptance rate α reaches 0.85–0.95, Speculative Decoding (especially EAGLE-2/3 and N-gram Prompt Lookup) delivers dramatic real-world speedups!
Understanding why Speculative Decoding shines at small batch sizes (memory-bound regime) and shrinks at huge batch sizes (when the GPU is already compute-bound) prevents misconfigured deployments.
Gold-standard LLM inference question: 'Prove why Speculative Decoding is 100% mathematically lossless — even at Temperature > 0 — and explain why verifying 5 tokens costs ~1 token of time!'
Why does running a 70B target LLM on K = 5 draft tokens in parallel take almost the exact same wall-clock time as running it on 1 single token?
- Explain why verifying K = 5 draft tokens in parallel through a 70B target model takes almost the EXACT same latency as generating 1 token!
- Derive Modified Rejection Sampling: accept with min(1, p(x)/q(x)) and resample from norm(max(0, p − q)) on rejection to prove 100% lossless distribution equivalence
- Compute expected accepted tokens per target pass E[tokens] = (1 − α^(K+1)) / (1 − α) from acceptance rate α
- Compare classic Draft-Model speculation (1B → 70B) with Draft-Free Speculation (Medusa, EAGLE, and Prompt-Lookup N-gram drafting)
Think of a senior architect (70B Target) and a fast junior intern (1B Draft) writing boilerplate Python. Instead of the senior architect typing 1 word at a time, the intern quickly drafts the next 5 words (def fib(n): return). The senior architect glances at all 5 words in one blink: 'Yes, yes, yes, wait — change return to if!' You just got 4 exact senior-architect words in the time of 1 blink!
1. Draft-K → Parallel Target Verify
Because causal self-attention computes logits for ALL positions simultaneously in 1 forward pass, the 70B target verifies all draft tokens — AND gives you a free bonus -th token if all pass!
Why Even a Rejection Still Produces Progress
In the worst case where the very first draft token is rejected, the target pass STILL produces 1 exact token from — so every target pass ALWAYS advances by between and exact tokens!
2. Modified Rejection Sampling: Proof of 100% Lossless Equivalence
Even at stochastic sampling, Speculative Decoding guarantees the EXACT target probability for every token in the vocabulary!
The 2-Line Lossless Math (Leviathan et al. / Chen et al., 2023)
For each draft token :
- Accept with probability .
- On the first rejection, discard remaining drafts and sample a replacement from the normalized positive residual: Summing the probability of emitting any token (via acceptance OR via residual resampling) equals identically!
3. Speedup Math & Modern Draft-Free Speculation (EAGLE / Prompt Lookup)
Slide the acceptance rate α in the mini-lab below to see how high agreement (like code & RAG!) multiplies effective tokens/pass!
Beyond Separate Draft Models
- EAGLE-2/3 & Medusa: Attach tiny lightweight prediction heads directly onto the target LLM's own last hidden states — zero separate draft model to manage, higher acceptance rate !
- Prompt Lookup (N-gram) Drafting: For RAG, code editing, and summarization, simply copy matching n-grams straight from the user's input prompt as draft tokens — zero draft compute and up to 3×–4× speedup on editing tasks!
Worked Example: Checking Modified Rejection Sampling
Target p('cat') = 0.40, Draft q('cat') = 0.50. Draft sampled 'cat'.
(80% chance to accept!).
q('cat') × accept = 0.50 × 0.80 = 0.40 = p('cat') — exact!Speculative Decoding Expected Tokens / Pass (K = 5)
In Speculative Decoding, if the 1B Draft model and 70B Target model have identical tokenizers, does Speculative Decoding change the output quality of the 70B model?
Why does Speculative Decoding's speedup shrink (and can even slow down!) at very large serving batch sizes like B = 128?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Yes for standard token-level Speculative Decoding (e.g., LLaMA-3.2-1B drafting for LLaMA-3.3-70B, or Qwen-0.5B drafting for Qwen-72B), so token probabilities and align 1-to-1!
Speculative Decoding: Draft-4 → Single-Pass Target Verification
Watch a tiny 1B draft model propose 4 code tokens, then watch the 70B target verify all 4 in ONE parallel pass (3 accepted + 1 exact correction = 4 tokens!).
Ultra-Fast Code Editing (Fast Apply / Prompt Lookup)
Drafts unchanged lines directly from the existing source file to rewrite 500-line files at 500–1,000+ tok/s!
import torch
# Lossless Speculative Decoding Rejection Sampling Step (preserves exact target distribution)
def speculative_accept(p_target: torch.Tensor, q_draft: torch.Tensor, draft_tokens: list[int]) -> tuple[list[int], int]:
accepted = []
for i, tok in enumerate(draft_tokens):
ratio = (p_target[i, tok] / max(q_draft[i, tok].item(), 1e-9)).clamp(max=1.0).item()
if torch.rand(1).item() < ratio:
accepted.append(tok)
else:
# Sample corrected token from residual distribution max(0, p - q)
residual = (p_target[i] - q_draft[i]).clamp(min=0.0)
residual /= residual.sum()
return accepted, int(torch.multinomial(residual, 1).item())
return accepted, -1 # all draft tokens accepted!
torch.manual_seed(3)
p = torch.softmax(torch.tensor([[3.0, 1.0, 0.2], [0.5, 2.8, 0.1]]), dim=-1)
q = torch.softmax(torch.tensor([[2.9, 1.1, 0.2], [0.4, 2.7, 0.2]]), dim=-1)
acc, corr = speculative_accept(p, q, draft_tokens=[0, 1])
print(f"Accepted draft tokens: {acc} | Correction token: {corr}")
T = 0) decoding.T > 0, Top-P) too!