Arithmetic Transformers Can Length-Generalize in Both Operand Length and Count

Hanseul Cho, Jaeyoung Cha, Srinadh Bhojanapalli, Chulhee Yun

Overview

Transformers typically fail to generalize to sequences longer than their training data, especially in complex arithmetic tasks. This paper solves this limitation by combining task-specific "scratchpads" (to spell out intermediate steps) with multi-level "Position Coupling" (to explicitly tell the model which digits align). The result is a massive 2–3x length generalization leap, allowing models trained on short, simple math problems to solve much longer and more complex ones with high accuracy.

The Challenge of Length Generalization

Large Language Models (LLMs) notoriously struggle with length generalization—the ability to perform well on input sequences that are longer than those seen during training. If a model only sees 5-digit addition during training, it usually fails catastrophically when asked to add 15-digit numbers.

While researchers use arithmetic as a standard testbed for this problem, certain variations are considered exceptionally difficult. For example, multi-operand addition requires the model to generalize over both the length of the numbers (e.g., 10 digits vs. 30 digits) AND the count of the numbers (e.g., adding 5 numbers together vs. adding 30 numbers together). Similarly, multiplication requires generalizing over the lengths of both operands simultaneously. Prior to this paper, no arithmetic Transformer had successfully achieved length generalization across both of these dimensions at the same time.

Proposed Method: Scratchpads and Position Coupling

The authors achieve this breakthrough by combining two existing techniques in a highly complementary, novel way:

  1. Task-Specific Scratchpads: Instead of forcing the model to predict the final answer directly, the authors format the training data to include intermediate reasoning steps (a scratchpad).
  2. For addition, the scratchpad keeps a running cumulative sum.
  3. For multiplication, they introduce a novel two-stage scratchpad: first, it computes $M$-digit $\times$ $1$-digit multiplications, and second, it performs a shifted multi-operand addition (mimicking human long multiplication).
  4. Multi-Level Position Coupling: Standard position embeddings just count tokens sequentially (1, 2, 3...). Position Coupling instead assigns the identical position ID to tokens that share structural symmetry—for example, assigning the same ID to all the "tens" digits across different numbers. The authors extend this to multi-level Position Coupling:
  5. Level 1 aligns the digit significance (ones, tens, hundreds).
  6. Level 2 distinguishes the different numbers/operands from one another.
  7. Level 3 (used in multiplication) handles the leftward shifts required when adding up the intermediate products.

Theoretical Proof: The authors mathematically prove that a tiny 1-layer, 4-head Transformer equipped with this specific scratchpad and bi-level Position Coupling is highly expressive enough to solve multi-operand addition for exponentially long and exponentially numerous operands.

Experimental Setup

The authors generated synthetic datasets for their experiments:

  • Addition ($S_A$): Problems with up to $n$ digits and $m$ operands. The baseline training set was $S_A(10, 10)$ (up to 10 digits, up to 10 operands). Evaluation was done on a grid up to 30 digits and 30 operands.
  • Multiplication ($S_M$): Problems where the first operand has up to $n$ digits and the second has up to $m$ digits. Baseline training was $S_M(10, 10)$. Evaluation was done on a grid up to 30x30 digits.
  • Parity Task: A standard algorithmic warm-up task used to test length generalization on binary sequences.

Key Insights

  • Synergy is the secret sauce: Scratchpads alone or Position Coupling alone fail on these extreme generalization tasks. Together, they simplify the problem beautifully. The scratchpad ensures the model only ever needs to look at a constant number of tokens at any given step (e.g., just the current digit and the running sum digit), while Position Coupling tells the model exactly where those specific tokens are, regardless of how long the sequence has grown.
  • Attention patterns confirm the theory: When the authors visualized the attention heads of their trained models, the patterns perfectly matched their theoretical 1-layer construction. The model learned to look exactly at the current query digit and the corresponding scratchpad digit, completely ignoring the noise of the growing sequence length.
  • Multiplication can be decomposed: By forcing the model to mimic human long multiplication via a two-stage scratchpad, the complex $N \times M$ multiplication task is reduced to simpler sub-tasks (single-digit multiplication and multi-operand addition) that the model can already generalize on.

Results: Unprecedented Generalization

Bi-level Position Coupling (+Scratchpad) achieves near-perfect generalization up to 30 digits and 30 operands.
Baseline FIRE (+Scratchpad) fails to generalize significantly beyond the 10x10 training distribution (red box).
  • Multi-Operand Addition: Models trained on a maximum of 10 digits and 10 operands successfully generalized to problems with 30 digits and 30 operands, achieving a median exact-match accuracy of $\ge 90.0\%$. State-of-the-art baselines (NoPE, FIRE) completely failed (near 0% accuracy) even on much smaller leaps, like 13 operands of 13 digits.
  • Multiplication: Models trained on $10 \times 10$ digit multiplication generalized to $20 \times 15$ digit multiplication with $\ge 78.55\%$ exact-match accuracy.
  • Architecture Efficiency: Surprisingly, tiny models (like 2-layer, 2-head Transformers) sometimes outperformed larger baselines (6-layer, 8-head) on these specific algorithmic tasks when equipped with this method, proving that the bottleneck is often data representation, not model size.

Limitations and Future Directions

Limitations: The primary drawback of this approach is that it requires the task's underlying algorithmic structure to be well-understood by the human programmer. You have to explicitly hand-design the scratchpad format and the multi-level position coupling scheme to match the math. It is not a "plug-and-play" solution for arbitrary text.

Future Work: The authors suggest two main directions: (1) establishing a clear, automated principle for applying scratchpads and Position Coupling when a task's structure is known, reducing the reliance on human intuition; and (2) extending this method to tasks where the underlying structure is implicit or entirely unknown.

Summary updated

BibTeX
@inproceedings{cho2025arithmetic,
  author = {Hanseul Cho and Jaeyoung Cha and Srinadh Bhojanapalli and Chulhee Yun},
  title = {{Arithmetic Transformers Can Length-Generalize in Both Operand Length and Count}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=eIgGesYKLG},
}
Figure