Should gradients of logsumexp([-inf, -inf]) be 0s instead NaNs?
Author: khoCreated May 21, 2021Updated Sep 29, 2026
LabelsbugP2 (eventual)
Consider this snippet
import jax
import jax.numpy as jnp
def f(x):
return jax.nn.logsumexp(jnp.stack([-jnp.inf, -jnp.inf + x]))
jax.grad(f)(1.)The computation inside f is essentially what happens inside the dynamic program of the CTC loss and similar losses that calculates a shortest distance over the log semiring. There, -jnp.inf is really "log(0)", and we can see that (0 + 0 * exp(x))'s gradient w.r.t. x should indeed be 0.
Right now, the gradients are NaN instead because the gradients of logsumexp([-inf, -inf]) are NaNs.
Source: jax-ml/jax