eig3 produces NaN gradients for eigenvalue-only loss at repeated eigenvalues
Author: shi-ericCreated Sep 30, 2026Updated Sep 30, 2026
Labelsbug
Differentiating the sum of wp.eig3()'s eigenvalues produces NaN input gradients for diag(2,2,3) on both CPU and CUDA. The forward loss is finite.
Expected behavior
For a symmetric matrix, the sum of its eigenvalues is its trace. The input gradient should therefore be the identity matrix for both inputs below, including the one with repeated eigenvalues.
Reproducer
import numpy as np
import warp as wp
@wp.kernel
def sum_eigenvalues(a: wp.array[wp.mat33], loss: wp.array[float]):
Q, d = wp.eig3(a[0])
loss[0] = d[0] + d[1] + d[2]
for device in ("cpu", "cuda:0"):
for name, matrix in (
("repeated", np.diag([2, 2, 3]).astype(np.float32)),
("distinct", np.diag([1, 2, 3]).astype(np.float32)),
):
a = wp.array(matrix[None], dtype=wp.mat33, requires_grad=True, device=device)
loss = wp.zeros(1, dtype=float, requires_grad=True, device=device)
with wp.Tape() as tape:
wp.launch(sum_eigenvalues, dim=1, inputs=[a, loss], device=device)
tape.backward(loss=loss)
print(device, name, "loss:", loss.numpy()[0])
print(device, name, "gradient:\n", tape.gradients[a].numpy()[0])Observed behavior
Results with Warp 1.19.0.dev0 and float32:
| Device | Input | Loss | Input gradient |
|---|---|---|---|
| CPU | diag(2,2,3) |
6.999998 |
NaN in all nine entries |
| CUDA | diag(2,2,3) |
6.999996 |
NaN in all nine entries |
| CPU | diag(1,2,3) |
6.0 |
Identity matrix |
| CUDA | diag(1,2,3) |
6.0 |
Identity matrix |
Source: NVIDIA/warp