用于鲁棒低精度LLM推理的在线伪平均移位注意力(PASA):算法与数值分析
Online Pseudo-average Shifting Attention(PASA) for Robust Low-precision LLM Inference: Algorithms and Numerical Analysis
AI总结:
针对长序列推理中注意力计算耗时问题,提出基于Flash Attention的低精度等价算法PASA,通过在线伪平均移位和全局恢复技术避免半精度溢出并提升受限硬件性能,经多模型验证有效。
摘要:
在大模型中,对于文本或图像/视频生成等长序列推理任务,注意力计算极其耗时。为了加速这一过程,我们基于Flash Attention开发了一种低精度、数学上等价的算法,称为PASA。PASA引入了两项新技术:在线伪平均移位和全局恢复。这些技术允许在整个Flash Attention过程中使用半精度计算,而不会引起溢出不稳定性或不可接受的数值精度损失。该算法通过减少数据移动和增加计算FLOPs,提升了在内存受限的AI硬件架构(如昇腾神经网络处理单元(NPU))上的性能。该算法通过设计的随机基准和真实大模型进行了验证。我们发现,在两类不同的大模型(Qwen2-7B语言模型和Stable-Video-Diffusion多模态模型)中,注意力输入数据的大偏置和振幅是导致数值溢出(半精度下>65504)的关键因素。具体而言,在Stable-Video-Diffusion模型中,溢出是由于序列维度上的大偏置以及查询和键在头维度上的共振机制引起的。共振机制定义为查询和键矩阵之间的相位重合或180度相位偏移。它会显著放大注意力分数矩阵的元素值。这个问题同样适用于Qwen模型。此外,通过均方根误差(RMSE)以及将最终生成的文本和视频与使用高精度注意力生成的结果进行比较,对数值精度进行了评估。
英文摘要:
Attention calculation is extremely time-consuming for long-sequence inference tasks, such as text or image/video generation, in large models. To accelerate this process, we developed a low-precision, mathematically-equivalent algorithm called PASA, based on Flash Attention. PASA introduces two novel techniques: online pseudo-average shifting and global recovering. These techniques enable the use of half-precision computation throughout the Flash Attention process without incurring overflow instability or unacceptable numerical accuracy loss. This algorithm enhances performance on memory-restricted AI hardware architectures, such as the Ascend Neural-network Processing Unit(NPU), by reducing data movement and increasing computational FLOPs. The algorithm is validated using both designed random benchmarks and real large models. We find that the large bias and amplitude of attention input data are critical factors contributing to numerical overflow ($>65504$ for half precision) in two different categories of large models (Qwen2-7B language models and Stable-Video-Diffusion multi-modal models). Specifically, overflow arises due to the large bias in the sequence dimension and the resonance mechanism between the query and key in the head dimension of the Stable-Video-Diffusion models. The resonance mechanism is defined as phase coincidence or 180-degree phase shift between query and key matrices. It will remarkably amplify the element values of attention score matrix. This issue also applies to the Qwen models. Additionally, numerical accuracy is assessed through root mean square error (RMSE) and by comparing the final generated texts and videos to those produced using high-precision attention.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。