FlashAttention is an exact attention algorithm that streams queries, keys and values through small tiles of fast on-chip memory. The full attention matrix is never written to main video memory. Most diffusion transformers and language models run on it or a descendant.
Origins
Tri Dao and coauthors published FlashAttention in May 2022. The paper argued that attention was limited by memory traffic, not arithmetic, and reported a 3x speedup on GPT-2.
FlashAttention-2 followed in July 2023 and roughly doubled speed. FlashAttention-3 for Hopper came in July 2024. Its FP8 path nears 1.2 PFLOPs/s. FlashAttention-4 for Blackwell arrived in March 2026. It reaches 1613 TFLOPs/s on a B200. Quantized rivals followed, see SageAttention explained.
How tiling works
Standard attention builds an N by N score matrix in HBM, applies softmax, then reads it back. FlashAttention splits Q, K and V into blocks that fit in SRAM and loops over them.
An online softmax keeps a running maximum and sum per row, so partial results combine exactly. Only the output is written back, so memory grows linearly with length. The backward pass recomputes scores instead of storing them.
Use in image, video and language models
Long-context LLMs and video transformers both depend on it. A video model can feed tens of thousands of latent tokens through every layer. PyTorch SDPA picks a FlashAttention, cuDNN or memory-efficient backend automatically.
ComfyUI has --use-flash-attention and --use-sage-attention switches (setup). SageAttention quantizes Q and K to 8-bit integers. Its paper reports about 2.1x the operations per second of the older kernel. xFormers is another kernel source. Text decoding pairs it with a KV cache, and quantization shrinks the inputs.
Sources
- FlashAttention paper, arXiv 2205.14135: original tiling algorithm and first speedups.
- FlashAttention-2 paper and FlashAttention-3 paper: work partitioning and FP8 results.
- FlashAttention-4 paper: kernel design and throughput on B200.
- Dao-AILab repository and SageAttention paper: hardware support and quantized attention.