diff --git a/RELEASES.md b/RELEASES.md index b0e12bb7c..05bea49d6 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -31,6 +31,7 @@ - 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) - Allow `NumpyBackend.seed` to adopt an existing `np.random.RandomState` instance and remove NumPy-specific random sampling paths in sliced utilities (PR #849, Issue #848) +- 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) - Remove a leftover debug `print` from `ot.utils.projection_sparse_simplex` with `axis=1`, and make the `ot.datasets.make_gauss_hd` docstring a raw string so importing `ot` no longer emits a `SyntaxWarning` (PR #860) - Fix the value reported by the entropic unbalanced OT solvers (`ot.unbalanced.sinkhorn_unbalanced`, `sinkhorn_unbalanced2`, `sinkhorn_knopp_unbalanced`, `sinkhorn_stabilized_unbalanced` and `sinkhorn_unbalanced_translation_invariant`, also exposed as `ot.solve(..., reg=..., unbalanced=...).value`): every penalization is now evaluated with the generalized KL divergence, and with `reg_type="entropy"` the constant `dim_a * dim_b` is removed so that the reported value matches the documented negative entropy regularizer. The transport plans are unchanged. `reg_type` is now handled case insensitively by all the methods and an unknown value raises a `ValueError` instead of silently falling back to `"kl"`. The docstrings now state the generalized KL convention, the regularizer `Omega_reg_type(gamma, c)` with its two cases, the keys of the `log` dictionary and the multi-histogram behaviour (PR #874) - Fix `ot.dist` ignoring the weights `w` for `metric="cityblock"`, which returned the unweighted distance although the weights are documented for this metric (PR #859) 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