Unit 2 of 11 · about 4 hours · explain, derive

Logical tokens, paged storage, block size and block tables

Variable-length, paged and decode attention supplied the kernel mapping. Here the same mapping becomes an allocator contract that must survive growth, reuse and physical-page permutation.

You'll be able to

  • Translate logical token positions through a sequence block table into physical KV slots.
  • Explain how a contiguous physical pool supports noncontiguous per-request storage.
  • Defend block size using tail waste, table overhead and reuse granularity.

Covers

Logical KV tokens and physical allocationContiguous versus paged KV storageBlock size and block-table address translation
1

Read

In this order, only the parts listed

0 of 2 done

  1. PagedAttention: storage and translationEfficient Memory Management for Large Language Model Serving with PagedAttention

    arXiv:2309.06180v1, Sections 4.1 to 4.3, Figures 5 to 7; Section 7.2, Impact of Block Size

    Trace storage and address translation. Use your existing attention oracle rather than the displayed block-softmax equation; retain normalization over valid tokens. Treat the block-size experiments as the paper's workloads, not a universal optimum.

  2. Nano-vLLM: from tables to write locationsNano-vLLM - Block Manager and Sequence Lifecycle

    prepare_block_tables, prepare_prefill and prepare_decode: positions, context lengths and slot_mapping

    Read the metadata construction only; no engine launch or kernel rewrite is needed.

2

Focus on

  • For zero-based token position and block size , logical block is and offset is . The table entry, not the logical block number, selects physical storage.
  • The pool itself can be contiguous while each request's blocks are scattered. Paging removes the requirement for a contiguous request-sized allocation, not all contiguous memory.
  • Valid length is separate from allocated capacity. A full last page has occupancy equal to the block size, not zero; an empty request needs an explicit convention.
  • Smaller pages reduce tail waste and make block-based reuse finer. They also increase table entries and management work. Measure the trade-off rather than assuming the smallest page wins.
Common mistakes · 3
  • Using physical slot numbers as RoPE positions. Relocation must not change logical positions.
  • Reading the whole final page. Its unused slots can contain stale values from an earlier owner.
  • Expecting bit-identical floating-point attention after changing kernels. Storage relocation preserves the mathematical inputs; reduction-order differences still need the earlier numerical contract.
3

Try it yourself

Closed-book, before checking anything

Closed-book, place a 10-token synthetic request with block size 4 on physical blocks [5, 1, 7]. List all token locations: token 9 is block 7, offset 1, flat slot 29. Gather integer token labels on CPU, permute physical blocks while updating the table, and require identical gathered labels. Keep absolute positions unchanged and verify that unused tail slots never appear in the gather.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.With block size 4 and table [5, 1, 7], where is token 4?Answer

    Logical block 1 maps to physical block 1, offset 0, flat slot 4.

  2. 2.Does moving a physical block require recomputing positional keys?Answer

    No, if the values and logical positions are preserved. Moving storage is not moving tokens within the sequence.

  3. 3.Why can free space in separate pages admit a request when a contiguous allocator cannot?Answer

    The page table joins disjoint fixed-size pages into one logical sequence. A contiguous allocator needs one sufficiently large free interval.

You're done when

Your CPU gather preserves every valid token under physical-page permutation and excludes tail slots, and you can translate an unseen table without source code.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 12 more in the 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.

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

Also in this section