jnp.nextafter and jnp.spacing do not have differentiation rules implemented
Description
jnp.nextafter and jnp.spacing raise NotImplementedError: Differentiation rule for 'nextafter' not implemented when used with jax.grad. Unlike jnp.floor which explicitly defines a zero JVP via ad.defjvp_zero(floor_p), nextafter_p has no JVP rule registered at all — meaning this is a missing implementation rather than an intentional design decision.
jnp.nextafter(x, y) has a well-defined gradient with respect to x (approximately 1.0, confirmed via finite differences), so a gradient rule is both mathematically meaningful and implementable.
Minimal Reproducer
import jax
import jax.numpy as jnp
x = jnp.array([1.0, 2.0])
y = jnp.array([2.0, 3.0])
# Forward works
print(jnp.nextafter(x, y)) # [1.0000001 2.0000002]
# Gradient fails
jax.grad(lambda x: jnp.nextafter(x, y).sum())(x) # NotImplementedError
# jnp.spacing also fails
jax.grad(lambda x: jnp.spacing(x).sum())(x) # NotImplementedError
Error
NotImplementedError: Differentiation rule for 'nextafter' not implemented
Expected Behavior
jnp.nextafter should support gradient computation. Finite difference verification confirms a well-defined gradient:
import numpy as np
x = np.float32(1.0)
eps = np.float32(1e-7)
finite_diff = (np.nextafter(x + eps, np.float32(2.0)) - np.nextafter(x, np.float32(2.0))) / eps
print(finite_diff) # 1.1920929 — non-zero, well-defined
Unlike jnp.floor which explicitly registers ad.defjvp_zero(floor_p) to indicate its gradient is zero, nextafter_p has no JVP rule registered at all, indicating this is an oversight rather than an intentional decision.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.10.1
jaxlib: 0.10.1
numpy: 2.4.6
python: 3.12.13 | packaged by Anaconda, Inc. | (main, Mar 19 2026, 20:20:58) [GCC 14.3.0]
device info: cpu-1, 1 local devices
process_count: 1
platform: uname_result(system='Linux', node='unicorn-login-01', release='6.8.0-110-geographic', version='#110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026', machine='x86_64')
JAX_PLATFORMS=cpu
I believe the thinking here was that these functions have to do with the details of the floating point representation, which do not sensibly map to any real-valued functions. Since differentiation is a procedure that only makes sense in the context of real-valued functions, it follows that autodiff rules are poorly defined for nextafter, spacing, and other functions which expose the details of the floating point representation. What do you think
@jakevdp Your point makes a lot of sense — nextafter and spacing are fundamentally about floating point representation rather than smooth real-valued functions, so autodiff in the traditional sense is indeed ill-defined. That said, I'd argue there's still value in registering an explicit defjvp_zero rule (as done for floor, ceil, round, etc.) rather than leaving it as NotImplementedError. The current behavior raises an error that interrupts computation graphs containing these ops, even in cases where the user might reasonably expect a zero gradient (e.g. treating nextafter as a constant with respect to its inputs in a larger expression). Would you consider registering ad.defjvp_zero(nextafter_p) and ad.defjvp_zero(spacing_p) as a conservative fix? This would be consistent with how other discontinuous/representation-level ops are handled, and would avoid breaking gradient computation through graphs that incidentally contain these ops.
That seems reasonable – would you like to send a PR adding defjvp_zero for these primitives? If not I'm happy to put it on my TODO list.
Thanks for raising the issue!
@jakevdp Sounds great! I can send out a PR for this on Friday, thank you!
That seems reasonable – would you like to send a PR adding
defjvp_zerofor these primitives? If not I'm happy to put it on my TODO list.Thanks for raising the issue!
Hi @jakevdp, submitted a PR for this here: https://github.com/jax-ml/jax/pull/38811