faiss 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ext/faiss/extconf.rb +1 -1
  6. data/lib/faiss/version.rb +1 -1
  7. data/vendor/faiss/faiss/AutoTune.cpp +36 -33
  8. data/vendor/faiss/faiss/AutoTune.h +6 -3
  9. data/vendor/faiss/faiss/Clustering.cpp +16 -12
  10. data/vendor/faiss/faiss/Index.cpp +3 -4
  11. data/vendor/faiss/faiss/Index.h +3 -3
  12. data/vendor/faiss/faiss/IndexBinary.cpp +3 -4
  13. data/vendor/faiss/faiss/IndexBinary.h +1 -1
  14. data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -12
  15. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +1 -2
  16. data/vendor/faiss/faiss/IndexFlat.cpp +0 -148
  17. data/vendor/faiss/faiss/IndexFlat.h +0 -51
  18. data/vendor/faiss/faiss/IndexHNSW.cpp +4 -5
  19. data/vendor/faiss/faiss/IndexIVF.cpp +118 -31
  20. data/vendor/faiss/faiss/IndexIVF.h +22 -15
  21. data/vendor/faiss/faiss/IndexIVFFlat.cpp +3 -3
  22. data/vendor/faiss/faiss/IndexIVFFlat.h +2 -1
  23. data/vendor/faiss/faiss/IndexIVFPQ.cpp +39 -15
  24. data/vendor/faiss/faiss/IndexIVFPQ.h +25 -9
  25. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
  26. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
  27. data/vendor/faiss/faiss/IndexIVFPQR.cpp +8 -9
  28. data/vendor/faiss/faiss/IndexIVFPQR.h +2 -1
  29. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +1 -2
  30. data/vendor/faiss/faiss/IndexPQ.cpp +34 -18
  31. data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
  32. data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
  33. data/vendor/faiss/faiss/IndexPreTransform.cpp +47 -0
  34. data/vendor/faiss/faiss/IndexPreTransform.h +2 -0
  35. data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
  36. data/vendor/faiss/faiss/IndexRefine.h +73 -0
  37. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +2 -2
  38. data/vendor/faiss/faiss/IndexScalarQuantizer.h +1 -1
  39. data/vendor/faiss/faiss/gpu/GpuDistance.h +1 -1
  40. data/vendor/faiss/faiss/gpu/GpuIndex.h +16 -9
  41. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +8 -1
  42. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -11
  43. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +19 -2
  44. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +28 -2
  45. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +24 -14
  46. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +29 -2
  47. data/vendor/faiss/faiss/gpu/GpuResources.h +4 -0
  48. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +60 -27
  49. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +28 -6
  50. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
  51. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
  52. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +3 -3
  53. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +3 -2
  54. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
  55. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +7 -2
  56. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +5 -1
  57. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
  58. data/vendor/faiss/faiss/gpu/test/TestUtils.h +33 -0
  59. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -0
  60. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +6 -0
  61. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +5 -6
  62. data/vendor/faiss/faiss/gpu/utils/Timer.h +2 -2
  63. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +5 -4
  64. data/vendor/faiss/faiss/impl/HNSW.cpp +2 -4
  65. data/vendor/faiss/faiss/impl/PolysemousTraining.h +4 -4
  66. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +22 -12
  67. data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -0
  68. data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
  69. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -19
  70. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +6 -0
  71. data/vendor/faiss/faiss/impl/index_read.cpp +64 -96
  72. data/vendor/faiss/faiss/impl/index_write.cpp +34 -25
  73. data/vendor/faiss/faiss/impl/io.cpp +33 -2
  74. data/vendor/faiss/faiss/impl/io.h +7 -2
  75. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -15
  76. data/vendor/faiss/faiss/impl/platform_macros.h +44 -0
  77. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
  78. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
  79. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
  80. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
  81. data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
  82. data/vendor/faiss/faiss/index_factory.cpp +112 -7
  83. data/vendor/faiss/faiss/index_io.h +1 -48
  84. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
  85. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
  86. data/vendor/faiss/faiss/{DirectMap.cpp → invlists/DirectMap.cpp} +1 -1
  87. data/vendor/faiss/faiss/{DirectMap.h → invlists/DirectMap.h} +1 -1
  88. data/vendor/faiss/faiss/{InvertedLists.cpp → invlists/InvertedLists.cpp} +72 -1
  89. data/vendor/faiss/faiss/{InvertedLists.h → invlists/InvertedLists.h} +32 -1
  90. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
  91. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
  92. data/vendor/faiss/faiss/{OnDiskInvertedLists.cpp → invlists/OnDiskInvertedLists.cpp} +21 -6
  93. data/vendor/faiss/faiss/{OnDiskInvertedLists.h → invlists/OnDiskInvertedLists.h} +5 -2
  94. data/vendor/faiss/faiss/python/python_callbacks.h +8 -1
  95. data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
  96. data/vendor/faiss/faiss/utils/Heap.cpp +2 -4
  97. data/vendor/faiss/faiss/utils/Heap.h +61 -50
  98. data/vendor/faiss/faiss/utils/distances.cpp +164 -319
  99. data/vendor/faiss/faiss/utils/distances.h +28 -20
  100. data/vendor/faiss/faiss/utils/distances_simd.cpp +277 -49
  101. data/vendor/faiss/faiss/utils/extra_distances.cpp +1 -2
  102. data/vendor/faiss/faiss/utils/hamming-inl.h +4 -4
  103. data/vendor/faiss/faiss/utils/hamming.cpp +3 -6
  104. data/vendor/faiss/faiss/utils/hamming.h +2 -7
  105. data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
  106. data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
  107. data/vendor/faiss/faiss/utils/partitioning.h +69 -0
  108. data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
  109. data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
  110. data/vendor/faiss/faiss/utils/simdlib.h +31 -0
  111. data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
  112. data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
  113. metadata +43 -141
  114. data/vendor/faiss/benchs/bench_6bit_codec.cpp +0 -80
  115. data/vendor/faiss/c_api/AutoTune_c.cpp +0 -83
  116. data/vendor/faiss/c_api/AutoTune_c.h +0 -66
  117. data/vendor/faiss/c_api/Clustering_c.cpp +0 -145
  118. data/vendor/faiss/c_api/Clustering_c.h +0 -123
  119. data/vendor/faiss/c_api/IndexFlat_c.cpp +0 -140
  120. data/vendor/faiss/c_api/IndexFlat_c.h +0 -115
  121. data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +0 -64
  122. data/vendor/faiss/c_api/IndexIVFFlat_c.h +0 -58
  123. data/vendor/faiss/c_api/IndexIVF_c.cpp +0 -99
  124. data/vendor/faiss/c_api/IndexIVF_c.h +0 -142
  125. data/vendor/faiss/c_api/IndexLSH_c.cpp +0 -37
  126. data/vendor/faiss/c_api/IndexLSH_c.h +0 -40
  127. data/vendor/faiss/c_api/IndexPreTransform_c.cpp +0 -21
  128. data/vendor/faiss/c_api/IndexPreTransform_c.h +0 -32
  129. data/vendor/faiss/c_api/IndexShards_c.cpp +0 -38
  130. data/vendor/faiss/c_api/IndexShards_c.h +0 -39
  131. data/vendor/faiss/c_api/Index_c.cpp +0 -105
  132. data/vendor/faiss/c_api/Index_c.h +0 -183
  133. data/vendor/faiss/c_api/MetaIndexes_c.cpp +0 -49
  134. data/vendor/faiss/c_api/MetaIndexes_c.h +0 -49
  135. data/vendor/faiss/c_api/clone_index_c.cpp +0 -23
  136. data/vendor/faiss/c_api/clone_index_c.h +0 -32
  137. data/vendor/faiss/c_api/error_c.h +0 -42
  138. data/vendor/faiss/c_api/error_impl.cpp +0 -27
  139. data/vendor/faiss/c_api/error_impl.h +0 -16
  140. data/vendor/faiss/c_api/faiss_c.h +0 -58
  141. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +0 -98
  142. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +0 -56
  143. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +0 -52
  144. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +0 -68
  145. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +0 -17
  146. data/vendor/faiss/c_api/gpu/GpuIndex_c.h +0 -30
  147. data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +0 -38
  148. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +0 -86
  149. data/vendor/faiss/c_api/gpu/GpuResources_c.h +0 -66
  150. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +0 -54
  151. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +0 -53
  152. data/vendor/faiss/c_api/gpu/macros_impl.h +0 -42
  153. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +0 -220
  154. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +0 -149
  155. data/vendor/faiss/c_api/index_factory_c.cpp +0 -26
  156. data/vendor/faiss/c_api/index_factory_c.h +0 -30
  157. data/vendor/faiss/c_api/index_io_c.cpp +0 -42
  158. data/vendor/faiss/c_api/index_io_c.h +0 -50
  159. data/vendor/faiss/c_api/macros_impl.h +0 -110
  160. data/vendor/faiss/demos/demo_imi_flat.cpp +0 -154
  161. data/vendor/faiss/demos/demo_imi_pq.cpp +0 -203
  162. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +0 -151
  163. data/vendor/faiss/demos/demo_sift1M.cpp +0 -252
  164. data/vendor/faiss/demos/demo_weighted_kmeans.cpp +0 -185
  165. data/vendor/faiss/misc/test_blas.cpp +0 -87
  166. data/vendor/faiss/tests/test_binary_flat.cpp +0 -62
  167. data/vendor/faiss/tests/test_dealloc_invlists.cpp +0 -188
  168. data/vendor/faiss/tests/test_ivfpq_codec.cpp +0 -70
  169. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +0 -100
  170. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +0 -573
  171. data/vendor/faiss/tests/test_merge.cpp +0 -260
  172. data/vendor/faiss/tests/test_omp_threads.cpp +0 -14
  173. data/vendor/faiss/tests/test_ondisk_ivf.cpp +0 -225
  174. data/vendor/faiss/tests/test_pairs_decoding.cpp +0 -193
  175. data/vendor/faiss/tests/test_params_override.cpp +0 -236
  176. data/vendor/faiss/tests/test_pq_encoding.cpp +0 -98
  177. data/vendor/faiss/tests/test_sliding_ivf.cpp +0 -246
  178. data/vendor/faiss/tests/test_threaded_index.cpp +0 -253
  179. data/vendor/faiss/tests/test_transfer_invlists.cpp +0 -159
  180. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +0 -104
  181. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +0 -85
  182. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +0 -98
  183. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +0 -122
  184. data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +0 -104
@@ -1,66 +0,0 @@
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_AUTO_TUNE_C_H
12
- #define FAISS_AUTO_TUNE_C_H
13
-
14
- #include "faiss_c.h"
15
- #include "Index_c.h"
16
-
17
- #ifdef __cplusplus
18
- extern "C" {
19
- #endif
20
-
21
- /// possible values of a parameter, sorted from least to most expensive/accurate
22
- FAISS_DECLARE_CLASS(ParameterRange)
23
-
24
- FAISS_DECLARE_GETTER(ParameterRange, const char*, name)
25
-
26
- /// Getter for the values in the range. The output values are invalidated
27
- /// upon any other modification of the range.
28
- void faiss_ParameterRange_values(FaissParameterRange*, double**, size_t*);
29
-
30
- /** Uses a-priori knowledge on the Faiss indexes to extract tunable parameters.
31
- */
32
- FAISS_DECLARE_CLASS(ParameterSpace)
33
-
34
- FAISS_DECLARE_DESTRUCTOR(ParameterSpace)
35
-
36
- /// Parameter space default constructor
37
- int faiss_ParameterSpace_new(FaissParameterSpace** space);
38
-
39
- /// nb of combinations, = product of values sizes
40
- size_t faiss_ParameterSpace_n_combinations(const FaissParameterSpace*);
41
-
42
- /// get string representation of the combination
43
- /// by writing it to the given character buffer.
44
- /// A buffer size of 1000 ensures that the full name is collected.
45
- int faiss_ParameterSpace_combination_name(const FaissParameterSpace*, size_t, char*, size_t);
46
-
47
- /// set a combination of parameters described by a string
48
- int faiss_ParameterSpace_set_index_parameters(const FaissParameterSpace*, FaissIndex*, const char *);
49
-
50
- /// set a combination of parameters on an index
51
- int faiss_ParameterSpace_set_index_parameters_cno(const FaissParameterSpace*, FaissIndex*, size_t);
52
-
53
- /// set one of the parameters
54
- int faiss_ParameterSpace_set_index_parameter(const FaissParameterSpace*, FaissIndex*, const char *, double);
55
-
56
- /// print a description on stdout
57
- void faiss_ParameterSpace_display(const FaissParameterSpace*);
58
-
59
- /// add a new parameter (or return it if it exists)
60
- int faiss_ParameterSpace_add_range(FaissParameterSpace*, const char*, FaissParameterRange**);
61
-
62
- #ifdef __cplusplus
63
- }
64
- #endif
65
-
66
- #endif
@@ -1,145 +0,0 @@
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
- using faiss::ClusteringIterationStats;
23
-
24
- DEFINE_GETTER(Clustering, int, niter)
25
- DEFINE_GETTER(Clustering, int, nredo)
26
- DEFINE_GETTER(Clustering, int, verbose)
27
- DEFINE_GETTER(Clustering, int, spherical)
28
- DEFINE_GETTER(Clustering, int, update_index)
29
- DEFINE_GETTER(Clustering, int, frozen_centroids)
30
-
31
- DEFINE_GETTER(Clustering, int, min_points_per_centroid)
32
- DEFINE_GETTER(Clustering, int, max_points_per_centroid)
33
-
34
- DEFINE_GETTER(Clustering, int, seed)
35
-
36
- /// getter for d
37
- DEFINE_GETTER(Clustering, size_t, d)
38
-
39
- /// getter for k
40
- DEFINE_GETTER(Clustering, size_t, k)
41
-
42
- DEFINE_GETTER(ClusteringIterationStats, float, obj)
43
- DEFINE_GETTER(ClusteringIterationStats, double, time)
44
- DEFINE_GETTER(ClusteringIterationStats, double, time_search)
45
- DEFINE_GETTER(ClusteringIterationStats, double, imbalance_factor)
46
- DEFINE_GETTER(ClusteringIterationStats, int, nsplit)
47
-
48
- void faiss_ClusteringParameters_init(FaissClusteringParameters* params) {
49
- ClusteringParameters d;
50
- params->frozen_centroids = d.frozen_centroids;
51
- params->max_points_per_centroid = d.max_points_per_centroid;
52
- params->min_points_per_centroid = d.min_points_per_centroid;
53
- params->niter = d.niter;
54
- params->nredo = d.nredo;
55
- params->seed = d.seed;
56
- params->spherical = d.spherical;
57
- params->update_index = d.update_index;
58
- params->verbose = d.verbose;
59
- }
60
-
61
- // This conversion is required because the two types are not memory-compatible
62
- inline ClusteringParameters from_faiss_c(const FaissClusteringParameters* params) {
63
- ClusteringParameters o;
64
- o.frozen_centroids = params->frozen_centroids;
65
- o.max_points_per_centroid = params->max_points_per_centroid;
66
- o.min_points_per_centroid = params->min_points_per_centroid;
67
- o.niter = params->niter;
68
- o.nredo = params->nredo;
69
- o.seed = params->seed;
70
- o.spherical = params->spherical;
71
- o.update_index = params->update_index;
72
- o.verbose = params->verbose;
73
- return o;
74
- }
75
-
76
- /// getter for centroids (size = k * d)
77
- void faiss_Clustering_centroids(
78
- FaissClustering* clustering, float** centroids, size_t* size) {
79
- std::vector<float>& v = reinterpret_cast<Clustering*>(clustering)->centroids;
80
- if (centroids) {
81
- *centroids = v.data();
82
- }
83
- if (size) {
84
- *size = v.size();
85
- }
86
- }
87
-
88
- /// getter for iteration stats
89
- void faiss_Clustering_iteration_stats(
90
- FaissClustering* clustering, FaissClusteringIterationStats** iteration_stats, size_t* size) {
91
- std::vector<ClusteringIterationStats>& v = reinterpret_cast<Clustering*>(clustering)->iteration_stats;
92
- if (iteration_stats) {
93
- *iteration_stats = reinterpret_cast<FaissClusteringIterationStats*>(v.data());
94
- }
95
- if (size) {
96
- *size = v.size();
97
- }
98
- }
99
-
100
- /// the only mandatory parameters are k and d
101
- int faiss_Clustering_new(FaissClustering** p_clustering, int d, int k) {
102
- try {
103
- Clustering* c = new Clustering(d, k);
104
- *p_clustering = reinterpret_cast<FaissClustering*>(c);
105
- return 0;
106
- } CATCH_AND_HANDLE
107
- }
108
-
109
- int faiss_Clustering_new_with_params(
110
- FaissClustering** p_clustering, int d, int k, const FaissClusteringParameters* cp) {
111
- try {
112
- Clustering* c = new Clustering(d, k, from_faiss_c(cp));
113
- *p_clustering = reinterpret_cast<FaissClustering*>(c);
114
- return 0;
115
- } CATCH_AND_HANDLE
116
- }
117
-
118
- /// Index is used during the assignment stage
119
- int faiss_Clustering_train(
120
- FaissClustering* clustering, idx_t n, const float* x, FaissIndex* index) {
121
- try {
122
- reinterpret_cast<Clustering*>(clustering)->train(
123
- n, x, *reinterpret_cast<Index*>(index));
124
- return 0;
125
- } CATCH_AND_HANDLE
126
- }
127
-
128
- void faiss_Clustering_free(FaissClustering* clustering) {
129
- delete reinterpret_cast<Clustering*>(clustering);
130
- }
131
-
132
- int faiss_kmeans_clustering (size_t d, size_t n, size_t k,
133
- const float *x,
134
- float *centroids,
135
- float *q_error) {
136
- try {
137
- float out = faiss::kmeans_clustering(d, n, k, x, centroids);
138
- if (q_error) {
139
- *q_error = out;
140
- }
141
- return 0;
142
- } CATCH_AND_HANDLE
143
- }
144
-
145
- }
@@ -1,123 +0,0 @@
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 centroids 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
- FAISS_DECLARE_CLASS(ClusteringIterationStats)
79
- FAISS_DECLARE_GETTER(ClusteringIterationStats, float, obj)
80
- FAISS_DECLARE_GETTER(ClusteringIterationStats, double, time)
81
- FAISS_DECLARE_GETTER(ClusteringIterationStats, double, time_search)
82
- FAISS_DECLARE_GETTER(ClusteringIterationStats, double, imbalance_factor)
83
- FAISS_DECLARE_GETTER(ClusteringIterationStats, int, nsplit)
84
-
85
- /// getter for centroids (size = k * d)
86
- void faiss_Clustering_centroids(
87
- FaissClustering* clustering, float** centroids, size_t* size);
88
-
89
- /// getter for iteration stats
90
- void faiss_Clustering_iteration_stats(
91
- FaissClustering* clustering, FaissClusteringIterationStats** iteration_stats, size_t* size);
92
-
93
- /// the only mandatory parameters are k and d
94
- int faiss_Clustering_new(FaissClustering** p_clustering, int d, int k);
95
-
96
- int faiss_Clustering_new_with_params(
97
- FaissClustering** p_clustering, int d, int k, const FaissClusteringParameters* cp);
98
-
99
- int faiss_Clustering_train(
100
- FaissClustering* clustering, idx_t n, const float* x, FaissIndex* index);
101
-
102
- void faiss_Clustering_free(FaissClustering* clustering);
103
-
104
- /** simplified interface
105
- *
106
- * @param d dimension of the data
107
- * @param n nb of training vectors
108
- * @param k nb of output centroids
109
- * @param x training set (size n * d)
110
- * @param centroids output centroids (size k * d)
111
- * @param q_error final quantization error
112
- * @return error code
113
- */
114
- int faiss_kmeans_clustering (size_t d, size_t n, size_t k,
115
- const float *x,
116
- float *centroids,
117
- float *q_error);
118
-
119
- #ifdef __cplusplus
120
- }
121
- #endif
122
-
123
- #endif
@@ -1,140 +0,0 @@
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
- }