You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect gradients when a kernel uses the value returned by wp.atomic_add() #2157
When a kernel uses the value returned by wp.atomic_add() in a differentiable computation, tape.backward() produces a NaN gradient. The forward result is correct, and Warp reports no error or warning.
Running on the CPU device prints the same output. wp.atomic_sub() also gives a NaN gradient in the same setup, and a kernel that uses the returned value as an array index (slot = wp.atomic_add(count, 0, 1) followed by out[slot] = ...) produces incorrect input gradients.
The differentiability guide covers a related case in its custom replay function example. That example uses the index returned by wp.atomic_add(), shows the incorrect gradient that results, and explains it by saying the index returned by the adjoint of wp.atomic_add() "is always zero". The repro above does not match that explanation: a previous of zero would give an x.grad of [0.], not [nan]. Neither the guide's Limitations and Workarounds section nor the wp.atomic_add() reference page lists this pattern as unsupported.
Expected: x.grad is [10.]. In the forward pass previous is 10.0 and does not depend on x, so the derivative of y with respect to x is 10.0. If Warp cannot differentiate through this pattern, it should raise an error instead of returning NaN.
Related: #1858 covers documenting atomic behavior, including how a consumed return value affects backward replay.
System Information
Warp 1.20.0.dev0 (current main)
NVIDIA GeForce RTX 3090, CUDA Toolkit 13.4, Driver 13.4
Hi @shi-eric ! I'd like to contribute to this issue. I can start by reproducing the reported NaN gradient, investigating the generated backward replay for wp.atomic_add(), and adding a regression test. If the issue is still available, would it be okay for me to work on it? Thank you!
Activity
navenavelimargam commentedon Oct 25, 2026
Hi @shi-eric ! I'd like to contribute to this issue. I can start by reproducing the reported
NaNgradient, investigating the generated backward replay forwp.atomic_add(), and adding a regression test. If the issue is still available, would it be okay for me to work on it? Thank you!