Research reveals widespread errors in PyTorch KL divergence calculations
A new study identifies significant inaccuracies in PyTorch's KL divergence implementations, revealing that negative values and gradient errors are common.
Source: Grafy Research LabA recent research paper has identified significant numerical inaccuracies in how the PyTorch library handles Kullback--Leibler (KL) divergence, a fundamental metric in machine learning. The study reveals that the common practice of clamping negative KL divergence values to zero may be masking deeper, systemic failures in calculation accuracy and gradient precision.
Why are negative KL divergence values appearing in PyTorch?
By definition, KL divergence is a measure of how one probability distribution differs from a second, reference probability distribution, and it should mathematically never be negative. However, according to the research conducted by Zhen Yang, Wei Tang, Chu Chen, Rui Qian, Yixuan Chen, Fan Yang, and Kangning Cui, PyTorch users have reported encountering negative values for several years.
The authors suggest that these negative values are not just a minor sign error but are indicative of a "silent failure" within the implementation. In many machine learning workflows, developers encounter these negative results and simply apply a clamp to force the value to zero. The researchers argue that this approach ignores the underlying issue: the conventional formulas used for KL divergence lose accuracy when comparing distributions that are very close to one another.
What is the scale of the error in these calculations?
To quantify the extent of the problem, the researchers tested the software against 8.5 million inputs. These inputs were checked against certified references across 18 different closed-form KL families, as well as the standard F.kl_div function.
The findings were extensive, showing widespread errors in both the resulting values and the calculated gradients on both CPU and GPU architectures. Specifically, the study found that among scalar families operating within ordinary parameter ranges, over 85% of values calculated using 32-bit floating-point precision (FP32) exhibited at least a 1% relative error. This suggests that the inaccuracies are not isolated edge cases but are a prevalent issue in standard deep learning environments.
How do these errors affect machine learning models?
When a model relies on KL divergence for tasks such as variational inference, reinforcement learning, or distribution matching, the accuracy of the gradient is critical. Gradients guide the optimization process, telling the model how to adjust its parameters to minimize loss.
According to the paper, the errors identified are not limited to the output values themselves but extend to the gradients. If the gradients are inaccurate, the model may receive incorrect signals during training. This can lead to sub-optimal convergence, instability in training, or models that fail to accurately capture the target distribution. Because the errors can be "silent," a researcher might observe a model performing poorly without ever realizing that the underlying mathematical engine is providing flawed directional data.
Can these errors be fixed by using higher precision?
The research highlights that the issue persists even in standard precision environments. While moving to higher precision (such as FP64) can often mitigate numerical instability, the core of the problem identified by the authors relates to how the formulas handle distributions that are nearly identical. In these high-similarity scenarios, the precision requirements of the mathematical operations exceed what standard implementations provide, leading to the observed relative errors in FP32.
How can researchers stay updated on such mathematical flaws?
For researchers and developers working on cutting-edge AI, staying informed about the numerical limitations of popular frameworks is essential for building reliable systems. Understanding that a tool like PyTorch might have specific mathematical blind spots allows for more rigorous validation of experimental results.
If you are currently reviewing similar technical papers or investigating the mathematical foundations of loss functions, you can use Grafy Reader to perform deep research over PDFs and academic papers. This can help you cross-reference findings or dive deeper into the specific closed-form families mentioned in the study.
Frequently asked questions
Is KL divergence supposed to be negative?
No, KL divergence is mathematically defined such that it is always greater than or equal to zero. If a software implementation produces a negative value, it indicates a numerical error or an inaccuracy in the underlying calculation method rather than a valid statistical result.
What is the main cause of these errors in PyTorch?
The errors appear to stem from the loss of accuracy in conventional KL formulas when they are used to compare probability distributions that are very close to each other. This leads to significant relative errors in both the values and the gradients during computation.
How many FP32 values were found to be inaccurate?
The research team found that over 85% of values calculated using FP32 precision had at least a 1% relative error when tested against certified references across 18 different closed-form KL families.
Source: https://arxiv.org/abs/2611.30835
Source: Grafy Research Lab — The KL Divergence Is Never Negative, Except in PyTorch


