Rust in Everything
← Papers

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

Delyan Boychev, Ivo Petrov, Dimitar I. Dimitrov

2026-11-09▲ 19arXiv 2611.01182v1Hugging Face

AI-generated from the full paper (deepseek-flash). It may contain mistakes; check the original.

TL;DR

The paper presents an exact Triton tiled attention algorithm that fuses additive relative-position terms into FlashAttention-style computation with offset-aware reuse and atomic-free gradients, speeding up forward and backward passes and reducing memory.

Problem

Relative-position attention modifies attention scores by adding content–position or bias–position terms. Unlike RoPE's per-token rotations, these terms require pairwise offset-dependent computation or lookups, complicating fused attention kernels and adding memory, bandwidth, and compute overhead as sequences scale. Existing implementations gather or shift terms into a dense N×N bias or use pointwise callbacks that cannot exploit constant-offset tiles and cause atomic contention in backward.

Method

The paper proposes an exact attention algorithm that incorporates additive relative-position terms into FlashAttention-style tiled computation. It uses offset-aware tiling, computes only reachable key/query–relation products, and reuses them across token pairs in forward and backward passes. Scores are formed on chip as Eq. 7 with at most three scalar lookups from Z^Q, Z^K, and b; saturated tiles broadcast one entry per query and key. Backward uses offset-based gradient sharding and aggregation, assigning each query tile to one thread block, with a symmetric key pass, eliminating atomic additions. The method is implemented in Triton as a drop-in replacement for relative-position attention.

Results

  • For 8K-token sequences, the method speeds up the forward and backward passes and reduces peak memory over the strongest baseline for each mechanism, and enables faster lower-memory end-to-end transcription. §Abstract
  • Without atomic-free gradients, the backward pass is slower, needs more memory, and loses determinism. §5.4
  • Without projections, every tile computes its own relation products, slowing forward and backward passes but using much less memory. §5.4
  • The group size is aligned with the maximum tile size, giving only small column overhead at 8K tokens. §4.3.1
  • Recomputing projections instead of retaining them cuts memory for a small slowdown. §5.4

Limitations

  • Identity maps such as Transformer-XL's assign a distinct relation row to every offset, so projection work and space remain quadratic in sequence length; computing projections inside each tile makes space linear but is 2.0–2.9 times slower in the forward pass.
  • Maps whose relation rows do not cover consecutive offsets fall back to FP32 atomic accumulation, which is slower and not deterministic, although every evaluated mechanism avoids it.
  • The method favors generality across mechanisms, maps, and GPUs over hardware specialization; it uses no architecture-specific primitives such as warp specialization or asynchronous bulk transfers, which are left as future work.

Key points

  • A canonical score equation with query-relation, key-relation, and scalar-bias terms unifies Shaw, Transformer-XL, DeBERTa, and T5 relative-position attention. §3.2
  • Existing backends materialize or gather a dense N×N bias, incur unreachable columns, redundant fetches, and many-to-one atomic contention in backward. §3.3
  • The method evaluates relation terms on chip with at most three scalar lookups per attention logit instead of a d-dimensional dot product. §4.1
  • Offset-aware tiling uses tile edge offsets to detect saturated tiles and broadcast one relation entry per query and key. §4.3.1
  • The group size is set to 128, aligned with the maximum tile size, giving 1.6 percent column overhead at 8K tokens. §4.3.1
  • Atomic-free backward assigns each query tile to one thread block and accumulates gradients by offset, with a symmetric key-owned pass. §4.2
  • The Triton kernels are specialized at compile time to active relation terms, map type, masking, sequence packing, dropout, precision, and tile sizes. §4.3.3
  • Ablations show that atomic-free gradients and projection reuse carry most of the speedup, while retained versus recomputed projections trade memory for compute. §5.4

Mind map

Relative-position attention kernels
Problem
Pairwise offset terms
Dense bias and bandwidth costs
Atomic contention in backward
Pointwise backends lack tile reuse
Method
Offset-aware tiling
Reachable relation projections
On-chip scalar lookups
Atomic-free offset gradients
Two-pass backward
Triton drop-in kernels
Evaluation
Attention-level speed and memory
Speech transcription
Document classification
Ablations
Limitations
Identity maps remain quadratic
Nonconsecutive maps use atomics
No architecture-specific primitives