faiss 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +103 -3
- data/ext/faiss/ext.cpp +99 -32
- data/ext/faiss/extconf.rb +12 -2
- data/lib/faiss/ext.bundle +0 -0
- data/lib/faiss/index.rb +3 -3
- data/lib/faiss/index_binary.rb +3 -3
- data/lib/faiss/kmeans.rb +1 -1
- data/lib/faiss/pca_matrix.rb +2 -2
- data/lib/faiss/product_quantizer.rb +3 -3
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/AutoTune.cpp +719 -0
- data/vendor/faiss/AutoTune.h +212 -0
- data/vendor/faiss/Clustering.cpp +261 -0
- data/vendor/faiss/Clustering.h +101 -0
- data/vendor/faiss/IVFlib.cpp +339 -0
- data/vendor/faiss/IVFlib.h +132 -0
- data/vendor/faiss/Index.cpp +171 -0
- data/vendor/faiss/Index.h +261 -0
- data/vendor/faiss/Index2Layer.cpp +437 -0
- data/vendor/faiss/Index2Layer.h +85 -0
- data/vendor/faiss/IndexBinary.cpp +77 -0
- data/vendor/faiss/IndexBinary.h +163 -0
- data/vendor/faiss/IndexBinaryFlat.cpp +83 -0
- data/vendor/faiss/IndexBinaryFlat.h +54 -0
- data/vendor/faiss/IndexBinaryFromFloat.cpp +78 -0
- data/vendor/faiss/IndexBinaryFromFloat.h +52 -0
- data/vendor/faiss/IndexBinaryHNSW.cpp +325 -0
- data/vendor/faiss/IndexBinaryHNSW.h +56 -0
- data/vendor/faiss/IndexBinaryIVF.cpp +671 -0
- data/vendor/faiss/IndexBinaryIVF.h +211 -0
- data/vendor/faiss/IndexFlat.cpp +508 -0
- data/vendor/faiss/IndexFlat.h +175 -0
- data/vendor/faiss/IndexHNSW.cpp +1090 -0
- data/vendor/faiss/IndexHNSW.h +170 -0
- data/vendor/faiss/IndexIVF.cpp +909 -0
- data/vendor/faiss/IndexIVF.h +353 -0
- data/vendor/faiss/IndexIVFFlat.cpp +502 -0
- data/vendor/faiss/IndexIVFFlat.h +118 -0
- data/vendor/faiss/IndexIVFPQ.cpp +1207 -0
- data/vendor/faiss/IndexIVFPQ.h +161 -0
- data/vendor/faiss/IndexIVFPQR.cpp +219 -0
- data/vendor/faiss/IndexIVFPQR.h +65 -0
- data/vendor/faiss/IndexIVFSpectralHash.cpp +331 -0
- data/vendor/faiss/IndexIVFSpectralHash.h +75 -0
- data/vendor/faiss/IndexLSH.cpp +225 -0
- data/vendor/faiss/IndexLSH.h +87 -0
- data/vendor/faiss/IndexLattice.cpp +143 -0
- data/vendor/faiss/IndexLattice.h +68 -0
- data/vendor/faiss/IndexPQ.cpp +1188 -0
- data/vendor/faiss/IndexPQ.h +199 -0
- data/vendor/faiss/IndexPreTransform.cpp +288 -0
- data/vendor/faiss/IndexPreTransform.h +91 -0
- data/vendor/faiss/IndexReplicas.cpp +123 -0
- data/vendor/faiss/IndexReplicas.h +76 -0
- data/vendor/faiss/IndexScalarQuantizer.cpp +317 -0
- data/vendor/faiss/IndexScalarQuantizer.h +127 -0
- data/vendor/faiss/IndexShards.cpp +317 -0
- data/vendor/faiss/IndexShards.h +100 -0
- data/vendor/faiss/InvertedLists.cpp +623 -0
- data/vendor/faiss/InvertedLists.h +334 -0
- data/vendor/faiss/LICENSE +21 -0
- data/vendor/faiss/MatrixStats.cpp +252 -0
- data/vendor/faiss/MatrixStats.h +62 -0
- data/vendor/faiss/MetaIndexes.cpp +351 -0
- data/vendor/faiss/MetaIndexes.h +126 -0
- data/vendor/faiss/OnDiskInvertedLists.cpp +674 -0
- data/vendor/faiss/OnDiskInvertedLists.h +127 -0
- data/vendor/faiss/VectorTransform.cpp +1157 -0
- data/vendor/faiss/VectorTransform.h +322 -0
- data/vendor/faiss/c_api/AutoTune_c.cpp +83 -0
- data/vendor/faiss/c_api/AutoTune_c.h +64 -0
- data/vendor/faiss/c_api/Clustering_c.cpp +139 -0
- data/vendor/faiss/c_api/Clustering_c.h +117 -0
- data/vendor/faiss/c_api/IndexFlat_c.cpp +140 -0
- data/vendor/faiss/c_api/IndexFlat_c.h +115 -0
- data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +64 -0
- data/vendor/faiss/c_api/IndexIVFFlat_c.h +58 -0
- data/vendor/faiss/c_api/IndexIVF_c.cpp +92 -0
- data/vendor/faiss/c_api/IndexIVF_c.h +135 -0
- data/vendor/faiss/c_api/IndexLSH_c.cpp +37 -0
- data/vendor/faiss/c_api/IndexLSH_c.h +40 -0
- data/vendor/faiss/c_api/IndexShards_c.cpp +44 -0
- data/vendor/faiss/c_api/IndexShards_c.h +42 -0
- data/vendor/faiss/c_api/Index_c.cpp +105 -0
- data/vendor/faiss/c_api/Index_c.h +183 -0
- data/vendor/faiss/c_api/MetaIndexes_c.cpp +49 -0
- data/vendor/faiss/c_api/MetaIndexes_c.h +49 -0
- data/vendor/faiss/c_api/clone_index_c.cpp +23 -0
- data/vendor/faiss/c_api/clone_index_c.h +32 -0
- data/vendor/faiss/c_api/error_c.h +42 -0
- data/vendor/faiss/c_api/error_impl.cpp +27 -0
- data/vendor/faiss/c_api/error_impl.h +16 -0
- data/vendor/faiss/c_api/faiss_c.h +58 -0
- data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +96 -0
- data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +56 -0
- data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +52 -0
- data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +68 -0
- data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +17 -0
- data/vendor/faiss/c_api/gpu/GpuIndex_c.h +30 -0
- data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +38 -0
- data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +86 -0
- data/vendor/faiss/c_api/gpu/GpuResources_c.h +66 -0
- data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +54 -0
- data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +53 -0
- data/vendor/faiss/c_api/gpu/macros_impl.h +42 -0
- data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +220 -0
- data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +149 -0
- data/vendor/faiss/c_api/index_factory_c.cpp +26 -0
- data/vendor/faiss/c_api/index_factory_c.h +30 -0
- data/vendor/faiss/c_api/index_io_c.cpp +42 -0
- data/vendor/faiss/c_api/index_io_c.h +50 -0
- data/vendor/faiss/c_api/macros_impl.h +110 -0
- data/vendor/faiss/clone_index.cpp +147 -0
- data/vendor/faiss/clone_index.h +38 -0
- data/vendor/faiss/demos/demo_imi_flat.cpp +151 -0
- data/vendor/faiss/demos/demo_imi_pq.cpp +199 -0
- data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +146 -0
- data/vendor/faiss/demos/demo_sift1M.cpp +252 -0
- data/vendor/faiss/gpu/GpuAutoTune.cpp +95 -0
- data/vendor/faiss/gpu/GpuAutoTune.h +27 -0
- data/vendor/faiss/gpu/GpuCloner.cpp +403 -0
- data/vendor/faiss/gpu/GpuCloner.h +82 -0
- data/vendor/faiss/gpu/GpuClonerOptions.cpp +28 -0
- data/vendor/faiss/gpu/GpuClonerOptions.h +53 -0
- data/vendor/faiss/gpu/GpuDistance.h +52 -0
- data/vendor/faiss/gpu/GpuFaissAssert.h +29 -0
- data/vendor/faiss/gpu/GpuIndex.h +148 -0
- data/vendor/faiss/gpu/GpuIndexBinaryFlat.h +89 -0
- data/vendor/faiss/gpu/GpuIndexFlat.h +190 -0
- data/vendor/faiss/gpu/GpuIndexIVF.h +89 -0
- data/vendor/faiss/gpu/GpuIndexIVFFlat.h +85 -0
- data/vendor/faiss/gpu/GpuIndexIVFPQ.h +143 -0
- data/vendor/faiss/gpu/GpuIndexIVFScalarQuantizer.h +100 -0
- data/vendor/faiss/gpu/GpuIndicesOptions.h +30 -0
- data/vendor/faiss/gpu/GpuResources.cpp +52 -0
- data/vendor/faiss/gpu/GpuResources.h +73 -0
- data/vendor/faiss/gpu/StandardGpuResources.cpp +295 -0
- data/vendor/faiss/gpu/StandardGpuResources.h +114 -0
- data/vendor/faiss/gpu/impl/RemapIndices.cpp +43 -0
- data/vendor/faiss/gpu/impl/RemapIndices.h +24 -0
- data/vendor/faiss/gpu/perf/IndexWrapper-inl.h +71 -0
- data/vendor/faiss/gpu/perf/IndexWrapper.h +39 -0
- data/vendor/faiss/gpu/perf/PerfClustering.cpp +115 -0
- data/vendor/faiss/gpu/perf/PerfIVFPQAdd.cpp +139 -0
- data/vendor/faiss/gpu/perf/WriteIndex.cpp +102 -0
- data/vendor/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +130 -0
- data/vendor/faiss/gpu/test/TestGpuIndexFlat.cpp +371 -0
- data/vendor/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +550 -0
- data/vendor/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +450 -0
- data/vendor/faiss/gpu/test/TestGpuMemoryException.cpp +84 -0
- data/vendor/faiss/gpu/test/TestUtils.cpp +315 -0
- data/vendor/faiss/gpu/test/TestUtils.h +93 -0
- data/vendor/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +159 -0
- data/vendor/faiss/gpu/utils/DeviceMemory.cpp +77 -0
- data/vendor/faiss/gpu/utils/DeviceMemory.h +71 -0
- data/vendor/faiss/gpu/utils/DeviceUtils.h +185 -0
- data/vendor/faiss/gpu/utils/MemorySpace.cpp +89 -0
- data/vendor/faiss/gpu/utils/MemorySpace.h +44 -0
- data/vendor/faiss/gpu/utils/StackDeviceMemory.cpp +239 -0
- data/vendor/faiss/gpu/utils/StackDeviceMemory.h +129 -0
- data/vendor/faiss/gpu/utils/StaticUtils.h +83 -0
- data/vendor/faiss/gpu/utils/Timer.cpp +60 -0
- data/vendor/faiss/gpu/utils/Timer.h +52 -0
- data/vendor/faiss/impl/AuxIndexStructures.cpp +305 -0
- data/vendor/faiss/impl/AuxIndexStructures.h +246 -0
- data/vendor/faiss/impl/FaissAssert.h +95 -0
- data/vendor/faiss/impl/FaissException.cpp +66 -0
- data/vendor/faiss/impl/FaissException.h +71 -0
- data/vendor/faiss/impl/HNSW.cpp +818 -0
- data/vendor/faiss/impl/HNSW.h +275 -0
- data/vendor/faiss/impl/PolysemousTraining.cpp +953 -0
- data/vendor/faiss/impl/PolysemousTraining.h +158 -0
- data/vendor/faiss/impl/ProductQuantizer.cpp +876 -0
- data/vendor/faiss/impl/ProductQuantizer.h +242 -0
- data/vendor/faiss/impl/ScalarQuantizer.cpp +1628 -0
- data/vendor/faiss/impl/ScalarQuantizer.h +120 -0
- data/vendor/faiss/impl/ThreadedIndex-inl.h +192 -0
- data/vendor/faiss/impl/ThreadedIndex.h +80 -0
- data/vendor/faiss/impl/index_read.cpp +793 -0
- data/vendor/faiss/impl/index_write.cpp +558 -0
- data/vendor/faiss/impl/io.cpp +142 -0
- data/vendor/faiss/impl/io.h +98 -0
- data/vendor/faiss/impl/lattice_Zn.cpp +712 -0
- data/vendor/faiss/impl/lattice_Zn.h +199 -0
- data/vendor/faiss/index_factory.cpp +392 -0
- data/vendor/faiss/index_factory.h +25 -0
- data/vendor/faiss/index_io.h +75 -0
- data/vendor/faiss/misc/test_blas.cpp +84 -0
- data/vendor/faiss/tests/test_binary_flat.cpp +64 -0
- data/vendor/faiss/tests/test_dealloc_invlists.cpp +183 -0
- data/vendor/faiss/tests/test_ivfpq_codec.cpp +67 -0
- data/vendor/faiss/tests/test_ivfpq_indexing.cpp +98 -0
- data/vendor/faiss/tests/test_lowlevel_ivf.cpp +566 -0
- data/vendor/faiss/tests/test_merge.cpp +258 -0
- data/vendor/faiss/tests/test_omp_threads.cpp +14 -0
- data/vendor/faiss/tests/test_ondisk_ivf.cpp +220 -0
- data/vendor/faiss/tests/test_pairs_decoding.cpp +189 -0
- data/vendor/faiss/tests/test_params_override.cpp +231 -0
- data/vendor/faiss/tests/test_pq_encoding.cpp +98 -0
- data/vendor/faiss/tests/test_sliding_ivf.cpp +240 -0
- data/vendor/faiss/tests/test_threaded_index.cpp +253 -0
- data/vendor/faiss/tests/test_transfer_invlists.cpp +159 -0
- data/vendor/faiss/tutorial/cpp/1-Flat.cpp +98 -0
- data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +81 -0
- data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +93 -0
- data/vendor/faiss/tutorial/cpp/4-GPU.cpp +119 -0
- data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +99 -0
- data/vendor/faiss/utils/Heap.cpp +122 -0
- data/vendor/faiss/utils/Heap.h +495 -0
- data/vendor/faiss/utils/WorkerThread.cpp +126 -0
- data/vendor/faiss/utils/WorkerThread.h +61 -0
- data/vendor/faiss/utils/distances.cpp +765 -0
- data/vendor/faiss/utils/distances.h +243 -0
- data/vendor/faiss/utils/distances_simd.cpp +809 -0
- data/vendor/faiss/utils/extra_distances.cpp +336 -0
- data/vendor/faiss/utils/extra_distances.h +54 -0
- data/vendor/faiss/utils/hamming-inl.h +472 -0
- data/vendor/faiss/utils/hamming.cpp +792 -0
- data/vendor/faiss/utils/hamming.h +220 -0
- data/vendor/faiss/utils/random.cpp +192 -0
- data/vendor/faiss/utils/random.h +60 -0
- data/vendor/faiss/utils/utils.cpp +783 -0
- data/vendor/faiss/utils/utils.h +181 -0
- metadata +216 -2
@@ -0,0 +1,189 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <cstdio>
|
9
|
+
#include <cstdlib>
|
10
|
+
|
11
|
+
#include <memory>
|
12
|
+
#include <vector>
|
13
|
+
|
14
|
+
#include <gtest/gtest.h>
|
15
|
+
|
16
|
+
#include <faiss/IndexIVF.h>
|
17
|
+
#include <faiss/index_factory.h>
|
18
|
+
#include <faiss/VectorTransform.h>
|
19
|
+
#include <faiss/IVFlib.h>
|
20
|
+
|
21
|
+
|
22
|
+
namespace {
|
23
|
+
|
24
|
+
typedef faiss::Index::idx_t idx_t;
|
25
|
+
|
26
|
+
/*************************************************************
|
27
|
+
* Test utils
|
28
|
+
*************************************************************/
|
29
|
+
|
30
|
+
|
31
|
+
// dimension of the vectors to index
|
32
|
+
int d = 64;
|
33
|
+
|
34
|
+
// size of the database we plan to index
|
35
|
+
size_t nb = 8000;
|
36
|
+
|
37
|
+
// nb of queries
|
38
|
+
size_t nq = 200;
|
39
|
+
|
40
|
+
std::vector<float> make_data(size_t n)
|
41
|
+
{
|
42
|
+
std::vector <float> database (n * d);
|
43
|
+
for (size_t i = 0; i < n * d; i++) {
|
44
|
+
database[i] = drand48();
|
45
|
+
}
|
46
|
+
return database;
|
47
|
+
}
|
48
|
+
|
49
|
+
std::unique_ptr<faiss::Index> make_index(const char *index_type,
|
50
|
+
const std::vector<float> & x) {
|
51
|
+
|
52
|
+
auto index = std::unique_ptr<faiss::Index> (
|
53
|
+
faiss::index_factory(d, index_type));
|
54
|
+
index->train(nb, x.data());
|
55
|
+
index->add(nb, x.data());
|
56
|
+
return index;
|
57
|
+
}
|
58
|
+
|
59
|
+
/*************************************************************
|
60
|
+
* Test functions for a given index type
|
61
|
+
*************************************************************/
|
62
|
+
|
63
|
+
bool test_search_centroid(const char *index_key) {
|
64
|
+
std::vector<float> xb = make_data(nb); // database vectors
|
65
|
+
auto index = make_index(index_key, xb);
|
66
|
+
|
67
|
+
/* First test: find the centroids associated to the database
|
68
|
+
vectors and make sure that each vector does indeed appear in
|
69
|
+
the inverted list corresponding to its centroid */
|
70
|
+
|
71
|
+
std::vector<idx_t> centroid_ids (nb);
|
72
|
+
faiss::ivflib::search_centroid(
|
73
|
+
index.get(), xb.data(), nb, centroid_ids.data());
|
74
|
+
|
75
|
+
const faiss::IndexIVF * ivf = faiss::ivflib::extract_index_ivf
|
76
|
+
(index.get());
|
77
|
+
|
78
|
+
for(int i = 0; i < nb; i++) {
|
79
|
+
bool found = false;
|
80
|
+
int list_no = centroid_ids[i];
|
81
|
+
int list_size = ivf->invlists->list_size (list_no);
|
82
|
+
auto * list = ivf->invlists->get_ids (list_no);
|
83
|
+
|
84
|
+
for(int j = 0; j < list_size; j++) {
|
85
|
+
if (list[j] == i) {
|
86
|
+
found = true;
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
if(!found) return false;
|
91
|
+
}
|
92
|
+
return true;
|
93
|
+
}
|
94
|
+
|
95
|
+
int test_search_and_return_centroids(const char *index_key) {
|
96
|
+
std::vector<float> xb = make_data(nb); // database vectors
|
97
|
+
auto index = make_index(index_key, xb);
|
98
|
+
|
99
|
+
std::vector<idx_t> centroid_ids (nb);
|
100
|
+
faiss::ivflib::search_centroid(index.get(), xb.data(),
|
101
|
+
nb, centroid_ids.data());
|
102
|
+
|
103
|
+
faiss::IndexIVF * ivf =
|
104
|
+
faiss::ivflib::extract_index_ivf (index.get());
|
105
|
+
ivf->nprobe = 4;
|
106
|
+
|
107
|
+
std::vector<float> xq = make_data(nq); // database vectors
|
108
|
+
|
109
|
+
int k = 5;
|
110
|
+
|
111
|
+
// compute a reference search result
|
112
|
+
|
113
|
+
std::vector<idx_t> refI (nq * k);
|
114
|
+
std::vector<float> refD (nq * k);
|
115
|
+
index->search (nq, xq.data(), k, refD.data(), refI.data());
|
116
|
+
|
117
|
+
// compute search result
|
118
|
+
|
119
|
+
std::vector<idx_t> newI (nq * k);
|
120
|
+
std::vector<float> newD (nq * k);
|
121
|
+
|
122
|
+
std::vector<idx_t> query_centroid_ids (nq);
|
123
|
+
std::vector<idx_t> result_centroid_ids (nq * k);
|
124
|
+
|
125
|
+
faiss::ivflib::search_and_return_centroids(index.get(),
|
126
|
+
nq, xq.data(), k,
|
127
|
+
newD.data(), newI.data(),
|
128
|
+
query_centroid_ids.data(),
|
129
|
+
result_centroid_ids.data());
|
130
|
+
|
131
|
+
// first verify that we have the same result as the standard search
|
132
|
+
|
133
|
+
if (newI != refI) {
|
134
|
+
return 1;
|
135
|
+
}
|
136
|
+
|
137
|
+
// then check if the result ids are indeed in the inverted list
|
138
|
+
// they are supposed to be in
|
139
|
+
|
140
|
+
for(int i = 0; i < nq * k; i++) {
|
141
|
+
int list_no = result_centroid_ids[i];
|
142
|
+
int result_no = newI[i];
|
143
|
+
|
144
|
+
if (result_no < 0) continue;
|
145
|
+
|
146
|
+
bool found = false;
|
147
|
+
|
148
|
+
int list_size = ivf->invlists->list_size (list_no);
|
149
|
+
auto * list = ivf->invlists->get_ids (list_no);
|
150
|
+
|
151
|
+
for(int j = 0; j < list_size; j++) {
|
152
|
+
if (list[j] == result_no) {
|
153
|
+
found = true;
|
154
|
+
break;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
if(!found) return 2;
|
158
|
+
}
|
159
|
+
return 0;
|
160
|
+
}
|
161
|
+
|
162
|
+
} // namespace
|
163
|
+
|
164
|
+
|
165
|
+
/*************************************************************
|
166
|
+
* Test entry points
|
167
|
+
*************************************************************/
|
168
|
+
|
169
|
+
TEST(test_search_centroid, IVFFlat) {
|
170
|
+
bool ok = test_search_centroid("IVF32,Flat");
|
171
|
+
EXPECT_TRUE(ok);
|
172
|
+
}
|
173
|
+
|
174
|
+
TEST(test_search_centroid, PCAIVFFlat) {
|
175
|
+
bool ok = test_search_centroid("PCA16,IVF32,Flat");
|
176
|
+
EXPECT_TRUE(ok);
|
177
|
+
}
|
178
|
+
|
179
|
+
TEST(test_search_and_return_centroids, IVFFlat) {
|
180
|
+
int err = test_search_and_return_centroids("IVF32,Flat");
|
181
|
+
EXPECT_NE(err, 1);
|
182
|
+
EXPECT_NE(err, 2);
|
183
|
+
}
|
184
|
+
|
185
|
+
TEST(test_search_and_return_centroids, PCAIVFFlat) {
|
186
|
+
int err = test_search_and_return_centroids("PCA16,IVF32,Flat");
|
187
|
+
EXPECT_NE(err, 1);
|
188
|
+
EXPECT_NE(err, 2);
|
189
|
+
}
|
@@ -0,0 +1,231 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <cstdio>
|
9
|
+
#include <cstdlib>
|
10
|
+
|
11
|
+
#include <memory>
|
12
|
+
#include <vector>
|
13
|
+
|
14
|
+
#include <gtest/gtest.h>
|
15
|
+
|
16
|
+
#include <faiss/IndexIVF.h>
|
17
|
+
#include <faiss/IndexBinaryIVF.h>
|
18
|
+
#include <faiss/index_factory.h>
|
19
|
+
#include <faiss/AutoTune.h>
|
20
|
+
#include <faiss/IVFlib.h>
|
21
|
+
|
22
|
+
using namespace faiss;
|
23
|
+
|
24
|
+
namespace {
|
25
|
+
|
26
|
+
typedef Index::idx_t idx_t;
|
27
|
+
|
28
|
+
|
29
|
+
// dimension of the vectors to index
|
30
|
+
int d = 32;
|
31
|
+
|
32
|
+
// size of the database we plan to index
|
33
|
+
size_t nb = 1000;
|
34
|
+
|
35
|
+
// nb of queries
|
36
|
+
size_t nq = 200;
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
std::vector<float> make_data(size_t n)
|
41
|
+
{
|
42
|
+
std::vector <float> database (n * d);
|
43
|
+
for (size_t i = 0; i < n * d; i++) {
|
44
|
+
database[i] = drand48();
|
45
|
+
}
|
46
|
+
return database;
|
47
|
+
}
|
48
|
+
|
49
|
+
std::unique_ptr<Index> make_index(const char *index_type,
|
50
|
+
MetricType metric,
|
51
|
+
const std::vector<float> & x)
|
52
|
+
{
|
53
|
+
std::unique_ptr<Index> index(index_factory(d, index_type, metric));
|
54
|
+
index->train(nb, x.data());
|
55
|
+
index->add(nb, x.data());
|
56
|
+
return index;
|
57
|
+
}
|
58
|
+
|
59
|
+
std::vector<idx_t> search_index(Index *index, const float *xq) {
|
60
|
+
int k = 10;
|
61
|
+
std::vector<idx_t> I(k * nq);
|
62
|
+
std::vector<float> D(k * nq);
|
63
|
+
index->search (nq, xq, k, D.data(), I.data());
|
64
|
+
return I;
|
65
|
+
}
|
66
|
+
|
67
|
+
std::vector<idx_t> search_index_with_params(
|
68
|
+
Index *index, const float *xq, IVFSearchParameters *params) {
|
69
|
+
int k = 10;
|
70
|
+
std::vector<idx_t> I(k * nq);
|
71
|
+
std::vector<float> D(k * nq);
|
72
|
+
ivflib::search_with_parameters (index, nq, xq, k,
|
73
|
+
D.data(), I.data(), params);
|
74
|
+
return I;
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
/*************************************************************
|
81
|
+
* Test functions for a given index type
|
82
|
+
*************************************************************/
|
83
|
+
|
84
|
+
int test_params_override (const char *index_key, MetricType metric) {
|
85
|
+
std::vector<float> xb = make_data(nb); // database vectors
|
86
|
+
auto index = make_index(index_key, metric, xb);
|
87
|
+
//index->train(nb, xb.data());
|
88
|
+
// index->add(nb, xb.data());
|
89
|
+
std::vector<float> xq = make_data(nq);
|
90
|
+
ParameterSpace ps;
|
91
|
+
ps.set_index_parameter(index.get(), "nprobe", 2);
|
92
|
+
auto res2ref = search_index(index.get(), xq.data());
|
93
|
+
ps.set_index_parameter(index.get(), "nprobe", 9);
|
94
|
+
auto res9ref = search_index(index.get(), xq.data());
|
95
|
+
ps.set_index_parameter(index.get(), "nprobe", 1);
|
96
|
+
|
97
|
+
IVFSearchParameters params;
|
98
|
+
params.max_codes = 0;
|
99
|
+
params.nprobe = 2;
|
100
|
+
auto res2new = search_index_with_params(index.get(), xq.data(), ¶ms);
|
101
|
+
params.nprobe = 9;
|
102
|
+
auto res9new = search_index_with_params(index.get(), xq.data(), ¶ms);
|
103
|
+
|
104
|
+
if (res2ref != res2new)
|
105
|
+
return 2;
|
106
|
+
|
107
|
+
if (res9ref != res9new)
|
108
|
+
return 9;
|
109
|
+
|
110
|
+
return 0;
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
} // namespace
|
115
|
+
|
116
|
+
|
117
|
+
/*************************************************************
|
118
|
+
* Test entry points
|
119
|
+
*************************************************************/
|
120
|
+
|
121
|
+
TEST(TPO, IVFFlat) {
|
122
|
+
int err1 = test_params_override ("IVF32,Flat", METRIC_L2);
|
123
|
+
EXPECT_EQ(err1, 0);
|
124
|
+
int err2 = test_params_override ("IVF32,Flat", METRIC_INNER_PRODUCT);
|
125
|
+
EXPECT_EQ(err2, 0);
|
126
|
+
}
|
127
|
+
|
128
|
+
TEST(TPO, IVFPQ) {
|
129
|
+
int err1 = test_params_override ("IVF32,PQ8np", METRIC_L2);
|
130
|
+
EXPECT_EQ(err1, 0);
|
131
|
+
int err2 = test_params_override ("IVF32,PQ8np", METRIC_INNER_PRODUCT);
|
132
|
+
EXPECT_EQ(err2, 0);
|
133
|
+
}
|
134
|
+
|
135
|
+
TEST(TPO, IVFSQ) {
|
136
|
+
int err1 = test_params_override ("IVF32,SQ8", METRIC_L2);
|
137
|
+
EXPECT_EQ(err1, 0);
|
138
|
+
int err2 = test_params_override ("IVF32,SQ8", METRIC_INNER_PRODUCT);
|
139
|
+
EXPECT_EQ(err2, 0);
|
140
|
+
}
|
141
|
+
|
142
|
+
TEST(TPO, IVFFlatPP) {
|
143
|
+
int err1 = test_params_override ("PCA16,IVF32,SQ8", METRIC_L2);
|
144
|
+
EXPECT_EQ(err1, 0);
|
145
|
+
int err2 = test_params_override ("PCA16,IVF32,SQ8", METRIC_INNER_PRODUCT);
|
146
|
+
EXPECT_EQ(err2, 0);
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
/*************************************************************
|
152
|
+
* Same for binary indexes
|
153
|
+
*************************************************************/
|
154
|
+
|
155
|
+
|
156
|
+
std::vector<uint8_t> make_data_binary(size_t n) {
|
157
|
+
std::vector <uint8_t> database (n * d / 8);
|
158
|
+
for (size_t i = 0; i < n * d / 8; i++) {
|
159
|
+
database[i] = lrand48();
|
160
|
+
}
|
161
|
+
return database;
|
162
|
+
}
|
163
|
+
|
164
|
+
std::unique_ptr<IndexBinaryIVF> make_index(const char *index_type,
|
165
|
+
const std::vector<uint8_t> & x)
|
166
|
+
{
|
167
|
+
|
168
|
+
auto index = std::unique_ptr<IndexBinaryIVF>
|
169
|
+
(dynamic_cast<IndexBinaryIVF*>(index_binary_factory (d, index_type)));
|
170
|
+
index->train(nb, x.data());
|
171
|
+
index->add(nb, x.data());
|
172
|
+
return index;
|
173
|
+
}
|
174
|
+
|
175
|
+
std::vector<idx_t> search_index(IndexBinaryIVF *index, const uint8_t *xq) {
|
176
|
+
int k = 10;
|
177
|
+
std::vector<idx_t> I(k * nq);
|
178
|
+
std::vector<int32_t> D(k * nq);
|
179
|
+
index->search (nq, xq, k, D.data(), I.data());
|
180
|
+
return I;
|
181
|
+
}
|
182
|
+
|
183
|
+
std::vector<idx_t> search_index_with_params(
|
184
|
+
IndexBinaryIVF *index, const uint8_t *xq, IVFSearchParameters *params) {
|
185
|
+
int k = 10;
|
186
|
+
std::vector<idx_t> I(k * nq);
|
187
|
+
std::vector<int32_t> D(k * nq);
|
188
|
+
|
189
|
+
std::vector<idx_t> Iq(params->nprobe * nq);
|
190
|
+
std::vector<int32_t> Dq(params->nprobe * nq);
|
191
|
+
|
192
|
+
index->quantizer->search(nq, xq, params->nprobe,
|
193
|
+
Dq.data(), Iq.data());
|
194
|
+
index->search_preassigned(nq, xq, k, Iq.data(), Dq.data(),
|
195
|
+
D.data(), I.data(),
|
196
|
+
false, params);
|
197
|
+
return I;
|
198
|
+
}
|
199
|
+
|
200
|
+
int test_params_override_binary (const char *index_key) {
|
201
|
+
std::vector<uint8_t> xb = make_data_binary(nb); // database vectors
|
202
|
+
auto index = make_index (index_key, xb);
|
203
|
+
index->train(nb, xb.data());
|
204
|
+
index->add(nb, xb.data());
|
205
|
+
std::vector<uint8_t> xq = make_data_binary(nq);
|
206
|
+
index->nprobe = 2;
|
207
|
+
auto res2ref = search_index(index.get(), xq.data());
|
208
|
+
index->nprobe = 9;
|
209
|
+
auto res9ref = search_index(index.get(), xq.data());
|
210
|
+
index->nprobe = 1;
|
211
|
+
|
212
|
+
IVFSearchParameters params;
|
213
|
+
params.max_codes = 0;
|
214
|
+
params.nprobe = 2;
|
215
|
+
auto res2new = search_index_with_params(index.get(), xq.data(), ¶ms);
|
216
|
+
params.nprobe = 9;
|
217
|
+
auto res9new = search_index_with_params(index.get(), xq.data(), ¶ms);
|
218
|
+
|
219
|
+
if (res2ref != res2new)
|
220
|
+
return 2;
|
221
|
+
|
222
|
+
if (res9ref != res9new)
|
223
|
+
return 9;
|
224
|
+
|
225
|
+
return 0;
|
226
|
+
}
|
227
|
+
|
228
|
+
TEST(TPOB, IVF) {
|
229
|
+
int err1 = test_params_override_binary ("BIVF32");
|
230
|
+
EXPECT_EQ(err1, 0);
|
231
|
+
}
|
@@ -0,0 +1,98 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*/
|
7
|
+
|
8
|
+
|
9
|
+
#include <iostream>
|
10
|
+
#include <vector>
|
11
|
+
#include <memory>
|
12
|
+
|
13
|
+
#include <gtest/gtest.h>
|
14
|
+
|
15
|
+
#include <faiss/impl/ProductQuantizer.h>
|
16
|
+
|
17
|
+
|
18
|
+
namespace {
|
19
|
+
|
20
|
+
const std::vector<uint64_t> random_vector(size_t s) {
|
21
|
+
std::vector<uint64_t> v(s, 0);
|
22
|
+
for (size_t i = 0; i < s; ++i) {
|
23
|
+
v[i] = rand();
|
24
|
+
}
|
25
|
+
|
26
|
+
return v;
|
27
|
+
}
|
28
|
+
|
29
|
+
} // namespace
|
30
|
+
|
31
|
+
|
32
|
+
TEST(PQEncoderGeneric, encode) {
|
33
|
+
const int nsubcodes = 97;
|
34
|
+
const int minbits = 1;
|
35
|
+
const int maxbits = 24;
|
36
|
+
const std::vector<uint64_t> values = random_vector(nsubcodes);
|
37
|
+
|
38
|
+
for(int nbits = minbits; nbits <= maxbits; ++nbits) {
|
39
|
+
std::cerr << "nbits = " << nbits << std::endl;
|
40
|
+
|
41
|
+
const uint64_t mask = (1ull << nbits) - 1;
|
42
|
+
std::unique_ptr<uint8_t[]> codes(
|
43
|
+
new uint8_t[(nsubcodes * maxbits + 7) / 8]
|
44
|
+
);
|
45
|
+
|
46
|
+
// NOTE(hoss): Necessary scope to ensure trailing bits are flushed to mem.
|
47
|
+
{
|
48
|
+
faiss::ProductQuantizer::PQEncoderGeneric encoder(codes.get(), nbits);
|
49
|
+
for (const auto& v : values) {
|
50
|
+
encoder.encode(v & mask);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
faiss::ProductQuantizer::PQDecoderGeneric decoder(codes.get(), nbits);
|
55
|
+
for (int i = 0; i < nsubcodes; ++i) {
|
56
|
+
uint64_t v = decoder.decode();
|
57
|
+
EXPECT_EQ(values[i] & mask, v);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
|
63
|
+
TEST(PQEncoder8, encode) {
|
64
|
+
const int nsubcodes = 100;
|
65
|
+
const std::vector<uint64_t> values = random_vector(nsubcodes);
|
66
|
+
const uint64_t mask = 0xFF;
|
67
|
+
std::unique_ptr<uint8_t[]> codes(new uint8_t[nsubcodes]);
|
68
|
+
|
69
|
+
faiss::ProductQuantizer::PQEncoder8 encoder(codes.get(), 8);
|
70
|
+
for (const auto& v : values) {
|
71
|
+
encoder.encode(v & mask);
|
72
|
+
}
|
73
|
+
|
74
|
+
faiss::ProductQuantizer::PQDecoder8 decoder(codes.get(), 8);
|
75
|
+
for (int i = 0; i < nsubcodes; ++i) {
|
76
|
+
uint64_t v = decoder.decode();
|
77
|
+
EXPECT_EQ(values[i] & mask, v);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
TEST(PQEncoder16, encode) {
|
83
|
+
const int nsubcodes = 100;
|
84
|
+
const std::vector<uint64_t> values = random_vector(nsubcodes);
|
85
|
+
const uint64_t mask = 0xFFFF;
|
86
|
+
std::unique_ptr<uint8_t[]> codes(new uint8_t[2 * nsubcodes]);
|
87
|
+
|
88
|
+
faiss::ProductQuantizer::PQEncoder16 encoder(codes.get(), 16);
|
89
|
+
for (const auto& v : values) {
|
90
|
+
encoder.encode(v & mask);
|
91
|
+
}
|
92
|
+
|
93
|
+
faiss::ProductQuantizer::PQDecoder16 decoder(codes.get(), 16);
|
94
|
+
for (int i = 0; i < nsubcodes; ++i) {
|
95
|
+
uint64_t v = decoder.decode();
|
96
|
+
EXPECT_EQ(values[i] & mask, v);
|
97
|
+
}
|
98
|
+
}
|