Numerical repairs cut errors in probability comparisons
Across tested probability comparisons, numerical repairs removed over 97% of significant errors from conventional formulas while working at the same precision.

Our read
- Studied:
- Preprint: method
Editor of record: Boris Djordjevic · Paperfoot
The preprint introduced numerical repairs for Kullback–Leibler divergence, a measure of how probability distributions differ, tested across 18 formula families and PyTorch’s built-in divergence function. The evaluation used 8.5 million inputs with certified reference answers on central processors and graphics chips.
Conventional formulas often gave inaccurate values when distributions were close, sometimes returning negative answers for a quantity that cannot be negative. Simply using higher precision left substantial errors. The repairs combined stable formulas, arithmetic that compensates for rounding errors, and explicit formulas for gradients—how the answer changes as inputs change. They removed over 97% of significant errors at the precision being used. Stricter repairs eliminated significant errors on 7.2 million scored formula-based samples and correctly rounded all 567 million scored components of PyTorch’s built-in function.
Why it matters
These calculations help guide model training and other decisions. The tested errors affected parameter fitting and several model-analysis tasks; with repaired code, those effects all but vanished.
Caveats
The results cover the selected formula families, function and scored inputs, not every possible probability comparison. The work is a preprint, not yet peer-reviewed.
From the paper
- Limitation
Error rates are conditional on a sampling design that oversamples small divergences and stress regions on purpose.
Quoted word for word from the full text on arXiv.
In this paper
numerical accuracy of Kullback–Leibler divergence computation
- Methods
- Rewrite (this paper)Ours (this paper)logits path (this paper)Official (baseline)torch.distributions.kl_divergence (evaluated)torch.nn.functional.kl_div (evaluated)AdamSGDQwen2.5-1.5B-InstructGPT-2 smallGPT-2 medium
- Datasets
- WikiText-2 (evaluated)
- Links
- Code
Results
- Rewrite, significant error reduction, working format: over 97%
Stable closed-form decompositions, compensated arithmetic, and analytic gradients remove over 97% of significant errors in the working format.
Abstract
- Ours, correctly rounded components, forward scored components, 942,200 batches: all 83,899,496; Ours, correctly rounded components, backward scored components, 942,200 batches: 483,279,156
For interface B (942,200 batches), Ours is correctly rounded on all 83,899,496 forward and 483,279,156 backward scored components, against 52.5% and 74.9% for Official, with no regression.
Results on the frozen data.
- Official, top-1 disagreement, Adam, FP16, largest vocabulary: 97.4%; Official, final true KL, Adam, FP16, largest vocabulary: 3.6 nats
The largest vocabulary is worse than the smallest in every Adam comparison, most strongly in FP16, where the disagreement reaches 97.4% and the final true KL 3.6 nats.
Result.
- Rewrite, cost relative to official call, GPU, five of seven timed families: 2.4–5.9 ×
The price is arithmetic rather than precision, at 2.4–5.9 × the official call on a GPU for five of seven timed families (Section C.4).
Results on the frozen data.
What it shows
- Standard closed-form KL formulas suffer severe numerical inaccuracies near small divergences, with over 85% of FP32 values showing at least 1% relative error near KL 10^(-7).
Among scalar families in ordinary parameter ranges, over 85% of FP32 values have at least 1% relative error near KL 10^(-7) (FP16: 10^(-3)), and about 30% become negative at smaller divergences.
Abstract
- Evaluating the existing formulas with promoted precision fails to resolve errors for 22 of 23 configurations, leaving 7-51% of FP32 values significantly wrong.
More precision is not the cure: evaluated one precision higher, the same formulas still leave 7–51% of FP32 values significantly wrong for 22 of 23 configurations.
Abstract
- The strict repair method completely eliminates significant errors across 7.2 million scored closed-form samples and correctly rounds all 567 million scored F.kl_div components.
Strict repairs eliminate significant errors on 7.2 million scored closed-form samples and correctly round all 567 million scored F.kl_div components.
Abstract
- Reported error rates depend on a sampling methodology that deliberately oversamples small divergences and stress parameter regions.
Error rates are conditional on a sampling design that oversamples small divergences and stress regions on purpose.
Limitations.
Quoted word for word from the full text on arXiv.
The paper
The KL Divergence Is Never Negative, Except in PyTorch
arXiv, Nov 23, 2026, Open access, Preprint, not yet peer-reviewed
doi.org/10.48550/arxiv.2611.30835
arXiv:2611.30835v1 in cs.LG
Cite as BibTeX
@misc{yang2026divergence,
title = {The KL Divergence Is Never Negative, Except in PyTorch},
author = {Yang, Zhen and Tang, Wei and Chen, Chu and Qian, Rui and Chen, Yixuan and Yang, Fan and Cui, Kangning},
year = {2026},
eprint = {2611.30835},
archivePrefix = {arXiv},
primaryClass = {cs.LG},
doi = {10.48550/arxiv.2611.30835},
url = {https://arxiv.org/abs/2611.30835}
}