Ankit Patel
WritingPapersLabsAboutContact
Paper Reviews··2 min read·192 views

FlashAttention: The Attention Matrix You Don't Store

FlashAttention · Dao et al., NeurIPS 2022

A GPU's main memory holds tens of gigabytes, while its 192 KB on-chip SRAM runs roughly ten times faster

Recomputing attention intermediates during backpropagation sounded wasteful to me at first until you compare GPU arithmetic speed against memory traffic.

Counting floating-point operations alone treats every multiplication as the bottleneck. Modern GPUs flip that equation on memory-bound kernels: shuttling intermediate tensors out to off-chip memory and reading them back takes far longer than repeating the math on chip.

GPU memory sits in a steep hierarchy. High-bandwidth memory (HBM) holds tens of gigabytes at much higher access latency than the tiny, fast on-chip SRAM sitting directly beside the compute units. Both capacity limits and byte transfers across that gap govern wall-clock runtime.

Standard self-attention scores every token against every other token across sequence length N, materializing an N x N attention matrix in HBM so softmax, dropout, and value multiplication can read it in sequence. As context windows lengthen, reading and writing that quadratic intermediate tensor dominates runtime and exhausts GPU memory.

FlashAttention tiles queries, keys, and values into blocks sized to fit inside on-chip SRAM. Using running softmax normalization statistics across blocks, it computes exact attention outputs in a single fused kernel without ever writing the full N x N matrix to HBM. During the training backward pass, it recomputes attention blocks on chip from saved inputs and normalization scalars instead of storing the massive forward attention matrix.

Arithmetic complexity stays quadratic in N. Avoiding O(N^2) HBM storage removes memory-bandwidth stalls and unlocks longer sequences, though dense attention FLOPs still scale quadratically and reordered floating-point reductions introduce normal rounding differences.

Compare two kernels implementing the same mathematical layer. One minimizes FLOPs while streaming huge intermediate tensors to HBM three times. The other performs extra recomputation while keeping its working set inside fast local SRAM. Paper FLOP counts pick the first kernel, whereas hardware profiling picks the second.

Evaluating custom attention kernels in a real stack comes down to hardware specifics rather than headline speedups: which sequence lengths and head dimensions dominate traffic, whether your serving framework already dispatches fused attention under the hood, and whether required attention masks and precision formats keep numerical behavior stable end to end.

Every systems layer faces the same three options for an intermediate result: store it, move it, or recompute it. FlashAttention shows when recomputing wins decisively.

Source: Dao et al., FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (2022).

Continue in Paper Reviews