The KL Divergence Is Never Negative, Except in PyTorch

Anon84 91 points 37 comments November 24, 2026
arxiv.org · View on Hacker News

Discussion Highlights (10 comments)

divyekapoor

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

Der_Einzige

So obvious this entire paper was generated by Claude. That's good in terms of the epistemological correctness of the claim being higher than it was historically (lots of lies before AI agents in research), but it's infuriating to notice this from literally the first sentence "A KL divergence is never negative..." I really, REALLY want whoever at Anthropic is okay with the tiny pool of annotators that their model is way WAY too over RL'd on to reincarnate into a durian fruit or something. I don't care that the author used AI to write something. I do care that the ensuing prose is really crappy.

carterschonwald

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

derf_

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_

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

bobmarleybiceps

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.

fluxem

Here we go, a slop paper for a contrived problem. They assume two near identical distributions, which is never the case in real training

lambda

What a weird way to submit a bug report and patch: publishing an AI generated preprint. Seems like a pull request would be a better way to handle this?

newmana

So this is a bug from 2022? https://github.com/pytorch/pytorch/issues/80488 They didn't really show any real world impacts - maybe in distillation? And what impact using logits throughout would have? This seems like an engineering tradeoff decision and each of their changes they haven't quantified it - how much slower does each change give you for an effect in the simulated/real world? Could you just cast things to float64 and call it a day?

lhk931122

I'm not sure why this topic blew up. For a long time I've gotten negative values a lot when using the KL div loss, so I had 0 clipping + epsilon on it? But back then I was working in float32 so I didn't care, but if you do a lot of work with fewer bits like these days, I think it could get kind of critical.

Semantic search powered by Rivestack pgvector
4,072 stories · 37,019 chunks indexed