cd /news/large-language-models/same-model-same-prompt-different-ans… · home › topics › large-language-models › article
[ARTICLE · art-178192] src=dev.to ↗ pub= topic=large-language-models verified=true sentiment=· neutral

Same model, same prompt, different answer: where LLM nondeterminism really comes from

A developer's CPU-based PyTorch experiment shows that greedy LLM decoding is not bit-reproducible because floating-point summation order changes with batch shape: the same row multiplied by a 4096×4096 weight matrix differed in 3,993 of 4,096 outputs once batched with 8 or more rows, though only 2 values still differed after rounding to bf16. The writeup traces the divergence to library kernel selection and argues that fixing a single summation order across batch sizes, kernels and devices would make identical requests return identical bits.

recommendation

Pin one summation order across batch sizes, kernels and devices if you need reproducible LLM outputs, since batching alone altered 97% of output values in the source's test.

more in playbook: Inference and self-hosting
by read4 min views8 publishedNov 29, 2026

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.

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.

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.

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.

Once you think in terms of "who decides the summation order", the sources line up:

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.

If outputs only matched to within a tolerance, you might shrug. Three places where you can't:

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.

import numpy as np, torch

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)))

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()))

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.

── more in #large-language-models 4 stories · sorted by recency
── more on @pytorch 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/same-model-same-prom…] indexed:0 read:4min 2026-11-29 · —