In a Transformer, attention compares tokens with other tokens to decide which information to combine. A straightforward implementation first writes a matrix of scores and probabilities to GPU device memory. Its size grows with the square of the sequence length. At long sequence lengths, reading and writing that intermediate can take substantial time even if the GPU can do the arithmetic quickly. Tri Dao and his collaborators treated those transfers as part of the problem in their 2022 FlashAttention paper. [1] [2]

The method works on blocks sized for fast on-chip memory. It keeps the running information needed to normalize scores across blocks and combines the partial results into the final output. During the backward pass it recomputes selected intermediate quantities instead of saving the full probability matrix. These are changes to how the dense calculation is performed; they do not require discarding low-scoring token pairs or altering the mathematical operation. [1] [2]

In one BERT-large training experiment on eight NVIDIA A100 GPUs, the researchers reported reaching the benchmark target in 17.4 minutes, versus 20.0 minutes for the comparison from MLPerf 1.1. In their GPT-2 experiment they reported up to three times faster end-to-end training than one Hugging Face implementation and up to 1.7 times faster than their Megatron-LM comparison. Those measurements describe particular software, models and hardware, not a fixed gain that applies to all Transformers. [2]

PyTorch later confirmed that it shipped a FlashAttention kernel for suitable inputs as part of its scaled dot-product attention support in PyTorch 2.0. Its release notes describe selecting between implementations, including a fallback for cases that do not meet the faster kernels’ constraints. This provides evidence that the technique was integrated into a separate framework; it does not independently reproduce every original benchmark. [3]

The algorithm trades some extra arithmetic for fewer slow memory transfers. In the original paper’s comparison for one GPT-2 attention configuration, FlashAttention performed more floating-point operations but transferred much less data to and from device memory, finishing the operation faster. Counting only operations would have predicted the wrong winner in that example. [2]

Exact dense attention still compares token pairs; avoiding a large stored matrix does not make the amount of dense arithmetic linear in sequence length. The authors also studied a block-sparse variant, but that is a separate approximate method. Teams choosing an implementation should distinguish memory required for intermediates, work performed by the kernel and total execution time. A gain on one measure does not imply the same gain on the others. [1] [2]

Analysis & application

Before changing a production model, benchmark the same sequence lengths, precision, masks and batch sizes used in the real workload. Record peak memory and elapsed time for a representative step, then verify which attention implementation the framework actually chose. A supported attention call can still use a fallback when an input does not meet the faster kernel’s requirements. Compare output and training behavior using tolerances appropriate to the precision, since equivalent formulas can produce small differences when operations are reordered. [2] [3]

The broader principle applies to other performance work: profile what data must move between memory levels, identify which intermediates are written only to be read immediately, and compare storing with recomputing. Report the gain for the whole workload as well as the isolated operation so readers can tell whether the change addresses their bottleneck. [1] [2]

Later versions continued optimizing how work is divided across the GPU. PyTorch announced a FlashAttention-2 integration in PyTorch 2.2, following its initial FlashAttention support. The original 2022 contribution remains distinct: it showed that changing how exact attention moves data could make the same dense calculation practical at workloads where the previous implementation spent too much time and memory storing intermediate results. [2] [3] [4]

FOLLOW THE EVIDENCE

The sources

  1. FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness arXiv · arxiv.org ·
  2. FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness NeurIPS proceedings · proceedings.neurips.cc
  3. Accelerated PyTorch 2 Transformers PyTorch · pytorch.org ·
  4. PyTorch 2.2: FlashAttention-v2 integration, AOTInductor PyTorch · pytorch.org ·