Float16 jet atan2 returns zero for representable gradients
Bug Description
Both wp.JetSpace and wp.JetSpace2 return zero gradients for float16 atan2 at (256,256) and (2**-14,2**-14). The unit-scale control returns the expected gradient.
For independent variables x=y=v, the gradient of atan2(y,x) with respect to (x,y) is [-1/(2*v), 1/(2*v)]. These expected gradients are representable in float16:
v |
Observed, both jet spaces | Expected |
|---|---|---|
1 |
[-0.5, 0.5] |
[-0.5, 0.5] |
256 |
[0, 0] |
[-0.001953125, 0.001953125] |
2**-14 |
[0, 0] |
[-8192, 8192] |
import warp as wp
J1 = wp.JetSpace(2, dtype=wp.float16)
J2 = wp.JetSpace2(2, dtype=wp.float16)
@wp.kernel(enable_backward=False)
def evaluate(
values: wp.array[wp.float16],
first: wp.array[wp.vec2h],
second: wp.array[wp.vec2h],
):
i = wp.tid()
x1 = J1.variable(values[i], 0)
y1 = J1.variable(values[i], 1)
first[i] = wp.atan2(y1, x1).grad
x2 = J2.variable(values[i], 0)
y2 = J2.variable(values[i], 1)
second[i] = wp.atan2(y2, x2).grad
values = wp.array([1.0, 256.0, 2**-14], dtype=wp.float16)
first = wp.zeros(3, dtype=wp.vec2h)
second = wp.zeros_like(first)
wp.launch(evaluate, dim=3, inputs=[values], outputs=[first, second])
print("JetSpace:", first.numpy())
print("JetSpace2:", second.numpy())
Observed:
JetSpace: [[-0.5 0.5]
[-0. 0. ]
[ 0. 0. ]]
JetSpace2: [[-0.5 0.5]
[ 0. 0. ]
[ 0. 0. ]]
Application / Workflow Impact
Found through isolated testing while verifying #2244. No downstream application failure has been demonstrated.
Warp Version
Source-built Warp 1.20.0.dev0 at f9fb080f39486f317ad44900d899f43d7233e2dc.
System Information
Python 3.12.3, Ubuntu 24.04.1 LTS, Linux x86_64. Reproduced on CPU and CUDA. CUDA Toolkit 12.9, NVIDIA driver 595.58.03, NVIDIA RTX PRO 6000 Blackwell Server Edition, MIG 2g.48gb (sm_120). Native libraries rebuilt from the tested revision.
I reproduced the gradient failure on CPU and RTX 3060 and found a second-order case within the same operation. For atan2(s, s * (1 + t)) at t = 0 and positive s, the derivative is -0.5 and the second derivative is +0.5. With s = 128, both jet spaces return the correct first derivative but JetSpace2 already returns zero for the second derivative. At s = 256, I also see NaNs on this path.
A focused patch keeps the half-precision atan2 partials and their chain-rule products in float32 until they are written into the jet. A regression with two directions also checks the mixed and nonzero-input-Hessian terms. It fails on 703efa7e on both devices and passes with the patch. The two jet test modules pass 91 tests in total. Would you welcome a PR for #2331?
Codex assisted with preparing the patch and validation.
This is addressed by bd896d3280a711241a1b7906a38e954c97549570.
Float16 atan2 jets now form the squared denominator, partial derivatives, and first- and second-order chain-rule contributions in float32 before storing the float16 derivatives. Both jet spaces return the reported gradients at large and small coordinates. Leaving this open for reporter verification.
Public API behavior
wp.JetSpace and wp.JetSpace2 construction is unchanged. Inside kernels and Warp functions, their float16 wp.atan2 overloads retain float16 value, gradient, and Hessian storage while using wider derivative intermediates.
Test coverage
warp/tests/test_jet_ops.py::TestJetNativeConventions.test_float16_atan_keeps_representable_derivativeschecks both jet orders' atan2 gradients outside the float16 squared-input range and Hessians where the expected entries fit in float16.warp/tests/test_jet_ops.py::TestJetNativeConventions.test_float16_cancellations_stay_finitechecks cancellation of large float16 atan2 derivative contributions and float32 cancellation controls.
Behavioral verification
Independent checks passed on CPU and CUDA. At v = 256 and v = 2**-14, both jet spaces returned [-0.001953125, 0.001953125] and [-8192, 8192], respectively. The discussion's atan2(s, s * (1 + t)) example returned first derivative -0.5 and second derivative +0.5 at s = 128 and 256. Two-direction compositions with mixed partials and nonzero input Hessians also matched the reference.
Drafted with the warp-closing-issue skill.
Confirmed on CPU after building bd896d3280a711241a1b7906a38e954c97549570 from source. Both jet spaces return the expected gradients at 1, 256, and 2^-14. For atan2(s, s*(1+t)) at t=0, JetSpace2 also returns the expected first and second derivatives (-0.5, +0.5) at both s=128 and s=256. Thanks for the fix.
Codex assisted with the verification.