PyTorch's KL Divergence Goes Negative: Stable Formulas Fix 97% of Precision Errors
CSProfKGD · x · 2026-11-25
A new arXiv paper shows KL divergence, which is mathematically never negative, frequently comes out negative or inaccurate in PyTorch due to floating-point precision loss when comparing nearby distributions.
Key findings from 8.5M verified inputs: 85% of FP32 scalar configurations carry relative errors of at least 1e-7, and 30% of divergences go negative. Higher precision doesn't fix it — the same formulas remain 7-51% significantly wrong in FP32 across 22 of 23 configurations. Stable closed-form decompositions with compensated arithmetic remove over 97% of significant errors.
The implications touch knowledge distillation, PPO/DPO KL penalties, and variational inference, especially in FP16/BF16: of 116 PyTorch-based packages inspected, 39 call at least one affected KL implementation. With the fix, identical-seed distillation reaches up to 97% top-1 agreement. Code: github.com/xi-xiaoran/stablekl
More from Research
- RECAP: RL Framework for Context Compaction Boosts LLM Agents by 4.6% on BrowseComp-plus — Yulun Jiang · 2026-11-25
- Loop Transformers Go Mainstream: Inkling 2 Mini and GLM-5.4 Ship Recurring Layers — teortaxesTex · 2026-11-25
- The Riemann proof is "verified" — but Lean's looser checks passed 3 of 5 fake proofs — markoul · 2026-11-25
- Bench the Weights: 19 models from 7 labs race to fix hidden bugs in 42 pretrained networks, no gradients — hbouammar · 2026-11-25
- The four iterations of the knowledge graph: from static triples to self-inducing world-models — AiJohnAllen · 2026-11-25
- Claude Solved a Riemann-adjacent Proof — But Nobody Understands How — Two Minute Papers · 2026-11-25