Back to Feed
Efficiency & Inference

Fast and Memory Efficient Exact Attention

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

Listen to the summary

Uses a voice available on your device

Audio options
On this page 4 sections
Related concepts 1 concepts

Key Takeaways

  • Transformers are slow and memory-hungry on long sequences because self-attention complexity is quadratic in sequence length.
  • FlashAttention restructures computation by splitting the input into blocks, making several passes to incrementally perform softmax reduction, and storing normalization factors for the backward pass.
  • The method achieves a 15% end-to-end wall-clock speedup on BERT-large with sequence length 512 compared to the MLPerf 1.1 training speed record.
  • FlashAttention yields a 0.7 improvement in perplexity on GPT-2 and 6.4 points of lift on long-document classification.
  • Block-sparse FlashAttention achieves 63.1% accuracy on Path-256 with a sequence length of 64K.

Summary & Methodology Analysis

Transformers are slow and memory-hungry on long sequences because the time and memory complexity of self-attention is quadratic in sequence length. To solve this, the authors developed FlashAttention, which restructures the attention computation to split the input into blocks and makes several passes over input blocks to incrementally perform the softmax reduction. It stores the softmax normalization factor from the forward pass to quickly recompute attention on-chip in the backward pass. Furthermore, the authors implement FlashAttention in CUDA to achieve fine-grained control over memory access and fuse all attention operations into one GPU kernel, and they extend it to block-sparse attention by skipping zero blocks based on a predefined block sparsity mask.

The method yields significant performance improvements across multiple models and tasks. FlashAttention achieves a 15% end-to-end wall-clock speedup on BERT-large with a sequence length of 512 compared to the MLPerf 1.1 training speed record. It yields a 0.7 improvement in perplexity on GPT-2, 6.4 points of lift on long-document classification, and 61.4% accuracy on the Path-X challenge with a sequence length of 16K. Additionally, block-sparse FlashAttention achieves 63.1% accuracy on Path-256 with a sequence length of 64K. The models and datasets used in the research include BERT-large, GPT-2, BERT, Linformer, OpenWebtext, ListOps, Text, Retrieval, Image, Pathfinder, Performer, Reformer, Smyrf, RoBERTa, MIMIC-III, ECtHR, Path-X, Path-256, and Path-64.

Despite its strong performance, the approach has limitations. Writing a new CUDA kernel for each new attention implementation requires significant engineering effort and a considerably lower-level language than PyTorch. Furthermore, CUDA implementations may not be transferrable across GPU architectures. The paper does not specify any other limitations beyond these two engineering constraints.

Interactive System Flowchart

Click diagram to expand and zoom

Cross-Examination & FAQs

A deeper dive clarifying mechanics, constraints, and baseline evaluations.

Q1. What is the core problem addressed by the paper?

Transformers are slow and memory-hungry on long sequences because the time and memory complexity of self-attention are quadratic in sequence length.

Q2. What is FlashAttention?

FlashAttention is a fast and memory-efficient exact attention method built with IO-awareness that restructures attention computation using block splitting and GPU kernel fusion.

Q3. What are some of the main results achieved by FlashAttention?

FlashAttention achieves a 15% end-to-end wall-clock speedup on BERT-large, a 0.7 improvement in perplexity on GPT-2, and 6.4 points of lift on long-document classification.

Q4. How does FlashAttention handle the backward pass?

It stores the softmax normalization factor from the forward pass to quickly recompute attention on-chip in the backward pass.

Q5. What is block-sparse FlashAttention?

It is an extension of FlashAttention that skips zero blocks based on a predefined block sparsity mask.

Q6. What accuracy does FlashAttention achieve on the Path-X challenge?

FlashAttention achieves 61.4% accuracy on the Path-X challenge with a sequence length of 16K.

Q7. What accuracy does block-sparse FlashAttention achieve on Path-256?

Block-sparse FlashAttention achieves 63.1% accuracy on Path-256 with a sequence length of 64K.

Q8. What are the limitations of FlashAttention?

Writing a new CUDA kernel for each new attention implementation requires significant engineering effort and a lower-level language than PyTorch, and CUDA implementations may not be transferrable across GPU architectures.

Q9. Which models and datasets are mentioned in the research?

The paper references BERT-large, GPT-2, BERT, Linformer, OpenWebtext, ListOps, Text, Retrieval, Image, Pathfinder, Performer, Reformer, Smyrf, RoBERTa, MIMIC-III, ECtHR, Path-X, Path-256, and Path-64.

Flag an issue

What is wrong with this summary?

What is wrong?