Skip to content

Fix bwk - #11

Merged
JeremieGince merged 3 commits into
devfrom
fix_bwk
Jun 20, 2026
Merged

Fix bwk#11
JeremieGince merged 3 commits into
devfrom
fix_bwk

Conversation

@JeremieGince

@JeremieGince JeremieGince commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Description

This pull request improves the numerical stability and correctness of the backward (gradient) computations for Pfaffian-related strategies, especially in the presence of singular or ill-conditioned matrices. The main changes ensure that the backward passes avoid using numerically unstable pseudo-inverses, compute correct gradients for singular cases, and include robust test coverage for these scenarios.

Numerical stability and correctness improvements:

  • The backward methods in PfaffianBlockDet and PfaffianFDBPf now use torch.linalg.inv (LU factorization) instead of torch.linalg.pinv (SVD) to compute matrix inverses, avoiding convergence issues on ill-conditioned inputs. For singular matrices, the code replaces them with the identity matrix before inversion and computes the correct gradient using the cofactor (adjugate) matrix where necessary. [1] [2] [3]
  • The docstrings and comments in these strategies have been updated to clarify the mathematical reasoning, document the new approach for singular cases, and explain the rationale for using LU-based inversion. [1] [2] [3]

Algorithmic and implementation refinements:

  • The Parlett-Reid implementation has been refactored for clarity and efficiency, including improved pivoting logic and in-place matrix updates.
  • A new static method _cofactor_matrix is introduced in PfaffianBlockDet to compute the cofactor matrix for singular blocks, ensuring gradients are finite and correct.

Expanded and improved test coverage:

  • New tests verify that the cofactor matrix matches the classical adjugate transpose for invertible matrices, and that the backward computation for singular blocks produces finite, correct gradients.
  • Additional tests ensure that the backward pass does not use the SVD-based pseudo-inverse, remains finite for odd-dimensional (always singular) matrices, and matches the closed-form gradient for both singular and ill-conditioned inputs.

These changes collectively make the Pfaffian strategies more robust and reliable for a wider range of input matrices.


Checklist

Please complete the following checklist when submitting a PR. The PR will not be reviewed until all items are checked.

  • All new features include a unit test.
    Make sure that the tests passed and the coverage is
    sufficient by running
    uv run pytest --session-timeout=600.
  • All new functions and code are clearly documented.
  • The code passes all pre-commit hooks.
    You can do this by running uvx pre-commit run --all-files.
  • The code is type-checked using Mypy.
    You can do this by running uv run mypy src tests.

@github-actions

Copy link
Copy Markdown
Contributor

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
274 274 100% 98% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/torch_pfaffian/strategies/pfaffian_block_det.py 100% 🟢
src/torch_pfaffian/strategies/pfaffian_fdbpf.py 100% 🟢
src/torch_pfaffian/strategies/pfaffian_parlett_reid.py 100% 🟢
src/torch_pfaffian/strategies/strategy.py 100% 🟢
TOTAL 100% 🟢

updated for commit: 7606fc2 by action🐍

@JeremieGince
JeremieGince merged commit 64655cb into dev Jun 20, 2026
11 checks passed
@JeremieGince
JeremieGince deleted the fix_bwk branch June 20, 2026 03:17
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.

1 participant