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,12 +5,11 @@
|
|
|
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
|
#include <sstream>
|
|
16
15
|
#include <vector>
|
|
@@ -20,93 +19,109 @@ constexpr float kF16MaxRelErr = 0.07f;
|
|
|
20
19
|
constexpr float kF32MaxRelErr = 6e-3f;
|
|
21
20
|
|
|
22
21
|
struct TestFlatOptions {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
int dimOverride;
|
|
22
|
+
TestFlatOptions()
|
|
23
|
+
: metric(faiss::MetricType::METRIC_L2),
|
|
24
|
+
metricArg(0),
|
|
25
|
+
useFloat16(false),
|
|
26
|
+
useTransposed(false),
|
|
27
|
+
numVecsOverride(-1),
|
|
28
|
+
numQueriesOverride(-1),
|
|
29
|
+
kOverride(-1),
|
|
30
|
+
dimOverride(-1) {}
|
|
31
|
+
|
|
32
|
+
faiss::MetricType metric;
|
|
33
|
+
float metricArg;
|
|
34
|
+
|
|
35
|
+
bool useFloat16;
|
|
36
|
+
bool useTransposed;
|
|
37
|
+
int numVecsOverride;
|
|
38
|
+
int numQueriesOverride;
|
|
39
|
+
int kOverride;
|
|
40
|
+
int dimOverride;
|
|
43
41
|
};
|
|
44
42
|
|
|
45
43
|
void testFlat(const TestFlatOptions& opt) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
44
|
+
int numVecs = opt.numVecsOverride > 0 ? opt.numVecsOverride
|
|
45
|
+
: faiss::gpu::randVal(1000, 5000);
|
|
46
|
+
int dim = opt.dimOverride > 0 ? opt.dimOverride
|
|
47
|
+
: faiss::gpu::randVal(50, 800);
|
|
48
|
+
int numQuery = opt.numQueriesOverride > 0 ? opt.numQueriesOverride
|
|
49
|
+
: faiss::gpu::randVal(1, 512);
|
|
50
|
+
|
|
51
|
+
// Due to loss of precision in a float16 accumulator, for large k,
|
|
52
|
+
// the number of differences is pretty huge. Restrict ourselves to a
|
|
53
|
+
// fairly small `k` for float16
|
|
54
|
+
int k = opt.useFloat16
|
|
55
|
+
? std::min(faiss::gpu::randVal(1, 50), numVecs)
|
|
56
|
+
: std::min(
|
|
57
|
+
faiss::gpu::randVal(1, faiss::gpu::getMaxKSelection()),
|
|
58
|
+
numVecs);
|
|
59
|
+
if (opt.kOverride > 0) {
|
|
60
|
+
k = opt.kOverride;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
faiss::IndexFlat cpuIndex(dim, opt.metric);
|
|
64
|
+
cpuIndex.metric_arg = opt.metricArg;
|
|
65
|
+
|
|
66
|
+
// Construct on a random device to test multi-device, if we have
|
|
67
|
+
// multiple devices
|
|
68
|
+
int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
|
69
|
+
|
|
70
|
+
faiss::gpu::StandardGpuResources res;
|
|
71
|
+
res.noTempMemory();
|
|
72
|
+
|
|
73
|
+
faiss::gpu::GpuIndexFlatConfig config;
|
|
74
|
+
config.device = device;
|
|
75
|
+
config.useFloat16 = opt.useFloat16;
|
|
76
|
+
config.storeTransposed = opt.useTransposed;
|
|
77
|
+
|
|
78
|
+
faiss::gpu::GpuIndexFlat gpuIndex(&res, dim, opt.metric, config);
|
|
79
|
+
gpuIndex.metric_arg = opt.metricArg;
|
|
80
|
+
|
|
81
|
+
std::vector<float> vecs = faiss::gpu::randVecs(numVecs, dim);
|
|
82
|
+
cpuIndex.add(numVecs, vecs.data());
|
|
83
|
+
gpuIndex.add(numVecs, vecs.data());
|
|
84
|
+
|
|
85
|
+
std::stringstream str;
|
|
86
|
+
str << "metric " << opt.metric << " marg " << opt.metricArg << " numVecs "
|
|
87
|
+
<< numVecs << " dim " << dim << " useFloat16 " << opt.useFloat16
|
|
88
|
+
<< " transposed " << opt.useTransposed << " numQuery " << numQuery
|
|
89
|
+
<< " k " << k;
|
|
90
|
+
|
|
91
|
+
// To some extent, we depend upon the relative error for the test
|
|
92
|
+
// for float16
|
|
93
|
+
faiss::gpu::compareIndices(
|
|
94
|
+
cpuIndex,
|
|
95
|
+
gpuIndex,
|
|
96
|
+
numQuery,
|
|
97
|
+
dim,
|
|
98
|
+
k,
|
|
99
|
+
str.str(),
|
|
100
|
+
opt.useFloat16 ? kF16MaxRelErr : kF32MaxRelErr,
|
|
101
|
+
// FIXME: the fp16 bounds are
|
|
102
|
+
// useless when math (the accumulator) is
|
|
103
|
+
// in fp16. Figure out another way to test
|
|
104
|
+
opt.useFloat16 ? 0.99f : 0.1f,
|
|
105
|
+
opt.useFloat16 ? 0.65f : 0.015f);
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
TEST(TestGpuIndexFlat, IP_Float32) {
|
|
107
|
-
|
|
109
|
+
for (int tries = 0; tries < 3; ++tries) {
|
|
110
|
+
TestFlatOptions opt;
|
|
111
|
+
opt.metric = faiss::MetricType::METRIC_INNER_PRODUCT;
|
|
112
|
+
opt.useFloat16 = false;
|
|
113
|
+
opt.useTransposed = false;
|
|
114
|
+
|
|
115
|
+
testFlat(opt);
|
|
116
|
+
|
|
117
|
+
opt.useTransposed = true;
|
|
118
|
+
testFlat(opt);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
TEST(TestGpuIndexFlat, L1_Float32) {
|
|
108
123
|
TestFlatOptions opt;
|
|
109
|
-
opt.metric = faiss::MetricType::
|
|
124
|
+
opt.metric = faiss::MetricType::METRIC_L1;
|
|
110
125
|
opt.useFloat16 = false;
|
|
111
126
|
opt.useTransposed = false;
|
|
112
127
|
|
|
@@ -114,280 +129,272 @@ TEST(TestGpuIndexFlat, IP_Float32) {
|
|
|
114
129
|
|
|
115
130
|
opt.useTransposed = true;
|
|
116
131
|
testFlat(opt);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
TEST(TestGpuIndexFlat, L1_Float32) {
|
|
121
|
-
TestFlatOptions opt;
|
|
122
|
-
opt.metric = faiss::MetricType::METRIC_L1;
|
|
123
|
-
opt.useFloat16 = false;
|
|
124
|
-
opt.useTransposed = false;
|
|
125
|
-
|
|
126
|
-
testFlat(opt);
|
|
127
|
-
|
|
128
|
-
opt.useTransposed = true;
|
|
129
|
-
testFlat(opt);
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
TEST(TestGpuIndexFlat, Lp_Float32) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
TestFlatOptions opt;
|
|
136
|
+
opt.metric = faiss::MetricType::METRIC_Lp;
|
|
137
|
+
opt.metricArg = 5;
|
|
138
|
+
opt.useFloat16 = false;
|
|
139
|
+
opt.useTransposed = false;
|
|
138
140
|
|
|
139
|
-
|
|
141
|
+
testFlat(opt);
|
|
140
142
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
+
// Don't bother testing the transposed version, the L1 test should be good
|
|
144
|
+
// enough for that
|
|
143
145
|
}
|
|
144
146
|
|
|
145
147
|
TEST(TestGpuIndexFlat, L2_Float32) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
148
|
+
for (int tries = 0; tries < 3; ++tries) {
|
|
149
|
+
TestFlatOptions opt;
|
|
150
|
+
opt.metric = faiss::MetricType::METRIC_L2;
|
|
149
151
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
opt.useFloat16 = false;
|
|
153
|
+
opt.useTransposed = false;
|
|
152
154
|
|
|
153
|
-
|
|
155
|
+
testFlat(opt);
|
|
154
156
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
157
|
+
opt.useTransposed = true;
|
|
158
|
+
testFlat(opt);
|
|
159
|
+
}
|
|
158
160
|
}
|
|
159
161
|
|
|
160
162
|
// test specialized k == 1 codepath
|
|
161
163
|
TEST(TestGpuIndexFlat, L2_Float32_K1) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
164
|
+
for (int tries = 0; tries < 3; ++tries) {
|
|
165
|
+
TestFlatOptions opt;
|
|
166
|
+
opt.metric = faiss::MetricType::METRIC_L2;
|
|
167
|
+
opt.useFloat16 = false;
|
|
168
|
+
opt.useTransposed = false;
|
|
169
|
+
opt.kOverride = 1;
|
|
170
|
+
|
|
171
|
+
testFlat(opt);
|
|
172
|
+
}
|
|
171
173
|
}
|
|
172
174
|
|
|
173
175
|
TEST(TestGpuIndexFlat, IP_Float16) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
176
|
+
for (int tries = 0; tries < 3; ++tries) {
|
|
177
|
+
TestFlatOptions opt;
|
|
178
|
+
opt.metric = faiss::MetricType::METRIC_INNER_PRODUCT;
|
|
179
|
+
opt.useFloat16 = true;
|
|
180
|
+
opt.useTransposed = false;
|
|
179
181
|
|
|
180
|
-
|
|
182
|
+
testFlat(opt);
|
|
181
183
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
184
|
+
opt.useTransposed = true;
|
|
185
|
+
testFlat(opt);
|
|
186
|
+
}
|
|
185
187
|
}
|
|
186
188
|
|
|
187
189
|
TEST(TestGpuIndexFlat, L2_Float16) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
190
|
+
for (int tries = 0; tries < 3; ++tries) {
|
|
191
|
+
TestFlatOptions opt;
|
|
192
|
+
opt.metric = faiss::MetricType::METRIC_L2;
|
|
193
|
+
opt.useFloat16 = true;
|
|
194
|
+
opt.useTransposed = false;
|
|
193
195
|
|
|
194
|
-
|
|
196
|
+
testFlat(opt);
|
|
195
197
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
198
|
+
opt.useTransposed = true;
|
|
199
|
+
testFlat(opt);
|
|
200
|
+
}
|
|
199
201
|
}
|
|
200
202
|
|
|
201
203
|
// test specialized k == 1 codepath
|
|
202
204
|
TEST(TestGpuIndexFlat, L2_Float16_K1) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
205
|
+
for (int tries = 0; tries < 3; ++tries) {
|
|
206
|
+
TestFlatOptions opt;
|
|
207
|
+
opt.metric = faiss::MetricType::METRIC_L2;
|
|
208
|
+
opt.useFloat16 = true;
|
|
209
|
+
opt.useTransposed = false;
|
|
210
|
+
opt.kOverride = 1;
|
|
211
|
+
|
|
212
|
+
testFlat(opt);
|
|
213
|
+
}
|
|
212
214
|
}
|
|
213
215
|
|
|
214
216
|
// test tiling along a huge vector set
|
|
215
217
|
TEST(TestGpuIndexFlat, L2_Tiling) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
218
|
+
for (int tries = 0; tries < 2; ++tries) {
|
|
219
|
+
TestFlatOptions opt;
|
|
220
|
+
opt.metric = faiss::MetricType::METRIC_L2;
|
|
221
|
+
opt.useFloat16 = false;
|
|
222
|
+
opt.useTransposed = false;
|
|
223
|
+
opt.numVecsOverride = 1000000;
|
|
224
|
+
|
|
225
|
+
// keep the rest of the problem reasonably small
|
|
226
|
+
opt.numQueriesOverride = 4;
|
|
227
|
+
opt.dimOverride = 64;
|
|
228
|
+
opt.kOverride = 64;
|
|
229
|
+
|
|
230
|
+
testFlat(opt);
|
|
231
|
+
}
|
|
230
232
|
}
|
|
231
233
|
|
|
232
234
|
TEST(TestGpuIndexFlat, QueryEmpty) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
+
faiss::gpu::StandardGpuResources res;
|
|
236
|
+
res.noTempMemory();
|
|
235
237
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
238
|
+
faiss::gpu::GpuIndexFlatConfig config;
|
|
239
|
+
config.device = 0;
|
|
240
|
+
config.useFloat16 = false;
|
|
241
|
+
config.storeTransposed = false;
|
|
240
242
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
+
int dim = 128;
|
|
244
|
+
faiss::gpu::GpuIndexFlatL2 gpuIndex(&res, dim, config);
|
|
243
245
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
246
|
+
// Querying an empty index should not blow up, and just return
|
|
247
|
+
// (FLT_MAX, -1)
|
|
248
|
+
int numQuery = 10;
|
|
249
|
+
int k = 50;
|
|
250
|
+
std::vector<float> queries(numQuery * dim, 1.0f);
|
|
249
251
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
+
std::vector<float> dist(numQuery * k, 0);
|
|
253
|
+
std::vector<faiss::Index::idx_t> ind(numQuery * k);
|
|
252
254
|
|
|
253
|
-
|
|
255
|
+
gpuIndex.search(numQuery, queries.data(), k, dist.data(), ind.data());
|
|
254
256
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
257
|
+
for (auto d : dist) {
|
|
258
|
+
EXPECT_EQ(d, std::numeric_limits<float>::max());
|
|
259
|
+
}
|
|
258
260
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
261
|
+
for (auto i : ind) {
|
|
262
|
+
EXPECT_EQ(i, -1);
|
|
263
|
+
}
|
|
262
264
|
}
|
|
263
265
|
|
|
264
266
|
TEST(TestGpuIndexFlat, CopyFrom) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
+
int numVecs = faiss::gpu::randVal(100, 200);
|
|
268
|
+
int dim = faiss::gpu::randVal(1, 1000);
|
|
267
269
|
|
|
268
|
-
|
|
270
|
+
faiss::IndexFlatL2 cpuIndex(dim);
|
|
269
271
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
+
std::vector<float> vecs = faiss::gpu::randVecs(numVecs, dim);
|
|
273
|
+
cpuIndex.add(numVecs, vecs.data());
|
|
272
274
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
+
faiss::gpu::StandardGpuResources res;
|
|
276
|
+
res.noTempMemory();
|
|
275
277
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
+
// Fill with garbage values
|
|
279
|
+
int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
|
278
280
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
281
|
+
faiss::gpu::GpuIndexFlatConfig config;
|
|
282
|
+
config.device = device;
|
|
283
|
+
config.useFloat16 = false;
|
|
284
|
+
config.storeTransposed = false;
|
|
283
285
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
+
faiss::gpu::GpuIndexFlatL2 gpuIndex(&res, 2000, config);
|
|
287
|
+
gpuIndex.copyFrom(&cpuIndex);
|
|
286
288
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
+
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
|
290
|
+
EXPECT_EQ(gpuIndex.ntotal, numVecs);
|
|
289
291
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
+
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
|
293
|
+
EXPECT_EQ(cpuIndex.d, dim);
|
|
292
294
|
|
|
293
|
-
|
|
295
|
+
int idx = faiss::gpu::randVal(0, numVecs - 1);
|
|
294
296
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
+
std::vector<float> gpuVals(dim);
|
|
298
|
+
gpuIndex.reconstruct(idx, gpuVals.data());
|
|
297
299
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
+
std::vector<float> cpuVals(dim);
|
|
301
|
+
cpuIndex.reconstruct(idx, cpuVals.data());
|
|
300
302
|
|
|
301
|
-
|
|
303
|
+
EXPECT_EQ(gpuVals, cpuVals);
|
|
302
304
|
}
|
|
303
305
|
|
|
304
306
|
TEST(TestGpuIndexFlat, CopyTo) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
+
faiss::gpu::StandardGpuResources res;
|
|
308
|
+
res.noTempMemory();
|
|
307
309
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
+
int numVecs = faiss::gpu::randVal(100, 200);
|
|
311
|
+
int dim = faiss::gpu::randVal(1, 1000);
|
|
310
312
|
|
|
311
|
-
|
|
313
|
+
int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
|
312
314
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
315
|
+
faiss::gpu::GpuIndexFlatConfig config;
|
|
316
|
+
config.device = device;
|
|
317
|
+
config.useFloat16 = false;
|
|
318
|
+
config.storeTransposed = false;
|
|
317
319
|
|
|
318
|
-
|
|
320
|
+
faiss::gpu::GpuIndexFlatL2 gpuIndex(&res, dim, config);
|
|
319
321
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
+
std::vector<float> vecs = faiss::gpu::randVecs(numVecs, dim);
|
|
323
|
+
gpuIndex.add(numVecs, vecs.data());
|
|
322
324
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
325
|
+
// Fill with garbage values
|
|
326
|
+
faiss::IndexFlatL2 cpuIndex(2000);
|
|
327
|
+
gpuIndex.copyTo(&cpuIndex);
|
|
326
328
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
+
EXPECT_EQ(cpuIndex.ntotal, gpuIndex.ntotal);
|
|
330
|
+
EXPECT_EQ(gpuIndex.ntotal, numVecs);
|
|
329
331
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
+
EXPECT_EQ(cpuIndex.d, gpuIndex.d);
|
|
333
|
+
EXPECT_EQ(cpuIndex.d, dim);
|
|
332
334
|
|
|
333
|
-
|
|
335
|
+
int idx = faiss::gpu::randVal(0, numVecs - 1);
|
|
334
336
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
+
std::vector<float> gpuVals(dim);
|
|
338
|
+
gpuIndex.reconstruct(idx, gpuVals.data());
|
|
337
339
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
+
std::vector<float> cpuVals(dim);
|
|
341
|
+
cpuIndex.reconstruct(idx, cpuVals.data());
|
|
340
342
|
|
|
341
|
-
|
|
343
|
+
EXPECT_EQ(gpuVals, cpuVals);
|
|
342
344
|
}
|
|
343
345
|
|
|
344
346
|
TEST(TestGpuIndexFlat, UnifiedMemory) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
347
|
+
// Construct on a random device to test multi-device, if we have
|
|
348
|
+
// multiple devices
|
|
349
|
+
int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
|
|
350
|
+
|
|
351
|
+
if (!faiss::gpu::getFullUnifiedMemSupport(device)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
int dim = 256;
|
|
356
|
+
|
|
357
|
+
// FIXME: GpuIndexFlat doesn't support > 2^31 (vecs * dims) due to
|
|
358
|
+
// kernel indexing, so we can't test unified memory for memory
|
|
359
|
+
// oversubscription.
|
|
360
|
+
size_t numVecs = 50000;
|
|
361
|
+
int numQuery = 10;
|
|
362
|
+
int k = 10;
|
|
363
|
+
|
|
364
|
+
faiss::IndexFlatL2 cpuIndexL2(dim);
|
|
365
|
+
|
|
366
|
+
faiss::gpu::StandardGpuResources res;
|
|
367
|
+
res.noTempMemory();
|
|
368
|
+
|
|
369
|
+
faiss::gpu::GpuIndexFlatConfig config;
|
|
370
|
+
config.device = device;
|
|
371
|
+
config.memorySpace = faiss::gpu::MemorySpace::Unified;
|
|
372
|
+
|
|
373
|
+
faiss::gpu::GpuIndexFlatL2 gpuIndexL2(&res, dim, config);
|
|
374
|
+
|
|
375
|
+
std::vector<float> vecs = faiss::gpu::randVecs(numVecs, dim);
|
|
376
|
+
cpuIndexL2.add(numVecs, vecs.data());
|
|
377
|
+
gpuIndexL2.add(numVecs, vecs.data());
|
|
378
|
+
|
|
379
|
+
// To some extent, we depend upon the relative error for the test
|
|
380
|
+
// for float16
|
|
381
|
+
faiss::gpu::compareIndices(
|
|
382
|
+
cpuIndexL2,
|
|
383
|
+
gpuIndexL2,
|
|
384
|
+
numQuery,
|
|
385
|
+
dim,
|
|
386
|
+
k,
|
|
387
|
+
"Unified Memory",
|
|
388
|
+
kF32MaxRelErr,
|
|
389
|
+
0.1f,
|
|
390
|
+
0.015f);
|
|
384
391
|
}
|
|
385
392
|
|
|
386
393
|
int main(int argc, char** argv) {
|
|
387
|
-
|
|
394
|
+
testing::InitGoogleTest(&argc, argv);
|
|
388
395
|
|
|
389
|
-
|
|
390
|
-
|
|
396
|
+
// just run with a fixed test seed
|
|
397
|
+
faiss::gpu::setTestSeed(100);
|
|
391
398
|
|
|
392
|
-
|
|
399
|
+
return RUN_ALL_TESTS();
|
|
393
400
|
}
|