NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Length Generalization in Arithmetic Transformers

Samy Jelassi, Stéphane d'Ascoli, Carles Domingo-Enrich, Yuhuai Wu, Yuanzhi Li, François ChartonarXiv 2023

Abstract

We examine how transformers cope with two challenges: learning basic integer arithmetic, and generalizing to longer sequences than seen during training. We find that relative position embeddings enable length generalization for simple tasks, such as addition: models trained on $5$-digit numbers can perform $15$-digit sums. However, this method fails for multiplication, and we propose train set priming: adding a few ($10$ to $50$) long sequences to the training set. We show that priming allows models trained on $5$-digit $\times$ $3$-digit multiplications to generalize to $35\times 3$ examples. We also show that models can be primed for different generalization lengths, and that the priming sample size scales as the logarithm of the training set size. Finally, we discuss potential applications of priming beyond arithmetic.

Introduction

Large Language Models (LLMs) demonstrate remarkable capabilities in natural language tasks but often fail at basic arithmetic when sequence lengths exceed those seen during training. This failure in length generalization—the ability to apply rules learned on short sequences to significantly longer ones—is a critical bottleneck for mathematical reasoning in AI.

The authors identify two primary obstacles:

  1. Absolute Position Embeddings (APE): Standard Transformers tie token meanings to specific indices. If a model is trained on sequences up to length 10, it lacks the representation for tokens at index 20.
  2. Task Complexity: While addition is a relatively local operation, multiplication is globally complex, requiring more than simple architectural adjustments to generalize effectively.

Methods

The researchers utilized encoder-only Transformers (similar to BERT) rather than the standard decoder-only (GPT-style) architecture. They posit that arithmetic is better framed as processing a complete expression into a result rather than next-token prediction.

Transformer architecture for arithmetic tasks.

Key technical innovations included:

  • Relative Position Embeddings (RPE): By encoding the distance between tokens (e.g., distance from the '+' sign) rather than absolute indices, the model can learn a "sliding window" algorithm applicable to any sequence length.
  • Train Set Priming: To address multiplication, the authors introduced a tiny fraction (0.1% to 1%) of long-sequence examples into the initial training phase. This "priming" avoids the catastrophic forgetting associated with traditional fine-tuning.
  • Universal Transformers (UTransformers): These models apply recurrent layer weights, mimicking the iterative nature of mathematical algorithms.

The study employed synthetic datasets for addition (trained on 5 digits, tested up to 20), multiplication (5x3 trained, tested on 35x3), and modular arithmetic.

Results and Discussion

The experiments revealed that RPE acts as a "sliding window," allowing models to learn unified algorithms for addition. As shown in the performance table, RPE-based models maintain high accuracy far beyond their training length, whereas APE models fail immediately upon encountering longer sequences.

Accuracy of addition tasks across different sequence lengths and position embeddings.

For addition, RPE models trained on 5-digit sums achieved 97.2% accuracy on 15-digit sums, while APE models dropped to 0% accuracy beyond 6 digits. In multiplication, train set priming with just 50 examples enabled near 100% accuracy on 35x3 problems, despite the bulk of training data being 5x3. The efficiency of priming was found to be logarithmic; the number of long-sequence examples required grows slowly relative to the total training set size.

Conclusion

While RPE solves length generalization for local operations like addition, more complex tasks like multiplication require "priming" with a small number of long-sequence examples. Future work will explore whether these techniques generalize to symbolic math, matrix operations, and broader NLP contexts, potentially enabling LLMs to handle much longer contexts without performance degradation.

Summary updated

BibTeX
@misc{jelassi2023length,
  author = {Samy Jelassi and Stéphane d'Ascoli and Carles Domingo-Enrich and Yuhuai Wu and Yuanzhi Li and François Charton},
  title = {{Length Generalization in Arithmetic Transformers}},
  year = {2023},
  eprint = {2306.15400},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2306.15400},
}
Figure