← All stories
● Covered by 1 source · 1 reportMedium impact1 negative

PyTorch's KL Divergence Function Exhibits Widespread Numerical Errors, Producing Negative Values

🔄 Updated 1d ago
New to BrevFeed? We gather this story from every outlet covering it into one summary — ranked by real-world impact, not just the latest headline — so you never miss what matters. What is BrevFeed? →

Key points

  • PyTorch's KL divergence calculations often yield incorrect, sometimes negative, values.
  • Errors affect FP32 and FP16, with over 85% of FP32 values having >1% error near KL 10^(-7).
  • Impacts include parameter fitting, Bayesian decisions, and LLM analysis.
  • Repairs using stable decompositions eliminate most significant errors.

Widespread Inaccuracy in PyTorch's KL Divergence

A recent study has identified significant numerical inaccuracies in PyTorch's Kullback–Leibler (KL) divergence computations, including the `F.kl_div` function. Despite KL divergence theoretically never being negative, PyTorch users have reported negative values for years. The research found widespread errors in both values and gradients across CPU and GPU, affecting 18 closed-form KL families and `F.kl_div`.

Extent of the Errors

The analysis, conducted on 8.5 million inputs, revealed that for scalar families within ordinary parameter ranges, over 85% of FP32 values exhibit at least 1% relative error near KL 10^(-7). For FP16, this threshold is 10^(-3). Approximately 30% of these calculations become negative at smaller divergences. The issue is not resolved by higher precision; evaluating formulas one precision higher still leaves 7–51% of FP32 values significantly incorrect in 22 out of 23 configurations.

Impact on Machine Learning Applications

These numerical errors have practical consequences for various machine learning tasks. Affected areas include parameter fitting, Bayesian decision-making, identical-start distillation (showing up to 97% top-1 disagreement), KL-based layer rankings in quantized Large Language Models (LLMs), and 19–59% of GPT-2 circuit edge scores. The inaccuracies can lead to flawed model training and analysis.

Proposed Solutions and Effectiveness

The study proposes solutions involving stable closed-form decompositions, compensated arithmetic, and analytic gradients. Implementing these repairs removes over 97% of significant errors in the working format. Specifically, strict repairs eliminated significant errors on 7.2 million scored closed-form samples and correctly rounded all 567 million scored `F.kl_div` components, effectively resolving the issues in the affected applications.

✨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors — check the original sources. How BrevFeed works →

The daily brief

One email each morning: the day's tech stories, clustered across outlets and summarized. No account needed.

One email a day. Unsubscribe in one click, any time.

Today's brief

Spend a few minutes, get the whole day. Every topic's top stories in one hands-free rundown — listen, watch, or read the transcript.

~4 min · 3 stories · Nov 26

▶ Play today's brief Listen on Spotify

New every morning, and the back catalogue is archived by date.

Reporting from

A new paper reveals that PyTorch's `F.kl_div` function and other KL divergence calculations frequently produce incorrect values, including negative results, across CPU and GPU. These errors affect machine learning applications like parameter fitting, Bayesian decisions, and LLM analysis, with over 85% of FP32 values showing significant relative error near KL 10^(-7). Stable decompositions and compensated arithmetic can resolve over 97% of these errors.