Yatishara Blog
Back
Paper digest: FlashAttention

Paper digest: FlashAttention

Research

Dao et al. on IO-aware exact attention. Why Transformers got memory- and bandwidth-efficient without approximating the math.

Paper: Dao, Fu, Ermon, Rudra, Ré — FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Stanford / University at Buffalo; NeurIPS 2022)
Source: arXiv:2205.14135 · PDF
Lineage: FlashAttention-2 (Dao, 2023) · FlashAttention-3 (Shah et al., 2024)

One line claim

Standard self-attention is exact but wasteful on modern GPUs because it materializes huge intermediate matrices in slow HBM. FlashAttention recomputes attention in GPU SRAM-aware tiles, keeps the math exact, and turns a memory wall into a throughput win.

Attention is IO-bound. The bottleneck is moving Q, K, V and the attention matrix between HBM and on-chip SRAM — not FLOPs alone.

Dao et al., arXiv:2205.14135

Why this paper exists

Attention Is All You Need made self-attention the backbone of sequence models. That win came with a systems tax. For sequence length $N$ and head dimension $d$, naïve attention builds an $N \times N$ score matrix, softmaxes it, and multiplies by values. Memory for that matrix scales as $O(N^2)$. On long contexts — documents, codebases, multimodal token grids — the quadratic matrix dominates both peak memory and wall-clock time even when the GPU still has spare arithmetic capacity.

By 2022 the literature already offered approximate attention: sparse patterns, low-rank projections, kernelized linear attention. Those methods often trade exactness for asymptotic complexity. Production labs were wary: approximate attention can change training dynamics and eval numbers in ways that are hard to audit. The open engineering question was narrower and sharper. Can exact attention be made fast and memory-efficient by respecting how GPUs actually move data?

FlashAttention answers that question as a systems paper sitting on top of the Transformer math. It does not replace multi-head attention. It changes how the same softmax attention is scheduled on hardware. That is why the paper became infrastructure rather than another modeling fad: PyTorch, Hugging Face, vLLM, and nearly every long-context LLM stack eventually absorbed FlashAttention-style kernels.

Method, without the wall of equations

Start from the hardware facts the authors treat as first-class:

  • HBM (high-bandwidth memory) is large and relatively slow.
  • SRAM on the streaming multiprocessor is small and fast.
  • A naïve attention implementation writes the full $N \times N$ matrix to HBM, then reads it back for softmax and the value multiply. Those round-trips dominate runtime for typical LLM head sizes.

FlashAttention’s core move is tiling + kernel fusion + recomputation:

  1. Tile queries, keys, and values into blocks that fit in SRAM.
  2. Fuse the score, softmax, and value multiply for each tile inside one CUDA kernel so intermediate attention matrices never fully land in HBM.
  3. Maintain online softmax statistics (running max and sum) so partial tiles can be combined into the exact softmax result, following the numerically stable online-softmax idea rather than a naive two-pass over a materialized matrix.
  4. Recompute attention scores in the backward pass instead of storing the full matrix from the forward pass. Extra FLOPs, far fewer HBM bytes. On GPUs, that trade is often a win because attention is memory-bound.

The algorithm is exact for standard softmax attention under the usual floating-point caveats. That exactness claim is the product reason people adopted it: you get speed and lower memory without changing the model definition.

SRAM-tiled attention kernel: scores stay on-chip; HBM sees Q/K/V and outputs
SRAM-tiled attention kernel: scores stay on-chip; HBM sees Q/K/V and outputs

IO-awareness as the design lens. Classic complexity talks in FLOPs. FlashAttention talks in HBM accesses. The authors analyze how many bytes move between HBM and SRAM and show their tiled schedule reduces those transfers asymptotically relative to standard attention for long sequences. That framing is why the paper aged well: newer GPUs change FLOP/s charts, but the HBM-vs-SRAM gap remains the constraint that long-context serving hits first.

Block sizes and practical tuning. Tile shapes depend on head dimension, sequence length, and SRAM budget. The implementation detail matters: a theoretical tiling that does not fit real SM memory does not ship. FlashAttention’s contribution includes the engineering that made the schedule runnable, not only the asymptotic argument.

Results that moved

Numbers below are from the FlashAttention paper’s reported measurements (A100-class experiments in the original write-up). Exact wall-clock figures vary by batch, head count, and software stack; the directional claims are what stuck in production.

ExactSoftmax attention math
O(N)Extra HBM vs O(N²) matrix
2–4×Typical speedups reported

End-to-end training speed. On BERT-scale and GPT-style workloads, the authors report substantial wall-clock reductions versus standard PyTorch attention, with larger gains as sequence length grows. The point is not a single magic multiplier. It is that exact attention can be accelerated enough that longer-context training becomes affordable without switching to approximate attention families.

Memory. Peak activation memory for attention drops because the $N \times N$ matrix is not materialized. That unlocks longer sequences at the same GPU memory envelope, or larger batches at fixed context. For labs, memory headroom often matters more than a pure FLOP race: it decides whether a 4k/8k/16k context run fits on the hardware you already rented.

Quality. Because the algorithm is exact, model quality tracks standard attention. That is the contrast with sparse or low-rank approximations that need separate ablations to prove they did not quietly hurt perplexity or downstream accuracy.

Downstream lineage. FlashAttention-2 (arXiv:2307.08691) reorders work to improve occupancy and parallelism across warps/threadblocks. FlashAttention-3 (arXiv:2407.08608) targets Hopper-generation features (asynchrony, Tensor Memory Accelerator paths, FP8 considerations). The research story is iterative systems refinement on the same IO-aware thesis, not a sequence of incompatible model architectures.

Memory wall: quadratic attention matrix vs tiled working set in SRAM
Memory wall: quadratic attention matrix vs tiled working set in SRAM

How to read the paper against approximate attention

Before FlashAttention, a common pitch was: quadratic attention is doomed, so replace softmax with a linear or sparse surrogate. Those lines of work still matter for extreme sequence lengths. FlashAttention changed the default engineering bet for the lengths most LLM products actually ship. If you can keep exact attention and still train/serve at 4k–32k-class contexts on current GPUs, you should not accept quality risk from approximations unless measurements force you to.

That is also why the paper belongs in a research digest series next to modeling work. LaViDa-style diffusion reasoners and Omni-style multimodal token models still sit on attention-heavy stacks somewhere in the pipeline. MoE models still pay attention compute on every token even when FFNs are sparse — see Mixture-of-Experts. Making attention IO-efficient is orthogonal to those modeling bets and compounds with them.

A useful mental model for non-systems readers: imagine attention as a warehouse problem. The math wants every query to compare against every key. Naïve code ships the entire comparison spreadsheet to a slow warehouse aisle (HBM), then wheels it back for softmax. FlashAttention keeps working trays on the fast bench (SRAM), updates running totals, and only writes finished outputs to the warehouse. Same spreadsheet answer; fewer aisle trips.

Limits (from the paper and from common sense)

FlashAttention does not remove the quadratic compute of dense attention. It reduces the memory and IO pain of implementing that compute. For extremely long contexts, people still reach for sparse attention, sliding windows, SSM hybrids, or retrieval rather than pretending $O(N^2)$ FLOPs disappeared.

Kernel performance is hardware- and shape-sensitive. Head dimensions, sequence lengths, dropout, causal masks, multi-query / grouped-query layouts, and mixed precision all affect whether a given FlashAttention version wins. “Install flash-attn” is not a guarantee on every edge case; serving stacks keep multiple attention backends for a reason.

Numerical behavior in low precision still needs care. Exact in the algorithmic sense does not mean bit-identical to a naive fp32 reference under every fused fp16/bf16 path. Production teams validate training loss curves when swapping kernels.

The original paper is also not a full serving treatise. Decode-time attention (KV cache heavy, batch-varying lengths) has its own bottlenecks. Later inference engines composed FlashAttention ideas with paged KV caches and continuous batching; those are sibling systems papers, not claims FlashAttention alone solved LLM serving.

Who should care

ML systems engineers and inference teams. If you own training throughput or tokens-per-second, this paper is table stakes literacy. The transferable lesson is to profile HBM traffic before you invent a new approximate attention paper for your product.

Founders evaluating “long context” decks. Ask whether the vendor’s long context is exact dense attention with IO-efficient kernels, an approximate pattern, or retrieval pretending to be context. FlashAttention made exact long-ish context cheaper; it did not make infinite context free.

Caribbean operators and small studios. You will rarely compile custom CUDA attention. You still buy APIs and local GPUs whose economics quietly assume FlashAttention-class kernels. Longer briefs, full WhatsApp export dumps, and multi-asset creative prompts all push context length. When a Caribbean team compares hosted models, the practical questions are: What context is actually usable at your price tier? Does latency collapse when prompts get long? Are you paying for quadratic waste the vendor already optimized away? Pair this digest with the architectural ancestor in Attention Is All You Need so “Transformer” and “why my long prompt is expensive” stay connected.

Researchers chasing cultural or low-resource language models should also care indirectly. Longer usable context helps few-shot exemplars and document-grounded answers — the same pressure that shows up in Creole / low-resource NLP and RAG for cultural LLMs. Systems efficiency is how those product patterns become affordable.

Students and hiring managers. If a candidate can explain why attention is memory-bound and what “exact” means in FlashAttention, they understand production Transformers better than someone who only recites multi-head diagrams. That interview signal travels well in Port of Spain, Kingston, and Bridgetown shops that will never write a CUDA kernel but will debug why a long creative brief times out.

Bottom line

FlashAttention reframed Transformer attention as an IO problem. By tiling Q/K/V into SRAM, fusing softmax attention, and recomputing in the backward pass, Dao et al. kept exact attention while cutting memory and accelerating wall-clock training. The paper’s descendants are now default infrastructure in the LLM stack. The modeling lesson remains Vaswani’s; the shipping lesson is FlashAttention’s: respect the memory hierarchy or the quadratic matrix will bill you twice.

We report papers. Digests written at Yatishara Blog; not a CUDA course.