Zhidaily AI · Intelligence · Daily
Zhidaily
Issue 26 · November 25, 2026 · Wed
Today's stories
16 stories · 6 companies
Home / November 25, 2026 / PyTorch's KL Divergence Returns Negative Values, Paper Finds
← Back
Paper Models · Meta Score72

PyTorch's KL Divergence Returns Negative Values, Paper Finds

In brief
A new arXiv paper measures PyTorch's KL divergence against certified references and finds widespread value and gradient errors on nearby distributions — including negative results for a quantity that
Hacker News Anon84 · Hacker News · arxiv.org
Nov 24 · 23:50 Read the original ↗
01Summary

An arXiv paper reports that PyTorch's KL divergence (a measure of how far apart two probability distributions are, which can never be negative) produces wrong values and gradients when the two distributions are close. Testing 8.5 million inputs across 18 closed-form KL families and F.kl_div on CPU and GPU, the authors find that in ordinary parameter ranges over 85% of FP32 values carry at least 1% relative error near KL 10^-7, and about 30% turn negative at smaller divergences. Raising precision does not fix it: one precision higher, 22 of 23 configurations still leave 7–51% of FP32 values significantly wrong. Stable closed-form decompositions, compensated arithmetic and analytic gradients remove over 97% of significant errors in the working format, and the repaired code correctly rounds all 567 million scored F.kl_div components. The authors say the errors distort parameter fitting, Bayesian decisions, distillation from identical starting weights (up to 97% top-1 disagreement) and KL-based layer rankings in a quantized LLM; code and data are released, but the fix is not yet in PyTorch itself.

02Why this keeps happening

KL divergence measures how far one probability distribution is from another, and by definition it can never be below zero. PyTorch users have nonetheless reported negative values from `torch.nn.functional.kl_div` for years, with a GitHub issue from June 2022 noting that an earlier fix (#32520) had not stopped the problem. The standard workaround has been to clamp the result at zero, which hides the sign but not the underlying loss of precision. The paper's authors have also published a companion repository, stablekl, with corrected implementations.

03Impact

Anyone using KL divergence for distillation, Bayesian comparison or quantization analysis should not trust small values from stock PyTorch, and clamping at zero hides the problem rather than fixing it. Until the repair lands upstream, teams can adopt the paper's released code or compute KL from log-probabilities with a stable decomposition instead of the naive formula.

04Community Discussion

Commenters found the result plausible rather than shocking: one described a clamp that failed on negative zero because of a macro comparison, and another reported a PyTorch miscompilation they had upstreamed in August that caused 100x relative gradient errors for tensor dimensions not aligned to a large power of two. One commenter suggested numerical issues go unnoticed because ML workloads are often insensitive to them, while another questioned whether arXiv is the right venue for a bug report.