{"slug": "same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes", "title": "Same model, same prompt, different answer: where LLM nondeterminism really comes from", "summary": "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.", "body_md": "*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.*\n\nSet 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.\n\nSo where does the difference come from? Not from randomness. It comes from **arithmetic order**, and once you see it, it shows up everywhere.\n\nIn real numbers, (a + b) + c = a + (b + c). In floating point it isn't guaranteed:\n\n```\n>>> (0.1 + 0.2) + 0.3\n0.6000000000000001\n>>> 0.1 + (0.2 + 0.3)\n0.6\n```\n\nEvery 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.\n\nHere are 200,000 random fp32 numbers summed three ways:\n\n| Order | Result | \n|---|---|\n| Left to right | 26.134367 | \n| Right to left | 26.134922 | \n| Pairwise (NumPy) | 26.135086 | \n| Exact (fp64) | 26.135116 | \n\nSame numbers, three different answers. None of them is \"wrong\"; they're all legal fp32 results. They're just different.\n\nYou 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.\n\nThe 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.\n\n| Batch the row was computed in | Bit-identical to batch of 1? | Output values that differ (of 4096) | \n|---|---|---|\n| 1, 2, 3 | yes | 0 | \n| 8, 16, 32, 64 | **no** | **3,993** | \n\nSame 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.\n\nThis 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.\n\nModels 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.\n\nThat 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.\n\nSo: tiny arithmetic difference → rarely visible → occasionally flips one token → the rest of the answer is different.\n\nOnce you think in terms of \"who decides the summation order\", the sources line up:\n\nNote 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.\n\nIf outputs only matched to within a tolerance, you might shrug. Three places where you can't:\n\nYes, 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.\n\nThe 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.\n\nFurther reading: Horace He and Thinking Machines Lab, *Defeating Nondeterminism in LLM Inference* (2025), on batch invariance in GPU kernels.\n\n``` python\nimport numpy as np, torch\n\n# 1. order of summation\nrng = np.random.default_rng(0)\nx = rng.standard_normal(200_000).astype(np.float32)\nfwd = np.float32(0)\nfor v in x: fwd = np.float32(fwd + v)\nrev = np.float32(0)\nfor v in x[::-1]: rev = np.float32(rev + v)\nprint(fwd, rev, np.sum(x, dtype=np.float32), np.sum(x.astype(np.float64)))\n\n# 2. batch size changes the bits of one row\ntorch.manual_seed(0)\nW = torch.randn(4096, 4096); X = torch.randn(64, 4096)\none = X[:1] @ W\nfor bs in (1, 2, 3, 8, 16, 32, 64):\n    row = (X[:bs] @ W)[:1]\n    print(bs, torch.equal(row, one), int((row != one).sum()))\n\n# 3. after bf16 rounding\nfull = (X @ W)[:1]\nprint(int((one.bfloat16() != full.bfloat16()).sum()))\n```\n\nResults depend on your CPU and PyTorch build; that's the point. Run it and see where *your* machine switches kernels.", "url": "https://wpnews.pro/news/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes", "canonical_source": "https://dev.to/vahana/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes-from-480m", "published_at": "2026-11-29 16:34:27+00:00", "updated_at": "2026-11-29 16:45:49.646684+00:00", "lang": "en", "topics": ["large-language-models", "machine-learning", "ai-infrastructure", "mlops"], "entities": ["PyTorch", "NumPy"], "also_reported_by": [], "takeaway": "Batching a row with 8 or more others changed 3,993 of 4,096 outputs in the last bits, and 2 of those survived bf16 rounding — enough to flip an argmax token and diverge a generation.", "recommendation": {"advice": "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.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes", "markdown": "https://wpnews.pro/news/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes.md", "text": "https://wpnews.pro/news/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes.txt", "jsonld": "https://wpnews.pro/news/same-model-same-prompt-different-answer-where-llm-nondeterminism-really-comes.jsonld"}}