FlashAttention

last edited

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.

keys, in tilesqueriesoutputscore tiles live in sram and are never saved to hbm

Origins

fa12022tiling, a100fa22023work splitfa32024h100, fp8fa42026b200each version rewrites the kernel for new hardware

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

hbmsramq tileblockrunning maxand sumoutput, written oncek, v blockso

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

n × n scorestiled, linear1ktokens, video grows it64kmemory per head

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