Naive attention has a quadratic memory bill that capped GPT-3 at 2K tokens. Then in 2022 Tri Dao's FlashAttention paper found a way to never write the N×N matrix to slow GPU memory, and frontier labs adopted it. Long contexts like 200K, 128K or even 2M tokens depend on the tricks in this lesson.
After this lesson, you will be able to:
Explain why naive attention is O(N²) in both compute and memory and how FlashAttention's IO-aware tiling avoids ever writing the full N×N attention matrix to GPU HBM
Tell Multi-Head, Multi-Query, and Grouped-Query Attention apart by what they share, and pick the right one based on inference latency, memory budget, and quality targets
Use a KV cache to skip recomputing keys and values during autoregressive generation, and recognize when paged attention (vLLM) is the right next step for serving
Diagnose a long-context bottleneck (memory? throughput? latency?) and reach for the right tool (FlashAttention, sliding window, attention sinks, or linear attention) instead of just buying more H100s
Don't worry if "FlashAttention" sounds like a paper you have to skim. The core insight is one sentence: never write the full attention matrix to slow memory. Once you see the picture, every optimization in this lesson is a variation on that theme.
The attention output is mathematically the same whether you compute it block-by-block or all at once. FlashAttention exploits this by tiling the computation across blocks of Q, K, V that fit in SRAM. The catch is making the softmax work block-by-block: softmax requires the row max and row sum across the entire row, but you only have a block at a time.
The trick: maintain running statistics (running max m_i, running sum ℓ_i) and rescale outputs as new blocks arrive. This is the online softmax algorithm (Milakov & Gimelshein 2018), wrapped around tiled matmul.
Block size B≈4dkMSRAM⇒HBM accesses Θ(MSRAMN2d2) vs Θ(Nd+N2) for standard attention
The block-by-block trick only works if a softmax can be built piece by piece. Open the derivation to see exactly how the running max and running sum are updated, and why the old sum is rescaled.
Press play to watch the running max and running sum update tile by tile.
Try it: FlashAttention tilesInteractive
Loading visualization...
The paper states the memory-traffic result as a theorem. Open the derivation to see where the count comes from.
FlashAttention-2 (2023) rebalanced work between thread blocks for better GPU utilization on long sequences, hitting ~1.7x over FlashAttention-1. FlashAttention-3 (2024) added FP8 / asynchronous Tensor Cores for H100s, hitting ~2x over FA-2 on top of that. By 2026, every serious training and inference stack (PyTorch's scaled_dot_product_attention, HuggingFace, vLLM, TensorRT) defaults to a FlashAttention variant.
The KV cache (covered next) stores keys and values from prior tokens for fast autoregressive generation. For a model with H heads, the KV cache is proportional to H. Cut the number of K and V projections, and you cut KV-cache memory linearly.
Multi-Head Attention (MHA). Every head has its own Q, K, V. Best quality, biggest cache.
Multi-Query Attention (MQA). H query heads, 1 shared K head, 1 shared V head. Cache is H times smaller. Quality drop on training-from-scratch is real but small; on retrofitting, it can be severe.
Grouped-Query Attention (GQA). H query heads, G key/value groups (G < H). Cache is H/G times smaller. With G = H/8, you keep almost all of MHA's quality and most of MQA's savings.
The lesson's formula counts one layer. A whole model multiplies by the number of layers and the bytes per number. Open the derivation to build the full size from one stored vector.
Watch the cache grow as tokens are generated, and change the layers and model width to see the memory scale.
Try it: the KV cache step by stepInteractive
Loading visualization...
This lab puts numbers on the three layouts: change context length, heads and batch, and read the cache size per layer.
Try it: compare KV cache layoutsInteractive
Loading visualization...
Do: Leave N at 8192, drag G (KV heads) from 2 up to 8 and watch the GQA number, then raise B (batch).
Notice: With the defaults (8 heads, d_h 128, fp16) one layer's cache is 32.0 MB under MHA, 8.0 MB at G = 2 and 4.0 MB under MQA. Cache size moves in step with N, B and the number of K/V heads, which is why GQA with G = 8 equals MHA here.
Why: The numbers come from the lesson formula, B x kv_heads x N x d_h x 2 x 2 bytes, for one layer, so multiply by the layer count for a whole model. If you switch the third column to PagedAttention, that panel is a fixed three-prompt picture of block tables, not a measured server, and its byte count assumes one K/V head.
What Do You Think?
You serve a 70B Llama at 100k context to thousands of concurrent users. Which combination?
GQA is the quality/memory sweet spot Llama 2/3 chose. FlashAttention is needed for the prefill phase at 100k context, because naive attention would run out of memory. Paged KV cache (vLLM-style) is what lets you batch unrelated requests together by sharing GPU memory blocks dynamically. MHA loses the memory war at this scale; full MQA gives up too much quality. Buying H100s helps but doesn't scale.
Autoregressive generation is "predict next token, append, repeat." Every step the model runs attention over the entire prefix plus the new token. Without a cache, each step does N forward passes: O(N²) total work per generated token. With a KV cache, only the new token's K and V are computed; everything prior is reused.
Without cache: O(N²) compute per generated token. With cache: O(N) compute per token plus O(N) memory growth.
The numpy version below runs on this page. Edit it and run it.
pythonplayground.py · Pyodide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Tests: Check that the windowed output differs from full attention but has no NaN, that position 500 sees 256 tokens, and that the receptive field grows linearly with depth.
The same idea with real PyTorch needs a library that does not run on this page. Copy the code below to Google Colab.
PythonRunnable cell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copy to Colab: needs torch, so it cannot run on this page.
# Sliding-window attention — bound how far each token can look back.
# This is the core of Mistral's long-context strategy: each layer has window W,
# but the receptive field grows linearly with depth (W + (L-1)*W).
import torch
import torch.nn.functional as F
def sliding_window_mask(N, W):
"""Causal mask + sliding window of width W."""
# Causal: i can attend to j only if j <= i (no looking forward)
# Window: i can attend to j only if j >= i - W + 1
i = torch.arange(N).unsqueeze(1)
j = torch.arange(N).unsqueeze(0)
causal = j <= i
window = j >= i - W + 1
mask = causal & window
return mask # True where attention is allowed
def windowed_attention(Q, K, V, W):
"""O(N*W) attention instead of O(N^2)."""
N, d = Q.shape
mask = sliding_window_mask(N, W).to(Q.device)
scores = (Q @ K.T) / (d ** 0.5)
scores = scores.masked_fill(~mask, float('-inf'))
P = F.softmax(scores, dim=-1)
return P @ V
# Demo: 1000 tokens, window of 256
torch.manual_seed(0)
N, d = 1000, 64
Q = torch.randn(N, d)
K = torch.randn(N, d)
V = torch.randn(N, d)
out_full = windowed_attention(Q, K, V, W=N) # full causal attention
out_win = windowed_attention(Q, K, V, W=256) # 256-token window
print(f"Full attention output norm: {out_full.norm():.4f}")
print(f"Window=256 attention norm: {out_win.norm():.4f}")
print(f"Output difference: {(out_full - out_win).norm():.4f}")
print()
print("Token at position 500 attends to:")
mask500 = sliding_window_mask(N, 256)[500]
print(f" Full attention: positions 0-500 ({mask500.sum()} tokens with full)")
mask500_win = sliding_window_mask(N, 256)[500]
print(f" Window=256: positions {500-255}-500 ({mask500_win.sum()} tokens)")
# Stack 8 layers: each adds 256 to the receptive field
print("\nReceptive field growth across stacked layers (W=256):")
for layer in range(1, 9):
rf = 256 * layer
print(f" After layer {layer}: receptive field = {rf} tokens")
# Real Mistral 7B uses W=4096 with 32 layers → effective receptive field = 131k tokens
# even though each layer's attention is bounded.
# The trick: stacking sliding-window attention = effectively long context with O(N*W*L) cost.
#
# To extend further: combine with attention sinks (StreamingLLM) — keep the first 4 tokens
# always attended to. Without sinks, sliding window degrades quality after the initial tokens
# scroll out of every layer's window.
Naive KV-cache management allocates one contiguous block per request. Result: severe fragmentation when requests have varying lengths: a 90% wasted GPU memory budget on real serving traffic.
vLLM's PagedAttention treats the KV cache like an OS treats RAM. Memory is split into fixed-size blocks (typically 16 tokens). Each request keeps a block table mapping logical positions to physical blocks. Free blocks return to a pool. Adjacent requests can share blocks (e.g., shared prefixes for parallel sampling) via copy-on-write.
Block table: logicali⟼physicalj(j may not be contiguous)
For very long sequences (1M+ tokens), even FlashAttention's O(N²) compute is impractical. Several approximations dominate:
Sliding-window attention (Mistral). Each token attends only to the last W tokens; receptive field grows with depth.
Attention sinks (StreamingLLM, Xiao 2023). The first few tokens accumulate all the attention from later positions; preserve them and you can stream indefinitely without quality collapse.
Linear attention (Performer, Linformer, Longformer). Kernel approximations that bring attention to O(N) at the cost of some accuracy.
State Space Models (Mamba, RWKV). Covered in the Mamba lesson; RNN-like recurrence with parallel training.
Two of the cheaper-attention ideas can be derived in a few lines. The first counts the scores a window keeps and how far stacked windows see. The second moves a pair of brackets.
Sliding-window and attention-sink tricks were the 2023 answer. The 2025 wave is a generation more sophisticated: instead of one fixed sparsity pattern, modern long-context models route each query through several attention pathways at once and let the model learn which pathway matters where. Four designs dominate the frontier.
NSA (Native Sparse Attention, DeepSeek 2025) ships in DeepSeek-V3 and is the most influential of the new wave. NSA runs three parallel attention pathways for every query: a compression branch that attends to coarse block summaries (one key/value per 64-token block, giving a low-resolution view of the entire context), a selection branch where a small router scores blocks and the query attends only to the top-k highest-scoring blocks at full resolution, and a sliding-window branch that always attends to the most-recent W tokens for local fidelity. The three branches' outputs are fused with learned gates. The whole pattern is trained end-to-end and stays hardware-friendly because each branch is itself a dense, kernel-friendly matmul.
MoBA (Mixture of Block Attention, Moonshot 2025) powers Kimi K1.5's million-token context. MoBA chunks the KV-cache into blocks, scores each block against the query through a lightweight gating network, and lets the query attend only to its top-k blocks. Unlike NSA's hand-designed three pathways, MoBA's routing is fully learned and looks closer to a Mixture-of-Experts router operating over context blocks rather than over MLP experts. The gating is differentiable enough to train from scratch, which is what distinguishes MoBA from earlier retrieval-style "select-then-attend" approaches.
Hybrid local + global stacks in Gemma 2 and Llama 4 alternate layer types, roughly every fourth layer uses dense global attention while the rest use sliding-window-only attention. The intuition is that you do not need every layer to see every token: a few global layers per stack are enough to mix long-range information, and the rest can stay cheap. This is a tiny architectural change that buys most of the cost benefit of sparse attention without changing the kernel.
Mamba2-Hybrid in Jamba 1.5 (AI21) goes further by mixing layer types: some layers are state-space (Mamba) blocks with O(N) recurrence, others are full attention, and a sprinkle of MoE FFN layers sits between them. The attention layers handle the few global lookups; the SSM layers carry the long-distance state cheaply. Jamba 1.5 Mini fits 256K context on a single 80 GB GPU because most of its layers no longer pay the O(N²) bill.
Quick check
You are choosing a long-context attention strategy for a 1M-token coding assistant that runs on a single H100. Which trade-off correctly maps each technique to its strongest use case?
Attention's wall is memory bandwidth, not FLOPs. Naive attention writes O(N²) bytes to HBM. FlashAttention's IO-aware tiling cuts HBM accesses from Θ(Nd + N²) to Θ(N²d²/M), giving a large wall-clock speedup with no quality change.
GQA is the modern default. Between Multi-Head (full quality, full cache) and Multi-Query (smallest cache, quality risk), Grouped-Query Attention is the sweet spot every frontier 70B+ model has chosen since Llama 2.
The KV cache is what makes inference cheap. Without it, generating each token costs O(N²) compute; with it, O(N). It also dominates inference memory, which is why GQA matters so much.
PagedAttention is the OS analogy that 24x'd serving. Chunking the KV cache into fixed blocks and tracking them with a block table eliminates fragmentation and enables prefix sharing.
Long context (>32k) requires a stack, not a single trick. FlashAttention + GQA + sliding window + attention sinks together; or switch to alternative architectures (Mamba, hybrid).
Attention's quadratic wall is the central engineering challenge of the LLM era. Next we look at the inference-side optimizations: quantization, speculative decoding, and the serving stacks that run large models as APIs.