Skip to content

[MRG] Add a PyTorch solver to ot.dr.wda (#806) - #858

Open
deeb01 wants to merge 4 commits into
PythonOT:masterfrom
deeb01:feat-806-wda-torch-solver
Open

deeb01 wants to merge 4 commits into
PythonOT:masterfrom
deeb01:feat-806-wda-torch-solver

Conversation

@deeb01

@deeb01 deeb01 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Types of changes

New feature (non-breaking change which adds functionality).

Motivation and context / Related issue

Addresses #806, following @rflamary's request for solver='torch' alongside the default solver='autograd'.

solver='torch' runs PyTorch autodiff with Riemannian gradient descent on Stiefel — QR retraction, backtracking with an adaptive initial step — mirroring pymanopt's SteepestDescent so both solvers target the same optimum. It needs only torch, and accepts torch tensors directly, keeping device and dtype.

ot.dr dependencies are now imported optionally, each function raising an ImportError naming what it needs. This changes import ot.dr from raising to succeeding on a partial install — flagging it in case you prefer the old behaviour.

On speed: pymanopt is 0.6–1.8% of runtime and this solver is 1.0–1.75× faster, so the dependency choice is the real benefit. A frozen-plan gradient for a larger win stalls at a worse objective; numbers on the issue.

Also raises a clear ValueError when the between-class cost underflows to zero, previously a divide-by-zero.

How has this been tested (if it applies)

New tests assert the torch objective and gradient match the autograd ones (rtol 1e-10, 1e-8) and that both solvers reach a comparable objective from the same start; ratio 0.997–1.004 across n, k, C and sinkhorn_method. Also covered: tensor input dtype, sinkhorn_log, input not mutated, error path.

test_dr.py 16 passed; full suite 2668 passed, 62 skipped, 6 xfailed; pre-commit clean.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

Adds solver='torch' to wda: PyTorch autodiff with Riemannian gradient
descent on the Stiefel manifold, using a QR retraction and backtracking
with an adaptive initial step. It mirrors what pymanopt's SteepestDescent
does, so both solvers target the same optimum rather than two different
algorithms.

The torch path needs only torch, so it works on installations without
autograd or pymanopt, and accepts torch tensors directly, keeping their
device and dtype. To make that possible, ot.dr's dependencies are now
imported optionally and each function raises an ImportError naming what it
needs, rather than the module failing to import unless all of them are
present.

Verified that the torch objective and its gradient match the autograd ones
at the same point, and that both solvers reach a comparable objective from
the same starting point.

Also raises a clear ValueError when the between-class transport cost
underflows to zero, which previously produced a divide-by-zero warning and
an undefined objective.
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.00881% with 59 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.64%. Comparing base (3073aa0) to head (4a3741b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #858      +/-   ##
==========================================
- Coverage   96.85%   96.64%   -0.21%     
==========================================
  Files         128      128              
  Lines       26161    26384     +223     
==========================================
+ Hits        25337    25499     +162     
- Misses        824      885      +61     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants