NLP Lineage

27 May 2022 · Language modelling

FlashAttention

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

T. Dao, D. Fu, S. Ermon, A. Rudra, C. Ré

Date
27 May 2022
What the date marks
arXiv submission
Kind
Technique
Organisation
Stanford / SUNY Buffalo
Access
BSD-3-Clause
Other dates
NeurIPS 2022; FA2 2023, FA3 2024
Threads
In the guided tour
Chapter 9. Making it run anywhere
Learning phases
1. Transformer architecture; 6. Local models and deployment

A milestone: 3 later milestones descend directly from it, and its descendants reach 4 of the 10 threads. How levels are decided

Drafted with Claude from the paper on 15 October 2026; reviewed by Zeeshan Arif. Read from arxiv.org (arXiv version 2 (June 2022); the paper was later published at NeurIPS 2022).

What it changed

It made exact Transformer attention several times faster and much lighter on memory, not by doing less arithmetic but by reading and writing the GPU’s slow memory far less often.

The problem before it

Self-attention compares every token with every other token, so its time and memory grow with the square of the sequence length. Long inputs were slow and quickly ran out of memory.

Many methods tried to fix this by computing attention only approximately, for example with sparse attention such as BigBird, or with low-rank shortcuts. They cut the number of arithmetic operations, but the authors point out that many of them were not actually faster in wall-clock time, and few were widely used.

The authors’ diagnosis: on modern GPUs, arithmetic has become much faster than memory. A GPU has a small, very fast on-chip memory (SRAM, about 20 MB in total on an A100) and a large, slower main memory (HBM, 40 to 80 GB). Standard attention writes the full N × N table of attention scores to HBM, reads it back for the softmax, writes it again, and reads it again. Most of the time goes into this traffic, not into the arithmetic. The usual programming tools, such as PyTorch, give no fine control over which memory is used.

The idea

The mechanism. FlashAttention computes exactly the same result as standard attention, but never stores the full N × N table in HBM. It is “IO-aware”: it is designed around the cost of moving data between the two kinds of memory. It uses two known techniques:

All the steps (matrix multiply, mask, softmax, dropout, matrix multiply) run in one GPU program, a single “kernel”, written in CUDA. Memory use grows only linearly with sequence length.

The paper also gives block-sparse FlashAttention: the same method, but skipping blocks that a fixed sparsity pattern marks as empty. This is an approximate method, and faster again.

The notation. Standard attention computes S = QKᵀ, P = softmax(S), O = PV, where Q, K and V have N rows (the sequence length) and d columns (the head size). The paper proves that standard attention needs Θ(Nd + N²) accesses to HBM, while FlashAttention needs Θ(N²d²/M), where M is the size of SRAM. With typical d of 64 to 128 and M of about 100 KB, d² is many times smaller than M. The paper also proves that no exact attention method can do asymptotically better for all SRAM sizes.

What it showed

Reported in the paper. We read version 2 of the arXiv paper, a preprint. The paper was later published at NeurIPS 2022. Training times are on 8 A100 GPUs unless stated.

Limits, and what came later

The limits come from the paper:

Later work built on it directly. FlashAttention-3 is a later version of the same kernel. Mistral 7B used FlashAttention for efficient attention. Mamba took up its hardware-aware design.

What to know first

How to read it

The main text is 10 pages, followed by references, proofs and full results.

  1. Read section 1 with Figure 1. The left side shows the two memories and the loops.
  2. Read section 2 for the GPU background and standard attention.
  3. Read section 3.1 and Algorithm 1. The softmax rescaling is the key step.
  4. Look at Figure 2 (left), then read section 4 with Tables 1 to 4.
  5. Read section 5 for the authors’ own limits.

Skip the proofs in Appendix C on a first reading. Expect about 1.5 hours for the parts above (an estimate).

In brief, from the research data

Before it

Long context was attacked by approximating attention with sparse patterns.

The problem

Attention was slow because of memory traffic between GPU levels, not because of arithmetic.

What it introduced

Tile the computation so it stays in fast on-chip memory and never writes the full attention matrix. Exact, not approximate.

Why it matters

It made long context an engineering problem rather than an architectural compromise, and most approximations became unnecessary.

What modern models inherit

The kernel your model is almost certainly using right now.

What came next

FlashAttention-2 and 3, vLLM's serving stack, and the long-context race.

To understand it

Learn

  • Memory hierarchy on a GPU
  • Tiling to avoid writing the attention matrix
  • Exact versus approximate attention

Helpful first

Self-attention and why it is quadratic.

Before and after

The milestones it descends from, and the milestones that descend from it. Work developed alongside it is not counted. Weak links are left out unless you include them.

Before: where it came from

FlashAttention has 12 documented ancestors across 5 chapters, reaching back to Recurrent networks in 1990. Showing its direct parents and the 5 turning points among them.

FlashAttention has 12 documented ancestors across 5 chapters, reaching back to Recurrent networks in 1990. Showing all of them.

FlashAttention has 14 recorded ancestors, including indirect ones, across 5 chapters, reaching back to Recurrent networks in 1990. Showing its direct parents and the 5 turning points among them.

FlashAttention has 14 recorded ancestors, including indirect ones, across 5 chapters, reaching back to Recurrent networks in 1990. Showing all of them.

  1. 2022FlashAttentionThis milestone.
  2. 2020BigBirdFlashAttention succeeded thisChapter 10. Reading more than you remember
  3. 2019Sparse Transformer Turning pointBigBird was influenced by thisChapter 4. Pretrain once, use everywhere
  4. 2017Transformer Turning pointFlashAttention built directly on thisChapter 3. Attention, then only attention
  5. 2016Layer normTransformer built directly on thisChapter 3. Attention, then only attention
  6. 2015Residual connectionsTransformer was influenced by thisChapter 3. Attention, then only attention
  7. 2015Luong attentionTransformer was influenced by thisChapter 3. Attention, then only attention
  8. 2014AdamTransformer was influenced by thisChapter 2. Words become vectors
  9. 2014Seq2seq Turning pointTransformer built directly on thisChapter 2. Words become vectors
  10. 2014Attention Turning pointTransformer built directly on thisChapter 3. Attention, then only attention
  11. 2014GRU / encoder-decoderAttention built directly on thisChapter 2. Words become vectors
  12. 1997LSTM Turning pointSeq2seq built directly on thisChapter 1. Language as probability
  13. 1990Recurrent networksLSTM was influenced by thisChapter 1. Language as probability
  1. 2022FlashAttentionThis milestone.
  2. 2020BigBirdFlashAttention succeeded thisChapter 10. Reading more than you remember
  3. 2019Sparse Transformer Turning pointBigBird was influenced by thisChapter 4. Pretrain once, use everywhere
  4. 2019Transformer-XLSparse Transformer was influenced by this (indirect influence)Chapter 4. Pretrain once, use everywhere
  5. 2017Transformer Turning pointFlashAttention built directly on thisChapter 3. Attention, then only attention
  6. 2017ConvS2STransformer was influenced by this (indirect influence)Chapter 3. Attention, then only attention
  7. 2016Layer normTransformer built directly on thisChapter 3. Attention, then only attention
  8. 2015Residual connectionsTransformer was influenced by thisChapter 3. Attention, then only attention
  9. 2015Luong attentionTransformer was influenced by thisChapter 3. Attention, then only attention
  10. 2014AdamTransformer was influenced by thisChapter 2. Words become vectors
  11. 2014Seq2seq Turning pointTransformer built directly on thisChapter 2. Words become vectors
  12. 2014Attention Turning pointTransformer built directly on thisChapter 3. Attention, then only attention
  13. 2014GRU / encoder-decoderAttention built directly on thisChapter 2. Words become vectors
  14. 1997LSTM Turning pointSeq2seq built directly on thisChapter 1. Language as probability
  15. 1990Recurrent networksLSTM was influenced by thisChapter 1. Language as probability

After: what it led to

FlashAttention has 5 documented descendants across 2 chapters, reaching forward to FlashAttention-3 in 2024. Showing its direct descendants.

FlashAttention has 5 documented descendants across 2 chapters, reaching forward to FlashAttention-3 in 2024. Showing all of them.

FlashAttention has 7 recorded descendants, including indirect ones, across 2 chapters, reaching forward to Muse Spark in 2026. Showing its direct descendants.

FlashAttention has 7 recorded descendants, including indirect ones, across 2 chapters, reaching forward to Muse Spark in 2026. Showing all of them.

  1. 2022FlashAttentionThis milestone.
  2. 2023Mistral 7Bwas influenced by FlashAttentionChapter 8. The weights get out
  3. 2023Mambawas influenced by FlashAttentionChapter 9. Making it run anywhere
  4. 2023Mixtral 8x7Bbuilt directly on Mistral 7BChapter 8. The weights get out
  5. 2024Gemmawas influenced by Mistral 7BChapter 8. The weights get out
  6. 2024FlashAttention-3built directly on FlashAttentionChapter 9. Making it run anywhere
  1. 2022FlashAttentionThis milestone.
  2. 2023Mistral 7Bwas influenced by FlashAttentionChapter 8. The weights get out
  3. 2023Mambawas influenced by FlashAttentionChapter 9. Making it run anywhere
  4. 2023Mixtral 8x7Bbuilt directly on Mistral 7BChapter 8. The weights get out
  5. 2024Gemmawas influenced by Mistral 7BChapter 8. The weights get out
  6. 2024FlashAttention-3built directly on FlashAttentionChapter 9. Making it run anywhere
  7. 2025Llama 4was influenced by Mixtral 8x7B (indirect influence)Chapter 8. The weights get out
  8. 2026Muse Sparksucceeded Llama 4Chapter 8. The weights get out

Direct links and their sources

Where it came from

Built directly on

Succeeded

What it led to

Built on directly by

Influenced

Developed alongside

Terms