Unit 7 of 7 · about 2 hours · explain

Beyond one cache: prefix sharing, paging and speculation

Single-request caching stops at the serving door. Shared prefixes, paged blocks and draft verification are the vocabulary that later tracks use to reason about real serving.

You'll be able to

  • Explain prefix sharing, paged blocks with block tables and copy-on-write vocabulary without implementing a server.
  • Explain the speculative-decoding promise (draft guesses, target verifies in parallel) and its exactness contract (output equals target-only sampling).
  • Draw a small block-sharing diagram for a shared prefix and a divergence.

Covers

Prefix sharing and paged-cache vocabularySpeculative decoding promise and exactness contractOne small prefix-sharing state diagram
1

Read

In this order, only the parts listed

0 of 3 done

  1. CS336 Lecture 10: paged attention and speculative samplingStanford CS336 - Lecture 10 Inference

    paged_attention (blocks, fragmentation, sharing the system prompt and parallel samples, copy-on-write at block level) and speculative_sampling (draft guesses 4 tokens with target verifying in parallel, exact-sample guarantee with the proof, 70B/8B and 8B/1B pairs, Medusa and EAGLE)

  2. Modern LLM Notebook 21 Section 7: PagedAttention wasteModern LLM Notebook - KV cache and inference acceleration

    Section 7 (contiguous versus paged diagram with fixed-size pages, simulate_memory with block_size 16 showing paged waste 3 to 5 percent versus contiguous 13 to 20 percent)

  3. Modern LLM Notebook 23 Sections 2 and 3: acceptance and exactnessModern LLM Notebook - KV cache and inference acceleration

    Section 2 (draft guesses tokens, target verifies in one pass) and Section 3 (accept_prob as ) with Exercise 1 (0.5 versus 0.8 giving 1.0)

    Do Sections 2 and 3 and Exercise 1 only. Skip Exercises 2 and 3: their expected-token formula drops the extra token the target always contributes, so their numbers disagree with the ceiling. Skip draft-model training and variants; they belong to later tracks.

2

Focus on

  • Sharing needs exact token equality on the prefix. One differing token ends sharing after that point, and only later blocks diverge.
  • Pages are fixed-size blocks with a table from logical to physical positions. Waste per sequence caps at instead of .
  • Forks share prefix pages and copy on write. Only the diverged block is copied, not the whole prefix.
  • Speculation is exact: the acceptance ratio plus the residual correction reproduce target-only sampling. Speed depends on draft quality, not on luck.
Common mistakes · 3
  • Treating speculative decoding as approximate sampling. Unverified draft tokens change the distribution; only the accept plus correction loop preserves it.
  • Expecting sharing across different prefixes. Similar text with different token ids shares nothing; the match is on tokens, not meaning.
  • Paging away the math. Blocks only relocate storage; attention still gathers the same and , so outputs must match contiguous storage exactly.
3

Try it yourself

Closed-book, before checking anything

Draw the shared-prefix block table for two requests over an 8-token prefix with block_size 4, show the copy-on-write step on divergence, and state the speculative exactness contract with the acceptance formula.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Two requests share an 8-token system prefix with block_size 4. How many physical prefix blocks with sharing versus without?Answer

    2 shared blocks versus 4 (2 per request). Sharing saves half the prefix memory, and a divergence copies only the affected block.

  2. 2.Draft assigns token 0.5 and target assigns 0.8. What is the acceptance probability?Answer

    , so always accept.

  3. 3.A draft proposes tokens and each is accepted with probability 0.5, independently. How many tokens does one target pass yield on average, and what is the ceiling?Answer

    , counting the token the target always adds after the last accepted draft token. A perfect draft gives . Notebook 23's exercises use and drop that token, so trust the form.

  4. 4.Does speculative decoding change the output distribution?Answer

    No. With the residual correction it is exact and equals target-only sampling. It changes speed, not the distribution.

5

Practice

  • Modern LLM Notebook 23 Exercise 1: acceptance probability

    Exercise 1 (acceptance probability for draft 0.5 versus target 0.8, asserting 1.0)

    Skip Exercises 2 and 3, whose expected-token formula is off by one token. Then compute the expected tokens per pass for at acceptance 0.5 and 0.8 with the form, and draw the 8-token shared-prefix diagram from the reproduce step.

You're done when

Exercise 1 passes, your expected-token numbers use the form, and your diagram shows shared blocks plus the copy-on-write step with the exactness contract stated.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 4 more in the section
  • Modern LLM Notebook - KV cache and inference accelerationWalking Labs · required

    Use the KV-cache portions of English notebooks 11-mla-kv-cache and 21-inference-acceleration. Skip MLA and advanced acceleration as core work. Recalculate cache sizes and verify timing after warmup and explicit synchronization.

  • Stanford CS336 - Lecture 10 InferenceStanford University · required

    Use the Spring 2026 inference lecture to connect KV arithmetic, phase behavior and batching vocabulary. Keep kernels, distributed serving and expensive performance experiments for the later tracks.

Also in this section

  • LLMs from Scratch companion - KV cache and GQASebastian Raschka · primary

    Use ch04/03_kv-cache and ch04/04_gqa with the chapter model code. Add explicit per-layer ownership, position offsets, variable lengths, EOS compaction and MHA-versus-GQA cache arithmetic to the runnable comparison.

  • How to Scale Your Model - inference chapterJAX ML · depth

    After cache equivalence and phase measurement pass, read the inference chapter for arithmetic intensity, parallelism and production-scale reasoning. Treat it as the handoff to inference and performance tracks, not required foundation work.

  • AI Performance Engineering - inference-engine bridgeWafer · depth

    Use Sections 4 and 5 only after this gate to choose the next inference, KV-cache, scheduling, quantization or distributed-serving resource. Do not treat its GPU and kernel material as unfinished LLM Foundations work.

  • No Magic - micro KV or paged-cache simulationNo Magic · optional

    Use one small CPU simulation such as microkv.py or micropaged.py only to visualize state ownership. Do not present Python timing as GPU or serving-performance evidence.