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,32 @@
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
+
13
+ #ifndef FAISS_CLONE_INDEX_C_H
14
+ #define FAISS_CLONE_INDEX_C_H
15
+
16
+ #include <stdio.h>
17
+ #include "faiss_c.h"
18
+ #include "Index_c.h"
19
+
20
+ #ifdef __cplusplus
21
+ extern "C" {
22
+ #endif
23
+
24
+ /* cloning functions */
25
+
26
+ /** Clone an index. This is equivalent to `faiss::clone_index` */
27
+ int faiss_clone_index (const FaissIndex *, FaissIndex ** p_out);
28
+
29
+ #ifdef __cplusplus
30
+ }
31
+ #endif
32
+ #endif
@@ -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 FAISS_ERROR_C_H
12
+ #define FAISS_ERROR_C_H
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ /// An error code which depends on the exception thrown from the previous
19
+ /// operation. See `faiss_get_last_error` to retrieve the error message.
20
+ typedef enum FaissErrorCode {
21
+ /// No error
22
+ OK = 0,
23
+ /// Any exception other than Faiss or standard C++ library exceptions
24
+ UNKNOWN_EXCEPT = -1,
25
+ /// Faiss library exception
26
+ FAISS_EXCEPT = -2,
27
+ /// Standard C++ library exception
28
+ STD_EXCEPT = -4
29
+ } FaissErrorCode;
30
+
31
+ /**
32
+ * Get the error message of the last failed operation performed by Faiss.
33
+ * The given pointer is only invalid until another Faiss function is
34
+ * called.
35
+ */
36
+ const char* faiss_get_last_error();
37
+
38
+ #ifdef __cplusplus
39
+ }
40
+ #endif
41
+
42
+ #endif
@@ -0,0 +1,27 @@
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 "error_c.h"
12
+ #include "error_impl.h"
13
+ #include "FaissException.h"
14
+ #include <exception>
15
+
16
+ thread_local std::exception_ptr faiss_last_exception;
17
+
18
+ const char* faiss_get_last_error() {
19
+ if (faiss_last_exception) {
20
+ try {
21
+ std::rethrow_exception(faiss_last_exception);
22
+ } catch (std::exception& e) {
23
+ return e.what();
24
+ }
25
+ }
26
+ return nullptr;
27
+ }
@@ -0,0 +1,16 @@
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 <exception>
12
+
13
+ /** global variable for holding the last exception thrown by
14
+ * calls to Faiss functions through the C API
15
+ */
16
+ extern thread_local std::exception_ptr faiss_last_exception;
@@ -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
+ /// Macros and typedefs for C wrapper API declarations
12
+
13
+ #ifndef FAISS_C_H
14
+ #define FAISS_C_H
15
+
16
+ #include <stdint.h>
17
+
18
+ typedef int64_t faiss_idx_t; ///< all indices are this type
19
+ typedef faiss_idx_t idx_t;
20
+ typedef float faiss_component_t; ///< all vector components are this type
21
+ typedef float faiss_distance_t; ///< all distances between vectors are this type
22
+
23
+ /// Declare an opaque type for a class type `clazz`.
24
+ #define FAISS_DECLARE_CLASS(clazz) \
25
+ typedef struct Faiss ## clazz ## _H Faiss ## clazz;
26
+
27
+ /// Declare an opaque type for a class type `clazz`, while
28
+ /// actually aliasing it to an existing parent class type `parent`.
29
+ #define FAISS_DECLARE_CLASS_INHERITED(clazz, parent) \
30
+ typedef struct Faiss ## parent ## _H Faiss ## clazz;
31
+
32
+ /// Declare a dynamic downcast operation from a base `FaissIndex*` pointer
33
+ /// type to a more specific index type. The function returns the same pointer
34
+ /// if the downcast is valid, and `NULL` otherwise.
35
+ #define FAISS_DECLARE_INDEX_DOWNCAST(clazz) \
36
+ Faiss ## clazz * faiss_ ## clazz ## _cast (FaissIndex*);
37
+
38
+ /// Declare a getter for the field `name` in class `clazz`,
39
+ /// of return type `ty`
40
+ #define FAISS_DECLARE_GETTER(clazz, ty, name) \
41
+ ty faiss_ ## clazz ## _ ## name (const Faiss ## clazz *);
42
+
43
+ /// Declare a setter for the field `name` in class `clazz`,
44
+ /// in which the user provides a value of type `ty`
45
+ #define FAISS_DECLARE_SETTER(clazz, ty, name) \
46
+ void faiss_ ## clazz ## _set_ ## name (Faiss ## clazz *, ty);
47
+
48
+ /// Declare a getter and setter for the field `name` in class `clazz`.
49
+ #define FAISS_DECLARE_GETTER_SETTER(clazz, ty, name) \
50
+ FAISS_DECLARE_GETTER(clazz, ty, name) \
51
+ FAISS_DECLARE_SETTER(clazz, ty, name)
52
+
53
+ /// Declare a destructor function which frees an object of
54
+ /// type `clazz`.
55
+ #define FAISS_DECLARE_DESTRUCTOR(clazz) \
56
+ void faiss_ ## clazz ## _free (Faiss ## clazz *obj);
57
+
58
+ #endif
@@ -0,0 +1,96 @@
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 "GpuAutoTune_c.h"
12
+ #include "GpuClonerOptions_c.h"
13
+ #include "macros_impl.h"
14
+ #include "Index.h"
15
+ #include "gpu/GpuAutoTune.h"
16
+ #include "gpu/GpuClonerOptions.h"
17
+ #include <vector>
18
+
19
+ using faiss::Index;
20
+ using faiss::gpu::GpuResources;
21
+ using faiss::gpu::GpuClonerOptions;
22
+ using faiss::gpu::GpuMultipleClonerOptions;
23
+
24
+ int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out) {
25
+ try {
26
+ auto cpu_index = faiss::gpu::index_gpu_to_cpu(
27
+ reinterpret_cast<const Index*>(gpu_index)
28
+ );
29
+ *p_out = reinterpret_cast<FaissIndex*>(cpu_index);
30
+ } CATCH_AND_HANDLE
31
+ }
32
+
33
+ /// converts any CPU index that can be converted to GPU
34
+ int faiss_index_cpu_to_gpu(FaissGpuResources* resources, int device, const FaissIndex *index, FaissGpuIndex** p_out) {
35
+ try {
36
+ auto res = reinterpret_cast<GpuResources*>(resources);
37
+ auto gpu_index = faiss::gpu::index_cpu_to_gpu(
38
+ res, device, reinterpret_cast<const Index*>(index)
39
+ );
40
+ *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
41
+ } CATCH_AND_HANDLE
42
+ }
43
+
44
+ int faiss_index_cpu_to_gpu_with_options(
45
+ FaissGpuResources* resources, int device,
46
+ const FaissIndex *index, const FaissGpuClonerOptions* options,
47
+ FaissGpuIndex** p_out)
48
+ {
49
+ try {
50
+ auto res = reinterpret_cast<GpuResources*>(resources);
51
+ auto gpu_index = faiss::gpu::index_cpu_to_gpu(
52
+ res, device, reinterpret_cast<const Index*>(index),
53
+ reinterpret_cast<const GpuClonerOptions*>(options));
54
+ *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
55
+ } CATCH_AND_HANDLE
56
+ }
57
+
58
+ int faiss_index_cpu_to_gpu_multiple(
59
+ FaissGpuResources* const* resources_vec,
60
+ const int* devices, size_t devices_size,
61
+ const FaissIndex* index, FaissGpuIndex** p_out)
62
+ {
63
+ try {
64
+ std::vector<GpuResources*> res(devices_size);
65
+ for (auto i = 0u; i < devices_size; ++i) {
66
+ res[i] = reinterpret_cast<GpuResources*>(resources_vec[i]);
67
+ }
68
+
69
+ std::vector<int> dev(devices, devices + devices_size);
70
+
71
+ auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple(
72
+ res, dev, reinterpret_cast<const Index*>(index));
73
+ *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
74
+ } CATCH_AND_HANDLE
75
+ }
76
+
77
+ int faiss_index_cpu_to_gpu_multiple_with_options(
78
+ FaissGpuResources** resources_vec, size_t resources_vec_size,
79
+ int* devices, size_t devices_size,
80
+ const FaissIndex* index, const FaissGpuMultipleClonerOptions* options,
81
+ FaissGpuIndex** p_out)
82
+ {
83
+ try {
84
+ std::vector<GpuResources*> res(resources_vec_size);
85
+ for (auto i = 0u; i < resources_vec_size; ++i) {
86
+ res[i] = reinterpret_cast<GpuResources*>(resources_vec[i]);
87
+ }
88
+
89
+ std::vector<int> dev(devices, devices + devices_size);
90
+
91
+ auto gpu_index = faiss::gpu::index_cpu_to_gpu_multiple(
92
+ res, dev, reinterpret_cast<const Index*>(index),
93
+ reinterpret_cast<const GpuMultipleClonerOptions*>(options));
94
+ *p_out = reinterpret_cast<FaissGpuIndex*>(gpu_index);
95
+ } CATCH_AND_HANDLE
96
+ }
@@ -0,0 +1,56 @@
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_GPU_AUTO_TUNE_C_H
12
+ #define FAISS_GPU_AUTO_TUNE_C_H
13
+
14
+ #include <stddef.h>
15
+ #include "faiss_c.h"
16
+ #include "GpuClonerOptions_c.h"
17
+ #include "GpuResources_c.h"
18
+ #include "GpuIndex_c.h"
19
+ #include "Index_c.h"
20
+
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ /// converts any GPU index inside gpu_index to a CPU index
26
+ int faiss_index_gpu_to_cpu(const FaissIndex* gpu_index, FaissIndex** p_out);
27
+
28
+ /// converts any CPU index that can be converted to GPU
29
+ int faiss_index_cpu_to_gpu(
30
+ FaissGpuResources* resources, int device,
31
+ const FaissIndex *index, FaissGpuIndex** p_out);
32
+
33
+ /// converts any CPU index that can be converted to GPU
34
+ int faiss_index_cpu_to_gpu_with_options(
35
+ FaissGpuResources* resources, int device,
36
+ const FaissIndex *index, const FaissGpuClonerOptions* options,
37
+ FaissGpuIndex** p_out);
38
+
39
+ /// converts any CPU index that can be converted to GPU
40
+ int faiss_index_cpu_to_gpu_multiple(
41
+ FaissGpuResources* const* resources_vec, const int* devices, size_t devices_size,
42
+ const FaissIndex* index, FaissGpuIndex** p_out);
43
+
44
+ /// converts any CPU index that can be converted to GPU
45
+ int faiss_index_cpu_to_gpu_multiple_with_options(
46
+ FaissGpuResources* const* resources_vec, const int* devices, size_t devices_size,
47
+ const FaissIndex* index, const FaissGpuMultipleClonerOptions* options,
48
+ FaissGpuIndex** p_out);
49
+
50
+ /// parameter space and setters for GPU indexes
51
+ FAISS_DECLARE_CLASS_INHERITED(GpuParameterSpace, ParameterSpace)
52
+
53
+ #ifdef __cplusplus
54
+ }
55
+ #endif
56
+ #endif
@@ -0,0 +1,52 @@
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 "GpuClonerOptions_c.h"
12
+ #include "gpu/GpuClonerOptions.h"
13
+ #include "macros_impl.h"
14
+
15
+ using faiss::gpu::IndicesOptions;
16
+ using faiss::gpu::GpuClonerOptions;
17
+ using faiss::gpu::GpuMultipleClonerOptions;
18
+
19
+ int faiss_GpuClonerOptions_new(FaissGpuClonerOptions** p) {
20
+ try {
21
+ *p = reinterpret_cast<FaissGpuClonerOptions*>(new GpuClonerOptions());
22
+ } CATCH_AND_HANDLE
23
+ }
24
+
25
+ int faiss_GpuMultipleClonerOptions_new(FaissGpuMultipleClonerOptions** p) {
26
+ try {
27
+ *p = reinterpret_cast<FaissGpuMultipleClonerOptions*>(new GpuMultipleClonerOptions());
28
+ } CATCH_AND_HANDLE
29
+ }
30
+
31
+ DEFINE_DESTRUCTOR(GpuClonerOptions)
32
+ DEFINE_DESTRUCTOR(GpuMultipleClonerOptions)
33
+
34
+ DEFINE_GETTER(GpuClonerOptions, FaissIndicesOptions, indicesOptions)
35
+ DEFINE_GETTER(GpuClonerOptions, int, useFloat16CoarseQuantizer)
36
+ DEFINE_GETTER(GpuClonerOptions, int, useFloat16)
37
+ DEFINE_GETTER(GpuClonerOptions, int, usePrecomputed)
38
+ DEFINE_GETTER(GpuClonerOptions, long, reserveVecs)
39
+ DEFINE_GETTER(GpuClonerOptions, int, storeTransposed)
40
+ DEFINE_GETTER(GpuClonerOptions, int, verbose)
41
+ DEFINE_GETTER(GpuMultipleClonerOptions, int, shard)
42
+ DEFINE_GETTER(GpuMultipleClonerOptions, int, shard_type)
43
+
44
+ DEFINE_SETTER_STATIC(GpuClonerOptions, IndicesOptions, FaissIndicesOptions, indicesOptions)
45
+ DEFINE_SETTER_STATIC(GpuClonerOptions, bool, int, useFloat16CoarseQuantizer)
46
+ DEFINE_SETTER_STATIC(GpuClonerOptions, bool, int, useFloat16)
47
+ DEFINE_SETTER_STATIC(GpuClonerOptions, bool, int, usePrecomputed)
48
+ DEFINE_SETTER(GpuClonerOptions, long, reserveVecs)
49
+ DEFINE_SETTER_STATIC(GpuClonerOptions, bool, int, storeTransposed)
50
+ DEFINE_SETTER_STATIC(GpuClonerOptions, bool, int, verbose)
51
+ DEFINE_SETTER_STATIC(GpuMultipleClonerOptions, bool, int, shard)
52
+ DEFINE_SETTER(GpuMultipleClonerOptions, int, shard_type)
@@ -0,0 +1,68 @@
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_GPU_CLONER_OPTIONS_C_H
12
+ #define FAISS_GPU_CLONER_OPTIONS_C_H
13
+
14
+ #include "faiss_c.h"
15
+ #include "GpuIndicesOptions_c.h"
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ FAISS_DECLARE_CLASS(GpuClonerOptions)
22
+
23
+ FAISS_DECLARE_DESTRUCTOR(GpuClonerOptions)
24
+
25
+ /// Default constructor for GpuClonerOptions
26
+ int faiss_GpuClonerOptions_new(FaissGpuClonerOptions**);
27
+
28
+ /// how should indices be stored on index types that support indices
29
+ /// (anything but GpuIndexFlat*)?
30
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, FaissIndicesOptions, indicesOptions)
31
+
32
+ /// (boolean) is the coarse quantizer in float16?
33
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, useFloat16CoarseQuantizer)
34
+
35
+ /// (boolean) for GpuIndexIVFFlat, is storage in float16?
36
+ /// for GpuIndexIVFPQ, are intermediate calculations in float16?
37
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, useFloat16)
38
+
39
+ /// (boolean) use precomputed tables?
40
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, usePrecomputed)
41
+
42
+ /// reserve vectors in the invfiles?
43
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, long, reserveVecs)
44
+
45
+ /// (boolean) For GpuIndexFlat, store data in transposed layout?
46
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, storeTransposed)
47
+
48
+ /// (boolean) Set verbose options on the index
49
+ FAISS_DECLARE_GETTER_SETTER(GpuClonerOptions, int, verbose)
50
+
51
+ FAISS_DECLARE_CLASS_INHERITED(GpuMultipleClonerOptions, GpuClonerOptions)
52
+
53
+ FAISS_DECLARE_DESTRUCTOR(GpuMultipleClonerOptions)
54
+
55
+ /// Default constructor for GpuMultipleClonerOptions
56
+ int faiss_GpuMultipleClonerOptions_new(FaissGpuMultipleClonerOptions**);
57
+
58
+ /// (boolean) Whether to shard the index across GPUs, versus replication
59
+ /// across GPUs
60
+ FAISS_DECLARE_GETTER_SETTER(GpuMultipleClonerOptions, int, shard)
61
+
62
+ /// IndexIVF::copy_subset_to subset type
63
+ FAISS_DECLARE_GETTER_SETTER(GpuMultipleClonerOptions, int, shard_type)
64
+
65
+ #ifdef __cplusplus
66
+ }
67
+ #endif
68
+ #endif