Skip to content

Fix eig3 NaN gradients for zero eigenvector adjoints (GH-2010) - #2034

Closed
moritzvii wants to merge 1 commit into
NVIDIA:mainfrom
moritzvii:moritzvii/fix-eig3-zero-adjoints
Closed

moritzvii wants to merge 1 commit into
NVIDIA:mainfrom
moritzvii:moritzvii/fix-eig3-zero-adjoints

Conversation

@moritzvii

@moritzvii moritzvii commented Oct 4, 2026 •

Copy link
Copy Markdown

What problem are you trying to solve?

Closes #2010.

At repeated eigenvalues, wp.eig3() can produce NaN input gradients even when the loss uses only eigenvalues. An unrelated eigendecomposition diagnostic recorded on the same tape can also contaminate the gradient of trace(A). Likewise, a zero-seeded sample can poison a shared parameter's gradient and make an otherwise valid optimization step NaN.

Standalone reproducers demonstrate the unrelated diagnostic and masked shared parameter cases.

What does this change?

When adj_Q is zero, adj_eig3 accumulates the eigenvalue contribution Q diag(adj_d) Qᵀ without forming reciprocal eigenvalue gaps. When both output adjoints are zero, it returns without modifying the accumulated input gradient.

Add regression tests for eigenvalue sum and squared sum, accumulation with a direct trace gradient, unrelated diagnostics and masked shared parameters. Cover repeated eigenvalues in all pair positions, isotropic, zero, negative and rotated matrices, with separated-eigenvalue controls across float16, float32 and float64. Include a changelog entry.

Existing pull requests

  • I searched open pull requests for duplicate or overlapping work.

Validation

  • Regression validation: All nine new CPU tests fail without the guard (21 failing subtests) and pass with it. The unrelated diagnostic preserves the identity gradient; masked samples produce parameter gradients 6 and 0 for seeds [0,1] and [0,0].
  • NVIDIA GPU: Not tested. Validation was on Apple Silicon CPU only.
  • Other validation: Native CPU build, all 78 tests in test_mat.py, and applicable pre-commit checks passed. The tests are registered for CPU and CUDA devices across the three floating-point types.

The CPU build and test results predate the latest base update; the fix itself is unchanged. They have not been rerun on the current base.

Test command:

uv run -m unittest warp.tests.matrix.test_mat -v

Checklist

  • I have read and followed the Contributing Guidelines.
  • This pull request does not bundle unrelated changes unless a maintainer agreed to the broader scope.
  • My branch is up to date with main and contains one commit, unless a maintainer agreed to keep the change as multiple commits.
  • I included any required changelog fragments and used a + prefix only for changes that do not require a GitHub issue.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed NaN gradients from wp.eig3() at repeated eigenvalues when eigenvector gradients are zero, including cases involving separate tape diagnostics and shared parameters with zero-seeded samples.
  • Tests

    • Added gradient coverage for repeated and varied eigenvalues, independent trace losses, and shared-parameter gradient accumulation.

Repeated eigenvalues make reciprocal gaps singular even when the
eigenvector adjoint is zero. This contaminates eigenvalue-only
losses, unrelated tape diagnostics and masked shared parameters.

Eigenvalue-only derivatives do not require reciprocal gaps, and a
fully unused decomposition must leave accumulated gradients intact.

Signed-off-by: Moritz von Büren <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used
📚 Code guidelines (1)
AGENTS.md — auto-discovered

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/warp/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 37531fb8-3c3b-4141-a7bb-b1eec8c93154
📥 Commits

Reviewing files that changed from the base of the PR and between 67bce43 and 55f39a7.

📒 Files selected for processing (3)
  • changelog/2010.fixed.md
  • warp/native/svd.h
  • warp/tests/matrix/test_mat.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Walkthrough

Walkthrough

adj_eig3 now bypasses eigenvector-gap calculations when adj_Q is zero. New tests cover eigenvalue gradients, tape diagnostics, and shared-parameter gradient accumulation.

Changes

eig3 Gradient Handling

Layer / File(s) Summary
Zero eigenvector-adjoint fast path
warp/native/svd.h
When adj_Q and adj_d are both zero, adj_eig3 returns without changing adj_A. When only adj_Q is zero, it adds the eigenvalue-only gradient and returns before calculating reciprocal eigenvalue gaps.
Gradient tests and changelog
warp/tests/matrix/test_mat.py, changelog/2010.fixed.md
Tests compare eigenvalue gradients with analytical references across repeated, zero, negative, and rotated eigenvalues. They also check trace-loss gradients with recorded eig3 outputs and gradient accumulation through a shared scalar parameter. The changelog describes the fix and tested cases.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: shi-eric

Merge Risk: ⚪ Minimal · up to 55f39

This fix stops NaN gradients from wp.eig3() when eigenvectors receive no gradient and eigenvalues repeat. Regression tests are included. GPU testing was not performed, so run CI on CUDA before merging.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: fixing NaN gradients from eig3 when eigenvector adjoints are zero.
Linked Issues check ✅ Passed Issue #2010 requires the eigenvalue-sum gradient to equal the identity for repeated and distinct eigenvalues. The adj_eig3 zero-adj_Q path accumulates Q diag(adj_d) Qᵀ without reciprocal eigenva…
Out of Scope Changes check ✅ Passed The reported changes are limited to the adj_eig3 fix, regression tests for the repeated-eigenvalue and zero-adjoint cases described in the PR, and a changelog entry. These changes support issue #201…
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Fixes gradient computation for eigenvalue decomposition.

The PR appears safe to merge based on the reviewed changes.

Summary

The PR adds a zero-eigenvector-adjoint path to adj_eig3 so eigenvalue-only and disconnected losses avoid NaN gradients at repeated eigenvalues. It also adds CPU/CUDA-registered regression tests and a changelog fragment.

Reviews (1) · Last reviewed commit: "Prevent eig3 NaNs from zero adjoints (GH..."

@fusheng-ji fusheng-ji mentioned this pull request Oct 8, 2026
4 of 5 tasks
@shi-eric

Copy link
Copy Markdown
Contributor

Thanks @moritzvii for the contribution and the additional reproducer. #2010 was fixed separately in 109b6fa. The upstream fix covers eigenvalue-only losses and prevents unused eig3() diagnostics from contaminating other gradients on the same tape. Closing this PR as superseded since the fix is already on main.

@shi-eric shi-eric closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants