Skip to content

[MRG] Reuse quadratic gradients in GW/FGW conditional-gradient solvers - #935

Open
xihaian251 wants to merge 2 commits into
PythonOT:masterfrom
xihaian251:phase30-gw-gradient-reuse
Open

xihaian251 wants to merge 2 commits into
PythonOT:masterfrom
xihaian251:phase30-gw-gradient-reuse

Conversation

@xihaian251

Copy link
Copy Markdown
Contributor

Types of changes

Performance improvement of an existing core solver. Public API unchanged.

Motivation and context

In every iteration of the conditional-gradient solvers for Gromov-Wasserstein
and fused Gromov-Wasserstein, the exact line search in
solve_gromov_linesearch already computes

dot = hC1 @ deltaG @ hC2.T

in order to form the coefficients of the 1-D quadratic step. The resulting
matrix is discarded when the function returns (alpha, 1, cost_G).

On the next iteration, the generic conditional-gradient solver recomputes
df(G) = 2 * (constC - hC1 @ G @ hC2.T) from scratch, which evaluates the same
pair of matrix products again. For symmetric structures this work is redundant.

Issue #491 discusses the cost of the line search for GW descent. This PR does
not implement the "skip line search" option requested there, and leaves the
default step size and the [0, 1] step constraint untouched. It only avoids
recomputing a matrix product that is already available.

Mathematical identity

With POT's convention tensor_product(G) = constC - hC1 G hC2.T and
gwggrad(G) = 2 * tensor_product(G), writing D = hC1 deltaG hC2.T gives, for
any step alpha (including a clipped one):

g(G + alpha * deltaG) = g(G) - 2 * alpha * D

The quadratic model remains

f(G + alpha * deltaG) = f(G) + b * alpha + a * alpha**2
b = <g(G), deltaG> = -2 * <D, G>
a = -<D, deltaG>

The linear feature cost in FGW changes b but not the identity above. The
identity is independent of the marginals and holds for both square_loss and
kl_loss. Exact rational algebra was verified in Wolfram; an independent
four-index summation oracle agrees with the fast objective to 8.9e-16 and with
the cached gradient to 3.6e-15 relative error.

Implementation

  • Extract the unchanged line-search body into a private helper that additionally
    returns dot. The public solve_gromov_linesearch keeps its three-value
    contract and its exact numerical behaviour.
  • Add a private four-value wrapper that returns
    df(G_next) = df(G) - 2 * alpha * dot, consumed by the existing four-value
    branch of generic_conditional_gradient.
  • Enable it only for symmetric=True and armijo=False in
    gromov_wasserstein and fused_gromov_wasserstein. Asymmetric and Armijo
    paths are untouched, since the asymmetric gradient needs a second cross term
    that the line search does not compute.

Diff: two production files (ot/gromov/_gw.py, RELEASES.md) plus focused
regression tests.

Results

Numerical: 18 end-to-end cases (NumPy/PyTorch/JAX, float32/float64,
square/kl, GW/FGW, symmetric/asymmetric/Armijo, user G0, near-degenerate)
produce bit-identical plans and identical objectives and iteration counts.
A 14-case, 150-iteration drift audit shows a maximum relative gradient
difference of 8.2e-16 with no trajectory divergence, so no periodic refresh is
needed.

Performance (single BLAS thread, 9 paired repetitions, two independent random
orders, bootstrap 95% CI):

Case GEMM reduction Speedup, run 1 Speedup, run 2
GW 100x100 45.9% 1.104 1.102
GW 200x200 46.6% 1.219 1.153
FGW 200x200 46.2% 1.154 1.119

EMD still accounts for 53-80% of the wall time, so gains at sizes below 100 are
within measurement noise and are not claimed.

How has this been tested

  • New targeted tests: 10 passed.
  • pytest test/gromov/ test/test_optim.py: 511 passed, 135 skipped, 6 xfailed.
  • Full pytest: 3644 passed, 468 skipped, 6 xfailed, 1 failure unrelated to this change (test_helpers.py::test_helpers, a distutils MSVC compiler-probe failure in this Windows environment; reproduced on the unmodified base commit).
  • Native C++/Cython extensions built from this exact source commit with MSVC
    2022 and the repository's Eigen submodule.
  • ruff checks: no new findings relative to the base commit.

PR checklist

  • CONTRIBUTING.md read
  • Documentation / RELEASES.md updated
  • All relevant tests pass and new regressions are added
  • PR and issue background referenced in RELEASES.md

The exact GW line search already computes dot = hC1 @ deltaG @ hC2.T.
Carry the gradient forward with df(G + alpha*deltaG) = df(G) - 2*alpha*dot
instead of recomputing tensor_product on every conditional-gradient
iteration. Enabled only for symmetric structures with the closed-form
line search; the public solve_gromov_linesearch keeps its three-value
contract and asymmetric/Armijo paths are unchanged.

Adds focused regression tests and a RELEASES.md entry.
Fix the two E731 lint errors reported by the upstream Lint job without
changing the tested numerical behaviour.
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.02970% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.09%. Comparing base (367355d) to head (175f976).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #935   +/-   ##
=======================================
  Coverage   97.09%   97.09%           
=======================================
  Files         130      131    +1     
  Lines       27229    27320   +91     
=======================================
+ Hits        26437    26527   +90     
- Misses        792      793    +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xihaian251

Copy link
Copy Markdown
Contributor Author

Hi POT maintainers,

PR #935 is ready for technical review. All GitHub Actions checks have completed successfully (16 passed, 1 skipped), including Lint, source builds, cross-platform tests, and doctests.

The only remaining red statuses are from the legacy CircleCI build_docs job and its missing artifact.

I investigated this without modifying the PR:

  • The CircleCI job failed after 114 seconds, whereas the previous commit's corresponding job succeeded in approximately 25 minutes. The exact failing step remains unavailable through the public API.
  • The latest commit (175f976..815de82) changes only two lambda assignments in a regression test to named functions, fixing Ruff E731. No production code, documentation, release notes, or CircleCI configuration changed.
  • The PR merge ref has the same Git tree as the current PR HEAD.
  • In an isolated WSL2 Ubuntu / Python 3.12.3 environment, a clean documentation build succeeded after applying CircleCI's RELEASES.md rewrite. All 87 Sphinx-Gallery examples completed successfully.
  • The local environment is not identical to CircleCI, so this does not establish the cause of the remote failure.

These results suggest that the remaining CircleCI failure is unlikely to be caused by the latest lint-only change, although its exact cause is still unverified.

Could you please rerun the CircleCI build_docs job, or share the failure report / first failing step if it persists?

The mathematical correctness, numerical equivalence, performance measurements, and regression tests for the GW/FGW optimization have already been documented in the PR.

Thank you for your consideration.

This branch has not been deployed

No deployments
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.

1 participant