What this is about
Ask a processor and a graphics card to compute the same thing, and they may answer with slightly different numbers. Both are working correctly. The difference is in the last digits, and it comes from how computers store decimals and how they add them up.
This article explains why that happens in plain words, and then tells how we built one function that gives the exact same bits on both chips.
Why it matters
Most of the time, a difference in the 7th digit does not matter. But sometimes it does. If you want to check a graphics card's answer by running the same maths on the processor, any difference makes the check useless. You cannot tell a real fault from harmless rounding.
And once a tiny difference enters a long chain of calculations, it can grow. In a language model, the same prompt can then produce a different answer on a different chip.
How it works
Computers round. A computer stores a decimal number with a limited number of binary digits. Most decimal fractions do not fit exactly. In Python, 0.1 + 0.2 prints 0.30000000000000004, not 0.3. That is not a bug. The stored values are close to 0.1 and 0.2, and their sum is close to 0.3 but not exactly it.
The order of a sum matters. Here is a Python example:
(0.1 + 0.2) + 0.3 gives 0.6000000000000001
0.1 + (0.2 + 0.3) gives 0.6
Same three numbers, a different order, a different last digit. On a processor, a sum usually runs in a fixed order. On a graphics card, thousands of threads add pieces and then combine them in a tree. The grouping changes, so the rounding changes.
Special functions differ by chip. Functions such as exp are not built from plain addition. Each chip or library uses its own recipe, with its own tiny rounding. The recipe on the card's hardware is not published.
A common word here is ulp, which means "unit in the last place". It is the size of the smallest step between two neighbouring numbers. "One ulp off" means the answers are next-door neighbours on the number line.
What we found in our own engine
In our engine, mojo-baro, we use a 32-bit decimal type called Float32. We fed the standard exp function 40,017 inputs from a seeded generator. The processor and the graphics card disagreed on 19,437 of them, which is 49 percent. A claim that both give the same numbers failed at the first softmax, an early step inside the model.
We tried two fixes.
- Copy the card. We modeled the card's
expon the processor. It matched 96.6 percent of results bit for bit, and the rest were one ulp off. The hardware unit is not documented, so that route could never reach 100 percent. We dropped it. - Share one recipe. We wrote one
expin Mojo, calledfexp32, using a fixed recipe. We made sure a multiply and an add are always fused the same way on both sides. Both chips run the same source.
The result: across the whole range, fexp32 is at most 1 ulp from the true value on both chips, and the two chips differ on 0 of 40,017 inputs. On the range we use for softmax, from -30 to 0, the standard function on the card was up to 28 ulp off. So the shared version was also the more accurate one there.
It was not enough to fix one function
A correct function does not make a correct layer. Three more things broke on the first attempt. The order in which a dot product adds its pieces had to match the GPU's. A reduction on the card fuses a multiply with an add, and the processor copy had to do the same. After that, one whole attention layer, 14 stages over 8 steps, matched bit for bit. A planted tiny error of 1.0000001 in one place failed the one stage it touched and no other, so the test could fail.
The match holds only with a special build flag. In the default build, there are still mismatches. We say so in the post and in the claim.
What it cost
The shared exp is slower. In a micro-test, 16 million threads each did 16 dependent exp calls. The standard one took 0.168 ms. Ours took 0.609 ms, 3.6 times as long. By arithmetic, at 8,000 tokens of context that costs about 14 microseconds per token, around 0.1 percent of a 10 to 20 ms token. We have not measured it inside real decoding, so we do not quote it as a result.
Words you may not know
- Float32: a way to store a decimal number in 32 binary digits.
- Ulp: the gap between two neighbouring numbers a computer can store.
- Bit for bit: identical down to every stored one and zero.
- Softmax: a step in a language model that turns scores into odds.
- Fused multiply-add: one operation that multiplies and adds with a single rounding.
- Oracle: a trusted reference you compare with.
Try it
- Run
0.1 + 0.2and the two orders above in Python and see the last digits. - Open the mojo-libs table and look at
mojo-pyformat, which formats numbers exactly as Python does. - Read the mojo-baro page to see where the engine uses these functions.
Read next
- Same bits on the CPU and the GPU has every number, the failed mutants build and the open gaps.

Comments
No comments yet.
Log in to comment.