deglib 0.3.1__tar.gz → 0.3.3__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.
- {deglib-0.3.1/src/deglib.egg-info → deglib-0.3.3}/PKG-INFO +19 -22
- {deglib-0.3.1 → deglib-0.3.3}/README.md +18 -21
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/build.h +1 -1
- deglib-0.3.3/lib/deglib/include/deglib/concurrent.h +246 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/int4.h +1 -1
- deglib-0.3.3/lib/deglib/include/deglib/distance/int4_ip.h +289 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/int8.h +1 -1
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint4_ip.h +68 -113
- deglib-0.3.3/lib/deglib/include/deglib/distance/uint4_l1.h +236 -0
- deglib-0.3.3/lib/deglib/include/deglib/distance/uint4_l2.h +296 -0
- deglib-0.3.3/lib/deglib/include/deglib/distance/uint8_l1.h +212 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint8_l2.h +29 -40
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distances.h +26 -12
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/dynamic_graph.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/internal_graph.h +116 -63
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/readonly_graph.h +1 -2
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/sizebounded_graph.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/visited_list_pool.h +2 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/optimization.h +4 -2
- deglib-0.3.3/lib/deglib/include/deglib/search/entry_selector.h +1054 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/search/searcher.h +8 -9
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/search.h +2 -2
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/evp.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/mips.h +99 -20
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/normalize.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/rotation.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/fp16.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/int4.h +5 -3
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/int8.h +5 -3
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/uint4.h +2 -1
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/uint7.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/uint8.h +1 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation.h +10 -0
- deglib-0.3.3/lib/deglib/include/deglib/tuning.h +2497 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/utils/cpu.h +115 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/test/CMakeLists.txt +1 -2
- {deglib-0.3.1 → deglib-0.3.3}/lib/test/src/common/test_helpers.h +0 -8
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/bindings_distances.cpp +2 -2
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/bindings_graph.cpp +5 -4
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/bindings_optimization.cpp +1 -1
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/bindings_search.cpp +2 -2
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/bindings_transformation.cpp +65 -3
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/bindings_tuning.cpp +81 -20
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/__init__.py +2 -2
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/builder.py +190 -58
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/distances.py +9 -7
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/graph.py +51 -36
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/optimization.py +3 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/search.py +112 -33
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/transformation.py +12 -6
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/tuning.py +52 -14
- {deglib-0.3.1 → deglib-0.3.3/src/deglib.egg-info}/PKG-INFO +19 -22
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib.egg-info/SOURCES.txt +4 -3
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_builder.py +7 -7
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_deglib.py +30 -25
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_distances.py +28 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_graph.py +13 -17
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_l2_normalize.py +30 -2
- deglib-0.3.3/tests/test_quantization_matrix.py +353 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_rotation_transform.py +3 -1
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_searcher.py +44 -18
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_transformation_graph.py +37 -16
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_tuning.py +54 -105
- deglib-0.3.1/lib/deglib/include/deglib/concurrent.h +0 -158
- deglib-0.3.1/lib/deglib/include/deglib/distance/int4_ip.h +0 -198
- deglib-0.3.1/lib/deglib/include/deglib/distance/int4_l2.h +0 -411
- deglib-0.3.1/lib/deglib/include/deglib/distance/int8_l2.h +0 -360
- deglib-0.3.1/lib/deglib/include/deglib/distance/uint4_l2.h +0 -353
- deglib-0.3.1/lib/deglib/include/deglib/search/kmeans.h +0 -471
- deglib-0.3.1/lib/deglib/include/deglib/tuning.h +0 -1212
- {deglib-0.3.1 → deglib-0.3.3}/CMakeLists.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/LICENSE +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/MANIFEST.in +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/CMakeLists.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/CMakeLists.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/benchmark.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/dataset.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/file_io.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/logging.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/repository.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/bench/include/stopwatch.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/CMakeLists.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/analysis.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/builder.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/config.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/deglib.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/evp_ip.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp16.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp16_ip.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp16_l2.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp32.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp32_ip.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/fp32_l2.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/int8_ip.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/tail_mask.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint4.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint7_ip.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint7_l2.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint8.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/distance/uint8_ip.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/filter.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/graph.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/search/pareto_line.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/search/recall_table.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/search/reranker.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/search/result_list.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/serialization.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/transformation/scalar/common.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/utils/memory.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/deglib/include/deglib/utils/random.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/pyproject.toml +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/setup.cfg +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/setup.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deg_cpp/deglib_cpp.cpp +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/analysis.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/cpu.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib/utils.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib.egg-info/dependency_links.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib.egg-info/requires.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/src/deglib.egg-info/top_level.txt +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_analysis.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_cpu.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_evp_quantizer.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_filter.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_pareto_line.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_presort.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_recall_table.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_rerank.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_scalar_quantizer.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_scalar_quantizer_int4.py +0 -0
- {deglib-0.3.1 → deglib-0.3.3}/tests/test_transformation.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deglib
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Python bindings for the Dynamic Exploration Graph library by Nico Hezel
|
|
5
5
|
Author: Nico Hezel
|
|
6
6
|
Author-email: Bruno Schilling <bruno.schilling@htw-berlin.de>
|
|
@@ -102,14 +102,17 @@ num_samples, dims = 10_000, 128
|
|
|
102
102
|
data = np.random.random((num_samples, dims)).astype(np.float32)
|
|
103
103
|
query = np.random.random(dims).astype(np.float32)
|
|
104
104
|
|
|
105
|
-
# 2. Build index directly from data (
|
|
106
|
-
graph = deglib.build(data
|
|
105
|
+
# 2. Build index directly from data (metric="l2" by default)
|
|
106
|
+
graph = deglib.build(data)
|
|
107
107
|
|
|
108
|
-
#
|
|
108
|
+
# 3a. Query directly on the graph
|
|
109
109
|
res = graph.search(query, k=10, eps_or_ef=0.1)
|
|
110
110
|
|
|
111
|
+
# 3b. Or wrap in a Searcher for extra search optimizations (see python/API.md#deglibsearcher)
|
|
112
|
+
searcher = deglib.searcher(graph)
|
|
113
|
+
res = searcher.search(query, k=10, eps_or_ef=0.1)
|
|
114
|
+
|
|
111
115
|
print("Nearest neighbors:", res.indices)
|
|
112
|
-
print("Distances:", res.distances)
|
|
113
116
|
print("Distance evals:", res.graph_distance_count, "| Hops:", res.graph_hop_count)
|
|
114
117
|
```
|
|
115
118
|
|
|
@@ -250,25 +253,13 @@ num_vectors, dims = 10_000, 128
|
|
|
250
253
|
data = np.random.randn(num_vectors, dims).astype(np.float32)
|
|
251
254
|
query = np.random.randn(dims).astype(np.float32)
|
|
252
255
|
|
|
253
|
-
# 1.
|
|
254
|
-
graph = deglib.build(
|
|
255
|
-
data,
|
|
256
|
-
metric="cosine",
|
|
257
|
-
edges_per_vertex=32,
|
|
258
|
-
presort=True,
|
|
259
|
-
optimize=False,
|
|
260
|
-
show_progress=True,
|
|
261
|
-
)
|
|
256
|
+
# 1. Build exploration graph in FP32
|
|
257
|
+
graph = deglib.build(data, metric="cosine")
|
|
262
258
|
|
|
263
|
-
# 2.
|
|
264
|
-
|
|
265
|
-
searcher = deglib.searcher(
|
|
266
|
-
graph,
|
|
267
|
-
compress="8bit",
|
|
268
|
-
reranker=reranker,
|
|
269
|
-
)
|
|
259
|
+
# 2. Compress to 8-bit and auto-extract an FP16 reranker
|
|
260
|
+
searcher = deglib.searcher(graph, compress="8bit", reranker=True)
|
|
270
261
|
|
|
271
|
-
# 3.
|
|
262
|
+
# 3. Fetch 1.5 * k candidates from graph and re-score with reranker
|
|
272
263
|
res = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
273
264
|
print("Top-10 nearest neighbor indices:", res.indices)
|
|
274
265
|
print("Top-10 exact distances:", res.distances)
|
|
@@ -279,6 +270,12 @@ print("Graph dists:", res.graph_distance_count, "| Rerank dists:", res.rerank_di
|
|
|
279
270
|
|
|
280
271
|
## Concepts & Parameters
|
|
281
272
|
|
|
273
|
+
### Choosing `metric` (`"l2"`, `"cosine"`, `"normalized"`, `"ip"`)
|
|
274
|
+
- **`"l2"`** *(default)*: Euclidean distance — works on both unnormalized and normalized vectors.
|
|
275
|
+
- **`"cosine"`** *(unnormalized vectors)*: Direction/angle similarity — automatically normalizes database and query vectors.
|
|
276
|
+
- **`"normalized"`** *(already unit-norm vectors, `||x||_2 == 1`)*: Same similarity as `"cosine"`, without redundant normalization overhead.
|
|
277
|
+
- **`"ip"`** *(unnormalized vectors)*: Norm-sensitive Maximum Inner Product Search (MIPS).
|
|
278
|
+
|
|
282
279
|
### `OptimizationTarget`
|
|
283
280
|
Controls the topology optimization strategy:
|
|
284
281
|
- `OptimizationTarget.LowLID`: Default for datasets with low local intrinsic dimensionality (supports multithreaded building).
|
|
@@ -74,14 +74,17 @@ num_samples, dims = 10_000, 128
|
|
|
74
74
|
data = np.random.random((num_samples, dims)).astype(np.float32)
|
|
75
75
|
query = np.random.random(dims).astype(np.float32)
|
|
76
76
|
|
|
77
|
-
# 2. Build index directly from data (
|
|
78
|
-
graph = deglib.build(data
|
|
77
|
+
# 2. Build index directly from data (metric="l2" by default)
|
|
78
|
+
graph = deglib.build(data)
|
|
79
79
|
|
|
80
|
-
#
|
|
80
|
+
# 3a. Query directly on the graph
|
|
81
81
|
res = graph.search(query, k=10, eps_or_ef=0.1)
|
|
82
82
|
|
|
83
|
+
# 3b. Or wrap in a Searcher for extra search optimizations (see python/API.md#deglibsearcher)
|
|
84
|
+
searcher = deglib.searcher(graph)
|
|
85
|
+
res = searcher.search(query, k=10, eps_or_ef=0.1)
|
|
86
|
+
|
|
83
87
|
print("Nearest neighbors:", res.indices)
|
|
84
|
-
print("Distances:", res.distances)
|
|
85
88
|
print("Distance evals:", res.graph_distance_count, "| Hops:", res.graph_hop_count)
|
|
86
89
|
```
|
|
87
90
|
|
|
@@ -222,25 +225,13 @@ num_vectors, dims = 10_000, 128
|
|
|
222
225
|
data = np.random.randn(num_vectors, dims).astype(np.float32)
|
|
223
226
|
query = np.random.randn(dims).astype(np.float32)
|
|
224
227
|
|
|
225
|
-
# 1.
|
|
226
|
-
graph = deglib.build(
|
|
227
|
-
data,
|
|
228
|
-
metric="cosine",
|
|
229
|
-
edges_per_vertex=32,
|
|
230
|
-
presort=True,
|
|
231
|
-
optimize=False,
|
|
232
|
-
show_progress=True,
|
|
233
|
-
)
|
|
228
|
+
# 1. Build exploration graph in FP32
|
|
229
|
+
graph = deglib.build(data, metric="cosine")
|
|
234
230
|
|
|
235
|
-
# 2.
|
|
236
|
-
|
|
237
|
-
searcher = deglib.searcher(
|
|
238
|
-
graph,
|
|
239
|
-
compress="8bit",
|
|
240
|
-
reranker=reranker,
|
|
241
|
-
)
|
|
231
|
+
# 2. Compress to 8-bit and auto-extract an FP16 reranker
|
|
232
|
+
searcher = deglib.searcher(graph, compress="8bit", reranker=True)
|
|
242
233
|
|
|
243
|
-
# 3.
|
|
234
|
+
# 3. Fetch 1.5 * k candidates from graph and re-score with reranker
|
|
244
235
|
res = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
245
236
|
print("Top-10 nearest neighbor indices:", res.indices)
|
|
246
237
|
print("Top-10 exact distances:", res.distances)
|
|
@@ -251,6 +242,12 @@ print("Graph dists:", res.graph_distance_count, "| Rerank dists:", res.rerank_di
|
|
|
251
242
|
|
|
252
243
|
## Concepts & Parameters
|
|
253
244
|
|
|
245
|
+
### Choosing `metric` (`"l2"`, `"cosine"`, `"normalized"`, `"ip"`)
|
|
246
|
+
- **`"l2"`** *(default)*: Euclidean distance — works on both unnormalized and normalized vectors.
|
|
247
|
+
- **`"cosine"`** *(unnormalized vectors)*: Direction/angle similarity — automatically normalizes database and query vectors.
|
|
248
|
+
- **`"normalized"`** *(already unit-norm vectors, `||x||_2 == 1`)*: Same similarity as `"cosine"`, without redundant normalization overhead.
|
|
249
|
+
- **`"ip"`** *(unnormalized vectors)*: Norm-sensitive Maximum Inner Product Search (MIPS).
|
|
250
|
+
|
|
254
251
|
### `OptimizationTarget`
|
|
255
252
|
Controls the topology optimization strategy:
|
|
256
253
|
- `OptimizationTarget.LowLID`: Default for datasets with low local intrinsic dimensionality (supports multithreaded building).
|
|
@@ -80,7 +80,7 @@ inline void create_graph(
|
|
|
80
80
|
|
|
81
81
|
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, lid, k_ext, eps_ext, k_opt, eps_opt, i_opt, improve_tries);
|
|
82
82
|
builder.setThreadCount(thread_count);
|
|
83
|
-
builder.setBatchSize(
|
|
83
|
+
builder.setBatchSize(32, 10);
|
|
84
84
|
|
|
85
85
|
auto base_size = uint32_t(repository.size());
|
|
86
86
|
auto addEntry = [&builder, &repository, feature_byte_size](auto idx) {
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "deglib/config.h"
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <atomic>
|
|
7
|
+
#include <condition_variable>
|
|
8
|
+
#include <functional>
|
|
9
|
+
#include <mutex>
|
|
10
|
+
#include <thread>
|
|
11
|
+
#include <vector>
|
|
12
|
+
|
|
13
|
+
namespace deglib::concurrent {
|
|
14
|
+
|
|
15
|
+
namespace detail {
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Lightweight persistent worker pool backing `parallel_for` and `parallel_batch_for`.
|
|
19
|
+
*
|
|
20
|
+
* Keeps OS threads (and their `thread_local` workspaces) alive across calls so repeated
|
|
21
|
+
* short parallel batches avoid `std::thread` spawn/join and workspace reallocation overhead.
|
|
22
|
+
* Re-entrant or concurrent callers fall back to ad-hoc `std::thread` execution without blocking.
|
|
23
|
+
*/
|
|
24
|
+
class PersistentThreadPool {
|
|
25
|
+
public:
|
|
26
|
+
PersistentThreadPool() = default;
|
|
27
|
+
PersistentThreadPool(const PersistentThreadPool&) = delete;
|
|
28
|
+
PersistentThreadPool& operator=(const PersistentThreadPool&) = delete;
|
|
29
|
+
|
|
30
|
+
template <class WorkerFn>
|
|
31
|
+
bool try_run(size_t actual_threads, WorkerFn&& worker) {
|
|
32
|
+
if (busy_.exchange(true, std::memory_order_acquire)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
struct BusyGuard {
|
|
36
|
+
std::atomic<bool>& flag;
|
|
37
|
+
~BusyGuard() noexcept { flag.store(false, std::memory_order_release); }
|
|
38
|
+
} guard{busy_};
|
|
39
|
+
|
|
40
|
+
std::atomic<size_t> remaining(actual_threads - 1);
|
|
41
|
+
{
|
|
42
|
+
std::lock_guard<std::mutex> lk(mtx_);
|
|
43
|
+
while (workers_.size() < actual_threads - 1) {
|
|
44
|
+
const size_t tid = workers_.size() + 1;
|
|
45
|
+
workers_.emplace_back([this, tid] { worker_loop(tid); });
|
|
46
|
+
}
|
|
47
|
+
job_fn_ = [&](size_t tid) { worker(tid); };
|
|
48
|
+
active_threads_ = actual_threads;
|
|
49
|
+
remaining_ = &remaining;
|
|
50
|
+
++generation_;
|
|
51
|
+
}
|
|
52
|
+
work_cv_.notify_all();
|
|
53
|
+
|
|
54
|
+
worker(0); // caller participates as worker 0
|
|
55
|
+
|
|
56
|
+
if (remaining.load(std::memory_order_acquire) > 0) {
|
|
57
|
+
std::unique_lock<std::mutex> lk(mtx_);
|
|
58
|
+
done_cv_.wait(lk, [&] { return remaining.load(std::memory_order_acquire) == 0; });
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private:
|
|
64
|
+
void worker_loop(size_t tid) {
|
|
65
|
+
size_t my_gen = 0;
|
|
66
|
+
while (true) {
|
|
67
|
+
std::function<void(size_t)> fn;
|
|
68
|
+
std::atomic<size_t>* rem_ptr = nullptr;
|
|
69
|
+
{
|
|
70
|
+
std::unique_lock<std::mutex> lk(mtx_);
|
|
71
|
+
work_cv_.wait(lk, [&] { return generation_ != my_gen && tid < active_threads_; });
|
|
72
|
+
my_gen = generation_;
|
|
73
|
+
fn = job_fn_;
|
|
74
|
+
rem_ptr = remaining_;
|
|
75
|
+
}
|
|
76
|
+
fn(tid);
|
|
77
|
+
if (rem_ptr->fetch_sub(1, std::memory_order_acq_rel) == 1) {
|
|
78
|
+
std::lock_guard<std::mutex> lk(mtx_);
|
|
79
|
+
done_cv_.notify_one();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
std::atomic<bool> busy_{false};
|
|
85
|
+
std::mutex mtx_;
|
|
86
|
+
std::condition_variable work_cv_;
|
|
87
|
+
std::condition_variable done_cv_;
|
|
88
|
+
std::vector<std::thread> workers_;
|
|
89
|
+
std::function<void(size_t)> job_fn_;
|
|
90
|
+
std::atomic<size_t>* remaining_ = nullptr;
|
|
91
|
+
size_t active_threads_ = 1;
|
|
92
|
+
size_t generation_ = 0;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
inline PersistentThreadPool& default_pool() {
|
|
96
|
+
// Intentional process-lifetime singleton (avoids Windows DLL_PROCESS_DETACH join deadlocks).
|
|
97
|
+
static auto* pool = new PersistentThreadPool();
|
|
98
|
+
return *pool;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
template <class WorkerFn>
|
|
102
|
+
inline void run_workers(size_t actual_threads, WorkerFn&& worker) {
|
|
103
|
+
if (default_pool().try_run(actual_threads, worker)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
std::vector<std::thread> threads;
|
|
107
|
+
threads.reserve(actual_threads - 1);
|
|
108
|
+
for (size_t thread_id = 1; thread_id < actual_threads; ++thread_id) {
|
|
109
|
+
threads.emplace_back(worker, thread_id);
|
|
110
|
+
}
|
|
111
|
+
worker(0);
|
|
112
|
+
for (auto& thread : threads) {
|
|
113
|
+
thread.join();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
} // namespace detail
|
|
118
|
+
|
|
119
|
+
// Multithreaded executor
|
|
120
|
+
// The helper function copied from https://github.com/nmslib/hnswlib/blob/master/examples/cpp/example_mt_search.cpp (and that itself is copied from nmslib)
|
|
121
|
+
// An alternative is using #pragme omp parallel for or any other C++ threading
|
|
122
|
+
template <class Function>
|
|
123
|
+
inline void parallel_for(size_t start, size_t end, size_t numThreads, Function fn) {
|
|
124
|
+
if (numThreads <= 0) {
|
|
125
|
+
numThreads = std::thread::hardware_concurrency();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const size_t count = end - start;
|
|
129
|
+
if (count == 0) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const size_t actualThreads = std::min(std::max<size_t>(1, numThreads), count);
|
|
134
|
+
if (actualThreads == 1) {
|
|
135
|
+
for (size_t id = start; id < end; id++) {
|
|
136
|
+
fn(id, 0);
|
|
137
|
+
}
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
std::atomic<size_t> current(start);
|
|
142
|
+
std::exception_ptr lastException = nullptr;
|
|
143
|
+
std::mutex lastExceptMutex;
|
|
144
|
+
|
|
145
|
+
auto worker = [&](size_t threadId) {
|
|
146
|
+
while (true) {
|
|
147
|
+
const size_t id = current.fetch_add(1, std::memory_order_relaxed);
|
|
148
|
+
if (id >= end) {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
fn(id, threadId);
|
|
153
|
+
} catch (...) {
|
|
154
|
+
std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
|
|
155
|
+
lastException = std::current_exception();
|
|
156
|
+
current.store(end, std::memory_order_relaxed);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
detail::run_workers(actualThreads, worker);
|
|
163
|
+
if (lastException) {
|
|
164
|
+
std::rethrow_exception(lastException);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ---------------------------------------------------------------------------
|
|
169
|
+
// parallel_batch_for: parallel loop over contiguous batches.
|
|
170
|
+
//
|
|
171
|
+
// Splits [start, end) into min(end - start, actualThreads * chunksPerThread)
|
|
172
|
+
// contiguous chunks and invokes
|
|
173
|
+
// fn(begin, end, threadId)
|
|
174
|
+
// once per non-empty chunk, with the half-open range [begin, end) and
|
|
175
|
+
// threadId in [0, actualThreads).
|
|
176
|
+
//
|
|
177
|
+
// Unlike parallel_for (which calls fn once per element and increments a
|
|
178
|
+
// shared atomic counter per element), the counter is only touched once per
|
|
179
|
+
// CHUNK. On fine-grained work (short fn bodies) that removes the cache-line
|
|
180
|
+
// ping-pong between workers, which is what makes element-wise loops with a
|
|
181
|
+
// shared counter scale poorly. When chunksPerThread > 1, chunks are claimed
|
|
182
|
+
// dynamically by the workers, balancing variable-cost workloads and OS
|
|
183
|
+
// preemption while keeping atomic contention low.
|
|
184
|
+
//
|
|
185
|
+
// The caller thread participates as worker 0 (like parallel_for). The range
|
|
186
|
+
// [begin, end) is passed so callers can write `for (size_t i = begin; i < end;
|
|
187
|
+
// ++i) { ... }` or use std::copy_n / std::memcpy on the whole slice.
|
|
188
|
+
// Exceptions thrown by fn are captured and re-thrown on the caller thread.
|
|
189
|
+
// ---------------------------------------------------------------------------
|
|
190
|
+
template <class Function>
|
|
191
|
+
inline void parallel_batch_for(size_t start, size_t end, size_t numThreads, Function fn, size_t chunksPerThread = 1) {
|
|
192
|
+
if (numThreads <= 0) {
|
|
193
|
+
numThreads = std::thread::hardware_concurrency();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const size_t count = end - start;
|
|
197
|
+
if (count == 0) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const size_t actualThreads = std::min(std::max<size_t>(1, numThreads), count);
|
|
202
|
+
if (actualThreads == 1) {
|
|
203
|
+
fn(start, end, 0);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const size_t mult = std::max<size_t>(1, chunksPerThread);
|
|
208
|
+
const size_t numChunks = std::min(count, actualThreads * mult);
|
|
209
|
+
|
|
210
|
+
// Static partition of [0, count) into numChunks contiguous chunks:
|
|
211
|
+
// base = count / numChunks, rem = count % numChunks
|
|
212
|
+
// chunk c = [c*base + min(c, rem), c*base + min(c, rem) + base + (c < rem))
|
|
213
|
+
// The first `rem` chunks have base+1 elements, the rest have base.
|
|
214
|
+
const size_t base = count / numChunks;
|
|
215
|
+
const size_t rem = count % numChunks;
|
|
216
|
+
|
|
217
|
+
std::atomic<size_t> current(0);
|
|
218
|
+
std::exception_ptr lastException = nullptr;
|
|
219
|
+
std::mutex lastExceptMutex;
|
|
220
|
+
|
|
221
|
+
auto worker = [&](size_t threadId) {
|
|
222
|
+
while (true) {
|
|
223
|
+
const size_t c = current.fetch_add(1, std::memory_order_relaxed);
|
|
224
|
+
if (c >= numChunks) {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
const size_t cbegin = start + c * base + std::min(c, rem);
|
|
228
|
+
const size_t clen = base + (c < rem ? 1 : 0);
|
|
229
|
+
try {
|
|
230
|
+
fn(cbegin, cbegin + clen, threadId);
|
|
231
|
+
} catch (...) {
|
|
232
|
+
std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
|
|
233
|
+
lastException = std::current_exception();
|
|
234
|
+
current.store(numChunks, std::memory_order_relaxed);
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
detail::run_workers(actualThreads, worker);
|
|
241
|
+
if (lastException) {
|
|
242
|
+
std::rethrow_exception(lastException);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
} // namespace deglib::concurrent
|