diff --git a/RELEASES.md b/RELEASES.md index 03093ac6f..9f292a6f5 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,6 +18,7 @@ #### Closed issues +- Fix the envelope gradient of `ot.batch.solve_gromov_batch` with respect to the marginals `a` and `b` for the PyTorch backend: `grad="envelope"` previously returned no marginal gradient even when the solver reached the correct optimum. The first-order marginal gradient is now reconstructed from the centred dual scaling potentials with a batch-aware VJP. `res.value` keeps its envelope gradients with respect to `M`, `Ca` and `Cb`; `res.value_linear` keeps a gradient path to `M` only (`d value_linear / d M = (1-alpha) T`) and is not differentiable with respect to `Ca`/`Cb`. The forward `value` and `plan` are unchanged. The custom marginal gradient is registered only on the trusted Torch envelope path when the outer solve converged, the plan is marginally feasible and the centred sensitivities are stable under one extra proximal projection. That extra projection and the three checks are evaluated only on this path; elsewhere the corresponding `res.log` flags are `None` (not evaluated). The checks are reduced over the whole batch: one failing sample suppresses marginal gradients for all samples rather than emitting a zero gradient. Torch-only, first-order, locally stationary branch; the stability check is an empirically validated criterion, not a guarantee for every GW problem. JAX/TensorFlow marginal gradients and higher-order differentiation are not covered - Fix ordinary JAX gradients for dense `ot.emd2` and preserve the magnitude and higher-order dependence of gradients supplied to `JaxBackend.set_gradients`, including the envelope gradients in `ot.solve` (Issue #625) - Fix `ot.semidiscrete_wasserstein2_unif_circle` pairing sorted sample locations with unsorted weights, which broke permutation and rotation invariance for nonuniform weights (Issue #921) - Fix an out-of-bounds read in `ot.bsp.merge_bijections`: the number of plans was taken from `perms.shape[1]` (the number of samples) instead of `perms.shape[0]`, so merging fewer plans than samples read past the array and merging more silently dropped plans. A `perms` outside the documented `(n_plans, n_samples)` layout, an empty plan stack and rows that are not permutations are now rejected (PR #911, Issue #910) diff --git a/ot/batch/_quadratic.py b/ot/batch/_quadratic.py index 052c46437..5c3621e23 100644 --- a/ot/batch/_quadratic.py +++ b/ot/batch/_quadratic.py @@ -8,12 +8,53 @@ # # License: MIT License +import math + from ..utils import OTResult -from ot.backend import get_backend +from ot.backend import get_backend, torch from ot.batch._linear import loss_linear_batch from ot.batch._utils import bmv, bop, bregman_log_projection_batch from ot.utils import check_marginal, deprecated, list_to_array +if torch: + + class _BatchGWMarginals(torch.autograd.Function): + r"""Attach a batch-aware marginal VJP to an envelope-mode GW value. + + ``Backend.set_gradients`` is a scalar-output VJP; on the ``(B,)`` value + returned by :func:`solve_gromov_batch` it mis-broadcasts the upstream + cotangent. This local Function aligns the batch axis explicitly and + passes ``grad_output`` through to ``value`` so that the existing + gradients with respect to the cost matrices (``M``, ``Ca``, ``Cb``) + remain intact. + """ + + @staticmethod + def forward(ctx, value, grad_a, grad_b, a, b): + ctx.save_for_backward(grad_a, grad_b) + return value + + @staticmethod + def backward(ctx, grad_output): + grad_a, grad_b = ctx.saved_tensors + w = grad_output.reshape((-1,) + (1,) * (grad_a.ndim - 1)) + # gradients: value, grad_a, grad_b, a, b + return grad_output, None, None, w * grad_a, w * grad_b + + +def _centered_marginal_sensitivities(u, v, reg, nx): + r"""Return the registered marginal sensitivities from dual potentials. + + Centering removes the gauge freedom ``u -> u + c``, ``v -> v - c`` of the + log-domain Bregman potentials. The resulting arrays are exactly the + gradients registered by :class:`_BatchGWMarginals` and are invariant under + that gauge transformation. + """ + n_u, n_v = u.shape[1], v.shape[1] + grad_a = reg * (u - nx.sum(u, axis=1, keepdims=True) / n_u) + grad_b = reg * (v - nx.sum(v, axis=1, keepdims=True) / n_v) + return grad_a, grad_b + def tensor_batch( a, b, Ca, Cb, symmetric=True, nx=None, loss="sqeuclidean", logits=None @@ -581,10 +622,31 @@ def solve_gromov_batch( tol_inner : float, optional Tolerance for convergence of the inner Bregman projection. Default is 1e-5. grad : str, optional - Type of gradient computation, either or 'autodiff', 'envelope' or 'detach'. 'autodiff' provides gradients wrt all - outputs (`plan, value, value_linear`) but with important memory cost. - 'envelope' provides gradients only for (`value, value_linear`)`. `detach`` is the fastest option but - provides no gradients. Default is 'envelope'. + Type of gradient computation, either 'autodiff', 'envelope' or + 'detach'. 'autodiff' provides gradients wrt all outputs + (`plan, value, value_linear`) but with important memory cost. + 'envelope' detaches the plan and provides gradients only for `value` + and `value_linear`. `res.value` keeps its envelope gradient paths to + the cost inputs `M`, `Ca` and `Cb`. `res.value_linear = (1-alpha)` + keeps a gradient path to `M` only (`d value_linear / d M = (1-alpha) T`); + it does NOT depend on `Ca` or `Cb`, since those enter solely through + the quadratic term. With PyTorch, the first-order gradient with + respect to the marginals `a` and `b` is additionally registered on + `res.value` only when the solver converged, the plan satisfies the + marginal constraints, and the centred marginal sensitivities are + stable under one extra proximal projection. This extra projection and + these checks run only on the Torch envelope marginal path; elsewhere + the corresponding `res.log` entries are `None` (not evaluated). The + stability check is an empirically validated criterion, not a + mathematical guarantee for every GW problem. The three flags are + reduced over the whole batch: if any sample fails, the marginal + gradient is withheld for every sample rather than returning a zero + gradient for the failing ones. This marginal gradient is + PyTorch-only, first-order, and relies on a locally stable stationary + branch; it is not a global-optimum sensitivity at non-unique or + active-set switching points. JAX and TensorFlow batch marginal + gradients are not part of this behaviour. `detach` is the fastest + option and provides no gradients. Default is 'envelope'. logits : bool, optional For KL loss, whether Ca and Cb are logits (unnormalized log probabilities). Default is None. @@ -669,14 +731,30 @@ def solve_gromov_batch( grad_inner = "autodiff" if grad == "autodiff" else "detach" + # The extra stability projection is an autograd-plumbing cost incurred + # only for the Torch envelope marginal gradient. All other backends and + # gradient modes keep the original iteration and computational cost. + need_marginal_grad = ( + grad == "envelope" + and reg is not None + and reg > 0 + and torch + and isinstance(a, torch.Tensor) + ) + T = T_init log_T = nx.log(T + 1e-15) # Avoid log(0) + converged = False + # Diagnostics of the final iterate, used to decide whether an envelope + # marginal gradient can be trusted (see the note below). + verify_dual_change = None for iter in range(max_iter): T_prev = T + log_T_prev = log_T Mk = (1 - alpha) * M + 2 * alpha * tensor_product_batch( L, T, nx=nx, recompute_const=False, symmetric=symmetric ) - K = -Mk / reg + log_T + K = -Mk / reg + log_T_prev out = bregman_log_projection_batch( K, a, b, nx=nx, max_iter=max_iter_inner, tol=tol_inner, grad=grad_inner ) @@ -684,6 +762,36 @@ def solve_gromov_batch( log_T = out["log_T"] max_err = nx.max(nx.mean(nx.abs(T_prev - T), axis=(1, 2))) if max_err < tol: + converged = True + if need_marginal_grad: + # Verify that the *registered* sensitivities are a fixed + # point of the proximal map. Compare centred potentials, + # not raw ones: the gauge u->u+c, v->v-c leaves both the + # plan and the marginal gradient unchanged. A loose outer + # ``tol`` can stop while the value is correct but the + # sensitivities are still moving, giving a wrong gradient. + Mk_next = (1 - alpha) * M + 2 * alpha * tensor_product_batch( + L, T, nx=nx, recompute_const=False, symmetric=symmetric + ) + K_next = -Mk_next / reg + log_T + out_next = bregman_log_projection_batch( + K_next, + a, + b, + nx=nx, + max_iter=max_iter_inner, + tol=tol_inner, + grad="detach", + ) + u_now, v_now = out["potentials"] + u_next, v_next = out_next["potentials"] + ga_now, gb_now = _centered_marginal_sensitivities(u_now, v_now, reg, nx) + ga_next, gb_next = _centered_marginal_sensitivities( + u_next, v_next, reg, nx + ) + du = nx.max(nx.abs(ga_next - ga_now), axis=1) + dv = nx.max(nx.abs(gb_next - gb_now), axis=1) + verify_dual_change = nx.detach(nx.maximum(du, dv)) break if grad == "detach": @@ -698,7 +806,60 @@ def solve_gromov_batch( L, T, nx=nx, recompute_const=True, symmetric=symmetric ) # Always recompute const for accurate value value = (1 - alpha) * value_linear + alpha * value_quadratic - log = {"n_iter": iter} + + # The marginal sensitivity of a GW/FGW solution is recovered from the + # last Bregman projection's log-scaling potentials only when three + # independent conditions hold: + # (1) the outer iteration actually converged; + # (2) the transport plan satisfies the marginal constraints; + # (3) the registered, centred sensitivities are stable under one extra + # proximal projection (reg-weighted change below a tol-scaled + # threshold). Centring makes this check invariant to the + # u -> u+c, v -> v-c gauge freedom of the log potentials. + # The outer break flag alone is NOT sufficient: a loose ``tol`` can stop + # early with a correct value but stale potentials, giving a wrong marginal + # gradient. Batch semantics are conservative: the three checks are + # reduced over the whole batch, so a single sample failing any check + # suppresses the marginal gradient for *all* samples. No zero gradient + # is emitted for an untrusted sample. Cost-matrix gradients (M, Ca, Cb) + # keep their autograd path through value in every case. + # Diagnostics are evaluated ONLY on the Torch envelope marginal path. + # Non-target backends and detach/autodiff keep their original behaviour + # and pay no extra reduction/float() cost; the flags are reported as None + # (not evaluated) there rather than as a misleading True. + marginal_feasible = None + potentials_stable = None + if need_marginal_grad and converged: + row_res = nx.max(nx.abs(nx.sum(T, axis=2) - a)) + col_res = nx.max(nx.abs(nx.sum(T, axis=1) - b)) + marginal_feasible = bool( + max(float(nx.detach(row_res)), float(nx.detach(col_res))) <= max(tol, 1e-12) + ) + if verify_dual_change is not None: + dual_res = float(nx.detach(nx.max(verify_dual_change))) + dual_tol = max(math.sqrt(float(tol)), 1e-12) * reg + potentials_stable = dual_res <= dual_tol + else: + potentials_stable = False + local_envelope = ( + need_marginal_grad + and converged + and marginal_feasible is True + and potentials_stable is True + and isinstance(value, torch.Tensor) + ) + if local_envelope: + u, v = out["potentials"] # log-domain scaling potentials (B, n), (B, m) + grad_a, grad_b = _centered_marginal_sensitivities(u, v, reg, nx) + value = _BatchGWMarginals.apply( + value, nx.detach(grad_a), nx.detach(grad_b), a, b + ) + log = { + "n_iter": iter, + "converged": converged, + "marginal_feasible": marginal_feasible, + "potentials_stable": potentials_stable, + } res = OTResult( value=value, diff --git a/test/batch/test_solve_gromov_batch.py b/test/batch/test_solve_gromov_batch.py index eb92fa8dd..ad290e5be 100644 --- a/test/batch/test_solve_gromov_batch.py +++ b/test/batch/test_solve_gromov_batch.py @@ -18,7 +18,7 @@ from ot.batch._linear import dist_batch import pytest from itertools import product -from ot.backend import torch +from ot.backend import torch, get_backend from ot.batch._quadratic import ( div_between_product_batch, loss_quadratic_samples_batch, @@ -566,3 +566,456 @@ def test_loss_quadratic_batch_log_fugw(): expected = expected + unbalanced * log["value_unbalanced"] np.testing.assert_allclose(value, expected) np.testing.assert_allclose(log["value"], expected) + + +@pytest.mark.skipif(not torch, reason="torch not installed") +@pytest.mark.parametrize("fused", [False, True]) +def test_solve_gromov_batch_envelope_marginal_gradients_torch(fused): + """Envelope marginal gradients match the exact 2x2 GW/FGW Oracle. + + Ca=[[0,1],[1,0]], Cb=[[0,2],[2,0]], a=(p,1-p), b=(q,1-q), p=3/5, q=3/10. + The optimum is the boundary t*=0 with G*=18/25 (pure) and F*=51/100 + (fused, M=diag(3,3), alpha=1/2). Sensitivities are dG/dp=-14/5, + dG/dq=-8/5 and dF/dp=-29/10, dF/dq=-23/10 (verified with Wolfram). + """ + Ca = torch.tensor([[0.0, 1.0], [1.0, 0.0]], dtype=torch.float64) + Cb = torch.tensor([[0.0, 2.0], [2.0, 0.0]], dtype=torch.float64) + p = torch.tensor([0.6], dtype=torch.float64, requires_grad=True) + q = torch.tensor([0.3], dtype=torch.float64, requires_grad=True) + a = torch.cat([p, 1 - p]).unsqueeze(0) + b = torch.cat([q, 1 - q]).unsqueeze(0) + kw = {} + if fused: + M = torch.tensor([[3.0, 0.0], [0.0, 3.0]], dtype=torch.float64) + kw = dict(M=M.unsqueeze(0), alpha=0.5) + + res = solve_gromov_batch( + Ca.unsqueeze(0), + Cb.unsqueeze(0), + reg=0.1, + a=a, + b=b, + loss="sqeuclidean", + symmetric=True, + grad="envelope", + max_iter=200, + tol=1e-12, + max_iter_inner=200, + tol_inner=1e-12, + **kw, + ) + inputs = [p, q] + gp, gq = torch.autograd.grad(res.value.sum(), inputs, allow_unused=True) + + assert gp is not None and gq is not None + exp_p, exp_q = (-2.9, -2.3) if fused else (-2.8, -1.6) + np.testing.assert_allclose(gp.numpy(), [exp_p], atol=1e-8) + np.testing.assert_allclose(gq.numpy(), [exp_q], atol=1e-8) + exp_val = 0.51 if fused else 0.72 + np.testing.assert_allclose(res.value.item(), exp_val, atol=1e-8) + np.testing.assert_allclose( + a.detach().numpy(), res.plan.sum(2).detach().numpy(), atol=1e-10 + ) + assert res.log["converged"] is True + assert gp.shape == p.shape and gp.dtype == p.dtype and gp.device == p.device + + +@pytest.mark.skipif(not torch, reason="torch not installed") +@pytest.mark.parametrize("batch_size", [2, 3, 8]) +def test_solve_gromov_batch_envelope_batch_cotangent_torch(batch_size): + """Upstream cotangent is applied per batch with no cross-batch leakage.""" + Ca = torch.tensor([[0.0, 1.0], [1.0, 0.0]], dtype=torch.float64) + Cb = torch.tensor([[0.0, 2.0], [2.0, 0.0]], dtype=torch.float64) + w_np = np.asarray([(-1) ** i * (i + 1) / 4 for i in range(batch_size)]) + p = torch.full((batch_size,), 0.6, dtype=torch.float64, requires_grad=True) + q = torch.full((batch_size,), 0.3, dtype=torch.float64, requires_grad=True) + a = torch.stack([p, 1 - p], dim=1) + b = torch.stack([q, 1 - q], dim=1) + res = solve_gromov_batch( + Ca.unsqueeze(0).expand(batch_size, -1, -1), + Cb.unsqueeze(0).expand(batch_size, -1, -1), + reg=0.1, + a=a, + b=b, + loss="sqeuclidean", + symmetric=True, + grad="envelope", + max_iter=200, + tol=1e-12, + max_iter_inner=200, + tol_inner=1e-12, + ) + w = torch.tensor(w_np, dtype=torch.float64) + gp, gq = torch.autograd.grad((res.value * w).sum(), (p, q), allow_unused=True) + np.testing.assert_allclose(gp.numpy(), w_np * -2.8, atol=1e-9) + np.testing.assert_allclose(gq.numpy(), w_np * -1.6, atol=1e-9) + + +@pytest.mark.skipif(not torch, reason="torch not installed") +def test_solve_gromov_batch_envelope_gate_keeps_cost_gradients_only(): + """When the marginal gate fails, cost gradients remain but p/q get none. + + Two gate failures are covered: + * non-converged outer loop (converged=False); + * converged=True but insufficient inner iterations, which violates + marginal feasibility. + In both cases ``torch.autograd.grad(..., allow_unused=True)`` must return + a gradient for Ca and None for p and q. + """ + for name, kwargs in [ + ( + "outer-not-converged", + dict(max_iter=3, tol=1e-14, max_iter_inner=5, tol_inner=1e-14), + ), + ( + "marginal-infeasible", + dict(max_iter=200, tol=1e-12, max_iter_inner=2, tol_inner=1e-12), + ), + ]: + Ca = torch.tensor( + [[0.26, 0.42], [0.42, 0.54]], dtype=torch.float64, requires_grad=True + ) + Cb = torch.tensor( + [[0.22, 0.39], [0.39, 0.89]], dtype=torch.float64, requires_grad=True + ) + p = torch.tensor([0.6], dtype=torch.float64, requires_grad=True) + q = torch.tensor([0.3], dtype=torch.float64, requires_grad=True) + a = torch.cat([p, 1 - p]).unsqueeze(0) + b = torch.cat([q, 1 - q]).unsqueeze(0) + M = torch.tensor( + [[1.0, 0.0], [0.0, 1.0]], dtype=torch.float64, requires_grad=True + ) + res = solve_gromov_batch( + Ca.unsqueeze(0), + Cb.unsqueeze(0), + reg=0.1, + a=a, + b=b, + M=M.unsqueeze(0), + alpha=0.5, + loss="sqeuclidean", + symmetric=True, + grad="envelope", + **kwargs, + ) + gp, gq, gCa, gCb, gM = torch.autograd.grad( + res.value.sum(), (p, q, Ca, Cb, M), allow_unused=True + ) + assert gp is None and gq is None, f"{name}: unexpected marginal gradient" + assert gCa is not None and gCb is not None and gM is not None + assert torch.isfinite(gCa).all() and torch.isfinite(gCb).all() + # Exposed flags must reflect the actual decision. + trusted = ( + res.log["converged"] + and res.log["marginal_feasible"] + and res.log["potentials_stable"] + ) + assert trusted is False, f"{name}: gate flags inconsistent" + + +@pytest.mark.skipif(not torch, reason="torch not installed") +def test_solve_gromov_batch_envelope_converged_flag_is_not_sufficient(): + """A loose outer tol can stop with the right value but stale potentials. + + This is a negative control for the converged-only gate: value matches the + exact oracle, but the returned marginal gradient is wrong and must be + rejected by the potential-stability check. + """ + Ca = torch.tensor([[0.0, 1.0], [1.0, 0.0]], dtype=torch.float64, requires_grad=True) + Cb = torch.tensor([[0.0, 2.0], [2.0, 0.0]], dtype=torch.float64, requires_grad=True) + p = torch.tensor([0.6], dtype=torch.float64, requires_grad=True) + q = torch.tensor([0.3], dtype=torch.float64, requires_grad=True) + a = torch.cat([p, 1 - p]).unsqueeze(0) + b = torch.cat([q, 1 - q]).unsqueeze(0) + res = solve_gromov_batch( + Ca.unsqueeze(0), + Cb.unsqueeze(0), + reg=0.1, + a=a, + b=b, + loss="sqeuclidean", + symmetric=True, + grad="envelope", + max_iter=200, + tol=1e-2, + max_iter_inner=200, + tol_inner=1e-12, + ) + np.testing.assert_allclose(res.value.item(), 0.72, atol=1e-8) + assert res.log["converged"] is True + assert res.log["potentials_stable"] is False + # A valid cost graph guarantees autograd.grad returns rather than raises, + # so allow_unused=True gives a precise assertion: cost gradients present, + # marginal gradients exactly None. No blanket RuntimeError catch is used. + gp, gq, gCa, gCb = torch.autograd.grad( + res.value.sum(), (p, q, Ca, Cb), allow_unused=True + ) + assert gp is None and gq is None + assert gCa is not None and gCb is not None + assert torch.isfinite(gCa).all() and torch.isfinite(gCb).all() + # value_linear = keeps its path to M in envelope mode (it does not + # depend on Ca/Cb, which only enter the quadratic term). + M = torch.tensor([[1.0, 0.0], [0.0, 1.0]], dtype=torch.float64, requires_grad=True) + res_m = solve_gromov_batch( + Ca.unsqueeze(0), + Cb.unsqueeze(0), + reg=0.1, + a=a, + b=b, + M=M.unsqueeze(0), + alpha=0.0, + loss="sqeuclidean", + symmetric=True, + grad="envelope", + max_iter=200, + tol=1e-2, + max_iter_inner=200, + tol_inner=1e-12, + ) + gl = torch.autograd.grad(res_m.value_linear.sum(), M, allow_unused=True)[0] + assert gl is not None and torch.isfinite(gl).all() + # value_linear = (1-alpha) with T detached, so d(value_linear)/dM + # must equal (1-alpha) * T. alpha=0 here gives exactly T. + np.testing.assert_allclose( + gl.detach().numpy().reshape(M.shape), + res_m.plan.detach().numpy().reshape(M.shape), + atol=1e-10, + ) + np.testing.assert_allclose( + res_m.value_linear.item(), + float((M.detach().unsqueeze(0) * res_m.plan.detach()).sum()), + atol=1e-10, + ) + + +@pytest.mark.skipif(not torch, reason="torch not installed") +def test_solve_gromov_batch_envelope_centered_sensitivities_gauge_invariant(): + """The registered marginal sensitivities are invariant to u+c, v-c.""" + from ot.batch._quadratic import _centered_marginal_sensitivities + + u = torch.tensor([[2.46796, 30.46796], [1.0, 5.0]], dtype=torch.float64) + v = torch.tensor([[-10.46796, 5.53204], [3.0, -2.0]], dtype=torch.float64) + nx = get_backend(u, v) + ga, gb = _centered_marginal_sensitivities(u, v, 0.1, nx) + ga_c, gb_c = _centered_marginal_sensitivities(u + 137.0, v - 137.0, 0.1, nx) + torch.testing.assert_close(ga, ga_c, atol=1e-12, rtol=0) + torch.testing.assert_close(gb, gb_c, atol=1e-12, rtol=0) + + +@pytest.mark.skipif(not torch, reason="torch not installed") +def test_solve_gromov_batch_envelope_gauge_shift_keeps_gate_and_gradient(monkeypatch): + """A large constant gauge shift must not change the gate or the gradient. + + The inner Bregman projection is wrapped to add +250/-250 to its returned + potentials. Because the gate compares centred sensitivities, all log + flags and the registered marginal gradients must remain unchanged. + """ + import ot.batch._quadratic as qm + + original = qm.bregman_log_projection_batch + + def shifted(*args, **kwargs): + out = original(*args, **kwargs) + u, v = out["potentials"] + return dict(out, potentials=(u + 250.0, v - 250.0)) + + def solve(tol): + Ca = torch.tensor( + [[0.0, 1.0], [1.0, 0.0]], dtype=torch.float64, requires_grad=True + )[None] + Cb = torch.tensor( + [[0.0, 2.0], [2.0, 0.0]], dtype=torch.float64, requires_grad=True + )[None] + p = torch.tensor([0.6], dtype=torch.float64, requires_grad=True) + q = torch.tensor([0.3], dtype=torch.float64, requires_grad=True) + r = solve_gromov_batch( + Ca, + Cb, + reg=0.1, + a=torch.cat([p, 1 - p])[None], + b=torch.cat([q, 1 - q])[None], + loss="sqeuclidean", + symmetric=True, + grad="envelope", + max_iter=200, + tol=tol, + max_iter_inner=200, + tol_inner=1e-12, + ) + # Keep a valid cost graph so autograd returns rather than raises; + # marginal gradients are then asserted precisely as None/absent. + gp, gq, gCa, gCb = torch.autograd.grad( + r.value.sum(), (p, q, Ca, Cb), allow_unused=True + ) + assert gCa is not None and gCb is not None + grads = ( + gp.item() if gp is not None else None, + gq.item() if gq is not None else None, + ) + return r.log, grads + + base = solve(1e-12) + monkeypatch.setattr(qm, "bregman_log_projection_batch", shifted) + shifted_result = solve(1e-12) + assert base[0] == shifted_result[0] + np.testing.assert_allclose(base[1], shifted_result[1], atol=1e-9) + # The loose-tol counterexample must still be rejected under a gauge shift. + assert base[0]["potentials_stable"] is True + assert solve(1e-2)[0]["potentials_stable"] is False + monkeypatch.undo() + + +def test_solve_gromov_batch_extra_projection_scope_numpy(): + """Non-Torch backends must not pay for the extra stability projection. + + This test runs unconditionally (NumPy is always available). It asserts + that envelope/detach/autodiff all perform the original number of inner + projections, i.e. no extra projection is inserted outside the Torch + envelope marginal path. + """ + import ot.batch._quadratic as qm + + original = qm.bregman_log_projection_batch + counter = {"n": 0} + + def counted(*args, **kwargs): + counter["n"] += 1 + return original(*args, **kwargs) + + Ca = np.array([[[0.0, 1.0], [1.0, 0.0]]]) + Cb = np.array([[[0.0, 2.0], [2.0, 0.0]]]) + a = np.array([[0.6, 0.4]]) + b = np.array([[0.3, 0.7]]) + + counts = {} + monkeypatch = pytest.MonkeyPatch() + monkeypatch.setattr(qm, "bregman_log_projection_batch", counted) + try: + for grad in ["envelope", "detach", "autodiff"]: + counter["n"] = 0 + solve_gromov_batch( + Ca, + Cb, + reg=0.1, + a=a, + b=b, + loss="sqeuclidean", + symmetric=True, + grad=grad, + max_iter=200, + tol=1e-12, + max_iter_inner=200, + tol_inner=1e-12, + ) + counts[grad] = counter["n"] + finally: + monkeypatch.undo() + # No extra projection for any NumPy gradient mode. + assert counts["envelope"] == counts["detach"] == counts["autodiff"] + assert counts["envelope"] > 0 + + +@pytest.mark.skipif(not torch, reason="torch not installed") +def test_solve_gromov_batch_extra_projection_scope_torch(): + """Only the Torch envelope marginal path pays for one extra projection.""" + import ot.batch._quadratic as qm + + original = qm.bregman_log_projection_batch + counter = {"n": 0} + + def counted(*args, **kwargs): + counter["n"] += 1 + return original(*args, **kwargs) + + Ca = np.array([[[0.0, 1.0], [1.0, 0.0]]]) + Cb = np.array([[[0.0, 2.0], [2.0, 0.0]]]) + a = np.array([[0.6, 0.4]]) + b = np.array([[0.3, 0.7]]) + + def run(use_torch, grad): + counter["n"] = 0 + if use_torch: + c_a, c_b, aa, bb = ( + torch.tensor(Ca), + torch.tensor(Cb), + torch.tensor(a), + torch.tensor(b), + ) + else: + c_a, c_b, aa, bb = Ca, Cb, a, b + solve_gromov_batch( + c_a, + c_b, + reg=0.1, + a=aa, + b=bb, + loss="sqeuclidean", + symmetric=True, + grad=grad, + max_iter=200, + tol=1e-12, + max_iter_inner=200, + tol_inner=1e-12, + ) + return counter["n"] + + monkeypatch = pytest.MonkeyPatch() + monkeypatch.setattr(qm, "bregman_log_projection_batch", counted) + try: + base = run(False, "envelope") + for use_torch, grad in [ + (False, "detach"), + (False, "autodiff"), + (True, "detach"), + (True, "autodiff"), + ]: + assert run(use_torch, grad) == base + # Torch envelope performs exactly one additional projection. + assert run(True, "envelope") == base + 1 + finally: + monkeypatch.undo() + + +@pytest.mark.skipif(not torch, reason="torch not installed") +def test_solve_gromov_batch_envelope_mixed_batch_rejects_all(): + """One failing sample suppresses marginal gradients for the whole batch.""" + Ca = torch.tensor([[0.0, 1.0], [1.0, 0.0]], dtype=torch.float64)[None] + Cb = torch.tensor([[0.0, 2.0], [2.0, 0.0]], dtype=torch.float64)[None] + + def solve(ps, qs): + ca = Ca.expand(len(ps), -1, -1).clone().requires_grad_(True) + cb = Cb.expand(len(ps), -1, -1).clone().requires_grad_(True) + p = torch.tensor(ps, dtype=torch.float64, requires_grad=True) + q = torch.tensor(qs, dtype=torch.float64, requires_grad=True) + r = solve_gromov_batch( + ca, + cb, + reg=0.1, + a=torch.stack([p, 1 - p], 1), + b=torch.stack([q, 1 - q], 1), + loss="sqeuclidean", + symmetric=True, + grad="envelope", + max_iter=200, + tol=1e-3, + max_iter_inner=200, + tol_inner=1e-12, + ) + # Valid cost graph: autograd must return, not raise. Assert that the + # cost gradients exist while the marginal gradients are exactly None. + gp, gq, gca, gcb = torch.autograd.grad( + r.value.sum(), (p, q, ca, cb), allow_unused=True + ) + assert gca is not None and gcb is not None + has = gp is not None + if not has: + assert gq is None + return r.log, has + + single_ok, has_ok = solve([0.6], [0.3]) + single_bad, has_bad = solve([0.999], [0.999]) + mixed, has_mixed = solve([0.6, 0.999], [0.3, 0.999]) + assert has_ok is True and single_ok["potentials_stable"] is True + assert has_bad is False and single_bad["potentials_stable"] is False + # Conservative whole-batch rejection: no zero-gradient substitute. + assert has_mixed is False and mixed["potentials_stable"] is False