Frontier Lab Notes

Attention Mechanics and KV Cache

Attention and the KV Cache
Attention & the KV Cache Why autoregressive decode caches keys and values 1 · Prefill Process the whole prompt at once, fill the cache prompt: t₁ t₂ t₃ t₄ Q K V compute Q,K,V for all prompt positions KV cache stores K,V for t₁..t₄ cost ≈ O(T²·d) attention 2 · Decode Generate one token at a time; reuse the cache Qₜ only the new token cached K , V (t₁..t₄, tₜ) new tₜ₊₁ softmax(Qₜ·Kᵀ)·V over cached history → append new K,V 3 · Cache grows with sequence length step → KV bytes grows with batch · layers · KV heads · precision Saves recompute but costs memory — decode becomes memory-bandwidth-bound. MQA / GQA shrink the cache.

Why autoregressive decoding caches keys and values: prompt prefill fills the cache, each decode step runs one new query against cached K/V, and the cache grows with sequence length.

Attention is the core token-mixing mechanism in transformer LLMs. The KV cache is the key serving trick that makes autoregressive generation feasible.

Self-Attention In One Sentence

Each token asks a learned question, compares it against learned addresses for previous tokens, and takes a weighted sum of their learned content.

Q, K, V

Given hidden state h, linear projections produce:

Q = h W_Q
K = h W_K
V = h W_V

Then:

scores = QK^T / sqrt(d_head)
weights = softmax(mask(scores))
output = weights V

The causal mask forces position t to see only positions <= t.

Multi-Head Attention

Multi-head attention splits the representation into multiple heads. Each head can learn a different relation pattern.

Shape intuition:

h: [B, T, d_model]
Q/K/V: [B, n_heads, T, d_head]
attention weights: [B, n_heads, T, T]

The heads are concatenated and projected back to d_model.

Why Attention Is Expensive

For sequence length T, full attention forms a T x T attention matrix per head.

Training/prefill cost scales roughly:

O(T^2 * d)

This is why long-context models are expensive, and why Long Context and Efficient Sequence Models matters.

Autoregressive Inference

During generation, the model produces one token at a time:

x_1 -> x_2 -> x_3 -> ...

Without caching, each new token would recompute keys and values for all previous tokens. The KV cache stores prior K and V tensors so the model only computes new query/key/value projections for the latest token and attends over cached history.

KV Cache Tradeoff

The KV cache saves compute but costs memory.

Memory grows with:

This is why inference can become memory-bandwidth-bound rather than FLOP-bound.

MHA, MQA, And GQA

MHA: Multi-Head Attention

Each query head has its own key and value heads.

Pros:

Cons:

MQA: Multi-Query Attention

Many query heads share one key/value head.

Pros:

Cons:

Primary source: Fast Transformer Decoding: One Write-Head is All You Need.

GQA: Grouped-Query Attention

Query heads are partitioned into groups; each group shares key/value heads.

Pros:

Primary source: GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints.

FlashAttention

FlashAttention is not a new attention definition. It computes exact attention with IO-aware tiling so less data is moved between GPU memory levels.

Primary source: FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness.

Key idea:

Research Questions

Related