deglib 0.2.7__tar.gz → 0.3.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 (141) hide show
  1. {deglib-0.2.7/src/deglib.egg-info → deglib-0.3.0}/PKG-INFO +35 -50
  2. {deglib-0.2.7 → deglib-0.3.0}/README.md +34 -49
  3. {deglib-0.2.7 → deglib-0.3.0}/lib/CMakeLists.txt +1 -1
  4. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/dataset.h +1 -1
  5. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/analysis.h +5 -3
  6. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/builder.h +4 -4
  7. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/concurrent.h +15 -12
  8. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/config.h +6 -2
  9. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/deglib.h +4 -3
  10. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/evp_ip.h +7 -217
  11. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp16.h +4 -15
  12. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp16_ip.h +14 -167
  13. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp16_l2.h +16 -169
  14. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp32_ip.h +14 -156
  15. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp32_l2.h +36 -149
  16. deglib-0.3.0/lib/deglib/include/deglib/distance/int4.h +11 -0
  17. deglib-0.3.0/lib/deglib/include/deglib/distance/int4_ip.h +241 -0
  18. deglib-0.3.0/lib/deglib/include/deglib/distance/int4_l2.h +436 -0
  19. deglib-0.3.0/lib/deglib/include/deglib/distance/int8_ip.h +477 -0
  20. deglib-0.3.0/lib/deglib/include/deglib/distance/int8_l2.h +550 -0
  21. deglib-0.3.0/lib/deglib/include/deglib/distance/uint4.h +11 -0
  22. deglib-0.3.0/lib/deglib/include/deglib/distance/uint4_ip.h +354 -0
  23. deglib-0.3.0/lib/deglib/include/deglib/distance/uint4_l2.h +362 -0
  24. deglib-0.3.0/lib/deglib/include/deglib/distance/uint7_ip.h +222 -0
  25. deglib-0.3.0/lib/deglib/include/deglib/distance/uint7_l2.h +229 -0
  26. deglib-0.3.0/lib/deglib/include/deglib/distance/uint8_ip.h +479 -0
  27. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/uint8_l2.h +16 -185
  28. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distances.h +79 -38
  29. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph/dynamic_graph.h +33 -24
  30. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph/internal_graph.h +247 -101
  31. deglib-0.3.0/lib/deglib/include/deglib/graph/readonly_graph.h +314 -0
  32. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph/sizebounded_graph.h +33 -24
  33. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph/visited_list_pool.h +4 -4
  34. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph.h +292 -248
  35. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/search/kmeans.h +226 -85
  36. deglib-0.3.0/lib/deglib/include/deglib/search/reranker.h +903 -0
  37. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/search/result_list.h +11 -0
  38. deglib-0.3.0/lib/deglib/include/deglib/search/searcher.h +925 -0
  39. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/transformation/evp.h +280 -257
  40. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/transformation/normalize.h +9 -5
  41. deglib-0.3.0/lib/deglib/include/deglib/transformation/rotation.h +413 -0
  42. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/common.h +173 -0
  43. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/fp16.h +204 -0
  44. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/int4.h +241 -0
  45. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/int8.h +212 -0
  46. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/uint4.h +430 -0
  47. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/uint7.h +209 -0
  48. deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/uint8.h +221 -0
  49. deglib-0.3.0/lib/deglib/include/deglib/transformation.h +597 -0
  50. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/utils/cpu.h +21 -5
  51. deglib-0.3.0/lib/deglib/include/deglib/utils/memory.h +83 -0
  52. {deglib-0.2.7 → deglib-0.3.0}/lib/test/CMakeLists.txt +4 -0
  53. {deglib-0.2.7 → deglib-0.3.0}/lib/test/src/common/test_helpers.h +7 -7
  54. {deglib-0.2.7 → deglib-0.3.0}/src/deg_cpp/deglib_cpp.cpp +843 -722
  55. deglib-0.3.0/src/deglib/__init__.py +125 -0
  56. {deglib-0.2.7 → deglib-0.3.0}/src/deglib/analysis.py +57 -4
  57. deglib-0.3.0/src/deglib/builder.py +470 -0
  58. deglib-0.3.0/src/deglib/cpu.py +112 -0
  59. deglib-0.3.0/src/deglib/distances.py +409 -0
  60. deglib-0.3.0/src/deglib/graph.py +799 -0
  61. deglib-0.3.0/src/deglib/optimization.py +111 -0
  62. deglib-0.3.0/src/deglib/search.py +396 -0
  63. deglib-0.3.0/src/deglib/transformation.py +220 -0
  64. {deglib-0.2.7 → deglib-0.3.0}/src/deglib/utils.py +16 -5
  65. {deglib-0.2.7 → deglib-0.3.0/src/deglib.egg-info}/PKG-INFO +35 -50
  66. {deglib-0.2.7 → deglib-0.3.0}/src/deglib.egg-info/SOURCES.txt +20 -1
  67. {deglib-0.2.7 → deglib-0.3.0}/tests/test_analysis.py +2 -1
  68. {deglib-0.2.7 → deglib-0.3.0}/tests/test_builder.py +100 -18
  69. deglib-0.3.0/tests/test_cpu.py +20 -0
  70. deglib-0.3.0/tests/test_distances.py +74 -0
  71. deglib-0.3.0/tests/test_evp_quantizer.py +90 -0
  72. {deglib-0.2.7 → deglib-0.3.0}/tests/test_filter.py +2 -2
  73. {deglib-0.2.7 → deglib-0.3.0}/tests/test_graph.py +158 -88
  74. deglib-0.3.0/tests/test_l2_normalize.py +52 -0
  75. deglib-0.3.0/tests/test_presort.py +77 -0
  76. deglib-0.3.0/tests/test_rerank.py +239 -0
  77. deglib-0.3.0/tests/test_rotation_transform.py +128 -0
  78. deglib-0.3.0/tests/test_scalar_quantizer.py +234 -0
  79. deglib-0.3.0/tests/test_scalar_quantizer_int4.py +34 -0
  80. deglib-0.3.0/tests/test_searcher.py +477 -0
  81. deglib-0.3.0/tests/test_transformation.py +63 -0
  82. {deglib-0.2.7 → deglib-0.3.0}/tests/test_transformation_graph.py +82 -97
  83. deglib-0.2.7/lib/deglib/include/deglib/distance/int8_ip.h +0 -811
  84. deglib-0.2.7/lib/deglib/include/deglib/distance/int8_l2.h +0 -422
  85. deglib-0.2.7/lib/deglib/include/deglib/distance/uint8_ip.h +0 -771
  86. deglib-0.2.7/lib/deglib/include/deglib/graph/readonly_graph.h +0 -290
  87. deglib-0.2.7/lib/deglib/include/deglib/search/reranker.h +0 -603
  88. deglib-0.2.7/lib/deglib/include/deglib/search/searcher.h +0 -698
  89. deglib-0.2.7/lib/deglib/include/deglib/transformation/scalar.h +0 -1362
  90. deglib-0.2.7/lib/deglib/include/deglib/transformation.h +0 -266
  91. deglib-0.2.7/lib/deglib/include/deglib/utils/memory.h +0 -23
  92. deglib-0.2.7/src/deglib/__init__.py +0 -83
  93. deglib-0.2.7/src/deglib/builder.py +0 -456
  94. deglib-0.2.7/src/deglib/cpu.py +0 -59
  95. deglib-0.2.7/src/deglib/distances.py +0 -149
  96. deglib-0.2.7/src/deglib/graph.py +0 -548
  97. deglib-0.2.7/src/deglib/optimization.py +0 -116
  98. deglib-0.2.7/src/deglib/search.py +0 -327
  99. deglib-0.2.7/src/deglib/transformation.py +0 -96
  100. deglib-0.2.7/tests/test_evp_quantizer.py +0 -132
  101. deglib-0.2.7/tests/test_l2_normalize.py +0 -58
  102. deglib-0.2.7/tests/test_presort.py +0 -97
  103. deglib-0.2.7/tests/test_rerank.py +0 -290
  104. deglib-0.2.7/tests/test_scalar_quantizer.py +0 -302
  105. deglib-0.2.7/tests/test_searcher.py +0 -326
  106. deglib-0.2.7/tests/test_transformation.py +0 -65
  107. {deglib-0.2.7 → deglib-0.3.0}/CMakeLists.txt +0 -0
  108. {deglib-0.2.7 → deglib-0.3.0}/LICENSE +0 -0
  109. {deglib-0.2.7 → deglib-0.3.0}/MANIFEST.in +0 -0
  110. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/CMakeLists.txt +0 -0
  111. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/benchmark.h +0 -0
  112. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/build.h +0 -0
  113. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/file_io.h +0 -0
  114. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/logging.h +0 -0
  115. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/repository.h +0 -0
  116. {deglib-0.2.7 → deglib-0.3.0}/lib/bench/include/stopwatch.h +0 -0
  117. {deglib-0.2.7 → deglib-0.3.0}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
  118. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/CMakeLists.txt +0 -0
  119. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp32.h +0 -0
  120. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/int8.h +0 -0
  121. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
  122. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/distance/uint8.h +0 -0
  123. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/filter.h +0 -0
  124. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
  125. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
  126. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +0 -0
  127. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +0 -0
  128. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
  129. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
  130. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/optimization.h +0 -0
  131. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/search.h +0 -0
  132. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/serialization.h +0 -0
  133. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/transformation/mips.h +0 -0
  134. {deglib-0.2.7 → deglib-0.3.0}/lib/deglib/include/deglib/utils/random.h +0 -0
  135. {deglib-0.2.7 → deglib-0.3.0}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
  136. {deglib-0.2.7 → deglib-0.3.0}/pyproject.toml +0 -0
  137. {deglib-0.2.7 → deglib-0.3.0}/setup.cfg +0 -0
  138. {deglib-0.2.7 → deglib-0.3.0}/setup.py +0 -0
  139. {deglib-0.2.7 → deglib-0.3.0}/src/deglib.egg-info/dependency_links.txt +0 -0
  140. {deglib-0.2.7 → deglib-0.3.0}/src/deglib.egg-info/requires.txt +0 -0
  141. {deglib-0.2.7 → deglib-0.3.0}/src/deglib.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deglib
3
- Version: 0.2.7
3
+ Version: 0.3.0
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>
@@ -90,11 +90,11 @@ num_samples, dims = 10_000, 128
90
90
  data = np.random.random((num_samples, dims)).astype(np.float32)
91
91
  query = np.random.random(dims).astype(np.float32)
92
92
 
93
- # 2. Build index directly from data (multithreaded by default)
94
- graph = deglib.builder.build_from_data(data, edges_per_vertex=32, show_progress=True)
93
+ # 2. Build index directly from data (multithreaded & optimized by default)
94
+ graph = deglib.build(data, edges_per_vertex=32, show_progress=True)
95
95
 
96
96
  # 3. Query top-k nearest neighbors
97
- indices, distances = graph.search(query, k=10, eps=0.1)
97
+ indices, distances = graph.search(query, k=10, eps_or_ef=0.1)
98
98
 
99
99
  print("Nearest neighbors:", indices)
100
100
  print("Distances:", distances)
@@ -108,13 +108,12 @@ All search graphs are represented by `DynamicExplorationGraph`, backed by one of
108
108
 
109
109
  1. **Fixed-Capacity Mutable (`SizeBoundedGraph`)**: Memory is preallocated for a fixed maximum capacity. Fast and memory-efficient for static/batch datasets.
110
110
  ```python
111
- space = deglib.FloatSpace.create(dim=128, metric=deglib.Metric.FP32_L2)
112
- graph = deglib.create_empty(capacity=10_000, feature_space=space, edges_per_vertex=32)
111
+ graph = deglib.create_empty(capacity=10_000, dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2)
113
112
  ```
114
113
 
115
114
  2. **Chunk-Allocated Mutable (`DynamicGraph`)**: Dynamically allocates memory in chunks (e.g. 1024 vertices per chunk). Ideal for streaming datasets where total capacity is unknown.
116
115
  ```python
117
- graph = deglib.create_dynamic_empty(feature_space=space, edges_per_vertex=32, chunk_size=1024)
116
+ graph = deglib.create_dynamic_empty(dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2, chunk_size=1024)
118
117
  ```
119
118
 
120
119
  3. **Read-Only Deployment (`ReadOnlyGraph`)**: Stripped of mutation structures for minimal memory footprint and maximum query throughput.
@@ -150,15 +149,14 @@ For continuous additions and removals, use `GraphBuilder`:
150
149
  import numpy as np
151
150
  import deglib
152
151
  from deglib.builder import GraphBuilder, OptimizationTarget
153
- from deglib.distances import FloatSpace, Metric
152
+ from deglib.distances import Metric
154
153
 
155
154
  dims = 128
156
155
  max_capacity = 10_000
157
156
  edges_per_vertex = 32
158
157
 
159
- # 1. Create feature space and empty mutable graph
160
- space = FloatSpace.create(dims, metric=Metric.FP32_L2)
161
- graph = deglib.create_empty(max_capacity, space, edges_per_vertex)
158
+ # 1. Create empty mutable graph
159
+ graph = deglib.create_empty(max_capacity, dims, edges_per_vertex, metric=Metric.FP32_L2)
162
160
 
163
161
  # 2. Initialize builder
164
162
  builder = GraphBuilder(graph, optimization_target=OptimizationTarget.StreamingData, seed=42)
@@ -194,7 +192,7 @@ candidates = np.random.randint(0, 1000, size=(10, 50), dtype=np.uint32)
194
192
  base_vectors = np.random.random((1000, dims)).astype(np.float32)
195
193
 
196
194
  top_indices, top_distances = rerank(
197
- space=graph.get_feature_space(),
195
+ metric=graph.get_metric(),
198
196
  queries=queries,
199
197
  candidate_indices=candidates,
200
198
  base_vectors=base_vectors,
@@ -226,55 +224,33 @@ batch_neighbors = graph.get_neighbors_batch(batch_entries, threads=4) # shape:
226
224
 
227
225
  ### Graph Optimization, Quantization & Reranking Pipeline
228
226
 
229
- A complete end-to-end pipeline demonstrating **FLAS pre-sorting**, **multithreaded graph building**, **RNG edge pruning**, **Int8 quantization**, **ReadOnlyGraph conversion**, and deployment with **`Searcher`** for automatic query quantization and high-precision reranking:
227
+ A complete end-to-end pipeline demonstrating **FLAS pre-sorting**, **multithreaded graph building**, **Int8 self-quantization**, **ReadOnlyGraph conversion**, and deployment with **`Searcher`** for automatic query quantization and high-precision reranking:
230
228
 
231
229
  ```python
232
230
  import numpy as np
233
231
  import deglib
234
- from deglib.optimization import presort, prune_non_mrng_edges
235
- from deglib.transformation import ScalarQuantizerInt8
236
- from deglib.search import create_searcher
237
232
 
238
233
  num_vectors, dims = 10_000, 128
239
234
  data = np.random.randn(num_vectors, dims).astype(np.float32)
240
235
  query = np.random.randn(dims).astype(np.float32)
241
236
 
242
- # 1. Pre-sort vectors using FLAS for improved memory locality and index construction speed
243
- perm = presort(data, metric=deglib.Metric.FP32_InnerProduct, show_progress=True)
244
- sorted_data = data[perm]
245
-
246
- # 2. Build exploration graph on unquantized/original data
247
- graph = deglib.builder.build_from_data(
248
- sorted_data,
237
+ # 1. Pre-sort with FLAS and build exploration graph in FP32
238
+ graph = deglib.build(
239
+ data,
249
240
  metric=deglib.Metric.FP32_InnerProduct,
250
241
  edges_per_vertex=32,
242
+ presort=True,
243
+ optimize=False,
251
244
  show_progress=True,
252
245
  )
253
246
 
254
- # 3. Prune redundant non-MRNG edges to optimize graph topology
255
- pruned_count = prune_non_mrng_edges(graph)
256
- print(f"Pruned {pruned_count} redundant edges.")
257
-
258
- # 4. Fit Int8 quantizer on base dataset and quantize features for memory reduction and ultra-fast search
259
- quantizer = ScalarQuantizerInt8()
260
- quantizer.fit(sorted_data)
261
- quant_int8_data = quantizer.quantize(sorted_data)
262
- int8_space = deglib.FloatSpace.create(dims, deglib.Metric.Int8_InnerProduct)
263
-
264
- # 5. Convert mutable graph to a compact ReadOnlyGraph equipped with the quantized features
265
- readonly_graph = graph.to_readonly(
266
- feature_space=int8_space, custom_features=quant_int8_data, transformations=[quantizer]
267
- )
268
-
269
- # 6. Create high-performance Searcher equipped with base vectors for exact candidate reranking
270
- rerank_space = deglib.FloatSpace.create(dims, deglib.Metric.FP32_InnerProduct)
271
- searcher = create_searcher(
272
- graph=readonly_graph,
273
- rerank_space=rerank_space,
274
- rerank_data=sorted_data,
247
+ # 2. Convert into Int8 ReadOnlyGraph and attach FP16 reranker extracted from FP32 graph
248
+ searcher = graph.to_searcher(
249
+ metric=deglib.Metric.Int8_InnerProduct,
250
+ rerank_metric=deglib.Metric.FP16_InnerProduct,
275
251
  )
276
252
 
277
- # 7. Query search (query is automatically quantized and top candidates are reranked)
253
+ # 3. Query search (query is automatically quantized and top candidates are reranked)
278
254
  indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
279
255
  print("Top-10 nearest neighbor indices:", indices)
280
256
  print("Top-10 exact distances:", distances)
@@ -300,10 +276,13 @@ Controls the topology optimization strategy:
300
276
 
301
277
  ```python
302
278
  # Mode 1: Relative distance margin (default: eps_or_ef=0.1)
303
- indices, distances = searcher.search(query, k=10, eps_or_ef=0.1)
279
+ indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, return_distances=True)
304
280
 
305
281
  # Mode 2: Fixed candidate pool (HNSW-style)
306
- indices, distances = searcher.search(query, k=10, eps_or_ef=128)
282
+ indices, distances = searcher.search(query, k=10, eps_or_ef=128, return_distances=True)
283
+
284
+ # Optional: hard per-query budget of graph distance computations (0 = unlimited)
285
+ indices = searcher.search(query, k=10, eps_or_ef=128, max_distance_computation_count=700)
307
286
  ```
308
287
 
309
288
  ### Supported Metrics & Data Types
@@ -311,11 +290,17 @@ indices, distances = searcher.search(query, k=10, eps_or_ef=128)
311
290
  - `Metric.FP32_InnerProduct`: Inner product / cosine distance (`np.float32`)
312
291
  - `Metric.Uint8_L2`: 8-bit unsigned integer Euclidean distance (`np.uint8`)
313
292
  - `Metric.Uint8_InnerProduct`: 8-bit unsigned integer inner product (`np.uint8`)
314
- - `Metric.FP16_L2`: 16-bit half-precision Euclidean distance (`np.uint16`)
315
- - `Metric.FP16_InnerProduct`: 16-bit half-precision inner product (`np.uint16`)
316
- - `Metric.EVP_InnerProduct`: Quantized Extreme Value Property bit-packed vectors (`np.uint8`)
293
+ - `Metric.FP16_L2`: 16-bit half-precision Euclidean distance (`np.float16` / `np.uint16`)
294
+ - `Metric.FP16_InnerProduct`: 16-bit half-precision inner product (`np.float16` / `np.uint16`)
295
+ - `Metric.EVP_InnerProduct`: Quantized Equi-Voronoi Polytopes (EVP) bit-packed vectors (`np.uint8`)
317
296
  - `Metric.Int8_InnerProduct`: 8-bit signed integer inner product (`np.int8`)
318
297
  - `Metric.Int8_L2`: 8-bit signed integer Euclidean distance (`np.int8`)
298
+ - `Metric.Uint4_L2`: 4-bit unsigned integer Euclidean distance, packed 2 per byte (`np.uint8`)
299
+ - `Metric.Uint4_InnerProduct`: 4-bit unsigned integer inner product, packed 2 per byte (`np.uint8`)
300
+ - `Metric.Int4_L2`: 4-bit signed integer Euclidean distance, packed 2 per byte (`np.uint8`)
301
+ - `Metric.Int4_InnerProduct`: 4-bit signed integer inner product, packed 2 per byte (`np.uint8`)
302
+ - `Metric.UInt7_L2`: 7-bit unsigned integer Euclidean distance in [0, 127] (`np.int8`)
303
+ - `Metric.UInt7_InnerProduct`: 7-bit unsigned integer inner product in [0, 127] (`np.int8`)
319
304
 
320
305
  ---
321
306
 
@@ -62,11 +62,11 @@ num_samples, dims = 10_000, 128
62
62
  data = np.random.random((num_samples, dims)).astype(np.float32)
63
63
  query = np.random.random(dims).astype(np.float32)
64
64
 
65
- # 2. Build index directly from data (multithreaded by default)
66
- graph = deglib.builder.build_from_data(data, edges_per_vertex=32, show_progress=True)
65
+ # 2. Build index directly from data (multithreaded & optimized by default)
66
+ graph = deglib.build(data, edges_per_vertex=32, show_progress=True)
67
67
 
68
68
  # 3. Query top-k nearest neighbors
69
- indices, distances = graph.search(query, k=10, eps=0.1)
69
+ indices, distances = graph.search(query, k=10, eps_or_ef=0.1)
70
70
 
71
71
  print("Nearest neighbors:", indices)
72
72
  print("Distances:", distances)
@@ -80,13 +80,12 @@ All search graphs are represented by `DynamicExplorationGraph`, backed by one of
80
80
 
81
81
  1. **Fixed-Capacity Mutable (`SizeBoundedGraph`)**: Memory is preallocated for a fixed maximum capacity. Fast and memory-efficient for static/batch datasets.
82
82
  ```python
83
- space = deglib.FloatSpace.create(dim=128, metric=deglib.Metric.FP32_L2)
84
- graph = deglib.create_empty(capacity=10_000, feature_space=space, edges_per_vertex=32)
83
+ graph = deglib.create_empty(capacity=10_000, dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2)
85
84
  ```
86
85
 
87
86
  2. **Chunk-Allocated Mutable (`DynamicGraph`)**: Dynamically allocates memory in chunks (e.g. 1024 vertices per chunk). Ideal for streaming datasets where total capacity is unknown.
88
87
  ```python
89
- graph = deglib.create_dynamic_empty(feature_space=space, edges_per_vertex=32, chunk_size=1024)
88
+ graph = deglib.create_dynamic_empty(dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2, chunk_size=1024)
90
89
  ```
91
90
 
92
91
  3. **Read-Only Deployment (`ReadOnlyGraph`)**: Stripped of mutation structures for minimal memory footprint and maximum query throughput.
@@ -122,15 +121,14 @@ For continuous additions and removals, use `GraphBuilder`:
122
121
  import numpy as np
123
122
  import deglib
124
123
  from deglib.builder import GraphBuilder, OptimizationTarget
125
- from deglib.distances import FloatSpace, Metric
124
+ from deglib.distances import Metric
126
125
 
127
126
  dims = 128
128
127
  max_capacity = 10_000
129
128
  edges_per_vertex = 32
130
129
 
131
- # 1. Create feature space and empty mutable graph
132
- space = FloatSpace.create(dims, metric=Metric.FP32_L2)
133
- graph = deglib.create_empty(max_capacity, space, edges_per_vertex)
130
+ # 1. Create empty mutable graph
131
+ graph = deglib.create_empty(max_capacity, dims, edges_per_vertex, metric=Metric.FP32_L2)
134
132
 
135
133
  # 2. Initialize builder
136
134
  builder = GraphBuilder(graph, optimization_target=OptimizationTarget.StreamingData, seed=42)
@@ -166,7 +164,7 @@ candidates = np.random.randint(0, 1000, size=(10, 50), dtype=np.uint32)
166
164
  base_vectors = np.random.random((1000, dims)).astype(np.float32)
167
165
 
168
166
  top_indices, top_distances = rerank(
169
- space=graph.get_feature_space(),
167
+ metric=graph.get_metric(),
170
168
  queries=queries,
171
169
  candidate_indices=candidates,
172
170
  base_vectors=base_vectors,
@@ -198,55 +196,33 @@ batch_neighbors = graph.get_neighbors_batch(batch_entries, threads=4) # shape:
198
196
 
199
197
  ### Graph Optimization, Quantization & Reranking Pipeline
200
198
 
201
- A complete end-to-end pipeline demonstrating **FLAS pre-sorting**, **multithreaded graph building**, **RNG edge pruning**, **Int8 quantization**, **ReadOnlyGraph conversion**, and deployment with **`Searcher`** for automatic query quantization and high-precision reranking:
199
+ A complete end-to-end pipeline demonstrating **FLAS pre-sorting**, **multithreaded graph building**, **Int8 self-quantization**, **ReadOnlyGraph conversion**, and deployment with **`Searcher`** for automatic query quantization and high-precision reranking:
202
200
 
203
201
  ```python
204
202
  import numpy as np
205
203
  import deglib
206
- from deglib.optimization import presort, prune_non_mrng_edges
207
- from deglib.transformation import ScalarQuantizerInt8
208
- from deglib.search import create_searcher
209
204
 
210
205
  num_vectors, dims = 10_000, 128
211
206
  data = np.random.randn(num_vectors, dims).astype(np.float32)
212
207
  query = np.random.randn(dims).astype(np.float32)
213
208
 
214
- # 1. Pre-sort vectors using FLAS for improved memory locality and index construction speed
215
- perm = presort(data, metric=deglib.Metric.FP32_InnerProduct, show_progress=True)
216
- sorted_data = data[perm]
217
-
218
- # 2. Build exploration graph on unquantized/original data
219
- graph = deglib.builder.build_from_data(
220
- sorted_data,
209
+ # 1. Pre-sort with FLAS and build exploration graph in FP32
210
+ graph = deglib.build(
211
+ data,
221
212
  metric=deglib.Metric.FP32_InnerProduct,
222
213
  edges_per_vertex=32,
214
+ presort=True,
215
+ optimize=False,
223
216
  show_progress=True,
224
217
  )
225
218
 
226
- # 3. Prune redundant non-MRNG edges to optimize graph topology
227
- pruned_count = prune_non_mrng_edges(graph)
228
- print(f"Pruned {pruned_count} redundant edges.")
229
-
230
- # 4. Fit Int8 quantizer on base dataset and quantize features for memory reduction and ultra-fast search
231
- quantizer = ScalarQuantizerInt8()
232
- quantizer.fit(sorted_data)
233
- quant_int8_data = quantizer.quantize(sorted_data)
234
- int8_space = deglib.FloatSpace.create(dims, deglib.Metric.Int8_InnerProduct)
235
-
236
- # 5. Convert mutable graph to a compact ReadOnlyGraph equipped with the quantized features
237
- readonly_graph = graph.to_readonly(
238
- feature_space=int8_space, custom_features=quant_int8_data, transformations=[quantizer]
239
- )
240
-
241
- # 6. Create high-performance Searcher equipped with base vectors for exact candidate reranking
242
- rerank_space = deglib.FloatSpace.create(dims, deglib.Metric.FP32_InnerProduct)
243
- searcher = create_searcher(
244
- graph=readonly_graph,
245
- rerank_space=rerank_space,
246
- rerank_data=sorted_data,
219
+ # 2. Convert into Int8 ReadOnlyGraph and attach FP16 reranker extracted from FP32 graph
220
+ searcher = graph.to_searcher(
221
+ metric=deglib.Metric.Int8_InnerProduct,
222
+ rerank_metric=deglib.Metric.FP16_InnerProduct,
247
223
  )
248
224
 
249
- # 7. Query search (query is automatically quantized and top candidates are reranked)
225
+ # 3. Query search (query is automatically quantized and top candidates are reranked)
250
226
  indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
251
227
  print("Top-10 nearest neighbor indices:", indices)
252
228
  print("Top-10 exact distances:", distances)
@@ -272,10 +248,13 @@ Controls the topology optimization strategy:
272
248
 
273
249
  ```python
274
250
  # Mode 1: Relative distance margin (default: eps_or_ef=0.1)
275
- indices, distances = searcher.search(query, k=10, eps_or_ef=0.1)
251
+ indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, return_distances=True)
276
252
 
277
253
  # Mode 2: Fixed candidate pool (HNSW-style)
278
- indices, distances = searcher.search(query, k=10, eps_or_ef=128)
254
+ indices, distances = searcher.search(query, k=10, eps_or_ef=128, return_distances=True)
255
+
256
+ # Optional: hard per-query budget of graph distance computations (0 = unlimited)
257
+ indices = searcher.search(query, k=10, eps_or_ef=128, max_distance_computation_count=700)
279
258
  ```
280
259
 
281
260
  ### Supported Metrics & Data Types
@@ -283,11 +262,17 @@ indices, distances = searcher.search(query, k=10, eps_or_ef=128)
283
262
  - `Metric.FP32_InnerProduct`: Inner product / cosine distance (`np.float32`)
284
263
  - `Metric.Uint8_L2`: 8-bit unsigned integer Euclidean distance (`np.uint8`)
285
264
  - `Metric.Uint8_InnerProduct`: 8-bit unsigned integer inner product (`np.uint8`)
286
- - `Metric.FP16_L2`: 16-bit half-precision Euclidean distance (`np.uint16`)
287
- - `Metric.FP16_InnerProduct`: 16-bit half-precision inner product (`np.uint16`)
288
- - `Metric.EVP_InnerProduct`: Quantized Extreme Value Property bit-packed vectors (`np.uint8`)
265
+ - `Metric.FP16_L2`: 16-bit half-precision Euclidean distance (`np.float16` / `np.uint16`)
266
+ - `Metric.FP16_InnerProduct`: 16-bit half-precision inner product (`np.float16` / `np.uint16`)
267
+ - `Metric.EVP_InnerProduct`: Quantized Equi-Voronoi Polytopes (EVP) bit-packed vectors (`np.uint8`)
289
268
  - `Metric.Int8_InnerProduct`: 8-bit signed integer inner product (`np.int8`)
290
269
  - `Metric.Int8_L2`: 8-bit signed integer Euclidean distance (`np.int8`)
270
+ - `Metric.Uint4_L2`: 4-bit unsigned integer Euclidean distance, packed 2 per byte (`np.uint8`)
271
+ - `Metric.Uint4_InnerProduct`: 4-bit unsigned integer inner product, packed 2 per byte (`np.uint8`)
272
+ - `Metric.Int4_L2`: 4-bit signed integer Euclidean distance, packed 2 per byte (`np.uint8`)
273
+ - `Metric.Int4_InnerProduct`: 4-bit signed integer inner product, packed 2 per byte (`np.uint8`)
274
+ - `Metric.UInt7_L2`: 7-bit unsigned integer Euclidean distance in [0, 127] (`np.int8`)
275
+ - `Metric.UInt7_InnerProduct`: 7-bit unsigned integer inner product in [0, 127] (`np.int8`)
291
276
 
292
277
  ---
293
278
 
@@ -6,7 +6,7 @@ cmake_policy(SET CMP0054 NEW)
6
6
 
7
7
  project(
8
8
  deg_lib
9
- HOMEPAGE_URL "https://github.com/Neiko2002/DEG"
9
+ HOMEPAGE_URL "https://github.com/Visual-Computing/DynamicExplorationGraph"
10
10
  DESCRIPTION
11
11
  "Dynamic Exploration Graphs for fast approximate nearest neighbor search and navigation in large image datasets"
12
12
  VERSION 0.0.1
@@ -363,7 +363,7 @@ namespace detail {
363
363
 
364
364
  inline bool setup_sift1m_files(const Dataset& ds) {
365
365
  const auto& info = ds.info();
366
- const auto archive_file = ds.dataset_dir() / "sift.tar.gz";
366
+ const auto archive_file = ds.dataset_dir() / "sift1m.tar.gz";
367
367
  const auto tmp_dir = ds.dataset_dir() / "_tmp_extract";
368
368
 
369
369
  ensure_directory(ds.dataset_dir());
@@ -493,7 +493,8 @@ static float calc_graph_quality(
493
493
  std::vector<uint32_t> nbr_labels;
494
494
  nbr_labels.reserve(edges_per_vertex);
495
495
 
496
- for (size_t v = start_idx; v < end_idx; ++v) {
496
+ for (size_t s = start_idx; s < end_idx; ++s) {
497
+ const size_t v = (total_eval < graph_size) ? ((s * graph_size) / total_eval) : s;
497
498
  const auto ext_label = graph.getExternalLabel(static_cast<uint32_t>(v));
498
499
  if (ext_label >= base_top_rows) {
499
500
  continue;
@@ -533,7 +534,7 @@ static float calc_graph_quality(
533
534
  thread_hits[thread_id] += hits;
534
535
  thread_edges[thread_id] += edges;
535
536
  }
536
- });
537
+ }, 8);
537
538
 
538
539
  uint64_t total_hits = 0;
539
540
  uint64_t total_edges = 0;
@@ -631,7 +632,8 @@ static GraphStats analyze_graph(
631
632
  if (graph_size == 0) stats.min_in_degree = 0;
632
633
 
633
634
  // Memory estimation
634
- stats.memory_bytes = stats.vertex_count * (stats.edges_per_vertex * 4 + stats.edges_per_vertex * 4 + stats.feature_dims * 4);
635
+ const size_t feature_bytes = graph.getFeatureSpace().get_data_size();
636
+ stats.memory_bytes = static_cast<size_t>(stats.vertex_count) * (static_cast<size_t>(stats.edges_per_vertex) * 8 + feature_bytes);
635
637
 
636
638
  // Reachability metrics
637
639
  uint32_t reachable = calc_search_reachability(graph);
@@ -357,11 +357,11 @@ class EvenRegularGraphBuilder {
357
357
  const size_t target_size = graph_.getFeatureSpace().get_data_size();
358
358
  std::vector<std::byte> compact(target_size);
359
359
  if constexpr (std::is_same_v<T, float>) {
360
- deglib::transformation::apply_database(transformations_, feature.data(), compact.data(), 1);
360
+ deglib::transformation::apply_on_database(transformations_, feature, std::span<std::byte>(compact));
361
361
  } else {
362
362
  std::vector<float> f_feat(feature.size());
363
363
  deglib::distances::fp16::fp16_to_floats(feature.data(), f_feat.data(), feature.size());
364
- deglib::transformation::apply_database(transformations_, f_feat.data(), compact.data(), 1);
364
+ deglib::transformation::apply_on_database(transformations_, std::span<const float>(f_feat), std::span<std::byte>(compact));
365
365
  }
366
366
  addEntry(label, std::move(compact));
367
367
  return;
@@ -389,11 +389,11 @@ class EvenRegularGraphBuilder {
389
389
  std::vector<std::byte> compact_batch(count * target_size);
390
390
 
391
391
  if constexpr (std::is_same_v<T, float>) {
392
- deglib::transformation::apply_database(transformations_, features.data(), compact_batch.data(), count);
392
+ deglib::transformation::apply_on_database(transformations_, features, std::span<std::byte>(compact_batch));
393
393
  } else {
394
394
  std::vector<float> f_feat(features.size());
395
395
  deglib::distances::fp16::fp16_to_floats(features.data(), f_feat.data(), features.size());
396
- deglib::transformation::apply_database(transformations_, f_feat.data(), compact_batch.data(), count);
396
+ deglib::transformation::apply_on_database(transformations_, std::span<const float>(f_feat), std::span<std::byte>(compact_batch));
397
397
  }
398
398
 
399
399
  for (size_t i = 0; i < count; ++i) {
@@ -70,19 +70,19 @@ inline void parallel_for(size_t start, size_t end, size_t numThreads, Function f
70
70
  // ---------------------------------------------------------------------------
71
71
  // parallel_batch_for: parallel loop over contiguous batches.
72
72
  //
73
- // Splits [start, end) into min(numThreads, end-start) contiguous chunks and
74
- // invokes
73
+ // Splits [start, end) into min(end - start, actualThreads * chunksPerThread)
74
+ // contiguous chunks and invokes
75
75
  // fn(begin, end, threadId)
76
- // exactly once per non-empty chunk, with the half-open range [begin, end).
76
+ // once per non-empty chunk, with the half-open range [begin, end) and
77
+ // threadId in [0, actualThreads).
77
78
  //
78
79
  // Unlike parallel_for (which calls fn once per element and increments a
79
80
  // shared atomic counter per element), the counter is only touched once per
80
81
  // CHUNK. On fine-grained work (short fn bodies) that removes the cache-line
81
82
  // ping-pong between workers, which is what makes element-wise loops with a
82
- // shared counter scale poorly. Chunks are still claimed dynamically by the
83
- // workers, so load stays balanced even when the work per element varies.
84
- // Contiguous chunks also give better cache locality than interleaved
85
- // element-wise claiming.
83
+ // shared counter scale poorly. When chunksPerThread > 1, chunks are claimed
84
+ // dynamically by the workers, balancing variable-cost workloads and OS
85
+ // preemption while keeping atomic contention low.
86
86
  //
87
87
  // The caller thread participates as worker 0 (like parallel_for). The range
88
88
  // [begin, end) is passed so callers can write `for (size_t i = begin; i < end;
@@ -90,7 +90,7 @@ inline void parallel_for(size_t start, size_t end, size_t numThreads, Function f
90
90
  // Exceptions thrown by fn are captured and re-thrown on the caller thread.
91
91
  // ---------------------------------------------------------------------------
92
92
  template <class Function>
93
- inline void parallel_batch_for(size_t start, size_t end, size_t numThreads, Function fn) {
93
+ inline void parallel_batch_for(size_t start, size_t end, size_t numThreads, Function fn, size_t chunksPerThread = 1) {
94
94
  if (numThreads <= 0) {
95
95
  numThreads = std::thread::hardware_concurrency();
96
96
  }
@@ -100,12 +100,15 @@ inline void parallel_batch_for(size_t start, size_t end, size_t numThreads, Func
100
100
  return;
101
101
  }
102
102
 
103
- const size_t numChunks = std::min(numThreads, count);
104
- if (numChunks == 1) {
103
+ const size_t actualThreads = std::min(numThreads, count);
104
+ if (actualThreads == 1) {
105
105
  fn(start, end, 0);
106
106
  return;
107
107
  }
108
108
 
109
+ const size_t mult = std::max<size_t>(1, chunksPerThread);
110
+ const size_t numChunks = std::min(count, actualThreads * mult);
111
+
109
112
  // Static partition of [0, count) into numChunks contiguous chunks:
110
113
  // base = count / numChunks, rem = count % numChunks
111
114
  // chunk c = [c*base + min(c, rem), c*base + min(c, rem) + base + (c < rem))
@@ -139,8 +142,8 @@ inline void parallel_batch_for(size_t start, size_t end, size_t numThreads, Func
139
142
  }
140
143
  };
141
144
 
142
- threads.reserve(numChunks - 1);
143
- for (size_t threadId = 1; threadId < numChunks; ++threadId) {
145
+ threads.reserve(actualThreads - 1);
146
+ for (size_t threadId = 1; threadId < actualThreads; ++threadId) {
144
147
  threads.emplace_back(worker, threadId);
145
148
  }
146
149
  worker(0); // caller participates
@@ -9,13 +9,17 @@
9
9
 
10
10
  // Compile methods with this attribute for AVX-512 base functions on GCC/Clang
11
11
  #if defined(DEGLIB_X86) && (defined(__GNUC__) || defined(__clang__))
12
- #define DEGLIB_TARGET_AVX512 __attribute__((target("avx512f,avx512dq,avx512bw,avx512vpopcntdq,avx2,f16c,fma")))
13
- #define DEGLIB_TARGET_AVX512_VNNI __attribute__((target("avx512f,avx512dq,avx512bw,avx512vpopcntdq,avx512vnni,avx2,f16c,fma")))
12
+ #define DEGLIB_TARGET_AVX512 __attribute__((target("avx512f,avx512dq,avx512bw,avx512vl,avx2,f16c,fma")))
13
+ #define DEGLIB_TARGET_AVX512_VPOPCNTDQ __attribute__((target("avx512f,avx512dq,avx512bw,avx512vl,avx512vpopcntdq,avx2,f16c,fma")))
14
+ #define DEGLIB_TARGET_AVX512_VNNI __attribute__((target("avx512f,avx512dq,avx512bw,avx512vl,avx512vnni,avx2,f16c,fma")))
15
+ #define DEGLIB_TARGET_AVX512_VBMI __attribute__((target("avx512f,avx512dq,avx512bw,avx512vl,avx512vpopcntdq,avx512vbmi,avx2,f16c,fma")))
14
16
  #define DEGLIB_TARGET_AVX2_VNNI __attribute__((target("avx2,f16c,fma,avxvnni")))
15
17
  #define DEGLIB_TARGET_AVX2 __attribute__((target("avx2,f16c,fma")))
16
18
  #else
17
19
  #define DEGLIB_TARGET_AVX512
20
+ #define DEGLIB_TARGET_AVX512_VPOPCNTDQ
18
21
  #define DEGLIB_TARGET_AVX512_VNNI
22
+ #define DEGLIB_TARGET_AVX512_VBMI
19
23
  #define DEGLIB_TARGET_AVX2_VNNI
20
24
  #define DEGLIB_TARGET_AVX2
21
25
  #endif
@@ -14,9 +14,6 @@
14
14
  #include "deglib/utils/random.h"
15
15
 
16
16
  namespace deglib {
17
- using builder::build_from_data;
18
- using builder::EvenRegularGraphBuilder;
19
- using builder::OptimizationTarget;
20
17
 
21
18
  /**
22
19
  * Create an EvenRegularGraphBuilder for building and incrementally modifying a DynamicExplorationGraph.
@@ -121,6 +118,10 @@ inline std::unique_ptr<deglib::search::Searcher> make_searcher(const DynamicExpl
121
118
  return std::make_unique<deglib::search::Searcher>(graph.internal(), graph.getTransformations());
122
119
  }
123
120
 
121
+ inline std::unique_ptr<deglib::search::Searcher> make_searcher(const DynamicExplorationGraph& graph, std::shared_ptr<deglib::search::Reranker> reranker) {
122
+ return std::make_unique<deglib::search::Searcher>(graph.internal(), graph.getTransformations(), std::move(reranker));
123
+ }
124
+
124
125
  inline std::unique_ptr<deglib::search::Searcher> make_searcher(const DynamicExplorationGraph& graph, deglib::search::Reranker reranker) {
125
126
  return std::make_unique<deglib::search::Searcher>(graph.internal(), graph.getTransformations(), std::move(reranker));
126
127
  }