Unit 8 of 12 · about 4 hours · explain, implement
Cache advance, rollback, stop handling and tokenizer compatibility
Acceptance math means nothing if rejected tokens stay in the cache. Rollback and stopping are where exact sampling meets request state.
You'll be able to
- Implement cache advance for proposed tokens and exact rollback to the confirmed prefix on rejection.
- Handle stop tokens inside accepted drafts without leaking later tokens into the output or the cache.
- State the tokenizer compatibility contract and the one supported exception, with its sampling limit.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Detailed steps: discard and restartHands-On LLM Serving and Optimization - Quantization (Chapter 6) and Speculative Decoding (Chapter 7)
Chapter 7, Speculative Decoding: Detailed Steps (printed pp. 234 to 235): draft speculation with the 0.6 versus 0.8 and 0.4 acceptance examples, discarding all following draft tokens on rejection, and the Token3 correction restart
The book states the logical discard; your unit adds the KV entries that must go with it.
- Verification divergence and backtrackingAI Systems Performance Engineering - Chapters 15 and 16, speculation and quantization
Chapter 15, Speculative Decoding and Parallel Token Generation Techniques: the draft-verification divergence paragraphs (at least one token kept per round) and the Medusa backtracking note
Read for the discard-and-correct contract, not for the headline speedup multiples.
- vLLM speculation methods and vocabulary handlingvLLM Speculative Decoding and Quantization (v0.31.0)
Method Selection at a Glance table and the N-gram keys (
prompt_lookup_min,prompt_lookup_max), then Cross-Vocabulary Draft Models (TLI): shared-token constraint with greedy draft sampling onlyThe Day 15 note that vLLM V1 supports only n-gram and EAGLE is outdated; the v0.31.0 method list (EAGLE, MTP, draft model, PARD, MLP speculator, n-gram and suffix decoding) supersedes it.
- Day 15: end-to-end loop with bonus and stop100 Days of LLM Inference - Days 13, 14 and 15
Notebook
day15/speculative-decoding.ipynb: End-to-End Three-Step Loop with Real Models (speculative_decode_realwith the bonus token and the stop-token break)The loop reruns both models over the full sequence with no KV cache, and it appends the whole accepted run before its stop check, so tokens after an accepted stop token leak into the output.
Focus on
- Verification writes KV entries for every proposed token. On rejection, truncate the cache to the accepted prefix; the correction or bonus token is emitted now, and its KV is written by the next forward pass. Nothing past the first rejection survives.
- Each round emits at least one token and at most . The bonus token comes from at the final position, already computed by verification.
- A stop token inside the accepted prefix ends the sequence there. Later drafts are discarded even if verification accepted them; acceptance never overrides stopping.
- Draft and target share one tokenizer by default. The v0.31.0 cross-vocabulary path constrains drafts to shared tokens and supports greedy draft sampling only.
Common mistakes · 3
- Keeping rejected KV entries for speed. Stale entries corrupt every later attention computation silently; truncate to the confirmed prefix first.
- Emitting drafts past a stop token. Verification acceptance is about distribution match, not sequence end; check stopping on the accepted prefix before extending.
- Pairing a draft and target with different tokenizers. vLLM requires a shared vocabulary unless the TLI mapping is on; in your own loop, mismatched ids keep the output exact but collapse acceptance, so a correct output can hide a slowdown.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Four tokens are proposed and the second is rejected. Which KV entries survive?Answer
Only the entry for the first, accepted token. Entries for proposals two to four are discarded; the correction sampled at position two is emitted, and its entry is written by the next round's forward pass.
2.All four drafts are accepted. How many tokens does the round emit, and why?Answer
Five: the four drafts plus a bonus token sampled from the already-computed target distribution at the final position.
3.An accepted draft contains the stop token at position two of four. What is emitted?Answer
The prefix through the stop token. Positions three and four are discarded; the sequence ends regardless of their acceptance.
4.When may draft and target use different vocabularies in vLLM v0.31.0?Answer
Only with
use_heterogeneous_vocabon thedraft_modelmethod (token-level intersection), which constrains drafts to shared tokens and supports greedy draft sampling only.
Practice
- 100 Days of LLM Inference Day 15: end-to-end loop
End-to-End Three-Step Loop cells: add a KV cache to
speculative_decode_real(aDynamicCachetrimmed withcrop, or a token-list stand-in), assert its length equals the accepted length after every round, then repeat withngram_proposedrafts scored as one-hot proposalsAdd a planted stop-token case and fix the notebook stop check, which appends the whole accepted run first; your rollback assertion is the deliverable.
You're done when
Your cache holds entries only for accepted tokens after rejections, stop tokens and all-accepted rounds, under an assertion that fails on any residue, and no token after a stop token reaches the output.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 9 more in the section
- Hands-On LLM Serving and Optimization - Quantization (Chapter 6) and Speculative Decoding (Chapter 7)Chi Wang and Peiheng Hu (O'Reilly) · primary
Read "Model Compression" in Chapter 6 for quantization and "Speculative Decoding" in Chapter 7, with its hands-on example. For each method write its correctness contract, the engine support it needs and a predicted winning and losing workload before running it.
- AI Systems Performance Engineering - Chapters 15 and 16, speculation and quantizationChris Fregly (O'Reilly) · support
Read "Speculative Decoding and Parallel Token Generation Techniques" in Chapter 15 (draft models, EAGLE, self-speculation, Medusa) and "Quantization Approaches for Real-Time Inference" in Chapter 16.
- vLLM Speculative Decoding and Quantization (v0.31.0)vLLM Project · required
Use the speculative decoding page for the supported proposers (draft model, n-gram, EAGLE, MTP) and its losslessness notes, and the quantization page for the hardware support table. Check every method against your GPU's compute capability before planning a run.
- 100 Days of LLM Inference - Days 13, 14 and 15Elizabeth Thomas · support
Use the numeric-format, quantization-algorithm and speculative-decoding notebooks as compact preparation. Keep their synthetic demos separate from production-engine and hardware evidence.
Also in this section
- MIT 6.5940 TinyML and Efficient Deep Learning Computing - Quantization LecturesMIT Han Lab · required
Use Lectures 5 and 6 for quantization fundamentals and Lecture 13, Efficient LLM Deployment, for SmoothQuant and AWQ with TinyChat. Implement a small affine quantizer and an outlier experiment before using a pre-quantized checkpoint.
- SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language ModelsMIT and NVIDIA · required
Study activation outliers and the offline smoothing transformation. Reproduce the algebra on one linear layer and compare calibration-sensitive quality with an unmodified reference.
- AWQ: Activation-Aware Weight Quantization for LLM Compression and AccelerationMIT Han Lab · required
Focus on salient-weight protection, group scaling and the distinction between simulated quality and real kernel speed. Compare one supported AWQ path with the same unquantized workload.
- Fast Inference from Transformers via Speculative DecodingDeepMind · required
Derive acceptance and correction probabilities and implement the exact categorical simulator. Prove distributional equality on tiny distributions before adding a model or engine.
- AI Systems Performance Engineering companion code - Chapters 16, 18 and 19 labsChris Fregly · required
Use Chapter 16 AWQ, GPTQ and SmoothQuant pairs, Chapter 18 speculation sweeps and Chapter 19 dynamic quantized cache. Run one family at a time, require quality or numerical parity, and regenerate results on available hardware.
- TensorTonic Language Modeling from Scratch - Perform Exact Speculative SamplingTensorTonic · support
Solve after the acceptance-rule unit, then check your implementation's output distribution against the target by exact enumeration on a tiny vocabulary.
- LLM Inference: From scratch, one token at a time - Module 08Tushar Amrit · support
Work the Speculative Decoding module's acceptance arithmetic and checkpoint before the speedup-model unit.
- AI Performance Engineering - Quantization and Speculative DecodingWafer · reference
A curated index. Use Section 4 only to find a quantization or speculation paper the other entries lack.
- Inference Engineering - Section 5.1, QuantizationPhilip Kiely (Baseten Books) · support
Read 5.1.1 Number Formats for the format table and Precision/Type/Scale-factor with Dynamic range/Granularity, 5.1.2 for PTQ versus QAT and the what-to-quantize decisions, and 5.1.3 for the perplexity, benchmark and custom-eval quality checks.