PyTorch F.kl_div yields negative KL divergence errors corrected
Researchers found widespread errors in values and gradients for Kullback--Leibler divergence formulas in machine learning applications on CPU and GPU. Evaluating common precision ranges showed that over 85 percent of FP32 scalar family values have significant relative error near certain thresholds, and about 30 percent become negative. The study demonstrates that stable closed-form decompositions, compensated arithmetic, and analytic gradients successfully remove over 97 percent of these significant errors. Open article →