arXiv:2611.01182cs.LGOct 2026

Manuscript Offprint

arXiv:2611.01182

Fast and Exact Relative-Position Attention via Structure-Aware Kernels

Delyan Boychev, Ivo Petrov, Dimitar I. Dimitrov

ABSTRACT

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.4× (2.0–21×) and reduces peak memory up to 2.9–128× (1.4–54×) over the strongest baseline for each mechanism, enabling long-context end-to-end transcription that is 1.8× faster and uses 2.4× less memory. Our method thus provides a drop-in, hardware-efficient solution for scaling relative-position attention transformers.

1 INTRODUCTION

The Transformer architecture (Vaswani et al., 2017) has revolutionized modern machine learning, from large language models (LLMs) in natural language processing to foundation models in speech, vision, and biology. At its core is the scaled dot-product attention (SDPA) mechanism, which allows input tokens to directly attend to each other and aggregate mutual information. However, scaling these models to larger inputs introduces two fundamental challenges, namely injecting scalable positional information into the network, and keeping the computation and memory footprint of SDPA tractable despite its quadratic complexity.

For standard content-based attention, IO-aware kernels such as FlashAttention (Dao et al., 2022; Dao, 2024) resolved the quadratic memory bottleneck by streaming blocks of tokens through fast on-chip SRAM, avoiding the materialization of the full N×NN \times N score matrix in GPU high-bandwidth memory (HBM). Paired with parameter-free, token-wise rotary position embeddings (RoPE) (Su et al., 2024), which can be seamlessly integrated into fused kernels, this pipeline now serves as the backbone of most autoregressive LLMs.

Relative-position attention Parameter-free positional encodings, such as RoPE and ALiBi (Press et al., 2022), are not without drawbacks. Despite their computational efficiency, they often fail to use context beyond the sequence lengths seen during training (Kazemnejad et al., 2023; Hua et al., 2025), where learned relative biases such as T5's generalize better (Kazemnejad et al., 2023; Li et al., 2024). Consequently, learnable relative-position encodings remain an enticing alternative for Transformer models. This class of mechanisms additively augments attention scores with learnable terms that depend on the relative distance i−ji-j between query and key tokens, either through content-to-position projections or scalar biases. This allows modeling explicit, shift-invariant relationships across sequences, and has remained enticing for speech models (Barrault et al., 2023; Sekoyan et al., 2025; Abouelenin et al., 2025), text encoding (He et al., 2023; Zaratiana et al., 2025), proteomics (Elnaggar et al., 2022), and genomics (Linder et al., 2025).

Efficiency limitations of relative-position attention Despite their modeling advantages, relative-position attention mechanisms are largely incompatible with standard fused attention kernels. Training or running inference with these models typically requires materializing the full N×NN \times N additive position matrix in HBM, reintroducing the exact memory-bandwidth bottleneck that FlashAttention was de-

¹INSAIT, Sofia University “St. Kliment Ohridski”. Correspondence to: Delyan Boychev delyan.boychev@insait.ai.

Preprint. Under review.

Figure

Figure 1. Overview of our method. (a) Each score adds the relation terms of its row r=ϕ(i,j)r=\phi(i,j), which stays constant on tiles far from the diagonal. (b) Our method first computes each query group gg only on its reachable rows ΩgQ\Omega_g^Q. (c) Each shared entry Zi,rQZ_{i,r}^Q is loaded once and reused across key tiles. (d) One thread block per query tile sums gradients on chip, avoiding atomic contention.

signed to eliminate. Programmable attention frameworks such as FlexAttention (Dong et al., 2025) offer a partial solution by fusing user-defined per-score callbacks and enabling limited precomputation; however, they still suffer from redundant computation, repeated memory lookups, and atomic contention during backpropagation. Lacking efficient kernels, practitioners often resort to replacing relative-position encodings entirely and retraining models from scratch, as seen in Flashzoi (Hingerl et al., 2025) and Flash-Swin (Kataria et al., 2026), or switch to RoPE-based alternatives such as ModernBERT (Warner et al., 2025).

Our work To address these limitations, we introduce an exact algorithm for relative-position mechanisms. Rather than treating pairwise offsets as opaque per-element lookups, our method exploits the block-level structure of relative distances across the attention matrix (Fig. 1a). Specifically, our method projects queries and keys only onto the offset ranges reachable within each token block (Fig. 1b), structures tile traversal around constant- and variable offset regions to maximize data reuse and avoid redundant indexing (Fig. 1c), and shards backward gradient accumulation by offset to eliminate atomic contention entirely (Fig. 1d).

Empirically, our method substantially reduces both runtime and memory consumption across forward and backward passes in production speech and text models. Our kernels significantly outperform generalist and specialized implementations, as well as state-of-the-art frameworks such as FlexAttention, FlashAttention-4 (Zadouri et al., 2026), and FlashInfer (Ye et al., 2025), even when those frameworks are augmented with our preprojected relation decomposition. In particular, for 8K-token sequences, our method beats the strongest baseline for each mechanism by 1.8–6.4× in the forward pass and 2.0–21× in the backward pass, while

reducing peak memory usage by 2.9–128× and 1.4–54×, respectively. Applied end-to-end to long-context speech transcription, it delivers a 1.8× speedup and 2.4× lower memory consumption on Parakeet-TDT-0.6B-v3 (Sekoyan et al., 2025), enabling significantly more efficient deployment and training.

Key contributions Our main contributions include:

  • A unified formulation of additive relative-position attention spanning four mechanisms, exposing shared structure amenable to kernel-level optimization.

  • An exact, drop-in tiled algorithm for fast and memory-efficient relative-position attention.

  • High-performance implementations of our method, validated against a high-precision reference, and comprehensive evaluations across diverse model architectures, sequence lengths, and NVIDIA GPU architectures.

References (78)