faiss 0.2.0 → 0.2.4
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 +16 -0
- data/LICENSE.txt +1 -1
- data/README.md +7 -7
- data/ext/faiss/extconf.rb +6 -3
- data/ext/faiss/numo.hpp +4 -4
- data/ext/faiss/utils.cpp +1 -1
- data/ext/faiss/utils.h +1 -1
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +292 -291
- data/vendor/faiss/faiss/AutoTune.h +55 -56
- data/vendor/faiss/faiss/Clustering.cpp +365 -194
- data/vendor/faiss/faiss/Clustering.h +102 -35
- data/vendor/faiss/faiss/IVFlib.cpp +171 -195
- data/vendor/faiss/faiss/IVFlib.h +48 -51
- data/vendor/faiss/faiss/Index.cpp +85 -103
- data/vendor/faiss/faiss/Index.h +54 -48
- data/vendor/faiss/faiss/Index2Layer.cpp +126 -224
- data/vendor/faiss/faiss/Index2Layer.h +22 -36
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +407 -0
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +195 -0
- data/vendor/faiss/faiss/IndexBinary.cpp +45 -37
- data/vendor/faiss/faiss/IndexBinary.h +140 -132
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +73 -53
- data/vendor/faiss/faiss/IndexBinaryFlat.h +29 -24
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +46 -43
- data/vendor/faiss/faiss/IndexBinaryFromFloat.h +16 -15
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +215 -232
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +25 -24
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +182 -177
- data/vendor/faiss/faiss/IndexBinaryHash.h +41 -34
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +489 -461
- data/vendor/faiss/faiss/IndexBinaryIVF.h +97 -68
- data/vendor/faiss/faiss/IndexFlat.cpp +115 -176
- data/vendor/faiss/faiss/IndexFlat.h +42 -59
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +67 -0
- data/vendor/faiss/faiss/IndexFlatCodes.h +47 -0
- data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
- data/vendor/faiss/faiss/IndexHNSW.h +57 -41
- data/vendor/faiss/faiss/IndexIVF.cpp +545 -453
- data/vendor/faiss/faiss/IndexIVF.h +169 -118
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +316 -0
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +121 -0
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +247 -252
- data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +459 -517
- data/vendor/faiss/faiss/IndexIVFPQ.h +75 -67
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +406 -372
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +82 -57
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +104 -102
- data/vendor/faiss/faiss/IndexIVFPQR.h +33 -28
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +163 -150
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +38 -25
- data/vendor/faiss/faiss/IndexLSH.cpp +66 -113
- data/vendor/faiss/faiss/IndexLSH.h +20 -38
- data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
- data/vendor/faiss/faiss/IndexLattice.h +11 -16
- data/vendor/faiss/faiss/IndexNNDescent.cpp +229 -0
- data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
- data/vendor/faiss/faiss/IndexNSG.cpp +301 -0
- data/vendor/faiss/faiss/IndexNSG.h +85 -0
- data/vendor/faiss/faiss/IndexPQ.cpp +387 -495
- data/vendor/faiss/faiss/IndexPQ.h +64 -82
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +143 -170
- data/vendor/faiss/faiss/IndexPQFastScan.h +46 -32
- data/vendor/faiss/faiss/IndexPreTransform.cpp +120 -150
- data/vendor/faiss/faiss/IndexPreTransform.h +33 -36
- data/vendor/faiss/faiss/IndexRefine.cpp +139 -127
- data/vendor/faiss/faiss/IndexRefine.h +32 -23
- data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
- data/vendor/faiss/faiss/IndexReplicas.h +62 -56
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +111 -172
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -59
- data/vendor/faiss/faiss/IndexShards.cpp +256 -240
- data/vendor/faiss/faiss/IndexShards.h +85 -73
- data/vendor/faiss/faiss/MatrixStats.cpp +112 -97
- data/vendor/faiss/faiss/MatrixStats.h +7 -10
- data/vendor/faiss/faiss/MetaIndexes.cpp +135 -157
- data/vendor/faiss/faiss/MetaIndexes.h +40 -34
- data/vendor/faiss/faiss/MetricType.h +7 -7
- data/vendor/faiss/faiss/VectorTransform.cpp +654 -475
- data/vendor/faiss/faiss/VectorTransform.h +64 -89
- data/vendor/faiss/faiss/clone_index.cpp +78 -73
- data/vendor/faiss/faiss/clone_index.h +4 -9
- data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +33 -38
- data/vendor/faiss/faiss/gpu/GpuAutoTune.h +11 -9
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +198 -171
- data/vendor/faiss/faiss/gpu/GpuCloner.h +53 -35
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +12 -14
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +27 -25
- data/vendor/faiss/faiss/gpu/GpuDistance.h +116 -112
- data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -2
- data/vendor/faiss/faiss/gpu/GpuIcmEncoder.h +60 -0
- data/vendor/faiss/faiss/gpu/GpuIndex.h +134 -137
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +76 -73
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +173 -162
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +67 -64
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +89 -86
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +150 -141
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +101 -103
- data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +17 -16
- data/vendor/faiss/faiss/gpu/GpuResources.cpp +116 -128
- data/vendor/faiss/faiss/gpu/GpuResources.h +182 -186
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +433 -422
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +131 -130
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +468 -456
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +25 -19
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +22 -20
- data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +9 -8
- data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +39 -44
- data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +16 -14
- data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +77 -71
- data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +109 -88
- data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +75 -64
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +230 -215
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +80 -86
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +284 -277
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +416 -416
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +611 -517
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +166 -164
- data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +61 -53
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +274 -238
- data/vendor/faiss/faiss/gpu/test/TestUtils.h +73 -57
- data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +47 -50
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +79 -72
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +140 -146
- data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +69 -71
- data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +21 -16
- data/vendor/faiss/faiss/gpu/utils/Timer.cpp +25 -29
- data/vendor/faiss/faiss/gpu/utils/Timer.h +30 -29
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +503 -0
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +175 -0
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +90 -120
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +81 -65
- data/vendor/faiss/faiss/impl/FaissAssert.h +73 -58
- data/vendor/faiss/faiss/impl/FaissException.cpp +56 -48
- data/vendor/faiss/faiss/impl/FaissException.h +41 -29
- data/vendor/faiss/faiss/impl/HNSW.cpp +606 -617
- data/vendor/faiss/faiss/impl/HNSW.h +179 -200
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +855 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +244 -0
- data/vendor/faiss/faiss/impl/NNDescent.cpp +487 -0
- data/vendor/faiss/faiss/impl/NNDescent.h +154 -0
- data/vendor/faiss/faiss/impl/NSG.cpp +679 -0
- data/vendor/faiss/faiss/impl/NSG.h +199 -0
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +484 -454
- data/vendor/faiss/faiss/impl/PolysemousTraining.h +52 -55
- data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +26 -47
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +469 -459
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +76 -87
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +758 -0
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +188 -0
- data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +647 -707
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +48 -46
- data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +129 -131
- data/vendor/faiss/faiss/impl/ThreadedIndex.h +61 -55
- data/vendor/faiss/faiss/impl/index_read.cpp +631 -480
- data/vendor/faiss/faiss/impl/index_write.cpp +547 -407
- data/vendor/faiss/faiss/impl/io.cpp +76 -95
- data/vendor/faiss/faiss/impl/io.h +31 -41
- data/vendor/faiss/faiss/impl/io_macros.h +60 -29
- data/vendor/faiss/faiss/impl/kmeans1d.cpp +301 -0
- data/vendor/faiss/faiss/impl/kmeans1d.h +48 -0
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +137 -186
- data/vendor/faiss/faiss/impl/lattice_Zn.h +40 -51
- data/vendor/faiss/faiss/impl/platform_macros.h +29 -8
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +77 -124
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +39 -48
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +41 -52
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +80 -117
- data/vendor/faiss/faiss/impl/simd_result_handlers.h +109 -137
- data/vendor/faiss/faiss/index_factory.cpp +619 -397
- data/vendor/faiss/faiss/index_factory.h +8 -6
- data/vendor/faiss/faiss/index_io.h +23 -26
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +67 -75
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +22 -24
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +96 -112
- data/vendor/faiss/faiss/invlists/DirectMap.h +29 -33
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +307 -364
- data/vendor/faiss/faiss/invlists/InvertedLists.h +151 -151
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +29 -34
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +17 -18
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +257 -293
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +50 -45
- data/vendor/faiss/faiss/python/python_callbacks.cpp +23 -26
- data/vendor/faiss/faiss/python/python_callbacks.h +9 -16
- data/vendor/faiss/faiss/utils/AlignedTable.h +79 -44
- data/vendor/faiss/faiss/utils/Heap.cpp +40 -48
- data/vendor/faiss/faiss/utils/Heap.h +186 -209
- data/vendor/faiss/faiss/utils/WorkerThread.cpp +67 -76
- data/vendor/faiss/faiss/utils/WorkerThread.h +32 -33
- data/vendor/faiss/faiss/utils/distances.cpp +305 -312
- data/vendor/faiss/faiss/utils/distances.h +170 -122
- data/vendor/faiss/faiss/utils/distances_simd.cpp +498 -508
- data/vendor/faiss/faiss/utils/extra_distances-inl.h +117 -0
- data/vendor/faiss/faiss/utils/extra_distances.cpp +113 -232
- data/vendor/faiss/faiss/utils/extra_distances.h +30 -29
- data/vendor/faiss/faiss/utils/hamming-inl.h +260 -209
- data/vendor/faiss/faiss/utils/hamming.cpp +375 -469
- data/vendor/faiss/faiss/utils/hamming.h +62 -85
- data/vendor/faiss/faiss/utils/ordered_key_value.h +16 -18
- data/vendor/faiss/faiss/utils/partitioning.cpp +393 -318
- data/vendor/faiss/faiss/utils/partitioning.h +26 -21
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +78 -66
- data/vendor/faiss/faiss/utils/quantize_lut.h +22 -20
- data/vendor/faiss/faiss/utils/random.cpp +39 -63
- data/vendor/faiss/faiss/utils/random.h +13 -16
- data/vendor/faiss/faiss/utils/simdlib.h +4 -2
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +88 -85
- data/vendor/faiss/faiss/utils/simdlib_emulated.h +226 -165
- data/vendor/faiss/faiss/utils/simdlib_neon.h +832 -0
- data/vendor/faiss/faiss/utils/utils.cpp +304 -287
- data/vendor/faiss/faiss/utils/utils.h +54 -49
- metadata +29 -4
|
@@ -12,108 +12,95 @@
|
|
|
12
12
|
#include <memory>
|
|
13
13
|
|
|
14
14
|
#include <faiss/IndexPreTransform.h>
|
|
15
|
-
#include <faiss/impl/FaissAssert.h>
|
|
16
15
|
#include <faiss/MetaIndexes.h>
|
|
16
|
+
#include <faiss/impl/FaissAssert.h>
|
|
17
17
|
#include <faiss/utils/utils.h>
|
|
18
18
|
|
|
19
|
+
namespace faiss {
|
|
20
|
+
namespace ivflib {
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
void check_compatible_for_merge (const Index * index0,
|
|
25
|
-
const Index * index1)
|
|
26
|
-
{
|
|
27
|
-
|
|
28
|
-
const faiss::IndexPreTransform *pt0 =
|
|
29
|
-
dynamic_cast<const faiss::IndexPreTransform *>(index0);
|
|
22
|
+
void check_compatible_for_merge(const Index* index0, const Index* index1) {
|
|
23
|
+
const faiss::IndexPreTransform* pt0 =
|
|
24
|
+
dynamic_cast<const faiss::IndexPreTransform*>(index0);
|
|
30
25
|
|
|
31
26
|
if (pt0) {
|
|
32
|
-
const faiss::IndexPreTransform
|
|
33
|
-
|
|
34
|
-
FAISS_THROW_IF_NOT_MSG
|
|
27
|
+
const faiss::IndexPreTransform* pt1 =
|
|
28
|
+
dynamic_cast<const faiss::IndexPreTransform*>(index1);
|
|
29
|
+
FAISS_THROW_IF_NOT_MSG(pt1, "both indexes should be pretransforms");
|
|
35
30
|
|
|
36
|
-
FAISS_THROW_IF_NOT
|
|
31
|
+
FAISS_THROW_IF_NOT(pt0->chain.size() == pt1->chain.size());
|
|
37
32
|
for (int i = 0; i < pt0->chain.size(); i++) {
|
|
38
|
-
FAISS_THROW_IF_NOT
|
|
33
|
+
FAISS_THROW_IF_NOT(typeid(pt0->chain[i]) == typeid(pt1->chain[i]));
|
|
39
34
|
}
|
|
40
35
|
|
|
41
36
|
index0 = pt0->index;
|
|
42
37
|
index1 = pt1->index;
|
|
43
38
|
}
|
|
44
|
-
FAISS_THROW_IF_NOT
|
|
45
|
-
FAISS_THROW_IF_NOT
|
|
46
|
-
|
|
39
|
+
FAISS_THROW_IF_NOT(typeid(index0) == typeid(index1));
|
|
40
|
+
FAISS_THROW_IF_NOT(
|
|
41
|
+
index0->d == index1->d &&
|
|
42
|
+
index0->metric_type == index1->metric_type);
|
|
47
43
|
|
|
48
|
-
const faiss::IndexIVF
|
|
44
|
+
const faiss::IndexIVF* ivf0 = dynamic_cast<const faiss::IndexIVF*>(index0);
|
|
49
45
|
if (ivf0) {
|
|
50
|
-
const faiss::IndexIVF
|
|
51
|
-
|
|
52
|
-
FAISS_THROW_IF_NOT
|
|
46
|
+
const faiss::IndexIVF* ivf1 =
|
|
47
|
+
dynamic_cast<const faiss::IndexIVF*>(index1);
|
|
48
|
+
FAISS_THROW_IF_NOT(ivf1);
|
|
53
49
|
|
|
54
|
-
ivf0->check_compatible_for_merge
|
|
50
|
+
ivf0->check_compatible_for_merge(*ivf1);
|
|
55
51
|
}
|
|
56
52
|
|
|
57
53
|
// TODO: check as thoroughfully for other index types
|
|
58
|
-
|
|
59
54
|
}
|
|
60
55
|
|
|
61
|
-
const IndexIVF
|
|
62
|
-
{
|
|
63
|
-
if (auto *pt =
|
|
64
|
-
dynamic_cast<const IndexPreTransform *>(index)) {
|
|
56
|
+
const IndexIVF* try_extract_index_ivf(const Index* index) {
|
|
57
|
+
if (auto* pt = dynamic_cast<const IndexPreTransform*>(index)) {
|
|
65
58
|
index = pt->index;
|
|
66
59
|
}
|
|
67
60
|
|
|
68
|
-
if (auto
|
|
69
|
-
dynamic_cast<const IndexIDMap *>(index)) {
|
|
61
|
+
if (auto* idmap = dynamic_cast<const IndexIDMap*>(index)) {
|
|
70
62
|
index = idmap->index;
|
|
71
63
|
}
|
|
72
|
-
if (auto
|
|
73
|
-
dynamic_cast<const IndexIDMap2 *>(index)) {
|
|
64
|
+
if (auto* idmap = dynamic_cast<const IndexIDMap2*>(index)) {
|
|
74
65
|
index = idmap->index;
|
|
75
66
|
}
|
|
76
67
|
|
|
77
|
-
auto
|
|
68
|
+
auto* ivf = dynamic_cast<const IndexIVF*>(index);
|
|
78
69
|
|
|
79
70
|
return ivf;
|
|
80
71
|
}
|
|
81
72
|
|
|
82
|
-
IndexIVF
|
|
83
|
-
return const_cast<IndexIVF*>
|
|
73
|
+
IndexIVF* try_extract_index_ivf(Index* index) {
|
|
74
|
+
return const_cast<IndexIVF*>(try_extract_index_ivf((const Index*)(index)));
|
|
84
75
|
}
|
|
85
76
|
|
|
86
|
-
const IndexIVF
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
FAISS_THROW_IF_NOT (ivf);
|
|
77
|
+
const IndexIVF* extract_index_ivf(const Index* index) {
|
|
78
|
+
const IndexIVF* ivf = try_extract_index_ivf(index);
|
|
79
|
+
FAISS_THROW_IF_NOT(ivf);
|
|
90
80
|
return ivf;
|
|
91
81
|
}
|
|
92
82
|
|
|
93
|
-
IndexIVF
|
|
94
|
-
return const_cast<IndexIVF*>
|
|
83
|
+
IndexIVF* extract_index_ivf(Index* index) {
|
|
84
|
+
return const_cast<IndexIVF*>(extract_index_ivf((const Index*)(index)));
|
|
95
85
|
}
|
|
96
86
|
|
|
87
|
+
void merge_into(faiss::Index* index0, faiss::Index* index1, bool shift_ids) {
|
|
88
|
+
check_compatible_for_merge(index0, index1);
|
|
89
|
+
IndexIVF* ivf0 = extract_index_ivf(index0);
|
|
90
|
+
IndexIVF* ivf1 = extract_index_ivf(index1);
|
|
97
91
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
check_compatible_for_merge (index0, index1);
|
|
101
|
-
IndexIVF * ivf0 = extract_index_ivf (index0);
|
|
102
|
-
IndexIVF * ivf1 = extract_index_ivf (index1);
|
|
103
|
-
|
|
104
|
-
ivf0->merge_from (*ivf1, shift_ids ? ivf0->ntotal : 0);
|
|
92
|
+
ivf0->merge_from(*ivf1, shift_ids ? ivf0->ntotal : 0);
|
|
105
93
|
|
|
106
94
|
// useful for IndexPreTransform
|
|
107
95
|
index0->ntotal = ivf0->ntotal;
|
|
108
96
|
index1->ntotal = ivf1->ntotal;
|
|
109
97
|
}
|
|
110
98
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
{
|
|
99
|
+
void search_centroid(
|
|
100
|
+
faiss::Index* index,
|
|
101
|
+
const float* x,
|
|
102
|
+
int n,
|
|
103
|
+
idx_t* centroid_ids) {
|
|
117
104
|
std::unique_ptr<float[]> del;
|
|
118
105
|
if (auto index_pre = dynamic_cast<faiss::IndexPreTransform*>(index)) {
|
|
119
106
|
x = index_pre->apply_chain(n, x);
|
|
@@ -125,19 +112,17 @@ void search_centroid(faiss::Index *index,
|
|
|
125
112
|
index_ivf->quantizer->assign(n, x, centroid_ids);
|
|
126
113
|
}
|
|
127
114
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const float *x = xin;
|
|
140
|
-
std::unique_ptr<float []> del;
|
|
115
|
+
void search_and_return_centroids(
|
|
116
|
+
faiss::Index* index,
|
|
117
|
+
size_t n,
|
|
118
|
+
const float* xin,
|
|
119
|
+
long k,
|
|
120
|
+
float* distances,
|
|
121
|
+
idx_t* labels,
|
|
122
|
+
idx_t* query_centroid_ids,
|
|
123
|
+
idx_t* result_centroid_ids) {
|
|
124
|
+
const float* x = xin;
|
|
125
|
+
std::unique_ptr<float[]> del;
|
|
141
126
|
if (auto index_pre = dynamic_cast<faiss::IndexPreTransform*>(index)) {
|
|
142
127
|
x = index_pre->apply_chain(n, x);
|
|
143
128
|
del.reset((float*)x);
|
|
@@ -147,19 +132,18 @@ void search_and_return_centroids(faiss::Index *index,
|
|
|
147
132
|
assert(index_ivf);
|
|
148
133
|
|
|
149
134
|
size_t nprobe = index_ivf->nprobe;
|
|
150
|
-
std::vector<idx_t> cent_nos
|
|
151
|
-
std::vector<float> cent_dis
|
|
135
|
+
std::vector<idx_t> cent_nos(n * nprobe);
|
|
136
|
+
std::vector<float> cent_dis(n * nprobe);
|
|
152
137
|
index_ivf->quantizer->search(
|
|
153
|
-
|
|
138
|
+
n, x, nprobe, cent_dis.data(), cent_nos.data());
|
|
154
139
|
|
|
155
140
|
if (query_centroid_ids) {
|
|
156
141
|
for (size_t i = 0; i < n; i++)
|
|
157
142
|
query_centroid_ids[i] = cent_nos[i * nprobe];
|
|
158
143
|
}
|
|
159
144
|
|
|
160
|
-
index_ivf->search_preassigned
|
|
161
|
-
|
|
162
|
-
distances, labels, true);
|
|
145
|
+
index_ivf->search_preassigned(
|
|
146
|
+
n, x, k, cent_nos.data(), cent_dis.data(), distances, labels, true);
|
|
163
147
|
|
|
164
148
|
for (size_t i = 0; i < n * k; i++) {
|
|
165
149
|
idx_t label = labels[i];
|
|
@@ -167,8 +151,8 @@ void search_and_return_centroids(faiss::Index *index,
|
|
|
167
151
|
if (result_centroid_ids)
|
|
168
152
|
result_centroid_ids[i] = -1;
|
|
169
153
|
} else {
|
|
170
|
-
long list_no = lo_listno
|
|
171
|
-
long list_index = lo_offset
|
|
154
|
+
long list_no = lo_listno(label);
|
|
155
|
+
long list_index = lo_offset(label);
|
|
172
156
|
if (result_centroid_ids)
|
|
173
157
|
result_centroid_ids[i] = list_no;
|
|
174
158
|
labels[i] = index_ivf->invlists->get_single_id(list_no, list_index);
|
|
@@ -176,61 +160,61 @@ void search_and_return_centroids(faiss::Index *index,
|
|
|
176
160
|
}
|
|
177
161
|
}
|
|
178
162
|
|
|
179
|
-
|
|
180
|
-
SlidingIndexWindow::SlidingIndexWindow (Index *index): index (index) {
|
|
163
|
+
SlidingIndexWindow::SlidingIndexWindow(Index* index) : index(index) {
|
|
181
164
|
n_slice = 0;
|
|
182
|
-
IndexIVF* index_ivf = const_cast<IndexIVF*>(extract_index_ivf
|
|
183
|
-
ils = dynamic_cast<ArrayInvertedLists
|
|
184
|
-
FAISS_THROW_IF_NOT_MSG
|
|
185
|
-
|
|
165
|
+
IndexIVF* index_ivf = const_cast<IndexIVF*>(extract_index_ivf(index));
|
|
166
|
+
ils = dynamic_cast<ArrayInvertedLists*>(index_ivf->invlists);
|
|
167
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
168
|
+
ils, "only supports indexes with ArrayInvertedLists");
|
|
186
169
|
nlist = ils->nlist;
|
|
187
170
|
sizes.resize(nlist);
|
|
188
171
|
}
|
|
189
172
|
|
|
190
|
-
template<class T>
|
|
191
|
-
static void shift_and_add
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
{
|
|
173
|
+
template <class T>
|
|
174
|
+
static void shift_and_add(
|
|
175
|
+
std::vector<T>& dst,
|
|
176
|
+
size_t remove,
|
|
177
|
+
const std::vector<T>& src) {
|
|
195
178
|
if (remove > 0)
|
|
196
|
-
memmove
|
|
197
|
-
|
|
179
|
+
memmove(dst.data(),
|
|
180
|
+
dst.data() + remove,
|
|
181
|
+
(dst.size() - remove) * sizeof(T));
|
|
198
182
|
size_t insert_point = dst.size() - remove;
|
|
199
|
-
dst.resize
|
|
200
|
-
memcpy
|
|
183
|
+
dst.resize(insert_point + src.size());
|
|
184
|
+
memcpy(dst.data() + insert_point, src.data(), src.size() * sizeof(T));
|
|
201
185
|
}
|
|
202
186
|
|
|
203
|
-
template<class T>
|
|
204
|
-
static void remove_from_begin
|
|
205
|
-
size_t remove)
|
|
206
|
-
{
|
|
187
|
+
template <class T>
|
|
188
|
+
static void remove_from_begin(std::vector<T>& v, size_t remove) {
|
|
207
189
|
if (remove > 0)
|
|
208
|
-
v.erase
|
|
190
|
+
v.erase(v.begin(), v.begin() + remove);
|
|
209
191
|
}
|
|
210
192
|
|
|
211
|
-
void SlidingIndexWindow::step(const Index
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
193
|
+
void SlidingIndexWindow::step(const Index* sub_index, bool remove_oldest) {
|
|
194
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
195
|
+
!remove_oldest || n_slice > 0,
|
|
196
|
+
"cannot remove slice: there is none");
|
|
215
197
|
|
|
216
|
-
const ArrayInvertedLists
|
|
217
|
-
if(sub_index) {
|
|
218
|
-
check_compatible_for_merge
|
|
198
|
+
const ArrayInvertedLists* ils2 = nullptr;
|
|
199
|
+
if (sub_index) {
|
|
200
|
+
check_compatible_for_merge(index, sub_index);
|
|
219
201
|
ils2 = dynamic_cast<const ArrayInvertedLists*>(
|
|
220
|
-
|
|
221
|
-
FAISS_THROW_IF_NOT_MSG
|
|
202
|
+
extract_index_ivf(sub_index)->invlists);
|
|
203
|
+
FAISS_THROW_IF_NOT_MSG(ils2, "supports only ArrayInvertedLists");
|
|
222
204
|
}
|
|
223
|
-
IndexIVF
|
|
205
|
+
IndexIVF* index_ivf = extract_index_ivf(index);
|
|
224
206
|
|
|
225
207
|
if (remove_oldest && ils2) {
|
|
226
208
|
for (int i = 0; i < nlist; i++) {
|
|
227
|
-
std::vector<size_t
|
|
209
|
+
std::vector<size_t>& sizesi = sizes[i];
|
|
228
210
|
size_t amount_to_remove = sizesi[0];
|
|
229
211
|
index_ivf->ntotal += ils2->ids[i].size() - amount_to_remove;
|
|
230
212
|
|
|
231
|
-
shift_and_add
|
|
232
|
-
shift_and_add
|
|
233
|
-
|
|
213
|
+
shift_and_add(ils->ids[i], amount_to_remove, ils2->ids[i]);
|
|
214
|
+
shift_and_add(
|
|
215
|
+
ils->codes[i],
|
|
216
|
+
amount_to_remove * ils->code_size,
|
|
217
|
+
ils2->codes[i]);
|
|
234
218
|
for (int j = 0; j + 1 < n_slice; j++) {
|
|
235
219
|
sizesi[j] = sizesi[j + 1] - amount_to_remove;
|
|
236
220
|
}
|
|
@@ -239,8 +223,8 @@ void SlidingIndexWindow::step(const Index *sub_index, bool remove_oldest) {
|
|
|
239
223
|
} else if (ils2) {
|
|
240
224
|
for (int i = 0; i < nlist; i++) {
|
|
241
225
|
index_ivf->ntotal += ils2->ids[i].size();
|
|
242
|
-
shift_and_add
|
|
243
|
-
shift_and_add
|
|
226
|
+
shift_and_add(ils->ids[i], 0, ils2->ids[i]);
|
|
227
|
+
shift_and_add(ils->codes[i], 0, ils2->codes[i]);
|
|
244
228
|
sizes[i].push_back(ils->ids[i].size());
|
|
245
229
|
}
|
|
246
230
|
n_slice++;
|
|
@@ -248,74 +232,72 @@ void SlidingIndexWindow::step(const Index *sub_index, bool remove_oldest) {
|
|
|
248
232
|
for (int i = 0; i < nlist; i++) {
|
|
249
233
|
size_t amount_to_remove = sizes[i][0];
|
|
250
234
|
index_ivf->ntotal -= amount_to_remove;
|
|
251
|
-
remove_from_begin
|
|
252
|
-
remove_from_begin
|
|
253
|
-
amount_to_remove * ils->code_size);
|
|
235
|
+
remove_from_begin(ils->ids[i], amount_to_remove);
|
|
236
|
+
remove_from_begin(ils->codes[i], amount_to_remove * ils->code_size);
|
|
254
237
|
for (int j = 0; j + 1 < n_slice; j++) {
|
|
255
238
|
sizes[i][j] = sizes[i][j + 1] - amount_to_remove;
|
|
256
239
|
}
|
|
257
|
-
sizes[i].pop_back
|
|
240
|
+
sizes[i].pop_back();
|
|
258
241
|
}
|
|
259
242
|
n_slice--;
|
|
260
243
|
} else {
|
|
261
|
-
FAISS_THROW_MSG
|
|
244
|
+
FAISS_THROW_MSG("nothing to do???");
|
|
262
245
|
}
|
|
263
246
|
index->ntotal = index_ivf->ntotal;
|
|
264
247
|
}
|
|
265
248
|
|
|
266
|
-
|
|
267
|
-
|
|
268
249
|
// Get a subset of inverted lists [i0, i1). Works on IndexIVF's and
|
|
269
250
|
// IndexIVF's embedded in a IndexPreTransform
|
|
270
251
|
|
|
271
|
-
ArrayInvertedLists *
|
|
272
|
-
|
|
273
|
-
{
|
|
274
|
-
const IndexIVF *ivf = extract_index_ivf (index);
|
|
252
|
+
ArrayInvertedLists* get_invlist_range(const Index* index, long i0, long i1) {
|
|
253
|
+
const IndexIVF* ivf = extract_index_ivf(index);
|
|
275
254
|
|
|
276
|
-
FAISS_THROW_IF_NOT
|
|
255
|
+
FAISS_THROW_IF_NOT(0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
|
|
277
256
|
|
|
278
|
-
const InvertedLists
|
|
257
|
+
const InvertedLists* src = ivf->invlists;
|
|
279
258
|
|
|
280
|
-
ArrayInvertedLists
|
|
259
|
+
ArrayInvertedLists* il = new ArrayInvertedLists(i1 - i0, src->code_size);
|
|
281
260
|
|
|
282
261
|
for (long i = i0; i < i1; i++) {
|
|
283
|
-
il->add_entries(
|
|
284
|
-
|
|
285
|
-
|
|
262
|
+
il->add_entries(
|
|
263
|
+
i - i0,
|
|
264
|
+
src->list_size(i),
|
|
265
|
+
InvertedLists::ScopedIds(src, i).get(),
|
|
266
|
+
InvertedLists::ScopedCodes(src, i).get());
|
|
286
267
|
}
|
|
287
268
|
return il;
|
|
288
269
|
}
|
|
289
270
|
|
|
271
|
+
void set_invlist_range(
|
|
272
|
+
Index* index,
|
|
273
|
+
long i0,
|
|
274
|
+
long i1,
|
|
275
|
+
ArrayInvertedLists* src) {
|
|
276
|
+
IndexIVF* ivf = extract_index_ivf(index);
|
|
290
277
|
|
|
278
|
+
FAISS_THROW_IF_NOT(0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
|
|
291
279
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
FAISS_THROW_IF_NOT (0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
|
|
298
|
-
|
|
299
|
-
ArrayInvertedLists *dst = dynamic_cast<ArrayInvertedLists *>(ivf->invlists);
|
|
300
|
-
FAISS_THROW_IF_NOT_MSG (dst, "only ArrayInvertedLists supported");
|
|
301
|
-
FAISS_THROW_IF_NOT (src->nlist == i1 - i0 &&
|
|
302
|
-
dst->code_size == src->code_size);
|
|
280
|
+
ArrayInvertedLists* dst = dynamic_cast<ArrayInvertedLists*>(ivf->invlists);
|
|
281
|
+
FAISS_THROW_IF_NOT_MSG(dst, "only ArrayInvertedLists supported");
|
|
282
|
+
FAISS_THROW_IF_NOT(
|
|
283
|
+
src->nlist == i1 - i0 && dst->code_size == src->code_size);
|
|
303
284
|
|
|
304
285
|
size_t ntotal = index->ntotal;
|
|
305
|
-
for (long i = i0
|
|
306
|
-
ntotal -= dst->list_size
|
|
307
|
-
ntotal += src->list_size
|
|
308
|
-
std::swap
|
|
309
|
-
std::swap
|
|
286
|
+
for (long i = i0; i < i1; i++) {
|
|
287
|
+
ntotal -= dst->list_size(i);
|
|
288
|
+
ntotal += src->list_size(i - i0);
|
|
289
|
+
std::swap(src->codes[i - i0], dst->codes[i]);
|
|
290
|
+
std::swap(src->ids[i - i0], dst->ids[i]);
|
|
310
291
|
}
|
|
311
292
|
ivf->ntotal = index->ntotal = ntotal;
|
|
312
293
|
}
|
|
313
294
|
|
|
314
|
-
static size_t count_ndis(
|
|
315
|
-
|
|
316
|
-
|
|
295
|
+
static size_t count_ndis(
|
|
296
|
+
const IndexIVF* index_ivf,
|
|
297
|
+
size_t n_list_scan,
|
|
298
|
+
const idx_t* Iq) {
|
|
317
299
|
size_t nb_dis = 0;
|
|
318
|
-
const InvertedLists
|
|
300
|
+
const InvertedLists* il = index_ivf->invlists;
|
|
319
301
|
for (idx_t i = 0; i < n_list_scan; i++) {
|
|
320
302
|
if (Iq[i] >= 0) {
|
|
321
303
|
nb_dis += il->list_size(Iq[i]);
|
|
@@ -324,23 +306,24 @@ static size_t count_ndis(const IndexIVF * index_ivf, size_t n_list_scan,
|
|
|
324
306
|
return nb_dis;
|
|
325
307
|
}
|
|
326
308
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
309
|
+
void search_with_parameters(
|
|
310
|
+
const Index* index,
|
|
311
|
+
idx_t n,
|
|
312
|
+
const float* x,
|
|
313
|
+
idx_t k,
|
|
314
|
+
float* distances,
|
|
315
|
+
idx_t* labels,
|
|
316
|
+
const IVFSearchParameters* params,
|
|
317
|
+
size_t* nb_dis_ptr,
|
|
318
|
+
double* ms_per_stage) {
|
|
319
|
+
FAISS_THROW_IF_NOT(params);
|
|
320
|
+
const float* prev_x = x;
|
|
338
321
|
ScopeDeleter<float> del;
|
|
339
322
|
|
|
340
323
|
double t0 = getmillisecs();
|
|
341
324
|
|
|
342
|
-
if (auto ip = dynamic_cast<const IndexPreTransform
|
|
343
|
-
x = ip->apply_chain
|
|
325
|
+
if (auto ip = dynamic_cast<const IndexPreTransform*>(index)) {
|
|
326
|
+
x = ip->apply_chain(n, x);
|
|
344
327
|
if (x != prev_x) {
|
|
345
328
|
del.set(x);
|
|
346
329
|
}
|
|
@@ -352,21 +335,19 @@ void search_with_parameters (const Index *index,
|
|
|
352
335
|
std::vector<idx_t> Iq(params->nprobe * n);
|
|
353
336
|
std::vector<float> Dq(params->nprobe * n);
|
|
354
337
|
|
|
355
|
-
const IndexIVF
|
|
356
|
-
FAISS_THROW_IF_NOT
|
|
338
|
+
const IndexIVF* index_ivf = dynamic_cast<const IndexIVF*>(index);
|
|
339
|
+
FAISS_THROW_IF_NOT(index_ivf);
|
|
357
340
|
|
|
358
|
-
index_ivf->quantizer->search(n, x, params->nprobe,
|
|
359
|
-
Dq.data(), Iq.data());
|
|
341
|
+
index_ivf->quantizer->search(n, x, params->nprobe, Dq.data(), Iq.data());
|
|
360
342
|
|
|
361
343
|
if (nb_dis_ptr) {
|
|
362
|
-
*nb_dis_ptr = count_ndis
|
|
344
|
+
*nb_dis_ptr = count_ndis(index_ivf, n * params->nprobe, Iq.data());
|
|
363
345
|
}
|
|
364
346
|
|
|
365
347
|
double t2 = getmillisecs();
|
|
366
348
|
|
|
367
|
-
index_ivf->search_preassigned(
|
|
368
|
-
|
|
369
|
-
false, params);
|
|
349
|
+
index_ivf->search_preassigned(
|
|
350
|
+
n, x, k, Iq.data(), Dq.data(), distances, labels, false, params);
|
|
370
351
|
double t3 = getmillisecs();
|
|
371
352
|
if (ms_per_stage) {
|
|
372
353
|
ms_per_stage[0] = t1 - t0;
|
|
@@ -375,21 +356,23 @@ void search_with_parameters (const Index *index,
|
|
|
375
356
|
}
|
|
376
357
|
}
|
|
377
358
|
|
|
378
|
-
void range_search_with_parameters
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
359
|
+
void range_search_with_parameters(
|
|
360
|
+
const Index* index,
|
|
361
|
+
idx_t n,
|
|
362
|
+
const float* x,
|
|
363
|
+
float radius,
|
|
364
|
+
RangeSearchResult* result,
|
|
365
|
+
const IVFSearchParameters* params,
|
|
366
|
+
size_t* nb_dis_ptr,
|
|
367
|
+
double* ms_per_stage) {
|
|
368
|
+
FAISS_THROW_IF_NOT(params);
|
|
369
|
+
const float* prev_x = x;
|
|
387
370
|
ScopeDeleter<float> del;
|
|
388
371
|
|
|
389
372
|
double t0 = getmillisecs();
|
|
390
373
|
|
|
391
|
-
if (auto ip = dynamic_cast<const IndexPreTransform
|
|
392
|
-
x = ip->apply_chain
|
|
374
|
+
if (auto ip = dynamic_cast<const IndexPreTransform*>(index)) {
|
|
375
|
+
x = ip->apply_chain(n, x);
|
|
393
376
|
if (x != prev_x) {
|
|
394
377
|
del.set(x);
|
|
395
378
|
}
|
|
@@ -401,22 +384,19 @@ void range_search_with_parameters (const Index *index,
|
|
|
401
384
|
std::vector<idx_t> Iq(params->nprobe * n);
|
|
402
385
|
std::vector<float> Dq(params->nprobe * n);
|
|
403
386
|
|
|
404
|
-
const IndexIVF
|
|
405
|
-
FAISS_THROW_IF_NOT
|
|
387
|
+
const IndexIVF* index_ivf = dynamic_cast<const IndexIVF*>(index);
|
|
388
|
+
FAISS_THROW_IF_NOT(index_ivf);
|
|
406
389
|
|
|
407
|
-
index_ivf->quantizer->search(n, x, params->nprobe,
|
|
408
|
-
Dq.data(), Iq.data());
|
|
390
|
+
index_ivf->quantizer->search(n, x, params->nprobe, Dq.data(), Iq.data());
|
|
409
391
|
|
|
410
392
|
if (nb_dis_ptr) {
|
|
411
|
-
*nb_dis_ptr = count_ndis
|
|
393
|
+
*nb_dis_ptr = count_ndis(index_ivf, n * params->nprobe, Iq.data());
|
|
412
394
|
}
|
|
413
395
|
|
|
414
396
|
double t2 = getmillisecs();
|
|
415
397
|
|
|
416
398
|
index_ivf->range_search_preassigned(
|
|
417
|
-
n, x, radius, Iq.data(), Dq.data(),
|
|
418
|
-
result, false, params
|
|
419
|
-
);
|
|
399
|
+
n, x, radius, Iq.data(), Dq.data(), result, false, params);
|
|
420
400
|
|
|
421
401
|
double t3 = getmillisecs();
|
|
422
402
|
if (ms_per_stage) {
|
|
@@ -426,9 +406,5 @@ void range_search_with_parameters (const Index *index,
|
|
|
426
406
|
}
|
|
427
407
|
}
|
|
428
408
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
} } // namespace faiss::ivflib
|
|
409
|
+
} // namespace ivflib
|
|
410
|
+
} // namespace faiss
|