← All research notes
NOTE 13 · SYSTEMS FOR ML & NUMERICAL STABILITY · VERSION 4.0 Marcio Diaz · Space AI · 2026-11-25 05:42

Silent Cancellation and Backward Gradient Corruption in Low-Precision KL Divergence: Theory, Fused Triton Kernels, and LLM Post-Training

Triggered automatically by CoreRank Discovery (CoreRank Method 1 Bayesian Surprise (#3 on 4B, +7.65)) and executed end-to-end on our NVIDIA GeForce RTX 5090 workstation GPU in 127.14 seconds.

AUTONOMOUS GPU INVESTIGATION SUMMARY

Standard PyTorch F.kl_div computes KL divergence via cancellation-prone log-space subtraction, causing negative outputs in FP16/BF16/FP32 and backward gradient dead-zones/sign-flips under clamp(min=0) patches. We prove formal floating-point truncation and cancellation bounds for a two-branch 6th-order Bregman-Taylor identity with cancellation-free analytic gradients, benchmark our fused Blackwell Triton kernel against FP32-upcast, torch.compile (TorchInductor), Liger-style standard fused Triton, and chunked-FP32 up to V=151,936, and validate 3-seed BF16 SFT+KL and 200-step real RLHF policy optimization on Qwen3-1.7B (V=151,936) and SmolLM2-1.7B (V=49,152).

6.53x
Faster vs FP32 @ V=152k
1.81x
Faster vs torch.compile
-754 MB
Qwen3-1.7B E2E VRAM Saved
0.0%
Neg & Dead KL Tokens

1. What the GPU experiment actually found

  • Forward & backward gradient corruption across 36 regimes (Wilcoxon p=1.45e-11): PyTorch F.kl_div returns negative values on up to 50.4% of inputs and corrupts BF16 reverse-KL gradients (eps=1e-2) with 39.1% sign flips (cos=0.589). Clamping F.kl_div.clamp(min=0) induces 50.4% silent zero-gradient dead-zones and 61.8% sign flips (cos=0.371).
  • Even in FP32 (eps=1e-4), F.kl_div and FP32-upcast yield 48.1% negative outputs and median relative error 21.14 (2,114%); our 6th-order Bregman-Taylor branch cuts median error by 149,111x to 1.42e-04 (0.0% negative, 0.0% error >=1%).
  • Blackwell RTX 5090 kernel scaling vs. torch.compile & chunking at Qwen3 V=151,936 (N=512, BF16 fwd+bwd): Fused Triton runs in 0.90 ms (296.8 MB extra VRAM; Reverse KL 0.90 ms): 6.53x faster (-80.0% VRAM) vs. FP32-upcast, 1.81x faster (-33.3% VRAM) vs. torch.compile Bregman-Taylor (1.63 ms), and 25.6x faster vs. Chunked-FP32 (22.99 ms).
  • Real end-to-end 1.7B LLM LoRA post-training (3 seeds each): On Qwen3-1.7B (V=151,936), Fused Triton is fastest overall at 25.1 ms/step (1.25x faster end-to-end, -754 MB VRAM vs. FP32-upcast 31.3 ms/step) with 0.0% negative/dead tokens vs. 13.8% negative (BF16 F.kl_div) and 18.3% dead gradients (Clamped). On SmolLM2-1.7B (V=49,152), Fused Triton is both fastest (20.4 ms/step) and lowest-VRAM (4616 MB vs. 4862 MB FP32), eliminating 30.2% negative-KL tokens and 34.1% dead-gradient tokens.
Figure 1. Version 3.0 audit on NVIDIA GeForce RTX 5090: (a) Forward KL relative error vs. FP64 oracle; (b) Reverse-KL backward gradient fidelity; (c) 3-seed RLHF policy optimization trajectory (V=32k); (d-e) Kernel latency and peak extra VRAM vs. FP32-upcast, torch.compile (Inductor), and Chunked-FP32 up to V=151,936; (f) Real end-to-end 1.7B LLM LoRA post-training peak VRAM and step speedup on Qwen3-1.7B and SmolLM2-1.7B.
Figure 1. Version 3.0 audit on NVIDIA GeForce RTX 5090: (a) Forward KL relative error vs. FP64 oracle; (b) Reverse-KL backward gradient fidelity; (c) 3-seed RLHF policy optimization trajectory (V=32k); (d-e) Kernel latency and peak extra VRAM vs. FP32-upcast, torch.compile (Inductor), and Chunked-FP32 up to V=151,936; (f) Real end-to-end 1.7B LLM LoRA post-training peak VRAM and step speedup on Qwen3-1.7B and SmolLM2-1.7B.

2. Complete empirical table

Table 1. Forward and backward gradient fidelity vs. certified FP64 oracle (V = 4,096, N = 2,048). Across all 36 regimes, Fused Triton Bregman-Taylor achieves 0.0% negative KL (Wilcoxon p = 1.45e-11 vs. F.kl_div) and zero dead gradients.
RegimeMethodNeg % (↓)Med RelErr (↓)Grad Cos (↑)Dead Grad % (↓)Sign Flip % (↓)
Forward KL · FP32 (ε=1e-4)PyTorch F.kl_div48.1%2.11e+011.00000.0%0.0%
Forward KL · FP32 (ε=1e-4)Clamped F.kl_div0.0%1.00e+000.519048.1%48.1%
Forward KL · FP32 (ε=1e-4)Schulman k30.0%1.41e+001.00000.0%0.0%
Forward KL · FP32 (ε=1e-4)FP32-Upcast F.kl_div48.1%2.11e+011.00000.0%0.0%
Forward KL · FP32 (ε=1e-4)Triton Bregman-Taylor (Ours)0.0%1.42e-041.00000.0%0.0%
Reverse KL · FP16 (ε=3e-3)PyTorch F.kl_div46.1%2.08e+010.85810.0%33.2%
Reverse KL · FP16 (ε=3e-3)Clamped F.kl_div0.0%1.77e+000.462646.1%63.9%
Reverse KL · FP16 (ε=3e-3)Schulman k30.0%1.00e+000.86400.0%42.5%
Reverse KL · FP16 (ε=3e-3)FP32-Upcast F.kl_div0.0%7.24e-020.91570.0%3.0%
Reverse KL · FP16 (ε=3e-3)Triton Bregman-Taylor (Ours)0.0%6.82e-020.91570.0%3.0%
Reverse KL · BF16 (ε=1e-3)PyTorch F.kl_div46.6%2.55e+020.13690.0%48.9%
Reverse KL · BF16 (ε=1e-3)Clamped F.kl_div0.0%2.97e+010.071046.6%72.7%
Reverse KL · BF16 (ε=1e-3)Schulman k30.0%1.00e+000.17170.0%48.7%
Reverse KL · BF16 (ε=1e-3)FP32-Upcast F.kl_div0.0%7.56e+000.22890.0%32.8%
Reverse KL · BF16 (ε=1e-3)Triton Bregman-Taylor (Ours)0.0%7.48e+000.22890.0%32.8%
Reverse KL · BF16 (ε=1e-2)PyTorch F.kl_div37.2%9.41e+000.58940.0%39.1%
Reverse KL · BF16 (ε=1e-2)Clamped F.kl_div0.0%3.49e+000.370637.2%61.8%
Reverse KL · BF16 (ε=1e-2)Schulman k30.0%1.00e+000.60370.0%38.6%
Reverse KL · BF16 (ε=1e-2)FP32-Upcast F.kl_div0.0%3.66e-010.70670.0%7.4%
Reverse KL · BF16 (ε=1e-2)Triton Bregman-Taylor (Ours)0.0%3.66e-010.70670.0%7.4%
Table 2. NVIDIA GeForce RTX 5090 BF16 forward+backward latency (CUDA events, 20 timed runs) and peak extra VRAM vs. FP32-upcast, PyTorch 2.11 torch.compile (TorchInductor), and row-chunked FP32 (chunk=64).
Vocabulary (Batch)FP32-Upcasttorch.compile BregmanChunked-FP32Triton Fused (Fwd/Rev)vs FP32vs compileVRAM Saved
V=256 (Toy) (N=2048)0.13ms / 10MB0.16ms / 1MB12.96ms / 2MB0.15ms (0.16) / 2MB0.87x1.05x-80.0%
V=32k (Llama-2) (N=1024)2.10ms / 625MB0.56ms / 188MB7.71ms / 182MB0.36ms (0.36) / 125MB5.79x1.56x-80.0%
V=128k (Llama-3) (N=512)4.93ms / 1252MB1.38ms / 376MB17.86ms / 603MB0.77ms (0.79) / 250MB6.43x1.80x-80.0%
V=152k (Qwen3) (N=512)5.88ms / 1484MB1.63ms / 445MB22.99ms / 714MB0.90ms (0.90) / 297MB6.53x1.81x-80.0%
Table 3. Downstream 3-seed BF16 RLHF/GRPO policy optimization (V = 32,000, β = 2.0, 120 steps) evaluated against the certified FP64 objective J*(q) = E[r] − β DKL(q || pref). Clamping zeros out KL gradients on 99.4% of steps, letting the policy drift unconstrained (5.7× higher true KL) so true J*(q) turns negative.
KL EstimatorFinal True J*(q) (↑)Final True KLMed KL RelErr (↓)Neg-KL StepsDead-KL Steps
PyTorch F.kl_div+0.00134 ± 0.000240.000810.59310.3%0.0%
Clamped F.kl_div-0.00162 ± 0.000210.003940.5890.0%99.4%
Schulman k3+0.00132 ± 0.000240.000820.7670.0%0.0%
FP32-Upcast F.kl_div+0.00164 ± 0.000240.000690.0000.0%0.0%
Triton Bregman-Taylor (Ours)+0.00164 ± 0.000240.000690.0000.0%0.0%
Table 4. Real end-to-end 3-seed BF16 KL-regularized LoRA post-training (pre-cached frozen lower-layer prefix; full BF16 forward, backward, and AdamW update through the trainable top 4 transformer blocks + rank-16 LoRA + full LM head + KL loss, 512 real tokens/step, 24 steps, β = 2.0) on Qwen3-1.7B (V = 151,936) and SmolLM2-1.7B (V = 49,152).
Model (Vocab)KL EstimatorStep (ms)Peak VRAM (MB)Neg Tok %Dead Tok %Med KL ErrVal Obj (FP64)
Qwen3-1.7B (V=151,936)PyTorch F.kl_div27.0 ± 0.0556913.8%0.0%0.06963.6065 ± 0.0016
Qwen3-1.7B (V=151,936)Clamped F.kl_div27.0 ± 0.056680.0%18.3%0.42033.6063 ± 0.0022
Qwen3-1.7B (V=151,936)Schulman k328.8 ± 0.0624912.1%0.0%0.06723.6064 ± 0.0007
Qwen3-1.7B (V=151,936)FP32-Upcast F.kl_div31.3 ± 0.068540.3%0.0%0.00003.6060 ± 0.0016
Qwen3-1.7B (V=151,936)Triton Bregman-Taylor (Ours)25.1 ± 0.061000.0%0.0%0.00003.6056 ± 0.0010
SmolLM2-1.7B (V=49,152)PyTorch F.kl_div19.4 ± 0.1503230.2%0.0%0.24563.4402 ± 0.0008
SmolLM2-1.7B (V=49,152)Clamped F.kl_div19.4 ± 0.149480.0%34.1%0.98983.4402 ± 0.0007
SmolLM2-1.7B (V=49,152)Schulman k319.7 ± 0.047186.9%0.0%0.20093.4391 ± 0.0008
SmolLM2-1.7B (V=49,152)FP32-Upcast F.kl_div20.5 ± 0.048620.0%0.0%0.00003.4402 ± 0.0011
SmolLM2-1.7B (V=49,152)Triton Bregman-Taylor (Ours)20.4 ± 1.446160.0%0.0%0.00003.4394 ± 0.0004

3. Source papers connected by CoreRank

4. Honest limitations & next steps

  • Irreducible BF16 pre-quantization floor (Theorem 2): sub-ULP logit differences rounded prior to KL evaluation bound all post-quantization kernels equally; fusing directly with FP32 LM-head output eliminates this floor.
  • Targets full-vocabulary categorical KL (V up to 151,936); sampled-token Monte Carlo GRPO benefits primarily from scalar Eq. (1) stabilization.
  • Real 1.7B LLM runs validate 3-seed end-to-end LoRA post-training over 24 steps; multi-thousand-step RLVR reasoning runs at 7B-70B scale remain future work.
Full Technical Preprint & Raw GPU Metrics

Read the generated technical manuscript (with BibTeX, exact run configuration, and raw JSON telemetry) or explore the source papers in CoreRank Discovery.