warp icon indicating copy to clipboard operation
warp copied to clipboard

smoothstep interval powers corrupt representable gradients

Open resonance-null opened this issue 1 month ago • 0 comments

smoothstep can return a finite value but non-finite gradients for an ordinary float16 interpolation interval. I found this while checking Warp's built-in derivative across interval scales; I do not have a downstream application failure to report.

At ea7b74810b6427ad93bfe940ff22b44c603e4318, with edge0=0, edge1=float16(0.01) and x=float16(0.005), the forward value is 0.5. A direct native CUDA call to adj_smoothstep on an RTX 3060 gives edge gradients (-inf, -inf) and an x gradient of 148.125. The real-valued derivatives of the actual quantized inputs are approximately (-74.98398, -74.98398, 149.96796), all representable in float16.

A Python Tape reproducer is:

import warp as wp

@wp.kernel
def evaluate(a: wp.array[wp.float16], b: wp.array[wp.float16],
             x: wp.array[wp.float16], y: wp.array[wp.float16]):
    i = wp.tid()
    y[i] = wp.smoothstep(a[i], b[i], x[i])

wp.init()
device = "cuda:0"  # Also reproduced on CPU.
a = wp.array([0.0], dtype=wp.float16, device=device, requires_grad=True)
b = wp.array([0.01], dtype=wp.float16, device=device, requires_grad=True)
x = wp.array([0.005], dtype=wp.float16, device=device, requires_grad=True)
y = wp.zeros(1, dtype=wp.float16, device=device, requires_grad=True)
with wp.Tape() as tape:
    wp.launch(evaluate, dim=1, inputs=[a, b, x], outputs=[y], device=device)
tape.backward(grads={y: wp.ones(1, dtype=wp.float16, device=device)})
print(y.numpy(), a.grad.numpy(), b.grad.numpy(), x.grad.numpy())

adj_smoothstep divides expressions containing interval powers by the interval cubed/fourth power. Those intermediates underflow or overflow before the final derivative does. Wide float16 intervals can similarly produce zero or NaN gradients. The same scale dependence affects float32/64 at their corresponding ranges.

The derivative can instead use normalized interpolation coordinates and a single division by the interval, preserving the existing clamp and accumulation behavior.

Validation environment: Ubuntu 25.10, Python 3.12.13, NVIDIA GeForce RTX 3060 (sm_86), driver 580.159.03, CUDA toolkit 12.4.131. Both native libraries were built from the cited source with the in-tree CMake development build, fast math disabled, MathDx disabled. The build used the project's pinned LLVM 22 SDK and official CCCL 12.9.27 headers with the installed CUDA 12.4 compiler; the newer CUB headers were needed by an unrelated current-source sparse kernel. CPU and CUDA Tape tests reproduce the failure. I directed this work and take responsibility for the report and follow-up. AI assistance was used in producing it; the results above come from local native execution.

resonance-null avatar Nov 04 '26 16:11 resonance-null