The Length Generalization Gap
Standard Transformers suffer from a "length generalization" gap: they perform well on sequences of the length they were trained on (e.g., 2,048 tokens), but their accuracy collapses when faced with longer inputs (e.g., 8,192 tokens). This happens because the Position Encodings (PE) used to tell the model the order of words often fail to handle "out-of-distribution" distances.
While methods like Alibi or T5’s Relative Position Encoding (RPE) help, they often come with rigid inductive biases—such as forcing the model to only look at nearby words—or are computationally inefficient on modern hardware. The challenge is creating a position encoding that is both flexible (learnable from data) and robust enough to handle arbitrary lengths.
To address this, researchers introduce FIRE (Functional Interpolation for Relative Positional Encoding), a novel approach that allows Transformers to generalize to sequence lengths far beyond what they saw during training. By using a learnable MLP combined with a "progressive interpolation" technique, FIRE enables models to maintain high performance on long contexts without sacrificing speed or short-context accuracy.
How FIRE Works
FIRE stands out through three key components:
- Functional Approach: Instead of a fixed lookup table for position biases, FIRE uses a small Multi-Layer Perceptron (MLP). This allows the model to learn complex, task-specific positional patterns rather than relying on hard-coded mathematical functions.
- Progressive Interpolation: This is the core innovation. To prevent the MLP from seeing distances larger than those encountered during training, FIRE normalizes the relative distance between a query and a key by the query's own position index. This maps all relative distances into a bounded range of $[0, 1]$. As the sequence grows, the model simply sees a "finer grid" within that same $[0, 1]$ range, effectively interpolating the learned function.
- Thresholding and Log Transforms: To ensure the model doesn't lose "absolute" distance information for short sequences, the authors introduce a learnable threshold $L$. For positions smaller than $L$, the normalization is fixed, preserving exact distance. They also apply a log transformation to the distances to give the model more "resolution" for tokens that are close to each other.
Key Insights
Unification of Existing Methods: The authors theoretically prove that FIRE is a "universal" relative encoding. Because it uses a learnable MLP, it can theoretically represent other popular methods like Alibi, T5 RPE, and Kerple.
Breaking the Locality Bias: Many long-context methods force the model to focus on nearby tokens (locality bias). Visualization of FIRE shows that it learns both local patterns and "anti-local" patterns—where some attention heads specifically learn to look at very distant tokens.
Hardware Efficiency: Unlike T5’s RPE, which involves slow vector operations, FIRE relies on MLPs (matrix multiplications), making it much more "accelerator-friendly" and faster during inference on GPUs and TPUs.
Experimental Results
The researchers evaluated FIRE across a diverse set of benchmarks, including language modeling (C4, arXiv, Github), long text benchmarks (SCROLLS, NarrativeQA), and short context benchmarks (GLUE, SuperGLUE).
- Superior Generalization: When trained on a sequence length of 2,048 and tested on up to 8,192, FIRE consistently maintained low perplexity. On the C4 dataset, it outperformed the best baseline (Kerple) by 2.28 perplexity points.
- State-of-the-Art on SCROLLS: FIRE achieved the highest average score across the SCROLLS benchmark, outperforming RoPE, Alibi, and T5 RPE on both base (125M) and large (350M) model sizes.
- Zero-Shot QA: On NarrativeQA, FIRE maintained stable performance up to 32,768 tokens, whereas RoPE-based models saw their scores plummet after exceeding the training length.
- Inference Speed: A variant called FIRE-S (which shares the MLP weights across all layers) was found to be faster than all baselines except for models with no position encoding at all, providing a great speed-accuracy trade-off.
Limitations and Future Work
The authors acknowledge a few areas for further exploration. First, this study focused exclusively on decoder-only models (like GPT); it remains to be seen how FIRE performs in encoder-decoder architectures. Second, while the paper focuses strictly on positional encoding, other factors like training data distribution and optimizer settings also play a role in length generalization. Finally, testing FIRE on "frontier" scale models (7B+ parameters) would further validate its robustness.
Summary updated