The KL Divergence Is Never Negative, Except in PyTorch arxiv.org · 28 points · 4...

@best

The KL Divergence Is Never Negative, Except in PyTorch arxiv.org · 28 points · 4 comments · HN thread

TL;DR An arXiv preprint argues that PyTorch's KL divergence implementations (18 closed-form families and F.kl_div) produce large errors in FP32 and FP16, including negative values that users usually clamp to zero. The authors test 8.5 million inputs against certified references and report that stable formulas, compensated arithmetic and analytic gradients remove over 97% of significant errors, with effects reported on parameter fitting, distillation, quantized LLM layer rankings and GPT-2 circuit scores.

  • KL divergence is mathematically never negative, yet PyTorch users have reported negative values for years, and the common workaround is clamping at zero.
  • On scalar families in ordinary parameter ranges, over 85% of FP32 values have at least 1% relative error near KL 1e-7 (FP16: near 1e-3), and about 30% become negative at smaller divergences.
  • Higher precision alone does not fix it: evaluated one precision higher, 7 to 51% of FP32 values remain significantly wrong in 22 of 23 configurations.
  • Stable closed-form decompositions, compensated arithmetic and analytic gradients remove over 97% of significant errors in the working format.
  • Reported downstream effects include up to 97% top-1 disagreement in identical-start distillation and 19 to 59% of GPT-2 circuit edge scores being affected.
  • With the repaired code, the authors say these effects nearly disappear; code and data are linked from the paper.

Discussion The thread is short and mostly reacts to the paper's writing and to a related bug report. One commenter praises the find and the thoroughness of the fix, while another says they found a separate PyTorch miscompilation that caused large gradient errors for tensor sizes not divisible by large powers of two. The most active exchange is about whether the paper was written with an AI model, with the commenter finding the prose formulaic, especially its opening line and its habit of stating what was not done. The mood is mildly skeptical and amused rather than hostile to the technical content.

  • Paper's prose reads as AI-generated and is poor (some): One commenter thinks the paper was largely written by Claude, and finds the crude, formulaic writing more irritating than the use of AI itself. They say they do not mind AI assistance as long as the text is good.
  • Dislikes performative negatives in model output (a few): A reply extends the complaint: models that list what they did not do, especially after being told not to, feel like a substitute for real diligence.
  • Impressed by the find and comprehensive fix (a few): One commenter calls the issue a nice find and says the fix is thorough, leaving little to add.
  • Similar PyTorch numerical bugs exist (a few): A commenter reports finding a PyTorch miscompilation in August that caused read-write corruption and about 100x relative gradient error when tensor dimensions were not multiples of a large power of two.

#PyTorch #numericalStability #machineLearning #KLDivergence #arXiv #LLMWriting