通过结构感知内核实现快速精确的相对位置注意力

·

研究论文 · 2026/11/09

相对位置注意力机制在音频等领域优于RoPE,但其逐对偏移计算难以融入融合注意力内核,导致内存和计算开销大。本文提出一种精确注意力算法,将加性相对位置项融入FlashAttention式分块计算,通过偏移感知分块、关系乘积复用和基于偏移的梯度分片,在Triton中统一实现。在8K序列上,前向和反向分别加速1.8-6.4倍和2.0-21倍,峰值内存降低2.9-128倍和1.4-54倍,端到端长上下文转录提速1.8倍且内存减少2.4倍。

该工作解决了相对位置注意力难以高效融入现代注意力内核的瓶颈,提供了硬件高效的即插即用方案,显著提升长序列任务的效率和可扩展性。

Relative-position attention mechanisms offer compelling alternatives to RoPE across various transformer architectures, delivering superior performance in domains such as audio. These mechanisms typically modify attention scores by adding content-position or bias-position terms. Unlike RoPE, which encodes position via per-token rotations directly on the queries and keys, relative-position terms generally require pairwise offset-dependent computation or lookups, complicating their integration into fused attention kernels and introducing severe memory, bandwidth, and compute overheads as sequences scale. To address this bottleneck, we propose an exact attention algorithm that incorporates additive relative-position terms into FlashAttention-style tiled computation. Our method (1) uses offset-aware tiling to avoid redundant indexing and memory accesses, (2) computes only the necessary key/query-relation products and reuses them across token pairs in both forward and backward passes, and (3) uses offset-based gradient sharding and aggregation, eliminating atomic contention. We provide a unified, efficient Triton implementation across diverse relative-position attention mechanisms and evaluate it against naive and specialized implementations. For 8K-token sequences, our method speeds up the forward (backward) pass by 1.8-6.4times (2.0-21times) and reduces peak memory up to 2.9-128times (1.4-54times) over the strongest baseline for each mechanism, enabling long-context end-to-end transcription that is 1.8times faster and uses 2.4times less memory. Our method thus provides a drop-in, hardware-efficient solution for scaling relative-position attention transformers.

阅读原文

参与讨论