cd /news/machine-learning/the-kl-divergence-is-never-negative-… · home › topics › machine-learning › article
[ARTICLE · art-175508] src=arxiv.org ↗ pub= topic=machine-learning verified=true sentiment=↓ negative

The KL Divergence Is Never Negative, Except in PyTorch

A paper submitted to arXiv on 23 November 2026 reports that PyTorch's KL divergence implementations produce widespread numerical errors, including negative values, across 8.5 million inputs with certified references spanning 18 closed-form KL families and F.kl_div on CPU and GPU. 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; evaluating one precision higher still leaves 7–51% of FP32 values significantly wrong for 22 of 23 configurations. The authors report that stable closed-form decompositions, compensated arithmetic and analytic gradients remove over 97% of significant errors in the working format, eliminating significant errors on 7.2 million scored closed-form samples and correctly rounding all 567 million scored F.kl_div components, with code and data released on GitHub.

recommendation

Replace conventional KL formulas with stable closed-form decompositions, compensated arithmetic and analytic gradients when comparing nearby distributions; the authors' repairs removed over 97% of significant value and gradient errors.

more in playbook: Evals and testing for LLM apps
read2 min views1 publishedNov 24, 2026
The KL Divergence Is Never Negative, Except in PyTorch
Image: source
  [Submitted on 23 Nov 2026]


[View PDF](https://arxiv.org/pdf/2611.30835)

[HTML (experimental)](https://arxiv.org/html/2611.30835v1)

Abstract:A Kullback--Leibler (KL) divergence is never negative, yet PyTorch users have reported negative values for years, and the standard advice is to clamp them at zero. The sign is only the visible part of a silent failure. Many machine-learning applications compare nearby distributions, where conventional KL formulas lose accuracy. On 8.5 million inputs with certified references across 18 closed-form KL families and F.kl_div, we find widespread errors in values and gradients on CPU and GPU. 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. 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. Stable closed-form decompositions, compensated arithmetic, and analytic gradients remove over 97% of significant errors in the working format. Strict repairs eliminate significant errors on 7.2 million scored closed-form samples and correctly round all 567 million scored F.kl_div components. The errors affect parameter fitting, Bayesian decisions, identical-start distillation (up to 97% top-1 disagreement), KL-based layer rankings in a quantized LLM, and 19--59% of GPT-2 circuit edge scores. With the repaired code they all but vanish. Code and data: this https URL

Additional Features

References & Citations

...

Bibliographic Explorer

(What is the Explorer?) Connected Papers

(What is Connected Papers?) Litmaps

(What is Litmaps?) scite Smart Citations

(What are Smart Citations?) alphaXiv

(What is alphaXiv?) CatalyzeX Code Finder for Papers

(What is CatalyzeX?) DagsHub

(What is DagsHub?) Gotit.pub

(What is GotitPub?) Hugging Face

(What is Huggingface?) ScienceCast

(What is ScienceCast?) Influence Flower

(What are Influence Flowers?) CORE Recommender

(What is CORE?) IArxiv Recommender

(What is IArxiv?) arXivLabs is a framework that allows collaborators to develop and share new arXiv features directly on our website.

Both individuals and organizations that work with arXivLabs have embraced and accepted our values of openness, community, excellence, and user data privacy. arXiv is committed to these values and only works with partners that adhere to them.

Have an idea for a project that will add value for arXiv's community? Learn more about arXivLabs.

── more in #machine-learning 4 stories · sorted by recency
── more on @pytorch 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/the-kl-divergence-is…] indexed:0 read:2min 2026-11-24 · —