faiss 0.1.4 → 0.2.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 +26 -1
- data/README.md +15 -3
- data/ext/faiss/ext.cpp +12 -308
- data/ext/faiss/extconf.rb +5 -2
- data/ext/faiss/index.cpp +189 -0
- data/ext/faiss/index_binary.cpp +75 -0
- data/ext/faiss/kmeans.cpp +40 -0
- data/ext/faiss/numo.hpp +867 -0
- data/ext/faiss/pca_matrix.cpp +33 -0
- data/ext/faiss/product_quantizer.cpp +53 -0
- data/ext/faiss/utils.cpp +13 -0
- data/ext/faiss/utils.h +5 -0
- data/lib/faiss.rb +0 -5
- 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 +334 -195
- data/vendor/faiss/faiss/Clustering.h +88 -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 +139 -164
- data/vendor/faiss/faiss/Index2Layer.h +22 -22
- 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 +116 -147
- data/vendor/faiss/faiss/IndexFlat.h +35 -46
- data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
- data/vendor/faiss/faiss/IndexHNSW.h +57 -41
- data/vendor/faiss/faiss/IndexIVF.cpp +474 -454
- data/vendor/faiss/faiss/IndexIVF.h +146 -113
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +248 -250
- data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +457 -516
- data/vendor/faiss/faiss/IndexIVFPQ.h +74 -66
- 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 +125 -133
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +19 -21
- data/vendor/faiss/faiss/IndexLSH.cpp +75 -96
- data/vendor/faiss/faiss/IndexLSH.h +21 -26
- data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
- data/vendor/faiss/faiss/IndexLattice.h +11 -16
- data/vendor/faiss/faiss/IndexNNDescent.cpp +231 -0
- data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
- data/vendor/faiss/faiss/IndexNSG.cpp +303 -0
- data/vendor/faiss/faiss/IndexNSG.h +85 -0
- data/vendor/faiss/faiss/IndexPQ.cpp +405 -464
- data/vendor/faiss/faiss/IndexPQ.h +64 -67
- 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 +115 -131
- data/vendor/faiss/faiss/IndexRefine.h +22 -23
- data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
- data/vendor/faiss/faiss/IndexReplicas.h +62 -56
- data/vendor/faiss/faiss/IndexResidual.cpp +291 -0
- data/vendor/faiss/faiss/IndexResidual.h +152 -0
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +120 -155
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -45
- 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 +652 -474
- data/vendor/faiss/faiss/VectorTransform.h +61 -89
- data/vendor/faiss/faiss/clone_index.cpp +77 -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 +197 -170
- 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/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 +270 -0
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +115 -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 +595 -611
- data/vendor/faiss/faiss/impl/HNSW.h +179 -200
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +672 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +172 -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 +682 -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 +448 -0
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +130 -0
- data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +648 -701
- 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 +547 -479
- data/vendor/faiss/faiss/impl/index_write.cpp +497 -407
- data/vendor/faiss/faiss/impl/io.cpp +75 -94
- data/vendor/faiss/faiss/impl/io.h +31 -41
- data/vendor/faiss/faiss/impl/io_macros.h +40 -29
- 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 +269 -218
- data/vendor/faiss/faiss/index_factory.h +6 -7
- 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 +301 -310
- data/vendor/faiss/faiss/utils/distances.h +133 -118
- data/vendor/faiss/faiss/utils/distances_simd.cpp +456 -516
- 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 +53 -48
- metadata +31 -10
- data/lib/faiss/index.rb +0 -20
- data/lib/faiss/index_binary.rb +0 -20
- data/lib/faiss/kmeans.rb +0 -15
- data/lib/faiss/pca_matrix.rb +0 -15
- data/lib/faiss/product_quantizer.rb +0 -22
@@ -5,322 +5,526 @@
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
6
6
|
*/
|
7
7
|
|
8
|
-
|
9
8
|
#include <faiss/IndexFlat.h>
|
10
9
|
#include <faiss/IndexIVFPQ.h>
|
11
10
|
#include <faiss/gpu/GpuIndexIVFPQ.h>
|
12
11
|
#include <faiss/gpu/StandardGpuResources.h>
|
13
|
-
#include <faiss/gpu/utils/DeviceUtils.h>
|
14
12
|
#include <faiss/gpu/test/TestUtils.h>
|
15
|
-
#include <
|
13
|
+
#include <faiss/gpu/utils/DeviceUtils.h>
|
16
14
|
#include <gtest/gtest.h>
|
15
|
+
#include <cmath>
|
17
16
|
#include <sstream>
|
18
17
|
#include <vector>
|
19
18
|
|
20
|
-
|
21
19
|
void pickEncoding(int& codes, int& dim) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
return;
|
20
|
+
std::vector<int> codeSizes{
|
21
|
+
3, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 96};
|
22
|
+
|
23
|
+
// Above 32 doesn't work with no precomputed codes
|
24
|
+
std::vector<int> dimSizes{4, 8, 10, 12, 16, 20, 24, 28, 32};
|
25
|
+
|
26
|
+
while (true) {
|
27
|
+
codes = codeSizes[faiss::gpu::randVal(0, codeSizes.size() - 1)];
|
28
|
+
dim = codes * dimSizes[faiss::gpu::randVal(0, dimSizes.size() - 1)];
|
29
|
+
|
30
|
+
// for such a small test, super-low or high dim is more likely to
|
31
|
+
// generate comparison errors
|
32
|
+
if (dim < 256 && dim >= 64) {
|
33
|
+
return;
|
34
|
+
}
|
38
35
|
}
|
39
|
-
}
|
40
36
|
}
|
41
37
|
|
42
38
|
struct Options {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
39
|
+
Options() {
|
40
|
+
numAdd = faiss::gpu::randVal(2000, 5000);
|
41
|
+
numCentroids = std::sqrt((float)numAdd);
|
42
|
+
numTrain = numCentroids * 40;
|
43
|
+
|
44
|
+
pickEncoding(codes, dim);
|
45
|
+
|
46
|
+
// TODO: Change back to `faiss::gpu::randVal(3, 7)` when we officially
|
47
|
+
// support non-multiple of 8 subcodes for IVFPQ.
|
48
|
+
bitsPerCode = 8;
|
49
|
+
nprobe = std::min(faiss::gpu::randVal(40, 1000), numCentroids);
|
50
|
+
numQuery = faiss::gpu::randVal(4, 8);
|
51
|
+
|
52
|
+
// Due to the approximate nature of the query and of floating point
|
53
|
+
// differences between GPU and CPU, to stay within our error bounds,
|
54
|
+
// only use a small k
|
55
|
+
k = std::min(faiss::gpu::randVal(5, 20), numAdd / 40);
|
56
|
+
usePrecomputed = faiss::gpu::randBool();
|
57
|
+
indicesOpt = faiss::gpu::randSelect(
|
58
|
+
{faiss::gpu::INDICES_CPU,
|
59
|
+
faiss::gpu::INDICES_32_BIT,
|
60
|
+
faiss::gpu::INDICES_64_BIT});
|
61
|
+
if (codes > 48) {
|
62
|
+
// large codes can only fit using float16
|
63
|
+
useFloat16 = true;
|
64
|
+
} else {
|
65
|
+
useFloat16 = faiss::gpu::randBool();
|
66
|
+
}
|
67
|
+
|
68
|
+
device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
70
69
|
}
|
71
70
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
<< " numCentroids " << numCentroids
|
81
|
-
<< " codes " << codes
|
82
|
-
<< " bitsPerCode " << bitsPerCode
|
83
|
-
<< " nprobe " << nprobe
|
84
|
-
<< " numQuery " << numQuery
|
85
|
-
<< " k " << k
|
86
|
-
<< " usePrecomputed " << usePrecomputed
|
87
|
-
<< " indicesOpt " << indicesOpt
|
88
|
-
<< " useFloat16 " << useFloat16;
|
89
|
-
|
90
|
-
return str.str();
|
91
|
-
}
|
92
|
-
|
93
|
-
float getCompareEpsilon() const {
|
94
|
-
return 0.035f;
|
95
|
-
}
|
96
|
-
|
97
|
-
float getPctMaxDiff1() const {
|
98
|
-
return useFloat16 ? 0.30f : 0.10f;
|
99
|
-
}
|
100
|
-
|
101
|
-
float getPctMaxDiffN() const {
|
102
|
-
return useFloat16 ? 0.05f : 0.02f;
|
103
|
-
}
|
104
|
-
|
105
|
-
int numAdd;
|
106
|
-
int numCentroids;
|
107
|
-
int numTrain;
|
108
|
-
int codes;
|
109
|
-
int dim;
|
110
|
-
int bitsPerCode;
|
111
|
-
int nprobe;
|
112
|
-
int numQuery;
|
113
|
-
int k;
|
114
|
-
bool usePrecomputed;
|
115
|
-
faiss::gpu::IndicesOptions indicesOpt;
|
116
|
-
bool useFloat16;
|
117
|
-
int device;
|
118
|
-
};
|
119
|
-
|
120
|
-
TEST(TestGpuIndexIVFPQ, Query_L2) {
|
121
|
-
for (int tries = 0; tries < 2; ++tries) {
|
122
|
-
Options opt;
|
71
|
+
std::string toString() const {
|
72
|
+
std::stringstream str;
|
73
|
+
str << "IVFPQ device " << device << " numVecs " << numAdd << " dim "
|
74
|
+
<< dim << " numCentroids " << numCentroids << " codes " << codes
|
75
|
+
<< " bitsPerCode " << bitsPerCode << " nprobe " << nprobe
|
76
|
+
<< " numQuery " << numQuery << " k " << k << " usePrecomputed "
|
77
|
+
<< usePrecomputed << " indicesOpt " << indicesOpt << " useFloat16 "
|
78
|
+
<< useFloat16;
|
123
79
|
|
124
|
-
|
125
|
-
|
80
|
+
return str.str();
|
81
|
+
}
|
126
82
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
cpuIndex.nprobe = opt.nprobe;
|
131
|
-
cpuIndex.train(opt.numTrain, trainVecs.data());
|
132
|
-
cpuIndex.add(opt.numAdd, addVecs.data());
|
83
|
+
float getCompareEpsilon() const {
|
84
|
+
return 0.035f;
|
85
|
+
}
|
133
86
|
|
134
|
-
|
135
|
-
|
87
|
+
float getPctMaxDiff1() const {
|
88
|
+
return useFloat16 ? 0.30f : 0.10f;
|
89
|
+
}
|
136
90
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
config.indicesOptions = opt.indicesOpt;
|
141
|
-
config.useFloat16LookupTables = opt.useFloat16;
|
91
|
+
float getPctMaxDiffN() const {
|
92
|
+
return useFloat16 ? 0.05f : 0.02f;
|
93
|
+
}
|
142
94
|
|
143
|
-
|
144
|
-
|
95
|
+
int numAdd;
|
96
|
+
int numCentroids;
|
97
|
+
int numTrain;
|
98
|
+
int codes;
|
99
|
+
int dim;
|
100
|
+
int bitsPerCode;
|
101
|
+
int nprobe;
|
102
|
+
int numQuery;
|
103
|
+
int k;
|
104
|
+
bool usePrecomputed;
|
105
|
+
faiss::gpu::IndicesOptions indicesOpt;
|
106
|
+
bool useFloat16;
|
107
|
+
int device;
|
108
|
+
};
|
145
109
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
110
|
+
TEST(TestGpuIndexIVFPQ, Query_L2) {
|
111
|
+
for (int tries = 0; tries < 2; ++tries) {
|
112
|
+
Options opt;
|
113
|
+
|
114
|
+
std::vector<float> trainVecs =
|
115
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
116
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
117
|
+
|
118
|
+
faiss::IndexFlatL2 coarseQuantizer(opt.dim);
|
119
|
+
faiss::IndexIVFPQ cpuIndex(
|
120
|
+
&coarseQuantizer,
|
121
|
+
opt.dim,
|
122
|
+
opt.numCentroids,
|
123
|
+
opt.codes,
|
124
|
+
opt.bitsPerCode);
|
125
|
+
cpuIndex.nprobe = opt.nprobe;
|
126
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
127
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
128
|
+
|
129
|
+
// Use the default temporary memory management to test the memory
|
130
|
+
// manager
|
131
|
+
faiss::gpu::StandardGpuResources res;
|
132
|
+
|
133
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
134
|
+
config.device = opt.device;
|
135
|
+
config.usePrecomputedTables = (tries % 2 == 0);
|
136
|
+
config.indicesOptions = opt.indicesOpt;
|
137
|
+
config.useFloat16LookupTables = opt.useFloat16;
|
138
|
+
|
139
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
140
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
141
|
+
|
142
|
+
faiss::gpu::compareIndices(
|
143
|
+
cpuIndex,
|
144
|
+
gpuIndex,
|
145
|
+
opt.numQuery,
|
146
|
+
opt.dim,
|
147
|
+
opt.k,
|
148
|
+
opt.toString(),
|
149
|
+
opt.getCompareEpsilon(),
|
150
|
+
opt.getPctMaxDiff1(),
|
151
|
+
opt.getPctMaxDiffN());
|
152
|
+
}
|
152
153
|
}
|
153
154
|
|
154
155
|
void testMMCodeDistance(faiss::MetricType mt) {
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
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
|
-
faiss::IndexFlat coarseQuantizer(opt.dim, mt);
|
205
|
-
faiss::IndexIVFPQ cpuIndex(&coarseQuantizer, opt.dim, opt.numCentroids,
|
206
|
-
opt.codes, opt.bitsPerCode);
|
207
|
-
cpuIndex.nprobe = opt.nprobe;
|
208
|
-
cpuIndex.train(opt.numTrain, trainVecs.data());
|
209
|
-
cpuIndex.add(opt.numAdd, addVecs.data());
|
210
|
-
|
211
|
-
// Use the default temporary memory management to test the memory manager
|
212
|
-
faiss::gpu::StandardGpuResources res;
|
213
|
-
|
214
|
-
faiss::gpu::GpuIndexIVFPQConfig config;
|
215
|
-
config.device = opt.device;
|
216
|
-
config.usePrecomputedTables = false;
|
217
|
-
config.indicesOptions = opt.indicesOpt;
|
218
|
-
|
219
|
-
// Make sure that the float16 version works as well
|
220
|
-
config.useFloat16LookupTables = (dimPerSubQ == 7);
|
221
|
-
|
222
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
223
|
-
gpuIndex.setNumProbes(opt.nprobe);
|
156
|
+
// Explicitly test the code distance via batch matrix multiplication route
|
157
|
+
// (even for dimension sizes that would otherwise be handled by the
|
158
|
+
// specialized route (via enabling `useMMCodeDistance`)
|
159
|
+
for (int tries = 0; tries < 2; ++tries) {
|
160
|
+
Options opt;
|
161
|
+
|
162
|
+
std::vector<float> trainVecs =
|
163
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
164
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
165
|
+
|
166
|
+
faiss::IndexFlat coarseQuantizer(opt.dim, mt);
|
167
|
+
faiss::IndexIVFPQ cpuIndex(
|
168
|
+
&coarseQuantizer,
|
169
|
+
opt.dim,
|
170
|
+
opt.numCentroids,
|
171
|
+
opt.codes,
|
172
|
+
opt.bitsPerCode);
|
173
|
+
cpuIndex.nprobe = opt.nprobe;
|
174
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
175
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
176
|
+
|
177
|
+
// Use the default temporary memory management to test the memory
|
178
|
+
// manager
|
179
|
+
faiss::gpu::StandardGpuResources res;
|
180
|
+
|
181
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
182
|
+
config.device = opt.device;
|
183
|
+
config.usePrecomputedTables = false;
|
184
|
+
config.useMMCodeDistance = true;
|
185
|
+
config.indicesOptions = opt.indicesOpt;
|
186
|
+
|
187
|
+
// Make sure that the float16 version works as well
|
188
|
+
config.useFloat16LookupTables = (tries % 2 == 0);
|
189
|
+
config.flatConfig.useFloat16 = (tries % 2 == 1);
|
190
|
+
|
191
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
192
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
193
|
+
|
194
|
+
faiss::gpu::compareIndices(
|
195
|
+
cpuIndex,
|
196
|
+
gpuIndex,
|
197
|
+
opt.numQuery,
|
198
|
+
opt.dim,
|
199
|
+
opt.k,
|
200
|
+
opt.toString(),
|
201
|
+
opt.getCompareEpsilon(),
|
202
|
+
opt.getPctMaxDiff1(),
|
203
|
+
opt.getPctMaxDiffN());
|
204
|
+
}
|
224
205
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
206
|
+
// These sizes are not specialized, they will fall back to the MM version
|
207
|
+
for (int dimPerSubQ : {7, 11}) {
|
208
|
+
Options opt;
|
209
|
+
|
210
|
+
opt.codes = 12;
|
211
|
+
opt.dim = dimPerSubQ * opt.codes;
|
212
|
+
|
213
|
+
std::vector<float> trainVecs =
|
214
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
215
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
216
|
+
|
217
|
+
faiss::IndexFlat coarseQuantizer(opt.dim, mt);
|
218
|
+
faiss::IndexIVFPQ cpuIndex(
|
219
|
+
&coarseQuantizer,
|
220
|
+
opt.dim,
|
221
|
+
opt.numCentroids,
|
222
|
+
opt.codes,
|
223
|
+
opt.bitsPerCode);
|
224
|
+
cpuIndex.nprobe = opt.nprobe;
|
225
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
226
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
227
|
+
|
228
|
+
// Use the default temporary memory management to test the memory
|
229
|
+
// manager
|
230
|
+
faiss::gpu::StandardGpuResources res;
|
231
|
+
|
232
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
233
|
+
config.device = opt.device;
|
234
|
+
config.usePrecomputedTables = false;
|
235
|
+
config.indicesOptions = opt.indicesOpt;
|
236
|
+
|
237
|
+
// Make sure that the float16 version works as well
|
238
|
+
config.useFloat16LookupTables = (dimPerSubQ == 7);
|
239
|
+
|
240
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
241
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
242
|
+
|
243
|
+
faiss::gpu::compareIndices(
|
244
|
+
cpuIndex,
|
245
|
+
gpuIndex,
|
246
|
+
opt.numQuery,
|
247
|
+
opt.dim,
|
248
|
+
opt.k,
|
249
|
+
opt.toString(),
|
250
|
+
opt.getCompareEpsilon(),
|
251
|
+
opt.getPctMaxDiff1(),
|
252
|
+
opt.getPctMaxDiffN());
|
253
|
+
}
|
231
254
|
}
|
232
255
|
|
233
256
|
TEST(TestGpuIndexIVFPQ, Query_L2_MMCodeDistance) {
|
234
|
-
|
257
|
+
testMMCodeDistance(faiss::MetricType::METRIC_L2);
|
235
258
|
}
|
236
259
|
|
237
260
|
TEST(TestGpuIndexIVFPQ, Query_IP_MMCodeDistance) {
|
238
|
-
|
261
|
+
testMMCodeDistance(faiss::MetricType::METRIC_INNER_PRODUCT);
|
239
262
|
}
|
240
263
|
|
241
264
|
TEST(TestGpuIndexIVFPQ, Query_IP) {
|
242
|
-
|
265
|
+
for (int tries = 0; tries < 2; ++tries) {
|
266
|
+
Options opt;
|
267
|
+
|
268
|
+
std::vector<float> trainVecs =
|
269
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
270
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
271
|
+
|
272
|
+
faiss::IndexFlatIP coarseQuantizer(opt.dim);
|
273
|
+
faiss::IndexIVFPQ cpuIndex(
|
274
|
+
&coarseQuantizer,
|
275
|
+
opt.dim,
|
276
|
+
opt.numCentroids,
|
277
|
+
opt.codes,
|
278
|
+
opt.bitsPerCode);
|
279
|
+
cpuIndex.metric_type = faiss::MetricType::METRIC_INNER_PRODUCT;
|
280
|
+
|
281
|
+
cpuIndex.nprobe = opt.nprobe;
|
282
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
283
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
284
|
+
|
285
|
+
// Use the default temporary memory management to test the memory
|
286
|
+
// manager
|
287
|
+
faiss::gpu::StandardGpuResources res;
|
288
|
+
|
289
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
290
|
+
config.device = opt.device;
|
291
|
+
config.usePrecomputedTables = false; // not supported/required for IP
|
292
|
+
config.indicesOptions = opt.indicesOpt;
|
293
|
+
config.useFloat16LookupTables = opt.useFloat16;
|
294
|
+
|
295
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
296
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
297
|
+
|
298
|
+
faiss::gpu::compareIndices(
|
299
|
+
cpuIndex,
|
300
|
+
gpuIndex,
|
301
|
+
opt.numQuery,
|
302
|
+
opt.dim,
|
303
|
+
opt.k,
|
304
|
+
opt.toString(),
|
305
|
+
opt.getCompareEpsilon(),
|
306
|
+
opt.getPctMaxDiff1(),
|
307
|
+
opt.getPctMaxDiffN());
|
308
|
+
}
|
309
|
+
}
|
310
|
+
|
311
|
+
TEST(TestGpuIndexIVFPQ, Float16Coarse) {
|
243
312
|
Options opt;
|
244
313
|
|
245
314
|
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
246
315
|
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
247
316
|
|
248
|
-
faiss::
|
249
|
-
faiss::IndexIVFPQ cpuIndex(
|
250
|
-
|
251
|
-
|
252
|
-
|
317
|
+
faiss::IndexFlatL2 coarseQuantizer(opt.dim);
|
318
|
+
faiss::IndexIVFPQ cpuIndex(
|
319
|
+
&coarseQuantizer,
|
320
|
+
opt.dim,
|
321
|
+
opt.numCentroids,
|
322
|
+
opt.codes,
|
323
|
+
opt.bitsPerCode);
|
253
324
|
cpuIndex.nprobe = opt.nprobe;
|
254
325
|
cpuIndex.train(opt.numTrain, trainVecs.data());
|
255
|
-
cpuIndex.add(opt.numAdd, addVecs.data());
|
256
326
|
|
257
327
|
// Use the default temporary memory management to test the memory manager
|
258
328
|
faiss::gpu::StandardGpuResources res;
|
259
329
|
|
260
330
|
faiss::gpu::GpuIndexIVFPQConfig config;
|
261
331
|
config.device = opt.device;
|
262
|
-
config.
|
332
|
+
config.flatConfig.useFloat16 = true;
|
333
|
+
config.usePrecomputedTables = opt.usePrecomputed;
|
263
334
|
config.indicesOptions = opt.indicesOpt;
|
264
335
|
config.useFloat16LookupTables = opt.useFloat16;
|
265
336
|
|
266
337
|
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
267
338
|
gpuIndex.setNumProbes(opt.nprobe);
|
268
339
|
|
269
|
-
|
270
|
-
|
271
|
-
opt.getCompareEpsilon(),
|
272
|
-
opt.getPctMaxDiff1(),
|
273
|
-
opt.getPctMaxDiffN());
|
274
|
-
}
|
275
|
-
}
|
340
|
+
gpuIndex.add(opt.numAdd, addVecs.data());
|
341
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
276
342
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
cpuIndex.train(opt.numTrain, trainVecs.data());
|
288
|
-
|
289
|
-
// Use the default temporary memory management to test the memory manager
|
290
|
-
faiss::gpu::StandardGpuResources res;
|
291
|
-
|
292
|
-
faiss::gpu::GpuIndexIVFPQConfig config;
|
293
|
-
config.device = opt.device;
|
294
|
-
config.flatConfig.useFloat16 = true;
|
295
|
-
config.usePrecomputedTables = opt.usePrecomputed;
|
296
|
-
config.indicesOptions = opt.indicesOpt;
|
297
|
-
config.useFloat16LookupTables = opt.useFloat16;
|
298
|
-
|
299
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
300
|
-
gpuIndex.setNumProbes(opt.nprobe);
|
301
|
-
|
302
|
-
gpuIndex.add(opt.numAdd, addVecs.data());
|
303
|
-
cpuIndex.add(opt.numAdd, addVecs.data());
|
304
|
-
|
305
|
-
faiss::gpu::compareIndices(cpuIndex, gpuIndex,
|
306
|
-
opt.numQuery, opt.dim, opt.k, opt.toString(),
|
307
|
-
opt.getCompareEpsilon(),
|
308
|
-
opt.getPctMaxDiff1(),
|
309
|
-
opt.getPctMaxDiffN());
|
343
|
+
faiss::gpu::compareIndices(
|
344
|
+
cpuIndex,
|
345
|
+
gpuIndex,
|
346
|
+
opt.numQuery,
|
347
|
+
opt.dim,
|
348
|
+
opt.k,
|
349
|
+
opt.toString(),
|
350
|
+
opt.getCompareEpsilon(),
|
351
|
+
opt.getPctMaxDiff1(),
|
352
|
+
opt.getPctMaxDiffN());
|
310
353
|
}
|
311
354
|
|
312
355
|
TEST(TestGpuIndexIVFPQ, Add_L2) {
|
313
|
-
|
314
|
-
|
356
|
+
for (int tries = 0; tries < 2; ++tries) {
|
357
|
+
Options opt;
|
358
|
+
|
359
|
+
std::vector<float> trainVecs =
|
360
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
361
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
362
|
+
|
363
|
+
faiss::IndexFlatL2 coarseQuantizer(opt.dim);
|
364
|
+
faiss::IndexIVFPQ cpuIndex(
|
365
|
+
&coarseQuantizer,
|
366
|
+
opt.dim,
|
367
|
+
opt.numCentroids,
|
368
|
+
opt.codes,
|
369
|
+
opt.bitsPerCode);
|
370
|
+
cpuIndex.nprobe = opt.nprobe;
|
371
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
372
|
+
|
373
|
+
// Use the default temporary memory management to test the memory
|
374
|
+
// manager
|
375
|
+
faiss::gpu::StandardGpuResources res;
|
376
|
+
|
377
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
378
|
+
config.device = opt.device;
|
379
|
+
config.usePrecomputedTables = opt.usePrecomputed;
|
380
|
+
config.indicesOptions = opt.indicesOpt;
|
381
|
+
config.useFloat16LookupTables = opt.useFloat16;
|
382
|
+
|
383
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
384
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
385
|
+
|
386
|
+
gpuIndex.add(opt.numAdd, addVecs.data());
|
387
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
388
|
+
|
389
|
+
faiss::gpu::compareIndices(
|
390
|
+
cpuIndex,
|
391
|
+
gpuIndex,
|
392
|
+
opt.numQuery,
|
393
|
+
opt.dim,
|
394
|
+
opt.k,
|
395
|
+
opt.toString(),
|
396
|
+
opt.getCompareEpsilon(),
|
397
|
+
opt.getPctMaxDiff1(),
|
398
|
+
opt.getPctMaxDiffN());
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
TEST(TestGpuIndexIVFPQ, Add_IP) {
|
403
|
+
for (int tries = 0; tries < 2; ++tries) {
|
404
|
+
Options opt;
|
405
|
+
|
406
|
+
std::vector<float> trainVecs =
|
407
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
408
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
409
|
+
|
410
|
+
faiss::IndexFlatIP coarseQuantizer(opt.dim);
|
411
|
+
faiss::IndexIVFPQ cpuIndex(
|
412
|
+
&coarseQuantizer,
|
413
|
+
opt.dim,
|
414
|
+
opt.numCentroids,
|
415
|
+
opt.codes,
|
416
|
+
opt.bitsPerCode);
|
417
|
+
cpuIndex.metric_type = faiss::MetricType::METRIC_INNER_PRODUCT;
|
418
|
+
cpuIndex.nprobe = opt.nprobe;
|
419
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
420
|
+
|
421
|
+
// Use the default temporary memory management to test the memory
|
422
|
+
// manager
|
423
|
+
faiss::gpu::StandardGpuResources res;
|
424
|
+
|
425
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
426
|
+
config.device = opt.device;
|
427
|
+
config.usePrecomputedTables = opt.usePrecomputed;
|
428
|
+
config.indicesOptions = opt.indicesOpt;
|
429
|
+
config.useFloat16LookupTables = opt.useFloat16;
|
430
|
+
|
431
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res, &cpuIndex, config);
|
432
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
433
|
+
|
434
|
+
gpuIndex.add(opt.numAdd, addVecs.data());
|
435
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
436
|
+
|
437
|
+
faiss::gpu::compareIndices(
|
438
|
+
cpuIndex,
|
439
|
+
gpuIndex,
|
440
|
+
opt.numQuery,
|
441
|
+
opt.dim,
|
442
|
+
opt.k,
|
443
|
+
opt.toString(),
|
444
|
+
opt.getCompareEpsilon(),
|
445
|
+
opt.getPctMaxDiff1(),
|
446
|
+
opt.getPctMaxDiffN());
|
447
|
+
}
|
448
|
+
}
|
449
|
+
|
450
|
+
TEST(TestGpuIndexIVFPQ, CopyTo) {
|
451
|
+
for (int tries = 0; tries < 2; ++tries) {
|
452
|
+
Options opt;
|
453
|
+
std::vector<float> trainVecs =
|
454
|
+
faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
455
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
456
|
+
|
457
|
+
// Use the default temporary memory management to test the memory
|
458
|
+
// manager
|
459
|
+
faiss::gpu::StandardGpuResources res;
|
460
|
+
|
461
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
462
|
+
config.device = opt.device;
|
463
|
+
config.usePrecomputedTables = (tries % 2 == 0);
|
464
|
+
config.indicesOptions = opt.indicesOpt;
|
465
|
+
config.useFloat16LookupTables = opt.useFloat16;
|
466
|
+
|
467
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
468
|
+
&res,
|
469
|
+
opt.dim,
|
470
|
+
opt.numCentroids,
|
471
|
+
opt.codes,
|
472
|
+
opt.bitsPerCode,
|
473
|
+
faiss::METRIC_L2,
|
474
|
+
config);
|
475
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
476
|
+
gpuIndex.train(opt.numTrain, trainVecs.data());
|
477
|
+
gpuIndex.add(opt.numAdd, addVecs.data());
|
478
|
+
|
479
|
+
// Use garbage values to see if we overwrite them
|
480
|
+
faiss::IndexFlatL2 cpuQuantizer(1);
|
481
|
+
faiss::IndexIVFPQ cpuIndex(&cpuQuantizer, 1, 1, 1, 1);
|
482
|
+
|
483
|
+
gpuIndex.copyTo(&cpuIndex);
|
484
|
+
|
485
|
+
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
486
|
+
EXPECT_EQ(gpuIndex.ntotal, opt.numAdd);
|
487
|
+
|
488
|
+
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
489
|
+
EXPECT_EQ(cpuIndex.d, opt.dim);
|
490
|
+
EXPECT_EQ(cpuIndex.nlist, gpuIndex.getNumLists());
|
491
|
+
EXPECT_EQ(cpuIndex.nprobe, gpuIndex.getNumProbes());
|
492
|
+
EXPECT_EQ(cpuIndex.pq.M, gpuIndex.getNumSubQuantizers());
|
493
|
+
EXPECT_EQ(gpuIndex.getNumSubQuantizers(), opt.codes);
|
494
|
+
EXPECT_EQ(cpuIndex.pq.nbits, gpuIndex.getBitsPerCode());
|
495
|
+
EXPECT_EQ(gpuIndex.getBitsPerCode(), opt.bitsPerCode);
|
496
|
+
|
497
|
+
testIVFEquality(cpuIndex, gpuIndex);
|
498
|
+
|
499
|
+
// Query both objects; results should be equivalent
|
500
|
+
faiss::gpu::compareIndices(
|
501
|
+
cpuIndex,
|
502
|
+
gpuIndex,
|
503
|
+
opt.numQuery,
|
504
|
+
opt.dim,
|
505
|
+
opt.k,
|
506
|
+
opt.toString(),
|
507
|
+
opt.getCompareEpsilon(),
|
508
|
+
opt.getPctMaxDiff1(),
|
509
|
+
opt.getPctMaxDiffN());
|
510
|
+
}
|
511
|
+
}
|
315
512
|
|
513
|
+
TEST(TestGpuIndexIVFPQ, CopyFrom) {
|
514
|
+
Options opt;
|
316
515
|
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
317
516
|
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
318
517
|
|
319
518
|
faiss::IndexFlatL2 coarseQuantizer(opt.dim);
|
320
|
-
faiss::IndexIVFPQ cpuIndex(
|
321
|
-
|
519
|
+
faiss::IndexIVFPQ cpuIndex(
|
520
|
+
&coarseQuantizer,
|
521
|
+
opt.dim,
|
522
|
+
opt.numCentroids,
|
523
|
+
opt.codes,
|
524
|
+
opt.bitsPerCode);
|
322
525
|
cpuIndex.nprobe = opt.nprobe;
|
323
526
|
cpuIndex.train(opt.numTrain, trainVecs.data());
|
527
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
324
528
|
|
325
529
|
// Use the default temporary memory management to test the memory manager
|
326
530
|
faiss::gpu::StandardGpuResources res;
|
@@ -331,34 +535,47 @@ TEST(TestGpuIndexIVFPQ, Add_L2) {
|
|
331
535
|
config.indicesOptions = opt.indicesOpt;
|
332
536
|
config.useFloat16LookupTables = opt.useFloat16;
|
333
537
|
|
334
|
-
|
335
|
-
gpuIndex
|
538
|
+
// Use garbage values to see if we overwrite them
|
539
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
540
|
+
&res, 1, 1, 1, 8, faiss::METRIC_L2, config);
|
541
|
+
gpuIndex.setNumProbes(1);
|
336
542
|
|
337
|
-
gpuIndex.
|
338
|
-
|
543
|
+
gpuIndex.copyFrom(&cpuIndex);
|
544
|
+
|
545
|
+
// Make sure we are equivalent
|
546
|
+
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
547
|
+
EXPECT_EQ(gpuIndex.ntotal, opt.numAdd);
|
548
|
+
|
549
|
+
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
550
|
+
EXPECT_EQ(cpuIndex.d, opt.dim);
|
551
|
+
EXPECT_EQ(cpuIndex.nlist, gpuIndex.getNumLists());
|
552
|
+
EXPECT_EQ(cpuIndex.nprobe, gpuIndex.getNumProbes());
|
553
|
+
EXPECT_EQ(cpuIndex.pq.M, gpuIndex.getNumSubQuantizers());
|
554
|
+
EXPECT_EQ(gpuIndex.getNumSubQuantizers(), opt.codes);
|
555
|
+
EXPECT_EQ(cpuIndex.pq.nbits, gpuIndex.getBitsPerCode());
|
556
|
+
EXPECT_EQ(gpuIndex.getBitsPerCode(), opt.bitsPerCode);
|
557
|
+
|
558
|
+
testIVFEquality(cpuIndex, gpuIndex);
|
339
559
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
560
|
+
// Query both objects; results should be equivalent
|
561
|
+
faiss::gpu::compareIndices(
|
562
|
+
cpuIndex,
|
563
|
+
gpuIndex,
|
564
|
+
opt.numQuery,
|
565
|
+
opt.dim,
|
566
|
+
opt.k,
|
567
|
+
opt.toString(),
|
568
|
+
opt.getCompareEpsilon(),
|
569
|
+
opt.getPctMaxDiff1(),
|
570
|
+
opt.getPctMaxDiffN());
|
346
571
|
}
|
347
572
|
|
348
|
-
TEST(TestGpuIndexIVFPQ,
|
349
|
-
for (int tries = 0; tries < 2; ++tries) {
|
573
|
+
TEST(TestGpuIndexIVFPQ, QueryNaN) {
|
350
574
|
Options opt;
|
351
575
|
|
352
576
|
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
353
577
|
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
354
578
|
|
355
|
-
faiss::IndexFlatIP coarseQuantizer(opt.dim);
|
356
|
-
faiss::IndexIVFPQ cpuIndex(&coarseQuantizer, opt.dim, opt.numCentroids,
|
357
|
-
opt.codes, opt.bitsPerCode);
|
358
|
-
cpuIndex.metric_type = faiss::MetricType::METRIC_INNER_PRODUCT;
|
359
|
-
cpuIndex.nprobe = opt.nprobe;
|
360
|
-
cpuIndex.train(opt.numTrain, trainVecs.data());
|
361
|
-
|
362
579
|
// Use the default temporary memory management to test the memory manager
|
363
580
|
faiss::gpu::StandardGpuResources res;
|
364
581
|
|
@@ -368,284 +585,161 @@ TEST(TestGpuIndexIVFPQ, Add_IP) {
|
|
368
585
|
config.indicesOptions = opt.indicesOpt;
|
369
586
|
config.useFloat16LookupTables = opt.useFloat16;
|
370
587
|
|
371
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
588
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
589
|
+
&res,
|
590
|
+
opt.dim,
|
591
|
+
opt.numCentroids,
|
592
|
+
opt.codes,
|
593
|
+
opt.bitsPerCode,
|
594
|
+
faiss::METRIC_L2,
|
595
|
+
config);
|
596
|
+
|
372
597
|
gpuIndex.setNumProbes(opt.nprobe);
|
373
598
|
|
599
|
+
gpuIndex.train(opt.numTrain, trainVecs.data());
|
374
600
|
gpuIndex.add(opt.numAdd, addVecs.data());
|
375
|
-
cpuIndex.add(opt.numAdd, addVecs.data());
|
376
601
|
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
602
|
+
int numQuery = 5;
|
603
|
+
std::vector<float> nans(
|
604
|
+
numQuery * opt.dim, std::numeric_limits<float>::quiet_NaN());
|
605
|
+
|
606
|
+
std::vector<float> distances(numQuery * opt.k, 0);
|
607
|
+
std::vector<faiss::Index::idx_t> indices(numQuery * opt.k, 0);
|
608
|
+
|
609
|
+
gpuIndex.search(
|
610
|
+
numQuery, nans.data(), opt.k, distances.data(), indices.data());
|
611
|
+
|
612
|
+
for (int q = 0; q < numQuery; ++q) {
|
613
|
+
for (int k = 0; k < opt.k; ++k) {
|
614
|
+
EXPECT_EQ(indices[q * opt.k + k], -1);
|
615
|
+
EXPECT_EQ(
|
616
|
+
distances[q * opt.k + k],
|
617
|
+
std::numeric_limits<float>::max());
|
618
|
+
}
|
619
|
+
}
|
383
620
|
}
|
384
621
|
|
385
|
-
TEST(TestGpuIndexIVFPQ,
|
386
|
-
for (int tries = 0; tries < 2; ++tries) {
|
622
|
+
TEST(TestGpuIndexIVFPQ, AddNaN) {
|
387
623
|
Options opt;
|
388
|
-
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
389
|
-
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
390
624
|
|
391
625
|
// Use the default temporary memory management to test the memory manager
|
392
626
|
faiss::gpu::StandardGpuResources res;
|
393
627
|
|
394
628
|
faiss::gpu::GpuIndexIVFPQConfig config;
|
395
629
|
config.device = opt.device;
|
396
|
-
config.usePrecomputedTables =
|
630
|
+
config.usePrecomputedTables = opt.usePrecomputed;
|
397
631
|
config.indicesOptions = opt.indicesOpt;
|
398
632
|
config.useFloat16LookupTables = opt.useFloat16;
|
399
633
|
|
400
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
634
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
635
|
+
&res,
|
636
|
+
opt.dim,
|
637
|
+
opt.numCentroids,
|
638
|
+
opt.codes,
|
639
|
+
opt.bitsPerCode,
|
640
|
+
faiss::METRIC_L2,
|
641
|
+
config);
|
642
|
+
|
407
643
|
gpuIndex.setNumProbes(opt.nprobe);
|
644
|
+
|
645
|
+
int numNans = 10;
|
646
|
+
std::vector<float> nans(
|
647
|
+
numNans * opt.dim, std::numeric_limits<float>::quiet_NaN());
|
648
|
+
|
649
|
+
// Make one vector valid, which should actually add
|
650
|
+
for (int i = 0; i < opt.dim; ++i) {
|
651
|
+
nans[i] = 0.0f;
|
652
|
+
}
|
653
|
+
|
654
|
+
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
408
655
|
gpuIndex.train(opt.numTrain, trainVecs.data());
|
409
|
-
gpuIndex.add(opt.numAdd, addVecs.data());
|
410
656
|
|
411
|
-
//
|
412
|
-
|
413
|
-
|
657
|
+
// should not crash
|
658
|
+
EXPECT_EQ(gpuIndex.ntotal, 0);
|
659
|
+
gpuIndex.add(numNans, nans.data());
|
660
|
+
|
661
|
+
std::vector<float> queryVecs = faiss::gpu::randVecs(opt.numQuery, opt.dim);
|
662
|
+
std::vector<float> distance(opt.numQuery * opt.k, 0);
|
663
|
+
std::vector<faiss::Index::idx_t> indices(opt.numQuery * opt.k, 0);
|
664
|
+
|
665
|
+
// should not crash
|
666
|
+
gpuIndex.search(
|
667
|
+
opt.numQuery,
|
668
|
+
queryVecs.data(),
|
669
|
+
opt.k,
|
670
|
+
distance.data(),
|
671
|
+
indices.data());
|
672
|
+
}
|
414
673
|
|
415
|
-
|
674
|
+
TEST(TestGpuIndexIVFPQ, UnifiedMemory) {
|
675
|
+
// Construct on a random device to test multi-device, if we have
|
676
|
+
// multiple devices
|
677
|
+
int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
416
678
|
|
417
|
-
|
418
|
-
|
679
|
+
if (!faiss::gpu::getFullUnifiedMemSupport(device)) {
|
680
|
+
return;
|
681
|
+
}
|
419
682
|
|
420
|
-
|
421
|
-
EXPECT_EQ(cpuIndex.d, opt.dim);
|
422
|
-
EXPECT_EQ(cpuIndex.nlist, gpuIndex.getNumLists());
|
423
|
-
EXPECT_EQ(cpuIndex.nprobe, gpuIndex.getNumProbes());
|
424
|
-
EXPECT_EQ(cpuIndex.pq.M, gpuIndex.getNumSubQuantizers());
|
425
|
-
EXPECT_EQ(gpuIndex.getNumSubQuantizers(), opt.codes);
|
426
|
-
EXPECT_EQ(cpuIndex.pq.nbits, gpuIndex.getBitsPerCode());
|
427
|
-
EXPECT_EQ(gpuIndex.getBitsPerCode(), opt.bitsPerCode);
|
683
|
+
int dim = 128;
|
428
684
|
|
429
|
-
|
685
|
+
int numCentroids = 256;
|
686
|
+
// Unfortunately it would take forever to add 24 GB in IVFPQ data,
|
687
|
+
// so just perform a small test with data allocated in the unified
|
688
|
+
// memory address space
|
689
|
+
size_t numAdd = 10000;
|
690
|
+
size_t numTrain = numCentroids * 40;
|
691
|
+
int numQuery = 10;
|
692
|
+
int k = 10;
|
693
|
+
int nprobe = 8;
|
694
|
+
int codes = 8;
|
695
|
+
int bitsPerCode = 8;
|
430
696
|
|
431
|
-
|
432
|
-
faiss::gpu::
|
433
|
-
opt.numQuery, opt.dim, opt.k, opt.toString(),
|
434
|
-
opt.getCompareEpsilon(),
|
435
|
-
opt.getPctMaxDiff1(),
|
436
|
-
opt.getPctMaxDiffN());
|
437
|
-
}
|
438
|
-
}
|
697
|
+
std::vector<float> trainVecs = faiss::gpu::randVecs(numTrain, dim);
|
698
|
+
std::vector<float> addVecs = faiss::gpu::randVecs(numAdd, dim);
|
439
699
|
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
444
|
-
|
445
|
-
faiss::IndexFlatL2 coarseQuantizer(opt.dim);
|
446
|
-
faiss::IndexIVFPQ cpuIndex(&coarseQuantizer, opt.dim, opt.numCentroids,
|
447
|
-
opt.codes, opt.bitsPerCode);
|
448
|
-
cpuIndex.nprobe = opt.nprobe;
|
449
|
-
cpuIndex.train(opt.numTrain, trainVecs.data());
|
450
|
-
cpuIndex.add(opt.numAdd, addVecs.data());
|
451
|
-
|
452
|
-
// Use the default temporary memory management to test the memory manager
|
453
|
-
faiss::gpu::StandardGpuResources res;
|
454
|
-
|
455
|
-
faiss::gpu::GpuIndexIVFPQConfig config;
|
456
|
-
config.device = opt.device;
|
457
|
-
config.usePrecomputedTables = opt.usePrecomputed;
|
458
|
-
config.indicesOptions = opt.indicesOpt;
|
459
|
-
config.useFloat16LookupTables = opt.useFloat16;
|
460
|
-
|
461
|
-
// Use garbage values to see if we overwrite them
|
462
|
-
faiss::gpu::GpuIndexIVFPQ
|
463
|
-
gpuIndex(&res, 1, 1, 1, 8, faiss::METRIC_L2, config);
|
464
|
-
gpuIndex.setNumProbes(1);
|
465
|
-
|
466
|
-
gpuIndex.copyFrom(&cpuIndex);
|
467
|
-
|
468
|
-
// Make sure we are equivalent
|
469
|
-
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
470
|
-
EXPECT_EQ(gpuIndex.ntotal, opt.numAdd);
|
471
|
-
|
472
|
-
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
473
|
-
EXPECT_EQ(cpuIndex.d, opt.dim);
|
474
|
-
EXPECT_EQ(cpuIndex.nlist, gpuIndex.getNumLists());
|
475
|
-
EXPECT_EQ(cpuIndex.nprobe, gpuIndex.getNumProbes());
|
476
|
-
EXPECT_EQ(cpuIndex.pq.M, gpuIndex.getNumSubQuantizers());
|
477
|
-
EXPECT_EQ(gpuIndex.getNumSubQuantizers(), opt.codes);
|
478
|
-
EXPECT_EQ(cpuIndex.pq.nbits, gpuIndex.getBitsPerCode());
|
479
|
-
EXPECT_EQ(gpuIndex.getBitsPerCode(), opt.bitsPerCode);
|
480
|
-
|
481
|
-
testIVFEquality(cpuIndex, gpuIndex);
|
482
|
-
|
483
|
-
// Query both objects; results should be equivalent
|
484
|
-
faiss::gpu::compareIndices(cpuIndex, gpuIndex,
|
485
|
-
opt.numQuery, opt.dim, opt.k, opt.toString(),
|
486
|
-
opt.getCompareEpsilon(),
|
487
|
-
opt.getPctMaxDiff1(),
|
488
|
-
opt.getPctMaxDiffN());
|
489
|
-
}
|
700
|
+
faiss::IndexFlatL2 quantizer(dim);
|
701
|
+
faiss::IndexIVFPQ cpuIndex(
|
702
|
+
&quantizer, dim, numCentroids, codes, bitsPerCode);
|
490
703
|
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
495
|
-
std::vector<float> addVecs = faiss::gpu::randVecs(opt.numAdd, opt.dim);
|
496
|
-
|
497
|
-
// Use the default temporary memory management to test the memory manager
|
498
|
-
faiss::gpu::StandardGpuResources res;
|
499
|
-
|
500
|
-
faiss::gpu::GpuIndexIVFPQConfig config;
|
501
|
-
config.device = opt.device;
|
502
|
-
config.usePrecomputedTables = opt.usePrecomputed;
|
503
|
-
config.indicesOptions = opt.indicesOpt;
|
504
|
-
config.useFloat16LookupTables = opt.useFloat16;
|
505
|
-
|
506
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res,
|
507
|
-
opt.dim,
|
508
|
-
opt.numCentroids,
|
509
|
-
opt.codes,
|
510
|
-
opt.bitsPerCode,
|
511
|
-
faiss::METRIC_L2,
|
512
|
-
config);
|
513
|
-
|
514
|
-
gpuIndex.setNumProbes(opt.nprobe);
|
515
|
-
|
516
|
-
gpuIndex.train(opt.numTrain, trainVecs.data());
|
517
|
-
gpuIndex.add(opt.numAdd, addVecs.data());
|
518
|
-
|
519
|
-
int numQuery = 5;
|
520
|
-
std::vector<float> nans(numQuery * opt.dim,
|
521
|
-
std::numeric_limits<float>::quiet_NaN());
|
522
|
-
|
523
|
-
std::vector<float> distances(numQuery * opt.k, 0);
|
524
|
-
std::vector<faiss::Index::idx_t> indices(numQuery * opt.k, 0);
|
525
|
-
|
526
|
-
gpuIndex.search(numQuery,
|
527
|
-
nans.data(),
|
528
|
-
opt.k,
|
529
|
-
distances.data(),
|
530
|
-
indices.data());
|
531
|
-
|
532
|
-
for (int q = 0; q < numQuery; ++q) {
|
533
|
-
for (int k = 0; k < opt.k; ++k) {
|
534
|
-
EXPECT_EQ(indices[q * opt.k + k], -1);
|
535
|
-
EXPECT_EQ(distances[q * opt.k + k], std::numeric_limits<float>::max());
|
536
|
-
}
|
537
|
-
}
|
538
|
-
}
|
704
|
+
cpuIndex.train(numTrain, trainVecs.data());
|
705
|
+
cpuIndex.add(numAdd, addVecs.data());
|
706
|
+
cpuIndex.nprobe = nprobe;
|
539
707
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
// Use the default temporary memory management to test the memory manager
|
544
|
-
faiss::gpu::StandardGpuResources res;
|
545
|
-
|
546
|
-
faiss::gpu::GpuIndexIVFPQConfig config;
|
547
|
-
config.device = opt.device;
|
548
|
-
config.usePrecomputedTables = opt.usePrecomputed;
|
549
|
-
config.indicesOptions = opt.indicesOpt;
|
550
|
-
config.useFloat16LookupTables = opt.useFloat16;
|
551
|
-
|
552
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res,
|
553
|
-
opt.dim,
|
554
|
-
opt.numCentroids,
|
555
|
-
opt.codes,
|
556
|
-
opt.bitsPerCode,
|
557
|
-
faiss::METRIC_L2,
|
558
|
-
config);
|
559
|
-
|
560
|
-
gpuIndex.setNumProbes(opt.nprobe);
|
561
|
-
|
562
|
-
int numNans = 10;
|
563
|
-
std::vector<float> nans(numNans * opt.dim,
|
564
|
-
std::numeric_limits<float>::quiet_NaN());
|
565
|
-
|
566
|
-
// Make one vector valid, which should actually add
|
567
|
-
for (int i = 0; i < opt.dim; ++i) {
|
568
|
-
nans[i] = 0.0f;
|
569
|
-
}
|
570
|
-
|
571
|
-
std::vector<float> trainVecs = faiss::gpu::randVecs(opt.numTrain, opt.dim);
|
572
|
-
gpuIndex.train(opt.numTrain, trainVecs.data());
|
573
|
-
|
574
|
-
// should not crash
|
575
|
-
EXPECT_EQ(gpuIndex.ntotal, 0);
|
576
|
-
gpuIndex.add(numNans, nans.data());
|
577
|
-
|
578
|
-
std::vector<float> queryVecs = faiss::gpu::randVecs(opt.numQuery, opt.dim);
|
579
|
-
std::vector<float> distance(opt.numQuery * opt.k, 0);
|
580
|
-
std::vector<faiss::Index::idx_t> indices(opt.numQuery * opt.k, 0);
|
581
|
-
|
582
|
-
// should not crash
|
583
|
-
gpuIndex.search(opt.numQuery, queryVecs.data(), opt.k,
|
584
|
-
distance.data(), indices.data());
|
585
|
-
}
|
708
|
+
faiss::gpu::StandardGpuResources res;
|
709
|
+
res.noTempMemory();
|
586
710
|
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
faiss::IndexFlatL2 quantizer(dim);
|
614
|
-
faiss::IndexIVFPQ cpuIndex(&quantizer, dim, numCentroids, codes, bitsPerCode);
|
615
|
-
|
616
|
-
cpuIndex.train(numTrain, trainVecs.data());
|
617
|
-
cpuIndex.add(numAdd, addVecs.data());
|
618
|
-
cpuIndex.nprobe = nprobe;
|
619
|
-
|
620
|
-
faiss::gpu::StandardGpuResources res;
|
621
|
-
res.noTempMemory();
|
622
|
-
|
623
|
-
faiss::gpu::GpuIndexIVFPQConfig config;
|
624
|
-
config.device = device;
|
625
|
-
config.memorySpace = faiss::gpu::MemorySpace::Unified;
|
626
|
-
|
627
|
-
faiss::gpu::GpuIndexIVFPQ gpuIndex(&res,
|
628
|
-
dim,
|
629
|
-
numCentroids,
|
630
|
-
codes,
|
631
|
-
bitsPerCode,
|
632
|
-
faiss::METRIC_L2,
|
633
|
-
config);
|
634
|
-
gpuIndex.copyFrom(&cpuIndex);
|
635
|
-
gpuIndex.setNumProbes(nprobe);
|
636
|
-
|
637
|
-
faiss::gpu::compareIndices(cpuIndex, gpuIndex,
|
638
|
-
numQuery, dim, k, "Unified Memory",
|
639
|
-
0.015f,
|
640
|
-
0.1f,
|
641
|
-
0.015f);
|
711
|
+
faiss::gpu::GpuIndexIVFPQConfig config;
|
712
|
+
config.device = device;
|
713
|
+
config.memorySpace = faiss::gpu::MemorySpace::Unified;
|
714
|
+
|
715
|
+
faiss::gpu::GpuIndexIVFPQ gpuIndex(
|
716
|
+
&res,
|
717
|
+
dim,
|
718
|
+
numCentroids,
|
719
|
+
codes,
|
720
|
+
bitsPerCode,
|
721
|
+
faiss::METRIC_L2,
|
722
|
+
config);
|
723
|
+
gpuIndex.copyFrom(&cpuIndex);
|
724
|
+
gpuIndex.setNumProbes(nprobe);
|
725
|
+
|
726
|
+
faiss::gpu::compareIndices(
|
727
|
+
cpuIndex,
|
728
|
+
gpuIndex,
|
729
|
+
numQuery,
|
730
|
+
dim,
|
731
|
+
k,
|
732
|
+
"Unified Memory",
|
733
|
+
0.015f,
|
734
|
+
0.1f,
|
735
|
+
0.015f);
|
642
736
|
}
|
643
737
|
|
644
738
|
int main(int argc, char** argv) {
|
645
|
-
|
739
|
+
testing::InitGoogleTest(&argc, argv);
|
646
740
|
|
647
|
-
|
648
|
-
|
741
|
+
// just run with a fixed test seed
|
742
|
+
faiss::gpu::setTestSeed(100);
|
649
743
|
|
650
|
-
|
744
|
+
return RUN_ALL_TESTS();
|
651
745
|
}
|