Create and explore imaginative 3D worlds with our new product Dreamfield (opens in a new tab)

The KL Divergence Is Never Negative, Except in PyTorch

This lightning talk examines a fundamental numerical instability in PyTorch's KL divergence implementations that goes far beyond occasional negative values. The paper demonstrates that catastrophic cancellation near distributional equality produces systematically wrong results and gradients across 18 distribution families, affecting everything from Bayesian experimental design to language model quantization. The authors show that higher precision alone cannot fix the problem and present stable reformulations that eliminate errors through algebraic decomposition and logits-based computation.
Script
The KL divergence is mathematically guaranteed to be non-negative, yet PyTorch's implementation can return negative, zero, or wildly inaccurate values. This isn't a minor rounding quirk, it's a structural failure that breaks optimization, corrupts experimental design, and invalidates model analysis.
When two distributions approach equality, their divergence drops to second order in the perturbation, but PyTorch subtracts first-order terms that differ by tiny amounts. In 32-bit floats, 86 percent of values near a true KL of 10 to the negative 7 have relative error above 1 percent, and nearly 30 percent turn negative outright.
The authors tested three repair strategies across 7.5 million closed-form samples and nearly a million batches. Simply promoting the unchanged formula to the next precision format leaves 7 to 51 percent of values significantly wrong. The fix requires algebraic reformulation that preserves non-negativity, compensated arithmetic to avoid cancellation, and in the tensor interface, computing divergence directly from logits rather than rounded probabilities.
In identical-start distillation, a student initialized as an exact copy of its teacher should produce zero loss and zero gradient. Under the official implementation with Adam, the student drifts in all 2,160 runs, reaching 97 percent top-1 disagreement in 16-bit precision. The logits path keeps the student stationary in every trial, confirming that the movement is purely numerical artifact amplified by the optimizer's normalization.
The errors propagate into downstream decisions. When selecting layers for mixed-precision quantization, the unstable 16-bit categorical KL inverts 19 of 378 layer pairs and selects four wrong layers for the final mixed model. That mistake increases the true divergence of the quantized model by nearly 10 percent on held-out data, while the logits path produces no inversions and perfect rank correlation.
Clamping negative outputs to zero might seem like a fix, but the paper shows it only hides the symptom. Positive outputs and their gradients remain systematically wrong whenever distributions are close, and the error compounds in any adaptive process that drives divergences toward zero. For reliable probabilistic computation, visit EmergentMind.com to explore this paper in depth and create your own video presentations from cutting-edge research.