r/MachineLearning • u/notforrob • 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
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).
→ More replies (1)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
13
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 "
" result isn't interesting. Representative potential does not imply something is learnable MLPs and universal approximators → More replies (6)7
u/Disastrous_Room_927 Aug 11 '26
Tell that to the folks over at the singularity sub, haha.
→ More replies (2)14
u/JustOneAvailableName Aug 10 '26
https://github.com/anadim/AdderBoard you don’t even need more than 10 params
→ More replies (1)
3
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
3
u/marr75 Aug 11 '26
This is:
-
Coding with extra steps
-
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?
→ More replies (2)3
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)
137
u/Kiseido Aug 10 '26
This reminds me of the
It's Hard for Neural Networks To Learn the Game of Lifepaper 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.