From 9bf8058665a73b0ad19c04821a5602e3a009c994 Mon Sep 17 00:00:00 2001 From: DiadkoShmek <207617272+DiadkoShmek@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:40:44 +0200 Subject: [PATCH 1/2] Fix paired Gaussian distances for unequal batch sizes --- RELEASES.md | 2 ++ ot/gaussian.py | 2 +- test/test_gaussian.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/RELEASES.md b/RELEASES.md index 5d58d2128..a813deae5 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,6 +18,8 @@ #### Closed issues +- Fix `ot.gaussian.bures_wasserstein_distance(..., paired=True)` returning incorrectly broadcast distances or raising an error for unequal batch sizes, instead of returning all crossed distances as documented. + - 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 duplicate function and class descriptions in generated module API pages while preserving top-level aliases and example backreferences (Issue #527) - Fix internal arrays created on the torch default device instead of the input device in `ot.partial.partial_wasserstein`, `ot.wasserstein_circle` (and `ot.sliced_wasserstein_sphere`), the high-dimensional Bures-Wasserstein functions in `ot.gaussian`, `ot.gmm`, `ot.lowrank_sinkhorn` with `init="deterministic"`, the semi-relaxed (F)GW barycenters and `ot.utils.projection_sparse_simplex`, which made them fail on GPU inputs. Add a `meta_default_device` test fixture to catch such allocations on CPU (PR #883, Issue #852) diff --git a/ot/gaussian.py b/ot/gaussian.py index 29c7c9c7f..f949143b2 100644 --- a/ot/gaussian.py +++ b/ot/gaussian.py @@ -592,7 +592,7 @@ def bures_wasserstein_distance(ms, mt, Cs, Ct, paired=False, log=False): assert ( len(ms.shape) == 2 and len(mt.shape) == 2 ), "Both ms and mt should be batched" - if paired and len(ms.shape) == len(mt.shape): + if paired and len(ms) == len(mt): # Return shape (n,) squared_dist_m = nx.norm(ms - mt, axis=-1) ** 2 else: diff --git a/test/test_gaussian.py b/test/test_gaussian.py index 3199e2403..9b9fec54a 100644 --- a/test/test_gaussian.py +++ b/test/test_gaussian.py @@ -672,3 +672,42 @@ def test_bures_barycenter_fixpoint_no_convergence_warning(): C = A @ A.transpose(0, 2, 1) + np.eye(4)[None] with pytest.warns(UserWarning, match="did not converge"): ot.gaussian.bures_barycenter_fixpoint(C, num_iter=1) + + +@pytest.mark.parametrize("batch_sizes", [(2, 1), (1, 3), (2, 3), (3, 2), (2, 2)]) +@pytest.mark.parametrize("paired", [False, True]) +@pytest.mark.parametrize("log", [False, True]) +@pytest.mark.parametrize("dtype", [np.float32, np.float64]) +def test_bures_wasserstein_distance_unequal_batches( + nx, batch_sizes, paired, log, dtype +): + ns, nt = batch_sizes + ms = np.array([[0, 0], [3, 4], [-2, 1]], dtype=dtype)[:ns] + mt = np.array([[1, -1], [2, 5], [-3, 2]], dtype=dtype)[:nt] + stds = np.array([[1, 2], [4, 5], [2, 3]], dtype=dtype)[:ns] + stdt = np.array([[2, 3], [1, 4], [3, 1]], dtype=dtype)[:nt] + Cs = np.array([np.diag(std**2) for std in stds]) + Ct = np.array([np.diag(std**2) for std in stdt]) + + # For diagonal covariances, W2 is the Euclidean distance between the + # concatenated means and standard deviations. + reference = np.sqrt( + np.sum((ms[:, None] - mt[None]) ** 2, axis=-1) + + np.sum((stds[:, None] - stdt[None]) ** 2, axis=-1) + ) + if paired and ns == nt: + reference = reference.diagonal() + + inputs = nx.from_numpy(ms, mt, Cs, Ct) + snapshots = [nx.to_numpy(value).copy() for value in inputs] + result = ot.gaussian.bures_wasserstein_distance(*inputs, paired=paired, log=log) + if log: + result, info = result + assert info["Cs12"].shape == Cs.shape + + assert result.shape == reference.shape + np.testing.assert_allclose(nx.to_numpy(result), reference, rtol=1e-6, atol=1e-6) + nx.assert_same_dtype_device(inputs[0], result) + assert nx.to_numpy(result).dtype == dtype + for value, snapshot in zip(inputs, snapshots): + np.testing.assert_array_equal(nx.to_numpy(value), snapshot) From 80ca25524365d3494c3fa4dd3dbd8c4f9ce5a586 Mon Sep 17 00:00:00 2001 From: DiadkoShmek <207617272+DiadkoShmek@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:52:18 +0200 Subject: [PATCH 2/2] Link Gaussian batch fix in release notes --- RELEASES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASES.md b/RELEASES.md index a813deae5..025816a05 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,7 +18,7 @@ #### Closed issues -- Fix `ot.gaussian.bures_wasserstein_distance(..., paired=True)` returning incorrectly broadcast distances or raising an error for unequal batch sizes, instead of returning all crossed distances as documented. +- Fix `ot.gaussian.bures_wasserstein_distance(..., paired=True)` returning incorrectly broadcast distances or raising an error for unequal batch sizes, instead of returning all crossed distances as documented. (PR #909) - 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 duplicate function and class descriptions in generated module API pages while preserving top-level aliases and example backreferences (Issue #527)