PyTorch's F.kl_div returns negative KL divergences for near-identical distributions in float32
A developer tested PyTorch's F.kl_div on thousands of nearly identical distributions in float32 and found that about a quarter to a third of results were negative, even though KL divergence is mathematically never negative. The problem appears sharply once the true divergence drops below roughly 1e-7, with nearly all values carrying large relative errors. The author confirmed the behavior of a recent arXiv paper, which reported similar findings across many divergence formulas. The cause is catastrophic cancellation: subtracting nearly equal logarithms in limited-precision arithmetic leaves little meaningful signal. The paper also notes that simply moving to higher precision does not reliably fix the issue.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in