wp.log backward can overflow before applying a representable seed
Bug Description
The reverse-mode adjoint for wp.log forms the reciprocal of a positive input before multiplying it by the incoming gradient. The reciprocal can overflow even when the quotient incoming / input is exactly representable. An incoming zero then produces NaN and overwrites a finite accumulated gradient.
import warp as wp
@wp.kernel
def evaluate(x: wp.array[wp.float16], out: wp.array[wp.float16]):
i = wp.tid()
out[i] = wp.log(x[i])
device = "cpu" # also reproduced on cuda:0
small = 2.0**-16
x = wp.array([small, small], dtype=wp.float16, device=device, requires_grad=True)
out = wp.empty_like(x, requires_grad=True)
with wp.Tape() as tape:
wp.launch(evaluate, dim=len(x), inputs=[x], outputs=[out], device=device)
x.grad.fill_(0.25)
seeds = wp.array([small, 0.0], dtype=wp.float16, device=device)
tape.backward(grads={out: seeds})
print(x.grad.numpy())
Observed [inf, nan]. The exact contributions are [1, 0], so the accumulated gradient should be [1.25, 0.25]. The same check reproduces for float32 at 2**-128 and float64 at 2**-1024, on CPU and CUDA.
Application / Workflow Impact
Found while checking small positive inputs through Warp's Tape. A weighted logarithm can contribute nonfinite input gradients even when the selected seed makes its derivative finite.
Warp Version
703efa7e2a489f7e97906e760b937064eff6150c, Warp 1.20.0.dev20261115, CUDA 12.9 CI runtime with JIT compilation of the headers from that revision.
System Information
Ubuntu 25.10, x86-64, AMD Ryzen 5 5500 and NVIDIA GeForce RTX 3060 (sm_86).
Codex assisted with preparing the reproduction and numerical checks.