faiss 0.1.0 → 0.1.1
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 +5 -0
- data/README.md +103 -3
- data/ext/faiss/ext.cpp +99 -32
- data/ext/faiss/extconf.rb +12 -2
- data/lib/faiss/ext.bundle +0 -0
- data/lib/faiss/index.rb +3 -3
- data/lib/faiss/index_binary.rb +3 -3
- data/lib/faiss/kmeans.rb +1 -1
- data/lib/faiss/pca_matrix.rb +2 -2
- data/lib/faiss/product_quantizer.rb +3 -3
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/AutoTune.cpp +719 -0
- data/vendor/faiss/AutoTune.h +212 -0
- data/vendor/faiss/Clustering.cpp +261 -0
- data/vendor/faiss/Clustering.h +101 -0
- data/vendor/faiss/IVFlib.cpp +339 -0
- data/vendor/faiss/IVFlib.h +132 -0
- data/vendor/faiss/Index.cpp +171 -0
- data/vendor/faiss/Index.h +261 -0
- data/vendor/faiss/Index2Layer.cpp +437 -0
- data/vendor/faiss/Index2Layer.h +85 -0
- data/vendor/faiss/IndexBinary.cpp +77 -0
- data/vendor/faiss/IndexBinary.h +163 -0
- data/vendor/faiss/IndexBinaryFlat.cpp +83 -0
- data/vendor/faiss/IndexBinaryFlat.h +54 -0
- data/vendor/faiss/IndexBinaryFromFloat.cpp +78 -0
- data/vendor/faiss/IndexBinaryFromFloat.h +52 -0
- data/vendor/faiss/IndexBinaryHNSW.cpp +325 -0
- data/vendor/faiss/IndexBinaryHNSW.h +56 -0
- data/vendor/faiss/IndexBinaryIVF.cpp +671 -0
- data/vendor/faiss/IndexBinaryIVF.h +211 -0
- data/vendor/faiss/IndexFlat.cpp +508 -0
- data/vendor/faiss/IndexFlat.h +175 -0
- data/vendor/faiss/IndexHNSW.cpp +1090 -0
- data/vendor/faiss/IndexHNSW.h +170 -0
- data/vendor/faiss/IndexIVF.cpp +909 -0
- data/vendor/faiss/IndexIVF.h +353 -0
- data/vendor/faiss/IndexIVFFlat.cpp +502 -0
- data/vendor/faiss/IndexIVFFlat.h +118 -0
- data/vendor/faiss/IndexIVFPQ.cpp +1207 -0
- data/vendor/faiss/IndexIVFPQ.h +161 -0
- data/vendor/faiss/IndexIVFPQR.cpp +219 -0
- data/vendor/faiss/IndexIVFPQR.h +65 -0
- data/vendor/faiss/IndexIVFSpectralHash.cpp +331 -0
- data/vendor/faiss/IndexIVFSpectralHash.h +75 -0
- data/vendor/faiss/IndexLSH.cpp +225 -0
- data/vendor/faiss/IndexLSH.h +87 -0
- data/vendor/faiss/IndexLattice.cpp +143 -0
- data/vendor/faiss/IndexLattice.h +68 -0
- data/vendor/faiss/IndexPQ.cpp +1188 -0
- data/vendor/faiss/IndexPQ.h +199 -0
- data/vendor/faiss/IndexPreTransform.cpp +288 -0
- data/vendor/faiss/IndexPreTransform.h +91 -0
- data/vendor/faiss/IndexReplicas.cpp +123 -0
- data/vendor/faiss/IndexReplicas.h +76 -0
- data/vendor/faiss/IndexScalarQuantizer.cpp +317 -0
- data/vendor/faiss/IndexScalarQuantizer.h +127 -0
- data/vendor/faiss/IndexShards.cpp +317 -0
- data/vendor/faiss/IndexShards.h +100 -0
- data/vendor/faiss/InvertedLists.cpp +623 -0
- data/vendor/faiss/InvertedLists.h +334 -0
- data/vendor/faiss/LICENSE +21 -0
- data/vendor/faiss/MatrixStats.cpp +252 -0
- data/vendor/faiss/MatrixStats.h +62 -0
- data/vendor/faiss/MetaIndexes.cpp +351 -0
- data/vendor/faiss/MetaIndexes.h +126 -0
- data/vendor/faiss/OnDiskInvertedLists.cpp +674 -0
- data/vendor/faiss/OnDiskInvertedLists.h +127 -0
- data/vendor/faiss/VectorTransform.cpp +1157 -0
- data/vendor/faiss/VectorTransform.h +322 -0
- data/vendor/faiss/c_api/AutoTune_c.cpp +83 -0
- data/vendor/faiss/c_api/AutoTune_c.h +64 -0
- data/vendor/faiss/c_api/Clustering_c.cpp +139 -0
- data/vendor/faiss/c_api/Clustering_c.h +117 -0
- data/vendor/faiss/c_api/IndexFlat_c.cpp +140 -0
- data/vendor/faiss/c_api/IndexFlat_c.h +115 -0
- data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +64 -0
- data/vendor/faiss/c_api/IndexIVFFlat_c.h +58 -0
- data/vendor/faiss/c_api/IndexIVF_c.cpp +92 -0
- data/vendor/faiss/c_api/IndexIVF_c.h +135 -0
- data/vendor/faiss/c_api/IndexLSH_c.cpp +37 -0
- data/vendor/faiss/c_api/IndexLSH_c.h +40 -0
- data/vendor/faiss/c_api/IndexShards_c.cpp +44 -0
- data/vendor/faiss/c_api/IndexShards_c.h +42 -0
- data/vendor/faiss/c_api/Index_c.cpp +105 -0
- data/vendor/faiss/c_api/Index_c.h +183 -0
- data/vendor/faiss/c_api/MetaIndexes_c.cpp +49 -0
- data/vendor/faiss/c_api/MetaIndexes_c.h +49 -0
- data/vendor/faiss/c_api/clone_index_c.cpp +23 -0
- data/vendor/faiss/c_api/clone_index_c.h +32 -0
- data/vendor/faiss/c_api/error_c.h +42 -0
- data/vendor/faiss/c_api/error_impl.cpp +27 -0
- data/vendor/faiss/c_api/error_impl.h +16 -0
- data/vendor/faiss/c_api/faiss_c.h +58 -0
- data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +96 -0
- data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +56 -0
- data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +52 -0
- data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +68 -0
- data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +17 -0
- data/vendor/faiss/c_api/gpu/GpuIndex_c.h +30 -0
- data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +38 -0
- data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +86 -0
- data/vendor/faiss/c_api/gpu/GpuResources_c.h +66 -0
- data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +54 -0
- data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +53 -0
- data/vendor/faiss/c_api/gpu/macros_impl.h +42 -0
- data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +220 -0
- data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +149 -0
- data/vendor/faiss/c_api/index_factory_c.cpp +26 -0
- data/vendor/faiss/c_api/index_factory_c.h +30 -0
- data/vendor/faiss/c_api/index_io_c.cpp +42 -0
- data/vendor/faiss/c_api/index_io_c.h +50 -0
- data/vendor/faiss/c_api/macros_impl.h +110 -0
- data/vendor/faiss/clone_index.cpp +147 -0
- data/vendor/faiss/clone_index.h +38 -0
- data/vendor/faiss/demos/demo_imi_flat.cpp +151 -0
- data/vendor/faiss/demos/demo_imi_pq.cpp +199 -0
- data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +146 -0
- data/vendor/faiss/demos/demo_sift1M.cpp +252 -0
- data/vendor/faiss/gpu/GpuAutoTune.cpp +95 -0
- data/vendor/faiss/gpu/GpuAutoTune.h +27 -0
- data/vendor/faiss/gpu/GpuCloner.cpp +403 -0
- data/vendor/faiss/gpu/GpuCloner.h +82 -0
- data/vendor/faiss/gpu/GpuClonerOptions.cpp +28 -0
- data/vendor/faiss/gpu/GpuClonerOptions.h +53 -0
- data/vendor/faiss/gpu/GpuDistance.h +52 -0
- data/vendor/faiss/gpu/GpuFaissAssert.h +29 -0
- data/vendor/faiss/gpu/GpuIndex.h +148 -0
- data/vendor/faiss/gpu/GpuIndexBinaryFlat.h +89 -0
- data/vendor/faiss/gpu/GpuIndexFlat.h +190 -0
- data/vendor/faiss/gpu/GpuIndexIVF.h +89 -0
- data/vendor/faiss/gpu/GpuIndexIVFFlat.h +85 -0
- data/vendor/faiss/gpu/GpuIndexIVFPQ.h +143 -0
- data/vendor/faiss/gpu/GpuIndexIVFScalarQuantizer.h +100 -0
- data/vendor/faiss/gpu/GpuIndicesOptions.h +30 -0
- data/vendor/faiss/gpu/GpuResources.cpp +52 -0
- data/vendor/faiss/gpu/GpuResources.h +73 -0
- data/vendor/faiss/gpu/StandardGpuResources.cpp +295 -0
- data/vendor/faiss/gpu/StandardGpuResources.h +114 -0
- data/vendor/faiss/gpu/impl/RemapIndices.cpp +43 -0
- data/vendor/faiss/gpu/impl/RemapIndices.h +24 -0
- data/vendor/faiss/gpu/perf/IndexWrapper-inl.h +71 -0
- data/vendor/faiss/gpu/perf/IndexWrapper.h +39 -0
- data/vendor/faiss/gpu/perf/PerfClustering.cpp +115 -0
- data/vendor/faiss/gpu/perf/PerfIVFPQAdd.cpp +139 -0
- data/vendor/faiss/gpu/perf/WriteIndex.cpp +102 -0
- data/vendor/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +130 -0
- data/vendor/faiss/gpu/test/TestGpuIndexFlat.cpp +371 -0
- data/vendor/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +550 -0
- data/vendor/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +450 -0
- data/vendor/faiss/gpu/test/TestGpuMemoryException.cpp +84 -0
- data/vendor/faiss/gpu/test/TestUtils.cpp +315 -0
- data/vendor/faiss/gpu/test/TestUtils.h +93 -0
- data/vendor/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +159 -0
- data/vendor/faiss/gpu/utils/DeviceMemory.cpp +77 -0
- data/vendor/faiss/gpu/utils/DeviceMemory.h +71 -0
- data/vendor/faiss/gpu/utils/DeviceUtils.h +185 -0
- data/vendor/faiss/gpu/utils/MemorySpace.cpp +89 -0
- data/vendor/faiss/gpu/utils/MemorySpace.h +44 -0
- data/vendor/faiss/gpu/utils/StackDeviceMemory.cpp +239 -0
- data/vendor/faiss/gpu/utils/StackDeviceMemory.h +129 -0
- data/vendor/faiss/gpu/utils/StaticUtils.h +83 -0
- data/vendor/faiss/gpu/utils/Timer.cpp +60 -0
- data/vendor/faiss/gpu/utils/Timer.h +52 -0
- data/vendor/faiss/impl/AuxIndexStructures.cpp +305 -0
- data/vendor/faiss/impl/AuxIndexStructures.h +246 -0
- data/vendor/faiss/impl/FaissAssert.h +95 -0
- data/vendor/faiss/impl/FaissException.cpp +66 -0
- data/vendor/faiss/impl/FaissException.h +71 -0
- data/vendor/faiss/impl/HNSW.cpp +818 -0
- data/vendor/faiss/impl/HNSW.h +275 -0
- data/vendor/faiss/impl/PolysemousTraining.cpp +953 -0
- data/vendor/faiss/impl/PolysemousTraining.h +158 -0
- data/vendor/faiss/impl/ProductQuantizer.cpp +876 -0
- data/vendor/faiss/impl/ProductQuantizer.h +242 -0
- data/vendor/faiss/impl/ScalarQuantizer.cpp +1628 -0
- data/vendor/faiss/impl/ScalarQuantizer.h +120 -0
- data/vendor/faiss/impl/ThreadedIndex-inl.h +192 -0
- data/vendor/faiss/impl/ThreadedIndex.h +80 -0
- data/vendor/faiss/impl/index_read.cpp +793 -0
- data/vendor/faiss/impl/index_write.cpp +558 -0
- data/vendor/faiss/impl/io.cpp +142 -0
- data/vendor/faiss/impl/io.h +98 -0
- data/vendor/faiss/impl/lattice_Zn.cpp +712 -0
- data/vendor/faiss/impl/lattice_Zn.h +199 -0
- data/vendor/faiss/index_factory.cpp +392 -0
- data/vendor/faiss/index_factory.h +25 -0
- data/vendor/faiss/index_io.h +75 -0
- data/vendor/faiss/misc/test_blas.cpp +84 -0
- data/vendor/faiss/tests/test_binary_flat.cpp +64 -0
- data/vendor/faiss/tests/test_dealloc_invlists.cpp +183 -0
- data/vendor/faiss/tests/test_ivfpq_codec.cpp +67 -0
- data/vendor/faiss/tests/test_ivfpq_indexing.cpp +98 -0
- data/vendor/faiss/tests/test_lowlevel_ivf.cpp +566 -0
- data/vendor/faiss/tests/test_merge.cpp +258 -0
- data/vendor/faiss/tests/test_omp_threads.cpp +14 -0
- data/vendor/faiss/tests/test_ondisk_ivf.cpp +220 -0
- data/vendor/faiss/tests/test_pairs_decoding.cpp +189 -0
- data/vendor/faiss/tests/test_params_override.cpp +231 -0
- data/vendor/faiss/tests/test_pq_encoding.cpp +98 -0
- data/vendor/faiss/tests/test_sliding_ivf.cpp +240 -0
- data/vendor/faiss/tests/test_threaded_index.cpp +253 -0
- data/vendor/faiss/tests/test_transfer_invlists.cpp +159 -0
- data/vendor/faiss/tutorial/cpp/1-Flat.cpp +98 -0
- data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +81 -0
- data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +93 -0
- data/vendor/faiss/tutorial/cpp/4-GPU.cpp +119 -0
- data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +99 -0
- data/vendor/faiss/utils/Heap.cpp +122 -0
- data/vendor/faiss/utils/Heap.h +495 -0
- data/vendor/faiss/utils/WorkerThread.cpp +126 -0
- data/vendor/faiss/utils/WorkerThread.h +61 -0
- data/vendor/faiss/utils/distances.cpp +765 -0
- data/vendor/faiss/utils/distances.h +243 -0
- data/vendor/faiss/utils/distances_simd.cpp +809 -0
- data/vendor/faiss/utils/extra_distances.cpp +336 -0
- data/vendor/faiss/utils/extra_distances.h +54 -0
- data/vendor/faiss/utils/hamming-inl.h +472 -0
- data/vendor/faiss/utils/hamming.cpp +792 -0
- data/vendor/faiss/utils/hamming.h +220 -0
- data/vendor/faiss/utils/random.cpp +192 -0
- data/vendor/faiss/utils/random.h +60 -0
- data/vendor/faiss/utils/utils.cpp +783 -0
- data/vendor/faiss/utils/utils.h +181 -0
- metadata +216 -2
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#include <faiss/gpu/GpuIndex.h>
|
|
12
|
+
|
|
13
|
+
namespace faiss {
|
|
14
|
+
|
|
15
|
+
struct IndexFlat;
|
|
16
|
+
struct IndexFlatL2;
|
|
17
|
+
struct IndexFlatIP;
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
namespace faiss { namespace gpu {
|
|
22
|
+
|
|
23
|
+
struct FlatIndex;
|
|
24
|
+
|
|
25
|
+
struct GpuIndexFlatConfig : public GpuIndexConfig {
|
|
26
|
+
inline GpuIndexFlatConfig()
|
|
27
|
+
: useFloat16(false),
|
|
28
|
+
useFloat16Accumulator(false),
|
|
29
|
+
storeTransposed(false) {
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// Whether or not data is stored as float16
|
|
33
|
+
bool useFloat16;
|
|
34
|
+
|
|
35
|
+
/// Whether or not all math is performed in float16, if useFloat16 is
|
|
36
|
+
/// specified. If true, we use cublasHgemm, supported only on CC
|
|
37
|
+
/// 5.3+. Otherwise, we use cublasSgemmEx.
|
|
38
|
+
bool useFloat16Accumulator;
|
|
39
|
+
|
|
40
|
+
/// Whether or not data is stored (transparently) in a transposed
|
|
41
|
+
/// layout, enabling use of the NN GEMM call, which is ~10% faster.
|
|
42
|
+
/// This will improve the speed of the flat index, but will
|
|
43
|
+
/// substantially slow down any add() calls made, as all data must
|
|
44
|
+
/// be transposed, and will increase storage requirements (we store
|
|
45
|
+
/// data in both transposed and non-transposed layouts).
|
|
46
|
+
bool storeTransposed;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/// Wrapper around the GPU implementation that looks like
|
|
50
|
+
/// faiss::IndexFlat; copies over centroid data from a given
|
|
51
|
+
/// faiss::IndexFlat
|
|
52
|
+
class GpuIndexFlat : public GpuIndex {
|
|
53
|
+
public:
|
|
54
|
+
/// Construct from a pre-existing faiss::IndexFlat instance, copying
|
|
55
|
+
/// data over to the given GPU
|
|
56
|
+
GpuIndexFlat(GpuResources* resources,
|
|
57
|
+
const faiss::IndexFlat* index,
|
|
58
|
+
GpuIndexFlatConfig config = GpuIndexFlatConfig());
|
|
59
|
+
|
|
60
|
+
/// Construct an empty instance that can be added to
|
|
61
|
+
GpuIndexFlat(GpuResources* resources,
|
|
62
|
+
int dims,
|
|
63
|
+
faiss::MetricType metric,
|
|
64
|
+
GpuIndexFlatConfig config = GpuIndexFlatConfig());
|
|
65
|
+
|
|
66
|
+
~GpuIndexFlat() override;
|
|
67
|
+
|
|
68
|
+
/// Initialize ourselves from the given CPU index; will overwrite
|
|
69
|
+
/// all data in ourselves
|
|
70
|
+
void copyFrom(const faiss::IndexFlat* index);
|
|
71
|
+
|
|
72
|
+
/// Copy ourselves to the given CPU index; will overwrite all data
|
|
73
|
+
/// in the index instance
|
|
74
|
+
void copyTo(faiss::IndexFlat* index) const;
|
|
75
|
+
|
|
76
|
+
/// Returns the number of vectors we contain
|
|
77
|
+
size_t getNumVecs() const;
|
|
78
|
+
|
|
79
|
+
/// Clears all vectors from this index
|
|
80
|
+
void reset() override;
|
|
81
|
+
|
|
82
|
+
/// This index is not trained, so this does nothing
|
|
83
|
+
void train(Index::idx_t n, const float* x) override;
|
|
84
|
+
|
|
85
|
+
/// Overrides to avoid excessive copies
|
|
86
|
+
void add(faiss::Index::idx_t, const float* x) override;
|
|
87
|
+
|
|
88
|
+
/// Reconstruction methods; prefer the batch reconstruct as it will
|
|
89
|
+
/// be more efficient
|
|
90
|
+
void reconstruct(faiss::Index::idx_t key, float* out) const override;
|
|
91
|
+
|
|
92
|
+
/// Batch reconstruction method
|
|
93
|
+
void reconstruct_n(faiss::Index::idx_t i0,
|
|
94
|
+
faiss::Index::idx_t num,
|
|
95
|
+
float* out) const override;
|
|
96
|
+
|
|
97
|
+
/// Compute residual
|
|
98
|
+
void compute_residual(const float* x,
|
|
99
|
+
float* residual,
|
|
100
|
+
faiss::Index::idx_t key) const override;
|
|
101
|
+
|
|
102
|
+
/// Compute residual (batch mode)
|
|
103
|
+
void compute_residual_n(faiss::Index::idx_t n,
|
|
104
|
+
const float* xs,
|
|
105
|
+
float* residuals,
|
|
106
|
+
const faiss::Index::idx_t* keys) const override;
|
|
107
|
+
|
|
108
|
+
/// For internal access
|
|
109
|
+
inline FlatIndex* getGpuData() { return data_; }
|
|
110
|
+
|
|
111
|
+
protected:
|
|
112
|
+
/// Flat index does not require IDs as there is no storage available for them
|
|
113
|
+
bool addImplRequiresIDs_() const override;
|
|
114
|
+
|
|
115
|
+
/// Called from GpuIndex for add
|
|
116
|
+
void addImpl_(int n,
|
|
117
|
+
const float* x,
|
|
118
|
+
const Index::idx_t* ids) override;
|
|
119
|
+
|
|
120
|
+
/// Called from GpuIndex for search
|
|
121
|
+
void searchImpl_(int n,
|
|
122
|
+
const float* x,
|
|
123
|
+
int k,
|
|
124
|
+
float* distances,
|
|
125
|
+
faiss::Index::idx_t* labels) const override;
|
|
126
|
+
|
|
127
|
+
private:
|
|
128
|
+
/// Checks user settings for consistency
|
|
129
|
+
void verifySettings_() const;
|
|
130
|
+
|
|
131
|
+
protected:
|
|
132
|
+
/// Our config object
|
|
133
|
+
const GpuIndexFlatConfig config_;
|
|
134
|
+
|
|
135
|
+
/// Holds our GPU data containing the list of vectors; is managed via raw
|
|
136
|
+
/// pointer so as to allow non-CUDA compilers to see this header
|
|
137
|
+
FlatIndex* data_;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/// Wrapper around the GPU implementation that looks like
|
|
141
|
+
/// faiss::IndexFlatL2; copies over centroid data from a given
|
|
142
|
+
/// faiss::IndexFlat
|
|
143
|
+
class GpuIndexFlatL2 : public GpuIndexFlat {
|
|
144
|
+
public:
|
|
145
|
+
/// Construct from a pre-existing faiss::IndexFlatL2 instance, copying
|
|
146
|
+
/// data over to the given GPU
|
|
147
|
+
GpuIndexFlatL2(GpuResources* resources,
|
|
148
|
+
faiss::IndexFlatL2* index,
|
|
149
|
+
GpuIndexFlatConfig config = GpuIndexFlatConfig());
|
|
150
|
+
|
|
151
|
+
/// Construct an empty instance that can be added to
|
|
152
|
+
GpuIndexFlatL2(GpuResources* resources,
|
|
153
|
+
int dims,
|
|
154
|
+
GpuIndexFlatConfig config = GpuIndexFlatConfig());
|
|
155
|
+
|
|
156
|
+
/// Initialize ourselves from the given CPU index; will overwrite
|
|
157
|
+
/// all data in ourselves
|
|
158
|
+
void copyFrom(faiss::IndexFlat* index);
|
|
159
|
+
|
|
160
|
+
/// Copy ourselves to the given CPU index; will overwrite all data
|
|
161
|
+
/// in the index instance
|
|
162
|
+
void copyTo(faiss::IndexFlat* index);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/// Wrapper around the GPU implementation that looks like
|
|
166
|
+
/// faiss::IndexFlatIP; copies over centroid data from a given
|
|
167
|
+
/// faiss::IndexFlat
|
|
168
|
+
class GpuIndexFlatIP : public GpuIndexFlat {
|
|
169
|
+
public:
|
|
170
|
+
/// Construct from a pre-existing faiss::IndexFlatIP instance, copying
|
|
171
|
+
/// data over to the given GPU
|
|
172
|
+
GpuIndexFlatIP(GpuResources* resources,
|
|
173
|
+
faiss::IndexFlatIP* index,
|
|
174
|
+
GpuIndexFlatConfig config = GpuIndexFlatConfig());
|
|
175
|
+
|
|
176
|
+
/// Construct an empty instance that can be added to
|
|
177
|
+
GpuIndexFlatIP(GpuResources* resources,
|
|
178
|
+
int dims,
|
|
179
|
+
GpuIndexFlatConfig config = GpuIndexFlatConfig());
|
|
180
|
+
|
|
181
|
+
/// Initialize ourselves from the given CPU index; will overwrite
|
|
182
|
+
/// all data in ourselves
|
|
183
|
+
void copyFrom(faiss::IndexFlat* index);
|
|
184
|
+
|
|
185
|
+
/// Copy ourselves to the given CPU index; will overwrite all data
|
|
186
|
+
/// in the index instance
|
|
187
|
+
void copyTo(faiss::IndexFlat* index);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
} } // namespace
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#include <faiss/gpu/GpuIndex.h>
|
|
12
|
+
#include <faiss/gpu/GpuIndexFlat.h>
|
|
13
|
+
#include <faiss/gpu/GpuIndicesOptions.h>
|
|
14
|
+
#include <faiss/Clustering.h>
|
|
15
|
+
|
|
16
|
+
namespace faiss { struct IndexIVF; }
|
|
17
|
+
|
|
18
|
+
namespace faiss { namespace gpu {
|
|
19
|
+
|
|
20
|
+
class GpuIndexFlat;
|
|
21
|
+
class GpuResources;
|
|
22
|
+
|
|
23
|
+
struct GpuIndexIVFConfig : public GpuIndexConfig {
|
|
24
|
+
inline GpuIndexIVFConfig()
|
|
25
|
+
: indicesOptions(INDICES_64_BIT) {
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Index storage options for the GPU
|
|
29
|
+
IndicesOptions indicesOptions;
|
|
30
|
+
|
|
31
|
+
/// Configuration for the coarse quantizer object
|
|
32
|
+
GpuIndexFlatConfig flatConfig;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
class GpuIndexIVF : public GpuIndex {
|
|
36
|
+
public:
|
|
37
|
+
GpuIndexIVF(GpuResources* resources,
|
|
38
|
+
int dims,
|
|
39
|
+
faiss::MetricType metric,
|
|
40
|
+
int nlist,
|
|
41
|
+
GpuIndexIVFConfig config = GpuIndexIVFConfig());
|
|
42
|
+
|
|
43
|
+
~GpuIndexIVF() override;
|
|
44
|
+
|
|
45
|
+
private:
|
|
46
|
+
/// Shared initialization functions
|
|
47
|
+
void init_();
|
|
48
|
+
|
|
49
|
+
public:
|
|
50
|
+
/// Copy what we need from the CPU equivalent
|
|
51
|
+
void copyFrom(const faiss::IndexIVF* index);
|
|
52
|
+
|
|
53
|
+
/// Copy what we have to the CPU equivalent
|
|
54
|
+
void copyTo(faiss::IndexIVF* index) const;
|
|
55
|
+
|
|
56
|
+
/// Returns the number of inverted lists we're managing
|
|
57
|
+
int getNumLists() const;
|
|
58
|
+
|
|
59
|
+
/// Return the quantizer we're using
|
|
60
|
+
GpuIndexFlat* getQuantizer();
|
|
61
|
+
|
|
62
|
+
/// Sets the number of list probes per query
|
|
63
|
+
void setNumProbes(int nprobe);
|
|
64
|
+
|
|
65
|
+
/// Returns our current number of list probes per query
|
|
66
|
+
int getNumProbes() const;
|
|
67
|
+
|
|
68
|
+
protected:
|
|
69
|
+
bool addImplRequiresIDs_() const override;
|
|
70
|
+
void trainQuantizer_(faiss::Index::idx_t n, const float* x);
|
|
71
|
+
|
|
72
|
+
public:
|
|
73
|
+
/// Exposing this like the CPU version for manipulation
|
|
74
|
+
ClusteringParameters cp;
|
|
75
|
+
|
|
76
|
+
/// Exposing this like the CPU version for query
|
|
77
|
+
int nlist;
|
|
78
|
+
|
|
79
|
+
/// Exposing this like the CPU version for manipulation
|
|
80
|
+
int nprobe;
|
|
81
|
+
|
|
82
|
+
/// Exposeing this like the CPU version for query
|
|
83
|
+
GpuIndexFlat* quantizer;
|
|
84
|
+
|
|
85
|
+
protected:
|
|
86
|
+
GpuIndexIVFConfig ivfConfig_;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
} } // namespace
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#include <faiss/gpu/GpuIndexIVF.h>
|
|
12
|
+
|
|
13
|
+
namespace faiss { struct IndexIVFFlat; }
|
|
14
|
+
|
|
15
|
+
namespace faiss { namespace gpu {
|
|
16
|
+
|
|
17
|
+
class IVFFlat;
|
|
18
|
+
class GpuIndexFlat;
|
|
19
|
+
|
|
20
|
+
struct GpuIndexIVFFlatConfig : public GpuIndexIVFConfig {
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/// Wrapper around the GPU implementation that looks like
|
|
24
|
+
/// faiss::IndexIVFFlat
|
|
25
|
+
class GpuIndexIVFFlat : public GpuIndexIVF {
|
|
26
|
+
public:
|
|
27
|
+
/// Construct from a pre-existing faiss::IndexIVFFlat instance, copying
|
|
28
|
+
/// data over to the given GPU, if the input index is trained.
|
|
29
|
+
GpuIndexIVFFlat(GpuResources* resources,
|
|
30
|
+
const faiss::IndexIVFFlat* index,
|
|
31
|
+
GpuIndexIVFFlatConfig config = GpuIndexIVFFlatConfig());
|
|
32
|
+
|
|
33
|
+
/// Constructs a new instance with an empty flat quantizer; the user
|
|
34
|
+
/// provides the number of lists desired.
|
|
35
|
+
GpuIndexIVFFlat(GpuResources* resources,
|
|
36
|
+
int dims,
|
|
37
|
+
int nlist,
|
|
38
|
+
faiss::MetricType metric,
|
|
39
|
+
GpuIndexIVFFlatConfig config = GpuIndexIVFFlatConfig());
|
|
40
|
+
|
|
41
|
+
~GpuIndexIVFFlat() override;
|
|
42
|
+
|
|
43
|
+
/// Reserve GPU memory in our inverted lists for this number of vectors
|
|
44
|
+
void reserveMemory(size_t numVecs);
|
|
45
|
+
|
|
46
|
+
/// Initialize ourselves from the given CPU index; will overwrite
|
|
47
|
+
/// all data in ourselves
|
|
48
|
+
void copyFrom(const faiss::IndexIVFFlat* index);
|
|
49
|
+
|
|
50
|
+
/// Copy ourselves to the given CPU index; will overwrite all data
|
|
51
|
+
/// in the index instance
|
|
52
|
+
void copyTo(faiss::IndexIVFFlat* index) const;
|
|
53
|
+
|
|
54
|
+
/// After adding vectors, one can call this to reclaim device memory
|
|
55
|
+
/// to exactly the amount needed. Returns space reclaimed in bytes
|
|
56
|
+
size_t reclaimMemory();
|
|
57
|
+
|
|
58
|
+
void reset() override;
|
|
59
|
+
|
|
60
|
+
void train(Index::idx_t n, const float* x) override;
|
|
61
|
+
|
|
62
|
+
protected:
|
|
63
|
+
/// Called from GpuIndex for add/add_with_ids
|
|
64
|
+
void addImpl_(int n,
|
|
65
|
+
const float* x,
|
|
66
|
+
const Index::idx_t* ids) override;
|
|
67
|
+
|
|
68
|
+
/// Called from GpuIndex for search
|
|
69
|
+
void searchImpl_(int n,
|
|
70
|
+
const float* x,
|
|
71
|
+
int k,
|
|
72
|
+
float* distances,
|
|
73
|
+
Index::idx_t* labels) const override;
|
|
74
|
+
|
|
75
|
+
private:
|
|
76
|
+
GpuIndexIVFFlatConfig ivfFlatConfig_;
|
|
77
|
+
|
|
78
|
+
/// Desired inverted list memory reservation
|
|
79
|
+
size_t reserveMemoryVecs_;
|
|
80
|
+
|
|
81
|
+
/// Instance that we own; contains the inverted list
|
|
82
|
+
IVFFlat* index_;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
} } // namespace
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#include <faiss/gpu/GpuIndexIVF.h>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
namespace faiss { struct IndexIVFPQ; }
|
|
15
|
+
|
|
16
|
+
namespace faiss { namespace gpu {
|
|
17
|
+
|
|
18
|
+
class GpuIndexFlat;
|
|
19
|
+
class IVFPQ;
|
|
20
|
+
|
|
21
|
+
struct GpuIndexIVFPQConfig : public GpuIndexIVFConfig {
|
|
22
|
+
inline GpuIndexIVFPQConfig()
|
|
23
|
+
: useFloat16LookupTables(false),
|
|
24
|
+
usePrecomputedTables(false) {
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// Whether or not float16 residual distance tables are used in the
|
|
28
|
+
/// list scanning kernels. When subQuantizers * 2^bitsPerCode >
|
|
29
|
+
/// 16384, this is required.
|
|
30
|
+
bool useFloat16LookupTables;
|
|
31
|
+
|
|
32
|
+
/// Whether or not we enable the precomputed table option for
|
|
33
|
+
/// search, which can substantially increase the memory requirement.
|
|
34
|
+
bool usePrecomputedTables;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/// IVFPQ index for the GPU
|
|
38
|
+
class GpuIndexIVFPQ : public GpuIndexIVF {
|
|
39
|
+
public:
|
|
40
|
+
/// Construct from a pre-existing faiss::IndexIVFPQ instance, copying
|
|
41
|
+
/// data over to the given GPU, if the input index is trained.
|
|
42
|
+
GpuIndexIVFPQ(GpuResources* resources,
|
|
43
|
+
const faiss::IndexIVFPQ* index,
|
|
44
|
+
GpuIndexIVFPQConfig config = GpuIndexIVFPQConfig());
|
|
45
|
+
|
|
46
|
+
/// Construct an empty index
|
|
47
|
+
GpuIndexIVFPQ(GpuResources* resources,
|
|
48
|
+
int dims,
|
|
49
|
+
int nlist,
|
|
50
|
+
int subQuantizers,
|
|
51
|
+
int bitsPerCode,
|
|
52
|
+
faiss::MetricType metric,
|
|
53
|
+
GpuIndexIVFPQConfig config = GpuIndexIVFPQConfig());
|
|
54
|
+
|
|
55
|
+
~GpuIndexIVFPQ() override;
|
|
56
|
+
|
|
57
|
+
/// Reserve space on the GPU for the inverted lists for `num`
|
|
58
|
+
/// vectors, assumed equally distributed among
|
|
59
|
+
|
|
60
|
+
/// Initialize ourselves from the given CPU index; will overwrite
|
|
61
|
+
/// all data in ourselves
|
|
62
|
+
void copyFrom(const faiss::IndexIVFPQ* index);
|
|
63
|
+
|
|
64
|
+
/// Copy ourselves to the given CPU index; will overwrite all data
|
|
65
|
+
/// in the index instance
|
|
66
|
+
void copyTo(faiss::IndexIVFPQ* index) const;
|
|
67
|
+
|
|
68
|
+
/// Reserve GPU memory in our inverted lists for this number of vectors
|
|
69
|
+
void reserveMemory(size_t numVecs);
|
|
70
|
+
|
|
71
|
+
/// Enable or disable pre-computed codes
|
|
72
|
+
void setPrecomputedCodes(bool enable);
|
|
73
|
+
|
|
74
|
+
/// Are pre-computed codes enabled?
|
|
75
|
+
bool getPrecomputedCodes() const;
|
|
76
|
+
|
|
77
|
+
/// Return the number of sub-quantizers we are using
|
|
78
|
+
int getNumSubQuantizers() const;
|
|
79
|
+
|
|
80
|
+
/// Return the number of bits per PQ code
|
|
81
|
+
int getBitsPerCode() const;
|
|
82
|
+
|
|
83
|
+
/// Return the number of centroids per PQ code (2^bits per code)
|
|
84
|
+
int getCentroidsPerSubQuantizer() const;
|
|
85
|
+
|
|
86
|
+
/// After adding vectors, one can call this to reclaim device memory
|
|
87
|
+
/// to exactly the amount needed. Returns space reclaimed in bytes
|
|
88
|
+
size_t reclaimMemory();
|
|
89
|
+
|
|
90
|
+
/// Clears out all inverted lists, but retains the coarse and
|
|
91
|
+
/// product centroid information
|
|
92
|
+
void reset() override;
|
|
93
|
+
|
|
94
|
+
void train(Index::idx_t n, const float* x) override;
|
|
95
|
+
|
|
96
|
+
/// For debugging purposes, return the list length of a particular
|
|
97
|
+
/// list
|
|
98
|
+
int getListLength(int listId) const;
|
|
99
|
+
|
|
100
|
+
/// For debugging purposes, return the list codes of a particular
|
|
101
|
+
/// list
|
|
102
|
+
std::vector<unsigned char> getListCodes(int listId) const;
|
|
103
|
+
|
|
104
|
+
/// For debugging purposes, return the list indices of a particular
|
|
105
|
+
/// list
|
|
106
|
+
std::vector<long> getListIndices(int listId) const;
|
|
107
|
+
|
|
108
|
+
protected:
|
|
109
|
+
/// Called from GpuIndex for add/add_with_ids
|
|
110
|
+
void addImpl_(int n,
|
|
111
|
+
const float* x,
|
|
112
|
+
const Index::idx_t* ids) override;
|
|
113
|
+
|
|
114
|
+
/// Called from GpuIndex for search
|
|
115
|
+
void searchImpl_(int n,
|
|
116
|
+
const float* x,
|
|
117
|
+
int k,
|
|
118
|
+
float* distances,
|
|
119
|
+
Index::idx_t* labels) const override;
|
|
120
|
+
|
|
121
|
+
private:
|
|
122
|
+
void verifySettings_() const;
|
|
123
|
+
|
|
124
|
+
void trainResidualQuantizer_(Index::idx_t n, const float* x);
|
|
125
|
+
|
|
126
|
+
private:
|
|
127
|
+
GpuIndexIVFPQConfig ivfpqConfig_;
|
|
128
|
+
|
|
129
|
+
/// Number of sub-quantizers per encoded vector
|
|
130
|
+
int subQuantizers_;
|
|
131
|
+
|
|
132
|
+
/// Bits per sub-quantizer code
|
|
133
|
+
int bitsPerCode_;
|
|
134
|
+
|
|
135
|
+
/// Desired inverted list memory reservation
|
|
136
|
+
size_t reserveMemoryVecs_;
|
|
137
|
+
|
|
138
|
+
/// The product quantizer instance that we own; contains the
|
|
139
|
+
/// inverted lists
|
|
140
|
+
IVFPQ* index_;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
} } // namespace
|