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

Cache advance, rollback and stop handlingTokenizer and vocabulary compatibility
1

Read

In this order, only the parts listed

0 of 4 done

  1. 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.

  2. 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.

  3. 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 only

    The 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.

  4. 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_real with 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.

2

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.
3

Try it yourself

Closed-book, before checking anything

From a blank file, implement a cache as a token list with append and truncate: propose 4 tokens, reject at position 2, and show the cache holds only the accepted token's entry, the output holds that token plus the correction, and nothing else remains.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 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. 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. 4.When may draft and target use different vocabularies in vLLM v0.31.0?Answer

    Only with use_heterogeneous_vocab on the draft_model method (token-level intersection), which constrains drafts to shared tokens and supports greedy draft sampling only.

5

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 (a DynamicCache trimmed with crop, or a token-list stand-in), assert its length equals the accepted length after every round, then repeat with ngram_propose drafts scored as one-hot proposals

    Add 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

Also in this section