Token LabBuscar

SYSTEMS / ENGINEERING BRIDGE

KV memory, paging and serving deadlines

Separate allocation efficiency from model compute and queueing delay.

Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

This technical article displays the English edition.

Mechanism & assumptions

PagedAttention addresses KV allocation and sharing. It does not erase the attention computation or guarantee a latency target. Measure prefill and decode separately; include queue time in user-visible latency. Prefix caching only helps when the prefix and cache identity match; account for eviction and isolation.

Text / 原始公式
KV bytes = 2 × layers × KV_heads × head_dim × tokens × bytes_per_element
pages = ceil(tokens / block_size)
unused_slots = pages × block_size − tokens

Worked example

For 32 layers, 8 KV heads, head dimension 128, 4096 tokens and 2-byte elements, KV is 536,870,912 bytes = 512 MiB per sequence (excluding metadata). At block size 16, a 33-token sequence allocates 48 slots and wastes 15. Paging reduces contiguous-allocation requirements but still has final-block slack.

Implementation exercise

Compare block sizes 8/16/32 on a fixed list of sequence lengths. Report slot utilization, page-table entries and a separate measured latency profile; allocation arithmetic cannot predict GPU speed.

Deliver code, fixed seeds, raw results and one counterexample. These are experiment specifications, not executed GPU benchmarks.

Why can tokens/second improve while p99 worsens?

Larger batches can improve device utilization while increasing queueing or interference. Fix arrival process and input/output distributions; report throughput and latency together.

arXiv ↗ · PDF ↗

Original teaching examples; sources checked October 2026. These are foundational mechanisms relevant to engineering, not a claim to cover every latest model.

AI/LM curriculum & checkpoints →Systems index →
MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Count both K and V, KV heads rather than query heads, and sequence×batch exactly once. The byte formula excludes activations, weights, allocator fragmentation and attention workspace.

Formula conditions, units and worked arithmetic reviewed. The implementation isolates one numerical mechanism; it does not train a policy or control real hardware.

Per-layer K and V tensors → KV heads × width × token count → Multiply layers and bytes/value → 512 MiB logical cache · add overhead
Mechanism map: follow the transformations from top to bottom.

Before running: predict a boundary case

Double batch doubles MiB; change query heads alone and KV storage stays unchanged.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node kv-serving.mjs. No third-party runtime dependencies.

const solve=()=>{
const layers=32,kvHeads=8,headDim=128,sequence=4096,batch=1,bytes=2;
return 2*layers*kvHeads*headDim*sequence*batch*bytes/2**20;
};
const actual=solve();
const expected=512;
if(JSON.stringify(actual)===JSON.stringify(expected)){console.log('PASS',actual)}else{
const a=[actual].flat(), b=[expected].flat();
if(a.length!==b.length||a.some((v,i)=>!Number.isFinite(v)||Math.abs(v-b[i])>1e-10))throw Error('Numerical mismatch');
console.log('PASS within 1e-10',actual);
}
Reference output — reveal after predicting
512

Test and transfer

Double batch doubles MiB; change query heads alone and KV storage stays unchanged.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope