Flash Attention

IO-aware exact attention: the tiling algorithm, online softmax trick, memory hierarchy exploitation, FlashAttention-2 improvements, and FlashAttention-3 with FP8 — achieving 2-4x speedup without approximation.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Memory Hierarchy Problem
  5. Online Softmax: The Key Insight
  6. FlashAttention Algorithm
  7. Memory Analysis
  8. FlashAttention-2: Optimizations
  9. FlashAttention-3: FP8 and Pipelining
  10. Backward Pass
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Explain the GPU memory hierarchy (SRAM vs HBM) and its impact on attention.
  2. Derive the online softmax algorithm for block-wise computation.
  3. Trace through the FlashAttention tiling algorithm step by step.
  4. Prove that FlashAttention computes exact attention with O(T)O(T) HBM memory.
  5. Explain the key improvements in FlashAttention-2 and FlashAttention-3.

Notation

  • HBM\text{HBM} — High Bandwidth Memory (GPU main memory, ~80 GB, ~2 TB/s)
  • SRAM\text{SRAM} — Static RAM (on-chip, ~20 MB, ~19 TB/s)
  • Br,BcB_r, B_c — block sizes for rows (queries) and columns (keys)
  • i,mi\ell_i, m_i — running softmax statistics (sum and max)

Core Intuition

Standard attention computes the full T×TT \times T attention matrix and stores it in GPU memory (HBM). This is wasteful: we write T2T^2 values to slow HBM only to immediately read them back for the matrix multiply with V. FlashAttention never materializes this matrix — it computes attention in tiles that fit in fast on-chip SRAM, fusing all operations into a single kernel. Same math, different execution order, dramatic speedup.

FlashAttention Tiling

Attention matrix (N×N)Memory (HBM)Standard: O(N²) = 64Flash: O(N) ≈ 32Tile 1/16Never materialize full matrixProcess tiles in fast SRAMSaved: 50% HBM
Block
2
Tile
0
Active tileProcessed
Explore: FlashAttention fuses QKᵀ, softmax, and ×V tile-by-tile in fast SRAM. Same exact output, but HBM usage scales O(N) instead of materializing O(N²).

The Memory Hierarchy Problem

GPU memory hierarchy:

  • SRAM (L1/shared memory): ~20 MB, ~19 TB/s bandwidth. Fast but tiny.
  • HBM (DRAM): ~80 GB, ~2 TB/s bandwidth. Large but 10x slower.

Standard attention IO cost: For TT tokens, dd-dimensional:

  1. Write S=QKT\mathbf{S} = \mathbf{QK}^T to HBM: O(T2)O(T^2) writes.
  2. Read S\mathbf{S} from HBM for softmax: O(T2)O(T^2) reads.
  3. Write A=softmax(S)\mathbf{A} = \text{softmax}(\mathbf{S}) to HBM: O(T2)O(T^2) writes.
  4. Read A\mathbf{A} from HBM for AV\mathbf{AV}: O(T2)O(T^2) reads.

Total HBM accesses: O(T2)O(T^2) — dominates the runtime for long sequences.

FlashAttention: Reduces HBM accesses to O(T2d2/M)O(T^2d^2/M) where MM is SRAM size — much smaller.


Online Softmax: The Key Insight

Problem: Softmax requires the global maximum over all keys to prevent overflow:

softmax(sj)=esjmkeskm,m=maxksk.(1)\text{softmax}(s_j) = \frac{e^{s_j - m}}{\sum_k e^{s_k - m}}, \quad m = \max_k s_k. \tag{1}

If we process keys in blocks, we don't know the global max until we've seen all blocks. How to compute softmax incrementally?

Online softmax algorithm. Maintain running statistics (mi,i)(m_i, \ell_i):

  • mim_i = max of scores seen so far (through block ii)
  • i\ell_i = sum of exponentials seen so far (with current max)

Update rule when incorporating new block with scores snew\mathbf{s}_{\text{new}}:

mnew=max(mold,max(snew)),(2)m_{\text{new}} = \max(m_{\text{old}}, \max(\mathbf{s}_{\text{new}})), \tag{2} new=oldemoldmnew+jesjnewmnew,(3)\ell_{\text{new}} = \ell_{\text{old}} \cdot e^{m_{\text{old}} - m_{\text{new}}} + \sum_j e^{s_j^{\text{new}} - m_{\text{new}}}, \tag{3} onew=ooldoldemoldmnewnew+jesjnewmnewvjnew.(4)\mathbf{o}_{\text{new}} = \mathbf{o}_{\text{old}} \cdot \frac{\ell_{\text{old}} \cdot e^{m_{\text{old}} - m_{\text{new}}}}{\ell_{\text{new}}} + \frac{\sum_j e^{s_j^{\text{new}} - m_{\text{new}}} \mathbf{v}_j}{\ell_{\text{new}}}. \tag{4}

This rescales the accumulated output whenever the max changes — numerically exact.


FlashAttention Algorithm

Input: Q,K,VRT×d\mathbf{Q}, \mathbf{K}, \mathbf{V} \in \mathbb{R}^{T \times d} in HBM. Output: ORT×d\mathbf{O} \in \mathbb{R}^{T \times d} in HBM.

Procedure:

  1. Divide Q\mathbf{Q} into blocks of BrB_r rows, K,V\mathbf{K}, \mathbf{V} into blocks of BcB_c rows.
  2. For each Q-block ii:
    • Initialize Oi=0\mathbf{O}_i = \mathbf{0}, i=0\ell_i = \mathbf{0}, mi=m_i = -\infty.
    • For each K/V-block jj:
      • Load Qi,Kj,Vj\mathbf{Q}_i, \mathbf{K}_j, \mathbf{V}_j from HBM to SRAM.
      • Compute Sij=QiKjT/dk\mathbf{S}_{ij} = \mathbf{Q}_i\mathbf{K}_j^T / \sqrt{d_k} in SRAM.
      • Update online softmax: compute new mi,im_i, \ell_i.
      • Update output: Oi\mathbf{O}_i accumulates weighted values.
    • Write final Oi\mathbf{O}_i to HBM.

Block sizes chosen so that Br×d+Bc×d+Br×BcMB_r \times d + B_c \times d + B_r \times B_c \leq M (fits in SRAM).


Memory Analysis

HBM reads:

  • Q\mathbf{Q}: O(Td)O(Td) (each Q-block read once)
  • K,V\mathbf{K}, \mathbf{V}: O(TdT/Br)O(Td \cdot T/B_r) = O(T2d/Br)O(T^2d/B_r) (each K/V-block read once per Q-block)

HBM writes: O(Td)O(Td) (only the output O\mathbf{O}).

Total HBM accesses: O(T2d2/M)O(T^2d^2/M) where MM = SRAM size.

Attention matrix storage: O(Br×Bc)O(B_r \times B_c) in SRAM — never stored in full in HBM. So HBM memory for the attention matrix: O(T)O(T) (just the statistics m,m, \ell).

Compared to standard: O(T2)O(T^2) HBM memory → O(T)O(T) HBM memory. For T=16KT=16K: saves ~1 GB of memory for the attention matrix alone.


FlashAttention-2: Optimizations

Key improvements over FlashAttention-1:

1. Reduced non-matmul FLOPs: Restructure the online softmax update to minimize scalar operations. Move rescaling to the end of the inner loop.

2. Better parallelism: FlashAttention-1 parallelizes over batch and heads. FlashAttention-2 additionally parallelizes over the sequence length dimension (Q-blocks), improving GPU occupancy.

3. Warp-level optimizations: Partition work within a thread block to reduce shared memory reads/writes between warps.

Result: ~2x speedup over FlashAttention-1, reaching 50–73% of theoretical peak FLOPS on A100.


FlashAttention-3: FP8 and Pipelining

Targets Hopper architecture (H100):

1. Asynchronous pipelining: Overlap GEMM computation with softmax and data loading using the Tensor Memory Accelerator (TMA).

2. FP8 quantization within the kernel: Perform Q×K in FP8 for higher throughput (doubling FLOPS vs FP16), then accumulate in FP32 for numerical stability.

3. Block quantization: Quantize K/V blocks to FP8 with per-block scaling factors.

Result: Up to 1.5-2x speedup over FlashAttention-2 on H100 GPUs.


Backward Pass

Challenge: Standard backward pass needs the attention matrix A\mathbf{A}, which FlashAttention doesn't store.

Solution: Recompute A\mathbf{A} block-by-block during backward (same tiling as forward). Store only the softmax statistics (mi,i)(m_i, \ell_i) to enable exact recomputation.

Extra cost: One additional pass over Q, K, V (~33% overhead compared to storing A\mathbf{A}). But memory savings far outweigh the compute cost.

Gradient computation:

LQ=LOOAASSQ,(5)\frac{\partial \mathcal{L}}{\partial \mathbf{Q}} = \frac{\partial \mathcal{L}}{\partial \mathbf{O}} \cdot \frac{\partial \mathbf{O}}{\partial \mathbf{A}} \cdot \frac{\partial \mathbf{A}}{\partial \mathbf{S}} \cdot \frac{\partial \mathbf{S}}{\partial \mathbf{Q}}, \tag{5}

computed block-wise using the stored statistics for exact softmax gradient.


Common Pitfalls

Pitfall 1. Assuming FlashAttention is an approximation. It computes exact standard attention — the same output as the naive algorithm, just with different execution order.

Pitfall 2. Using FlashAttention with very small TT. For short sequences (T<512T < 512), the tiling overhead can make FlashAttention slower than standard fused attention.

Pitfall 3. Expecting FlashAttention to reduce FLOPs. The FLOP count is identical (O(T2d)O(T^2d)). The speedup comes entirely from reduced memory IO, not less computation.


Summary

  • FlashAttention exploits the GPU memory hierarchy: compute in fast SRAM, minimize slow HBM access.
  • Online softmax enables block-wise exact attention computation.
  • Memory: O(T)O(T) HBM instead of O(T2)O(T^2); enables 4–16x longer sequences.
  • Speed: 2–4x faster than standard attention (IO-bound → compute-bound).
  • FlashAttention-2: better parallelism, 2x over v1.
  • FlashAttention-3: FP8, H100 pipelining, 1.5–2x over v2.
  • Exact computation — no quality trade-off.

Exercises

Exercise 1. For SRAM size M=192M = 192 KB, d=128d=128, compute the maximum block sizes Br=BcB_r = B_c that fit (in FP16).

Exercise 2. Derive the total HBM bytes accessed by FlashAttention for T=8192,d=128,Br=Bc=128,M=192T=8192, d=128, B_r=B_c=128, M=192 KB.

Exercise 3. Prove that the online softmax (equations 2–4) produces the same result as computing the global softmax.

Exercise 4. Compute the memory savings (in GB) from FlashAttention vs standard attention for T=32768,H=32,dk=128T=32768, H=32, d_k=128 in FP16.

Exercise 5. Explain why FlashAttention's backward pass stores O(T)O(T) values (the statistics) rather than the full O(T2)O(T^2) attention matrix.