Transformers Can Do Arithmetic with the Right Embeddings
Sean McLeish, Arpit Bansal, Alex Stein, Neel Jain, John Kirchenbauer, Brian R. Bartoldson, Bhavya Kailkhura, Abhinav Bhatele, Jonas Geiping, Avi Schwarzschild, Tom Goldstein
TL;DR
The paper tackles the challenge of arithmetic reasoning in transformers by identifying digit-position tracking as a critical bottleneck and proposing Abacus Embeddings that encode digit significance within each number. When combined with input injection and looped transformer architectures, these embeddings enable strong length generalization, achieving up to $99\%$ accuracy on 100-digit additions and extending to $120$-digit problems, as well as improving performance on multiplication and sorting. The authors also demonstrate the benefits of recurrence, showing that looped transformers with progressive loss can outperform standard architectures across ID and OOD settings. These results suggest a path toward more capable algorithmic reasoning in transformers without external tools, with potential applicability to a range of numerically intensive tasks and broader relational reasoning challenges.
Abstract
The poor performance of transformers on arithmetic tasks seems to stem in large part from their inability to keep track of the exact position of each digit inside of a large span of digits. We mend this problem by adding an embedding to each digit that encodes its position relative to the start of the number. In addition to the boost these embeddings provide on their own, we show that this fix enables architectural modifications such as input injection and recurrent layers to improve performance even further. With positions resolved, we can study the logical extrapolation ability of transformers. Can they solve arithmetic problems that are larger and more complex than those in their training data? We find that training on only 20 digit numbers with a single GPU for one day, we can reach state-of-the-art performance, achieving up to 99% accuracy on 100 digit addition problems. Finally, we show that these gains in numeracy also unlock improvements on other multi-step reasoning tasks including sorting and multiplication.
