NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Length Generalization Bounds for Transformers

Andy Yang, Pascal Bergsträßer, Georg Zetzsche, David Chiang, Anthony W. Lin

TL;DR

This paper delivers a definitive theoretical blow to the quest for universal length generalization guarantees in transformers, proving that computing a general length bound beyond which a model is guaranteed to generalize is mathematically impossible (undecidable) for any transformer with two or more layers. However, for real-world fixed-precision transformers, the authors show that length generalization bounds are computable, though they scale exponentially with the model's size.

What Problem Does This Paper Address?

Transformers often struggle to process inputs longer than those seen during training—a challenge known as the length generalization problem. While empirical scaling laws help engineers optimize model and dataset sizes to minimize test loss, they completely fail to predict whether a model will successfully generalize to longer sequences.

This paper tackles the open question of non-asymptotic length generalization: Can we compute a threshold length $N$ such that if a learning algorithm trains perfectly on strings up to length $N$, the resulting model is guaranteed to generalize to strings of any length? To answer this, the authors analyze C-RASP (Counting RASP), a formal programming language designed to mirror the inner workings and expressivity of transformers.

Methods

Instead of running empirical training loops, the authors use formal language theory and mathematical logic:

  1. C-RASP as a Transformer Proxy: The authors leverage the depth-preserving equivalence between C-RASP programs and transformers to draw conclusions about neural networks by analyzing code.
  2. Reduction from Hilbert's 10th Problem: To test if length bounds are computable, the authors look at the language emptiness problem (deciding if a program accepts any strings at all). They prove that a two-layer C-RASP program can simulate multiplication and encode Diophantine equations. Because solving these equations is famously undecidable (Hilbert's 10th Problem), computing a general length generalization bound is also impossible.
  3. Temporal Logic Mapping: For the positive fragment of C-RASP ($C-RASP_+$)—which corresponds to fixed-precision transformers—the authors map the language to Unary Temporal Logic ($TL[\diamondsuit^-]$) containing only past operators. They use the properties of this logic to derive a computable upper bound.

Datasets

As a purely theoretical paper, no empirical datasets or training benchmarks were used. The proofs rely on formal language classes, such as the Dyck language (balanced parentheses) and custom languages designed to simulate mathematical operations.

Key Insights and Ideas

  • The Depth-2 Wall: Length generalization bounds become completely uncomputable the moment a transformer uses two or more layers. At this depth, the network gains enough expressive power to cross the threshold into undecidability.
  • Fixed Precision Saves Computability: Real-world hardware operates on fixed precision (e.g., floating-point math). By restricting the analysis to fixed-precision transformers, the length generalization problem becomes decidable.
  • The Exponential Catch: Even though fixed-precision bounds are computable, they are exponential in the size of the program. This explains why length generalization is so elusive in practice: a model might need to see unfeasibly long sequences during training before perfect generalization is mathematically guaranteed.

Key Results

  • Universal Uncomputability (Theorem 1.1): There is no terminating algorithm that can compute a length generalization bound for general transformers of depth two or beyond.
  • The Fixed-Precision Bound (Theorem 1.2): For fixed-precision transformers, a perfect length generalization bound exists but is strictly exponential. To guarantee generalization, the model must be exposed to at least one string of exponential length relative to its size.
  • Dyck Language Expressivity: The authors prove that bounded-depth Dyck languages cannot be expressed by more restricted versions of C-RASP used in prior literature, demonstrating that their uncomputability results apply to a much broader and more realistic class of transformers.

The paper illustrates how a standard C-RASP program can easily express these structured languages:

C-RASP program recognizing a depth-4 Dyck language

Limitations and Future Work

  • Worst-Case vs. Average-Case: The exponential bound proven for fixed-precision transformers represents a theoretical worst-case scenario. The authors note that the actual sample size (the number of distinct training strings required) might still be polynomial, leaving sample complexity as an open question.
  • Alternative Fragments: A key direction for future work is identifying other specific subsets of C-RASP or alternative transformer architectures that might admit tighter, polynomial length generalization bounds.
  • Idealized Learning: This framework assumes a "best-case" learner that optimizes perfectly over all possible short examples. Incorporating actual gradient descent dynamics (like SGD on limited data) remains a difficult challenge for future theoretical analysis.

Summary updated

BibTeX
@inproceedings{yang2026length,
  author = {Andy Yang and Pascal Bergsträßer and Georg Zetzsche and David Chiang and Anthony W. Lin},
  title = {{Length Generalization Bounds for Transformers}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=XNXU8BJRT7},
}
Figure