deglib 0.2.8__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.
- {deglib-0.2.8/src/deglib.egg-info → deglib-0.3.0}/PKG-INFO +34 -44
- {deglib-0.2.8 → deglib-0.3.0}/README.md +33 -43
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/dataset.h +1 -1
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/analysis.h +5 -3
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/builder.h +4 -4
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/concurrent.h +15 -12
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/config.h +6 -2
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/deglib.h +4 -3
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/evp_ip.h +7 -3
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp16.h +4 -15
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp16_ip.h +14 -10
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp16_l2.h +16 -12
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp32_ip.h +14 -10
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp32_l2.h +36 -10
- deglib-0.3.0/lib/deglib/include/deglib/distance/int4.h +11 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/int4_ip.h +241 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/int4_l2.h +436 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/int8_ip.h +93 -182
- deglib-0.3.0/lib/deglib/include/deglib/distance/int8_l2.h +550 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/uint4.h +11 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/uint4_ip.h +354 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/uint4_l2.h +362 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/uint7_ip.h +222 -0
- deglib-0.3.0/lib/deglib/include/deglib/distance/uint7_l2.h +229 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/uint8_ip.h +75 -122
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/uint8_l2.h +16 -12
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distances.h +77 -10
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph/dynamic_graph.h +33 -24
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph/internal_graph.h +247 -101
- deglib-0.3.0/lib/deglib/include/deglib/graph/readonly_graph.h +314 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph/sizebounded_graph.h +33 -24
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph/visited_list_pool.h +4 -4
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph.h +292 -248
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/search/kmeans.h +225 -86
- deglib-0.3.0/lib/deglib/include/deglib/search/reranker.h +903 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/search/result_list.h +11 -0
- deglib-0.3.0/lib/deglib/include/deglib/search/searcher.h +925 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/transformation/evp.h +280 -257
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/transformation/normalize.h +9 -5
- deglib-0.3.0/lib/deglib/include/deglib/transformation/rotation.h +413 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/common.h +173 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/fp16.h +204 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/int4.h +241 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/int8.h +212 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/uint4.h +430 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/uint7.h +209 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation/scalar/uint8.h +221 -0
- deglib-0.3.0/lib/deglib/include/deglib/transformation.h +597 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/utils/cpu.h +21 -5
- deglib-0.3.0/lib/deglib/include/deglib/utils/memory.h +83 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/test/CMakeLists.txt +4 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/test/src/common/test_helpers.h +7 -7
- {deglib-0.2.8 → deglib-0.3.0}/src/deg_cpp/deglib_cpp.cpp +835 -722
- deglib-0.3.0/src/deglib/__init__.py +125 -0
- {deglib-0.2.8 → deglib-0.3.0}/src/deglib/analysis.py +57 -4
- deglib-0.3.0/src/deglib/builder.py +470 -0
- deglib-0.3.0/src/deglib/cpu.py +112 -0
- deglib-0.3.0/src/deglib/distances.py +409 -0
- deglib-0.3.0/src/deglib/graph.py +799 -0
- deglib-0.3.0/src/deglib/optimization.py +111 -0
- deglib-0.3.0/src/deglib/search.py +396 -0
- deglib-0.3.0/src/deglib/transformation.py +220 -0
- {deglib-0.2.8 → deglib-0.3.0}/src/deglib/utils.py +16 -5
- {deglib-0.2.8 → deglib-0.3.0/src/deglib.egg-info}/PKG-INFO +34 -44
- {deglib-0.2.8 → deglib-0.3.0}/src/deglib.egg-info/SOURCES.txt +20 -1
- {deglib-0.2.8 → deglib-0.3.0}/tests/test_analysis.py +2 -1
- {deglib-0.2.8 → deglib-0.3.0}/tests/test_builder.py +100 -18
- deglib-0.3.0/tests/test_cpu.py +20 -0
- deglib-0.3.0/tests/test_distances.py +74 -0
- deglib-0.3.0/tests/test_evp_quantizer.py +90 -0
- {deglib-0.2.8 → deglib-0.3.0}/tests/test_filter.py +2 -2
- {deglib-0.2.8 → deglib-0.3.0}/tests/test_graph.py +158 -88
- deglib-0.3.0/tests/test_l2_normalize.py +52 -0
- deglib-0.3.0/tests/test_presort.py +77 -0
- deglib-0.3.0/tests/test_rerank.py +239 -0
- deglib-0.3.0/tests/test_rotation_transform.py +128 -0
- deglib-0.3.0/tests/test_scalar_quantizer.py +234 -0
- deglib-0.3.0/tests/test_scalar_quantizer_int4.py +34 -0
- {deglib-0.2.8 → deglib-0.3.0}/tests/test_searcher.py +169 -42
- deglib-0.3.0/tests/test_transformation.py +63 -0
- {deglib-0.2.8 → deglib-0.3.0}/tests/test_transformation_graph.py +82 -97
- deglib-0.2.8/lib/deglib/include/deglib/distance/int8_l2.h +0 -249
- deglib-0.2.8/lib/deglib/include/deglib/graph/readonly_graph.h +0 -290
- deglib-0.2.8/lib/deglib/include/deglib/search/reranker.h +0 -603
- deglib-0.2.8/lib/deglib/include/deglib/search/searcher.h +0 -698
- deglib-0.2.8/lib/deglib/include/deglib/transformation/scalar.h +0 -1362
- deglib-0.2.8/lib/deglib/include/deglib/transformation.h +0 -266
- deglib-0.2.8/lib/deglib/include/deglib/utils/memory.h +0 -23
- deglib-0.2.8/src/deglib/__init__.py +0 -83
- deglib-0.2.8/src/deglib/builder.py +0 -456
- deglib-0.2.8/src/deglib/cpu.py +0 -59
- deglib-0.2.8/src/deglib/distances.py +0 -149
- deglib-0.2.8/src/deglib/graph.py +0 -548
- deglib-0.2.8/src/deglib/optimization.py +0 -116
- deglib-0.2.8/src/deglib/search.py +0 -327
- deglib-0.2.8/src/deglib/transformation.py +0 -96
- deglib-0.2.8/tests/test_evp_quantizer.py +0 -132
- deglib-0.2.8/tests/test_l2_normalize.py +0 -58
- deglib-0.2.8/tests/test_presort.py +0 -97
- deglib-0.2.8/tests/test_rerank.py +0 -290
- deglib-0.2.8/tests/test_scalar_quantizer.py +0 -302
- deglib-0.2.8/tests/test_transformation.py +0 -65
- {deglib-0.2.8 → deglib-0.3.0}/CMakeLists.txt +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/LICENSE +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/MANIFEST.in +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/CMakeLists.txt +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/CMakeLists.txt +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/benchmark.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/build.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/file_io.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/logging.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/repository.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/bench/include/stopwatch.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/CMakeLists.txt +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/fp32.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/int8.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/distance/uint8.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/filter.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/optimization.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/search.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/serialization.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/transformation/mips.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/deglib/include/deglib/utils/random.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/pyproject.toml +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/setup.cfg +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/setup.py +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/src/deglib.egg-info/dependency_links.txt +0 -0
- {deglib-0.2.8 → deglib-0.3.0}/src/deglib.egg-info/requires.txt +0 -0
- {deglib-0.2.8 → 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.
|
|
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,8 +90,8 @@ 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.
|
|
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
97
|
indices, distances = graph.search(query, k=10, eps_or_ef=0.1)
|
|
@@ -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
|
-
|
|
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(
|
|
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
|
|
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
|
|
160
|
-
|
|
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
|
-
|
|
195
|
+
metric=graph.get_metric(),
|
|
198
196
|
queries=queries,
|
|
199
197
|
candidate_indices=candidates,
|
|
200
198
|
base_vectors=base_vectors,
|
|
@@ -226,50 +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**, **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
|
|
235
|
-
from deglib.transformation import make_scalar_quantizer_int8
|
|
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
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
#
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
# 4. Convert mutable graph to a compact ReadOnlyGraph equipped with the quantized features
|
|
260
|
-
readonly_graph = graph.to_readonly(
|
|
261
|
-
feature_space=int8_space, custom_features=quant_int8_data, transformations=[quantizer]
|
|
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,
|
|
262
251
|
)
|
|
263
252
|
|
|
264
|
-
#
|
|
265
|
-
rerank_space = deglib.FloatSpace.create(dims, deglib.Metric.FP32_InnerProduct)
|
|
266
|
-
searcher = create_searcher(
|
|
267
|
-
graph=readonly_graph,
|
|
268
|
-
rerank_space=rerank_space,
|
|
269
|
-
rerank_data=sorted_data,
|
|
270
|
-
)
|
|
271
|
-
|
|
272
|
-
# 6. Query search (query is automatically quantized and top candidates are reranked)
|
|
253
|
+
# 3. Query search (query is automatically quantized and top candidates are reranked)
|
|
273
254
|
indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
274
255
|
print("Top-10 nearest neighbor indices:", indices)
|
|
275
256
|
print("Top-10 exact distances:", distances)
|
|
@@ -295,10 +276,13 @@ Controls the topology optimization strategy:
|
|
|
295
276
|
|
|
296
277
|
```python
|
|
297
278
|
# Mode 1: Relative distance margin (default: eps_or_ef=0.1)
|
|
298
|
-
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)
|
|
299
280
|
|
|
300
281
|
# Mode 2: Fixed candidate pool (HNSW-style)
|
|
301
|
-
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)
|
|
302
286
|
```
|
|
303
287
|
|
|
304
288
|
### Supported Metrics & Data Types
|
|
@@ -306,11 +290,17 @@ indices, distances = searcher.search(query, k=10, eps_or_ef=128)
|
|
|
306
290
|
- `Metric.FP32_InnerProduct`: Inner product / cosine distance (`np.float32`)
|
|
307
291
|
- `Metric.Uint8_L2`: 8-bit unsigned integer Euclidean distance (`np.uint8`)
|
|
308
292
|
- `Metric.Uint8_InnerProduct`: 8-bit unsigned integer inner product (`np.uint8`)
|
|
309
|
-
- `Metric.FP16_L2`: 16-bit half-precision Euclidean distance (`np.uint16`)
|
|
310
|
-
- `Metric.FP16_InnerProduct`: 16-bit half-precision inner product (`np.uint16`)
|
|
311
|
-
- `Metric.EVP_InnerProduct`: Quantized
|
|
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`)
|
|
312
296
|
- `Metric.Int8_InnerProduct`: 8-bit signed integer inner product (`np.int8`)
|
|
313
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`)
|
|
314
304
|
|
|
315
305
|
---
|
|
316
306
|
|
|
@@ -62,8 +62,8 @@ 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.
|
|
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
69
|
indices, distances = graph.search(query, k=10, eps_or_ef=0.1)
|
|
@@ -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
|
-
|
|
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(
|
|
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
|
|
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
|
|
132
|
-
|
|
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
|
-
|
|
167
|
+
metric=graph.get_metric(),
|
|
170
168
|
queries=queries,
|
|
171
169
|
candidate_indices=candidates,
|
|
172
170
|
base_vectors=base_vectors,
|
|
@@ -198,50 +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**, **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
|
|
207
|
-
from deglib.transformation import make_scalar_quantizer_int8
|
|
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
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
#
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
# 4. Convert mutable graph to a compact ReadOnlyGraph equipped with the quantized features
|
|
232
|
-
readonly_graph = graph.to_readonly(
|
|
233
|
-
feature_space=int8_space, custom_features=quant_int8_data, transformations=[quantizer]
|
|
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,
|
|
234
223
|
)
|
|
235
224
|
|
|
236
|
-
#
|
|
237
|
-
rerank_space = deglib.FloatSpace.create(dims, deglib.Metric.FP32_InnerProduct)
|
|
238
|
-
searcher = create_searcher(
|
|
239
|
-
graph=readonly_graph,
|
|
240
|
-
rerank_space=rerank_space,
|
|
241
|
-
rerank_data=sorted_data,
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
# 6. Query search (query is automatically quantized and top candidates are reranked)
|
|
225
|
+
# 3. Query search (query is automatically quantized and top candidates are reranked)
|
|
245
226
|
indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
246
227
|
print("Top-10 nearest neighbor indices:", indices)
|
|
247
228
|
print("Top-10 exact distances:", distances)
|
|
@@ -267,10 +248,13 @@ Controls the topology optimization strategy:
|
|
|
267
248
|
|
|
268
249
|
```python
|
|
269
250
|
# Mode 1: Relative distance margin (default: eps_or_ef=0.1)
|
|
270
|
-
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)
|
|
271
252
|
|
|
272
253
|
# Mode 2: Fixed candidate pool (HNSW-style)
|
|
273
|
-
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)
|
|
274
258
|
```
|
|
275
259
|
|
|
276
260
|
### Supported Metrics & Data Types
|
|
@@ -278,11 +262,17 @@ indices, distances = searcher.search(query, k=10, eps_or_ef=128)
|
|
|
278
262
|
- `Metric.FP32_InnerProduct`: Inner product / cosine distance (`np.float32`)
|
|
279
263
|
- `Metric.Uint8_L2`: 8-bit unsigned integer Euclidean distance (`np.uint8`)
|
|
280
264
|
- `Metric.Uint8_InnerProduct`: 8-bit unsigned integer inner product (`np.uint8`)
|
|
281
|
-
- `Metric.FP16_L2`: 16-bit half-precision Euclidean distance (`np.uint16`)
|
|
282
|
-
- `Metric.FP16_InnerProduct`: 16-bit half-precision inner product (`np.uint16`)
|
|
283
|
-
- `Metric.EVP_InnerProduct`: Quantized
|
|
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`)
|
|
284
268
|
- `Metric.Int8_InnerProduct`: 8-bit signed integer inner product (`np.int8`)
|
|
285
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`)
|
|
286
276
|
|
|
287
277
|
---
|
|
288
278
|
|
|
@@ -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() / "
|
|
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
|
|
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
|
-
|
|
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::
|
|
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::
|
|
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::
|
|
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::
|
|
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(
|
|
74
|
-
// invokes
|
|
73
|
+
// Splits [start, end) into min(end - start, actualThreads * chunksPerThread)
|
|
74
|
+
// contiguous chunks and invokes
|
|
75
75
|
// fn(begin, end, threadId)
|
|
76
|
-
//
|
|
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.
|
|
83
|
-
//
|
|
84
|
-
//
|
|
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
|
|
104
|
-
if (
|
|
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(
|
|
143
|
-
for (size_t threadId = 1; 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,
|
|
13
|
-
#define
|
|
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
|
}
|
|
@@ -119,7 +119,7 @@ class EvpInnerProduct_AVX512 {
|
|
|
119
119
|
public:
|
|
120
120
|
static constexpr const char* get_instruction() { return "AVX512"; }
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
DEGLIB_TARGET_AVX512_VPOPCNTDQ inline static float compare(const void* pVect1v, const void* pVect2v, const void* qty_ptr) {
|
|
123
123
|
const std::byte* a = static_cast<const std::byte*>(pVect1v);
|
|
124
124
|
const std::byte* b = static_cast<const std::byte*>(pVect2v);
|
|
125
125
|
const uint32_t dim = *static_cast<const uint32_t*>(qty_ptr);
|
|
@@ -325,7 +325,7 @@ inline DistanceVariant select_dist(const size_t dim, const deglib::cpu::Instruct
|
|
|
325
325
|
const auto target = deglib::cpu::resolve_instruction_set(instruction);
|
|
326
326
|
|
|
327
327
|
#if defined(DEGLIB_X86)
|
|
328
|
-
if (target == deglib::cpu::InstructionSet::AVX512 || target == deglib::cpu::InstructionSet::AVX512_VNNI) {
|
|
328
|
+
if ((target == deglib::cpu::InstructionSet::AVX512 || target == deglib::cpu::InstructionSet::AVX512_VNNI) && deglib::cpu::has_avx512_vpopcntdq()) {
|
|
329
329
|
if (dim < 512) {
|
|
330
330
|
return EvpInnerProduct_AVX512<ResidualMode::TailOnly>{};
|
|
331
331
|
} else if (dim < 1024) {
|
|
@@ -344,7 +344,11 @@ inline DistanceVariant select_dist(const size_t dim, const deglib::cpu::Instruct
|
|
|
344
344
|
else
|
|
345
345
|
return EvpInnerProduct_AVX512<ResidualMode::Full>{};
|
|
346
346
|
}
|
|
347
|
-
} else if (
|
|
347
|
+
} else if (
|
|
348
|
+
target == deglib::cpu::InstructionSet::AVX512 || target == deglib::cpu::InstructionSet::AVX512_VNNI || target == deglib::cpu::InstructionSet::AVX2 ||
|
|
349
|
+
target == deglib::cpu::InstructionSet::AVX2_VNNI
|
|
350
|
+
) {
|
|
351
|
+
// Fall back to AVX2 when AVX512_VPOPCNTDQ is not supported by the CPU (e.g. Cascade Lake).
|
|
348
352
|
if (dim < 256) {
|
|
349
353
|
return EvpInnerProduct_AVX2<ResidualMode::TailOnly>{};
|
|
350
354
|
} else if (dim < 512) {
|
|
@@ -317,13 +317,7 @@ inline void fp16_to_floats(const uint16_t* fp16_vals, float* floats, size_t coun
|
|
|
317
317
|
/**
|
|
318
318
|
* Computes centered vector (fp16 - mean) in float, and populates abs_vals for sorting.
|
|
319
319
|
*/
|
|
320
|
-
inline void center_and_abs(
|
|
321
|
-
const uint16_t* src_fp16,
|
|
322
|
-
const float* mean,
|
|
323
|
-
uint32_t dim,
|
|
324
|
-
float* out_centered,
|
|
325
|
-
std::pair<float, uint32_t>* out_abs_vals
|
|
326
|
-
) {
|
|
320
|
+
inline void center_and_abs(const uint16_t* src_fp16, const float* mean, uint32_t dim, float* out_centered, std::pair<float, uint32_t>* out_abs_vals) {
|
|
327
321
|
uint32_t d = 0;
|
|
328
322
|
#if defined(DEGLIB_X86)
|
|
329
323
|
if (deglib::cpu::has_avx2()) {
|
|
@@ -353,13 +347,8 @@ inline void center_and_abs(
|
|
|
353
347
|
* Directly subtracts mean_fp16 from src_fp16 in native FP16 hardware without converting to float,
|
|
354
348
|
* writes the FP16 difference into out_diff_fp16, and writes {abs_val (uint16_t), index} pairs.
|
|
355
349
|
*/
|
|
356
|
-
inline void
|
|
357
|
-
|
|
358
|
-
const uint16_t* mean_fp16,
|
|
359
|
-
uint32_t dim,
|
|
360
|
-
uint16_t* out_diff_fp16,
|
|
361
|
-
std::pair<uint16_t, uint32_t>* out_abs_vals
|
|
362
|
-
) {
|
|
350
|
+
inline void
|
|
351
|
+
sub_and_abs_fp16(const uint16_t* src_fp16, const uint16_t* mean_fp16, uint32_t dim, uint16_t* out_diff_fp16, std::pair<uint16_t, uint32_t>* out_abs_vals) {
|
|
363
352
|
uint32_t d = 0;
|
|
364
353
|
#if defined(DEGLIB_X86)
|
|
365
354
|
if (deglib::cpu::has_avx2()) {
|
|
@@ -429,7 +418,7 @@ inline void compute_column_mean(const uint16_t* src_fp16, size_t count, uint32_t
|
|
|
429
418
|
my_sum[d] += fp16_to_float(vec[d]);
|
|
430
419
|
}
|
|
431
420
|
}
|
|
432
|
-
});
|
|
421
|
+
}, 16);
|
|
433
422
|
|
|
434
423
|
const double inv_count = 1.0 / static_cast<double>(count);
|
|
435
424
|
for (uint32_t d = 0; d < dim; ++d) {
|
|
@@ -94,11 +94,13 @@ class InnerProductFP16_AVX512 {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
if constexpr (HasSimd) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
while (a + 15 < last) {
|
|
98
|
+
__m512 va1 = _mm512_cvtph_ps(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(a)));
|
|
99
|
+
__m512 vb1 = _mm512_cvtph_ps(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(b)));
|
|
100
|
+
sum512_1 = _mm512_fmadd_ps(va1, vb1, sum512_1);
|
|
101
|
+
a += 16;
|
|
102
|
+
b += 16;
|
|
103
|
+
}
|
|
102
104
|
}
|
|
103
105
|
|
|
104
106
|
// Horizontal reduce of SIMD accumulators
|
|
@@ -169,11 +171,13 @@ class InnerProductFP16_AVX2 {
|
|
|
169
171
|
}
|
|
170
172
|
}
|
|
171
173
|
if constexpr (HasSimd) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
174
|
+
while (a + 7 < last) {
|
|
175
|
+
__m256 va1 = _mm256_cvtph_ps(_mm_loadu_si128(reinterpret_cast<const __m128i*>(a)));
|
|
176
|
+
__m256 vb1 = _mm256_cvtph_ps(_mm_loadu_si128(reinterpret_cast<const __m128i*>(b)));
|
|
177
|
+
sum256_1 = _mm256_fmadd_ps(va1, vb1, sum256_1);
|
|
178
|
+
a += 8;
|
|
179
|
+
b += 8;
|
|
180
|
+
}
|
|
177
181
|
}
|
|
178
182
|
|
|
179
183
|
// Horizontal reduce of SIMD accumulators
|
|
@@ -96,12 +96,14 @@ class L2FP16_AVX512 {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
if constexpr (HasSimd) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
99
|
+
while (a + 15 < last) {
|
|
100
|
+
__m512 va1 = _mm512_cvtph_ps(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(a)));
|
|
101
|
+
__m512 vb1 = _mm512_cvtph_ps(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(b)));
|
|
102
|
+
__m512 diff1 = _mm512_sub_ps(va1, vb1);
|
|
103
|
+
sum512_1 = _mm512_fmadd_ps(diff1, diff1, sum512_1);
|
|
104
|
+
a += 16;
|
|
105
|
+
b += 16;
|
|
106
|
+
}
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
// Horizontal reduce of SIMD accumulators
|
|
@@ -177,12 +179,14 @@ class L2FP16_AVX2 {
|
|
|
177
179
|
}
|
|
178
180
|
}
|
|
179
181
|
if constexpr (HasSimd) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
182
|
+
while (a + 7 < last) {
|
|
183
|
+
__m256 va1 = _mm256_cvtph_ps(_mm_loadu_si128(reinterpret_cast<const __m128i*>(a)));
|
|
184
|
+
__m256 vb1 = _mm256_cvtph_ps(_mm_loadu_si128(reinterpret_cast<const __m128i*>(b)));
|
|
185
|
+
__m256 diff1 = _mm256_sub_ps(va1, vb1);
|
|
186
|
+
sum256_1 = _mm256_fmadd_ps(diff1, diff1, sum256_1);
|
|
187
|
+
a += 8;
|
|
188
|
+
b += 8;
|
|
189
|
+
}
|
|
186
190
|
}
|
|
187
191
|
|
|
188
192
|
// Horizontal reduce of SIMD accumulators
|