research notebook · tokenization and arithmetic in transformers

How Tokenization Decides Whether a Transformer Can Add

The same small transformer, trained on the same additions, either extends far past its training length or fails soon after it, depending only on how the numbers are cut into tokens, the order the answer is written in, and whether position follows place value. A controlled grid of 16 tokenization × encoding cells, three seeds each, at matched training compute.

research log trained on operands of ≤ 10 digits written with Claude (Anthropic) as research assistant
GPT-style tokens (3-digit chunks cut from the left), forward
0%
average exact-match at 19–21 digits, with the place-value encoding; 98% in-distribution.
Same chunks cut from the right, written units-first
98%
at 19–21 digits, same model, data and compute. Only the cut and the order changed.

How numbers are split into tokens is usually treated as a preprocessing detail. For arithmetic it is not: it decides which algorithm is easy to learn, and whether that algorithm keeps working on numbers longer than any seen in training. This log measures that directly, with everything else held fixed.

The setup

Addition problems with operands of up to 10 digits, freshly sampled, 30% of them built to contain long carry chains. Small GPT-style decoders (6 layers, 384 wide, ~10.6M parameters), EMA weights, three seeds per cell. Each tokenization is three independent choices:

Two position encodings: standard (rotary positions stretched by a random factor 1–4 per training sequence) and place-value (half of the rotary pairs driven by each chunk's index within its own number, also stretched, a form of position coupling). Cells are compared at matched training compute (~4.3·1015 FLOPs: 16k steps for k=1, 31k for GPT-2, 33k for k=3, since longer chunks mean shorter sequences). Test tiers: in-distribution (1–10 digits), a gap tier (11–13), and two out-of-distribution tiers (19–21, 24–26).

Results

Exact-match accuracy per tier, seeds 0 / 1 / 2 (final EMA weights)
tokenizationorderencodingin-dist11–1319–2124–26
k=1reversedplace-value1.00 / 1.00 / 1.001.00 / 1.00 / 1.001.00 / .99 / 1.001.00 / .92 / 1.00
k=1reversedstandard1.00 / 1.00 / 1.00.99 / .99 / 1.00.07 / .01 / .040 / 0 / <.01
k=1forwardplace-value.97 / .97 / .99.82 / .86 / .900 / .14 / <.010 / .04 / 0
k=1forwardstandard.99 / .99 / .99.94 / .94 / .950 / .01 / <.010 / 0 / 0
k=3 right-cutreversedplace-value1.00 / 1.00 / 1.001.00 / 1.00 / 1.001.00 / .96 / 1.00.99 / .93 / 1.00
k=3 right-cutreversedstandard1.00 / 1.00 / 1.00.39 / .28 / .260 / 0 / 00 / 0 / 0
k=3 right-cutforwardplace-value.98 / .99 / .98.44 / .28 / .32<.01 / 0 / <.010 / 0 / 0
k=3 right-cutforwardstandard.99 / .99 / .74.22 / .25 / .150 / 0 / 00 / 0 / 0
k=3 left-cut (GPT-style)forwardplace-value.98 / .99 / .99.03 / .07 / .05.01 / 0 / 0.01 / <.01 / 0
k=3 left-cutforwardstandard.99 / .99 / .98.24 / .16 / .120 / 0 / 00 / 0 / 0
k=3 left-cutreversedplace-value1.00 / .99 / .99.31 / .51 / .520 / 0 / 00 / 0 / 0
k=3 left-cutreversedstandard1.00 / 1.00 / 1.00.01 / .15 / .120 / 0 / 00 / 0 / 0
GPT-2 BPEreversedplace-value.90 / .89 / .92.62 / .65 / .70.03 / .06 / .110 / .01 / .01
GPT-2 BPEreversedstandard.92 / .91 / .93.20 / .22 / .570 / 0 / 00 / 0 / 0
GPT-2 BPEforwardplace-value.79 / .78 / .80.30 / .35 / .360 / 0 / .010 / 0 / 0
GPT-2 BPEforwardstandard.76 / .80 / .77.29 / .28 / .250 / 0 / 00 / 0 / 0

What the grid says

  1. Extending past the training length takes three things together: reversed order, chunks aligned with place value (k=1, or right-cut k=3), and a place-value position signal. Remove any one and accuracy beyond the training length is near zero, often with perfect in-distribution accuracy. Only the two cells with all three extend.
  2. Order. Writing the most significant digit first learns in-distribution but fails within a few digits of the training length, for every tokenization: the first answer token needs the carry from every column below it.
  3. Cut. Left-cut fails to extend even when reversed. Its chunk boundaries move with each number's length, so the rule "add chunk i of each operand" is a different rule for every pair of lengths.
  4. BPE. GPT-2's tokenizer is worst on both counts: it never reaches the fixed-chunk cells' in-distribution accuracy and barely extends in either order. Its splits are irregular (1034567 → [103][45][67]).
  5. The place-value signal only helps when chunks align with place value. With misaligned chunks it is useless or harmful.

This matches what Singh & Strouse found in frontier models: forcing GPT-3.5 and GPT-4 to group digits from the right (with commas) made their addition much more accurate, which they trace to alignment between the tokens of the operands and the answer.

How far the good cells reach

Accuracy on random problems far past the tiers, seeds 0 / 1 / 2 (place-value encoding)
cell26 digits405060
k=1 reversed, place-value1.00 / .93 / 1.001.00 / .62 / 1.00.80 / .23 / .83.38 / .03 / .05
k=3 right-cut reversed, place-value1.00 / .90 / .96.40 / .41 / .01.11 / .01 / 00 / 0 / 0

k=3 reaches fewer digits than k=1, not three times as many. The limit is not sequence length but coverage: the place-value rotation has only seen index differences up to about 4 chunks × the stretch of 4 during training, and reach ends near the edge of what it has seen. Reach depends on tokenization through how many chunks training numbers contain.

With a better position encoding

A companion log (Why Rotary Embeddings Stop Extrapolating) traced the coverage limit to rotary aliasing and fixed it for k=1: 2D rotary positions (row × column), a fixed multiplicative decay with column distance, and a position-free attention sink took k=1 to 100% at 200 digits on every seed. The same fix, trained from scratch on k=3 at the same compute:

right-cut, index-RoPE + stretch (mean)right-cut, 2D RoPE + decay + position-free sinkleft-cut, same fix (mean)
0% 25% 50% 75% 100% 26 40 60 80 100 150 200 operand length (digits); trained on ≤ 10 exact-match accuracy right-cut, index-RoPE (mean): 26 → 95% right-cut, index-RoPE (mean): 40 → 27% right-cut, index-RoPE (mean): 60 → 0% right-cut, index-RoPE (mean): 80 → 0% right-cut, index-RoPE (mean): 100 → 0% right-cut, index-RoPE (mean): 150 → 0% right-cut, index-RoPE (mean): 200 → 0% right-cut + fix, seed 0: 26 → 100% right-cut + fix, seed 0: 40 → 100% right-cut + fix, seed 0: 60 → 100% right-cut + fix, seed 0: 80 → 100% right-cut + fix, seed 0: 100 → 100% right-cut + fix, seed 0: 150 → 99% right-cut + fix, seed 0: 200 → 99% right-cut + fix, seed 1: 26 → 100% right-cut + fix, seed 1: 40 → 100% right-cut + fix, seed 1: 60 → 100% right-cut + fix, seed 1: 80 → 100% right-cut + fix, seed 1: 100 → 100% right-cut + fix, seed 1: 150 → 100% right-cut + fix, seed 1: 200 → 100% right-cut + fix, seed 2: 26 → 100% right-cut + fix, seed 2: 40 → 94% right-cut + fix, seed 2: 60 → 67% right-cut + fix, seed 2: 80 → 76% right-cut + fix, seed 2: 100 → 68% right-cut + fix, seed 2: 150 → 54% right-cut + fix, seed 2: 200 → 49% left-cut + fix (mean): 26 → 19% left-cut + fix (mean): 40 → 13% left-cut + fix (mean): 60 → 39% left-cut + fix (mean): 80 → 22% left-cut + fix (mean): 100 → 10% left-cut + fix (mean): 150 → 29% left-cut + fix (mean): 200 → 13% fix, seed 1 fix, seed 0 fix, seed 2 left-cut + fix index-RoPE

90 random problems per length. Right-cut with the fix: 1.00 / 1.00 / .67 at 60 digits and .99 / 1.00 / .49 at 200 (seeds 0 / 1 / 2), from 0 / 0 / 0 at 60 with index-RoPE. Seed 2's remaining failure is a separate mechanism (dilution of a weak attention sink; see the companion attention log).

Left-cut with the same fix gets .37 / .29 / .51 at 60 digits and .21 / .01 / .16 at 200, more than its zero baseline. Split by problem shape, nearly all of it comes from problems that are mostly copying:

k=3 left-cut reversed with the fix: accuracy by problem shape over all lengths (26–200 digits), seeds 0 / 1 / 2
problem shapeaccuracy
short operand 1 digit (the answer is mostly a copy of the long operand).92 / .20 / .60
short operand L/2 digits.04 / 0 / .10
both operands L digits (real chunk-by-chunk addition)0 / 0 / 0
a carry-out shifts the answer’s chunk boundaries0 / 0 / 0

So the fix sharpens the first finding rather than changing it. With place-aligned chunks the remaining limit was the position encoding, and fixing it takes k=3 from zero at 60 digits to near-perfect at 200 on two of three seeds. With left-cut chunks the model still cannot add past its training length at all: no position encoding can line up chunks whose boundaries move with each number's length.

And with more data instead

The other way to reach further is to train on longer problems. Keeping the place-value encoding and changing only the data (10% of problems with a 20–40 digit operand, warm-started from the 16k-step models):

k=1 reversed, place-value: reach by operand length, seeds 0 / 1 / 2
modellongest trained406080120160200400
baseline (16k steps)101.00 / .67 / 1.00.31 / 0 / .02.04 / 0 / .010 / 0 / 00 / 0 / 00 / 0 / 00 / 0 / 0
control (24k, normal data)101.00 / .72 / 1.00.01 / 0 / .01.02 / 0 / 00 / 0 / 00 / 0 / 00 / – / –0 / – / –
10% long problems (24k)401.00 / 1.00 / 1.001.00 / 1.00 / 1.001.00 / 1.00 / 1.001.00 / 1.00 / .981.00 / .82 / .940 / .03 / 00 / 0 / 0
trained on ≤ 10 digits (mean of 3 seeds)10% of problems at 20–40 digits (mean)
0% 25% 50% 75% 100% 26 40 80 160 200 300 400 operand length (digits) exact-match accuracy baseline (mean): 26 → 99% baseline (mean): 40 → 89% baseline (mean): 60 → 11% baseline (mean): 80 → 2% baseline (mean): 120 → 0% baseline (mean): 160 → 0% baseline (mean): 200 → 0% baseline (mean): 300 → 0% baseline (mean): 400 → 0% long mix (mean): 26 → 100% long mix (mean): 40 → 100% long mix (mean): 60 → 100% long mix (mean): 80 → 100% long mix (mean): 120 → 99% long mix (mean): 160 → 92% long mix (mean): 200 → 1% long mix (mean): 300 → 0% long mix (mean): 400 → 0% with long problems ≤ 10 digits

Dashed line: 160 digits, four times the longest training length. Means over three seeds.

Long data makes every seed (near-)perfect to 120 digits, but the cliff only moves: from about 40 to about 160 digits, four times the longest training length both times, which is exactly the encoding's random stretch factor. Data extends coverage; it does not teach a mechanism that works beyond it.

Limits

Where this leaves things

The tokenizer decides which algorithm is learnable.

Right-cut chunks written units-first make each answer token a local function of one column and a carry, and a model trained on 10 digits can then extend to 200 with the right position encoding. Left-cut chunks, the way GPT-style tokenizers cut numbers, change the rule with every number length, and no position encoding or amount of training length tested here undoes that.

References

  1. A. K. Singh, D. J. Strouse, Tokenization counts: the impact of tokenization on arithmetic in frontier LLMs, arXiv 2402.14903 (2024).
  2. H. Cho et al., Position Coupling: Improving Length Generalization of Arithmetic Transformers Using Task Structure, NeurIPS 2024 (arXiv 2405.20671).
  3. S. McLeish et al., Transformers Can Do Arithmetic with the Right Embeddings, NeurIPS 2024 (arXiv 2405.17399).
  4. Y. Zhou et al., Transformers Can Achieve Length Generalization But Not Robustly, arXiv 2402.09371 (2024).
  5. A. Radford et al., Language Models are Unsupervised Multitask Learners (GPT-2), OpenAI (2019).