arXivDaily arXiv每日学术速递 周一至周五更新
感恩节,周五至周末无更新,大家顺遂。周一再来吧
arXiv 2307.08691cs.LG
收藏

FlashAttention-2:通过更好的并行性和工作划分实现更快的注意力

FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning

Princeton University(普林斯顿大学);Stanford University(斯坦福大学)

Tri Dao

AI总结:

本文提出 FlashAttention-2,通过减少非矩阵乘法计算、跨线程块并行及在线程块内优化 warp 工作划分,将注意力速度提升至原版约 2 倍,并在 GPT 端到端训练中达到高 FLOP 利用率。

摘要:

过去几年,将 Transformers 扩展到更长的序列长度一直是一个重要问题,这有望提升语言建模和高分辨率图像理解的性能,并开启代码、音频和视频生成中的新应用。注意力层是扩展到更长序列的主要瓶颈,因为其运行时间和内存会随序列长度呈二次增长。FlashAttention 利用非对称的 GPU 内存层次结构,在不使用近似的情况下带来显著的内存节省(线性而非二次)和运行时加速(相比优化后的基线为 2-4 倍)。然而,FlashAttention 仍远不及优化的矩阵乘法(GEMM)操作快,只能达到理论最大 FLOPs/s 的 25-40%。我们观察到,这种低效源于 GPU 上不同线程块和 warp 之间次优的工作划分,导致占用率低或不必要的共享内存读/写。我们提出 FlashAttention-2,通过更好的工作划分来解决这些问题。具体而言,我们(1)调整算法以减少非矩阵乘法 FLOP 的数量;(2)即使针对单个注意力头,也将注意力计算并行化到不同线程块上,以提高占用率;(3)在每个线程块内,将工作分配给各个 warp,以减少通过共享内存进行的通信。这些改进使速度相比 FlashAttention 提升约 2 倍,在 A100 上达到理论最大 FLOPs/s 的 50-73%,并接近 GEMM 操作的效率。我们通过实验验证,当端到端用于训练 GPT 风格模型时,FlashAttention-2 在每块 A100 GPU 上达到最高 225 TFLOPs/s 的训练速度(72% 的模型 FLOP 利用率)。

英文摘要:

Scaling Transformers to longer sequence lengths has been a major problem in the last several years, promising to improve performance in language modeling and high-resolution image understanding, as well as to unlock new applications in code, audio, and video generation. The attention layer is the main bottleneck in scaling to longer sequences, as its runtime and memory increase quadratically in the sequence length. FlashAttention exploits the asymmetric GPU memory hierarchy to bring significant memory saving (linear instead of quadratic) and runtime speedup (2-4$\times$ compared to optimized baselines), with no approximation. However, FlashAttention is still not nearly as fast as optimized matrix-multiply (GEMM) operations, reaching only 25-40\% of the theoretical maximum FLOPs/s. We observe that the inefficiency is due to suboptimal work partitioning between different thread blocks and warps on the GPU, causing either low-occupancy or unnecessary shared memory reads/writes. We propose FlashAttention-2, with better work partitioning to address these issues. In particular, we (1) tweak the algorithm to reduce the number of non-matmul FLOPs (2) parallelize the attention computation, even for a single head, across different thread blocks to increase occupancy, and (3) within each thread block, distribute the work between warps to reduce communication through shared memory. These yield around 2$\times$ speedup compared to FlashAttention, reaching 50-73\% of the theoretical maximum FLOPs/s on A100 and getting close to the efficiency of GEMM operations. We empirically validate that when used end-to-end to train GPT-style models, FlashAttention-2 reaches training speed of up to 225 TFLOPs/s per A100 GPU (72\% model FLOPs utilization).

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑