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

Per-layer KV tensor layoutsMHA, GQA, MQA and MLA cache consequencesHybrid attention and state-space cache identity
1

Read

In this order, only the parts listed

0 of 3 done

  1. 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 to k_cache and v_cache

    Trace the single-worker case. Skip startup, distributed communication and graph capture.

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

  3. vLLM 0.31.0: per-layer state specificationsvLLM 0.31.0 - cache specifications and connector lifecycle

    AttentionSpec, MLAAttentionSpec, SlidingWindowSpec, MambaSpec and KVCacheGroupSpec; inspect their state shapes, page sizes and grouping contracts

    Read these classes only. Skip packed hardware-specific formats and the general layout-construction helpers.

2

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

Try it yourself

Closed-book, before checking anything

Closed-book, draw Nano-vLLM's pool and layer views for one worker. For a synthetic two-layer model with 8 query heads, 2 KV heads, equal key/value width 4 and two-byte elements, derive 64 bytes per token across layers. For a separate two-layer MLA model with latent width 6 and positional-key width 2 at the same dtype, derive 32 bytes. Label these payloads, not engine allocations; explain which state a hybrid recurrent layer would add.
4

Quiz yourself

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

0 got · 0 missed

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