Speculative decoding

In one sentence

A cheap draft guesses the next few tokens and the big model verifies them all in one forward pass, keeping those it agrees with: ~2–3× lower latency with exactly the target's output distribution.

Analogy: A junior drafts the next few words; the senior reads them at a glance, keeps the part they would have written anyway and corrects the first wrong word. Reviewing is much faster than writing.

One round: the draft guesses 4 tokens, the target checks them in a single pass, keeps the agreed prefix and fixes the first miss.

How it works

  • At small batches decode is memory-bound, so scoring 5 tokens in one pass costs about the same as 1.
  • At the first rejection, the target samples a replacement; if all k pass, it adds a bonus token.
  • Speed depends on the acceptance rate: at 80% with k=4, each target pass yields ~3.4 tokens.
  • Drafts can be a small same-tokenizer model, Medusa or EAGLE heads, or prompt lookup copying spans from the input.
  • Big batches erase the gain (e.g. ~1.1× at batch 64), so turn speculation off under peak load.

Check yourself

Answer to mark it learned; your answers aren't scored.

Speculative decoding cut latency 2.5× in your batch-1 benchmark. In production at peak load (batch 64) the gain is only ~1.1×, and acceptance rate is unchanged. Best next step?

Go deeper

Full explanation

Speculative decoding lets a big model emit several tokens per forward pass without changing what it would have produced.

At small batch sizes, decode is memory-bound: producing one token reads all of the target's weights while the GPU's math units mostly idle (see Prefill vs decode). Scoring 5 tokens in one pass costs about the same as scoring 1. Speculative decoding exploits that:

  1. A cheap draft proposes k tokens (typically 3–8) one by one.
  2. The target runs one forward pass over all k, getting its own probabilities at every position.
  3. Each draft token is accepted with probability min(1, p_target / p_draft). At the first rejection, a replacement is sampled from the corrected leftover distribution and the rest is discarded. If all k pass, the target adds one bonus token.

This rejection rule makes the output distribution identical to the target's (Leviathan et al.; Chen et al.): lossless, not an approximation. Speed depends on the acceptance rate: at 80% with k=4, each target pass yields ~3.4 tokens, typically 2–3× lower latency.

Draft options: a small model from the same family and tokenizer; Medusa heads or EAGLE, light heads trained on the target's own hidden states; or n-gram / prompt lookup, which copies spans from the prompt for free and shines on edits, summaries and RAG answers that quote sources.

Code Draft-model and prompt-lookup speculation
pythonDraft-model and prompt-lookup speculation
from transformers import AutoModelForCausalLM, AutoTokenizer

TARGET = "meta-llama/Llama-3.1-8B-Instruct"  # the model whose output you want
DRAFT = "meta-llama/Llama-3.2-1B-Instruct"   # same tokenizer, ~6x fewer params

tok = AutoTokenizer.from_pretrained(TARGET)
target = AutoModelForCausalLM.from_pretrained(TARGET, dtype="auto", device_map="auto")
draft = AutoModelForCausalLM.from_pretrained(DRAFT, dtype="auto", device_map="auto")
inputs = tok("Explain TCP slow start in two sentences.", return_tensors="pt").to(target.device)

# Draft proposes, target verifies in one pass; greedy output matches target alone
out = target.generate(**inputs, assistant_model=draft, do_sample=False, max_new_tokens=200)

# No draft model: propose n-grams copied from the prompt (edits, RAG quotes)
out = target.generate(**inputs, prompt_lookup_num_tokens=10, max_new_tokens=200)

# Serving: vLLM speculates per request inside continuous batching
from vllm import LLM

llm = LLM(model=TARGET, speculative_config={
    "model": DRAFT,               # or {"method": "ngram", "prompt_lookup_max": 4}, or an EAGLE head
    "num_speculative_tokens": 5,  # k: higher only pays while acceptance stays high
})
# Track acceptance rate in production: low acceptance = wasted draft + verify work
Real example

A coding assistant runs a 70B model at ~30 tokens/s per stream. Adding a 1B draft from the same family with k=5, acceptance on code is ~70%, so each target pass yields ~2.9 tokens; after draft overhead, streams reach ~70 tokens/s. On "apply this change to the file" requests, prompt lookup alone accepts most tokens because the output copies the input. At peak load (batch 64), the gain falls to ~1.1×, so they disable speculation above batch 32.

Trade-offs +3 · −3

+ You gain

  • Lossless: same output distribution as the target, so no quality trade-off to negotiate.
  • ~2–3× lower per-token latency at low batch on chat and coding workloads.
  • No retraining of the target for draft-model or n-gram variants; prompt lookup is free.

− You pay

  • Gains shrink, and can turn negative, at high batch sizes where the GPU is already compute-bound.
  • Extra memory and moving parts: draft weights plus their own KV cache, or trained heads, and k to tune.
  • Speedup depends on acceptance, which drops on creative, high-temperature or out-of-domain text.

Use it when

Latency-sensitive, low-to-moderate concurrency on self-hosted models (interactive chat, coding assistants, voice), especially when outputs echo the input: edits, extraction, RAG with quotes.

Avoid it when

Throughput-maximizing batch jobs or GPUs already saturated at large batch sizes, when no well-aligned draft exists, or when you consume a hosted API and can't control decoding.

Numbers to know 4
  • Leviathan et al.: 2–3× on T5-XXL with identical outputs; Chen et al.: 2–2.5× on Chinchilla 70B.
  • Expected tokens per target pass = (1 − α^(k+1)) / (1 − α); acceptance α = 0.8, k = 4 → ~3.4.
  • Medusa-2: 2.3–3.6×; EAGLE: 2.7–3.5× on LLaMA2-Chat 70B (papers).
  • DeepSeek-V3's multi-token prediction: 85–90% acceptance of the second token, ~1.8× tokens/s.
Common pitfalls 4
  • Benchmarking at batch 1 and promising the same speedup for production traffic at batch 64.
  • A mismatched draft (base model for a chat-tuned target, other family): low acceptance makes it slower than no speculation.
  • Setting k too high: every rejected token is wasted draft and verify compute. Tune k from measured acceptance.
  • Reading "lossless" as bit-identical: batched verification changes numerics, so exact-string tests can flake.
Interview questions 2

Answer out loud first, then reveal.

How can speculative decoding be faster if the big model still checks every token?Show answer

Because checking is parallel and generating is sequential. At small batch, decode is memory-bound: one step reads all the weights to produce one token while the math units idle. Verifying five draft tokens in one pass costs about the same as generating one. If the draft is right 80% of the time with k=4, each expensive pass yields ~3.4 tokens instead of one. And the rejection-sampling rule keeps the output distribution equal to the target's, so it's a pure latency win, not a quality trade.

When would you turn speculative decoding off?Show answer

When the GPU is compute-bound. At high batch sizes the verify pass is no longer free: every rejected token burns FLOPs another request could have used, so throughput can drop. Also when acceptance is low, for example high-temperature creative text or a poorly matched draft. In practice I enable it for latency-critical interactive traffic, disable it above a batch-size threshold, and decide with two metrics: acceptance rate and tokens per second per stream at real load.