Efficient Attention

The two costs of long context — quadratic attention compute and linear KV-cache memory — each have a family of solutions, and together they're why modern models can handle context lengths that were impossible a few years ago. Grouped-query attention shrinks the KV cache; FlashAttention computes exact attention far faster; sliding-window and sparse patterns break the quadratic. This post covers the techniques that made long context practical.

The previous post laid out the walls: O(n²) attention compute and a linearly-growing KV cache. This post covers the ingenuity that gets past them. The techniques split cleanly by which cost they attack — some shrink the KV-cache memory, some speed up the attention computation, some reduce how many token-pairs attention considers at all. Knowing which does which is the point.

Shrinking the KV cache: MQA and GQA

The KV cache stores keys and values for every token, for every attention head (post 2). A direct way to shrink it: have fewer key/value heads.

GQA is a great example of a cheap, widely-adopted win: a small architectural change that multiplies how much context and concurrency you can fit in memory, at little quality cost. It targets the KV-cache (memory/generation) cost, not the quadratic compute.

Computing exact attention faster: FlashAttention

A different and important idea: make the exact attention computation faster and more memory-efficient without changing what it computes. This is what FlashAttention does — and the distinction matters: it’s not an approximation. It computes the same attention, but rearranges how.

The insight is that attention on GPUs was bottlenecked not by raw compute but by memory traffic — repeatedly reading and writing the large intermediate attention-score matrix to slow high-bandwidth memory. FlashAttention is IO-aware: it computes attention in tiles that stay in the GPU’s fast on-chip memory, never materializing the full n×n score matrix in slow memory. The result is attention that’s substantially faster and uses far less memory, with identical mathematical output.

Because it’s exact, FlashAttention was adopted essentially universally — it’s a free speed-and-memory win with no quality tradeoff, which is rare. It doesn’t change the O(n²) arithmetic fundamentally, but it slashes the constant factors and the memory footprint enough to make long-context training and inference far more practical. The lesson it embodies: sometimes the win isn’t a cleverer algorithm but a hardware-aware implementation of the same one.

Breaking the quadratic: sparse and local attention

The remaining family attacks the O(n²) directly by not attending to every pair — restricting which tokens each token attends to, so the number of pairs grows less than quadratically:

These approximate full attention (unlike FlashAttention), trading some ability to attend to arbitrary distant tokens for a sub-quadratic cost. The tradeoff is real — a token can’t directly attend to something outside its window in one layer — so these are used where the locality assumption holds well, often mixed with occasional full-attention layers to preserve some global reach.

Which technique attacks which cost

The clean mental model, tying back to post 6’s two costs:

Which technique attacks which cost
Technique Attacks How Approximate?
GQA / MQA KV-cache memory fewer K/V heads → smaller cache slight (MQA more)
FlashAttention attention compute + memory traffic IO-aware exact computation no (exact)
Sliding-window / sparse quadratic compute attend to fewer pairs yes

They’re complementary and usually combined: a modern model might use GQA (smaller cache) and FlashAttention (fast exact kernels) and sliding-window attention on some layers (sub-quadratic) together. Each attacks a different part of the problem, so stacking them compounds the gains — which is exactly how context windows grew from a few thousand to hundreds of thousands of tokens. Alongside these, KV-cache management techniques from the serving world (paging, eviction, quantizing the cache) further stretch what’s possible at inference. The combination of parameter-scaling (MoE) and context-scaling (efficient attention) is what defines the shape of a modern frontier model — which the final post assembles.

Key takeaways

Further reading

Sources & References

IO-aware exact attention