arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~
arXiv 2410.01359cs.LG
收藏

FlashMask:FlashAttention的高效且丰富的掩码扩展

FlashMask: Efficient and Rich Mask Extension of FlashAttention

Baidu Inc.(百度公司)

Guoxia Wang, Jinle Zeng, Xiyuan Xiao, Siming Wu, Jiabin Yang, Lujing Zheng, Zeyu Chen, Jiang Bian, Dianhai Yu, Haifeng Wang

AI总结:

本文提出FlashMask,通过引入列式稀疏掩码表示扩展FlashAttention,实现线性内存复杂度并消除冗余计算,在LLM微调与对齐训练中实现1.65x至3.22x端到端加速,内核性能超越FlexAttention最高达60.7%。

摘要:

标准注意力机制的计算和内存需求随序列长度$N$呈二次方增长,这给Transformer模型处理长序列带来了巨大挑战。FlashAttention通过消除$O(N^2)$的内存依赖,并通过感知IO的内存优化来减少注意力延迟,从而缓解了这些挑战。然而,其对某些注意力掩码类型的原生支持有限,并且不能固有地适应更复杂的掩码需求。以前的方法求助于使用具有$O(N^2)$内存复杂度的稠密掩码,导致效率低下。在本文中,我们提出了FlashMask,这是FlashAttention的扩展,它引入了注意力掩码的列式稀疏表示。这种方法能高效地表示广泛的掩码类型,并促进了优化内核实现的开发。通过采用这种新颖的表示方法,FlashMask实现了线性内存复杂度$O(N)$,适用于长上下文序列建模。此外,该表示方法支持内核优化,通过利用注意力掩码中的稀疏性来消除不必要的计算,在不牺牲计算精度的情况下,实现了更高的计算效率。我们在SFT、LoRA、DPO和RM等LLM的微调和对齐训练中评估了FlashMask的性能。FlashMask实现了显著的吞吐量提升,与现有的FlashAttention稠密方法相比,端到端加速比从1.65倍到3.22倍不等。此外,我们的内核级比较表明,在内核TFLOPs/s方面,FlashMask超越了最新的同类产品FlexAttention 12.1%至60.7%,在A100 GPU上达到了理论最大FLOPs/s的37.8%至62.3%。该代码已在PaddlePaddle上开源并集成到PaddleNLP中,支持上下文长达128K tokens的超过1000亿参数的模型。

英文摘要:

The computational and memory demands of vanilla attention scale quadratically with the sequence length $N$, posing significant challenges for processing long sequences in Transformer models. FlashAttention alleviates these challenges by eliminating the $O(N^2)$ memory dependency and reducing attention latency through IO-aware memory optimizations. However, its native support for certain attention mask types is limited, and it does not inherently accommodate more complex masking requirements. Previous approaches resort to using dense masks with $O(N^2)$ memory complexity, leading to inefficiencies. In this paper, we propose FlashMask, an extension of FlashAttention that introduces a column-wise sparse representation of attention masks. This approach efficiently represents a wide range of mask types and facilitates the development of optimized kernel implementations. By adopting this novel representation, FlashMask achieves linear memory complexity $O(N)$, suitable for modeling long-context sequences. Moreover, this representation enables kernel optimizations that eliminate unnecessary computations by leveraging sparsity in the attention mask, without sacrificing computational accuracy, resulting in higher computational efficiency. We evaluate FlashMask's performance in fine-tuning and alignment training of LLMs such as SFT, LoRA, DPO, and RM. FlashMask achieves significant throughput improvements, with end-to-end speedups ranging from 1.65x to 3.22x compared to existing FlashAttention dense method. Additionally, our kernel-level comparisons demonstrate that FlashMask surpasses the latest counterpart, FlexAttention, by 12.1% to 60.7% in terms of kernel TFLOPs/s, achieving 37.8% to 62.3% of the theoretical maximum FLOPs/s on the A100 GPU. The code is open-sourced on PaddlePaddle and integrated into PaddleNLP, supporting models with over 100 billion parameters for contexts up to 128K tokens.

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

↑