Menu

1 / 5

35% of my KL divergences came back negative

DEV Community: pytorch·frank chu·11 days ago
#1VT1zITH
#devtopytorch#devto#dev#negative#kl_div
Long article (1050 words) — no AI summary yet.
Reading 0:00
15s threshold

35% of my KL divergences came back negative

I ran 2,000 random near-identical distributions through PyTorch's F.kl_div in float32. At a true KL of 1e-8, 25.7% of the answers came back negative — and 100% were off by more than 1%. KL divergence is mathematically never negative. The function disagrees.

The paper that sent me here

Yang et al. posted "The KL Divergence Is Never Negative, Except in PyTorch" to arXiv on November 23 (arXiv:2611.30835). They tested 8.5 million inputs across 18 closed-form KL families plus F.kl_div , against certified references. Their headline numbers: near a true KL of 1e-7, over 85% of FP32 values carry at least 1% relative error, and about 30% go negative at smaller divergences. More precision doesn't fix it — the same formulas evaluated one precision higher still leave 7–51% of values significantly wrong. Their repaired code removes over 97% of significant errors.

I don't take papers at their word.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.