faiss 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +16 -4
  5. data/ext/faiss/ext.cpp +12 -308
  6. data/ext/faiss/extconf.rb +6 -3
  7. data/ext/faiss/index.cpp +189 -0
  8. data/ext/faiss/index_binary.cpp +75 -0
  9. data/ext/faiss/kmeans.cpp +40 -0
  10. data/ext/faiss/numo.hpp +867 -0
  11. data/ext/faiss/pca_matrix.cpp +33 -0
  12. data/ext/faiss/product_quantizer.cpp +53 -0
  13. data/ext/faiss/utils.cpp +13 -0
  14. data/ext/faiss/utils.h +5 -0
  15. data/lib/faiss.rb +0 -5
  16. data/lib/faiss/version.rb +1 -1
  17. data/vendor/faiss/faiss/AutoTune.cpp +36 -33
  18. data/vendor/faiss/faiss/AutoTune.h +6 -3
  19. data/vendor/faiss/faiss/Clustering.cpp +16 -12
  20. data/vendor/faiss/faiss/Index.cpp +3 -4
  21. data/vendor/faiss/faiss/Index.h +3 -3
  22. data/vendor/faiss/faiss/IndexBinary.cpp +3 -4
  23. data/vendor/faiss/faiss/IndexBinary.h +1 -1
  24. data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -12
  25. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +1 -2
  26. data/vendor/faiss/faiss/IndexFlat.cpp +0 -148
  27. data/vendor/faiss/faiss/IndexFlat.h +0 -51
  28. data/vendor/faiss/faiss/IndexHNSW.cpp +4 -5
  29. data/vendor/faiss/faiss/IndexIVF.cpp +118 -31
  30. data/vendor/faiss/faiss/IndexIVF.h +22 -15
  31. data/vendor/faiss/faiss/IndexIVFFlat.cpp +3 -3
  32. data/vendor/faiss/faiss/IndexIVFFlat.h +2 -1
  33. data/vendor/faiss/faiss/IndexIVFPQ.cpp +39 -15
  34. data/vendor/faiss/faiss/IndexIVFPQ.h +25 -9
  35. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
  37. data/vendor/faiss/faiss/IndexIVFPQR.cpp +8 -9
  38. data/vendor/faiss/faiss/IndexIVFPQR.h +2 -1
  39. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +1 -2
  40. data/vendor/faiss/faiss/IndexPQ.cpp +34 -18
  41. data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
  42. data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
  43. data/vendor/faiss/faiss/IndexPreTransform.cpp +47 -0
  44. data/vendor/faiss/faiss/IndexPreTransform.h +2 -0
  45. data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
  46. data/vendor/faiss/faiss/IndexRefine.h +73 -0
  47. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +2 -2
  48. data/vendor/faiss/faiss/IndexScalarQuantizer.h +1 -1
  49. data/vendor/faiss/faiss/gpu/GpuDistance.h +1 -1
  50. data/vendor/faiss/faiss/gpu/GpuIndex.h +16 -9
  51. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +8 -1
  52. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -11
  53. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +19 -2
  54. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +28 -2
  55. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +24 -14
  56. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +29 -2
  57. data/vendor/faiss/faiss/gpu/GpuResources.h +4 -0
  58. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +60 -27
  59. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +28 -6
  60. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
  61. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
  62. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +3 -3
  63. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +3 -2
  64. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
  65. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +7 -2
  66. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +5 -1
  67. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
  68. data/vendor/faiss/faiss/gpu/test/TestUtils.h +33 -0
  69. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -0
  70. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +6 -0
  71. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +5 -6
  72. data/vendor/faiss/faiss/gpu/utils/Timer.h +2 -2
  73. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +5 -4
  74. data/vendor/faiss/faiss/impl/HNSW.cpp +2 -4
  75. data/vendor/faiss/faiss/impl/PolysemousTraining.h +4 -4
  76. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +22 -12
  77. data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -0
  78. data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
  79. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -19
  80. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +6 -0
  81. data/vendor/faiss/faiss/impl/index_read.cpp +64 -96
  82. data/vendor/faiss/faiss/impl/index_write.cpp +34 -25
  83. data/vendor/faiss/faiss/impl/io.cpp +33 -2
  84. data/vendor/faiss/faiss/impl/io.h +7 -2
  85. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -15
  86. data/vendor/faiss/faiss/impl/platform_macros.h +44 -0
  87. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
  88. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
  89. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
  90. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
  91. data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
  92. data/vendor/faiss/faiss/index_factory.cpp +112 -7
  93. data/vendor/faiss/faiss/index_io.h +1 -48
  94. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
  95. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
  96. data/vendor/faiss/faiss/{DirectMap.cpp → invlists/DirectMap.cpp} +1 -1
  97. data/vendor/faiss/faiss/{DirectMap.h → invlists/DirectMap.h} +1 -1
  98. data/vendor/faiss/faiss/{InvertedLists.cpp → invlists/InvertedLists.cpp} +72 -1
  99. data/vendor/faiss/faiss/{InvertedLists.h → invlists/InvertedLists.h} +32 -1
  100. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
  101. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
  102. data/vendor/faiss/faiss/{OnDiskInvertedLists.cpp → invlists/OnDiskInvertedLists.cpp} +21 -6
  103. data/vendor/faiss/faiss/{OnDiskInvertedLists.h → invlists/OnDiskInvertedLists.h} +5 -2
  104. data/vendor/faiss/faiss/python/python_callbacks.h +8 -1
  105. data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
  106. data/vendor/faiss/faiss/utils/Heap.cpp +2 -4
  107. data/vendor/faiss/faiss/utils/Heap.h +61 -50
  108. data/vendor/faiss/faiss/utils/distances.cpp +164 -319
  109. data/vendor/faiss/faiss/utils/distances.h +28 -20
  110. data/vendor/faiss/faiss/utils/distances_simd.cpp +277 -49
  111. data/vendor/faiss/faiss/utils/extra_distances.cpp +1 -2
  112. data/vendor/faiss/faiss/utils/hamming-inl.h +4 -4
  113. data/vendor/faiss/faiss/utils/hamming.cpp +3 -6
  114. data/vendor/faiss/faiss/utils/hamming.h +2 -7
  115. data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
  116. data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
  117. data/vendor/faiss/faiss/utils/partitioning.h +69 -0
  118. data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
  119. data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
  120. data/vendor/faiss/faiss/utils/simdlib.h +31 -0
  121. data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
  122. data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
  123. metadata +54 -149
  124. data/lib/faiss/index.rb +0 -20
  125. data/lib/faiss/index_binary.rb +0 -20
  126. data/lib/faiss/kmeans.rb +0 -15
  127. data/lib/faiss/pca_matrix.rb +0 -15
  128. data/lib/faiss/product_quantizer.rb +0 -22
  129. data/vendor/faiss/benchs/bench_6bit_codec.cpp +0 -80
  130. data/vendor/faiss/c_api/AutoTune_c.cpp +0 -83
  131. data/vendor/faiss/c_api/AutoTune_c.h +0 -66
  132. data/vendor/faiss/c_api/Clustering_c.cpp +0 -145
  133. data/vendor/faiss/c_api/Clustering_c.h +0 -123
  134. data/vendor/faiss/c_api/IndexFlat_c.cpp +0 -140
  135. data/vendor/faiss/c_api/IndexFlat_c.h +0 -115
  136. data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +0 -64
  137. data/vendor/faiss/c_api/IndexIVFFlat_c.h +0 -58
  138. data/vendor/faiss/c_api/IndexIVF_c.cpp +0 -99
  139. data/vendor/faiss/c_api/IndexIVF_c.h +0 -142
  140. data/vendor/faiss/c_api/IndexLSH_c.cpp +0 -37
  141. data/vendor/faiss/c_api/IndexLSH_c.h +0 -40
  142. data/vendor/faiss/c_api/IndexPreTransform_c.cpp +0 -21
  143. data/vendor/faiss/c_api/IndexPreTransform_c.h +0 -32
  144. data/vendor/faiss/c_api/IndexShards_c.cpp +0 -38
  145. data/vendor/faiss/c_api/IndexShards_c.h +0 -39
  146. data/vendor/faiss/c_api/Index_c.cpp +0 -105
  147. data/vendor/faiss/c_api/Index_c.h +0 -183
  148. data/vendor/faiss/c_api/MetaIndexes_c.cpp +0 -49
  149. data/vendor/faiss/c_api/MetaIndexes_c.h +0 -49
  150. data/vendor/faiss/c_api/clone_index_c.cpp +0 -23
  151. data/vendor/faiss/c_api/clone_index_c.h +0 -32
  152. data/vendor/faiss/c_api/error_c.h +0 -42
  153. data/vendor/faiss/c_api/error_impl.cpp +0 -27
  154. data/vendor/faiss/c_api/error_impl.h +0 -16
  155. data/vendor/faiss/c_api/faiss_c.h +0 -58
  156. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +0 -98
  157. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +0 -56
  158. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +0 -52
  159. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +0 -68
  160. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +0 -17
  161. data/vendor/faiss/c_api/gpu/GpuIndex_c.h +0 -30
  162. data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +0 -38
  163. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +0 -86
  164. data/vendor/faiss/c_api/gpu/GpuResources_c.h +0 -66
  165. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +0 -54
  166. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +0 -53
  167. data/vendor/faiss/c_api/gpu/macros_impl.h +0 -42
  168. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +0 -220
  169. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +0 -149
  170. data/vendor/faiss/c_api/index_factory_c.cpp +0 -26
  171. data/vendor/faiss/c_api/index_factory_c.h +0 -30
  172. data/vendor/faiss/c_api/index_io_c.cpp +0 -42
  173. data/vendor/faiss/c_api/index_io_c.h +0 -50
  174. data/vendor/faiss/c_api/macros_impl.h +0 -110
  175. data/vendor/faiss/demos/demo_imi_flat.cpp +0 -154
  176. data/vendor/faiss/demos/demo_imi_pq.cpp +0 -203
  177. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +0 -151
  178. data/vendor/faiss/demos/demo_sift1M.cpp +0 -252
  179. data/vendor/faiss/demos/demo_weighted_kmeans.cpp +0 -185
  180. data/vendor/faiss/misc/test_blas.cpp +0 -87
  181. data/vendor/faiss/tests/test_binary_flat.cpp +0 -62
  182. data/vendor/faiss/tests/test_dealloc_invlists.cpp +0 -188
  183. data/vendor/faiss/tests/test_ivfpq_codec.cpp +0 -70
  184. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +0 -100
  185. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +0 -573
  186. data/vendor/faiss/tests/test_merge.cpp +0 -260
  187. data/vendor/faiss/tests/test_omp_threads.cpp +0 -14
  188. data/vendor/faiss/tests/test_ondisk_ivf.cpp +0 -225
  189. data/vendor/faiss/tests/test_pairs_decoding.cpp +0 -193
  190. data/vendor/faiss/tests/test_params_override.cpp +0 -236
  191. data/vendor/faiss/tests/test_pq_encoding.cpp +0 -98
  192. data/vendor/faiss/tests/test_sliding_ivf.cpp +0 -246
  193. data/vendor/faiss/tests/test_threaded_index.cpp +0 -253
  194. data/vendor/faiss/tests/test_transfer_invlists.cpp +0 -159
  195. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +0 -104
  196. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +0 -85
  197. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +0 -98
  198. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +0 -122
  199. data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +0 -104
@@ -1,83 +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 <cstring>
12
- #include "AutoTune.h"
13
- #include "AutoTune_c.h"
14
- #include "macros_impl.h"
15
-
16
- using faiss::Index;
17
- using faiss::ParameterRange;
18
- using faiss::ParameterSpace;
19
-
20
- const char* faiss_ParameterRange_name(const FaissParameterRange* range) {
21
- return reinterpret_cast<const ParameterRange*>(range)->name.c_str();
22
- }
23
-
24
- void faiss_ParameterRange_values(FaissParameterRange* range, double** p_values, size_t* p_size) {
25
- auto& values = reinterpret_cast<ParameterRange*>(range)->values;
26
- *p_values = values.data();
27
- *p_size = values.size();
28
- }
29
-
30
- int faiss_ParameterSpace_new(FaissParameterSpace** space) {
31
- try {
32
- auto new_space = new ParameterSpace();
33
- *space = reinterpret_cast<FaissParameterSpace*>(new_space);
34
- } CATCH_AND_HANDLE
35
- }
36
-
37
- DEFINE_DESTRUCTOR(ParameterSpace)
38
-
39
- size_t faiss_ParameterSpace_n_combinations(const FaissParameterSpace* space) {
40
- return reinterpret_cast<const ParameterSpace*>(space)->n_combinations();
41
- }
42
-
43
- int faiss_ParameterSpace_combination_name(const FaissParameterSpace* space, size_t cno, char* char_buffer, size_t size) {
44
- try {
45
- auto rep = reinterpret_cast<const ParameterSpace*>(space)->combination_name(cno);
46
- strncpy(char_buffer, rep.c_str(), size);
47
- } CATCH_AND_HANDLE
48
- }
49
-
50
- int faiss_ParameterSpace_set_index_parameters(const FaissParameterSpace* space, FaissIndex* cindex, const char* param_string) {
51
- try {
52
- auto index = reinterpret_cast<Index*>(cindex);
53
- reinterpret_cast<const ParameterSpace*>(space)->set_index_parameters(index, param_string);
54
- } CATCH_AND_HANDLE
55
- }
56
-
57
- /// set a combination of parameters on an index
58
- int faiss_ParameterSpace_set_index_parameters_cno(const FaissParameterSpace* space, FaissIndex* cindex, size_t cno) {
59
- try {
60
- auto index = reinterpret_cast<Index*>(cindex);
61
- reinterpret_cast<const ParameterSpace*>(space)->set_index_parameters(index, cno);
62
- } CATCH_AND_HANDLE
63
- }
64
-
65
- int faiss_ParameterSpace_set_index_parameter(const FaissParameterSpace* space, FaissIndex* cindex, const char * name, double value) {
66
- try {
67
- auto index = reinterpret_cast<Index*>(cindex);
68
- reinterpret_cast<const ParameterSpace*>(space)->set_index_parameter(index, name, value);
69
- } CATCH_AND_HANDLE
70
- }
71
-
72
- void faiss_ParameterSpace_display(const FaissParameterSpace* space) {
73
- reinterpret_cast<const ParameterSpace*>(space)->display();
74
- }
75
-
76
- int faiss_ParameterSpace_add_range(FaissParameterSpace* space, const char* name, FaissParameterRange** p_range) {
77
- try {
78
- ParameterRange& range = reinterpret_cast<ParameterSpace*>(space)->add_range(name);
79
- if (p_range) {
80
- *p_range = reinterpret_cast<FaissParameterRange*>(&range);
81
- }
82
- } CATCH_AND_HANDLE
83
- }
@@ -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
- }