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

Teaching Arithmetic to Small Transformers

Nayoung Lee, Kartik Sreenivasan, Jason D. Lee, Kangwook Lee, Dimitris Papailiopoulos

Introduction

Large Language Models (LLMs) like GPT-4 exhibit impressive "emergent" abilities in mathematics, yet the exact mechanisms behind this remain elusive. Standard autoregressive Transformers predict tokens sequentially from left to right. However, in standard arithmetic (e.g., $128 + 367 = 495$), the most significant digit of the answer depends on all subsequent digits due to potential carry-overs. This creates an "attention glitch," forcing the model to perform a complex global calculation just to predict the first token of the output.

This paper investigates whether small models (as small as 10M parameters) can learn arithmetic efficiently from scratch if the training data is formatted to better align with how Transformers process information. By simply reversing the order of digits or providing intermediate "scratchpad" steps, researchers achieved 100% accuracy with significantly less data, suggesting that emergent math abilities are heavily influenced by data formatting.

Rethinking Data Formats

To test this hypothesis, the researchers trained small decoder-only Transformers (NanoGPT and GPT-2) from random initialization using four distinct data formats for addition:

  1. Plain: The standard format (e.g., $128+367=495$).
  2. Reverse: Reversing the result (e.g., $128+367=594$). This allows the model to predict the least significant digit first, making the carry logic a local, sequential operation rather than a global one.
  3. Simplified Scratchpad: Recording the digit-wise sum and carries before the final answer.
  4. Detailed Scratchpad: Providing a full, step-by-step "Chain-of-Thought" (CoT) breakdown of the math.
Data formatting strategies and their corresponding impact on test accuracy.

As shown above, the formatting drastically alters sample efficiency. In 3-digit addition, the Plain format struggled to reach high accuracy even with 5,000 training examples. In contrast, the Reverse format reached 100% accuracy with roughly 2,500 examples, and the Detailed Scratchpad proved the most efficient, hitting 100% accuracy with only 1,000 examples.

Key Insights: Matrix Completion and Function Decomposition

The success of these formatting tricks reveals fundamental insights into how Transformers learn algorithms:

  • The Reversal Trick: Because the $i$-th digit of a sum only depends on the $i$-th digits of the operands and the carry from the $(i-1)$-th position, reversing the output turns a complex global problem into a manageable local one.
  • Addition as Matrix Completion: The authors demonstrate that learning an addition table is mathematically akin to completing a low-rank matrix. This theoretical connection explains the "sharp phase transitions" observed during training, where a model's accuracy jumps from near 0% to 100% almost instantly once a specific data threshold is met.
Comparing the phase transitions of Low-Rank Matrix Completion (LRMC) and NanoGPT training.
  • Scratchpads as Function Decompositions: Training on "Chain-of-Thought" data is highly effective because it breaks down a high-dimensional, complex mapping into a series of lower-dimensional, easier-to-learn steps.

Token Trade-offs and Fine-Tuning

While scratchpads are incredibly sample-efficient and accurate, they introduce a "token trade-off." They require significantly more tokens to express an answer, which increases computational costs during inference. The Reverse format often serves as the best middle ground, balancing sample efficiency with inference speed.

These formatting benefits extend beyond training from scratch. When fine-tuning OpenAI’s GPT-3 models on these formats, even the smallest model (Ada) reached 99.8% accuracy on addition using the Detailed Scratchpad, compared to near 0% in a standard zero-shot setting.

Limitations and Future Work

Despite these successes, length generalization remains a significant hurdle. Models trained perfectly on 1- to 3-digit addition failed when asked to add 4-digit numbers. This indicates that while the models learn the underlying algorithm, their learned representation is constrained to the specific lengths seen during training, rather than acting as a universally flexible algorithm.

Furthermore, while these formatting techniques work well for addition and subtraction, more complex operations like multiplication and elementary functions (e.g., Sine or Square Root) remain challenging. The intermediate steps for these operations (such as Taylor series expansions) are computationally intensive. Future research will need to explore new data formats and training strategies that enable models to generalize across different numerical scales and tackle more complex algorithmic tasks.

Summary updated

BibTeX
@inproceedings{lee2024teaching,
  author = {Nayoung Lee and Kartik Sreenivasan and Jason D. Lee and Kangwook Lee and Dimitris Papailiopoulos},
  title = {{Teaching Arithmetic to Small Transformers}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=dsUB4bst9S},
}
Figure