faiss 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +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,42 @@
|
|
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
|
+
// Copyright 2004-present Facebook. All Rights Reserved.
|
9
|
+
// -*- c++ -*-
|
10
|
+
|
11
|
+
#ifndef INDEXSHARDS_C_H
|
12
|
+
#define INDEXSHARDS_C_H
|
13
|
+
|
14
|
+
#include "faiss_c.h"
|
15
|
+
#include "Index_c.h"
|
16
|
+
|
17
|
+
#ifdef __cplusplus
|
18
|
+
extern "C" {
|
19
|
+
#endif
|
20
|
+
|
21
|
+
/** Index that concatenates the results from several sub-indexes
|
22
|
+
*/
|
23
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexShards, Index)
|
24
|
+
|
25
|
+
FAISS_DECLARE_GETTER_SETTER(IndexShards, int, own_fields)
|
26
|
+
FAISS_DECLARE_GETTER_SETTER(IndexShards, int, successive_ids)
|
27
|
+
|
28
|
+
int faiss_IndexShards_new(FaissIndexShards** p_index, idx_t d);
|
29
|
+
|
30
|
+
int faiss_IndexShards_new_with_options(FaissIndexShards** p_index, idx_t d, int threaded, int successive_ids);
|
31
|
+
|
32
|
+
int faiss_IndexShards_add_shard(FaissIndexShards* index, FaissIndex* shard);
|
33
|
+
|
34
|
+
/// update metric_type and ntotal
|
35
|
+
int faiss_IndexShards_sync_with_shard_indexes(FaissIndexShards* index);
|
36
|
+
|
37
|
+
FaissIndex* faiss_IndexShards_at(FaissIndexShards* index, int i);
|
38
|
+
|
39
|
+
#ifdef __cplusplus
|
40
|
+
}
|
41
|
+
#endif
|
42
|
+
#endif
|
@@ -0,0 +1,105 @@
|
|
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
|
+
// Copyright 2004-present Facebook. All Rights Reserved.
|
9
|
+
// -*- c++ -*-
|
10
|
+
|
11
|
+
#include "Index_c.h"
|
12
|
+
#include "Index.h"
|
13
|
+
#include "macros_impl.h"
|
14
|
+
|
15
|
+
extern "C" {
|
16
|
+
|
17
|
+
DEFINE_DESTRUCTOR(Index)
|
18
|
+
|
19
|
+
DEFINE_GETTER(Index, int, d)
|
20
|
+
|
21
|
+
DEFINE_GETTER(Index, int, is_trained)
|
22
|
+
|
23
|
+
DEFINE_GETTER(Index, idx_t, ntotal)
|
24
|
+
|
25
|
+
DEFINE_GETTER(Index, FaissMetricType, metric_type)
|
26
|
+
|
27
|
+
int faiss_Index_train(FaissIndex* index, idx_t n, const float* x) {
|
28
|
+
try {
|
29
|
+
reinterpret_cast<faiss::Index*>(index)->train(n, x);
|
30
|
+
} CATCH_AND_HANDLE
|
31
|
+
}
|
32
|
+
|
33
|
+
int faiss_Index_add(FaissIndex* index, idx_t n, const float* x) {
|
34
|
+
try {
|
35
|
+
reinterpret_cast<faiss::Index*>(index)->add(n, x);
|
36
|
+
} CATCH_AND_HANDLE
|
37
|
+
}
|
38
|
+
|
39
|
+
int faiss_Index_add_with_ids(FaissIndex* index, idx_t n, const float* x, const idx_t* xids) {
|
40
|
+
try {
|
41
|
+
reinterpret_cast<faiss::Index*>(index)->add_with_ids(n, x, xids);
|
42
|
+
} CATCH_AND_HANDLE
|
43
|
+
}
|
44
|
+
|
45
|
+
int faiss_Index_search(const FaissIndex* index, idx_t n, const float* x, idx_t k,
|
46
|
+
float* distances, idx_t* labels) {
|
47
|
+
try {
|
48
|
+
reinterpret_cast<const faiss::Index*>(index)->search(n, x, k, distances, labels);
|
49
|
+
} CATCH_AND_HANDLE
|
50
|
+
}
|
51
|
+
|
52
|
+
int faiss_Index_range_search(const FaissIndex* index, idx_t n, const float* x, float radius,
|
53
|
+
FaissRangeSearchResult* result) {
|
54
|
+
try {
|
55
|
+
reinterpret_cast<const faiss::Index*>(index)->range_search(
|
56
|
+
n, x, radius, reinterpret_cast<faiss::RangeSearchResult*>(result));
|
57
|
+
} CATCH_AND_HANDLE
|
58
|
+
}
|
59
|
+
|
60
|
+
int faiss_Index_assign(FaissIndex* index, idx_t n, const float * x, idx_t * labels, idx_t k) {
|
61
|
+
try {
|
62
|
+
reinterpret_cast<faiss::Index*>(index)->assign(n, x, labels, k);
|
63
|
+
} CATCH_AND_HANDLE
|
64
|
+
}
|
65
|
+
|
66
|
+
int faiss_Index_reset(FaissIndex* index) {
|
67
|
+
try {
|
68
|
+
reinterpret_cast<faiss::Index*>(index)->reset();
|
69
|
+
} CATCH_AND_HANDLE
|
70
|
+
}
|
71
|
+
|
72
|
+
int faiss_Index_remove_ids(FaissIndex* index, const FaissIDSelector* sel, size_t* n_removed) {
|
73
|
+
try {
|
74
|
+
size_t n {reinterpret_cast<faiss::Index*>(index)->remove_ids(
|
75
|
+
*reinterpret_cast<const faiss::IDSelector*>(sel))};
|
76
|
+
if (n_removed) {
|
77
|
+
*n_removed = n;
|
78
|
+
}
|
79
|
+
} CATCH_AND_HANDLE
|
80
|
+
}
|
81
|
+
|
82
|
+
int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons) {
|
83
|
+
try {
|
84
|
+
reinterpret_cast<const faiss::Index*>(index)->reconstruct(key, recons);
|
85
|
+
} CATCH_AND_HANDLE
|
86
|
+
}
|
87
|
+
|
88
|
+
int faiss_Index_reconstruct_n (const FaissIndex* index, idx_t i0, idx_t ni, float* recons) {
|
89
|
+
try {
|
90
|
+
reinterpret_cast<const faiss::Index*>(index)->reconstruct_n(i0, ni, recons);
|
91
|
+
} CATCH_AND_HANDLE
|
92
|
+
}
|
93
|
+
|
94
|
+
int faiss_Index_compute_residual(const FaissIndex* index, const float* x, float* residual, idx_t key) {
|
95
|
+
try {
|
96
|
+
reinterpret_cast<const faiss::Index*>(index)->compute_residual(x, residual, key);
|
97
|
+
} CATCH_AND_HANDLE
|
98
|
+
}
|
99
|
+
|
100
|
+
int faiss_Index_compute_residual_n(const FaissIndex* index, idx_t n, const float* x, float* residuals, const idx_t* keys) {
|
101
|
+
try {
|
102
|
+
reinterpret_cast<const faiss::Index *>(index)->compute_residual_n(n, x, residuals, keys);
|
103
|
+
} CATCH_AND_HANDLE
|
104
|
+
}
|
105
|
+
}
|
@@ -0,0 +1,183 @@
|
|
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
|
+
// Copyright 2004-present Facebook. All Rights Reserved
|
9
|
+
// -*- c -*-
|
10
|
+
|
11
|
+
#ifndef FAISS_INDEX_C_H
|
12
|
+
#define FAISS_INDEX_C_H
|
13
|
+
|
14
|
+
#include <stddef.h>
|
15
|
+
#include "faiss_c.h"
|
16
|
+
|
17
|
+
#ifdef __cplusplus
|
18
|
+
extern "C" {
|
19
|
+
#endif
|
20
|
+
|
21
|
+
// forward declaration required here
|
22
|
+
FAISS_DECLARE_CLASS(RangeSearchResult)
|
23
|
+
|
24
|
+
//typedef struct FaissRangeSearchResult_H FaissRangeSearchResult;
|
25
|
+
typedef struct FaissIDSelector_H FaissIDSelector;
|
26
|
+
|
27
|
+
/// Some algorithms support both an inner product version and a L2 search version.
|
28
|
+
typedef enum FaissMetricType {
|
29
|
+
METRIC_INNER_PRODUCT = 0, ///< maximum inner product search
|
30
|
+
METRIC_L2 = 1, ///< squared L2 search
|
31
|
+
METRIC_L1, ///< L1 (aka cityblock)
|
32
|
+
METRIC_Linf, ///< infinity distance
|
33
|
+
METRIC_Lp, ///< L_p distance, p is given by metric_arg
|
34
|
+
|
35
|
+
/// some additional metrics defined in scipy.spatial.distance
|
36
|
+
METRIC_Canberra = 20,
|
37
|
+
METRIC_BrayCurtis,
|
38
|
+
METRIC_JensenShannon,
|
39
|
+
} FaissMetricType;
|
40
|
+
|
41
|
+
/// Opaque type for referencing to an index object
|
42
|
+
FAISS_DECLARE_CLASS(Index)
|
43
|
+
FAISS_DECLARE_DESTRUCTOR(Index)
|
44
|
+
|
45
|
+
/// Getter for d
|
46
|
+
FAISS_DECLARE_GETTER(Index, int, d)
|
47
|
+
|
48
|
+
/// Getter for is_trained
|
49
|
+
FAISS_DECLARE_GETTER(Index, int, is_trained)
|
50
|
+
|
51
|
+
/// Getter for ntotal
|
52
|
+
FAISS_DECLARE_GETTER(Index, idx_t, ntotal)
|
53
|
+
|
54
|
+
/// Getter for metric_type
|
55
|
+
FAISS_DECLARE_GETTER(Index, FaissMetricType, metric_type)
|
56
|
+
|
57
|
+
/** Perform training on a representative set of vectors
|
58
|
+
*
|
59
|
+
* @param index opaque pointer to index object
|
60
|
+
* @param n nb of training vectors
|
61
|
+
* @param x training vecors, size n * d
|
62
|
+
*/
|
63
|
+
int faiss_Index_train(FaissIndex* index, idx_t n, const float* x);
|
64
|
+
|
65
|
+
/** Add n vectors of dimension d to the index.
|
66
|
+
*
|
67
|
+
* Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
|
68
|
+
* This function slices the input vectors in chuncks smaller than
|
69
|
+
* blocksize_add and calls add_core.
|
70
|
+
* @param index opaque pointer to index object
|
71
|
+
* @param x input matrix, size n * d
|
72
|
+
*/
|
73
|
+
int faiss_Index_add(FaissIndex* index, idx_t n, const float* x);
|
74
|
+
|
75
|
+
/** Same as add, but stores xids instead of sequential ids.
|
76
|
+
*
|
77
|
+
* The default implementation fails with an assertion, as it is
|
78
|
+
* not supported by all indexes.
|
79
|
+
*
|
80
|
+
* @param index opaque pointer to index object
|
81
|
+
* @param xids if non-null, ids to store for the vectors (size n)
|
82
|
+
*/
|
83
|
+
int faiss_Index_add_with_ids(FaissIndex* index, idx_t n, const float* x, const idx_t* xids);
|
84
|
+
|
85
|
+
/** query n vectors of dimension d to the index.
|
86
|
+
*
|
87
|
+
* return at most k vectors. If there are not enough results for a
|
88
|
+
* query, the result array is padded with -1s.
|
89
|
+
*
|
90
|
+
* @param index opaque pointer to index object
|
91
|
+
* @param x input vectors to search, size n * d
|
92
|
+
* @param labels output labels of the NNs, size n*k
|
93
|
+
* @param distances output pairwise distances, size n*k
|
94
|
+
*/
|
95
|
+
int faiss_Index_search(const FaissIndex* index, idx_t n, const float* x, idx_t k,
|
96
|
+
float* distances, idx_t* labels);
|
97
|
+
|
98
|
+
/** query n vectors of dimension d to the index.
|
99
|
+
*
|
100
|
+
* return all vectors with distance < radius. Note that many
|
101
|
+
* indexes do not implement the range_search (only the k-NN search
|
102
|
+
* is mandatory).
|
103
|
+
*
|
104
|
+
* @param index opaque pointer to index object
|
105
|
+
* @param x input vectors to search, size n * d
|
106
|
+
* @param radius search radius
|
107
|
+
* @param result result table
|
108
|
+
*/
|
109
|
+
int faiss_Index_range_search(const FaissIndex* index, idx_t n, const float* x,
|
110
|
+
float radius, FaissRangeSearchResult* result);
|
111
|
+
|
112
|
+
/** return the indexes of the k vectors closest to the query x.
|
113
|
+
*
|
114
|
+
* This function is identical as search but only return labels of neighbors.
|
115
|
+
* @param index opaque pointer to index object
|
116
|
+
* @param x input vectors to search, size n * d
|
117
|
+
* @param labels output labels of the NNs, size n*k
|
118
|
+
*/
|
119
|
+
int faiss_Index_assign(FaissIndex* index, idx_t n, const float * x, idx_t * labels, idx_t k);
|
120
|
+
|
121
|
+
/** removes all elements from the database.
|
122
|
+
* @param index opaque pointer to index object
|
123
|
+
*/
|
124
|
+
int faiss_Index_reset(FaissIndex* index);
|
125
|
+
|
126
|
+
/** removes IDs from the index. Not supported by all indexes
|
127
|
+
* @param index opaque pointer to index object
|
128
|
+
* @param nremove output for the number of IDs removed
|
129
|
+
*/
|
130
|
+
int faiss_Index_remove_ids(FaissIndex* index, const FaissIDSelector* sel, size_t* n_removed);
|
131
|
+
|
132
|
+
/** Reconstruct a stored vector (or an approximation if lossy coding)
|
133
|
+
*
|
134
|
+
* this function may not be defined for some indexes
|
135
|
+
* @param index opaque pointer to index object
|
136
|
+
* @param key id of the vector to reconstruct
|
137
|
+
* @param recons reconstucted vector (size d)
|
138
|
+
*/
|
139
|
+
int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons);
|
140
|
+
|
141
|
+
/** Reconstruct vectors i0 to i0 + ni - 1
|
142
|
+
*
|
143
|
+
* this function may not be defined for some indexes
|
144
|
+
* @param index opaque pointer to index object
|
145
|
+
* @param recons reconstucted vector (size ni * d)
|
146
|
+
*/
|
147
|
+
int faiss_Index_reconstruct_n (const FaissIndex* index, idx_t i0, idx_t ni, float* recons);
|
148
|
+
|
149
|
+
/** Computes a residual vector after indexing encoding.
|
150
|
+
*
|
151
|
+
* The residual vector is the difference between a vector and the
|
152
|
+
* reconstruction that can be decoded from its representation in
|
153
|
+
* the index. The residual can be used for multiple-stage indexing
|
154
|
+
* methods, like IndexIVF's methods.
|
155
|
+
*
|
156
|
+
* @param index opaque pointer to index object
|
157
|
+
* @param x input vector, size d
|
158
|
+
* @param residual output residual vector, size d
|
159
|
+
* @param key encoded index, as returned by search and assign
|
160
|
+
*/
|
161
|
+
int faiss_Index_compute_residual(const FaissIndex* index, const float* x, float* residual, idx_t key);
|
162
|
+
|
163
|
+
/** Computes a residual vector after indexing encoding.
|
164
|
+
*
|
165
|
+
* The residual vector is the difference between a vector and the
|
166
|
+
* reconstruction that can be decoded from its representation in
|
167
|
+
* the index. The residual can be used for multiple-stage indexing
|
168
|
+
* methods, like IndexIVF's methods.
|
169
|
+
*
|
170
|
+
* @param index opaque pointer to index object
|
171
|
+
* @param n number of vectors
|
172
|
+
* @param x input vector, size (n x d)
|
173
|
+
* @param residuals output residual vectors, size (n x d)
|
174
|
+
* @param keys encoded index, as returned by search and assign
|
175
|
+
*/
|
176
|
+
int faiss_Index_compute_residual_n(const FaissIndex* index, idx_t n, const float* x, float* residuals, const idx_t* keys);
|
177
|
+
|
178
|
+
|
179
|
+
#ifdef __cplusplus
|
180
|
+
}
|
181
|
+
#endif
|
182
|
+
|
183
|
+
#endif
|
@@ -0,0 +1,49 @@
|
|
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
|
+
// Copyright 2004-present Facebook. All Rights Reserved.
|
9
|
+
// -*- c++ -*-
|
10
|
+
|
11
|
+
#include "MetaIndexes_c.h"
|
12
|
+
#include "MetaIndexes.h"
|
13
|
+
#include "macros_impl.h"
|
14
|
+
|
15
|
+
using faiss::Index;
|
16
|
+
using faiss::IndexIDMap;
|
17
|
+
using faiss::IndexIDMap2;
|
18
|
+
|
19
|
+
DEFINE_GETTER(IndexIDMap, int, own_fields)
|
20
|
+
DEFINE_SETTER(IndexIDMap, int, own_fields)
|
21
|
+
|
22
|
+
int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index) {
|
23
|
+
try {
|
24
|
+
auto out = new IndexIDMap(reinterpret_cast<Index*>(index));
|
25
|
+
*p_index = reinterpret_cast<FaissIndexIDMap*>(out);
|
26
|
+
} CATCH_AND_HANDLE
|
27
|
+
}
|
28
|
+
|
29
|
+
void faiss_IndexIDMap_id_map(FaissIndexIDMap* index, idx_t** p_id_map, size_t* p_size) {
|
30
|
+
auto idx = reinterpret_cast<IndexIDMap*>(index);
|
31
|
+
if (p_id_map)
|
32
|
+
*p_id_map = idx->id_map.data();
|
33
|
+
if (p_size)
|
34
|
+
*p_size = idx->id_map.size();
|
35
|
+
}
|
36
|
+
|
37
|
+
int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index) {
|
38
|
+
try {
|
39
|
+
auto out = new IndexIDMap2(reinterpret_cast<Index*>(index));
|
40
|
+
*p_index = reinterpret_cast<FaissIndexIDMap2*>(out);
|
41
|
+
} CATCH_AND_HANDLE
|
42
|
+
}
|
43
|
+
|
44
|
+
int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index) {
|
45
|
+
try {
|
46
|
+
reinterpret_cast<IndexIDMap2*>(index)->construct_rev_map();
|
47
|
+
} CATCH_AND_HANDLE
|
48
|
+
}
|
49
|
+
|
@@ -0,0 +1,49 @@
|
|
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
|
+
// Copyright 2004-present Facebook. All Rights Reserved.
|
9
|
+
// -*- c++ -*-
|
10
|
+
|
11
|
+
#ifndef METAINDEXES_C_H
|
12
|
+
#define METAINDEXES_C_H
|
13
|
+
|
14
|
+
#include "faiss_c.h"
|
15
|
+
#include "Index_c.h"
|
16
|
+
|
17
|
+
#ifdef __cplusplus
|
18
|
+
extern "C" {
|
19
|
+
#endif
|
20
|
+
|
21
|
+
/** Index that translates search results to ids */
|
22
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexIDMap, Index)
|
23
|
+
|
24
|
+
FAISS_DECLARE_GETTER_SETTER(IndexIDMap, int, own_fields)
|
25
|
+
|
26
|
+
int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index);
|
27
|
+
|
28
|
+
/** get a pointer to the index map's internal ID vector (the `id_map` field). The
|
29
|
+
* outputs of this function become invalid after any operation that can modify the index.
|
30
|
+
*
|
31
|
+
* @param index opaque pointer to index object
|
32
|
+
* @param p_id_map output, the pointer to the beginning of `id_map`.
|
33
|
+
* @param p_size output, the current length of `id_map`.
|
34
|
+
*/
|
35
|
+
void faiss_IndexIDMap_id_map(FaissIndexIDMap* index, idx_t** p_id_map, size_t* p_size);
|
36
|
+
|
37
|
+
/** same as IndexIDMap but also provides an efficient reconstruction
|
38
|
+
implementation via a 2-way index */
|
39
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexIDMap2, IndexIDMap)
|
40
|
+
|
41
|
+
int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index);
|
42
|
+
|
43
|
+
/// make the rev_map from scratch
|
44
|
+
int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index);
|
45
|
+
|
46
|
+
#ifdef __cplusplus
|
47
|
+
}
|
48
|
+
#endif
|
49
|
+
#endif
|
@@ -0,0 +1,23 @@
|
|
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
|
+
// Copyright 2004-present Facebook. All Rights Reserved
|
9
|
+
// -*- c++ -*-
|
10
|
+
// I/O code for indexes
|
11
|
+
|
12
|
+
#include "clone_index_c.h"
|
13
|
+
#include "clone_index.h"
|
14
|
+
#include "macros_impl.h"
|
15
|
+
|
16
|
+
using faiss::Index;
|
17
|
+
|
18
|
+
int faiss_clone_index (const FaissIndex *idx, FaissIndex **p_out) {
|
19
|
+
try {
|
20
|
+
auto out = faiss::clone_index(reinterpret_cast<const Index*>(idx));
|
21
|
+
*p_out = reinterpret_cast<FaissIndex*>(out);
|
22
|
+
} CATCH_AND_HANDLE
|
23
|
+
}
|