二十四:Flash Attention自测题¶
来源:http://mp.weixin.qq.com/s?__biz=MzYyNTk3Njg1NA==&mid=2247484254&idx=1&sn=0a670819333e1fde2bce51a7465d29fb&chksm=f01eb027c7693931c3cda16253677f96552b6d151a5cdf047f49edaa21eb49f849514d49c3d1#rd
A. Attention 基础与瓶颈¶
-
请写出 scaled dot-product attention 的标准公式,并说明 Q、K、V、S、P、O 分别代表什么。
-
在 self-attention 中,如果输入形状是
[B, H, N, D],attention score 和输出的形状分别是什么? -
标准 attention 的计算复杂度是多少?主要来自哪两次矩阵乘?
-
标准 attention 的显存复杂度为什么会出现
O(N^2)? -
为什么长序列训练中 attention 往往受 memory bandwidth 限制,而不只是 FLOPs 限制?
-
请用一个具体例子估算 attention matrix 的显存占用。
-
FlashAttention 优化的是 attention 的哪一部分瓶颈?
-
FlashAttention 是否改变了 attention 的数学定义?为什么?
-
FlashAttention 和 sparse attention、linear attention 的根本区别是什么?
-
为什么说 FlashAttention 对长序列更有价值,而短序列上收益可能不明显?
B. GPU 存储层次与 IO-Aware¶
-
GPU 中 HBM、SRAM/shared memory、register 的速度和容量关系是什么?
-
IO-aware algorithm 的核心思想是什么?
-
为什么单纯统计 FLOPs 不能准确解释 FlashAttention 的加速?
-
标准 attention 中哪些中间结果会被写入和读回 HBM?
-
FlashAttention 如何减少 HBM 读写?
-
kernel fusion 在 FlashAttention 中起到什么作用?
-
为什么普通 PyTorch 算子组合很难达到 FlashAttention 的效果?
-
tiling 的基本思想是什么?
-
block size 的选择会受到哪些硬件和模型因素影响?
-
FlashAttention 为什么需要专门 CUDA/Triton kernel?
C. Forward 原理与 Online Softmax¶
-
FlashAttention forward 的整体计算流程是什么?
-
对一个 Q block,FlashAttention 为什么要逐块扫描 K/V block?
-
FlashAttention 中为什么不能简单对每个 score block 单独做 softmax?
-
online safe softmax 需要维护哪些统计量?
-
请写出 online softmax 中
m_new和l_new的更新公式。 -
为什么更新
m后,旧的l和旧的累积输出需要重新缩放? -
FlashAttention 中
acc或 partial output 表示什么? -
为什么 FlashAttention 可以不显式保存完整 attention probability
P? -
在 FlashAttention forward 中,最终输出
O_block如何由累积量得到? -
online softmax 如何保证数值稳定?
-
FlashAttention 的结果和标准 attention 是否 bitwise identical?为什么?
-
causal mask 在 block-wise attention 中如何处理?
-
padding mask 和变长序列在 FlashAttention 中通常如何处理?
-
dropout 在 FlashAttention 中有什么特殊注意点?
-
attention scale
1/sqrt(d)在 FlashAttention 中应在哪里生效?
D. Backward、显存与复杂度¶
-
标准 attention backward 为什么需要大量显存?
-
FlashAttention backward 的核心策略是什么?
-
FlashAttention backward 通常保存哪些较小的中间量?
-
为什么 backward 中重算局部 score 反而可能让整体训练更快?
-
请写出 attention backward 中
dV、dP、dS、dQ、dK的基本关系。 -
FlashAttention 的计算复杂度是否从
O(N^2D)降低到了O(ND)?请解释。 -
FlashAttention 的 activation memory 相比标准 attention 主要省在哪里?
-
FlashAttention 和 gradient checkpointing 有什么相似点和区别?
E. 版本演进与硬件特性¶
-
FlashAttention 1 的核心贡献是什么?
-
FlashAttention 2 相比 FlashAttention 1 主要改进了什么?
-
FlashAttention 2 为什么强调更好的并行性和 work partitioning?
-
FlashAttention 3 主要面向什么硬件和优化目标?
-
FlashAttention 3 中异步数据搬运和矩阵乘重叠的意义是什么?
-
FlashAttention 3 为什么会关注 FP8?
-
FlashAttention 1/2/3 的共同主线是什么?
-
版本越新是否一定在所有场景都更快?为什么?
-
FlashAttention 和 PyTorch memory-efficient attention 有什么关系和区别?
-
FlashAttention 与 xFormers、Triton attention kernel 在工程上如何理解?
F. 工程使用与模型集成¶
-
PyTorch
scaled_dot_product_attention的常见输入形状是什么? -
PyTorch SDPA 如何选择 math、memory-efficient、FlashAttention 等后端?
-
使用 PyTorch SDPA 时,哪些条件可能导致没有走 FlashAttention 后端?
-
flash-attn 包常见 API 的 Q/K/V layout 和 PyTorch SDPA 有什么差异?
-
在 Hugging Face Transformers 中启用 FlashAttention 或 SDPA 时,需要注意哪些配置?
-
MHA、MQA、GQA 在 Q/K/V head 数上有什么区别?
-
FlashAttention 对 prefill 和 decode 阶段的收益为什么不同?
-
KV cache 场景下,decode 阶段的主要瓶颈通常是什么?
-
使用 FlashAttention 时为什么仍可能需要 paged attention 或专门 decode kernel?
G. 调试、评测与面试综合¶
-
如何正确 benchmark FlashAttention 的速度和显存收益?
-
为什么 benchmark 时需要 warmup 和
torch.cuda.synchronize()? -
如果使用 FlashAttention 后速度没有提升,你会如何排查?
-
如果使用 FlashAttention 后显存没有下降,你会如何排查?
-
如果输出结果异常或出现 NaN,你会如何排查?
-
FlashAttention 常见限制有哪些?
-
面试中如何用 1 分钟清晰解释 FlashAttention 的原理?
-
请完整比较标准 attention、FlashAttention、sparse attention、linear attention 在数学精确性、复杂度、显存、速度收益和适用场景上的差异。
预览时标签不可点<div class="