deglib 0.3.1__tar.gz → 0.3.2__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 (134) hide show
  1. {deglib-0.3.1/src/deglib.egg-info → deglib-0.3.2}/PKG-INFO +19 -22
  2. {deglib-0.3.1 → deglib-0.3.2}/README.md +18 -21
  3. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distances.h +13 -1
  4. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/internal_graph.h +114 -80
  5. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search/searcher.h +1 -2
  6. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/evp.h +1 -0
  7. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/mips.h +1 -0
  8. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/normalize.h +1 -0
  9. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/rotation.h +1 -0
  10. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/fp16.h +1 -0
  11. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/int4.h +1 -0
  12. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/int8.h +1 -0
  13. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/uint4.h +1 -0
  14. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/uint7.h +1 -0
  15. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/uint8.h +1 -0
  16. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation.h +10 -0
  17. deglib-0.3.2/lib/deglib/include/deglib/tuning.h +2497 -0
  18. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/bindings_graph.cpp +1 -1
  19. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/bindings_transformation.cpp +50 -0
  20. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/bindings_tuning.cpp +81 -20
  21. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/__init__.py +2 -2
  22. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/builder.py +166 -56
  23. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/graph.py +22 -29
  24. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/search.py +89 -19
  25. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/transformation.py +6 -2
  26. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/tuning.py +52 -14
  27. {deglib-0.3.1 → deglib-0.3.2/src/deglib.egg-info}/PKG-INFO +19 -22
  28. {deglib-0.3.1 → deglib-0.3.2}/src/deglib.egg-info/SOURCES.txt +1 -0
  29. {deglib-0.3.1 → deglib-0.3.2}/tests/test_deglib.py +21 -19
  30. {deglib-0.3.1 → deglib-0.3.2}/tests/test_graph.py +1 -1
  31. {deglib-0.3.1 → deglib-0.3.2}/tests/test_l2_normalize.py +30 -2
  32. deglib-0.3.2/tests/test_quantization_matrix.py +300 -0
  33. {deglib-0.3.1 → deglib-0.3.2}/tests/test_rotation_transform.py +3 -1
  34. {deglib-0.3.1 → deglib-0.3.2}/tests/test_searcher.py +32 -4
  35. {deglib-0.3.1 → deglib-0.3.2}/tests/test_transformation_graph.py +23 -2
  36. {deglib-0.3.1 → deglib-0.3.2}/tests/test_tuning.py +54 -105
  37. deglib-0.3.1/lib/deglib/include/deglib/tuning.h +0 -1212
  38. {deglib-0.3.1 → deglib-0.3.2}/CMakeLists.txt +0 -0
  39. {deglib-0.3.1 → deglib-0.3.2}/LICENSE +0 -0
  40. {deglib-0.3.1 → deglib-0.3.2}/MANIFEST.in +0 -0
  41. {deglib-0.3.1 → deglib-0.3.2}/lib/CMakeLists.txt +0 -0
  42. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/CMakeLists.txt +0 -0
  43. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/benchmark.h +0 -0
  44. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/build.h +0 -0
  45. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/dataset.h +0 -0
  46. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/file_io.h +0 -0
  47. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/logging.h +0 -0
  48. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/repository.h +0 -0
  49. {deglib-0.3.1 → deglib-0.3.2}/lib/bench/include/stopwatch.h +0 -0
  50. {deglib-0.3.1 → deglib-0.3.2}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
  51. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/CMakeLists.txt +0 -0
  52. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/analysis.h +0 -0
  53. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/builder.h +0 -0
  54. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/concurrent.h +0 -0
  55. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/config.h +0 -0
  56. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/deglib.h +0 -0
  57. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/evp_ip.h +0 -0
  58. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp16.h +0 -0
  59. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp16_ip.h +0 -0
  60. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp16_l2.h +0 -0
  61. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp32.h +0 -0
  62. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp32_ip.h +0 -0
  63. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp32_l2.h +0 -0
  64. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int4.h +0 -0
  65. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int4_ip.h +0 -0
  66. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int4_l2.h +0 -0
  67. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int8.h +0 -0
  68. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int8_ip.h +0 -0
  69. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int8_l2.h +0 -0
  70. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
  71. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/tail_mask.h +0 -0
  72. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint4.h +0 -0
  73. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint4_ip.h +0 -0
  74. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint4_l2.h +0 -0
  75. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint7_ip.h +0 -0
  76. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint7_l2.h +0 -0
  77. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint8.h +0 -0
  78. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint8_ip.h +0 -0
  79. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint8_l2.h +0 -0
  80. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/filter.h +0 -0
  81. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/dynamic_graph.h +0 -0
  82. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
  83. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
  84. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/readonly_graph.h +0 -0
  85. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/sizebounded_graph.h +0 -0
  86. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph/visited_list_pool.h +0 -0
  87. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/graph.h +0 -0
  88. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +0 -0
  89. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +0 -0
  90. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
  91. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
  92. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/optimization.h +0 -0
  93. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search/kmeans.h +0 -0
  94. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search/pareto_line.h +0 -0
  95. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search/recall_table.h +0 -0
  96. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search/reranker.h +0 -0
  97. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search/result_list.h +0 -0
  98. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/search.h +0 -0
  99. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/serialization.h +0 -0
  100. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/common.h +0 -0
  101. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/utils/cpu.h +0 -0
  102. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/utils/memory.h +0 -0
  103. {deglib-0.3.1 → deglib-0.3.2}/lib/deglib/include/deglib/utils/random.h +0 -0
  104. {deglib-0.3.1 → deglib-0.3.2}/lib/test/CMakeLists.txt +0 -0
  105. {deglib-0.3.1 → deglib-0.3.2}/lib/test/src/common/test_helpers.h +0 -0
  106. {deglib-0.3.1 → deglib-0.3.2}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
  107. {deglib-0.3.1 → deglib-0.3.2}/pyproject.toml +0 -0
  108. {deglib-0.3.1 → deglib-0.3.2}/setup.cfg +0 -0
  109. {deglib-0.3.1 → deglib-0.3.2}/setup.py +0 -0
  110. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/bindings_distances.cpp +0 -0
  111. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/bindings_optimization.cpp +0 -0
  112. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/bindings_search.cpp +0 -0
  113. {deglib-0.3.1 → deglib-0.3.2}/src/deg_cpp/deglib_cpp.cpp +0 -0
  114. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/analysis.py +0 -0
  115. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/cpu.py +0 -0
  116. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/distances.py +0 -0
  117. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/optimization.py +0 -0
  118. {deglib-0.3.1 → deglib-0.3.2}/src/deglib/utils.py +0 -0
  119. {deglib-0.3.1 → deglib-0.3.2}/src/deglib.egg-info/dependency_links.txt +0 -0
  120. {deglib-0.3.1 → deglib-0.3.2}/src/deglib.egg-info/requires.txt +0 -0
  121. {deglib-0.3.1 → deglib-0.3.2}/src/deglib.egg-info/top_level.txt +0 -0
  122. {deglib-0.3.1 → deglib-0.3.2}/tests/test_analysis.py +0 -0
  123. {deglib-0.3.1 → deglib-0.3.2}/tests/test_builder.py +0 -0
  124. {deglib-0.3.1 → deglib-0.3.2}/tests/test_cpu.py +0 -0
  125. {deglib-0.3.1 → deglib-0.3.2}/tests/test_distances.py +0 -0
  126. {deglib-0.3.1 → deglib-0.3.2}/tests/test_evp_quantizer.py +0 -0
  127. {deglib-0.3.1 → deglib-0.3.2}/tests/test_filter.py +0 -0
  128. {deglib-0.3.1 → deglib-0.3.2}/tests/test_pareto_line.py +0 -0
  129. {deglib-0.3.1 → deglib-0.3.2}/tests/test_presort.py +0 -0
  130. {deglib-0.3.1 → deglib-0.3.2}/tests/test_recall_table.py +0 -0
  131. {deglib-0.3.1 → deglib-0.3.2}/tests/test_rerank.py +0 -0
  132. {deglib-0.3.1 → deglib-0.3.2}/tests/test_scalar_quantizer.py +0 -0
  133. {deglib-0.3.1 → deglib-0.3.2}/tests/test_scalar_quantizer_int4.py +0 -0
  134. {deglib-0.3.1 → deglib-0.3.2}/tests/test_transformation.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deglib
3
- Version: 0.3.1
3
+ Version: 0.3.2
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 (multithreaded & optimized by default)
106
- graph = deglib.build(data, edges_per_vertex=32, show_progress=True)
105
+ # 2. Build index directly from data (metric="l2" by default)
106
+ graph = deglib.build(data)
107
107
 
108
- # 3. Query top-k nearest neighbors (returns SearchResults with zero-copy .indices/.distances and telemetry)
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. Pre-sort with FLAS and build exploration graph in FP32
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. Extract FP16 reranker from FP32 graph and create compressed 8-bit Searcher
264
- reranker = deglib.reranker(graph, compress="16bit")
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. Query search (query is automatically quantized and top candidates are reranked)
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 (multithreaded & optimized by default)
78
- graph = deglib.build(data, edges_per_vertex=32, show_progress=True)
77
+ # 2. Build index directly from data (metric="l2" by default)
78
+ graph = deglib.build(data)
79
79
 
80
- # 3. Query top-k nearest neighbors (returns SearchResults with zero-copy .indices/.distances and telemetry)
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. Pre-sort with FLAS and build exploration graph in FP32
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. Extract FP16 reranker from FP32 graph and create compressed 8-bit Searcher
236
- reranker = deglib.reranker(graph, compress="16bit")
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. Query search (query is automatically quantized and top candidates are reranked)
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).
@@ -162,6 +162,11 @@ concept DistanceFunction = requires(const void* a, const void* b, const void* pa
162
162
  { T::get_instruction() } -> std::convertible_to<const char*>;
163
163
  } && std::is_convertible_v<decltype(&T::compare), DISTFUNC>;
164
164
 
165
+ template <typename Dist>
166
+ concept HasCompareBatch4 = requires(const void* q, const void* t, const void* param, float* out) {
167
+ Dist::compare_batch4(q, t, t, t, t, param, out);
168
+ };
169
+
165
170
  namespace detail {
166
171
 
167
172
  template <DistanceFunction Dist>
@@ -174,7 +179,7 @@ inline void batch_compare_impl(
174
179
  const int32_t prefetch_offset,
175
180
  const size_t prefetch_bytes
176
181
  ) {
177
- constexpr bool has_batch4 = requires { Dist::compare_batch4(query, query, query, query, query, param, out_distances); };
182
+ constexpr bool has_batch4 = HasCompareBatch4<Dist>;
178
183
  if (prefetch_offset > 0 && prefetch_bytes > 0) {
179
184
  const size_t po = static_cast<size_t>(prefetch_offset);
180
185
  const size_t warmup = std::min(po, count);
@@ -252,6 +257,7 @@ struct DistanceKernel {
252
257
  BATCH_DISTFUNC compare_batch = nullptr;
253
258
  STRIDED_DISTFUNC compare_strided = nullptr;
254
259
  const char* instruction = "Unknown";
260
+ bool has_batch4 = false;
255
261
 
256
262
  template <DistanceFunction Dist>
257
263
  static consteval DistanceKernel make() noexcept {
@@ -260,6 +266,7 @@ struct DistanceKernel {
260
266
  &detail::batch_compare_impl<Dist>,
261
267
  &detail::strided_compare_impl<Dist>,
262
268
  Dist::get_instruction(),
269
+ HasCompareBatch4<Dist>,
263
270
  };
264
271
  }
265
272
  };
@@ -394,6 +401,11 @@ class FloatSpace {
394
401
  */
395
402
  STRIDED_DISTFUNC get_strided_dist_func() const noexcept { return kernel_.compare_strided; }
396
403
 
404
+ /**
405
+ * Returns true if the selected distance kernel provides a 4-way batch comparison (compare_batch4).
406
+ */
407
+ bool has_batch4() const noexcept { return kernel_.has_batch4; }
408
+
397
409
  /**
398
410
  * Returns the instruction set name (e.g. "AVX512", "AVX2", "Scalar") used by the distance function.
399
411
  */
@@ -7,8 +7,9 @@
7
7
  #include "deglib/search/result_list.h"
8
8
 
9
9
  #include <algorithm>
10
- #include <chrono>
11
10
  #include <array>
11
+ #include <chrono>
12
+ #include <concepts>
12
13
  #include <cstddef>
13
14
  #include <cstdint>
14
15
  #include <limits>
@@ -733,6 +734,59 @@ class InternalGraph {
733
734
  return ws;
734
735
  }
735
736
 
737
+ /**
738
+ * Evaluates distances for collected neighbors using either 4-way batching (when supported by
739
+ * the kernel) or interleaved single-target prefetching, invoking `on_neighbor(neighbor_index, dist)`
740
+ * for each neighbor.
741
+ */
742
+ struct NeighborEvaluator {
743
+ const void* query;
744
+ deglib::distances::DISTFUNC dist_func;
745
+ deglib::distances::BATCH_DISTFUNC batch_dist_func;
746
+ const void* dist_param;
747
+ bool use_batch;
748
+ int32_t po;
749
+ size_t prefetch_bytes;
750
+
751
+ template <typename GraphType>
752
+ NeighborEvaluator(const GraphType& self, const void* q) noexcept
753
+ : query(q),
754
+ dist_func(self.feature_space_.get_dist_func()),
755
+ batch_dist_func(self.feature_space_.get_batch_dist_func()),
756
+ dist_param(self.feature_space_.get_dist_func_param()),
757
+ use_batch(self.feature_space_.has_batch4()),
758
+ po(self.getPo()),
759
+ prefetch_bytes(static_cast<size_t>(self.getPl()) * deglib::memory::L1_CACHE_LINE_SIZE) {}
760
+
761
+ inline void operator()(
762
+ std::span<const uint32_t> neighbors,
763
+ std::span<const void* const> features,
764
+ std::invocable<uint32_t, float> auto&& on_neighbor
765
+ ) const {
766
+ const size_t count = neighbors.size();
767
+ if (use_batch) {
768
+ alignas(64) float distances[256];
769
+ batch_dist_func(query, features.data(), count, dist_param, distances, po, prefetch_bytes);
770
+ for (size_t i = 0; i < count; ++i) {
771
+ on_neighbor(neighbors[i], distances[i]);
772
+ }
773
+ } else {
774
+ const bool do_prefetch = po > 0 && prefetch_bytes > 0;
775
+ const size_t po_u = do_prefetch ? static_cast<size_t>(po) : 0;
776
+ const size_t warmup = do_prefetch ? (std::min)(po_u, count) : 0;
777
+ for (size_t i = 0; i < warmup; ++i) {
778
+ deglib::memory::prefetch(reinterpret_cast<const char*>(features[i]), prefetch_bytes);
779
+ }
780
+ for (size_t i = 0; i < count; ++i) {
781
+ if (do_prefetch && i + po_u < count) {
782
+ deglib::memory::prefetch(reinterpret_cast<const char*>(features[i + po_u]), prefetch_bytes);
783
+ }
784
+ on_neighbor(neighbors[i], dist_func(query, features[i], dist_param));
785
+ }
786
+ }
787
+ }
788
+ };
789
+
736
790
  /**
737
791
  * Statically dispatched exploration and k-NN search implementation.
738
792
  * Inlines graph member accesses (features, neighbors, labels) via GraphType to avoid vtable calls.
@@ -829,8 +883,7 @@ class InternalGraph {
829
883
  auto radius = (results.size() < k) ? std::numeric_limits<float>::max() : results.top().getDistance();
830
884
  auto exploration_radius = (radius == std::numeric_limits<float>::max()) ? radius : radius * ((radius < 0) ? (1 - eps) : (1 + eps));
831
885
 
832
- const int32_t po = self.getPo();
833
- const size_t prefetch_bytes = static_cast<size_t>(self.getPl()) * deglib::memory::L1_CACHE_LINE_SIZE;
886
+ const NeighborEvaluator evaluate(self, query);
834
887
  const int32_t nl = self.getNl();
835
888
  const size_t edges_per_vertex = self.edges_per_vertex_;
836
889
  alignas(64) uint32_t good_neighbors[256];
@@ -868,46 +921,40 @@ class InternalGraph {
868
921
  if (good_neighbor_count <= 0) break;
869
922
  }
870
923
 
871
- const int32_t warmup = (po > 0 && prefetch_bytes > 0) ? (std::min)(po, good_neighbor_count) : 0;
872
- for (int32_t i = 0; i < warmup; ++i) {
873
- deglib::memory::prefetch(reinterpret_cast<const char*>(good_features[i]), prefetch_bytes);
874
- }
875
-
876
- for (int32_t i = 0; i < good_neighbor_count; ++i) {
877
- if (i + po < good_neighbor_count && prefetch_bytes > 0) {
878
- deglib::memory::prefetch(reinterpret_cast<const char*>(good_features[i + po]), prefetch_bytes);
879
- }
880
- const uint32_t neighbor_index = good_neighbors[i];
881
- const float neighbor_distance = dist_func(query, good_features[i], dist_func_param);
882
-
883
- // check the neighborhood of this vertex later, if its good enough
884
- if (neighbor_distance <= exploration_radius) {
885
- next_vertices.emplace(neighbor_index, neighbor_distance);
886
- deglib::memory::prefetch(
887
- reinterpret_cast<const char*>(self.neighbors_by_index(neighbor_index)), static_cast<size_t>(nl) * deglib::memory::L1_CACHE_LINE_SIZE
888
- );
924
+ const size_t n = static_cast<size_t>(good_neighbor_count);
925
+ evaluate(
926
+ {good_neighbors, n},
927
+ {good_features, n},
928
+ [&](const uint32_t neighbor_index, const float neighbor_distance) {
929
+ // check the neighborhood of this vertex later, if its good enough
930
+ if (neighbor_distance <= exploration_radius) {
931
+ next_vertices.emplace(neighbor_index, neighbor_distance);
932
+ deglib::memory::prefetch(
933
+ reinterpret_cast<const char*>(self.neighbors_by_index(neighbor_index)), static_cast<size_t>(nl) * deglib::memory::L1_CACHE_LINE_SIZE
934
+ );
889
935
 
890
- // remember the vertex, if its better than the worst in the result list
891
- if (neighbor_distance < radius) {
892
- bool insert = true;
893
- if constexpr (use_filter) {
894
- insert = filter->is_valid(self.label_by_index(neighbor_index));
895
- }
896
- if (insert) {
897
- if (results.size() < k) {
898
- results.emplace(neighbor_index, neighbor_distance);
899
- if (results.size() == k) {
900
- radius = results.top().getDistance();
936
+ // remember the vertex, if its better than the worst in the result list
937
+ if (neighbor_distance < radius) {
938
+ bool insert = true;
939
+ if constexpr (use_filter) {
940
+ insert = filter->is_valid(self.label_by_index(neighbor_index));
941
+ }
942
+ if (insert) {
943
+ if (results.size() < k) {
944
+ results.emplace(neighbor_index, neighbor_distance);
945
+ if (results.size() == k) {
946
+ radius = results.top().getDistance();
947
+ exploration_radius = radius * ((radius < 0) ? (1 - eps) : (1 + eps));
948
+ }
949
+ } else {
950
+ radius = results.replace_top(neighbor_index, neighbor_distance).getDistance();
901
951
  exploration_radius = radius * ((radius < 0) ? (1 - eps) : (1 + eps));
902
952
  }
903
- } else {
904
- radius = results.replace_top(neighbor_index, neighbor_distance).getDistance();
905
- exploration_radius = radius * ((radius < 0) ? (1 - eps) : (1 + eps));
906
953
  }
907
954
  }
908
955
  }
909
956
  }
910
- }
957
+ );
911
958
 
912
959
  dist_count += static_cast<uint32_t>(good_neighbor_count);
913
960
  if constexpr (use_max_distance_count) {
@@ -1019,8 +1066,7 @@ class InternalGraph {
1019
1066
  }
1020
1067
  }
1021
1068
 
1022
- const int32_t po = self.getPo();
1023
- const size_t prefetch_bytes = static_cast<size_t>(self.getPl()) * deglib::memory::L1_CACHE_LINE_SIZE;
1069
+ const NeighborEvaluator evaluate(self, query);
1024
1070
  const int32_t nl = self.getNl();
1025
1071
  const size_t edges_per_vertex = self.edges_per_vertex_;
1026
1072
  alignas(64) uint32_t good_neighbors[256];
@@ -1061,32 +1107,26 @@ class InternalGraph {
1061
1107
  if (good_neighbor_count <= 0) break;
1062
1108
  }
1063
1109
 
1064
- const int32_t warmup = (po > 0 && prefetch_bytes > 0) ? (std::min)(po, good_neighbor_count) : 0;
1065
- for (int32_t i = 0; i < warmup; ++i) {
1066
- deglib::memory::prefetch(reinterpret_cast<const char*>(good_features[i]), prefetch_bytes);
1067
- }
1068
-
1069
- for (int32_t i = 0; i < good_neighbor_count; ++i) {
1070
- if (i + po < good_neighbor_count && prefetch_bytes > 0) {
1071
- deglib::memory::prefetch(reinterpret_cast<const char*>(good_features[i + po]), prefetch_bytes);
1072
- }
1073
- const uint32_t neighbor_index = good_neighbors[i];
1074
- const float dist = dist_func(query, good_features[i], dist_func_param);
1075
-
1076
- // check the neighborhood of this vertex later, if its good enough
1077
- if (dist <= radius) {
1078
- next_vertices.emplace(neighbor_index, dist);
1079
- deglib::memory::prefetch(
1080
- reinterpret_cast<const char*>(self.neighbors_by_index(neighbor_index)), static_cast<size_t>(nl) * deglib::memory::L1_CACHE_LINE_SIZE
1081
- );
1082
- }
1110
+ const size_t n = static_cast<size_t>(good_neighbor_count);
1111
+ evaluate(
1112
+ {good_neighbors, n},
1113
+ {good_features, n},
1114
+ [&](const uint32_t neighbor_index, const float dist) {
1115
+ // check the neighborhood of this vertex later, if its good enough
1116
+ if (dist <= radius) {
1117
+ next_vertices.emplace(neighbor_index, dist);
1118
+ deglib::memory::prefetch(
1119
+ reinterpret_cast<const char*>(self.neighbors_by_index(neighbor_index)), static_cast<size_t>(nl) * deglib::memory::L1_CACHE_LINE_SIZE
1120
+ );
1121
+ }
1083
1122
 
1084
- if constexpr (use_filter) {
1085
- if (filter->is_valid(self.label_by_index(neighbor_index))) valid_results.insert(neighbor_index, dist);
1086
- } else {
1087
- valid_results.insert(neighbor_index, dist);
1123
+ if constexpr (use_filter) {
1124
+ if (filter->is_valid(self.label_by_index(neighbor_index))) valid_results.insert(neighbor_index, dist);
1125
+ } else {
1126
+ valid_results.insert(neighbor_index, dist);
1127
+ }
1088
1128
  }
1089
- }
1129
+ );
1090
1130
 
1091
1131
  dist_count += static_cast<uint32_t>(good_neighbor_count);
1092
1132
  if constexpr (use_max_distance_count) {
@@ -1169,8 +1209,7 @@ class InternalGraph {
1169
1209
  }
1170
1210
  }
1171
1211
 
1172
- const int32_t po = self.getPo();
1173
- const size_t prefetch_bytes = static_cast<size_t>(self.getPl()) * deglib::memory::L1_CACHE_LINE_SIZE;
1212
+ const NeighborEvaluator evaluate(self, query);
1174
1213
  const int32_t nl = self.getNl();
1175
1214
  const size_t edges_per_vertex = self.edges_per_vertex_;
1176
1215
  alignas(64) uint32_t good_neighbors[256];
@@ -1204,23 +1243,18 @@ class InternalGraph {
1204
1243
  if (good_neighbor_count <= 0) break;
1205
1244
  }
1206
1245
 
1207
- const int32_t warmup = (po > 0 && prefetch_bytes > 0) ? (std::min)(po, good_neighbor_count) : 0;
1208
- for (int32_t i = 0; i < warmup; ++i) {
1209
- deglib::memory::prefetch(reinterpret_cast<const char*>(good_features[i]), prefetch_bytes);
1210
- }
1211
-
1212
- for (int32_t i = 0; i < good_neighbor_count; ++i) {
1213
- if (i + po < good_neighbor_count && prefetch_bytes > 0) {
1214
- deglib::memory::prefetch(reinterpret_cast<const char*>(good_features[i + po]), prefetch_bytes);
1215
- }
1216
- const uint32_t neighbor_index = good_neighbors[i];
1217
- const float neighbor_distance = dist_func(query, good_features[i], dist_func_param);
1218
- if (pool.insert(neighbor_index, neighbor_distance)) {
1219
- deglib::memory::prefetch(
1220
- reinterpret_cast<const char*>(self.neighbors_by_index(neighbor_index)), static_cast<size_t>(nl) * deglib::memory::L1_CACHE_LINE_SIZE
1221
- );
1246
+ const size_t n = static_cast<size_t>(good_neighbor_count);
1247
+ evaluate(
1248
+ {good_neighbors, n},
1249
+ {good_features, n},
1250
+ [&](const uint32_t neighbor_index, const float neighbor_distance) {
1251
+ if (pool.insert(neighbor_index, neighbor_distance)) {
1252
+ deglib::memory::prefetch(
1253
+ reinterpret_cast<const char*>(self.neighbors_by_index(neighbor_index)), static_cast<size_t>(nl) * deglib::memory::L1_CACHE_LINE_SIZE
1254
+ );
1255
+ }
1222
1256
  }
1223
- }
1257
+ );
1224
1258
 
1225
1259
  dist_count += static_cast<uint32_t>(good_neighbor_count);
1226
1260
  if constexpr (use_max_distance_count) {
@@ -69,8 +69,7 @@ class Searcher {
69
69
  bool normalize_query_ = false;
70
70
 
71
71
  static inline bool is_spatial_prefix_transform(const deglib::transformation::FeatureTransformation& t) noexcept {
72
- const auto n = t.name();
73
- return n == "L2NormalizeTransform" || n == "MipsTransform";
72
+ return t.type() == deglib::transformation::TransformationType::Spatial;
74
73
  }
75
74
 
76
75
  void split_shared_prefix_transforms() {
@@ -693,6 +693,7 @@ class EvpQuantizer : public FeatureTransformation {
693
693
  explicit EvpQuantizer(uint32_t dim, uint32_t non_zeros = 0) : dim_(dim), non_zeros_(non_zeros) {}
694
694
 
695
695
  uint32_t tag() const noexcept override { return 0x51505645; } // 'EVPQ'
696
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
696
697
  std::string_view name() const noexcept override { return "EvpQuantizer"; }
697
698
  uint32_t input_dim() const noexcept override { return dim_; }
698
699
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -130,6 +130,7 @@ class MipsTransform : public FeatureTransformation {
130
130
  MipsTransform(uint32_t dim, float max_norm = 0.0f) : dim_(dim), max_norm_(max_norm) {}
131
131
 
132
132
  uint32_t tag() const noexcept override { return 0x5350494D; } // 'MIPS'
133
+ TransformationType type() const noexcept override { return TransformationType::Spatial; }
133
134
  std::string_view name() const noexcept override { return "MipsTransform"; }
134
135
  uint32_t input_dim() const noexcept override { return dim_; }
135
136
  uint32_t output_dim() const noexcept override { return dim_ + 1; }
@@ -57,6 +57,7 @@ class L2NormalizeTransform : public FeatureTransformation {
57
57
  void set_dim(uint32_t d) noexcept { dim_ = d; }
58
58
 
59
59
  uint32_t tag() const noexcept override { return TAG_NORM; }
60
+ TransformationType type() const noexcept override { return TransformationType::Spatial; }
60
61
  std::string_view name() const noexcept override { return "L2NormalizeTransform"; }
61
62
  uint32_t input_dim() const noexcept override { return dim_; }
62
63
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -212,6 +212,7 @@ class FastHadamardRotation : public FeatureTransformation {
212
212
  }
213
213
 
214
214
  uint32_t tag() const noexcept override { return TAG_FWHT; }
215
+ TransformationType type() const noexcept override { return TransformationType::Rotation; }
215
216
  std::string_view name() const noexcept override { return "FastHadamardRotation"; }
216
217
  uint32_t input_dim() const noexcept override { return dim_; }
217
218
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -29,6 +29,7 @@ class ScalarQuantizerFP16 : public FeatureTransformation {
29
29
  ScalarQuantizerFP16(uint32_t dim, float abs_max) : dim_(dim) { set_abs_max(abs_max); }
30
30
 
31
31
  uint32_t tag() const noexcept override { return TAG_SQ16; }
32
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
32
33
  std::string_view name() const noexcept override { return "ScalarQuantizerFP16"; }
33
34
  uint32_t input_dim() const noexcept override { return dim_; }
34
35
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -29,6 +29,7 @@ class ScalarQuantizerInt4 : public FeatureTransformation {
29
29
  explicit ScalarQuantizerInt4(uint32_t dim) : dim_(dim) {}
30
30
 
31
31
  uint32_t tag() const noexcept override { return TAG_SQI4; }
32
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
32
33
  std::string_view name() const noexcept override { return "ScalarQuantizerInt4"; }
33
34
  uint32_t input_dim() const noexcept override { return dim_; }
34
35
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -26,6 +26,7 @@ class ScalarQuantizerInt8 : public FeatureTransformation {
26
26
  explicit ScalarQuantizerInt8(uint32_t dim) : dim_(dim) {}
27
27
 
28
28
  uint32_t tag() const noexcept override { return TAG_SQI8; }
29
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
29
30
  std::string_view name() const noexcept override { return "ScalarQuantizerInt8"; }
30
31
  uint32_t input_dim() const noexcept override { return dim_; }
31
32
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -190,6 +190,7 @@ class ScalarQuantizerUint4 : public FeatureTransformation {
190
190
  explicit ScalarQuantizerUint4(uint32_t dim) : dim_(dim) {}
191
191
 
192
192
  uint32_t tag() const noexcept override { return TAG_SQU4; }
193
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
193
194
  std::string_view name() const noexcept override { return "ScalarQuantizerUint4"; }
194
195
  uint32_t input_dim() const noexcept override { return dim_; }
195
196
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -25,6 +25,7 @@ class ScalarQuantizerUint7 : public FeatureTransformation {
25
25
  explicit ScalarQuantizerUint7(uint32_t dim) : dim_(dim) {}
26
26
 
27
27
  uint32_t tag() const noexcept override { return TAG_SQU7; }
28
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
28
29
  std::string_view name() const noexcept override { return "ScalarQuantizerUint7"; }
29
30
  uint32_t input_dim() const noexcept override { return dim_; }
30
31
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -26,6 +26,7 @@ class ScalarQuantizerUint8 : public FeatureTransformation {
26
26
  explicit ScalarQuantizerUint8(uint32_t dim) : dim_(dim) {}
27
27
 
28
28
  uint32_t tag() const noexcept override { return TAG_SQU8; }
29
+ TransformationType type() const noexcept override { return TransformationType::Quantizer; }
29
30
  std::string_view name() const noexcept override { return "ScalarQuantizerUint8"; }
30
31
  uint32_t input_dim() const noexcept override { return dim_; }
31
32
  uint32_t output_dim() const noexcept override { return dim_; }
@@ -31,6 +31,15 @@ inline constexpr uint32_t TAG_EVPQ = uint32_t('E') | (uint32_t('V') << 8) | (uin
31
31
  inline constexpr uint32_t TAG_NORM = uint32_t('N') | (uint32_t('O') << 8) | (uint32_t('R') << 16) | (uint32_t('M') << 24);
32
32
  inline constexpr uint32_t TAG_FWHT = uint32_t('F') | (uint32_t('W') << 8) | (uint32_t('H') << 16) | (uint32_t('T') << 24);
33
33
 
34
+ /**
35
+ * @brief Semantic category of a FeatureTransformation stage.
36
+ */
37
+ enum class TransformationType : uint8_t {
38
+ Spatial, ///< Space-preserving or space-lifting prefix transforms (e.g. L2NormalizeTransform, MipsTransform)
39
+ Rotation, ///< Orthogonal pre-quantization transforms (e.g. FastHadamardRotation)
40
+ Quantizer, ///< Precision-reducing quantizers (e.g. ScalarQuantizer*, EvpQuantizer)
41
+ };
42
+
34
43
  /**
35
44
  * @brief Abstract base class for all feature and vector transformations.
36
45
  */
@@ -39,6 +48,7 @@ class FeatureTransformation {
39
48
  virtual ~FeatureTransformation() = default;
40
49
 
41
50
  virtual uint32_t tag() const noexcept = 0;
51
+ virtual TransformationType type() const noexcept = 0;
42
52
  virtual std::string_view name() const noexcept = 0;
43
53
  virtual uint32_t input_dim() const noexcept = 0;
44
54
  virtual uint32_t output_dim() const noexcept = 0;