Files
myWiki/concepts/flash-attention.md

42 lines
1.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: FlashAttention
created: 2025-04-15
updated: 2026-05-01
type: concept
tags: []
sources: []
---
# FlashAttention
**IO感知的精确注意力优化**,由 Dao 等 2022 年提出,是注意力计算效率的最大单次突破。
## 核心洞见
传统注意力实现的瓶颈不是计算FLOPs而是**GPU 内存层级之间的数据传输**IO。标准实现需要多次读写 HBM高带宽内存而 HBM 带宽远低于 SRAM。
## 关键创新
1. **Tiling**: 将注意力矩阵分块计算,每块保持在快速 SRAM 中
2. **Recomputation**: 反向传播时重新计算 softmax 而非存储中间结果,节省内存
3. **IO-Aware**: 算法设计以最小化 HBM↔SRAM 数据传输为核心目标
## 性能收益
- **速度**: 比标准注意力快 2-4x
- **内存**: 内存占用量从 O(n²) 降至 O(n)
- **精度**: 数值精确(非近似),无精度损失
## 版本演进
- **FlashAttention-1** (2022): Tiling + Recomputation
- **FlashAttention-2** (2023): 更好的并行化和 work partitioning
- **FlashAttention-3** (2024): 异步计算 + 低精度 (FP8)
## 相关概念
- [[flash-attention-3]] — 最新版本
- [[kv-cache-bottleneck]] — KV 缓存瓶颈FlashAttention 不直接解决但互补)
- [[sparse-attention-patterns]] — 稀疏注意力也可结合 FlashAttention
- [[llm-attention-survey-2026]] — 综述参考