Explain FlashAttention. Why can it compute exact attention faster while using less memory?
Answer
FlashAttention is an exact, IO-aware implementation of scaled dot-product attention. Instead of materializing the full score and probability matrices in high-bandwidth memory (HBM), it loads blocks of
,
, and
into fast on-chip SRAM, computes attention block by block, and maintains online softmax statistics. Tiling reduces expensive HBM reads and writes, while recomputation during the backward pass can be cheaper than storing large intermediates. The mathematical result matches standard attention up to normal floating-point differences; the speedup comes from changing the execution schedule, not from approximating attention.

Figure 1: IO comparison showing why avoiding N×N intermediate writes makes FlashAttention faster and more memory efficient.
(1) IO Awareness: The kernel is organized around the GPU memory hierarchy so most score computation and softmax updates happen in SRAM.
(2) Online Softmax: A running row maximum and normalization sum allow each tile to update the output without retaining previous score blocks.
(3) Exact Result: All query-key interactions are evaluated; causal masking, dropout, and backward gradients are fused into specialized kernels rather than approximated.
Mathematical Formulation:
Where:
indexes key/value tiles,
indexes a query row, and
indexes keys inside tile
.
is the scaled score between query row
and key row
, with head width
.
is the running row maximum after tile
, initialized with
.
is the running softmax denominator, initialized with
; the exponential factors rescale earlier partial sums when the maximum changes.

Figure 2: Blockwise FlashAttention loop with running maximum, normalization, output rescaling, and final HBM write.
Leave a Reply