Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
blocks, selectable with `sampling_slices` in `sliced_wasserstein_distance`,
as described in [97] (PR #853)

#### Performance improvements

- Reuse the quadratic cross term already computed by the exact line search to carry the GW gradient across conditional-gradient iterations in symmetric `ot.gromov.gromov_wasserstein` and `ot.gromov.fused_gromov_wasserstein`. This removes two matrix products per iteration without changing the step size, the reported cost or the public `solve_gromov_linesearch` return contract (Issue #491 as background).

#### Documentation

- Clarify how transported mass is interpreted in partial optimal transport and extend the existing 2D example with a deterministic mass sweep and matched-mass summaries (Issue #890).
Expand Down
120 changes: 103 additions & 17 deletions ot/gromov/_gw.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ def line_search(cost, G, deltaG, Mi, cost_G, df_G, **kwargs):
else:

def line_search(cost, G, deltaG, Mi, cost_G, df_G, **kwargs):
if symmetric:
# The quadratic GW objective lets the gradient be carried
# forward instead of recomputing hC1 @ G @ hC2.T each iteration.
return _solve_gromov_linesearch_with_grad(
G,
deltaG,
cost_G,
hC1,
hC2,
M=0.0,
reg=1.0,
df_G=df_G,
nx=np_,
symmetric=True,
**kwargs,
)
return solve_gromov_linesearch(
G,
deltaG,
Expand Down Expand Up @@ -595,6 +611,22 @@ def line_search(cost, G, deltaG, Mi, cost_G, df_G, **kwargs):
else:

def line_search(cost, G, deltaG, Mi, cost_G, df_G, **kwargs):
if symmetric:
# The feature cost is linear in G, so it does not change the
# quadratic GW gradient update carried by the linesearch.
return _solve_gromov_linesearch_with_grad(
G,
deltaG,
cost_G,
hC1,
hC2,
M=(1 - alpha) * M,
reg=alpha,
df_G=df_G,
nx=np_,
symmetric=True,
**kwargs,
)
return solve_gromov_linesearch(
G,
deltaG,
Expand Down Expand Up @@ -850,6 +882,73 @@ def fused_gromov_wasserstein2(
return fgw_dist


def _solve_gromov_linesearch_impl(
G, deltaG, cost_G, C1, C2, M, reg, alpha_min, alpha_max, nx, symmetric
):
"""Core exact GW linesearch, also exposing the reusable quadratic cross term.

Returns ``(alpha, fc, cost_G, dot)`` with ``dot = C1 @ deltaG @ C2.T``.
The public :func:`solve_gromov_linesearch` keeps its three-value contract.
"""
dot = nx.dot(nx.dot(C1, deltaG), C2.T)
a = -reg * nx.sum(dot * deltaG)
if symmetric:
b = nx.sum(M * deltaG) - 2 * reg * nx.sum(dot * G)
else:
b = nx.sum(M * deltaG) - reg * (
nx.sum(dot * G) + nx.sum(nx.dot(nx.dot(C1, G), C2.T) * deltaG)
)

alpha = solve_1d_linesearch_quad(a, b)
if alpha_min is not None or alpha_max is not None:
alpha = np.clip(alpha, alpha_min, alpha_max)

cost_G = cost_G + a * (alpha**2) + b * alpha

return alpha, 1, cost_G, dot


def _solve_gromov_linesearch_with_grad(
G,
deltaG,
cost_G,
C1,
C2,
M,
reg,
df_G,
alpha_min=None,
alpha_max=None,
nx=None,
symmetric=False,
**kwargs,
):
"""Exact GW linesearch that also returns the next conditional-gradient value.

With ``dot = C1 @ deltaG @ C2.T`` and POT's convention
``gwggrad(G) = 2 * (constC - C1 @ G @ C2.T)``, the GW quadratic objective
satisfies, for every admissible step ``alpha``,

.. math::
g(G + \\alpha \\Delta G) = g(G) - 2 \\alpha\\, C1 \\Delta G C2^T .

This lets the next conditional-gradient iteration skip one evaluation of
``tensor_product`` (two matrix products) at no change to the step size or
to the reported cost. Only used internally by the symmetric, closed-form
GW/FGW solvers; the public linesearch API is left untouched.
"""
if nx is None:
if isinstance(M, (int, float)):
nx = get_backend(G, deltaG, C1, C2, df_G)
else:
nx = get_backend(G, deltaG, C1, C2, M, df_G)

alpha, fc, cost_G, dot = _solve_gromov_linesearch_impl(
G, deltaG, cost_G, C1, C2, M, reg, alpha_min, alpha_max, nx, symmetric
)
return alpha, fc, cost_G, df_G - 2 * alpha * dot


def solve_gromov_linesearch(
G,
deltaG,
Expand Down Expand Up @@ -923,23 +1022,10 @@ def solve_gromov_linesearch(
else:
nx = get_backend(G, deltaG, C1, C2, M)

dot = nx.dot(nx.dot(C1, deltaG), C2.T)
a = -reg * nx.sum(dot * deltaG)
if symmetric:
b = nx.sum(M * deltaG) - 2 * reg * nx.sum(dot * G)
else:
b = nx.sum(M * deltaG) - reg * (
nx.sum(dot * G) + nx.sum(nx.dot(nx.dot(C1, G), C2.T) * deltaG)
)

alpha = solve_1d_linesearch_quad(a, b)
if alpha_min is not None or alpha_max is not None:
alpha = np.clip(alpha, alpha_min, alpha_max)

# the new cost is deduced from the line search quadratic function
cost_G = cost_G + a * (alpha**2) + b * alpha

return alpha, 1, cost_G
alpha, fc, cost_G, _ = _solve_gromov_linesearch_impl(
G, deltaG, cost_G, C1, C2, M, reg, alpha_min, alpha_max, nx, symmetric
)
return alpha, fc, cost_G


def gromov_barycenters(
Expand Down
172 changes: 172 additions & 0 deletions test/gromov/test_gw_gradient_reuse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"""Regression tests for gradient reuse in symmetric GW/FGW conditional gradient."""

import numpy as np
import pytest

import ot
from ot.backend import NumpyBackend
from ot.gromov._gw import (
_solve_gromov_linesearch_impl,
_solve_gromov_linesearch_with_grad,
)
from ot.gromov._utils import gwggrad, init_matrix


def _problem(n=7, m=6, seed=0, loss_fun="square_loss"):
rng = np.random.default_rng(seed)
X = rng.normal(size=(n, n))
Y = rng.normal(size=(m, m))
C1 = np.abs(X - X.T)
C2 = np.abs(Y - Y.T)
p = rng.dirichlet(np.ones(n))
q = rng.dirichlet(np.ones(m))
M = rng.random((n, m))
return C1, C2, p, q, M


def test_public_linesearch_keeps_three_value_contract():
C1, C2, p, q, M = _problem()
_, hC1, hC2 = init_matrix(C1, C2, p, q)
G = p[:, None] * q[None, :]
deltaG = ot.emd(p, q, M) - G
res = ot.gromov.solve_gromov_linesearch(
G, deltaG, 1.0, hC1, hC2, M=0.0, reg=1.0, nx=NumpyBackend(), symmetric=True
)
assert len(res) == 3


def test_cached_gradient_matches_gwggrad_for_all_alphas_and_losses():
nx = NumpyBackend()
for loss_fun in ("square_loss", "kl_loss"):
C1, C2, p, q, M = _problem(seed=3, loss_fun=loss_fun)
if loss_fun == "kl_loss":
C1, C2 = C1 + 1.0, C2 + 1.0
constC, hC1, hC2 = init_matrix(C1, C2, p, q, loss_fun)
G = p[:, None] * q[None, :]
deltaG = ot.emd(p, q, M) - G
df_G = gwggrad(constC, hC1, hC2, G, nx)
for alpha in (0.0, 0.2, 0.5, 1.0):
res = _solve_gromov_linesearch_with_grad(
G,
deltaG,
0.0,
hC1,
hC2,
0.0,
1.0,
df_G,
nx=nx,
symmetric=True,
)
# force the requested alpha through alpha_min/alpha_max clipping
clipped = _solve_gromov_linesearch_with_grad(
G,
deltaG,
0.0,
hC1,
hC2,
0.0,
1.0,
df_G,
alpha_min=alpha,
alpha_max=alpha,
nx=nx,
symmetric=True,
)
G_next = G + clipped[0] * deltaG
expected = gwggrad(constC, hC1, hC2, G_next, nx)
np.testing.assert_allclose(clipped[3], expected, rtol=1e-12, atol=1e-12)
assert len(res) == 4


def test_impl_matches_public_linesearch_exactly():
nx = NumpyBackend()
C1, C2, p, q, M = _problem()
_, hC1, hC2 = init_matrix(C1, C2, p, q)
G = p[:, None] * q[None, :]
deltaG = ot.emd(p, q, M) - G
for symmetric in (True, False):
args = (G, deltaG, 1.37, hC1, hC2, M, 0.4)
public = ot.gromov.solve_gromov_linesearch(*args, nx=nx, symmetric=symmetric)
core = _solve_gromov_linesearch_impl(*args, None, None, nx, symmetric)
assert public == core[:3]


@pytest.mark.parametrize(
"armijo,symmetric", [(False, True), (False, False), (True, True)]
)
@pytest.mark.parametrize("fgw", [False, True])
def test_solver_paths_unchanged(armijo, symmetric, fgw):
C1, C2, p, q, M = _problem(seed=8)
kw = {
"loss_fun": "square_loss",
"symmetric": symmetric,
"armijo": armijo,
"log": True,
"max_iter": 60,
"tol_rel": 1e-9,
"tol_abs": 1e-11,
}
if fgw:
G, log = ot.gromov.fused_gromov_wasserstein(M, C1, C2, p, q, alpha=0.4, **kw)
assert "fgw_dist" in log
else:
G, log = ot.gromov.gromov_wasserstein(C1, C2, p, q, **kw)
assert "gw_dist" in log
np.testing.assert_allclose(G.sum(axis=1), p, atol=1e-9)
np.testing.assert_allclose(G.sum(axis=0), q, atol=1e-9)
assert G.min() >= -1e-12
assert {"loss", "u", "v"}.issubset(log.keys())


def test_reuse_solver_matches_explicit_gradient_reference():
C1, C2, p, q, _ = _problem(n=9, m=9, seed=11)
kw = {
"loss_fun": "square_loss",
"symmetric": True,
"log": True,
"max_iter": 80,
"tol_rel": 1e-9,
"tol_abs": 1e-11,
}
G_fast, log_fast = ot.gromov.gromov_wasserstein(C1, C2, p, q, **kw)

# Reference: force the generic solver to recompute df at every iteration by
# calling the public three-value linesearch explicitly.
constC, hC1, hC2 = init_matrix(C1, C2, p, q)
def f(T):
return ot.gromov.gwloss(constC, hC1, hC2, T, NumpyBackend())

def df(T):
return gwggrad(constC, hC1, hC2, T, NumpyBackend())

def line_search(cost, Gt, deltaG, Mi, cost_G, df_G, **kwargs):
return ot.gromov.solve_gromov_linesearch(
Gt,
deltaG,
cost_G,
hC1,
hC2,
M=0.0,
reg=1.0,
nx=NumpyBackend(),
symmetric=True,
**kwargs,
)

G_ref, log_ref = ot.optim.cg(
p,
q,
0.0,
1.0,
f,
df,
None,
line_search,
log=True,
numItermax=80,
stopThr=1e-9,
stopThr2=1e-11,
)
np.testing.assert_allclose(G_fast, G_ref, rtol=0, atol=1e-12)
np.testing.assert_allclose(log_fast["loss"], log_ref["loss"], rtol=0, atol=1e-12)
Loading