Research

Deterministic Inference for LLMs

A Statistical Perspective on Reproducibility

Type
Research
Reading time
5 min
Tools
Python, PyTorch, CUDA, LLM Concurrency
Report
This page is the report.
Question
Why does the same prompt, at temperature zero, still give different answers?
Answer
Not rounding alone. Kernels change their reduction strategy with batch size, and batch size moves with server load, so it acts like a hidden confounder.
Evidence
In a published test, 1,000 runs of one prompt gave 80 distinct completions. Batch-invariant kernels brought that to 1, at roughly a 60% slowdown.
Why it matters
RLHF needs it: inference noise inflates the KL estimate in PPO. Audits and regulation need it too.

What it shows

  • Explains run-to-run variance as batch size acting like a hidden confounder
  • Walks through RMSNorm, matmul and attention as sources of variance
  • Shows why PPO and RLHF need zero-variance inference

Caveats

  • Builds on published benchmarks (Qwen 235B and 8B) rather than new experiments

Status

Research note

A 5-minute read

Die, rolled 1,000 times

Ask a model the same question at temperature zero and you can still get different answers. The usual suspect is floating-point rounding; the real one is batch size, which moves with server load and changes how GPU kernels add numbers up, so it acts like a hidden confounder. In one published test a thousand runs of a single prompt gave eighty distinct completions, and batch-invariant kernels brought that to one at roughly a sixty percent slowdown. It matters wherever answers must repeat: in RLHF, where inference noise inflates PPO's KL estimate, and in audits.

At a glance

1000
Trials per prompt
80 → 1
Distinct completions, before vs. after
~60%
Deterministic slowdown

Abstract

Large language models (LLMs) exhibit surprising non-determinism: identical prompts, under supposedly deterministic configurations (temperature=0, greedy decoding), can still produce divergent outputs. This undermines reproducibility, a cornerstone of both science and engineering. While the common explanation points to floating-point rounding and GPU concurrency, we argue that nondeterminism primarily arises from batch-size-dependent kernel numerics, which change the effective sampling distribution of model outputs. By reframing the issue statistically, we show how nondeterministic inference can be understood as uncontrolled variance injected into the system. We then describe engineering methods to achieve batch-invariant kernels and quantify the trade-offs. Finally, we argue that deterministic inference enables more rigorous evaluation and enables truly on-policy reinforcement learning.

Keywords: Large language models, nondeterminism, deterministic inference, batch size, kernel numerics, reproducibility

In statistics, reproducibility can be compared to controlling variance. When a system outputs different results from the same inputs, we can think of its variance as non-zero even under fixed conditions. For LLM inference, the "noise" is not intentional randomness (like temperature > 0) but uncontrolled variance introduced by hardware implementation details.

This implies that instead of sampling from a fixed distribution P(y|x), the system effectively samples from a family of slightly different distributions depending on batch size, kernel tiling, or server load. From the user’s perspective, the model’s conditional distribution is ill-defined. This is equivalent to adding hidden nuisance parameters into the sampling process.

Floating-Point Arithmetic: Bias and Variance

Floating-point non-associativity is the seed of divergence. The IEEE 754 standard defines floating-point addition as non-associative, meaning (a + b) + c ≠ a + (b + c) due to rounding errors. The operation order can alter rounding, producing multiple possible outcomes. Two clarifications help frame this statistically:

Bias: The effect introduces bias relative to exact arithmetic (since finite-precision results systematically deviate).

Variance: However, when the order of operations changes nondeterministically across runs, it adds variance on top of that bias.

Thus, nondeterminism is not just about inaccuracy, but about run-to-run variance. Crucially, in many GPU kernels, the operation order is actually fixed, making variance zero for repeated runs under identical kernel configurations. So where does the variance re-enter?

Batch Invariance: Controlling Nuisance Variables

Inference servers batch requests dynamically. Suppose kernel output K(X, W, B) depends not only on the input X and model parameters W, but also on the batch configuration B (number of requests, tiling strategy). If B varies unpredictably with server load, then the marginal distribution P(y|x) = ∑ P(y|x, b)P(b) is effectively a mixture distribution over many batch-dependent sub-distributions. This mixture is what users experience as nondeterminism.

Batch size B behaves like an unobserved confounder: its random fluctuations induce spurious variance in outputs. Achieving batch invariance is equivalent to conditioning away this nuisance variable, enforcing P(y | x, b1) = P(y | x, b2).

Case Studies: Kernels as Sources of Variance

RMS Normalization

RMSNorm computes a mean-square reduction across features. Parallel implementations typically change reduction strategy (e.g., from thread-local to warp-shuffle reductions) at small vs. large batch sizes. The estimator used to calculate the mean-square changes its form depending on B, yielding different rounding noise profiles. By fixing the reduction strategy, we hold the estimator constant and remove batch-induced variance.

Matrix Multiplication

Matrix multiply is a summation estimator over dot products. Adaptive kernels (using different tile sizes or split-K strategies) correspond to switching between different unbiased estimators with different finite-precision variances. If the estimator choice depends on batch size, then variance is injected stochastically. Fixing a single estimator across shapes stabilizes results, even if not variance-optimal for each shape.

Attention Mechanisms

Attention reduces across both sequence and feature dimensions. Optimizations like chunked prefill or KV-cache splitting are equivalent to changing the decomposition of the estimator for the softmax denominator. This is like switching between stratified sampling schemes, unbiased in expectation but with different variance characteristics. When which scheme is chosen depends on server load, the effective sampling distribution for tokens drifts nondeterministically. Batch-invariant attention kernels eliminate this uncontrolled design switching.

Measuring Nondeterminism: Empirical Variance

To quantify nondeterminism, consider repeated completions of the same prompt at temperature 0. If inference were deterministic, the empirical distribution would have zero variance, every trial identical.

In practice, one study with a Qwen-235B model produced 80 distinct completions across 1000 trials. Divergence occurred at token 103, with completions splitting between "Queens, New York" and "New York City". From a statistical perspective, the system induced an artificial branching in the probability distribution. When batch-invariant kernels were enabled, variance collapsed to zero: all 1000 completions were identical, as theoretically expected.

Performance Tradeoffs: Variance vs. Efficiency

One often trades variance for bias or efficiency. Similarly, deterministic inference trades efficiency for reduced variance. Benchmarks with Qwen-8B showed:

ConfigurationTime
Default vLLM26s
Deterministic (unoptimized)55s
Deterministic (tuned)42s

This ~60% slowdown is non-negligible but far from catastrophic. More importantly, it yields a zero-variance system where reproducibility is guaranteed.

Implications for Reinforcement Learning

Deterministic inference is not merely a debugging convenience; it is mathematically essential for Reinforcement Learning from Human Feedback (RLHF), particularly for algorithms like Proximal Policy Optimization (PPO).

RLHF relies on the Kullback-Leibler (KL) divergence to constrain the updating policy πθ so it does not drift excessively from a reference policy πref. The KL divergence DKL measures the amount of information lost when one distribution approximates another, defined formally as:

DKL(πθ || πref) = ∑ πθ(x) log( πθ(x) / πref(x) )

With nondeterministic inference, even temperature=0 rollouts do not correspond exactly to the training distribution. Inference randomness introduces a noise term ε into the log-probabilities log πθ(x). Because the KL term involves the ratio of probabilities, this noise is amplified, effectively increasing the variance of the KL estimator. The optimizer cannot distinguish between "good" policy exploration and "bad" hardware noise.

Experiments have shown that without deterministic correction, the estimated divergence grows artificially, causing the penalty coefficient β to fluctuate wildly and leading to reward collapse. However, with deterministic inference, the hardware-induced variance is removed (Var(ε)=0), ensuring that DKL reflects only true policy changes. This allows for unbiased gradient estimates and enables true on-policy reinforcement learning.

Conclusion

Nondeterminism in LLM inference is not a mysterious inevitability of floating-point math. It is a statistical variance problem caused by kernel implementations that alter reduction strategies with batch size. By enforcing batch invariance, we remove this hidden source of variance, enabling deterministic inference.

While the primary discussion has focused on reproducibility in research, the implications of deterministic inference extend into critical application areas where auditability is mandatory. With careful kernel engineering, it is achievable, transforming LLMs from stochastic black boxes into reliable infrastructure.

Tools

Python, PyTorch, CUDA, LLM Concurrency

Works cited

  1. [1]Improving Reproducibility in Machine Learning Research (NeurIPS 2019)
  2. [2]Defeating Nondeterminism in LLM Inference - Thinking Machines Lab
  3. [3]Proximal Policy Optimization - Wikipedia
  4. [4]Approximating KL Divergence - John Schulman
  5. [5]vLLM: Easy, Fast, and Cheap LLM Serving with PagedAttention
  6. [6]opML: Optimistic Machine Learning on Blockchain - arXiv

Keeper of this file

Xerox

Looks exactly like every other penguin. Prefers it that way.

← Poke it