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
|
@@ -5,227 +5,229 @@
|
|
|
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/IndexScalarQuantizer.h>
|
|
11
10
|
#include <faiss/gpu/GpuIndexIVFScalarQuantizer.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
19
|
constexpr float kF32MaxRelErr = 0.03f;
|
|
21
20
|
|
|
22
21
|
struct Options {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
int numQuery;
|
|
64
|
-
int k;
|
|
65
|
-
int device;
|
|
66
|
-
faiss::gpu::IndicesOptions indicesOpt;
|
|
22
|
+
Options() {
|
|
23
|
+
numAdd = 2 * faiss::gpu::randVal(2000, 5000);
|
|
24
|
+
dim = faiss::gpu::randVal(64, 200);
|
|
25
|
+
|
|
26
|
+
numCentroids = std::sqrt((float)numAdd / 2);
|
|
27
|
+
numTrain = numCentroids * 40;
|
|
28
|
+
nprobe = faiss::gpu::randVal(std::min(10, numCentroids), numCentroids);
|
|
29
|
+
numQuery = faiss::gpu::randVal(32, 100);
|
|
30
|
+
|
|
31
|
+
// Due to the approximate nature of the query and of floating point
|
|
32
|
+
// differences between GPU and CPU, to stay within our error bounds,
|
|
33
|
+
// only use a small k
|
|
34
|
+
k = std::min(faiss::gpu::randVal(10, 30), numAdd / 40);
|
|
35
|
+
indicesOpt = faiss::gpu::randSelect(
|
|
36
|
+
{faiss::gpu::INDICES_CPU,
|
|
37
|
+
faiss::gpu::INDICES_32_BIT,
|
|
38
|
+
faiss::gpu::INDICES_64_BIT});
|
|
39
|
+
|
|
40
|
+
device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
std::string toString() const {
|
|
44
|
+
std::stringstream str;
|
|
45
|
+
str << "IVFFlat device " << device << " numVecs " << numAdd << " dim "
|
|
46
|
+
<< dim << " numCentroids " << numCentroids << " nprobe " << nprobe
|
|
47
|
+
<< " numQuery " << numQuery << " k " << k << " indicesOpt "
|
|
48
|
+
<< indicesOpt;
|
|
49
|
+
|
|
50
|
+
return str.str();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
int numAdd;
|
|
54
|
+
int dim;
|
|
55
|
+
int numCentroids;
|
|
56
|
+
int numTrain;
|
|
57
|
+
int nprobe;
|
|
58
|
+
int numQuery;
|
|
59
|
+
int k;
|
|
60
|
+
int device;
|
|
61
|
+
faiss::gpu::IndicesOptions indicesOpt;
|
|
67
62
|
};
|
|
68
63
|
|
|
69
64
|
void runCopyToTest(faiss::ScalarQuantizer::QuantizerType qtype) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
65
|
+
using namespace faiss;
|
|
66
|
+
using namespace faiss::gpu;
|
|
67
|
+
|
|
68
|
+
Options opt;
|
|
69
|
+
std::vector<float> trainVecs = randVecs(opt.numTrain, opt.dim);
|
|
70
|
+
std::vector<float> addVecs = randVecs(opt.numAdd, opt.dim);
|
|
71
|
+
|
|
72
|
+
StandardGpuResources res;
|
|
73
|
+
res.noTempMemory();
|
|
74
|
+
|
|
75
|
+
auto config = GpuIndexIVFScalarQuantizerConfig();
|
|
76
|
+
config.device = opt.device;
|
|
77
|
+
|
|
78
|
+
GpuIndexIVFScalarQuantizer gpuIndex(
|
|
79
|
+
&res, opt.dim, opt.numCentroids, qtype, METRIC_L2, true, config);
|
|
80
|
+
gpuIndex.train(opt.numTrain, trainVecs.data());
|
|
81
|
+
gpuIndex.add(opt.numAdd, addVecs.data());
|
|
82
|
+
gpuIndex.setNumProbes(opt.nprobe);
|
|
83
|
+
|
|
84
|
+
// use garbage values to see if we overwrite then
|
|
85
|
+
IndexFlatL2 cpuQuantizer(1);
|
|
86
|
+
IndexIVFScalarQuantizer cpuIndex(
|
|
87
|
+
&cpuQuantizer,
|
|
88
|
+
1,
|
|
89
|
+
1,
|
|
90
|
+
ScalarQuantizer::QuantizerType::QT_6bit,
|
|
91
|
+
METRIC_L2);
|
|
92
|
+
cpuIndex.nprobe = 1;
|
|
93
|
+
|
|
94
|
+
gpuIndex.copyTo(&cpuIndex);
|
|
95
|
+
|
|
96
|
+
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
|
97
|
+
EXPECT_EQ(gpuIndex.ntotal, opt.numAdd);
|
|
98
|
+
|
|
99
|
+
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
|
100
|
+
EXPECT_EQ(cpuIndex.quantizer->d, gpuIndex.quantizer->d);
|
|
101
|
+
EXPECT_EQ(cpuIndex.d, opt.dim);
|
|
102
|
+
EXPECT_EQ(cpuIndex.nlist, gpuIndex.getNumLists());
|
|
103
|
+
EXPECT_EQ(cpuIndex.nprobe, gpuIndex.getNumProbes());
|
|
104
|
+
|
|
105
|
+
testIVFEquality(cpuIndex, gpuIndex);
|
|
106
|
+
|
|
107
|
+
// Query both objects; results should be equivalent
|
|
108
|
+
compareIndices(
|
|
109
|
+
cpuIndex,
|
|
110
|
+
gpuIndex,
|
|
111
|
+
opt.numQuery,
|
|
112
|
+
opt.dim,
|
|
113
|
+
opt.k,
|
|
114
|
+
opt.toString(),
|
|
115
|
+
kF32MaxRelErr,
|
|
116
|
+
0.1f,
|
|
117
|
+
0.015f);
|
|
120
118
|
}
|
|
121
119
|
|
|
122
120
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyTo_fp16) {
|
|
123
|
-
|
|
121
|
+
runCopyToTest(faiss::ScalarQuantizer::QuantizerType::QT_fp16);
|
|
124
122
|
}
|
|
125
123
|
|
|
126
124
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyTo_8bit) {
|
|
127
|
-
|
|
125
|
+
runCopyToTest(faiss::ScalarQuantizer::QuantizerType::QT_8bit);
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyTo_8bit_uniform) {
|
|
131
|
-
|
|
129
|
+
runCopyToTest(faiss::ScalarQuantizer::QuantizerType::QT_8bit_uniform);
|
|
132
130
|
}
|
|
133
131
|
|
|
134
132
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyTo_6bit) {
|
|
135
|
-
|
|
133
|
+
runCopyToTest(faiss::ScalarQuantizer::QuantizerType::QT_6bit);
|
|
136
134
|
}
|
|
137
135
|
|
|
138
136
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyTo_4bit) {
|
|
139
|
-
|
|
137
|
+
runCopyToTest(faiss::ScalarQuantizer::QuantizerType::QT_4bit);
|
|
140
138
|
}
|
|
141
139
|
|
|
142
140
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyTo_4bit_uniform) {
|
|
143
|
-
|
|
141
|
+
runCopyToTest(faiss::ScalarQuantizer::QuantizerType::QT_4bit_uniform);
|
|
144
142
|
}
|
|
145
143
|
|
|
146
144
|
void runCopyFromTest(faiss::ScalarQuantizer::QuantizerType qtype) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
145
|
+
using namespace faiss;
|
|
146
|
+
using namespace faiss::gpu;
|
|
147
|
+
|
|
148
|
+
Options opt;
|
|
149
|
+
std::vector<float> trainVecs = randVecs(opt.numTrain, opt.dim);
|
|
150
|
+
std::vector<float> addVecs = randVecs(opt.numAdd, opt.dim);
|
|
151
|
+
|
|
152
|
+
IndexFlatL2 cpuQuantizer(opt.dim);
|
|
153
|
+
IndexIVFScalarQuantizer cpuIndex(
|
|
154
|
+
&cpuQuantizer, opt.dim, opt.numCentroids, qtype, METRIC_L2);
|
|
155
|
+
|
|
156
|
+
cpuIndex.nprobe = opt.nprobe;
|
|
157
|
+
cpuIndex.train(opt.numTrain, trainVecs.data());
|
|
158
|
+
cpuIndex.add(opt.numAdd, addVecs.data());
|
|
159
|
+
|
|
160
|
+
// use garbage values to see if we overwrite then
|
|
161
|
+
StandardGpuResources res;
|
|
162
|
+
res.noTempMemory();
|
|
163
|
+
|
|
164
|
+
auto config = GpuIndexIVFScalarQuantizerConfig();
|
|
165
|
+
config.device = opt.device;
|
|
166
|
+
|
|
167
|
+
GpuIndexIVFScalarQuantizer gpuIndex(
|
|
168
|
+
&res,
|
|
169
|
+
1,
|
|
170
|
+
1,
|
|
171
|
+
ScalarQuantizer::QuantizerType::QT_4bit,
|
|
172
|
+
METRIC_L2,
|
|
173
|
+
false,
|
|
174
|
+
config);
|
|
175
|
+
gpuIndex.setNumProbes(1);
|
|
176
|
+
|
|
177
|
+
gpuIndex.copyFrom(&cpuIndex);
|
|
178
|
+
|
|
179
|
+
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
|
180
|
+
EXPECT_EQ(gpuIndex.ntotal, opt.numAdd);
|
|
181
|
+
|
|
182
|
+
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
|
183
|
+
EXPECT_EQ(cpuIndex.d, opt.dim);
|
|
184
|
+
EXPECT_EQ(cpuIndex.nlist, gpuIndex.getNumLists());
|
|
185
|
+
EXPECT_EQ(cpuIndex.nprobe, gpuIndex.getNumProbes());
|
|
186
|
+
|
|
187
|
+
testIVFEquality(cpuIndex, gpuIndex);
|
|
188
|
+
|
|
189
|
+
// Query both objects; results should be equivalent
|
|
190
|
+
compareIndices(
|
|
191
|
+
cpuIndex,
|
|
192
|
+
gpuIndex,
|
|
193
|
+
opt.numQuery,
|
|
194
|
+
opt.dim,
|
|
195
|
+
opt.k,
|
|
196
|
+
opt.toString(),
|
|
197
|
+
kF32MaxRelErr,
|
|
198
|
+
0.1f,
|
|
199
|
+
0.015f);
|
|
198
200
|
}
|
|
199
201
|
|
|
200
202
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyFrom_fp16) {
|
|
201
|
-
|
|
203
|
+
runCopyFromTest(faiss::ScalarQuantizer::QuantizerType::QT_fp16);
|
|
202
204
|
}
|
|
203
205
|
|
|
204
206
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyFrom_8bit) {
|
|
205
|
-
|
|
207
|
+
runCopyFromTest(faiss::ScalarQuantizer::QuantizerType::QT_8bit);
|
|
206
208
|
}
|
|
207
209
|
|
|
208
210
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyFrom_8bit_uniform) {
|
|
209
|
-
|
|
211
|
+
runCopyFromTest(faiss::ScalarQuantizer::QuantizerType::QT_8bit_uniform);
|
|
210
212
|
}
|
|
211
213
|
|
|
212
214
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyFrom_6bit) {
|
|
213
|
-
|
|
215
|
+
runCopyFromTest(faiss::ScalarQuantizer::QuantizerType::QT_6bit);
|
|
214
216
|
}
|
|
215
217
|
|
|
216
218
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyFrom_4bit) {
|
|
217
|
-
|
|
219
|
+
runCopyFromTest(faiss::ScalarQuantizer::QuantizerType::QT_4bit);
|
|
218
220
|
}
|
|
219
221
|
|
|
220
222
|
TEST(TestGpuIndexIVFScalarQuantizer, CopyFrom_4bit_uniform) {
|
|
221
|
-
|
|
223
|
+
runCopyFromTest(faiss::ScalarQuantizer::QuantizerType::QT_4bit_uniform);
|
|
222
224
|
}
|
|
223
225
|
|
|
224
226
|
int main(int argc, char** argv) {
|
|
225
|
-
|
|
227
|
+
testing::InitGoogleTest(&argc, argv);
|
|
226
228
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
+
// just run with a fixed test seed
|
|
230
|
+
faiss::gpu::setTestSeed(100);
|
|
229
231
|
|
|
230
|
-
|
|
232
|
+
return RUN_ALL_TESTS();
|
|
231
233
|
}
|
|
@@ -5,80 +5,88 @@
|
|
|
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/gpu/GpuIndexFlat.h>
|
|
11
10
|
#include <faiss/gpu/StandardGpuResources.h>
|
|
12
|
-
#include <faiss/gpu/utils/DeviceUtils.h>
|
|
13
11
|
#include <faiss/gpu/test/TestUtils.h>
|
|
12
|
+
#include <faiss/gpu/utils/DeviceUtils.h>
|
|
14
13
|
#include <gtest/gtest.h>
|
|
15
14
|
|
|
16
15
|
// Test to see if we can recover after attempting to allocate too much GPU
|
|
17
16
|
// memory
|
|
18
17
|
TEST(TestGpuMemoryException, AddException) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
size_t numBrokenAdd = std::numeric_limits<int>::max();
|
|
19
|
+
size_t numRealAdd = 10000;
|
|
20
|
+
size_t devFree = 0;
|
|
21
|
+
size_t devTotal = 0;
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
CUDA_VERIFY(cudaMemGetInfo(&devFree, &devTotal));
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
// Figure out the dimensionality needed to get at least greater than
|
|
26
|
+
// devTotal
|
|
27
|
+
size_t brokenAddDims = ((devTotal / sizeof(float)) / numBrokenAdd) + 1;
|
|
28
|
+
size_t realAddDims = 128;
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
faiss::gpu::StandardGpuResources res;
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
faiss::gpu::GpuIndexFlatConfig config;
|
|
33
|
+
config.device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
faiss::IndexFlatL2
|
|
40
|
-
cpuIndex((int) realAddDims);
|
|
35
|
+
faiss::gpu::GpuIndexFlatL2 gpuIndexL2Broken(
|
|
36
|
+
&res, (int)brokenAddDims, config);
|
|
37
|
+
faiss::gpu::GpuIndexFlatL2 gpuIndexL2(&res, (int)realAddDims, config);
|
|
38
|
+
faiss::IndexFlatL2 cpuIndex((int)realAddDims);
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
40
|
+
// Should throw on attempting to allocate too much data
|
|
41
|
+
{
|
|
42
|
+
// allocate memory without initialization
|
|
43
|
+
auto vecs = std::unique_ptr<float[]>(
|
|
44
|
+
new float[numBrokenAdd * brokenAddDims]);
|
|
45
|
+
EXPECT_THROW(
|
|
46
|
+
gpuIndexL2Broken.add(numBrokenAdd, vecs.get()),
|
|
47
|
+
faiss::FaissException);
|
|
48
|
+
}
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
// Should be able to add a smaller set of data now
|
|
51
|
+
{
|
|
52
|
+
auto vecs = faiss::gpu::randVecs(numRealAdd, realAddDims);
|
|
53
|
+
EXPECT_NO_THROW(gpuIndexL2.add(numRealAdd, vecs.data()));
|
|
54
|
+
cpuIndex.add(numRealAdd, vecs.data());
|
|
55
|
+
}
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
// Should throw on attempting to allocate too much data
|
|
58
|
+
{
|
|
59
|
+
// allocate memory without initialization
|
|
60
|
+
auto vecs = std::unique_ptr<float[]>(
|
|
61
|
+
new float[numBrokenAdd * brokenAddDims]);
|
|
62
|
+
EXPECT_THROW(
|
|
63
|
+
gpuIndexL2Broken.add(numBrokenAdd, vecs.get()),
|
|
64
|
+
faiss::FaissException);
|
|
65
|
+
}
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
67
|
+
// Should be able to query results from what we had before
|
|
68
|
+
{
|
|
69
|
+
size_t numQuery = 10;
|
|
70
|
+
auto vecs = faiss::gpu::randVecs(numQuery, realAddDims);
|
|
71
|
+
EXPECT_NO_THROW(compareIndices(
|
|
72
|
+
vecs,
|
|
73
|
+
cpuIndex,
|
|
74
|
+
gpuIndexL2,
|
|
75
|
+
numQuery,
|
|
76
|
+
realAddDims,
|
|
77
|
+
50,
|
|
78
|
+
"",
|
|
79
|
+
6e-3f,
|
|
80
|
+
0.1f,
|
|
81
|
+
0.015f));
|
|
82
|
+
}
|
|
75
83
|
}
|
|
76
84
|
|
|
77
85
|
int main(int argc, char** argv) {
|
|
78
|
-
|
|
86
|
+
testing::InitGoogleTest(&argc, argv);
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
// just run with a fixed test seed
|
|
89
|
+
faiss::gpu::setTestSeed(100);
|
|
82
90
|
|
|
83
|
-
|
|
91
|
+
return RUN_ALL_TESTS();
|
|
84
92
|
}
|