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

Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré

Published
Lab
Stanford
Citations
6,036

TL;DR

FlashAttention is an exact attention algorithm that tiles the computation to cut reads and writes between GPU HBM and on-chip SRAM, so Transformers train faster and can use longer context.

Read this as a

Why builders care

Editor's note

Attention's quadratic cost is often an IO problem, not just a math one. This paper got wall-clock training speedups without approximating attention, and made much longer sequences practical.

How to apply it

Editor's note
  1. 1Use an IO-aware exact attention kernel before reaching for approximate attention that trades quality for speed.
  2. 2The measured training speedups: 15% end-to-end on BERT-large (sequence length 512) versus the MLPerf 1.1 record, 3 speedup on GPT-2 (length 1K), and 2.4 speedup on long-range arena (length 1K-4K).
  3. 3If you need very long context, the same work reports the first Transformers with better-than-chance accuracy on Path-X at length 16K (61.4%) and Path-256 at length 64K (63.1%).

Code

Not found

No verified code link yet. We checked the abstract, arXiv comments and Hugging Face; if the authors release code later it will show up here.

Practical notes

AI-generated
  • Account for reads and writes between levels of GPU memory by making attention algorithms IO-aware.
  • Use tiling to reduce the number of memory reads and writes between GPU high bandwidth memory and on-chip SRAM.

Key results

From the abstract
  1. 1
    15% end-to-end wall-clock speedup

    FlashAttention trains Transformers faster than existing baselines: 15% end-to-end wall-clock speedup on BERT-large (seq. length 512) compared to the MLPerf 1.1 training speed record, 3 speedup on GPT-2 (seq. length 1K), and 2.4 speedup on long-range arena (seq. length 1K-4K).

  2. 2
    61.4% accuracy

    FlashAttention and block-sparse FlashAttention enable longer context in Transformers, yielding higher quality models (0.7 better perplexity on GPT-2 and 6.4 points of lift on long-document classification) and entirely new capabilities: the first Transformers to achieve better-than-chance performance on the Path-X challenge (seq. length 16K, 61.4% accuracy) and Path-256 (seq. length 64K, 63.1% accuracy).

  • Transformers are slow and memory-hungry on long sequences, since the time and memory complexity of self-attention are quadratic in sequence length.
  • Approximate attention methods have attempted to address this problem by trading off model quality to reduce the compute complexity, but often do not achieve wall-clock speedup.

  • We argue that a missing principle is making attention algorithms IO-aware -- accounting for reads and writes between levels of GPU memory.
  • We propose FlashAttention, an IO-aware exact attention algorithm that uses tiling to reduce the number of memory reads/writes between GPU high bandwidth memory (HBM) and GPU on-chip SRAM.
  • We analyze the IO complexity of FlashAttention, showing that it requires fewer HBM accesses than standard attention, and is optimal for a range of SRAM sizes.

Transformers are slow and memory-hungry on long sequences, since the time and memory complexity of self-attention are quadratic in sequence length. Approximate attention methods have attempted to address this problem by trading off model quality to reduce the compute complexity, but often do not achieve wall-clock speedup. We argue that a missing principle is making attention algorithms IO-aware -- accounting for reads and writes between levels of GPU memory. We propose FlashAttention, an IO-aware exact attention algorithm that uses tiling to reduce the number of memory reads/writes between GPU high bandwidth memory (HBM) and GPU on-chip SRAM. We analyze the IO complexity of FlashAttention, showing that it requires fewer HBM accesses than standard attention, and is optimal for a range of SRAM sizes. We also extend FlashAttention to block-sparse attention, yielding an approximate attention algorithm that is faster than any existing approximate attention method. FlashAttention trains Transformers faster than existing baselines: 15% end-to-end wall-clock speedup on BERT-large (seq. length 512) compared to the MLPerf 1.1 training speed record, 3 speedup on GPT-2 (seq. length 1K), and 2.4 speedup on long-range arena (seq. length 1K-4K). FlashAttention and block-sparse FlashAttention enable longer context in Transformers, yielding higher quality models (0.7 better perplexity on GPT-2 and 6.4 points of lift on long-document classification) and entirely new capabilities: the first Transformers to achieve better-than-chance performance on the Path-X challenge (seq. length 16K, 61.4% accuracy) and Path-256 (seq. length 64K, 63.1% accuracy).

Everything on this page is written from this abstract. For methods and full results, read the paper PDF.

Ask the paper

Answers come from the paper summary and abstract.

Ask anything about “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness”, or start with one of these:

Closest matches across the feed and every collection, by shared words in titles, topics and abstracts.