跳到正文
arXiv (AI + Vision + Imaging)· Zhen Yang, Wei Tang, Chu Chen, Rui Qian, Yixuan Chen, Fan Yang, Kangning Cui·· 1 天前AI 评分20

PyTorch的KL散度为何出现负值:850万输入测试揭示数值误差与修复方案

The KL Divergence Is Never Negative, Except in PyTorch

AI 导读

在850万输入、18类闭式KL散度及F.kl_div上测试发现,PyTorch在CPU和GPU上的KL值与梯度普遍存在误差,普通参数范围内超85%的FP32值在KL接近10^(-7)时相对误差至少1%,约30%在更小散度下变为负值。

正文 · 原文

arXiv:2611.30835v1 Announce Type: new Abstract: A Kullback--Leibler (KL) divergence is never negative, yet PyTorch users have reported negative values for years, and the standard advice is to clamp them at zero. The sign is only the visible part of a silent failure. Many machine-learning applications compare nearby distributions, where conventional KL formulas lose accuracy. On 8.5 million inputs with certified references across 18 closed-form KL families and F.kl_div, we find widespread errors in values and gradients on CPU and GPU. Among scalar families in ordinary parameter ranges, over 85% of FP32 values have at least 1% relative error near KL 10^(-7) (FP16: 10^(-3)), and about 30% become negative at smaller divergences. More precision is not the cure: evaluated one precision higher, the same formulas still leave 7--51% of FP32 values significantly wrong for 22 of 23 configurations. Stable closed-form decompositions, compensated arithmetic, and analytic gradients remove over 97% of significant errors in the working format. Strict repairs eliminate significant errors on 7.2 million scored closed-form samples and correctly round all 567 million scored F.kl_div components. The errors affect parameter fitting, Bayesian decisions, identical-start distillation (up to 97% top-1 disagreement), KL-based layer rankings in a quantized LLM, and 19--59% of GPT-2 circuit edge scores. With the repaired code they all but vanish. Code and data: https://github.com/xi-xiaoran/stablekl

来源:arXiv (AI + Vision + Imaging) · arxiv.org