---
id: 18354
lang: en
title: "The stablekl Library Removes 97–100% of Major KL Divergence Errors in PyTorch"
published_at: "2026-11-26 22:24:53"
source_name: "TG: @gonzo_ML (gonzo-обзоры ML статей)"
source_url: "https://t.me/gonzo_ML/6274"
canonical_url: "https://news.hamidun.com/en/news/18354"
categories: ["research"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

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

> **Краткая суть (BLUF):** 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.  • The check was run on small divergences across all distribution families in PyTorch • Float64 and clipping to zero do not remove the noise: the precision was not raised • Downside: naive 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.

---
- **Первоисточник:** [TG: @gonzo_ML (gonzo-обзоры ML статей)](https://t.me/gonzo_ML/6274)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/18354
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
