diff --git a/RELEASES.md b/RELEASES.md index 03093ac6f..b4a7358ef 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,6 +18,7 @@ #### Closed issues +- Return transport marginals from sparse and lazy `OTResult` plans without materializing the full sparse plan. - 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/utils.py b/ot/utils.py index 69a4854b5..3999651d3 100644 --- a/ot/utils.py +++ b/ot/utils.py @@ -1180,6 +1180,10 @@ class UndefinedParameter(Exception): class OTResult: """Base class for OT results. + Marginals are available for dense, sparse and lazy transport plans. Sparse + plans are reduced without constructing the full dense matrix, while lazy + plans are evaluated in batches controlled by ``batch_size``. + Parameters ---------- @@ -1380,7 +1384,11 @@ def value_quad(self): def marginals(self): """Marginals of the transport plan: should be very close to "a" and "b" for balanced OT.""" - if self._plan is not None: + if ( + self._plan is not None + or self._sparse_plan is not None + or self._lazy_plan is not None + ): return self.marginal_a, self.marginal_b else: return None @@ -1390,6 +1398,11 @@ def marginal_a(self): """First marginal of the transport plan, with the same shape as "a".""" if self._plan is not None: return self._backend.sum(self._plan, 1) + elif self._sparse_plan is not None: + nx = self._backend + # Reduce the sparse plan without materializing the full matrix. + ones = nx.ones((self._sparse_plan.shape[1], 1), type_as=self._sparse_plan) + return nx.reshape(self._sparse_plan @ ones, (-1,)) elif self._lazy_plan is not None: lp = self._lazy_plan bs = self._batch_size @@ -1403,6 +1416,10 @@ def marginal_b(self): """Second marginal of the transport plan, with the same shape as "b".""" if self._plan is not None: return self._backend.sum(self._plan, 0) + elif self._sparse_plan is not None: + nx = self._backend + ones = nx.ones((1, self._sparse_plan.shape[0]), type_as=self._sparse_plan) + return nx.reshape(ones @ self._sparse_plan, (-1,)) elif self._lazy_plan is not None: lp = self._lazy_plan bs = self._batch_size diff --git a/test/test_solvers.py b/test/test_solvers.py index b08c594c3..5d5ce0fef 100644 --- a/test/test_solvers.py +++ b/test/test_solvers.py @@ -654,6 +654,15 @@ def test_solve_sample_lazy_emd(nx, metric): # Lazy solver: compute distances on-the-fly sol_lazy = ot.solve_sample(X_sb, X_tb, ab, bb, lazy=True, metric=metric) + # Access marginals without materializing the full sparse transport plan. + assert sol_lazy.marginals is not None + for actual, expected in [ + (sol_lazy.marginal_a, ab), + (sol_lazy.marginal_b, bb), + *zip(sol_lazy.marginals, (ab, bb)), + ]: + np.testing.assert_allclose(nx.to_numpy(actual), nx.to_numpy(expected)) + # Check that optimal costs match np.testing.assert_allclose( nx.to_numpy(sol_standard.value), diff --git a/test/test_utils.py b/test/test_utils.py index 9c93d520a..8a5544a03 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -562,6 +562,116 @@ def test_OTResult(): getattr(res, at) +@pytest.mark.parametrize("representation", ["dense", "sparse", "lazy"]) +@pytest.mark.parametrize("dtype", [np.float32, np.float64]) +def test_OTResult_marginals(nx, representation, dtype): + # A rectangular plan with a zero target marginal and nonuniform weights. + plan_np = np.array([[0.2, 0.0, 0.3], [0.0, 0.0, 0.5]], dtype=dtype) + plan = nx.from_numpy(plan_np) + if representation == "sparse": + rows, cols = np.nonzero(plan_np) + sparse = nx.coo_matrix( + *nx.from_numpy(plan_np[rows, cols], rows, cols), + shape=plan_np.shape, + type_as=plan, + ) + res = ot.utils.OTResult(sparse_plan=sparse, backend=nx) + elif representation == "lazy": + lazy = ot.utils.LazyTensor(plan.shape, lambda i, j: plan[i, j]) + res = ot.utils.OTResult(lazy_plan=lazy, backend=nx, batch_size=1) + else: + res = ot.utils.OTResult(plan=plan, backend=nx) + + expected_a = np.array([0.5, 0.5]) + expected_b = np.array([0.2, 0.0, 0.8]) + assert res.marginal_a is not None + assert res.marginal_b is not None + assert res.marginals is not None + for actual, expected in [ + (res.marginal_a, expected_a), + (res.marginal_b, expected_b), + *zip(res.marginals, (expected_a, expected_b)), + ]: + assert actual.shape == expected.shape + nx.assert_same_dtype_device(plan, actual) + np.testing.assert_allclose(nx.to_numpy(actual), expected) + + +def test_OTResult_sparse_matrix_marginals(): + # csr_matrix reductions otherwise retain an extra dimension as np.matrix. + plan = scipy.sparse.csr_matrix([[0.2, 0.0, 0.3], [0.0, 0.0, 0.5]]) + res = ot.utils.OTResult(sparse_plan=plan) + assert res.marginal_a is not None + assert res.marginal_b is not None + assert res.marginal_a.shape == (2,) + assert res.marginal_b.shape == (3,) + np.testing.assert_allclose(res.marginal_a, [0.5, 0.5]) + np.testing.assert_allclose(res.marginal_b, [0.2, 0.0, 0.8]) + + +def test_OTResult_sparse_marginals_no_densification(nx, monkeypatch): + # Duplicate entries and a rectangular shape exercise real sparse reductions. + data = np.array([0.1, 0.1, 0.3, 0.5]) + rows = np.array([0, 0, 0, 1]) + cols = np.array([0, 0, 2, 2]) + data, rows, cols = nx.from_numpy(data, rows, cols) + plan = nx.coo_matrix(data, rows, cols, shape=(2, 3), type_as=data) + res = ot.utils.OTResult(sparse_plan=plan, backend=nx) + + def no_dense_plan(*args, **kwargs): + raise AssertionError("The full sparse plan must not be densified") + + monkeypatch.setattr(nx, "todense", no_dense_plan) + assert res.marginals is not None + a, b = res.marginals + np.testing.assert_allclose(nx.to_numpy(a), [0.5, 0.5]) + np.testing.assert_allclose(nx.to_numpy(b), [0.2, 0.0, 0.8]) + + +@pytest.mark.parametrize("layout", ["csr", "csc"]) +@pytest.mark.parametrize("dtype", [np.float32, np.float64]) +def test_OTResult_compressed_sparse_marginals_torch(layout, dtype, monkeypatch): + torch = pytest.importorskip("torch") + nx = ot.backend.TorchBackend() + dense = torch.from_numpy(np.array([[0.1, 0.0, 0.2], [0.0, 0.3, 0.4]], dtype=dtype)) + plan = dense.to_sparse_csr() if layout == "csr" else dense.to_sparse_csc() + res = ot.utils.OTResult(sparse_plan=plan, backend=nx) + # Backend detection currently recognizes CSR; an explicit CSC plan also works. + assert nx.issparse(plan) == (layout == "csr") + assert res.sparse_plan is plan + + def no_dense_plan(*args, **kwargs): + raise AssertionError("The full sparse plan must not be densified") + + monkeypatch.setattr(nx, "todense", no_dense_plan) + expected_a = np.array([0.3, 0.7]) + expected_b = np.array([0.1, 0.3, 0.6]) + assert res.marginals is not None + for actual, expected in [ + (res.marginal_a, expected_a), + (res.marginal_b, expected_b), + *zip(res.marginals, (expected_a, expected_b)), + ]: + assert actual.shape == expected.shape + nx.assert_same_dtype_device(dense, actual) + np.testing.assert_allclose(nx.to_numpy(actual), expected) + + +def test_OTResult_sparse_marginals_torch_gradients(): + torch = pytest.importorskip("torch") + data = torch.tensor([0.2, 0.3, 0.5], dtype=torch.float64, requires_grad=True) + indices = torch.tensor([[0, 0, 1], [0, 2, 2]]) + plan = torch.sparse_coo_tensor(indices, data, (2, 3)) + res = ot.utils.OTResult(sparse_plan=plan, backend=ot.backend.TorchBackend()) + assert res.marginals is not None + a, b = res.marginals + loss = a @ torch.tensor([2.0, 3.0], dtype=data.dtype) + loss = loss + b @ torch.tensor([5.0, 7.0, 11.0], dtype=data.dtype) + loss.backward() + # Each transport entry contributes its row coefficient plus its column's. + np.testing.assert_allclose(data.grad, [7.0, 13.0, 14.0]) + + def test_BaryResult(): res = ot.utils.BaryResult()