21.6 Speculative decoding: n-gram, prompt-lookup, and model drafts
The generate loop produces one token per forward pass. Profile it on SmolLM2 and the pass is dominated by reading every weight from memory once; the arithmetic for one token is small. A pass over 5 tokens (with the KV cache, 5 new query rows) reads the same weights once and costs barely more. So decoding wastes most of what each pass could do.
Concepts from other sections (14)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- KV cache: per-layer storage of past keys and values, so each decode step processes only the new token. Read more: 21.2 The KV cache, incremental decoding, and generate
- Speculative decoding: drafting several tokens cheaply and verifying them with one target forward pass, keeping the longest acceptable prefix. Read more: 23.9 Speculative decoding in the engine
- n-gram model: a language model that conditions each token only on the previous tokens. Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- N-gram draft: a draft that picks, token by token, the most frequent continuation of the last few tokens in the context, ties to the lowest id. Read more: 23.9 Speculative decoding in the engine
- Rejection sampling: drawing from a proposal and accepting each draw with a probability chosen so that accepted draws follow the target . Read more: 13.7 Rejection sampling and residual distributions in Mathematics
- PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- Acceptance probability: , the chance a draft token is kept. Read more: 13.7 Rejection sampling and residual distributions in Mathematics
- Total variation distance: ; it satisfies . Read more: 13.7 Rejection sampling and residual distributions in Mathematics
- Request generator: the PCG32
samplesub-stream seeded by the request’s seed. Read more: 21.1 Sampling and logit processors - Cumulative distribution function (CDF): , defined for every random variable, discrete or continuous; it rises from 0 to 1, and for a continuous variable its derivative is the density. Read more: Chapter 13: Probability and Statistics in Mathematics
- Repetition penalty: a Hugging Face rule that divides positive and multiplies negative logits of seen ids by , once per distinct id. Read more: 21.1 Sampling and logit processors
- greedy decoding: taking the single most likely token at every step; a beam of size 1. Read more: 10.4 Beam search, generic over a step function
- hidden states: the block inputs and outputs, before the final norm, that a VLM can tap. Read more: 14.3 Vision transformer encoder
Speculative decoding spends that slack. Something cheap guesses the next tokens, the target scores all of them in one pass, and every correct guess is a token you did not pay a pass for. The trap is correctness: a guess that is accepted when the target would not have produced it changes the output, and for sampling it changes the distribution. This section builds the verification rules that make the speedup free of either, plus three drafts (prompt lookup, an n-gram model, and a small model). Speculative decoding in the engine is held to this section on shared draft and target logits; on a real model, greedy speculation with an n-gram draft gives token-identical output to plain decoding and reports acceptance_rate.
The verification step is rejection sampling and residual distributions (speculative_step) from Mathematics; entropy and KL explains total variation. From this book you will use KVCache.truncate, cache_dims, and Generation from the KV cache and generate, NGramLM from the n-gram language model, and sample, sampling_distribution, token_logprobs, and request_rng from sampling. The solution is spec.py; the contract, including the DraftModel protocol, is spec.pyi.
Learning objectives
- Describe one speculative round: the single target pass, verification, rollback, and the pending token.
- Prove that rejection sampling with the residual emits exactly the target’s distribution, for any draft.
- Apply the greedy rule and explain why it never changes the output.
- Estimate tokens per target pass from the acceptance rate.
- Consume randomness in the fixed order the Rust port reproduces.
The round, and the cache
Section titled “The round, and the cache”The section uses this notation.
| Symbol | Meaning | Type / shape |
|---|---|---|
| vocabulary size | int | |
| the number of draft tokens per round | int | |
| the draft tokens of one round, | ids | |
| the target’s sampling distribution at draft position , given everything before | float64 | |
| the draft’s distribution that was drawn from (one-hot for a deterministic draft) | float64 | |
| a uniform draw in from the request’s PCG32 stream | float | |
| the acceptance probability of one draft token | ||
| total variation distance; |
The target cache holds the positions that are kept. One round goes like this:
- the draft proposes after the context (prompt plus generated);
- the target runs one forward pass over the tokens not yet in its cache: the pending token (the previous round’s last token, or the whole prompt in the first round) followed by . Its last rows of logits are the target’s predictions after the context, after , …, after ;
verify_draftkeeps for some and adds one more token of the target’s own (a correction, or a bonus when all were kept);- the cache now holds draft positions, but only are kept:
KVCache.truncate(committed + len(pending) + n)drops the rest. was never fed, so it becomes the next round’s pending token.
Every round emits tokens for one target pass, so speculation can never be slower in passes than plain decoding (with it is plain decoding).
Exactness: why rejection sampling gives exactly
Section titled “Exactness: why rejection sampling gives exactly ppp”The heart of the method is step 3. Draw . Keep it with probability (the rejection_accept test: ). If it is rejected, draw from the residual with . For any token :
because . Nothing in this argument needs to be good: a bad draft is rejected more often, never wrong.
A deterministic draft (prompt lookup, greedy n-gram) proposes a fixed , which is a draw from the one-hot . Then is kept with probability , and the residual is with removed, renormalized.
At position the same step runs with , the target’s distribution after (which the target already computed in the same pass), and only if was kept. Conditioned on the kept prefix, each position is a fresh exact step, so every emitted token follows the target’s distribution given the tokens before it. Penalties (repetition, presence, frequency) are part of : their history includes the drafts kept so far in this round.
Greedy is the temperature-0 limit
Section titled “Greedy is the temperature-0 limit”At temperature 0 the target’s distribution is one-hot on its argmax (ties to the lowest id). The rule becomes: keep while ; at the first mismatch emit and stop; after matches emit . No uniform is drawn. The emitted tokens are exactly the target’s greedy tokens, which is the claim the end-to-end run checks on a real model.
How fast: expected tokens per pass
Section titled “How fast: expected tokens per pass”Correctness costs nothing; speed depends on the draft. If each draft token is accepted independently with probability , a round of drafts emits
tokens on average. With and that is 3.36 tokens per target pass. The cost side is the draft: an n-gram lookup or a prompt lookup is free, while a smaller model costs of its own passes per round. acceptance_rate (accepted drafts over proposed drafts) is the number that tells you whether a draft is worth it.
The three drafts suit different text. Prompt lookup shines when the output repeats the input (code edits, summaries quoting their source). An n-gram model trained on similar text catches common continuations. A model draft generalizes best and costs the most. For repetitive text, the cheap drafts are enough.
Randomness, in a fixed order
Section titled “Randomness, in a fixed order”One request generator (request_rng(seed)) serves the draft and the verifier in program order. Per verified draft position the verifier always draws two uniforms, u_accept then u_resample (speculative_step takes both, and drawing both keeps the count independent of the outcome); the bonus token is the sampler’s sample (one uniform). The Rust port draws in the same order, so on the same logits it emits the same ids.
Working it by hand
Section titled “Working it by hand”Sampled. Take , target , draft , and draft token .
| Step | Computation | Result |
|---|---|---|
| acceptance probability | ||
| ? no | rejected | |
| residual | , | |
| inverse CDF of | token 0; emitted [0], 0 accepted |
With the draft is kept, is drawn and ignored, and the bonus token comes from the target’s next row with a third uniform. Averaged over the draft as well (): token 1 comes out only when is kept, with probability ; token 0 comes out when (always kept, ) or when is rejected (), total . That is the exactness identity; enumerating every outcome on a larger example gives the same equality as exact fractions.
Greedy. Target rows with argmax 2, 0, 1 ([[0, 1, 3], [2, 1, 0], [0, 5, 1]]) and draft [2, 1]: at position 0, draft 2 equals argmax 2, keep; at position 1, draft 1 argmax 0, emit 0 and stop. The result is [2, 0], one accepted, no uniform drawn. Draft [2, 0] matches twice and the bonus is row 2’s argmax, giving [2, 0, 1].
Prompt lookup. Context 1 2 3 9 1 2, , max_ngram = 3: the last 3-gram 9 1 2 never occurred earlier; the last 2-gram 1 2 occurred at the start, followed by 3 9 1: that is the draft.
Designing the API
Section titled “Designing the API”class DraftModel(Protocol): def propose(self, ctx: Sequence[int], k: int, rng: Optional[UniformSource]) -> tuple[list[int], Optional[NDArray]]class NGramDraft: def __init__(self, lm: NGramLM)class PromptLookupDraft: def __init__(self, max_ngram: int = 3, min_ngram: int = 1)class ModelDraft: def __init__(self, model, temperature: float = 1.0)def verify_draft(target_logits, draft_ids, draft_probs, p, history, rng, prompt=()) -> tuple[list[int], int]def speculative_generate(target, draft, tok, prompt, p, k=4, kv_dtype=np.float32, eos_ids=()) -> Generationrng=None asks a draft for its greedy guess (and no distribution). speculative_generate reports drafted, accepted, acceptance_rate, and target_calls in Generation.stats, besides the usual Generation fields.
To check it, use a toy model whose logits at a position depend only on the tokens up to it and are computed row by row in float64 (so whether the target scores one token or five cannot change a bit), and which asserts on every call that the cache holds exactly the positions before the chunk.
Building it step by step
Section titled “Building it step by step”The reasoning
Section titled “The reasoning”- A target pass costs about the same for 1 or tokens, so the design goal is: as many emitted tokens per target pass as possible, with the output unchanged (greedy) or identically distributed (sampled).
- The target must score the pending token and every draft in one pass. Its row after the pending token is the prediction for ; the row after predicts ; the last row is the bonus. So the pass feeds
pending + draftsand keeps the last rows. - Verification walks positions left to right and stops at the first rejection, because the rows after a rejected were computed with in the context and are no longer valid predictions.
- Every round emits one token of the target’s own: the correction at the rejected position, or the bonus. That makes each round at least as productive as one plain decoding step and makes identical to plain decoding.
- The emitted extra token was never fed to the target, and the rejected drafts were. So the cache is truncated to the kept length and the extra token becomes the next round’s pending input.
- Exactness comes from
speculative_step, which needs and as full distributions. issampling_distributionwith the history including the drafts kept so far (penalties depend on it); is the draft’s row, or a one-hot for a deterministic draft. - Randomness must be consumed in a fixed order so the Rust port reproduces the stream: two uniforms per sampled position, regardless of the outcome; one for the bonus; none when greedy.
Verification
Section titled “Verification”verify_draft is the whole correctness argument in code:
def verify_draft(target_logits, draft_ids, draft_probs, p, history, rng, prompt=()): rows = np.asarray(target_logits, dtype=np.float64) m = len(draft_ids) # m drafts need m + 1 rows: one per draft position plus the bonus row. if rows.ndim != 2 or rows.shape[0] != m + 1: raise ValueError(...) V = rows.shape[1] q = None if draft_probs is None else np.asarray(draft_probs, dtype=np.float64) if q is not None and q.shape != (m, V): raise ValueError(...) hist = [int(t) for t in history] emitted: list[int] = [] for i, x in enumerate(int(t) for t in draft_ids): if p.temperature == 0: # Greedy: the target's argmax at this position, with # penalties over everything emitted so far; no uniform drawn. g = sample(rows[i], p, hist + emitted, rng, prompt)[0] emitted.append(g) if g != x: return emitted, i # g is the correction; i drafts kept continue # g == x: the draft is kept # Sampled: p_i sees the drafts kept this round. P = sampling_distribution(rows[i], p, hist + emitted, prompt) if q is None: Q = np.zeros(V) # a deterministic draft is a draw from Q[x] = 1.0 # the one-hot at x else: Q = q[i] u_accept = rng.uniform() # always two draws, in this order, u_resample = rng.uniform() # whatever the outcome y, ok = speculative_step(P, Q, x, u_accept, u_resample) emitted.append(int(y)) # x if kept, else a residual draw if not ok: return emitted, i # All m kept: the bonus comes from the last row. emitted.append(sample(rows[m], p, hist + emitted, rng, prompt)[0]) return emitted, mTrace the sampled hand example: P = [1/2, 1/4, 1/4], Q = [1/4, 1/2, 1/4], , , so speculative_step rejects and draws token 0 from the residual with ; the function returns ([0], 0).
The generation loop
Section titled “The generation loop”The loop in speculative_generate keeps the cache honest:
committed = 0 # positions in the target cache that are keptpending = list(prompt_ids) # ids whose keys and values are not cached yetwhile len(out) < p.max_tokens: room = limit - committed - len(pending) # cache positions left for drafts # Never draft more than fits the cache, or than the token budget allows # once the round's own extra token is counted (hence the - 1). k_eff = max(0, min(k, p.max_tokens - len(out) - 1, room)) drafts, q = (draft.propose(prompt_ids + out, k_eff, draft_rng) if k_eff > 0 else ([], None)) drafts = [int(t) for t in drafts[:k_eff]] if q is not None: q = np.asarray(q, dtype=np.float64)[: len(drafts)] # One target pass over pending + drafts; keep the last m + 1 rows # (the row after the last pending token, then one after each draft). rows = _forward_rows(target, pending + drafts, committed, cache)[len(pending) - 1 :] calls += 1 emitted, n_acc = verify_draft(rows, drafts, q if drafts else None, p, out, rng, prompt_ids) drafted += len(drafts) accepted += n_acc committed += len(pending) + n_acc cache.truncate(committed) # drop the rejected drafts' keys and values pending = [emitted[-1]] # the extra token is not in the cache yet for j, t in enumerate(emitted): if t in eos: # EOS ends generation and is not emitted finish = "stop" break lps.append(float(token_logprobs(rows[j], p, out, prompt_ids)[t])) # the target's out.append(t) # ... stop strings: decode, cut the text at the first stop, finish = "stop" ... if finish == "stop" or committed + len(pending) >= limit: breakdraft_rng is None at temperature 0 (drafts propose their greedy guess) and the request’s own rng otherwise, so the draft’s draws and the verifier’s draws interleave in one stream in program order. The cache is sized min(model_max, len(prompt_ids) + p.max_tokens + k): room for the prompt, every emitted token, and one round of drafts beyond them.
The three drafts
Section titled “The three drafts”Prompt lookup searches the longest suffix n-gram first, and the most recent earlier occurrence of it:
def propose(self, ctx, k, rng): ctx = [int(t) for t in ctx] L = len(ctx) if k <= 0: return [], None for n in range(min(self.max_ngram, L - 1), self.min_ngram - 1, -1): # longest first suffix = ctx[L - n :] for i in range(L - n - 1, -1, -1): # most recent earlier occurrence first if ctx[i : i + n] == suffix: cont = ctx[i + n : i + n + k] # at most k tokens if cont: return cont, None # deterministic: no distribution return [], NoneOn the hand example 1 2 3 9 1 2: finds no earlier 9 1 2; finds 1 2 at and returns ctx[2:5] = [3, 9, 1].
NGramDraft chains draws from lm.logprobs(cur), appending each token to its own context. With rng=None it is the argmax chain; otherwise it uses sample, keeping the exact distribution as the row.
ModelDraft owns a KV cache and keeps it in sync with a context that the verifier may have cut:
# Sync the cache with ctx: keep the common prefix, but always re-feed# at least the last id, whose logits are the ones we need.common = 0while common < min(len(self._ids), len(ctx) - 1) and self._ids[common] == ctx[common]: common += 1self._cache.truncate(common) # forget rejected drafts and anything after themself._ids = ctx[:common]logits = _forward_rows(self.model, ctx[common:], common, self._cache)[-1]self._ids = list(ctx)# ... then k draws, feeding each drafted token back for the next oneThe full solution is spec.py.
Why this design
Section titled “Why this design”Each round costs one target pass over tokens, plus the draft’s cost: none for prompt lookup ( list comparisons), table lookups for the n-gram, small-model steps for ModelDraft. Expected tokens per pass are .
It might seem simpler to accept a draft when it is “likely enough”, but any threshold rule changes the distribution. The rejection-and-residual rule keeps exactly for any , which is why enumerating outcomes gives exactly as fractions. Re-running the target on the kept prefix would also be wasteful: truncating the cache and feeding only the extra token reuses every kept position’s keys and values, while recomputing would spend the pass the method saved. And one generator for both draft and verifier, in program order, is reproducible by the Rust port draw for draw; two streams would need a second seed and an agreed interleaving anyway.
One tradeoff is accepted. Drafts are a single path (no tree), so one early rejection discards the rest of the round. Tree speculation verifies several paths in one pass at the cost of tree-masked attention.
What can go wrong
Section titled “What can go wrong”The checks below cover every rule in this section.
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| The sampled hand example | unit, smoke | [0] with 0 accepted for ; [1, 2] for ; three uniforms | the draw order the Rust engine must follow |
| The greedy hand example | unit, smoke | [2, 0], [2, 0, 1], [2]; no draw | greedy verification |
| One token is exactly the target | statistical | , all outcomes enumerated: the first token’s distribution equals as fractions | the exactness theorem |
| Two drafts are exact at each position | statistical | : first token ; after a kept , second | the right row and history per position |
| A deterministic draft is exact | statistical | one-hot drafts (every , including one with ) give exactly | prompt lookup and n-gram drafts at temperature > 0 |
| Penalties see the kept drafts | unit | a repetition penalty rejects a second copy of a just-kept token | penalties match plain generate |
| Shapes are checked | boundary | rows must be , draft distributions | no off-by-one row |
| The prompt-lookup hand example | unit, smoke | most recent occurrence; longest n-gram first; | the lookup rule the Rust engine ports |
| The n-gram draft follows its model | unit | greedy ids are the argmax chain; sampled rows are the model’s distribution | the n-gram draft |
| The model draft syncs its cache | unit | the draft’s own cache follows a changing context; proposals equal fresh greedy decoding | a model draft after rejections |
| Greedy speculation equals greedy decoding | differential | four drafts: ids equal generate and a plain argmax loop | the end-to-end greedy claim |
| Rejected drafts are rolled back | property | every target pass starts at the kept length with the right token | the cache rollback |
| Stats, and one pass per round | unit | self-draft: 40 tokens in 8 passes, acceptance 1; : 40 passes | the speedup is real |
| Sampled speculation matches the target distribution | statistical | end to end, 400 seeds, chi-square | sampling through the whole loop |
| EOS, stop strings, and the budget | boundary | EOS ends without being emitted; a stop string cuts the text; never more than max_tokens | generate’s contract kept |
| Bad arguments | boundary | , empty prompt, prompt longer than the model | caller errors early |
| Logprobs are the target’s | unit | logprobs equal plain generate’s | the API reports the target’s numbers |
The mistakes that break those checks fall into a few families: the wrong distribution, the wrong draw order, and a dirty cache.
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
Treating the draft as if it sampled from the target (), or ignoring draft_probs | every draft kept: the output follows the draft, not the target | passes the draft’s own row q[i] as Q to speculative_step |
| A deterministic draft treated as uniform, or its one-hot at the wrong index | biased output, or a crash on | builds Q as the one-hot at the draft id x |
| Resampling from instead of the residual after a rejection | tokens the draft favors are over-represented | delegates the rejected case to speculative_step, which draws from |
| Drawing the uniforms in another order | Python and Rust streams diverge | u_accept then u_resample on every sampled position, then one draw for the bonus |
| Greedy: keeping the draft on a mismatch | the output changes with the draft | returns at the first g != x, with g emitted |
| The bonus from the wrong row | the token after a fully kept draft is wrong | the bonus samples rows[m], the row after the last draft |
| Penalties that do not see the drafts kept this round | sampled and greedy output differ from plain generate with penalties | passes hist + emitted as the history |
| Prompt lookup: oldest occurrence, shortest n-gram first, or ignoring | different drafts than the Rust engine; longer drafts than asked | loops downward, downward, and slices at most tokens |
| A model draft that does not truncate or re-feed its own cache | stale keys: proposals drift from the draft model’s real greedy output | truncates to the common prefix and always re-feeds at least the last id |
| No rollback, or the extra token counted as cached, or never fed | the next pass attends to rejected tokens; output diverges | cache.truncate(committed) then pending = [emitted[-1]] |
EOS kept in ids, or a stop string not cut | the API returns text past the stop | breaks before appending EOS; cuts the decoded text at the first stop |
| A round allowed tokens when fewer remain | more than max_tokens ids | k_eff is capped at max_tokens - len(out) - 1 |
Where this leads
Section titled “Where this leads”Speculative decoding in the engine implements prompt-lookup and n-gram speculation in Rust, held to verify_draft on shared logits and to greedy equality.
Production systems extend each piece:
- vLLM’s rejection sampler (
v1/sample/rejection_sampler.py) isverify_draftbatched on the GPU, with per-request draft lengths and the residual in log space. - vLLM’s n-gram proposer (
v1/spec_decode/ngram_proposer.py) and Hugging Face’sprompt_lookup_num_tokenscorrespond toPromptLookupDraft, with a KMP-style search over the whole context and minimum and maximum n-gram sizes. - EAGLE and Medusa replace
ModelDraftwith a draft head on the target’s own hidden states and a tree of candidates verified with one tree-masked attention pass (Li et al., EAGLE (2024); Cai et al., Medusa (2024)). - SGLang and vLLM tree speculation (SGLang
srt/speculative/) go beyond one draft path: several candidate continuations per round, verified together.
Further reading
Section titled “Further reading”- Leviathan, Kalman, and Matias, Fast Inference from Transformers via Speculative Decoding (ICML 2023).
- Chen et al., Accelerating Large Language Model Decoding with Speculative Sampling (2023).
- Saxena, Prompt Lookup Decoding (2023).
Key terms
Section titled “Key terms”- Draft: a cheap proposer of the next tokens.
- Target: the model whose output speculation must reproduce.
- Pending token: the token emitted last round but not yet fed to the target’s cache.
- Residual distribution: normalized, from which a rejected position is resampled.
- Acceptance rate: accepted draft tokens over proposed draft tokens; per token, .
- Bonus token: the target’s extra token when every draft in a round was kept.
- Prompt lookup: drafting by copying what followed the most recent earlier occurrence of the context’s suffix.
Summary
Section titled “Summary”- Decoding one token costs one pass over the weights; scoring tokens costs about the same, so a draft that guesses tokens and a target that checks them in one pass can emit several tokens per pass.
- Greedy verification never changes the output: keep drafts while they equal the target’s argmax, then add the target’s own token.
- Sampled verification keeps the target’s distribution exactly: accept with probability , otherwise draw from normalized; enumerated exactly, the output is as fractions.
- Rejected drafts must leave no trace in the KV cache: truncate back to the kept length, then feed the replacement token first next round.
- Cheap drafts are enough for repetitive text: copying from the prompt or an n-gram model costs nothing to run.
Review questions
Section titled “Review questions”- Why does a round always emit at least one token, even if the first draft is rejected?
Answer
The target's row at the rejected position is valid (it was computed from kept context only), so the rejection step draws a replacement from the residual (or, greedy, emits the argmax). That token is the target's own, so the round is never worse than one plain decoding step. - Why are both uniforms drawn even when the draft is accepted?
Answer
So the number of draws per position does not depend on the outcome. The Rust port can then consume the same stream position by position and emit the same ids on the same logits; skipping a draw on acceptance would shift every later draw. - After a round keeps of drafts, why is the cache truncated to
committed + len(pending) + nand not+ n + 1?Answer
The cache holds pending plus all $m$ drafts. Only the pending tokens and the $n$ kept drafts are valid; the extra token $y$ was emitted from a row but never fed, so it has no keys or values yet. It becomes next round's pending input. - How does a deterministic draft fit the sampled rule, and what happens when ?
Answer
It is a draw from the one-hot $q = e_x$, so $x$ is kept with probability $\min(1, p(x)/1) = p(x)$, and the residual is $p$ with $x$ removed, renormalized. When $p(x) = 0$ the draft is always rejected and the output is drawn from $p$ itself, which is still exact. - Why must the history passed to
sampling_distributionincludeemitted?Answer
Penalties depend on the tokens generated so far. Plain decoding would have seen the kept drafts before choosing the next token, so $p_i$ must see them too, or a repetition penalty would fail to discourage a second copy of a token kept earlier in the same round.