Module 4 · Lesson 4 of 5

PagedAttention: logical history, physical storage

Follow a processed token through its logical block, table entry and physical KV slot.

A sequence is logical; its storage can be scattered

Logical order says position 0 precedes position 1. Physical block IDs say where their K/V happens to be stored. A block table connects the two.

What you will be able to explain

Resolve token positions through a block table; predict boundary allocations; calculate slack and explain fragmentation.

A KV block contains K/V for C positions. C = 4 in this pool. For processed position t: logical block = floor(t / C), offset = t mod C. The table resolves logical block → physical block. For t = 6: logical 1, offset 2.

Each illustrated slot stores one K vector and one V vector, not the text/token ID alone. Labels p0, p1… identify persistent objects. Numerical K/V are invented first-layer/head rows; diagrams do not simulate learned projections or whole-model outputs.

With seven processed positions, one slot remains in the tail. Processing position 7 fills it. Processing position 8 allocates another block. A sampled output only gains K/V when processed in a forward step.

Two KV blocks hold seven valid positions; appending the eighth uses existing storage, the ninth needs a new block.

Payload, allocated capacity and slack

Raw KV payload counts valid stored values. Allocated capacity rounds each sequence to whole blocks. Tail slack is allocated space without valid rows.

Bytes per processed token = 2 × L × HKV × D × B
Without sharing: raw = bytes/token × Σ request lengths
Allocated = bytes/token × C × Σ ceil(length / C)

L = attention layers; HKV = KV heads; D = equal K/V vector width; B = bytes per stored number. Query heads do not appear. For unequal K/V widths, replace 2D by dk + dv. This aggregate byte formula does not require all layers physically stored together.

Illustrative L = 32, HKV = 8, D = 128, B = 2 gives 131,072 bytes = 128 KiB per processed token. Independent lengths 6 and 4 have 1.25 MiB raw payload. Four-token blocks allocate 1.5 MiB; 0.25 MiB is slack. Weights, runtime workspace, metadata and allocator padding excluded.

Compare a declared policy reserving 16 positions for each request: 32 slots = 4 MiB. That is a specific reservation baseline, not every contiguous allocator’s behavior.

Predict: KV paging removes unused tail slots entirely?

Explore block capacities 2, 4 and 8 with a fixed 32-token-slot pool, so physical byte capacity stays constant.

Three different sources of unused space

Internal slack, external fragmentation and reservation need different explanations. Show the allocation policy before comparing them.

Internal fragmentation
A live allocation contains unused slots; here, the partial final KV block.
External fragmentation
Free extents cannot satisfy a required contiguous allocation, despite enough total free space. Example: runs of 2, 1 and 2 slots cannot satisfy a 4-slot contiguous request.
Future reservation
Space deliberately set aside for possible growth; distinguish it from current valid payload.

Within a fixed-size KV pool, a new logical block can use any free physical block. The kernel follows the block table to gather K/V in logical order. Random physical IDs must not change which key/value belongs to each position.

logical = position // tokens_per_block
offset = position % tokens_per_block
physical = block_table[request][logical]
key = key_cache[physical][offset]
value = value_cache[physical][offset]

Actual key/value tensors may have different physical layouts; address calculation above is conceptual. Block size affects slack, mapping overhead and kernel layout. No universal optimum follows from this small pool.

PagedAttention §§4.1–4.3. Allocation failure needs admission, preemption, offload or another policy; paging does not create unlimited capacity. Those serving policies are later engineering work.

Takeaway: Paging removes the need for a contiguous run, not the reads: attention still reads every allowed K/V row.