Skip to content

Optimization and Stability - #16

Merged
JeremieGince merged 8 commits into
mainfrom
dev
Jul 13, 2026
Merged

JeremieGince merged 8 commits into
mainfrom
dev

Conversation

@JeremieGince

@JeremieGince JeremieGince commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

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_pfaffian function that returns both the unit-modulus phase and the logarithm of the absolute value, and updates the Python pfaffian function 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:

  • Added the slog_pfaffian function to the Python interface (src/torch_pfaffian/__init__.py), which returns (phase, log_abs) for a batch of skew-symmetric matrices, using the new SlogPfaffianStrategy. The docstring explains its differentiability and relationship to the standard Pfaffian. (F921f98bL41R41)
  • Updated the main pfaffian function to automatically use the log-domain path to recover from overflows when check_finite=True, and expanded the docstring to document the new routing and overflow handling.

Rust backend: log-domain kernel and batch support:

  • Introduced the SlogScalar trait and implemented it for f32, f64, Complex<f32>, and Complex<f64>, providing a generic way to extract the unit phase for log-domain calculations.
  • Added the slog_pfaffian_from_flat kernel, which computes the log-domain signed Pfaffian for a single matrix, and the signed_slog_flat batch function, with parallelization support. [1] [2]
  • Exposed new Python-callable functions (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:

  • Refactored the internal Pfaffian kernel to operate on flat buffers (pfaffian_from_flat) and clarified the distinction between linear and log-domain paths, improving code clarity and efficiency. [1] [2]
  • Added tests for the new log-domain kernel, including reconstruction, exact zero handling, and complex input support.

Python interface improvements:

  • Added AUTO_SMALL_MAX to 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.

  • 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

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

☂️ Python Coverage

current status: ✅

Overall Coverage

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

New Files

File Coverage Status
src/torch_pfaffian/kernels/init.py 100% 🟢
src/torch_pfaffian/kernels/det_magnitude.py 100% 🟢
src/torch_pfaffian/kernels/parlett_reid_slog.py 100% 🟢
src/torch_pfaffian/kernels/small_n.py 100% 🟢
src/torch_pfaffian/strategies/pfaffian_slog.py 100% 🟢
src/torch_pfaffian/strategies/pfaffian_small.py 100% 🟢
TOTAL 100% 🟢

Modified Files

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

updated for commit: b1e5b0d by action🐍

- 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.
@JeremieGince
JeremieGince merged commit 84beae2 into main Jul 13, 2026
12 checks passed
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