deglib 0.3.0__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.
- {deglib-0.3.0 → deglib-0.3.2}/CMakeLists.txt +12 -3
- {deglib-0.3.0/src/deglib.egg-info → deglib-0.3.2}/PKG-INFO +56 -40
- {deglib-0.3.0 → deglib-0.3.2}/README.md +55 -39
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/benchmark.h +10 -13
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/dataset.h +13 -15
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/builder.h +11 -11
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/deglib.h +1 -17
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/evp_ip.h +144 -31
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp16_ip.h +134 -39
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp16_l2.h +158 -46
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp32_ip.h +130 -38
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp32_l2.h +153 -41
- deglib-0.3.2/lib/deglib/include/deglib/distance/int4_ip.h +198 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int4_l2.h +58 -83
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int8_ip.h +41 -108
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int8_l2.h +38 -228
- deglib-0.3.2/lib/deglib/include/deglib/distance/tail_mask.h +123 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint4_ip.h +51 -73
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint4_l2.h +57 -66
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint7_ip.h +33 -52
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint7_l2.h +35 -54
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint8_ip.h +43 -112
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint8_l2.h +20 -26
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distances.h +196 -136
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/dynamic_graph.h +202 -115
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/internal_graph.h +656 -478
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/readonly_graph.h +115 -69
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/sizebounded_graph.h +206 -132
- deglib-0.3.2/lib/deglib/include/deglib/graph.h +1364 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +119 -50
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +78 -7
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/optimization.h +21 -10
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/search/kmeans.h +51 -79
- deglib-0.3.2/lib/deglib/include/deglib/search/pareto_line.h +690 -0
- deglib-0.3.2/lib/deglib/include/deglib/search/recall_table.h +968 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/search/reranker.h +256 -148
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/search/result_list.h +47 -25
- deglib-0.3.2/lib/deglib/include/deglib/search/searcher.h +1514 -0
- deglib-0.3.2/lib/deglib/include/deglib/search.h +18 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/evp.h +1 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/mips.h +25 -12
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/normalize.h +1 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/rotation.h +1 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/common.h +24 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/fp16.h +1 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/int4.h +89 -10
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/int8.h +75 -2
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/uint4.h +84 -3
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/uint7.h +99 -22
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation/scalar/uint8.h +83 -2
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/transformation.h +74 -49
- deglib-0.3.2/lib/deglib/include/deglib/tuning.h +2497 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/test/CMakeLists.txt +3 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/test/src/common/test_helpers.h +13 -25
- {deglib-0.3.0 → deglib-0.3.2}/pyproject.toml +1 -1
- {deglib-0.3.0 → deglib-0.3.2}/setup.py +4 -4
- deglib-0.3.2/src/deg_cpp/bindings_distances.cpp +191 -0
- deglib-0.3.2/src/deg_cpp/bindings_graph.cpp +865 -0
- deglib-0.3.2/src/deg_cpp/bindings_optimization.cpp +35 -0
- deglib-0.3.2/src/deg_cpp/bindings_search.cpp +571 -0
- deglib-0.3.2/src/deg_cpp/bindings_transformation.cpp +799 -0
- deglib-0.3.2/src/deg_cpp/bindings_tuning.cpp +257 -0
- deglib-0.3.2/src/deg_cpp/deglib_cpp.cpp +16 -0
- deglib-0.3.2/src/deglib/__init__.py +45 -0
- deglib-0.3.2/src/deglib/builder.py +726 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib/distances.py +8 -8
- deglib-0.3.2/src/deglib/graph.py +1154 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib/optimization.py +47 -13
- deglib-0.3.2/src/deglib/search.py +978 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib/transformation.py +52 -0
- deglib-0.3.2/src/deglib/tuning.py +282 -0
- {deglib-0.3.0 → deglib-0.3.2/src/deglib.egg-info}/PKG-INFO +56 -40
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib.egg-info/SOURCES.txt +17 -1
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_builder.py +53 -49
- deglib-0.3.2/tests/test_deglib.py +610 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_distances.py +2 -1
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_filter.py +4 -3
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_graph.py +266 -125
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_l2_normalize.py +33 -5
- deglib-0.3.2/tests/test_pareto_line.py +225 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_presort.py +1 -3
- deglib-0.3.2/tests/test_quantization_matrix.py +300 -0
- deglib-0.3.2/tests/test_recall_table.py +249 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_rerank.py +53 -14
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_rotation_transform.py +37 -36
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_scalar_quantizer.py +3 -3
- deglib-0.3.2/tests/test_searcher.py +728 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_transformation.py +3 -3
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_transformation_graph.py +89 -40
- deglib-0.3.2/tests/test_tuning.py +263 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/int4_ip.h +0 -241
- deglib-0.3.0/lib/deglib/include/deglib/graph.h +0 -1393
- deglib-0.3.0/lib/deglib/include/deglib/search/searcher.h +0 -925
- deglib-0.3.0/lib/deglib/include/deglib/search.h +0 -14
- deglib-0.3.0/src/deg_cpp/deglib_cpp.cpp +0 -2661
- deglib-0.3.0/src/deglib/__init__.py +0 -125
- deglib-0.3.0/src/deglib/builder.py +0 -470
- deglib-0.3.0/src/deglib/graph.py +0 -799
- deglib-0.3.0/src/deglib/search.py +0 -396
- deglib-0.3.0/tests/test_searcher.py +0 -477
- {deglib-0.3.0 → deglib-0.3.2}/LICENSE +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/MANIFEST.in +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/CMakeLists.txt +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/CMakeLists.txt +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/build.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/file_io.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/logging.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/repository.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/bench/include/stopwatch.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/CMakeLists.txt +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/analysis.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/concurrent.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/config.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp16.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/fp32.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int4.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/int8.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint4.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/distance/uint8.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/filter.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/graph/visited_list_pool.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/serialization.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/utils/cpu.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/utils/memory.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/deglib/include/deglib/utils/random.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/setup.cfg +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib/analysis.py +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib/cpu.py +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib/utils.py +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib.egg-info/dependency_links.txt +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib.egg-info/requires.txt +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/src/deglib.egg-info/top_level.txt +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_analysis.py +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_cpu.py +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_evp_quantizer.py +0 -0
- {deglib-0.3.0 → deglib-0.3.2}/tests/test_scalar_quantizer_int4.py +0 -0
|
@@ -8,8 +8,8 @@ if(DEFINED PYTHON_EXECUTABLE)
|
|
|
8
8
|
endif()
|
|
9
9
|
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
|
|
10
10
|
|
|
11
|
-
# Add
|
|
12
|
-
find_package(
|
|
11
|
+
# Add nanobind
|
|
12
|
+
find_package(nanobind CONFIG REQUIRED)
|
|
13
13
|
|
|
14
14
|
# disable benchmark and test building
|
|
15
15
|
set(ENABLE_BENCHMARKS OFF CACHE BOOL "Disable benchmarks" FORCE)
|
|
@@ -26,7 +26,16 @@ endif()
|
|
|
26
26
|
|
|
27
27
|
# Add cpp-deglib
|
|
28
28
|
add_subdirectory(${DEGLIB_CPP_DIR} deglib_cpp_build)
|
|
29
|
-
|
|
29
|
+
nanobind_add_module(
|
|
30
|
+
deglib_cpp NOMINSIZE
|
|
31
|
+
src/deg_cpp/deglib_cpp.cpp
|
|
32
|
+
src/deg_cpp/bindings_distances.cpp
|
|
33
|
+
src/deg_cpp/bindings_optimization.cpp
|
|
34
|
+
src/deg_cpp/bindings_transformation.cpp
|
|
35
|
+
src/deg_cpp/bindings_search.cpp
|
|
36
|
+
src/deg_cpp/bindings_graph.cpp
|
|
37
|
+
src/deg_cpp/bindings_tuning.cpp
|
|
38
|
+
)
|
|
30
39
|
|
|
31
40
|
# Specify the include directories
|
|
32
41
|
include_directories(${DEGLIB_CPP_DIR}/deglib/include)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deglib
|
|
3
|
-
Version: 0.3.
|
|
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>
|
|
@@ -69,13 +69,25 @@ To build and install the package directly from the repository:
|
|
|
69
69
|
```bash
|
|
70
70
|
cd python/
|
|
71
71
|
uv venv
|
|
72
|
-
uv pip install setuptools==83.0.0
|
|
72
|
+
uv pip install setuptools==83.0.0 nanobind==3.1.0 build==1.5.0 wheel==0.48.0
|
|
73
73
|
uv run python setup.py copy_build_files
|
|
74
74
|
uv pip install . --no-build-isolation
|
|
75
75
|
```
|
|
76
76
|
|
|
77
77
|
---
|
|
78
78
|
|
|
79
|
+
See [python/API.md](API.md) for the full API reference and [docs/python_metric_quantization_matrix.md](../docs/python_metric_quantization_matrix.md) for the complete metric/quantization behavior matrix.
|
|
80
|
+
|
|
81
|
+
## API Layers: High-Level vs. Low-Level
|
|
82
|
+
|
|
83
|
+
Top-level `deglib` intentionally exports only the **High-Level API** and core result/config types:
|
|
84
|
+
- `deglib.build`, `deglib.presort`, `deglib.compress`, `deglib.reranker`, `deglib.reranker_with_data`, `deglib.searcher`, `deglib.load_graph`
|
|
85
|
+
- `deglib.DynamicExplorationGraph`, `deglib.Searcher`, `deglib.Reranker`, `deglib.SearchResults`, `deglib.Metric`, `deglib.OptimizationTarget`
|
|
86
|
+
|
|
87
|
+
All **Low-Level / Expert APIs** live in submodules (`deglib.graph.create_empty`, `deglib.builder.GraphBuilder`, `deglib.optimization.prune_non_mrng_edges`, `deglib.transformation.make_*`, `deglib.search.RecallTable`, `deglib.cpu.InstructionSet`, `deglib.analysis.analyze_graph`, `deglib.tuning.predict_*`).
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
79
91
|
## Quickstart & Examples
|
|
80
92
|
|
|
81
93
|
### Basic Usage
|
|
@@ -90,14 +102,18 @@ num_samples, dims = 10_000, 128
|
|
|
90
102
|
data = np.random.random((num_samples, dims)).astype(np.float32)
|
|
91
103
|
query = np.random.random(dims).astype(np.float32)
|
|
92
104
|
|
|
93
|
-
# 2. Build index directly from data (
|
|
94
|
-
graph = deglib.build(data
|
|
105
|
+
# 2. Build index directly from data (metric="l2" by default)
|
|
106
|
+
graph = deglib.build(data)
|
|
107
|
+
|
|
108
|
+
# 3a. Query directly on the graph
|
|
109
|
+
res = graph.search(query, k=10, eps_or_ef=0.1)
|
|
95
110
|
|
|
96
|
-
#
|
|
97
|
-
|
|
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)
|
|
98
114
|
|
|
99
|
-
print("Nearest neighbors:", indices)
|
|
100
|
-
print("
|
|
115
|
+
print("Nearest neighbors:", res.indices)
|
|
116
|
+
print("Distance evals:", res.graph_distance_count, "| Hops:", res.graph_hop_count)
|
|
101
117
|
```
|
|
102
118
|
|
|
103
119
|
---
|
|
@@ -108,12 +124,12 @@ All search graphs are represented by `DynamicExplorationGraph`, backed by one of
|
|
|
108
124
|
|
|
109
125
|
1. **Fixed-Capacity Mutable (`SizeBoundedGraph`)**: Memory is preallocated for a fixed maximum capacity. Fast and memory-efficient for static/batch datasets.
|
|
110
126
|
```python
|
|
111
|
-
graph = deglib.create_empty(capacity=10_000, dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2)
|
|
127
|
+
graph = deglib.graph.create_empty(capacity=10_000, dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2)
|
|
112
128
|
```
|
|
113
129
|
|
|
114
130
|
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.
|
|
115
131
|
```python
|
|
116
|
-
graph = deglib.create_dynamic_empty(dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2, chunk_size=1024)
|
|
132
|
+
graph = deglib.graph.create_dynamic_empty(dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2, chunk_size=1024)
|
|
117
133
|
```
|
|
118
134
|
|
|
119
135
|
3. **Read-Only Deployment (`ReadOnlyGraph`)**: Stripped of mutation structures for minimal memory footprint and maximum query throughput.
|
|
@@ -130,13 +146,13 @@ All search graphs are represented by `DynamicExplorationGraph`, backed by one of
|
|
|
130
146
|
graph.save_graph("index.deg")
|
|
131
147
|
|
|
132
148
|
# Load as compact, optimized read-only graph for production search
|
|
133
|
-
readonly_graph = deglib.
|
|
149
|
+
readonly_graph = deglib.load_graph("index.deg")
|
|
134
150
|
|
|
135
151
|
# Load as dynamic graph supporting further insertions and deletions
|
|
136
|
-
dynamic_graph = deglib.load_dynamic_graph("index.deg")
|
|
152
|
+
dynamic_graph = deglib.graph.load_dynamic_graph("index.deg")
|
|
137
153
|
|
|
138
154
|
# Load as fixed-capacity mutable graph
|
|
139
|
-
mutable_graph = deglib.load_mutable_graph("index.deg")
|
|
155
|
+
mutable_graph = deglib.graph.load_mutable_graph("index.deg")
|
|
140
156
|
```
|
|
141
157
|
|
|
142
158
|
---
|
|
@@ -156,7 +172,7 @@ max_capacity = 10_000
|
|
|
156
172
|
edges_per_vertex = 32
|
|
157
173
|
|
|
158
174
|
# 1. Create empty mutable graph
|
|
159
|
-
graph = deglib.create_empty(max_capacity, dims, edges_per_vertex, metric=Metric.FP32_L2)
|
|
175
|
+
graph = deglib.graph.create_empty(max_capacity, dims, edges_per_vertex, metric=Metric.FP32_L2)
|
|
160
176
|
|
|
161
177
|
# 2. Initialize builder
|
|
162
178
|
builder = GraphBuilder(graph, optimization_target=OptimizationTarget.StreamingData, seed=42)
|
|
@@ -184,7 +200,8 @@ from deglib.search import Filter, rerank
|
|
|
184
200
|
allowed_ids = np.array([1, 5, 10, 42, 99], dtype=np.int32)
|
|
185
201
|
search_filter = Filter(allowed_ids)
|
|
186
202
|
|
|
187
|
-
|
|
203
|
+
res = graph.search(query, k=5, eps_or_ef=0.1, filter_labels=search_filter)
|
|
204
|
+
indices, distances = res.indices, res.distances
|
|
188
205
|
|
|
189
206
|
# Exact distance reranking across candidates
|
|
190
207
|
queries = np.random.random((10, dims)).astype(np.float32)
|
|
@@ -209,15 +226,17 @@ DEG supports exploratory search directly from existing vertex labels (single or
|
|
|
209
226
|
|
|
210
227
|
```python
|
|
211
228
|
# Explore graph starting from entry vertex label 105
|
|
212
|
-
|
|
229
|
+
exp_res = graph.explore(entry_external_label=105, k=10, eps_or_ef=0.1, include_entry=False)
|
|
230
|
+
explored_labels, distances = exp_res.indices, exp_res.distances
|
|
213
231
|
|
|
214
232
|
# Batch exploration from multiple entry vertices in parallel
|
|
215
233
|
batch_entries = np.array([105, 210, 315], dtype=np.uint32)
|
|
216
|
-
|
|
234
|
+
batch_exp = graph.explore(batch_entries, k=10, eps_or_ef=0.1, threads=4)
|
|
235
|
+
batch_labels, batch_distances = batch_exp.indices, batch_exp.distances
|
|
217
236
|
|
|
218
237
|
# Retrieve graph neighbors directly
|
|
219
238
|
neighbors = graph.get_neighbors(105)
|
|
220
|
-
batch_neighbors = graph.
|
|
239
|
+
batch_neighbors = graph.get_neighbors(batch_entries, threads=4) # shape: (3, edges_per_vertex)
|
|
221
240
|
```
|
|
222
241
|
|
|
223
242
|
---
|
|
@@ -234,32 +253,29 @@ num_vectors, dims = 10_000, 128
|
|
|
234
253
|
data = np.random.randn(num_vectors, dims).astype(np.float32)
|
|
235
254
|
query = np.random.randn(dims).astype(np.float32)
|
|
236
255
|
|
|
237
|
-
# 1.
|
|
238
|
-
graph = deglib.build(
|
|
239
|
-
data,
|
|
240
|
-
metric=deglib.Metric.FP32_InnerProduct,
|
|
241
|
-
edges_per_vertex=32,
|
|
242
|
-
presort=True,
|
|
243
|
-
optimize=False,
|
|
244
|
-
show_progress=True,
|
|
245
|
-
)
|
|
256
|
+
# 1. Build exploration graph in FP32
|
|
257
|
+
graph = deglib.build(data, metric="cosine")
|
|
246
258
|
|
|
247
|
-
# 2.
|
|
248
|
-
searcher =
|
|
249
|
-
metric=deglib.Metric.Int8_InnerProduct,
|
|
250
|
-
rerank_metric=deglib.Metric.FP16_InnerProduct,
|
|
251
|
-
)
|
|
259
|
+
# 2. Compress to 8-bit and auto-extract an FP16 reranker
|
|
260
|
+
searcher = deglib.searcher(graph, compress="8bit", reranker=True)
|
|
252
261
|
|
|
253
|
-
# 3.
|
|
254
|
-
|
|
255
|
-
print("Top-10 nearest neighbor indices:", indices)
|
|
256
|
-
print("Top-10 exact distances:", distances)
|
|
262
|
+
# 3. Fetch 1.5 * k candidates from graph and re-score with reranker
|
|
263
|
+
res = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
264
|
+
print("Top-10 nearest neighbor indices:", res.indices)
|
|
265
|
+
print("Top-10 exact distances:", res.distances)
|
|
266
|
+
print("Graph dists:", res.graph_distance_count, "| Rerank dists:", res.rerank_distance_count)
|
|
257
267
|
```
|
|
258
268
|
|
|
259
269
|
---
|
|
260
270
|
|
|
261
271
|
## Concepts & Parameters
|
|
262
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
|
+
|
|
263
279
|
### `OptimizationTarget`
|
|
264
280
|
Controls the topology optimization strategy:
|
|
265
281
|
- `OptimizationTarget.LowLID`: Default for datasets with low local intrinsic dimensionality (supports multithreaded building).
|
|
@@ -276,13 +292,13 @@ Controls the topology optimization strategy:
|
|
|
276
292
|
|
|
277
293
|
```python
|
|
278
294
|
# Mode 1: Relative distance margin (default: eps_or_ef=0.1)
|
|
279
|
-
|
|
295
|
+
res = searcher.search(query, k=10, eps_or_ef=0.1, return_distances=True)
|
|
280
296
|
|
|
281
297
|
# Mode 2: Fixed candidate pool (HNSW-style)
|
|
282
|
-
|
|
298
|
+
res_ef = searcher.search(query, k=10, eps_or_ef=128, return_distances=True)
|
|
283
299
|
|
|
284
300
|
# 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)
|
|
301
|
+
indices = searcher.search(query, k=10, eps_or_ef=128, max_distance_computation_count=700).indices
|
|
286
302
|
```
|
|
287
303
|
|
|
288
304
|
### Supported Metrics & Data Types
|
|
@@ -332,7 +348,7 @@ If you want to contribute, modify C++ bindings, run tests, or build release pack
|
|
|
332
348
|
# 1. Setup virtual environment and dependencies (using uv)
|
|
333
349
|
cd python/
|
|
334
350
|
uv venv
|
|
335
|
-
uv pip install setuptools==83.0.0
|
|
351
|
+
uv pip install setuptools==83.0.0 nanobind==3.1.0 build==1.5.0 wheel==0.48.0
|
|
336
352
|
uv run python setup.py copy_build_files
|
|
337
353
|
|
|
338
354
|
# 2. Install in editable mode
|
|
@@ -41,13 +41,25 @@ To build and install the package directly from the repository:
|
|
|
41
41
|
```bash
|
|
42
42
|
cd python/
|
|
43
43
|
uv venv
|
|
44
|
-
uv pip install setuptools==83.0.0
|
|
44
|
+
uv pip install setuptools==83.0.0 nanobind==3.1.0 build==1.5.0 wheel==0.48.0
|
|
45
45
|
uv run python setup.py copy_build_files
|
|
46
46
|
uv pip install . --no-build-isolation
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
---
|
|
50
50
|
|
|
51
|
+
See [python/API.md](API.md) for the full API reference and [docs/python_metric_quantization_matrix.md](../docs/python_metric_quantization_matrix.md) for the complete metric/quantization behavior matrix.
|
|
52
|
+
|
|
53
|
+
## API Layers: High-Level vs. Low-Level
|
|
54
|
+
|
|
55
|
+
Top-level `deglib` intentionally exports only the **High-Level API** and core result/config types:
|
|
56
|
+
- `deglib.build`, `deglib.presort`, `deglib.compress`, `deglib.reranker`, `deglib.reranker_with_data`, `deglib.searcher`, `deglib.load_graph`
|
|
57
|
+
- `deglib.DynamicExplorationGraph`, `deglib.Searcher`, `deglib.Reranker`, `deglib.SearchResults`, `deglib.Metric`, `deglib.OptimizationTarget`
|
|
58
|
+
|
|
59
|
+
All **Low-Level / Expert APIs** live in submodules (`deglib.graph.create_empty`, `deglib.builder.GraphBuilder`, `deglib.optimization.prune_non_mrng_edges`, `deglib.transformation.make_*`, `deglib.search.RecallTable`, `deglib.cpu.InstructionSet`, `deglib.analysis.analyze_graph`, `deglib.tuning.predict_*`).
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
51
63
|
## Quickstart & Examples
|
|
52
64
|
|
|
53
65
|
### Basic Usage
|
|
@@ -62,14 +74,18 @@ num_samples, dims = 10_000, 128
|
|
|
62
74
|
data = np.random.random((num_samples, dims)).astype(np.float32)
|
|
63
75
|
query = np.random.random(dims).astype(np.float32)
|
|
64
76
|
|
|
65
|
-
# 2. Build index directly from data (
|
|
66
|
-
graph = deglib.build(data
|
|
77
|
+
# 2. Build index directly from data (metric="l2" by default)
|
|
78
|
+
graph = deglib.build(data)
|
|
79
|
+
|
|
80
|
+
# 3a. Query directly on the graph
|
|
81
|
+
res = graph.search(query, k=10, eps_or_ef=0.1)
|
|
67
82
|
|
|
68
|
-
#
|
|
69
|
-
|
|
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)
|
|
70
86
|
|
|
71
|
-
print("Nearest neighbors:", indices)
|
|
72
|
-
print("
|
|
87
|
+
print("Nearest neighbors:", res.indices)
|
|
88
|
+
print("Distance evals:", res.graph_distance_count, "| Hops:", res.graph_hop_count)
|
|
73
89
|
```
|
|
74
90
|
|
|
75
91
|
---
|
|
@@ -80,12 +96,12 @@ All search graphs are represented by `DynamicExplorationGraph`, backed by one of
|
|
|
80
96
|
|
|
81
97
|
1. **Fixed-Capacity Mutable (`SizeBoundedGraph`)**: Memory is preallocated for a fixed maximum capacity. Fast and memory-efficient for static/batch datasets.
|
|
82
98
|
```python
|
|
83
|
-
graph = deglib.create_empty(capacity=10_000, dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2)
|
|
99
|
+
graph = deglib.graph.create_empty(capacity=10_000, dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2)
|
|
84
100
|
```
|
|
85
101
|
|
|
86
102
|
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.
|
|
87
103
|
```python
|
|
88
|
-
graph = deglib.create_dynamic_empty(dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2, chunk_size=1024)
|
|
104
|
+
graph = deglib.graph.create_dynamic_empty(dim=128, edges_per_vertex=32, metric=deglib.Metric.FP32_L2, chunk_size=1024)
|
|
89
105
|
```
|
|
90
106
|
|
|
91
107
|
3. **Read-Only Deployment (`ReadOnlyGraph`)**: Stripped of mutation structures for minimal memory footprint and maximum query throughput.
|
|
@@ -102,13 +118,13 @@ All search graphs are represented by `DynamicExplorationGraph`, backed by one of
|
|
|
102
118
|
graph.save_graph("index.deg")
|
|
103
119
|
|
|
104
120
|
# Load as compact, optimized read-only graph for production search
|
|
105
|
-
readonly_graph = deglib.
|
|
121
|
+
readonly_graph = deglib.load_graph("index.deg")
|
|
106
122
|
|
|
107
123
|
# Load as dynamic graph supporting further insertions and deletions
|
|
108
|
-
dynamic_graph = deglib.load_dynamic_graph("index.deg")
|
|
124
|
+
dynamic_graph = deglib.graph.load_dynamic_graph("index.deg")
|
|
109
125
|
|
|
110
126
|
# Load as fixed-capacity mutable graph
|
|
111
|
-
mutable_graph = deglib.load_mutable_graph("index.deg")
|
|
127
|
+
mutable_graph = deglib.graph.load_mutable_graph("index.deg")
|
|
112
128
|
```
|
|
113
129
|
|
|
114
130
|
---
|
|
@@ -128,7 +144,7 @@ max_capacity = 10_000
|
|
|
128
144
|
edges_per_vertex = 32
|
|
129
145
|
|
|
130
146
|
# 1. Create empty mutable graph
|
|
131
|
-
graph = deglib.create_empty(max_capacity, dims, edges_per_vertex, metric=Metric.FP32_L2)
|
|
147
|
+
graph = deglib.graph.create_empty(max_capacity, dims, edges_per_vertex, metric=Metric.FP32_L2)
|
|
132
148
|
|
|
133
149
|
# 2. Initialize builder
|
|
134
150
|
builder = GraphBuilder(graph, optimization_target=OptimizationTarget.StreamingData, seed=42)
|
|
@@ -156,7 +172,8 @@ from deglib.search import Filter, rerank
|
|
|
156
172
|
allowed_ids = np.array([1, 5, 10, 42, 99], dtype=np.int32)
|
|
157
173
|
search_filter = Filter(allowed_ids)
|
|
158
174
|
|
|
159
|
-
|
|
175
|
+
res = graph.search(query, k=5, eps_or_ef=0.1, filter_labels=search_filter)
|
|
176
|
+
indices, distances = res.indices, res.distances
|
|
160
177
|
|
|
161
178
|
# Exact distance reranking across candidates
|
|
162
179
|
queries = np.random.random((10, dims)).astype(np.float32)
|
|
@@ -181,15 +198,17 @@ DEG supports exploratory search directly from existing vertex labels (single or
|
|
|
181
198
|
|
|
182
199
|
```python
|
|
183
200
|
# Explore graph starting from entry vertex label 105
|
|
184
|
-
|
|
201
|
+
exp_res = graph.explore(entry_external_label=105, k=10, eps_or_ef=0.1, include_entry=False)
|
|
202
|
+
explored_labels, distances = exp_res.indices, exp_res.distances
|
|
185
203
|
|
|
186
204
|
# Batch exploration from multiple entry vertices in parallel
|
|
187
205
|
batch_entries = np.array([105, 210, 315], dtype=np.uint32)
|
|
188
|
-
|
|
206
|
+
batch_exp = graph.explore(batch_entries, k=10, eps_or_ef=0.1, threads=4)
|
|
207
|
+
batch_labels, batch_distances = batch_exp.indices, batch_exp.distances
|
|
189
208
|
|
|
190
209
|
# Retrieve graph neighbors directly
|
|
191
210
|
neighbors = graph.get_neighbors(105)
|
|
192
|
-
batch_neighbors = graph.
|
|
211
|
+
batch_neighbors = graph.get_neighbors(batch_entries, threads=4) # shape: (3, edges_per_vertex)
|
|
193
212
|
```
|
|
194
213
|
|
|
195
214
|
---
|
|
@@ -206,32 +225,29 @@ num_vectors, dims = 10_000, 128
|
|
|
206
225
|
data = np.random.randn(num_vectors, dims).astype(np.float32)
|
|
207
226
|
query = np.random.randn(dims).astype(np.float32)
|
|
208
227
|
|
|
209
|
-
# 1.
|
|
210
|
-
graph = deglib.build(
|
|
211
|
-
data,
|
|
212
|
-
metric=deglib.Metric.FP32_InnerProduct,
|
|
213
|
-
edges_per_vertex=32,
|
|
214
|
-
presort=True,
|
|
215
|
-
optimize=False,
|
|
216
|
-
show_progress=True,
|
|
217
|
-
)
|
|
228
|
+
# 1. Build exploration graph in FP32
|
|
229
|
+
graph = deglib.build(data, metric="cosine")
|
|
218
230
|
|
|
219
|
-
# 2.
|
|
220
|
-
searcher =
|
|
221
|
-
metric=deglib.Metric.Int8_InnerProduct,
|
|
222
|
-
rerank_metric=deglib.Metric.FP16_InnerProduct,
|
|
223
|
-
)
|
|
231
|
+
# 2. Compress to 8-bit and auto-extract an FP16 reranker
|
|
232
|
+
searcher = deglib.searcher(graph, compress="8bit", reranker=True)
|
|
224
233
|
|
|
225
|
-
# 3.
|
|
226
|
-
|
|
227
|
-
print("Top-10 nearest neighbor indices:", indices)
|
|
228
|
-
print("Top-10 exact distances:", distances)
|
|
234
|
+
# 3. Fetch 1.5 * k candidates from graph and re-score with reranker
|
|
235
|
+
res = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
236
|
+
print("Top-10 nearest neighbor indices:", res.indices)
|
|
237
|
+
print("Top-10 exact distances:", res.distances)
|
|
238
|
+
print("Graph dists:", res.graph_distance_count, "| Rerank dists:", res.rerank_distance_count)
|
|
229
239
|
```
|
|
230
240
|
|
|
231
241
|
---
|
|
232
242
|
|
|
233
243
|
## Concepts & Parameters
|
|
234
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
|
+
|
|
235
251
|
### `OptimizationTarget`
|
|
236
252
|
Controls the topology optimization strategy:
|
|
237
253
|
- `OptimizationTarget.LowLID`: Default for datasets with low local intrinsic dimensionality (supports multithreaded building).
|
|
@@ -248,13 +264,13 @@ Controls the topology optimization strategy:
|
|
|
248
264
|
|
|
249
265
|
```python
|
|
250
266
|
# Mode 1: Relative distance margin (default: eps_or_ef=0.1)
|
|
251
|
-
|
|
267
|
+
res = searcher.search(query, k=10, eps_or_ef=0.1, return_distances=True)
|
|
252
268
|
|
|
253
269
|
# Mode 2: Fixed candidate pool (HNSW-style)
|
|
254
|
-
|
|
270
|
+
res_ef = searcher.search(query, k=10, eps_or_ef=128, return_distances=True)
|
|
255
271
|
|
|
256
272
|
# 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)
|
|
273
|
+
indices = searcher.search(query, k=10, eps_or_ef=128, max_distance_computation_count=700).indices
|
|
258
274
|
```
|
|
259
275
|
|
|
260
276
|
### Supported Metrics & Data Types
|
|
@@ -304,7 +320,7 @@ If you want to contribute, modify C++ bindings, run tests, or build release pack
|
|
|
304
320
|
# 1. Setup virtual environment and dependencies (using uv)
|
|
305
321
|
cd python/
|
|
306
322
|
uv venv
|
|
307
|
-
uv pip install setuptools==83.0.0
|
|
323
|
+
uv pip install setuptools==83.0.0 nanobind==3.1.0 build==1.5.0 wheel==0.48.0
|
|
308
324
|
uv run python setup.py copy_build_files
|
|
309
325
|
|
|
310
326
|
# 2. Install in editable mode
|
|
@@ -34,10 +34,7 @@ namespace deglib::benchmark {
|
|
|
34
34
|
* @param base_gt Optional contiguous BaseGroundTruth buffer
|
|
35
35
|
* @return GraphStats with all computed statistics
|
|
36
36
|
*/
|
|
37
|
-
inline deglib::analysis::GraphStats analyze_graph(
|
|
38
|
-
const deglib::graph::InternalGraph& graph,
|
|
39
|
-
const BaseGroundTruth& base_gt = {}
|
|
40
|
-
) {
|
|
37
|
+
inline deglib::analysis::GraphStats analyze_graph(const deglib::graph::InternalGraph& graph, const BaseGroundTruth& base_gt = {}) {
|
|
41
38
|
auto stats = deglib::analysis::analyze_graph(graph, base_gt.data(), base_gt.rows, base_gt.k);
|
|
42
39
|
|
|
43
40
|
log("Graph Statistics:\n");
|
|
@@ -133,11 +130,11 @@ static float test_approx_anns(
|
|
|
133
130
|
|
|
134
131
|
uint32_t correct = 0;
|
|
135
132
|
const auto& gt = ground_truth[i];
|
|
136
|
-
while (result_queue.empty() == false) {
|
|
137
|
-
const auto& result = result_queue.top();
|
|
133
|
+
while (result_queue.results.empty() == false) {
|
|
134
|
+
const auto& result = result_queue.results.top();
|
|
138
135
|
const auto external_id = graph.getExternalLabel(result.getIdentifier());
|
|
139
136
|
if (std::binary_search(gt.begin(), gt.end(), external_id)) correct++;
|
|
140
|
-
result_queue.pop();
|
|
137
|
+
result_queue.results.pop();
|
|
141
138
|
}
|
|
142
139
|
|
|
143
140
|
corrects[thread_id] += correct;
|
|
@@ -172,11 +169,11 @@ static float test_approx_explore(
|
|
|
172
169
|
|
|
173
170
|
uint32_t correct = 0;
|
|
174
171
|
const auto& gt = ground_truth[i];
|
|
175
|
-
while (result_queue.empty() == false) {
|
|
176
|
-
const auto& result = result_queue.top();
|
|
172
|
+
while (result_queue.results.empty() == false) {
|
|
173
|
+
const auto& result = result_queue.results.top();
|
|
177
174
|
const auto external_id = graph.getExternalLabel(result.getIdentifier());
|
|
178
175
|
if (std::binary_search(gt.begin(), gt.end(), external_id)) correct++;
|
|
179
|
-
result_queue.pop();
|
|
176
|
+
result_queue.results.pop();
|
|
180
177
|
}
|
|
181
178
|
|
|
182
179
|
corrects[thread_id] += correct;
|
|
@@ -212,11 +209,11 @@ static std::vector<float> estimate_recall(
|
|
|
212
209
|
total += result_queue.size();
|
|
213
210
|
|
|
214
211
|
size_t local_correct = 0;
|
|
215
|
-
while (result_queue.empty() == false) {
|
|
216
|
-
const auto internal_index = result_queue.top().getIdentifier();
|
|
212
|
+
while (result_queue.results.empty() == false) {
|
|
213
|
+
const auto internal_index = result_queue.results.top().getIdentifier();
|
|
217
214
|
const auto external_id = graph.getExternalLabel(internal_index);
|
|
218
215
|
if (std::binary_search(gt.begin(), gt.end(), external_id)) local_correct++;
|
|
219
|
-
result_queue.pop();
|
|
216
|
+
result_queue.results.pop();
|
|
220
217
|
}
|
|
221
218
|
correct += local_correct;
|
|
222
219
|
});
|
|
@@ -179,12 +179,8 @@ struct BaseGroundTruth {
|
|
|
179
179
|
size_t rows = 0;
|
|
180
180
|
size_t k = 0;
|
|
181
181
|
|
|
182
|
-
const uint32_t* data() const noexcept {
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
bool empty() const noexcept {
|
|
186
|
-
return !buffer || rows == 0 || k == 0;
|
|
187
|
-
}
|
|
182
|
+
const uint32_t* data() const noexcept { return reinterpret_cast<const uint32_t*>(buffer.get()); }
|
|
183
|
+
bool empty() const noexcept { return !buffer || rows == 0 || k == 0; }
|
|
188
184
|
};
|
|
189
185
|
|
|
190
186
|
class Dataset {
|
|
@@ -211,7 +207,8 @@ class Dataset {
|
|
|
211
207
|
std::string base_groundtruth_half_file() const { return (files_dir_ / info_.base_groundtruth_half_file).string(); }
|
|
212
208
|
|
|
213
209
|
std::string groundtruth_file(size_t nb) const {
|
|
214
|
-
return (files_dir_ / fmt::format("{}_groundtruth_top{}_nb{}_{}.ivecs", info_.name(), DatasetInfo::GROUNDTRUTH_TOPK, nb, info_.metric.to_string()))
|
|
210
|
+
return (files_dir_ / fmt::format("{}_groundtruth_top{}_nb{}_{}.ivecs", info_.name(), DatasetInfo::GROUNDTRUTH_TOPK, nb, info_.metric.to_string()))
|
|
211
|
+
.string();
|
|
215
212
|
}
|
|
216
213
|
std::string groundtruth_file_full() const { return groundtruth_file(info_.base_count); }
|
|
217
214
|
std::string groundtruth_file_half() const { return groundtruth_file(info_.base_count / 2); }
|
|
@@ -702,7 +699,7 @@ inline bool generate_base_groundtruth_files(
|
|
|
702
699
|
|
|
703
700
|
if (quality_mode == GraphQualityMode::Approximated && !graph_path.empty() && file_exists(graph_path)) {
|
|
704
701
|
fmt::print("Computing base ground truth (ANNS graph exploration) using {} -> {}\n", graph_path, out_file);
|
|
705
|
-
const auto graph = deglib::
|
|
702
|
+
const auto graph = deglib::DynamicExplorationGraph::load_readonly(graph_path.c_str());
|
|
706
703
|
const std::string tmp_file = out_file + ".tmp";
|
|
707
704
|
auto out = std::ofstream(tmp_file, std::ios::out | std::ios::binary);
|
|
708
705
|
if (!out.is_open()) {
|
|
@@ -722,12 +719,10 @@ inline bool generate_base_groundtruth_files(
|
|
|
722
719
|
std::fill(row, row + topk, (std::numeric_limits<uint32_t>::max)());
|
|
723
720
|
return;
|
|
724
721
|
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
row[j] = graph.getExternalLabel(result_queue.top().getIdentifier());
|
|
730
|
-
result_queue.pop();
|
|
722
|
+
auto result_queue = graph.explore(ext_label, topk, 0, 0.1f, /*include_entry=*/false);
|
|
723
|
+
for (uint32_t j = 0; j < topk; ++j) {
|
|
724
|
+
if (j < result_queue.indices.size()) {
|
|
725
|
+
row[j] = result_queue.indices[j];
|
|
731
726
|
} else {
|
|
732
727
|
row[j] = (std::numeric_limits<uint32_t>::max)();
|
|
733
728
|
}
|
|
@@ -749,7 +744,10 @@ inline bool generate_base_groundtruth_files(
|
|
|
749
744
|
return true;
|
|
750
745
|
}
|
|
751
746
|
|
|
752
|
-
fmt::print(
|
|
747
|
+
fmt::print(
|
|
748
|
+
"Computing base ground truth (linear search, metric: {}) -> {} (total: {} queries, base limit: {})...\n", info.metric.to_string(), out_file,
|
|
749
|
+
total_queries, base_limit
|
|
750
|
+
);
|
|
753
751
|
const std::string tmp_file = out_file + ".tmp";
|
|
754
752
|
auto out = std::ofstream(tmp_file, std::ios::out | std::ios::binary);
|
|
755
753
|
if (!out.is_open()) {
|
|
@@ -584,10 +584,10 @@ class EvenRegularGraphBuilder {
|
|
|
584
584
|
// find good neighbor candidates for the new vertex
|
|
585
585
|
auto distrib = deglib::random::DeterministicUniformIntDistribution<uint32_t>(0, uint32_t(graph.size() - 1));
|
|
586
586
|
const std::vector<uint32_t> entry_vertex_indices = {distrib(this->rnd_)};
|
|
587
|
-
auto
|
|
587
|
+
auto ws = graph.search_intern(
|
|
588
588
|
entry_vertex_indices, new_vertex_feature, std::max(uint32_t(this->extend_k_), edges_per_vertex * 2), this->extend_eps_
|
|
589
589
|
); // need 2x otherwise it might lock during neighbor selection
|
|
590
|
-
const auto candidates = topListAscending(
|
|
590
|
+
const auto candidates = topListAscending(ws.results);
|
|
591
591
|
|
|
592
592
|
// their should always be enough neighbors (search candidates), otherwise the graph would be broken
|
|
593
593
|
if (candidates.size() < edges_per_vertex) {
|
|
@@ -725,9 +725,9 @@ class EvenRegularGraphBuilder {
|
|
|
725
725
|
|
|
726
726
|
// find good neighbors for the new vertex
|
|
727
727
|
const auto entry_vertex_indices = graph.getEntryVertexIndices();
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
const auto results = topListAscending(
|
|
728
|
+
thread_local deglib::graph::SearchWorkspace ws;
|
|
729
|
+
graph.search_intern(ws, entry_vertex_indices, new_vertex_feature, std::max(uint32_t(this->extend_k_), edges_per_vertex), this->extend_eps_);
|
|
730
|
+
const auto results = topListAscending(ws.results);
|
|
731
731
|
|
|
732
732
|
// their should always be enough neighbors (search results), otherwise the graph would be broken
|
|
733
733
|
if (results.size() < edges_per_vertex) {
|
|
@@ -1231,8 +1231,8 @@ class EvenRegularGraphBuilder {
|
|
|
1231
1231
|
// their subgraph and would therefore connect the two potential subgraphs.
|
|
1232
1232
|
{
|
|
1233
1233
|
const auto vertex2_feature = graph.getFeatureVector(vertex2);
|
|
1234
|
-
const
|
|
1235
|
-
auto
|
|
1234
|
+
const uint32_t entry_vertex_indices[2] = {vertex3, vertex4};
|
|
1235
|
+
auto ws = graph.search_intern(entry_vertex_indices, vertex2_feature, improve_k_, this->improve_eps_);
|
|
1236
1236
|
|
|
1237
1237
|
// find a good new vertex3
|
|
1238
1238
|
float best_gain = total_gain;
|
|
@@ -1241,7 +1241,7 @@ class EvenRegularGraphBuilder {
|
|
|
1241
1241
|
|
|
1242
1242
|
// We use the descending order to find the worst swap combination with the best gain
|
|
1243
1243
|
// Sometimes the gain between the two best combinations is the same, its better to use one with the bad edges to make later improvements easier
|
|
1244
|
-
for (auto&& result : topListDescending(
|
|
1244
|
+
for (auto&& result : topListDescending(ws.results)) {
|
|
1245
1245
|
const uint32_t new_vertex3 = result.getIdentifier();
|
|
1246
1246
|
|
|
1247
1247
|
// vertex1 and vertex2 got tested in the recursive call before and vertex4 got just disconnected from vertex2
|
|
@@ -1302,9 +1302,9 @@ class EvenRegularGraphBuilder {
|
|
|
1302
1302
|
// two vertices are already connected to vertex4.
|
|
1303
1303
|
if (vertex1 == vertex4) {
|
|
1304
1304
|
// find a good (not yet connected) vertex for vertex1/vertex4
|
|
1305
|
-
const
|
|
1305
|
+
const uint32_t entry_vertex_indices[2] = {vertex2, vertex3};
|
|
1306
1306
|
const auto vertex4_feature = graph.getFeatureVector(vertex4);
|
|
1307
|
-
auto
|
|
1307
|
+
auto ws = graph.search_intern(entry_vertex_indices, vertex4_feature, improve_k_, this->improve_eps_);
|
|
1308
1308
|
|
|
1309
1309
|
float best_gain = 0;
|
|
1310
1310
|
uint32_t best_selected_neighbor = 0;
|
|
@@ -1312,7 +1312,7 @@ class EvenRegularGraphBuilder {
|
|
|
1312
1312
|
float best_new_neighbor_dist = 0;
|
|
1313
1313
|
uint32_t best_good_vertex = 0;
|
|
1314
1314
|
float best_good_vertex_dist = 0;
|
|
1315
|
-
for (auto&& result : topListAscending(
|
|
1315
|
+
for (auto&& result : topListAscending(ws.results)) {
|
|
1316
1316
|
const auto good_vertex = result.getIdentifier();
|
|
1317
1317
|
|
|
1318
1318
|
// the new vertex should not be connected to vertex4 yet
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
#include "deglib/search.h"
|
|
11
11
|
#include "deglib/search/searcher.h"
|
|
12
12
|
#include "deglib/transformation.h"
|
|
13
|
+
#include "deglib/tuning.h"
|
|
13
14
|
#include "deglib/utils/memory.h"
|
|
14
15
|
#include "deglib/utils/random.h"
|
|
15
16
|
|
|
@@ -109,21 +110,4 @@ inline DynamicExplorationGraph load_mutable_graph(const char* path_graph, const
|
|
|
109
110
|
return DynamicExplorationGraph::load_mutable(path_graph, new_max_size);
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
/**
|
|
113
|
-
* Create a high-performance Searcher over a DynamicExplorationGraph facade.
|
|
114
|
-
* Convenience overloads that forward to deglib::search::make_searcher using the graph's internal representation,
|
|
115
|
-
* so end users can pass the public graph type directly.
|
|
116
|
-
*/
|
|
117
|
-
inline std::unique_ptr<deglib::search::Searcher> make_searcher(const DynamicExplorationGraph& graph) {
|
|
118
|
-
return std::make_unique<deglib::search::Searcher>(graph.internal(), graph.getTransformations());
|
|
119
|
-
}
|
|
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
|
-
|
|
125
|
-
inline std::unique_ptr<deglib::search::Searcher> make_searcher(const DynamicExplorationGraph& graph, deglib::search::Reranker reranker) {
|
|
126
|
-
return std::make_unique<deglib::search::Searcher>(graph.internal(), graph.getTransformations(), std::move(reranker));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
113
|
} // namespace deglib
|