Skip to content

!fix(sqrt_inverse_matrix_semi_positive): Fix the eigh, delete preferred_linalg_library option#192

Merged
TheoRudkiewicz merged 11 commits intogrowingnet:mainfrom
TheoRudkiewicz:fix-eigh
Feb 18, 2026
Merged

!fix(sqrt_inverse_matrix_semi_positive): Fix the eigh, delete preferred_linalg_library option#192
TheoRudkiewicz merged 11 commits intogrowingnet:mainfrom
TheoRudkiewicz:fix-eigh

Conversation

@TheoRudkiewicz
Copy link
Copy Markdown
Collaborator

@TheoRudkiewicz TheoRudkiewicz commented Dec 27, 2025

In cases where S is not postive definite, torch.linalg.eigh may fail:
torch._C._LinAlgError: linalg.eigh: The algorithm failed to converge because the input matrix is ill-conditioned or has too many repeated eigenvalues (error code: 329464).
In case this happens I add a small perturbation to make it positive definite. I could not cover those lines because I did not suceed to trigger the bug on purpose.

I also remove the preferred_linalg_library option as we do not use it and it does not make sense to use it at this level. We should switch at the full execution level. (It's what I do in my hydra-script)

@TheoRudkiewicz TheoRudkiewicz self-assigned this Dec 27, 2025
@TheoRudkiewicz TheoRudkiewicz changed the title !fix: Fix the eigh fix: Fix the eigh Dec 27, 2025
@TheoRudkiewicz TheoRudkiewicz changed the title fix: Fix the eigh !fix: Fix the eigh, delete preferred_linalg_library option Feb 17, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 95.13% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/gromo/utils/tools.py 99.17% <100.00%> (-0.02%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TheoRudkiewicz TheoRudkiewicz changed the title !fix: Fix the eigh, delete preferred_linalg_library option !fix(sqrt_inverse_matrix_semi_positive): Fix the eigh, delete preferred_linalg_library option Feb 17, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the sqrt_inverse_matrix_semi_positive function to handle ill-conditioned matrices that cause torch.linalg.eigh to fail. When a LinAlgError occurs due to numerical issues, the function now adds a small identity matrix perturbation (1e-6) to make the matrix positive definite and retries. The PR also removes the preferred_linalg_library parameter from this function, as the decision of which linear algebra library to use should be made at a higher execution level rather than within individual functions.

Changes:

  • Modified error handling in sqrt_inverse_matrix_semi_positive to add identity perturbation when eigh fails
  • Removed preferred_linalg_library parameter from the function signature and its usage
  • Updated all tests to remove the preferred_linalg_library parameter and improved test code quality with better assertion methods

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
src/gromo/utils/tools.py Removed preferred_linalg_library parameter, added LinAlgError recovery with identity matrix perturbation
tests/test_tools.py Updated tests to remove preferred_linalg_library usage, improved assertion methods, moved manual_seed to setUp, removed obsolete mocked error tests
tests/torch_unittest.py Added setUp method with torch.manual_seed(0) for consistent test behavior, minor f-string formatting fixes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_tools.py Outdated
Comment thread src/gromo/utils/tools.py Outdated
Comment thread src/gromo/utils/tools.py Outdated
Comment thread src/gromo/utils/tools.py
Comment thread tests/test_tools.py Outdated
Comment thread tests/test_tools.py Outdated
Comment thread src/gromo/utils/tools.py Outdated
Comment thread tests/test_tools.py
@TheoRudkiewicz TheoRudkiewicz marked this pull request as ready for review February 17, 2026 18:18
Copy link
Copy Markdown
Collaborator

@sylvchev sylvchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank Théo!

Comment thread src/gromo/utils/tools.py Outdated
# Sometimes, due to numerical issues, we get an error:
# The algorithm failed to converge because the input matrix is
# ill-conditioned or has too many repeated eigenvalues
matrix += 1e-6 * torch.eye(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the factor the same for all float precision? Shouldn't 1e-6 value depend on float16, float32 or float64 type?

@TheoRudkiewicz TheoRudkiewicz merged commit bfd3132 into growingnet:main Feb 18, 2026
8 of 9 checks passed
@sylvchev
Copy link
Copy Markdown
Collaborator

@TheoRudkiewicz for future reference, there is a nice way of handling dynamical error correction, depending on the type shown in the SPDLearn lib with the get_epsilon function.

@TheoRudkiewicz TheoRudkiewicz deleted the fix-eigh branch February 19, 2026 16:25
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.

3 participants