A Formal Framework for Understanding Length Generalization in Transformers

Xinting Huang, Andy Yang, Satwik Bhattamishra, Yash Sarrof, Andreas Krebs, Hattie Zhou, Preetum Nakkiran, Michael Hahn

Introduction

While Transformers are the backbone of modern AI, they exhibit a notorious "short-sightedness": a model trained on sequences of length 50 often fails spectacularly when asked to process sequences of length 100. This phenomenon, known as length generalization failure, is particularly puzzling because it is not universal. Transformers can learn to copy long strings easily, yet they struggle with simple algorithmic tasks like adding large numbers or determining the parity of a bitstring.

The core challenge lies in Absolute Positional Encodings (APE). Because these encodings are tied to specific indices (e.g., "this is the 5th token"), a model has no inherent way to interpret the "101st token" if its training data only spanned up to 50 tokens. Previous research offered empirical observations or restricted programming languages (like RASP-L) to explain this, but lacked a rigorous mathematical proof to predict success or failure across different tasks.

This paper introduces a formal mathematical framework called "Limit Transformers" to prove that length generalization is guaranteed for tasks expressible using only local and periodic positional information.

Methodology: Limit Transformers and C-RASP

The authors bridge the gap between theory and practice using several key innovations:

  1. Limit Transformers: Since a standard Transformer has a fixed width, it cannot distinguish between an infinite number of positions. The authors define a "Limit Transformer"—a mathematical abstraction representing the behavior of a sequence of Transformers as the input length grows to infinity.
  2. Idealized Inference Scheme: Instead of modeling the chaotic dynamics of Stochastic Gradient Descent (SGD), the authors analyze an idealized learner. This learner fits the training data while minimizing a norm-based regularizer that discourages "accidental" long-distance dependencies.
  3. C-RASP (Complex RASP): The researchers extend RASP, a programming language designed for Transformers, to include positional logic. They prove that if a task can be written in C-RASP using only LOCAL (looking at nearby neighbors) and PERIODIC (looking at repeating patterns) functions, it is guaranteed to length-generalize.
C-RASP Operations

To illustrate how these operations combine to solve sequence tasks, consider the following example program for a formal language:

Example C-RASP Program
  1. Communication Complexity: To prove why some tasks cannot generalize, the authors use communication theory. If computing the second half of a sequence requires the first half to transmit too much information, a fixed-width Transformer lacks the "bandwidth" to generalize.

Key Insights and Empirical Validation

The researchers validated their theory against two main suites of tasks: 8 algorithmic sequence-to-sequence problems (including Binary Majority, Sorting, Parity, and Addition) and a benchmark of 17 formal regular languages (including Tomita Grammars and Dyck-1).

  • The "Local/Periodic" Rule: The critical insight is that length generalization depends on how logic uses position. If a task requires knowing exactly how far a token is from the start (Absolute Position), it fails. If it only relies on nearby tokens or repeating patterns (Local/Periodic), it succeeds.
  • Predicting Success and Failure: The theoretical framework acted as a near-perfect predictor for empirical performance. Tasks like MAJORITY and DYCK-1 are definable in the C-RASP [periodic, local] framework and achieved near 100% accuracy on extended sequences. Conversely, the theory proved that PARITY and Addition are not expressible in this restricted framework; empirically, models trained on these tasks saw accuracy drop to near-random levels when sequence length doubled.
  • Communication as a Bottleneck: Tasks like Addition and Copying with repeated substrings have high communication complexity. Transformers must increase their depth or width to solve these as sequences lengthen, preventing generalization from a fixed-size training set.
  • NoPE vs. APE: For tasks requiring no positional information, even Transformers with No Positional Encodings (NoPE) can generalize perfectly.

Limitations and Future Work

While the framework provides strong theoretical guarantees, it assumes the model finds a global minimum under a specific regularizer. Real-world SGD might not always converge to these "clean" solutions. Furthermore, the study focuses on Absolute Positional Encodings (APE). Extending this framework to relative schemes like RoPE or ALiBi is a logical next step. Finally, while the theory guarantees generalization exists after a certain training length threshold, calculating exactly how much training data is "enough" for a specific task remains an open question.

Summary updated

BibTeX
@inproceedings{huang2025formal,
  author = {Xinting Huang and Andy Yang and Satwik Bhattamishra and Yash Sarrof and Andreas Krebs and Hattie Zhou and Preetum Nakkiran and Michael Hahn},
  title = {{A Formal Framework for Understanding Length Generalization in Transformers}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=U49N5V51rU},
}
Figure