Chip Foundry Services Request Assessment

Flash Attention Io Aware

Flash Attention is the IO-aware exact attention algorithm that computes self-attention with $O(N)$ memory instead of $O(N^2)$ and $2-4\times $ faster wall-clock time than standard attention — by restructuring the attention computation into tiles that fit in GPU SRAM (shared memory), minimizing expensive reads and writes to GPU HBM (global memory), without any approximation or change to the mathematical output.

The Memory Bandwidth Bottleneck

Standard attention implementation:
1. Compute $S = \mathrm{QK}^T (N\times N$ matrix) — write to $\mathrm{HBM}: O(N^2)$ writes.
2. Compute $P = \operatorname{softmax}(S)$ — read $S$ from HBM, write $P: O(N^2)$ reads + writes.
3. Compute $O = \mathrm{PV}$ — read $P$ and $V$ from $\mathrm{HBM}: O(N^2)$ reads.

Total HBM accesses: $O(N^2). For N=8K$ with $d=128$ at FP16: the $N\times N$ attention matrix is 128 MB — larger than the GPU's $\mathrm{SRAM} (\sim 20 \mathrm{\text{MB}}$ per SM), forcing multiple round-trips to/from HBM (bandwidth: $\sim 2$ TB/s on A100 vs. $\sim 19$ TB/s SRAM bandwidth).

Flash Attention Algorithm

Key insight: compute attention in tiles without ever materializing the full $N\times N$ attention matrix in HBM.

1. Outer loop: Iterate over blocks of $K$ and $V$ (block size $B_c$).
2. Inner loop: For each $K/V$ block, iterate over blocks of $Q$ (block size $B_r$).
3. In SRAM: Load a $Q$ block and $K/V$ block into SRAM. Compute the local attention scores $S_{ij} = Q_i \cdot K_j^T$. Compute local softmax (with online softmax tracking running max and sum). Compute local output $O_{ij} = \operatorname{softmax}(S_{ij}) \cdot V_j$. Accumulate into the output using the running softmax denominator.
4. Write only O: The final output $O$ is written to HBM once. The $N\times N$ attention matrix never exists in HBM.

Online Softmax

The mathematical challenge: softmax requires the max and sum across all $K$ positions, but we process $K$ in blocks. The online softmax algorithm (Milakov & Gimelshein, 2018) maintains running max and exponential sum, updating them as each new $K$ block is processed. This allows correct softmax computation without a second pass over the data.

Performance Impact

MetricStandard AttentionFlash Attention
Memory$O(N^2)$$O(N)$
HBM reads/writes$O(N^2)$$O(N^2/M)$ where $M = \mathrm{SRAM}$ size
Wall clock (A100, $N=4096$)$\sim 15$ ms$\sim 5$ ms
Max sequence length (40 GB)$\sim 16K$$\sim 64K+$

Flash Attention 2 and 3

  • Flash Attention 2: Better work partitioning across GPU warps and thread blocks. $\text{Non-matmul} \mathrm{FLOP}s$ reduced $2-4\times . Achieves$ 50-73% of A100 peak FLOPS (vs. 25-40% for FA1).
  • Flash Attention 3: Optimized for Hopper architecture (H100). Uses asynchronous TMA (Tensor Memory Accelerator) for hardware-accelerated data movement, FP8 support, and warp-specialized pipelining. Up to $1.5-2\times $ speedup over FA2 on H100.

Integration

Flash Attention is integrated into PyTorch (torch.nn.functional.scaled_dot_product_attention), HuggingFace Transformers, and all major LLM training/inference frameworks. It is the default attention implementation for virtually all modern LLM training.

Flash Attention is the systems optimization that made long-context Transformers practical — demonstrating that the attention bottleneck was not the $O(N^2)$ computation but the $O(N^2)$ memory traffic, and that restructuring the computation to respect the GPU memory hierarchy eliminates the bottleneck without changing a single mathematical operation.