faiss 0.6.5 → 0.6.6
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/faiss/extconf.rb +1 -0
- data/ext/faiss/index.cpp +42 -0
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/IVFlib.cpp +48 -51
- data/vendor/faiss/faiss/IVFlib.h +6 -2
- data/vendor/faiss/faiss/Index.cpp +2 -2
- data/vendor/faiss/faiss/Index.h +3 -3
- data/vendor/faiss/faiss/IndexBinary.cpp +2 -2
- data/vendor/faiss/faiss/IndexBinary.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +6 -3
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +6 -11
- data/vendor/faiss/faiss/IndexFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexFlat.cpp +1 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +12 -10
- data/vendor/faiss/faiss/IndexHNSW.h +2 -5
- data/vendor/faiss/faiss/IndexIDMap.cpp +17 -4
- data/vendor/faiss/faiss/IndexIDMap.h +10 -5
- data/vendor/faiss/faiss/IndexIVF.cpp +68 -37
- data/vendor/faiss/faiss/IndexIVF.h +37 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -15
- data/vendor/faiss/faiss/IndexIVFFlat.h +6 -6
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +28 -44
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +2 -11
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +7 -2
- data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -0
- data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
- data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +2 -2
- data/vendor/faiss/faiss/SuperKMeans.cpp +2 -2
- data/vendor/faiss/faiss/VectorTransform.cpp +156 -9
- data/vendor/faiss/faiss/VectorTransform.h +24 -1
- data/vendor/faiss/faiss/factory_tools.cpp +19 -1
- data/vendor/faiss/faiss/gpu/Dataset.h +66 -14
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
- data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
- data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +31 -50
- data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
- data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
- data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
- data/vendor/faiss/faiss/impl/HNSW.cpp +2 -2
- data/vendor/faiss/faiss/impl/HNSW.h +2 -2
- data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
- data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
- data/vendor/faiss/faiss/impl/PiPNN.cpp +4 -2
- data/vendor/faiss/faiss/impl/PiPNN.h +1 -1
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +29 -0
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +14 -13
- data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
- data/vendor/faiss/faiss/impl/expanded_scanners.h +29 -24
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +68 -6
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +13 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +84 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +64 -55
- data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
- data/vendor/faiss/faiss/impl/index_read.cpp +146 -9
- data/vendor/faiss/faiss/impl/index_write.cpp +29 -5
- data/vendor/faiss/faiss/impl/io.cpp +1 -0
- data/vendor/faiss/faiss/impl/io_macros.h +2 -2
- data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
- data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
- data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
- data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +1 -1
- data/vendor/faiss/faiss/impl/platform_macros.h +47 -28
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +6 -1
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
- data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
- data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -2
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +26 -26
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +48 -45
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +34 -32
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +8 -6
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +4 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +34 -33
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
- data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
- data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
- data/vendor/faiss/faiss/index_factory.cpp +30 -6
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
- data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
- data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +12 -9
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
- data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
- data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
- data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
- data/vendor/faiss/faiss/utils/distances.cpp +246 -34
- data/vendor/faiss/faiss/utils/distances.h +145 -2
- data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
- data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
- data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
- data/vendor/faiss/faiss/utils/rabitq_simd.h +120 -6
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +50 -10
- data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +13 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +131 -36
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +156 -45
- data/vendor/faiss/faiss/utils/simd_levels.cpp +3 -3
- data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
- data/vendor/faiss/faiss/utils/utils.cpp +4 -0
- metadata +20 -1
|
@@ -271,9 +271,7 @@ void search_knn_hamming_count(
|
|
|
271
271
|
}
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
-
|
|
275
|
-
indexIVF_stats.nlist += nlistv;
|
|
276
|
-
indexIVF_stats.ndis += ndis;
|
|
274
|
+
add_search_stats(nx, nlistv, ndis);
|
|
277
275
|
}
|
|
278
276
|
|
|
279
277
|
/* Manages NQ queries at a time, stores results */
|
|
@@ -69,8 +69,9 @@ size_t run_scan_codes1(
|
|
|
69
69
|
|
|
70
70
|
// Batched variant of run_scan_codes1 for the SQ scanners: distances four codes
|
|
71
71
|
// per step via distance_to_codes_batch_4, then applies the threshold and heap
|
|
72
|
-
// updates in id order so results match run_scan_codes1.
|
|
73
|
-
|
|
72
|
+
// updates in id order so results match run_scan_codes1. With use_sel, the
|
|
73
|
+
// selector is tested only on codes that beat the threshold.
|
|
74
|
+
template <class ScannerType, typename C, bool store_pairs, bool use_sel>
|
|
74
75
|
size_t run_scan_codes4(
|
|
75
76
|
const ScannerType& scanner,
|
|
76
77
|
size_t list_size,
|
|
@@ -81,6 +82,21 @@ size_t run_scan_codes4(
|
|
|
81
82
|
size_t list_no = scanner.list_no;
|
|
82
83
|
size_t code_size = scanner.code_size;
|
|
83
84
|
float threshold = handler.threshold;
|
|
85
|
+
auto add = [&](size_t j, float dis) {
|
|
86
|
+
if (!C::cmp(threshold, dis)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
int64_t id = store_pairs ? lo_build(list_no, j) : ids[j];
|
|
90
|
+
// No scan context: few codes get here, so a lookahead would be wasted.
|
|
91
|
+
if (use_sel && !scanner.sel->is_member(id)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (handler.add_result(dis, id)) {
|
|
95
|
+
handler.stats.nheap_updates++;
|
|
96
|
+
nup++;
|
|
97
|
+
threshold = handler.threshold;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
84
100
|
|
|
85
101
|
size_t j = 0;
|
|
86
102
|
for (; j + 4 <= list_size; j += 4) {
|
|
@@ -96,15 +112,7 @@ size_t run_scan_codes4(
|
|
|
96
112
|
dis[3]);
|
|
97
113
|
handler.stats.scan_cnt += 4;
|
|
98
114
|
for (size_t b = 0; b < 4; b++) {
|
|
99
|
-
|
|
100
|
-
int64_t id =
|
|
101
|
-
store_pairs ? lo_build(list_no, j + b) : ids[j + b];
|
|
102
|
-
if (handler.add_result(dis[b], id)) {
|
|
103
|
-
handler.stats.nheap_updates++;
|
|
104
|
-
nup++;
|
|
105
|
-
threshold = handler.threshold;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
115
|
+
add(j + b, dis[b]);
|
|
108
116
|
}
|
|
109
117
|
codes += 4 * code_size;
|
|
110
118
|
}
|
|
@@ -112,15 +120,7 @@ size_t run_scan_codes4(
|
|
|
112
120
|
// tail: the final < 4 codes, one at a time
|
|
113
121
|
for (; j < list_size; j++) {
|
|
114
122
|
handler.stats.scan_cnt++;
|
|
115
|
-
|
|
116
|
-
if (C::cmp(threshold, dis)) {
|
|
117
|
-
int64_t id = store_pairs ? lo_build(list_no, j) : ids[j];
|
|
118
|
-
if (handler.add_result(dis, id)) {
|
|
119
|
-
handler.stats.nheap_updates++;
|
|
120
|
-
nup++;
|
|
121
|
-
threshold = handler.threshold;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
123
|
+
add(j, scanner.distance_to_code(codes));
|
|
124
124
|
codes += code_size;
|
|
125
125
|
}
|
|
126
126
|
|
|
@@ -196,8 +196,9 @@ size_t run_scan_codes_fix_C(
|
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
// Routing wrapper for the SQ scanners: the no-selector path
|
|
200
|
-
//
|
|
199
|
+
// Routing wrapper for the SQ scanners: the no-selector path and a selector
|
|
200
|
+
// tested after the distance take the batched run_scan_codes4, a selector tested
|
|
201
|
+
// first stays on run_scan_codes1.
|
|
201
202
|
template <class C, class ScannerType>
|
|
202
203
|
size_t run_scan_codes4_fix_C(
|
|
203
204
|
const ScannerType& scanner,
|
|
@@ -205,6 +206,10 @@ size_t run_scan_codes4_fix_C(
|
|
|
205
206
|
const uint8_t* codes,
|
|
206
207
|
const idx_t* ids,
|
|
207
208
|
ResultHandler& handler) {
|
|
209
|
+
if (scanner.sel && scanner.sel_after_distance && !scanner.store_pairs) {
|
|
210
|
+
return run_scan_codes4<ScannerType, C, false, true>(
|
|
211
|
+
scanner, list_size, codes, ids, handler);
|
|
212
|
+
}
|
|
208
213
|
if (scanner.sel) {
|
|
209
214
|
if (scanner.store_pairs) {
|
|
210
215
|
return run_scan_codes1<ScannerType, C, true, true>(
|
|
@@ -215,10 +220,10 @@ size_t run_scan_codes4_fix_C(
|
|
|
215
220
|
}
|
|
216
221
|
} else {
|
|
217
222
|
if (scanner.store_pairs) {
|
|
218
|
-
return run_scan_codes4<ScannerType, C, true>(
|
|
223
|
+
return run_scan_codes4<ScannerType, C, true, false>(
|
|
219
224
|
scanner, list_size, codes, ids, handler);
|
|
220
225
|
} else {
|
|
221
|
-
return run_scan_codes4<ScannerType, C, false>(
|
|
226
|
+
return run_scan_codes4<ScannerType, C, false, false>(
|
|
222
227
|
scanner, list_size, codes, ids, handler);
|
|
223
228
|
}
|
|
224
229
|
}
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
#include <faiss/impl/fast_scan/simd_result_handlers.h>
|
|
13
13
|
#include <faiss/impl/simd_dispatch.h>
|
|
14
14
|
|
|
15
|
+
#include <algorithm>
|
|
15
16
|
#include <array>
|
|
16
17
|
|
|
17
18
|
namespace faiss {
|
|
@@ -39,8 +40,8 @@ void get_matrix_column(
|
|
|
39
40
|
int64_t i,
|
|
40
41
|
int64_t j,
|
|
41
42
|
TA& dest) {
|
|
42
|
-
for (int64_t k = 0; k < dest.size(); k++) {
|
|
43
|
-
if (k + i >= 0 && k + i < m) {
|
|
43
|
+
for (int64_t k = 0; k < static_cast<int64_t>(dest.size()); k++) {
|
|
44
|
+
if (k + i >= 0 && k + i < static_cast<int64_t>(m)) {
|
|
44
45
|
dest[k] = src[(k + i) * n + j];
|
|
45
46
|
} else {
|
|
46
47
|
dest[k] = 0;
|
|
@@ -88,7 +89,7 @@ void pq4_pack_codes(
|
|
|
88
89
|
|
|
89
90
|
uint8_t* codes2 = blocks;
|
|
90
91
|
for (size_t i0 = 0; i0 < nb; i0 += bbs) {
|
|
91
|
-
for (
|
|
92
|
+
for (size_t sq = 0; sq < nsq; sq += 2) {
|
|
92
93
|
for (size_t i = 0; i < bbs; i += 32) {
|
|
93
94
|
std::array<uint8_t, 32> c, c0, c1;
|
|
94
95
|
get_matrix_column(
|
|
@@ -147,7 +148,7 @@ void pq4_pack_codes_range(
|
|
|
147
148
|
for (size_t b = block0; b < block1; b++) {
|
|
148
149
|
uint8_t* codes2 = blocks + b * block_stride;
|
|
149
150
|
int64_t i_base = b * bbs - i0;
|
|
150
|
-
for (
|
|
151
|
+
for (size_t sq = 0; sq < nsq; sq += 2) {
|
|
151
152
|
for (size_t i = 0; i < bbs; i += 32) {
|
|
152
153
|
std::array<uint8_t, 32> c, c0, c1;
|
|
153
154
|
get_matrix_column(
|
|
@@ -241,8 +242,8 @@ void pq4_set_packed_element(
|
|
|
241
242
|
* CodePackerPQ4 implementation
|
|
242
243
|
***************************************************************/
|
|
243
244
|
|
|
244
|
-
CodePackerPQ4::CodePackerPQ4(size_t
|
|
245
|
-
|
|
245
|
+
CodePackerPQ4::CodePackerPQ4(size_t nsq_in, size_t bbs) {
|
|
246
|
+
nsq = nsq_in;
|
|
246
247
|
nvec = bbs;
|
|
247
248
|
code_size = (nsq * 4 + 7) / 8;
|
|
248
249
|
block_size = ((nsq + 1) / 2) * bbs;
|
|
@@ -437,6 +438,67 @@ void accumulate_to_mem(
|
|
|
437
438
|
});
|
|
438
439
|
}
|
|
439
440
|
|
|
441
|
+
/***************************************************************
|
|
442
|
+
* accumulate_hacc: NONE specialization + runtime dispatch.
|
|
443
|
+
***************************************************************/
|
|
444
|
+
|
|
445
|
+
template <>
|
|
446
|
+
void accumulate_hacc_impl<SIMDLevel::NONE>(
|
|
447
|
+
int nq,
|
|
448
|
+
size_t ntotal2,
|
|
449
|
+
int nsq,
|
|
450
|
+
const uint8_t* codes,
|
|
451
|
+
const uint8_t* LUT,
|
|
452
|
+
int32_t* accu) {
|
|
453
|
+
// byte j of a 16-byte half holds the codes of elements perm0[j] (low
|
|
454
|
+
// nibble) and perm0[j] + 16 (high nibble), as packed by pq4_pack_codes
|
|
455
|
+
#ifdef FAISS_BIG_ENDIAN
|
|
456
|
+
const uint8_t perm0[16] = {
|
|
457
|
+
8, 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7};
|
|
458
|
+
#else
|
|
459
|
+
const uint8_t perm0[16] = {
|
|
460
|
+
0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15};
|
|
461
|
+
#endif
|
|
462
|
+
const size_t block_stride = 32 * size_t(nsq) / 2;
|
|
463
|
+
for (int q = 0; q < nq; q++) {
|
|
464
|
+
const uint8_t* lut_lo = LUT + size_t(q) * 2 * 16 * nsq;
|
|
465
|
+
const uint8_t* lut_hi = lut_lo + 16 * size_t(nsq);
|
|
466
|
+
for (size_t b = 0; b < ntotal2 / 32; b++) {
|
|
467
|
+
const uint8_t* block = codes + b * block_stride;
|
|
468
|
+
int32_t* out = accu + q * ntotal2 + b * 32;
|
|
469
|
+
std::fill(out, out + 32, 0);
|
|
470
|
+
for (int sq = 0; sq < nsq; sq++) {
|
|
471
|
+
const uint8_t* c = block + (sq / 2) * 32 + (sq % 2) * 16;
|
|
472
|
+
const uint8_t* lo = lut_lo + 16 * sq;
|
|
473
|
+
const uint8_t* hi = lut_hi + 16 * sq;
|
|
474
|
+
for (int j = 0; j < 16; j++) {
|
|
475
|
+
const int c0 = c[j] & 15, c1 = c[j] >> 4;
|
|
476
|
+
out[perm0[j]] += lo[c0] + (int32_t(hi[c0]) << 8);
|
|
477
|
+
out[perm0[j] + 16] += lo[c1] + (int32_t(hi[c1]) << 8);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
void accumulate_hacc(
|
|
485
|
+
int nq,
|
|
486
|
+
size_t ntotal2,
|
|
487
|
+
int nsq,
|
|
488
|
+
const uint8_t* codes,
|
|
489
|
+
const uint8_t* LUT,
|
|
490
|
+
int32_t* accu) {
|
|
491
|
+
FAISS_THROW_IF_NOT(ntotal2 % 32 == 0);
|
|
492
|
+
FAISS_THROW_IF_NOT(nsq % 2 == 0);
|
|
493
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
494
|
+
nsq <= 32768,
|
|
495
|
+
"accumulate_hacc: nsq=%d > 32768 would overflow the int32 sums",
|
|
496
|
+
nsq);
|
|
497
|
+
with_selected_simd_levels<FAST_SCAN_HACC_SIMD_LEVELS>([&]<SIMDLevel SL>() {
|
|
498
|
+
accumulate_hacc_impl<SL>(nq, ntotal2, nsq, codes, LUT, accu);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
440
502
|
} // namespace faiss
|
|
441
503
|
|
|
442
504
|
namespace faiss {
|
|
@@ -178,6 +178,49 @@ void accumulate_to_mem_impl(
|
|
|
178
178
|
const uint8_t* LUT,
|
|
179
179
|
uint16_t* accu);
|
|
180
180
|
|
|
181
|
+
/// SIMD levels with high-accuracy FastScan accumulation implementations.
|
|
182
|
+
/// The AVX512 kernel returns wrong sums when compiled with MSVC, where the
|
|
183
|
+
/// AVX2 one is used instead.
|
|
184
|
+
#ifdef _MSC_VER
|
|
185
|
+
constexpr int FAST_SCAN_HACC_SIMD_LEVELS =
|
|
186
|
+
(1 << int(SIMDLevel::NONE)) | (1 << int(SIMDLevel::AVX2));
|
|
187
|
+
#else
|
|
188
|
+
constexpr int FAST_SCAN_HACC_SIMD_LEVELS = (1 << int(SIMDLevel::NONE)) |
|
|
189
|
+
(1 << int(SIMDLevel::AVX2)) | (1 << int(SIMDLevel::AVX512)) |
|
|
190
|
+
(1 << int(SIMDLevel::ARM_NEON));
|
|
191
|
+
#endif
|
|
192
|
+
|
|
193
|
+
/** Same as accumulate_to_mem, but with 16-bit look-up table entries summed
|
|
194
|
+
* exactly into 32-bit accumulators for nsq <= 32768. Each 16-bit table is given
|
|
195
|
+
* as two 8-bit tables: the low bytes of its entries, then the high bytes.
|
|
196
|
+
*
|
|
197
|
+
* @param nq number of queries
|
|
198
|
+
* @param ntotal2 number of database elements (multiple of 32)
|
|
199
|
+
* @param nsq number of sub-quantizers (multiple of 2)
|
|
200
|
+
* @param codes packed codes array, in blocks of 32 elements
|
|
201
|
+
* @param LUT for each query, 16 * nsq bytes of low bytes, then 16 * nsq
|
|
202
|
+
* bytes of high bytes, each laid out as by
|
|
203
|
+
* pq4_pack_LUT(1, nsq, ...)
|
|
204
|
+
* @param accu array to store the results, size nq * ntotal2
|
|
205
|
+
*/
|
|
206
|
+
void accumulate_hacc(
|
|
207
|
+
int nq,
|
|
208
|
+
size_t ntotal2,
|
|
209
|
+
int nsq,
|
|
210
|
+
const uint8_t* codes,
|
|
211
|
+
const uint8_t* LUT,
|
|
212
|
+
int32_t* accu);
|
|
213
|
+
|
|
214
|
+
/// Per-SIMD specialization of accumulate_hacc (defined in per-SIMD TUs)
|
|
215
|
+
template <SIMDLevel SL>
|
|
216
|
+
void accumulate_hacc_impl(
|
|
217
|
+
int nq,
|
|
218
|
+
size_t ntotal2,
|
|
219
|
+
int nsq,
|
|
220
|
+
const uint8_t* codes,
|
|
221
|
+
const uint8_t* LUT,
|
|
222
|
+
int32_t* accu);
|
|
223
|
+
|
|
181
224
|
/***************************************************************
|
|
182
225
|
* FastScanCodeScanner: virtual base that bundles handler + kernel
|
|
183
226
|
* behind the SIMD dispatch boundary. Per-SIMD TUs instantiate this
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
#include <faiss/impl/fast_scan/rabitq_dispatching.h> // IWYU pragma: keep
|
|
11
11
|
|
|
12
12
|
#include <faiss/impl/fast_scan/decompose_qbs.h>
|
|
13
|
+
#include <faiss/impl/fast_scan/kernels_simd256.h>
|
|
14
|
+
|
|
15
|
+
#include <cstdint>
|
|
13
16
|
|
|
14
17
|
namespace faiss {
|
|
15
18
|
|
|
@@ -29,4 +32,16 @@ void accumulate_to_mem_impl<SIMDLevel::AVX2>(
|
|
|
29
32
|
nq, ntotal2, nsq, codes, LUT, handler, scaler, 32 * nsq / 2);
|
|
30
33
|
}
|
|
31
34
|
|
|
35
|
+
template <>
|
|
36
|
+
void accumulate_hacc_impl<SIMDLevel::AVX2>(
|
|
37
|
+
int nq,
|
|
38
|
+
size_t ntotal2,
|
|
39
|
+
int nsq,
|
|
40
|
+
const uint8_t* codes,
|
|
41
|
+
const uint8_t* LUT,
|
|
42
|
+
int32_t* accu) {
|
|
43
|
+
accumulate_hacc_simdlib<SIMDLevel::AVX2>(
|
|
44
|
+
nq, ntotal2, nsq, codes, LUT, accu);
|
|
45
|
+
}
|
|
46
|
+
|
|
32
47
|
} // namespace faiss
|
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
#include <faiss/impl/fast_scan/rabitq_dispatching.h> // IWYU pragma: keep
|
|
11
11
|
|
|
12
12
|
#include <faiss/impl/fast_scan/decompose_qbs.h>
|
|
13
|
+
#include <faiss/impl/fast_scan/kernels_simd512.h>
|
|
14
|
+
|
|
15
|
+
#include <immintrin.h>
|
|
16
|
+
#include <algorithm>
|
|
17
|
+
#include <cstdint>
|
|
13
18
|
|
|
14
19
|
namespace faiss {
|
|
15
20
|
|
|
@@ -33,4 +38,114 @@ void accumulate_to_mem_impl<SIMDLevel::AVX512>(
|
|
|
33
38
|
nq, ntotal2, nsq, codes, LUT, handler, scaler, 32 * nsq / 2);
|
|
34
39
|
}
|
|
35
40
|
|
|
41
|
+
/* accumulate_hacc over one block of 32 codes. One 512-bit register holds 4
|
|
42
|
+
* sub-quantizers, one per 128-bit lane. The sums of two lanes are added in
|
|
43
|
+
* 16 bits before widening, so segments of 512 sub-quantizers keep them below
|
|
44
|
+
* 255 * 512 / 2 < 2^16. */
|
|
45
|
+
static inline void accumulate_hacc_block_avx512(
|
|
46
|
+
int nsq,
|
|
47
|
+
const uint8_t* codes,
|
|
48
|
+
const uint8_t* lut_lo,
|
|
49
|
+
const uint8_t* lut_hi,
|
|
50
|
+
int32_t* accu) {
|
|
51
|
+
constexpr int kSegmentSq = 512;
|
|
52
|
+
const __m512i low_mask = _mm512_set1_epi8(0xf);
|
|
53
|
+
__m512i res[2] = {_mm512_setzero_si512(), _mm512_setzero_si512()};
|
|
54
|
+
|
|
55
|
+
for (int seg = 0; seg < nsq; seg += kSegmentSq) {
|
|
56
|
+
const int seg_end = std::min(nsq, seg + kSegmentSq);
|
|
57
|
+
// 4 sub-quantizers from sq on; when only 2 are left, the upper
|
|
58
|
+
// lanes are zero: zero codes look up zero table entries
|
|
59
|
+
auto load4 = [seg_end](const uint8_t* p, int sq) {
|
|
60
|
+
if (sq + 4 <= seg_end) {
|
|
61
|
+
return _mm512_loadu_si512(p);
|
|
62
|
+
}
|
|
63
|
+
return _mm512_inserti64x4(
|
|
64
|
+
_mm512_setzero_si512(),
|
|
65
|
+
_mm256_loadu_si256((const __m256i*)p),
|
|
66
|
+
0);
|
|
67
|
+
};
|
|
68
|
+
// [low or high byte table][elements 0-15 even / odd bytes,
|
|
69
|
+
// elements 16-31 even / odd bytes]
|
|
70
|
+
__m512i acc[2][4];
|
|
71
|
+
for (auto& a : acc) {
|
|
72
|
+
for (auto& reg : a) {
|
|
73
|
+
reg = _mm512_setzero_si512();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (int sq = seg; sq < seg_end; sq += 4) {
|
|
77
|
+
const __m512i c = load4(codes + 16 * sq, sq);
|
|
78
|
+
const __m512i clo = _mm512_and_si512(c, low_mask);
|
|
79
|
+
const __m512i chi =
|
|
80
|
+
_mm512_and_si512(_mm512_srli_epi16(c, 4), low_mask);
|
|
81
|
+
const uint8_t* luts[2] = {lut_lo + 16 * sq, lut_hi + 16 * sq};
|
|
82
|
+
for (int t = 0; t < 2; t++) {
|
|
83
|
+
const __m512i lut = load4(luts[t], sq);
|
|
84
|
+
const __m512i res_lo = _mm512_shuffle_epi8(lut, clo);
|
|
85
|
+
const __m512i res_hi = _mm512_shuffle_epi8(lut, chi);
|
|
86
|
+
acc[t][0] = _mm512_add_epi16(acc[t][0], res_lo);
|
|
87
|
+
acc[t][1] = _mm512_add_epi16(
|
|
88
|
+
acc[t][1], _mm512_srli_epi16(res_lo, 8));
|
|
89
|
+
acc[t][2] = _mm512_add_epi16(acc[t][2], res_hi);
|
|
90
|
+
acc[t][3] = _mm512_add_epi16(
|
|
91
|
+
acc[t][3], _mm512_srli_epi16(res_hi, 8));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// add the two 256-bit halves, then the lanes holding the same
|
|
96
|
+
// elements, widening to 32 bits
|
|
97
|
+
auto combine = [](__m512i even, __m512i odd) {
|
|
98
|
+
__m256i e = _mm256_add_epi16(
|
|
99
|
+
_mm512_castsi512_si256(even),
|
|
100
|
+
_mm512_extracti64x4_epi64(even, 1));
|
|
101
|
+
__m256i o = _mm256_add_epi16(
|
|
102
|
+
_mm512_castsi512_si256(odd),
|
|
103
|
+
_mm512_extracti64x4_epi64(odd, 1));
|
|
104
|
+
e = _mm256_sub_epi16(e, _mm256_slli_epi16(o, 8));
|
|
105
|
+
return _mm512_add_epi32(
|
|
106
|
+
_mm512_cvtepu16_epi32(
|
|
107
|
+
_mm256_permute2f128_si256(e, o, 0x21)),
|
|
108
|
+
_mm512_cvtepu16_epi32(_mm256_blend_epi32(e, o, 0xF0)));
|
|
109
|
+
};
|
|
110
|
+
__m512i dis0[2], dis1[2];
|
|
111
|
+
for (int t = 0; t < 2; t++) {
|
|
112
|
+
dis0[t] = combine(acc[t][0], acc[t][1]);
|
|
113
|
+
dis1[t] = combine(acc[t][2], acc[t][3]);
|
|
114
|
+
}
|
|
115
|
+
// low + (high << 8)
|
|
116
|
+
res[0] = _mm512_add_epi32(
|
|
117
|
+
res[0],
|
|
118
|
+
_mm512_add_epi32(dis0[0], _mm512_slli_epi32(dis0[1], 8)));
|
|
119
|
+
res[1] = _mm512_add_epi32(
|
|
120
|
+
res[1],
|
|
121
|
+
_mm512_add_epi32(dis1[0], _mm512_slli_epi32(dis1[1], 8)));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_mm512_storeu_si512(accu, res[0]); // elements 0 to 15
|
|
125
|
+
_mm512_storeu_si512(accu + 16, res[1]); // elements 16 to 31
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
template <>
|
|
129
|
+
void accumulate_hacc_impl<SIMDLevel::AVX512>(
|
|
130
|
+
int nq,
|
|
131
|
+
size_t ntotal2,
|
|
132
|
+
int nsq,
|
|
133
|
+
const uint8_t* codes,
|
|
134
|
+
const uint8_t* LUT,
|
|
135
|
+
int32_t* accu) {
|
|
136
|
+
const size_t block_stride = 32 * size_t(nsq) / 2;
|
|
137
|
+
for (int q = 0; q < nq; q++) {
|
|
138
|
+
const uint8_t* lut_lo = LUT + size_t(q) * 2 * 16 * nsq;
|
|
139
|
+
const uint8_t* lut_hi = lut_lo + 16 * size_t(nsq);
|
|
140
|
+
for (size_t b = 0; b < ntotal2 / 32; b++) {
|
|
141
|
+
accumulate_hacc_block_avx512(
|
|
142
|
+
nsq,
|
|
143
|
+
codes + b * block_stride,
|
|
144
|
+
lut_lo,
|
|
145
|
+
lut_hi,
|
|
146
|
+
accu + q * ntotal2 + b * 32);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
36
151
|
} // namespace faiss
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
#include <faiss/impl/fast_scan/rabitq_dispatching.h> // IWYU pragma: keep
|
|
11
11
|
|
|
12
12
|
#include <faiss/impl/fast_scan/decompose_qbs.h>
|
|
13
|
+
#include <faiss/impl/fast_scan/kernels_simd256.h>
|
|
13
14
|
|
|
14
15
|
namespace faiss {
|
|
15
16
|
|
|
@@ -29,4 +30,16 @@ void accumulate_to_mem_impl<SIMDLevel::ARM_NEON>(
|
|
|
29
30
|
nq, ntotal2, nsq, codes, LUT, handler, scaler, 32 * nsq / 2);
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
template <>
|
|
34
|
+
void accumulate_hacc_impl<SIMDLevel::ARM_NEON>(
|
|
35
|
+
int nq,
|
|
36
|
+
size_t ntotal2,
|
|
37
|
+
int nsq,
|
|
38
|
+
const uint8_t* codes,
|
|
39
|
+
const uint8_t* LUT,
|
|
40
|
+
int32_t* accu) {
|
|
41
|
+
accumulate_hacc_simdlib<SIMDLevel::ARM_NEON>(
|
|
42
|
+
nq, ntotal2, nsq, codes, LUT, accu);
|
|
43
|
+
}
|
|
44
|
+
|
|
32
45
|
} // namespace faiss
|
|
@@ -17,8 +17,92 @@
|
|
|
17
17
|
|
|
18
18
|
#include <faiss/impl/simdlib/simdlib_dispatch.h>
|
|
19
19
|
|
|
20
|
+
#include <algorithm>
|
|
21
|
+
#include <cstdint>
|
|
22
|
+
|
|
20
23
|
namespace faiss {
|
|
21
24
|
|
|
25
|
+
/*
|
|
26
|
+
* accumulate_hacc (see fast_scan.h) over one block of 32 codes. The low and
|
|
27
|
+
* high byte tables are looked up separately, and their sums are kept in
|
|
28
|
+
* 16-bit lanes over segments of 256 sub-quantizers, short enough for
|
|
29
|
+
* 255 * 256 < 2^16. Each segment is then added to the 32-bit accumulators as
|
|
30
|
+
* low + (high << 8).
|
|
31
|
+
*/
|
|
32
|
+
template <SIMDLevel KernelSL>
|
|
33
|
+
void kernel_accumulate_hacc_block(
|
|
34
|
+
int nsq,
|
|
35
|
+
const uint8_t* codes,
|
|
36
|
+
const uint8_t* lut_lo,
|
|
37
|
+
const uint8_t* lut_hi,
|
|
38
|
+
int32_t* accu) {
|
|
39
|
+
constexpr SIMDLevel SL256 = simd256_level_selector<KernelSL>::value;
|
|
40
|
+
using simd16uint16 = simd16uint16_tpl<SL256>;
|
|
41
|
+
using simd32uint8 = simd32uint8_tpl<SL256>;
|
|
42
|
+
constexpr int kSegmentSq = 256;
|
|
43
|
+
const simd32uint8 mask(15);
|
|
44
|
+
std::fill(accu, accu + 32, 0);
|
|
45
|
+
for (int seg = 0; seg < nsq; seg += kSegmentSq) {
|
|
46
|
+
const int seg_end = std::min(nsq, seg + kSegmentSq);
|
|
47
|
+
// [low or high byte table][as in kernel_accumulate_block]
|
|
48
|
+
simd16uint16 acc[2][4];
|
|
49
|
+
for (auto& a : acc) {
|
|
50
|
+
for (auto& r : a) {
|
|
51
|
+
r.clear();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (int sq = seg; sq < seg_end; sq += 2) {
|
|
55
|
+
const simd32uint8 c(codes + 16 * sq);
|
|
56
|
+
const simd32uint8 clo = c & mask;
|
|
57
|
+
const simd32uint8 chi = simd32uint8(simd16uint16(c) >> 4) & mask;
|
|
58
|
+
const uint8_t* luts[2] = {lut_lo + 16 * sq, lut_hi + 16 * sq};
|
|
59
|
+
for (int t = 0; t < 2; t++) {
|
|
60
|
+
const simd32uint8 lut(luts[t]);
|
|
61
|
+
const simd32uint8 res0 = lut.lookup_2_lanes(clo);
|
|
62
|
+
const simd32uint8 res1 = lut.lookup_2_lanes(chi);
|
|
63
|
+
acc[t][0] += simd16uint16(res0);
|
|
64
|
+
acc[t][1] += simd16uint16(res0) >> 8;
|
|
65
|
+
acc[t][2] += simd16uint16(res1);
|
|
66
|
+
acc[t][3] += simd16uint16(res1) >> 8;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
uint16_t sums[2][32]; // [table][element]
|
|
70
|
+
for (int t = 0; t < 2; t++) {
|
|
71
|
+
acc[t][0] -= acc[t][1] << 8;
|
|
72
|
+
combine2x2(acc[t][0], acc[t][1]).storeu(sums[t]);
|
|
73
|
+
acc[t][2] -= acc[t][3] << 8;
|
|
74
|
+
combine2x2(acc[t][2], acc[t][3]).storeu(sums[t] + 16);
|
|
75
|
+
}
|
|
76
|
+
for (int i = 0; i < 32; i++) {
|
|
77
|
+
accu[i] += sums[0][i] + (int32_t(sums[1][i]) << 8);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// accumulate_hacc with kernel_accumulate_hacc_block
|
|
83
|
+
template <SIMDLevel KernelSL>
|
|
84
|
+
void accumulate_hacc_simdlib(
|
|
85
|
+
int nq,
|
|
86
|
+
size_t ntotal2,
|
|
87
|
+
int nsq,
|
|
88
|
+
const uint8_t* codes,
|
|
89
|
+
const uint8_t* LUT,
|
|
90
|
+
int32_t* accu) {
|
|
91
|
+
const size_t block_stride = 32 * size_t(nsq) / 2;
|
|
92
|
+
for (int q = 0; q < nq; q++) {
|
|
93
|
+
const uint8_t* lut_lo = LUT + size_t(q) * 2 * 16 * nsq;
|
|
94
|
+
const uint8_t* lut_hi = lut_lo + 16 * size_t(nsq);
|
|
95
|
+
for (size_t b = 0; b < ntotal2 / 32; b++) {
|
|
96
|
+
kernel_accumulate_hacc_block<KernelSL>(
|
|
97
|
+
nsq,
|
|
98
|
+
codes + b * block_stride,
|
|
99
|
+
lut_lo,
|
|
100
|
+
lut_hi,
|
|
101
|
+
accu + q * ntotal2 + b * 32);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
22
106
|
/*
|
|
23
107
|
* Multi-BB variant: accumulates NQ queries x BB*32 database elements.
|
|
24
108
|
* Used by the search_1 path (bbs > 32).
|
|
@@ -15,10 +15,6 @@
|
|
|
15
15
|
|
|
16
16
|
namespace faiss {
|
|
17
17
|
|
|
18
|
-
// Explicit SIMD-level aliases for this file (no global bare aliases).
|
|
19
|
-
using simd32uint16 = simd32uint16_tpl<SINGLE_SIMD_LEVEL_512>;
|
|
20
|
-
using simd64uint8 = simd64uint8_tpl<SINGLE_SIMD_LEVEL_512>;
|
|
21
|
-
|
|
22
18
|
// NQ=1 specialization: processes 512-bit chunks aggressively.
|
|
23
19
|
// a special version for NQ=1.
|
|
24
20
|
// Despite the function being large in the text form, it compiles to a very
|