Repository navigation
Description
Bug Description
tape.backward() returns zero gradients through wp.frac() and through wp.mod() on vectors. Warp reports no error or warning. Away from the discontinuities, frac(x) = x - trunc(x) has derivative 1, and each component of the vector mod(a, b) has derivative 1 with respect to a, the same as scalar wp.mod().
import warp as wp
@wp.kernel
def frac_kernel(x: wp.array[float], y: wp.array[float]):
i = wp.tid()
y[i] = wp.frac(x[i])
@wp.kernel
def vec_mod_kernel(a: wp.array[wp.vec3], b: wp.array[wp.vec3], y: wp.array[wp.vec3]):
i = wp.tid()
y[i] = wp.mod(a[i], b[i])
x = wp.array([0.25, 1.75, -2.5], dtype=float, requires_grad=True)
y = wp.zeros(3, dtype=float, requires_grad=True)
with wp.Tape() as tape:
wp.launch(frac_kernel, dim=3, inputs=[x], outputs=[y])
tape.backward(grads={y: wp.ones(3, dtype=float)})
print("frac x.grad:", x.grad.numpy())
a = wp.array([[5.25, -5.25, 0.75]], dtype=wp.vec3, requires_grad=True)
b = wp.array([[2.0, 2.0, 2.0]], dtype=wp.vec3, requires_grad=True)
y = wp.zeros(1, dtype=wp.vec3, requires_grad=True)
with wp.Tape() as tape:
wp.launch(vec_mod_kernel, dim=1, inputs=[a, b], outputs=[y])
tape.backward(grads={y: wp.ones(1, dtype=wp.vec3)})
print("vec3 mod a.grad:", a.grad.numpy())Actual output:
frac x.grad: [0. 0. 0.]
vec3 mod a.grad: [[0. 0. 0.]]
Expected output:
frac x.grad: [1. 1. 1.]
vec3 mod a.grad: [[1. 1. 1.]]
Both built-ins are registered with is_differentiable=False in warp/_src/builtins.py, so codegen emits no reverse call for them. Their native adjoints are also MISSINGADJOINT stubs: adj_frac() in warp/native/builtin.h and the three vector adj_mod() overloads in warp/native/vec.h. The vector stub's comment proposes floor(a[i] / b[i]) for the divisor term, but wp.mod() uses truncated division (fmod), so that term would be trunc(a[i] / b[i]).
The existing tests do not catch this. test_float_to_int in test_arithmetic.py asserts zero gradients for its first five outputs only and skips frac, and test_modulo in test_vec_scalar_ops.py builds its kernel with enable_backward=False.
The gradient of scalar wp.mod() with respect to the divisor is a separate bug, tracked in #2223 with a fix in #2225. This issue covers wp.frac() and the vector overloads of wp.mod().
I have a fix with regression tests and will open a pull request. It marks both built-ins as differentiable, implements adj_frac(), and makes the vector adj_mod() call the scalar adj_mod() for each component, so vector and scalar wp.mod() always share one gradient rule. The vector divisor gradient then becomes correct as soon as #2225 lands.
Application / Workflow Impact
I use Warp through Newton for differentiable robot simulation in my PhD research. I found this bug in isolated testing, while checking Warp's built-in adjoints against finite differences with wp.autograd.jacobian() and jacobian_fd(). The affected patterns are common in robotics code. For example, Newton's UR10 example (newton/examples/robot/example_robot_ur10.py) interpolates a joint trajectory with wp.frac(t), so optimizing the timing through that interpolation gets a zero gradient. Wrapping positions into a periodic box with wp.mod() on a vec3 loses the gradient with respect to the position in the same way.
Warp Version
1.20.0.dev20261109 (also present on main at 4165779)
System Information
Python 3.12.2, macOS 27.2 on an Apple M3, CPU device. I have not run this on CUDA. The adjoints live in shared header code, so I expect the same result there.
Activity
Fix frac and vector mod gradients (NVIDIAGH-2280)