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,42 @@
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 INDEXSHARDS_C_H
12
+ #define INDEXSHARDS_C_H
13
+
14
+ #include "faiss_c.h"
15
+ #include "Index_c.h"
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /** Index that concatenates the results from several sub-indexes
22
+ */
23
+ FAISS_DECLARE_CLASS_INHERITED(IndexShards, Index)
24
+
25
+ FAISS_DECLARE_GETTER_SETTER(IndexShards, int, own_fields)
26
+ FAISS_DECLARE_GETTER_SETTER(IndexShards, int, successive_ids)
27
+
28
+ int faiss_IndexShards_new(FaissIndexShards** p_index, idx_t d);
29
+
30
+ int faiss_IndexShards_new_with_options(FaissIndexShards** p_index, idx_t d, int threaded, int successive_ids);
31
+
32
+ int faiss_IndexShards_add_shard(FaissIndexShards* index, FaissIndex* shard);
33
+
34
+ /// update metric_type and ntotal
35
+ int faiss_IndexShards_sync_with_shard_indexes(FaissIndexShards* index);
36
+
37
+ FaissIndex* faiss_IndexShards_at(FaissIndexShards* index, int i);
38
+
39
+ #ifdef __cplusplus
40
+ }
41
+ #endif
42
+ #endif
@@ -0,0 +1,105 @@
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 "Index.h"
13
+ #include "macros_impl.h"
14
+
15
+ extern "C" {
16
+
17
+ DEFINE_DESTRUCTOR(Index)
18
+
19
+ DEFINE_GETTER(Index, int, d)
20
+
21
+ DEFINE_GETTER(Index, int, is_trained)
22
+
23
+ DEFINE_GETTER(Index, idx_t, ntotal)
24
+
25
+ DEFINE_GETTER(Index, FaissMetricType, metric_type)
26
+
27
+ int faiss_Index_train(FaissIndex* index, idx_t n, const float* x) {
28
+ try {
29
+ reinterpret_cast<faiss::Index*>(index)->train(n, x);
30
+ } CATCH_AND_HANDLE
31
+ }
32
+
33
+ int faiss_Index_add(FaissIndex* index, idx_t n, const float* x) {
34
+ try {
35
+ reinterpret_cast<faiss::Index*>(index)->add(n, x);
36
+ } CATCH_AND_HANDLE
37
+ }
38
+
39
+ int faiss_Index_add_with_ids(FaissIndex* index, idx_t n, const float* x, const idx_t* xids) {
40
+ try {
41
+ reinterpret_cast<faiss::Index*>(index)->add_with_ids(n, x, xids);
42
+ } CATCH_AND_HANDLE
43
+ }
44
+
45
+ int faiss_Index_search(const FaissIndex* index, idx_t n, const float* x, idx_t k,
46
+ float* distances, idx_t* labels) {
47
+ try {
48
+ reinterpret_cast<const faiss::Index*>(index)->search(n, x, k, distances, labels);
49
+ } CATCH_AND_HANDLE
50
+ }
51
+
52
+ int faiss_Index_range_search(const FaissIndex* index, idx_t n, const float* x, float radius,
53
+ FaissRangeSearchResult* result) {
54
+ try {
55
+ reinterpret_cast<const faiss::Index*>(index)->range_search(
56
+ n, x, radius, reinterpret_cast<faiss::RangeSearchResult*>(result));
57
+ } CATCH_AND_HANDLE
58
+ }
59
+
60
+ int faiss_Index_assign(FaissIndex* index, idx_t n, const float * x, idx_t * labels, idx_t k) {
61
+ try {
62
+ reinterpret_cast<faiss::Index*>(index)->assign(n, x, labels, k);
63
+ } CATCH_AND_HANDLE
64
+ }
65
+
66
+ int faiss_Index_reset(FaissIndex* index) {
67
+ try {
68
+ reinterpret_cast<faiss::Index*>(index)->reset();
69
+ } CATCH_AND_HANDLE
70
+ }
71
+
72
+ int faiss_Index_remove_ids(FaissIndex* index, const FaissIDSelector* sel, size_t* n_removed) {
73
+ try {
74
+ size_t n {reinterpret_cast<faiss::Index*>(index)->remove_ids(
75
+ *reinterpret_cast<const faiss::IDSelector*>(sel))};
76
+ if (n_removed) {
77
+ *n_removed = n;
78
+ }
79
+ } CATCH_AND_HANDLE
80
+ }
81
+
82
+ int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons) {
83
+ try {
84
+ reinterpret_cast<const faiss::Index*>(index)->reconstruct(key, recons);
85
+ } CATCH_AND_HANDLE
86
+ }
87
+
88
+ int faiss_Index_reconstruct_n (const FaissIndex* index, idx_t i0, idx_t ni, float* recons) {
89
+ try {
90
+ reinterpret_cast<const faiss::Index*>(index)->reconstruct_n(i0, ni, recons);
91
+ } CATCH_AND_HANDLE
92
+ }
93
+
94
+ int faiss_Index_compute_residual(const FaissIndex* index, const float* x, float* residual, idx_t key) {
95
+ try {
96
+ reinterpret_cast<const faiss::Index*>(index)->compute_residual(x, residual, key);
97
+ } CATCH_AND_HANDLE
98
+ }
99
+
100
+ int faiss_Index_compute_residual_n(const FaissIndex* index, idx_t n, const float* x, float* residuals, const idx_t* keys) {
101
+ try {
102
+ reinterpret_cast<const faiss::Index *>(index)->compute_residual_n(n, x, residuals, keys);
103
+ } CATCH_AND_HANDLE
104
+ }
105
+ }
@@ -0,0 +1,183 @@
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_C_H
12
+ #define FAISS_INDEX_C_H
13
+
14
+ #include <stddef.h>
15
+ #include "faiss_c.h"
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ // forward declaration required here
22
+ FAISS_DECLARE_CLASS(RangeSearchResult)
23
+
24
+ //typedef struct FaissRangeSearchResult_H FaissRangeSearchResult;
25
+ typedef struct FaissIDSelector_H FaissIDSelector;
26
+
27
+ /// Some algorithms support both an inner product version and a L2 search version.
28
+ typedef enum FaissMetricType {
29
+ METRIC_INNER_PRODUCT = 0, ///< maximum inner product search
30
+ METRIC_L2 = 1, ///< squared L2 search
31
+ METRIC_L1, ///< L1 (aka cityblock)
32
+ METRIC_Linf, ///< infinity distance
33
+ METRIC_Lp, ///< L_p distance, p is given by metric_arg
34
+
35
+ /// some additional metrics defined in scipy.spatial.distance
36
+ METRIC_Canberra = 20,
37
+ METRIC_BrayCurtis,
38
+ METRIC_JensenShannon,
39
+ } FaissMetricType;
40
+
41
+ /// Opaque type for referencing to an index object
42
+ FAISS_DECLARE_CLASS(Index)
43
+ FAISS_DECLARE_DESTRUCTOR(Index)
44
+
45
+ /// Getter for d
46
+ FAISS_DECLARE_GETTER(Index, int, d)
47
+
48
+ /// Getter for is_trained
49
+ FAISS_DECLARE_GETTER(Index, int, is_trained)
50
+
51
+ /// Getter for ntotal
52
+ FAISS_DECLARE_GETTER(Index, idx_t, ntotal)
53
+
54
+ /// Getter for metric_type
55
+ FAISS_DECLARE_GETTER(Index, FaissMetricType, metric_type)
56
+
57
+ /** Perform training on a representative set of vectors
58
+ *
59
+ * @param index opaque pointer to index object
60
+ * @param n nb of training vectors
61
+ * @param x training vecors, size n * d
62
+ */
63
+ int faiss_Index_train(FaissIndex* index, idx_t n, const float* x);
64
+
65
+ /** Add n vectors of dimension d to the index.
66
+ *
67
+ * Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
68
+ * This function slices the input vectors in chuncks smaller than
69
+ * blocksize_add and calls add_core.
70
+ * @param index opaque pointer to index object
71
+ * @param x input matrix, size n * d
72
+ */
73
+ int faiss_Index_add(FaissIndex* index, idx_t n, const float* x);
74
+
75
+ /** Same as add, but stores xids instead of sequential ids.
76
+ *
77
+ * The default implementation fails with an assertion, as it is
78
+ * not supported by all indexes.
79
+ *
80
+ * @param index opaque pointer to index object
81
+ * @param xids if non-null, ids to store for the vectors (size n)
82
+ */
83
+ int faiss_Index_add_with_ids(FaissIndex* index, idx_t n, const float* x, const idx_t* xids);
84
+
85
+ /** query n vectors of dimension d to the index.
86
+ *
87
+ * return at most k vectors. If there are not enough results for a
88
+ * query, the result array is padded with -1s.
89
+ *
90
+ * @param index opaque pointer to index object
91
+ * @param x input vectors to search, size n * d
92
+ * @param labels output labels of the NNs, size n*k
93
+ * @param distances output pairwise distances, size n*k
94
+ */
95
+ int faiss_Index_search(const FaissIndex* index, idx_t n, const float* x, idx_t k,
96
+ float* distances, idx_t* labels);
97
+
98
+ /** query n vectors of dimension d to the index.
99
+ *
100
+ * return all vectors with distance < radius. Note that many
101
+ * indexes do not implement the range_search (only the k-NN search
102
+ * is mandatory).
103
+ *
104
+ * @param index opaque pointer to index object
105
+ * @param x input vectors to search, size n * d
106
+ * @param radius search radius
107
+ * @param result result table
108
+ */
109
+ int faiss_Index_range_search(const FaissIndex* index, idx_t n, const float* x,
110
+ float radius, FaissRangeSearchResult* result);
111
+
112
+ /** return the indexes of the k vectors closest to the query x.
113
+ *
114
+ * This function is identical as search but only return labels of neighbors.
115
+ * @param index opaque pointer to index object
116
+ * @param x input vectors to search, size n * d
117
+ * @param labels output labels of the NNs, size n*k
118
+ */
119
+ int faiss_Index_assign(FaissIndex* index, idx_t n, const float * x, idx_t * labels, idx_t k);
120
+
121
+ /** removes all elements from the database.
122
+ * @param index opaque pointer to index object
123
+ */
124
+ int faiss_Index_reset(FaissIndex* index);
125
+
126
+ /** removes IDs from the index. Not supported by all indexes
127
+ * @param index opaque pointer to index object
128
+ * @param nremove output for the number of IDs removed
129
+ */
130
+ int faiss_Index_remove_ids(FaissIndex* index, const FaissIDSelector* sel, size_t* n_removed);
131
+
132
+ /** Reconstruct a stored vector (or an approximation if lossy coding)
133
+ *
134
+ * this function may not be defined for some indexes
135
+ * @param index opaque pointer to index object
136
+ * @param key id of the vector to reconstruct
137
+ * @param recons reconstucted vector (size d)
138
+ */
139
+ int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons);
140
+
141
+ /** Reconstruct vectors i0 to i0 + ni - 1
142
+ *
143
+ * this function may not be defined for some indexes
144
+ * @param index opaque pointer to index object
145
+ * @param recons reconstucted vector (size ni * d)
146
+ */
147
+ int faiss_Index_reconstruct_n (const FaissIndex* index, idx_t i0, idx_t ni, float* recons);
148
+
149
+ /** Computes a residual vector after indexing encoding.
150
+ *
151
+ * The residual vector is the difference between a vector and the
152
+ * reconstruction that can be decoded from its representation in
153
+ * the index. The residual can be used for multiple-stage indexing
154
+ * methods, like IndexIVF's methods.
155
+ *
156
+ * @param index opaque pointer to index object
157
+ * @param x input vector, size d
158
+ * @param residual output residual vector, size d
159
+ * @param key encoded index, as returned by search and assign
160
+ */
161
+ int faiss_Index_compute_residual(const FaissIndex* index, const float* x, float* residual, idx_t key);
162
+
163
+ /** Computes a residual vector after indexing encoding.
164
+ *
165
+ * The residual vector is the difference between a vector and the
166
+ * reconstruction that can be decoded from its representation in
167
+ * the index. The residual can be used for multiple-stage indexing
168
+ * methods, like IndexIVF's methods.
169
+ *
170
+ * @param index opaque pointer to index object
171
+ * @param n number of vectors
172
+ * @param x input vector, size (n x d)
173
+ * @param residuals output residual vectors, size (n x d)
174
+ * @param keys encoded index, as returned by search and assign
175
+ */
176
+ int faiss_Index_compute_residual_n(const FaissIndex* index, idx_t n, const float* x, float* residuals, const idx_t* keys);
177
+
178
+
179
+ #ifdef __cplusplus
180
+ }
181
+ #endif
182
+
183
+ #endif
@@ -0,0 +1,49 @@
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 "MetaIndexes_c.h"
12
+ #include "MetaIndexes.h"
13
+ #include "macros_impl.h"
14
+
15
+ using faiss::Index;
16
+ using faiss::IndexIDMap;
17
+ using faiss::IndexIDMap2;
18
+
19
+ DEFINE_GETTER(IndexIDMap, int, own_fields)
20
+ DEFINE_SETTER(IndexIDMap, int, own_fields)
21
+
22
+ int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index) {
23
+ try {
24
+ auto out = new IndexIDMap(reinterpret_cast<Index*>(index));
25
+ *p_index = reinterpret_cast<FaissIndexIDMap*>(out);
26
+ } CATCH_AND_HANDLE
27
+ }
28
+
29
+ void faiss_IndexIDMap_id_map(FaissIndexIDMap* index, idx_t** p_id_map, size_t* p_size) {
30
+ auto idx = reinterpret_cast<IndexIDMap*>(index);
31
+ if (p_id_map)
32
+ *p_id_map = idx->id_map.data();
33
+ if (p_size)
34
+ *p_size = idx->id_map.size();
35
+ }
36
+
37
+ int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index) {
38
+ try {
39
+ auto out = new IndexIDMap2(reinterpret_cast<Index*>(index));
40
+ *p_index = reinterpret_cast<FaissIndexIDMap2*>(out);
41
+ } CATCH_AND_HANDLE
42
+ }
43
+
44
+ int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index) {
45
+ try {
46
+ reinterpret_cast<IndexIDMap2*>(index)->construct_rev_map();
47
+ } CATCH_AND_HANDLE
48
+ }
49
+
@@ -0,0 +1,49 @@
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 METAINDEXES_C_H
12
+ #define METAINDEXES_C_H
13
+
14
+ #include "faiss_c.h"
15
+ #include "Index_c.h"
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /** Index that translates search results to ids */
22
+ FAISS_DECLARE_CLASS_INHERITED(IndexIDMap, Index)
23
+
24
+ FAISS_DECLARE_GETTER_SETTER(IndexIDMap, int, own_fields)
25
+
26
+ int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index);
27
+
28
+ /** get a pointer to the index map's internal ID vector (the `id_map` field). The
29
+ * outputs of this function become invalid after any operation that can modify the index.
30
+ *
31
+ * @param index opaque pointer to index object
32
+ * @param p_id_map output, the pointer to the beginning of `id_map`.
33
+ * @param p_size output, the current length of `id_map`.
34
+ */
35
+ void faiss_IndexIDMap_id_map(FaissIndexIDMap* index, idx_t** p_id_map, size_t* p_size);
36
+
37
+ /** same as IndexIDMap but also provides an efficient reconstruction
38
+ implementation via a 2-way index */
39
+ FAISS_DECLARE_CLASS_INHERITED(IndexIDMap2, IndexIDMap)
40
+
41
+ int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index);
42
+
43
+ /// make the rev_map from scratch
44
+ int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index);
45
+
46
+ #ifdef __cplusplus
47
+ }
48
+ #endif
49
+ #endif
@@ -0,0 +1,23 @@
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
+ // I/O code for indexes
11
+
12
+ #include "clone_index_c.h"
13
+ #include "clone_index.h"
14
+ #include "macros_impl.h"
15
+
16
+ using faiss::Index;
17
+
18
+ int faiss_clone_index (const FaissIndex *idx, FaissIndex **p_out) {
19
+ try {
20
+ auto out = faiss::clone_index(reinterpret_cast<const Index*>(idx));
21
+ *p_out = reinterpret_cast<FaissIndex*>(out);
22
+ } CATCH_AND_HANDLE
23
+ }