{"slug": "the-kl-divergence-is-never-negative-except-in-pytorch", "title": "The KL Divergence Is Never Negative, Except in PyTorch", "summary": "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.", "body_md": "# Computer Science > Machine Learning\n\n  [Submitted on 23 Nov 2026]\n\n# Title:The KL Divergence Is Never Negative, Except in PyTorch\n\n[View PDF](https://arxiv.org/pdf/2611.30835)\n\n[HTML (experimental)](https://arxiv.org/html/2611.30835v1)\n\nAbstract: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](https://github.com/xi-xiaoran/stablekl)\n\n### Additional Features\n\n### References & Citations\n\nLoading...\n\n# Bibliographic and Citation Tools\n\nBibliographic Explorer \n\n*(*[What is the Explorer?](https://info.arxiv.org/labs/showcase.html#arxiv-bibliographic-explorer))\nConnected Papers \n\n*(*[What is Connected Papers?](https://www.connectedpapers.com/about))\nLitmaps \n\n*(*[What is Litmaps?](https://www.litmaps.co/))\nscite Smart Citations \n\n*(*[What are Smart Citations?](https://www.scite.ai/))\n# Code, Data and Media Associated with this Article\n\nalphaXiv \n\n*(*[What is alphaXiv?](https://alphaxiv.org/))\nCatalyzeX Code Finder for Papers \n\n*(*[What is CatalyzeX?](https://www.catalyzex.com))\nDagsHub \n\n*(*[What is DagsHub?](https://dagshub.com/))\nGotit.pub \n\n*(*[What is GotitPub?](http://gotit.pub/faq))\nHugging Face \n\n*(*[What is Huggingface?](https://huggingface.co/huggingface))\nScienceCast \n\n*(*[What is ScienceCast?](https://sciencecast.org/welcome))\n# Demos\n\n# Recommenders and Search Tools\n\nInfluence Flower \n\n*(*[What are Influence Flowers?](https://influencemap.cmlab.dev/))\nCORE Recommender \n\n*(*[What is CORE?](https://core.ac.uk/services/recommender))\nIArxiv Recommender\n\n*(*[What is IArxiv?](https://iarxiv.org/about))\n# arXivLabs: experimental projects with community collaborators\n\narXivLabs is a framework that allows collaborators to develop and share new arXiv features directly on our website.\n\nBoth 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.\n\nHave an idea for a project that will add value for arXiv's community? [**Learn more about arXivLabs**](https://info.arxiv.org/labs/index.html).", "url": "https://wpnews.pro/news/the-kl-divergence-is-never-negative-except-in-pytorch", "canonical_source": "https://arxiv.org/abs/2611.30835", "published_at": "2026-11-24 15:50:22+00:00", "updated_at": "2026-11-24 16:12:18.871555+00:00", "lang": "en", "topics": ["machine-learning", "ai-research", "large-language-models", "ai-tools"], "entities": ["PyTorch", "arXiv", "GPT-2", "F.kl_div", "GitHub"], "also_reported_by": [], "takeaway": "Clamping negative KL values at zero hides a silent failure: over 85% of FP32 scalar-family values carry at least 1% relative error near KL 10^(-7), and stable closed-form decompositions with compensated arithmetic and analytic gradients remove over 97% of significant errors.", "recommendation": {"advice": "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.", "playbook": "evals-testing", "playbook_url": "https://wpnews.pro/playbooks/evals-testing"}, "alternates": {"html": "https://wpnews.pro/news/the-kl-divergence-is-never-negative-except-in-pytorch", "markdown": "https://wpnews.pro/news/the-kl-divergence-is-never-negative-except-in-pytorch.md", "text": "https://wpnews.pro/news/the-kl-divergence-is-never-negative-except-in-pytorch.txt", "jsonld": "https://wpnews.pro/news/the-kl-divergence-is-never-negative-except-in-pytorch.jsonld"}}