Free · Daily brief + weekly playbookDaily 60-sec AI Voice Brief + Weekly Production AI Agent PlaybookGet the brief →

The stablekl Library Removes 97–100% of Major KL Divergence Errors in PyTorch

KL divergence measures how much two distributions of a model's answers differ, and by definition it can never be negative. But PyTorch computes it by subtracting two huge, nearly equal numbers, and for similar models, say an original and a compressed copy, rounding noise remains. The stablekl library sums only non-negative differences and removes 97–100% of the major errors in the official code. When comparing compressed models, small KL values should be computed this way.

AI-processed from TG: @gonzo_ML (gonzo-обзоры ML статей); edited by Hamidun News
The stablekl Library Removes 97–100% of Major KL Divergence Errors in PyTorch
Source: TG: @gonzo_ML (gonzo-обзоры ML статей). Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceTG: @gonzo_ML (gonzo-обзоры ML статей) ↗
Editor-in-Chief
Key metric #1The check was run on small divergences across all distribution families in PyTorch
Key metric #2Float64 and clipping to zero do not remove the noise: the precision was not raised
Downsidenaive Python code slows down the GPU, while regular training barely changes

You have compressed a language model and want to check how far its answers differ from the original. For this, people compute KL divergence (the Kullback–Leibler divergence): a number that shows how much one probability distribution differs from another. Zero means a perfect match, and it cannot be below zero. Yet a negative value appears in the logs. This is the subject of the paper "The KL Divergence Is Never Negative, Except in PyTorch" and the stablekl library: the authors showed that the standard computation in PyTorch produces noise at small values, and their rewritten formulas remove 97% to 100% of the major errors in the official code.

Where the noise comes from

KL divergence is used everywhere: in distillation, in reinforcement learning from feedback schemes, in evaluating quantization, in ablations (when parts of a system are turned off one by one to see what changed), and in model drift monitoring. In all these cases, small values often matter most: the distributions nearly coincide, and the goal is to understand how nearly.

Standard formulas compute it by subtracting large numbers. Imagine finding the difference between 1,000,000.3 and 1,000,000.2. Each number is stored with limited precision, and after subtraction the true difference is mostly rounding error. The same happens with divergence: when distributions are similar, the real difference is tiny, while the terms that cancel each other out are enormous by comparison. The result can dip negative or simply be wrong.

The usual remedies, according to the authors, do not help. Clipping the result to zero turns the negative value into zero, but does not recover the lost information. Increasing number precision shifts the threshold at which the noise becomes visible, but does not remove the cause.

What the authors propose

The stablekl library for PyTorch rewrites the formulas so that cancelling quantities are never subtracted. Instead, it directly sums non-negative differences, computed from distribution parameters or from raw logits (the numbers a model outputs before they are turned into probabilities). For this approach the authors also derived analytical gradients, so the new function can be used for training.

On a stress grid of small divergences, the rewritten formulas remove 97% to 100% of the major errors in the official code. This holds for all analytical distribution families available in PyTorch. An important detail: the gain was achieved without raising the precision of inputs and outputs, meaning the improvement comes from the formula itself, not from "heavier" numbers.

The summary of the work does not give details on which distributions were included in the grid or how the authors defined a major error. These points should be checked in the paper itself and in the GitHub repository.

What to do in practice

Those who rely on small KL values are most at risk. If you are checking how far a quantized model has drifted from the original, comparing ablations, or tracking a slight drift, the distortion may be comparable to the signal itself. You may think the model has become better or worse, when in fact you are looking at rounding.

For regular training, the picture is calmer. The authors note that if you start from mismatched weights, the divergence is large, and the new computation does not noticeably change the course of training. There is no need to rewrite the training loop.

The approach has a cost. Unoptimized Python code significantly slows down work on the GPU. So it makes sense to:

  • use stablekl where you evaluate models and look at small divergence values;
  • not put the library in a hot training loop without profiling;
  • if you see a negative KL in the logs, do not clip it to zero, but check how exactly it is computed.

The work is small but useful: it is a reminder that even basic functions in popular frameworks can quietly corrupt metrics. A negative value at least stands out. The more dangerous cases are when the noise stays positive and looks like a normal result. For teams comparing compressed models, adding such a check to the evaluation pipeline is inexpensive.

Story

Key Questions Answered

Where the noise comes from?

KL divergence is used everywhere: in distillation, in reinforcement learning from feedback schemes, in evaluating quantization, in ablations (when parts of a system are turned off one by one to see what changed), and in model drift monitoring. In all these cases, small values often matter most: the distributions nearly coincide, and the goal is to understand how nearly. Standard formulas compute it by subtracting large numbers. Imagine finding the difference between 1,000,000.3 and 1,000,000.2.

What the authors propose?

The stablekl library for PyTorch rewrites the formulas so that cancelling quantities are never subtracted. Instead, it directly sums non-negative differences, computed from distribution parameters or from raw logits (the numbers a model outputs before they are turned into probabilities). For this approach the authors also derived analytical gradients, so the new function can be used for training.

What to do in practice?

Those who rely on small KL values are most at risk. If you are checking how far a quantized model has drifted from the original, comparing ablations, or tracking a slight drift, the distortion may be comparable to the signal itself. You may think the model has become better or worse, when in fact you are looking at rounding. For regular training, the picture is calmer.

ZK
Zhemal Khamidun — Head of AI at Alpina Digital · MIPT (academic supervisor of the Tech Entrepreneurship department). Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
Jemal Hamidun

Editor-in-chief · Cite this analysis

Jemal Hamidun

Head of AI at Alpina Digital · Academic Supervisor of the Tech Entrepreneurship Department at MIPT

Head of AI & CPO of AlpinaGPT at Alpina Digital, Academic Lead of Tech Entrepreneurship at MIPT. Founder of Hamidun News and Vibe Academy.

Ready-to-paste citation for journalists, blogs and Telegram channels

Source: Hamidun News — The stablekl Library Removes 97–100% of Major KL Divergence Errors in PyTorch https://news.hamidun.com/en/news/18354/the-stablekl-library-removes-97-100-of-major-kl-divergence-e

What do you think?
Loading comments…