Paper Synthesis • • written with human intelligence

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.

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

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

Standard attention in Transformers suffers from an O(N2)O(N^2) memory footprint and runtime bottleneck with respect to sequence length NN. 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:

  1. Load Q,KQ, K from HBM →\to compute S=QKTS = QK^T →\to write SS back to HBM (N×NN \times N).
  2. Load SS from HBM →\to compute P=softmax(S)P = \text{softmax}(S) →\to write PP back to HBM (N×NN \times N).
  3. Load P,VP, V from HBM →\to compute O=PVO = PV →\to write OO back to HBM.

The repeated read/write of N×NN \times N 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 N×NN \times N 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 N×NN \times N 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

  1. Hardware-aware algorithm design matters: Peak FLOPs rarely bottleneck deep learning workloads today; memory hierarchy and IO bandwidth dominate.
  2. 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.
Notice: This post was written and synthesized with human intelligence and genuine understanding — not generated by AI.
Tags: #Systems #LLMs #CUDA #Attention