Non-determinism and batch invariance: why temperature 0 is not repeatable
Why the same prompt at temperature 0 can give different answers from a serving engine, traced to its real cause: kernels that change how they add numbers when the batch changes. A runnable BF16 model shows how small differences flip greedy tokens and shift log-probabilities, what that breaks (evals, regression tests, RL), how batch-invariant kernels and deterministic modes fix it, what they cost, and how to test your own stack.
By Sandip Maity, Applied AI Engineer
Send the same prompt to the same model twice at temperature 0 and you expect the same answer. Through most serving engines you often get two different ones. This is not a bug in sampling, and it is not mainly about random seeds or GPU quirks. It comes from how fast kernels add numbers, and it matters for evaluations, regression tests, debugging and reinforcement learning.
You need Python 3 and numpy.
#Floating-point addition depends on order
Computers round after every addition, so the grouping changes the answer:
import numpy as np
# 1. Floating-point addition is not associative
a, b, c = np.float32(1e8), np.float32(-1e8), np.float32(1.0)
print("1. (a + b) + c =", (a + b) + c, " a + (b + c) =", a + (b + c))
rng = np.random.default_rng(0)
x = rng.normal(size=4096).astype(np.float32)
def chunked_sum(v, k):
return np.sum([np.sum(p, dtype=np.float32) for p in np.array_split(v, k)], dtype=np.float32)
s1 = np.float32(0)
for v in x: # strict left-to-right
s1 = np.float32(s1 + v)
print(f" sum of 4,096 numbers: left-to-right {s1:.7f}, 4 chunks {chunked_sum(x, 4):.7f}, 16 chunks {chunked_sum(x, 16):.7f}")1. (a + b) + c = 1.0 a + (b + c) = 0.0
sum of 4,096 numbers: left-to-right -66.0645828, 4 chunks -66.0645981, 16 chunks -66.0646057Every one of those sums is "correct". They differ in the last digits because the additions happened in a different order. A GPU does billions of such sums per token, inside matrix multiplies, normalisation layers and attention.
#Why the order changes in a server
A single kernel run twice on the same inputs usually is deterministic. The problem is that serving kernels pick different strategies for different batch sizes, and in a shared server your request's batch depends on everyone else's traffic:
- Matrix multiplies split the inner dimension across thread blocks when the batch is small (split-K), so the GPU has enough work. At larger batches they do not. Different splits, different addition order.
- Attention splits a long cache into chunks at small batch (split-KV decoding), with the number of chunks chosen from batch size and length.
- Normalisation kernels may parallelise one row across threads when there are few rows.
- Chunked prefill and prefix caching change how a prompt is processed: in one pass, in chunks, or partly from cache.
- Different kernels altogether: engines pick from several matmul and attention implementations by shape, and CUDA graphs are captured per batch size.
Atomic additions (where threads add into one place in whatever order they arrive) are another source, but most forward-pass kernels avoid them. Research by Thinking Machines in 2025 showed that batch dependence, not run-to-run randomness, explains most of the variation people see in serving. Their phrase for the fix stuck: batch invariance.
Dot product of 4,096 terms, summed in 8 parts:
Logit for “Paris”
12.34567
Greedy token
Paris
Your request runs alone
Batch 1: the matmul kernel splits each dot product 8 ways to keep the GPU busy, then adds the 8 partial sums.
Illustrative digits. The real effect: kernels choose how to split sums by batch size, and floating-point addition depends on order.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- From tiny differences to different answers
- Who gets hurt
- Fixing it: batch-invariant kernels
- Test your own stack
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- What is the main reason an inference server gives different outputs for the same prompt at temperature 0?
- In the lesson's BF16 model, 0.6% of rows changed their greedy token between two kernel configurations. Why does that matter for a 1,000-token answer?
- What makes a kernel batch-invariant?
- Why do RL systems care about log-probability differences of 0.01?
Preparing for an interview?
- Batching and continuous batching interview questions (5 questions with model answers)