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
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.
78
u/DotRealistic Researcher 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
13
u/alrojo Aug 10 '26
https://arxiv.org/abs/2001.05016
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 ML Engineer 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
3
u/Sharp-Objective9078 Aug 10 '26
Cool stuff!
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:
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?
4
u/Random-Number-1144 Aug 11 '26
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.
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)
1
u/ricafernandes Aug 11 '26
The thing to learn those "subnets" autonomously is consistency across samples, which always has been transformers main limitation
Perhaps Yann models or some optimization algo could improve params without collapsing prior "consolidated knowledge" (which would itself be hard to define autonomously for any possible case) or contradicting itselves accross samples
1
u/Nice-Dragonfly-4823 Aug 13 '26
this is very interesting, nice work!
1
u/blimpyway Aug 11 '26
That made me curious - aren't transformers fetishized enough, a MLP can't do it? Then I asked Gemini (sic) to provide an algorithmic generator of a MLP implementing multiplication and it did it. It's a 2 hidden layer network with the bulk of its 100M weights in a 10k X 10k large matrix. Interestingly, most of those weights are 0.0, with 1.0 values on its diagonal.
0
u/Deto Aug 10 '26
I'm actually surprised the models can even do this a little bit without reasoning. It'd be like asking a person to arrive at the answer directly without working through it using the standard algorithm
0
u/slashdave Aug 13 '26
It's multiplication (completely linear). Think carefully about what one weight layer can accomplish in a model.
-2
u/[deleted] Aug 11 '26
[removed] — view removed comment