Unit 3 of 11 · about 4 hours · derive

Fragmentation, page waste, capacity and KV compression

The KV bytes per token result is already known. The new question is how many physical blocks remain usable after tails, retained prefixes and allocator reserve consume the pool.

You'll be able to

  • Derive logical payload, allocated slots and page waste for a length distribution.
  • Account for reserve and shared physical blocks when predicting admission capacity.
  • Distinguish exact storage management from lossy KV quantization or token removal.

Covers

Internal and external fragmentationCapacity and page-waste derivationKV quantization and compression orientation
1

Read

In this order, only the parts listed

0 of 2 done

  1. PagedAttention: the three kinds of wasteEfficient Memory Management for Large Language Model Serving with PagedAttention

    arXiv:2309.06180v1, Section 3.1 with Figure 3; Sections 4.2 to 4.3 for on-demand blocks; Section 7.2 for block-size sensitivity

  2. Module 06: KV compression and qualityLLM Inference: From scratch, one token at a time - Modules 02 and 06

    KV cache quantization, and why K and V want different treatment, then the next concept, Measuring quality honestly

    Read for orientation only. Skip the Code lab and its expected concurrency figures: its paged append mutates length before capacity failure, and the driver ignores that failure. Do not use its utilization as evidence. Treat outlier patterns and quality costs as workload-dependent.

2

Focus on

  • For independent dense sequences of lengths , page size and payload bytes per token, allocated bytes are ; tail waste is . This excludes metadata and padding.
  • A nonempty private sequence wastes at most slots in its tail. Mean waste is only if length residues are uniform; block-aligned lengths waste none.
  • With sharing, count the union of physical blocks, not the sum of request-table lengths. Partition the pool into pinned, reclaimable cached and uncached free blocks; reserve reduces admission headroom.
  • Fixed-size pages avoid external fragmentation within one homogeneous pool. Quantization adds scales, packing and possible padding; dropping attended tokens changes the computation even at unchanged dtype.
Common mistakes · 3
  • Calling cached, evictable memory either permanently unavailable or simultaneously free and cached. Use disjoint accounting categories.
  • Assuming half a page of waste for every distribution. The length residues determine the mean.
  • Equating nominal bit width with allocated bytes or safe quality. Metadata and alignment affect capacity; attention and task checks govern quality.
3

Try it yourself

Closed-book, before checking anything

Derive and check a synthetic CPU ledger with no sharing: lengths [1, 4, 5, 8], block size 4, 64 payload bytes per token, and an eight-block pool with one block reserved. The requests use six blocks, 24 slots and 1536 bytes for 18 tokens of payload; tail waste is six slots or 384 bytes. One additional block is admissible after reserve. Then explain how sharing would change the physical-block count, not the per-token payload formula.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.What tail waste does an 8-token sequence have with block size 4?Answer

    None. Both pages are full; a full final page is not an extra empty page.

  2. 2.Two requests reference the same two prefix pages. How many physical pages do those references consume?Answer

    Two, not four. The references still need separate table entries and ownership accounting.

  3. 3.Does exact paging justify removing middle-context tokens to fit memory?Answer

    No. Paging changes addresses without dropping valid keys or values. Removing attended tokens changes attention and needs a separate quality contract.

You're done when

Your derived physical-byte and waste totals match the allocator ledger for aligned, partial and shared cases, with reserve and metadata stated separately.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 12 more in the section

Also in this section