Skip to content

eig3 produces NaN gradients for eigenvalue-only loss at repeated eigenvalues #2010

Closed

Description

@shi-eric

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

Activity

  1. added
    bug
    on Sep 30, 2026
  2. added theissue type on Sep 30, 2026
  3. added this to the 1.19.0 milestone on Sep 30, 2026
  4. moritzvii commented on Oct 3, 2026

    @moritzvii

    While working on a private project, I also encountered this issue. I noticed that its impact extends beyond eigenvalue-based losses: even a loss that does not depend on the eigendecomposition at all can have its gradient contaminated by an unrelated eig3() call recorded on the same tape.

    For diag(2, 2, 3), differentiating trace(A) gives the expected identity gradient. Adding a separate eig3() diagnostic kernel to the same tape makes that gradient NaN, even though the diagnostic output is not used by the loss.

    Minimal reproducer, saved as a Python file:

    import numpy as np
    import warp as wp
    
    
    @wp.kernel
    def eigenvalue_diagnostic(a: wp.array[wp.mat33], diagnostic: wp.array[float]):
        _q, d = wp.eig3(a[0])
        diagnostic[0] = d[0] + d[1] + d[2]
    
    
    @wp.kernel
    def trace_loss(a: wp.array[wp.mat33], loss: wp.array[float]):
        loss[0] = wp.trace(a[0])
    
    
    wp.set_device("cpu")
    for include_diagnostic in (False, True):
        a = wp.array(np.diag([2, 2, 3])[None], dtype=wp.mat33, requires_grad=True)
        diagnostic = wp.zeros(1, requires_grad=True)
        loss = wp.zeros(1, requires_grad=True)
    
        with wp.Tape() as tape:
            if include_diagnostic:
                wp.launch(eigenvalue_diagnostic, dim=1, inputs=[a, diagnostic])
            wp.launch(trace_loss, dim=1, inputs=[a, loss])
    
        tape.backward(loss=loss)
        print("Diagnostic:", include_diagnostic, "loss:", loss.numpy()[0])
        print(a.grad.numpy()[0])

    Observed with Warp 1.19.0.dev0:

    • Without the diagnostic: loss 7, gradient is the identity matrix.
    • With the diagnostic: loss 7, all nine gradient entries are NaN.

    The expected gradient is the identity matrix in both cases because the loss is unchanged. Unlike the original reproducer, this loss does not depend on either output of eig3(). The NaNs therefore also affect an otherwise valid, independent gradient when the decomposition is recorded only for diagnostics on the same tape. This broadens the impact beyond eigenvalue-based losses.

  5. shi-eric commented on Oct 8, 2026

    @shi-eric
    ContributorAuthor

    This is addressed by 109b6fa.

    The backward pass now skips the eigenvector derivative when its incoming gradient is zero, avoiding singular eigenvalue-gap calculations while preserving the eigenvalue contribution and gradient accumulation. This fixes the reported eigenvalue-sum loss and prevents an unused eig3() diagnostic on the same tape from contaminating an independent trace-loss gradient. This issue is ready to close as completed.

    Public API behavior

    Both kernel overloads of wp.eig3() support smooth, permutation-invariant eigenvalue-only losses, including eigenvalue sums and sums of squares at repeated eigenvalues. Gradients through eigenvectors can still be unreliable at close or repeated eigenvalues; selecting an individual eigenvalue can also be nondifferentiable at a repeated eigenvalue.

    Test coverage

    • warp/tests/matrix/test_mat.py adds analytical loss and gradient checks covering both overloads and float16, float32, and float64 on CPU and CUDA. Cases include distinct, repeated, closely spaced, rotated, negative, identity, and zero matrices, plus gradient accumulation.
    • Mixed batches check zero eigenvector gradients alongside small nonzero eigenvector gradients, with normalized checks that ensure those small contributions are preserved.

    Behavioral verification

    The original reproducer and the unused-diagnostic trace-loss reproducer both produced finite identity gradients on CPU and CUDA. All 30 focused eigen-decomposition tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions