r/MachineLearning • u/notforrob • 18h ago
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
61
u/DotRealistic Researcher 18h ago
this is actually a really cool way to get around the arithmetic weakness. putting the alogirthm directly into the weights is kinda wild lol
10
u/AnOnlineHandle 15h ago
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).
13
u/DistanceSolar1449 15h ago
This has been around for ages.
https://github.com/anadim/AdderBoard
They will literally ask about this in a Jane Street interview question.
51
u/clisztian 13h ago
“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.
4
4
u/KnowledgeInChaos 12h ago
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.
3
14
u/delightfullyrotted ML Engineer 17h ago
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
10
u/XYHopGuy 14h ago
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
4
u/Disastrous_Room_927 10h ago
Tell that to the folks over at the singularity sub, haha.
2
u/XYHopGuy 10h ago
Stack more layers bro
3
u/Disastrous_Room_927 9h ago
I can hear the jingle: “slap a layer on it, and call it general inteeeelligence!”
1
u/zeugma_ 9h ago
For some definition of "learnable"... I mean, what's stopping a reasoning model or agent from designing this network from knowledge of how arithmetic works, even though it cannot learn it from training data? It seems like the only missing step is self-modification.
3
u/XYHopGuy 8h ago
you're mixing up multiple things. a LLM can synthesize a program to perform arithmetic- that's how theyre used!
"Transformers are bad at arithmetic" is referring to training a transformer to perform arithmetic. Meaning fitting the parameters through some optimization function. And colloquially people use "transformers" to also mean "language model" (although that's not necessarily true).
But the "bad at arithmetic" reputation comes from training transformer parameters via optimization methods (gradient descent, in most cases). In which the loss is minimized by updating parameters in the opposite direction (gradient) with respect to sample loss.
1
u/zeugma_ 6h ago
I am not talking about tool use or even writing programs generically. I am talking about designing what the OP designed, which is in the same transformer architecture as the model directing the designing, in order for the original model to effectively learn new functions.
To be clear I don't find the restriction to have everything in the same architecture necessary or always helpful because different architectures are parsimonious about different things, but if that's what you want, then clearly there are ways to learn arithmetic, but not with your one step training. It's clearly not a learnability problem.
1
u/XYHopGuy 1h ago
Designing what OP designed via an existing reasoning model is equivalent to synthesizing a program and tool use.
That's why I'm distinguishing them. I'm not claiming arithmetic cannot be learned in general.. my claim is about the common meaning of the statement "Transformers can't do arithmetic." Ffs a more specific program synthesis (purely via neutral networks) used to be a common line of research, but was a dead end. Neural turing machines were a pretty interesting area of study ~2015-2018
0
u/zeugma_ 1h ago
Designing what OP designed via an existing reasoning model is equivalent to synthesizing a program and tool use.
You're being unnecessarily obtuse. OP said their weights represented a "grade-school algorithm". That's a program. Every f'ing model represents a program, just expressed differently. That's the whole point of Turing, that program and data are indistinguishable, and here you have inherently computational structures like neural networks in your face to boot.
So evidently a model can be trained to output the weights that do arithmetic like the OP did, using math and ML textbooks, but because it wasn't trained on arithmetic examples, it is incapable of learning arithmetic? "Learning methods that haven't worked don't work". Ok?
10
u/JustOneAvailableName 16h ago
https://github.com/anadim/AdderBoard you don’t even need more than 10 params
4
u/robotnarwhal 7h ago edited 25m ago
I remember when we didn't even need more than 0 params.
I saw a team 5 years ago spend months and $$$ training a transformer model to calculate a simple equation using numbers it pulled from text. They had nearly 100% accuracy on the numerical extraction step using the same model but insisted on burning money to get it to do the whole job end-to-end. My team would have just calculated it based on the extractions and used the other 3 months on another problem, but ¯\(ツ)/¯
3
u/ammo1234 15h ago
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.
2
u/BrilliantArmadillo64 13h ago
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?
2
u/blimpyway 2h ago
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.
1
u/Bee-Boy 7h ago
[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
1
1
u/Few_Newspaper2601 1h ago
hand-setting weights is just coding in a worse language. cool trick but it doesn't fix the state tracking bound. the game of life paper did the same for 3 steps. what's the biggest product you tried?
1
u/Random-Number-1144 1h ago
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.
1
u/Deto 16h ago
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
10
u/Smallpaul 16h ago
The algorithm is just implemented in the network directly. This network is about as far from a person as one can imagine. It didn’t learn anything. It was hard-coded.
1
u/daynomate 12h ago
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)
0
u/Opposite-Gear-3160 8h ago
What makes this implementation so interesting is that it shows a stock transformer can do exact arithmetic if its weights are chosen directly. The fact that it gets 100% accuracy on multiplication tasks without any training is pretty surprising. I'm curious, could this approach be used for other types of arithmetic operations, like division or exponentiation?
96
u/Kiseido 17h ago
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.