What FlashAttention is
FlashAttention computes exactly the same attention as the MHA tab, only in a different order. Standard attention builds the score matrix, writes it to the GPU's main memory (HBM), reads it back for the softmax, writes the weights, and reads them again to multiply by the values. On a GPU those reads and writes, not the arithmetic, take most of the time. FlashAttention cuts the matrix into tiles small enough for fast on-chip memory (SRAM) and uses an online softmax, so that the full matrix is never stored at all.
This page runs the tiled loop of the FlashAttention-2 forward pass on GPT-2 small's recorded queries, keys and values in layer 6, with tiles of (real kernels use 64 to 128). Its output matches standard attention to within in float32 and in float64, as the toolbar shows.
Tiles
The query rows are split into blocks of and the keys into tiles of . For each row block the loop loads one block of queries, then walks the key tiles from left to right: it loads a tile of keys and values, computes the scores, and updates each row's running state. Under the causal mask, tiles that lie entirely to the right of the diagonal hold only masked scores, so the kernel skips them.
The step control in the toolbar replays this loop order tile by tile, and the head card outlines the tiles, marking the skipped ones. For the 12-token Coreference prompt there are tiles, of which 6 are visited and 3 skipped. While the loop runs, the matrix shows each row's current softmax estimate, which changes as new tiles arrive.
The online softmax
A softmax normally needs the whole row: its maximum, for numerical stability, and its sum, to normalise. The online softmax keeps both as running values and corrects what it has accumulated whenever they change. For one query row, after key tile with scores :
and at the end . The factor shrinks everything accumulated so far whenever a new tile raises the maximum; if the maximum does not change, . The Online softmax panel lists, for the selected query, each tile's maximum and the running , and , with the softmax estimate and after every tile; the page opens on head 5, whose last query's maximum grows in a later tile, so the rescaling shows. Milakov and Gimelshein had described this one-pass computation of the softmax normaliser in 2018.
What it saves
The panel's last lines count the memory traffic for this prompt. Standard attention writes scores and as many weights to memory and reads them back: 3,456 floats. The kernel keeps a tile of queries, a tile of keys and values and each row's , and on chip, and writes only the output and one number per row, the log-sum-exp : 144 floats. The extra memory grows linearly in instead of quadratically.
The arithmetic does not shrink; the reads and writes do. Dao et al. proved that FlashAttention needs accesses to HBM for SRAM of size , against for standard attention, and reported a 3 times speed-up training GPT-2 at sequence length 1K. The backward pass uses the stored log-sum-exp to recompute the weights tile by tile, rather than storing them.
Where it came from
Rabe and Staats (2021) showed that attention can be computed in chunks with memory that grows far slower than . FlashAttention (2022) made tiling IO-aware and fused the whole computation into one GPU kernel. FlashAttention-2 (2023), whose loop this page follows, puts the query blocks in the outer loop so that each thread block owns its rows, defers the division by to the end and parallelises over the sequence; it roughly doubled the speed of the first version. FlashAttention-3 (2024) uses the asynchronous and low-precision features of Hopper GPUs. None of them changes the result, which is why a trained model can switch to them without retraining; the Sparse and Window tabs change what is computed instead.
Papers
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness Introduces tiled, IO-aware exact attention with an online softmax, and its IO-complexity analysis.
- FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning The loop order this page replays: query blocks outside, key tiles inside, normalisation deferred to the end.
- Online normalizer calculation for softmax Computes the softmax maximum and normaliser in a single pass, the update on this page.
- Self-attention Does Not Need O(n²) Memory Chunked attention with memory far below quadratic, the same idea at the algorithm level.
- FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision The next version, using the asynchronous units and FP8 of Hopper GPUs.