Length Generalization for Transformers via Compression

Georg Zetzsche, Hongjian Jiang, Andy Yang, Pascal Bergsträßer, Marco Sälzer, David Chiang, Anthony W. LinarXiv 2026

TL;DR

This paper provides an exponentially tighter sample size bound for transformer length generalization by establishing a novel connection to string compression via "power words." By reducing the theoretical sample size bound from doubly exponential to single exponential, the authors resolve a long-standing paradox explaining why transformers easily learn certain counting tasks but fail on bounded parity tasks.

What Problem Does This Paper Address?

Transformers frequently struggle with length generalization—the ability to correctly process sequences longer than those seen during training. The C-RASP hypothesis (a formalized framework based on the RASP programming language) posits that a transformer can length-generalize on a task if and only if the solution can be expressed in the C-RASP language.

While empirically successful, this theory suffered from two major flaws:

  1. Impractical Bounds: Previous length generalization bounds for key fragments of C-RASP ($C\text{-RASP}_+$ and $C\text{-RASP}_1$) were doubly exponential with respect to the formula size, leaving it unclear if transformers could realistically generalize within reasonable training limits.
  2. The Parity Paradox: Certain tasks, like $\text{PARITY}_k$ (checking if a string has an even number of 'a's up to a maximum of $k$), are theoretically expressible in C-RASP. According to the old theory, transformers should length-generalize on them, but empirical experiments show they consistently fail even for moderate values of $k$ (e.g., $k=50$).

This paper aims to tighten these theoretical bounds and resolve the contradiction between theory and practice.

Methods

The authors introduce a novel technique that analyzes transformer expressivity through the lens of string compression, specifically using power words. Power words are a run-length encoding variant where repeating factors are written compactly (e.g., representing $a^{2^n}$ using $O(n)$ bits).

The proof proceeds in three main steps:

  1. Automata Translation: They show that any formula in $C\text{-RASP}_+$ or $C\text{-RASP}_1$ can be translated into a reversal-bounded counter automaton (an automaton where counters can change direction from incrementing to decrementing only a bounded number of times).
  2. Integer VASS Mapping: These counter automata are then mapped to Vector Addition Systems with States over integers ($\mathbb{Z}\text{-VASS}$).
  3. Applying ILP Bounds: By leveraging classic bounds from integer linear programming (specifically Eisenbrand & Shmonin's results on integer cones), the authors prove that if a C-RASP formula is satisfiable, it can be satisfied by a short compressed power word of polynomial length.

This polynomial bound in the compressed space directly translates to a single-exponential sample size bound in the uncompressed space.

Datasets

The authors validated their theoretical insights using synthetic datasets across three language-recognition tasks over the alphabet $\{a, b\}$:

  • $\text{COUNT}_k$: Strings where the number of 'a's is strictly greater than $k$.
  • $\text{MAJ}_k$: Strings where the number of 'a's is greater than $k$ and greater than the number of 'b's.
  • $\text{PARITY}_k$: Strings where the number of 'a's is at most $2k$ and is an even number.

Models were trained on shorter sequence lengths and evaluated on two out-of-distribution (OOD) test sets containing significantly longer sequences.

Key Insights and Ideas

  • Compression Simplifies Generalization Theory: The "aha moment" of the paper is realizing that length generalization bounds become much tighter and more manageable when analyzed via compressed string representations rather than raw sequence lengths.
  • Formula Compactness Dictates Trainability: The paper resolves the Parity Paradox by showing that the size of the underlying C-RASP formula determines how many samples a transformer needs to see to generalize.
  • $\text{COUNT}_k$ and $\text{MAJ}_k$ can be expressed compactly with formulas of size $O(\log k)$, leading to a polynomial sample size requirement.
  • $\text{PARITY}_k$ requires a formula of size $O(k)$, demanding an exponential sample size. This explains why transformers fail empirically on parity—they would require an astronomically large training set to learn to generalize.

Key Results

  • Tighter Bounds: The paper successfully proves that the compressed length complexity for $C\text{-RASP}_+$ and $C\text{-RASP}_1$ is polynomial, which guarantees an exponential (rather than doubly exponential) sample size bound for length generalization.
  • Empirical Confirmation: Experiments using a 4-layer transformer perfectly mirrored the fine-grained theoretical predictions:
  • For $\text{COUNT}_k$ and $\text{MAJ}_k$, the transformer achieved near-perfect OOD generalization accuracy ($\ge 93.8\%$ and $\ge 96.8\%$, respectively) even up to $k=64$.
  • For $\text{PARITY}_k$, generalization performance degraded sharply as $k$ scaled, dropping down to $74.2\%$ accuracy at $k=64$.

Limitations and Future Work

The primary limitation is that these tight polynomial bounds currently apply only to specific fragments of C-RASP ($C\text{-RASP}_1$ and $C\text{-RASP}_+$). There are other length-generalizable languages (such as $(ab)^*$) that are expressible in full C-RASP but fall outside these fragments. The authors note that finding other fragments of C-RASP that permit reasonable sample size bounds remains an open direction for future work.

Summary updated

BibTeX
@misc{zetzsche2026length,
  author = {Georg Zetzsche and Hongjian Jiang and Andy Yang and Pascal Bergsträßer and Marco Sälzer and David Chiang and Anthony W. Lin},
  title = {{Length Generalization for Transformers via Compression}},
  year = {2026},
  eprint = {2609.08851},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.08851},
}
Figure