Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### Closed issues

- 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 `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)
Expand Down
4 changes: 3 additions & 1 deletion ot/lp/dmmot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/test_dmmot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import numpy as np
import ot
import pytest
from ot.backend import torch


def create_test_data(nx):
Expand Down Expand Up @@ -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
)
Loading