Optimization and Stability - #16
Merged
Merged
Conversation
Fix Pfaffian backward for numerically singular inputs
…and add tests for edge cases
Contributor
☂️ Python Coverage
Overall Coverage
New Files
Modified Files
|
- Add a lightweight tutorial for the public `pfaffian` function that executes live during the Sphinx build (replaces the removed benchmark). - Fix index.rst placeholders (<PackageName>) and point the Tutorials toctree at the new notebook. - Enable MyST dollarmath/amsmath and use the maintained jsDelivr MathJax build so the tutorial's LaTeX renders instead of showing as raw text.
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.
Description
This pull request adds a new log-domain ("slog") Pfaffian computation to both the Rust backend and the Python interface, improving numerical stability for large or tiny Pfaffians and providing new user-facing APIs. It introduces a
slog_pfaffianfunction that returns both the unit-modulus phase and the logarithm of the absolute value, and updates the Pythonpfaffianfunction to transparently recover from overflows using the log-domain path. The Rust backend is refactored to support these features, with new trait bounds, kernels, and batch APIs, and is tested accordingly.Log-domain Pfaffian computation and API:
slog_pfaffianfunction to the Python interface (src/torch_pfaffian/__init__.py), which returns(phase, log_abs)for a batch of skew-symmetric matrices, using the newSlogPfaffianStrategy. The docstring explains its differentiability and relationship to the standard Pfaffian. (F921f98bL41R41)pfaffianfunction to automatically use the log-domain path to recover from overflows whencheck_finite=True, and expanded the docstring to document the new routing and overflow handling.Rust backend: log-domain kernel and batch support:
SlogScalartrait and implemented it forf32,f64,Complex<f32>, andComplex<f64>, providing a generic way to extract the unit phase for log-domain calculations.slog_pfaffian_from_flatkernel, which computes the log-domain signed Pfaffian for a single matrix, and thesigned_slog_flatbatch function, with parallelization support. [1] [2]signed_slog_pfaffian_f64,signed_slog_pfaffian_f32,signed_slog_pfaffian_c128,signed_slog_pfaffian_c64) that return arrays of phases and log-magnitudes for batches of matrices.Refactoring and enhancements:
pfaffian_from_flat) and clarified the distinction between linear and log-domain paths, improving code clarity and efficiency. [1] [2]Python interface improvements:
AUTO_SMALL_MAXto route small matrices to the exact unrolled kernel, and updated the documentation to explain the routing logic and log-domain recovery. [1] [2]These changes make the Pfaffian computation more robust, especially for numerically challenging cases, and provide a more flexible and transparent API for users.
Checklist
Please complete the following checklist when submitting a PR. The PR will not be reviewed until all items are checked.
Make sure that the tests passed and the coverage is
sufficient by running
uv run pytest --session-timeout=600.You can do this by running
uvx pre-commit run --all-files.You can do this by running
uv run mypy src tests.