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,64 @@
|
|
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 "Clustering_c.h"
|
13
|
+
#include "IndexIVFFlat_c.h"
|
14
|
+
#include "IndexIVFFlat.h"
|
15
|
+
#include "macros_impl.h"
|
16
|
+
|
17
|
+
using faiss::Index;
|
18
|
+
using faiss::IndexIVFFlat;
|
19
|
+
using faiss::MetricType;
|
20
|
+
|
21
|
+
DEFINE_DESTRUCTOR(IndexIVFFlat)
|
22
|
+
DEFINE_INDEX_DOWNCAST(IndexIVFFlat)
|
23
|
+
|
24
|
+
int faiss_IndexIVFFlat_new(FaissIndexIVFFlat** p_index) {
|
25
|
+
try {
|
26
|
+
*p_index = reinterpret_cast<FaissIndexIVFFlat*>(new IndexIVFFlat());
|
27
|
+
} CATCH_AND_HANDLE
|
28
|
+
}
|
29
|
+
|
30
|
+
int faiss_IndexIVFFlat_new_with(FaissIndexIVFFlat** p_index,
|
31
|
+
FaissIndex* quantizer, size_t d, size_t nlist)
|
32
|
+
{
|
33
|
+
try {
|
34
|
+
auto q = reinterpret_cast<Index*>(quantizer);
|
35
|
+
*p_index = reinterpret_cast<FaissIndexIVFFlat*>(new IndexIVFFlat(q, d, nlist));
|
36
|
+
} CATCH_AND_HANDLE
|
37
|
+
}
|
38
|
+
|
39
|
+
int faiss_IndexIVFFlat_new_with_metric(
|
40
|
+
FaissIndexIVFFlat** p_index, FaissIndex* quantizer, size_t d, size_t nlist,
|
41
|
+
FaissMetricType metric)
|
42
|
+
{
|
43
|
+
try {
|
44
|
+
auto q = reinterpret_cast<Index*>(quantizer);
|
45
|
+
auto m = static_cast<MetricType>(metric);
|
46
|
+
*p_index = reinterpret_cast<FaissIndexIVFFlat*>(new IndexIVFFlat(q, d, nlist, m));
|
47
|
+
} CATCH_AND_HANDLE
|
48
|
+
}
|
49
|
+
|
50
|
+
int faiss_IndexIVFFlat_add_core(FaissIndexIVFFlat* index, idx_t n,
|
51
|
+
const float * x, const idx_t *xids, const int64_t *precomputed_idx)
|
52
|
+
{
|
53
|
+
try {
|
54
|
+
reinterpret_cast<IndexIVFFlat*>(index)->add_core(n, x, xids, precomputed_idx);
|
55
|
+
} CATCH_AND_HANDLE
|
56
|
+
}
|
57
|
+
|
58
|
+
int faiss_IndexIVFFlat_update_vectors(FaissIndexIVFFlat* index, int nv,
|
59
|
+
idx_t *idx, const float *v)
|
60
|
+
{
|
61
|
+
try {
|
62
|
+
reinterpret_cast<IndexIVFFlat*>(index)->update_vectors(nv, idx, v);
|
63
|
+
} CATCH_AND_HANDLE
|
64
|
+
}
|
@@ -0,0 +1,58 @@
|
|
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_IVF_FLAT_C_H
|
12
|
+
#define FAISS_INDEX_IVF_FLAT_C_H
|
13
|
+
|
14
|
+
#include "faiss_c.h"
|
15
|
+
#include "Index_c.h"
|
16
|
+
#include "Clustering_c.h"
|
17
|
+
|
18
|
+
#ifdef __cplusplus
|
19
|
+
extern "C" {
|
20
|
+
#endif
|
21
|
+
|
22
|
+
/** Inverted file with stored vectors. Here the inverted file
|
23
|
+
* pre-selects the vectors to be searched, but they are not otherwise
|
24
|
+
* encoded, the code array just contains the raw float entries.
|
25
|
+
*/
|
26
|
+
FAISS_DECLARE_CLASS(IndexIVFFlat)
|
27
|
+
FAISS_DECLARE_DESTRUCTOR(IndexIVFFlat)
|
28
|
+
FAISS_DECLARE_INDEX_DOWNCAST(IndexIVFFlat)
|
29
|
+
|
30
|
+
int faiss_IndexIVFFlat_new(FaissIndexIVFFlat** p_index);
|
31
|
+
|
32
|
+
int faiss_IndexIVFFlat_new_with(FaissIndexIVFFlat** p_index,
|
33
|
+
FaissIndex* quantizer, size_t d, size_t nlist);
|
34
|
+
|
35
|
+
int faiss_IndexIVFFlat_new_with_metric(
|
36
|
+
FaissIndexIVFFlat** p_index, FaissIndex* quantizer, size_t d, size_t nlist,
|
37
|
+
FaissMetricType metric);
|
38
|
+
|
39
|
+
int faiss_IndexIVFFlat_add_core(FaissIndexIVFFlat* index, idx_t n,
|
40
|
+
const float * x, const idx_t *xids, const int64_t *precomputed_idx);
|
41
|
+
|
42
|
+
/** Update a subset of vectors.
|
43
|
+
*
|
44
|
+
* The index must have a direct_map
|
45
|
+
*
|
46
|
+
* @param nv nb of vectors to update
|
47
|
+
* @param idx vector indices to update, size nv
|
48
|
+
* @param v vectors of new values, size nv*d
|
49
|
+
*/
|
50
|
+
int faiss_IndexIVFFlat_update_vectors(FaissIndexIVFFlat* index, int nv,
|
51
|
+
idx_t *idx, const float *v);
|
52
|
+
|
53
|
+
#ifdef __cplusplus
|
54
|
+
}
|
55
|
+
#endif
|
56
|
+
|
57
|
+
|
58
|
+
#endif
|
@@ -0,0 +1,92 @@
|
|
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 "Clustering_c.h"
|
13
|
+
#include "IndexIVF_c.h"
|
14
|
+
#include "IndexIVF.h"
|
15
|
+
#include "macros_impl.h"
|
16
|
+
|
17
|
+
using faiss::IndexIVF;
|
18
|
+
using faiss::IndexIVFStats;
|
19
|
+
|
20
|
+
DEFINE_DESTRUCTOR(IndexIVF)
|
21
|
+
DEFINE_INDEX_DOWNCAST(IndexIVF)
|
22
|
+
|
23
|
+
/// number of possible key values
|
24
|
+
DEFINE_GETTER(IndexIVF, size_t, nlist)
|
25
|
+
/// number of probes at query time
|
26
|
+
DEFINE_GETTER(IndexIVF, size_t, nprobe)
|
27
|
+
/// quantizer that maps vectors to inverted lists
|
28
|
+
DEFINE_GETTER_PERMISSIVE(IndexIVF, FaissIndex*, quantizer)
|
29
|
+
|
30
|
+
/**
|
31
|
+
* = 0: use the quantizer as index in a kmeans training
|
32
|
+
* = 1: just pass on the training set to the train() of the quantizer
|
33
|
+
* = 2: kmeans training on a flat index + add the centroids to the quantizer
|
34
|
+
*/
|
35
|
+
DEFINE_GETTER(IndexIVF, char, quantizer_trains_alone)
|
36
|
+
|
37
|
+
/// whether object owns the quantizer
|
38
|
+
DEFINE_GETTER(IndexIVF, int, own_fields)
|
39
|
+
|
40
|
+
using faiss::IndexIVF;
|
41
|
+
|
42
|
+
int faiss_IndexIVF_merge_from(
|
43
|
+
FaissIndexIVF* index, FaissIndexIVF* other, idx_t add_id) {
|
44
|
+
try {
|
45
|
+
reinterpret_cast<IndexIVF*>(index)->merge_from(
|
46
|
+
*reinterpret_cast<IndexIVF*>(other), add_id);
|
47
|
+
} CATCH_AND_HANDLE
|
48
|
+
}
|
49
|
+
|
50
|
+
int faiss_IndexIVF_copy_subset_to(
|
51
|
+
const FaissIndexIVF* index, FaissIndexIVF* other, int subset_type, idx_t a1,
|
52
|
+
idx_t a2) {
|
53
|
+
try {
|
54
|
+
reinterpret_cast<const IndexIVF*>(index)->copy_subset_to(
|
55
|
+
*reinterpret_cast<IndexIVF*>(other), subset_type, a1, a2);
|
56
|
+
} CATCH_AND_HANDLE
|
57
|
+
}
|
58
|
+
|
59
|
+
int faiss_IndexIVF_search_preassigned (const FaissIndexIVF* index,
|
60
|
+
idx_t n, const float *x, idx_t k, const idx_t *assign,
|
61
|
+
const float *centroid_dis, float *distances, idx_t *labels,
|
62
|
+
int store_pairs) {
|
63
|
+
try {
|
64
|
+
reinterpret_cast<const IndexIVF*>(index)->search_preassigned(
|
65
|
+
n, x, k, assign, centroid_dis, distances, labels, store_pairs);
|
66
|
+
} CATCH_AND_HANDLE
|
67
|
+
}
|
68
|
+
|
69
|
+
size_t faiss_IndexIVF_get_list_size(const FaissIndexIVF* index, size_t list_no) {
|
70
|
+
return reinterpret_cast<const IndexIVF*>(index)->get_list_size(list_no);
|
71
|
+
}
|
72
|
+
|
73
|
+
int faiss_IndexIVF_make_direct_map(FaissIndexIVF* index,
|
74
|
+
int new_maintain_direct_map) {
|
75
|
+
try {
|
76
|
+
reinterpret_cast<IndexIVF*>(index)->make_direct_map(
|
77
|
+
static_cast<bool>(new_maintain_direct_map));
|
78
|
+
} CATCH_AND_HANDLE
|
79
|
+
}
|
80
|
+
|
81
|
+
double faiss_IndexIVF_imbalance_factor (const FaissIndexIVF* index) {
|
82
|
+
return reinterpret_cast<const IndexIVF*>(index)->invlists->imbalance_factor();
|
83
|
+
}
|
84
|
+
|
85
|
+
/// display some stats about the inverted lists
|
86
|
+
void faiss_IndexIVF_print_stats (const FaissIndexIVF* index) {
|
87
|
+
reinterpret_cast<const IndexIVF*>(index)->invlists->print_stats();
|
88
|
+
}
|
89
|
+
|
90
|
+
void faiss_IndexIVFStats_reset(FaissIndexIVFStats* stats) {
|
91
|
+
reinterpret_cast<IndexIVFStats*>(stats)->reset();
|
92
|
+
}
|
@@ -0,0 +1,135 @@
|
|
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_IVF_C_H
|
12
|
+
#define FAISS_INDEX_IVF_C_H
|
13
|
+
|
14
|
+
#include "faiss_c.h"
|
15
|
+
#include "Index_c.h"
|
16
|
+
#include "Clustering_c.h"
|
17
|
+
|
18
|
+
#ifdef __cplusplus
|
19
|
+
extern "C" {
|
20
|
+
#endif
|
21
|
+
|
22
|
+
/** Index based on a inverted file (IVF)
|
23
|
+
*
|
24
|
+
* In the inverted file, the quantizer (an Index instance) provides a
|
25
|
+
* quantization index for each vector to be added. The quantization
|
26
|
+
* index maps to a list (aka inverted list or posting list), where the
|
27
|
+
* id of the vector is then stored.
|
28
|
+
*
|
29
|
+
* At search time, the vector to be searched is also quantized, and
|
30
|
+
* only the list corresponding to the quantization index is
|
31
|
+
* searched. This speeds up the search by making it
|
32
|
+
* non-exhaustive. This can be relaxed using multi-probe search: a few
|
33
|
+
* (nprobe) quantization indices are selected and several inverted
|
34
|
+
* lists are visited.
|
35
|
+
*
|
36
|
+
* Sub-classes implement a post-filtering of the index that refines
|
37
|
+
* the distance estimation from the query to databse vectors.
|
38
|
+
*/
|
39
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexIVF, Index)
|
40
|
+
FAISS_DECLARE_DESTRUCTOR(IndexIVF)
|
41
|
+
FAISS_DECLARE_INDEX_DOWNCAST(IndexIVF)
|
42
|
+
|
43
|
+
/// number of possible key values
|
44
|
+
FAISS_DECLARE_GETTER(IndexIVF, size_t, nlist)
|
45
|
+
/// number of probes at query time
|
46
|
+
FAISS_DECLARE_GETTER(IndexIVF, size_t, nprobe)
|
47
|
+
/// quantizer that maps vectors to inverted lists
|
48
|
+
FAISS_DECLARE_GETTER(IndexIVF, FaissIndex*, quantizer)
|
49
|
+
/**
|
50
|
+
* = 0: use the quantizer as index in a kmeans training
|
51
|
+
* = 1: just pass on the training set to the train() of the quantizer
|
52
|
+
* = 2: kmeans training on a flat index + add the centroids to the quantizer
|
53
|
+
*/
|
54
|
+
FAISS_DECLARE_GETTER(IndexIVF, char, quantizer_trains_alone)
|
55
|
+
|
56
|
+
/// whether object owns the quantizer
|
57
|
+
FAISS_DECLARE_GETTER(IndexIVF, int, own_fields)
|
58
|
+
|
59
|
+
/** moves the entries from another dataset to self. On output,
|
60
|
+
* other is empty. add_id is added to all moved ids (for
|
61
|
+
* sequential ids, this would be this->ntotal */
|
62
|
+
int faiss_IndexIVF_merge_from(
|
63
|
+
FaissIndexIVF* index, FaissIndexIVF* other, idx_t add_id);
|
64
|
+
|
65
|
+
/** copy a subset of the entries index to the other index
|
66
|
+
*
|
67
|
+
* if subset_type == 0: copies ids in [a1, a2)
|
68
|
+
* if subset_type == 1: copies ids if id % a1 == a2
|
69
|
+
* if subset_type == 2: copies inverted lists such that a1
|
70
|
+
* elements are left before and a2 elements are after
|
71
|
+
*/
|
72
|
+
int faiss_IndexIVF_copy_subset_to(
|
73
|
+
const FaissIndexIVF* index, FaissIndexIVF* other, int subset_type, idx_t a1,
|
74
|
+
idx_t a2);
|
75
|
+
|
76
|
+
/** search a set of vectors, that are pre-quantized by the IVF
|
77
|
+
* quantizer. Fill in the corresponding heaps with the query
|
78
|
+
* results. search() calls this.
|
79
|
+
*
|
80
|
+
* @param n nb of vectors to query
|
81
|
+
* @param x query vectors, size nx * d
|
82
|
+
* @param assign coarse quantization indices, size nx * nprobe
|
83
|
+
* @param centroid_dis
|
84
|
+
* distances to coarse centroids, size nx * nprobe
|
85
|
+
* @param distance
|
86
|
+
* output distances, size n * k
|
87
|
+
* @param labels output labels, size n * k
|
88
|
+
* @param store_pairs store inv list index + inv list offset
|
89
|
+
* instead in upper/lower 32 bit of result,
|
90
|
+
* instead of ids (used for reranking).
|
91
|
+
*/
|
92
|
+
int faiss_IndexIVF_search_preassigned (const FaissIndexIVF* index,
|
93
|
+
idx_t n, const float *x, idx_t k, const idx_t *assign,
|
94
|
+
const float *centroid_dis, float *distances, idx_t *labels,
|
95
|
+
int store_pairs);
|
96
|
+
|
97
|
+
size_t faiss_IndexIVF_get_list_size(const FaissIndexIVF* index,
|
98
|
+
size_t list_no);
|
99
|
+
|
100
|
+
/** intialize a direct map
|
101
|
+
*
|
102
|
+
* @param new_maintain_direct_map if true, create a direct map,
|
103
|
+
* else clear it
|
104
|
+
*/
|
105
|
+
int faiss_IndexIVF_make_direct_map(FaissIndexIVF* index,
|
106
|
+
int new_maintain_direct_map);
|
107
|
+
|
108
|
+
/** Check the inverted lists' imbalance factor.
|
109
|
+
*
|
110
|
+
* 1= perfectly balanced, >1: imbalanced
|
111
|
+
*/
|
112
|
+
double faiss_IndexIVF_imbalance_factor (const FaissIndexIVF* index);
|
113
|
+
|
114
|
+
/// display some stats about the inverted lists of the index
|
115
|
+
void faiss_IndexIVF_print_stats (const FaissIndexIVF* index);
|
116
|
+
|
117
|
+
|
118
|
+
typedef struct FaissIndexIVFStats {
|
119
|
+
size_t nq; // nb of queries run
|
120
|
+
size_t nlist; // nb of inverted lists scanned
|
121
|
+
size_t ndis; // nb of distancs computed
|
122
|
+
} FaissIndexIVFStats;
|
123
|
+
|
124
|
+
void faiss_IndexIVFStats_reset(FaissIndexIVFStats* stats);
|
125
|
+
|
126
|
+
inline void faiss_IndexIVFStats_init(FaissIndexIVFStats* stats) {
|
127
|
+
faiss_IndexIVFStats_reset(stats);
|
128
|
+
}
|
129
|
+
|
130
|
+
#ifdef __cplusplus
|
131
|
+
}
|
132
|
+
#endif
|
133
|
+
|
134
|
+
|
135
|
+
#endif
|
@@ -0,0 +1,37 @@
|
|
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 "IndexLSH_c.h"
|
12
|
+
#include "IndexLSH.h"
|
13
|
+
#include "macros_impl.h"
|
14
|
+
|
15
|
+
using faiss::Index;
|
16
|
+
using faiss::IndexLSH;
|
17
|
+
|
18
|
+
DEFINE_DESTRUCTOR(IndexLSH)
|
19
|
+
DEFINE_INDEX_DOWNCAST(IndexLSH)
|
20
|
+
|
21
|
+
DEFINE_GETTER(IndexLSH, int, nbits)
|
22
|
+
DEFINE_GETTER(IndexLSH, int, bytes_per_vec)
|
23
|
+
DEFINE_GETTER_PERMISSIVE(IndexLSH, int, rotate_data)
|
24
|
+
DEFINE_GETTER_PERMISSIVE(IndexLSH, int, train_thresholds)
|
25
|
+
|
26
|
+
int faiss_IndexLSH_new(FaissIndexLSH** p_index, idx_t d, int nbits) {
|
27
|
+
try {
|
28
|
+
*p_index = reinterpret_cast<FaissIndexLSH*>(new IndexLSH(d, nbits));
|
29
|
+
} CATCH_AND_HANDLE
|
30
|
+
}
|
31
|
+
|
32
|
+
int faiss_IndexLSH_new_with_options(FaissIndexLSH** p_index, idx_t d, int nbits, int rotate_data, int train_thresholds) {
|
33
|
+
try {
|
34
|
+
*p_index = reinterpret_cast<FaissIndexLSH*>(
|
35
|
+
new IndexLSH(d, nbits, static_cast<bool>(rotate_data), static_cast<bool>(train_thresholds)));
|
36
|
+
} CATCH_AND_HANDLE
|
37
|
+
}
|
@@ -0,0 +1,40 @@
|
|
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 INDEX_LSH_C_H
|
12
|
+
#define INDEX_LSH_C_H
|
13
|
+
|
14
|
+
#include "faiss_c.h"
|
15
|
+
#include "Index_c.h"
|
16
|
+
#include "Clustering_c.h"
|
17
|
+
|
18
|
+
#ifdef __cplusplus
|
19
|
+
extern "C" {
|
20
|
+
#endif
|
21
|
+
|
22
|
+
/** The sign of each vector component is put in a binary signature */
|
23
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexLSH, Index)
|
24
|
+
FAISS_DECLARE_DESTRUCTOR(IndexLSH)
|
25
|
+
FAISS_DECLARE_INDEX_DOWNCAST(IndexLSH)
|
26
|
+
|
27
|
+
FAISS_DECLARE_GETTER(IndexLSH, int, nbits)
|
28
|
+
FAISS_DECLARE_GETTER(IndexLSH, int, bytes_per_vec)
|
29
|
+
FAISS_DECLARE_GETTER(IndexLSH, int, rotate_data)
|
30
|
+
FAISS_DECLARE_GETTER(IndexLSH, int, train_thresholds)
|
31
|
+
|
32
|
+
int faiss_IndexLSH_new(FaissIndexLSH** p_index, idx_t d, int nbits);
|
33
|
+
|
34
|
+
int faiss_IndexLSH_new_with_options(FaissIndexLSH** p_index, idx_t d, int nbits, int rotate_data, int train_thresholds);
|
35
|
+
|
36
|
+
#ifdef __cplusplus
|
37
|
+
}
|
38
|
+
#endif
|
39
|
+
|
40
|
+
#endif
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#include "IndexShards_c.h"
|
2
|
+
#include "IndexShards.h"
|
3
|
+
#include "macros_impl.h"
|
4
|
+
|
5
|
+
using faiss::Index;
|
6
|
+
using faiss::IndexShards;
|
7
|
+
|
8
|
+
DEFINE_GETTER(IndexShards, int, own_fields)
|
9
|
+
DEFINE_SETTER(IndexShards, int, own_fields)
|
10
|
+
|
11
|
+
DEFINE_GETTER(IndexShards, int, successive_ids)
|
12
|
+
DEFINE_SETTER(IndexShards, int, successive_ids)
|
13
|
+
|
14
|
+
int faiss_IndexShards_new(FaissIndexShards** p_index, idx_t d) {
|
15
|
+
try {
|
16
|
+
auto out = new IndexShards(d);
|
17
|
+
*p_index = reinterpret_cast<FaissIndexShards*>(out);
|
18
|
+
} CATCH_AND_HANDLE
|
19
|
+
}
|
20
|
+
|
21
|
+
int faiss_IndexShards_new_with_options(FaissIndexShards** p_index, idx_t d, int threaded, int successive_ids) {
|
22
|
+
try {
|
23
|
+
auto out = new IndexShards(d, static_cast<bool>(threaded), static_cast<bool>(successive_ids));
|
24
|
+
*p_index = reinterpret_cast<FaissIndexShards*>(out);
|
25
|
+
} CATCH_AND_HANDLE
|
26
|
+
}
|
27
|
+
|
28
|
+
int faiss_IndexShards_add_shard(FaissIndexShards* index, FaissIndex* shard) {
|
29
|
+
try {
|
30
|
+
reinterpret_cast<IndexShards*>(index)->add_shard(
|
31
|
+
reinterpret_cast<Index*>(shard));
|
32
|
+
} CATCH_AND_HANDLE
|
33
|
+
}
|
34
|
+
|
35
|
+
int faiss_IndexShards_sync_with_shard_indexes(FaissIndexShards* index) {
|
36
|
+
try {
|
37
|
+
reinterpret_cast<IndexShards*>(index)->sync_with_shard_indexes();
|
38
|
+
} CATCH_AND_HANDLE
|
39
|
+
}
|
40
|
+
|
41
|
+
FaissIndex* faiss_IndexShards_at(FaissIndexShards* index, int i) {
|
42
|
+
auto shard = reinterpret_cast<IndexShards*>(index)->at(i);
|
43
|
+
return reinterpret_cast<FaissIndex*>(shard);
|
44
|
+
}
|