deblais 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. deblais-0.1.0/.gitignore +31 -0
  2. deblais-0.1.0/CHANGELOG.md +37 -0
  3. deblais-0.1.0/CMakeLists.txt +97 -0
  4. deblais-0.1.0/LICENSE-APACHE +202 -0
  5. deblais-0.1.0/LICENSE-MIT +21 -0
  6. deblais-0.1.0/PKG-INFO +278 -0
  7. deblais-0.1.0/README.md +241 -0
  8. deblais-0.1.0/THIRD_PARTY.md +45 -0
  9. deblais-0.1.0/cmake/deblaisConfig.cmake.in +11 -0
  10. deblais-0.1.0/examples/CMakeLists.txt +6 -0
  11. deblais-0.1.0/examples/bench.cpp +109 -0
  12. deblais-0.1.0/examples/certify.cpp +33 -0
  13. deblais-0.1.0/examples/color_transfer.py +96 -0
  14. deblais-0.1.0/examples/generative.py +125 -0
  15. deblais-0.1.0/examples/morph.py +110 -0
  16. deblais-0.1.0/examples/paris_london.py +115 -0
  17. deblais-0.1.0/examples/quickstart.cpp +31 -0
  18. deblais-0.1.0/examples/stippling.py +99 -0
  19. deblais-0.1.0/include/deblais/certificate.hpp +139 -0
  20. deblais-0.1.0/include/deblais/deblais.hpp +34 -0
  21. deblais-0.1.0/include/deblais/detail/auction.hpp +348 -0
  22. deblais-0.1.0/include/deblais/detail/cost.hpp +78 -0
  23. deblais-0.1.0/include/deblais/detail/curve.hpp +82 -0
  24. deblais-0.1.0/include/deblais/detail/line.hpp +274 -0
  25. deblais-0.1.0/include/deblais/detail/multiscale.hpp +888 -0
  26. deblais-0.1.0/include/deblais/detail/parallel.hpp +54 -0
  27. deblais-0.1.0/include/deblais/detail/pool_simplex.hpp +2599 -0
  28. deblais-0.1.0/include/deblais/detail/pricing.hpp +370 -0
  29. deblais-0.1.0/include/deblais/options.hpp +166 -0
  30. deblais-0.1.0/include/deblais/plan.hpp +102 -0
  31. deblais-0.1.0/include/deblais/solve.hpp +662 -0
  32. deblais-0.1.0/include/deblais/tune.hpp +298 -0
  33. deblais-0.1.0/include/deblais/version.hpp +24 -0
  34. deblais-0.1.0/pyproject.toml +74 -0
  35. deblais-0.1.0/python/bindings.cpp +202 -0
  36. deblais-0.1.0/python/deblais/__init__.py +31 -0
  37. deblais-0.1.0/python/deblais/_api.py +343 -0
  38. deblais-0.1.0/python/deblais/_openmp.py +71 -0
  39. deblais-0.1.0/python/deblais/py.typed +0 -0
  40. deblais-0.1.0/tests/cpp/CMakeLists.txt +13 -0
  41. deblais-0.1.0/tests/cpp/odr_a.cpp +11 -0
  42. deblais-0.1.0/tests/cpp/odr_b.cpp +7 -0
  43. deblais-0.1.0/tests/cpp/test_deblais.cpp +370 -0
  44. deblais-0.1.0/tests/python/test_numpy.py +202 -0
  45. deblais-0.1.0/tests/python/test_torch.py +101 -0
@@ -0,0 +1,31 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ .pytest_cache/
5
+ .mypy_cache/
6
+ .ruff_cache/
7
+ .venv/
8
+ *.egg-info/
9
+ .coverage
10
+ htmlcov/
11
+
12
+ # Builds
13
+ /build/
14
+ /dist/
15
+ /.cache/
16
+
17
+ # Local configuration
18
+ .env
19
+ .env.*
20
+ !.env.example
21
+ site/
22
+
23
+ # Tests
24
+ .hypothesis/
25
+
26
+ # C++ builds
27
+ /build*/
28
+ /_skbuild/
29
+ *.o
30
+ *.so
31
+ compile_commands.json
@@ -0,0 +1,37 @@
1
+ # Changelog
2
+
3
+ All notable changes to deblais. Versions follow
4
+ [semantic versioning](https://semver.org/): a major version may break existing
5
+ code, a minor version only adds features. Each section below is also the text
6
+ of the corresponding [GitHub release](https://github.com/keckj/deblais/releases).
7
+
8
+ ## 0.1.0 (2026-11-28)
9
+
10
+ First version: exact optimal transport between point clouds for the squared
11
+ Euclidean cost, as a header-only C++17 library and a Python package.
12
+
13
+ ### Added
14
+
15
+ - **C++ library** (`#include <deblais/deblais.hpp>`): `deblais::solve`
16
+ returns an optimal sparse plan, its cost and the Kantorovich potentials, in
17
+ dimension 1 and above. No dependency; OpenMP is used when available.
18
+ - **Four exact methods**: closed form on the line, network simplex with
19
+ column generation (the cost matrix is never stored), multiscale warm start,
20
+ and parallel sweeps of a domain decomposition. An auction replaces the
21
+ simplex on the levels of assignment problems; it gives the problem back to
22
+ the simplex when it starts a price war (clustered clouds).
23
+ - **Automatic mode**: the method, the arithmetic of the flows (integer or
24
+ real) and the parameters are chosen from the dimension, the sizes and the
25
+ masses. `deblais::recipe` tells what will run.
26
+ - **Tuning**: `deblais::tune` (`deblais.tune` in Python) times solves of the
27
+ given clouds and returns the fastest options on this machine (method,
28
+ threads, coarsest level of the multiscale start, auction, sweeps).
29
+ - **Certificates**: `deblais::certify` measures the marginals, the duality
30
+ gap and the dual feasibility of a plan.
31
+ - **Python package**: `deblais.solve` and `deblais.wasserstein2` for NumPy
32
+ arrays and PyTorch tensors (any device, differentiable cost), with sparse,
33
+ dense, barycentric map and assignment views of the plan.
34
+ - The Python module shares the OpenMP runtime of PyTorch when PyTorch is
35
+ installed.
36
+ - Documentation, with a page on the twin project
37
+ [remblais](https://github.com/keckj/remblais) (entropic transport on GPUs).
@@ -0,0 +1,97 @@
1
+ # SPDX-License-Identifier: MIT OR Apache-2.0
2
+ cmake_minimum_required(VERSION 3.18)
3
+
4
+ file(STRINGS include/deblais/version.hpp _deblais_version REGEX "#define DEBLAIS_VERSION_STRING")
5
+ string(REGEX REPLACE ".*\"([0-9.]+)\".*" "\\1" DEBLAIS_VERSION "${_deblais_version}")
6
+
7
+ project(deblais VERSION ${DEBLAIS_VERSION} LANGUAGES CXX
8
+ DESCRIPTION "Exact optimal transport between point clouds, header only"
9
+ HOMEPAGE_URL "https://github.com/keckj/deblais")
10
+
11
+ # Top level project (not add_subdirectory / FetchContent): tests and examples default to ON.
12
+ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR AND NOT SKBUILD)
13
+ set(_deblais_top ON)
14
+ else()
15
+ set(_deblais_top OFF)
16
+ endif()
17
+ option(DEBLAIS_BUILD_TESTS "Build the C++ tests" ${_deblais_top})
18
+ option(DEBLAIS_BUILD_EXAMPLES "Build the C++ examples" ${_deblais_top})
19
+ option(DEBLAIS_BUILD_PYTHON "Build the Python extension module" OFF)
20
+ option(DEBLAIS_OPENMP "Use OpenMP when it is available" ON)
21
+ option(DEBLAIS_NATIVE "Tune the tests, examples and Python module for this machine (-march=native)" OFF)
22
+ set(DEBLAIS_PYTHON_MAX_DIM 8 CACHE STRING "Largest dimension of the Python module")
23
+ set(DEBLAIS_ARCH "" CACHE STRING "Instruction set of the tests, examples and Python module (-march=<value>), e.g. x86-64-v3")
24
+
25
+ include(GNUInstallDirs)
26
+
27
+ # ---- The library: headers, C++17, threads, and OpenMP when found
28
+
29
+ add_library(deblais INTERFACE)
30
+ add_library(deblais::deblais ALIAS deblais)
31
+ target_compile_features(deblais INTERFACE cxx_std_17)
32
+ target_include_directories(deblais INTERFACE
33
+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
34
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
35
+
36
+ find_package(Threads REQUIRED)
37
+ target_link_libraries(deblais INTERFACE Threads::Threads)
38
+
39
+ if(DEBLAIS_OPENMP)
40
+ find_package(OpenMP COMPONENTS CXX)
41
+ if(OpenMP_CXX_FOUND)
42
+ target_link_libraries(deblais INTERFACE OpenMP::OpenMP_CXX)
43
+ else()
44
+ message(WARNING "deblais: OpenMP not found, the solvers will run on one thread")
45
+ endif()
46
+ endif()
47
+
48
+ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES AND (_deblais_top OR SKBUILD))
49
+ set(CMAKE_BUILD_TYPE Release)
50
+ endif()
51
+
52
+ function(deblais_tune target)
53
+ if(DEBLAIS_NATIVE)
54
+ target_compile_options(${target} PRIVATE -march=native)
55
+ elseif(DEBLAIS_ARCH)
56
+ target_compile_options(${target} PRIVATE -march=${DEBLAIS_ARCH})
57
+ endif()
58
+ endfunction()
59
+
60
+ # ---- Tests and examples
61
+
62
+ if(DEBLAIS_BUILD_TESTS)
63
+ enable_testing()
64
+ add_subdirectory(tests/cpp)
65
+ endif()
66
+ if(DEBLAIS_BUILD_EXAMPLES)
67
+ add_subdirectory(examples)
68
+ endif()
69
+
70
+ # ---- Python module (built by scikit-build-core: pip install .)
71
+
72
+ if(DEBLAIS_BUILD_PYTHON OR SKBUILD)
73
+ set(PYBIND11_FINDPYTHON ON)
74
+ find_package(pybind11 CONFIG REQUIRED)
75
+ pybind11_add_module(_core MODULE python/bindings.cpp)
76
+ target_link_libraries(_core PRIVATE deblais::deblais)
77
+ target_compile_definitions(_core PRIVATE DEBLAIS_MAX_DIM=${DEBLAIS_PYTHON_MAX_DIM})
78
+ deblais_tune(_core)
79
+ if(SKBUILD)
80
+ install(TARGETS _core LIBRARY DESTINATION deblais)
81
+ endif()
82
+ endif()
83
+
84
+ # ---- Install: headers and a CMake package (find_package(deblais))
85
+
86
+ if(NOT SKBUILD)
87
+ include(CMakePackageConfigHelpers)
88
+ install(DIRECTORY include/deblais DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
89
+ install(TARGETS deblais EXPORT deblaisTargets)
90
+ install(EXPORT deblaisTargets NAMESPACE deblais:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/deblais)
91
+ configure_package_config_file(cmake/deblaisConfig.cmake.in ${PROJECT_BINARY_DIR}/deblaisConfig.cmake
92
+ INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/deblais)
93
+ write_basic_package_version_file(${PROJECT_BINARY_DIR}/deblaisConfigVersion.cmake
94
+ COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT)
95
+ install(FILES ${PROJECT_BINARY_DIR}/deblaisConfig.cmake ${PROJECT_BINARY_DIR}/deblaisConfigVersion.cmake
96
+ DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/deblais)
97
+ endif()
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jean-Baptiste Keck
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
deblais-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,278 @@
1
+ Metadata-Version: 2.4
2
+ Name: deblais
3
+ Version: 0.1.0
4
+ Summary: Exact optimal transport between point clouds: a header-only C++17 network simplex with multiscale starts and domain decomposition, for NumPy and PyTorch
5
+ Keywords: optimal transport,wasserstein,earth mover's distance,network simplex,linear assignment,numpy,pytorch,openmp
6
+ Author: Jean-Baptiste Keck
7
+ License-Expression: MIT OR Apache-2.0
8
+ License-File: LICENSE-APACHE
9
+ License-File: LICENSE-MIT
10
+ License-File: THIRD_PARTY.md
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Programming Language :: C++
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Operating System :: POSIX :: Linux
17
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
18
+ Project-URL: Homepage, https://github.com/keckj/deblais
19
+ Project-URL: Documentation, https://keckj.github.io/deblais/
20
+ Project-URL: Source, https://github.com/keckj/deblais
21
+ Project-URL: Issues, https://github.com/keckj/deblais/issues
22
+ Project-URL: Changelog, https://github.com/keckj/deblais/releases
23
+ Project-URL: remblais, https://github.com/keckj/remblais
24
+ Requires-Python: >=3.10
25
+ Requires-Dist: numpy
26
+ Provides-Extra: torch
27
+ Requires-Dist: torch; extra == "torch"
28
+ Provides-Extra: test
29
+ Requires-Dist: pytest; extra == "test"
30
+ Requires-Dist: scipy; extra == "test"
31
+ Provides-Extra: docs
32
+ Requires-Dist: mkdocs-material; extra == "docs"
33
+ Requires-Dist: mkdocstrings[python]; extra == "docs"
34
+ Requires-Dist: mkdocs-glightbox; extra == "docs"
35
+ Requires-Dist: ruff; extra == "docs"
36
+ Description-Content-Type: text/markdown
37
+
38
+ <h1 align="center">
39
+ <picture>
40
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/keckj/deblais/v0.1.0/docs/logo-dark.svg">
41
+ <img alt="deblais" src="https://raw.githubusercontent.com/keckj/deblais/v0.1.0/docs/logo.svg" width="760">
42
+ </picture>
43
+ </h1>
44
+
45
+ deblais computes **exact optimal transport** between point clouds: the
46
+ optimal plan, its cost (the squared Wasserstein-2 distance) and the
47
+ Kantorovich potentials, for the squared Euclidean cost. It is a header-only
48
+ C++17 library with no dependency, and a Python package on top of it that takes
49
+ NumPy arrays and PyTorch tensors.
50
+
51
+ The solver is a network simplex that never stores the cost matrix, started by
52
+ a multiscale scheme and by parallel sweeps of a domain decomposition. It
53
+ solves a million points against a million in the plane in 13 to 31 seconds
54
+ on 8 threads depending on the masses, in linear memory, and returns a certificate of
55
+ optimality.
56
+
57
+ deblais is the twin of [remblais](https://github.com/keckj/remblais), which
58
+ computes geometric losses (Sinkhorn divergences, kernel norms, Hausdorff
59
+ divergences):
60
+ [which one do I need?](https://github.com/keckj/deblais/blob/v0.1.0/README.md#deblais-and-remblais)
61
+
62
+ - Documentation: <https://keckj.github.io/deblais/>
63
+ - Getting started: <https://keckj.github.io/deblais/getting-started/>
64
+ - Example, ten million grains from the streets of Paris to the streets of
65
+ London, in your browser: <https://keckj.github.io/deblais/demo/paris-london/>
66
+ - More examples (morphing, color transfer, stippling, a generative network
67
+ trained with the exact distance): <https://keckj.github.io/deblais/examples/>
68
+
69
+ ```python
70
+ import deblais
71
+
72
+ plan = deblais.solve(x, y) # x: (n, d), y: (m, d), NumPy or PyTorch
73
+ plan.cost # W2^2, exact
74
+ plan.source, plan.target, plan.mass # sparse optimal plan
75
+ plan.f, plan.g # dual potentials
76
+ ```
77
+
78
+ ```cpp
79
+ #include <deblais/deblais.hpp>
80
+
81
+ const deblais::Plan plan = deblais::solve(2, x, y); // std::vector<double>, point after point
82
+ ```
83
+
84
+ ## Why deblais
85
+
86
+ - **Exact.** The result is an optimal vertex of the transport polytope: no
87
+ regularization, no blur, no tolerance to tune. With uniform or integer
88
+ masses the flows are computed in integer arithmetic. The plan comes with
89
+ dual potentials that prove its optimality, and `deblais::certify` checks
90
+ them independently of the solver.
91
+ - **Fast.** The time grows almost linearly with the number of points, in
92
+ every dimension from 1 to 4. On 8 threads, a million points against a
93
+ million take 13 to 31 seconds in the plane, ten million points 4 to 10
94
+ minutes.
95
+ At 100 000 points, deblais is 450 times faster than the network simplex it
96
+ started from on one thread, and 900 times faster when both run on 16
97
+ cores. See [Performance](https://keckj.github.io/deblais/performance/).
98
+ - **Large problems.** The cost matrix is never stored: memory is linear in the
99
+ number of points. A dense solver needs 8 TB for the cost matrix of a
100
+ million points; deblais solves that problem in seconds.
101
+ - **Automatic.** `method="auto"` picks the algorithm and its parameters from
102
+ the dimension, the sizes and the masses: closed form on the line, plain
103
+ simplex on small problems, multiscale domain decomposition on large ones,
104
+ auction on assignment problems. `deblais.tune` times a few solves of your
105
+ clouds and returns the fastest options on your machine. See
106
+ [Automatic mode](https://keckj.github.io/deblais/automatic-mode/).
107
+ - **Header-only C++.** One include, C++17, no dependency. OpenMP is used when
108
+ the compiler has it. CMake target `deblais::deblais`, or just `-I include`.
109
+ - **NumPy and PyTorch.** Tensors go in on any device and come back on it. The
110
+ cost is differentiable with respect to the points and the masses. The
111
+ package shares the OpenMP runtime that PyTorch ships.
112
+ - **Permissive license.** MIT or Apache-2.0, no GPL code.
113
+
114
+ ## Installation
115
+
116
+ Python, on x86-64 Linux (Python 3.10 or later):
117
+
118
+ ```sh
119
+ pip install deblais
120
+ ```
121
+
122
+ C++, with CMake:
123
+
124
+ ```cmake
125
+ include(FetchContent)
126
+ FetchContent_Declare(deblais GIT_REPOSITORY https://github.com/keckj/deblais.git GIT_TAG v0.1.0)
127
+ FetchContent_MakeAvailable(deblais)
128
+ target_link_libraries(my_program PRIVATE deblais::deblais)
129
+ ```
130
+
131
+ or without it: copy `include/` and compile with
132
+ `g++ -std=c++17 -O3 -march=native -fopenmp -I include`. The details, and the
133
+ build from source, are in [Getting started](https://keckj.github.io/deblais/getting-started/).
134
+
135
+ ## Usage
136
+
137
+ ### Python
138
+
139
+ ```python
140
+ import numpy as np
141
+ import deblais
142
+
143
+ rng = np.random.default_rng(0)
144
+ x, y = rng.random((100_000, 3)), rng.random((80_000, 3))
145
+ a, b = rng.random(100_000), rng.random(80_000)
146
+
147
+ plan = deblais.solve(x, y, a / a.sum(), b / b.sum())
148
+
149
+ print(plan.cost, plan.status, plan.method) # exact cost, 'optimal', 'decomposition'
150
+ T = plan.map() # barycentric map, shape (100000, 3)
151
+ P = plan.sparse() # scipy.sparse matrix, at most n + m - 1 entries
152
+ ```
153
+
154
+ With PyTorch, as a loss:
155
+
156
+ ```python
157
+ import torch
158
+
159
+ x = torch.rand(20_000, 2, device="cuda", requires_grad=True)
160
+ y = torch.rand(20_000, 2, device="cuda")
161
+
162
+ loss = deblais.wasserstein2(x, y)
163
+ loss.backward()
164
+ ```
165
+
166
+ ### C++
167
+
168
+ ```cpp
169
+ #include <deblais/deblais.hpp>
170
+ #include <cstdio>
171
+ #include <vector>
172
+
173
+ // n source points and m target points in dimension 3, stored point after
174
+ // point (x0 y0 z0 x1 y1 z1 ...), with one mass per point
175
+ std::vector<double> x(3 * n), a(n), y(3 * m), b(m);
176
+
177
+ deblais::Cloud source(x.data(), n, a.data()); // views, nothing is copied
178
+ deblais::Cloud target(y.data(), m, b.data());
179
+
180
+ deblais::Options options; // method, threads, ...: all automatic by default
181
+ const deblais::Plan plan = deblais::solve<3>(source, target, options);
182
+
183
+ // The plan is a list of arcs: arc k moves plan.mass[k] from the source point
184
+ // plan.source[k] to the target point plan.target[k]
185
+ std::printf("cost %g, %zu arcs\n", plan.cost, plan.size());
186
+ for (std::size_t k = 0; k < plan.size(); ++k)
187
+ std::printf("%g from source %d to target %d\n", plan.mass[k], plan.source[k], plan.target[k]);
188
+
189
+ const deblais::Certificate proof = deblais::certify<3>(source, target, plan);
190
+ assert(proof.optimal());
191
+ ```
192
+
193
+ ## Performance
194
+
195
+ Against [network_simplex](https://github.com/nbonneel/network_simplex), the
196
+ implementation deblais started from (the one behind POT's `ot.emd`), on its
197
+ own test case (dimension 2, random masses, two AMD EPYC 9655).
198
+
199
+ network_simplex grows like $n^{2.5}$ and stores the cost matrix (80 GB at
200
+ 100 000 points); deblais grows like $n$ in linear memory, and on a single
201
+ thread it is 450 times faster at 100 000 points.
202
+
203
+ <picture>
204
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/keckj/deblais/v0.1.0/docs/perf/comparison-dark.png">
205
+ <img alt="Time of network_simplex and deblais against the number of points, log-log scale" src="https://raw.githubusercontent.com/keckj/deblais/v0.1.0/docs/perf/comparison-light.png">
206
+ </picture>
207
+
208
+ | Points per cloud | network_simplex, 1 thread | network_simplex, 16 threads | deblais, 1 thread | deblais, 16 threads |
209
+ | ---: | ---: | ---: | ---: | ---: |
210
+ | 10 000 | 4.4 s | 3.4 s | 0.21 s | 0.092 s |
211
+ | 100 000 | 1 385 s | 658 s | 3.1 s | 0.72 s |
212
+ | 1 000 000 | | | 47 s | 8.4 s |
213
+
214
+ Larger problems, in every dimension: wall time of a complete solve in the
215
+ automatic mode, optimality certificate included, for a million points
216
+ against a million drawn uniformly in the unit cube, two AMD EPYC 9654 (192
217
+ threads, pinned), GCC 14.
218
+
219
+ | Masses | Dimension 1 | Dimension 2 | Dimension 3 | Dimension 4 |
220
+ | --- | ---: | ---: | ---: | ---: |
221
+ | uniform (assignment) | 0.19 s | 14 s | 11 s | 21 s |
222
+ | random integers | 0.33 s | 25 s | 33 s | 92 s |
223
+ | random reals | 0.36 s | 7.5 s | 15 s | 49 s |
224
+
225
+ <picture>
226
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/keckj/deblais/v0.1.0/docs/perf/size-grid-dark.png">
227
+ <img alt="Time against the number of points, one panel per dimension, log-log scale" src="https://raw.githubusercontent.com/keckj/deblais/v0.1.0/docs/perf/size-grid-light.png">
228
+ </picture>
229
+
230
+ The time grows like $n^\alpha$ with the number $n$ of points per cloud,
231
+ with $\alpha$ between 1.0 and 1.3 in every dimension, up to thirty million
232
+ points (a hundred million on the line).
233
+
234
+ The protocol, the fitted exponents, the three families of masses, the
235
+ scaling with the number of threads and the command lines that reproduce
236
+ these numbers are on the [Performance](https://keckj.github.io/deblais/performance/) page.
237
+
238
+ ## deblais and remblais
239
+
240
+ deblais and [remblais](https://github.com/keckj/remblais) are twin projects:
241
+ two solvers for the two ways of computing discrete-discrete optimal transport.
242
+
243
+ | | **deblais** | **[remblais](https://github.com/keckj/remblais)** |
244
+ | --- | --- | --- |
245
+ | Problem | exact optimal transport | entropic transport: Sinkhorn divergences, kernel norms (MMD), Hausdorff |
246
+ | Algorithm | network simplex, domain decomposition | Sinkhorn iterations |
247
+ | Result | optimal sparse plan, exact cost, certificate | a smooth loss, with a blur that trades accuracy for speed |
248
+ | Runs on | CPUs | NVIDIA, AMD and Intel GPUs, several GPUs or nodes, CPUs |
249
+ | Interface | C++17 headers, NumPy, PyTorch | PyTorch, drop-in replacement of GeomLoss |
250
+
251
+ Take deblais for a plan, an assignment or a reference value; take remblais for
252
+ a loss to minimize, for other costs, unbalanced measures, images and
253
+ volumes. The page [deblais and remblais](https://keckj.github.io/deblais/remblais/) compares them in
254
+ detail and shows how to use them together.
255
+
256
+ ## About the name
257
+
258
+ In 1781, Gaspard Monge asked how to move a pile of earth, the *déblais*, into
259
+ a hole of the same volume, the *remblais*, at the lowest cost: the founding
260
+ problem of optimal transport. deblais is where the mass comes from, and
261
+ [remblais](https://github.com/keckj/remblais) is where it ends up.
262
+
263
+ ## License
264
+
265
+ At your option, [MIT](https://github.com/keckj/deblais/blob/v0.1.0/LICENSE-MIT) or [Apache-2.0](https://github.com/keckj/deblais/blob/v0.1.0/LICENSE-APACHE). deblais
266
+ contains no third-party code; the algorithms it implements are credited in
267
+ [`THIRD_PARTY.md`](https://github.com/keckj/deblais/blob/v0.1.0/THIRD_PARTY.md).
268
+
269
+ ## Related projects
270
+
271
+ - remblais, the twin project: Sinkhorn divergences, kernel norms and
272
+ Hausdorff divergences, a drop-in replacement of GeomLoss:
273
+ <https://github.com/keckj/remblais>
274
+ - network_simplex by Nicolas Bonneel, the implementation deblais started
275
+ from as a baseline: <https://github.com/nbonneel/network_simplex>
276
+ - LEMON, the graph library behind it: <https://lemon.cs.elte.hu>
277
+ - kernox, fused GPU and CPU reductions, the engine of remblais:
278
+ <https://github.com/keckj/kernox>