Published November 24, 2026 | Version 4.0

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

Authors/Creators

  • 1. Space AI

Contributors

Research group:

  • 1. https://spaceresearch.ai

Description

Abstract (v4.0): 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).

Empirical Findings (NVIDIA GeForce RTX 5090):

  • 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.

Web preprint & reproducible artifacts: https://spaceresearch.ai/papers/pytorch-kl-bregman-stability.html

Notes

Version 4.0 | Author contact: marcio.diaz@gmail.com | Institution: Space AI (https://spaceresearch.ai) | Discovery provenance: CoreRank Method 1 Bayesian Surprise (#3 on 4B, +7.65)

Files

pytorch-kl-bregman-stability.json

Files (1.5 MB)

Name Size
md5:998bea7fbc54065719407cc36f1a4f53
153.2 kB Preview Download
md5:405ff73cc63d4bfc351e0afede0b7ed4
12.2 kB Preview Download
md5:a9201d4b67b6807a9858b6accc04c638
1.3 MB Preview Download

Additional details

References

  • Schulman, J. (2020). Approximating KL Divergence. Technical Blog Note (k1/k2/k3 estimators).
  • Shao, Z., Wang, P., Zhu, Q., Xu, R., Song, J., Zhang, M., Li, Y. K., Wu, Y., & Guo, D. (2024). DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models (GRPO). arXiv:2402.03300.
  • Tillet, P., Kung, H. T., & Cox, D. (2019). Triton: An Intermediate Language and Compiler for Tiled Neural Network Computations. MAPL 2019.
  • Hsu, P.-L., Dai, Y., Kothapalli, V., Song, Q., Tang, S., Tang, S., et al. (2024). Liger Kernel: Efficient Triton Kernels for LLM Training. arXiv:2410.10989.
  • Dao, T., Fu, D. Y., Ermon, S., Rudra, A., & Re, C. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. NeurIPS 2022.
  • Higham, N. J. (2002). Accuracy and Stability of Numerical Algorithms (2nd ed.). SIAM.
  • Ouyang, L., Wu, J., Jiang, X., Almeida, D., Wainwright, C., Mishkin, P., et al. (2022). Training Language Models to Follow Instructions with Human Feedback. NeurIPS 2022.
  • Ansel, J., Yang, E., He, H., Gimelshein, N., Jain, A., Voznesensky, M., et al. (2024). PyTorch 2: Faster Machine Learning Through Dynamic Python Bytecode Transformation and Graph Compilation. ASPLOS 2024.
  • Yang, Z., Tang, W., Chen, C., Qian, R., & Chen, Y. (2026). The KL Divergence Is Never Negative, Except in PyTorch. arXiv:2611.30835.