Kullback-Leibler divergence is mathematically nonnegative, but floating-point calculations in common PyTorch routines can return negative values - and the sign error points to a broader accuracy problem. Testing 8.5 million inputs against certified reference values, across 18 closed-form divergence families and PyTorch’s F.kl_div on CPUs and GPUs, the researchers found substantial errors in both outputs and gradients, especially when comparing similar distributions. In ordinary parameter ranges near divergences of 10⁻⁷, more than 85% of FP32 values had at least 1% relative error; roughly 30% became negative at even smaller divergences. Simply computing in higher precision did not reliably solve the problem: standard formulas still produced significantly wrong results in most tested configurations.
The researchers propose stable algebraic decompositions, compensated arithmetic, and analytic gradients. Together, these methods remove more than 97% of significant errors in the working format; stricter repairs eliminate them in 7.2 million tested closed-form cases and correctly round all 567 million tested F.kl_div components. The numerical flaws have practical consequences: they can distort parameter fitting and Bayesian decisions, cause identical-start distillation runs to disagree on top predictions by as much as 97%, alter layer rankings in a quantized language model, and affect 19-59% of circuit-edge scores in GPT-2. With repaired calculations, these effects largely disappear. The central warning is that clamping negative results to zero hides a symptom without fixing inaccurate values or gradients.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.