FlashAttention:快速且内存高效的精确注意力机制,具有 IO 感知能力

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness

特里·道 Tri Dao · Stanford · 2022-05-27 · arXiv:2205.14135 ↗ · 被引 4671

打开互动全文版(逐段中英对照 + 图/公式 + 论文问答)→

摘要 · Abstract

Transformer 在处理长序列时速度慢且内存消耗大,因为自注意力的时间和内存复杂度与序列长度呈二次方关系。近似注意力方法试图通过牺牲模型质量来降低计算复杂度,但通常无法实现实际加速。我们认为缺失的原则是使注意力算法具有 IO 感知能力——即考虑 GPU 内存层级之间的读写操作。我们提出了 FlashAttention,一种具有 IO 感知能力的精确注意力算法,它使用分块技术来减少 GPU 高带宽内存(HBM)和片上 SRAM 之间的内存读写次数。我们分析了 FlashAttention 的 IO 复杂度,表明它比标准注意力需要更少的 HBM 访问,并且在多种 SRAM 大小下都是最优的。我们还将 FlashAttention 扩展到块稀疏注意力,产生了一种比任何现有近似注意力方法更快的近似注意力算法。FlashAttention 训练 Transformer 的速度比现有基线更快:在 BERT-large(序列长度 512)上,相比 MLPerf 1.1 训练速度记录,端到端加速 15%;在 GPT-2(序列长度 1K)上加速 3 倍;在长距离竞技场(序列长度 1K-4K)上加速 2.4 倍。FlashAttention 和块稀疏 FlashAttention 使 Transformer 能够处理更长的上下文,从而获得更高质量的模型(GPT-2 上困惑度降低 0.7,长文档分类提升 6.4 个百分点),并实现了全新的能力:首个在 Path-X 挑战(序列长度 16K,准确率 61.4%)和 Path-256(序列长度 64K,准确率 63.1%)上取得优于随机水平性能的 Transformer。

Transformers are slow and memory-hungry on long sequences, since the time and memory complexity of self-attention are quadratic in sequence length. Approximate attention methods have attempted to address this problem by trading off model quality to reduce the compute complexity, but often do not achieve wall-clock speedup. We argue that a missing principle is making attention algorithms IO-aware -- accounting for reads and writes between levels of GPU memory. We propose FlashAttention, an IO-aware exact attention algorithm that uses tiling to reduce the number of memory reads/writes between GPU high bandwidth memory (HBM) and GPU on-chip SRAM. We analyze the IO complexity of FlashAttention, showing that it requires fewer HBM accesses than standard attention, and is optimal for a range of SRAM sizes. We also extend FlashAttention to block-sparse attention, yielding an approximate attention algorithm that is faster than any existing approximate attention method. FlashAttention trains Transformers faster than existing baselines: 15% end-to-end wall-clock speedup on BERT-large (seq. length 512) compared to the MLPerf 1.1 training speed record, 3$\times$ speedup on GPT-2 (seq. length 1K), and 2.4$\times$ speedup on long-range arena (seq. length 1K-4K). FlashAttention and block-sparse FlashAttention enable longer context in Transformers, yielding higher quality models (0.7 better perplexity on GPT-2 and 6.4 points of lift on long-document classification) and entirely new capabilities: the first Transformers to achieve better-than-chance performance on the Path-X challenge (seq. length 16K, 61.4% accuracy) and Path-256 (seq. length 64K, 63.1% accuracy).

核心贡献 · Key contributions

局限 · Limitations

论文章节 · Sections(共 14)

阅读逐段中英对照全文 →