Length Generalization for Transformers via Compression

Georg Zetzsche, Hongjian Jiang, Andy Yang, Pascal Bergsträßer, Marco Sälzer, David Chiang, Anthony Widjaja LinNeurIPS 2026

TL;DR

How do you know if a transformer trained on short strings will actually figure out the underlying rule and work on much longer ones? By borrowing a string-compression trick from computational group theory, this paper shows that the training examples needed to guarantee "length generalization" aren't massive, random sequences—they are structured strings that compress down to tiny, polynomial-sized representations. This insight slashes the theoretical cost of generating complete training sets by an exponential factor and solves a nagging paradox about why transformers fail on tasks theory says they should ace.

What Problem Does This Paper Address?

Imagine teaching a model addition using only 3-digit numbers, hoping it will seamlessly handle 50-digit numbers at test time. That’s length generalization.

To predict when transformers will pull this off, researchers rely on the C-RASP hypothesis: the idea that a transformer can length-generalize on a task if and only if that task can be written as a program in C-RASP (a formal logic language that mimics how finite-precision transformers count and attend to tokens). While empirically useful, this theory has two big headaches:

  1. Theory underestimates transformers: To guarantee learning, how much training data do we actually need to generate? For well-behaved subsets of C-RASP (specifically $\text{C-RASP}_+$, which uses only positive weights, and $\text{C-RASP}_1$, which doesn't nest counting operations), existing bounds on required string lengths are exponential. Naively generating a "complete sample"—a dataset guaranteed to distinguish the true underlying program from any impostor up to a certain size—would take doubly exponential time. Yet in practice, transformers learn these languages much faster.
  2. Theory overestimates transformers: Transformers famously cannot length-generalize on PARITY (checking if a string has an even number of a's), which aligns with theory since C-RASP can't express it. However, bounded parity (PARITY$_k$, checking for an even number of a's, up to at most $2k$ of them) is expressible in C-RASP. Theory says transformers should nail it; experiments show they flop, even for small $k$ like $25$.

Methods

The authors tackle the sample enumeration problem: given a maximum program size $c$, output a complete set of training strings that uniquely identifies any $\text{C-RASP}_+$ or $\text{C-RASP}_1$ program of that size.

What’s novel is their bridge to power words—a string compression scheme previously confined mostly to computational group theory. A power word is like run-length encoding on steroids: it lets you repeat entire substrings using binary exponents. For example, $(ab^6)^4$ neatly compresses a 28-character string.

To prove that short power words suffice for learning, the authors build a three-step theoretical pipeline:

  1. Logic to Automata: They translate $\text{C-RASP}_+$ and $\text{C-RASP}_1$ formulas into reversal-bounded counter automata—state machines with counters that can only switch between incrementing and decrementing a bounded number of times.
  2. Automata to $\mathbb{Z}$-VASS: Using standard reductions, they convert these machines into integer Vector Addition Systems with States ($\mathbb{Z}$-VASS), where transitions simply add integer vectors to counters that must start and end at zero.
  3. Cycle Pruning: Taking an accepting run on a massive string, they strip out repeating state cycles and use classic bounds on integer linear equations ($Ax = b$) to show you only need to re-insert a polynomial number of cycles, repeated at most exponentially many times. Because binary exponents write exponential numbers in polynomial space, the resulting string is a polynomial-sized power word!

Thus, their sample enumeration algorithm is dead simple: enumerate all power words up to a polynomial length and decompress them.

Datasets

The authors evaluate neural models on three synthetic formal-language tasks over characters a and b (for $k \in \{2, 4, 8, 16, 32, 64\}$), using 10,000 training samples per task and evaluating on held-out strings of length 301–450:

  • COUNT$_k$: Checks if the number of a's exceeds $k$.
  • MAJ$_k$: Checks if a's outnumber b's, with at most $k$ occurrences of b.
  • PARITY$_k$: Checks if the number of a's is even and at most $2k$.

Key Insights and Ideas

  • Long strings, tiny cores: Even when distinguishing two transformer programs requires an exponentially long input string, that string isn't complex—it is highly repetitive and compresses into a polynomial-length power word.
  • Resolving the PARITY$_k$ paradox: Why do transformers fail on PARITY$_k$ if it's in C-RASP? It comes down to program size. Writing COUNT$_k$ or MAJ$_k$ in C-RASP takes $O(\log k)$ space because $k$ is encoded in binary, yielding a compact quasi-polynomial complete sample size. PARITY$_k$, however, essentially forces you to write out "$0$ a's OR $2$ a's OR ... $2k$ a's"—a formula of size $O(k)$ (which the authors prove cannot be compressed to $O(\log k)$). Plugging $O(k)$ into their bound yields an exponential sample requirement, explaining why bounded parity is dramatically harder to learn.
  • A recipe for synthetic data: Instead of uniformly sampling strings, randomly sampling short power words and decompressing them could be a principled way to generate length-generalization training data.

Key Results

  • Exponential Theoretical Speedup: Sample enumeration for $\text{C-RASP}_+$ and $\text{C-RASP}_1$ drops from doubly exponential time and exponential space to exponential time and polynomial space—which the authors prove is time-optimal. (As a bonus, checking if a $\text{C-RASP}_+$ formula accepts any string at all is shown to be NP-complete.)
  • Empirical Validation: Across 30,000 training steps, held-out accuracy on COUNT$_k$ stayed strong at 70.52% to 95.12%. Meanwhile, PARITY$_k$ degraded as $k$ grew from $2$ to $64$ (82.62% at $k=2$ down to 61.98% at $k=32$ and 65.75% at $k=64$).
  • Hidden Subgroup Failures: Overall accuracy often masked total algorithmic failure. On PARITY$_{16}$, overall accuracy looked like 71.08%, but balanced even–odd accuracy was basically a coin flip (48.95%). Similarly, MAJ$_{64}$ hit 84.83% overall accuracy, yet 100% of tested strings just over the boundary (65 or 66 b's, with more a's than b's) were wrongly accepted.

Limitations and Future Work

  • Fragment boundaries: The theoretical bounds apply strictly to $\text{C-RASP}_+$ and $\text{C-RASP}_1$. Some simple languages outside these fragments—like $(ab)^*$—are definable in full C-RASP and easily length-generalize in practice. Finding a broader C-RASP fragment that unifies these while keeping polynomial power-word bounds is an open goal.
  • Theory vs. SGD: The bounds measure how many examples are needed to uniquely pin down a program in principle, not how many gradient steps a neural network needs to find it. Furthermore, because their test sets varied both string length and word order, the experiments qualitatively support the theory without strictly isolating pure length extrapolation.

Summary updated

Abstract

Recent advancements in transformer length generalization theory enable us to reliably predict when a transformer can learn to solve a task. In particular, the C-RASP hypothesis (a formalized version of the so-called RASP-l conjecture) posits that transformers length-generalize on a task if and only if a solution is expressible in the C-RASP language. While this hypothesis has strong empirical validation, it faces a theoretical problem in the fact that C-RASP admits no computable bound on length complexity (that is, the length of strings required to learn a language perfectly), and an empirical problem in the form of experimental results that seem to contradict it. To address these problems, we refine the C-RASP hypothesis using the fragments C-RASP+ and C-RASP1. Recent work has shown that these fragments have length-complexity bounds that are computable, but in the worst case exponential. In this paper, we study the problem of sample enumeration: computing the training data that are required for length generalization. That is, given a bound on the permitted C-RASP+ or C-RASP1 program size (not a particular program), to compute a complete sample for learning. Previous results implied a double exponential time upper bound for the complexity of sample enumeration. In this paper, we show a tight polynomial space and exponential time upper bound. Our algorithm is in fact a simple enumeration of short (polynomially sized) compressed strings, in terms of power words. As an application, we show how this yields a fine-grained analysis of the C-RASP conjecture that provides a possible explanation for the apparent contradiction with experimental evidence.
BibTeX
@inproceedings{zetzsche2026length,
  author = {Georg Zetzsche and Hongjian Jiang and Andy Yang and Pascal Bergsträßer and Marco Sälzer and David Chiang and Anthony Widjaja Lin},
  title = {{Length Generalization for Transformers via Compression}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=gx6QOo6yY5},
}
Figure