DASTANZAR / MLSYS
kv desk / attention memory

Spend less attention memory.

Every token a model reads back is stored — the KV cache. It is usually the thing that runs a GPU out of memory first. Four measured moves make it smaller: share what repeats, keep the tokens that matter, prune the budget, and compress the rest. One budget, three receipts.

The decision

Where does the memory go, what does cutting it cost in accuracy, and which move is free?

receipts / pagemem · kvstamp · pagedattn
all values read from committed receipts
share → choose → prune → compress
01

Share

Eight conversations share one 512-token system prompt. If each reserves the whole prompt, that is 504 blocks. Paged allocation stores the shared prefix once.

204 blocks to hold eight conversations
preallocated504 blocks
paged + shared204 blocks

Prefix hit rate 100%; external fragmentation 0.98%. Savings: 59.5% fewer blocks.

02

Choose

When memory is tight you keep a fixed number of tokens and drop the rest. Which ones you keep is the whole game: at the same 256-token budget the policy decides how much future attention survives.

96.4% of future attention retained at 256 kept
lru16.5%
h2o96.4%
sink + window37.9%
03

Prune

Compression is a budget, not a free lunch. Keep fewer tokens and the output drifts. Drag the budget and watch the error knee appear.

budget 256 / 512kept 50.0%
0.383 relative output error

At this budget the cache is 2.0× smaller (derived from kept fraction). Full FP16 KV is 128 KiB; here it is 64 KiB (derived).

04

Compress

Prune first, then store what is left in fewer bits. At a 64-token budget the KV cache drops from 128 KiB to 16 KiB — an 8× cut.

8.0× smaller KV cache at budget 64 / 512
full fp16128 KiB
at budget 6416 KiB

The layout is the cheap part to change: streaming paged attention matched dense attention on 9/9 parity cases at a max absolute error of 2.4e-7. Same answer, different memory layout.

The read

Share the prefix first — it is free.

Sharing the repeated system prompt cuts the block count 59.5% with no accuracy cost. Pruning and compression are where accuracy is spent, so they come second. And the choice of policy — not just the budget — moves retained attention from 16.5% to 96.4%.

59.5% fewer blocks from sharing (free)
5.8× attention retained: best policy vs worst (derived)
8.0× smaller KV at budget 64
9/9 paged-attention parity cases
Sources pagemem · kvstamp · pagedattn receipts Exact: blocks, retained attention, kept fraction, output error, parity Derived here: bytes, compression, policy ratio Open receipt ↗