A Length-Extrapolatable Transformer

Yutao Sun, Li Dong, Barun Patra, Shuming Ma, Shaohan Huang, Alon Benhaim, Vishrav Chaudhary, Xia Song, Furu Wei

Abstract

Standard Transformers struggle with length extrapolation, often failing when processing sequences longer than those seen during training. The LEX Transformer introduces a novel position embedding, XPOS, and a blockwise attention mechanism (BCA) to stabilize "attention resolution." Unlike traditional models, the LEX Transformer's performance actually improves as input sequences lengthen, even far beyond the training window. This paper demonstrates significant improvements in perplexity and long-context handling compared to existing methods like RoPE and Alibi.

The Challenge of Length Extrapolation

Standard Transformers are notoriously "short-sighted." A model trained on 1,024 tokens typically fails when asked to process 8,192 tokens. This is the length extrapolation problem.

The root cause lies in position tracking. Absolute position embeddings do not generalize to unseen indices. Relative position embeddings, such as RoPE (Rotary Position Embedding), perform better but suffer from dramatic oscillations at long distances. These oscillations degrade the model's attention resolution—the ability to distinguish between tokens at different positions. Maintaining high resolution is essential for successful extrapolation.

Methods: XPOS and BCA

The authors propose the Length-Extrapolatable (LEX) Transformer, which utilizes two primary innovations:

  1. Extrapolatable Position Embedding (XPOS): A generalized version of RoPE. While RoPE uses a rotation matrix to encode relative distance, XPOS adds an exponential decay factor. This decay suppresses unstable, oscillating dimensions at long distances, effectively dividing dependencies into stable short-term and long-term components.
  2. Blockwise Causal Attention (BCA): During inference, the model divides the query into blocks (e.g., half the training length). Each query block interacts with its own block and the immediately preceding one. This windowed approach maintains high attention resolution and is computationally efficient.

To guide their design, the authors formally defined Attention Resolution ($R(s)$) to quantify a model's ability to recognize and distinguish positions based on attention scores.

Key Insights: The Oscillation Trap

A critical finding is the visualization of RoPE’s attention expectation. At long distances, the signal becomes a noisy mess of zig-zags, making it difficult for the model to differentiate positions. XPOS smooths this signal, providing a stable "decaying" trend that the model can interpret.

Attention expectation of RoPE vs. xPos (XPOS). xPos provides a stable decaying signal compared to the oscillating noise of RoPE at long distances.

Extrapolation is not merely about memory capacity; it is about the mathematical ability to tell tokens apart. High resolution ensures that a token at distance 2,000 does not look identical to one at distance 2,001.

Key Results

The LEX Transformer significantly outperforms existing methods in extrapolation settings. While most models show a "U-shaped" perplexity curve—where performance improves slightly with length before exploding—LEX shows a continuous downward trend.

Perplexity vs. Validation Length. LEX (Ours) shows a continuous downward trend in perplexity as sequence length increases, while other methods like Transformer and Roformer explode or rise.
  • Perplexity on PG22: When trained on 1,024 tokens and evaluated on 8,192, the standard Transformer's perplexity exploded to 12,747.41. RoPE reached 458.83. LEX achieved a remarkable 24.89.
  • Interpolation vs. Extrapolation: LEX also achieves the lowest perplexity on short sequences (interpolation), showing that better position modeling benefits the model across all lengths.
  • Efficiency: XPOS only adds approximately 6% to inference time compared to absolute position embeddings, a small trade-off for the massive gain in capability.

Limitations and Future Work

The current research focuses on causal language modeling (decoder-only architectures). Integrating these methods into bidirectional attention (like BERT-style masked language modeling) remains a subject for future study. Additionally, while the 6% inference overhead is manageable, future work will explore scaling LEX to multi-billion parameter models to ensure these benefits hold at the frontier of Large Language Model (LLM) scale.

Summary updated

BibTeX
@inproceedings{sun2023length,
  author = {Yutao Sun and Li Dong and Barun Patra and Shuming Ma and Shaohan Huang and Alon Benhaim and Vishrav Chaudhary and Xia Song and Furu Wei},
  title = {{A Length-Extrapolatable Transformer}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  url = {https://openreview.net/forum?id=JtpBU-jsst},
}
Figure