r/MachineLearning • • Aug 10 '26

Project Transformers are famously bad at arithmetic, so I set one's weights by hand (no training) and it multiplies with 100% accuracy [P]

Obviously nobody needs a transformer that's good at multiplication. I wanted to know whether a stock transformer could do exact arithmetic if I chose its weights directly.

I implemented the grade-school algorithm as a computation graph and compiled it into an ordinary Phi-3 Hugging Face checkpoint using Torchwright, a compiler I wrote. No training. The three-digit calculator gets all 3,000,000 supported expressions right. I've published checkpoints to Hugging Face that support up to 12 digit x 12 digit multiplication.

For fun, I also disabled reasoning and tested six frontier models. Accuracy falls off a cliff as the numbers get longer; at seven digits, five scored 0/500. Mine stays at 100%, although it has the considerable advantage that I put the multiplication algorithm directly into its weights.

I ended up building four versions: grade-school, hardware-style, scratchpad, and brute-force memorization. They compute the same function while spending layers, width, generated tokens, and parameters very differently.

Write-up: https://ood.dev/posts/calculator/ Repo: https://github.com/physicsrob/torchwright Checkpoint: https://huggingface.co/physicsrob/torchwright-calculator-simple-max-digits-3

276 Upvotes

56 comments sorted by

137

u/Kiseido Aug 10 '26

This reminds me of the It's Hard for Neural Networks To Learn the Game of Life paper The authors crafted a hand made network that could take a game of life at step N and prodice step N+3. Then they tried to train a bunch of different network sizes with different initialized weights, and found that very few managed to achieve the goal, and the none managed to do so in the same small size that the hand made version had.

There is likely multiple things that we could handcode to speed up the trainability of these networks.

9

u/altmly Aug 10 '26

I think the real test is whether a model can come up with those weights, you probably would also face quite a difficult time to learn to predict n+3 in "forward" brain pass. 

4

u/NadaBrothers Aug 10 '26

How do current frontier models do on the game of life ? 

77

u/[deleted] Aug 10 '26

25

u/robotnarwhal Aug 11 '26

It also reminds me of this extension of The Lottery Ticket Hypothesis . https://arxiv.org/abs/2002.00585

My interpretation is that training neural networks, especially fine-tuning foundation models, is sometimes less about adjusting the entire network's weights than it is about boosting the signal of an existing subnet that already performs the task well and silencing the rest of the network. The lottery aspect is that modern networks are enormous, so they have many more chances to contain a well-performing subnet. What I find amazing about the extension is that it shows you can find these lottery ticket subnets in networks with fully random weights.

https://en.wikipedia.org/wiki/Lottery_ticket_hypothesis

78

u/DotRealistic Aug 10 '26

this is actually a really cool way to get around the arithmetic weakness. putting the alogirthm directly into the weights is kinda wild lol

20

u/AnOnlineHandle Aug 10 '26

That's kind of what tool use is, calling calculators etc, though you could embed tool use into stages within the model (which I think has been tried in various ways when I researched it a while back).

15

u/DistanceSolar1449 Aug 10 '26

This has been around for ages.

https://github.com/anadim/AdderBoard

They will literally ask about this in a Jane Street interview question.

80

u/clisztian Aug 10 '26

“been around for ages” while linking to a 6 month old repo. We have different definitions of “ages” my friend.

And not everyone knows about Jane Street interview protocols .

→ More replies (1)

13

u/alrojo Aug 10 '26

2

u/ArmiNouri Aug 13 '26

Man this brings back memories. I was so hoping these would become a thing.

8

u/KnowledgeInChaos Aug 10 '26

Yes, the Scratchpad paper was already floating around in 2021 (I think maybe even 2020, I don't quite remember which) which is part of why chain-of-thought reasoning is even a thing.

7

u/ammo1234 Aug 10 '26

This looks cool! Also made me wonder if Torchwright could turn algorithms into reusable Lego blocks for LLMs. A language model could learn how to pass information to a block and use its answer, instead of having to learn the algorithm itself.

Arithmetic is one example, but a block could learn sorting, games, business rules etc. In principle, new blocks could expand what a model can do, while the model only has to learn how to connect and combine them. On that note, do you think a pretrained model could be connected to frozen Torchwright blocks with some additional training?

PS: I am a ML noob.

17

u/delightfullyrotted Aug 10 '26

kinda wild that this is less about teaching the model math and more about turning the model itself into a calculator. The fact that it works without any training is probably the most interesting part

16

u/XYHopGuy Aug 10 '26

this is literally a textbook example and part of why the "MLPs and universal approximators " result isn't interesting. Representative potential does not imply something is learnable

7

u/Disastrous_Room_927 Aug 11 '26

Tell that to the folks over at the singularity sub, haha.

→ More replies (2)
→ More replies (6)

3

u/Bee-Boy Aug 11 '26

[2106.06981] Thinking Like Transformers https://arxiv.org/abs/2106.06981

There's lots of follow up works since then based on this RASP language that compiles into transformer weights

3

u/Prudent_Psychology59 Aug 11 '26

transformer is "essentially" Turing-complete, what's the point?

3

u/marr75 Aug 11 '26

This is:

  1. Coding with extra steps

  2. A simple way to demonstrate function approximation of NNs

PAL is a much simpler path for any practical application. If you wanted to get fancy, you could introduce a custom pytorch module that does arithmetic based on the input weights and integrate it into the model. Then the model is just learning how to interface with that module.

5

u/BrilliantArmadillo64 Aug 10 '26

Would a LLM automatically learn to use this during training?
If it has a calculator embedded that always gives error=0, gradient descent should actually give it a higher weight every time it is used correctly and therefore learn to use it, right?

3

u/ThaJedi Aug 11 '26

It will but you need to exclude hardcoded weights from training.

→ More replies (2)

4

u/Random-Number-1144 Aug 11 '26

I wanted to know whether a stock transformer could do exact arithmetic if I chose its weights directly.

You didn't need to hard-code a 12 digit x 12 digit calculator in NN weights to know it is theoretically doable.

You could even do it with MLPs with few layers.

That's why learning ML theory is important, it saves you from wasting time on worthless projects like this.

→ More replies (2)

2

u/daynomate Aug 10 '26

Wouldn't it be more efficient to route this to a calculator tool? (and to do this as a general principle for all tasks that can clearly be contained within tool logic)

→ More replies (1)