← Back to AI Infrastructure Mind Map
🖥️ 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=QK⊤dS = \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=l⋅em−mnew+exi−mnewl_{new} = l \cdot e^{m - m_{new}} + e^{x_i - m_{new}}, rescaling the output by em−mnewe^{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.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of FlashAttention Kernel & Online Softmax.
Use Cases
In production engineering, what are the primary use cases for FlashAttention Kernel & Online Softmax and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Derive standard attention's HBM bottleneck: why the N×NN \times N round-trip between S=QK⊤dS = \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=l⋅em−mnew+exi−mnewl_{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
🎯
Verify Mastery: Practice Exam Questions for "FlashAttention Kernel & Online Softmax"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardInference Quantization (cross-module)Next Card →KV Cache Optimization

🔗 More AI Infrastructure Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/AI_Infra/card/flashattention-kernel
中文速览与面试要点

FlashAttention 与 Online Softmax (FlashAttention Kernel & Online Softmax)

定义: FlashAttention = 对标准注意力做 GPU 访存优化:通过 SRAM 切块(Tiling)与 Online Softmax 增量归一化,把 HBM 读写从 O(N^2) 降到 O(N^2 / M)(大 N 时近似 O(N),M 为 SRAM 块容量)。标准注意力 S = QK^ d、softmax、O = SV,N N 的 S 矩阵需写回 HBM 再读回——这是 O(N^2) 的访存瓶颈;FlashAttention 把 Q/K/V 切成块常驻 SRAM,逐块计算局部 softmax 并用运行最大值 + 重缩放合并: 处理元素 x_i 时,m_new = (m, x_i),l_new = l e^m - m_new + e^x_i - m_new,输出按 e^m - m_new 重缩放,最终 1l 归一化,数学结果与标准 softmax 完全一致。 使用场景: 长序列注意力(4K/32K/1M 上下文)训练与推理的标准 kernel;面试高频“FlashAttention 为什么快”“Online Softmax 是什么”“FA-1/2/3 区别”;也是 PD 分离、KV 量化等推理优化的底层支撑。 解决的问题: 标准注意力受 HBM 带宽支配:长序列下 S 矩阵 O(N^2) 读写主导耗时,且显存 O(N^2) 存不下。FlashAttention-1(2022)用切块 + Online Softmax + 反传重算(不存 S)把 HBM 访问降一个数量级、显存从 O(N^2) 降到 O(N),64K 序列上比 PyTorch eager 快 2-4 倍;FA-2(2023)做多头并行、把 N^2 的 softmax 开销移出并行域,约再快 2 倍;FA-3(2024)用 Hopper 的 warp specialization + TMA 异步拷贝,并把 FP8 引入 kernel。

核心要点

  • 推导标准注意力的 HBM 瓶颈: 为什么 S = QK^ d 与 softmax 之间的 N N 矩阵往返是 O(N^2) 访存;长序列(如 128K)下为什么带宽主导?
  • 写出 Online Softmax 增量公式: m_new = (m, x_i)、l_new = l e^m - m_new + e^x_i - m_new;为什么需要 running max + 重缩放而不能直接累加 e^x_i?
  • Tiling 细节: 块大小受什么限制(每 SM SRAM 如 H100 约 228KB)?HBM 访问如何从 O(N^2) 降到 O(N^2 / M);causal mask 如何在 kernel 内处理?
  • FA-1/2/3 演进: 反传重算为何把显存从 O(N^2) 降到 O(N);FA-2 的多头并行与 upcast 优化;FA-3 的 warp specialization/TMA 异步与 FP8 各解决什么?
  • FlashAttention 与 KV cache/PagedAttention 的关系: kernel 层优化为何不与推理引擎层优化冲突;为什么 decode 阶段带宽瓶颈(逐 token 读权重)不能靠 FA 解决?