Developer Tools

PyTorch fixes CUDA signbit for float64 negative zero bug

Negative zero was incorrectly handled in GPU kernels, breaking precision.

Deep Dive

PyTorch has merged a crucial fix for CUDA code generation in its Inductor compiler backend, addressing a subtle bug with the signbit function for float64 negative zero. The issue (#187935) was that Inductor’s Triton codegen used the comparison `x < 0` to implement signbit for all dtypes except float32. While this works for most numbers, it fails for float64 negative zero because `-0.0 < 0` evaluates to false in IEEE 754 arithmetic, causing the sign bit to be lost. This meant that compiled GPU kernels returned incorrect results for signbit on negative zero, diverging from PyTorch’s eager execution mode.

The fix, authored via Codex and approved by GitHub user jansel, extends the existing `libdevice.signbit` path to also cover `tl.float64`. This ensures that the correct sign bit is preserved for float64 negative zero, while other dtypes continue to use the previous fallback. The change is minimal but important for numerical correctness in scientific computing and machine learning workloads where sign of zero matters (e.g., gradient computations, special functions). All local tests, including a dedicated test for negative zero, pass successfully.

Key Points
  • The bug affected float64 negative zero in CUDA signbit due to using `x < 0` comparison.
  • Fix extends `libdevice.signbit` path to `tl.float64`, preserving sign bit correctly.
  • PR #187941 approved by PyTorch core contributor jansel; all tests pass locally.

Why It Matters

Ensures numerical consistency between eager and compiled modes for critical float64 operations.

📬 Get the top 10 AI stories daily