FlashAttention: Exact Attention That Respects Memory
Attention can spend more time moving intermediates than computing them. FlashAttention keeps score tiles near the processor. Online normalization and backward recomputation preserve dense attention without storing its full score grid.
#Section 27The Real Bottleneck: Moving Data
A GPU has small, fast on-chip SRAM and larger main HBM, or high-bandwidth memory. Computation needs its inputs on-chip, so repeatedly transferring large intermediates can dominate execution.
Arithmetic intensity is work per byte moved. Matrix multiplication can reuse data heavily; separate masking and softmax operations often do little work per transfer and become memory-limited.
#Section 28Standard Attention’s Round Trips
Now trace the hand-written attention of Chapter 1 through that hierarchy, for one head with tokens and head size . , and live in HBM. The GPU reads and onto the chip, computes the score grid , and, because is far too big to keep on the chip for long sequences, writes it back to HBM: numbers out. Then it reads back in, computes the softmax , and writes out: in, out. Then it reads and back in and computes the output : another in.
The score and probability grids are large intermediates that are written and read again. Avoiding those round trips is the main opportunity, rather than changing the attention formula.
#Section 29Tiles and Online Softmax
FlashAttention, published by Tri Dao, Daniel Fu, Stefano Ermon, Atri Rudra and Christopher Ré in 2022, attacks exactly that traffic. Its idea is never to build the whole score grid. Cut into blocks of rows and and into blocks of rows too. Load one query block into SRAM and keep it there. Then sweep across the key/value blocks one at a time. For each one, compute that tile of scores, use it immediately to update the output for the query block, and throw it away. When the sweep finishes, the query block's output is complete; write it to HBM once and move on to the next query block.
The catch is softmax. To normalize a row you need the maximum score and the total of the exponentials across the whole row, but the sweep only ever sees one tile of the row at a time. The solution is the online softmax of Chapter 1, Section 57, extended to carry the output along. For each query row keep three running quantities: , the largest score seen so far; , the running sum of ; and , the running sum of . When a new tile arrives with scores and values :
and at the very end the output is . Read the update in words: if the new tile contains a bigger maximum, everything accumulated so far was measured against the old maximum and is too large by a factor of , so shrink it by ; then add the new tile's contributions measured against the new maximum.
Why the running result stays correct
Each tile contributes scores and weighted values relative to a running maximum.
When a later tile raises that maximum, rescale the earlier sums to the new reference before adding the tile's contributions.
The final normalized sum agrees with the all-at-once calculation, up to floating-point rounding. The figure gives a small example.
#Section 30What It Buys
Less traffic
Keeping score tiles on-chip reduces HBM traffic. The original paper measured faster training in its tested workloads; actual savings depend on shapes, hardware, and how much of the total runtime attention occupies.
Memory use falls even more dramatically. Standard attention stores the full grid; FlashAttention stores nothing of size , so its extra memory grows only linearly with . That is what made long context windows practical.
Backward: recompute instead of store
Backward needs attention probabilities, but retaining the entire grid is expensive. Save each row's log-sum-exp, then reconstruct probabilities tile by tile from queries and keys during backward.
That costs extra arithmetic, roughly redoing the forward computation. But arithmetic is cheap and memory traffic is expensive, so the backward pass ends up faster and uses a tiny fraction of the memory. "Recompute rather than store" is one of the most useful ideas in GPU programming, and FlashAttention is its best-known example.
Causal masking for free, at block level
Skip tiles wholly above the causal diagonal. Tiles below it need no causal masking; only tiles crossing it require cell-level checks.
#Section 31Three Versions
FlashAttention (2022) introduced the combination of tiling, online softmax and recomputation in the backward pass. FlashAttention-2 (Tri Dao, 2023) reorganized how work is divided among the GPU's processors, in particular parallelizing along the sequence dimension so that long sequences with small batches still keep every processor busy, and cut the non-matrix-multiply operations that GPUs run slowly. It is roughly twice as fast as the first version and reaches 50% to 73% of an A100's theoretical peak. FlashAttention-3 (Jay Shah, Tri Dao and colleagues, 2024) was written for NVIDIA's Hopper generation, such as the H100. It uses Hopper's asynchronous hardware to overlap loading the next tile with computing on the current one, and adds support for 8-bit floating-point (FP8) inputs.
You rarely call FlashAttention directly. PyTorch's SDPA (Part V) dispatches to it automatically when your inputs qualify, and inference engines such as vLLM bundle their own variants. What matters is knowing what it requires and what it guarantees.
#Section 32Sharp Edges of FlashAttention
Exact, not approximate, but not bitwise identical. It computes the same mathematics in a different order. Floating-point addition rounds at every step, so results can differ in the last few digits from a hand-written version, the same way adding a column of numbers top to bottom and bottom to top can differ. Expect tiny numeric differences, not quality loss.
16-bit only. It is built for fp16 and bf16. With 32-bit inputs it will not run, and frameworks fall back quietly to slower code. Run attention under bf16 autocast.
Head size limits. The kernels are tuned for particular head sizes, up to 256 in FlashAttention-2. Unusual sizes are unsupported or slower. Keep head size at 64, 128 or 256.
Nondeterministic backward. In the backward pass, several processors add into the same gradient numbers in whatever order they finish (Chapter 1, Section 75). Results can vary very slightly between runs. Use the deterministic option when you need bitwise reproducibility, at some speed cost.
Packed sequences. When many short texts are packed together, padding each to the longest wastes work. FlashAttention's variable-length interface takes the real lengths, as cumulative offsets, and attends within each sequence only, with no padding at all.