Understanding FlashAttention: Fast and Memory-Efficient Exact Attention
A deep dive into IO-awareness, GPU SRAM vs HBM memory hierarchy, and how tiling makes attention fast without approximation.
Authors: Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré
Standard attention in Transformers suffers from an memory footprint and runtime bottleneck with respect to sequence length . FlashAttention addresses this not by approximating attention (like sparse or low-rank attention), but by redesigning the computation around the GPU memory hierarchy.
The Core Bottleneck: Memory Bandwidth (IO), Not FLOPs
Modern GPUs (such as A100 or H100) have massive compute capabilities (hundreds of TFLOPs), but accessing High Bandwidth Memory (HBM) is orders of magnitude slower than accessing on-chip SRAM:
- SRAM (on-chip): ~19 TB/s bandwidth, but only ~20 MB per SM.
- HBM (off-chip): ~1.5–3 TB/s bandwidth, 40–80 GB capacity.
In standard attention:
- Load from HBM compute write back to HBM ().
- Load from HBM compute write back to HBM ().
- Load from HBM compute write back to HBM.
The repeated read/write of intermediate matrices to slow HBM is what chokes standard attention.
FlashAttention’s Key Innovations
1. Tiling & Online Softmax
Instead of computing the full softmax across the entire sequence at once, FlashAttention tiles the inputs into blocks that fit entirely within SRAM.
Using the online softmax trick, we can incrementally compute the softmax normalizer across blocks without storing the entire matrix:
# Online softmax accumulator concept
m_new = max(m_prev, row_max(block))
d_new = exp(m_prev - m_new) * d_prev + sum(exp(block - m_new))This lets us compute exact attention in a single pass through fast SRAM, keeping intermediate matrices off HBM entirely.
2. Recomputation in Backward Pass
Standard attention caches the attention matrix for backpropagation. FlashAttention instead stores only the normalization statistics and recomputes the attention matrix block-by-block during the backward pass in SRAM.
Counterintuitively, recomputing attention in SRAM is significantly faster than reading the cached matrix from HBM!
Key Takeaways
- Hardware-aware algorithm design matters: Peak FLOPs rarely bottleneck deep learning workloads today; memory hierarchy and IO bandwidth dominate.
- Exact is viable: You don’t always need to trade accuracy for speed—optimizing IO gave a 2-4x wall-clock speedup while computing mathematically identical attention.