Skip to content
mlmentorship

KV cache: reuse past projections, retain growing reads

Trace cached projections, growing attention reads, and exact storage costs during causal decoding.

Published · 6 min read ·Role-specific ·Advanced
Reading depth

Summary

A KV cache retains past keys and values so causal decoding need not project the same past positions again. Each new query still reads its allowed prefix.

Suppose three input positions are already cached. Process four more inputs, one at a time, with no eviction. Which work disappears, which work grows, and how much storage is needed?

The mechanism

For each new input, compute its Q, K and V, append K/V, then attend over valid positions. Q serves the current query; it is not normally retained in the persistent KV cache. The current forward pass predicts the next output token.

Earlier representations remain valid because causal masking prevents dependence on later inputs. Reuse requires compatible model weights, prefix, positions and attention rules.

Three positions already cached

Write one new pair while reading the growing cache

One new cache write does not mean one key readAt decode position 8, write one new key/value pair while attention reads eight available pairs. Persistent storage grows with the prefix.One new write; read the completeprefixQ7KV0KV1KV2KV3KV4KV5KV6KV77 retained; one appended; all 8 readReusing projections does not removeattention access.
Derivation

Follow four processed inputs after prefix length three. Each appends one K/V pair per KV head; its query reads all valid positions.

Decode stepCache beforeAppend onceNewest query reads
Input 4Positions 1–3K4, V41 new pairPositions 1–4
4 pairs
Input 5Positions 1–4K5, V51 new pairPositions 1–5
5 pairs
Input 6Positions 1–5K6, V61 new pairPositions 1–6
6 pairs
Input 7Positions 1–6K7, V71 new pairPositions 1–7
7 pairs

4 appends · 4 + 5 + 6 + 7 = 22 pair reads

appends stay constant per processed input; the newest query reads a growing prefix. Counts exclude creation of the initial three-position cache.
Check: Why are there only four new pairs but twenty-two attended pairs over the four steps?
Read it this way: At decode position 8, write one new key/value pair while attention reads eight available pairs. Persistent storage grows with the prefix.
How many old pairs are read when only one new pair is written?

Only four new position pairs are projected per KV head, rather than repeating projections over all 22 prefix positions. Newest-query attention still visits pairs per query head. A full-square recomputation baseline would form score entries, including masked entries. That baseline is not a claim about every uncached implementation.

The memory cost

Persistent K and V each have shape [B, G, S, d_head] per layer. For layers, KV heads, valid length and bytes per element:

KV cache size = 2 (K and V) * num_layers * num_kv_heads * d_head * sequence_length * batch_size * dtype_bytes

Take , , d_head=128, , , :

2 * 80 * 8 * 128 * 8192 * 1 * 2 = 2,684,354,560 bytes
										 = 2.5 GiB
										 = 2.68435456 GB

Batch 32 needs 80 GiB of ideal payload. Quadrupling that batch’s context to 32768 positions needs 320 GiB. These exclude allocator overhead, metadata and temporary buffers; the example is not a universal 70B layout.

Change stored heads or precision

1. Reduce KV heads from eight to one

New task: reduce persistent cache through architecture design or checkpoint adaptation.

Picture and change: change learned K/V projections and query-to-group mapping together. Do not relabel eight independent heads as one.

Check: ideal batch-one payload becomes GiB. Original outputs are not guaranteed; train or adapt, then evaluate.

Use when: the model design can change.

2. Store one-byte rather than two-byte elements

New task: reduce logical cache bytes without changing head count.

Picture and change: quantize writes, store scale metadata and use compatible reads. Keep positions and valid length unchanged.

Check: ideal payload halves to 1.25 GiB, excluding metadata. Measure output error and kernel costs.

Use when: logical payload, not fragmentation alone, exceeds capacity.

Failure and transfer

Changed weights or incompatible position encodings can invalidate cached keys. Bidirectional re-encoding can change earlier hidden states, making their old K/V stale.

Does fourfold context require fourfold persistent Q storage?

Check your answer

No. KV payload and each new query’s allowed read grow fourfold. The new Q still represents one position; it is not a growing persistent prefix cache.

What an interviewer expects you to say

State the operation before its complexity. Across lengths one through , cached dense attention visits newest-query pairs. Repeated full-prefix projection also accumulates quadratically; full-square score recomputation accumulates entries. Sliding windows or other attention rules change these counts.

Multi-query and grouped-query attention

MHA uses KV heads for query heads. MQA uses ; GQA shares each KV head among a query group. At fixed other dimensions, their payload ratios to MHA are and . Quality and runtime effects require evaluation, not a universal compromise claim.

Paged attention (vLLM)

Paged storage allocates blocks on demand and can reduce fragmentation relative to reserved contiguous buffers. Compatible identical prefixes may share blocks under a correct cache identity and ownership policy. Paging alone does not halve logical bytes per valid token.

Common confusions

Cache capacity does not establish context quality. KV reuse describes a causal attention mechanism, not every possible model or reusable inference state.

Why decoding is memory-bound

Small-batch decode can be limited by weight bandwidth; long contexts can expose KV traffic. Batching reuses weights but retains per-sequence cache work. Quantization and speculative verification have kernel, error and acceptance costs; verification is not free. Profile the workload using GPU memory accounting.

References: cache explanation, Pope et al., and PagedAttention.


Related: FlashAttention, Speculative decoding. Related interview: How would you reduce LLM inference cost by 10x?.