From e18fe7d3c00043ffc3e7d1e4b9df6c0fe4d9bb9e Mon Sep 17 00:00:00 2001 From: Zhe Wang <53119724+flyingdoubleG@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:46:25 +0800 Subject: [PATCH 1/2] Fix sinkhorn_unbalanced_translation_invariant with asymmetric reg_m The exponents k_rho1 and k_rho2 applied to the sums over the other marginal in the updates of v and u were swapped. They coincide when reg_m1 == reg_m2, so only asymmetric marginal penalties converged to a wrong plan. --- RELEASES.md | 1 + ot/unbalanced/_sinkhorn.py | 6 +++-- test/unbalanced/test_sinkhorn.py | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index b0e12bb7c..6f25fc2ad 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -26,6 +26,7 @@ - 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) +- Fix `ot.unbalanced.sinkhorn_unbalanced_translation_invariant` (also `method="sinkhorn_translation_invariant"` in `ot.unbalanced.sinkhorn_unbalanced`) converging to a wrong plan when `reg_m` has two different values: the exponents `k_rho1` and `k_rho2` of the updates of `v` and `u` were swapped, which only cancels when `reg_m1 == reg_m2` (PR #XXX) - Fix overflow to NaN in `ot.stochastic.coordinate_grad_semi_dual` by shifting before exponentiating, as `ot.stochastic.c_transform_entropic` already does. The factor cancels in the normalisation, so results are unchanged where the old code did not overflow (PR #866, Issue #264) - Fix `ot.bsp.compute_bspot_bijection` (and `random_state` of `ot.solve_sample(..., method="bsp")`) not being reproducible for a given `seed`: its candidate plans came from a `thread_local` `std::mt19937` seeded once per OpenMP thread, so the result depended on the call history and on the number of threads. Every thread started from the same state, so the previous version could underperform with multiple threads. Each plan now draws its own stream, derived from the seed and the plan index (PR #898, Issue #897) - Fix duplicate function and class descriptions in generated module API pages while preserving top-level aliases and example backreferences (Issue #527) diff --git a/ot/unbalanced/_sinkhorn.py b/ot/unbalanced/_sinkhorn.py index 56749b319..472b489ef 100644 --- a/ot/unbalanced/_sinkhorn.py +++ b/ot/unbalanced/_sinkhorn.py @@ -1550,8 +1550,10 @@ def sinkhorn_unbalanced_translation_invariant( else 0 ) - k_rho1 = k1 * reg_m1 / reg if reg_m1 != float("inf") else 0 - k_rho2 = k2 * reg_m2 / reg if reg_m2 != float("inf") else 0 + # k_rho1 is used in the update of v and k_rho2 in the update of u: with + # reg_m1 != reg_m2 the other pairing converges to another fixed point + k_rho1 = k2 * reg_m1 / reg if reg_m1 != float("inf") else 0 + k_rho2 = k1 * reg_m2 / reg if reg_m2 != float("inf") else 0 if reg_m1 == float("inf") and reg_m2 == float("inf"): xi1, xi2 = 0, 0 diff --git a/test/unbalanced/test_sinkhorn.py b/test/unbalanced/test_sinkhorn.py index 616a18c8e..5b9794b5f 100644 --- a/test/unbalanced/test_sinkhorn.py +++ b/test/unbalanced/test_sinkhorn.py @@ -586,6 +586,47 @@ def test_unbalanced_multiple_inputs(nx, method): np.testing.assert_allclose(nx.to_numpy(losses), np.array([loss1, loss2]), atol=1e-4) +@pytest.mark.parametrize("reg_m", [(0.5, 2.0), (5.0, 0.1), (1.0, 50.0)]) +def test_translation_invariant_asymmetric_reg_m(nx, reg_m): + # the translation invariant solver must solve the same problem as + # sinkhorn_knopp_unbalanced when the marginal penalties differ + rng = np.random.RandomState(0) + a = rng.rand(6) + 0.1 + a = 1.7 * a / a.sum() + b = rng.rand(8) + 0.1 + b = b / b.sum() + M = 2 * rng.rand(6, 8) + reg = 0.1 + + ab, bb, Mb = nx.from_numpy(a, b, M) + + G = ot.unbalanced.sinkhorn_unbalanced_translation_invariant( + ab, bb, Mb, reg, reg_m, numItermax=100000, stopThr=1e-12 + ) + G_ref = ot.unbalanced.sinkhorn_knopp_unbalanced( + a, b, M, reg, reg_m, numItermax=100000, stopThr=1e-14 + ) + np.testing.assert_allclose(nx.to_numpy(G), G_ref, atol=1e-8) + + # several histograms at once give the cost of each column + B = np.stack([b, 2 * b], axis=1) + costs = ot.unbalanced.sinkhorn_unbalanced_translation_invariant( + ab, nx.from_numpy(B), Mb, reg, reg_m, numItermax=100000, stopThr=1e-12 + ) + for j in range(2): + G_j = ot.unbalanced.sinkhorn_knopp_unbalanced( + a, + B[:, j], + M, + reg, + reg_m, + reg_type="entropy", + numItermax=100000, + stopThr=1e-14, + ) + np.testing.assert_allclose(nx.to_numpy(costs)[j], np.sum(G_j * M), rtol=1e-7) + + def test_stabilized_vs_sinkhorn(nx): # test if stable version matches sinkhorn n = 100 From a91cc3a976e63abbcabce207af4402656fb13641 Mon Sep 17 00:00:00 2001 From: Zhe Wang <53119724+flyingdoubleG@users.noreply.github.com> Date: Mon, 2 Nov 2026 23:14:27 +0800 Subject: [PATCH 2/2] Add PR number to RELEASES.md --- RELEASES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASES.md b/RELEASES.md index 6f25fc2ad..9b386d6c3 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -26,7 +26,7 @@ - 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) -- Fix `ot.unbalanced.sinkhorn_unbalanced_translation_invariant` (also `method="sinkhorn_translation_invariant"` in `ot.unbalanced.sinkhorn_unbalanced`) converging to a wrong plan when `reg_m` has two different values: the exponents `k_rho1` and `k_rho2` of the updates of `v` and `u` were swapped, which only cancels when `reg_m1 == reg_m2` (PR #XXX) +- Fix `ot.unbalanced.sinkhorn_unbalanced_translation_invariant` (also `method="sinkhorn_translation_invariant"` in `ot.unbalanced.sinkhorn_unbalanced`) converging to a wrong plan when `reg_m` has two different values: the exponents `k_rho1` and `k_rho2` of the updates of `v` and `u` were swapped, which only cancels when `reg_m1 == reg_m2` (PR #908) - Fix overflow to NaN in `ot.stochastic.coordinate_grad_semi_dual` by shifting before exponentiating, as `ot.stochastic.c_transform_entropic` already does. The factor cancels in the normalisation, so results are unchanged where the old code did not overflow (PR #866, Issue #264) - Fix `ot.bsp.compute_bspot_bijection` (and `random_state` of `ot.solve_sample(..., method="bsp")`) not being reproducible for a given `seed`: its candidate plans came from a `thread_local` `std::mt19937` seeded once per OpenMP thread, so the result depended on the call history and on the number of threads. Every thread started from the same state, so the previous version could underperform with multiple threads. Each plan now draws its own stream, derived from the seed and the plan index (PR #898, Issue #897) - Fix duplicate function and class descriptions in generated module API pages while preserving top-level aliases and example backreferences (Issue #527)