float16 atan2 loses gradients after coordinate scaling
Bug Description
wp.atan2 can silently return zero float16 gradients for an ordinary finite polar-angle objective. Scaling both coordinates should preserve the angle and its gradient with respect to the original coordinates. With p=(1,1) and scale=256, the forward angle is correct but p.grad becomes [0, 0] instead of [-0.5, 0.5] on both CPU and CUDA.
I found this while validating a differentiable polar-angle objective in local optimization checks. The corresponding half-precision SGD check stalls after this units change even though the expected parameter update is representable.
Save this as a Python file and run it with Warp:
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import warp as wp
wp.init()
@wp.kernel
def polar_angle(p: wp.array[wp.vec2h], scale: wp.float16, angle: wp.array[wp.float16]):
xy = p[0]
angle[0] = wp.atan2(scale * xy[1], scale * xy[0])
for device in wp.get_devices():
for scale in (1, 256):
p = wp.array([[1.0, 1.0]], dtype=wp.vec2h, device=device, requires_grad=True)
angle = wp.zeros(1, dtype=wp.float16, device=device, requires_grad=True)
with wp.Tape() as tape:
wp.launch(polar_angle, dim=1, inputs=[p, wp.float16(scale)], outputs=[angle], device=device)
tape.backward(loss=angle)
print(f"{device} scale={scale}: angle={float(angle.numpy()[0])}, gradient={p.grad.numpy().tolist()}")
Observed on both cpu and cuda:0:
scale=1: angle=0.78515625, gradient=[[-0.5, 0.5]]
scale=256: angle=0.78515625, gradient=[[0.0, 0.0]]
Expected: both scales produce [[-0.5, 0.5]]. Analytically, for positive s, the gradient of atan2(s*y, s*x) with respect to (x,y) is (-y,x)/(x*x+y*y).
The half-precision adj_atan2 squares its inputs in half precision, so the denominator overflows for (256,256) before division. Very small half inputs also expose underflow. I have a focused fix that evaluates this adjoint's half intermediates in float32 and converts each contribution back before the existing gradient accumulation. It leaves the other scalar adjoints and non-half arithmetic paths unchanged. The reproducer above then produces the expected gradients on both devices.
System Information
- Warp source:
mainat446fd5d0d1a5a66e557e3e7404d16aff2761073a, version1.20.0.dev0. - Python 3.12.13, Linux x86-64.
- CUDA toolkit 12.4; NVIDIA driver 580.159.03; GeForce RTX 3060 (
sm_86). - Genuine source-built core libraries from
38c8a522ec56de254ef51e78600727001ee62dab, whose production and build inputs are byte-identical to this main revision. Only the unrelated APIC test changed. CPU/CUDA kernels for this reproducer were freshly compiled against the current source headers with normal version/ABI checks.
I directed this work and take responsibility for the contribution and its follow-up. AI assistance was used in producing the change.
This is addressed by f9fb080f39486f317ad44900d899f43d7233e2dc.
The float16 atan2 adjoint now evaluates squared magnitudes and seeded derivative contributions in float32 before converting each contribution back to float16 for accumulation. This prevents intermediate overflow or underflow from erasing representable gradients in the reported polar-angle objective. This issue is already closed as completed.
Public API behavior
The fix applies to automatic differentiation of scalar float16 wp.atan2(y, x) calls inside Warp kernels and functions.
Test coverage
warp/tests/test_math.py::test_atan2_gradchecks forward angles and gradients for float16, float32, and float64, covering all quadrants, squared magnitudes outside the float16 range, non-unit gradient seeds, and accumulation into zero and nonzero gradients. The CPU and CUDA variants both passed.
Behavioral verification
A targeted reproduction on CPU and CUDA returned [-0.5, 0.5] for p=(1, 1) at scales 1, 128, 256, 512, and 2**-14. A half-precision gradient-descent step also produced the expected parameter update at every scale. These checks passed with floating-point verification enabled as well.
Drafted with the warp-closing-issue skill.