Unit 1 of 11 · about 4 hours · explain, derive
Per-layer KV layouts and the cache consequences of attention variants
Building a KV cache and KV memory arithmetic supplied the single-request contract. Serving now needs a shared pool whose layer views, attention variants and state checkpoints remain compatible under reuse.
You'll be able to
- Map a serving engine's per-layer cache views to physical storage and distinguish layout from ownership.
- Derive dense and latent cache payloads without expanding shared KV heads in storage.
- Explain why hybrid attention and recurrent layers need different retention and prefix-checkpoint contracts.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Nano-vLLM: the pool and its layer viewsNano-vLLM - Block Manager and Sequence Lifecycle
ModelRunner.allocate_kv_cache:block_bytes, the six-dimensional allocation, and assignment tok_cacheandv_cacheTrace the single-worker case. Skip startup, distributed communication and graph capture.
- Module 06: architectural cache reductionLLM Inference: From scratch, one token at a time - Modules 02 and 06
MQA to GQA to MLA: attacking the formula itself, including the latent and decoupled RoPE cache diagram
Stop before weight quantization. Treat reported architecture savings as model-specific, not a serving toggle or a quality guarantee.
- vLLM 0.31.0: per-layer state specificationsvLLM 0.31.0 - cache specifications and connector lifecycle
AttentionSpec,MLAAttentionSpec,SlidingWindowSpec,MambaSpecandKVCacheGroupSpec; inspect their state shapes, page sizes and grouping contractsRead these classes only. Skip packed hardware-specific formats and the general layout-construction helpers.
Focus on
- Nano-vLLM allocates key/value, layer, physical block, token slot, KV head and channel axes. A block ID selects corresponding storage across layers; it does not merge their projections.
- For dense caches, sum per-layer payloads as bytes per token. MHA, GQA and MQA change stored KV heads, not the number of query heads.
- A conventional MLA cache stores the shared KV latent and the decoupled positional key, not two full per-head tensors. Its payload is per layer per token, before padding or metadata.
- Hybrid layers can retain full history, a bounded window, or recurrent state at a checkpoint. A reusable boundary must provide valid state for every layer type, under the same positions and model version.
Common mistakes · 3
- Expanding GQA keys in the persistent cache because queries have more heads. Expansion for computation is not the storage contract.
- Applying the dense key-plus-value formula to an MLA latent or a recurrent state. Count the actual stored representation.
- Reusing a hybrid prefix because the full-attention layers hit. Missing recurrent checkpoints or window state can still make that boundary unusable.
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.Does sharing one block ID across layers mean that their keys are equal?Answer
No. The ID selects different per-layer storage regions. Each layer still has its own projections and cached values.
2.Why is there no extra factor of two in the conventional MLA payload formula?Answer
The single latent jointly represents key and value content. Count it once, then add the separately stored positional key.
3.Can a recurrent checkpoint at one prefix boundary stand in for an earlier boundary?Answer
Not generally. It summarizes the history at its own boundary. An earlier state needs its own checkpoint or recomputation; recurrent updates need not be invertible.
You're done when
Your state descriptors reconcile the two synthetic payloads and reject a reuse boundary with missing layer state, without rebuilding the earlier single-request cache.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 11 more in the section
- Nano-vLLM - Block Manager and Sequence LifecycleGeeeekExplorer · required
Trace nanovllm/engine/block_manager.py, sequence.py, scheduler.py and attention.py. Draw allocate, append, share, preempt and free transitions, then implement randomized allocator tests in your own project before touching production-engine code.
- LLM Inference: From scratch, one token at a time - Modules 02 and 06Tushar Amrit · support
Use The KV Cache for fragmentation and reuse and Memory Optimization for paging, prefix sharing and KV compression. Work the checkpoints before the matching units.
- vLLM 0.31.0 - cache specifications and connector lifecyclevLLM Project · reference
Read the per-layer attention, MLA and Mamba specifications for hybrid state contracts; read the V1 connector metadata, load synchronization and deferred-free methods for transfer ownership. These fill the pool's hybrid-state and connector-contract gaps, without requiring distributed execution.
Also in this section
- Efficient Memory Management for Large Language Model Serving with PagedAttentionvLLM Project · primary
Read the memory-waste motivation, block-table design, sharing and copy-on-write sections, then reproduce the logical-to-physical translation on paper. Separate the paper's reported workloads from results on your engine and hardware.
- SGLang: Efficient Execution of Structured Language Model ProgramsSGLang Project · required
Study RadixAttention, longest-prefix matching, eviction and cache-aware scheduling. Compare its sequence-level radix index with vLLM's block-level paging instead of treating them as competing names for one mechanism.
- Hands-On LLM Serving and Optimization - Prefix Caching and Advanced KV CachingChi Wang and Peiheng Hu (O'Reilly) · required
Read "Prefix Caching" in Chapter 6 (RadixAttention, use cases, best practices, scaling the prefix cache) and "Advanced KV Caching" in Chapter 7 (long context, cost and latency, LMCache). Separate exact-reuse mechanisms from capacity extensions such as offload.
- AI Systems Performance Engineering - Chapter 18, tuning KV-cache utilizationChris Fregly (O'Reilly) · support
Read "Tuning KV Cache Utilization and Management" in Chapter 18 and "Prefix Caching" in Chapter 16. Leave the disaggregated pool and transfer sections for INF-7.
- Mini-SGLang - Radix Cache and Cache TestsSGLang Project · required
Read docs/structures.md, the cache allocation code and tests/core/test_cache_allocate.py. Compare exact prefix identity and lifecycle with Nano-vLLM, then add tenant and version fields to your own prefix index.
- vLLM Automatic Prefix Caching DesignvLLM Project · required
Check block hashing, extra hash keys (multimodal inputs, LoRA, cache salt), isolation and eviction against v0.31.0, the version pinned here; cache-key fields change between releases.
- SGLang documentation - Radix cache eviction and HiCacheSGLang Project · support
Use the radix eviction policy and session radix cache pages to compare eviction choices with vLLM, and the HiCache design page for a GPU, host and storage hierarchy.
- AI Performance Engineering - KV Cache SystemsWafer · reference
A curated index. Use Section 4 only to find a primary KV-cache source the other entries lack.
- 100 Days of LLM Inference - Days 5, 9, 10 and 16Elizabeth Thomas · support
Use the four notebooks for KV arithmetic, paged allocation, radix reuse and prompt-layout experiments. Re-run conclusions against your own property tests and workload because these notebooks are a supplementary learning journal.
- TensorTonic Language Modeling from Scratch - Find Reusable Prefixes in a KV TrieTensorTonic · support
Solve after the prefix-index unit and add the cases its public tests leave out to your own property tests.
- LLM Inference Systems Laboratory - KV StateRoadmap-Final · support
Implement the paged allocator and prefix index with differential, randomized, cancellation, collision, fork and pressure tests. Retain one reuse workload that improves hit rate but not goodput.