deglib 0.2.7__tar.gz → 0.2.8__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.7/src/deglib.egg-info → deglib-0.2.8}/PKG-INFO +10 -15
- {deglib-0.2.7 → deglib-0.2.8}/README.md +9 -14
- {deglib-0.2.7 → deglib-0.2.8}/lib/CMakeLists.txt +1 -1
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/evp_ip.h +0 -214
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/fp16_ip.h +0 -157
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/fp16_l2.h +0 -157
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/fp32_ip.h +0 -146
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/fp32_l2.h +0 -139
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/int8_ip.h +0 -245
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/int8_l2.h +0 -173
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/uint8_ip.h +0 -245
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/uint8_l2.h +0 -173
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distances.h +3 -29
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/search/kmeans.h +5 -3
- {deglib-0.2.7 → deglib-0.2.8}/src/deg_cpp/deglib_cpp.cpp +12 -4
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/__init__.py +1 -1
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/search.py +6 -6
- {deglib-0.2.7 → deglib-0.2.8/src/deglib.egg-info}/PKG-INFO +10 -15
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_searcher.py +24 -0
- {deglib-0.2.7 → deglib-0.2.8}/CMakeLists.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/LICENSE +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/MANIFEST.in +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/CMakeLists.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/benchmark.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/build.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/dataset.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/file_io.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/logging.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/repository.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/bench/include/stopwatch.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/cmake_modules/DetectCPUFeatures.cmake +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/CMakeLists.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/analysis.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/builder.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/concurrent.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/config.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/deglib.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/fp16.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/fp32.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/int8.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/residual_mode.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/distance/uint8.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/filter.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/dynamic_graph.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/internal_graph.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/intrusive_list.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/mutable_graph.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/readonly_graph.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/sizebounded_graph.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph/visited_list_pool.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/graph.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/optimization/flas/fast_linear_assignment_sorter_mt.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/optimization/flas/junker_volgenant_solver.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/optimization/pruning.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/optimization.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/search/reranker.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/search/result_list.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/search/searcher.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/search.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/serialization.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/transformation/evp.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/transformation/mips.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/transformation/normalize.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/transformation/scalar.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/transformation.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/utils/cpu.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/utils/memory.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/deglib/include/deglib/utils/random.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/test/CMakeLists.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/test/src/common/test_helpers.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/lib/test/src/unit/graph/test_dynamic_graph_common.h +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/pyproject.toml +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/setup.cfg +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/setup.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/analysis.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/builder.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/cpu.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/distances.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/graph.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/optimization.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/transformation.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib/utils.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib.egg-info/SOURCES.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib.egg-info/dependency_links.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib.egg-info/requires.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/src/deglib.egg-info/top_level.txt +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_analysis.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_builder.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_evp_quantizer.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_filter.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_graph.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_l2_normalize.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_presort.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_rerank.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_scalar_quantizer.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_transformation.py +0 -0
- {deglib-0.2.7 → deglib-0.2.8}/tests/test_transformation_graph.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deglib
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.8
|
|
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>
|
|
@@ -94,7 +94,7 @@ query = np.random.random(dims).astype(np.float32)
|
|
|
94
94
|
graph = deglib.builder.build_from_data(data, edges_per_vertex=32, show_progress=True)
|
|
95
95
|
|
|
96
96
|
# 3. Query top-k nearest neighbors
|
|
97
|
-
indices, distances = graph.search(query, k=10,
|
|
97
|
+
indices, distances = graph.search(query, k=10, eps_or_ef=0.1)
|
|
98
98
|
|
|
99
99
|
print("Nearest neighbors:", indices)
|
|
100
100
|
print("Distances:", distances)
|
|
@@ -226,13 +226,13 @@ batch_neighbors = graph.get_neighbors_batch(batch_entries, threads=4) # shape:
|
|
|
226
226
|
|
|
227
227
|
### Graph Optimization, Quantization & Reranking Pipeline
|
|
228
228
|
|
|
229
|
-
A complete end-to-end pipeline demonstrating **FLAS pre-sorting**, **multithreaded graph building**, **
|
|
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:
|
|
230
230
|
|
|
231
231
|
```python
|
|
232
232
|
import numpy as np
|
|
233
233
|
import deglib
|
|
234
|
-
from deglib.optimization import presort
|
|
235
|
-
from deglib.transformation import
|
|
234
|
+
from deglib.optimization import presort
|
|
235
|
+
from deglib.transformation import make_scalar_quantizer_int8
|
|
236
236
|
from deglib.search import create_searcher
|
|
237
237
|
|
|
238
238
|
num_vectors, dims = 10_000, 128
|
|
@@ -251,22 +251,17 @@ graph = deglib.builder.build_from_data(
|
|
|
251
251
|
show_progress=True,
|
|
252
252
|
)
|
|
253
253
|
|
|
254
|
-
# 3.
|
|
255
|
-
|
|
256
|
-
print(f"Pruned {pruned_count} redundant edges.")
|
|
257
|
-
|
|
258
|
-
# 4. Fit Int8 quantizer on base dataset and quantize features for memory reduction and ultra-fast search
|
|
259
|
-
quantizer = ScalarQuantizerInt8()
|
|
260
|
-
quantizer.fit(sorted_data)
|
|
254
|
+
# 3. Quantize features to Int8 for memory reduction and ultra-fast search
|
|
255
|
+
quantizer = make_scalar_quantizer_int8(sorted_data)
|
|
261
256
|
quant_int8_data = quantizer.quantize(sorted_data)
|
|
262
257
|
int8_space = deglib.FloatSpace.create(dims, deglib.Metric.Int8_InnerProduct)
|
|
263
258
|
|
|
264
|
-
#
|
|
259
|
+
# 4. Convert mutable graph to a compact ReadOnlyGraph equipped with the quantized features
|
|
265
260
|
readonly_graph = graph.to_readonly(
|
|
266
261
|
feature_space=int8_space, custom_features=quant_int8_data, transformations=[quantizer]
|
|
267
262
|
)
|
|
268
263
|
|
|
269
|
-
#
|
|
264
|
+
# 5. Create high-performance Searcher equipped with base vectors for exact candidate reranking
|
|
270
265
|
rerank_space = deglib.FloatSpace.create(dims, deglib.Metric.FP32_InnerProduct)
|
|
271
266
|
searcher = create_searcher(
|
|
272
267
|
graph=readonly_graph,
|
|
@@ -274,7 +269,7 @@ searcher = create_searcher(
|
|
|
274
269
|
rerank_data=sorted_data,
|
|
275
270
|
)
|
|
276
271
|
|
|
277
|
-
#
|
|
272
|
+
# 6. Query search (query is automatically quantized and top candidates are reranked)
|
|
278
273
|
indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
279
274
|
print("Top-10 nearest neighbor indices:", indices)
|
|
280
275
|
print("Top-10 exact distances:", distances)
|
|
@@ -66,7 +66,7 @@ query = np.random.random(dims).astype(np.float32)
|
|
|
66
66
|
graph = deglib.builder.build_from_data(data, edges_per_vertex=32, show_progress=True)
|
|
67
67
|
|
|
68
68
|
# 3. Query top-k nearest neighbors
|
|
69
|
-
indices, distances = graph.search(query, k=10,
|
|
69
|
+
indices, distances = graph.search(query, k=10, eps_or_ef=0.1)
|
|
70
70
|
|
|
71
71
|
print("Nearest neighbors:", indices)
|
|
72
72
|
print("Distances:", distances)
|
|
@@ -198,13 +198,13 @@ batch_neighbors = graph.get_neighbors_batch(batch_entries, threads=4) # shape:
|
|
|
198
198
|
|
|
199
199
|
### Graph Optimization, Quantization & Reranking Pipeline
|
|
200
200
|
|
|
201
|
-
A complete end-to-end pipeline demonstrating **FLAS pre-sorting**, **multithreaded graph building**, **
|
|
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:
|
|
202
202
|
|
|
203
203
|
```python
|
|
204
204
|
import numpy as np
|
|
205
205
|
import deglib
|
|
206
|
-
from deglib.optimization import presort
|
|
207
|
-
from deglib.transformation import
|
|
206
|
+
from deglib.optimization import presort
|
|
207
|
+
from deglib.transformation import make_scalar_quantizer_int8
|
|
208
208
|
from deglib.search import create_searcher
|
|
209
209
|
|
|
210
210
|
num_vectors, dims = 10_000, 128
|
|
@@ -223,22 +223,17 @@ graph = deglib.builder.build_from_data(
|
|
|
223
223
|
show_progress=True,
|
|
224
224
|
)
|
|
225
225
|
|
|
226
|
-
# 3.
|
|
227
|
-
|
|
228
|
-
print(f"Pruned {pruned_count} redundant edges.")
|
|
229
|
-
|
|
230
|
-
# 4. Fit Int8 quantizer on base dataset and quantize features for memory reduction and ultra-fast search
|
|
231
|
-
quantizer = ScalarQuantizerInt8()
|
|
232
|
-
quantizer.fit(sorted_data)
|
|
226
|
+
# 3. Quantize features to Int8 for memory reduction and ultra-fast search
|
|
227
|
+
quantizer = make_scalar_quantizer_int8(sorted_data)
|
|
233
228
|
quant_int8_data = quantizer.quantize(sorted_data)
|
|
234
229
|
int8_space = deglib.FloatSpace.create(dims, deglib.Metric.Int8_InnerProduct)
|
|
235
230
|
|
|
236
|
-
#
|
|
231
|
+
# 4. Convert mutable graph to a compact ReadOnlyGraph equipped with the quantized features
|
|
237
232
|
readonly_graph = graph.to_readonly(
|
|
238
233
|
feature_space=int8_space, custom_features=quant_int8_data, transformations=[quantizer]
|
|
239
234
|
)
|
|
240
235
|
|
|
241
|
-
#
|
|
236
|
+
# 5. Create high-performance Searcher equipped with base vectors for exact candidate reranking
|
|
242
237
|
rerank_space = deglib.FloatSpace.create(dims, deglib.Metric.FP32_InnerProduct)
|
|
243
238
|
searcher = create_searcher(
|
|
244
239
|
graph=readonly_graph,
|
|
@@ -246,7 +241,7 @@ searcher = create_searcher(
|
|
|
246
241
|
rerank_data=sorted_data,
|
|
247
242
|
)
|
|
248
243
|
|
|
249
|
-
#
|
|
244
|
+
# 6. Query search (query is automatically quantized and top candidates are reranked)
|
|
250
245
|
indices, distances = searcher.search(query, k=10, eps_or_ef=0.1, rerank_factor=1.5, return_distances=True)
|
|
251
246
|
print("Top-10 nearest neighbor indices:", indices)
|
|
252
247
|
print("Top-10 exact distances:", distances)
|
|
@@ -6,7 +6,7 @@ cmake_policy(SET CMP0054 NEW)
|
|
|
6
6
|
|
|
7
7
|
project(
|
|
8
8
|
deg_lib
|
|
9
|
-
HOMEPAGE_URL "https://github.com/
|
|
9
|
+
HOMEPAGE_URL "https://github.com/Visual-Computing/DynamicExplorationGraph"
|
|
10
10
|
DESCRIPTION
|
|
11
11
|
"Dynamic Exploration Graphs for fast approximate nearest neighbor search and navigation in large image datasets"
|
|
12
12
|
VERSION 0.0.1
|
|
@@ -48,12 +48,6 @@ class EvpInnerProduct {
|
|
|
48
48
|
return 1.f - (compare_naive(pVect1v, pVect2v, qty_ptr) / (2.f * static_cast<float>(*static_cast<const uint32_t*>(qty_ptr))));
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
inline static void compare_batch(const void* query_ptr, const void* const* db_arr, size_t count, const void* qty_ptr, float* dists) {
|
|
52
|
-
for (size_t i = 0; i < count; ++i) {
|
|
53
|
-
dists[i] = compare(query_ptr, db_arr[i], qty_ptr);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
51
|
inline static float compare_naive(const void* pVect1v, const void* pVect2v, const void* qty_ptr) {
|
|
58
52
|
const std::byte* a = static_cast<const std::byte*>(pVect1v);
|
|
59
53
|
const std::byte* b = static_cast<const std::byte*>(pVect2v);
|
|
@@ -203,104 +197,6 @@ class EvpInnerProduct_AVX512 {
|
|
|
203
197
|
const float sim = static_cast<float>(same_count + dim) - static_cast<float>(diff_count);
|
|
204
198
|
return 1.f - sim / (2.f * static_cast<float>(dim));
|
|
205
199
|
}
|
|
206
|
-
|
|
207
|
-
DEGLIB_TARGET_AVX512 inline static void compare_batch(const void* query_ptr, const void* const* db_arr, size_t count, const void* qty_ptr, float* dists) {
|
|
208
|
-
static constexpr size_t BATCH_SIZE = 4;
|
|
209
|
-
const std::byte* query = static_cast<const std::byte*>(query_ptr);
|
|
210
|
-
const uint32_t dim = *static_cast<const uint32_t*>(qty_ptr);
|
|
211
|
-
const size_t mask_bytes = (dim + 7) / 8;
|
|
212
|
-
|
|
213
|
-
auto batch_impl = [query, mask_bytes, dim](const void* const* db, float* out_dists) DEGLIB_TARGET_AVX512 {
|
|
214
|
-
const std::byte* q_ones = query;
|
|
215
|
-
const std::byte* q_negs = query + mask_bytes;
|
|
216
|
-
|
|
217
|
-
alignas(64) __m512i acc_same[BATCH_SIZE];
|
|
218
|
-
alignas(64) __m512i acc_diff[BATCH_SIZE];
|
|
219
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
220
|
-
acc_same[j] = _mm512_setzero_si512();
|
|
221
|
-
acc_diff[j] = _mm512_setzero_si512();
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const size_t block = 64;
|
|
225
|
-
size_t i = 0;
|
|
226
|
-
if constexpr (HasDualSimd) {
|
|
227
|
-
for (; i + 2 * block <= mask_bytes; i += 2 * block) {
|
|
228
|
-
__m512i q_o1 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&q_ones[i]));
|
|
229
|
-
__m512i q_n1 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&q_negs[i]));
|
|
230
|
-
__m512i q_o1b = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&q_ones[i + block]));
|
|
231
|
-
__m512i q_n1b = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&q_negs[i + block]));
|
|
232
|
-
|
|
233
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
234
|
-
const std::byte* db_ = static_cast<const std::byte*>(db[j]);
|
|
235
|
-
__m512i o2 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&db_[i]));
|
|
236
|
-
__m512i n2 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&db_[i + mask_bytes]));
|
|
237
|
-
__m512i o2b = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&db_[i + block]));
|
|
238
|
-
__m512i n2b = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&db_[i + block + mask_bytes]));
|
|
239
|
-
|
|
240
|
-
__m512i same = _mm512_or_si512(_mm512_and_si512(q_o1, o2), _mm512_and_si512(q_n1, n2));
|
|
241
|
-
__m512i diff = _mm512_or_si512(_mm512_and_si512(q_o1, n2), _mm512_and_si512(o2, q_n1));
|
|
242
|
-
acc_same[j] = _mm512_add_epi64(acc_same[j], _mm512_popcnt_epi64(same));
|
|
243
|
-
acc_diff[j] = _mm512_add_epi64(acc_diff[j], _mm512_popcnt_epi64(diff));
|
|
244
|
-
|
|
245
|
-
__m512i same_b = _mm512_or_si512(_mm512_and_si512(q_o1b, o2b), _mm512_and_si512(q_n1b, n2b));
|
|
246
|
-
__m512i diff_b = _mm512_or_si512(_mm512_and_si512(q_o1b, n2b), _mm512_and_si512(o2b, q_n1b));
|
|
247
|
-
acc_same[j] = _mm512_add_epi64(acc_same[j], _mm512_popcnt_epi64(same_b));
|
|
248
|
-
acc_diff[j] = _mm512_add_epi64(acc_diff[j], _mm512_popcnt_epi64(diff_b));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if constexpr (HasSimd) {
|
|
253
|
-
for (; i + block <= mask_bytes; i += block) {
|
|
254
|
-
__m512i q_o1 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&q_ones[i]));
|
|
255
|
-
__m512i q_n1 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&q_negs[i]));
|
|
256
|
-
|
|
257
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
258
|
-
const std::byte* db_ = static_cast<const std::byte*>(db[j]);
|
|
259
|
-
__m512i o2 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&db_[i]));
|
|
260
|
-
__m512i n2 = _mm512_loadu_si512(reinterpret_cast<const __m512i*>(&db_[i + mask_bytes]));
|
|
261
|
-
|
|
262
|
-
__m512i same = _mm512_or_si512(_mm512_and_si512(q_o1, o2), _mm512_and_si512(q_n1, n2));
|
|
263
|
-
__m512i diff = _mm512_or_si512(_mm512_and_si512(q_o1, n2), _mm512_and_si512(o2, q_n1));
|
|
264
|
-
acc_same[j] = _mm512_add_epi64(acc_same[j], _mm512_popcnt_epi64(same));
|
|
265
|
-
acc_diff[j] = _mm512_add_epi64(acc_diff[j], _mm512_popcnt_epi64(diff));
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
alignas(64) uint64_t same_res[BATCH_SIZE], diff_res[BATCH_SIZE];
|
|
271
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
272
|
-
same_res[j] = evp_hsum512(acc_same[j]);
|
|
273
|
-
diff_res[j] = evp_hsum512(acc_diff[j]);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if constexpr (HasTail) {
|
|
277
|
-
for (; i < mask_bytes; ++i) {
|
|
278
|
-
const unsigned int q_b1 = static_cast<uint8_t>(q_ones[i]);
|
|
279
|
-
const unsigned int q_n1 = static_cast<uint8_t>(q_negs[i]);
|
|
280
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
281
|
-
const std::byte* db_ = static_cast<const std::byte*>(db[j]);
|
|
282
|
-
const unsigned int b2 = static_cast<uint8_t>(db_[i]);
|
|
283
|
-
const unsigned int n2 = static_cast<uint8_t>(db_[i + mask_bytes]);
|
|
284
|
-
same_res[j] += std::popcount((q_b1 & b2) | (q_n1 & n2));
|
|
285
|
-
diff_res[j] += std::popcount((q_b1 & n2) | (b2 & q_n1));
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
291
|
-
const float sim = static_cast<float>(same_res[j] + dim) - static_cast<float>(diff_res[j]);
|
|
292
|
-
out_dists[j] = 1.f - sim / (2.f * static_cast<float>(dim));
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
size_t i = 0;
|
|
297
|
-
for (; i + BATCH_SIZE <= count; i += BATCH_SIZE) {
|
|
298
|
-
batch_impl(&db_arr[i], &dists[i]);
|
|
299
|
-
}
|
|
300
|
-
for (; i < count; ++i) {
|
|
301
|
-
dists[i] = compare(query_ptr, db_arr[i], qty_ptr);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
200
|
};
|
|
305
201
|
|
|
306
202
|
template <ResidualMode Mode = ResidualMode::Full>
|
|
@@ -401,116 +297,6 @@ class EvpInnerProduct_AVX2 {
|
|
|
401
297
|
const float sim = static_cast<float>(same_count + dim) - static_cast<float>(diff_count);
|
|
402
298
|
return 1.f - sim / (2.f * static_cast<float>(dim));
|
|
403
299
|
}
|
|
404
|
-
|
|
405
|
-
DEGLIB_TARGET_AVX2 inline static void compare_batch(const void* query_ptr, const void* const* db_arr, size_t count, const void* qty_ptr, float* dists) {
|
|
406
|
-
static constexpr size_t BATCH_SIZE = 4;
|
|
407
|
-
const std::byte* query = static_cast<const std::byte*>(query_ptr);
|
|
408
|
-
const uint32_t dim = *static_cast<const uint32_t*>(qty_ptr);
|
|
409
|
-
const size_t mask_bytes = (dim + 7) / 8;
|
|
410
|
-
|
|
411
|
-
auto batch_impl = [query, mask_bytes, dim](const void* const* db, float* out_dists) DEGLIB_TARGET_AVX2 {
|
|
412
|
-
const std::byte* q_ones = query;
|
|
413
|
-
const std::byte* q_negs = query + mask_bytes;
|
|
414
|
-
|
|
415
|
-
const __m256i lookup = _mm256_setr_epi8(0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4);
|
|
416
|
-
const __m256i low_mask = _mm256_set1_epi8(0x0F);
|
|
417
|
-
const __m256i zero = _mm256_setzero_si256();
|
|
418
|
-
|
|
419
|
-
auto vector_popcnt = [&](__m256i vec) {
|
|
420
|
-
__m256i lo = _mm256_and_si256(vec, low_mask);
|
|
421
|
-
__m256i hi = _mm256_and_si256(_mm256_srli_epi16(vec, 4), low_mask);
|
|
422
|
-
__m256i popcnt1 = _mm256_shuffle_epi8(lookup, lo);
|
|
423
|
-
__m256i popcnt2 = _mm256_shuffle_epi8(lookup, hi);
|
|
424
|
-
return _mm256_add_epi8(popcnt1, popcnt2);
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
alignas(32) __m256i acc_same[BATCH_SIZE];
|
|
428
|
-
alignas(32) __m256i acc_diff[BATCH_SIZE];
|
|
429
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
430
|
-
acc_same[j] = _mm256_setzero_si256();
|
|
431
|
-
acc_diff[j] = _mm256_setzero_si256();
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
const size_t block = 32;
|
|
435
|
-
size_t i = 0;
|
|
436
|
-
if constexpr (HasDualSimd) {
|
|
437
|
-
for (; i + 2 * block <= mask_bytes; i += 2 * block) {
|
|
438
|
-
__m256i q_o1 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&q_ones[i]));
|
|
439
|
-
__m256i q_n1 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&q_negs[i]));
|
|
440
|
-
__m256i q_o1b = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&q_ones[i + block]));
|
|
441
|
-
__m256i q_n1b = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&q_negs[i + block]));
|
|
442
|
-
|
|
443
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
444
|
-
const std::byte* db_ = static_cast<const std::byte*>(db[j]);
|
|
445
|
-
__m256i o2 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[i]));
|
|
446
|
-
__m256i n2 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[i + mask_bytes]));
|
|
447
|
-
__m256i o2b = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[i + block]));
|
|
448
|
-
__m256i n2b = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[i + block + mask_bytes]));
|
|
449
|
-
|
|
450
|
-
__m256i same = _mm256_or_si256(_mm256_and_si256(q_o1, o2), _mm256_and_si256(q_n1, n2));
|
|
451
|
-
__m256i diff = _mm256_or_si256(_mm256_and_si256(q_o1, n2), _mm256_and_si256(o2, q_n1));
|
|
452
|
-
acc_same[j] = _mm256_add_epi64(acc_same[j], _mm256_sad_epu8(vector_popcnt(same), zero));
|
|
453
|
-
acc_diff[j] = _mm256_add_epi64(acc_diff[j], _mm256_sad_epu8(vector_popcnt(diff), zero));
|
|
454
|
-
|
|
455
|
-
__m256i same_b = _mm256_or_si256(_mm256_and_si256(q_o1b, o2b), _mm256_and_si256(q_n1b, n2b));
|
|
456
|
-
__m256i diff_b = _mm256_or_si256(_mm256_and_si256(q_o1b, n2b), _mm256_and_si256(o2b, q_n1b));
|
|
457
|
-
acc_same[j] = _mm256_add_epi64(acc_same[j], _mm256_sad_epu8(vector_popcnt(same_b), zero));
|
|
458
|
-
acc_diff[j] = _mm256_add_epi64(acc_diff[j], _mm256_sad_epu8(vector_popcnt(diff_b), zero));
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if constexpr (HasSimd) {
|
|
463
|
-
for (; i + block <= mask_bytes; i += block) {
|
|
464
|
-
__m256i q_o1 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&q_ones[i]));
|
|
465
|
-
__m256i q_n1 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&q_negs[i]));
|
|
466
|
-
|
|
467
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
468
|
-
const std::byte* db_ = static_cast<const std::byte*>(db[j]);
|
|
469
|
-
__m256i o2 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[i]));
|
|
470
|
-
__m256i n2 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[i + mask_bytes]));
|
|
471
|
-
|
|
472
|
-
__m256i same = _mm256_or_si256(_mm256_and_si256(q_o1, o2), _mm256_and_si256(q_n1, n2));
|
|
473
|
-
__m256i diff = _mm256_or_si256(_mm256_and_si256(q_o1, n2), _mm256_and_si256(o2, q_n1));
|
|
474
|
-
acc_same[j] = _mm256_add_epi64(acc_same[j], _mm256_sad_epu8(vector_popcnt(same), zero));
|
|
475
|
-
acc_diff[j] = _mm256_add_epi64(acc_diff[j], _mm256_sad_epu8(vector_popcnt(diff), zero));
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
alignas(32) uint64_t same_res[BATCH_SIZE], diff_res[BATCH_SIZE];
|
|
481
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
482
|
-
same_res[j] = evp_hsum256(acc_same[j]);
|
|
483
|
-
diff_res[j] = evp_hsum256(acc_diff[j]);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
if constexpr (HasTail) {
|
|
487
|
-
for (; i < mask_bytes; ++i) {
|
|
488
|
-
const unsigned int q_b1 = static_cast<uint8_t>(q_ones[i]);
|
|
489
|
-
const unsigned int q_n1 = static_cast<uint8_t>(q_negs[i]);
|
|
490
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
491
|
-
const std::byte* db_ = static_cast<const std::byte*>(db[j]);
|
|
492
|
-
const unsigned int b2 = static_cast<uint8_t>(db_[i]);
|
|
493
|
-
const unsigned int n2 = static_cast<uint8_t>(db_[i + mask_bytes]);
|
|
494
|
-
same_res[j] += std::popcount((q_b1 & b2) | (q_n1 & n2));
|
|
495
|
-
diff_res[j] += std::popcount((q_b1 & n2) | (b2 & q_n1));
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
501
|
-
const float sim = static_cast<float>(same_res[j] + dim) - static_cast<float>(diff_res[j]);
|
|
502
|
-
out_dists[j] = 1.f - sim / (2.f * static_cast<float>(dim));
|
|
503
|
-
}
|
|
504
|
-
};
|
|
505
|
-
|
|
506
|
-
size_t i = 0;
|
|
507
|
-
for (; i + BATCH_SIZE <= count; i += BATCH_SIZE) {
|
|
508
|
-
batch_impl(&db_arr[i], &dists[i]);
|
|
509
|
-
}
|
|
510
|
-
for (; i < count; ++i) {
|
|
511
|
-
dists[i] = compare(query_ptr, db_arr[i], qty_ptr);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
300
|
};
|
|
515
301
|
#endif
|
|
516
302
|
|
|
@@ -36,11 +36,6 @@ class InnerProductFP16 {
|
|
|
36
36
|
}
|
|
37
37
|
return 1.f - result;
|
|
38
38
|
}
|
|
39
|
-
inline static void compare_batch(const void* query_ptr, const void* const* db_arr, size_t count, const void* qty_ptr, float* dists) {
|
|
40
|
-
for (size_t i = 0; i < count; ++i) {
|
|
41
|
-
dists[i] = compare(query_ptr, db_arr[i], qty_ptr);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
39
|
};
|
|
45
40
|
|
|
46
41
|
#if defined(DEGLIB_X86)
|
|
@@ -139,82 +134,6 @@ class InnerProductFP16_AVX512 {
|
|
|
139
134
|
|
|
140
135
|
return 1.f - result;
|
|
141
136
|
}
|
|
142
|
-
|
|
143
|
-
DEGLIB_TARGET_AVX512 inline static void compare_batch(const void* query_ptr, const void* const* db_arr, size_t count, const void* qty_ptr, float* dists) {
|
|
144
|
-
static constexpr size_t BATCH_SIZE = 8;
|
|
145
|
-
const uint16_t* query = static_cast<const uint16_t*>(query_ptr);
|
|
146
|
-
const size_t dim = *((const size_t*)qty_ptr);
|
|
147
|
-
|
|
148
|
-
auto batch_impl = [query, dim](const void* const* db, float* out_dists) DEGLIB_TARGET_AVX512 {
|
|
149
|
-
size_t offset = 0;
|
|
150
|
-
alignas(64) __m512 s[BATCH_SIZE];
|
|
151
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
152
|
-
s[j] = _mm512_setzero_ps();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if constexpr (HasDualSimd) {
|
|
156
|
-
const size_t nc32 = dim / 32;
|
|
157
|
-
offset = nc32 * 32;
|
|
158
|
-
|
|
159
|
-
for (size_t c = 0; c < nc32; ++c) {
|
|
160
|
-
size_t idx = c * 32;
|
|
161
|
-
__m256i q_raw_lo = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&query[idx]));
|
|
162
|
-
__m256i q_raw_hi = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&query[idx + 16]));
|
|
163
|
-
__m512 q_lo = _mm512_cvtph_ps(q_raw_lo);
|
|
164
|
-
__m512 q_hi = _mm512_cvtph_ps(q_raw_hi);
|
|
165
|
-
|
|
166
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
167
|
-
const uint16_t* db_ = static_cast<const uint16_t*>(db[j]);
|
|
168
|
-
__m256i r_lo_ = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[idx]));
|
|
169
|
-
__m256i r_hi_ = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[idx + 16]));
|
|
170
|
-
s[j] = _mm512_fmadd_ps(q_lo, _mm512_cvtph_ps(r_lo_), s[j]);
|
|
171
|
-
s[j] = _mm512_fmadd_ps(q_hi, _mm512_cvtph_ps(r_hi_), s[j]);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if constexpr (HasSimd) {
|
|
177
|
-
__m256i q_raw = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&query[offset]));
|
|
178
|
-
__m512 qf = _mm512_cvtph_ps(q_raw);
|
|
179
|
-
|
|
180
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
181
|
-
const uint16_t* db_ = static_cast<const uint16_t*>(db[j]);
|
|
182
|
-
__m256i r_raw = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&db_[offset]));
|
|
183
|
-
s[j] = _mm512_fmadd_ps(qf, _mm512_cvtph_ps(r_raw), s[j]);
|
|
184
|
-
}
|
|
185
|
-
offset += 16;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
189
|
-
out_dists[j] = fp16_hsum512(s[j]);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if constexpr (HasTail) {
|
|
193
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
194
|
-
const uint16_t* db_ptr = static_cast<const uint16_t*>(db[j]);
|
|
195
|
-
float tail_dot = 0.0f;
|
|
196
|
-
for (size_t k = offset; k < dim; ++k) {
|
|
197
|
-
float fa = deglib::distances::fp16::fp16_to_float(query[k]);
|
|
198
|
-
float fb = deglib::distances::fp16::fp16_to_float(db_ptr[k]);
|
|
199
|
-
tail_dot = std::fma(fa, fb, tail_dot);
|
|
200
|
-
}
|
|
201
|
-
out_dists[j] += tail_dot;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
206
|
-
out_dists[j] = 1.0f - out_dists[j];
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
size_t i = 0;
|
|
211
|
-
for (; i + BATCH_SIZE <= count; i += BATCH_SIZE) {
|
|
212
|
-
batch_impl(&db_arr[i], &dists[i]);
|
|
213
|
-
}
|
|
214
|
-
for (; i < count; ++i) {
|
|
215
|
-
dists[i] = compare(query_ptr, db_arr[i], qty_ptr);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
137
|
};
|
|
219
138
|
|
|
220
139
|
template <ResidualMode Mode = ResidualMode::Full>
|
|
@@ -282,82 +201,6 @@ class InnerProductFP16_AVX2 {
|
|
|
282
201
|
|
|
283
202
|
return 1.f - result;
|
|
284
203
|
}
|
|
285
|
-
|
|
286
|
-
DEGLIB_TARGET_AVX2 inline static void compare_batch(const void* query_ptr, const void* const* db_arr, size_t count, const void* qty_ptr, float* dists) {
|
|
287
|
-
static constexpr size_t BATCH_SIZE = 8;
|
|
288
|
-
const uint16_t* query = static_cast<const uint16_t*>(query_ptr);
|
|
289
|
-
const size_t dim = *((const size_t*)qty_ptr);
|
|
290
|
-
|
|
291
|
-
auto batch_impl = [query, dim](const void* const* db, float* out_dists) DEGLIB_TARGET_AVX2 {
|
|
292
|
-
size_t offset = 0;
|
|
293
|
-
alignas(32) __m256 s[BATCH_SIZE];
|
|
294
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
295
|
-
s[j] = _mm256_setzero_ps();
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if constexpr (HasDualSimd) {
|
|
299
|
-
const size_t nc = dim / 16;
|
|
300
|
-
offset = nc * 16;
|
|
301
|
-
|
|
302
|
-
for (size_t c = 0; c < nc; ++c) {
|
|
303
|
-
size_t idx = c * 16;
|
|
304
|
-
__m128i q_raw_lo = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&query[idx]));
|
|
305
|
-
__m128i q_raw_hi = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&query[idx + 8]));
|
|
306
|
-
__m256 q_lo = _mm256_cvtph_ps(q_raw_lo);
|
|
307
|
-
__m256 q_hi = _mm256_cvtph_ps(q_raw_hi);
|
|
308
|
-
|
|
309
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
310
|
-
const uint16_t* db_ = static_cast<const uint16_t*>(db[j]);
|
|
311
|
-
__m128i r_lo_ = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&db_[idx]));
|
|
312
|
-
__m128i r_hi_ = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&db_[idx + 8]));
|
|
313
|
-
s[j] = _mm256_fmadd_ps(q_lo, _mm256_cvtph_ps(r_lo_), s[j]);
|
|
314
|
-
s[j] = _mm256_fmadd_ps(q_hi, _mm256_cvtph_ps(r_hi_), s[j]);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
if constexpr (HasSimd) {
|
|
320
|
-
__m128i q_raw = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&query[offset]));
|
|
321
|
-
__m256 q_vec = _mm256_cvtph_ps(q_raw);
|
|
322
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
323
|
-
const uint16_t* db_ptr = static_cast<const uint16_t*>(db[j]);
|
|
324
|
-
__m128i r_raw = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&db_ptr[offset]));
|
|
325
|
-
__m256 r_vec = _mm256_cvtph_ps(r_raw);
|
|
326
|
-
s[j] = _mm256_fmadd_ps(q_vec, r_vec, s[j]);
|
|
327
|
-
}
|
|
328
|
-
offset += 8;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
332
|
-
out_dists[j] = fp16_hsum256(s[j]);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
if constexpr (HasTail) {
|
|
336
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
337
|
-
const uint16_t* db_ptr = static_cast<const uint16_t*>(db[j]);
|
|
338
|
-
float tail_dot = 0.0f;
|
|
339
|
-
for (size_t k = offset; k < dim; ++k) {
|
|
340
|
-
float fa = deglib::distances::fp16::fp16_to_float(query[k]);
|
|
341
|
-
float fb = deglib::distances::fp16::fp16_to_float(db_ptr[k]);
|
|
342
|
-
tail_dot = std::fma(fa, fb, tail_dot);
|
|
343
|
-
}
|
|
344
|
-
out_dists[j] += tail_dot;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
for (size_t j = 0; j < BATCH_SIZE; ++j) {
|
|
349
|
-
out_dists[j] = 1.0f - out_dists[j];
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
size_t i = 0;
|
|
354
|
-
for (; i + BATCH_SIZE <= count; i += BATCH_SIZE) {
|
|
355
|
-
batch_impl(&db_arr[i], &dists[i]);
|
|
356
|
-
}
|
|
357
|
-
for (; i < count; ++i) {
|
|
358
|
-
dists[i] = compare(query_ptr, db_arr[i], qty_ptr);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
204
|
};
|
|
362
205
|
#endif
|
|
363
206
|
|