Back to AI Infrastructure Mind Map
中文·English
🖥️ AI InfrastructureID: flashattention-kernel

FlashAttention Kernel & Online Softmax

FlashAttention 与 Online Softmax
🎯Core Definition
FlashAttention is a GPU-memory-optimized attention kernel: by tiling Q/K/VQ/K/V into SRAM and merging partial softmaxes via Online Softmax, HBM traffic drops from O(N2)O(N^2) to O(N2/M)O(N^2 / M) (≈ O(N)O(N) for large NN; MM = SRAM block capacity). Standard attention computes S=QKdS = \frac{QK^\top}{\sqrt{d}}, softmax, then O=SVO = SV — the N×NN \times N matrix SS is written to and read back from HBM, an O(N2)O(N^2) memory bottleneck. FlashAttention keeps blocks of Q/K/VQ/K/V resident in SRAM, computes local softmax per block, and merges incrementally with a running max and rescaling: for element xix_i, mnew=max(m,xi)m_{new} = \max(m, x_i), lnew=lemmnew+eximnewl_{new} = l \cdot e^{m - m_{new}} + e^{x_i - m_{new}}, rescaling the output by emmnewe^{m - m_{new}} and normalizing by 1l\frac{1}{l} at the end — numerically identical to standard softmax.
💡Use Cases
the default kernel for long-context attention (4K/32K/1M tokens) in both training and inference; interview favorites: why FlashAttention is fast, what Online Softmax is, and FA-1/2/3 differences; it also underpins inference optimizations like PD separation and KV quantization.
Key Problems Solved
standard attention is dominated by HBM bandwidth — the O(N2)O(N^2) SS-matrix round-trip dominates runtime at long sequences, and O(N2)O(N^2) memory does not fit. FlashAttention-1 (2022) combines tiling, Online Softmax and backward recomputation (no SS stored) to cut HBM traffic by an order of magnitude and memory from O(N2)O(N^2) to O(N)O(N) — 2-4× faster than eager PyTorch at 64K sequences; FA-2 (2023) parallelizes across heads and moves the N2N^2 softmax overhead out of the parallel region, roughly doubling again; FA-3 (2024) exploits Hopper warp specialization, TMA asynchronous copies, and adds FP8.
🎯5 High-Frequency Exam Points
1
Derive standard attention's HBM bottleneck: why the N×NN \times N round-trip between S=QKdS = \frac{QK^\top}{\sqrt{d}} and softmax costs O(N2)O(N^2) traffic; why bandwidth dominates at long sequences (e.g. 128K)?
2
Write the Online Softmax updates: mnew=max(m,xi)m_{new} = \max(m, x_i), lnew=lemmnew+eximnewl_{new} = l \cdot e^{m - m_{new}} + e^{x_i - m_{new}}; why do you need a running max with rescaling instead of directly accumulating exie^{x_i}?
3
Tiling details: what limits block size (e.g. ~228KB SRAM per SM on H100)? How does HBM traffic drop from O(N2)O(N^2) to O(N2/M)O(N^2 / M); how is the causal mask handled inside the kernel?
4
FA-1/2/3 evolution: why backward recomputation cuts memory from O(N2)O(N^2) to O(N)O(N); FA-2's head parallelism and upcast optimization; what FA-3's warp specialization/TMA async and FP8 solve?
5
Relation between FlashAttention and KV cache/PagedAttention: why kernel-level optimization composes with engine-level optimization; why the decode bandwidth bottleneck (reading weights per token) cannot be fixed by FA alone?
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "FlashAttention Kernel & Online Softmax"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardInference Quantization (cross-module)Next CardKV Cache Optimization

🔗 More AI Infrastructure Knowledge Cards

Activation Memory EstimationAgent Runtime (cross-module)Autoscaling & CostCheckpointing & Recovery