`eigh` forward- and reverse-mode eigenvector gradients return all-`NaN` for eigenvalues above `2^p` (mantissa-precision cliff in the JVP rule's div-by-zero guard)
Description
jax.jacfwd/jax.jacrev of jnp.linalg.eigh's eigenvectors return an all-NaN Jacobian whenever the input matrix's eigenvalues exceed roughly 2^p, where p is the number of significant mantissa bits for the dtype in use (p=24 for float32, p=53 for float64). PyTorch's torch.linalg.eigh produces the correct finite gradient for the identical input and dtype.
Reproducible example
import numpy as np, jax, jax.numpy as jnp, torch
base = np.float32(2**24)
lam = np.array([base, base + 2, base + 4], dtype=np.float32)
A = np.diag(lam)
f = lambda M: jnp.sum(jnp.linalg.eigh(M)[1])
g = lambda M: torch.sum(torch.linalg.eigh(M)[1])
print("JAX (float32):")
print(jax.jacfwd(f)(jnp.asarray(A)))
print("PyTorch (float32):")
print(torch.autograd.functional.jacobian(g, torch.tensor(A)))
Output
JAX (float32):
[[nan nan nan]
[nan nan nan]
[nan nan nan]]
PyTorch (float32):
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
Root cause
The eigenvector derivative for symmetric matrices with distinct eigenvalues is:
┌ 1/(λ_j - λ_i) i ≠ j
F_ij = │
└ 0 i = j
dV = V (F ⊙ V^T dM V)
JAX's implementation avoids the literal i=j division-by-zero by adding the identity before inverting, then subtracting it back off, in _eigh_jvp_rule, jax/_src/lax/linalg.py.
Fmat = lax.integer_pow(eye_n + w[..., np.newaxis, :] - w[..., np.newaxis], -1) - eye_n
Walking the diagonal (i=j) at eigenvalue magnitude λ:
eye_n[i,i] + (λ - λ) = 1 + 0 = 1, so that step 2 inverts a safe nonzero value.- But floating point does
1 + λas a single rounded operation: onceλ ≥ 2^p(ulp(λ) > 1), the value1is smaller than the gap between adjacent representable floats nearλ, so1 + λrounds back down to exactlyλ. - The subsequent
- λ(fromw[..., None]) then gives exactly0, not1. 1/0 = inf, theninf - 1 = inf.- This
infis multiplied elementwise against the correctly-computed0entry ofV^T dM Von the diagonal, and the resultingNaNpropagates through the finaldot(v, Fmat * vdag_adot_v)into every entry of the output.
Because reverse-mode differentiation of eigh is obtained by transposing this same JVP rule registered, jax.jacrev fails identically to jax.jacfwd.
Why PyTorch is unaffected
PyTorch never derives the diagonal from an arithmetic identity. It assigns the diagonal outright.
In linalg_eig_backward(FunctionsManual.cpp#L4130-L4151):
auto Econj = [&L] {
auto Lconj = L.conj();
auto ret = Lconj.unsqueeze(-2) - Lconj.unsqueeze(-1);
ret.diagonal(0, -2, -1).fill_(1.);
return ret;
}();
auto ret = VhgV.div_(Econj);
if (gL.defined()) {
// ...
ret.diagonal(0, -2, -1).copy_(gL);
}
First, fill_(1.) is a plain store into the diagonal
rather than the sum λ + 1 - λ. Second, the
placeholder is irrelevant regardless: L4147 throws away the entire diagonal of the quotient and replaces it with gL. Similarily for forward mode.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.6.2
jaxlib: 0.6.2
numpy: 2.2.6
python: 3.10.19 (main, Oct 21 2025, 16:37:10) [Clang 20.1.8 ]
device info: cpu-1, 1 local devices"
process_count: 1
platform: uname_result(system='Darwin', node='xx', release='25.6.0', version='Darwin Kernel Version 25.6.0: Fri Jul 31 19:18:29 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T8122', machine='arm64')
Hi! I reproduced this on current main and I can confirm the failure comes from the evaluation order in the diagonal guard inside _eigh_jvp_rule. I’m worked on a fix that computes the eigenvalue differences first, then adds the identity guard, also added regression coverage for both jacfwd and jacrev around the float32 2**24 precision cliff.
Could a maintainer add the review label when convenient? Thanks!