Computing attention without materializing the full matrix
Standard attention computes a full N x N matrix of pairwise similarities (where N is sequence length), then softmax and multiplies by values. For a sequence of 4K tokens, this is a 16M-element matrix, consuming gigabytes of memory and bandwidth. FlashAttention reduces memory and compute by processing attention in tiles that fit in fast SRAM.
The trick is recomputing softmax incrementally. As you process tiles, you update the running sum and normalization separately, avoiding the need to store the full attention matrix.
Speed and memory improvements in practice
FlashAttention is 2-4x faster than standard attention on modern GPUs, with proportional memory savings. For long contexts (8K to 100K+ tokens), this difference is transformative: standard attention becomes infeasible, while FlashAttention remains practical.
The fused kernel (combining multiple operations into one) is key. Separate calls to compute attention, softmax, and multiply incur overhead; one fused kernel avoids it.
Enabling longer contexts in training and inference
FlashAttention made long-context pretraining feasible. Models like Llama 2 with 4K context and later 8K were enabled by this. Without FlashAttention, context length is limited by available memory, forcing shorter sequences and shorter-range dependencies.