-
Notifications
You must be signed in to change notification settings - Fork 538
Sparse emd implementation #781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
nathanneike
wants to merge
17
commits into
PythonOT:master
from
nathanneike:sparse-emd-implementation
Closed
Sparse emd implementation #781
nathanneike
wants to merge
17
commits into
PythonOT:master
from
nathanneike:sparse-emd-implementation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Implement sparse bipartite graph EMD solver in C++
- Add Python bindings for sparse solver (emd_wrap.pyx, _network_simplex.py)
- Add unit tests to verify sparse and dense solvers produce identical results
- Tests use augmented k-NN approach to ensure fair comparison
- Update setup.py to include sparse solver compilation
Both test_emd_sparse_vs_dense() and test_emd2_sparse_vs_dense() verify:
* Identical costs between sparse and dense solvers
* Marginal constraint satisfaction for both solvers
This PR implements a sparse bipartite graph EMD solver for memory-efficient
optimal transport when the cost matrix has many infinite or forbidden edges.
Changes:
- Implement sparse bipartite graph EMD solver in C++
- Add Python bindings for sparse solver (emd_wrap.pyx, _network_simplex.py)
- Add unit tests to verify sparse and dense solvers produce identical results
- Tests use augmented k-NN approach to ensure fair comparison
Tests verify correctness:
* test_emd_sparse_vs_dense() - verifies identical costs and marginal constraints
* test_emd2_sparse_vs_dense() - verifies cost-only version
Status: WIP - seeking feedback on implementation approach
TODO: Add example script and documentation
…trix parameter from emd and fix linting issues
- Remove tuple format support for sparse matrices (use scipy.sparse only) - Change index types from int64_t to uint64_t throughout (indices are never negative) - Refactor emd() and emd2() with clear sparse/dense code path separation - Add sparse_bipartitegraph.h to MANIFEST.in to fix build - Add test_emd_sparse_backends() to verify backend compatibility
Refactor sparse optimal transport implementation to work across different backends (NumPy/scipy.sparse, PyTorch/torch.sparse). Key changes: - Add `sparse_coo_data()` method to backend layer for uniform sparse matrix handling across NumPy, PyTorch, JAX, and TensorFlow backends - Update `emd()` and `emd2()` to return transport plans in backend-native sparse format (scipy.sparse for NumPy, torch.sparse for PyTorch) - Refactor `plot2D_samples_mat()` to efficiently visualize both dense and sparse transport plans by detecting format and iterating only over non-zero entries for sparse matrices - Update `plot_sparse_emd.py` example to use new plotting function - Add comprehensive tests for sparse EMD across backends - Update documentation to reflect backend-agnostic sparse support
- Preserve PyTorch sparse tensors through numpy conversion for autograd - Verify gradient w.r.t. M equals transport plan - Add sparse backend compatibility checks and teststhrow error when unsupported backend used for sparse"
- Use sklearn.NearestNeighbors in dist_knn() (1.4x faster) - Remove redundant test code (~50 lines) - Migrate coo_matrix → coo_array - Fix parameter ordering consistency
… proper backend adaptation
…t to test coo_array functionnality
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Types of changes
Motivation and context / Related issue
Modernizes the POT backend by migrating from the deprecated
scipy.sparse.coo_matrixto the modernscipy.sparse.coo_arrayAPI. This ensures future compatibility as SciPy moves away from matrix-based sparse classes in favor of array-based ones.How has this been tested (if it applies)
coo_matrix()method to returncoo_arrayinstead ofcoo_matrixsparse_coo_data()to handle bothcoo_arrayandcoo_matrixfor backward compatibilitycoo_arrayelement-wise multiplication functionalityPR checklist