warp icon indicating copy to clipboard operation
warp copied to clipboard

Float16 wp.atan returns zero for a representable gradient

Open shi-eric opened this issue 3 weeks ago • 3 comments

Bug Description

Differentiating wp.atan(x) at float16 input x=256 returns zero. The expected derivative is 1 / (1 + 256**2), which rounds to the representable float16 value 0.0000152587890625.

import warp as wp


@wp.kernel
def evaluate(x: wp.array[wp.float16], out: wp.array[wp.float16]):
    out[0] = wp.atan(x[0])


x = wp.array([256.0], dtype=wp.float16, requires_grad=True)
out = wp.zeros_like(x, requires_grad=True)
with wp.Tape() as tape:
    wp.launch(evaluate, dim=1, inputs=[x], outputs=[out])
tape.backward(loss=out)
print(x.grad.numpy())

Observed:

[0.]

Expected: [0.0000152587890625].

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.

shi-eric avatar Nov 14 '26 18:11 shi-eric

Thanks for checking atan while verifying #2245. I reproduced the zero gradient on an RTX 3060 and checked a case that also exercises where the cast happens. At x = 65504 with an incoming gradient of 65504, the current adjoint returns zero, but the seeded contribution should round to 1.52587890625e-5. With a unit seed at the same input, zero is correct.

I have a focused patch that extends the existing atan2 float32 intermediate policy to atan. The added regression fails on 703efa7e and passes with the patch on CPU and CUDA. All 33 TestMath tests pass on that configuration. Would you welcome a PR for #2330?

Codex assisted with preparing the patch and validation.

resonance-null avatar Nov 15 '26 18:11 resonance-null

This is addressed by bd896d3280a711241a1b7906a38e954c97549570.

The float16 atan adjoint now forms the squared-input denominator and applies the incoming gradient in float32 before rounding the contribution back to float16. At x = 256, the reported unit-seed gradient is now 0.0000152587890625. Leaving this open for reporter verification.

Public API behavior

The existing wp.atan kernel builtin keeps its signature and float16 result and gradient storage. The change preserves representable derivatives when the squared input exceeds the float16 range.

Test coverage

  • warp/tests/test_math.py::test_atan_grad covers seeded reverse-mode gradients for positive, negative, large, and small inputs across float16, float32, and float64.
  • warp/tests/test_jet_ops.py::TestJetNativeConventions.test_float16_atan_keeps_representable_derivatives also covers float16 atan first- and second-order jet derivatives.

Behavioral verification

A targeted reproduction returned the expected gradient on CPU and CUDA. The discussion's boundary case at x = 65504 with incoming gradient 65504 also returned 0.0000152587890625; a unit seed at that input correctly rounded to zero.

Drafted with the warp-closing-issue skill.

shi-eric avatar Nov 19 '26 09:11 shi-eric

Confirmed on CPU after building bd896d3280a711241a1b7906a38e954c97549570 from source. At x=256 with a unit seed and at x=65504 with a seed of 65504, the float16 gradient is 1.52587890625e-5. The unit-seed control at 65504 still rounds to zero. This resolves the cases I reported. Thanks for the fix.

Codex assisted with the verification.

resonance-null avatar Nov 21 '26 08:11 resonance-null