# PyTorch's KL Divergence Goes Negative, and Clamping It Isn't the Fix

Original title: The KL Divergence Is Never Negative, Except in PyTorch

> Hacker News story 50629962 · language: en · submitted 2026-11-24
> Web version: https://zeli.app/story/50629962
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 86 · by [Anon84](<https://news.ycombinator.com/user?id=Anon84>) · 💬 35
[original](<https://arxiv.org/abs/2611.30835>) · [HN discussion](<https://news.ycombinator.com/item?id=50629962>)

## Summary

KL divergence can never be negative, yet PyTorch users have reported negative values for years and been told to clamp them at zero. A study of 8.5 million inputs across 18 closed-form KL families and F.kl_div finds the sign is just the visible symptom: over 85% of FP32 values carry at least 1% relative error near KL 10⁻⁷, and about 30% turn negative at smaller divergences. Extra precision doesn't help, but stable decompositions and compensated arithmetic remove over 97% of significant errors.

## Key quote

> The sign is only the visible part of a silent failure.

## HN discussion (35 comments)

### [derf_](<https://news.ycombinator.com/item?id=50632200>)

> It is surprisingly easy to get unexpected negative values with floating point operations. One I remember was a square root that was failing on a negative argument despite that argument being clamped against zero. The problem was that the clamp was using some macros
>
> \#define MIN(a, b) ((a) < (b) ? (a) : (b))
>
> \#define MAX(a, b) ((a) < (b) ? (b) : (a))
>
> and the arguments were being clamped with MAX(x, 0). Well, if x is negative zero, it is not less than 0, so the macro returns x. The square root then looks at the sign bit and fails. Negative zero is a perfectly possible result from a series of calculations that destructively cancel.

### [sva_](<https://news.ycombinator.com/item?id=50632706>)

> Interesting choice to post bug reports to arxiv now? Pretty significant finding though if true.

### [divyekapoor](<https://news.ycombinator.com/item?id=50631371>)

> The issue is a nice find and the fix is comprehensive. They left nothing to comment about. :D

### [carterschonwald](<https://news.ycombinator.com/item?id=50631516>)

> lol , in august i found and upstreamed a gnarly pytorch miscompilatio read write corruption that would cause 100x relative error in gradients if your tensor dimensions arent a multiple of a large power of 2. granted everyone seems to pick powers of two usually

### [bobmarleybiceps](<https://news.ycombinator.com/item?id=50632718>)

> it is sort of surprising how many parts of pytorch seem (sort of) buggy / numerically not great... I think they just have sooo many operations that some are (relatively) unused and don't receive as much attention. (though kl-divergence seems pretty important (??))
>
> I wonder if it's also because ML stuff is just pretty insensitive to numerical issues, so users don't notice it.

---

## Cite

- Page: https://zeli.app/story/50629962
- Date: 2026-11-24
- Source: arxiv.org (https://arxiv.org/abs/2611.30835)
- Hacker News: https://news.ycombinator.com/item?id=50629962

## Other languages

- zh: https://zeli.app/zh/story/50629962 · markdown: https://zeli.app/zh/story/50629962.md
- ja: https://zeli.app/ja/story/50629962 · markdown: https://zeli.app/ja/story/50629962.md
- ko: https://zeli.app/ko/story/50629962 · markdown: https://zeli.app/ko/story/50629962.md
- de: https://zeli.app/de/story/50629962 · markdown: https://zeli.app/de/story/50629962.md
- es: https://zeli.app/es/story/50629962 · markdown: https://zeli.app/es/story/50629962.md
- ru: https://zeli.app/ru/story/50629962 · markdown: https://zeli.app/ru/story/50629962.md
