The KL Divergence Is Never Negative, Except in PyTorch
Comments
Ranked by a public rubric, not by votes: what a comment adds counts, not who wrote it. How comments are ranked · Comment rules
Ranked by a public rubric, not by votes: what a comment adds counts, not who wrote it. How comments are ranked · Comment rules
Summary
Researchers at Oxford, Cambridge and other universities report that PyTorch's two KL divergence functions lose accuracy when distributions are close, because the formulas subtract nearly equal terms. On 8.5 million certified inputs, over 85% of FP32 values near KL 1e-7 had at least 1% error, and about 30% turned negative at smaller values. Higher precision alone left 7–51% wrong. Rewritten formulas removed over 97% of significant errors, and the errors distorted distillation and GPT-2 circuit scores. The figures come from the authors' own evaluation.