jax
jax copied to clipboard
jax.jit produces nan when jax.scipy.linalg.expm output is used in scaled dot-product attention
Description
jax.jit produces nan for functions that pass jax.scipy.linalg.expm output through scaled dot-product attention (softmax + matmul), while eager mode correctly returns finite values. This is a JIT compiler correctness bug — eager and JIT disagree on the numerical result.
Reproducer 1
import jax
import jax.numpy as jnp
_key = jax.random.PRNGKey(344146)
_key, _k1 = jax.random.split(_key)
t = jax.random.normal(_k1, (5,))
_key, _k2 = jax.random.split(_key)
K = jax.random.normal(_k2, (5, 5))
_key, _k3 = jax.random.split(_key)
V = jax.random.normal(_k3, (5, 5))
def f(t, K, V):
y = jnp.expm1(jnp.clip(t, -4, 4))
z = jnp.arctan2(t, t)
w = jnp.outer(y, z)
m = jnp.einsum('ij,jk->ik', w, w)
Q = jax.scipy.linalg.expm(m * 0.1)
scale = Q.shape[-1] ** -0.5
weights = jax.nn.softmax(jnp.matmul(Q, K.T) * scale, axis=-1)
return jnp.matmul(weights, V)
eager = f(t, K, V)
jit_out = jax.jit(f)(t, K, V)
print('eager:', eager[0]) # [ 2.107 0.701 -0.217 0.089 0.078]
print('jit: ', jit_out[0]) # [nan nan nan nan nan]
Reproducer 2
import jax
import jax.numpy as jnp
_key = jax.random.PRNGKey(919749)
_key, _k1 = jax.random.split(_key)
t1 = jax.random.normal(_k1, (8, 3))
_key, _k2 = jax.random.split(_key)
t2 = jax.random.normal(_k2, (3, 3))
_key, _k3 = jax.random.split(_key)
t3 = jax.random.normal(_k3, (3, 3))
def f(t1, t2, t3):
s1 = jax.nn.sparse_plus(t1)
s2 = jax.scipy.special.digamma(jnp.abs(s1) + 0.1)
s3 = jnp.rot90(s2)
s4 = jnp.atleast_1d(jnp.inner(s3, s3))
Q = jax.scipy.linalg.expm(s4 * 0.1)
s6 = jnp.nansum(s4, axis=1)
Q = jnp.maximum(Q, s6)
scale = Q.shape[-1] ** -0.5
w = jax.nn.softmax(jnp.matmul(Q, jnp.swapaxes(t2, -2, -1)) * scale, axis=-1)
return jnp.matmul(w, t3)
eager = f(t1, t2, t3)
jit_out = jax.jit(f)(t1, t2, t3)
print('eager:', eager[0]) # [-0.468 0.066 -0.045]
print('jit: ', jit_out[0]) # [nan nan nan]
Expected vs Actual
Both reproducers: eager returns finite values, jax.jit returns nan.
The common pattern is jax.scipy.linalg.expm output used as the query matrix Q in scaled dot-product attention. XLA appears to miscompile this combination, producing nan where eager correctly produces finite results.
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-generic', version='#110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026', machine='x86_64')
JAX_PLATFORMS=cpu
fixed by #44738, verified on current nightly