Keep cache validity aligned with the committed token history
Discard rejected candidate states while preserving accepted prefixes, explicit pending-token conventions, logical positions, and request isolation.
On this page
The question for today
What you’ll understand
- Distinguish committed output length from processed KV length.
- Explain linear rollback and selected-path cache compaction.
- Check target and draft state against their own model and position contracts.
A speculative verifier emits two accepted draft tokens and one correction, but the next target call still attends to keys from a rejected third proposal. Its history is now wrong even if the correction sampler was exact. Cache handling is part of decoding correctness. Track which tokens have been committed, which have actually been processed into KV state, and which temporary candidate states remain valid. Logical rollback is about removing forbidden history, not merely changing a displayed output string or freeing some bytes.
Define the processed-state convention explicitly
Many autoregressive loops retain KV for all processed tokens while the most recently emitted token is pending as the next input. Under that convention, committed text length can exceed cache length by one. Other APIs can expose a different boundary, so read the supported cache contract rather than assuming one universal integer relationship. Before a speculative round, identify the known prefix and any pending token. After verification, preserve precisely the processed states consistent with the accepted history, and ensure the correction or bonus token is processed before its hidden state is needed.
In a linear round with a fully processed prefix of length L and γ scored candidate tokens, suppose k<γ proposals are accepted before rejection. The correction produces a committed continuation of k+1 tokens, but its KV has not been computed by the candidate pass because that pass processed a different token at the failing position. Keep prefix plus the k accepted proposal states and treat the correction as pending. Pinned Transformers 4.56.2's assisted path crops target cache to the new output length minus one under its loop convention. Do not generalize that expression to unrelated cache APIs without checking their processed boundary.
Discard logical descendants and select actual paths
Keys and values for a rejected token and its descendants were computed using a history that will not be committed. A linear implementation can truncate the valid sequence range; a tree implementation may gather the accepted path into the cache layout and remove sibling references. Updating an attention length without fixing parent/path mappings can still expose stale states. Physical allocations may remain reserved for reuse, but the valid logical cache must exclude them. In paged systems, block tables, ownership, and reference counts determine which physical memory corresponds to valid sequence positions.
Target and draft states are different model computations unless a supported architecture deliberately shares them. The draft may have advanced farther along its own proposed path and must also be realigned with the corrected token history using its own processed-state convention. Merely copying target KV into an unrelated draft is not valid. Shared-prefix caching needs request isolation: rollback of one request must not mutate another request's committed prefix. Copy-on-write or other supported ownership mechanisms address physical sharing, while the decoder still maintains the semantic token and position mapping.
Verify cache behavior at every acceptance boundary
Compare small runs with a no-cache or independently recomputed target reference. Force rejection at the first, middle, and last proposal; test all-accepted bonus output, EOS, output caps, repeated token IDs, and tree branch switches. Check the next target distribution after rollback, not only the emitted prefix. Validate position IDs, attention masks, valid lengths, and model-specific cache types under the pinned version. A memory leak and a history-contamination bug require different diagnoses, even though both can appear after repeated speculative rounds.
The cache lab illustrates retention without supplying these semantic invariants. For each separate view, Reset, set both capacity C and stride s=4, predict the initial counts, then inspect. Capacity four yields twelve hits and four misses; capacity two yields sixteen misses. This exact LRU address model is not a KV cache, append/rollback algorithm, or page-table validator. It has no token history or pending state. Its limitation matters: a favorable reuse count cannot establish that a cached representation belongs to the committed context, so real rollback tests must compare actual model conditionals.
Work it through
Keep the correction pending under the declared convention
A fictional target has a fully processed 100-token prefix. It scores four proposal tokens A, B, C, D at positions 100 through 103. A and B pass, C fails, and the residual sampler emits correction Z. The candidate pass temporarily stored KV through D.
The committed sequence becomes the original prefix plus A, B, Z, so its length is 103. The correction Z occupies logical position 102.
Only the original prefix plus A, B has valid target KV, giving 102 processed tokens. Truncate or logically exclude the two temporary candidate states C and D; their contexts are not the committed continuation.
Process pending Z at position 102 on the next appropriate target call before using its resulting state. Align the draft independently to the same actual token history under its own cache boundary.
The new text length is 103 while valid target KV length is 102 in this convention. Keeping a 103-token cache from the candidate path would incorrectly retain C where the output contains Z.
The living notebook
Explore the model
Interactive lab · cache
Retention counts cannot establish token-history validity
For each view: Reset, set both C and s=4, predict, then inspect the initial sixteen requests. C=4 gives 12 hits/4 misses and C=2 gives 0 hits/16 misses. This fully associative LRU trace has no token positions, KV tensors, branch paths, or rollback operation.
- Hits / requests
- 0 / 16
- Hit rate
- 0%
- Address-stream period
- 16 addresses
A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forSemantic cache validity requires the actual committed model history and processed boundary. Hit rate and physical retention alone cannot prove it.
Pause & reason
Check your understanding
Your turn
Try it yourself
A fully processed prefix has length forty. Five proposals are scored, but only the first one passes before a correction is emitted. Give the new committed length, valid target KV length, pending-token position, and number of temporary proposal states to exclude.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use k=1 with the declared processed-prefix convention.
Compare with the solution
Committed length is 42, valid target KV length 41, and the pending correction is at zero-based position 41. Four temporary proposal states belong to rejected or downstream tokens and must be excluded from the valid history. Physical storage may remain reserved but cannot remain semantically visible.
Keep the idea
Take it with you
- Track committed tokens and processed KV as separate state boundaries.
- Remove rejected descendants and preserve only the accepted logical path.
- Validate the next target conditional, model identity, positions, and request isolation after rollback.
Read further
Original sources and deeper explanations to follow at your own pace.
- Transformers 4.56.2: Assisted Generation and Speculative Sampling Implementation
- Transformers 4.56.2: Cache and Position Semantics
- Miao et al.: SpecInfer Tree Verification, v 4
- Kwon et al.: Efficient Memory Management with PagedAttention
- Chen et al.: Accelerating Large Language Model Decoding with Speculative Sampling
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.