TL;DR
Greedy decoding in LLMs is often assumed to be deterministic, but batching prompts during inference introduces floating-point noise that can "flip" the chosen token. This paper demonstrates that we can reduce these flips by ~35% during training by using a margin regularization (hinge loss) to widen the gap between the top two token candidates, albeit at a small cost to task accuracy.
What Problem Does This Paper Address?
In theory, setting an LLM's temperature to zero should make its output perfectly reproducible. In practice, if you run the same prompt alone versus as part of a batch, you might get different results.
This happens because floating-point addition is non-associative: $(a + b) + c$ does not always equal $a + (b + c)$. High-performance GPU kernels change the order of operations depending on the "shape" of the data (e.g., batch size or sequence length). These tiny numerical differences—often in the last representable bits—can shift the logits just enough to change which token wins the "argmax" race if the top two choices are very close. This "non-determinism" breaks regression suites, LLM-as-judge workflows, and distillation loops.
While current solutions (like batch-invariant operators) work at inference time, they often come with a 2x slowdown. The authors ask: Can we instead train the model to be more "decisive" so it is naturally resistant to this noise?
Methods
The authors propose a training-time intervention called Margin Regularisation.
- Characterization: They first measured the "noise" ($\delta$) produced by batching across different precisions (fp32, fp16, bf16). They found that the noise scales exactly with the number of mantissa bits in the format (e.g., bf16 has 8x the noise of fp16).
- Hinge Loss: They added a penalty term to the standard cross-entropy loss. This term penalizes the model if the gap between the top-1 and top-2 logits is smaller than a specific margin $m$: $$L = L_{CE} + \lambda \cdot \max(0, m - (logit_1 - logit_2))$$
- Calibration: Rather than tuning $m$ as a hyperparameter, they set it to twice the 99.9th percentile of the measured hardware noise ($2 \times Q_{0.999}$). This ensures the model is pushed to create a gap wide enough to "swallow" the hardware-induced perturbations.
Datasets
- WikiText-2: Used for initial characterization and profiling of the logit perturbations.
- GSM8K: The primary benchmark for evaluating reasoning and determinism.
- MBPP (Mostly Basic Python Problems): Used as an out-of-domain check for code generation.
- CodeParrot: Used for additional cross-corpus transfer testing.
Key Insights and Ideas
- The Precision Rule: The scale of batch-induced noise is a hardware constant. It doesn't depend on model confidence or sequence position; it depends almost entirely on the numerical precision (float16 vs. bfloat16).
- Accidental Determinism: The authors discovered that certain tensor shapes (like a sequence length of 512 on specific CUDA kernels) are "accidentally" batch-invariant. A researcher testing only at that length might wrongly conclude their model is perfectly deterministic.
- Decisiveness vs. Correctness: The hinge loss doesn't care which token is correct; it only cares that the model chooses one decisively. However, they found that hinging on the model's own top choice works better than hinging on the "correct" label, as the latter can actually create new near-ties.
- The Worst-Case is Fixed: Margin training makes flips less frequent, but it doesn't move the "noise ceiling." The maximum possible perturbation remains the same because it is a property of the hardware, not the weights.
Key Results
- Flip Reduction: On the GSM8K dataset, the method led to a 35% reduction in token flips.
- Sequence Divergence: The percentage of generated sequences that differed between batch sizes dropped from 53.8% to 38.1%.
- Accuracy Trade-off: This increased determinism comes at a cost of 1.3 to 2.9 accuracy points on GSM8K. The authors note that accuracy decreases monotonically as the margin increases.
- Efficiency Peak: Across multiple model families (Qwen, Llama, OPT), the "efficiency" (determinism gained per unit of perplexity lost) consistently peaked when the margin was set to exactly twice the measured noise quantile.
Limitations and Future Work
- Model Scale: Due to hardware constraints (22GB VRAM), full fine-tuning was limited to models under 0.5B parameters, though the authors verified that the noise characterization holds true for 7B models.
- Hardware Specificity: The results are primarily based on NVIDIA Turing and Ada architectures. The authors found that Apple's Metal (MPS) was actually bit-identical across batch sizes for the same operations, suggesting this is an implementation choice in CUDA/cuBLAS rather than an inevitability of math.
- Domain Sensitivity: The benefits of margin training are largely in-domain. A model trained for determinism on GSM8K does not necessarily show the same improvements when switched to a coding task (MBPP).
- Unexplained Failures: Some model families (like GPT-2 and Pythia) did not benefit from the technique for reasons the authors could not fully explain, marking this as a key area for future research.
Summary updated