Flex Attention:一种生成优化注意力核的编程模型
Flex Attention: A Programming Model for Generating Optimized Attention Kernels
AI总结:
针对FlashAttention单体式结构导致新注意力变体研发困难、手写高效融合核难度大的问题,提出编译器驱动的FlexAttention编程模型,仅需少量PyTorch代码即可实现多数注意力变体,性能媲美手写核,还支持变体组合以解决组合爆炸问题。
摘要:
过去7年间,注意力已成为深度学习中最重要的基元之一。优化注意力的主流方法是FlashAttention,它将运算融合在一起,大幅提升了运行速度并降低了内存消耗。然而,FlashAttention的重要性及其单体式的特性给想要尝试新型注意力变体的研究人员带来了一个问题——“软件彩票”困境。编写高效的融合注意力核难度极大,传统的基于编译器的方法难以奏效,这进一步加剧了该问题。我们提出了FlexAttention,这是一种全新的编译器驱动编程模型,仅需几行符合惯用写法的PyTorch代码,就能实现绝大多数注意力变体。我们验证了多种现有注意力变体(如Alibi、文档掩码(Document Masking)、PagedAttention等)均可通过FlexAttention实现,且其性能与这些手写核相比具有竞争力。最后,我们展示了FlexAttention如何轻松实现注意力变体的组合,解决了注意力变体的组合爆炸问题。
英文摘要:
Over the past 7 years, attention has become one of the most important primitives in deep learning. The primary approach to optimize attention is FlashAttention, which fuses the operation together, drastically improving both the runtime and the memory consumption. However, the importance of FlashAttention combined with its monolithic nature poses a problem for researchers aiming to try new attention variants -- a "software lottery". This problem is exacerbated by the difficulty of writing efficient fused attention kernels, resisting traditional compiler-based approaches. We introduce FlexAttention, a novel compiler-driven programming model that allows implementing the majority of attention variants in a few lines of idiomatic PyTorch code. We demonstrate that many existing attention variants (e.g. Alibi, Document Masking, PagedAttention, etc.) can be implemented via FlexAttention, and that we achieve competitive performance compared to these handwritten kernels. Finally, we demonstrate how FlexAttention allows for easy composition of attention variants, solving the combinatorial explosion of attention variants.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。