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:
- 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.
- Theory overestimates transformers: Transformers famously cannot length-generalize on
PARITY(checking if a string has an even number ofa's), which aligns with theory since C-RASP can't express it. However, bounded parity (PARITY$_k$, checking for an even number ofa'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:
- 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.
- 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.
- 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 ofa's exceeds $k$.MAJ$_k$: Checks ifa's outnumberb's, with at most $k$ occurrences ofb.PARITY$_k$: Checks if the number ofa'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 onPARITY$_k$ if it's in C-RASP? It comes down to program size. WritingCOUNT$_k$ orMAJ$_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 66b's, with morea's thanb'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