diff --git a/RELEASES.md b/RELEASES.md index 9317ee06c..df68270dd 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -22,6 +22,7 @@ #### Closed issues +- Fix `ot.emd`, `ot.emd2` and the solvers built on them (e.g. `ot.solve`, Gromov-Wasserstein) crashing the interpreter with a segmentation fault when no entry of the cost matrix is finite; the problem is now reported as infeasible (PR #869) - 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/lp/_network_simplex.py b/ot/lp/_network_simplex.py index e8b08cdc8..892e0abfe 100644 --- a/ot/lp/_network_simplex.py +++ b/ot/lp/_network_simplex.py @@ -214,6 +214,20 @@ def _prepare_warmstart(potentials_init, need_filter, row_mask, col_mask): return alpha_init, beta_init +def _emd_c_dense(a, b, M, numItermax, numThreads, alpha_init, beta_init): + """Call :any:`emd_c`, except for a cost matrix with no finite entry + + The network simplex crashes the interpreter when every cost is NaN or + infinite. Such a problem is reported as infeasible instead, which is what + the solver already returns when only some of the costs are not finite. + """ + if M.size > 0 and not np.isfinite(M).any(): + n1, n2 = M.shape + infeasible = 0 # ProblemType.INFEASIBLE in emd_wrap + return np.zeros((n1, n2)), 0.0, np.zeros(n1), np.zeros(n2), infeasible + return emd_c(a, b, M, numItermax, numThreads, alpha_init, beta_init) + + def emd( a, b, @@ -455,7 +469,7 @@ def emd( ) # Dense solver - G, cost, u, v, result_code = emd_c( + G, cost, u, v, result_code = _emd_c_dense( a_solver, b_solver, M_solver, numItermax, numThreads, alpha_init, beta_init ) @@ -775,7 +789,7 @@ def f(b): ) # Solve dense EMD - G, cost, u, v, result_code = emd_c( + G, cost, u, v, result_code = _emd_c_dense( a_solver, b_solver, M_solver, diff --git a/test/test_ot.py b/test/test_ot.py index 0e189cc00..3d32c863c 100644 --- a/test/test_ot.py +++ b/test/test_ot.py @@ -336,6 +336,28 @@ def test_emd_empty(): np.testing.assert_allclose(w, 0) +@pytest.mark.parametrize("value", [np.nan, np.inf]) +def test_emd_no_finite_cost(value): + # the network simplex crashed the interpreter when no cost was finite; + # the problem is reported as infeasible, as for partly non-finite costs + a = ot.utils.unif(4) + b = ot.utils.unif(3) + M = np.full((4, 3), value) + + with pytest.warns(UserWarning, match="infeasible"): + G, log = ot.emd(a, b, M, log=True) + np.testing.assert_allclose(G, np.zeros((4, 3))) + assert log["result_code"] == 0 + + with pytest.warns(UserWarning, match="infeasible"): + ot.emd2(a, b, M) + + # Gromov-Wasserstein builds such a cost matrix from a NaN structure + C1 = np.full((4, 4), value) + C2 = np.ones((3, 3)) + assert np.isnan(ot.gromov.gromov_wasserstein2(C1, C2, a, b)) + + def test_emd2_multi(): n = 500 # nb bins