real-kv-cache -- what fills up as GPT-2 generates
When a model writes a reply it goes one word at a time, and rather than re-read everything for each new word it keeps notes on the conversation so far and adds one column to them per word. This page runs a real GPT-2 in your browser and shows those notes filling up for real: the prompt fills them in one go, then every generated word appends another column, with the megabytes counted as you go. That growing pile is why a long conversation costs more than a short one, why there is a limit on how much a model will hold at once, and why the memory on a graphics card is the thing that runs out. About 550 MB, downloaded only if you click.
Interactive page: the real counterpart to the synthetic kv-cache page. It shows GPT-2's actual KV cache as it generates. Anchor: the KV-cache concept. One of the real-model grounding pages, which run a real trained model instead of synthetic weights.
What it shows
As GPT-2 generates, every layer caches the Keys and Values for all past positions, so each new token only attends — it doesn't recompute the past. The cache grows one position per token: the prompt fills it in parallel (prefill), then each generated token appends one column (decode).
GPT2.cache (in ../real-attention/gpt2.js) returns the real per-layer K/V plus the next-token logits from one forward. The page draws:
- the actual cached K values for a chosen layer/head (
positions × 64 dims), with the prefill/decode boundary marked; - the real memory — using GPT-2's true dims (
2 × 12 layers × 768 dim × dtype) — climbing live as you generate, plus a KV-cache-vs-context-length curve (128 → 32K) showing why long context is expensive.
Pick a dtype (fp16 / fp32 / int8), hit generate, and watch both the cache and the megabytes grow.
Verified against PyTorch
GPT2.cache is checked against past_key_values by ../real-attention/gpt2.test.mjs: the per- layer/head K and V tensors match PyTorch within max|Δ| ≈ 5e-5 (fixture gpt2-groundtruth.fixture.json).
Render tier
T2 (Canvas2D K-cache heatmap + memory bars; the "compute" tier is the GPT-2 forward in JS, one per generated token).
Wiring
layout.mount + controls.text('prompt') + layer/head steppers + a dtype select + gen tokens + prefill / generate / "load real GPT-2" buttons. Reuses gpt2.js cache; async, degrades to a labelled synthetic K cache offline (the memory math is real either way — the dims are GPT-2's). Two challenges (?ch=N): load the real model, and grow the cache by ≥ 8 decode positions. Headless hooks: ?prompt=, ?ids=, ?gen=N, ?layer=/?head=/?dtype=, ?real=0. Source: page.js.