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,82 @@
|
|
|
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
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
#include <faiss/Index.h>
|
|
13
|
+
#include <faiss/clone_index.h>
|
|
14
|
+
#include <faiss/gpu/GpuClonerOptions.h>
|
|
15
|
+
#include <faiss/gpu/GpuIndex.h>
|
|
16
|
+
#include <faiss/gpu/GpuIndicesOptions.h>
|
|
17
|
+
|
|
18
|
+
namespace faiss { namespace gpu {
|
|
19
|
+
|
|
20
|
+
class GpuResources;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/// Cloner specialized for GPU -> CPU
|
|
24
|
+
struct ToCPUCloner: faiss::Cloner {
|
|
25
|
+
void merge_index(Index *dst, Index *src, bool successive_ids);
|
|
26
|
+
Index *clone_Index(const Index *index) override;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/// Cloner specialized for CPU -> 1 GPU
|
|
31
|
+
struct ToGpuCloner: faiss::Cloner, GpuClonerOptions {
|
|
32
|
+
GpuResources *resources;
|
|
33
|
+
int device;
|
|
34
|
+
|
|
35
|
+
ToGpuCloner(GpuResources *resources, int device,
|
|
36
|
+
const GpuClonerOptions &options);
|
|
37
|
+
|
|
38
|
+
Index *clone_Index(const Index *index) override;
|
|
39
|
+
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/// Cloner specialized for CPU -> multiple GPUs
|
|
43
|
+
struct ToGpuClonerMultiple: faiss::Cloner, GpuMultipleClonerOptions {
|
|
44
|
+
std::vector<ToGpuCloner> sub_cloners;
|
|
45
|
+
|
|
46
|
+
ToGpuClonerMultiple(std::vector<GpuResources *> & resources,
|
|
47
|
+
std::vector<int>& devices,
|
|
48
|
+
const GpuMultipleClonerOptions &options);
|
|
49
|
+
|
|
50
|
+
ToGpuClonerMultiple(const std::vector<ToGpuCloner> & sub_cloners,
|
|
51
|
+
const GpuMultipleClonerOptions &options);
|
|
52
|
+
|
|
53
|
+
void copy_ivf_shard (const IndexIVF *index_ivf, IndexIVF *idx2,
|
|
54
|
+
long n, long i);
|
|
55
|
+
|
|
56
|
+
Index * clone_Index_to_shards (const Index *index);
|
|
57
|
+
|
|
58
|
+
/// main function
|
|
59
|
+
Index *clone_Index(const Index *index) override;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
/// converts any GPU index inside gpu_index to a CPU index
|
|
66
|
+
faiss::Index * index_gpu_to_cpu(const faiss::Index *gpu_index);
|
|
67
|
+
|
|
68
|
+
/// converts any CPU index that can be converted to GPU
|
|
69
|
+
faiss::Index * index_cpu_to_gpu(
|
|
70
|
+
GpuResources* resources, int device,
|
|
71
|
+
const faiss::Index *index,
|
|
72
|
+
const GpuClonerOptions *options = nullptr);
|
|
73
|
+
|
|
74
|
+
faiss::Index * index_cpu_to_gpu_multiple(
|
|
75
|
+
std::vector<GpuResources*> & resources,
|
|
76
|
+
std::vector<int> &devices,
|
|
77
|
+
const faiss::Index *index,
|
|
78
|
+
const GpuMultipleClonerOptions *options = nullptr);
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
} } // namespace
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include <faiss/gpu/GpuClonerOptions.h>
|
|
9
|
+
|
|
10
|
+
namespace faiss { namespace gpu {
|
|
11
|
+
|
|
12
|
+
GpuClonerOptions::GpuClonerOptions()
|
|
13
|
+
: indicesOptions(INDICES_64_BIT),
|
|
14
|
+
useFloat16CoarseQuantizer(false),
|
|
15
|
+
useFloat16(false),
|
|
16
|
+
usePrecomputed(true),
|
|
17
|
+
reserveVecs(0),
|
|
18
|
+
storeTransposed(false),
|
|
19
|
+
verbose(false) {
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
GpuMultipleClonerOptions::GpuMultipleClonerOptions()
|
|
23
|
+
: shard(false),
|
|
24
|
+
shard_type(1)
|
|
25
|
+
{
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
} } // namespace
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <faiss/gpu/GpuIndicesOptions.h>
|
|
11
|
+
|
|
12
|
+
namespace faiss { namespace gpu {
|
|
13
|
+
|
|
14
|
+
/// set some options on how to copy to GPU
|
|
15
|
+
struct GpuClonerOptions {
|
|
16
|
+
GpuClonerOptions();
|
|
17
|
+
|
|
18
|
+
/// how should indices be stored on index types that support indices
|
|
19
|
+
/// (anything but GpuIndexFlat*)?
|
|
20
|
+
IndicesOptions indicesOptions;
|
|
21
|
+
|
|
22
|
+
/// is the coarse quantizer in float16?
|
|
23
|
+
bool useFloat16CoarseQuantizer;
|
|
24
|
+
|
|
25
|
+
/// for GpuIndexIVFFlat, is storage in float16?
|
|
26
|
+
/// for GpuIndexIVFPQ, are intermediate calculations in float16?
|
|
27
|
+
bool useFloat16;
|
|
28
|
+
|
|
29
|
+
/// use precomputed tables?
|
|
30
|
+
bool usePrecomputed;
|
|
31
|
+
|
|
32
|
+
/// reserve vectors in the invfiles?
|
|
33
|
+
long reserveVecs;
|
|
34
|
+
|
|
35
|
+
/// For GpuIndexFlat, store data in transposed layout?
|
|
36
|
+
bool storeTransposed;
|
|
37
|
+
|
|
38
|
+
/// Set verbose options on the index
|
|
39
|
+
bool verbose;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
struct GpuMultipleClonerOptions : public GpuClonerOptions {
|
|
43
|
+
GpuMultipleClonerOptions ();
|
|
44
|
+
|
|
45
|
+
/// Whether to shard the index across GPUs, versus replication
|
|
46
|
+
/// across GPUs
|
|
47
|
+
bool shard;
|
|
48
|
+
|
|
49
|
+
/// IndexIVF::copy_subset_to subset type
|
|
50
|
+
int shard_type;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
} } // namespace
|
|
@@ -0,0 +1,52 @@
|
|
|
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/Index.h>
|
|
12
|
+
|
|
13
|
+
namespace faiss { namespace gpu {
|
|
14
|
+
|
|
15
|
+
class GpuResources;
|
|
16
|
+
|
|
17
|
+
/// A wrapper for gpu/impl/Distance.cuh to expose direct brute-force k-nearest
|
|
18
|
+
/// neighbor searches on an externally-provided region of memory (e.g., from a
|
|
19
|
+
/// pytorch tensor).
|
|
20
|
+
/// The data (vectors, queries, outDistances, outIndices) can be resident on the
|
|
21
|
+
/// GPU or the CPU, but all calculations are performed on the GPU. If the result
|
|
22
|
+
/// buffers are on the CPU, results will be copied back when done.
|
|
23
|
+
///
|
|
24
|
+
/// All GPU computation is performed on the current CUDA device, and ordered
|
|
25
|
+
/// with respect to resources->getDefaultStreamCurrentDevice().
|
|
26
|
+
///
|
|
27
|
+
/// For each vector in `queries`, searches all of `vectors` to find its k
|
|
28
|
+
/// nearest neighbors with respect to the given metric
|
|
29
|
+
void bruteForceKnn(GpuResources* resources,
|
|
30
|
+
faiss::MetricType metric,
|
|
31
|
+
// If vectorsRowMajor is true, this is
|
|
32
|
+
// numVectors x dims, with dims innermost; otherwise,
|
|
33
|
+
// dims x numVectors, with numVectors innermost
|
|
34
|
+
const float* vectors,
|
|
35
|
+
bool vectorsRowMajor,
|
|
36
|
+
int numVectors,
|
|
37
|
+
// If queriesRowMajor is true, this is
|
|
38
|
+
// numQueries x dims, with dims innermost; otherwise,
|
|
39
|
+
// dims x numQueries, with numQueries innermost
|
|
40
|
+
const float* queries,
|
|
41
|
+
bool queriesRowMajor,
|
|
42
|
+
int numQueries,
|
|
43
|
+
int dims,
|
|
44
|
+
int k,
|
|
45
|
+
// A region of memory size numQueries x k, with k
|
|
46
|
+
// innermost (row major)
|
|
47
|
+
float* outDistances,
|
|
48
|
+
// A region of memory size numQueries x k, with k
|
|
49
|
+
// innermost (row major)
|
|
50
|
+
faiss::Index::idx_t* outIndices);
|
|
51
|
+
|
|
52
|
+
} } // namespace
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
#ifndef GPU_FAISS_ASSERT_INCLUDED
|
|
10
|
+
#define GPU_FAISS_ASSERT_INCLUDED
|
|
11
|
+
|
|
12
|
+
#include <faiss/impl/FaissAssert.h>
|
|
13
|
+
#include <cuda.h>
|
|
14
|
+
|
|
15
|
+
///
|
|
16
|
+
/// Assertions
|
|
17
|
+
///
|
|
18
|
+
|
|
19
|
+
#ifdef __CUDA_ARCH__
|
|
20
|
+
#define GPU_FAISS_ASSERT(X) assert(X)
|
|
21
|
+
#define GPU_FAISS_ASSERT_MSG(X, MSG) assert(X)
|
|
22
|
+
#define GPU_FAISS_ASSERT_FMT(X, FMT, ...) assert(X)
|
|
23
|
+
#else
|
|
24
|
+
#define GPU_FAISS_ASSERT(X) FAISS_ASSERT(X)
|
|
25
|
+
#define GPU_FAISS_ASSERT_MSG(X, MSG) FAISS_ASSERT_MSG(X, MSG)
|
|
26
|
+
#define GPU_FAISS_ASSERT_FMT(X, FMT, ...) FAISS_ASSERT_FMT(X, FMT, __VA_ARGS)
|
|
27
|
+
#endif // __CUDA_ARCH__
|
|
28
|
+
|
|
29
|
+
#endif
|
|
@@ -0,0 +1,148 @@
|
|
|
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/Index.h>
|
|
12
|
+
#include <faiss/gpu/utils/MemorySpace.h>
|
|
13
|
+
|
|
14
|
+
namespace faiss { namespace gpu {
|
|
15
|
+
|
|
16
|
+
class GpuResources;
|
|
17
|
+
|
|
18
|
+
struct GpuIndexConfig {
|
|
19
|
+
inline GpuIndexConfig()
|
|
20
|
+
: device(0),
|
|
21
|
+
memorySpace(MemorySpace::Device) {
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// GPU device on which the index is resident
|
|
25
|
+
int device;
|
|
26
|
+
|
|
27
|
+
/// What memory space to use for primary storage.
|
|
28
|
+
/// On Pascal and above (CC 6+) architectures, allows GPUs to use
|
|
29
|
+
/// more memory than is available on the GPU.
|
|
30
|
+
MemorySpace memorySpace;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
class GpuIndex : public faiss::Index {
|
|
34
|
+
public:
|
|
35
|
+
GpuIndex(GpuResources* resources,
|
|
36
|
+
int dims,
|
|
37
|
+
faiss::MetricType metric,
|
|
38
|
+
GpuIndexConfig config);
|
|
39
|
+
|
|
40
|
+
inline int getDevice() const {
|
|
41
|
+
return device_;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
inline GpuResources* getResources() {
|
|
45
|
+
return resources_;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// Set the minimum data size for searches (in MiB) for which we use
|
|
49
|
+
/// CPU -> GPU paging
|
|
50
|
+
void setMinPagingSize(size_t size);
|
|
51
|
+
|
|
52
|
+
/// Returns the current minimum data size for paged searches
|
|
53
|
+
size_t getMinPagingSize() const;
|
|
54
|
+
|
|
55
|
+
/// `x` can be resident on the CPU or any GPU; copies are performed
|
|
56
|
+
/// as needed
|
|
57
|
+
/// Handles paged adds if the add set is too large; calls addInternal_
|
|
58
|
+
void add(faiss::Index::idx_t, const float* x) override;
|
|
59
|
+
|
|
60
|
+
/// `x` and `ids` can be resident on the CPU or any GPU; copies are
|
|
61
|
+
/// performed as needed
|
|
62
|
+
/// Handles paged adds if the add set is too large; calls addInternal_
|
|
63
|
+
void add_with_ids(Index::idx_t n,
|
|
64
|
+
const float* x,
|
|
65
|
+
const Index::idx_t* ids) override;
|
|
66
|
+
|
|
67
|
+
/// `x`, `distances` and `labels` can be resident on the CPU or any
|
|
68
|
+
/// GPU; copies are performed as needed
|
|
69
|
+
void search(Index::idx_t n,
|
|
70
|
+
const float* x,
|
|
71
|
+
Index::idx_t k,
|
|
72
|
+
float* distances,
|
|
73
|
+
Index::idx_t* labels) const override;
|
|
74
|
+
|
|
75
|
+
/// Overridden to force GPU indices to provide their own GPU-friendly
|
|
76
|
+
/// implementation
|
|
77
|
+
void compute_residual(const float* x,
|
|
78
|
+
float* residual,
|
|
79
|
+
Index::idx_t key) const override;
|
|
80
|
+
|
|
81
|
+
/// Overridden to force GPU indices to provide their own GPU-friendly
|
|
82
|
+
/// implementation
|
|
83
|
+
void compute_residual_n(Index::idx_t n,
|
|
84
|
+
const float* xs,
|
|
85
|
+
float* residuals,
|
|
86
|
+
const Index::idx_t* keys) const override;
|
|
87
|
+
|
|
88
|
+
protected:
|
|
89
|
+
/// Does addImpl_ require IDs? If so, and no IDs are provided, we will
|
|
90
|
+
/// generate them sequentially based on the order in which the IDs are added
|
|
91
|
+
virtual bool addImplRequiresIDs_() const = 0;
|
|
92
|
+
|
|
93
|
+
/// Overridden to actually perform the add
|
|
94
|
+
/// All data is guaranteed to be resident on our device
|
|
95
|
+
virtual void addImpl_(int n,
|
|
96
|
+
const float* x,
|
|
97
|
+
const Index::idx_t* ids) = 0;
|
|
98
|
+
|
|
99
|
+
/// Overridden to actually perform the search
|
|
100
|
+
/// All data is guaranteed to be resident on our device
|
|
101
|
+
virtual void searchImpl_(int n,
|
|
102
|
+
const float* x,
|
|
103
|
+
int k,
|
|
104
|
+
float* distances,
|
|
105
|
+
Index::idx_t* labels) const = 0;
|
|
106
|
+
|
|
107
|
+
private:
|
|
108
|
+
/// Handles paged adds if the add set is too large, passes to
|
|
109
|
+
/// addImpl_ to actually perform the add for the current page
|
|
110
|
+
void addPaged_(int n,
|
|
111
|
+
const float* x,
|
|
112
|
+
const Index::idx_t* ids);
|
|
113
|
+
|
|
114
|
+
/// Calls addImpl_ for a single page of GPU-resident data
|
|
115
|
+
void addPage_(int n,
|
|
116
|
+
const float* x,
|
|
117
|
+
const Index::idx_t* ids);
|
|
118
|
+
|
|
119
|
+
/// Calls searchImpl_ for a single page of GPU-resident data
|
|
120
|
+
void searchNonPaged_(int n,
|
|
121
|
+
const float* x,
|
|
122
|
+
int k,
|
|
123
|
+
float* outDistancesData,
|
|
124
|
+
Index::idx_t* outIndicesData) const;
|
|
125
|
+
|
|
126
|
+
/// Calls searchImpl_ for a single page of GPU-resident data,
|
|
127
|
+
/// handling paging of the data and copies from the CPU
|
|
128
|
+
void searchFromCpuPaged_(int n,
|
|
129
|
+
const float* x,
|
|
130
|
+
int k,
|
|
131
|
+
float* outDistancesData,
|
|
132
|
+
Index::idx_t* outIndicesData) const;
|
|
133
|
+
|
|
134
|
+
protected:
|
|
135
|
+
/// Manages streams, cuBLAS handles and scratch memory for devices
|
|
136
|
+
GpuResources* resources_;
|
|
137
|
+
|
|
138
|
+
/// The GPU device we are resident on
|
|
139
|
+
const int device_;
|
|
140
|
+
|
|
141
|
+
/// The memory space of our primary storage on the GPU
|
|
142
|
+
const MemorySpace memorySpace_;
|
|
143
|
+
|
|
144
|
+
/// Size above which we page copies from the CPU to GPU
|
|
145
|
+
size_t minPagedSize_;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
} } // 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
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <faiss/IndexBinaryFlat.h>
|
|
11
|
+
#include <faiss/gpu/GpuIndex.h>
|
|
12
|
+
|
|
13
|
+
namespace faiss { namespace gpu {
|
|
14
|
+
|
|
15
|
+
class BinaryFlatIndex;
|
|
16
|
+
class GpuResources;
|
|
17
|
+
|
|
18
|
+
struct GpuIndexBinaryFlatConfig : public GpuIndexConfig {
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/// A GPU version of IndexBinaryFlat for brute-force comparison of bit vectors
|
|
22
|
+
/// via Hamming distance
|
|
23
|
+
class GpuIndexBinaryFlat : public IndexBinary {
|
|
24
|
+
public:
|
|
25
|
+
/// Construct from a pre-existing faiss::IndexBinaryFlat instance, copying
|
|
26
|
+
/// data over to the given GPU
|
|
27
|
+
GpuIndexBinaryFlat(GpuResources* resources,
|
|
28
|
+
const faiss::IndexBinaryFlat* index,
|
|
29
|
+
GpuIndexBinaryFlatConfig config =
|
|
30
|
+
GpuIndexBinaryFlatConfig());
|
|
31
|
+
|
|
32
|
+
/// Construct an empty instance that can be added to
|
|
33
|
+
GpuIndexBinaryFlat(GpuResources* resources,
|
|
34
|
+
int dims,
|
|
35
|
+
GpuIndexBinaryFlatConfig config =
|
|
36
|
+
GpuIndexBinaryFlatConfig());
|
|
37
|
+
|
|
38
|
+
~GpuIndexBinaryFlat() override;
|
|
39
|
+
|
|
40
|
+
/// Initialize ourselves from the given CPU index; will overwrite
|
|
41
|
+
/// all data in ourselves
|
|
42
|
+
void copyFrom(const faiss::IndexBinaryFlat* index);
|
|
43
|
+
|
|
44
|
+
/// Copy ourselves to the given CPU index; will overwrite all data
|
|
45
|
+
/// in the index instance
|
|
46
|
+
void copyTo(faiss::IndexBinaryFlat* index) const;
|
|
47
|
+
|
|
48
|
+
void add(faiss::IndexBinary::idx_t n,
|
|
49
|
+
const uint8_t* x) override;
|
|
50
|
+
|
|
51
|
+
void reset() override;
|
|
52
|
+
|
|
53
|
+
void search(faiss::IndexBinary::idx_t n,
|
|
54
|
+
const uint8_t* x,
|
|
55
|
+
faiss::IndexBinary::idx_t k,
|
|
56
|
+
int32_t* distances,
|
|
57
|
+
faiss::IndexBinary::idx_t* labels) const override;
|
|
58
|
+
|
|
59
|
+
void reconstruct(faiss::IndexBinary::idx_t key,
|
|
60
|
+
uint8_t* recons) const override;
|
|
61
|
+
|
|
62
|
+
protected:
|
|
63
|
+
/// Called from search when the input data is on the CPU;
|
|
64
|
+
/// potentially allows for pinned memory usage
|
|
65
|
+
void searchFromCpuPaged_(int n,
|
|
66
|
+
const uint8_t* x,
|
|
67
|
+
int k,
|
|
68
|
+
int32_t* outDistancesData,
|
|
69
|
+
int* outIndicesData) const;
|
|
70
|
+
|
|
71
|
+
void searchNonPaged_(int n,
|
|
72
|
+
const uint8_t* x,
|
|
73
|
+
int k,
|
|
74
|
+
int32_t* outDistancesData,
|
|
75
|
+
int* outIndicesData) const;
|
|
76
|
+
|
|
77
|
+
protected:
|
|
78
|
+
/// Manages streans, cuBLAS handles and scratch memory for devices
|
|
79
|
+
GpuResources* resources_;
|
|
80
|
+
|
|
81
|
+
/// Configuration options
|
|
82
|
+
GpuIndexBinaryFlatConfig config_;
|
|
83
|
+
|
|
84
|
+
/// Holds our GPU data containing the list of vectors; is managed via raw
|
|
85
|
+
/// pointer so as to allow non-CUDA compilers to see this header
|
|
86
|
+
BinaryFlatIndex* data_;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
} } // namespace gpu
|