Every number below comes from a script I ran on my laptop (PyTorch 2.10, CPU, 8 threads). The script is at the end, so you can run it on yours.
Set temperature to 0. Send the same prompt to the same model twice. You'd expect the same answer twice. Often you don't get it, and not because of sampling. Greedy decoding is deterministic by definition: take the highest-scoring token, every time.
So where does the difference come from? Not from randomness. It comes from arithmetic order, and once you see it, it shows up everywhere.
1. Floating-point addition doesn't care what you learned in school
In real numbers, (a + b) + c = a + (b + c). In floating point it isn't guaranteed:
>>> (0.1 + 0.2) + 0.3
0.6000000000000001
>>> 0.1 + (0.2 + 0.3)
0.6
Every addition rounds to the nearest representable number, and where the rounding happens depends on the order. With three numbers the error is tiny. A transformer layer adds thousands of products for every output value.
Here are 200,000 random fp32 numbers summed three ways:
| Order | Result |
|---|---|
| Left to right | 26.134367 |
| Right to left | 26.134922 |
| Pairwise (NumPy) | 26.135086 |
| Exact (fp64) | 26.135116 |
Same numbers, three different answers. None of them is "wrong"; they're all legal fp32 results. They're just different.
2. Your batch size changes the order
You never choose the order of additions inside a matrix multiply: the library does. And libraries choose differently depending on the shape of the problem, because a different tiling or blocking is faster for a different shape.
The experiment: take one row x, multiply it by a 4096×4096 weight matrix, then do the same row again inside a batch of other rows, and compare only that row's result.
| Batch the row was computed in | Bit-identical to batch of 1? | Output values that differ (of 4096) |
|---|---|---|
| 1, 2, 3 | yes | 0 |
| 8, 16, 32, 64 | no | 3,993 |
Same row, same weights, same machine, same library. Put the row in a batch of 8 and 97% of its outputs change in the last bits (the largest difference was 0.00047). Somewhere between batch 3 and batch 8 the library switched to a different kernel path, and the reduction order changed with it.
This is why an inference server can give a different answer to your request depending on who else is using it at the same moment. Your request gets batched with strangers; the batch size changes the kernel; the kernel changes the order; the order changes the bits.
3. "It's only the last bits", until it isn't
Models usually run in bf16, which keeps only 8 bits of mantissa. Rounding to bf16 hides most of those tiny differences: of the 3,993 differing values above, only 2 still differed after rounding to bf16.
That sounds reassuring. It's actually the dangerous part. The differences don't disappear evenly; they survive exactly where a value sits near a rounding boundary. And a language model makes a hard decision every token: the argmax. When two candidate tokens are close (which happens constantly in long generations), a difference in the last bit of a logit picks a different word. From that token on, the two runs are reading different text, and they diverge completely.
So: tiny arithmetic difference → rarely visible → occasionally flips one token → the rest of the answer is different.
4. The full list of things that change the order
Once you think in terms of "who decides the summation order", the sources line up:
- Batch size and padding. Different shapes, different kernels (shown above).
- Hardware. A different GPU generation or CPU instruction set (AVX2 vs AVX-512 vs ARM NEON) has different vector widths, so the natural way to split a sum changes.
- Library and driver versions. A new cuBLAS, oneDNN or PyTorch release can pick a new kernel for the same shape.
- Thread count. Parallel reductions split work by thread; 8 threads and 14 threads split a sum differently.
- Kernel tricks for speed. Split-K matmuls, atomics in reductions, fused attention kernels that tile the sequence differently for prefill and decode.
- Where the weights live. Offloading systems that compute some layers on the CPU and some on the GPU are mixing two different arithmetic implementations by construction.
Note what's not on the list: true randomness. Run the exact same thing on the exact same setup and you usually do get the same bits. The problem is that "the exact same setup" almost never holds in production.
5. Why anyone should care
If outputs only matched to within a tolerance, you might shrug. Three places where you can't:
- Evaluations. Benchmark scores move between inference backends and batch sizes. If your eval number changes when you change the serving setup, you're measuring the setup, not the model.
- Debugging. "It produced this bad output yesterday" is hard to investigate when you can't reproduce yesterday's output.
- Verification. If a provider says "this answer came from model X with these settings", you can only check that claim by recomputing it. Recomputing only proves something if the bits are supposed to match.
6. Can it be fixed?
Yes, and the fix is conceptually simple: stop letting the shape, the hardware or the library choose the order. Pick one summation order for every dot product, and implement that exact order everywhere the model runs: every batch size, every kernel, every device. Then "the same request gives the same bits" stops being luck and becomes a property you can test.
The cost is that you give up some of the freedom kernels use to go fast. How much that costs, and where it costs nothing at all, is the interesting part. I'll write about that next.
Further reading: Horace He and Thinking Machines Lab, Defeating Nondeterminism in LLM Inference (2025), on batch invariance in GPU kernels.
The script (reproduce every number above)
import numpy as np, torch
# 1. order of summation
rng = np.random.default_rng(0)
x = rng.standard_normal(200_000).astype(np.float32)
fwd = np.float32(0)
for v in x: fwd = np.float32(fwd + v)
rev = np.float32(0)
for v in x[::-1]: rev = np.float32(rev + v)
print(fwd, rev, np.sum(x, dtype=np.float32), np.sum(x.astype(np.float64)))
# 2. batch size changes the bits of one row
torch.manual_seed(0)
W = torch.randn(4096, 4096); X = torch.randn(64, 4096)
one = X[:1] @ W
for bs in (1, 2, 3, 8, 16, 32, 64):
row = (X[:bs] @ W)[:1]
print(bs, torch.equal(row, one), int((row != one).sum()))
# 3. after bf16 rounding
full = (X @ W)[:1]
print(int((one.bfloat16() != full.bfloat16()).sum()))
Results depend on your CPU and PyTorch build; that's the point. Run it and see where your machine switches kernels.
Top comments (0)