[linalg] Fix eigh JVP division-by-zero on degenerate spectra and add precision tests (follow-up to #40149) - #40257
Open
SakshamKapoor2911 wants to merge 1 commit into
Open
Conversation
…precision tests PR jax-ml#40149 resolved the large-eigenvalue mantissa cliff by computing eigenvalue differences prior to reciprocal calculation (Fmat = (delta_w + eye_n)^(-1) - eye_n). However, for degenerate spectra (repeated eigenvalues), off-diagonal entries have delta_w == 0 and eye_n == 0, causing integer_pow(0, -1) to evaluate to inf. This patch masks all zero eigenvalue differences (delta_w == 0, spanning both the diagonal and degenerate off-diagonal pairs) to 1 before inverting: delta_w = lax.select(delta_w == 0, lax.full_like(delta_w, 1), delta_w) Fmat = lax.integer_pow(delta_w, -1) - eye_n This preserves finite invariant subspace tangents for degenerate spectra, keeps exact zero on the diagonal, and adds 5 comprehensive test methods in tests/lax_test.py and tests/linalg_test.py covering cliffs, ill-conditioned matrices, and degenerate spectra.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#ai-generated
Summary
PR #40149 ($|w| \ge 2^p$ ) by reordering the diagonal identity offset:
5fb69a3f70) fixed the large-eigenvalue mantissa cliff (While this cleanly addresses strictly distinct spectra, for degenerate spectra (repeated eigenvalues$\lambda_i = \lambda_j$ with $i \neq j$ ), $1/0 \to \infty$ ) and silently corrupting the gradient pass with
delta_w == 0off-diagonal. Becauseeye_nis 0 off-diagonal,delta_w + eye_nhas 0 at those positions, causinginteger_pow(0, -1)to evaluate toinf(inf/NaN.Prior to #40149, for normal-range eigenvalues,$1/1 = 1$ ).
eye_n + delta_wevaluated to1.0on degenerate entries, keeping the reciprocal finite (Fix
We mask all zero eigenvalue differences (
delta_w == 0, which covers both the diagonal and all degenerate pairs) to1.0prior to taking the reciprocal:delta_w == 0is masked to1.01/1 - 1 = 0.delta_w == 0is masked to1.01/1 - 0 = 1.0(finite, arbitrary for the degenerate invariant subspace).delta_w \neq 0is inverted normally asTests Added
Added 5 comprehensive test methods in
tests/linalg_test.pyandtests/lax_test.py:testEighGradDegenerate: Verifies that matrices with exactly repeated eigenvalues (e.g.jvpacrossfloat32,float64,complex64,complex128.testEighGradLargeEigenvalues: Tests thejsp.linalg.eighin forward and reverse mode (jvp,jacfwd,jacrev, holomorphicjacrev).testEighGradIllConditioned: Tests wide eigenvalue spreads (testEighJvpFiniteLargeEigenvalues: Direct primitive test onlax.linalg.eigh.testEighJvpLargeEigenvaluesAccuracy: Numeric accuracy verification against closed-form Givens perturbation reference.Local Verification
JAX_ENABLE_X64=1and default precision.