The KL Divergence Is Never Negative, Except in PyTorch
Organizations: Yale University, New Haven, Connecticut, USA · University of Oxford, Oxford, United Kingdom · University of Cambridge, Cambridge, United Kingdom · Fudan University, Shanghai, China ·
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
Figures & tables
| FP16 inputs | FP32 inputs | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Distribution | Official | Off.+ | Rewrite | Ours | Official | Off.+ | Rewrite | Ours | |
| Cauchy | 345,028 | 64.05 | 10.60 | 0.003 | 0 | 88.88 | 34.84 | 0 | 0 |
| Uniform | 181,798 | 51.62 | 0 | 0 | 0 | 75.08 | 0.012 | 0 | 0 |
| Exponential | 308,000 | 72.57 | 0 | 0 | 0 | 85.64 | 33.49 | 0 | 0 |
| Poisson | 323,498 | 75.64 | 9.75 | 0.002 | 0 | 85.82 | 34.44 | 0 | 0 |
| Normal | 339,499 | 42.40 | 0 | 0 | 0 | 82.18 | 13.84 | 0 | 0 |
| Adam, learning rate | SGD, learning rate | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| dtype | |||||||||
| 1,024 | FP32 | 0.3 / 0 | 1.0 / 0 | 4.1 / 0 | 11.9 / 0 | 31.4 / 0 | 0.0 / 0 | 0.0 / 0 | 0.0 / 0 |
| 1,024 | FP16 | 0.5 / 0 | 1.4 / 0 | 3.8 / 0 | 11.5 / 0 | 29.9 / 0 | 0.0 / 0 | 0.0 / 0 | 0.0 / 0 |
| 1,024 | BF16 | 0.7 / 0 | 1.4 / 0 | 4.1 / 0 | 12.1 / 0 | 30.0 / 0 | 0.0 / 0 | 0.0 / 0 | 0.2 / 0 |
| 32,768 | FP32 | 0.7 / 0 | 1.7 / 0 | 6.9 / 0 | 16.7 / 0 | 40.5 / 0 | 0.0 / 0 | 0.0 / 0 | 0.0 / 0 |
| 32,768 | FP16 | 2.1 / 0 | 4.3 / 0 | 9.8 / 0 | 19.3 / 0 | 41.0 / 0 | 0.0 / 0 | 0.0 / 0 | 0.4 / 0 |
| Part A | Part B | |||
|---|---|---|---|---|
| Method | mean err. (%) | tokens (%) | inv. | |
| F.kl_div | / | 30.2 / 12.3 neg. | 19 | 0.898 |
| F.kl_div , log_target | – | 35 | 0.815 | |
| F.kl_div , mean | reports 0 | – | 378 ties | |
| Categorical | 98.0 / 97.1 inf | not rankable | ||
| B fix | / | 16.9 / 7.5 neg. | 20 | 0.894 |
| true KL | F.kl_div | log_target | Categorical | logits path | |
|---|---|---|---|---|---|
| FP32 | 0.0 | ||||
| FP16 | non-finite | 0.0 | |||
| BF16 | 0.0 |
| library | logits path | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| task | model | edges | precision | wrong | negative | Spearman | top 1,000 | wrong | Spearman | top 1,000 |
| IOI | GPT-2 small | 32,477 | FP32 | 19.3% | 1,449 | 1.00 | 1.000 | 0 | 1.00 | 1.000 |
| FP16 | 81.4% | 3 | 0.74 | 0.963 | 0 | 1.00 | 1.000 | |||
| BF16 | 2.5% | 3 | 0.99 | 0.849 | 0 | 1.00 | 0.976 | |||
| Greater-than | GPT-2 small | 32,477 | FP32 | 45.8% | 3,271 | 0.97 | 1.000 | 0 | 1.00 | 1.000 |
| 32,476 | FP16 | 6.8% | 2 | 0.95 | 0.698 | 0.02% | 1.00 | 0.999 | ||
Appendix figures & tables21 assets
Supplementary material from the paper’s appendix.
Appendix
| input | evaluated | metric | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| FP32 | official | KL rel. err. | 0.1 | 0.5 | 2.4 | 9.2 | 23.7 | 56.0 | 86.4 | 96.2 | 96.9 | 97.0 | 98.0 | 99.1 | 99.6 | 99.8 | 99.9 |
| FP32 | official | KL negative | 0.0 | 0.0 | 0.0 | 0.2 | 0.8 | 3.4 | 8.2 | 14.6 | 23.6 | 28.4 | 29.1 | 29.5 | 29.3 | 28.9 | 29.1 |
| FP32 | official | grad. rel. err. | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.1 | 0.8 | 3.2 | 8.6 | 20.1 | 41.8 | 72.8 | 89.7 | 94.5 | 95.8 |
| FP32 | official | grad. cosine | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.3 | 1.6 | 4.4 | 9.4 | 19.6 | 37.0 | 51.2 |
| FP32 | in FP64 | KL rel. err. | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.2 | 0.8 | 3.2 | 10.7 | 24.7 | 56.2 |
| FP32 | in FP64 | KL negative | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.1 | 0.1 | 0.6 | 2.7 |
| GPU (A100) | CPU (4 threads) | |||||
|---|---|---|---|---|---|---|
| FP64 | Rewrite | Ours | FP64 | Rewrite | Ours | |
| Exponential | 1.1 / 1.0 | 2.5 / 2.9 | 3.5 / 8.0 | 1.6 / 3.1 | 3.3 / 7.2 | 6.9 / 17 |
| Laplace | 1.0 / 1.1 | 2.6 / 3.1 | 3.6 / 5.4 | 1.7 / 2.4 | 4.3 / 5.4 | 8.8 / 12 |
| Poisson | 1.0 / 1.1 | 2.4 / 2.7 | 3.4 / 5.0 | 1.8 / 2.8 | 2.7 / 7.3 | 5.4 / 18 |
| Normal | 1.0 / 1.0 | 4.7 / 2.6 | 5.4 / 7.0 | 1.4 / 2.9 | 11 / 22 | 27 / 40 |
| Bernoulli (logits) | 1.0 / 1.0 | 5.9 / 4.8 | 7.8 / 12 | 1.7 / 2.5 | 20 / 19 | 56 / 43 |
| GPU (A100) | CPU (4 threads) | |||||||
|---|---|---|---|---|---|---|---|---|
| inputs | log_target | FP64 | no check | repaired | FP64 | no check | repaired | |
| FP32 | False | 1.1 | 22 | 29 | 1.3 | 27 | 33 | |
| FP32 | True | 1.1 | 20 | 27 | 3.0 | 22 | 28 | |
| FP16 | False | 1.1 | 24 | 33 | 1.0 | 23 | 30 | |
| FP16 | True | 1.1 | 23 | 32 | 3.9 | 34 | 48 | |
| FP32 | False | 1.4 | 39 | 52 | 1.9 | 36 | 48 | |
| GPU (A100) | CPU (4 threads) | |||||
| logits | official (ms) | FP64 | logits path | FP64 | logits path | |
| FP32 | 0.19 | 1.2 | 3.1 | 2.4 | 4.4 | |
| FP32 | 0.21 | 2.0 | 8.0 | 3.1 | 8.5 | |
| FP32 | 0.49 | 2.6 | 8.8 | 2.8 | 14 | |
| FP32 | 6.20 | 2.3 | 10 | – | – | |
| BF16 | 0.19 | 1.2 | 4.6 | 7.2 | 13 | |
| Official | Ours | ||||
|---|---|---|---|---|---|
| inputs | CPU | GPU | CPU | GPU | |
| Interface A, KL value | FP16 | 70.08 | 71.47 | 0 | 0 |
| FP32 | 87.67 | 87.66 | 0 | 0 | |
| Interface B, forward | FP16 | 38.74 | 38.79 | 0 | 0 |
| FP32 | 56.19 | 61.15 | 0 | 0 | |
| Interface B, backward | FP16 | 21.95 | 21.95 | 0 | 0 |
| Setting | Official: deviation | Official: max. true KL | Repaired: deviation |
|---|---|---|---|
| FP16 probs, , | 3.7% / 9.8% | 0.08% / 0.31% | |
| FP16 probs, , | 95% / 96% | 1.7 | 0 / 0 |
| FP16 logits, , | 95% / 96% (9 runs); 3 runs non-finite | 0.18 | 0 / 0 |
| FP16 probs, , | 0 / 100% (5 runs collapse to ) | 6.0 | 0 / 0 |
| FP32 probs, , | 0.014% / 0.027% | 0.00007% / 0.00045% | |
| FP32 logits, , | 0.0012% / 0.0032% | 0.00005% / 0.0004% |
| official: decisions | official: variance ( exact) | ||||||||
| start | rounds | wrong (%) | first wrong round | lost (%) | zero or negative (%) | sum | worst arm | fewest | |
| cold | 1 | 100,000 | 91 | 380–422 | 20–22 | 41–44 | 1.01–1.05 | 1.08–1.17 | 8,308 (9,998) |
| 100 | 1,000 | 84–88 | 11–14 | 25–31 | 43–46 | 1.02–1.08 | 1.12–1.79 | 5,600 (10,000) | |
| 1,000 | 100 | 63–75 | 13–15 | 28–45 | 36–48 | 1.06–1.27 | 1.20–2.50 | 4,000 (10,000) | |
| 10,000 | 10 | 0 | – | 0 | 15–30 | 1.00 | 1.00 | 10,000 (10,000) | |
| warm | 1 | 100,000 | 84–91 | 1–97 | 20–24 | 43–50 | 1.01–1.10 | 0.98–1.67 | 8,749 (14,836) |
| Model | reference | F.kl_div FP16 | Categorical FP16 | interface-B fix FP16 | logits path FP16 | F.kl_div FP32 |
|---|---|---|---|---|---|---|
| GPTQ W8 | inf | 0.0% | 0.0% | |||
| RTN W8 | inf | 0.0% | 0.0% | |||
| AWQ W4 | 0.10139 | inf | 0.0% | 0.0% | ||
| GPTQ W4 | 0.13633 | inf | 0.0% | 0.0% | ||
| RTN W4 | 0.15677 | inf | 0.0% | 0.0% | ||
| RTN W3 | 0.99191 | inf | 0.0% | 0.0% |
| Method | negative | infinite | total | total | operator correctly rounded |
|---|---|---|---|---|---|
| F.kl_div FP16 | 30.2 / 12.3% | 0 | 99.0 / 97.0% | 49.2 / 21.6% | 0.1 / 0.2% |
| Categorical FP16 | 1.3 / 1.8% | 98.0 / 97.1% | 100 / 100% | 100 / 99.9% | – |
| interface-B fix FP16 | 16.9 / 7.5% | 0 | 98.8 / 96.8% | 45.6 / 20.0% | 100 / 100% |
| logits path FP16 | 0 | 0 | 2.0 / 1.1% ∗ | 0.1 / 0% | 100 / 100% |
| F.kl_div FP32 | 16 / 4 tokens | 0 | 7.5 / 4.3% | 0.2 / 0% | – |
| Categorical FP32 | 87 / 40 tokens | 0 | 20.1 / 10.9% | 1.0 / 0.5% | – |
| 8-bit per channel | 4-bit, group 128 | |||
|---|---|---|---|---|
| Method | inversions / ties | inversions / ties | ||
| F.kl_div FP16 batchmean | 19 / 1 | 0.898 | 1 / 0 | 0.995 |
| F.kl_div FP16 mean | 0 / 378 | – | 11 / 32 | 0.896 |
| F.kl_div log_target FP16 | 35 / 0 | 0.815 | 2 / 0 | 0.989 |
| Categorical FP16 | not rankable (inf) | not rankable (inf) | ||
| interface-B fix FP16 | 20 / 0 | 0.894 | 1 / 0 | 0.995 |
| precision | execution | true KL | top-1 differs (%) | F.kl_div | log_target | Categorical | logits path | sampled: official / cancellation-free |
|---|---|---|---|---|---|---|---|---|
| FP32 | cached decoding | 0.00 | / | |||||
| FP16 | cached decoding | 0.34 | non-finite | / | ||||
| BF16 | cached decoding | 1.90 | / | |||||
| FP32 | batch size 1 | 0.00 | / | |||||
| FP16 | batch size 1 | 0.29 | non-finite | / | ||||
| BF16 | batch size 1 | 2.00 | / |
| mean , rows per call | other | logits | ||||||
|---|---|---|---|---|---|---|---|---|
| copy | true KL | 1 | 16 | 128 | 512 | 4,096 | reductions | path |
| 8 bits | 57 | 49 | 58 | 0 | 0 | 94.7–95.7 | 99.98–100.01 | |
| 7 bits | 82 | 76 | 60 | 140 | 160 | 97.7–98.2 | 99.99–100.03 | |
| 6 bits | 99 | 99 | 101 | 91 | 87 | 99.6–99.7 | 99.99–100.02 | |
| 5 bits | 100 | 100 | 99 | 99 | 99 | 99.9–100.0 | 99.99–100.01 | |
| 4 bits (g128) | 100 | 100 | 101 | 100 | 98 | 99.9–100.0 | 99.97–100.01 | |
| selected differently | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| task, model | precision | score | wrong | negative | Spearman | top 1,000 | top 5,000 | |||
| IOI, GPT-2 small | FP32 | library | 19.3% | 1,449 | 84 | 0 | 0 | 0.9992 | 1.000 | 1.000 |
| logits path | 0 | 0 | 0 | 0 | 0 | 1.0000 | 1.000 | 1.000 | ||
| FP16 | library | 81.4% | 3 | 3 | 587 | 29 | 0.7447 | 0.963 | 0.637 | |
| logits path | 0 | 0 | 0 | 1 | 0 | 1.0000 | 1.000 | 0.998 | ||
| BF16 | library | 2.5% | 3 | 5 | 9 | 20 | 0.9922 | 0.849 | 0.888 | |
| package | version | A | B | use | package | version | A | B | use |
|---|---|---|---|---|---|---|---|---|---|
| adv.-robustness-toolbox | 1.20.1 | – | 4 | TRADES-type training | pfrl | 0.4.0 | 2 | – | TRPO, ACER |
| allennlp | 2.10.1 | 1 | – | fairness metrics | pyro-ppl | 1.9.2 | 3 | – | analytic ELBO terms |
| audiocraft | 1.3.0 | – | 1 | audio KLD metric | rsl-rl-lib | 5.5.2 | 1 | – | PPO adaptive learning rate |
| auto-circuit | 1.0.1 | – | 4 | circuit discovery | sae-lens | 6.55.5 | – | 1 | SAE evaluation |
| botorch | 0.18.1 | 2 | – | information gain, neural process | scvi-tools | 1.5.2 | 21 | 1 | VAE terms |
| cleanrl | 0.4.8 | 4 | – | PPG: old vs. new policy | sentence-transformers | 6.3.0 | – | 5 | distillation losses |
| setting | what was run | expected mechanism | what we measured |
| LLM distillation, nearby start | TRL GKD loss; student = 8-bit or 4-bit copy; 320 steps, 10 seeds | the BF16 loss mis-states small KLs by tens of percent and has a spurious gradient at identity | final eval KL: ours vs. official and (seed spread 2.8%, 1.9%) |
| LoRA, nearby start or KL anchor | rank 16, two learning rates, 3 seeds; quantized starts; task loss + KL to the base model | same loss in a low-dimensional update | differences below the seed spread, no consistent sign |
| LoRA, strong constraint | KL only from a perturbed adapter; task loss + KL, ; 63 runs | the KL term dominates the objective, and KL is reachable | median final eval KL 0.018–0.041 with every path; ours does not return to the start either |
| PPO adaptive learning rate | rsl-rl-lib 5.5.2, Beta policy, real training, decisions | the Beta closed form loses accuracy for large concentrations; a simulation predicted changed decisions | 0 decisions differ from FP64 |
| PPO KL thresholds | synthetic categorical policies, 6–50,000 actions, FP32 | the KL of consecutive policies is small | 0 of 960 decisions differ; error |
| TRPO Fisher-vector product | double backward of the official FP32 KL at the old policy | evaluated exactly at identical distributions | relative error |