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.
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. I installed torch 2.15.0 (CPU) and re-ran the core claim myself.
F.kl_div(input, target) computes sum(target * (log(target) - input)). Feed it input = log q and target = p and you get KL(p||q). I swept true KL from 1e-4 down to 1e-9, 2,000 random pairs per cell, and scored each FP32 answer against the same formula computed in float64. Two families: Bernoulli pairs, and 8-way categoricals from a Dirichlet draw with a small random perturbation.
import torch, torch.nn.functional as F
import numpy as np
def kl_bern_torch_f32(p, q):
lp = torch.tensor([np.log(q), np.log(1-q)], dtype=torch.float32)
t = torch.tensor([p, 1-p], dtype=torch.float32)
return F.kl_div(lp, t, reduction='sum').item()
Bernoulli, FP32 F.kl_div vs float64 exact (n=2000 per row):
true KL negative rel err > 1% median rel err
1e-4 0.0% 0.0% 0.02%
1e-5 0.0% 0.0% 0.16%
1e-6 0.0% 86.2% 1.65%
1e-7 0.0% 99.7% 19.21%
1e-8 25.7% 100.0% 198.02%
1e-9 35.1% 100.0% 2134.77%
Categorical (k=8), same setup:
1e-5 0.0% 0.8% 0.28%
1e-6 0.0% 79.2% 2.45%
1e-7 0.1% 99.5% 25.49%
1e-8 34.0% 100.0% 249.01%
My numbers land on the same side as the paper's, slightly worse: at 1e-7 they report "over 85%" with >1% error; I measured 99.7% on Bernoulli pairs and 99.5% on categoricals. The cliff is sharp and it sits exactly where you'd expect: between 1e-6 and 1e-7 the median error goes from 1.65% to 19%, and below 1e-8 a quarter to a third of all values flip sign.
You don't need 2,000 samples. Two numbers will do it:
p=0.3, q=0.30005: true KL = 5.952003e-09 F.kl_div fp32 = -1.490116e-08 (rel err 350%)
p=0.5, q=0.50002: true KL = 8.000000e-10 F.kl_div fp32 = 0.000000e+00 (rel err 100%)
The first one returns a negative divergence. The second returns exactly zero — which looks safe and is still 100% wrong. Run the same formula in float64 and the second case comes back with a relative error of 1.09e-06%. The formula is fine. The precision is the problem.
It's catastrophic cancellation, the oldest floating-point failure there is. When p and q are close, log p - log q is a tiny difference of two ordinary-sized numbers. FP32 has a 24-bit mantissa — roughly 7 decimal digits — so by the time you've subtracted, only a couple of bits of real signal are left, and then you multiply by p and add the terms up. The sign of the result is basically a coin flip once the true value drops below ~1e-8. The paper's deeper point is that this isn't a rounding annoyance you can outrun with float64 everywhere: the same naive formulas, evaluated one precision higher, still leave 7–51% of values significantly wrong in 22 of 23 configurations they tested. What actually fixes it is stable closed-form decompositions and compensated arithmetic — their repaired code is at github.com/xi-xiaoran/stablekl.
KL divergence is the loss function underneath distillation, RLHF, VAEs, and half the evaluation metrics in the field. And the failure mode peaks exactly where training converges: early on, when the student and teacher are far apart, the numbers are fine. Late in training, when the two distributions are nearly identical and you're measuring small improvements, your KL meter is reading noise — and some of that noise is negative, which is how you end up with the standard advice to "just clamp it at zero." Clamping turns my -1.49e-08 into 0. That's still 100% wrong, and now you've also hidden the evidence that your measurement is garbage. The paper traces the downstream damage: identical-start distillation runs disagreeing on the top-1 token up to 97% of the time, 19–59% of GPT-2 circuit edge scores affected, KL-based layer rankings in a quantized LLM reshuffled. Those are their numbers, not mine — but after watching a third of my own samples come back negative, I believe them.
My reproduction is deliberately small: two distribution families, one PyTorch version (2.15.0, CPU), 2,000 samples per cell, float64 as the reference. The paper's 8.5-million-input sweep covers 18 families and both CPU and GPU; I didn't re-run that, and my percentages are mine, not theirs. I also only tested the value — I checked the autograd gradient on a toy Bernoulli formula and it came back clean at these scales, so the failure I can personally vouch for is in the value, not the gradient. If your KL values live above 1e-5, none of this matters to you. If they live below 1e-7 — which is exactly where a converged distillation or a fine-grained eval lives — your meter is lying to you, and clamping the lie at zero doesn't make it true.
F.kl_div FP32 vs float64 reference, 2,000 random pairs per cell.