Loading the interactive visualization…

01 · Reference notes

← All visual guides

FlashAttention Explained

FlashAttention is an exact, IO-aware attention algorithm that reduces GPU memory traffic by computing attention in tiles in fast on-chip memory instead of materializing the full N×N attention matrix in HBM.

01 · Definition

What is FlashAttention?

It changes the schedule for the usual attention calculation, not the calculation itself. The output is the same as standard softmax attention; the win comes from moving far less intermediate data to and from slow GPU memory.

02 · Mechanism

Why standard attention is slow

For a sequence of N tokens, standard attention forms an N×N score matrix. At long context lengths that matrix is enormous. Writing scores to high-bandwidth memory (HBM), reading them back for softmax, and writing weights again can cost more time than the matrix multiplications themselves.

03 · Mechanism

HBM vs SRAM

HBM is large GPU memory: it stores a lot, but moving data there is expensive. SRAM is the much smaller on-chip workspace next to the compute units. FlashAttention organizes work so temporary score blocks fit in SRAM and are discarded before they ever need to live in HBM.

04 · Mechanism

How FlashAttention tiling works

The algorithm loads a block of queries and a block of keys and values, computes their small score tile, and immediately folds that tile into the output. It repeats this for the next key/value block. Only the final output and a small running state survive between tiles.

05 · Mechanism

How online softmax stays exact

Softmax usually appears to require every score in a row at once. FlashAttention instead tracks each row's running maximum m, normalizing sum ℓ, and partial output O. When a later tile contains a larger score, the earlier contribution is rescaled. Those three values preserve the same result as a full-row softmax.

06 · Mechanism

Is FlashAttention approximate?

No. FlashAttention is exact attention with a different memory-access pattern. It does not select fewer tokens or replace softmax; it avoids storing the full intermediate matrix.

07 · Mechanism

FlashAttention vs standard attention

Both compute QKᵀ, apply softmax, and combine V. Standard attention materializes large intermediates in HBM. FlashAttention processes the intermediates in SRAM-sized tiles, which makes the gap more valuable as sequences get longer.

Source · Original paper