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.
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Memory Hierarchy Problem
- Online Softmax: The Key Insight
- FlashAttention Algorithm
- Memory Analysis
- FlashAttention-2: Optimizations
- FlashAttention-3: FP8 and Pipelining
- Backward Pass
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Explain the GPU memory hierarchy (SRAM vs HBM) and its impact on attention.
- Derive the online softmax algorithm for block-wise computation.
- Trace through the FlashAttention tiling algorithm step by step.
- Prove that FlashAttention computes exact attention with HBM memory.
- Explain the key improvements in FlashAttention-2 and FlashAttention-3.
Notation
- — High Bandwidth Memory (GPU main memory, ~80 GB, ~2 TB/s)
- — Static RAM (on-chip, ~20 MB, ~19 TB/s)
- — block sizes for rows (queries) and columns (keys)
- — running softmax statistics (sum and max)
Core Intuition
Standard attention computes the full attention matrix and stores it in GPU memory (HBM). This is wasteful: we write 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
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 tokens, -dimensional:
- Write to HBM: writes.
- Read from HBM for softmax: reads.
- Write to HBM: writes.
- Read from HBM for : reads.
Total HBM accesses: — dominates the runtime for long sequences.
FlashAttention: Reduces HBM accesses to where is SRAM size — much smaller.
Online Softmax: The Key Insight
Problem: Softmax requires the global maximum over all keys to prevent overflow:
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 :
- = max of scores seen so far (through block )
- = sum of exponentials seen so far (with current max)
Update rule when incorporating new block with scores :
This rescales the accumulated output whenever the max changes — numerically exact.
FlashAttention Algorithm
Input: in HBM. Output: in HBM.
Procedure:
- Divide into blocks of rows, into blocks of rows.
- For each Q-block :
- Initialize , , .
- For each K/V-block :
- Load from HBM to SRAM.
- Compute in SRAM.
- Update online softmax: compute new .
- Update output: accumulates weighted values.
- Write final to HBM.
Block sizes chosen so that (fits in SRAM).
Memory Analysis
HBM reads:
- : (each Q-block read once)
- : = (each K/V-block read once per Q-block)
HBM writes: (only the output ).
Total HBM accesses: where = SRAM size.
Attention matrix storage: in SRAM — never stored in full in HBM. So HBM memory for the attention matrix: (just the statistics ).
Compared to standard: HBM memory → HBM memory. For : 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 , which FlashAttention doesn't store.
Solution: Recompute block-by-block during backward (same tiling as forward). Store only the softmax statistics to enable exact recomputation.
Extra cost: One additional pass over Q, K, V (~33% overhead compared to storing ). But memory savings far outweigh the compute cost.
Gradient computation:
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 . For short sequences (), the tiling overhead can make FlashAttention slower than standard fused attention.
Pitfall 3. Expecting FlashAttention to reduce FLOPs. The FLOP count is identical (). 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: HBM instead of ; 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 KB, , compute the maximum block sizes that fit (in FP16).
Exercise 2. Derive the total HBM bytes accessed by FlashAttention for 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 in FP16.
Exercise 5. Explain why FlashAttention's backward pass stores values (the statistics) rather than the full attention matrix.