The KL Divergence Is Never Negative, Except in PyTorch

Hacker News Top Papers

Summary

This paper shows that PyTorch's F.kl_div frequently returns inaccurate and even negative KL divergence values due to floating-point error, and that clamping hides the problem. Stable closed-form decompositions, compensated arithmetic, and analytic gradients remove over 97% of errors, greatly improving distillation, quantization, and circuit-scoring results.

No content available
Original Article
View Cached Full Text

Cached at: 11/24/26, 06:42 PM

# The KL Divergence Is Never Negative, Except in PyTorch
Source: [https://arxiv.org/html/2611.30835](https://arxiv.org/html/2611.30835)
Wei TangAffiliation:University of Oxford, Oxford, United KingdomChu ChenAffiliation:University of Cambridge, Cambridge, United KingdomRui QianAffiliation:Fudan University, Shanghai, ChinaYixuan ChenAffiliation:University of Oxford, Oxford, United KingdomFan YangAffiliation:Wake Forest University, Winston\-Salem, North Carolina, USACorrespondence to:[yangfan@wfu\.edu](mailto:[email protected])Kangning CuiAffiliation:City University of Hong Kong \(Dongguan\), Dongguan, ChinaCorrespondence to:[kangning\.cui@cityu\-dg\.edu\.cn](mailto:[email protected])

###### Abstract

A Kullback–Leibler \(KL\) divergence is never negative, yet PyTorch users have reported negative values for years, and the standard advice is to clamp them at zero\. The sign is only the visible part of a silent failure\. Many machine\-learning applications compare nearby distributions, where conventional KL formulas lose accuracy\. On 8\.5 million inputs with certified references across 18 closed\-form KL families andF\.kl\_div, we find widespread errors in values and gradients on CPU and GPU\. Among scalar families in ordinary parameter ranges, over 85% of FP32 values have at least 1% relative error near KL10−710^\{\-7\}\(FP16:10−310^\{\-3\}\), and about 30% become negative at smaller divergences\. More precision is not the cure: evaluated one precision higher, the same formulas still leave 7–51% of FP32 values significantly wrong for 22 of 23 configurations\. Stable closed\-form decompositions, compensated arithmetic, and analytic gradients remove over 97% of significant errors in the working format\. Strict repairs eliminate significant errors on 7\.2 million scored closed\-form samples and correctly round all 567 million scoredF\.kl\_divcomponents\. The errors affect parameter fitting, Bayesian decisions, identical\-start distillation \(up to 97% top\-1 disagreement\), KL\-based layer rankings in a quantized LLM, and 19–59% of GPT\-2 circuit edge scores\. With the repaired code they all but vanish\. Code and data:[https://github\.com/xi\-xiaoran/stablekl](https://github.com/xi-xiaoran/stablekl)\.

###### Keywords:

KL divergence, floating point, numerical stability, PyTorch, distillation, quantization

## 1Introduction

The Kullback–Leibler \(KL\) divergence\([Kullback and Leibler, 1951](https://arxiv.org/html/2611.30835#bib.bib1)\)between two distributions is non\-negative, and zero only if they coincide, but code does not always agree\. Since 2019, users of PyTorch\([Paszke et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib21)\)have repeatedly reported negative values from both of its KL functions, and the answer has been the same each time: this is floating\-point error, clamp the result at zero \([SectionF\.2](https://arxiv.org/html/2611.30835#A6.SS2)\)\. Clamping hides the symptom and leaves the error\. Clamping negative KL estimates sets their values and backpropagated gradients to zero\. Many inaccurate KL estimates are positive and finite\.

The KL divergence is the regulariser of variational autoencoders\([Kingma and Welling, 2014](https://arxiv.org/html/2611.30835#bib.bib30)\), the trust\-region constraint of policy optimisation\([Schulman et al\., 2015](https://arxiv.org/html/2611.30835#bib.bib33);[Schulman et al\., 2017](https://arxiv.org/html/2611.30835#bib.bib34);[Shao et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib36)\), the loss of knowledge distillation\([Hinton et al\., 2015](https://arxiv.org/html/2611.30835#bib.bib26);[Agarwal et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib28)\), and a standard metric for how far a compressed model has moved\([Nagel et al\., 2021](https://arxiv.org/html/2611.30835#bib.bib40);[Xin et al\., 2026](https://arxiv.org/html/2611.30835#bib.bib44)\)\. These applications often compare closely related distributions: consecutive policies, a student and its teacher, or a model and its quantized copy\. Among the 116 PyTorch\-based packages we inspected, 39 call at least one of the two KL interfaces \([AppendixF](https://arxiv.org/html/2611.30835#A6)\)\.

Figure 1:The problem and one consequence\. The official PyTorch code is shown in red and ours in blue, with solid lines for FP32 and dashed lines for FP16\.\(a\)Relative error of the returned KL between two Exponential distributions as they approach each other \(median over 3000 parameter pairs per point, with FP16 shown while the true value is a normal number\)\. The official closed form is implemented correctly, yet its error reaches 100% once the true KL falls below about4×10−84\\times 10^\{\-8\}in FP32 \(4×10−44\\times 10^\{\-4\}in FP16\), where it returns zero or a negative number\. The repaired divergence stays at the accuracy of the format\.\(b\)A student initialised as an exact copy of its teacher and trained with the KL loss alone \(vocabulary 151,936, Adam, learning rate10−210^\{\-2\}, median and range over 10 seeds, other settings in[AppendixE](https://arxiv.org/html/2611.30835#A5)\)\. With the official loss the student’s top\-1 predictions leave the teacher’s, whereas with our logits path they do not move\.Closeness is exactly where the textbook formulas break down\. IfPPandQQdiffer by a relative amountδ\\delta, then typicallyKL\(P∥Q\)=O\(δ2\)\\mathrm\{KL\}\(P\\,\\\|\\,Q\)=O\(\\delta^\{2\}\), while almost every closed form computes it as a sum ofO⁡\(1\)O\(1\)orO⁡\(δ\)O\(\\delta\)terms of mixed sign\. The rounding error of those terms is of the order of the unit roundoffuu\(about6×10−86\\times 10^\{\-8\}in FP32,5×10−45\\times 10^\{\-4\}in FP16\), so the computed divergence is noise once the true KL is of the order ofuuor smaller \([Figure1](https://arxiv.org/html/2611.30835#S1.F1)\)\. It may be negative, zero for distinct distributions, or non\-zero for identical ones, and its gradient need not point downhill\. Low\-precision formats\([Micikevicius et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib18);[Kalamkar et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib19)\)move this threshold into the range of practical values\.

Numerical failures ofsoftmaxand cross\-entropy explain stalled grokking\([Prieto et al\., 2025](https://arxiv.org/html/2611.30835#bib.bib46)\)and distillation drift\([Barron et al\., 2026](https://arxiv.org/html/2611.30835#bib.bib47)\), but the KL divergence itself, across distribution families and interfaces, has not been measured\. We measure it in both KL interfaces of PyTorch,torch\.distributions\.kl\_divergence\(interface A, closed forms between two distribution objects\) andtorch\.nn\.functional\.kl\_div\(interface B, the loss behindnn\.KLDivLoss\), and show how to remove the error:

- •The failure is systematic, and silent\([Section3](https://arxiv.org/html/2611.30835#S3)\)\. On 7\.5 million parameter pairs for the 18 same\-family closed forms of interface A and 0\.94 million batches for interface B, stratified by the true KL from10−1510^\{\-15\}to11and certified by interval arithmetic, the official results degrade steadily as the distributions approach each other, for every family, on CPU and GPU\.
- •More precision is not the fix, but a different formula is\([Section4](https://arxiv.org/html/2611.30835#S4)\)\. Across 18 closed forms, we combine non\-negative decompositions, compensated arithmetic, special\-function evaluation, and analytic gradients\. ForF\.kl\_div, we repair signed elements, reductions, and gradients\. The rewrites remove over 97% of significant errors in the working precision, and strict repairs yield no significant errors on 7\.2 million scored closed\-form samples and correctly round all 567 million interface\-B components\.
- •Two error sources must be told apart\([Section2](https://arxiv.org/html/2611.30835#S2)\): the*operator error*ofF\.kl\_divon the inputs it receives, and the*input\-representation error*already present in rounded \(log\-\)probabilities, which no implementation ofF\.kl\_divcan repair and which requires a divergence computed from logits\.
- •The errors change outcomes\([Section5](https://arxiv.org/html/2611.30835#S5)\), from fitting a single distribution and a sequential Bayesian experiment to distillation, KL\-based decisions for a quantized LLM and circuit analysis of GPT\-2 models\.

## 2Background and Error Model

#### Floating point\.

We use the standard modelfl⁡\(a∘b\)=\(a∘b\)​\(1\+ϵ\)\\mathrm\{fl\}\(a\\circ b\)=\(a\\circ b\)\(1\+\\epsilon\),\|ϵ\|≤u\|\\epsilon\|\\leq u, for∘∈\{\+,−,×,/\}\\circ\\in\\\{\+,\-,\\times,/\\\}\([Higham, 2002](https://arxiv.org/html/2611.30835#bib.bib3)\), for binary16 \(FP16\), binary32 \(FP32\), binary64 \(FP64\)\([IEEE, 2019](https://arxiv.org/html/2611.30835#bib.bib17)\)and bfloat16 \(BF16\)\. A result is*correctly rounded*if it equals the exact value rounded to nearest in the output format, and errors in*ULP*are in units of the spacing of the format at the exact value\. Subtracting two nearby floating\-point numbers is exact\([Sterbenz, 1974](https://arxiv.org/html/2611.30835#bib.bib5)\), but exposes the rounding errors already present in them \(*cancellation*\)\([Goldberg, 1991](https://arxiv.org/html/2611.30835#bib.bib2)\)\.

#### Cancellation near equality\.

Take two Exponential distributions with ratesλp,λq\\lambda\_\{p\},\\lambda\_\{q\}, for whichKL=r−1−log⁡r\\mathrm\{KL\}=r\-1\-\\log rwithr=λq/λp=1\+xr=\\lambda\_\{q\}/\\lambda\_\{p\}=1\+x\. The exact value isx2/2\+O⁡\(x3\)x^\{2\}/2\+O\(x^\{3\}\), butr−1−log⁡rr\-1\-\\log ris evaluated near11, where the absolute rounding error is aboutuu\. The relative error is therefore about2​u/x22u/x^\{2\}, which exceeds 100% once\|x\|<2​u≈3\.5×10−4\|x\|<\\sqrt\{2u\}\\approx 3\.5\\times 10^\{\-4\}in FP32\. For example, forλp=1\\lambda\_\{p\}=1andλq=fl⁡\(1\.0001\)\\lambda\_\{q\}=\\mathrm\{fl\}\(1\.0001\)in FP32 the true value is5\.0013×10−95\.0013\\times 10^\{\-9\}, butr−log⁡r=1\.000000005​…r\-\\log r=1\.000000005\\ldotsrounds to11and the official formula returns exactly00, so two different distributions are reported as identical \([Figure1](https://arxiv.org/html/2611.30835#S1.F1)a\)\. The same structure, a second\-order quantity obtained from first\-order or zeroth\-order terms, appears in the closed forms we examined \([AppendixB](https://arxiv.org/html/2611.30835#A2)\)\. When only the support changes \(Uniform, Pareto\), the KL is first order\. It is not removed by more precision alone, because FP32 parameters can differ byx=2−24x=2^\{\-24\}, for which the same formula in FP64, whose unit roundoff isu64=2−53u\_\{64\}=2^\{\-53\}, can have a relative error of2​u64/x2≈0\.062u\_\{64\}/x^\{2\}\\approx 0\.06\.

#### Interfaces\.

Interface A,kl\_divergence\(P,Q\)\(P,Q\), dispatches on the types of the two distribution objects to a closed form in their parameters\. Gradients come from automatic differentiation, and for families with two parameterisations \(probsorlogits\) the constructors convert and clamp the parameters first\. Interface B,F\.kl\_div\(x,t\)\(x,t\), takes a tensorxxof log\-probabilities and a targetttof probabilities \(or of log\-probabilities whenlog\_target=True\), evaluatest⁡\(log⁡t−x\)t\(\\log t\-x\)oret​\(t−x\)e^\{t\}\(t\-x\)element\-wise, and reduces\.

#### Operator error versus input\-representation error\.

For interface B the inputs are themselves outputs of a rounded computation, typicallylog\_softmaxof model logits\. LetTopT\_\{\\mathrm\{op\}\}be the exact operator result on its stored inputs andTe2eT\_\{\\mathrm\{e2e\}\}the corresponding result from the original logits, with the same reduction\. We call the difference between the computed output andTopT\_\{\\mathrm\{op\}\}the*operator error*, andTop−Te2eT\_\{\\mathrm\{op\}\}\-T\_\{\\mathrm\{e2e\}\}the*input\-representation error*\. Only the first belongs toF\.kl\_div\. A related backward error is Softmax Cancellation\([Barron et al\., 2026](https://arxiv.org/html/2611.30835#bib.bib47)\)\. The gradient of the forward KL with respect toxxinvolves no cancellation\. The cancellation happens afterwards, in the backward pass oflog\_softmax, whereq−pq\-p, the difference between the input distributionq=exq=e^\{x\}and the targetp=tp=t, is formed from two rounded probability vectors, and the result is non\-zero even when the two distributions are identical\.

## 3Numerical Errors in PyTorch

We first measure the failure, with a reference that is exact for the bit patterns the library receives, so that every reported error belongs to the implementation and not to the test\.

#### Data and ground truth\.

For interface A we test the 18 distribution\-specific same\-family closed forms in 23 parameter and dimension configurations \(five generic or delegated registrations are out of scope, and the two multivariate families are scored in FP32 only\)\. For each we drawPPfrom an ordinary parameter region R0 and from stress regions \(very large or small parameters, probabilities near 0 or 1\), and constructQQso that the*true*KL\(P∥Q\)\\mathrm\{KL\}\(P\\\|Q\)falls in a prescribed decade: one control bin\[10−1,1\)\[10^\{\-1\},1\)and 14 bins down to\[10−15,10−14\)\[10^\{\-15\},10^\{\-14\}\)\. Two further groups contain pairs whose parameters differ by 1–8 ULP, and identical pairs\. This gives 7,542,469 samples, of which 7,234,454 are scored\. Cells that the floating\-point grid cannot populate remain empty\. For interface B we generate 942,200 batches overlog\_target, FP16/FP32,K∈\{2,4,16,256,1024\}K\\in\\\{2,4,16,256,1024\\\}classes, two batch sizes and all four reductions, from logits stratified in the same way\. Parameters are stored in the target format first\. The exact KL and every gradient component are then enclosed with Arb ball arithmetic\([Johansson, 2017](https://arxiv.org/html/2611.30835#bib.bib15)\)tightly enough to determine a unique correctly rounded value in that format \([AppendicesA](https://arxiv.org/html/2611.30835#A1)and[H](https://arxiv.org/html/2611.30835#A8)\)\.

#### Methods and metrics\.

Officialis PyTorch 2\.15\.0 on CPU, and we repeated the evaluation on an NVIDIA A100 GPU \([AppendixD](https://arxiv.org/html/2611.30835#A4)\)\.Official\+, the natural “just use more precision” baseline, evaluates the unchanged official code after promoting the parameters exactly to the next format \(FP32 for FP16 inputs, FP64 for FP32 inputs\) and rounds the result once\. An output is a*significant error*if it is off by at least 8 ULP or is qualitatively wrong \(negative, NaN or infinite KL where the truth is finite and positive; spurious zero or non\-zero; sign\-flipped gradient component\)\.

Figure 2:Errors of the official interface\-A implementation \(red\) and of the same formula evaluated in the next higher precision \(green\) versus the true KL, for the 16 scalar\-family configurations in the ordinary parameter region R0\. Solid lines are FP32 inputs and dashed lines FP16 inputs\. The inset enlarges the lower right corner of the last panel\. FP16 relative error is shown only while the true KL is a normal number \(above about6×10−56\\times 10^\{\-5\}\)\.
#### Errors near equality\.

The error grows as the true KL approaches the unit roundoffuu\([Figure2](https://arxiv.org/html/2611.30835#S3.F2), which pools the 16 scalar\-family configurations in R0, with details in[SectionA\.1](https://arxiv.org/html/2611.30835#A1.SS1)\)\. In the control bin the official values are accurate \(0\.1% of the FP32 values are off by 1% or more\)\. As the KL approachesu≈6×10−8u\\approx 6\\times 10^\{\-8\}most are wrong \(86% at10−710^\{\-7\}\), and below that nearly all are, with about three in ten negative from10−1010^\{\-10\}down\. FP16 follows the same curve four decades earlier, as its roundoff predicts\. Gradients, being first\-order quantities, survive longer but fail in a worse way\. At10−1310^\{\-13\}a fifth of the gradient vectors have a cosine below 0\.99 with the true one, and at10−1510^\{\-15\}, 6\.8% point into the opposite half\-space\. No family is exempt \([Table1](https://arxiv.org/html/2611.30835#S4.T1), columnsOfficial\)\.

#### Precision promotion\.

Evaluating the same formula one format higher moves the failure; it does not remove it, because two inputs can still differ by one unit in the last place of the*input*format \([Section2](https://arxiv.org/html/2611.30835#S2)\)\. For FP32 inputsOfficial\+shifts the curve of[Figure2](https://arxiv.org/html/2611.30835#S3.F2)about nine decades down \(56% off by≥1%\\geq 1\\%at10−1510^\{\-15\}\), and for FP16 inputs it leaves a residue \(13% at10−510^\{\-5\}\)\. Over all bins and regions it leaves 7–51% of the FP32 KL values significantly wrong in 22 of 23 configurations \([Table1](https://arxiv.org/html/2611.30835#S4.T1)\), Uniform being the only exception\.

#### Loss\-operator errors\.

F\.kl\_divfails in a different way\. Its elementt⁡\(log⁡t−x\)t\(\\log t\-x\)cancels whenevert≈ext\\approx e^\{x\}\. For probability targets 35–70% of the FP32sumoutputs are off by at least 1%, and scaled reductions can produce large target\-gradient errors \(FP16,mean,K=256K=256: 95\.4% of gradient rows contain a component of the wrong sign\)\. Unlike the closed forms, these*operator*errors essentially disappear in FP64, but the error inherited from the inputs does not, sinceTopT\_\{\\mathrm\{op\}\}differs fromTe2eT\_\{\\mathrm\{e2e\}\}by≥1%\\geq 1\\%on 40–99% of the batches, in any precision of the operator \([AppendixC](https://arxiv.org/html/2611.30835#A3)\)\.

#### CPU and GPU\.

The failure persists on the GPU\. On an A100 the aggregate significant\-error rates for interface A differ from the CPU rates by at most 1\.5 percentage points\. ForF\.kl\_div, 50\.0% of forward and 71\.1% of non\-boundary backward components are correctly rounded \(CPU: 52\.5% and 76\.4%\)\. The two devices nevertheless do not return the same bits, and 27% of the FP32 KL values ofOfficialdiffer\. Our strict repairOurs\([Section4](https://arxiv.org/html/2611.30835#S4)\) has no significant error on either device \([AppendixD](https://arxiv.org/html/2611.30835#A4)\)\.

## 4Stable KL Computation

The repairs of the 18 closed forms differ in detail \([AppendixB](https://arxiv.org/html/2611.30835#A2)\) but share four ingredients, which we illustrate with the smallest example that needs each\.F\.kl\_divadditionally requires accurate signed elements and reductions \([AppendixC](https://arxiv.org/html/2611.30835#A3)\)\.

#### Stable decompositions\.

The function

φ⁡\(x\)=x−log1p⁡\(x\)≥0,x\>−1,\\varphi\(x\)=x\-\\operatorname\{log1p\}\(x\)\\;\\geq 0,\\qquad x\>\-1,\(1\)is the Exponential KL withx=\(λq−λp\)/λpx=\(\\lambda\_\{q\}\-\\lambda\_\{p\}\)/\\lambda\_\{p\}, and it is the building block of almost every other family\. For example, for two normal distributions𝒩p=𝒩⁡\(μp,σp2\)\\mathcal\{N\}\_\{p\}=\\mathcal\{N\}\(\\mu\_\{p\},\\sigma\_\{p\}^\{2\}\)and𝒩q=𝒩⁡\(μq,σq2\)\\mathcal\{N\}\_\{q\}=\\mathcal\{N\}\(\\mu\_\{q\},\\sigma\_\{q\}^\{2\}\), withe=\(σp−σq\)/σqe=\(\\sigma\_\{p\}\-\\sigma\_\{q\}\)/\\sigma\_\{q\}andw=\(μp−μq\)/σqw=\(\\mu\_\{p\}\-\\mu\_\{q\}\)/\\sigma\_\{q\},

KL\(𝒩p∥𝒩q\)=φ\(e\)\+12e2\+12w2,\\mathrm\{KL\}\\big\(\\mathcal\{N\}\_\{p\}\\,\\\|\\,\\mathcal\{N\}\_\{q\}\\big\)=\\varphi\(e\)\+\\tfrac\{1\}\{2\}e^\{2\}\+\\tfrac\{1\}\{2\}w^\{2\},\(2\)whereas the official code evaluates12​\(r2\+w2−1−log⁡r2\)\\tfrac\{1\}\{2\}\(r^\{2\}\+w^\{2\}\-1\-\\log r^\{2\}\)withr=σp/σqr=\\sigma\_\{p\}/\\sigma\_\{q\}, in whichw2w^\{2\}is absorbed byr2≈1r^\{2\}\\approx 1\. Analogous identities \([AppendixB](https://arxiv.org/html/2611.30835#A2), which defines each argument\) giveKL=log1p⁡\(r\)\\mathrm\{KL\}=\\operatorname\{log1p\}\(r\)withr≥0r\\geq 0for Cauchy and Uniform,p​φ​\(x1\)\+\(1−p\)​φ​\(x2\)p\\,\\varphi\(x\_\{1\}\)\+\(1\-p\)\\,\\varphi\(x\_\{2\}\)for Bernoulli,∑kpk​φ​\(ρk\)\\sum\_\{k\}p\_\{k\}\\varphi\(\\rho\_\{k\}\)for Categorical,∑iφ⁡\(Ni​i\)\+12​‖N‖F2\+12​‖v‖2\\sum\_\{i\}\\varphi\(N\_\{ii\}\)\+\\frac\{1\}\{2\}\\\|N\\\|\_\{F\}^\{2\}\+\\frac\{1\}\{2\}\\\|v\\\|^\{2\}for the multivariate normal, and sums ofφ\\varphiterms and Bregman divergences of convex functions for Gamma, Beta and Dirichlet\. Every term is non\-negative, so the sum is as accurate as its terms, cannot be negative, and is exactly zero when all terms are\.φ\\varphiitself still cancels asx→0x\\to 0, so on−34≤x≤3\-\\tfrac\{3\}\{4\}\\leq x\\leq 3we use the exact identitieslog1p⁡\(x\)=2​atanh⁡\(s\)\\operatorname\{log1p\}\(x\)=2\\operatorname\{atanh\}\(s\),s=x/\(2\+x\)s=x/\(2\+x\), to write

φ⁡\(x\)=x22\+x−2​s3​∑k≥1s2​\(k−1\)2​k\+1,\\varphi\(x\)=\\frac\{x^\{2\}\}\{2\+x\}\-2s^\{3\}\\sum\_\{k\\geq 1\}\\frac\{s^\{2\(k\-1\)\}\}\{2k\+1\},\(3\)a convergent series whose truncation after 9/17/36 terms \(FP16/FP32/FP64\) is bounded rigorously byu/16u/16relative toφ\\varphi\.

#### Accurate differences\.

A non\-negative decomposition is only useful if its arguments \(xx,ee,ρk\\rho\_\{k\},NN\) are accurate relative to their own size\. We therefore compute them from differences of the inputs, which are exact for nearby floating\-point numbers\([Sterbenz, 1974](https://arxiv.org/html/2611.30835#bib.bib5)\), and, where a difference of products is needed \(e\.g\.Nk=yk​Sx−xk​SyN\_\{k\}=y\_\{k\}S\_\{x\}\-x\_\{k\}S\_\{y\}for Categorical parametersxx,yywith sumsSxS\_\{x\},SyS\_\{y\},[SectionB\.16](https://arxiv.org/html/2611.30835#A2.SS16)\), from error\-free transformations\([Knuth, 1997](https://arxiv.org/html/2611.30835#bib.bib7);[Dekker, 1971](https://arxiv.org/html/2611.30835#bib.bib6);[Shewchuk, 1997](https://arxiv.org/html/2611.30835#bib.bib8);[Ogita et al\., 2005](https://arxiv.org/html/2611.30835#bib.bib9)\)\. As a consequence, structurally exact zeros are returned as\+0\+0, including pairs whose parameters differ but whose distributions coincide \(proportionalprobs, shiftedlogits, different low\-rank factors of one covariance\)\.

#### Analytic gradients\.

Automatic differentiation of a cancelling formula inherits the cancellation and adds failure modes of its own\. We implement the derivative of the rewritten form directly, e\.g\.∂KL/∂σp=e⁡\(e\+2\)/σp\\partial\\mathrm\{KL\}/\\partial\\sigma\_\{p\}=e\(e\+2\)/\\sigma\_\{p\}for[Equation2](https://arxiv.org/html/2611.30835#S4.E2), evaluating the resulting products and differences with stable arithmetic\. Where a gradient has a genuine zero crossing, the residual cancellation is intrinsic in the working precision and is resolved by the strict mode below\.

#### Strict rounding\.

Rewriteruns the rewritten algorithm entirely in the target precision, with compensated arithmetic\. The strict variant,Ours, evaluates the same algorithm in FP64 and rounds once to the target format\. Rounding once matters, because FP64→\\toFP32→\\toFP16 is a double rounding \([SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1)\)\. ForF\.kl\_divwe additionally guard the last rounding\. If the FP64 value is closer to a rounding boundary than its a\-priori error bound, the sum is re\-evaluated with interval arithmetic at increasing precision, in the spirit of[Ziv \(1991\)](https://arxiv.org/html/2611.30835#bib.bib11)\.

Table 1:Significant KL error rates \(%\) for interface A, across all KL bins and parameter regions\. Entries are percentages of scored inputs with≥8\\geq 8ULP error or a qualitative failure\.NNcounts scored inputs across supported formats\.Officialis PyTorch,Off\.\+the promoted official formula,Rewriteour same\-format formula andOursthe strict repair\. Ranges span dimension settings\. The two multivariate families lack official FP16 support\. Gradient results are in[AppendixB](https://arxiv.org/html/2611.30835#A2)\.
#### Results on the frozen data\.

[Table1](https://arxiv.org/html/2611.30835#S4.T1)compares the four methods on exactly the data of[Section3](https://arxiv.org/html/2611.30835#S3)\. The formula does most of the repair\.Rewrite, without promoting the input or output format, removes more than 97% of the significant KL errors in every configuration and all of them in seven\. What remains \(at most 0\.7% for FP32 and 2\.5% for FP16 inputs, 1\.6% in any gradient component,[AppendixB](https://arxiv.org/html/2611.30835#A2)\) sits mostly in gradients near their zero crossings and in the Beta and Dirichlet families\. The price is arithmetic rather than precision, at 2\.4–5\.9×\\timesthe official call on a GPU for five of seven timed families \([SectionC\.4](https://arxiv.org/html/2611.30835#A3.SS4)\)\. Conversely,Official\+shows that precision without the formula leaves up to half of the FP32 values wrong\. The strict mode removes the residue\.Ourshas no significant error in the KL value or in any gradient component on any of the 7,234,454 scored samples, returns exactly zero value and \(where it exists\) gradient at every identity sample, and never returns a negative KL or a NaN\.

For interface B \(942,200 batches\),Oursis correctly rounded on all 83,899,496 forward and 483,279,156 backward scored components, against 52\.5% and 74\.9% forOfficial, with no regression\.

#### From logits\.

For teacher logitsaaand student logitscc, we computeKL\(softmax\(a\)∥softmax\(c\)\)\\mathrm\{KL\}\(\\operatorname\{softmax\}\(a\)\\,\\\|\\,\\operatorname\{softmax\}\(c\)\)directly from logits\. Letq=softmax⁡\(c\)q=\\operatorname\{softmax\}\(c\)be computed in FP64, chooser=arg⁡maxj⁡cjr=\\arg\\max\_\{j\}c\_\{j\}, and form the compensated, centred differencesδj=\(aj−cj\)−\(ar−cr\)\\delta\_\{j\}=\(a\_\{j\}\-c\_\{j\}\)\-\(a\_\{r\}\-c\_\{r\}\)\([SectionB\.16](https://arxiv.org/html/2611.30835#A2.SS16)\),

Δ\\displaystyle\\Delta=log1p⁡\(∑jqj​expm1⁡\(δj\)\),\\displaystyle=\\operatorname\{log1p\}\\Big\(\\sum\\nolimits\_\{j\}q\_\{j\}\\,\\operatorname\{expm1\}\(\\delta\_\{j\}\)\\Big\),\(4\)wj\\displaystyle w\_\{j\}=δj−Δ,\\displaystyle=\\delta\_\{j\}\-\\Delta,KL\\displaystyle\\mathrm\{KL\}=∑jqj​\(wj​ewj−expm1⁡\(wj\)\),\\displaystyle=\\sum\\nolimits\_\{j\}q\_\{j\}\\big\(w\_\{j\}e^\{w\_\{j\}\}\-\\operatorname\{expm1\}\(w\_\{j\}\)\\big\),∂KL/∂cj\\displaystyle\\partial\\mathrm\{KL\}/\\partial c\_\{j\}=−qj​expm1⁡\(wj\)\.\\displaystyle=\-q\_\{j\}\\,\\operatorname\{expm1\}\(w\_\{j\}\)\.Herepj=qj​ewjp\_\{j\}=q\_\{j\}e^\{w\_\{j\}\}is the teacher probability andh⁡\(w\)=w​ew−expm1⁡\(w\)≥0h\(w\)=we^\{w\}\-\\operatorname\{expm1\}\(w\)\\geq 0for every realww\. Near zerohhis evaluated by its series∑n≥2\(n−1\)​wn/n\!\\sum\_\{n\\geq 2\}\(n\-1\)w^\{n\}/n\!, and atδ=0\\delta=0the KL and its logit gradients are exactly zero\. This*logits path*, a row\-vectorised form of theCategorical\(logits\)repair of[Table1](https://arxiv.org/html/2611.30835#S4.T1), is what the distillation and language\-model experiments use\. It is tested against high\-precision references and is closely related to the fused soft\-target loss of[Barron et al\. \(2026\)](https://arxiv.org/html/2611.30835#bib.bib47)\.

## 5Downstream Experiments

Do the wrong numbers matter? Four experiments, ordered by scale, compare the official implementation with the repaired one in tasks where nothing else changes\.

### 5\.1Parameter Fitting

#### Setup\.

Fitting one distribution to another ends where loss and gradient must both vanish, the regime of[Section3](https://arxiv.org/html/2611.30835#S3)\. We fitGeo⁡\(p\)\\mathrm\{Geo\}\(p\)to a fixedGeo⁡\(p⋆\)\\mathrm\{Geo\}\(p^\{\\star\}\)by optimising the logitθ\\theta, withp=σ⁡\(θ\)p=\\sigma\(\\theta\), using Adam \(learning rate10−210^\{\-2\}, 3000 steps\) from 12 shared starts, forp⋆∈\{0\.9,0\.3,0\.01,0\.001\}p^\{\\star\}\\in\\\{0\.9,0\.3,0\.01,0\.001\\\}, FP16 and FP32, both parameterisations and both KL directions \(32 settings\), withθ\\thetakept in FP32 and the true KL computed with 60\-digit arithmetic\.

#### Result\.

The official loss can miss a target that it is given exactly\. In FP16 withp⋆=0\.001p^\{\\star\}=0\.001andKL\(Geo\(p\)∥Geo\(p⋆\)\)\\mathrm\{KL\}\(\\mathrm\{Geo\}\(p\)\\\|\\mathrm\{Geo\}\(p^\{\\star\}\)\)it reaches the target from none of the 12 starts \(median relative deviation of the fittedpp95%, true KL up to 1\.7\), while the repaired loss converges to the target bit pattern from all 12\. In FP32, forp⋆≤0\.01p^\{\\star\}\\leq 0\.01, it stalls at relative deviations of up to4\.3×10−44\.3\\times 10^\{\-4\}, almost a hundred times further out than the repaired one\. The reason is visible at the optimum itself, where the official code returns a non\-zero loss or gradient in 28 of 32 settings \(e\.g\. loss 0\.098 and gradient−100\-100for FP16,p⋆=0\.001p^\{\\star\}=0\.001\)\. The repair returns exact zeros in all 32\. The official runs do not converge to a biased point but wander \([Figure7](https://arxiv.org/html/2611.30835#A5.F7)\)\.

### 5\.2Sequential Bayesian Design

#### Setup\.

Bayesian experimental design chooses the next observation by its expected information gain \(EIG\), the expected KL between the posterior after the observation and the current one\([Lindley, 1956](https://arxiv.org/html/2611.30835#bib.bib62);[Chaloner and Verdinelli, 1995](https://arxiv.org/html/2611.30835#bib.bib63)\)\. We takeK=10K=10Bernoulli arms with Beta posteriors\. In every round the arm with the largest EIG receives the nextmmobservations, 100,000 in total, form∈\{1,100,1000,104\}m\\in\\\{1,100,1000,10^\{4\}\\\}, from a cold and from a warm start, with 5 seeds\. The KL is the official FP32kl\_divergence\(Beta, Beta\)or our rewrite, also in FP32\. The reference policy uses the exact EIG\. A choice is counted as wrong if its exact EIG is more than 0\.1% below the maximum\. The experiment drives itself into the failing range, because the gain of one more observation shrinks with the number already made, and forBeta⁡\(n\+1,n\)\\mathrm\{Beta\}\(n\{\+\}1,n\)againstBeta⁡\(n,n\)\\mathrm\{Beta\}\(n,n\)withn=105n=10^\{5\}the true KL is2\.5×10−62\.5\\times 10^\{\-6\}and the official code returns\+0\.31\+0\.31\.

#### Result\.

The official KL misdirects the experiment: in seven of the eight settings 40–91% of the rounds choose a wrong arm, and 35–60% of the EIG values it computes there are zero or negative \([Table12](https://arxiv.org/html/2611.30835#A5.T12)\)\. With the rewrite no round is wrong in any of the 40 runs\. The cost depends on the feedback\. When the posterior is updated after every observation \(m=1m=1\), a wrong choice costs one observation, and the final posterior variance is only 1–10% higher than with the exact policy\. When a choice commits a batch, the loss can be much larger\. Form=104m=10^\{4\}from a warm start the total variance is 1\.2 to 5\.2 times and that of the worst arm up to 11 times the exact policy’s, because an arm with few observations is passed over in most rounds\.

### 5\.3Identical\-Start Distillation

#### Setup\.

A student MLP \(32→256→V32\\to 256\\to V\) is initialised as an exact copy of its teacher and trained for 300 steps with the forward KL as the only loss\. The exact loss and gradient are zero, so any movement is numerical\. We run the full grid of vocabularyV∈\{1024,32768,151936\}V\\in\\\{1024,32768,151936\\\}, precision∈\{\\in\\\{FP32, FP16, BF16\}\\\}, optimiser and learning rate \(Adam at five rates, SGD at three\), four losses and 10 seeds, 2,880 runs in total\. The official losses areF\.kl\_divonsoftmax/log\_softmaxoutputs \(with and withoutlog\_target\) and the officialCategorical\(logits\)KL\. Ours is the logits path\.

Table 2:Top\-1 disagreement \(%\) between student and teacher after 300 steps from an identical initialisation \(median over 10 seeds\)\. Each cell gives officialF\.kl\_div/ours\. The other two official variants agree with the first within 2\.1 points in every cell\. “0” means that no weight changed in any seed\.
#### Result\.

With the official losses the student moves in all 2,160 runs\. With ours it does not move in any of the 720 runs\. Every recorded weight change, the initial loss and the initial gradient norm are exactly zero \([Table2](https://arxiv.org/html/2611.30835#S5.T2)\)\. The optimiser determines how this small numerical error is translated into a parameter update\. In FP32 the spurious initial gradient has a norm of about5×10−85\\times 10^\{\-8\}\. SGD scales its step with this norm and barely moves, whereas Adam normalises it to a full\-size step\([Kingma and Ba, 2015](https://arxiv.org/html/2611.30835#bib.bib45)\), so that the drift grows with the learning rate, from below 1% to 31–47% top\-1 disagreement in FP32\. The largest vocabulary is worse than the smallest in every Adam comparison, most strongly in FP16, where the disagreement reaches 97\.4% and the final true KL 3\.6 nats\. A visible symptom is the one users report\. A quarter of the official runs show a negative loss at step 0\.

#### Attribution\.

The three official variants exhibit similar drift\. In the paired FP16 and FP32 tests, replacingF\.kl\_divalone yields student weights bit\-identical to the official runs\. The spurious parameter gradient arises inlog\_softmaxbackward, which the logits path avoids\([Barron et al\., 2026](https://arxiv.org/html/2611.30835#bib.bib47), see also\)\.

### 5\.4KL in Language Models

#### Setup\.

We use Qwen2\.5\-1\.5B\-Instruct\([Yang et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib56)\)\(vocabulary 151,936, FP16 forward\) on 16,384 WikiText\-2 tokens\([Merity et al\., 2017](https://arxiv.org/html/2611.30835#bib.bib57)\)and computeKL\(original∥quantized\)\\mathrm\{KL\}\(\\text\{original\}\\,\\\|\\,\\text\{quantized\}\)per token\. Part A evaluates six quantized versions \(round\-to\-nearest, RTN, at 8, 4 and 3 bits, GPTQ\([Frantar et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib37)\)at 8 and 4, AWQ\([Lin et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib38)\)at 4\)\. Part B ranks the 28 decoder layers by the KL caused by quantizing one layer alone, and uses the ranking to keep thek∈\{4,8\}k\\in\\\{4,8\\\}most sensitive layers in FP16, a standard mixed\-precision heuristic\([Dong et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib41);[Cai et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib42)\)\. A decision counts as harmful if it raises the true KL of the mixed model\. References are computed in FP64\.

Table 3:KL errors and layer rankings at 8\-bit quantization \(FP16\)\. Part A reports signed relative error \(%\) in mean KL and the percentage of tokens with negative or infinite KL \(GPTQ / RTN\)\. Part B reports inverted layer pairs out of 378 and Kendall’sτb\\tau\_\{b\}\(1 indicates perfect agreement\)\. “B fix” repairsF\.kl\_divon the same rounded probability inputs\.Table 4:KL between cached decoding and full forward evaluation of the same model\. The first numeric column is the FP64 reference KL, and all remaining values are signed relative errors \(%\) at model precision\.Table 5:Circuit edge\-score accuracy against the FP64 KL of the same stored logits\. “Wrong” is the percentage of positive\-reference edges with negative scores or relative error above 10%, and “negative” gives their count\. Spearman measures ranking agreement, and top\-1,000 overlap is the fraction of reference top edges recovered\. For GPT\-2 medium a stratified 13% sample of the edges is scored\. Detailed values appear in[Table18](https://arxiv.org/html/2611.30835#A5.T18)\.
#### Part A: KL Accuracy\.

The official KL is least reliable for the quantized models closest to the original\. For the two near\-lossless 8\-bit models the officialF\.kl\_divmisreports the mean KL by−19\.0%\-19\.0\\%and−4\.4%\-4\.4\\%, with 30\.2% and 12\.3% of the tokens negative \([Table3](https://arxiv.org/html/2611.30835#S5.T3)\), and withreduction="mean"it reports exactly 0\.Categorical\(logits\)returns\+∞\+\\inftyfor almost every token, because about 38% of the FP16softmaxprobabilities underflow to zero\. Repairing the operator alone is not enough\. The repairedF\.kl\_divis correctly rounded for its inputs on every token, yet 98\.8% and 96\.8% of the tokens remain off by≥1%\\geq 1\\%, because the error is already in the FP16softmaxoutputs\. The logits path equals the rounded FP64 reference on every token of every model in FP16 and FP32\.

#### Part B: Layer Ranking\.

A ranking depends on differences between small numbers, and here it breaks\. Single\-layer divergences at 8 bits are only3\.6×10−53\.6\\times 10^\{\-5\}to2\.6×10−42\.6\\times 10^\{\-4\}\. The official FP16F\.kl\_divreports a negative mean KL for 24 of 28 layers and inverts 19 of the 378 layer pairs \(up to 158 for the BF16 variants\), whilereduction="mean"reports 0 for all layers andCategoricalis infinite, so neither can rank at all\. The logits path has no inversion in FP16 or FP32\. The decision itself changes at the boundary\. Fork=8k=8, several configurations protect one wrong layer, and the BF16CategoricalKL protects four, which raises the true KL of the resulting mixed model by 9\.8% on the disjoint evaluation set\.

#### Part C: Execution Gaps\.

The KL also measures how far two executions of one model are apart, such as rollout and training in RL fine\-tuning\([Qi et al\., 2025](https://arxiv.org/html/2611.30835#bib.bib49)\)\. Each precision produces gaps of the size that its own KL cannot resolve \([Table4](https://arxiv.org/html/2611.30835#S5.T4)\)\. For cached decoding against a full forward pass the officialF\.kl\_divis off by more than 600% in FP32 and FP16 and by 19% in BF16, the logits path by less than 0\.05% on all 11 pairs\. Nor should a reported KL depend on the batch size, yet in FP16 with the default reduction \(mean\) the KL to an 8\-bit copy is reported as anything between 0 and 58% of the truth, depending on the number of rows per call, because the mean underflows, while the logits path varies by less than 0\.5% \([AppendixE](https://arxiv.org/html/2611.30835#A5)\)\.

#### Part D: Circuit Scores\.

Circuit discovery scores each edge of a model by the KL between the clean output and the output with that edge ablated\([Conmy et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib59)\)\. Most edges matter little, so most of these scores are tiny, and many of them fall in the failing range\. We score edges one at a time with the ablation loop of theauto\-circuitlibrary\([Miller et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib60)\): IOI\([Wang et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib61)\)and Greater\-than\([Hanna et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib64)\)on GPT\-2 small, Docstring\([Heimersheim and Janiak, 2023](https://arxiv.org/html/2611.30835#bib.bib65)\)on a 4\-layer attention\-only model, and IOI on GPT\-2 medium\.

In FP32, the default precision, the library’s score is wrong for 19%, 46% and 59% of the edges in the three GPT\-2 settings, and thousands of scores are negative \([Table5](https://arxiv.org/html/2611.30835#S5.T5)\), while the logits path is wrong for none\. The errors sit in the scores below about10−610^\{\-6\}, so they change which small edges pass a threshold while the ranking of the largest edges changes little, and the larger model is affected more because its scores are smaller \(median3×10−83\\times 10^\{\-8\}against9×10−79\\times 10^\{\-7\}\)\. In FP16 the ranking suffers as well\. The score is wrong for 54–81% of the edges in three of the four settings, and on GPT\-2 medium only 38% of the 1,000 highest\-scoring edges are correct, against 99\.9% for the logits path\. Wrong\-score rates alone do not determine ranking stability\. In Docstring FP32, 2\.4% wrong scores leave the top\-1,000 set unchanged, whereas in GPT\-2 small IOI with BF16, 2\.5% wrong scores yield a top\-1,000 overlap of only 0\.849\.

## 6Related Work

The work closest to ours is that of[Barron et al\. \(2026\)](https://arxiv.org/html/2611.30835#bib.bib47), who identify two bugs in standardsoftmax/cross\-entropy code,*Softmax Collapse*\(first diagnosed by[Prieto et al\. \(2025\)](https://arxiv.org/html/2611.30835#bib.bib46)\) and*Softmax Cancellation*, and show that a fused implementation stops distillation drift\. Our distillation experiment \([Section5\.3](https://arxiv.org/html/2611.30835#S5.SS3)\) observes the same drift independently, and our logits path is closely related to their soft\-target loss\. They treat the categorical soft\-target loss and its training dynamics\. We measure and repair the KL interfaces themselves, across 18 families and with certified ground truth, separate operator from input\-representation error, and study evaluation and decisions as well as training\. Like other work that traces a training failure to one floating\-point mechanism\([Prieto et al\., 2025](https://arxiv.org/html/2611.30835#bib.bib46);[Mishne et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib66);[Wortsman et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib67)\), we isolate a mechanism, but a mostly silent one\. Studies and detectors of numerical defects in deep\-learning software\([Zhang et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib50);[Humbatova et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib51);[Wang et al\., 2022](https://arxiv.org/html/2611.30835#bib.bib52);[Kloberdanz et al\., 2022](https://arxiv.org/html/2611.30835#bib.bib53);[Zhang et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib55);[Yan et al\., 2021](https://arxiv.org/html/2611.30835#bib.bib54)\)focus on exceptional values\. Our tools are those of classical numerical analysis\([Higham, 2002](https://arxiv.org/html/2611.30835#bib.bib3);[Sterbenz, 1974](https://arxiv.org/html/2611.30835#bib.bib5);[Dekker, 1971](https://arxiv.org/html/2611.30835#bib.bib6);[Shewchuk, 1997](https://arxiv.org/html/2611.30835#bib.bib8);[Ogita et al\., 2005](https://arxiv.org/html/2611.30835#bib.bib9);[Ziv, 1991](https://arxiv.org/html/2611.30835#bib.bib11);[Johansson, 2017](https://arxiv.org/html/2611.30835#bib.bib15)\)\. Automatic rewriting\([Panchekha et al\., 2015](https://arxiv.org/html/2611.30835#bib.bib14)\)targets local rewrites, whereas those needed here are global and must also deliver gradients\. Low\-precision training and inference\([Micikevicius et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib18);[Kalamkar et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib19);[Qi et al\., 2025](https://arxiv.org/html/2611.30835#bib.bib49);[Yuan et al\., 2025](https://arxiv.org/html/2611.30835#bib.bib48)\)make the regime we study routine \(see[AppendixG](https://arxiv.org/html/2611.30835#A7)\)\.

## 7Limitations and Conclusion

#### Limitations\.

Error rates are conditional on a sampling design that oversamples small divergences and stress regions on purpose\. We evaluate PyTorch 2\.15\.0 on CPU and 2\.4\.0 on an A100 GPU\. The repairs are unoptimised reference implementations\. On a GPU the rewrite costs 2\.4–5\.9×\\timesthe official call for five closed forms, and the repairs of Gamma, Beta andF\.kl\_divfar more \([SectionC\.4](https://arxiv.org/html/2611.30835#A3.SS4)\)\. The BF16 results are exploratory\. The first three experiments are synthetic or simulated, the training effect is shown for an identical start only, and in the layer\-ranking experiment the most common official configuration still made the correct selection\.[SectionF\.3](https://arxiv.org/html/2611.30835#A6.SS3)lists settings with no measured effect\.

#### Conclusion\.

Negative KL outputs reveal a broader numerical failure: many inaccurate KL values remain non\-negative, finite and unnoticed\. The cause is structural: the divergence between nearby distributions is typically a second\-order quantity assembled from larger terms\. For the closed forms, higher precision alone does not remove the resulting errors\. Rewriting does\. The practical advice is to compute divergences from parameters or logits rather than rounded probabilities, sum non\-negative terms, and add precision only where correct rounding is required\.

## Impact Statement

This paper aims to make a basic numerical primitive of machine learning more reliable\. We do not foresee specific negative societal consequences of this work\.

## AI Disclosure Statement

ChatGPT and Claude were used to assist with coding, debugging, technical discussions, and grammar checking\. All mathematical derivations, implementations, and experimental results were verified by the authors, who take full responsibility for the work\.

## References

- Agarwalet al\.\(2024\)R\. Agarwal, N\. Vieillard, Y\. Zhou, P\. Stanczyk, S\. Ramos, M\. Geist, and O\. BachemOn\-policy distillation of language models: learning from self\-generated mistakes\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Barronet al\.\(2026\)J\. T\. Barron, D\. Gnanapragasam, and M\. TschannenA fix for softmax and cross\-entropy\.arXiv preprint arXiv:2610\.31642\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px1.p1.1),[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px12.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p4.1),[§2](https://arxiv.org/html/2611.30835#S2.SS0.SSS0.Px4.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px6.p1.3),[§5\.3](https://arxiv.org/html/2611.30835#S5.SS3.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Binghamet al\.\(2019\)E\. Bingham, J\. P\. Chen, M\. Jankowiak, F\. Obermeyer, N\. Pradhan, T\. Karaletsos, R\. Singh, P\. Szerlip, P\. Horsfall, and N\. D\. GoodmanPyro: deep universal probabilistic programming\.Journal of Machine Learning Research20\(28\),pp\. 1–6\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1)\.
- Blanchardet al\.\(2021\)P\. Blanchard, D\. J\. Higham, and N\. J\. HighamAccurately computing the log\-sum\-exp and softmax functions\.IMA Journal of Numerical Analysis41\(4\),pp\. 2311–2330\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px1.p1.1)\.
- Bradburyet al\.\(2018\)J\. Bradbury, R\. Frostig, P\. Hawkins, M\. J\. Johnson, C\. Leary, D\. Maclaurin, G\. Necula, A\. Paszke, J\. VanderPlas, S\. Wanderman\-Milne, and Q\. ZhangJAX: composable transformations of Python\+NumPy programs\.Note:[http://github\.com/jax\-ml/jax](http://github.com/jax-ml/jax)Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px1.p1.1)\.
- Caiet al\.\(2020\)Y\. Cai, Z\. Yao, Z\. Dong, A\. Gholami, M\. W\. Mahoney, and K\. KeutzerZeroQ: a novel zero shot quantization framework\.InIEEE/CVF Conference on Computer Vision and Pattern Recognition \(CVPR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px1.p1.1)\.
- Carpenteret al\.\(2017\)B\. Carpenter, A\. Gelman, M\. D\. Hoffman, D\. Lee, B\. Goodrich, M\. Betancourt, M\. Brubaker, J\. Guo, P\. Li, and A\. RiddellStan: a probabilistic programming language\.Journal of Statistical Software76\(1\)\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1)\.
- Chaloner and Verdinelli \(1995\)K\. Chaloner and I\. VerdinelliBayesian experimental design: a review\.Statistical Science10\(3\),pp\. 273–304\.Cited by:[§E\.2](https://arxiv.org/html/2611.30835#A5.SS2.SSS0.Px1.p1.1),[§5\.2](https://arxiv.org/html/2611.30835#S5.SS2.SSS0.Px1.p1.1)\.
- Conmyet al\.\(2023\)A\. Conmy, A\. N\. Mavor\-Parker, A\. Lynch, S\. Heimersheim, and A\. Garriga\-AlonsoTowards automated circuit discovery for mechanistic interpretability\.InAdvances in Neural Information Processing Systems,Cited by:[§E\.4](https://arxiv.org/html/2611.30835#A5.SS4.SSS0.Px8.p1.1),[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px5.p1.1)\.
- Dekker \(1971\)T\. J\. DekkerA floating\-point technique for extending the available precision\.Numerische Mathematik18\(3\),pp\. 224–242\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px8.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px2.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Dillonet al\.\(2017\)J\. V\. Dillon, I\. Langmore, D\. Tran, E\. Brevdo, S\. Vasudevan, D\. Moore, B\. Patton, A\. Alemi, M\. Hoffman, and R\. A\. SaurousTensorFlow distributions\.arXiv preprint arXiv:1711\.10604\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1)\.
- Donget al\.\(2019\)Z\. Dong, Z\. Yao, A\. Gholami, M\. Mahoney, and K\. KeutzerHAWQ: hessian aware quantization of neural networks with mixed\-precision\.InIEEE/CVF International Conference on Computer Vision \(ICCV\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px1.p1.1)\.
- Frantaret al\.\(2023\)E\. Frantar, S\. Ashkboos, T\. Hoefler, and D\. AlistarhGPTQ: accurate post\-training quantization for generative pre\-trained transformers\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px1.p1.1)\.
- Goldberg \(1991\)D\. GoldbergWhat every computer scientist should know about floating\-point arithmetic\.ACM Computing Surveys23\(1\),pp\. 5–48\.Cited by:[§2](https://arxiv.org/html/2611.30835#S2.SS0.SSS0.Px1.p1.1)\.
- Guet al\.\(2024\)Y\. Gu, L\. Dong, F\. Wei, and M\. HuangMiniLLM: on\-policy distillation of large language models\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1)\.
- Hannaet al\.\(2023\)M\. Hanna, O\. Liu, and A\. VariengienHow does GPT\-2 compute greater\-than?: interpreting mathematical abilities in a pre\-trained language model\.InAdvances in Neural Information Processing Systems,Cited by:[§E\.4](https://arxiv.org/html/2611.30835#A5.SS4.SSS0.Px8.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px5.p1.1)\.
- Heimersheim and Janiak \(2023\)S\. Heimersheim and J\. JaniakA circuit for Python docstrings in a 4\-layer attention\-only transformer\.Note:AI Alignment Forum,[https://www\.alignmentforum\.org/posts/u6KXXmKFbXfWzoAXn](https://www.alignmentforum.org/posts/u6KXXmKFbXfWzoAXn)Cited by:[§E\.4](https://arxiv.org/html/2611.30835#A5.SS4.SSS0.Px8.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px5.p1.1)\.
- Higginset al\.\(2017\)I\. Higgins, L\. Matthey, A\. Pal, C\. Burgess, X\. Glorot, M\. Botvinick, S\. Mohamed, and A\. LerchnerBeta\-VAE: learning basic visual concepts with a constrained variational framework\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1)\.
- Higham \(2002\)N\. J\. HighamAccuracy and stability of numerical algorithms\.2nd edition,SIAM\.Cited by:[Appendix A](https://arxiv.org/html/2611.30835#A1.SS0.SSS0.Px4.p1.1),[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[§2](https://arxiv.org/html/2611.30835#S2.SS0.SSS0.Px1.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Hintonet al\.\(2015\)G\. Hinton, O\. Vinyals, and J\. DeanDistilling the knowledge in a neural network\.arXiv preprint arXiv:1503\.02531\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Huet al\.\(2022\)E\. J\. Hu, Y\. Shen, P\. Wallis, Z\. Allen\-Zhu, Y\. Li, S\. Wang, L\. Wang, and W\. ChenLoRA: low\-rank adaptation of large language models\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[§F\.3](https://arxiv.org/html/2611.30835#A6.SS3.SSS0.Px2.p1.1)\.
- Humbatovaet al\.\(2020\)N\. Humbatova, G\. Jahangirova, G\. Bavota, V\. Riccio, A\. Stocco, and P\. TonellaTaxonomy of real faults in deep learning systems\.InInternational Conference on Software Engineering \(ICSE\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- IEEE \(2019\)IEEEIEEE standard for floating\-point arithmetic\.Note:IEEE Std 754\-2019Cited by:[§2](https://arxiv.org/html/2611.30835#S2.SS0.SSS0.Px1.p1.1)\.
- Johansson \(2017\)F\. JohanssonArb: efficient arbitrary\-precision midpoint\-radius interval arithmetic\.IEEE Transactions on Computers66\(8\),pp\. 1281–1292\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px2.p1.1),[§3](https://arxiv.org/html/2611.30835#S3.SS0.SSS0.Px1.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Kahan \(1965\)W\. KahanPracniques: further remarks on reducing truncation errors\.Communications of the ACM8\(1\),pp\. 40\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1)\.
- Kalamkaret al\.\(2019\)D\. Kalamkar, D\. Mudigere, N\. Mellempudi, D\. Das, K\. Banerjee, S\. Avancha, D\. T\. Vooturi, N\. Jammalamadaka, J\. Huang, H\. Yuen,et al\.A study of BFLOAT16 for deep learning training\.arXiv preprint arXiv:1905\.12322\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px4.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p3.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Kimet al\.\(2021\)T\. Kim, J\. Oh, N\. Y\. Kim, S\. Cho, and S\. YunComparing Kullback\-Leibler divergence and mean squared error loss in knowledge distillation\.InInternational Joint Conference on Artificial Intelligence \(IJCAI\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1)\.
- Kingma and Ba \(2015\)D\. P\. Kingma and J\. BaAdam: a method for stochastic optimization\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[§5\.3](https://arxiv.org/html/2611.30835#S5.SS3.SSS0.Px2.p1.1)\.
- Kingma and Welling \(2014\)D\. P\. Kingma and M\. WellingAuto\-encoding variational Bayes\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Kloberdanzet al\.\(2022\)E\. Kloberdanz, K\. G\. Kloberdanz, and W\. LeDeepStability: a study of unstable numerical methods and their solutions in deep learning\.InInternational Conference on Software Engineering \(ICSE\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Knuth \(1997\)D\. E\. KnuthThe art of computer programming, volume 2: seminumerical algorithms\.3rd edition,Addison\-Wesley\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px8.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px2.p1.1)\.
- Kullback and Leibler \(1951\)S\. Kullback and R\. A\. LeiblerOn information and sufficiency\.The Annals of Mathematical Statistics22\(1\),pp\. 79–86\.Cited by:[§1](https://arxiv.org/html/2611.30835#S1.p1.1)\.
- Linet al\.\(2024\)J\. Lin, J\. Tang, H\. Tang, S\. Yang, W\. Chen, W\. Wang, G\. Xiao, X\. Dang, C\. Gan, and S\. HanAWQ: activation\-aware weight quantization for LLM compression and acceleration\.InProceedings of Machine Learning and Systems \(MLSys\),Cited by:[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px1.p1.1)\.
- Lindley \(1956\)D\. V\. LindleyOn a measure of the information provided by an experiment\.The Annals of Mathematical Statistics27\(4\),pp\. 986–1005\.Cited by:[§E\.2](https://arxiv.org/html/2611.30835#A5.SS2.SSS0.Px1.p1.1),[§5\.2](https://arxiv.org/html/2611.30835#S5.SS2.SSS0.Px1.p1.1)\.
- Liuet al\.\(2024\)Z\. Liu, B\. Oguz, C\. Zhao, E\. Chang, P\. Stock, Y\. Mehdad, Y\. Shi, R\. Krishnamoorthi, and V\. ChandraLLM\-QAT: data\-free quantization aware training for large language models\.InFindings of the Association for Computational Linguistics \(ACL\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1)\.
- Loaiza\-Ganem and Cunningham \(2019\)G\. Loaiza\-Ganem and J\. P\. CunninghamThe continuous Bernoulli: fixing a pervasive error in variational autoencoders\.InAdvances in Neural Information Processing Systems \(NeurIPS\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1)\.
- Merityet al\.\(2017\)S\. Merity, C\. Xiong, J\. Bradbury, and R\. SocherPointer sentinel mixture models\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px1.p1.1)\.
- Micikeviciuset al\.\(2018\)P\. Micikevicius, S\. Narang, J\. Alben, G\. Diamos, E\. Elsen, D\. Garcia, B\. Ginsburg, M\. Houston, O\. Kuchaiev, G\. Venkatesh, and H\. WuMixed precision training\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px4.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p3.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Milleret al\.\(2024\)J\. Miller, B\. Chughtai, and W\. SaundersTransformer circuit faithfulness metrics are not robust\.InConference on Language Modeling \(COLM\),Cited by:[§E\.4](https://arxiv.org/html/2611.30835#A5.SS4.SSS0.Px8.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px5.p1.1)\.
- Mishneet al\.\(2023\)G\. Mishne, Z\. Wan, Y\. Wang, and S\. YangThe numerical stability of hyperbolic representation learning\.InInternational Conference on Machine Learning \(ICML\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px1.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Mulleret al\.\(2018\)J\. Muller, N\. Brunie, F\. de Dinechin, C\. Jeannerod, M\. Joldes, V\. Lefèvre, G\. Melquiond, N\. Revol, and S\. TorresHandbook of floating\-point arithmetic\.2nd edition,Birkhäuser\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1)\.
- Nagelet al\.\(2021\)M\. Nagel, M\. Fournarakis, R\. A\. Amjad, Y\. Bondarenko, M\. van Baalen, and T\. BlankevoortA white paper on neural network quantization\.arXiv preprint arXiv:2106\.08295\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Ogitaet al\.\(2005\)T\. Ogita, S\. M\. Rump, and S\. OishiAccurate sum and dot product\.SIAM Journal on Scientific Computing26\(6\),pp\. 1955–1988\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px8.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px2.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Panchekhaet al\.\(2015\)P\. Panchekha, A\. Sanchez\-Stern, J\. R\. Wilcox, and Z\. TatlockAutomatically improving accuracy for floating point expressions\.InACM SIGPLAN Conference on Programming Language Design and Implementation \(PLDI\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Paszkeet al\.\(2019\)A\. Paszke, S\. Gross, F\. Massa, A\. Lerer, J\. Bradbury, G\. Chanan, T\. Killeen, Z\. Lin, N\. Gimelshein, L\. Antiga,et al\.PyTorch: an imperative style, high\-performance deep learning library\.InAdvances in Neural Information Processing Systems \(NeurIPS\),Cited by:[§1](https://arxiv.org/html/2611.30835#S1.p1.1)\.
- Prietoet al\.\(2025\)L\. Prieto, M\. Barsbey, P\. A\. M\. Mediano, and T\. BirdalGrokking at the edge of numerical stability\.InInternational Conference on Learning Representations \(ICLR\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px1.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p4.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Qiet al\.\(2025\)P\. Qi, Z\. Liu, X\. Zhou, T\. Pang, C\. Du, W\. S\. Lee, and M\. LinDefeating the training\-inference mismatch via FP16\.arXiv preprint arXiv:2510\.26788\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px4.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px4.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Schulmanet al\.\(2015\)J\. Schulman, S\. Levine, P\. Abbeel, M\. Jordan, and P\. MoritzTrust region policy optimization\.InInternational Conference on Machine Learning \(ICML\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Schulmanet al\.\(2017\)J\. Schulman, F\. Wolski, P\. Dhariwal, A\. Radford, and O\. KlimovProximal policy optimization algorithms\.arXiv preprint arXiv:1707\.06347\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Schulman \(2020\)J\. SchulmanApproximating KL divergence\.Note:Blog post,[http://joschu\.net/blog/kl\-approx\.html](http://joschu.net/blog/kl-approx.html)Cited by:[§F\.3](https://arxiv.org/html/2611.30835#A6.SS3.SSS0.Px3.p1.1),[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1)\.
- Shaoet al\.\(2024\)Z\. Shao, P\. Wang, Q\. Zhu, R\. Xu, J\. Song, X\. Bi, H\. Zhang, M\. Zhang, Y\. K\. Li, Y\. Wu, and D\. GuoDeepSeekMath: pushing the limits of mathematical reasoning in open language models\.arXiv preprint arXiv:2402\.03300\.Cited by:[§F\.3](https://arxiv.org/html/2611.30835#A6.SS3.SSS0.Px3.p1.1),[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Shewchuk \(1997\)J\. R\. ShewchukAdaptive precision floating\-point arithmetic and fast robust geometric predicates\.Discrete & Computational Geometry18\(3\),pp\. 305–363\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px8.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px2.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Sibidanovet al\.\(2022\)A\. Sibidanov, P\. Zimmermann, and S\. GlonduThe CORE\-MATH project\.InIEEE Symposium on Computer Arithmetic \(ARITH\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1)\.
- Sterbenz \(1974\)P\. H\. SterbenzFloating\-point computation\.Prentice\-Hall\.Cited by:[§B\.1](https://arxiv.org/html/2611.30835#A2.SS1.SSS0.Px1.p1.1),[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[§2](https://arxiv.org/html/2611.30835#S2.SS0.SSS0.Px1.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px2.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- The mpmath development team \(2023\)The mpmath development teamMpmath: a Python library for arbitrary\-precision floating\-point arithmetic\.Note:[https://mpmath\.org/](https://mpmath.org/)Cited by:[Appendix H](https://arxiv.org/html/2611.30835#A8.SS0.SSS0.Px2.p1.1)\.
- von Werraet al\.\(2020\)L\. von Werra, Y\. Belkada, L\. Tunstall,et al\.TRL: transformer reinforcement learning\.Note:[https://github\.com/huggingface/trl](https://github.com/huggingface/trl)Cited by:[§F\.3](https://arxiv.org/html/2611.30835#A6.SS3.SSS0.Px1.p1.1)\.
- Wanget al\.\(2022\)G\. Wang, Z\. Wang, J\. Chen, X\. Chen, and M\. YanAn empirical study on numerical bugs in deep learning programs\.InIEEE/ACM International Conference on Automated Software Engineering \(ASE\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Wanget al\.\(2023\)K\. Wang, A\. Variengien, A\. Conmy, B\. Shlegeris, and J\. SteinhardtInterpretability in the wild: a circuit for indirect object identification in GPT\-2 small\.InInternational Conference on Learning Representations,Cited by:[§E\.4](https://arxiv.org/html/2611.30835#A5.SS4.SSS0.Px8.p1.1),[§E\.4](https://arxiv.org/html/2611.30835#A5.SS4.SSS0.Px9.p1.1),[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px5.p1.1)\.
- Wortsmanet al\.\(2023\)M\. Wortsman, T\. Dettmers, L\. Zettlemoyer, A\. Morcos, A\. Farhadi, and L\. SchmidtStable and low\-precision training for large\-scale vision\-language models\.InAdvances in Neural Information Processing Systems \(NeurIPS\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px1.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Xinet al\.\(2026\)M\. Xin, S\. Priyadarshi, J\. Xin, B\. Kartal, A\. Vavre,et al\.Quantization\-aware distillation for NVFP4 inference accuracy recovery\.arXiv preprint arXiv:2601\.20088\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px5.p1.1),[§1](https://arxiv.org/html/2611.30835#S1.p2.1)\.
- Yanet al\.\(2021\)M\. Yan, J\. Chen, X\. Zhang, L\. Tan, G\. Wang, and Z\. WangExposing numerical bugs in deep learning via gradient back\-propagation\.InACM Joint European Software Engineering Conference and Symposium on the Foundations of Software Engineering \(ESEC/FSE\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Yanget al\.\(2024\)A\. Yang, B\. Yang, B\. Zhang, B\. Hui, B\. Zheng, B\. Yu, C\. Li, D\. Liu, F\. Huang,et al\.Qwen2\.5 technical report\.arXiv preprint arXiv:2412\.15115\.Cited by:[§5\.4](https://arxiv.org/html/2611.30835#S5.SS4.SSS0.Px1.p1.1)\.
- Yuanet al\.\(2025\)J\. Yuan, H\. Li, X\. Ding, W\. Xie, Y\. Li, W\. Zhao, K\. Wan, J\. Shi, X\. Hu, and Z\. LiuGive me FP32 or give me death? Challenges and solutions for reproducible reasoning\.arXiv preprint arXiv:2506\.09501\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px4.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Zamiraiet al\.\(2020\)P\. Zamirai, J\. Zhang, C\. R\. Aberger, and C\. De SaRevisiting BFloat16 training\.arXiv preprint arXiv:2010\.06192\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px4.p1.1)\.
- Zhanget al\.\(2018\)Y\. Zhang, Y\. Chen, S\. Cheung, Y\. Xiong, and L\. ZhangAn empirical study on TensorFlow program bugs\.InACM SIGSOFT International Symposium on Software Testing and Analysis \(ISSTA\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Zhanget al\.\(2020\)Y\. Zhang, L\. Ren, L\. Chen, Y\. Xiong, S\. Cheung, and T\. XieDetecting numerical bugs in neural network architectures\.InACM Joint European Software Engineering Conference and Symposium on the Foundations of Software Engineering \(ESEC/FSE\),Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px3.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.
- Ziv \(1991\)A\. ZivFast evaluation of elementary mathematical functions with correctly rounded last bit\.ACM Transactions on Mathematical Software17\(3\),pp\. 410–423\.Cited by:[Appendix G](https://arxiv.org/html/2611.30835#A7.SS0.SSS0.Px2.p1.1),[§4](https://arxiv.org/html/2611.30835#S4.SS0.SSS0.Px4.p1.1),[§6](https://arxiv.org/html/2611.30835#S6.p1.1)\.

## Appendix AMeasurement and Validation Protocol

#### Sampling \(interface A\)\.

For each configuration and precision, samples are organised in*cells*indexed by a parameter region and a KL group\. Regions: R0 \(ordinary parameters\), and stress regions R1, R2 \(and R3 for some families\) with very large or very small parameters or probabilities close to 0 or 1; for example, for Dirichlet the concentration components are log\-uniform in\[10−2,102\]\[10^\{\-2\},10^\{2\}\]\(R0\),\[102,104\]\[10^\{2\},10^\{4\}\]\(R1\) and\[10−4,10−2\]\[10^\{\-4\},10^\{\-2\}\]\(R2\)\. KL groups: a control bin\[10−1,1\)\[10^\{\-1\},1\), fourteen bins\[10−k−1,10−k\)\[10^\{\-k\-1\},10^\{\-k\}\),k=1,…,14k=1,\\dots,14, a ULP group \(QQobtained by moving parameters ofPPby 1–8 ULP\) and an identity group \(Q=PQ=P\)\. The quota is 10,000 samples per R0 cell and per ULP cell and 1,000 per stress cell\. A cell is recorded as*empty*after 2,000 consecutive failed attempts and as*below quota*when the quota is not reached within 50 times the quota in attempts; the dominant reason is thatQQrounds toPPin the target format\. For the 16 scalar\-family configurations \(5,057,067 samples\), 1,194 of 1,568 cells are full, 49 below quota and 325 empty \(285 of them in FP16\); the other 7 configurations \(Categorical, ContinuousBernoulli, Dirichlet and the two multivariate normal families, with their sub\-configurations; 2,485,402 samples\) follow identical rules\. Of the 7,542,469 samples, 7,234,454 are scored: the 308,015 FP16 samples of the two multivariate normal families are not scored for any method, because the official implementation does not run on them \([Table1](https://arxiv.org/html/2611.30835#S4.T1)\)\. A call of the official implementation that raises an error on a valid input counts as a significant error\.

#### Truth and scoring\.

For interface A, the reference is the exact KL of the distributions defined by the stored raw constructor arguments, with gradients taken with respect to those arguments\. Arb certifies the rounding of each reference value to the target format\. Six uncertifiable candidates were rejected\. Platform: PyTorch 2\.15\.0\+cpu, Python 3\.11\.8, python\-flint 0\.9\.0, mpmath 1\.4\.1, batch size 4,096, single thread\. Each output is classified as correctly rounded, off by a stated number of ULP, or qualitatively wrong \(negative KL, NaN, Inf, spurious zero, spurious non\-zero at a structural zero, sign flip, vanished gradient\)\. A*spurious zero*is a zero output where the correctly rounded reference is non\-zero; a positive KL whose correctly rounded value in the target format is zero is scored as correct when the output is zero\.*Significant error*means≥8\\geq 8ULP or any qualitative error; gradient vectors are additionally scored by relativeL2L\_\{2\}error and cosine\. Components that are not differentiable at the sample \(e\.g\. Uniform at shared end points, the strict upper triangle ofscale\_tril\) are not scored\.

#### Methods\.

A: official\. B32/B64: official code on exactly promoted parameters, rounded once \(Official\+, abbreviatedOff\.\+in tables, is B32 for FP16 inputs and B64 for FP32 inputs\)\. C: rewritten algorithm in the target precision \(Rewrite\)\. D32/D64: rewritten algorithm in FP32/FP64, rounded once; D64 is the strict repair \(Ours\)\.

#### Validation\.

All methods are evaluated on the same frozen inputs for correct rounding, significant errors, structural zeros, and regressions against the official baselines\. The diagnostic error estimateEEuses first\-order rounding\-error propagation with an empirical safety factor\([Higham, 2002](https://arxiv.org/html/2611.30835#bib.bib3)\); series and quadrature truncation bounds are rigorous\. A series may be used on a segment only if it comes from an exact identity, converges on the whole segment, and has a rigorous truncation bound of at mostu/16u/16relative to the final result \(or, for gradients that can cancel, relative to the approximated term\)\.

#### Interface B\.

20 configurations:log\_target∈\{\\,\\in\\\{False, True\}×\{\\\}\\times\\\{FP16, FP32\}×K∈\{2,4,16,256\}\\\}\\times K\\in\\\{2,4,16,256\\\}plus fourK=1024K=1024stress configurations; batch sizes 1 and 8; reductionsnone,sum,mean,batchmean; gradients with respect to both arguments\. Inputs are produced from logits in regions R0 \(components uniform in\[−5,5\]\[\-5,5\]\), R1 \(range in\[10,30\]\[10,30\]\) and R2 \(range in\[30,103\]\[30,10^\{3\}\]\) and from boundary strata \(zeros,−∞\-\\infty, extreme magnitudes\)\. 942,200 batches;5\.69×1085\.69\\times 10^\{8\}certified truths, of which5\.67×1085\.67\\times 10^\{8\}are scored \(inputs outside the domain of the operator and target gradients that do not exist are not\); 118 of the 1,920 cells were completed by additional sampling, kept as separate strata\. Three truths are stored per batch:TopT\_\{\\mathrm\{op\}\},Te2eT\_\{\\mathrm\{e2e\}\}and their difference\.

### A\.1Error by KL interval

[Table6](https://arxiv.org/html/2611.30835#A1.T6)gives the numbers behind[Figure2](https://arxiv.org/html/2611.30835#S3.F2): the 16 scalar\-family configurations pooled, region R0, per decade of the true KL\.[Figures3](https://arxiv.org/html/2611.30835#A1.F3)and[4](https://arxiv.org/html/2611.30835#A1.F4)resolve the first metric by configuration for all 33 configurations and sub\-configurations \(the 23 configurations of[Table1](https://arxiv.org/html/2611.30835#S4.T1)with their dimension variants listed separately\), for the official implementation on CPU and on GPU and forOurs\. In FP32 every configuration is above 90% byKL≈10−8\\mathrm\{KL\}\\approx 10^\{\-8\}, with Normal as the only partial exception \(50–99%\); the families that fail earliest are Dirichlet, Binomial and Beta\. The rows “in FP64” and “in FP32” of[Table6](https://arxiv.org/html/2611.30835#A1.T6)show that evaluating the official formula in the next precision moves the same pattern nine decades down \(FP32 inputs\) or leaves a visible residue already at10−410^\{\-4\}\(FP16 inputs\); it does not remove it\.

Table 6:Interface A, region R0, 16 scalar\-family configurations pooled: share of samples \(%\) per KL interval\. Column heads are the lower edge of the interval \(e\.g\.10−910^\{\-9\}means\[10−9,10−8\)\[10^\{\-9\},10^\{\-8\}\)\)\. “–”: fewer than 300 samples, or \(FP16 relative error\) true value subnormal\. Gradient metrics refer to the full gradient vector\.![Refer to caption](https://arxiv.org/html/2611.30835v1/fig_heat_A_fp32.png)Figure 3:Interface A, FP32 inputs, region R0\. Share of samples whose KL has relative error≥1%\\geq 1\\%, per configuration and KL interval\. The left panel shows the official implementation on CPU \(PyTorch 2\.15\.0\), the middle panel the official implementation on an A100 GPU \(PyTorch 2\.4\.0\), and the right panelOurs, whose CPU and GPU results are identical \(all zero\)\. Blank cells have fewer than 100 samples \(not constructible on the FP32 grid\)\.![Refer to caption](https://arxiv.org/html/2611.30835v1/fig_heat_A_fp16.png)Figure 4:Interface A, FP16 inputs, region R0, as[Figure3](https://arxiv.org/html/2611.30835#A1.F3)\. The relative error is defined only while the true KL is a normal FP16 number \(above about6×10−56\\times 10^\{\-5\}\)\. MultivariateNormal and LowRankMultivariateNormal have no FP16 results because the official implementation does not run\.

## Appendix BInterface A: Analysis and Repair of Each Closed Form

For every distribution we give the official formula \(PyTorch 2\.15\.0\), the origin of its error, the repair, the reason the repair works, and the result on the frozen data\. Rates are significant\-error rates in the order FP16 / FP32 unless stated otherwise; “Off\.\+” is the official code in the next precision, “Rewrite” the repaired formula in the target precision, and “Ours” the repaired formula in FP64 rounded once\. In all 18 distributionsOurshas zero significant errors in the KL and in every gradient component and zero new errors; we do not repeat this in each subsection\.

### B\.1Common building blocks

#### The functionφ\\varphi\.

φ⁡\(x\)=x−log1p⁡\(x\)≥0\\varphi\(x\)=x\-\\operatorname\{log1p\}\(x\)\\geq 0\. On−34≤x≤3\-\\tfrac\{3\}\{4\}\\leq x\\leq 3we evaluate[Equation3](https://arxiv.org/html/2611.30835#S4.E3); withz=s2≤0\.36z=s^\{2\}\\leq 0\.36andQK​\(z\)=∑k=1Kzk−1/\(2​k\+1\)Q\_\{K\}\(z\)=\\sum\_\{k=1\}^\{K\}z^\{k\-1\}/\(2k\+1\)\(writtenQQbelow\) evaluated by Horner’s rule, the remainderRKR\_\{K\}of the series afterKKterms satisfies\|RK\|/φ≤4​\(0\.6\)2​K\+1/\(2​K\+3\)\|R\_\{K\}\|/\\varphi\\leq 4\\,\(0\.6\)^\{2K\+1\}/\(2K\+3\), i\.e\.1\.2×10−51\.2\\times 10^\{\-5\},1\.9×10−91\.9\\times 10^\{\-9\},3\.4×10−183\.4\\times 10^\{\-18\}forK=9,17,36K=9,17,36\(FP16, FP32, FP64\), each belowu/16u/16\. The two terms have ratio at most about 0\.12 forx\>0x\>0and add forx<0x<0, so there is no cancellation\. Outside the segment,φ=x−log1p⁡\(x\)\\varphi=x\-\\operatorname\{log1p\}\(x\)\(x\>3x\>3\) andφ=x−log⁡\(b/a\)\\varphi=x\-\\log\(b/a\)\(x<−34x<\-\\tfrac\{3\}\{4\}, with1\+x=b/a1\+x=b/a\) are well conditioned \(the subtracted term is at most 0\.86 of the result, resp\. condition number≤3\.4\\leq 3\.4\)\. Whenx=d/ax=d/ais formed fromd=b−ad=b\-a,ddis exact forb∈\[a/2,2​a\]b\\in\[a/2,2a\]\([Sterbenz, 1974](https://arxiv.org/html/2611.30835#bib.bib5)\), and the series is evaluated withs=d/\(a\+b\)s=d/\(a\+b\)computed directly from the inputs\. We also use the exponential counterpartξ⁡\(t\)=et−1−t≥0\\xi\(t\)=e^\{t\}\-1\-t\\geq 0\.

#### Error\-free transformations\.

TwoSum returnsfl⁡\(a\+b\)\\mathrm\{fl\}\(a\+b\)together with the exact errora\+b−fl⁡\(a\+b\)a\+b\-\\mathrm\{fl\}\(a\+b\); Dekker’s TwoProduct returnsfl⁡\(a​b\)\\mathrm\{fl\}\(ab\)together witha​b−fl⁡\(a​b\)ab\-\\mathrm\{fl\}\(ab\)\(exact unless the product of the last significant bits underflows, which is checked per sample\); Shewchuk’s Grow\-Expansion sums such pairs exactly\.*Double\-length arithmetic*represents a number as an unevaluated pair \(hi, lo\) of working\-precision numbers and implements\+,×,÷,\+,\\times,\\div,\\sqrt\{\\ \}on pairs with these primitives\.

#### Mantissa/exponent products\.

Products and quotients whose intermediate results could overflow or become subnormal although the final result is representable are evaluated onfrexpmantissas, with the exponents added separately and applied by one exact scaling by2k2^\{k\}followed by a single rounding\.

#### Special functions\.

For Gumbel, Gamma, Beta and Dirichlet we do not calltorch\.lgamma,digammaorpolygamma\. We use the Binet functionw⁡\(a\)=log⁡Γ⁡\(a\)−\(a−12\)​log​a\+a−12​log​2​πw\(a\)=\\log\\Gamma\(a\)\-\(a\-\\tfrac\{1\}\{2\}\)\\log a\+a\-\\tfrac\{1\}\{2\}\\log 2\\pi, which is convex, its derivatives through Stirling\-type series with rigorous tail bounds, exact\-coefficient zeta series near small arguments, exact shifts by integers, and Gauss–Legendre quadrature \(5/7/13 nodes for FP16/FP32/FP64\) of same\-sign integrands for differences at nearby arguments\. The quadrature error bound \(maximum modulus on a Bernstein ellipse\) is rigorous; the node counts were verified on a parameter grid and independently measured as8\.3×10−108\.3\\times 10^\{\-10\},3\.9×10−143\.9\\times 10^\{\-14\},6\.3×10−276\.3\\times 10^\{\-27\}maximum relative error\.

#### Notation\.

∂θ\\partial\_\{\\theta\}denotes∂KL/∂θ\\partial\\mathrm\{KL\}/\\partial\\theta;ψ=\(log⁡Γ\)′\\psi=\(\\log\\Gamma\)^\{\\prime\}is the digamma function;Bf​\(x,y\)=f⁡\(x\)−f⁡\(y\)−f′​\(y\)​\(x−y\)≥0B\_\{f\}\(x,y\)=f\(x\)\-f\(y\)\-f^\{\\prime\}\(y\)\(x\-y\)\\geq 0is the Bregman divergence of a convex functionff;σ⁡\(⋅\)\\sigma\(\\cdot\)is the logistic sigmoid andlogit\\mathrm\{logit\}its inverse\.clog​uc\_\{\\log\}udenotes a bound on the relative error of the library’slogandlog1p;clogc\_\{\\log\}is a small constant measured on the platform\.emaxe\_\{\\max\}andxmaxx\_\{\\max\}are the largest exponent and the largest finite number of the format, andepsis its machine epsilon \(2−102^\{\-10\},2−232^\{\-23\},2−522^\{\-52\}\), the constant with which PyTorch’s constructors clamp probabilities to\[eps,1−eps\]\[\\texttt\{eps\},1\-\\texttt\{eps\}\]\.

#### Range and boundary semantics\.

We use power\-of\-two scaling and mantissa/exponent arithmetic for products and quotients near the format limits\. The repairs evaluate distributions from raw parameters, bypassing constructor clamps andsoftmax\.

#### Midpoint\-aware final rounding\.

Computing in FP64 and rounding once to FP16 or FP32 fails when the leading term of the result is exactly a rounding midpoint of the target format and the correction is below FP64 resolution relative to it\. This occurs for logit\-space gradients near zero \(leading term\(w−z\)/4\(w\-z\)/4for Bernoulli and\(w−z\)/2\(w\-z\)/2for Geometric, wherezzandwware the logits ofPPandQQ\)\. There we write the result as an exactly representable leading term plus a small series, recover the rounded\-away remainder with Fast2Sum, and move one ULP towards the truth when the FP64 result sits on a midpoint\.

### B\.2Cauchy

Official\.With locationsμp,μq\\mu\_\{p\},\\mu\_\{q\}, scalesγp,γq\\gamma\_\{p\},\\gamma\_\{q\}andN=\(γp\+γq\)2\+\(μp−μq\)2N=\(\\gamma\_\{p\}\+\\gamma\_\{q\}\)^\{2\}\+\(\\mu\_\{p\}\-\\mu\_\{q\}\)^\{2\}:KL=log⁡N−log⁡\(4​γp​γq\)\\mathrm\{KL\}=\\log N\-\\log\(4\\gamma\_\{p\}\\gamma\_\{q\}\)\. We writeΣ​γ=γp\+γq\\Sigma\\gamma=\\gamma\_\{p\}\+\\gamma\_\{q\},Δ​γ=γp−γq\\Delta\\gamma=\\gamma\_\{p\}\-\\gamma\_\{q\}andΔ​μ=μp−μq\\Delta\\mu=\\mu\_\{p\}\-\\mu\_\{q\}\.

Error\.ForP≈QP\\approx Qboth logarithms are≈log⁡\(4​γ2\)\\approx\\log\(4\\gamma^\{2\}\)withγ≈γp≈γq\\gamma\\approx\\gamma\_\{p\}\\approx\\gamma\_\{q\}; each carries an absolute error of order\(c\+\|log⁡N\|\)​u\(c\+\|\\log N\|\)uthat survives the subtraction, so the relative error is≈\(c1\+2​\|log⁡4​γ2\|\)​u/KL\\approx\(c\_\{1\}\+2\|\\log 4\\gamma^\{2\}\|\)u/\\mathrm\{KL\}, with small constantsc,c1c,c\_\{1\}, and the sign is random once KL is below that error\. Autograd gives∂KL/∂γp=2​Σ​γ/N−1/γp\\partial\\mathrm\{KL\}/\\partial\\gamma\_\{p\}=2\\Sigma\\gamma/N\-1/\\gamma\_\{p\}, a difference of twoO⁡\(1/γ\)O\(1/\\gamma\)terms\. In FP16, for smallγ\\gammaboth arguments of the logarithms become subnormal and round to the same number, the derivatives1/N1/Nand1/\(4​γp​γq\)1/\(4\\gamma\_\{p\}\\gamma\_\{q\}\)overflow, and the gradient is∞−∞=\\infty\-\\infty=NaN, also atP=QP=Q\. Theμ\\mugradient has no cancellation\.

Repair\.From\(γp\+γq\)2−4​γp​γq=\(γp−γq\)2\(\\gamma\_\{p\}\+\\gamma\_\{q\}\)^\{2\}\-4\\gamma\_\{p\}\\gamma\_\{q\}=\(\\gamma\_\{p\}\-\\gamma\_\{q\}\)^\{2\},

KL=log1p⁡\(r\),r=\(γp−γq\)2\+\(μp−μq\)24​γp​γq≥0\.\\mathrm\{KL\}=\\operatorname\{log1p\}\(r\),\\qquad r=\\frac\{\(\\gamma\_\{p\}\-\\gamma\_\{q\}\)^\{2\}\+\(\\mu\_\{p\}\-\\mu\_\{q\}\)^\{2\}\}\{4\\gamma\_\{p\}\\gamma\_\{q\}\}\\geq 0\.rris homogeneous of degree zero, so all four quantities are scaled by a power of two2k2^\{k\}withmax⁡\(γp,γq,\|Δ​μ\|\)​2k∈\[8,16\)\\max\(\\gamma\_\{p\},\\gamma\_\{q\},\|\\Delta\\mu\|\)\\,2^\{k\}\\in\[8,16\)\(applied as two representable factors\); ifrroverflows, a logarithmic fallback is used, which cannot cancel because thenKL\>log⁡xmax\\mathrm\{KL\}\>\\log x\_\{\\max\}\. Gradients are analytic:∂μp=2Δμ/N=−∂μq\\partial\_\{\\mu\_\{p\}\}=2\\Delta\\mu/N=\-\\partial\_\{\\mu\_\{q\}\},∂γp=\(ΔγΣγ−Δμ2\)/\(γpN\)\\partial\_\{\\gamma\_\{p\}\}=\(\\Delta\\gamma\\,\\Sigma\\gamma\-\\Delta\\mu^\{2\}\)/\(\\gamma\_\{p\}N\),∂γq=\(−ΔγΣγ−Δμ2\)/\(γqN\)\\partial\_\{\\gamma\_\{q\}\}=\(\-\\Delta\\gamma\\,\\Sigma\\gamma\-\\Delta\\mu^\{2\}\)/\(\\gamma\_\{q\}N\), evaluated on the scaled quantities\.

Why it works\.The squared differences enterrras small numbers instead of being recovered from large ones, andlog1p\\operatorname\{log1p\}is well conditioned forr≥0r\\geq 0: the first\-order relative error is about\(6\+clog\)​u\(6\+c\_\{\\log\}\)uindependent of KL\. The condition number of theγ\\gammagradient drops fromκA=\(2​γp​Σ​γ\+N\)/\|Δ​γ​Σ​γ−Δ​μ2\|\\kappa\_\{A\}=\(2\\gamma\_\{p\}\\Sigma\\gamma\+N\)/\|\\Delta\\gamma\\Sigma\\gamma\-\\Delta\\mu^\{2\}\|toκC=\(\|Δ​γ\|​Σ​γ\+Δ​μ2\)/\|Δ​γ​Σ​γ−Δ​μ2\|≤κA\\kappa\_\{C\}=\(\|\\Delta\\gamma\|\\Sigma\\gamma\+\\Delta\\mu^\{2\}\)/\|\\Delta\\gamma\\Sigma\\gamma\-\\Delta\\mu^\{2\}\|\\leq\\kappa\_\{A\}; the remaining cancellation atγp2≈γq2\+Δ​μ2\\gamma\_\{p\}^\{2\}\\approx\\gamma\_\{q\}^\{2\}\+\\Delta\\mu^\{2\}is intrinsic and is the reasonRewritekeeps a few errors andOursdoes not\. After scaling all denominators are positive and bounded away from zero, so no NaN can occur, andP=QP=Qgivesr=0r=0,KL=\+0\\mathrm\{KL\}=\+0and zero gradients\.

Frozen data\(345,028 samples\)\. KL:Official64\.05 / 88\.88%;Off\.\+10\.60 / 34\.84%;Rewrite0\.003 / 0%\.PP\-scale gradient:Official53\.27 / 82\.93%;Off\.\+0\.34 / 6\.37%;Rewrite0\.18 / 0\.09%\.Rewriteadds new errors on 41 samples \(0\.012%\), all within the error estimate\.

### B\.3Uniform

Official\.With supports\[pl,ph\]\[p\_\{l\},p\_\{h\}\]and\[ql,qh\]\[q\_\{l\},q\_\{h\}\]:KL=log⁡qh−qlph−pl\\mathrm\{KL\}=\\log\\dfrac\{q\_\{h\}\-q\_\{l\}\}\{p\_\{h\}\-p\_\{l\}\}for nested supports,\+∞\+\\inftyotherwise\. We writewp=ph−plw\_\{p\}=p\_\{h\}\-p\_\{l\}andwq=qh−qlw\_\{q\}=q\_\{h\}\-q\_\{l\}for the widths\.

Error\.The two widths are rounded separately and their ratio is≈1\+KL\\approx 1\+\\mathrm\{KL\}, so the relative error is about2​u/KL2u/\\mathrm\{KL\}\(12% atKL=10−6\\mathrm\{KL\}=10^\{\-6\}in FP32\), and the ratio rounds to 1 forKL<u/2\\mathrm\{KL\}<u/2\(spurious zero\)\. The gradients±1/wp\\pm 1/w\_\{p\}and±1/wq\\pm 1/w\_\{q\}involve no cancellation and are correct in the official code\.

Repair\.On nested supportswq−wp=\(qh−ph\)\+\(pl−ql\)w\_\{q\}\-w\_\{p\}=\(q\_\{h\}\-p\_\{h\}\)\+\(p\_\{l\}\-q\_\{l\}\)with both terms non\-negative, so

KL=log1p⁡\(r\),r=d1\+d2wp,d1=qh−ph≥0,d2=pl−ql≥0\.\\mathrm\{KL\}=\\operatorname\{log1p\}\(r\),\\qquad r=\\frac\{d\_\{1\}\+d\_\{2\}\}\{w\_\{p\}\},\\quad d\_\{1\}=q\_\{h\}\-p\_\{h\}\\geq 0,\\quad d\_\{2\}=p\_\{l\}\-q\_\{l\}\\geq 0\.All inputs are halved if one of them is≥2emax−1\\geq 2^\{e\_\{\\max\}\-1\}; if the ratio overflows,KL=log⁡wq−log⁡wp\\mathrm\{KL\}=\\log w\_\{q\}\-\\log w\_\{p\}\. Gradients are±1/wp\\pm 1/w\_\{p\},∓1/wq\\mp 1/w\_\{q\}\.

Why it works\.d1,d2d\_\{1\},d\_\{2\}are direct differences of the inputs \(exact for nearby end points\), the ratio has relative error≤4​u\\leq 4uandlog1p\\operatorname\{log1p\}has condition number≤1\\leq 1onr≥0r\\geq 0, hence the relative error of the KL is at most\(4\+clog\)​u\(4\+c\_\{\\log\}\)u\.P=QP=Qgives\+0\+0\.

Frozen data\(181,798\)\. KL:Official51\.62 / 75\.08%;Off\.\+0 / 0\.012%;Rewrite0 / 0% \(maximum error 2 ULP\)\. Gradients have no significant error in any method\.

### B\.4Exponential

Official\.Withr=λq/λpr=\\lambda\_\{q\}/\\lambda\_\{p\}:KL=−log⁡r\+r−1\\mathrm\{KL\}=\-\\log r\+r\-1\.

Error\.r−log⁡r≈1\+KLr\-\\log r\\approx 1\+\\mathrm\{KL\}is rounded near 1 with absolute error≈u\\approx uwhileKL≈x2/2\\mathrm\{KL\}\\approx x^\{2\}/2,x=r−1x=r\-1: relative error≈2​u/x2\\approx 2u/x^\{2\}, spurious zero forKL<u/2\\mathrm\{KL\}<u/2\. Autograd’s1−1/r1\-1/rcancels with relative error≈u/\|x\|\\approx u/\|x\|\. FP64 evaluation leaves 33\.5% significant errors for FP32 inputs becausexxcan be as small as2−242^\{\-24\}\.

Repair\.d=λq−λpd=\\lambda\_\{q\}\-\\lambda\_\{p\}\(direct\),x=d/λpx=d/\\lambda\_\{p\},KL=φ⁡\(x\)\\mathrm\{KL\}=\\varphi\(x\)as in[SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1); analytic gradients∂λp=−x/λp\\partial\_\{\\lambda\_\{p\}\}=\-x/\\lambda\_\{p\},∂λq=x/λq\\partial\_\{\\lambda\_\{q\}\}=x/\\lambda\_\{q\}\.

Why it works\.See[SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1); KL and gradients are computed from the same rounded valuex~\\tilde\{x\}ofxx, whose rounding changes KL by a relativeO⁡\(u\)O\(u\)\.P=QP=Qgivesx=0x=0and three exact zeros; a finalmax⁡\(⋅,0\)\\max\(\\cdot,0\)guaranteesKL≥0\\mathrm\{KL\}\\geq 0\.

Frozen data\(308,000\)\. KL:Official72\.57 / 85\.64%;Off\.\+0 / 33\.49%;Rewrite0 / 0% \(max\. 6 ULP\)\. Rate gradient:Official48\.78 / 75\.11%; all other methods 0\. A near\-exhaustive FP16 sweep of3\.6×1073\.6\\times 10^\{7\}pairs gave at most 7 ULP forRewriteand 1 ULP for the FP32 evaluation\.

### B\.5Poisson

Official\.With ratesa=λpa=\\lambda\_\{p\},b=λqb=\\lambda\_\{q\}:KL=a⁡\(log⁡a−log⁡b\)−\(a−b\)\\mathrm\{KL\}=a\(\\log a\-\\log b\)\-\(a\-b\)\. Letx=\(b−a\)/ax=\(b\-a\)/a\.

Error\.Two cancellations:log⁡a−log⁡b≈−x\\log a\-\\log b\\approx\-xwith each logarithm carrying an error≈u​\|log⁡a\|\\approx u\|\\log a\|, thena⁡\(⋅\)−\(a−b\)a\(\\cdot\)\-\(a\-b\)leavesa​x2/2a\\,x^\{2\}/2\. The relative error is≈4​clog​u​\|log⁡a\|/x2\\approx 4c\_\{\\log\}u\|\\log a\|/x^\{2\}; becauseaaranges up to10410^\{4\}, even the control binKL∈\[0\.1,1\)\\mathrm\{KL\}\\in\[0\.1,1\)has about 39% outputs off by≥8\\geq 8ULP, and 25–28% of all outputs are negative\. Gradients vanish when both logarithms round to the same value\.

Repair\.KL=a​φ​\(x\)\\mathrm\{KL\}=a\\,\\varphi\(x\), written so that the smallφ\\varphiis never formed and then multiplied by a largeaa: withd=b−ad=b\-a,s=d/\(a\+b\)s=d/\(a\+b\),z=s2z=s^\{2\},

KL=d​s−2​\(a​s\)​z​Q​\(z\)\(−34≤x≤3\),\\mathrm\{KL\}=d\\,s\-2\\,\(a\\,s\)\\,z\\,Q\(z\)\\qquad\(\-\\tfrac\{3\}\{4\}\\leq x\\leq 3\),anda​max⁡\(x−L,0\)a\\max\(x\-L,0\)ormax⁡\(d−a​L,0\)\\max\(d\-aL,0\)withL=log⁡\(b/a\)L=\\log\(b/a\)outside\. Gradients:∂λp=−L\\partial\_\{\\lambda\_\{p\}\}=\-L,∂λq=d/b\\partial\_\{\\lambda\_\{q\}\}=d/b\.

Why it works\.The factoraacancels exactly between the truncation error and the KL, so the bound of[SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1)is inherited;\|d​s\|\|d\\,s\|and\|a​s\|\|a\\,s\|never exceed\|d\|\|d\|, so nothing overflows before the result does\.

Frozen data\(323,498\)\. KL:Official75\.64 / 85\.82%;Off\.\+9\.75 / 34\.44%;Rewrite0\.002 / 0%\. Gradients:Official54\.8–78\.3%; others 0\. Negative KL disappears\.

### B\.6Normal

Official\.With meansμp,μq\\mu\_\{p\},\\mu\_\{q\}, standard deviationsσp,σq\\sigma\_\{p\},\\sigma\_\{q\},δ=μp−μq\\delta=\\mu\_\{p\}\-\\mu\_\{q\},r=σp/σqr=\\sigma\_\{p\}/\\sigma\_\{q\}andw=δ/σqw=\\delta/\\sigma\_\{q\}:KL=12​\(r2\+w2−1−log⁡r2\)\\mathrm\{KL\}=\\tfrac\{1\}\{2\}\\big\(r^\{2\}\+w^\{2\}\-1\-\\log r^\{2\}\\big\)\.

Error\.\(i\)r2−1−log⁡r2r^\{2\}\-1\-\\log r^\{2\}cancels like the Exponential kernel\. \(ii\)w2w^\{2\}is added tor2≈1r^\{2\}\\approx 1first and is lost whenw2<uw^\{2\}<u\(10\.6% spurious zeros in FP16\); in FP16w2w^\{2\}can overflow before the KL does\. \(iii\) Autograd’s scale gradients pass through12−12​r2\\tfrac\{1\}\{2\}\-\\tfrac\{1\}\{2r^\{2\}\}\. \(iv\) The exact∂σq=\(σq2−σp2−δ2\)/σq3\\partial\_\{\\sigma\_\{q\}\}=\(\\sigma\_\{q\}^\{2\}\-\\sigma\_\{p\}^\{2\}\-\\delta^\{2\}\)/\\sigma\_\{q\}^\{3\}has a genuine zero onσq2=σp2\+δ2\\sigma\_\{q\}^\{2\}=\\sigma\_\{p\}^\{2\}\+\\delta^\{2\}, where the official code returns non\-zero\. FP64 evaluation leaves 13\.84% for FP32 inputs\.

Repair\.[Equation2](https://arxiv.org/html/2611.30835#S4.E2):KL=φ⁡\(e\)\+12​e2\+12​w2\\mathrm\{KL\}=\\varphi\(e\)\+\\tfrac\{1\}\{2\}e^\{2\}\+\\tfrac\{1\}\{2\}w^\{2\}, withφ⁡\(e\)\\varphi\(e\)from[SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1)\(s=\(σp−σq\)/\(σp\+σq\)s=\(\\sigma\_\{p\}\-\\sigma\_\{q\}\)/\(\\sigma\_\{p\}\+\\sigma\_\{q\}\)\) andw2/2=\(w⋅12\)​ww^\{2\}/2=\(w\\cdot\\tfrac\{1\}\{2\}\)\\,w\. Gradients:∂μp=w/σq=−∂μq\\partial\_\{\\mu\_\{p\}\}=w/\\sigma\_\{q\}=\-\\partial\_\{\\mu\_\{q\}\},∂σp=e\(e\+2\)/σp\\partial\_\{\\sigma\_\{p\}\}=e\(e\+2\)/\\sigma\_\{p\},∂σq=N/σq3\\partial\_\{\\sigma\_\{q\}\}=N/\\sigma\_\{q\}^\{3\}withN=σq2−σp2−δ2N=\\sigma\_\{q\}^\{2\}\-\\sigma\_\{p\}^\{2\}\-\\delta^\{2\}\. Where cancellation inNNis possible \(σq≥max⁡\(σp,\|δ\|\)/2\\sigma\_\{q\}\\geq\\max\(\\sigma\_\{p\},\|\\delta\|\)/2\),NNis computed*exactly*in the working precision:δ\\deltaby TwoSum, power\-of\-two scaling, five Dekker products, and a ten\-term Grow\-Expansion\.

Why it works\.Three non\-negative terms; the truncation bound is inherited becauseKL≥φ⁡\(e\)\\mathrm\{KL\}\\geq\\varphi\(e\)\. WithNNexact,∂σq\\partial\_\{\\sigma\_\{q\}\}suffers only 4–5 roundings\. Ifσq2=σp2\+δ2\\sigma\_\{q\}^\{2\}=\\sigma\_\{p\}^\{2\}\+\\delta^\{2\}holds exactly, a 2\-adic argument shows that all scaled operands lie on a common binary grid, every product is exact, and the computedN^\\hat\{N\}is\+0\+0; all 109,558 FP16 and 908,771,452 FP32 Pythagorean triples we enumerated return\+0\+0\. Computing\(σq−σp\)​\(σq\+σp\)−δ2\(\\sigma\_\{q\}\-\\sigma\_\{p\}\)\(\\sigma\_\{q\}\+\\sigma\_\{p\}\)\-\\delta^\{2\}in working precision is not enough: it rounds, e\.g\. in FP16 for\(μp,σp,μq,σq\)=\(329,1080,0,1129\)\(\\mu\_\{p\},\\sigma\_\{p\},\\mu\_\{q\},\\sigma\_\{q\}\)=\(329,1080,0,1129\)\.

Frozen data\(339,499\)\. KL:Official42\.40 / 82\.18%;Off\.\+0 / 13\.84%;Rewrite0 / 0%\. Scale gradients:Official32\.9–61\.0%;Rewritehas one new case in FP16\.

### B\.7Laplace

Official\.With locationsμp,μq\\mu\_\{p\},\\mu\_\{q\}, scalesa=σpa=\\sigma\_\{p\},b=σqb=\\sigma\_\{q\},r=a/br=a/bandd=\|μp−μq\|d=\|\\mu\_\{p\}\-\\mu\_\{q\}\|:KL=−logr\+d/b\+re−d/a−1\\mathrm\{KL\}=\-\\log r\+d/b\+r\\,e^\{\-d/a\}\-1\.

Error\.FourO⁡\(1\)O\(1\)terms sum to a small KL \(43% spurious zeros and 1\.4% negative values in FP16\)\. Autograd’s location gradient\(1−e−t\)/b\(1\-e^\{\-t\}\)/b,t=d/at=d/a, vanishes whene−te^\{\-t\}rounds to 1 \(24\.5% in FP16\); scale gradients are differences ofO⁡\(1/b\)O\(1/b\)terms and flip sign; in FP16,d/\(a⋅a\)d/\(a\\cdot a\)becomes0/00/0fora<2−12a<2^\{\-12\},d=0d=0\. FP64 evaluation leaves 31\.95% \(KL\) and 5\.7% \(scale gradient\) for FP32 inputs\.

Repair\.Withx=\(a−b\)/bx=\(a\-b\)/bandt=d/at=d/a,

KL=φ⁡\(x\)\+r​ξ​\(−t\),ξ⁡\(−t\)=e−t−1\+t≥0\.\\mathrm\{KL\}=\\varphi\(x\)\+r\\,\\xi\(\-t\),\\qquad\\xi\(\-t\)=e^\{\-t\}\-1\+t\\geq 0\.ξ⁡\(−t\)\\xi\(\-t\)andχ⁡\(t\)=1−\(1\+t\)​e−t\\chi\(t\)=1\-\(1\+t\)e^\{\-t\}use alternating series fort≤1t\\leq 1\(7/11/18 and 8/12/19 terms\) andexpm1\\operatorname\{expm1\}beyond\. Gradients:∂μp=−sgn\(μp−μq\)expm1\(−t\)/b\\partial\_\{\\mu\_\{p\}\}=\-\\mathrm\{sgn\}\(\\mu\_\{p\}\-\\mu\_\{q\}\)\\,\\operatorname\{expm1\}\(\-t\)/b,∂a=\(a−b\)/\(ab\)−χ\(t\)/b\\partial\_\{a\}=\(a\-b\)/\(ab\)\-\\chi\(t\)/b,∂b=−\(x\+rξ\(−t\)\)/b\\partial\_\{b\}=\-\(x\+r\\,\\xi\(\-t\)\)/b\. Products of the formt2​X/bt^\{2\}X/bare evaluated as\(t⋅X\)​\(t/b\)\(t\\cdot X\)\(t/b\)to avoid underflow\.

Why it works\.Two non\-negative terms\. The series alternate with decreasing terms, so the tail is bounded by the first neglected term: the relative truncation afterKKterms is≤3/\(K\+2\)\!\\leq 3/\(K\+2\)\!forξ⁡\(−t\)\\xi\(\-t\)and≤6​\(K\+1\)/\(K\+2\)\!\\leq 6\(K\+1\)/\(K\+2\)\!forχ\\chi, belowu/16u/16\. There is no structural zero of the scale gradients ford≠0d\\neq 0: it would requiree−te^\{\-t\}rational for rationalt≠0t\\neq 0, impossible by the Lindemann–Weierstrass theorem\. The genuine zero crossings of∂a,∂b\\partial\_\{a\},\\partial\_\{b\}are resolved by the higher precision\.

Frozen data\(339,692\)\. KL:Official58\.32 / 85\.87%;Off\.\+2\.24 / 31\.95%;Rewrite1\.46 / 0% \(FP16 residue only\)\. Gradients:Official45\.4–79\.7%;Rewrite≤0\.15%\\leq 0\.15\\%, with 86 new cases at zero crossings \(<0\.03%<0\.03\\%of the repaired ones\)\.

### B\.8Pareto

Official\.Forsp≥sqs\_\{p\}\\geq s\_\{q\}\(scales\) and shapesap,aqa\_\{p\},a\_\{q\}:KL=aq​log⁡spsq−log⁡aqap\+aqap−1\\mathrm\{KL\}=a\_\{q\}\\log\\dfrac\{s\_\{p\}\}\{s\_\{q\}\}\-\\log\\dfrac\{a\_\{q\}\}\{a\_\{p\}\}\+\\dfrac\{a\_\{q\}\}\{a\_\{p\}\}\-1\.

Error\.FourO⁡\(1\)O\(1\)terms for a small KL \(33\.6% spurious zeros in FP16\); autograd’s shape gradients pass through1−1/\(aq/ap\)1\-1/\(a\_\{q\}/a\_\{p\}\)\. Scale gradients are accurate\. FP64 evaluation leaves 6\.99% for FP32 inputs\.

Repair\.Withx=\(sp−sq\)/sq≥0x=\(s\_\{p\}\-s\_\{q\}\)/s\_\{q\}\\geq 0andy=\(aq−ap\)/apy=\(a\_\{q\}\-a\_\{p\}\)/a\_\{p\},

KL=aq​log1p⁡\(x\)\+φ⁡\(y\)\.\\mathrm\{KL\}=a\_\{q\}\\,\\operatorname\{log1p\}\(x\)\+\\varphi\(y\)\.Gradients:∂sp=aq/sp\\partial\_\{s\_\{p\}\}=a\_\{q\}/s\_\{p\},∂sq=−aq/sq\\partial\_\{s\_\{q\}\}=\-a\_\{q\}/s\_\{q\},∂ap=\(\(ap−aq\)/ap\)/ap\\partial\_\{a\_\{p\}\}=\(\(a\_\{p\}\-a\_\{q\}\)/a\_\{p\}\)/a\_\{p\},∂aq=log1p\(x\)\+\(aq−ap\)/\(apaq\)\\partial\_\{a\_\{q\}\}=\\operatorname\{log1p\}\(x\)\+\(a\_\{q\}\-a\_\{p\}\)/\(a\_\{p\}a\_\{q\}\); when the second, negative, term of∂aq\\partial\_\{a\_\{q\}\}overflows alone while the sum is finite, the merged numerator\(aq​log1p⁡\(x\)\+\(aq−ap\)/ap\)/aq\(a\_\{q\}\\operatorname\{log1p\}\(x\)\+\(a\_\{q\}\-a\_\{p\}\)/a\_\{p\}\)/a\_\{q\}is used\.

Why it works\.Two non\-negative terms; the series occurs only inφ≤KL\\varphi\\leq\\mathrm\{KL\}\. Forsp≠sqs\_\{p\}\\neq s\_\{q\},log1p⁡\(x\)\\operatorname\{log1p\}\(x\)is the logarithm of a rational number≠1\\neq 1, hence transcendental, so∂aq\\partial\_\{a\_\{q\}\}has no structural zero\.

Frozen data\(206,950\)\. KL:Official73\.52 / 83\.76%;Off\.\+0 / 6\.99%;Rewrite0 / 0%\. Shape gradients:Official47\.9–81\.4%;Rewrite0\.40 / 0\.50% on∂aq\\partial\_\{a\_\{q\}\}\(14 new cases at its zero crossing\)\.

### B\.9Bernoulli \(probs and logits\)

Official\.With logitslp,lql\_\{p\},l\_\{q\}and probabilitiesp=σ⁡\(lp\)p=\\sigma\(l\_\{p\}\),q=σ⁡\(lq\)q=\\sigma\(l\_\{q\}\):KL=p⁡\(softplus⁡\(−lq\)−softplus⁡\(−lp\)\)\+\(1−p\)​\(softplus⁡\(lq\)−softplus⁡\(lp\)\)\\mathrm\{KL\}=p\\,\(\\mathrm\{softplus\}\(\-l\_\{q\}\)\-\\mathrm\{softplus\}\(\-l\_\{p\}\)\)\+\(1\-p\)\\,\(\\mathrm\{softplus\}\(l\_\{q\}\)\-\\mathrm\{softplus\}\(l\_\{p\}\)\)\. Theprobsconstructor first formsl=log⁡c−log1p⁡\(−c\)l=\\log c\-\\operatorname\{log1p\}\(\-c\)fromc=clamp⁡\(p,eps,1−eps\)c=\\mathrm\{clamp\}\(p,\\texttt\{eps\},1\-\\texttt\{eps\}\)\.

Error\.Each term is a probability times a difference of two nearly equal softplus values, and the two terms have opposite signs, whileKL≈\(p−q\)2/\(2​p​\(1−p\)\)\\mathrm\{KL\}\\approx\(p\-q\)^\{2\}/\(2p\(1\-p\)\): 17\.5–20\.8% of the outputs are negative\. Gradients inherit the same chain plus the sigmoid/logit conversion and the clamp\. For large\|l\|\|l\|the probability is subnormal\. FP64 evaluation leaves 27\.15% \(probs\) and 25\.48% \(logits\) for FP32 inputs\.

Repair \(both parameterisations\)\.Withx1=q/p−1x\_\{1\}=q/p\-1andx2=\(1−q\)/\(1−p\)−1x\_\{2\}=\(1\-q\)/\(1\-p\)\-1,

KL=p​φ​\(x1\)\+\(1−p\)​φ​\(x2\),\\mathrm\{KL\}=p\\,\\varphi\(x\_\{1\}\)\+\(1\-p\)\\,\\varphi\(x\_\{2\}\),with series argumentss1=\(q−p\)/\(p\+q\)s\_\{1\}=\(q\-p\)/\(p\+q\),s2=\(p−q\)/\(\(1−p\)\+\(1−q\)\)s\_\{2\}=\(p\-q\)/\(\(1\-p\)\+\(1\-q\)\)\.*Probs*: no clamp;∂p=log1p\(x2\)−log1p\(x1\)\\partial\_\{p\}=\\operatorname\{log1p\}\(x\_\{2\}\)\-\\operatorname\{log1p\}\(x\_\{1\}\),∂q=\(q−p\)/\(q\(1−q\)\)\\partial\_\{q\}=\(q\-p\)/\(q\(1\-q\)\); closed forms atp,q∈\{0,1\}p,q\\in\\\{0,1\\\}\.*Logits*\(z=lpz=l\_\{p\},w=lqw=l\_\{q\}\):∂z=p\(1−p\)\(z−w\)\\partial\_\{z\}=p\(1\-p\)\(z\-w\),∂w=q−p\\partial\_\{w\}=q\-p; probabilities, log\-ratios andp−qp\-qare written in logit space \(σ\\sigmathroughe−\|z\|e^\{\-\|z\|\}; a softplus difference written by hand, becausetorch\.nn\.functional\.softplusreturns its argumentyyfory\>20y\>20and thereby dropslog1p⁡\(e−y\)\\operatorname\{log1p\}\(e^\{\-y\}\)in FP64;p−q=p⁡\(1−q\)​\(1−ew−z\)p\-q=p\(1\-q\)\(1\-e^\{w\-z\}\)for\|z−w\|≤2\|z\-w\|\\leq 2\)\. Where a probability is subnormal \(\|logit\|\>9\.70\|\\mathrm\{logit\}\|\>9\.70,87\.3487\.34,708\.40708\.40for FP16, FP32, FP64\), every “probability×\\timesfactor” is evaluated as a product of two or four normal factorsez/2e^\{z/2\},ez/4e^\{z/4\}with one rounding\.Oursuses the midpoint\-aware final rounding of[SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1)for\|z\|,\|w\|≤1/16\|z\|,\|w\|\\leq 1/16\.

Why it works\.Two non\-negative terms; KL cannot be negative; the interior identityp=qp=qgives three exact\+0\+0\(including−0\-0versus\+0\+0logits\)\. The midpoint rule is necessary because for\|z\|,\|w\|≈10−4\|z\|,\|w\|\\approx 10^\{\-4\}the true gradient lies within10−710^\{\-7\}ULP of an FP16 subnormal midpoint \(its leading term\(w−z\)/4\(w\-z\)/4*is*a midpoint\); without it, the FP32\-evaluated variant misrounds 7 samples of the benchmark\.

Frozen data\(327,099 probs; 353,392 logits\)\. KL, probs:Official61\.43 / 81\.06%;Off\.\+0\.29 / 27\.15%;Rewrite0\.41 / 0%\. KL, logits:Official49\.92 / 75\.95%;Off\.\+1\.29 / 25\.48%;Rewrite0\.012 / 0\.012%\.PP\-gradient:Official60\.3–78\.0%;Off\.\+1\.5–9\.5%;Rewrite≤0\.002%\\leq 0\.002\\%\. Exhaustive check of all2\.36×1082\.36\\times 10^\{8\}FP16 pairs\(p,q\)\(p,q\):Rewriteat most 6 ULP \(KL\), FP32 evaluation at most 1 ULP\.

### B\.10Binomial \(probs and logits\)

Official\.KL=n⁡\(p⁡\(lp−lq\)\+log1p⁡\(−p\)−log1p⁡\(−q\)\)\\mathrm\{KL\}=n\\big\(p\\,\(l\_\{p\}\-l\_\{q\}\)\+\\operatorname\{log1p\}\(\-p\)\-\\operatorname\{log1p\}\(\-q\)\\big\)\(equal total countsn∈\{1,10,100,1000\}n\\in\\\{1,10,100,1000\\\}in our data\)\.

Error\.Three terms of mixed sign: 13\.4–23\.2% negative and 16\.6–30\.2% spurious zeros\. Whenqqrounds to 1,log1p⁡\(−q\)=−∞\\operatorname\{log1p\}\(\-q\)=\-\\infty: the logits configuration still returns∞\\inftyfor 2\.2% \(FP32 evaluation\) and 5\.3% \(FP64 evaluation\) of FP16 inputs\. FP64 evaluation leaves 37\.5% \(probs\) and 43\.8% \(logits\) for FP32 inputs\.

Repair\.KL=n⋅KLBernoulli\\mathrm\{KL\}=n\\cdot\\mathrm\{KL\}\_\{\\mathrm\{Bernoulli\}\}with the Bernoulli algorithm, butnnmust enter*before*any subnormal intermediate is formed and never in a product with an intermediate that can overflow: e\.g\. inside the series segmentn​φ=\(\(n​x\)​s\)−\(\(\(\(2​n\)​s\)​s\)​s\)​Q​\(z\)n\\varphi=\(\(n\\,x\)\\,s\)\-\(\(\(\(2n\)\\,s\)\\,s\)\\,s\)\\,Q\(z\), then multiplied byppor1−p1\-p; in logit spacennmultiplies only normal factors; the midpoint rule includesnnin its exact leading term\.

Why it works\.Each expression isnntimes an already verified Bernoulli quantity with the multiplications reordered; afternnhas entered, every further step multiplies by a factor≤1\\leq 1or is an exact addition\. Multiplying bynnlast would make the same\-precision FP16 KL wrong by up to 316 ULP and misround 7 gradients in the FP32\-evaluated variant\.

Frozen data\(329,885 probs; 345,715 logits\)\. KL, probs:Official83\.86 / 85\.60%;Off\.\+10\.43 / 37\.51%;Rewrite0\.32 / 0%\. KL, logits:Official69\.69 / 86\.95%;Off\.\+34\.93 / 43\.79% \(6\.12% for FP16 inputs even in FP64\);Rewrite0\.020 / 0\.031%\.PP\-gradient:Official73\.7–87\.8%\.

### B\.11Geometric \(probs and logits\)

Official\.For the failure\-count GeometricP⁡\(k\)=p​\(1−p\)kP\(k\)=p\(1\-p\)^\{k\}:KL=−H⁡\(p\)−log1p⁡\(−q\)/p−logit⁡\(q\)\\mathrm\{KL\}=\-H\(p\)\-\\operatorname\{log1p\}\(\-q\)/p\-\\mathrm\{logit\}\(q\), where the entropyH⁡\(p\)H\(p\)ofPPis computed asbinary\_cross\_entropy\_with\_logits\(logits,probs\)/p\(\\text\{logits\},\\text\{probs\}\)/p\.

Error\.Three terms, each a logarithm or a logarithm divided bypp, sum to a much smaller KL: the worst family in our study \(91–97% significant KL errors, 19–27% negative\)\. With logits,qqrounding to 1 gives−∞\-\\infty: 2\.8–12\.2% infinite and 1\.6–2\.5% NaN KL values, and 4\.4–15\.8% NaN gradients\. FP64 evaluation leaves 48\.4% \(probs\) and 51\.5% \(logits\) for FP32 inputs\.

Repair\.The Geometric KL equals the Bernoulli KL divided bypp\. Withx1=q/p−1x\_\{1\}=q/p\-1,x2=\(1−q\)/\(1−p\)−1x\_\{2\}=\(1\-q\)/\(1\-p\)\-1,φ¯​\(x\)=φ​\(x\)/x\\bar\{\\varphi\}\(x\)=\\varphi\(x\)/xandg⁡\(x\)=log1p⁡\(x\)/xg\(x\)=\\operatorname\{log1p\}\(x\)/x\(withφ¯​\(0\)=0\\bar\{\\varphi\}\(0\)=0andg⁡\(0\)=1g\(0\)=1\), all outputs are single products:

KL=x1​\(φ¯​\(x1\)−φ¯​\(x2\)\),\\mathrm\{KL\}=x\_\{1\}\\big\(\\bar\{\\varphi\}\(x\_\{1\}\)\-\\bar\{\\varphi\}\(x\_\{2\}\)\\big\),whereφ¯​\(x1\)\\bar\{\\varphi\}\(x\_\{1\}\)andφ¯​\(x2\)\\bar\{\\varphi\}\(x\_\{2\}\)have opposite signs, so their difference does not cancel; inside the series segmentφ¯=s−a⁡\(1−s\)\\bar\{\\varphi\}=s\-a\(1\-s\)andg=\(1−s\)​\(1\+a\)g=\(1\-s\)\(1\+a\)witha=s2​Q​\(s2\)a=s^\{2\}Q\(s^\{2\}\)\.*Probs*:∂p=\(\(−x1\)g\(x2\)\)/p/\(1−p\)\\partial\_\{p\}=\(\(\-x\_\{1\}\)\\,g\(x\_\{2\}\)\)/p/\(1\-p\),∂q=\(x1/q\)/\(1−q\)\\partial\_\{q\}=\(x\_\{1\}/q\)/\(1\-q\)\.*Logits*\(zzandwwthe logits ofPPandQQ\):∂w=x1\\partial\_\{w\}=x\_\{1\},∂z=−x1g\(x2\)\\partial\_\{z\}=\-x\_\{1\}g\(x\_\{2\}\), withx1=σ⁡\(−w\)​expm1⁡\(w−z\)x\_\{1\}=\\sigma\(\-w\)\\,\\operatorname\{expm1\}\(w\-z\)evaluated in three branches by the signs ofw−zw\-zandww\(the differencez−wz\-wexact by TwoSum where Sterbenz’s lemma does not apply\) and small or large factors \(σ⁡\(−w\)\\sigma\(\-w\),e−ze^\{\-z\}\) multiplied last through split exponents\. Midpoint\-aware rounding for\|z\|,\|w\|≤1/16\|z\|,\|w\|\\leq 1/16\(leading term\(w−z\)/2\(w\-z\)/2\)\.

Why it works\.The identities used arer​x2=−x1r\\,x\_\{2\}=\-x\_\{1\}withr=\(1−p\)/pr=\(1\-p\)/p, andlog1p⁡x=2​atanh⁡s\\operatorname\{log1p\}x=2\\operatorname\{atanh\}sonly; the KL is a single product, so there is no cancellation, no subnormal intermediate and no premature overflow\. As a check, the truth was also evaluated from the official formula at 1,500 digits; it agrees after rounding\.

Frozen data\(304,861 probs; 352,178 logits\)\. KL, probs:Official91\.17 / 94\.19%;Off\.\+36\.53 / 48\.35%;Rewrite0 / 0%\. KL, logits:Official94\.35 / 96\.62%;Off\.\+42\.77 / 51\.47% \(6\.60% for FP16 inputs in FP64\);Rewrite0\.17% / one sample\.PP\-gradient:Official78\.5–92\.5%;Off\.\+13\.8–19\.5%\.

### B\.12Gumbel

Official\.WithP=\(m,b\)P=\(m,b\),Q=\(n,c\)Q=\(n,c\)\(location, scale\) and Euler’s constantγ\\gamma:KL=−log⁡bc−nc\+mc\+γ​bc\+exp⁡\(nc\+log⁡Γ⁡\(1\+bc\)−mc\)−\(1\+γ\)\\mathrm\{KL\}=\-\\log\\frac\{b\}\{c\}\-\\frac\{n\}\{c\}\+\\frac\{m\}\{c\}\+\\gamma\\frac\{b\}\{c\}\+\\exp\\\!\\big\(\\frac\{n\}\{c\}\+\\log\\Gamma\(1\+\\frac\{b\}\{c\}\)\-\\frac\{m\}\{c\}\\big\)\-\(1\+\\gamma\)\.

Error\.n/cn/candm/cm/care rounded separately, so for large\|loc\|/scale\|\\text\{loc\}\|/\\text\{scale\}their absolute error exceeds the KL; fourO⁡\(1\)O\(1\)terms then sum to a small value \(10–14% negative, 17–36% spurious zeros\)\. Gradients inherit the cancellation; 7\.28% of the FP16QQ\-scale gradients are NaN\. FP64 evaluation leaves 38\.4% for FP32 inputs\.

Repair\.Witht=\(m−n\)/ct=\(m\-n\)/c,r=b/cr=b/c,y=log⁡Γ⁡\(1\+r\)−ty=\\log\\Gamma\(1\+r\)\-tandx=expm1⁡\(y\)x=\\operatorname\{expm1\}\(y\),

KL=φ⁡\(x\)\+H⁡\(r\),H⁡\(r\)=log⁡Γ⁡\(r\)\+γ⁡\(r−1\)≥0,\\mathrm\{KL\}=\\varphi\(x\)\+H\(r\),\\qquad H\(r\)=\\log\\Gamma\(r\)\+\\gamma\\,\(r\-1\)\\geq 0,whereHHis convex withH⁡\(1\)=H′​\(1\)=0H\(1\)=H^\{\\prime\}\(1\)=0\. Gradients:∂m=−x/c\\partial\_\{m\}=\-x/c,∂n=x/c\\partial\_\{n\}=x/c;c∂bc\\,\\partial\_\{b\}has two exactly equivalent forms,x​ψ​\(1\+r\)\+\(ψ⁡\(r\)\+γ\)x\\,\\psi\(1\+r\)\+\(\\psi\(r\)\+\\gamma\)andey​ψ​\(1\+r\)\+\(γ−1/r\)e^\{y\}\\psi\(1\+r\)\+\(\\gamma\-1/r\), of which the one with the smaller terms is chosen;c∂c=−\(tc∂m\+rc∂b\)c\\,\\partial\_\{c\}=\-\(t\\,c\\,\\partial\_\{m\}\+r\\,c\\,\\partial\_\{b\}\)by scale invariance\.log⁡Γ\\log\\Gammaandψ\\psiare implemented from exact\-coefficient zeta series in four ranges ofrr\([SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1)\)\. Eight FP16 range guards handle overflow ofm−nm\-n, ofxx, and of individual products\.

Why it works\.Two non\-negative terms; the identities used arelog⁡Γ⁡\(1\+r\)−log⁡r=log⁡Γ⁡\(r\)\\log\\Gamma\(1\+r\)\-\\log r=\\log\\Gamma\(r\),ψ⁡\(1\+r\)−1/r=ψ⁡\(r\)\\psi\(1\+r\)\-1/r=\\psi\(r\)andlog1p⁡\(x\)=y\\operatorname\{log1p\}\(x\)=y\. Residual cancellation in the gradients occurs only at genuine zeros \(y≈0y\\approx 0,∂c\\partial\_\{c\}crossing zero\)\.

Frozen data\(339,249\)\. KL:Official66\.18 / 90\.02%;Off\.\+13\.78 / 38\.42%;Rewrite0\.007 / 0%\. Gradients:Official73\.7–92\.5%;Off\.\+2\.4–8\.7%;Rewrite0\.31–0\.75% \(117 new cases near gradient zeros,<0\.02%<0\.02\\%of the repaired ones\)\.

### B\.13Gamma

Official\.WithP=\(ap,bp\)P=\(a\_\{p\},b\_\{p\}\),Q=\(aq,bq\)Q=\(a\_\{q\},b\_\{q\}\)\(concentration, rate\):KL=aq​log⁡bpbq\+log⁡Γ⁡\(aq\)−log⁡Γ⁡\(ap\)\+\(ap−aq\)​ψ​\(ap\)\+\(bq−bp\)​apbp\\mathrm\{KL\}=a\_\{q\}\\log\\frac\{b\_\{p\}\}\{b\_\{q\}\}\+\\log\\Gamma\(a\_\{q\}\)\-\\log\\Gamma\(a\_\{p\}\)\+\(a\_\{p\}\-a\_\{q\}\)\\,\\psi\(a\_\{p\}\)\+\(b\_\{q\}\-b\_\{p\}\)\\frac\{a\_\{p\}\}\{b\_\{p\}\}\.

Error\.Four terms of sizeO⁡\(a\)O\(a\)orO⁡\(a​log⁡a\)O\(a\\log a\)for a KL between10−1510^\{\-15\}and 1; the cancellation grows withaa\(up to10410^\{4\}in R1\): about 27% negative\. Autograd differentiates throughdigamma; in FP16lgammaoverflows foraaabove about 8200 \(about 4% NaN and 4% infinite concentration gradients\)\. FP64 evaluation leaves 36\.9% for FP32 inputs\.

Repair\.Withα=aq/ap−1\\alpha=a\_\{q\}/a\_\{p\}\-1andz=ap​bqaq​bp−1z=\\dfrac\{a\_\{p\}b\_\{q\}\}\{a\_\{q\}b\_\{p\}\}\-1\(ratio of the two means minus one\),

KL=aq​φ​\(z\)\+12​φ​\(α\)\+Bw​\(aq,ap\),\\mathrm\{KL\}=a\_\{q\}\\,\\varphi\(z\)\+\\tfrac\{1\}\{2\}\\varphi\(\\alpha\)\+B\_\{w\}\(a\_\{q\},a\_\{p\}\),whereBwB\_\{w\}is the Bregman divergence of the convex Binet functionww; all three terms are non\-negative\. Gradients:∂ap=\(aq/ap\)z−α\(12\+g\(ap\)\)/ap\\partial\_\{a\_\{p\}\}=\(a\_\{q\}/a\_\{p\}\)z\-\\alpha\(\\tfrac\{1\}\{2\}\+g\(a\_\{p\}\)\)/a\_\{p\}withg⁡\(s\)=s2​w′′​\(s\)g\(s\)=s^\{2\}w^\{\\prime\\prime\}\(s\);∂bp=−aqz/bp\\partial\_\{b\_\{p\}\}=\-a\_\{q\}z/b\_\{p\};∂aq=ΔL−log1p\(z\)\\partial\_\{a\_\{q\}\}=\\Delta L\-\\operatorname\{log1p\}\(z\)withL=ψ−logL=\\psi\-\\logandΔ​L=L⁡\(aq\)−L⁡\(ap\)\\Delta L=L\(a\_\{q\}\)\-L\(a\_\{p\}\);∂bq=aqz/bq\\partial\_\{b\_\{q\}\}=a\_\{q\}z/b\_\{q\}\. The product difference inzzis formed exactly using scaled TwoProduct and TwoSum before division, so equal means givez=0z=0exactly\. For near pairsBwB\_\{w\}andΔ​L\\Delta Lare same\-sign integrals evaluated by Gauss–Legendre quadrature; for far pairswwis evaluated by shifts and series\. Eight FP16 range guards use the mantissa/exponent form\.

Why it works\.Three non\-negative terms, an exact product difference forzz, and same\-sign quadrature sums\. Equivalence with the official formula was checked at 60 and 70 digits; the truth is computed from the official formula at 120–160 digits and does not depend on the rewriting\.

Frozen data\(331,067\)\. KL:Official78\.47 / 88\.67%;Off\.\+9\.09 / 36\.92%;Rewrite0\.003 / 0\.002%\. Gradients:Official64\.4–82\.5%;Rewrite≤0\.21%\\leq 0\.21\\%\(21 new cases near gradient zeros\)\.

### B\.14Beta

Official\.With concentrationsP=\(a,b\)P=\(a,b\),Q=\(c,d\)Q=\(c,d\)and the Beta functionBB:KL=log⁡B⁡\(c,d\)−log⁡B⁡\(a,b\)\+\(a−c\)​ψ​\(a\)\+\(b−d\)​ψ​\(b\)\+\(c−a\+d−b\)​ψ​\(a\+b\)\\mathrm\{KL\}=\\log B\(c,d\)\-\\log B\(a,b\)\+\(a\-c\)\\psi\(a\)\+\(b\-d\)\\psi\(b\)\+\(c\-a\+d\-b\)\\psi\(a\+b\), i\.e\. sixlgammaand threedigammaterms\.

Error\.Nine terms of sizeO⁡\(a​log⁡a\)O\(a\\log a\): about one third of the non\-identity outputs are negative; in FP16lgamma\(a\+b\)\(a\+b\)overflows \(2\.4% NaN\)\. The gradients are differences of first\-order terms and, for small parameters, of terms≈−1/a\\approx\-1/aand≈−1/b\\approx\-1/b\. FP64 evaluation leaves 43\.8% for FP32 inputs\.

Repair\.Lets=a\+bs=a\+b,sq=c\+ds\_\{q\}=c\+d,α=c/a−1\\alpha=c/a\-1,β=d/b−1\\beta=d/b\-1,z=a​d/\(b​c\)−1z=ad/\(bc\)\-1,v⁡\(x\)=log⁡Γ⁡\(x\)−x​log⁡x\+xv\(x\)=\\log\\Gamma\(x\)\-x\\log x\+xandω⁡\(x\)=x​v′′​\(x\)\\omega\(x\)=x\\,v^\{\\prime\\prime\}\(x\)\(decreasing\)\. Then

KL=c​φ​\(r1\)\+d​φ​\(r0\)\+BV,r1=bs​z,r0=−as​z1\+z,\\mathrm\{KL\}=c\\,\\varphi\(r\_\{1\}\)\+d\\,\\varphi\(r\_\{0\}\)\+B\_\{V\},\\qquad r\_\{1\}=\\tfrac\{b\}\{s\}\\,z,\\quad r\_\{0\}=\-\\tfrac\{a\}\{s\}\\,\\tfrac\{z\}\{1\+z\},whereBV=Bv​\(c,a\)\+Bv​\(d,b\)−Bv​\(sq,s\)B\_\{V\}=B\_\{v\}\(c,a\)\+B\_\{v\}\(d,b\)\-B\_\{v\}\(s\_\{q\},s\)is the Bregman divergence of the convex functionV⁡\(a,b\)=v⁡\(a\)\+v⁡\(b\)−v⁡\(a\+b\)V\(a,b\)=v\(a\)\+v\(b\)\-v\(a\+b\)\. Adding threeBvB\_\{v\}directly cancels when one parameter is much smaller than the other \(by a factor of about 100 in our samples\), so for near pairs

BV=12​𝒬\+T3​\(c,a\)\+T3​\(d,b\)−T3​\(sq,s\),𝒬=a⁡\(ω⁡\(a\)−ω⁡\(s\)\)​α2\+b⁡\(ω⁡\(b\)−ω⁡\(s\)\)​β2\+ω⁡\(s\)​a​bs​\(β−α\)2,B\_\{V\}=\\tfrac\{1\}\{2\}\\mathcal\{Q\}\+T\_\{3\}\(c,a\)\+T\_\{3\}\(d,b\)\-T\_\{3\}\(s\_\{q\},s\),\\qquad\\mathcal\{Q\}=a\\big\(\\omega\(a\)\-\\omega\(s\)\\big\)\\alpha^\{2\}\+b\\big\(\\omega\(b\)\-\\omega\(s\)\\big\)\\beta^\{2\}\+\\omega\(s\)\\tfrac\{ab\}\{s\}\(\\beta\-\\alpha\)^\{2\},with three non\-negative terms in𝒬\\mathcal\{Q\}and the third\-order remaindersT3​\(x,y\)=Bv​\(x,y\)−12​\(x−y\)2​v′′​\(y\)=∫yx12​\(x−t\)2​v′′′​\(t\)​𝑑tT\_\{3\}\(x,y\)=B\_\{v\}\(x,y\)\-\\tfrac\{1\}\{2\}\(x\-y\)^\{2\}v^\{\\prime\\prime\}\(y\)=\\int\_\{y\}^\{x\}\\tfrac\{1\}\{2\}\(x\-t\)^\{2\}v^\{\\prime\\prime\\prime\}\(t\)\\,dt, evaluated throught3​v′′′​\(t\)=t3​w′′′​\(t\)−1t^\{3\}v^\{\\prime\\prime\\prime\}\(t\)=t^\{3\}w^\{\\prime\\prime\\prime\}\(t\)\-1with the Binet functionww; for far pairs the threeBvB\_\{v\}are used directly\. Gradients use identities that are exact for arbitrary shifts, e\.g\.∂a=−α\(ω\(a\)−ω\(s\)\)\+bs\(β−α\)\(1\+ω\(s\)\)\\partial\_\{a\}=\-\\alpha\\,\(\\omega\(a\)\-\\omega\(s\)\)\+\\tfrac\{b\}\{s\}\(\\beta\-\\alpha\)\(1\+\\omega\(s\)\)\. The shared numeratora​d−b​cad\-bcis formed exactly from scaled mantissas before evaluatingzz,r1r\_\{1\},r0r\_\{0\}andβ−α\\beta\-\\alpha\.

Why it works\.All leading terms are non\-negative, with their ratio numerators computed exactly; residual cancellation is confined to the third\-order remainders, to theQQ\-side gradients near their zeros and to far pairs, all inside the error estimate\.

Frozen data\(329,156\)\. KL:Official85\.38 / 92\.62%;Off\.\+40\.32 / 43\.77%;Rewrite2\.11 / 0\.65%\. Gradients:Official73\.9–87\.6%;Off\.\+0\.7–2\.9%;Rewrite0\.15–1\.57%\.Rewriteintroduces 961 new cases, mostlyQQ\-side gradients at small parameters \(at most 0\.29% of the repaired ones\)\. This is the family in which the same\-precision variant is weakest\.

### B\.15Dirichlet \(K=2,4,16K=2,4,16\)

Official\.With concentrationsa1,…,aKa\_\{1\},\\dots,a\_\{K\}ofPPandc1,…,cKc\_\{1\},\\dots,c\_\{K\}ofQQ:KL=log⁡Γ⁡\(s\)−∑jlog⁡Γ⁡\(aj\)−log⁡Γ⁡\(sq\)\+∑jlog⁡Γ⁡\(cj\)\+∑j\(aj−cj\)​\(ψ⁡\(aj\)−ψ⁡\(s\)\)\\mathrm\{KL\}=\\log\\Gamma\(s\)\-\\sum\_\{j\}\\log\\Gamma\(a\_\{j\}\)\-\\log\\Gamma\(s\_\{q\}\)\+\\sum\_\{j\}\\log\\Gamma\(c\_\{j\}\)\+\\sum\_\{j\}\(a\_\{j\}\-c\_\{j\}\)\\big\(\\psi\(a\_\{j\}\)\-\\psi\(s\)\\big\),s=∑ajs=\\sum a\_\{j\},sq=∑cjs\_\{q\}=\\sum c\_\{j\}\.

Error\.Largelgamma/digammaterms cancel almost completely: the KL is not correctly rounded in 98\.76% \(FP16\) and 99\.92% \(FP32\) of the samples, negative in 37\.6% / 32\.8%, NaN in 3\.49% of FP16 samples, and the gradient direction is reversed in 6\.29% / 1\.45%\. In FP64, 56\.3% of FP32\-input KL values are still not correctly rounded and 2\.1% are negative\.

Repair\.The Beta construction generalises\. Withαj=cj/aj−1\\alpha\_\{j\}=c\_\{j\}/a\_\{j\}\-1,γ=\(sq−s\)/s\\gamma=\(s\_\{q\}\-s\)/s,δj=αj−γ\\delta\_\{j\}=\\alpha\_\{j\}\-\\gamma,rj=aj​sq/\(cj​s\)−1r\_\{j\}=a\_\{j\}s\_\{q\}/\(c\_\{j\}s\)\-1,

KL=∑jcj​φ​\(rj\)\+12​𝒬\+∑jT3​\(cj,aj\)−T3​\(sq,s\),𝒬=∑jaj​\(ω⁡\(aj\)−ω⁡\(s\)\)​αj2\+ω⁡\(s\)​∑jaj​δj2,\\mathrm\{KL\}=\\sum\_\{j\}c\_\{j\}\\,\\varphi\(r\_\{j\}\)\+\\tfrac\{1\}\{2\}\\mathcal\{Q\}\+\\sum\_\{j\}T\_\{3\}\(c\_\{j\},a\_\{j\}\)\-T\_\{3\}\(s\_\{q\},s\),\\qquad\\mathcal\{Q\}=\\sum\_\{j\}a\_\{j\}\\big\(\\omega\(a\_\{j\}\)\-\\omega\(s\)\\big\)\\alpha\_\{j\}^\{2\}\+\\omega\(s\)\\sum\_\{j\}a\_\{j\}\\delta\_\{j\}^\{2\},with every term of𝒬\\mathcal\{Q\}non\-negative andT3T\_\{3\}as for Beta; with a far pair,KL=∑jcj​φ​\(rj\)\+∑jBv​\(cj,aj\)−Bv​\(sq,s\)\\mathrm\{KL\}=\\sum\_\{j\}c\_\{j\}\\varphi\(r\_\{j\}\)\+\\sum\_\{j\}B\_\{v\}\(c\_\{j\},a\_\{j\}\)\-B\_\{v\}\(s\_\{q\},s\)\.δj\\delta\_\{j\}andrjr\_\{j\}are formed from double\-length ratios, never asαj−γ\\alpha\_\{j\}\-\\gamma\. When∑aj\\sum a\_\{j\}overflows in FP16,ssis represented as16​s¯16\\bar\{s\}withs¯\\bar\{s\}an exact expansion of∑jaj/16\\sum\_\{j\}a\_\{j\}/16\.

Why it works\.As for Beta, to which the formula reduces forK=2K=2\.

Frozen data\(314,451\)\. KL:Official84\.3–89\.0 / 92\.1–93\.8%;Off\.\+29\.6–55\.9 / 41\.6–49\.8%;Rewrite0\.009–2\.45 / 0\.003–0\.69%\. Gradients:Official79–95%\.Oursis correctly rounded on every sample\.

### B\.16Categorical \(probs and logits;K=2,4,16K=2,4,16\)

Official\.OverKKcategories with probabilitiespkp\_\{k\}\(PP\) andqkq\_\{k\}\(QQ\):KL=∑kpk​\(log⁡pk−log⁡qk\)\\mathrm\{KL\}=\\sum\_\{k\}p\_\{k\}\(\\log p\_\{k\}\-\\log q\_\{k\}\), term by term, with terms whereqk=0q\_\{k\}=0set to\+∞\+\\infty\. The constructor produces probabilities and log\-probabilities first:probsusesp=x/∑xp=x/\\sum xfor the raw parametersxxandlog⁡\(clamp⁡\(p,eps,1−eps\)\)\\log\(\\mathrm\{clamp\}\(p,\\texttt\{eps\},1\-\\texttt\{eps\}\)\);logitsusesz−logsumexp⁡\(z\)z\-\\mathrm\{logsumexp\}\(z\)andsoftmaxfor the raw logitszz\.

Error\.Five sources\. \(i\)*Cancellation*: for close distributions each term is first order \(≈pk​ρk\\approx p\_\{k\}\\rho\_\{k\}\) and the sum second order, because∑kpk​ρk=0\\sum\_\{k\}p\_\{k\}\\rho\_\{k\}=0exactly; in additionlog⁡pk\\log p\_\{k\}andlog⁡qk\\log q\_\{k\}carry errors of aboutu​\|log⁡pk\|u\|\\log p\_\{k\}\|\. \(ii\)*Clamp*\(probs\): probabilities belowepsare replaced byepsin value and gradient\. \(iii\)*Spurious\+∞\+\\infty*\(logits\):softmaxunderflows,qkq\_\{k\}is computed as 0 and the term is set to∞\\inftyalthough the true probability is positive\. \(iv\)*Normalisation*: rounding ofp=x/∑xp=x/\\sum xmakes∑p≠1\\sum p\\neq 1, which is amplified in the gradient\. \(v\)*Argument validation*\(FP16 probs\): the simplex check uses a tolerance of10−610^\{\-6\}, below FP16 resolution, and rejects about 25% of valid inputs\. FP64 evaluation leaves 32–38% for FP32 inputs\.

Repair, probs\(raw parametersxxofPPandyyofQQ;Sx=∑xS\_\{x\}=\\sum x,Sy=∑yS\_\{y\}=\\sum y\)\. Withρk=qk/pk−1\\rho\_\{k\}=q\_\{k\}/p\_\{k\}\-1and the linear term removed by∑kpk​ρk=0\\sum\_\{k\}p\_\{k\}\\rho\_\{k\}=0,

KL=∑kpk​φ​\(ρk\),ρk=Nkxk​Sy,Nk=yk​Sx−xk​Sy,\\mathrm\{KL\}=\\sum\_\{k\}p\_\{k\}\\,\\varphi\(\\rho\_\{k\}\),\\qquad\\rho\_\{k\}=\\frac\{N\_\{k\}\}\{x\_\{k\}S\_\{y\}\},\\qquad N\_\{k\}=y\_\{k\}S\_\{x\}\-x\_\{k\}S\_\{y\},∂KL∂yk=Nkyk​Sx​Sy,∂KL∂xk=−log1p⁡\(ρk\)\+KLSx=1Sx​∑j≠kpj​log⁡yj​xkxj​yk\.\\frac\{\\partial\\mathrm\{KL\}\}\{\\partial y\_\{k\}\}=\\frac\{N\_\{k\}\}\{y\_\{k\}S\_\{x\}S\_\{y\}\},\\qquad\\frac\{\\partial\\mathrm\{KL\}\}\{\\partial x\_\{k\}\}=\-\\frac\{\\operatorname\{log1p\}\(\\rho\_\{k\}\)\+\\mathrm\{KL\}\}\{S\_\{x\}\}=\\frac\{1\}\{S\_\{x\}\}\\sum\_\{j\\neq k\}p\_\{j\}\\log\\frac\{y\_\{j\}x\_\{k\}\}\{x\_\{j\}y\_\{k\}\}\.Sx,SyS\_\{x\},S\_\{y\}are exact expansions;NkN\_\{k\}is computed exactly from4​K4KDekker products\. Of the two equivalent forms of∂xk\\partial\_\{x\_\{k\}\}the one with the smaller sum of absolute terms is used\.

Repair, logits\(raw logitszzofPPandwwofQQ;dk=wk−zkd\_\{k\}=w\_\{k\}\-z\_\{k\}\)\. For any constantaaandtk=dk−at\_\{k\}=d\_\{k\}\-a,

KL=log1p⁡\(E\)−M=∑kpk​ξ​\(tk\)−φ⁡\(E\),E=∑kpk​expm1⁡\(tk\),M=∑kpk​tk,\\mathrm\{KL\}=\\operatorname\{log1p\}\(E\)\-M=\\sum\_\{k\}p\_\{k\}\\,\\xi\(t\_\{k\}\)\-\\varphi\(E\),\\quad E=\\sum\_\{k\}p\_\{k\}\\,\\operatorname\{expm1\}\(t\_\{k\}\),\\quad M=\\sum\_\{k\}p\_\{k\}t\_\{k\},∂KL∂zk=−pk​\(tk−M\),∂KL∂wk=pk​\(etk−L−1\),L=log1p⁡\(E\)\.\\frac\{\\partial\\mathrm\{KL\}\}\{\\partial z\_\{k\}\}=\-p\_\{k\}\\,\(t\_\{k\}\-M\),\\qquad\\frac\{\\partial\\mathrm\{KL\}\}\{\\partial w\_\{k\}\}=p\_\{k\}\\big\(e^\{t\_\{k\}\-L\}\-1\\big\),\\quad L=\\operatorname\{log1p\}\(E\)\.The centre is chosen as follows: withr=arg⁡max⁡zr=\\arg\\max zandsk=dk−drs\_\{k\}=d\_\{k\}\-d\_\{r\},c=fl⁡\(∑pk​sk\)c=\\mathrm\{fl\}\(\\sum p\_\{k\}s\_\{k\}\)andtk=sk−ct\_\{k\}=s\_\{k\}\-c, so thatMMis of rounding size\.pk=ezk−zmax/Zp\_\{k\}=e^\{z\_\{k\}\-z\_\{\\max\}\}/Z, withzmax=maxj⁡zjz\_\{\\max\}=\\max\_\{j\}z\_\{j\}andZ=∑jezj−zmaxZ=\\sum\_\{j\}e^\{z\_\{j\}\-z\_\{\\max\}\}, is carried as \(mantissa, exponent\) and cannot underflow to zero;sk,tks\_\{k\},t\_\{k\}are exact expansions of the inputs\. ForE\>1E\>1the formlog⁡\(∑pk​etk\)−M\\log\(\\sum p\_\{k\}e^\{t\_\{k\}\}\)\-Mis used\. Neithersoftmax,log\_softmaxnor a clamp is called\. The vectorised form used downstream \([Equation4](https://arxiv.org/html/2611.30835#S4.E4)\) is an equivalent arrangement of this identity, with the weights taken from the second argument, and is tested against the certified implementation\. It is evaluated in FP64 and rounded once\. The differenceaj−cja\_\{j\}\-c\_\{j\}is centred at the largest student logit, so that a common offset betweenaaandccgives exactly zero value and gradient; its rounding error, if any, is recovered by TwoSum and added back; and whereδj\\delta\_\{j\}is large,qj​eδjq\_\{j\}e^\{\\delta\_\{j\}\}is formed asexp⁡\(log⁡qj\+δj\)\\exp\(\\log q\_\{j\}\+\\delta\_\{j\}\)\. It is not itself certified by interval arithmetic\.

Zero probabilities\.A category withpk=0<qkp\_\{k\}=0<q\_\{k\}contributeslimp↓0p​φ​\(qk/p−1\)=qk\\lim\_\{p\\downarrow 0\}p\\,\\varphi\(q\_\{k\}/p\-1\)=q\_\{k\}\. Such inputs are reduced to the supportSSofPP:KL\(P∥Q\)=KL\(PS∥QS/Q\(S\)\)−logQ\(S\)\\mathrm\{KL\}\(P\\\|Q\)=\\mathrm\{KL\}\(P\_\{S\}\\\|Q\_\{S\}/Q\(S\)\)\-\\log Q\(S\), a sum of two non\-negative terms;qk=0<pkq\_\{k\}=0<p\_\{k\}gives\+∞\+\\infty\.

Why it works\.The cancellation is done algebraically: theprobsKL is a sum of non\-negative terms, while in thelogitsKL,S=∑kpk​ξ​\(tk\)S=\\sum\_\{k\}p\_\{k\}\\xi\(t\_\{k\}\)satisfiesE=M\+SE=M\+SandKL=S−φ⁡\(E\)\\mathrm\{KL\}=S\-\\varphi\(E\)\. When\|M\|≪S≪1\|M\|\\ll S\\ll 1,φ⁡\(E\)/S≈S/2\\varphi\(E\)/S\\approx S/2; forE\>1E\>1the other form is used\.NkN\_\{k\}is exact, so the relative error ofρk\\rho\_\{k\}does not depend on how closeppandqqare\. Proportionalprobsand shiftedlogits\(different parameters, same distribution\) give exactly\+0\+0\. Residual cancellation in the same\-precision variant occurs where a gradient component itself crosses zero; there its ULP error can be large while its error relative to the gradient vector is of orderuu\.

Frozen data\(372,429 probs; 367,746 logits\)\. KL, probs:Official46\.6–47\.6 / 86\.0–86\.9%;Off\.\+3\.4–4\.6 / 32\.2–32\.5%;Rewrite≤0\.01%\\leq 0\.01\\%\. KL, logits:Official59\.4–62\.7 / 88\.3–90\.5%;Off\.\+12\.4–18\.3 / 35\.8–38\.4%;Rewrite<0\.01%<0\.01\\%/ 0\. Gradients:Official47\.5–65\.3 / 75\.0–88\.9%;Rewriteat most 0\.81% \(QQ\-side, logitsK=16K=16, FP32\)\.Rewritereturns∞\\inftyfor a batch ofprobsvalues at the overflow threshold, eight of which \(K=2K=2\) have a finite true KL and are genuine errors\.Oursis correctly rounded on every sample\.

### B\.17ContinuousBernoulli \(probs and logits\)

Official\.With probability parametersλp,λq\\lambda\_\{p\},\\lambda\_\{q\}, natural parametersη=logit⁡\(λ\)\\eta=\\mathrm\{logit\}\(\\lambda\), meanμp\\mu\_\{p\}ofPPand log\-normaliserC⁡\(λ\)C\(\\lambda\):KL=μp​\(ηp−ηq\)\+\[C⁡\(λp\)\+log1p⁡\(−λp\)\]−\[C⁡\(λq\)\+log1p⁡\(−λq\)\]\\mathrm\{KL\}=\\mu\_\{p\}\(\\eta\_\{p\}\-\\eta\_\{q\}\)\+\[C\(\\lambda\_\{p\}\)\+\\operatorname\{log1p\}\(\-\\lambda\_\{p\}\)\]\-\[C\(\\lambda\_\{q\}\)\+\\operatorname\{log1p\}\(\-\\lambda\_\{q\}\)\]\. The constructor clampsλ\\lambdato\[eps,1−eps\]\[\\texttt\{eps\},1\-\\texttt\{eps\}\]\(forlogitsafter a sigmoid, whileη\\etakeeps the raw value\);CCand the mean use a truncated Taylor polynomial forλ∈\(0\.499,0\.501\]\\lambda\\in\(0\.499,0\.501\]and closed forms elsewhere\.

Error\.\(i\)*Clamp*: outside\[eps,1−eps\]\[\\texttt\{eps\},1\-\\texttt\{eps\}\]the normaliser and mean belong to a different distribution than the input; withlogitszzthis happens for\|z\|\>log⁡\(1/eps\)\|z\|\>\\log\(1/\\texttt\{eps\}\)\(about 6\.9, 15\.9, 36 for FP16, FP32, FP64\), where one term uses raw logits and the other two clamped probabilities, so the result is wrong in any precision\. \(ii\) Near pairs: three first\-order terms with a second\-order sum\. \(iii\) Nearλ=12\\lambda=\\tfrac\{1\}\{2\}outside the window: the mean is a difference of two numbers≈1/\(2​\|2​λ−1\|\)\\approx 1/\(2\|2\\lambda\-1\|\), cancelling by a factor of about 500 at the window edge\. \(iv\) Inside the window the polynomial is an approximation, not an identity\. \(v\) NaN gradients in FP16 from the unselected branch oftorch\.where\.

Repair\.Write the log\-partition function asA⁡\(η\)=η/2\+g⁡\(η\)A\(\\eta\)=\\eta/2\+g\(\\eta\)with

g⁡\(x\)=log⁡sinh⁡\(x/2\)x/2=∑k≥1ck​x2​k,ck=B2​k2​k​\(2​k\)\!,g\(x\)=\\log\\frac\{\\sinh\(x/2\)\}\{x/2\}=\\sum\_\{k\\geq 1\}c\_\{k\}x^\{2k\},\\qquad c\_\{k\}=\\frac\{B\_\{2k\}\}\{2k\\,\(2k\)\!\},whereB2​kB\_\{2k\}are the Bernoulli numbers, so that the linear part cancels exactly and, withp=ηpp=\\eta\_\{p\},q=ηqq=\\eta\_\{q\},d=q−pd=q\-p,

KL=g⁡\(q\)−g⁡\(p\)−d​g′​\(p\),∂pKL=−d​g′′​\(p\),∂qKL=g′​\(q\)−g′​\(p\)\.\\mathrm\{KL\}=g\(q\)\-g\(p\)\-d\\,g^\{\\prime\}\(p\),\\qquad\\partial\_\{p\}\\mathrm\{KL\}=\-d\\,g^\{\\prime\\prime\}\(p\),\\qquad\\partial\_\{q\}\\mathrm\{KL\}=g^\{\\prime\}\(q\)\-g^\{\\prime\}\(p\)\.Three regions in\(\|p\|,\|q\|\)\(\|p\|,\|q\|\), each with an exact identity: \(A\) both≤4\\leq 4:KL=d2​∑kck​H2​k−2​\(p,q\)\\mathrm\{KL\}=d^\{2\}\\sum\_\{k\}c\_\{k\}H\_\{2k\-2\}\(p,q\)with the polynomialsHn−2H\_\{n\-2\}defined byqn−pn−n​pn−1​d=d2​Hn−2​\(p,q\)q^\{n\}\-p^\{n\}\-np^\{n\-1\}d=d^\{2\}H\_\{n\-2\}\(p,q\), leading termd2/24d^\{2\}/24, with 17/27/50 terms; \(B\) same sign, both away from 0:KL=φ⁡\(ρ\)−φ⁡\(τ\)−hp​ξ​\(−D\)\\mathrm\{KL\}=\\varphi\(\\rho\)\-\\varphi\(\\tau\)\-h\_\{p\}\\,\\xi\(\-D\)withD=\|q\|−\|p\|D=\|q\|\-\|p\|,ρ=D/\|p\|\\rho=D/\|p\|,Ep=e−\|p\|E\_\{p\}=e^\{\-\|p\|\},Eq=e−\|q\|E\_\{q\}=e^\{\-\|q\|\},τ=\(Ep−Eq\)/\(1−Ep\)\\tau=\(E\_\{p\}\-E\_\{q\}\)/\(1\-E\_\{p\}\)andhp=Ep/\(1−Ep\)h\_\{p\}=E\_\{p\}/\(1\-E\_\{p\}\); \(C\) far pairs:g⁡\(\|q\|\)\+\[\|p\|​g′​\(\|p\|\)−g⁡\(\|p\|\)\]−sgn⁡\(p​q\)​\|q\|​g′​\(\|p\|\)g\(\|q\|\)\+\[\|p\|g^\{\\prime\}\(\|p\|\)\-g\(\|p\|\)\]\-\\mathrm\{sgn\}\(pq\)\\,\|q\|\\,g^\{\\prime\}\(\|p\|\), whose three terms are non\-negative for opposite signs\. Forprobs,d=log⁡\(λq/λp\)\+log⁡\(λ¯p/λ¯q\)d=\\log\(\\lambda\_\{q\}/\\lambda\_\{p\}\)\+\\log\(\\bar\{\\lambda\}\_\{p\}/\\bar\{\\lambda\}\_\{q\}\)withλ¯=1−λ\\bar\{\\lambda\}=1\-\\lambdaby TwoSum; the two logarithms have the same sign, and the factor1/\(λ⁡\(1−λ\)\)1/\(\\lambda\(1\-\\lambda\)\)of the gradient is merged into one product with one rounding\. Midpoint\-aware final rounding on thelogitspath\.

Why it works\.No clamp: logits up to\|z\|=1005\|z\|=1005and subnormalλ\\lambdaare evaluated for the distribution that was passed in\. The linear term is removed before any arithmetic; the series converges for\|η\|≤4<2​π\|\\eta\|\\leq 4<2\\pi, with the remainders of all six series asserted≤u/16\\leq u/16at import time; there is no window\. The remaining cancellation is bounded by a factor of 5–9 per region and enters the error estimate\.

Frozen data\(342,070 probs; 376,691 logits\)\. KL, probs:Official78\.37 / 89\.75%;Off\.\+6\.27 / 38\.92%;Rewrite0\.09 / 0\.04%\. KL, logits:Official86\.17 / 90\.89%;Off\.\+27\.81 / 42\.56% \(5\.07% for FP16 inputs in FP64\);Rewrite<0\.01%<0\.01\\%\. Gradients:Official78\.6–90\.8%\. For 938probsgradientsRewritereturns∞\\inftywhere the true value exceeds the format; these are not counted as errors but carry no finite error guarantee\.

### B\.18MultivariateNormal \(d=2,4d=2,4;scale\_tril\)

Official\.With dimensiondd, meansμp,μq\\mu\_\{p\},\\mu\_\{q\}, lower\-triangular Cholesky factorsLp,LqL\_\{p\},L\_\{q\}of the covariances \(scale\_tril\),M=Lq−1​LpM=L\_\{q\}^\{\-1\}L\_\{p\}andv=Lq−1​\(μp−μq\)v=L\_\{q\}^\{\-1\}\(\\mu\_\{p\}\-\\mu\_\{q\}\):KL=\[∑ilog⁡Lq,i​i−∑ilog⁡Lp,i​i\]\+12​\(‖M‖F2\+‖v‖2−d\)\\mathrm\{KL\}=\\big\[\\sum\_\{i\}\\log L\_\{q,ii\}\-\\sum\_\{i\}\\log L\_\{p,ii\}\\big\]\+\\tfrac\{1\}\{2\}\\big\(\\\|M\\\|\_\{F\}^\{2\}\+\\\|v\\\|^\{2\}\-d\\big\)\.

Error\.For close pairs‖M‖F2−d\\\|M\\\|\_\{F\}^\{2\}\-dand the log\-determinant difference are both first order and their sum is second order; the log\-determinant difference itself cancels\. The dominant cause is cancellation, not ill\-conditioning: the KL error rate is similar for condition numbers≤100\\leq 100, up to10410^\{4\}, and for large means\. 16\.4% \(d=2d=2\) and 11\.1% \(d=4d=4\) of non\-identity outputs are negative\. FP64 evaluation fixes most gradients but leaves about 36% of the KL values\.

Repair\.MMis lower triangular with positive diagonal\. WithN=M−IN=M\-Iand, for each diagonal entryn=Ni​in=N\_\{ii\},\(1\+n\)2−1−2​log⁡\(1\+n\)=n2\+2​φ​\(n\)\(1\+n\)^\{2\}\-1\-2\\log\(1\+n\)=n^\{2\}\+2\\varphi\(n\),

KL=∑iφ⁡\(Ni​i\)\+12​‖N‖F2\+12​‖v‖2,N=Lq−1​\(Lp−Lq\),\\mathrm\{KL\}=\\sum\_\{i\}\\varphi\(N\_\{ii\}\)\+\\tfrac\{1\}\{2\}\\\|N\\\|\_\{F\}^\{2\}\+\\tfrac\{1\}\{2\}\\\|v\\\|^\{2\},\\qquad N=L\_\{q\}^\{\-1\}\(L\_\{p\}\-L\_\{q\}\),∂μp=−∂μq=Lq−⁣⊤v,∂Lp=tril\(Lq−⁣⊤N\)\+diag\(Ni​i/Lp,i​i\),∂Lq=−tril\(Lq−⁣⊤\(N\+N⊤\+NN⊤\+vv⊤\)\)\.\\partial\_\{\\mu\_\{p\}\}=\-\\partial\_\{\\mu\_\{q\}\}=L\_\{q\}^\{\-\\top\}v,\\quad\\partial\_\{L\_\{p\}\}=\\mathrm\{tril\}\\big\(L\_\{q\}^\{\-\\top\}N\\big\)\+\\mathrm\{diag\}\\big\(N\_\{ii\}/L\_\{p,ii\}\\big\),\\quad\\partial\_\{L\_\{q\}\}=\-\\mathrm\{tril\}\\big\(L\_\{q\}^\{\-\\top\}\(N\+N^\{\\top\}\+NN^\{\\top\}\+vv^\{\\top\}\)\\big\)\.NNis obtained by forward substitution from the exact differenceLp−LqL\_\{p\}\-L\_\{q\}\(TwoSum\), never asM−IM\-I\. All matrix operations are element\-wise in double\-length arithmetic \(d≤4d\\leq 4; no call totorch\.linalg\)\.

Why it works\.Three non\-negative terms;NNis small because it is solved from an exact small right\-hand side; mean\-only perturbations giveN=0N=0and covariance\-only perturbations givev=0v=0exactly\. Double\-length arithmetic absorbs the condition number of the triangular solves: solving in plain FP32 would be worse than the official code on 5–15% of the low\-rank outputs\.

Frozen data\(202,000; FP32 only, because the official FP16 implementation is unsupported\)\. KL:Official88\.56 / 88\.86% \(d=2/4d=2/4\);Off\.\+35\.81 / 35\.78%;Rewrite0 \(max\. 2 ULP\)\. Gradients:Official27\.1–43\.0%;Off\.\+4\.2–7\.5%;Rewriteall correctly rounded\.Oursis correctly rounded on every sample\. Gradient components in the strict upper triangle ofscale\_trilare not scored; the official code leaks non\-zero gradient there\.

### B\.19LowRankMultivariateNormal \(d=2,4d=2,4;r=1,2r=1,2\)

Official\.With covariancesΣ=W​W⊤\+diag⁡\(D\)\\Sigma=WW^\{\\top\}\+\\mathrm\{diag\}\(D\), whereW∈ℝd×rW\\in\\mathbb\{R\}^\{d\\times r\}is the factor of rankrrandDDthe positive diagonal, the Woodbury identity and capacitance matrices giveKL=12\[logdetΣq−logdetΣp\+tr\(Σq−1Σp\)\+\(μq−μp\)⊤Σq−1\(μq−μp\)−d\]\\mathrm\{KL\}=\\tfrac\{1\}\{2\}\\big\[\\log\\det\\Sigma\_\{q\}\-\\log\\det\\Sigma\_\{p\}\+\\mathrm\{tr\}\(\\Sigma\_\{q\}^\{\-1\}\\Sigma\_\{p\}\)\+\(\\mu\_\{q\}\-\\mu\_\{p\}\)^\{\\top\}\\Sigma\_\{q\}^\{\-1\}\(\\mu\_\{q\}\-\\mu\_\{p\}\)\-d\\big\], each part computed separately\.

Error\.The same second\-order cancellation as for the full\-covariance case; in additionDDis swamped by the rounding ofW​W⊤WW^\{\\top\}when the covariance is assembled; and the parameterisation is not unique \(column permutations and sign flips ofWW, transfers betweenW​W⊤WW^\{\\top\}andDD\), soP=QP=Qcan hold with different parameters\. 19\.8–21\.3% of non\-identity outputs are negative; in FP64 about 40% of the KL values are still wrong and 0\.54% negative\.

Repair\.We return to the form of the previous subsection without forming the Cholesky factor ofΣp\\Sigma\_\{p\}\. WithLq=chol⁡\(Σq\)L\_\{q\}=\\mathrm\{chol\}\(\\Sigma\_\{q\}\),δ=μp−μq\\delta=\\mu\_\{p\}\-\\mu\_\{q\},v=Lq−1​δv=L\_\{q\}^\{\-1\}\\delta,Δ​W=Wp−Wq\\Delta W=W\_\{p\}\-W\_\{q\}andΔ​D=Dp−Dq\\Delta D=D\_\{p\}\-D\_\{q\},

E=Σp−Σq=Δ​W​Wp⊤\+Wq​Δ​W⊤\+diag⁡\(Δ​D\),X=Lq−1​E​Lq−⁣⊤,I\+X=G​G⊤,E=\\Sigma\_\{p\}\-\\Sigma\_\{q\}=\\Delta W\\,W\_\{p\}^\{\\top\}\+W\_\{q\}\\,\\Delta W^\{\\top\}\+\\mathrm\{diag\}\(\\Delta D\),\\qquad X=L\_\{q\}^\{\-1\}EL\_\{q\}^\{\-\\top\},\\qquad I\+X=GG^\{\\top\},andN=G−IN=G\-Iis computed by a*differential*Cholesky recursion,

sj=Xj​j−∑t<jGj​t2,Nj​j=sj1\+1\+sj,Gi​j=Xi​j−∑t<jGi​t​Gj​tGj​j,s\_\{j\}=X\_\{jj\}\-\\sum\_\{t<j\}G\_\{jt\}^\{2\},\\qquad N\_\{jj\}=\\frac\{s\_\{j\}\}\{1\+\\sqrt\{1\+s\_\{j\}\}\},\\qquad G\_\{ij\}=\\frac\{X\_\{ij\}\-\\sum\_\{t<j\}G\_\{it\}G\_\{jt\}\}\{G\_\{jj\}\},after whichKL=∑iφ⁡\(Ni​i\)\+12​‖N‖F2\+12​‖v‖2\\mathrm\{KL\}=\\sum\_\{i\}\\varphi\(N\_\{ii\}\)\+\\tfrac\{1\}\{2\}\\\|N\\\|\_\{F\}^\{2\}\+\\tfrac\{1\}\{2\}\\\|v\\\|^\{2\}\. Gradients:Hp=Lq−⁣⊤​X​\(I\+X\)−1​Lq−1H\_\{p\}=L\_\{q\}^\{\-\\top\}X\(I\+X\)^\{\-1\}L\_\{q\}^\{\-1\},Hq=Lq−⁣⊤​\[Lq−1​\(E\+δ​δ⊤\)​Lq−⁣⊤\]​Lq−1H\_\{q\}=L\_\{q\}^\{\-\\top\}\\big\[L\_\{q\}^\{\-1\}\(E\+\\delta\\delta^\{\\top\}\)L\_\{q\}^\{\-\\top\}\\big\]L\_\{q\}^\{\-1\}, and∂Wp=HpWp\\partial\_\{W\_\{p\}\}=H\_\{p\}W\_\{p\},∂Dp=12diag\(Hp\)\\partial\_\{D\_\{p\}\}=\\tfrac\{1\}\{2\}\\mathrm\{diag\}\(H\_\{p\}\),∂Wq=−HqWq\\partial\_\{W\_\{q\}\}=\-H\_\{q\}W\_\{q\},∂Dq=−12diag\(Hq\)\\partial\_\{D\_\{q\}\}=\-\\tfrac\{1\}\{2\}\\mathrm\{diag\}\(H\_\{q\}\),∂μp=Lq−⁣⊤v=−∂μq\\partial\_\{\\mu\_\{p\}\}=L\_\{q\}^\{\-\\top\}v=\-\\partial\_\{\\mu\_\{q\}\}\.EEis built from the exact parameter differences\. Three refinements are necessary: the double\-length square root is corrected to be correctly rounded \(the vectorised platform square root differed by 1 ULP from the correctly rounded one on 17\.2% of FP32 and 1\.3% of FP64 results\);HqH\_\{q\}addsE\+δ​δ⊤E\+\\delta\\delta^\{\\top\}*before*solving, so that an exactly vanishing sum gives an exact zero; and semantic identity is decided exactly: ifμp=μq\\mu\_\{p\}=\\mu\_\{q\}andWp​Wp⊤\+diag⁡\(Dp\)=Wq​Wq⊤\+diag⁡\(Dq\)W\_\{p\}W\_\{p\}^\{\\top\}\+\\mathrm\{diag\}\(D\_\{p\}\)=W\_\{q\}W\_\{q\}^\{\\top\}\+\\mathrm\{diag\}\(D\_\{q\}\)as rational numbers \(tested with error\-free expansions, no tolerance\), all outputs are\+0\+0\.

Why it works\.Lq​GL\_\{q\}Gis the Cholesky factor ofΣp\\Sigma\_\{p\}, so the non\-negative formula of the full\-covariance case applies withM=GM=G;EEis an exact small quantity;1\+s−1\\sqrt\{1\+s\}\-1is written without cancellation\.

Frozen data\(202,000; FP32 only\)\. KL:Official91\.1–93\.4%;Off\.\+39\.5–41\.9%;Rewrite0 \(max\. 3 ULP\)\. Gradients:Official59\.3–71\.0%;Off\.\+10\.8–24\.4%; of about 4\.24 million gradient valuesRewritemisrounds 72 and has one significant error, shared withOfficial\.Oursis correctly rounded on every sample\.

## Appendix CInterface B:torch\.nn\.functional\.kl\_div

### C\.1Official implementation and its errors

Element\-wise, the official code computesℓ=xlogy⁡\(t,t\)−t​x\\ell=\\mathrm\{xlogy\}\(t,t\)\-t\\,x, wherexlogy⁡\(t,t\)=t​log⁡t\\mathrm\{xlogy\}\(t,t\)=t\\log tand00att=0t=0\(log\_target=False\), orℓ=et​\(t−x\)\\ell=e^\{t\}\(t\-x\)\(log\_target=True\), then sums and divides by a factor*den*that depends on the reduction \(1 forsum, the batch size forbatchmean, the number of elements formean\)\. Gradients are produced by autograd\. We found six classes of behaviour; the first five are operator errors that a better implementation removes, and the first four of them also disappear in FP64\.

1. 1\.Element\-wise cancellation \(log\_target=False\)\.Whent≈ext\\approx e^\{x\},log⁡t\\log tandxxare nearly equal, and the rounding erroru​\|log⁡t\|u\|\\log t\|of the logarithm is amplified by the subtraction\. In FP32, 65–99% ofsumoutputs are off by≥8\\geq 8ULP and 35–70% by≥1%\\geq 1\\%, the more so the smaller the KL\. Withlog\_target=Truethe element\-wise error is small \(≤0\.3%\\leq 0\.3\\%at≥8\\geq 8ULP\)\.
2. 2\.Gradient with respect to the target under scaling\.Autograd writes the derivative ofxlogyin its second argument as\(g​t\)/t\(g\\,t\)/t, whereggis the upstream factor \(1/\(B​K\)1/\(BK\)formean, with batch sizeBBandKKclasses\)\. The exact value of this term isgg, but the product is formed first: wheng​tg\\,tis subnormal it loses precision and when it underflows the term vanishes\. With FP16 andK=256K=256, 99\.6% of gradient rows have relativeL2L\_\{2\}error≥1%\\geq 1\\%and 95\.4% contain a component of the wrong sign; withK=1024K=1024every row has cosine below 0\.9\. The same term makes the gradient0/0=0/0=NaN att=0t=0\. Evaluating\(g​log⁡t\+\(g​t\)/t\)\+\(−g\)​x\(g\\log t\+\(g\\,t\)/t\)\+\(\-g\)\\,xin this order reproduces the official gradient bit for bit on 39,513,600 components \(including 57,706 NaNs\)\.
3. 3\.Components rounded to zero undermean\(log\_target=True\)\.ete^\{t\}is rounded once and its product withggthen lands on a midpoint of the subnormal grid and is rounded to 0 \(20\.1% of target\-gradient rows and 14\.6% of input\-gradient rows for FP16,K=256K=256\)\.
4. 4\.Overflow before scaling\.The exactmean/batchmeanis finite but the sum overflows first \(extreme strata only\)\.
5. 5\.NaN at boundary inputs\.log\_target=Truewith a target of−∞\-\\infty\(probability zero\) returns NaN on all 20,400 such batches; so dox=−∞x=\-\\inftywitht=0t=0and the target gradient att=0t=0\. These are not removed by higher precision\.
6. 6\.Input\-representation error\.TopT\_\{\\mathrm\{op\}\}differs fromTe2eT\_\{\\mathrm\{e2e\}\}by≥1%\\geq 1\\%on 40–99% of the batches; evaluating the operator in FP64 changes the total error by less than 3 percentage points; withlog\_target=Falsetwo identical distributions give a non\-zeroTopT\_\{\\mathrm\{op\}\}\. This cannot be repaired inside the function\.

### C\.2Repair

The repair keeps the signature \(plus amethodargument\) and computes in FP64 with merged formulas and a single final rounding\.

#### Forward,log\_target=False\.

Writet=m​2kt=m\\,2^\{k\}with an integerkkandm∈\[1/2,2\)m\\in\[\\sqrt\{1/2\},\\sqrt\{2\}\)\. Thenlog⁡t−x=\(k​ln⁡2hi−x\)\+log⁡m\+k​ln⁡2lo\\log t\-x=\(k\\,\\ln 2\_\{\\mathrm\{hi\}\}\-x\)\+\\log m\+k\\,\\ln 2\_\{\\mathrm\{lo\}\}, whereln⁡2=ln⁡2hi\+ln⁡2lo\\ln 2=\\ln 2\_\{\\mathrm\{hi\}\}\+\\ln 2\_\{\\mathrm\{lo\}\}is split so thatln⁡2hi\\ln 2\_\{\\mathrm\{hi\}\}has few significant bits;k​ln⁡2hik\\,\\ln 2\_\{\\mathrm\{hi\}\}is then exact and cancels againstxxwithout error; the remaining error comes from\|log⁡m\|<0\.35\|\\log m\|<0\.35\. The four terms are accumulated with TwoSum\.

#### Forward,log\_target=True\.

ete^\{t\}andt−xt\-xare multiplied in FP64; ift−x=0t\-x=0the result is 0 regardless ofete^\{t\}\.

#### Reduction\.

Compensated pairwise summation of the unrounded element values, division by*den*, one rounding\.

#### Backward\.

Merged analytic gradients replace autograd’s term\-by\-term evaluation \(GGis the incoming gradient of the reduced loss, so thatg=G/𝑑𝑒𝑛g=G/\\mathit\{den\}above\):

The bracketlog⁡t\+1−x\\log t\+1\-xuses the forward decomposition;t−x\+1t\-x\+1is an error\-free three\-term sum, which also decides exactly whether it is zero\. Each component is resolved in a fixed order: outside the domain→\\tonon\-finiteGG→\\toG=0G=0→\\toundefined cell \(NaN\)→\\toexact zero factor→\\toformula\.

#### Certified rounding\.

When an intermediate of the fast path is not finite \(e\.g\.ete^\{t\}overflows although the result is finite\), or when the FP64 value is closer to a rounding boundary of the target format than the a\-priori error bound of the fast path, the sum is rewritten asR\+∑τcτ​f​\(τ\)R\+\\sum\_\{\\tau\}c\_\{\\tau\}f\(\\tau\), whereτ\\tauruns over the distinct target values,ffisexp\\exp\(log\_target=True\) orlog\\log\(log\_target=False\) andR,cτR,c\_\{\\tau\}are exact rationals, and evaluated by interval arithmetic on a precision ladder from 128 to 16,384 bits until both end points round to the same value\. If they still differ at 16,384 bits, the implementation raises an error and returns no value; this did not occur on the benchmark\. The guard is necessary, e\.g\. withlog\_target=Truefort=\(0,0,−1000\)t=\(0,0,\-1000\),x=\(−1,−2−24,−1001\)x=\(\-1,\-2^\{\-24\},\-1001\), whose exact value1\+2−24\+e−10001\+2^\{\-24\}\+e^\{\-1000\}sits next to a rounding midpoint that the droppede−1000e^\{\-1000\}decides\. FP64→\\toFP16 rounding goes through a round\-to\-odd FP32 value, which is equivalent to a single rounding\.

#### Why it works\.

The large termk​ln⁡2hik\\ln 2\_\{\\mathrm\{hi\}\}is exact, so the residual error is independent of\|log⁡t\|\|\\log t\|; the backward pass never forms\(g​t\)/t\(g\\,t\)/t; the unit roundoff of FP64 is2292^\{29\}\(FP32\) or2422^\{42\}\(FP16\) times smaller than that of the target, and the few results too close to a rounding boundary are certified by interval arithmetic\. The guard uses an a\-priori error bound of the fast path that assumes FP64logandexpaccurate to 1 ULP; on the benchmark, every scored component equals the certified rounding\. Zeros \(t=xt=x,t−x\+1=0t\-x\+1=0,G=0G=0\) are decided exactly\.

### C\.3Results on the frozen data

#### Subnormal gradients\.

For 233 FP16 vectors with subnormal reference gradients, componentwise correct rounding improves relativeL2L\_\{2\}error but can reduce cosine similarity\. All corresponding components are correctly rounded\.

#### Comparison methods\.

Evaluating the repaired formulas in FP32 for FP16 inputs fully repairs 7 of 10 forward cells; evaluating them in the input precision repairs 4 of 20 forward and 0 of 20 backward cells completely, because correct rounding is lost and FP16 subnormal results are rounded twice\. This is why the strict method computes in FP64\.

#### Cost and scope\.

The reference implementation is 28–48×\\timesslower on CPU \(B=64B=64,K=1024K=1024\);[Table8](https://arxiv.org/html/2611.30835#A3.T8)reports CPU and GPU costs, reaching 222×\\timesfor larger inputs\. The full benchmark was repeated on an A100 with identical correctly rounded component counts \([AppendixD](https://arxiv.org/html/2611.30835#A4)\)\.

#### What the operator repair does not do\.

In the distillation experiment \([Section5\.3](https://arxiv.org/html/2611.30835#S5.SS3)\), replacingF\.kl\_divalone gives bit\-identical training trajectories; the spurious gradient arises inlog\_softmaxbackward\. In the language\-model experiment \([Section5\.4](https://arxiv.org/html/2611.30835#S5.SS4)\), the dominant error comes from rounded probability inputs\. The logits path of[Equation4](https://arxiv.org/html/2611.30835#S4.E4)addresses both sources\.

### C\.4Cost of the reference implementations

[Tables7](https://arxiv.org/html/2611.30835#A3.T7),[8](https://arxiv.org/html/2611.30835#A3.T8)and[9](https://arxiv.org/html/2611.30835#A3.T9)give running times relative to the official call \(forward plus backward; median of 7 repetitions, each the mean of several calls\) on one A100 GPU and on four CPU threads of the same node \(PyTorch 2\.4\.0\)\. The node was shared with other jobs, so the CPU columns are less reliable than the GPU columns\. None of the repairs has been optimised\.

#### Closed forms\.

The official code and ours are both compositions of tensor operations in Python\. On the GPURewritecosts 2\.4–5\.9×\\timesthe official call for Exponential, Laplace, Poisson, Normal and Bernoulli, and the strict mode 3\.4–12×\\times\. Gamma \(7–14×\\timesand about 30×\\times\) and Beta \(54–116×\\timesand about 320×\\times\) are much slower because their repairs evaluate a quadrature\. Precision is not the cost: the official formulas in FP64 run at 1\.0–1\.1×\\timeson the GPU \(1\.6×\\timesfor Beta at the larger size\), and the overhead of the strict mode overRewritecomes from its additional rounding and conversion steps\. The cost ofRewriteis the additional arithmetic: series instead of one logarithm, all branches of a piecewise formula evaluated and then selected, double\-length sums, and hand\-written gradients\. On CPU the factors are larger \(Rewrite2\.7–22×\\timesfor the five families\)\.

#### Interface B\.

The officialF\.kl\_divis a compiled composite; the repair is 27–59×\\timesslower on the GPU and 28–222×\\timeson CPU, growing with the number of classes\. Skipping the boundary check that certifies the final rounding saves 24–31% on the GPU\. The remainder is error\-free arithmetic and the unfused evaluation of many small tensor operations\.

#### Logits path\.

It evaluates a 16\-term series in FP64 for every element\. On the GPU it costs 3–10×\\timesthe official call for FP32 logits and 5–22×\\timesfor FP16 or BF16 logits, the latter at a vocabulary of 151,936, where the official call in FP64 is itself 4–5×\\timesslower than in half precision; one call on512×151,936512\\times 151\{,\}936BF16 logits takes 72 ms against 3\.2 ms\.

Table 7:Closed forms: running time relative to the official FP32 call, for 65,536 / 1,048,576 pairs of FP32 parameters\. “FP64”: the official formula evaluated in FP64\.Table 8:F\.kl\_div: running time relative to the official call in the input precision \(batchmean\)\. “no check”: the repair with the boundary check of the final rounding skipped\.Table 9:Logits path: running time relative toF\.kl\_divonlog\_softmaxoutputs \(log\_target=True\) in the precision of the logits\. “FP64”: the same official call on logits cast to FP64\.

## Appendix DRepetition on a GPU

#### Setup\.

All measurements of[Section3](https://arxiv.org/html/2611.30835#S3)and all repairs were repeated on one NVIDIA A100 \(40 GB\) with PyTorch 2\.4\.0\+cu124, on the same frozen inputs and against the same reference values, with the scoring code of the CPU study\. Interface A: the 33 configurations and sub\-configurations \(2,707,000 scored FP16 and 4,527,454 scored FP32 samples\); the official implementation and the repairs were run on the GPU and, in the same PyTorch build, on the CPU for a bit\-wise comparison\. Interface B: the 20 configurations \(942,200 batches, 83\.9 million forward and 483\.3 million backward components\)\. The multivariate\-normal repairs were evaluated on the A100 using the same frozen inputs and references; both devices showed no significant errors\.

#### Results\.

[Table10](https://arxiv.org/html/2611.30835#A4.T10)summarises the two devices;[Figures3](https://arxiv.org/html/2611.30835#A1.F3),[4](https://arxiv.org/html/2611.30835#A1.F4),[5](https://arxiv.org/html/2611.30835#A4.F5)and[6](https://arxiv.org/html/2611.30835#A4.F6)resolve the relative error by configuration and KL interval\. The official implementations fail on the GPU as they do on the CPU\. For interface A the rates agree within 0\.6 points for all configurations with two exceptions: Gumbel with FP16 inputs \(significant\-error rate 93\.5% on the GPU, 66\.2% on the CPU; the share with relative error≥1%\\geq 1\\%in R0 is practically the same, 73\.2% and 73\.3%\) and MultivariateNormal withd=2d=2\(86\.6% against 88\.6%\)\. ForF\.kl\_divwithlog\_target=Trueand FP32 inputs, 28–50% of the forward and 32–34% of the backward components are not correctly rounded on the GPU, against 17–44% and 11–13% on the CPU\. The opposite also occurs: with FP16 probability targets and many classes thesumof the official implementation is more accurate on the GPU \(K=1024K=1024,KL∈\[10−2,10−1\)\\mathrm\{KL\}\\in\[10^\{\-2\},10^\{\-1\}\): 10% of the samples off by≥1%\\geq 1\\%against 65% on the CPU\)\.Oursgives no significant error for interface A on either device, and forF\.kl\_divthe counts of correctly rounded components are the same on both devices for each of the 20 configurations\.

#### The devices disagree bit\-wise\.

In the same PyTorch build, the KL value of the official interface\-A implementation differs between CPU and GPU for 27\.3% of the FP32 and 5\.6% of the FP16 samples\. ForF\.kl\_div, 12–13% of the element\-wise outputs and 33% \(FP16\) to 49% \(FP32\) of thesumoutputs differ between the GPU run and the CPU run; this comparison is across the two PyTorch versions\.

Table 10:Official implementation andOurson CPU \(PyTorch 2\.15\.0\) and on an A100 GPU \(PyTorch 2\.4\.0\), all KL intervals and parameter regions\. Interface A: significant\-error rate of the KL value \(%\)\. Interface B: share of components that are not correctly rounded \(%\); the backward rate of the official implementation excludes the boundary\-gradient components\.![Refer to caption](https://arxiv.org/html/2611.30835v1/fig_heat_B_fp32.png)Figure 5:Interface B, FP32 inputs, region R0, batches of one row,reduction="sum"\. Share of samples whose output has relative error≥1%\\geq 1\\%, per configuration and KL interval\. The left and middle panels show the officialF\.kl\_divon CPU and on GPU, and the right panelOurs, whose CPU and GPU results are identical \(all zero\)\.![Refer to caption](https://arxiv.org/html/2611.30835v1/fig_heat_B_fp16.png)Figure 6:Interface B, FP16 inputs, as[Figure5](https://arxiv.org/html/2611.30835#A4.F5)\. Blank cells have fewer than 100 samples\.

## Appendix EDownstream Experiments: Details

### E\.1Parameter Fitting

#### Protocol\.

Loss:kl\_divergence\(Geometric, Geometric\), failure\-count convention\. Parameterisations:probs=p=σ⁡\(θ\)=p=\\sigma\(\\theta\), with the logistic sigmoidσ\\sigma, orlogits=θ=\\theta\. Directions: trainable first argument,KL\(Geo\(p\)∥Geo\(p⋆\)\)\\mathrm\{KL\}\(\\mathrm\{Geo\}\(p\)\\\|\\mathrm\{Geo\}\(p^\{\\star\}\)\), and trainable second argument\. The target is rounded to the computation precision first, so the rounded value is the exact optimum\. Methods: official \(PyTorch 2\.15\.0\); the repaired formula in the target precision; the repaired formula in FP64 with one rounding of loss and gradient\. Optimisers: Adam, learning rate10−210^\{\-2\}, 3000 steps \(main\); Adam10−310^\{\-3\}and SGD with momentum 0\.9 at 0\.1, 10,000 steps \(robustness\)\. True KL by 60\-digit arithmetic\. CPU only\.

Table 11:Parameter fitting: 12 starts per target, shared by all settings and methods \(log\-uniform in\[p⋆/5,min⁡\(5​p⋆,0\.95\)\]\[p^\{\\star\}/5,\\min\(5p^\{\\star\},0\.95\)\]; the target is rounded once to the format of the loss\)\. Relative deviation of the fittedppfrom the target after 3000 Adam steps \(median / maximum over starts\) and the largest true KL\.
#### Further observations\.

With the trainable distribution in the second argument the official code usually works, but not always \(fourth row\)\. With a logit parameterisation, 3 of the 12 official FP16 runs forp⋆=0\.001p^\{\\star\}=0\.001abort with non\-finite gradients\. With SGD in FP16 andp⋆=0\.001p^\{\\star\}=0\.001the official loss diverges top=1p=1from all three tested starts \(true KL 6\.9\)\. In a two\-parameter version \(the sum of two Geometric KLs, targets0\.010\.01and0\.0020\.002in FP16,0\.010\.01and0\.0010\.001in FP32; same 12\-start protocol\) the final true loss over 12 starts is, median / maximum,0\.0740\.074/0\.730\.73\(official\) against7\.5×10−77\.5\\times 10^\{\-7\}/5\.2×10−65\.2\\times 10^\{\-6\}\(repaired\) in FP16, and6\.9×10−96\.9\\times 10^\{\-9\}/4\.9×10−74\.9\\times 10^\{\-7\}against4\.0×10−134\.0\\times 10^\{\-13\}/8\.0×10−128\.0\\times 10^\{\-12\}in FP32\. On a15×1515\\times 15grid around the region visited by the official FP16 trajectories, the official descent direction is within25∘25^\{\\circ\}of the true one at 54% of the points and more than90∘90^\{\\circ\}away at 5%; the official runs stall where the directions oppose\.[Figure7](https://arxiv.org/html/2611.30835#A5.F7)shows the true loss along the 12 runs\.

#### The failure is not convergence to a biased point\.

In the two\-parameter version \([Figure7](https://arxiv.org/html/2611.30835#A5.F7)\), the official runs stop improving but do not stop moving: the worst one travels a path of length 0\.45 in its last 500 steps for a net displacement of 0\.0024, about 1\.0 away from the target in logit units; after step 500, 82% of its gradients make an angle of more than90∘90^\{\\circ\}with the true gradient \(median cosine−0\.999\-0\.999\)\. Its reported loss jumps between about\+3\+3and−1\.5\-1\.5while the true loss stays at 0\.73, so the loss curve cannot be used for monitoring either; in three of the four combinations of precision and direction, about a third of the official training steps report a negative KL\.

#### Limits\.

One or two scalar parameters, no model and no data noise, one distribution family, CPU arithmetic\.

Figure 7:Parameter fitting in FP16 with two Geometric targets \(p⋆=0\.01p^\{\\star\}=0\.01and0\.0020\.002\) and Adam with learning rate10−210^\{\-2\}\. The curves show the exact KL to the target along the fit, from the same 12 starts for both losses \(thin lines\), and their median \(thick\)\. With the official loss \(red\) the runs stop improving after about 200 steps, at a median true loss of0\.0740\.074, whereas with the repaired loss \(blue\) they reach the resolution of FP16\.

### E\.2Sequential Bayesian Design

#### Setup\.

Bayesian experimental design chooses the next observation by its expected information gain, the expected KL between the posterior after the observation and the current one\([Lindley, 1956](https://arxiv.org/html/2611.30835#bib.bib62);[Chaloner and Verdinelli, 1995](https://arxiv.org/html/2611.30835#bib.bib63)\)\. We takeK=10K=10Bernoulli arms with unknown rates and Beta posteriors; an arm with posteriorBeta⁡\(a,b\)\\mathrm\{Beta\}\(a,b\)has

EIG⁡\(a,b\)=\\displaystyle\\mathrm\{EIG\}\(a,b\)=\{\}aa\+bKL\(Beta\(a\+1,b\)∥Beta\(a,b\)\)\\displaystyle\\tfrac\{a\}\{a\+b\}\\,\\mathrm\{KL\}\\bigl\(\\mathrm\{Beta\}\(a\{\+\}1,b\)\\,\\\|\\,\\mathrm\{Beta\}\(a,b\)\\bigr\)\+ba\+bKL\(Beta\(a,b\+1\)∥Beta\(a,b\)\),\\displaystyle\+\\tfrac\{b\}\{a\+b\}\\,\\mathrm\{KL\}\\bigl\(\\mathrm\{Beta\}\(a,b\{\+\}1\)\\,\\\|\\,\\mathrm\{Beta\}\(a,b\)\\bigr\),and in every round the arm with the largest EIG receives the nextmmobservations, for a total of 100,000\. The KL is computed with the official FP32kl\_divergence\(Beta, Beta\)or with our rewrite, also in FP32; the reference policy uses the exact EIG\. We run batch sizesm∈\{1,100,1000,104\}m\\in\\\{1,100,1000,10^\{4\}\\\}from a cold start \(no observations\) and from a warm start \(each arm already has10210^\{2\}to10510^\{5\}observations\), with 5 seeds and the same outcomes for all policies\.

#### Why this setting\.

The information gain of one more observation shrinks like the reciprocal of the number of observations already made, so the experiment itself drives the KL into the range where the closed form fails: forBeta⁡\(n\+1,n\)\\mathrm\{Beta\}\(n\{\+\}1,n\)againstBeta⁡\(n,n\)\\mathrm\{Beta\}\(n,n\)the true KL is2\.5×10−42\.5\\times 10^\{\-4\},2\.5×10−52\.5\\times 10^\{\-5\}and2\.5×10−62\.5\\times 10^\{\-6\}forn=103,104,105n=10^\{3\},10^\{4\},10^\{5\}, and the official FP32 code returns−1\.5×10−3\-1\.5\\times 10^\{\-3\},−5\.6×10−3\-5\.6\\times 10^\{\-3\}and\+0\.31\+0\.31\.

#### Summary of the result\.

With the official KL, 40–91% of the rounds choose a wrong arm in seven of the eight settings \([Table12](https://arxiv.org/html/2611.30835#A5.T12)\), and 35–60% of the EIG values it computes there are zero or negative\. With the rewrite no round is wrong in any of the 40 runs: its EIG is within a relative4\.4×10−74\.4\\times 10^\{\-7\}of the exact value, and its allocation of observations is that of the exact policy \(identical in 39 runs, one observation apart in the other\)\. The consequence for the experiment depends on the feedback\. When the posterior is updated after every observation \(m=1m=1\), a wrong choice costs one observation, and the total posterior variance at the end is only 1–10% higher than with the exact policy\. When a choice commits a batch, the loss can be much larger: form=1000m=1000the worst arm ends with up to 2\.5 times the variance, and form=104m=10^\{4\}from a warm start the total variance is 1\.2 to 5\.2 times and the worst arm up to 11 times that of the exact policy, because an arm with few observations is passed over in most rounds\. From a cold start withm=104m=10^\{4\}every round goes to an arm without observations, where the official value is accurate, and nothing changes\.

#### Protocol\.

Arm rates are drawn uniformly from\[0\.05,0\.95\]\[0\.05,0\.95\]; the prior isBeta⁡\(1,1\)\\mathrm\{Beta\}\(1,1\), so an arm withsssuccesses andfffailures has the posteriorBeta⁡\(1\+s,1\+f\)\\mathrm\{Beta\}\(1\+s,1\+f\)\. In the warm start armkkhasround⁡\(10yk\)\\mathrm\{round\}\(10^\{y\_\{k\}\}\)earlier observations withyky\_\{k\}drawn uniformly from\[2,5\]\[2,5\]\. The outcome of thejj\-th observation of an arm is the same for all policies, and ties are broken by the lowest index\. The policies differ only in the two KL values of the EIG:torch\.distributions\.kl\_divergence\(Beta, Beta\)in FP32 \(official\), the rewrite of[AppendixB](https://arxiv.org/html/2611.30835#A2)evaluated in FP32 \(ours\), and the identityKL\(Beta\(a\+1,b\)∥Beta\(a,b\)\)=loga\+ba\+ψ\(a\+1\)−ψ\(a\+b\+1\)\\mathrm\{KL\}\(\\mathrm\{Beta\}\(a\{\+\}1,b\)\\,\\\|\\,\\mathrm\{Beta\}\(a,b\)\)=\\log\\frac\{a\+b\}\{a\}\+\\psi\(a\+1\)\-\\psi\(a\+b\+1\), with the digamma functionψ\\psi, in FP64 \(exact\), which agrees with 60\-digit arithmetic to1\.1×10−81\.1\\times 10^\{\-8\}on posteriors with up to5×1065\\times 10^\{6\}observations\. The weightsa/\(a\+b\)a/\(a\+b\)andb/\(a\+b\)b/\(a\+b\)and the combination are computed in FP32 for the official code and for ours\. A round counts as wrong if the exact EIG of the chosen arm is below\(1−10−3\)\(1\-10^\{\-3\}\)times the largest exact EIG\. The posterior variance of an arm rate isa​b/\(\(a\+b\)2​\(a\+b\+1\)\)ab/\(\(a\+b\)^\{2\}\(a\+b\+1\)\), the expected squared error of its estimate\.

Table 12:Sequential Bayesian design, all settings; 100,000 observations,K=10K=10, ranges over 5 seeds\. “Lost”: mean over the rounds of1−EIGchosen/EIGmax1\-\\mathrm\{EIG\}\_\{\\mathrm\{chosen\}\}/\\mathrm\{EIG\}\_\{\\max\}, with exact values at the policy’s own posterior\. “Zero or negative”: share of the EIG values computed by the official code\. “Worst arm”: largest posterior variance of an arm, relative to the largest of the exact policy\. “Fewest”: smallest number of observations of an arm at the end over the seeds, official code \(exact policy\)\. The rewrite has no wrong round and variance ratios of 1\.0000 in all 40 runs; its allocation of the observations is identical to that of the exact policy in 39 runs and differs by one observation in one \(cold start,m=1m=1\)\.official: decisionsofficial: variance \(×\\timesexact\)startmmroundswrong \(%\)first wrong roundlost \(%\)zero or negative \(%\)sumworst armfewestcold1100,00091380–42220–2241–441\.01–1\.051\.08–1\.178,308 \(9,998\)1001,00084–8811–1425–3143–461\.02–1\.081\.12–1\.795,600 \(10,000\)1,00010063–7513–1528–4536–481\.06–1\.271\.20–2\.504,000 \(10,000\)10,000100–015–301\.001\.0010,000 \(10,000\)warm1100,00084–911–9720–2443–501\.01–1\.100\.98–1\.678,749 \(14,836\)1001,00084–901–522–3144–521\.03–1\.111\.09–1\.618,901 \(14,802\)1,00010083–851–436–4740–521\.06–1\.261\.15–1\.858,206 \(14,271\)10,0001040–701–331–5835–601\.17–5\.161\.12–10\.96576 \(10,291\)
#### Results\.

[Table12](https://arxiv.org/html/2611.30835#A5.T12)lists all settings\. From a cold start withm=1m=1the first wrong choice occurs after about 380 observations, that is, when each arm has about 38; from then on the share of wrong rounds grows, and the observations per arm at the end range from 8,308 to 16,176 instead of 9,998 to 10,001\. The EIG computed by the official code is off by factors of up to3×1043\\times 10^\{4\}; that of the rewrite by at most4\.4×10−74\.4\\times 10^\{\-7\}in relative terms\. Evaluating the rewrite in FP64 and rounding once \(strict mode\) gives the same choices as the FP32 rewrite in them=1m=1runs\. The outcome is sensitive to which rounds go wrong: for the warm start withm=104m=10^\{4\}the ratio of the total variance is 1\.17, 1\.19, 1\.22, 2\.55 and 5\.16 in the five seeds, and in the last one an arm that starts with 576 observations receives none of the ten batches, while the exact policy gives it 10,000\.

#### Limits\.

Simulated Bernoulli arms, one acquisition rule \(the arm with the largest one\-step EIG\),K=10K=10, five seeds\. Form=1m=1the effect on the final variance is small, and for the cold start withm=104m=10^\{4\}the official code makes no wrong choice\. Only the Beta closed form is exercised\.

### E\.3Identical\-Start Distillation

#### Protocol\.

Teacher and student: MLP32→256→V32\\to 256\\to Vwith identical weights \(seeded\); inputs: fixed random batches; loss: forward KL of the output distributions, mean over the batch; 300 steps; metrics at the last step: top\-1 disagreement with the teacher, exact KL \(FP64\), relative weight change\. Grid:V∈\{1024,32768,151936\}×\{V\\in\\\{1024,32768,151936\\\}\\times\\\{FP32, FP16, BF16\}×\{\\\}\\times\\\{Adam10−410^\{\-4\},3×10−43\{\\times\}10^\{\-4\},10−310^\{\-3\},3×10−33\{\\times\}10^\{\-3\},10−210^\{\-2\}; SGD0\.010\.01,0\.10\.1,1\}×41\\\}\\times 4losses×10\\times 10seeds=2,880=2\{,\}880runs on one A100 GPU \(PyTorch 2\.4\.0\)\. Adam uses the defaultβ=\(0\.9,0\.999\)\\beta=\(0\.9,0\.999\)andeps=10−8=10^\{\-8\}\. Weights, optimiser state and the forward pass of both networks are FP32; the logits are cast to the stated format, in which the loss and the start of the backward pass are computed, without autocast or gradient scaling\. All runs completed; none was non\-finite; 740 runs repeated in an independent execution are bit\-identical\.

#### Complete results\.

[Figure8](https://arxiv.org/html/2611.30835#A5.F8)plots[Table2](https://arxiv.org/html/2611.30835#S5.T2)\. Under Adam all 27 curves \(3 vocabularies×\\times3 precisions×\\times3 official variants\) increase strictly with the learning rate\. In the 45 Adam comparisons betweenV=1024V=1024andV=151,936V=151\{,\}936the larger vocabulary is always worse; betweenV=32,768V=32\{,\}768and151,936151\{,\}936three comparisons are reversed \(two FP32, one BF16\), within the seed range\. Under SGD the largest median is 2\.1% \(V=151,936V=151\{,\}936, FP16, learning rate 1\)\. 24\.4% of the official runs report a negative loss at step 0 \(e\.g\. median−1\.7×10−8\-1\.7\\times 10^\{\-8\}forV=151,936V=151\{,\}936, FP32\)\. The median initial gradient norm of the officialF\.kl\_divloss is44–5×10−85\\times 10^\{\-8\}in FP32,2×10−42\\times 10^\{\-4\}to1\.6×10−21\.6\\times 10^\{\-2\}in FP16 and about1\.5×10−31\.5\\times 10^\{\-3\}in BF16\.

Figure 8:Identical\-start distillation\. Top\-1 disagreement between student and teacher after 300 steps as a function of the learning rate \(median over 10 seeds\)\. The officialF\.kl\_divis shown in red, with line style and marker indicating the precision, and ours in blue \(identically zero in every cell\)\.
#### Limits\.

Synthetic MLP, identical initialisation, 300 steps\. The logits path in BF16 is exploratory \(BF16 is not covered by the certified evaluation\)\.

### E\.4KL in Language Models

#### Models\.

Qwen2\.5\-1\.5B\-Instruct, FP16 forward pass; perplexity of the original model 11\.66 on the 16,384 selection tokens\. Quantized versions and their perplexity: RTN 8\-bit per channel 11\.68; GPTQ 8\-bit 11\.67; AWQ 4\-bit 12\.74; GPTQ 4\-bit 13\.32; RTN 4\-bit \(group 128\) 13\.23; RTN 3\-bit \(group 128\) 31\.14\. Published GPTQ/AWQ weights were dequantized to FP16 and run through ordinary linear layers\. Only the KL computation changes precision\.

Table 13:Language models, Part A\. Reported mean KL \(batchmean\) and its deviation from the FP64 reference\. “inf”: the reported value is infinite\.Table 14:Language models, Part A\. Per\-token errors on 16,384 tokens for the two 8\-bit models \(GPTQ / RTN\)\. “Total” error is relative to the exact KL of the FP16 model logits; “operator” error is relative to the exact value on the inputs the call received\. For the logits path the last column is agreement with the rounded FP64 reference\.∗All of these tokens have exact KL≤2\.8×10−6\\leq 2\.8\\times 10^\{\-6\}, below the smallest normal FP16 number; they are correctly rounded\.

#### Part A, further observations\.

For the 4\- and 3\-bit models, whose KL is 0\.1–1 nat, all finite methods agree with the reference within 0\.1%\. The six models are far apart compared with these errors \(the closest pair, GPTQ and RTN at 4 bits, differs by 15%\)\. The repairedF\.kl\_divreduces the share of tokens with operator error≥1%\\geq 1\\%from 97\.6 / 95\.5% to 0\.3 / 0\.1%, while 98\.8 / 96\.8% of the tokens have input\-representation error≥1%\\geq 1\\%; the logits path has no input\-representation error\. Casting the FP16 logits to BF16 before computing the KL changes the divergence itself \(reported values off by 31–124% at 8 bits for every method, including the logits path, which is then correctly rounded for the BF16 logits\): the KL should be computed from the logits in the precision in which the model produced them\. All 15 model pairs are ordered correctly by every finite method except FP16reduction="mean", which reports both 8\-bit models as 0 \(a tie\)\. The references use a cancellation\-free formula in FP64, and the logits path reproduces the reference mean to four digits\. The officialCategoricalKL is infinite because it treats an underflowed zero inQQas a true zero\.

Table 15:Language models, Part B\. Ranking of 28 single\-layer quantizations: strict inversions among 378 pairs / reported ties, and Kendall’sτb\\tau\_\{b\}against the reference\.
#### Part B, decisions\.

At 4 bits, where single\-layer divergences are two orders of magnitude larger, inversions are rare \(1–2 pairs in FP16 withbatchmean\)\. A decision counts as harmful if the mixed model has a higher true KL on a disjoint 4,096\-token evaluation set\. The reference protects layers\{0,1,2,3,5,19,26,27\}\\\{0,1,2,3,5,19,26,27\\\}for 8 bits andk=8k=8\. Officiallog\_targetFP16, the officialF\.kl\_divforms in BF16 and the BF16 logits path select layer 25 instead of layer 5; the reference KL of layers 5, 23 and 25 is\(5\.787,5\.759,5\.474\)×10−5\(5\.787,\\,5\.759,\\,5\.474\)\\times 10^\{\-5\}, and the resulting mixed model has0\.25%0\.25\\%*lower*true KL on the disjoint evaluation set \(\+0\.87%\+0\.87\\%on the selection set\)\.CategoricalBF16 selects\{1,2,6,9,15,22,26,27\}\\\{1,2,6,9,15,22,26,27\\\}\(four wrong layers\):\+9\.78%\+9\.78\\%true KL on the evaluation set,\+15\.31%\+15\.31\\%on the selection set, perplexity difference−0\.009\-0\.009\. All rankable methods agree with the reference for 8 bits withk=4k=4and for 4 bits withk∈\{4,8\}k\\in\\\{4,8\\\}\. The reported per\-layer mean of the official FP16F\.kl\_divdeviates from the reference by−233%\-233\\%to−32%\-32\\%at 8 bits \(24 layers negative\) and by−3\.5%\-3\.5\\%to−0\.3%\-0\.3\\%at 4 bits; the logits path by±0\.02%\\pm 0\.02\\%\.

#### Limits\.

One model, one dataset, round\-to\-nearest quantizers, two values ofkk; references are FP64 \(not interval\-certified\), spot\-checked at 150 digits\. The most common official configuration, FP16F\.kl\_divwithbatchmean, still selects the correct layers, and perplexity differences are within±0\.01\\pm 0\.01throughout\.

#### Part C, protocol\.

Qwen2\.5\-1\.5B\-Instruct with the default \(sdpa\) attention implementation oftransformers; WikiText\-2 training text; nothing is trained\.*Executions\.*For each of FP32, FP16 and BF16 the same weights are run in four ways: one full forward pass with batch size 8 \(the reference execution\), batch size 1, token\-by\-token decoding with a key–value cache \(what a rollout engine does\), and theeagerattention implementation;8×256=2,0488\\times 256=2\{,\}048positions\. Each alternative is compared with the reference execution of the same precision, and the three reference executions with each other\. The official calls are evaluated in the precision of the model \(in FP32 for pairs of different precisions\), 128 rows per call,batchmean; the sampled estimators use tokens drawn from the first execution and FP32 log\-probabilities, as the RL fine\-tuning frameworks we inspected do \(k3\_klof verl, the KL estimators of OpenRLHF\)\.*Reproducibility\.*The logits of the model and of five round\-to\-nearest copies \(8, 7, 6 and 5 bits per output channel, 4 bits with group size 128\) are computed once on8×512=4,0968\\times 512=4\{,\}096positions in FP16 and in BF16\. The mean KL is then computed under settings that cannot change its value: 1, 16, 128, 512 or all rows per call; five reductions \(batchmean,sumdivided by the rows,meantimesVV,nonesummed in the same precision or in FP32\); two random row orders; CPU instead of GPU\.

Table 16:Language models, Part C: relative error \(%\) of the mean KL between two executions of the same weights\. Rows with one precision: the named execution against the full forward pass with batch size 8, KL computed in that precision; rows with two precisions: KL computed in FP32\. Last column: sampled estimatorex−x−1e^\{x\}\-x\-1/expm1⁡\(x\)−x\\operatorname\{expm1\}\(x\)\-x, withxxthe log\-probability ratio of the two executions at a sampled token, in FP32, relative to the same estimator in FP64\.Table 17:Language models, Part C: reported KL as a percentage of the truth, FP16, officialF\.kl\_divwith a probability target\. Columns 3–7:reduction="mean"for different numbers of rows per call; column 8: range over all 27 settings with another reduction; column 9: range of the logits path over its 12 settings\.
#### Part C, results\.

[Table16](https://arxiv.org/html/2611.30835#A5.T16)lists all pairs\. The gap between two executions grows with the coarseness of the precision \(about10−1010^\{\-10\},2×10−52\\times 10^\{\-5\}and10−310^\{\-3\}\), and in each precision the official KL of that precision cannot measure it; computed one precision higher, the official calls agree with the reference \(within 0\.1% in FP32 for the FP16 and BF16 pairs, within10−610^\{\-6\}in FP64 for all pairs\)\. In BF16 the most likely token differs at 1\.9% of the positions between cached decoding and the full forward pass\.[Table17](https://arxiv.org/html/2611.30835#A5.T17)shows the dependence on the batch size: withmeanthe result is of the order ofKL/V\\mathrm\{KL\}/V, which is subnormal or zero in FP16, so that the reported value depends on how many rows are averaged at once; the same underflow makes FP16meanreport zero for all layers in Part B\. With the other reductions the official value is stable over batch sizes, row orders and devices, with a bias of up to 5% for the 8\-bit copy;log\_targetbehaves like the probability target; the FP16CategoricalKL is non\-finite in all 17 settings\. With a BF16 forward pass the official values vary by at most 3\.6% and are off by at most 6\.2%\. The logits path varies by at most 0\.41% and is off by at most 0\.33% in every setting of both precisions\. The order of the five copies is correct in every setting that returns finite values\.

#### Part C, limits\.

One model, one text, a single run\. The executions are variants within one framework, not a rollout engine against a training framework\. The FP32 gap of about10−1010^\{\-10\}has no practical consequence\. The dependence on the batch size is confined to FP16 withreduction="mean"\. The FP16 forward pass witheagerattention is non\-finite at every position for this model, so that pair is omitted\.

#### Part D, protocol\.

auto\-circuit1\.0\.1\([Miller et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib60)\), unmodified, withtransformer\-lens2\.4\.0; models without weight processing; the prompts shipped with the library; first batch, last token position; factorised graph with separate query, key and value inputs\. The settings are IOI\([Wang et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib61)\)on GPT\-2 small \(batch of 64, 32,491 edges\) and on GPT\-2 medium \(batch of 64; 29,999 of 231,877 edges, sampled at random with the same fraction from each of the 97 destination modules\), Greater\-than\([Hanna et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib64)\)on GPT\-2 small \(batch of 64, 32,491 edges\), and Docstring\([Heimersheim and Janiak, 2023](https://arxiv.org/html/2611.30835#bib.bib65)\)on the 4\-layer attention\-only model of that study \(batch of 128, 1,281 edges,V=48,262V=48\{,\}262\)\. The loop is the library’s ACDC loop\([Conmy et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib59)\)\(same ablation tensors, patch mode, forward call and output slice\) with one change: every edge is restored after it has been scored, so that the scores are independent of each other\. For each edge we record, on the same output logits, the library’s score \(F\.kl\_divwithlog\_target,reduction="sum", divided by the number of rows, in the precision of the model\), the logits path, and as the reference the KL of the two logit tensors evaluated in FP64\. A score counts as wrong if it is negative or deviates from the reference by more than 10%\. BF16 was run for the first setting only\. The edge counts in[Tables5](https://arxiv.org/html/2611.30835#S5.T5)and[18](https://arxiv.org/html/2611.30835#A5.T18)are the edges with a positive reference score \(32,477, 32,477, 1,272 and 29,999 of the 32,491, 32,491, 1,281 and 29,999 edges; 32,476 for Greater\-than in FP16\); the others have a reference of exactly zero\.

Table 18:Language models, Part D, all settings\. “Wrong”: negative or more than 10% from the FP64 KL of the same logits, among the edges with a positive reference\. “Selected differently”: edges whose decisionscore≥τ\\text\{score\}\\geq\\taudiffers from the reference’s\. Spearman and top\-kkoverlap are with the reference ranking of the same model; the Docstring model has fewer than 5,000 edges\.
#### Part D, results\.

[Table18](https://arxiv.org/html/2611.30835#A5.T18)lists all settings\.*FP32\.*The errors are confined to small scores\. On GPT\-2 small with IOI the median reference score is8\.8×10−78\.8\\times 10^\{\-7\}, and the library’s score is wrong for 73% of the edges between10−810^\{\-8\}and10−710^\{\-7\}, for at least 97% below10−810^\{\-8\}, for 9% between10−710^\{\-7\}and10−610^\{\-6\}and for none above; the other GPT\-2 settings show the same profile with smaller medians \(8\.8×10−88\.8\\times 10^\{\-8\}for Greater\-than,3\.0×10−83\.0\\times 10^\{\-8\}on GPT\-2 medium\) and correspondingly more wrong scores\. With the library’s thresholdsτ=10−6,10−5,10−4,10−3\\tau=10^\{\-6\},10^\{\-5\},10^\{\-4\},10^\{\-3\}the numbers of edges selected differently are 84, 7, 0, 0 \(IOI, small\), 25, 1, 0, 0 \(Greater\-than\) and 23, 1, 0, 0 \(IOI, medium\), and the 1,000 highest\-scoring edges are the same in every setting\. The relative error of the logits path has a median of about2×10−82\\times 10^\{\-8\}and a 95th percentile of at most3\.6×10−73\.6\\times 10^\{\-7\}\. On the Docstring model the median score is1\.9×10−41\.9\\times 10^\{\-4\}and only 30 of 1,272 scores are wrong\.*FP16\.*The median relative error of the library’s score is 24% \(IOI, small\), 3% \(Greater\-than\), 12% \(Docstring\) and 28% \(IOI, medium\)\. On GPT\-2 medium the thresholdτ=10−4\\tau=10^\{\-4\}selects 17,192 edges by the reference and 9,829 edges are decided differently by the library’s score, 3 by the logits path\. On GPT\-2 small with IOI the library’s score is wrong for 341 of the 739 edges of the published circuit\([Wang et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib61)\)\. The logits path is wrong for 5 and 2 edges in two settings; these have a reference below10−710^\{\-7\}, where FP16 has at most one significant bit \(its smallest positive number is6×10−86\\times 10^\{\-8\}\), and the same value rounded to FP32 instead is wrong for none\.*BF16*\(IOI, small\)\. The library’s score is wrong for 2\.5% of the edges\.

#### Part D, limits\.

One batch and a single run per setting; GPT\-2 medium is a 13% sample of its edges\. In FP32, threshold decisions change, but the top 1,000 edges are unchanged\. That the larger model is affected more rests on one pair of models on one task\. Half\-precision scores on the GPT\-2 models are mostly noise of the forward pass \(median2\.1×10−42\.1\\times 10^\{\-4\}in FP16 against8\.8×10−78\.8\\times 10^\{\-7\}in FP32 for IOI on GPT\-2 small\): the logits path recovers the score of the half\-precision model, not that of the FP32 model, whose ranking the FP16 reference itself reproduces only with a Spearman correlation of 0\.63 \(IOI, small\), 0\.63 \(Greater\-than\) and 0\.39 \(IOI, medium\); for the Docstring model it is 0\.999\. With the library’s sequential ACDC \(three KL computations, GPT\-2 small, IOI\) the logits path did not bring the half\-precision circuits measurably closer to the FP32 circuit \(at most 3\.3% more edges in agreement\), because each decision changes all later ones, so that two accurate KL computations differ from each other by as many edges as either differs from the library’s, and because in BF16 the forward pass, not the KL, decides the circuit \(over 99\.9% of the edges are kept forτ≤10−4\\tau\\leq 10^\{\-4\}with every KL computation\)\.

## Appendix FUse of the Interfaces, User Reports, and Settings Without a Measurable Effect

This appendix collects material that bounds the practical reach of the errors from both sides: where the two interfaces are called \([SectionF\.1](https://arxiv.org/html/2611.30835#A6.SS1)\), earlier reports of the same failure by users \([SectionF\.2](https://arxiv.org/html/2611.30835#A6.SS2)\), and the settings we tested in which replacing the official KL changed nothing measurable \([SectionF\.3](https://arxiv.org/html/2611.30835#A6.SS3)\)\. The experiments of[SectionF\.3](https://arxiv.org/html/2611.30835#A6.SS3)are exploratory\.

### F\.1Call sites in PyTorch\-based libraries

We downloaded the current release of 116 PyTorch\-based packages from PyPI on 14 November 2026 and searched their Python sources, excluding tests, documentation and examples, for calls oftorch\.distributions\.kl\_divergence\(interface A\) and ofF\.kl\_divornn\.KLDivLoss\(interface B\)\. A call was counted only if the name resolves to PyTorch in that file; functions of the same name that a package defines for itself were not counted\. Interface A is referenced in 20 packages and interface B in 22; 39 packages use at least one \([Table19](https://arxiv.org/html/2611.30835#A6.T19)\)\. The packages were chosen by us from reinforcement learning, probabilistic modelling, distillation, speech, compression, interpretability, robustness and uncertainty estimation\. Whether a call is affected depends on how close its two distributions are and on the precision\. The call sites whose two arguments are close by construction are those that compare a policy before and after an update \(cleanrl,garage,pfrl,rsl\-rl\-lib,skrl,stable\-baselines3withsb3\-contrib,tianshou,torchrl\), a student with its teacher, or a model with an ablated or perturbed copy of itself \(auto\-circuit,sae\-lens,inseq,easyeditor, the adversarial\-training code ofadversarial\-robustness\-toolboxandtorchattacks\)\. The metric module ofauto\-circuitclamps its KL at zero with the comment that numerical errors can cause tiny negative values\.

Table 19:Packages \(of 116 inspected\) whose non\-test code calls interface A \(kl\_divergence\) or interface B \(F\.kl\_div,KLDivLoss\): number of source files with at least one call\. The use is our reading of the calling files\.sb3\-contrib2\.9\.2 \(TRPO\) calls interface A through the helper ofstable\-baselines3and is not counted separately\.
### F\.2Earlier user reports

The failure for nearly identical distributions has been reported repeatedly by users\. We found five reports between 2019 and 2023 by searching the PyTorch issue tracker and forum\.

1. 1\.Forum thread 48470 \(2019\): theCategoricalKL of two close distributions is negative \(−1\.65×10−8\-1\.65\\times 10^\{\-8\}\), and theprobsandlogitsparameterisations disagree\.[https://discuss\.pytorch\.org/t/48470](https://discuss.pytorch.org/t/48470)
2. 2\.Issue 32520 \(2020\):F\.kl\_divis negative, or non\-zero where it should be zero\. It was closed when thelog\_targetflag was added in version 1\.6\.[https://github\.com/pytorch/pytorch/issues/32520](https://github.com/pytorch/pytorch/issues/32520)
3. 3\.Issue 80488 \(2022\):F\.kl\_divis still negative after that change\. The answer was that this is numerical error, that removing it in the core would cost performance, and that the result can be clamped\.[https://github\.com/pytorch/pytorch/issues/80488](https://github.com/pytorch/pytorch/issues/80488)
4. 4\.Forum thread 149312 \(2022\): under automatic mixed precision theCategoricalKL is negative in more than 40% of the cases \(under 1% without\); in the example the true value is4\.1×10−64\.1\\times 10^\{\-6\}, the FP16CategoricalKL−8\.79×10−4\-8\.79\\times 10^\{\-4\}and the FP16F\.kl\_div−1\.52×10−4\-1\.52\\times 10^\{\-4\}\.[https://discuss\.pytorch\.org/t/149312](https://discuss.pytorch.org/t/149312)
5. 5\.Forum thread 166383 \(2023\):F\.kl\_divis negative for nearly equal outputs; the answer was that this is expected floating\-point error and that the result can be clamped\.[https://discuss\.pytorch\.org/t/166383](https://discuss.pytorch.org/t/166383)

Clamping removes negative outputs but cannot recover the true divergence or its gradient\. It also leaves inaccurate positive values unchanged\. Reports of a different kind \(overflow for extreme parameters, masked classes, logic errors\) are not listed\.

### F\.3Settings in which we measured no effect

The experiments of[Section5](https://arxiv.org/html/2611.30835#S5)show where the errors change an outcome\. In none of the settings below did the choice of KL implementation change the outcome beyond the run\-to\-run spread \([Table20](https://arxiv.org/html/2611.30835#A6.T20)\)\.

Table 20:Settings without a measurable effect of the KL implementation\. “Official” is the KL as the cited library computes it; “ours” is the logits path\. LLM runs use Qwen2\.5\-1\.5B\-Instruct with a BF16 forward pass on WikiText\-2; “eval KL” is the exact \(FP64\) KL between teacher and student on held\-out text\.settingwhat was runexpected mechanismwhat we measuredLLM distillation, nearby startTRL GKD loss; student = 8\-bit or 4\-bit copy; 320 steps, 10 seedsthe BF16 loss mis\-states small KLs by tens of percent and has a spurious gradient at identityfinal eval KL: ours vs\. official\+1\.4%\+1\.4\\%and−0\.3%\-0\.3\\%\(seed spread 2\.8%, 1\.9%\)LoRA, nearby start or KL anchorrank 16, two learning rates, 3 seeds; quantized starts; task loss \+ KL to the base modelsame loss in a low\-dimensional updatedifferences below the seed spread, no consistent signLoRA, strong constraintKL only from a perturbed adapter; task loss \+λ\\lambda\\,KL,λ=10,100,1000\\lambda=10,100,1000; 63 runsthe KL term dominates the objective, and KL=0=0is reachablemedian final eval KL 0\.018–0\.041 with every path; ours does not return to the start eitherPPO adaptive learning ratersl\-rl\-lib5\.5\.2, Beta policy, real training,2×8,0002\\times 8\{,\}000decisionsthe Beta closed form loses accuracy for large concentrations; a simulation predicted changed decisions0 decisions differ from FP64PPO KL thresholdssynthetic categorical policies, 6–50,000 actions, FP32the KL of consecutive policies is small0 of 960 decisions differ; error≤2\.5×10−5\\leq 2\.5\\times 10^\{\-5\}TRPO Fisher\-vector productdouble backward of the official FP32 KL at the old policyevaluated exactly at identical distributionsrelative error≤3\.3×10−7\\leq 3\.3\\times 10^\{\-7\}Sampled KL estimators in RL fine\-tuningk1=−xk\_\{1\}=\-x,k3=ex−x−1k\_\{3\}=e^\{x\}\-x\-1\(xx: log\-probability ratio at a sampled token\) and full KL on FP32 log\-probabilities of an LLMk3k\_\{3\}cancels for smallxxbatch mean within7\.8×10−67\.8\\times 10^\{\-6\}\(relative\) for KL10−310^\{\-3\}–1\.4×10−21\.4\\times 10^\{\-2\}Entropy calibrationthreshold search on 4 synthetic histograms, FP32, 24 trialsargmin over many small KLssame threshold as FP64 in 24 of 24Sequential ACDCauto\-circuit1\.0\.1, GPT\-2 small, 3 precisions, 4 thresholdsthresholds of10−610^\{\-6\}–10−310^\{\-3\}on KL differences≤3\.3%\\leq 3\.3\\%more edges in agreement with FP32#### Distillation of an LLM from a nearby start\.

We copied the loss of the generalised knowledge distillation trainer of TRL\([von Werra et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib58)\)\(log\_softmaxfollowed byF\.kl\_divwithlog\_target\) and trained a Qwen2\.5\-1\.5B student towards the original model for 320 steps \(AdamW withβ=\(0\.9,0\.999\)\\beta=\(0\.9,0\.999\),eps=10−8=10^\{\-8\}and no weight decay, learning rate2×10−52\\times 10^\{\-5\}, 10 seeds that fix the data order\), with BF16 logits as a DeepSpeed launch would produce them, with logits cast to FP32, and with the logits path\. From an*identical*start the result of[Section5\.3](https://arxiv.org/html/2611.30835#S5.SS3)carries over to the LLM: with either official variant the student ends at an eval KL of about 0\.10 \(0\.097–0\.103 over the 10 seeds\) with 13% top\-1 disagreement, while with the logits path no weight changes in any seed\. From the two nearby starts \(round\-to\-nearest copies at 8 and 4 bits, eval KL 0\.025 and 0\.22\) the three losses are indistinguishable: the median paired difference of the final eval KL between the logits path and the BF16 loss is\+1\.4%\+1\.4\\%\(95% bootstrap interval−1\.7%\-1\.7\\%to\+3\.0%\+3\.0\\%; 4 wins in 10\) and−0\.3%\-0\.3\\%\(−1\.4%\-1\.4\\%to\+1\.4%\+1\.4\\%; 6 wins in 10\), against a median seed\-to\-seed difference of 2\.8% and 1\.9% for a fixed loss\. The loss reported by the BF16 variant is off by 0\.11% in the median, and its parameter gradient has a cosine of 0\.998 with the exact one\. The 8\-bit student also ends*further*from the teacher than it started \(0\.10 against 0\.025\) with every loss\.

#### LoRA\.

With rank\-16 adapters\([Hu et al\., 2022](https://arxiv.org/html/2611.30835#bib.bib39)\)at learning rates10−410^\{\-4\}and3×10−43\\times 10^\{\-4\}\(3 seeds\) the picture is the same: from an identical start the official losses move the student to a median eval KL of 0\.025–0\.037 and the logits path leaves the adapter exactly at zero, while from the two quantized starts and for a task loss with a KL anchor to the base model the paths differ by less than the seeds do, without a consistent sign\. Two stronger constraints were also tested: a KL\-only objective from a slightly perturbed adapter \(start KL1\.5×10−31\.5\\times 10^\{\-3\}to1\.2×10−21\.2\\times 10^\{\-2\}, so that a KL of zero is reachable\), and a task loss with the KL weighted by 10, 100 and 1000\. In all 21 cells \(7 tasks, 3 KL paths\) the median final eval KL lies between 0\.018 and 0\.041 \(single runs: 0\.006 to 0\.045\), above the start, whatever the KL path; in particular the logits path does not return to the start either\.

#### Policy optimisation\.

Libraries for PPO and TRPO compare the policy before and after an update with interface A, in FP32\.*\(a\)*For the rule ofrsl\-rl\-libthat raises or lowers the learning rate when the KL leaves\[d/2,2​d\]\[d/2,2d\]withd=0\.01d=0\.01, a simulation with the Beta policy of that library predicted changed decisions for large concentration parameters and a KL far below the target \(up to 90 of 200 decisions\)\. In real training runs with the unmodified library \(a reaching task with 12 actions, two configurations, 8,000 decisions each\) no decision differs from the one taken with an FP64 KL\.*\(b\)*For synthetic categorical policies with 6 to 50,000 actions and a true KL between2×10−32\\times 10^\{\-3\}and5×10−25\\times 10^\{\-2\}, the official FP32 KL is within a relative2\.5×10−52\.5\\times 10^\{\-5\}of the truth and changes none of 960 threshold decisions\.*\(c\)*The Fisher\-vector product that TRPO obtains by differentiating the official KL twice at the old policy agrees with the exact Fisher matrix to3\.3×10−73\.3\\times 10^\{\-7\}\(categorical\) and7\.9×10−87\.9\\times 10^\{\-8\}\(diagonal normal\)\.*\(d\)*On LLM logits, the sampled estimators that RL fine\-tuning frameworks log and penalise\([Schulman, 2020](https://arxiv.org/html/2611.30835#bib.bib35);[Shao et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib36)\), evaluated on FP32 log\-probabilities as these frameworks do, have a batch mean within a relative7\.8×10−67\.8\\times 10^\{\-6\}of the exact value for a true KL of10−310^\{\-3\}to1\.4×10−21\.4\\times 10^\{\-2\}\.

#### Entropy calibration\.

Post\-training quantization tools choose a clipping threshold by minimising a KL between histograms\. On four synthetic activation distributions the threshold chosen with the official FP32 KL equals the one chosen in FP64 in all 24 trials \(minimum KL10−410^\{\-4\}to2×10−32\\times 10^\{\-3\}\)\.

#### Sequential circuit discovery\.

See the limits of Part D in[AppendixE](https://arxiv.org/html/2611.30835#A5)\.

#### Why the intuition fails\.

Three mechanisms explain these outcomes, and they also describe where an effect should*not*be expected\.*First*, the spurious part of the gradient is absolute, not relative: once student and teacher differ, the true gradient is orders of magnitude larger and the two cannot be told apart in the result\. The identical start is the only training setting we found in which the spurious part is all there is\.*Second*, two BF16 executions that are not bitwise identical are apart by a KL of about10−310^\{\-3\}or more \([Table4](https://arxiv.org/html/2611.30835#S5.T4)\), and trained students remain at an eval KL of 0\.02–0\.1 with every loss\. The error region of the official KL lies below that floor\.*Third*, algorithms that control a KL in a closed loop hold it near their target \(about10−210^\{\-2\}for PPO\-type rules\), again above the region in which FP32 closed forms fail; the failures of the official code start where the KL is several orders of magnitude below the working point\. The sequential design of[Section5\.2](https://arxiv.org/html/2611.30835#S5.SS2)is a closed loop of the opposite kind: there the information gain falls as the data accumulate, so the loop itself moves the KL into the failing range\. The errors therefore matter where the KL*value*is the product \(evaluation of near\-lossless changes, monitoring, rankings and thresholds on small divergences;[Sections5\.2](https://arxiv.org/html/2611.30835#S5.SS2)and[5\.4](https://arxiv.org/html/2611.30835#S5.SS4)\) or where the distributions are identical or nearly so by construction \([Sections5\.1](https://arxiv.org/html/2611.30835#S5.SS1)and[5\.3](https://arxiv.org/html/2611.30835#S5.SS3)\), and we found no case in which they change the result of ordinary training from a start that is already some distance away\.

## Appendix GExtended Related Work

#### Softmax, cross\-entropy and soft targets\.

The work closest to ours is that of[Barron et al\. \(2026\)](https://arxiv.org/html/2611.30835#bib.bib47), who identify two bugs in standardsoftmax/cross\-entropy code:*Softmax Collapse*, first diagnosed by[Prieto et al\. \(2025\)](https://arxiv.org/html/2611.30835#bib.bib46)in the context of grokking, and*Softmax Cancellation*, a non\-zero gradient for identical student and teacher logits; they give fused implementations in JAX\([Bradbury et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib25)\)and show that the fix stops distillation drift\. Our distillation experiment \([Section5\.3](https://arxiv.org/html/2611.30835#S5.SS3)\) observes the same drift independently and our logits path is closely related to their soft\-target loss\. The two papers are otherwise complementary: they treat the categorical soft\-target loss and its training dynamics; we measure and repair the KL interfaces themselves, across 18 families, with certified ground truth, separate the operator error ofF\.kl\_divfrom the input\-representation error, and study evaluation and decision making as well as training\. More broadly, a line of work traces an occasional training failure to one floating\-point mechanism: absorption in thesoftmax\([Prieto et al\., 2025](https://arxiv.org/html/2611.30835#bib.bib46)\), the representation limits of hyperbolic embeddings\([Mishne et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib66)\), and instabilities of low\-precision training of large models\([Wortsman et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib67)\)\. We follow the same approach for the KL divergence, with the difference that the failure we study is mostly silent\.[Blanchard et al\. \(2021\)](https://arxiv.org/html/2611.30835#bib.bib13)analyse the rounding error of the log\-sum\-exp and softmax*function values*; our results concern second\-order quantities built from two such evaluations, which those bounds do not cover\.

#### Numerical analysis\.

Our tools are standard: the rounding\-error model and running error bounds of[Higham \(2002\)](https://arxiv.org/html/2611.30835#bib.bib3), Sterbenz’s lemma\([Sterbenz, 1974](https://arxiv.org/html/2611.30835#bib.bib5)\), error\-free transformations and double\-length arithmetic\([Knuth, 1997](https://arxiv.org/html/2611.30835#bib.bib7);[Dekker, 1971](https://arxiv.org/html/2611.30835#bib.bib6);[Shewchuk, 1997](https://arxiv.org/html/2611.30835#bib.bib8);[Ogita et al\., 2005](https://arxiv.org/html/2611.30835#bib.bib9);[Muller et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib4)\), compensated summation\([Kahan, 1965](https://arxiv.org/html/2611.30835#bib.bib10)\), and Ziv’s strategy for correct rounding\([Ziv, 1991](https://arxiv.org/html/2611.30835#bib.bib11)\), also used by correctly rounded mathematical libraries\([Sibidanov et al\., 2022](https://arxiv.org/html/2611.30835#bib.bib12)\); ground truth relies on ball arithmetic\([Johansson, 2017](https://arxiv.org/html/2611.30835#bib.bib15)\)\. Herbie\([Panchekha et al\., 2015](https://arxiv.org/html/2611.30835#bib.bib14)\)searches for accurate rewrites automatically; the rewrites needed here go beyond local algebraic rewriting and must also deliver gradients\.

#### Numerical defects in deep\-learning software\.

Empirical studies catalogue numerical bugs in TensorFlow and PyTorch programs\([Zhang et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib50);[Humbatova et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib51);[Wang et al\., 2022](https://arxiv.org/html/2611.30835#bib.bib52)\)and in the libraries themselves\([Kloberdanz et al\., 2022](https://arxiv.org/html/2611.30835#bib.bib53)\), and tools detect NaN/Inf\-producing operations statically or by gradient\-guided search\([Zhang et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib55);[Yan et al\., 2021](https://arxiv.org/html/2611.30835#bib.bib54)\)\. These works focus on exceptional values\. Most of the errors we measure are silent: finite, plausible, and wrong\. Other probabilistic programming systems have similar KL registries\([Dillon et al\., 2017](https://arxiv.org/html/2611.30835#bib.bib22);[Bingham et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib23);[Carpenter et al\., 2017](https://arxiv.org/html/2611.30835#bib.bib24)\)\.

#### Low\-precision training and inference\.

Mixed\-precision training\([Micikevicius et al\., 2018](https://arxiv.org/html/2611.30835#bib.bib18)\)and BF16\([Kalamkar et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib19);[Zamirai et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib20)\)are now defaults for large models\.[Qi et al\. \(2025\)](https://arxiv.org/html/2611.30835#bib.bib49)trace the training–inference mismatch in reinforcement\-learning fine\-tuning of language models to BF16 rounding and recommend FP16;[Yuan et al\. \(2025\)](https://arxiv.org/html/2611.30835#bib.bib48)show that limited precision makes model outputs depend on batch size and hardware\. Part C of[Section5\.4](https://arxiv.org/html/2611.30835#S5.SS4)measures such gaps exactly\.

#### Where the KL is used\.

The KL between close distributions is the object of interest in trust\-region and proximal policy optimisation\([Schulman et al\., 2015](https://arxiv.org/html/2611.30835#bib.bib33);[Schulman et al\., 2017](https://arxiv.org/html/2611.30835#bib.bib34)\), where sample\-based estimators are also common\([Schulman, 2020](https://arxiv.org/html/2611.30835#bib.bib35);[Shao et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib36)\); in variational autoencoders and their variants\([Kingma and Welling, 2014](https://arxiv.org/html/2611.30835#bib.bib30);[Higgins et al\., 2017](https://arxiv.org/html/2611.30835#bib.bib31);[Loaiza\-Ganem and Cunningham, 2019](https://arxiv.org/html/2611.30835#bib.bib32)\); in knowledge distillation\([Hinton et al\., 2015](https://arxiv.org/html/2611.30835#bib.bib26);[Kim et al\., 2021](https://arxiv.org/html/2611.30835#bib.bib27)\), including distillation of language models\([Agarwal et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib28);[Gu et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib29)\); in circuit discovery\([Conmy et al\., 2023](https://arxiv.org/html/2611.30835#bib.bib59)\); and in model compression, both as a recovery loss for quantized models\([Liu et al\., 2024](https://arxiv.org/html/2611.30835#bib.bib43);[Xin et al\., 2026](https://arxiv.org/html/2611.30835#bib.bib44)\)and as a sensitivity metric for mixed\-precision assignment\([Dong et al\., 2019](https://arxiv.org/html/2611.30835#bib.bib41);[Cai et al\., 2020](https://arxiv.org/html/2611.30835#bib.bib42);[Nagel et al\., 2021](https://arxiv.org/html/2611.30835#bib.bib40)\)\.

## Appendix HAdditional Details for[Sections1](https://arxiv.org/html/2611.30835#S1),[3](https://arxiv.org/html/2611.30835#S3)and[4](https://arxiv.org/html/2611.30835#S4)

This section gives the full versions of passages that are stated briefly in[Sections1](https://arxiv.org/html/2611.30835#S1),[3](https://arxiv.org/html/2611.30835#S3)and[4](https://arxiv.org/html/2611.30835#S4)\.

#### Data\.

Interface A covers 18 distribution\-specific same\-family KL implementations\. We exclude five generic or delegated registrations: ExponentialFamily, HalfNormal, Independent, OneHotCategorical, and TransformedDistribution\. The 23 configurations include both parameterisations where applicable, categorical dimensionsK∈\{2,4,16\}K\\in\\\{2,4,16\\\}, and multivariate dimensionsd∈\{2,4\}d\\in\\\{2,4\\\}with ranksr∈\{1,2\}r\\in\\\{1,2\\\}\. For each configuration we drawPPfrom an ordinary parameter region R0 and from stress regions \(very large or small parameters, probabilities near 0 or 1\), and constructQQso that the*true*KL\(P∥Q\)\\mathrm\{KL\}\(P\\\|Q\)falls in a prescribed decade: one control bin\[10−1,1\)\[10^\{\-1\},1\)and 14 bins down to\[10−15,10−14\)\[10^\{\-15\},10^\{\-14\}\)\. Two further groups contain pairs whose parameters differ by 1–8 ULP, and identical pairsP=QP=Q\. Parameters are stored in the target format first, so that the truth refers to the bit patterns the library actually receives\. Cells that the floating\-point grid cannot populate \(mostly FP16 pairs in the smallest KL bins, where roundingQQmakes it identical toPP\) stay empty and are reported as such\. This gives 7,542,469 samples \(3\.0M FP16, 4\.5M FP32\)\. For interface B we generate 942,200 batches overlog\_target∈\{\\,\\in\\\{False, True\}\\\}, FP16/FP32,K∈\{2,4,16,256,1024\}K\\in\\\{2,4,16,256,1024\\\}, batch sizes 1 and 8, and all four reductions, from logits stratified in the same way\.

#### Ground truth\.

For every sample, the exact KL and every gradient component are enclosed with Arb ball arithmetic\([Johansson, 2017](https://arxiv.org/html/2611.30835#bib.bib15)\)tightly enough that the enclosure determines a unique correctly rounded value in the*target*format; samples that cannot be certified are excluded and counted\. Truth and output are always compared in the same format\. For interface B, an independent cross\-check with mpmath\([The mpmath development team, 2023](https://arxiv.org/html/2611.30835#bib.bib16)\)on 26 million values found no disagreement\.

#### Methods\.

Officialis PyTorch 2\.15\.0 on CPU; every measurement and every repair was repeated on an NVIDIA A100 GPU with PyTorch 2\.4\.0 \([AppendixD](https://arxiv.org/html/2611.30835#A4)\)\.Official\+evaluates the unchanged official code after promoting the parameters exactly to the next format \(FP32 for FP16 inputs, FP64 for FP32 inputs\) and rounds the result once to the target format; it is the natural “just use more precision” baseline\.

#### Metrics\.

We report the fraction of samples with relative error≥1%\\geq 1\\%and with negative KL, the relativeL2L\_\{2\}error and cosine of the gradient vector, and one summary rate: an output is a*significant error*if it is off by at least 8 ULP or is qualitatively wrong \(negative, NaN or infinite KL where the truth is finite and positive; spurious zero or non\-zero; sign\-flipped gradient component\)\.

#### Extreme parameters\.

In stress regions even FP64 does not help where the constructor clamps probabilities before the closed form is evaluated \(ContinuousBernoulli\) or wherelog1pof a rounded probability returns−∞\-\\infty\(Binomial, Geometric with logits\)\.

#### Additional interface\-B results\.

For probability targets \(log\_target=False\), 65–99% of the FP32sumoutputs are off by at least 8 ULP and 35–70% by at least 1%, increasing as KL decreases; withlog\_target=Truethe forward value is accurate\. Scaled reductions produce substantial target\-gradient errors in FP16: withreduction="mean", FP16 andK=256K=256, 99\.6% of gradient rows have relativeL2L\_\{2\}error≥1%\\geq 1\\%and 95\.4% contain a component of the wrong sign, because autograd evaluates the exact upstream factorggas\(g​t\)/t\(g\\,t\)/tandg​tg\\,tis subnormal\. Overall the official implementation is correctly rounded on 52\.5% of forward and 74\.9% of backward components\. Unlike interface A, these*operator*errors essentially disappear in FP64 \(NaNs at boundary inputs remain\)\. What does not disappear is the input\-representation error:TopT\_\{\\mathrm\{op\}\}differs fromTe2eT\_\{\\mathrm\{e2e\}\}by≥1%\\geq 1\\%on 40–99% of the batches, in any precision of the operator \([AppendixC](https://arxiv.org/html/2611.30835#A3)\)\.

#### Additional GPU results\.

On the A100 the significant\-error rate ofOfficialon the interface\-A data is 87\.7% for FP32 and 71\.5% for FP16 inputs \(CPU: 87\.7% and 70\.1%\), and the per\-interval error maps of the two devices agree within one percentage point in 583 of 616 cells \([Figures3](https://arxiv.org/html/2611.30835#A1.F3)and[4](https://arxiv.org/html/2611.30835#A1.F4)\)\. ForF\.kl\_divonly 50\.0% of the forward and 71\.1% of the non\-boundary backward components are correctly rounded on the GPU \(CPU: 52\.5% and 76\.4% of the same components\); withlog\_target=Trueand FP32 inputs the share of incorrectly rounded gradient components rises from 11–13% on CPU to 32–34%\. The two devices do not return the same bits: with the same PyTorch build, 27% of the FP32 KL values ofOfficialdiffer between CPU and GPU\.Oursis unaffected: no significant error for interface A and the same counts of correctly rounded components forF\.kl\_divon both devices\. The detailed GPU results are in[AppendixD](https://arxiv.org/html/2611.30835#A4): the summary by interface and precision \([Table10](https://arxiv.org/html/2611.30835#A4.T10)\), the per\-interval maps for both interfaces, the bit\-wise comparison of the two devices, and the cases in which the GPU is the more accurate one\.

#### Error\-free transformations\.

Where a difference of products is needed \(e\.g\.Nk=yk​Sx−xk​SyN\_\{k\}=y\_\{k\}S\_\{x\}\-x\_\{k\}S\_\{y\}for Categorical parameters \([SectionB\.16](https://arxiv.org/html/2611.30835#A2.SS16)\),σq2−σp2−δ2\\sigma\_\{q\}^\{2\}\-\\sigma\_\{p\}^\{2\}\-\\delta^\{2\}in the Normal scale gradient,Lq−1​\(Lp−Lq\)L\_\{q\}^\{\-1\}\(L\_\{p\}\-L\_\{q\}\)for the multivariate normal\), we use error\-free transformations: TwoSum\([Knuth, 1997](https://arxiv.org/html/2611.30835#bib.bib7)\), Dekker’s TwoProduct\([Dekker, 1971](https://arxiv.org/html/2611.30835#bib.bib6)\), expansion sums\([Shewchuk, 1997](https://arxiv.org/html/2611.30835#bib.bib8)\)and double\-length arithmetic\([Ogita et al\., 2005](https://arxiv.org/html/2611.30835#bib.bib9)\)\.

#### Autodiff errors\.

Besides inheriting the cancellation of the formula it differentiates, automatic differentiation adds failure modes of its own, e\.g\.\(g​t\)/t\(g\\,t\)/tinF\.kl\_div, or∞−∞\\infty\-\\inftythroughtorch\.where\.

#### Double rounding\.

FP64→\\toFP32→\\toFP16 is a double rounding; results whose leading term is itself a rounding midpoint are handled as described in[SectionB\.1](https://arxiv.org/html/2611.30835#A2.SS1)\.

#### Results ofRewrite\.

The residue ofRewriteis largest for Beta and Dirichlet; its few new errors are documented in[AppendixB](https://arxiv.org/html/2611.30835#A2)\. Its cost is moderate for five of the seven timed families and higher for Gamma and Beta \([SectionC\.4](https://arxiv.org/html/2611.30835#A3.SS4)\)\.

#### The logits path\.

[Equation4](https://arxiv.org/html/2611.30835#S4.E4)is a row\-vectorised re\-implementation of the certifiedCategorical\(logits\)repair of[Table1](https://arxiv.org/html/2611.30835#S4.T1), tested against it\. It is closely related to the fused soft\-target cross\-entropy proposed concurrently by[Barron et al\. \(2026\)](https://arxiv.org/html/2611.30835#bib.bib47); we arrive at it from the Categorical closed form and also repair theprobsparameterisation\.

Similar Articles

KL Zero: KL divergence intuition game

Hacker News Top

KL Zero is an interactive browser game where players draw a probability distribution to match a target KL divergence value, helping users intuitively understand the concept of KL divergence in machine learning.