Introduction
Standard Transformers are notoriously bad at "length generalization." If you train a model to add 20-digit numbers, it will likely fail miserably when asked to add 100-digit numbers. This suggests that instead of learning the underlying algorithm, the model is finding shortcuts or memorizing patterns specific to that length.
While researchers have found "tricks" to fix this for specific tasks (like adding "index hints" for addition), these solutions rarely work universally across other problems like multiplication or optimization. The fundamental challenge is finding a universal way to represent algorithmic reasoning that allows a Transformer to extrapolate to any length, regardless of the task.
To address this, researchers have developed Turing Programs, a universal scratchpad format that allows Transformers to generalize to much longer sequences than they saw during training. By mimicking the step-by-step tape updates of a Turing Machine and using specific positional encodings, models can learn the logic of an algorithm rather than just memorizing patterns.
Methods: Simulating a Turing Machine
The authors introduce a novel Chain-of-Thought (CoT) strategy called Turing Programs. The core idea is to decompose any algorithmic task into a series of steps that mimic a Turing Machine (TM).
- The Representation: Each step in the scratchpad is a "tape" representing the current state of the computation. To get to the next step, the model simply copies the previous tape while making a few local, elementary changes (like changing a single digit or moving a "head" pointer).
- The Architecture: The authors pair this format with Hard-ALiBi positional encoding. Unlike standard encodings (like RoPE), Hard-ALiBi forces attention heads to look only at a local window of tokens.
- The Logic: Because a Turing Machine only makes local changes to a tape, the task of "simulating" one becomes a sequence of modified copy operations. Since Hard-ALiBi is exceptionally good at length-generalized copying, the model can successfully "copy-and-edit" its way through an algorithm of any length.
The paper also includes a theoretical component, proving that Transformers can implement these Turing Programs using RASP (Restricted Access Sequence Processing), a specialized programming language designed to describe Transformer operations.
Datasets and Key Insights
The study relies on several synthetic algorithmic datasets designed to test extrapolation, including multi-digit addition (training on up to 50 digits, testing on 100), multiplication, in-context SGD, arithmetic sequences, and random Turing Machines.
A key insight from this approach is that copying is the primitive of reasoning. Complex algorithmic reasoning can be reduced to a "modified copy" task. If a model can generalize the ability to copy text with minor edits, it can generalize any algorithm. Furthermore, because any computable function can be represented by a Turing Machine, Turing Programs provide a blueprint for solving any algorithmic task with length generalization, moving away from task-specific hacks. By using Hard-ALiBi to restrict the model's "vision" to a local window, the authors prevent the model from relying on global shortcuts that break when the sequence length changes.
Key Results
The combination of Turing Programs and Hard-ALiBi yielded impressive extrapolation results across the board:
- Addition: Achieved 98% accuracy on 100-digit numbers after being trained only on 50-digit numbers (a 2x length increase).
- Multiplication: Maintained ~97% accuracy when generalizing from 50 to 100 digits.
- SGD: Generalizing from 50 to 80 examples resulted in 95% accuracy, significantly outperforming models that tried to output the answer directly.
- Universality Test: On 10 different random Turing Machines, the models achieved nearly 100% accuracy on tapes 2x to 3x longer than those seen in training.
Limitations and Future Work
Despite the success, the authors note several hurdles. First, Turing Programs are "verbose." Simulating a TM requires many intermediate steps, leading to very long sequences that consume significant compute and memory. Second, while accuracy was high, a single mistake in one "tape update" can cause the entire program to fail; in extremely long sequences, these small probabilities of error eventually stack up. Finally, while this works for clean, algorithmic tasks, it remains to be seen how this can be integrated into general-purpose LLMs for "messy" real-world reasoning without making the output unreadably long for humans.
Future work will likely focus on making these programs more efficient—perhaps by compressing multiple TM steps into one—and exploring if models can learn to generate these Turing-style scratchpads without being explicitly trained on them.
Summary updated