PyTorch KL Divergence Implementation Shows Widespread Negative Value Errors
November 24, 2026
A study of 8.5 million inputs across 18 KL families shows that PyTorch F.kl_div produces negative values and gradient errors on both CPU and GPU. In scalar families, 30% of values become negative at small divergences, and over 85% of FP32 values have at least 1% relative error near KL 10^(-7).
HOW THIS AFFECTS YOU
●
builderDo not rely on F.kl_div for precise distribution comparisons without manual clamping or alternative formulas.
●
researcherCheck your divergence calculations if you are comparing very similar distributions.