Exercises — Speculative decoding
Before we start, one shared picture. Two models run on the same prefix of already-generated tokens. The small draft model guesses ahead cheaply; the big target model checks all guesses in one pass.

How to read Figure 1. On the far left, the grey box is the prefix — the tokens already committed. The teal row across the top is the draft model producing its guesses one after another (it is sequential, so this is the slow-but-cheap part). The orange row at the bottom is the target model running a single parallel pass that checks all four guesses at once — that is why there is one orange strip under all four columns, not four separate passes. Follow the plum arrows downward: each guess drops into its own check. The plum "accept / resample" label on the right is the decision made per column — keep the guess, or (on the first failure) resample from the adjusted distribution and stop. Keep this picture in mind for every exercise: teal = many cheap passes, orange = one expensive pass, plum = the accept decision.
Throughout:
- = draft model's probability for a token — "how sure the fast intern is".
- = target model's probability — "how sure the expert editor is". This is the distribution we are legally required to reproduce.
- = the acceptance rate: the average chance a single draft token survives the check.
- = lookahead: how many tokens the draft guesses before we stop and verify.
- = time cost of one draft pass and one target pass.
Level 1 — Recognition
Exercise 1.1
The parent note calls speculative decoding "lossless". In one sentence, what does lossless mean here, and which of these does it NOT mean? (a) The output text is identical every run. (b) The output distribution matches the target model run alone. (c) The draft model is never wrong.
Recall Solution
WHAT: Lossless means (b) — the probability distribution of generated tokens is exactly , the same distribution you'd get by running the slow target model by itself. NOT (a): sampling is still random, so different runs give different text — same as normal decoding. NOT (c): the draft is frequently wrong; the acceptance/resampling step is precisely what fixes those mistakes so the final distribution stays correct.
Exercise 1.2
Why is single-token autoregressive decoding described as memory-bound rather than compute-bound?
Recall Solution
Each token needs one forward pass, which must load all model weights from GPU memory (billions of numbers). For a batch of one token, the GPU finishes the arithmetic long before the weights finish streaming in — so the wall-clock time is set by memory bandwidth, not by how many multiply-adds (FLOPs) the chip can do. The GPU sits idle waiting for memory. Speculative decoding fills that idle window by checking many tokens in the same load.
Level 2 — Application
Exercise 2.1
The draft proposes token "cat" at some position with . The target gives it . Compute the acceptance probability . Interpret the result.
Recall Solution
WHAT: , so . WHY: The target likes "cat" more than the draft did. When the target is at least as confident as the draft, there is no risk of over-representing that token, so we accept with certainty. The rule " accept probability ; else accept surely" caps at 1 exactly because a probability can't exceed 1.
Exercise 2.2
Now the draft proposes "cat" with but the target gives . Compute and state what happens on rejection.
Recall Solution
WHAT: , so . WHY: The draft was over-confident — it proposed "cat" more often than the target would. We accept only 25% of the time to bring the effective rate of emitting "cat" back down to the target's level. On rejection (75% of the time): we resample from the adjusted distribution This only keeps tokens the target wanted more than the draft supplied — repairing the bias.
Exercise 2.3
A draft is 8× faster than the target (, ), with lookahead . Compute the total cost of one speculation cycle (draft phase + one verification pass).
Recall Solution
WHAT: Cost units. WHY: The draft runs sequential cheap passes (cost ), then the target runs one pass over all guesses at once (cost ). The target verifies all positions in that single pass — that parallelism is the whole point.
Exercise 2.4
Edge case. The draft never proposes token "dog" at some position — it assigns . But the draft actually sampled some other token there, and separately you wonder: can "dog" ever be emitted, and does the ratio even make sense when ?
Recall Solution
WHAT about the ratio: The formula is only ever evaluated for the token the draft actually proposed. A token can only be proposed if for it, so the denominator you plug in is never zero — the division-by-zero never happens in the accept test. If the draft sampled some token , then by definition . WHAT about "dog" still appearing: Even though the draft can never propose "dog", the target can still want it: . Look at the resample distribution With this becomes . So on a rejection, "dog" can be resampled with positive weight. The resample branch is exactly what rescues tokens the draft assigned zero probability — that's why losslessness holds even for words the draft never dreams of. One-line rule: the accept ratio only sees proposed tokens (, safe division); the resample branch handles every token the target still needs.
Level 3 — Analysis
Exercise 3.1
With acceptance rate and lookahead , compute the expected number of output tokens per cycle using
Recall Solution
WHAT: tokens. WHY the closed form: Accepting the first draft tokens then rejecting has probability and yields output tokens (the resampled one counts). Accepting all has probability and yields (the bonus token). Summing telescopes into the geometric sum .
Exercise 3.2
Using the numbers from 3.1 plus , , compute the effective cost per token and the speedup versus target-only decoding.
Recall Solution
WHAT:
- Cycle cost units.
- Effective cost per token units.
- Baseline (target only) units/token.
- Speedup . WHY less than : Every rejection burns a whole target pass but only yields part of the guessed tokens. The waste caps the gain well below the naive " tokens per pass" dream.
Exercise 3.3
Show algebraically that as , , and interpret.
Recall Solution
WHAT: Write (finite geometric series, terms). As every term , so the sum . WHY / interpretation: If the draft is always right, we accept all guesses and also pick up the bonus token — tokens per single target pass. That is the theoretical ceiling on tokens-per-verification.

How to read Figure 2. The horizontal axis is the acceptance rate from to ; both curves use , , . The teal curve is per cycle — notice it climbs toward the dashed plum ceiling at but only reaches it in the extreme (this is exactly Exercise 3.3). The orange curve is the actual speedup versus target-only decoding; it always sits below the token curve because the denominator charges you for the draft passes. The two black dots mark the case from Exercises 3.1–3.2: read off tokens (teal) but speedup only (orange). Takeaway to carry into L4: raising (better draft agreement) pushes the orange curve up steeply — agreement, not raw draft speed, is the main lever.
Level 4 — Synthesis
Exercise 4.1
You must pick a draft model. Model A: , . Model B: , . Target , . Which gives greater speedup? Show the full comparison.
Recall Solution
Use Speedup with .
Model A (): . Cost . Speedup .
Model B (): . Cost . Speedup .
WHY B wins: Its far higher agreement ( vs ) buys many more accepted tokens per pass ( vs ), which more than pays for being twice as slow to draft. Agreement dominates draft speed here. See Knowledge distillation — the standard route to a high- draft.
Exercise 4.2
Derive the optimal lookahead direction: for fixed , , , does increasing always help? Explain using the speedup formula and identify the trade-off.
Recall Solution
Speedup. Numerator grows toward the ceiling but with diminishing returns: each extra guess is reached only with probability , which shrinks geometrically. Denominator grows linearly in (each extra guess costs no matter what). Trade-off: Eventually the linear cost of extra draft passes outruns the saturating benefit, so there is an optimal lookahead beyond which speedup falls. Larger pushes higher (agreement lets deep guesses survive); larger pushes lower (each guess is expensive). A quick numeric sweep (see verify) with finds speedup peaks around .
Level 5 — Mastery
Exercise 5.1
Prove the losslessness identity for a single position: show that the two-branch scheme (accept-with-, else resample from the adjusted distribution) emits token with total probability exactly .
Recall Solution
Setup. Two ways to emit :
- Accept branch: propose (prob ) and accept it (prob ). Contribution: .
- Reject branch: some proposal gets rejected (total reject prob ), then we resample from . Contribution: .
Total reject probability. Normaliser of the resample. Using , The cancels the ! So the reject branch contributes Add the branches. WHY it's beautiful: the reject mass exactly equals the missing mass , so resampling perfectly refills whatever the accept branch under-emitted. That cancellation is the mathematical heart of losslessness.
Exercise 5.2
Design task. A colleague suggests: "Skip resampling on rejection — just re-run the draft one token deeper and try again." Explain in distribution terms why this breaks losslessness, and what the correct fix costs.
Recall Solution
The flaw: Re-running the draft still samples from , so the emitted token's distribution stays biased toward , not . From 5.1, the accept branch alone gives only — it undershoots wherever . Without the resample, tokens the target favors more than the draft are permanently under-represented → output distribution → not lossless. Correct fix's cost: you already have the target's full distribution from the same verification pass, so resampling from is essentially free (one categorical draw). There is no reason to skip it. The colleague's idea trades a free correction for a broken guarantee.
Recall Quick self-check ledger (reveal to grade yourself)
L1 lossless means distribution match ::: distribution match, not identical text L2 accept prob for q=0.6,p=0.15 ::: 0.25 L2 cycle cost K=3,cq=1,cp=8 ::: 11 units L2 can a q=0 token still be emitted ::: yes, via the resample branch max(0,p-q)/Z L3 E[tokens] alpha=0.7,K=4 ::: about 2.773 L3 speedup alpha=0.7,K=4,cp=10 ::: about 1.98x L4 Model A vs B speedups ::: 1.647x vs 2.060x, B wins L5 losslessness key cancellation ::: reject mass R equals normaliser Z
Prerequisites & neighbours: Transformer architecture · Beam search · Distributed training