Transformers Can Do Arithmetic with the Right Embeddings

Sean Michael McLeish, Arpit Bansal, Alex Stein, Neel Jain, John Kirchenbauer, Brian R. Bartoldson, Bhavya Kailkhura, Abhinav Bhatele, Jonas Geiping, Avi Schwarzschild, Tom Goldstein

Introduction

Transformers have revolutionized natural language processing but remain notoriously "innumerate." While they can memorize small-scale arithmetic, they typically fail to generalize to numbers longer than those seen during training—a phenomenon known as the length generalization gap. This paper identifies the root cause not as a lack of logic, but as a failure of positional representation. Standard embeddings do not help models align digits of equal significance across different numbers. To solve this, the authors introduce Abacus Embeddings, a novel positional encoding scheme that enables Transformers to achieve near-perfect accuracy on 100-digit addition after training on only 20-digit examples.

Methods: Aligning for Arithmetic

The core innovation is the Abacus Embedding system. Unlike standard absolute or relative positional encodings that track a token's position in a sequence, Abacus Embeddings track a digit's significance relative to the start of the number it belongs to.

Illustration of Abacus Embeddings vs. Absolute Embeddings. By using Least Significant Digit (LSD) first formatting, Abacus Embeddings ensure that digits of the same place value receive the same positional ID, facilitating alignment.

The methodology consists of three key components:

  1. Abacus Embeddings & LSD Formatting: Numbers are formatted in "Least Significant Digit (LSD) first" order (e.g., 123 becomes 3, 2, 1). Each digit is assigned a learned embedding based on its distance from the start of the number. This ensures that the "ones" place always has position 0, the "tens" place position 1, and so on, regardless of where the number appears in the input string.
  2. Randomized Offsets: To prevent the model from overfitting to specific absolute positions, a random shift $k$ is applied to the starting index of numbers during training.
  3. Architectural Refinements: The authors utilize Input Injection (adding input embeddings to every decoder layer) and Looped Transformers (recurrent layers with shared weights). These allow the model to iteratively process carries and logic, effectively modeling algorithmic reasoning as a repetitive process.

Experimental Results

The researchers evaluated the architecture on tasks including multi-digit addition, subtraction, multiplication, and array sorting. The results demonstrate a significant leap in out-of-distribution (OOD) performance.

Heatmaps comparing length generalization for FIRE vs. Abacus embeddings. The red box indicates the training distribution (up to 20 digits). Abacus embeddings maintain high accuracy (dark blue) far beyond the training range, whereas FIRE fails immediately outside it.

Key Findings

  • Superior Generalization: Models trained on 20-digit addition reached 99% accuracy on 100-digit problems, representing a 6x generalization factor. In contrast, state-of-the-art embeddings like FIRE and NoPE failed completely on these OOD tasks.
  • Efficiency of Recurrence: Looped Transformers achieved these results with 10x fewer parameters than standard stacked architectures, suggesting that recurrence is more robust for algorithmic tasks than depth.
  • Broad Applicability: Beyond addition, Abacus Embeddings improved OOD performance in sorting variable-length arrays and achieved high in-distribution accuracy for 15-digit multiplication.

Discussion and Future Work

The success of Abacus Embeddings suggests that arithmetic is primarily a spatial alignment problem for Transformers. Once digits are correctly aligned in "columns" via the embedding space, the attention mechanism easily learns the underlying logic.

However, challenges remain. The current approach requires reversing numbers (LSD first), which deviates from standard natural language conventions. Furthermore, multiplication remains significantly more complex than addition due to its quadratic nature. Future research will explore integrating these specialized embeddings into general-purpose Large Language Models (LLMs) to enhance their native mathematical capabilities without sacrificing linguistic performance.

Summary updated

BibTeX
@inproceedings{mcleish2024transformers,
  author = {Sean Michael McLeish and Arpit Bansal and Alex Stein and Neel Jain and John Kirchenbauer and Brian R. Bartoldson and Bhavya Kailkhura and Abhinav Bhatele and Jonas Geiping and Avi Schwarzschild and Tom Goldstein},
  title = {{Transformers Can Do Arithmetic with the Right Embeddings}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=aIyNLWXuDO},
}
Figure