A new arXiv paper argues that PyTorch's KL divergence calculations are wrong beyond the negative values users see. Tests on 8.5 million inputs found widespread errors in values and gradients on CPU and GPU. The authors say stable formulas, compensated arithmetic and analytic gradients remove most significant errors.
A new research paper says the popular PyTorch machine-learning library has been getting a basic calculation wrong for years, and the negative numbers users keep seeing are only the most obvious sign of the problem.
Why does PyTorch return negative KL divergence values?
The paper, posted to the arXiv preprint server, focuses on Kullback-Leibler divergence, or KL divergence. Researchers use the measure to compare two probability distributions. By its mathematical definition, the value can never be negative. Yet PyTorch users have reported negative results for years, and the standard advice has been to clamp them at zero and move on.
The authors argue that clamping hides the real failure. They tested the calculations on 8.5 million inputs, comparing PyTorch's output against certified reference values across 18 closed-form KL families and the library's F.kl_div function. They found widespread errors in both values and gradients, on both CPU and GPU hardware.
How much do PyTorch KL divergence errors affect results?
The errors are worst when the two distributions are close together, which is a common situation in machine learning. Among scalar families across ordinary parameter ranges, more than 85% of single-precision, or FP32, values carried at least a 1% relative error when the divergence was near 10^-7. For half-precision, or FP16, values, the threshold was near 10^-3. About 30% of the results turned negative at smaller divergences.
Switching to higher precision does not solve the problem, the authors found. When they evaluated the same formulas one precision level higher, 7 to 51% of FP32 values were still significantly wrong in 22 of the 23 configurations they tested.
The team reports that a combination of fixes works much better. Stable closed-form decompositions, compensated arithmetic and analytic gradients removed more than 97% of significant errors in the working format. With strict repairs, the researchers say they eliminated significant errors across 7.2 million scored closed-form samples and correctly rounded all 567 million scored components from F.kl_div.
The consequences reach well beyond rounding. According to the paper, the errors affect parameter fitting and Bayesian decisions. In one test, identical-start distillation showed up to 97% top-1 disagreement. The errors also distorted KL-based layer rankings in a quantized large language model, and they changed 19 to 59% of GPT-2 circuit edge scores. The authors say that with the repaired code, those problems nearly disappear.
The researchers have released code and data alongside the paper so others can check the results. The paper was first submitted to arXiv on Nov. 23 and has not yet been formally published in a journal.
Questo articolo è stato scritto con l'assistenza dell'IA.
News Factory APP - notizie agentiche per potenziare il tuo SEO e AEO.
