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,139 @@
|
|
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 "Clustering_c.h"
|
12
|
+
#include "Clustering.h"
|
13
|
+
#include "Index.h"
|
14
|
+
#include <vector>
|
15
|
+
#include "macros_impl.h"
|
16
|
+
|
17
|
+
extern "C" {
|
18
|
+
|
19
|
+
using faiss::Clustering;
|
20
|
+
using faiss::ClusteringParameters;
|
21
|
+
using faiss::Index;
|
22
|
+
|
23
|
+
DEFINE_GETTER(Clustering, int, niter)
|
24
|
+
DEFINE_GETTER(Clustering, int, nredo)
|
25
|
+
DEFINE_GETTER(Clustering, int, verbose)
|
26
|
+
DEFINE_GETTER(Clustering, int, spherical)
|
27
|
+
DEFINE_GETTER(Clustering, int, update_index)
|
28
|
+
DEFINE_GETTER(Clustering, int, frozen_centroids)
|
29
|
+
|
30
|
+
DEFINE_GETTER(Clustering, int, min_points_per_centroid)
|
31
|
+
DEFINE_GETTER(Clustering, int, max_points_per_centroid)
|
32
|
+
|
33
|
+
DEFINE_GETTER(Clustering, int, seed)
|
34
|
+
|
35
|
+
/// getter for d
|
36
|
+
DEFINE_GETTER(Clustering, size_t, d)
|
37
|
+
|
38
|
+
/// getter for k
|
39
|
+
DEFINE_GETTER(Clustering, size_t, k)
|
40
|
+
|
41
|
+
void faiss_ClusteringParameters_init(FaissClusteringParameters* params) {
|
42
|
+
ClusteringParameters d;
|
43
|
+
params->frozen_centroids = d.frozen_centroids;
|
44
|
+
params->max_points_per_centroid = d.max_points_per_centroid;
|
45
|
+
params->min_points_per_centroid = d.min_points_per_centroid;
|
46
|
+
params->niter = d.niter;
|
47
|
+
params->nredo = d.nredo;
|
48
|
+
params->seed = d.seed;
|
49
|
+
params->spherical = d.spherical;
|
50
|
+
params->update_index = d.update_index;
|
51
|
+
params->verbose = d.verbose;
|
52
|
+
}
|
53
|
+
|
54
|
+
// This conversion is required because the two types are not memory-compatible
|
55
|
+
inline ClusteringParameters from_faiss_c(const FaissClusteringParameters* params) {
|
56
|
+
ClusteringParameters o;
|
57
|
+
o.frozen_centroids = params->frozen_centroids;
|
58
|
+
o.max_points_per_centroid = params->max_points_per_centroid;
|
59
|
+
o.min_points_per_centroid = params->min_points_per_centroid;
|
60
|
+
o.niter = params->niter;
|
61
|
+
o.nredo = params->nredo;
|
62
|
+
o.seed = params->seed;
|
63
|
+
o.spherical = params->spherical;
|
64
|
+
o.update_index = params->update_index;
|
65
|
+
o.verbose = params->verbose;
|
66
|
+
return o;
|
67
|
+
}
|
68
|
+
|
69
|
+
/// getter for centroids (size = k * d)
|
70
|
+
void faiss_Clustering_centroids(
|
71
|
+
FaissClustering* clustering, float** centroids, size_t* size) {
|
72
|
+
std::vector<float>& v = reinterpret_cast<Clustering*>(clustering)->centroids;
|
73
|
+
if (centroids) {
|
74
|
+
*centroids = v.data();
|
75
|
+
}
|
76
|
+
if (size) {
|
77
|
+
*size = v.size();
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
/// getter for objective values (sum of distances reported by index)
|
82
|
+
/// over iterations
|
83
|
+
void faiss_Clustering_obj(
|
84
|
+
FaissClustering* clustering, float** obj, size_t* size) {
|
85
|
+
std::vector<float>& v = reinterpret_cast<Clustering*>(clustering)->obj;
|
86
|
+
if (obj) {
|
87
|
+
*obj = v.data();
|
88
|
+
}
|
89
|
+
if (size) {
|
90
|
+
*size = v.size();
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
/// the only mandatory parameters are k and d
|
95
|
+
int faiss_Clustering_new(FaissClustering** p_clustering, int d, int k) {
|
96
|
+
try {
|
97
|
+
Clustering* c = new Clustering(d, k);
|
98
|
+
*p_clustering = reinterpret_cast<FaissClustering*>(c);
|
99
|
+
return 0;
|
100
|
+
} CATCH_AND_HANDLE
|
101
|
+
}
|
102
|
+
|
103
|
+
int faiss_Clustering_new_with_params(
|
104
|
+
FaissClustering** p_clustering, int d, int k, const FaissClusteringParameters* cp) {
|
105
|
+
try {
|
106
|
+
Clustering* c = new Clustering(d, k, from_faiss_c(cp));
|
107
|
+
*p_clustering = reinterpret_cast<FaissClustering*>(c);
|
108
|
+
return 0;
|
109
|
+
} CATCH_AND_HANDLE
|
110
|
+
}
|
111
|
+
|
112
|
+
/// Index is used during the assignment stage
|
113
|
+
int faiss_Clustering_train(
|
114
|
+
FaissClustering* clustering, idx_t n, const float* x, FaissIndex* index) {
|
115
|
+
try {
|
116
|
+
reinterpret_cast<Clustering*>(clustering)->train(
|
117
|
+
n, x, *reinterpret_cast<Index*>(index));
|
118
|
+
return 0;
|
119
|
+
} CATCH_AND_HANDLE
|
120
|
+
}
|
121
|
+
|
122
|
+
void faiss_Clustering_free(FaissClustering* clustering) {
|
123
|
+
delete reinterpret_cast<Clustering*>(clustering);
|
124
|
+
}
|
125
|
+
|
126
|
+
int faiss_kmeans_clustering (size_t d, size_t n, size_t k,
|
127
|
+
const float *x,
|
128
|
+
float *centroids,
|
129
|
+
float *q_error) {
|
130
|
+
try {
|
131
|
+
float out = faiss::kmeans_clustering(d, n, k, x, centroids);
|
132
|
+
if (q_error) {
|
133
|
+
*q_error = out;
|
134
|
+
}
|
135
|
+
return 0;
|
136
|
+
} CATCH_AND_HANDLE
|
137
|
+
}
|
138
|
+
|
139
|
+
}
|
@@ -0,0 +1,117 @@
|
|
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_CLUSTERING_C_H
|
12
|
+
#define FAISS_CLUSTERING_C_H
|
13
|
+
|
14
|
+
#include "Index_c.h"
|
15
|
+
#include "faiss_c.h"
|
16
|
+
|
17
|
+
#ifdef __cplusplus
|
18
|
+
extern "C" {
|
19
|
+
#endif
|
20
|
+
|
21
|
+
/** Class for the clustering parameters. Can be passed to the
|
22
|
+
* constructor of the Clustering object.
|
23
|
+
*/
|
24
|
+
typedef struct FaissClusteringParameters {
|
25
|
+
int niter; ///< clustering iterations
|
26
|
+
int nredo; ///< redo clustering this many times and keep best
|
27
|
+
|
28
|
+
int verbose; ///< (bool)
|
29
|
+
int spherical; ///< (bool) do we want normalized centroids?
|
30
|
+
int update_index; ///< (bool) update index after each iteration?
|
31
|
+
int frozen_centroids; ///< (bool) use the centroids provided as input and do not change them during iterations
|
32
|
+
|
33
|
+
int min_points_per_centroid; ///< otherwise you get a warning
|
34
|
+
int max_points_per_centroid; ///< to limit size of dataset
|
35
|
+
|
36
|
+
int seed; ///< seed for the random number generator
|
37
|
+
} FaissClusteringParameters;
|
38
|
+
|
39
|
+
|
40
|
+
/// Sets the ClusteringParameters object with reasonable defaults
|
41
|
+
void faiss_ClusteringParameters_init(FaissClusteringParameters* params);
|
42
|
+
|
43
|
+
|
44
|
+
/** clustering based on assignment - centroid update iterations
|
45
|
+
*
|
46
|
+
* The clustering is based on an Index object that assigns training
|
47
|
+
* points to the centroids. Therefore, at each iteration the centroids
|
48
|
+
* are added to the index.
|
49
|
+
*
|
50
|
+
* On output, the centoids table is set to the latest version
|
51
|
+
* of the centroids and they are also added to the index. If the
|
52
|
+
* centroids table it is not empty on input, it is also used for
|
53
|
+
* initialization.
|
54
|
+
*
|
55
|
+
* To do several clusterings, just call train() several times on
|
56
|
+
* different training sets, clearing the centroid table in between.
|
57
|
+
*/
|
58
|
+
FAISS_DECLARE_CLASS(Clustering)
|
59
|
+
|
60
|
+
FAISS_DECLARE_GETTER(Clustering, int, niter)
|
61
|
+
FAISS_DECLARE_GETTER(Clustering, int, nredo)
|
62
|
+
FAISS_DECLARE_GETTER(Clustering, int, verbose)
|
63
|
+
FAISS_DECLARE_GETTER(Clustering, int, spherical)
|
64
|
+
FAISS_DECLARE_GETTER(Clustering, int, update_index)
|
65
|
+
FAISS_DECLARE_GETTER(Clustering, int, frozen_centroids)
|
66
|
+
|
67
|
+
FAISS_DECLARE_GETTER(Clustering, int, min_points_per_centroid)
|
68
|
+
FAISS_DECLARE_GETTER(Clustering, int, max_points_per_centroid)
|
69
|
+
|
70
|
+
FAISS_DECLARE_GETTER(Clustering, int, seed)
|
71
|
+
|
72
|
+
/// getter for d
|
73
|
+
FAISS_DECLARE_GETTER(Clustering, size_t, d)
|
74
|
+
|
75
|
+
/// getter for k
|
76
|
+
FAISS_DECLARE_GETTER(Clustering, size_t, k)
|
77
|
+
|
78
|
+
/// getter for centroids (size = k * d)
|
79
|
+
void faiss_Clustering_centroids(
|
80
|
+
FaissClustering* clustering, float** centroids, size_t* size);
|
81
|
+
|
82
|
+
/// getter for objective values (sum of distances reported by index)
|
83
|
+
/// over iterations
|
84
|
+
void faiss_Clustering_obj(
|
85
|
+
FaissClustering* clustering, float** obj, size_t* size);
|
86
|
+
|
87
|
+
/// the only mandatory parameters are k and d
|
88
|
+
int faiss_Clustering_new(FaissClustering** p_clustering, int d, int k);
|
89
|
+
|
90
|
+
int faiss_Clustering_new_with_params(
|
91
|
+
FaissClustering** p_clustering, int d, int k, const FaissClusteringParameters* cp);
|
92
|
+
|
93
|
+
int faiss_Clustering_train(
|
94
|
+
FaissClustering* clustering, idx_t n, const float* x, FaissIndex* index);
|
95
|
+
|
96
|
+
void faiss_Clustering_free(FaissClustering* clustering);
|
97
|
+
|
98
|
+
/** simplified interface
|
99
|
+
*
|
100
|
+
* @param d dimension of the data
|
101
|
+
* @param n nb of training vectors
|
102
|
+
* @param k nb of output centroids
|
103
|
+
* @param x training set (size n * d)
|
104
|
+
* @param centroids output centroids (size k * d)
|
105
|
+
* @param q_error final quantization error
|
106
|
+
* @return error code
|
107
|
+
*/
|
108
|
+
int faiss_kmeans_clustering (size_t d, size_t n, size_t k,
|
109
|
+
const float *x,
|
110
|
+
float *centroids,
|
111
|
+
float *q_error);
|
112
|
+
|
113
|
+
#ifdef __cplusplus
|
114
|
+
}
|
115
|
+
#endif
|
116
|
+
|
117
|
+
#endif
|
@@ -0,0 +1,140 @@
|
|
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 "IndexFlat_c.h"
|
12
|
+
#include "IndexFlat.h"
|
13
|
+
#include "Index.h"
|
14
|
+
#include "macros_impl.h"
|
15
|
+
|
16
|
+
extern "C" {
|
17
|
+
|
18
|
+
using faiss::Index;
|
19
|
+
using faiss::IndexFlat;
|
20
|
+
using faiss::IndexFlatIP;
|
21
|
+
using faiss::IndexFlatL2;
|
22
|
+
using faiss::IndexFlatL2BaseShift;
|
23
|
+
using faiss::IndexRefineFlat;
|
24
|
+
using faiss::IndexFlat1D;
|
25
|
+
|
26
|
+
DEFINE_DESTRUCTOR(IndexFlat)
|
27
|
+
DEFINE_INDEX_DOWNCAST(IndexFlat)
|
28
|
+
|
29
|
+
int faiss_IndexFlat_new(FaissIndexFlat** p_index) {
|
30
|
+
try {
|
31
|
+
*p_index = reinterpret_cast<FaissIndexFlat*>(new IndexFlat());
|
32
|
+
return 0;
|
33
|
+
} CATCH_AND_HANDLE
|
34
|
+
}
|
35
|
+
|
36
|
+
int faiss_IndexFlat_new_with(FaissIndexFlat** p_index, idx_t d, FaissMetricType metric) {
|
37
|
+
try {
|
38
|
+
IndexFlat* index = new IndexFlat(d, static_cast<faiss::MetricType>(metric));
|
39
|
+
*p_index = reinterpret_cast<FaissIndexFlat*>(index);
|
40
|
+
return 0;
|
41
|
+
} CATCH_AND_HANDLE
|
42
|
+
}
|
43
|
+
|
44
|
+
void faiss_IndexFlat_xb(FaissIndexFlat* index, float** p_xb, size_t* p_size) {
|
45
|
+
auto& xb = reinterpret_cast<IndexFlat*>(index)->xb;
|
46
|
+
*p_xb = xb.data();
|
47
|
+
if (p_size) {
|
48
|
+
*p_size = xb.size();
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
int faiss_IndexFlat_compute_distance_subset(
|
53
|
+
FaissIndex* index,
|
54
|
+
idx_t n,
|
55
|
+
const float *x,
|
56
|
+
idx_t k,
|
57
|
+
float *distances,
|
58
|
+
const idx_t *labels) {
|
59
|
+
try {
|
60
|
+
reinterpret_cast<IndexFlat*>(index)->compute_distance_subset(
|
61
|
+
n, x, k, distances, labels);
|
62
|
+
return 0;
|
63
|
+
} CATCH_AND_HANDLE
|
64
|
+
}
|
65
|
+
|
66
|
+
int faiss_IndexFlatIP_new(FaissIndexFlatIP** p_index) {
|
67
|
+
try {
|
68
|
+
IndexFlatIP* index = new IndexFlatIP();
|
69
|
+
*p_index = reinterpret_cast<FaissIndexFlatIP*>(index);
|
70
|
+
return 0;
|
71
|
+
} CATCH_AND_HANDLE
|
72
|
+
}
|
73
|
+
|
74
|
+
int faiss_IndexFlatIP_new_with(FaissIndexFlatIP** p_index, idx_t d) {
|
75
|
+
try {
|
76
|
+
IndexFlatIP* index = new IndexFlatIP(d);
|
77
|
+
*p_index = reinterpret_cast<FaissIndexFlatIP*>(index);
|
78
|
+
return 0;
|
79
|
+
} CATCH_AND_HANDLE
|
80
|
+
}
|
81
|
+
|
82
|
+
int faiss_IndexFlatL2_new(FaissIndexFlatL2** p_index) {
|
83
|
+
try {
|
84
|
+
IndexFlatL2* index = new IndexFlatL2();
|
85
|
+
*p_index = reinterpret_cast<FaissIndexFlatL2*>(index);
|
86
|
+
return 0;
|
87
|
+
} CATCH_AND_HANDLE
|
88
|
+
}
|
89
|
+
|
90
|
+
int faiss_IndexFlatL2_new_with(FaissIndexFlatL2** p_index, idx_t d) {
|
91
|
+
try {
|
92
|
+
IndexFlatL2* index = new IndexFlatL2(d);
|
93
|
+
*p_index = reinterpret_cast<FaissIndexFlatL2*>(index);
|
94
|
+
return 0;
|
95
|
+
} CATCH_AND_HANDLE
|
96
|
+
}
|
97
|
+
|
98
|
+
int faiss_IndexFlatL2BaseShift_new(FaissIndexFlatL2BaseShift** p_index, idx_t d, size_t nshift, const float *shift) {
|
99
|
+
try {
|
100
|
+
IndexFlatL2BaseShift* index = new IndexFlatL2BaseShift(d, nshift, shift);
|
101
|
+
*p_index = reinterpret_cast<FaissIndexFlatL2BaseShift*>(index);
|
102
|
+
return 0;
|
103
|
+
} CATCH_AND_HANDLE
|
104
|
+
}
|
105
|
+
|
106
|
+
int faiss_IndexRefineFlat_new(FaissIndexRefineFlat** p_index, FaissIndex* base_index) {
|
107
|
+
try {
|
108
|
+
IndexRefineFlat* index = new IndexRefineFlat(
|
109
|
+
reinterpret_cast<faiss::Index*>(base_index));
|
110
|
+
*p_index = reinterpret_cast<FaissIndexRefineFlat*>(index);
|
111
|
+
return 0;
|
112
|
+
} CATCH_AND_HANDLE
|
113
|
+
}
|
114
|
+
|
115
|
+
DEFINE_DESTRUCTOR(IndexRefineFlat)
|
116
|
+
|
117
|
+
int faiss_IndexFlat1D_new(FaissIndexFlat1D** p_index) {
|
118
|
+
try {
|
119
|
+
IndexFlat1D* index = new IndexFlat1D();
|
120
|
+
*p_index = reinterpret_cast<FaissIndexFlat1D*>(index);
|
121
|
+
return 0;
|
122
|
+
} CATCH_AND_HANDLE
|
123
|
+
}
|
124
|
+
|
125
|
+
int faiss_IndexFlat1D_new_with(FaissIndexFlat1D** p_index, int continuous_update) {
|
126
|
+
try {
|
127
|
+
IndexFlat1D* index = new IndexFlat1D(static_cast<bool>(continuous_update));
|
128
|
+
*p_index = reinterpret_cast<FaissIndexFlat1D*>(index);
|
129
|
+
return 0;
|
130
|
+
} CATCH_AND_HANDLE
|
131
|
+
}
|
132
|
+
|
133
|
+
int faiss_IndexFlat1D_update_permutation(FaissIndexFlat1D* index) {
|
134
|
+
try {
|
135
|
+
reinterpret_cast<IndexFlat1D*>(index)->update_permutation();
|
136
|
+
return 0;
|
137
|
+
} CATCH_AND_HANDLE
|
138
|
+
}
|
139
|
+
|
140
|
+
}
|
@@ -0,0 +1,115 @@
|
|
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_FLAT_C_H
|
12
|
+
#define FAISS_INDEX_FLAT_C_H
|
13
|
+
|
14
|
+
#include "Index_c.h"
|
15
|
+
#include "faiss_c.h"
|
16
|
+
|
17
|
+
#ifdef __cplusplus
|
18
|
+
extern "C" {
|
19
|
+
#endif
|
20
|
+
|
21
|
+
// forward declaration
|
22
|
+
typedef enum FaissMetricType FaissMetricType;
|
23
|
+
|
24
|
+
/** Opaque type for IndexFlat */
|
25
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexFlat, Index)
|
26
|
+
|
27
|
+
int faiss_IndexFlat_new(FaissIndexFlat** p_index);
|
28
|
+
|
29
|
+
int faiss_IndexFlat_new_with(FaissIndexFlat** p_index, idx_t d, FaissMetricType metric);
|
30
|
+
|
31
|
+
/** get a pointer to the index's internal data (the `xb` field). The outputs
|
32
|
+
* become invalid after any data addition or removal operation.
|
33
|
+
*
|
34
|
+
* @param index opaque pointer to index object
|
35
|
+
* @param p_xb output, the pointer to the beginning of `xb`.
|
36
|
+
* @param p_size output, the current size of `sb` in number of float values.
|
37
|
+
*/
|
38
|
+
void faiss_IndexFlat_xb(FaissIndexFlat* index, float** p_xb, size_t* p_size);
|
39
|
+
|
40
|
+
/** attempt a dynamic cast to a flat index, thus checking
|
41
|
+
* check whether the underlying index type is `IndexFlat`.
|
42
|
+
*
|
43
|
+
* @param index opaque pointer to index object
|
44
|
+
* @return the same pointer if the index is a flat index, NULL otherwise
|
45
|
+
*/
|
46
|
+
FAISS_DECLARE_INDEX_DOWNCAST(IndexFlat)
|
47
|
+
|
48
|
+
FAISS_DECLARE_DESTRUCTOR(IndexFlat)
|
49
|
+
|
50
|
+
/** compute distance with a subset of vectors
|
51
|
+
*
|
52
|
+
* @param index opaque pointer to index object
|
53
|
+
* @param x query vectors, size n * d
|
54
|
+
* @param labels indices of the vectors that should be compared
|
55
|
+
* for each query vector, size n * k
|
56
|
+
* @param distances
|
57
|
+
* corresponding output distances, size n * k
|
58
|
+
*/
|
59
|
+
int faiss_IndexFlat_compute_distance_subset(
|
60
|
+
FaissIndex *index,
|
61
|
+
idx_t n,
|
62
|
+
const float *x,
|
63
|
+
idx_t k,
|
64
|
+
float *distances,
|
65
|
+
const idx_t *labels);
|
66
|
+
|
67
|
+
/** Opaque type for IndexFlatIP */
|
68
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexFlatIP, Index)
|
69
|
+
|
70
|
+
int faiss_IndexFlatIP_new(FaissIndexFlatIP** p_index);
|
71
|
+
|
72
|
+
int faiss_IndexFlatIP_new_with(FaissIndexFlatIP** p_index, idx_t d);
|
73
|
+
|
74
|
+
/** Opaque type for IndexFlatL2 */
|
75
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexFlatL2, Index)
|
76
|
+
|
77
|
+
int faiss_IndexFlatL2_new(FaissIndexFlatL2** p_index);
|
78
|
+
|
79
|
+
int faiss_IndexFlatL2_new_with(FaissIndexFlatL2** p_index, idx_t d);
|
80
|
+
|
81
|
+
/** Opaque type for IndexFlatL2BaseShift
|
82
|
+
*
|
83
|
+
* same as an IndexFlatL2 but a value is subtracted from each distance
|
84
|
+
*/
|
85
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexFlatL2BaseShift, Index)
|
86
|
+
|
87
|
+
int faiss_IndexFlatL2BaseShift_new(FaissIndexFlatL2BaseShift** p_index, idx_t d, size_t nshift, const float *shift);
|
88
|
+
|
89
|
+
/** Opaque type for IndexRefineFlat
|
90
|
+
*
|
91
|
+
* Index that queries in a base_index (a fast one) and refines the
|
92
|
+
* results with an exact search, hopefully improving the results.
|
93
|
+
*/
|
94
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexRefineFlat, Index)
|
95
|
+
|
96
|
+
int faiss_IndexRefineFlat_new(FaissIndexRefineFlat** p_index, FaissIndex* base_index);
|
97
|
+
|
98
|
+
FAISS_DECLARE_DESTRUCTOR(IndexRefineFlat)
|
99
|
+
|
100
|
+
/** Opaque type for IndexFlat1D
|
101
|
+
*
|
102
|
+
* optimized version for 1D "vectors"
|
103
|
+
*/
|
104
|
+
FAISS_DECLARE_CLASS_INHERITED(IndexFlat1D, Index)
|
105
|
+
|
106
|
+
int faiss_IndexFlat1D_new(FaissIndexFlat1D** p_index);
|
107
|
+
int faiss_IndexFlat1D_new_with(FaissIndexFlat1D** p_index, int continuous_update);
|
108
|
+
|
109
|
+
int faiss_IndexFlat1D_update_permutation(FaissIndexFlat1D* index);
|
110
|
+
|
111
|
+
#ifdef __cplusplus
|
112
|
+
}
|
113
|
+
#endif
|
114
|
+
|
115
|
+
#endif
|