Abstract
Transformers struggle with length generalisation, displaying poor performance even on basic tasks. We test whether these limitations can be explained through two key failures of the self-attention mechanism. The first is the inability to fully remove irrelevant information. The second is tied to position, even if the dot product between a key and query is highly negative (i.e. an irrelevant key) learned positional biases may unintentionally up-weight such information - dangerous when distances become out of distribution. Put together, these two failure cases lead to compounding generalisation difficulties. We test whether they can be mitigated through the combination of a) selective sparsity - completely removing irrelevant keys from the attention softmax and b) contextualised relative distance - distance is only considered as between the query and the keys that matter. We show how refactoring the attention mechanism with these two mitigations in place can substantially improve generalisation capabilities of decoder only transformers.
Introduction: The Challenge of Length Generalization
Decoder-only Transformers are notoriously brittle when processing sequences longer than those encountered during training. A model trained on 512 tokens often fails catastrophically when asked to handle 2,000. This research identifies a fundamental conflict in standard attention: semantic content and positional information are treated as independent, leading to two specific failure modes.
First, the standard softmax function never truly zeroes out irrelevant information; it merely assigns it a low weight. Second, learned positional biases (e.g., a preference for recent tokens) can accidentally up-weight this irrelevant information when token distances become "out-of-distribution" (OOD). Threshold Relative Attention (TRA) addresses these issues by combining selective sparsity with "contextualized" relative distances.
The TRA Mechanism
TRA is a modular addition to the attention mechanism that operates through two primary steps:
1. Selective Sparsity
Instead of allowing every key-query pair to influence the output, TRA applies a ReLU threshold to the raw semantic attention scores. Any key that fails to meet this threshold is completely masked out (set to $-\infty$ before the final softmax). This forces the model to perform a "hard" selection, effectively filtering out noise that would otherwise accumulate in long sequences.
2. Contextualized Relative Distance
Standard relative position encodings treat distance as a fixed physical gap. TRA instead calculates distance only between the keys that passed the threshold. For example, if a query seeks a specific command and is separated from it by 100 irrelevant tokens, TRA treats that command as being at distance 1, effectively "skipping" the noise. This distance is then transformed into weights using a parameterized forget gate, allowing the model to learn whether a specific head should prioritize recency or treat all relevant tokens equally.
Experimental Evaluation
The researchers evaluated TRA against a variety of synthetic and real-world benchmarks:
- Flip-Flop Language Modelling (FFLM): Requires tracking "write," "ignore," and "read" instructions.
- Flip-Flops++: A challenging new benchmark requiring retrieval based on first/last occurrences of triggers.
- Induction Heads & Copying: Tests of associative memory.
- WikiText-103: A standard natural language dataset to test real-world English text performance.
Key Results and Visual Proof
TRA achieved 100% accuracy on the Flip-Flop benchmark across all OOD settings, a feat where baselines like RoPE and APE failed significantly as sequence lengths increased. On WikiText-103, models trained on a window of 128 tokens were successfully tested on up to 4,096 tokens (a 32x extrapolation). TRA not only remained stable but showed improved perplexity as the context grew, indicating successful utilization of long-range information.
Attention heatmaps provide visual confirmation of TRA's efficacy. While standard models "smear" attention across irrelevant tokens in long sequences, TRA maintains a sharp, laser-like focus on the specific tokens required to solve the task, as shown in the figure above.
Discussion and Future Work
While TRA offers significant generalization benefits, it introduces a slight computational overhead (approximately 10-15% in training time) due to the "cumsum" operation used for contextual distances. Future work may involve integrating TRA into optimized kernels like FlashAttention to mitigate this penalty. Additionally, while currently tested on models up to 80M parameters, the next frontier is observing TRA's behavior in multi-billion parameter Large Language Models (LLMs).
Summary updated