From 10699dcdaa95c84e7c1be087825c17a35839ce5c Mon Sep 17 00:00:00 2001 From: xihaian251 <188557784+xihaian251@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:11:26 +0800 Subject: [PATCH 1/2] fix: register correctly shaped dmmot gradients --- RELEASES.md | 1 + ot/lp/dmmot.py | 4 +++- test/test_dmmot.py | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/RELEASES.md b/RELEASES.md index 85be4654d..2209120f7 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,6 +18,7 @@ #### Closed issues +- Fix `ot.lp.dmmot_monge_1dgrid_loss` raising on backward instead of returning a gradient: it passed the input tensor and a Python list of NumPy dual vectors to `nx.set_gradients`, which expects one gradient per input in the backend's own array type. The dual potentials are now stacked into a single array matching the shape of `A`, so the gradient of column `j` is the potential of marginal `j`. Forward values and `log["dual"]` are unchanged - 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 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) diff --git a/ot/lp/dmmot.py b/ot/lp/dmmot.py index f40a99b6f..e54ef4af3 100644 --- a/ot/lp/dmmot.py +++ b/ot/lp/dmmot.py @@ -195,7 +195,9 @@ def dmmot_monge_1dgrid_loss(A, verbose=False, log=False): } # define forward/backward relations for pytorch - obj = nx.set_gradients(obj, (A_copy), (dual)) + # dual[j] is the potential of marginal j: the gradient wrt column j of A + grad_A = np.stack(dual, axis=1) + obj = nx.set_gradients(obj, (A_copy,), (nx.from_numpy(grad_A, type_as=A_copy),)) if log: return obj, log_dict diff --git a/test/test_dmmot.py b/test/test_dmmot.py index 67ac985dd..cc0ba3f2e 100644 --- a/test/test_dmmot.py +++ b/test/test_dmmot.py @@ -7,6 +7,8 @@ import numpy as np import ot +import pytest +from ot.backend import torch def create_test_data(nx): @@ -80,3 +82,27 @@ def test_dmmot_monge_1dgrid_optimize(nx): err_msg="Test failed: \ Expected vectors of all zeros", ) + + +@pytest.mark.skipif(not torch, reason="PyTorch not installed") +@pytest.mark.parametrize("coeff", [1.0, 7.0, -2.0, 0.0]) +@pytest.mark.parametrize("n,d", [(4, 2), (6, 3)]) +def test_dmmot_monge_1dgrid_loss_gradients(coeff, n, d): + rng = np.random.RandomState(0) + A = rng.rand(n, d) + 0.1 + A /= A.sum(axis=0, keepdims=True) + + _, log = ot.lp.dmmot_monge_1dgrid_loss(A, log=True) + # dual[j] is the potential of marginal j, i.e. the gradient wrt column j of A + expected = np.stack(log["dual"], axis=1) + + A_torch = torch.from_numpy(A).requires_grad_() + loss = ot.lp.dmmot_monge_1dgrid_loss(A_torch) + np.testing.assert_allclose(float(loss.detach()), log["primal objective"], rtol=1e-12) + + (coeff * loss).backward() + + assert A_torch.grad.dtype == A_torch.dtype + np.testing.assert_allclose( + A_torch.grad.numpy(), coeff * expected, atol=1e-12, rtol=1e-12 + ) From 35b3d0a785b46e974aa7af9c8927065b3b52c9af Mon Sep 17 00:00:00 2001 From: xihaian251 <188557784+xihaian251@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:13:58 +0800 Subject: [PATCH 2/2] docs: reference PR #923 in the RELEASES entry --- RELEASES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASES.md b/RELEASES.md index 2209120f7..cf02b38db 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,7 +18,7 @@ #### Closed issues -- Fix `ot.lp.dmmot_monge_1dgrid_loss` raising on backward instead of returning a gradient: it passed the input tensor and a Python list of NumPy dual vectors to `nx.set_gradients`, which expects one gradient per input in the backend's own array type. The dual potentials are now stacked into a single array matching the shape of `A`, so the gradient of column `j` is the potential of marginal `j`. Forward values and `log["dual"]` are unchanged +- Fix `ot.lp.dmmot_monge_1dgrid_loss` raising on backward instead of returning a gradient: it passed the input tensor and a Python list of NumPy dual vectors to `nx.set_gradients`, which expects one gradient per input in the backend's own array type. The dual potentials are now stacked into a single array matching the shape of `A`, so the gradient of column `j` is the potential of marginal `j`. Forward values and `log["dual"]` are unchanged (PR #923) - 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 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)