Skip to content

Fix zero-adjoint SVD gradients (GH-2054) - #2064

Open
fusheng-ji wants to merge 2 commits into
NVIDIA:mainfrom
fusheng-ji:fusheng-ji/fix-svd-half-gradients
Open

fusheng-ji wants to merge 2 commits into
NVIDIA:mainfrom
fusheng-ji:fusheng-ji/fix-svd-half-gradients

Conversation

@fusheng-ji

@fusheng-ji fusheng-ji commented Oct 8, 2026 •

Copy link
Copy Markdown

What problem are you trying to solve?

Closes #2054.

With half-precision wp.svd2() and wp.svd3(), differentiating the squared singular-value norm of A = 2I produces NaN input gradients instead of approximately 4I. An unused SVD diagnostic on the same tape can also change the gradient of an independent trace(A) loss from I to NaN.

The adjoint evaluates inverse squared-singular-value gaps even when singular-vector adjoints are zero. At repeated singular values, the regularized reciprocal overflows in half precision, and multiplication by zero introduces NaNs.

This is an independent correctness investigation; I do not currently have downstream application or production impact to report. Outside-implementation guidance has been requested in #2054; no maintainer assignment or approval is claimed.

What does this change?

  • When both U and V adjoints are exactly zero, accumulate only U * diag(adj_sigma) * transpose(V) into the input adjoint.
  • If the singular-value adjoint is also zero, return without changing the existing input adjoint.
  • Add regression coverage for singular-value losses, gradient accumulation, unused diagnostics, mixed zero/nonzero seeds, and small nonzero U/V adjoints, plus a changelog fragment.

Public signatures, forward implementations, epsilon values and the general nonzero U/V adjoint formula are unchanged. This is independent of the SVD3 forward fix in #2063 and does not address the general repeated-spectrum findings in #2030.

Existing pull requests

Validation

Current source 733930ddf3ebe55f422cf1fbd7b59be2029ec15d, a single signed-off commit on official main 0382a97e2c72911c1f6bba45274e12833a19d8e2. The previous merged head 091d1e84 has been replaced; the focused patch still contains only svd.h, the existing matrix tests and 2054.fixed.md.

  • Fresh independent standard build: uv run build_lib.py, exit 0. The loaded native libraries were built in this checkout; source cleanliness and library SHA256 hashes are recorded.
  • 78/78 SVD tests passed, zero skipped, including existing FP32/64 finite differences and small nonzero U/V adjoint checks.
  • 295/295 matrix-module tests passed, zero skipped.
  • 540/540 numerical evaluations passed with finite losses and gradients, using unchanged FP16/32/64 tolerances.
  • CPU and two NVIDIA B200 GPUs on Linux x86_64; Python 3.12.13, NumPy 2.5.3, CUDA Toolkit 13.0.88, driver 580.126.20.
  • Applicable source pre-commit hooks and git diff --check passed. Complete sanitized build/test output, commands and exit codes are published.

Current report · Validation metadata · Environment and library hashes · SVD test log · Full matrix log · Build log · Checksums

Current numerical results

The standalone script covers two dimensions, two calling forms, five scenarios, three batch elements, three dtypes and three devices: 540 API/scenario/seed evaluations on A = 2I, not independent random matrices. References are computed in NumPy float64 from actual inputs; an evaluation passes only when both loss and gradient are finite and within tolerance. The published CSV references and pass flags were also independently recomputed.

Dtype Current evaluations Failed Max absolute gradient error Max gradient error / allowance
FP16 180 0 0.01171875 0.341796875
FP32 180 0 2.38418579e-06 0.104357025
FP64 180 0 1.77635684e-15 1.3366309e-05

The allowance is atol + rtol * abs(expected); both tolerances are 1e-2, 1e-5 and 1e-10 for FP16, FP32 and FP64. The FP16 residual is retained in the data.

Current-build SVD gradient error ratios

Per-evaluation CSV · Per-device summary · Measurement script · Reproduction commands · Plot script

Historical comparison

The unchanged assets at 40f663c6 compare c47fe4c8 (before) with e6185ac3 (after), with 540 evaluations per revision: 144 FP16 failures before, zero failures after. Those measurements were not rerun as a new baseline here. The earlier baseline unit-test failure counts have no located original log and are not presented as verified conclusions; the current 78/295 test counts above are backed by complete fresh logs.

Historical SVD3 FP16 gradients: c47fe4c8 before, e6185ac3 after

Historical report · Before CSV · After CSV · Historical summary figure

Current evidence links are pinned to asset commit 8a23e20cccb4c3aebec2601ce7246e0603f73bde. Historical links remain pinned separately.

uv run build_lib.py
uv run --extra dev -m unittest warp.tests.matrix.test_mat -k test_svd -v
uv run --extra dev -m unittest warp.tests.matrix.test_mat -v
uvx pre-commit run --files warp/native/svd.h warp/tests/matrix/test_mat.py changelog/2054.fixed.md

Checklist

  • I have read and followed the Contributing Guidelines. Outside-implementation guidance is requested in the linked issue comment; no maintainer approval is implied.
  • 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.

@copy-pr-bot

copy-pr-bot Bot commented Oct 8, 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 8, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed NaN gradients in half-precision 2D and 3D SVD operations when losses use singular values that are repeated.
    • Prevented unused SVD diagnostic outputs from affecting gradients in other calculations.
    • Improved gradient handling for singular-value-only losses when singular-vector derivatives are zero or very small, including matrices with repeated or distinct singular values.
    • Verified gradient behavior across SVD return-value and output-argument APIs, including seeded and masked batches.

The 2D and 3D SVD adjoints now handle cases where singular-vector adjoints are zero without running the general gradient calculation. Tests cover singular-value and singular-vector gradients, including repeated singular values and both API forms.

Changes: SVD gradient handling

Layer / File(s) Summary
Handle zero singular-vector adjoints
warp/native/svd.h
The 2D and 3D SVD adjoints return without changing the input gradient when all output adjoints are zero. When only singular-value adjoints are nonzero, they add the singular-value contribution and skip the singular-vector derivative calculation.
Test SVD gradient behavior
warp/tests/matrix/test_mat.py, changelog/2054.fixed.md
Tests check singular-value and singular-vector gradients for repeated and distinct singular values, both API forms, and supported dtypes. The changelog records the half-precision gradient issue and the effect of unused diagnostics.

Suggested reviewers: shi-eric, christophercrouzet, daedalus5, alecjacobson

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 8fd8d

This change stops half-precision SVD gradients from becoming NaN when only singular values contribute to the loss. The vector-adjoint path is unchanged, and tests cover both 2D and 3D and both API forms. No merge-blocking risk was identified.

Security architecture risk: Minimal · up to 8fd8d

The change avoids unnecessary derivative calculations while preserving gradient accumulation. Inspection found no introduced or worsened risk materially affecting security boundaries, failure containment, rollback, or ownership guarantees.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix (zero-adjoint SVD gradients) and the issue number. It matches the changeset, which changes adj_svd2 and adj_svd3 in svd.h to handle zero singular-vector adjoints, and adds test…
Description check Passed The description states the problem (NaN input gradients from half-precision svd2/svd3 at repeated singular values), the change (accumulating only U * diag(adj_sigma) * transpose(V) when U and V adjoin…
Linked Issues check Passed Directly linked issue #2054 requires that singular-value-only losses from wp.svd2() and wp.svd3() produce finite gradients, and that an unused SVD diagnostic not change an independent trace(A) gradien…
Out of Scope Changes check Passed The whole-PR changes at head are limited to warp/native/svd.h, the SVD regression tests in warp/tests/matrix/test_mat.py, and changelog/2054.fixed.md. The svd.h hunks touch only adj_svd2 and adj_svd3 …
Full details: Title check

Explanation

The title names the fix (zero-adjoint SVD gradients) and the issue number. It matches the changeset, which changes adj_svd2 and adj_svd3 in svd.h to handle zero singular-vector adjoints, and adds tests and a changelog entry. The title is short and specific enough for a reviewer scanning history.

Full details: Description check

Explanation

The description states the problem (NaN input gradients from half-precision svd2/svd3 at repeated singular values), the change (accumulating only U * diag(adj_sigma) * transpose(V) when U and V adjoints are zero), and the tests and validation run. It is directly related to the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #2054 requires that singular-value-only losses from wp.svd2() and wp.svd3() produce finite gradients, and that an unused SVD diagnostic not change an independent trace(A) gradient. The whole-PR diff of warp/native/svd.h (merge base to head) adds an early path to adj_svd3 and adj_svd2. When adj_U and adj_V are both exactly zero, the path either returns when adj_sigma is also zero, or adds only U * diag(adj_sigma) * transpose(V) to adj_A and returns. This avoids the inverse squared-singular-value gap terms that produce NaN at repeated singular values in half precision. For A = 2I, this yields 4I for the squared singular-value norm and leaves the trace gradient unchanged. The general nonzero U/V path is unchanged. The PR summary and changelog describe regression tests for both calling forms, FP16 losses, mixed seeds, and unused diagnostics. I did not read the test file hunks at head, so test coverage is taken from the summary. The reported validation counts are not independently rerun here. The code path meets the stated coding requirements.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes at head are limited to warp/native/svd.h, the SVD regression tests in warp/tests/matrix/test_mat.py, and changelog/2054.fixed.md. The svd.h hunks touch only adj_svd2 and adj_svd3 and add the zero-singular-vector-adjoint shortcut. No public signature, forward implementation, epsilon value, or SVD3 forward code (addressed by #2063) is changed in the diff. The shortcut directly serves #2054. The tests and changelog support that fix. No unrelated change is demonstrated.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/warp
  • Branch: main
  • File: .coderabbit.yml
  • Commit: d182530
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: e9865696-77e2-4007-bfd8-4325f5296e86
Commits

Reviewing files that changed from the base of the PR and between 733930d and 8fd8d96.

Files selected for processing: 2

  • 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; 10 remain after this review.

@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Adds zero-gradient shortcuts to SVD backward calculations so unused singular vectors do not introduce NaNs.

  • SVD gradients skip singular-vector math when both vector adjoints are zero.

No actionable issues found. Tests were not run during this review.

Reviews (4) · Last reviewed commit: "Merge branch 'main' into fusheng-ji/fix-..." · Reviewed by Greptile

@fusheng-ji
fusheng-ji marked this pull request as ready for review October 11, 2026 15:52
Inactive singular-vector derivatives can overflow in half precision and contaminate independent gradients. Skip those derivatives when their adjoints are exactly zero while retaining gradient accumulation and the general SVD adjoint.

Signed-off-by: Wenbo Ji <[email protected]>

This branch has not been deployed

No deployments
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.

Half-precision svd2 and svd3 produce NaN gradients for singular-value-only losses

1 participant