# FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness

> Reorders exact attention into SRAM-sized tiles with an online softmax, so the N×N matrix never touches slow GPU memory. Same outputs, 3.5× faster GPT-2 training, and attention memory that grows linearly with sequence length.

- arXiv: https://arxiv.org/abs/2205.14135
- Authors: Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré
- Published: 2022-05-27
- Topic: Inference & systems
- Visual carousel: https://instaxiv.com/p/2205.14135
- Explanation by InstaXiv, a secondary educational summary. Cite the original paper for exact results. InstaXiv is not affiliated with or endorsed by arXiv or Cornell University.

## Exact attention, several times faster, by respecting GPU memory

**3.5×** faster GPT-2 training than Hugging Face

## Attention is memory-bound, not compute-bound

**Standard attention:** Writes the full $N \times N$ score matrix to slow GPU memory, reads it back for softmax, then writes again. Most time goes to moving bytes, not math.

**FlashAttention:** Computes attention tile by tile inside fast on-chip SRAM and never materializes the full matrix. Exact same output, far fewer memory reads and writes.

Counting FLOPs missed the real cost: memory traffic.

## Tiles stay on chip; the big matrix never exists

![Figure 1](https://cdn.instaxiv.com/figures/2205.14135/figure1.webp)

SRAM runs near 19 TB/s but holds only 20 MB; HBM is 1.5 TB/s and 40 GB. Blocks of K and V stream through SRAM, making attention 7.6× faster on GPT-2. (Figure 1)

## Three tricks make exact attention IO-aware

1. **Tile the inputs**: Split Q, K and V into blocks sized to fit in SRAM, then loop over them.
2. **Online softmax**: Keep a running max and running sum per row, rescaling partial outputs so tiles combine into the exact softmax.
3. **Recompute, don't store**: Skip saving the attention matrix for the backward pass. Recompute it on chip; extra FLOPs cost less than memory reads.

## Online softmax merges tiles without revisiting them

$$
\begin{gathered} m = \max(m_1, m_2) \\ \ell = \rose{e^{m_1 - m}}\,\ell_1 + \rose{e^{m_2 - m}}\,\ell_2 \end{gathered}
$$

Each block tracks its own max and exponent sum. Rescaling by $e^{m_i - m}$ combines blocks exactly, so softmax never needs the full row at once.

- $m_i$: Row maximum within block i, for numerical stability
- $\ell_i$: Sum of exponentials within block i
- $e^{m_i - m}$: Correction aligning each block to the shared max

## Faster training at identical quality

| Method | Training time | Speedup | Perplexity |
| --- | --- | --- | --- |
| Hugging Face | 9.5 days | 1.0× | 18.2 |
| Megatron-LM | 4.7 days | 2.0× | 18.2 |
| **FlashAttention (3.5× faster)** | **2.7 days** | **3.5×** | **18.2** |

_GPT-2 small on OpenWebText, 8×A100. Perplexity is unchanged because attention stays exact._

## Speed comes with an engineering bill

- **Custom CUDA per variant**: Every new attention variant needs a hand-written kernel, far lower-level work than writing PyTorch.
- **Tuned per GPU generation**: Block sizes depend on SRAM size, so kernels may not transfer cleanly across GPU architectures.
- **Single-GPU analysis**: The IO-aware analysis covers one GPU; multi-GPU attention was left for future work.

Best used when: Training or serving Transformers at long context, where attention memory dominates.

## Key takeaways

- Count memory reads and writes, not just FLOPs, when optimizing GPU kernels.
- Exact attention with memory linear in sequence length unlocked 16K and 64K contexts.
- It now ships inside PyTorch's scaled dot-product attention, the default in most training stacks.

## Links

- arXiv: https://arxiv.org/abs/2205.14135
- PDF: https://arxiv.org/pdf/2205.14135
- Code: https://github.com/Dao-AILab/flash-attention

## Citation

```bibtex
@article{dao2022flashattention,
  title   = {FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness},
  author  = {Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and Ré, Christopher},
  journal = {arXiv preprint arXiv:2205.14135},
  year    = {2022},
  url     = {https://arxiv.org/abs/2205.14135}
}
```
