Skip to content

[MRG] Fix segfault in ot.emd when no cost is finite - #869

Open
itzzdev09 wants to merge 1 commit into
PythonOT:masterfrom
itzzdev09:emd-all-nonfinite-cost
Open

itzzdev09 wants to merge 1 commit into
PythonOT:masterfrom
itzzdev09:emd-all-nonfinite-cost

Conversation

@itzzdev09

Copy link
Copy Markdown
Contributor

Types of changes

  • Bug fix

Description

ot.emd and ot.emd2 crash the Python interpreter with a segmentation fault when every entry of the cost matrix is NaN or infinite:

import numpy as np, ot
a, b = ot.unif(4), ot.unif(3)
ot.emd(a, b, np.full((4, 3), np.nan))   # Segmentation fault
ot.emd(a, b, np.full((4, 3), np.inf))   # Segmentation fault

Everything built on them crashes too, e.g. ot.solve, and ot.gromov.gromov_wasserstein2 with an all-NaN structure matrix. That is the case #469 reported; it was closed because a single NaN now gives a NaN result, but a fully NaN matrix still crashes.

Only the all-non-finite case crashes. With a NaN or inf row, column, block, or every entry but one, the solver returns an infeasible result (zero plan, result_code 0, the usual "Problem infeasible" warning). The dense solver call now goes through a small _emd_c_dense helper that returns that same infeasible result when no cost is finite, without calling emd_c. Every other input behaves exactly as before. With this, gromov_wasserstein2 on an all-NaN structure returns NaN, as for a single NaN.

How has this been tested

Added test_emd_no_finite_cost (NaN and inf) in test/test_ot.py. It covers emd, emd2 and gromov_wasserstein2, and it segfaults on master. test_ot.py, test_1d_solver.py, test_solvers.py and test/gromov pass (593 passed).

Checklist

  • I have added tests to cover my changes.
  • I have updated the RELEASES.md file.

@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.08%. Comparing base (367355d) to head (ac17a8a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #869      +/-   ##
==========================================
- Coverage   97.09%   97.08%   -0.01%     
==========================================
  Files         130      130              
  Lines       27229    27249      +20     
==========================================
+ Hits        26437    26456      +19     
- Misses        792      793       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@itzzdev09
itzzdev09 force-pushed the emd-all-nonfinite-cost branch from 4718c0f to ac17a8a Compare November 22, 2026 06:46
@xihaian251

Copy link
Copy Markdown
Contributor

Hi, I independently checked how #869 integrates with #904, which addresses empty-support and native-buffer safety in EMD.
I built five complete source trees (master, each PR, and both merge orders), including their C++/Cython extensions, and ran 245 subprocess-isolated checks in total.
Both merge orders produced identical final trees. The only manual conflict involved retaining both sets of appended tests in test/test_ot.py.
In our all-NaN case, master returned UNBOUNDED, while #869 and both combined trees returned INFEASIBLE; we did not reproduce a crash for that particular case. The combined trees also preserve #904's safe rejection of empty cost dimensions, and normal finite cases remain OPTIMAL.
The results support reviewing and merging the two fixes independently. Reproduction logs are available if useful.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants