Abstract
Standard Transformer architectures frequently struggle with length generalisation, failing to maintain performance when processing sequences significantly longer than those encountered during training. This failure stems from a fundamental decoupling of semantic relevance and positional bias within the attention mechanism. We present Threshold Relative Attention (TRA), a novel approach that introduces selective sparsity and contextualised relative distances. By pruning irrelevant information and calculating distances based only on semantically significant tokens, TRA enables models to generalise to sequences up to 32x longer than their training window, achieving state-of-the-art results on both synthetic benchmarks and natural language tasks.
1. Introduction
Despite the transformative impact of the Transformer architecture, its ability to handle long-range dependencies remains constrained by its training context. A model trained on 512 tokens often suffers catastrophic performance degradation when evaluated on 2,000 tokens. This "length generalisation" gap is a critical bottleneck for applications requiring long-context reasoning.
The authors identify a core conflict in standard attention: the mechanism treats "what is relevant" (semantic content) and "where it is" (positional bias) as independent factors. Current positional encodings, such as RoPE or ALiBi, apply a "content-blind" bias. In practice, this means a nearby but irrelevant token may receive higher attention scores simply due to its physical proximity to the query. As sequence lengths increase, this accumulated noise leads to hallucinations and reasoning failures.
2. Threshold Relative Attention (TRA)
To resolve this conflict, the paper introduces Threshold Relative Attention (TRA), which refactors the attention mechanism into a three-stage process that makes positional information contingent on semantic content.
2.1 Selective Sparsity
Instead of the standard dense attention where every token receives a non-zero weight, TRA applies a simple ReLU-based threshold to the raw attention scores. If a key's semantic relevance to the query does not meet a specific threshold, it is zeroed out. This hard-masking prevents irrelevant keys from contributing noise to the positional calculations.
2.2 Contextualised Relative Distance
In standard models, distance is measured in absolute steps (e.g., $i - j$). TRA introduces a "contextual" distance metric. It measures distance based on the number of relevant tokens between a Query and a Key. For example, if 50 tokens exist between two points but only 2 pass the semantic threshold, TRA treats the distance as 2. This ensures the model's "geometry" is defined by the content of the sequence rather than its raw length.
2.3 Parameterised Forget Gate
These contextual distances are processed through a learned, sigmoid-based forget gate ($\delta$). This allows the model to dynamically adjust its focus, learning whether a specific head should maintain a sharp recency bias (focusing on the most recent relevant information) or a broad focus (treating all relevant information equally).
3. Experimental Evaluation
The researchers evaluated TRA against several competitive baselines, including RoPE, ALiBi (FoT), and CoPE, across both synthetic and natural language datasets.
- Flip-Flop Language Modelling: A state-tracking task requiring the model to maintain a read/write/ignore state over long distances.
- Flip-Flops++: A more rigorous synthetic benchmark requiring complex instruction following, such as identifying symbols relative to specific triggers.
- Induction Heads & Copy Tasks: Standard benchmarks for associative memory.
- WikiText-103: A natural language dataset used to evaluate the model's perplexity and generalisation in real-world scenarios.
4. Key Insights and Results
4.1 Unprecedented Length Scaling
TRA demonstrated remarkable out-of-distribution (OOD) scaling. On WikiText-103, models trained on a window of only 128 tokens maintained or improved their perplexity when evaluated on sequences up to 4,096 tokens—a 32x increase. This significantly outperformed existing positional encoding schemes which typically degrade rapidly beyond 2x or 4x the training length.
4.2 Solving Synthetic Stress Tests
TRA is the first Transformer-based architecture to achieve 100% accuracy on the Flip-Flop benchmark across all seeds. Historically, this level of robust state-tracking was only achievable by Recurrent Neural Networks like LSTMs. The success here highlights TRA's ability to filter out the "noise" of long sequences that typically confuses attention-based models.
4.3 The Role of Sparsity in Reasoning
A significant finding of the paper is that selective sparsity is not merely a computational optimization but a prerequisite for robust reasoning. By masking irrelevant keys, the model prevents OOD positional noise from corrupting the attention signal, allowing the semantic content to dictate the attention pattern.
5. Limitations and Future Work
While TRA offers superior generalisation, it currently faces a trade-off in computational efficiency. In its current implementation, training a "Medium" sized model with TRA took approximately 22 minutes longer than a standard RoPE baseline (178 minutes vs 156 minutes). This is largely due to the lack of optimized CUDA kernels, such as a TRA-specific version of FlashAttention.
Furthermore, the current reliance on a cumsum operation for distance calculation makes the mechanism inherently causal, limiting its immediate application to decoder-only models. Future research may explore adapting these relevancy gates for bidirectional encoder models and refining the thresholding mechanism beyond a simple ReLU.
6. Conclusion
Threshold Relative Attention represents a significant step toward solving the length generalisation problem in Transformers. By making positional bias dependent on semantic relevance through selective sparsity and contextual distances, TRA allows models to maintain high-fidelity reasoning across vastly extended contexts. This synergy between content and position provides a more robust foundation for the next generation of long-context large language models.
Summary updated