faiss 0.3.0 → 0.3.2
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 +9 -0
- data/LICENSE.txt +1 -1
- data/README.md +1 -1
- data/ext/faiss/extconf.rb +9 -2
- data/ext/faiss/index.cpp +1 -1
- data/ext/faiss/index_binary.cpp +2 -2
- data/ext/faiss/product_quantizer.cpp +1 -1
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +7 -7
- data/vendor/faiss/faiss/AutoTune.h +1 -2
- data/vendor/faiss/faiss/Clustering.cpp +39 -22
- data/vendor/faiss/faiss/Clustering.h +40 -21
- data/vendor/faiss/faiss/IVFlib.cpp +26 -12
- data/vendor/faiss/faiss/Index.cpp +1 -1
- data/vendor/faiss/faiss/Index.h +40 -10
- data/vendor/faiss/faiss/Index2Layer.cpp +7 -7
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +176 -166
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +15 -15
- data/vendor/faiss/faiss/IndexBinary.cpp +9 -4
- data/vendor/faiss/faiss/IndexBinary.h +8 -19
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +2 -1
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +24 -31
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +1 -1
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +25 -50
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +107 -188
- data/vendor/faiss/faiss/IndexFastScan.cpp +95 -146
- data/vendor/faiss/faiss/IndexFastScan.h +9 -8
- data/vendor/faiss/faiss/IndexFlat.cpp +206 -10
- data/vendor/faiss/faiss/IndexFlat.h +20 -1
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +170 -5
- data/vendor/faiss/faiss/IndexFlatCodes.h +23 -4
- data/vendor/faiss/faiss/IndexHNSW.cpp +231 -382
- data/vendor/faiss/faiss/IndexHNSW.h +62 -49
- data/vendor/faiss/faiss/IndexIDMap.cpp +69 -28
- data/vendor/faiss/faiss/IndexIDMap.h +24 -2
- data/vendor/faiss/faiss/IndexIVF.cpp +162 -56
- data/vendor/faiss/faiss/IndexIVF.h +46 -6
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +33 -26
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +6 -2
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +19 -46
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +4 -3
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +502 -401
- data/vendor/faiss/faiss/IndexIVFFastScan.h +63 -26
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +15 -5
- data/vendor/faiss/faiss/IndexIVFFlat.h +3 -2
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +172 -0
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +56 -0
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +79 -125
- data/vendor/faiss/faiss/IndexIVFPQ.h +6 -7
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +39 -52
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +4 -3
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +45 -29
- data/vendor/faiss/faiss/IndexIVFPQR.h +5 -2
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +25 -27
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +6 -6
- data/vendor/faiss/faiss/IndexLSH.cpp +14 -16
- data/vendor/faiss/faiss/IndexLattice.cpp +1 -19
- data/vendor/faiss/faiss/IndexLattice.h +3 -22
- data/vendor/faiss/faiss/IndexNNDescent.cpp +3 -33
- data/vendor/faiss/faiss/IndexNNDescent.h +1 -1
- data/vendor/faiss/faiss/IndexNSG.cpp +11 -27
- data/vendor/faiss/faiss/IndexNSG.h +11 -11
- data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +56 -0
- data/vendor/faiss/faiss/IndexNeuralNetCodec.h +49 -0
- data/vendor/faiss/faiss/IndexPQ.cpp +72 -88
- data/vendor/faiss/faiss/IndexPQ.h +1 -4
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexPreTransform.cpp +25 -31
- data/vendor/faiss/faiss/IndexPreTransform.h +1 -1
- data/vendor/faiss/faiss/IndexRefine.cpp +54 -24
- data/vendor/faiss/faiss/IndexRefine.h +7 -0
- data/vendor/faiss/faiss/IndexReplicas.cpp +23 -26
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +25 -17
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +6 -4
- data/vendor/faiss/faiss/IndexShards.cpp +21 -29
- data/vendor/faiss/faiss/IndexShardsIVF.cpp +1 -2
- data/vendor/faiss/faiss/MatrixStats.cpp +17 -32
- data/vendor/faiss/faiss/MatrixStats.h +21 -9
- data/vendor/faiss/faiss/MetaIndexes.cpp +35 -35
- data/vendor/faiss/faiss/MetricType.h +7 -2
- data/vendor/faiss/faiss/VectorTransform.cpp +13 -26
- data/vendor/faiss/faiss/VectorTransform.h +7 -7
- data/vendor/faiss/faiss/clone_index.cpp +15 -10
- data/vendor/faiss/faiss/clone_index.h +3 -0
- data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +95 -17
- data/vendor/faiss/faiss/cppcontrib/factory_tools.cpp +152 -0
- data/vendor/faiss/faiss/cppcontrib/factory_tools.h +24 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +83 -30
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +123 -8
- data/vendor/faiss/faiss/gpu/GpuCloner.h +22 -0
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +13 -0
- data/vendor/faiss/faiss/gpu/GpuDistance.h +46 -38
- data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -1
- data/vendor/faiss/faiss/gpu/GpuIndex.h +30 -12
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +282 -0
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +4 -4
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +14 -9
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +20 -3
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +22 -11
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +1 -3
- data/vendor/faiss/faiss/gpu/GpuResources.cpp +24 -3
- data/vendor/faiss/faiss/gpu/GpuResources.h +39 -11
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +142 -17
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +57 -3
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +26 -21
- data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +7 -1
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +8 -5
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +25 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +129 -9
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +332 -40
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +299 -208
- data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +1 -0
- data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +1 -1
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +6 -0
- data/vendor/faiss/faiss/gpu/utils/RaftUtils.h +75 -0
- data/vendor/faiss/faiss/gpu/utils/Timer.cpp +4 -1
- data/vendor/faiss/faiss/gpu/utils/Timer.h +1 -1
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +3 -1
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +5 -5
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +26 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +10 -3
- data/vendor/faiss/faiss/impl/DistanceComputer.h +70 -1
- data/vendor/faiss/faiss/impl/FaissAssert.h +4 -2
- data/vendor/faiss/faiss/impl/FaissException.h +13 -34
- data/vendor/faiss/faiss/impl/HNSW.cpp +605 -186
- data/vendor/faiss/faiss/impl/HNSW.h +52 -30
- data/vendor/faiss/faiss/impl/IDSelector.h +4 -4
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +11 -9
- data/vendor/faiss/faiss/impl/LookupTableScaler.h +34 -0
- data/vendor/faiss/faiss/impl/NNDescent.cpp +42 -27
- data/vendor/faiss/faiss/impl/NSG.cpp +0 -29
- data/vendor/faiss/faiss/impl/NSG.h +1 -1
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +14 -12
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +1 -1
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +25 -22
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +6 -2
- data/vendor/faiss/faiss/impl/Quantizer.h +1 -1
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +27 -1015
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +5 -63
- data/vendor/faiss/faiss/impl/ResultHandler.h +347 -172
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +1104 -147
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +3 -8
- data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +285 -42
- data/vendor/faiss/faiss/impl/code_distance/code_distance-avx512.h +248 -0
- data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +21 -14
- data/vendor/faiss/faiss/impl/code_distance/code_distance.h +22 -12
- data/vendor/faiss/faiss/impl/index_read.cpp +74 -34
- data/vendor/faiss/faiss/impl/index_read_utils.h +37 -0
- data/vendor/faiss/faiss/impl/index_write.cpp +88 -51
- data/vendor/faiss/faiss/impl/io.cpp +23 -15
- data/vendor/faiss/faiss/impl/io.h +4 -4
- data/vendor/faiss/faiss/impl/io_macros.h +6 -0
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -1
- data/vendor/faiss/faiss/impl/platform_macros.h +40 -1
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +14 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +7 -6
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +52 -38
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +487 -49
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +960 -0
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +176 -0
- data/vendor/faiss/faiss/impl/simd_result_handlers.h +481 -225
- data/vendor/faiss/faiss/index_factory.cpp +41 -20
- data/vendor/faiss/faiss/index_io.h +12 -5
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +28 -8
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +3 -0
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +10 -2
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +73 -17
- data/vendor/faiss/faiss/invlists/InvertedLists.h +26 -8
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +24 -9
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +2 -1
- data/vendor/faiss/faiss/python/python_callbacks.cpp +4 -4
- data/vendor/faiss/faiss/utils/Heap.cpp +3 -1
- data/vendor/faiss/faiss/utils/Heap.h +105 -0
- data/vendor/faiss/faiss/utils/NeuralNet.cpp +342 -0
- data/vendor/faiss/faiss/utils/NeuralNet.h +147 -0
- data/vendor/faiss/faiss/utils/WorkerThread.h +1 -0
- data/vendor/faiss/faiss/utils/bf16.h +36 -0
- data/vendor/faiss/faiss/utils/distances.cpp +147 -123
- data/vendor/faiss/faiss/utils/distances.h +86 -9
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +5 -5
- data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +1 -1
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +5 -5
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +1 -1
- data/vendor/faiss/faiss/utils/distances_simd.cpp +1589 -243
- data/vendor/faiss/faiss/utils/extra_distances-inl.h +70 -0
- data/vendor/faiss/faiss/utils/extra_distances.cpp +85 -137
- data/vendor/faiss/faiss/utils/extra_distances.h +3 -2
- data/vendor/faiss/faiss/utils/fp16-arm.h +29 -0
- data/vendor/faiss/faiss/utils/fp16.h +2 -0
- data/vendor/faiss/faiss/utils/hamming.cpp +163 -111
- data/vendor/faiss/faiss/utils/hamming.h +58 -0
- data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +16 -89
- data/vendor/faiss/faiss/utils/hamming_distance/common.h +1 -0
- data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +19 -88
- data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +58 -0
- data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +14 -104
- data/vendor/faiss/faiss/utils/partitioning.cpp +3 -4
- data/vendor/faiss/faiss/utils/prefetch.h +77 -0
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +0 -14
- data/vendor/faiss/faiss/utils/random.cpp +43 -0
- data/vendor/faiss/faiss/utils/random.h +25 -0
- data/vendor/faiss/faiss/utils/simdlib.h +10 -1
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +0 -6
- data/vendor/faiss/faiss/utils/simdlib_avx512.h +296 -0
- data/vendor/faiss/faiss/utils/simdlib_neon.h +77 -79
- data/vendor/faiss/faiss/utils/simdlib_ppc64.h +1084 -0
- data/vendor/faiss/faiss/utils/sorting.cpp +140 -5
- data/vendor/faiss/faiss/utils/sorting.h +27 -0
- data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +176 -0
- data/vendor/faiss/faiss/utils/utils.cpp +120 -7
- data/vendor/faiss/faiss/utils/utils.h +60 -20
- metadata +23 -4
- data/vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h +0 -102
|
@@ -20,11 +20,11 @@ namespace faiss {
|
|
|
20
20
|
* IO functions
|
|
21
21
|
***********************************************************************/
|
|
22
22
|
|
|
23
|
-
int IOReader::
|
|
23
|
+
int IOReader::filedescriptor() {
|
|
24
24
|
FAISS_THROW_MSG("IOReader does not support memory mapping");
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
int IOWriter::
|
|
27
|
+
int IOWriter::filedescriptor() {
|
|
28
28
|
FAISS_THROW_MSG("IOWriter does not support memory mapping");
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -85,8 +85,12 @@ size_t FileIOReader::operator()(void* ptr, size_t size, size_t nitems) {
|
|
|
85
85
|
return fread(ptr, size, nitems, f);
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
int FileIOReader::
|
|
88
|
+
int FileIOReader::filedescriptor() {
|
|
89
|
+
#ifdef _AIX
|
|
90
|
+
return fileno(f);
|
|
91
|
+
#else
|
|
89
92
|
return ::fileno(f);
|
|
93
|
+
#endif
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
FileIOWriter::FileIOWriter(FILE* wf) : f(wf) {}
|
|
@@ -116,8 +120,12 @@ size_t FileIOWriter::operator()(const void* ptr, size_t size, size_t nitems) {
|
|
|
116
120
|
return fwrite(ptr, size, nitems, f);
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
int FileIOWriter::
|
|
123
|
+
int FileIOWriter::filedescriptor() {
|
|
124
|
+
#ifdef _AIX
|
|
125
|
+
return fileno(f);
|
|
126
|
+
#else
|
|
120
127
|
return ::fileno(f);
|
|
128
|
+
#endif
|
|
121
129
|
}
|
|
122
130
|
|
|
123
131
|
/***********************************************************************
|
|
@@ -196,13 +204,13 @@ size_t BufferedIOWriter::operator()(
|
|
|
196
204
|
while (size > 0) {
|
|
197
205
|
assert(b0 == bsz);
|
|
198
206
|
// now we need to flush to add more bytes
|
|
199
|
-
size_t
|
|
207
|
+
size_t ofs_2 = 0;
|
|
200
208
|
do {
|
|
201
|
-
assert(
|
|
202
|
-
size_t written = (*writer)(buffer.data() +
|
|
209
|
+
assert(ofs_2 < 10000000);
|
|
210
|
+
size_t written = (*writer)(buffer.data() + ofs_2, 1, bsz - ofs_2);
|
|
203
211
|
FAISS_THROW_IF_NOT(written > 0);
|
|
204
|
-
|
|
205
|
-
} while (
|
|
212
|
+
ofs_2 += written;
|
|
213
|
+
} while (ofs_2 != bsz);
|
|
206
214
|
|
|
207
215
|
// copy src to buffer
|
|
208
216
|
size_t nb1 = std::min(bsz, size);
|
|
@@ -217,12 +225,12 @@ size_t BufferedIOWriter::operator()(
|
|
|
217
225
|
}
|
|
218
226
|
|
|
219
227
|
BufferedIOWriter::~BufferedIOWriter() {
|
|
220
|
-
size_t
|
|
221
|
-
while (
|
|
222
|
-
// printf("Destructor write %zd \n", b0 -
|
|
223
|
-
size_t written = (*writer)(buffer.data() +
|
|
228
|
+
size_t ofs_2 = 0;
|
|
229
|
+
while (ofs_2 != b0) {
|
|
230
|
+
// printf("Destructor write %zd \n", b0 - ofs_2);
|
|
231
|
+
size_t written = (*writer)(buffer.data() + ofs_2, 1, b0 - ofs_2);
|
|
224
232
|
FAISS_THROW_IF_NOT(written > 0);
|
|
225
|
-
|
|
233
|
+
ofs_2 += written;
|
|
226
234
|
}
|
|
227
235
|
}
|
|
228
236
|
|
|
@@ -259,7 +267,7 @@ std::string fourcc_inv_printable(uint32_t x) {
|
|
|
259
267
|
str += c;
|
|
260
268
|
} else {
|
|
261
269
|
char buf[10];
|
|
262
|
-
|
|
270
|
+
snprintf(buf, sizeof(buf), "\\x%02x", c);
|
|
263
271
|
str += buf;
|
|
264
272
|
}
|
|
265
273
|
}
|
|
@@ -32,7 +32,7 @@ struct IOReader {
|
|
|
32
32
|
virtual size_t operator()(void* ptr, size_t size, size_t nitems) = 0;
|
|
33
33
|
|
|
34
34
|
// return a file number that can be memory-mapped
|
|
35
|
-
virtual int
|
|
35
|
+
virtual int filedescriptor();
|
|
36
36
|
|
|
37
37
|
virtual ~IOReader() {}
|
|
38
38
|
};
|
|
@@ -45,7 +45,7 @@ struct IOWriter {
|
|
|
45
45
|
virtual size_t operator()(const void* ptr, size_t size, size_t nitems) = 0;
|
|
46
46
|
|
|
47
47
|
// return a file number that can be memory-mapped
|
|
48
|
-
virtual int
|
|
48
|
+
virtual int filedescriptor();
|
|
49
49
|
|
|
50
50
|
virtual ~IOWriter() noexcept(false) {}
|
|
51
51
|
};
|
|
@@ -73,7 +73,7 @@ struct FileIOReader : IOReader {
|
|
|
73
73
|
|
|
74
74
|
size_t operator()(void* ptr, size_t size, size_t nitems) override;
|
|
75
75
|
|
|
76
|
-
int
|
|
76
|
+
int filedescriptor() override;
|
|
77
77
|
};
|
|
78
78
|
|
|
79
79
|
struct FileIOWriter : IOWriter {
|
|
@@ -88,7 +88,7 @@ struct FileIOWriter : IOWriter {
|
|
|
88
88
|
|
|
89
89
|
size_t operator()(const void* ptr, size_t size, size_t nitems) override;
|
|
90
90
|
|
|
91
|
-
int
|
|
91
|
+
int filedescriptor() override;
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
/*******************************************************
|
|
@@ -40,11 +40,13 @@
|
|
|
40
40
|
|
|
41
41
|
#include <intrin.h>
|
|
42
42
|
|
|
43
|
+
#ifndef __clang__
|
|
43
44
|
inline int __builtin_ctzll(uint64_t x) {
|
|
44
45
|
unsigned long ret;
|
|
45
46
|
_BitScanForward64(&ret, x);
|
|
46
47
|
return (int)ret;
|
|
47
48
|
}
|
|
49
|
+
#endif
|
|
48
50
|
|
|
49
51
|
// cudatoolkit provides __builtin_ctz for NVCC >= 11.0
|
|
50
52
|
#if !defined(__CUDACC__) || __CUDACC_VER_MAJOR__ < 11
|
|
@@ -55,13 +57,20 @@ inline int __builtin_ctz(unsigned long x) {
|
|
|
55
57
|
}
|
|
56
58
|
#endif
|
|
57
59
|
|
|
60
|
+
#ifndef __clang__
|
|
58
61
|
inline int __builtin_clzll(uint64_t x) {
|
|
59
62
|
return (int)__lzcnt64(x);
|
|
60
63
|
}
|
|
64
|
+
#endif
|
|
61
65
|
|
|
62
66
|
#define __builtin_popcount __popcnt
|
|
63
67
|
#define __builtin_popcountl __popcnt64
|
|
64
68
|
|
|
69
|
+
#ifndef __clang__
|
|
70
|
+
#define __m128i_u __m128i
|
|
71
|
+
#define __m256i_u __m256i
|
|
72
|
+
#endif
|
|
73
|
+
|
|
65
74
|
// MSVC does not define __SSEx__, and _M_IX86_FP is only defined on 32-bit
|
|
66
75
|
// processors cf.
|
|
67
76
|
// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros
|
|
@@ -82,6 +91,8 @@ inline int __builtin_clzll(uint64_t x) {
|
|
|
82
91
|
#define __F16C__ 1
|
|
83
92
|
#endif
|
|
84
93
|
|
|
94
|
+
#define FAISS_ALWAYS_INLINE __forceinline
|
|
95
|
+
|
|
85
96
|
#else
|
|
86
97
|
/*******************************************************
|
|
87
98
|
* Linux and OSX
|
|
@@ -92,9 +103,15 @@ inline int __builtin_clzll(uint64_t x) {
|
|
|
92
103
|
|
|
93
104
|
// aligned should be *in front* of the declaration, for compatibility with
|
|
94
105
|
// windows
|
|
106
|
+
#ifdef SWIG
|
|
107
|
+
#define ALIGNED(x)
|
|
108
|
+
#else
|
|
95
109
|
#define ALIGNED(x) __attribute__((aligned(x)))
|
|
110
|
+
#endif
|
|
96
111
|
|
|
97
|
-
#
|
|
112
|
+
#define FAISS_ALWAYS_INLINE __attribute__((always_inline)) inline
|
|
113
|
+
|
|
114
|
+
#endif
|
|
98
115
|
|
|
99
116
|
#if defined(__GNUC__) || defined(__clang__)
|
|
100
117
|
#define FAISS_DEPRECATED(msg) __attribute__((deprecated(msg)))
|
|
@@ -110,6 +127,13 @@ inline int __builtin_clzll(uint64_t x) {
|
|
|
110
127
|
__pragma(float_control(precise, off, push))
|
|
111
128
|
#define FAISS_PRAGMA_IMPRECISE_FUNCTION_END __pragma(float_control(pop))
|
|
112
129
|
#elif defined(__clang__)
|
|
130
|
+
#if defined(__PPC__)
|
|
131
|
+
#define FAISS_PRAGMA_IMPRECISE_LOOP \
|
|
132
|
+
_Pragma("clang loop vectorize_width(4) interleave_count(8)")
|
|
133
|
+
#define FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN \
|
|
134
|
+
_Pragma("float_control(precise, off, push)")
|
|
135
|
+
#define FAISS_PRAGMA_IMPRECISE_FUNCTION_END _Pragma("float_control(pop)")
|
|
136
|
+
#else
|
|
113
137
|
#define FAISS_PRAGMA_IMPRECISE_LOOP \
|
|
114
138
|
_Pragma("clang loop vectorize(enable) interleave(enable)")
|
|
115
139
|
|
|
@@ -127,6 +151,7 @@ inline int __builtin_clzll(uint64_t x) {
|
|
|
127
151
|
#define FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
|
|
128
152
|
#define FAISS_PRAGMA_IMPRECISE_FUNCTION_END
|
|
129
153
|
#endif
|
|
154
|
+
#endif
|
|
130
155
|
#elif defined(__GNUC__)
|
|
131
156
|
// Unfortunately, GCC does not provide a pragma for detecting it.
|
|
132
157
|
// So, we have to stick to GNUC, which is defined by MANY compilers.
|
|
@@ -148,3 +173,17 @@ inline int __builtin_clzll(uint64_t x) {
|
|
|
148
173
|
#endif
|
|
149
174
|
|
|
150
175
|
// clang-format on
|
|
176
|
+
|
|
177
|
+
/*******************************************************
|
|
178
|
+
* BIGENDIAN specific macros
|
|
179
|
+
*******************************************************/
|
|
180
|
+
#if !defined(_MSC_VER) && \
|
|
181
|
+
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
|
|
182
|
+
#define FAISS_BIG_ENDIAN
|
|
183
|
+
#endif
|
|
184
|
+
|
|
185
|
+
#define Swap2Bytes(val) ((((val) >> 8) & 0x00FF) | (((val) << 8) & 0xFF00))
|
|
186
|
+
|
|
187
|
+
#define Swap4Bytes(val) \
|
|
188
|
+
((((val) >> 24) & 0x000000FF) | (((val) >> 8) & 0x0000FF00) | \
|
|
189
|
+
(((val) << 8) & 0x00FF0000) | (((val) << 24) & 0xFF000000))
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
#include <faiss/impl/FaissAssert.h>
|
|
9
|
+
#include <faiss/impl/platform_macros.h>
|
|
9
10
|
#include <faiss/impl/pq4_fast_scan.h>
|
|
10
11
|
#include <faiss/impl/simd_result_handlers.h>
|
|
11
12
|
|
|
@@ -54,9 +55,17 @@ void pq4_pack_codes(
|
|
|
54
55
|
FAISS_THROW_IF_NOT(nb % bbs == 0);
|
|
55
56
|
FAISS_THROW_IF_NOT(nsq % 2 == 0);
|
|
56
57
|
|
|
58
|
+
if (nb == 0) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
57
61
|
memset(blocks, 0, nb * nsq / 2);
|
|
62
|
+
#ifdef FAISS_BIG_ENDIAN
|
|
63
|
+
const uint8_t perm0[16] = {
|
|
64
|
+
8, 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7};
|
|
65
|
+
#else
|
|
58
66
|
const uint8_t perm0[16] = {
|
|
59
67
|
0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15};
|
|
68
|
+
#endif
|
|
60
69
|
|
|
61
70
|
uint8_t* codes2 = blocks;
|
|
62
71
|
for (size_t i0 = 0; i0 < nb; i0 += bbs) {
|
|
@@ -90,8 +99,13 @@ void pq4_pack_codes_range(
|
|
|
90
99
|
size_t bbs,
|
|
91
100
|
size_t nsq,
|
|
92
101
|
uint8_t* blocks) {
|
|
102
|
+
#ifdef FAISS_BIG_ENDIAN
|
|
103
|
+
const uint8_t perm0[16] = {
|
|
104
|
+
8, 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7};
|
|
105
|
+
#else
|
|
93
106
|
const uint8_t perm0[16] = {
|
|
94
107
|
0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15};
|
|
108
|
+
#endif
|
|
95
109
|
|
|
96
110
|
// range of affected blocks
|
|
97
111
|
size_t block0 = i0 / bbs;
|
|
@@ -24,6 +24,9 @@
|
|
|
24
24
|
|
|
25
25
|
namespace faiss {
|
|
26
26
|
|
|
27
|
+
struct NormTableScaler;
|
|
28
|
+
struct SIMDResultHandler;
|
|
29
|
+
|
|
27
30
|
/** Pack codes for consumption by the SIMD kernels.
|
|
28
31
|
* The unused bytes are set to 0.
|
|
29
32
|
*
|
|
@@ -117,7 +120,6 @@ void pq4_pack_LUT(int nq, int nsq, const uint8_t* src, uint8_t* dest);
|
|
|
117
120
|
* @param LUT packed look-up table
|
|
118
121
|
* @param scaler scaler to scale the encoded norm
|
|
119
122
|
*/
|
|
120
|
-
template <class ResultHandler, class Scaler>
|
|
121
123
|
void pq4_accumulate_loop(
|
|
122
124
|
int nq,
|
|
123
125
|
size_t nb,
|
|
@@ -125,8 +127,8 @@ void pq4_accumulate_loop(
|
|
|
125
127
|
int nsq,
|
|
126
128
|
const uint8_t* codes,
|
|
127
129
|
const uint8_t* LUT,
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
+
SIMDResultHandler& res,
|
|
131
|
+
const NormTableScaler* scaler);
|
|
130
132
|
|
|
131
133
|
/* qbs versions, supported only for bbs=32.
|
|
132
134
|
*
|
|
@@ -178,14 +180,13 @@ int pq4_pack_LUT_qbs_q_map(
|
|
|
178
180
|
* @param res call-back for the resutls
|
|
179
181
|
* @param scaler scaler to scale the encoded norm
|
|
180
182
|
*/
|
|
181
|
-
template <class ResultHandler, class Scaler>
|
|
182
183
|
void pq4_accumulate_loop_qbs(
|
|
183
184
|
int qbs,
|
|
184
185
|
size_t nb,
|
|
185
186
|
int nsq,
|
|
186
187
|
const uint8_t* codes,
|
|
187
188
|
const uint8_t* LUT,
|
|
188
|
-
|
|
189
|
-
const
|
|
189
|
+
SIMDResultHandler& res,
|
|
190
|
+
const NormTableScaler* scaler = nullptr);
|
|
190
191
|
|
|
191
192
|
} // namespace faiss
|
|
@@ -125,7 +125,7 @@ void accumulate_fixed_blocks(
|
|
|
125
125
|
ResultHandler& res,
|
|
126
126
|
const Scaler& scaler) {
|
|
127
127
|
constexpr int bbs = 32 * BB;
|
|
128
|
-
for (
|
|
128
|
+
for (size_t j0 = 0; j0 < nb; j0 += bbs) {
|
|
129
129
|
FixedStorageHandler<NQ, 2 * BB> res2;
|
|
130
130
|
kernel_accumulate_block<NQ, BB>(nsq, codes, LUT, res2, scaler);
|
|
131
131
|
res.set_block_origin(0, j0);
|
|
@@ -134,10 +134,8 @@ void accumulate_fixed_blocks(
|
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
} // anonymous namespace
|
|
138
|
-
|
|
139
137
|
template <class ResultHandler, class Scaler>
|
|
140
|
-
void
|
|
138
|
+
void pq4_accumulate_loop_fixed_scaler(
|
|
141
139
|
int nq,
|
|
142
140
|
size_t nb,
|
|
143
141
|
int bbs,
|
|
@@ -172,39 +170,55 @@ void pq4_accumulate_loop(
|
|
|
172
170
|
#undef DISPATCH
|
|
173
171
|
}
|
|
174
172
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
173
|
+
template <class ResultHandler>
|
|
174
|
+
void pq4_accumulate_loop_fixed_handler(
|
|
175
|
+
int nq,
|
|
176
|
+
size_t nb,
|
|
177
|
+
int bbs,
|
|
178
|
+
int nsq,
|
|
179
|
+
const uint8_t* codes,
|
|
180
|
+
const uint8_t* LUT,
|
|
181
|
+
ResultHandler& res,
|
|
182
|
+
const NormTableScaler* scaler) {
|
|
183
|
+
if (scaler) {
|
|
184
|
+
pq4_accumulate_loop_fixed_scaler(
|
|
185
|
+
nq, nb, bbs, nsq, codes, LUT, res, *scaler);
|
|
186
|
+
} else {
|
|
187
|
+
DummyScaler dscaler;
|
|
188
|
+
pq4_accumulate_loop_fixed_scaler(
|
|
189
|
+
nq, nb, bbs, nsq, codes, LUT, res, dscaler);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
struct Run_pq4_accumulate_loop {
|
|
194
|
+
template <class ResultHandler>
|
|
195
|
+
void f(ResultHandler& res,
|
|
196
|
+
int nq,
|
|
197
|
+
size_t nb,
|
|
198
|
+
int bbs,
|
|
199
|
+
int nsq,
|
|
200
|
+
const uint8_t* codes,
|
|
201
|
+
const uint8_t* LUT,
|
|
202
|
+
const NormTableScaler* scaler) {
|
|
203
|
+
pq4_accumulate_loop_fixed_handler(
|
|
204
|
+
nq, nb, bbs, nsq, codes, LUT, res, scaler);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
} // anonymous namespace
|
|
209
|
+
|
|
210
|
+
void pq4_accumulate_loop(
|
|
211
|
+
int nq,
|
|
212
|
+
size_t nb,
|
|
213
|
+
int bbs,
|
|
214
|
+
int nsq,
|
|
215
|
+
const uint8_t* codes,
|
|
216
|
+
const uint8_t* LUT,
|
|
217
|
+
SIMDResultHandler& res,
|
|
218
|
+
const NormTableScaler* scaler) {
|
|
219
|
+
Run_pq4_accumulate_loop consumer;
|
|
220
|
+
dispatch_SIMDResultHandler(
|
|
221
|
+
res, consumer, nq, nb, bbs, nsq, codes, LUT, scaler);
|
|
222
|
+
}
|
|
209
223
|
|
|
210
224
|
} // namespace faiss
|