faiss 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +103 -3
  4. data/ext/faiss/ext.cpp +99 -32
  5. data/ext/faiss/extconf.rb +12 -2
  6. data/lib/faiss/ext.bundle +0 -0
  7. data/lib/faiss/index.rb +3 -3
  8. data/lib/faiss/index_binary.rb +3 -3
  9. data/lib/faiss/kmeans.rb +1 -1
  10. data/lib/faiss/pca_matrix.rb +2 -2
  11. data/lib/faiss/product_quantizer.rb +3 -3
  12. data/lib/faiss/version.rb +1 -1
  13. data/vendor/faiss/AutoTune.cpp +719 -0
  14. data/vendor/faiss/AutoTune.h +212 -0
  15. data/vendor/faiss/Clustering.cpp +261 -0
  16. data/vendor/faiss/Clustering.h +101 -0
  17. data/vendor/faiss/IVFlib.cpp +339 -0
  18. data/vendor/faiss/IVFlib.h +132 -0
  19. data/vendor/faiss/Index.cpp +171 -0
  20. data/vendor/faiss/Index.h +261 -0
  21. data/vendor/faiss/Index2Layer.cpp +437 -0
  22. data/vendor/faiss/Index2Layer.h +85 -0
  23. data/vendor/faiss/IndexBinary.cpp +77 -0
  24. data/vendor/faiss/IndexBinary.h +163 -0
  25. data/vendor/faiss/IndexBinaryFlat.cpp +83 -0
  26. data/vendor/faiss/IndexBinaryFlat.h +54 -0
  27. data/vendor/faiss/IndexBinaryFromFloat.cpp +78 -0
  28. data/vendor/faiss/IndexBinaryFromFloat.h +52 -0
  29. data/vendor/faiss/IndexBinaryHNSW.cpp +325 -0
  30. data/vendor/faiss/IndexBinaryHNSW.h +56 -0
  31. data/vendor/faiss/IndexBinaryIVF.cpp +671 -0
  32. data/vendor/faiss/IndexBinaryIVF.h +211 -0
  33. data/vendor/faiss/IndexFlat.cpp +508 -0
  34. data/vendor/faiss/IndexFlat.h +175 -0
  35. data/vendor/faiss/IndexHNSW.cpp +1090 -0
  36. data/vendor/faiss/IndexHNSW.h +170 -0
  37. data/vendor/faiss/IndexIVF.cpp +909 -0
  38. data/vendor/faiss/IndexIVF.h +353 -0
  39. data/vendor/faiss/IndexIVFFlat.cpp +502 -0
  40. data/vendor/faiss/IndexIVFFlat.h +118 -0
  41. data/vendor/faiss/IndexIVFPQ.cpp +1207 -0
  42. data/vendor/faiss/IndexIVFPQ.h +161 -0
  43. data/vendor/faiss/IndexIVFPQR.cpp +219 -0
  44. data/vendor/faiss/IndexIVFPQR.h +65 -0
  45. data/vendor/faiss/IndexIVFSpectralHash.cpp +331 -0
  46. data/vendor/faiss/IndexIVFSpectralHash.h +75 -0
  47. data/vendor/faiss/IndexLSH.cpp +225 -0
  48. data/vendor/faiss/IndexLSH.h +87 -0
  49. data/vendor/faiss/IndexLattice.cpp +143 -0
  50. data/vendor/faiss/IndexLattice.h +68 -0
  51. data/vendor/faiss/IndexPQ.cpp +1188 -0
  52. data/vendor/faiss/IndexPQ.h +199 -0
  53. data/vendor/faiss/IndexPreTransform.cpp +288 -0
  54. data/vendor/faiss/IndexPreTransform.h +91 -0
  55. data/vendor/faiss/IndexReplicas.cpp +123 -0
  56. data/vendor/faiss/IndexReplicas.h +76 -0
  57. data/vendor/faiss/IndexScalarQuantizer.cpp +317 -0
  58. data/vendor/faiss/IndexScalarQuantizer.h +127 -0
  59. data/vendor/faiss/IndexShards.cpp +317 -0
  60. data/vendor/faiss/IndexShards.h +100 -0
  61. data/vendor/faiss/InvertedLists.cpp +623 -0
  62. data/vendor/faiss/InvertedLists.h +334 -0
  63. data/vendor/faiss/LICENSE +21 -0
  64. data/vendor/faiss/MatrixStats.cpp +252 -0
  65. data/vendor/faiss/MatrixStats.h +62 -0
  66. data/vendor/faiss/MetaIndexes.cpp +351 -0
  67. data/vendor/faiss/MetaIndexes.h +126 -0
  68. data/vendor/faiss/OnDiskInvertedLists.cpp +674 -0
  69. data/vendor/faiss/OnDiskInvertedLists.h +127 -0
  70. data/vendor/faiss/VectorTransform.cpp +1157 -0
  71. data/vendor/faiss/VectorTransform.h +322 -0
  72. data/vendor/faiss/c_api/AutoTune_c.cpp +83 -0
  73. data/vendor/faiss/c_api/AutoTune_c.h +64 -0
  74. data/vendor/faiss/c_api/Clustering_c.cpp +139 -0
  75. data/vendor/faiss/c_api/Clustering_c.h +117 -0
  76. data/vendor/faiss/c_api/IndexFlat_c.cpp +140 -0
  77. data/vendor/faiss/c_api/IndexFlat_c.h +115 -0
  78. data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +64 -0
  79. data/vendor/faiss/c_api/IndexIVFFlat_c.h +58 -0
  80. data/vendor/faiss/c_api/IndexIVF_c.cpp +92 -0
  81. data/vendor/faiss/c_api/IndexIVF_c.h +135 -0
  82. data/vendor/faiss/c_api/IndexLSH_c.cpp +37 -0
  83. data/vendor/faiss/c_api/IndexLSH_c.h +40 -0
  84. data/vendor/faiss/c_api/IndexShards_c.cpp +44 -0
  85. data/vendor/faiss/c_api/IndexShards_c.h +42 -0
  86. data/vendor/faiss/c_api/Index_c.cpp +105 -0
  87. data/vendor/faiss/c_api/Index_c.h +183 -0
  88. data/vendor/faiss/c_api/MetaIndexes_c.cpp +49 -0
  89. data/vendor/faiss/c_api/MetaIndexes_c.h +49 -0
  90. data/vendor/faiss/c_api/clone_index_c.cpp +23 -0
  91. data/vendor/faiss/c_api/clone_index_c.h +32 -0
  92. data/vendor/faiss/c_api/error_c.h +42 -0
  93. data/vendor/faiss/c_api/error_impl.cpp +27 -0
  94. data/vendor/faiss/c_api/error_impl.h +16 -0
  95. data/vendor/faiss/c_api/faiss_c.h +58 -0
  96. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +96 -0
  97. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +56 -0
  98. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +52 -0
  99. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +68 -0
  100. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +17 -0
  101. data/vendor/faiss/c_api/gpu/GpuIndex_c.h +30 -0
  102. data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +38 -0
  103. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +86 -0
  104. data/vendor/faiss/c_api/gpu/GpuResources_c.h +66 -0
  105. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +54 -0
  106. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +53 -0
  107. data/vendor/faiss/c_api/gpu/macros_impl.h +42 -0
  108. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +220 -0
  109. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +149 -0
  110. data/vendor/faiss/c_api/index_factory_c.cpp +26 -0
  111. data/vendor/faiss/c_api/index_factory_c.h +30 -0
  112. data/vendor/faiss/c_api/index_io_c.cpp +42 -0
  113. data/vendor/faiss/c_api/index_io_c.h +50 -0
  114. data/vendor/faiss/c_api/macros_impl.h +110 -0
  115. data/vendor/faiss/clone_index.cpp +147 -0
  116. data/vendor/faiss/clone_index.h +38 -0
  117. data/vendor/faiss/demos/demo_imi_flat.cpp +151 -0
  118. data/vendor/faiss/demos/demo_imi_pq.cpp +199 -0
  119. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +146 -0
  120. data/vendor/faiss/demos/demo_sift1M.cpp +252 -0
  121. data/vendor/faiss/gpu/GpuAutoTune.cpp +95 -0
  122. data/vendor/faiss/gpu/GpuAutoTune.h +27 -0
  123. data/vendor/faiss/gpu/GpuCloner.cpp +403 -0
  124. data/vendor/faiss/gpu/GpuCloner.h +82 -0
  125. data/vendor/faiss/gpu/GpuClonerOptions.cpp +28 -0
  126. data/vendor/faiss/gpu/GpuClonerOptions.h +53 -0
  127. data/vendor/faiss/gpu/GpuDistance.h +52 -0
  128. data/vendor/faiss/gpu/GpuFaissAssert.h +29 -0
  129. data/vendor/faiss/gpu/GpuIndex.h +148 -0
  130. data/vendor/faiss/gpu/GpuIndexBinaryFlat.h +89 -0
  131. data/vendor/faiss/gpu/GpuIndexFlat.h +190 -0
  132. data/vendor/faiss/gpu/GpuIndexIVF.h +89 -0
  133. data/vendor/faiss/gpu/GpuIndexIVFFlat.h +85 -0
  134. data/vendor/faiss/gpu/GpuIndexIVFPQ.h +143 -0
  135. data/vendor/faiss/gpu/GpuIndexIVFScalarQuantizer.h +100 -0
  136. data/vendor/faiss/gpu/GpuIndicesOptions.h +30 -0
  137. data/vendor/faiss/gpu/GpuResources.cpp +52 -0
  138. data/vendor/faiss/gpu/GpuResources.h +73 -0
  139. data/vendor/faiss/gpu/StandardGpuResources.cpp +295 -0
  140. data/vendor/faiss/gpu/StandardGpuResources.h +114 -0
  141. data/vendor/faiss/gpu/impl/RemapIndices.cpp +43 -0
  142. data/vendor/faiss/gpu/impl/RemapIndices.h +24 -0
  143. data/vendor/faiss/gpu/perf/IndexWrapper-inl.h +71 -0
  144. data/vendor/faiss/gpu/perf/IndexWrapper.h +39 -0
  145. data/vendor/faiss/gpu/perf/PerfClustering.cpp +115 -0
  146. data/vendor/faiss/gpu/perf/PerfIVFPQAdd.cpp +139 -0
  147. data/vendor/faiss/gpu/perf/WriteIndex.cpp +102 -0
  148. data/vendor/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +130 -0
  149. data/vendor/faiss/gpu/test/TestGpuIndexFlat.cpp +371 -0
  150. data/vendor/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +550 -0
  151. data/vendor/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +450 -0
  152. data/vendor/faiss/gpu/test/TestGpuMemoryException.cpp +84 -0
  153. data/vendor/faiss/gpu/test/TestUtils.cpp +315 -0
  154. data/vendor/faiss/gpu/test/TestUtils.h +93 -0
  155. data/vendor/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +159 -0
  156. data/vendor/faiss/gpu/utils/DeviceMemory.cpp +77 -0
  157. data/vendor/faiss/gpu/utils/DeviceMemory.h +71 -0
  158. data/vendor/faiss/gpu/utils/DeviceUtils.h +185 -0
  159. data/vendor/faiss/gpu/utils/MemorySpace.cpp +89 -0
  160. data/vendor/faiss/gpu/utils/MemorySpace.h +44 -0
  161. data/vendor/faiss/gpu/utils/StackDeviceMemory.cpp +239 -0
  162. data/vendor/faiss/gpu/utils/StackDeviceMemory.h +129 -0
  163. data/vendor/faiss/gpu/utils/StaticUtils.h +83 -0
  164. data/vendor/faiss/gpu/utils/Timer.cpp +60 -0
  165. data/vendor/faiss/gpu/utils/Timer.h +52 -0
  166. data/vendor/faiss/impl/AuxIndexStructures.cpp +305 -0
  167. data/vendor/faiss/impl/AuxIndexStructures.h +246 -0
  168. data/vendor/faiss/impl/FaissAssert.h +95 -0
  169. data/vendor/faiss/impl/FaissException.cpp +66 -0
  170. data/vendor/faiss/impl/FaissException.h +71 -0
  171. data/vendor/faiss/impl/HNSW.cpp +818 -0
  172. data/vendor/faiss/impl/HNSW.h +275 -0
  173. data/vendor/faiss/impl/PolysemousTraining.cpp +953 -0
  174. data/vendor/faiss/impl/PolysemousTraining.h +158 -0
  175. data/vendor/faiss/impl/ProductQuantizer.cpp +876 -0
  176. data/vendor/faiss/impl/ProductQuantizer.h +242 -0
  177. data/vendor/faiss/impl/ScalarQuantizer.cpp +1628 -0
  178. data/vendor/faiss/impl/ScalarQuantizer.h +120 -0
  179. data/vendor/faiss/impl/ThreadedIndex-inl.h +192 -0
  180. data/vendor/faiss/impl/ThreadedIndex.h +80 -0
  181. data/vendor/faiss/impl/index_read.cpp +793 -0
  182. data/vendor/faiss/impl/index_write.cpp +558 -0
  183. data/vendor/faiss/impl/io.cpp +142 -0
  184. data/vendor/faiss/impl/io.h +98 -0
  185. data/vendor/faiss/impl/lattice_Zn.cpp +712 -0
  186. data/vendor/faiss/impl/lattice_Zn.h +199 -0
  187. data/vendor/faiss/index_factory.cpp +392 -0
  188. data/vendor/faiss/index_factory.h +25 -0
  189. data/vendor/faiss/index_io.h +75 -0
  190. data/vendor/faiss/misc/test_blas.cpp +84 -0
  191. data/vendor/faiss/tests/test_binary_flat.cpp +64 -0
  192. data/vendor/faiss/tests/test_dealloc_invlists.cpp +183 -0
  193. data/vendor/faiss/tests/test_ivfpq_codec.cpp +67 -0
  194. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +98 -0
  195. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +566 -0
  196. data/vendor/faiss/tests/test_merge.cpp +258 -0
  197. data/vendor/faiss/tests/test_omp_threads.cpp +14 -0
  198. data/vendor/faiss/tests/test_ondisk_ivf.cpp +220 -0
  199. data/vendor/faiss/tests/test_pairs_decoding.cpp +189 -0
  200. data/vendor/faiss/tests/test_params_override.cpp +231 -0
  201. data/vendor/faiss/tests/test_pq_encoding.cpp +98 -0
  202. data/vendor/faiss/tests/test_sliding_ivf.cpp +240 -0
  203. data/vendor/faiss/tests/test_threaded_index.cpp +253 -0
  204. data/vendor/faiss/tests/test_transfer_invlists.cpp +159 -0
  205. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +98 -0
  206. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +81 -0
  207. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +93 -0
  208. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +119 -0
  209. data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +99 -0
  210. data/vendor/faiss/utils/Heap.cpp +122 -0
  211. data/vendor/faiss/utils/Heap.h +495 -0
  212. data/vendor/faiss/utils/WorkerThread.cpp +126 -0
  213. data/vendor/faiss/utils/WorkerThread.h +61 -0
  214. data/vendor/faiss/utils/distances.cpp +765 -0
  215. data/vendor/faiss/utils/distances.h +243 -0
  216. data/vendor/faiss/utils/distances_simd.cpp +809 -0
  217. data/vendor/faiss/utils/extra_distances.cpp +336 -0
  218. data/vendor/faiss/utils/extra_distances.h +54 -0
  219. data/vendor/faiss/utils/hamming-inl.h +472 -0
  220. data/vendor/faiss/utils/hamming.cpp +792 -0
  221. data/vendor/faiss/utils/hamming.h +220 -0
  222. data/vendor/faiss/utils/random.cpp +192 -0
  223. data/vendor/faiss/utils/random.h +60 -0
  224. data/vendor/faiss/utils/utils.cpp +783 -0
  225. data/vendor/faiss/utils/utils.h +181 -0
  226. 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
+ }