Papers › Transformers Can Do Arithmetic with the Right Embeddings

Transformers Can Do Arithmetic with the Right Embeddings

27 May 2024arXiv:2405.17399archive 2025-07-28

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

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.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2405.17399")

Code

Syntology Ran 12 of 13 code samples harvested from 1 repository linked to this paper; 1 has no recorded run. Of those that ran: 12 ran with no contract checked.

By repository: official repository: 13 samples from 1 repository, 12 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

mcleish7/arithmetic officialmentioned in papermentioned on GitHubpytorchMIT report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

13 samples harvested; 12 ran; 0 honoured the contract we drafted; 1 has no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

12ran
1unverified

Licence: 0 of the 13 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from mcleish7/arithmetic. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

check_checkpointing mcleish7/arithmetic/pretrain.py official repository ran MIT (permissive) · 6904fbecf8a80e45 · report
count_digits mcleish7/arithmetic/dataset_analysis.py official repository ran MIT (permissive) · 93142aa14ecbc7be · report
find_file mcleish7/arithmetic/pretty_plotter.py official repository ran fingerprinted MIT (permissive) · cff91a92610dfa6a · report
generate_dataset mcleish7/arithmetic/create_data_split.py official repository ran MIT (permissive) · 7cdfbdb5267c36af · report
generate_no_carry_addition mcleish7/arithmetic/create_data_split.py official repository ran fingerprinted MIT (permissive) · 52b246cf6203173b · report
get_time_elapsed mcleish7/arithmetic/pretrain.py official repository ran fingerprinted MIT (permissive) · 93ef1c29a04c0d05 · report
has_carry mcleish7/arithmetic/create_data_split.py official repository ran fingerprinted MIT (permissive) · 403c2dbbe04c7692 · report
index_hints_helper mcleish7/arithmetic/arithmetic_eval_quicker.py official repository ran MIT (permissive) · b3bb8f995329f91d · report
one_hot_vector mcleish7/arithmetic/create_pos_or_variants.py official repository ran fingerprinted MIT (permissive) · 86e5db13667ce384 · report
read_dataset mcleish7/arithmetic/dataset_analysis.py official repository ran MIT (permissive) · 3bff376763abe533 · report
remove_leading_zeros mcleish7/arithmetic/dataset_analysis.py official repository ran MIT (permissive) · 877bea5a25e5eb7a · report
zero_vector mcleish7/arithmetic/create_pos_or_variants.py official repository ran fingerprinted MIT (permissive) · dc757096a531ea2b · report
grid_logic mcleish7/arithmetic/arithmetic_eval_quicker.py official repository unverified MIT (permissive) · 63fd2ecb6d9d9780 · report

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

MEND

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections