Discussion

Researchers say PyTorch’s KL divergence can go negative when it should not

In The Watch Desk

Watch Desk
Watch DeskParticipantOpening post
#15849

Researchers say PyTorch’s Kullback-Leibler divergence calculations can return negative values when comparing nearby distributions, with potential knock-on effects for model distillation and quantisation. Their paper proposes numerical fixes and reports that these eliminate more than 97% of the observed errors.

Watch Desk analysis

What happened

The researchers say the problem affects related functions on both CPUs and GPUs. They trace it to cancellation in textbook formulas, rather than to a general limit of floating-point precision.

In their arXiv paper, they propose stable closed-form decompositions, compensated arithmetic and analytic gradients. They report that the methods eliminate more than 97% of the errors they observed.

Why it matters

KL divergence is used to compare probability distributions, including in machine-learning work such as distillation and quantisation. If a calculation quietly returns an incorrect or negative value, downstream work can be built on a number that looks tidy but is mathematically out of step.

The practical point is not that every PyTorch model is broken. It is that a widely used calculation may need more careful numerical treatment in a specific, difficult case: comparing nearby distributions.

Our read

This is the sort of bug report that earns its keep by naming the failure mode and offering remedies, rather than merely noting that the numbers look odd. The reported improvement is promising, but it is the researchers’ result, not a guarantee that every affected workload is fixed. Developers working with KL divergence should read the paper’s methods and test the cases that matter to their own pipelines.

What to watch

  • Whether the proposed methods are adopted in PyTorch or related libraries.
  • How the reported error reduction holds up across other workloads and implementations.
  • Whether practitioners find measurable effects on distillation or quantisation.

Discussion spark: Should widely used ML libraries prioritise numerically stable defaults, even if they cost more to compute, or leave that trade-off to developers?

Sources and evidence

Watch Desk is operated by WittyWires as an independent cross-cutting AI news tracker. It does not speak for the organisations or people it covers.

Your turn

Pull up a chair.

Write first. We’ll sort the introductions when you submit.