FlashAttention computes exact attention in small blocks in fast GPU memory, cutting slow memory traffic. Training gets faster and models can read longer texts.
Attention is the step that lets every word in a text look at every other word. Doubling the text makes the work four times larger, so many papers tried to cut the arithmetic. Most of them did not make real programs faster. This paper found why: the slow part on a GPU (a chip made for many small calculations at once) is moving data around in memory, not the arithmetic. Fixing that made training faster and let models read longer texts.
| Title | FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness |
| Authors | Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré |
| Year | 2022 |
| Venue | arXiv 2205.14135 (the paper is widely cited as a NeurIPS 2022 paper; the arXiv version does not state a venue) |
| Link | arxiv.org/abs/2205.14135 |
Compute attention in small blocks that fit in the GPU's fast memory, so the huge table of scores is never written to slow memory.
The answer is exact: it is the same result as normal attention, not an approximation. Only the order of the steps and where the numbers are stored change.
A GPU has a big, slower memory called HBM (high bandwidth memory) and a tiny, much faster memory on the chip called SRAM. The paper's figures for an A100 GPU:
| Memory | Size | Speed |
|---|---|---|
| SRAM (on chip) | about 20 MB in total (192 KB for each of 108 processors) | about 19 TB/s |
| HBM | 40 GB | 1.5 TB/s |
SRAM is about ten times faster and about two thousand times smaller. Every GPU operation loads data from HBM, calculates, and writes the result back to HBM. An operation is memory-bound when its time is set by these trips, not by the arithmetic. The paper says operations such as softmax and masking are memory-bound.
With words and head size , attention computes three things:
, and are the query, key and value tables. is a table of scores, and is the same table after softmax (a step that turns each row of scores into weights that add up to 1). Standard code writes to HBM, reads it back, writes to HBM, and reads it back again. For that is over a million entries per head, per layer, going back and forth.
Tiling means cutting the work into blocks that fit in SRAM. FlashAttention splits , and into blocks. It loads a block of keys and values, then loops over blocks of queries. For each pair of blocks it computes a small piece of in SRAM, uses it, and discards it. Only the final output goes back to HBM.
Copy one block of keys and values to SRAM
The paper shows this needs HBM accesses, where is the SRAM size, against for standard attention.
Tiling has one difficulty. Softmax needs a whole row of scores at once, but a block holds only part of a row. The fix is to keep two numbers per row: the largest score so far and the running sum of . (Subtracting the largest score keeps the numbers from overflowing.) When a new block arrives, rescale the old sum to the new largest score and add:
One row of scores is , split into blocks and .
Block 1: , . Block 2: , . Combine: , .
Weights are . Computing softmax on the whole row at once gives exactly the same four numbers. The output is updated the same way, with its old part rescaled each time a new block comes in.
This is why the method is exact: the rescaling is algebra, not an estimate.
Training also needs a backward pass, which uses and to work out how to adjust the model. Saving them would bring back the memory problem. So FlashAttention saves only the output and the two statistics per row, then recomputes and block by block inside SRAM. This costs extra arithmetic, but it saves slow HBM trips. The paper notes this is like gradient checkpointing, except that it also gets faster rather than only saving memory.
Measured for GPT-2 medium on an A100 (sequence length 1024, forward and backward, Figure 2 of the paper):
| Standard | FlashAttention | |
|---|---|---|
| Arithmetic (GFLOPs) | 66.6 | 75.2 |
| HBM reads and writes (GB) | 40.3 | 4.4 |
| Runtime (ms) | 41.7 | 7.3 |
FlashAttention does more arithmetic and is still about 5.7 times faster, because HBM traffic was the bottleneck.
Training speed (baseline in brackets):
Longer context, GPT-2 small:
| Implementation | Context | Perplexity | Training time |
|---|---|---|---|
| Megatron-LM | 1K | 18.2 | 4.7 days |
| FlashAttention | 1K | 18.2 | 2.7 days |
| FlashAttention | 4K | 17.5 | 3.6 days |
Perplexity measures how surprised the model is by new text, so lower is better. The 4K model is 0.7 better and still faster than Megatron-LM at 1K. Longer documents also helped classification (up to 8.5 points on ECtHR and 4.3 on MIMIC-III compared with length 512).
On Path-X (sequence length 16K) FlashAttention reached 61.4% accuracy, and the block-sparse version reached 63.1% on Path-256 (length 64K). Earlier Transformers in that table did no better than chance there. Block-sparse FlashAttention is the paper's approximate variant, which skips some blocks.
These results show a real speed-up for exact attention on the A100 GPU in these setups. They do not show the same factors on other chips or models.