faiss 0.1.0 → 0.1.1

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 (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,17 @@
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 "gpu/GpuIndex.h"
12
+ #include "GpuIndex_c.h"
13
+ #include "macros_impl.h"
14
+
15
+ using faiss::gpu::GpuIndexConfig;
16
+
17
+ DEFINE_GETTER(GpuIndexConfig, int, device)
@@ -0,0 +1,30 @@
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_INDEX_C_H
12
+ #define FAISS_GPU_INDEX_C_H
13
+
14
+ #include "faiss_c.h"
15
+
16
+ #ifdef __cplusplus
17
+ extern "C" {
18
+ #endif
19
+
20
+ FAISS_DECLARE_CLASS(GpuIndexConfig)
21
+
22
+ FAISS_DECLARE_GETTER(GpuIndexConfig, int, device)
23
+
24
+ FAISS_DECLARE_CLASS_INHERITED(GpuIndex, Index)
25
+
26
+ #ifdef __cplusplus
27
+ }
28
+ #endif
29
+
30
+ #endif
@@ -0,0 +1,38 @@
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_INDICES_OPTIONS_C_H
12
+ #define FAISS_GPU_INDICES_OPTIONS_C_H
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ /// How user vector index data is stored on the GPU
19
+ typedef enum FaissIndicesOptions {
20
+ /// The user indices are only stored on the CPU; the GPU returns
21
+ /// (inverted list, offset) to the CPU which is then translated to
22
+ /// the real user index.
23
+ INDICES_CPU = 0,
24
+ /// The indices are not stored at all, on either the CPU or
25
+ /// GPU. Only (inverted list, offset) is returned to the user as the
26
+ /// index.
27
+ INDICES_IVF = 1,
28
+ /// Indices are stored as 32 bit integers on the GPU, but returned
29
+ /// as 64 bit integers
30
+ INDICES_32_BIT = 2,
31
+ /// Indices are stored as 64 bit integers on the GPU
32
+ INDICES_64_BIT = 3,
33
+ } FaissIndicesOptions;
34
+
35
+ #ifdef __cplusplus
36
+ }
37
+ #endif
38
+ #endif
@@ -0,0 +1,86 @@
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 "gpu/GpuResources_c.h"
12
+ #include "gpu/GpuResources.h"
13
+ #include "macros_impl.h"
14
+
15
+ using faiss::gpu::GpuResources;
16
+
17
+ DEFINE_DESTRUCTOR(GpuResources)
18
+
19
+ int faiss_GpuResources_initializeForDevice(FaissGpuResources* res, int device) {
20
+ try {
21
+ reinterpret_cast<GpuResources*>(res)->initializeForDevice(device);
22
+ } CATCH_AND_HANDLE
23
+ }
24
+
25
+ int faiss_GpuResources_getBlasHandle(FaissGpuResources* res, int device, cublasHandle_t* out) {
26
+ try {
27
+ auto o = reinterpret_cast<GpuResources*>(res)->getBlasHandle(device);
28
+ *out = o;
29
+ } CATCH_AND_HANDLE
30
+ }
31
+
32
+ int faiss_GpuResources_getDefaultStream(FaissGpuResources* res, int device, cudaStream_t* out) {
33
+ try {
34
+ auto o = reinterpret_cast<GpuResources*>(res)->getDefaultStream(device);
35
+ *out = o;
36
+ } CATCH_AND_HANDLE
37
+ }
38
+
39
+ int faiss_GpuResources_getPinnedMemory(FaissGpuResources* res, void** p_buffer, size_t* p_size) {
40
+ try {
41
+ auto o = reinterpret_cast<GpuResources*>(res)->getPinnedMemory();
42
+ *p_buffer = o.first;
43
+ *p_size = o.second;
44
+ } CATCH_AND_HANDLE
45
+ }
46
+
47
+ int faiss_GpuResources_getAsyncCopyStream(FaissGpuResources* res, int device, cudaStream_t* out) {
48
+ try {
49
+ auto o = reinterpret_cast<GpuResources*>(res)->getAsyncCopyStream(device);
50
+ *out = o;
51
+ } CATCH_AND_HANDLE
52
+ }
53
+
54
+ int faiss_GpuResources_getBlasHandleCurrentDevice(FaissGpuResources* res, cublasHandle_t* out) {
55
+ try {
56
+ auto o = reinterpret_cast<GpuResources*>(res)->getBlasHandleCurrentDevice();
57
+ *out = o;
58
+ } CATCH_AND_HANDLE
59
+ }
60
+
61
+ int faiss_GpuResources_getDefaultStreamCurrentDevice(FaissGpuResources* res, cudaStream_t* out) {
62
+ try {
63
+ auto o = reinterpret_cast<GpuResources*>(res)->getDefaultStreamCurrentDevice();
64
+ *out = o;
65
+ } CATCH_AND_HANDLE
66
+ }
67
+
68
+ int faiss_GpuResources_syncDefaultStream(FaissGpuResources* res, int device) {
69
+ try {
70
+ reinterpret_cast<GpuResources*>(res)->syncDefaultStream(device);
71
+ } CATCH_AND_HANDLE
72
+ }
73
+
74
+ int faiss_GpuResources_syncDefaultStreamCurrentDevice(FaissGpuResources* res) {
75
+ try {
76
+ reinterpret_cast<GpuResources*>(res)->syncDefaultStreamCurrentDevice();
77
+ } CATCH_AND_HANDLE
78
+ }
79
+
80
+ int faiss_GpuResources_getAsyncCopyStreamCurrentDevice(FaissGpuResources* res, cudaStream_t* out) {
81
+ try {
82
+ auto o = reinterpret_cast<GpuResources*>(res)->getAsyncCopyStreamCurrentDevice();
83
+ *out = o;
84
+ } CATCH_AND_HANDLE
85
+ }
86
+
@@ -0,0 +1,66 @@
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_RESOURCES_C_H
12
+ #define FAISS_GPU_RESOURCES_C_H
13
+
14
+ #include <cuda_runtime_api.h>
15
+ #include <cublas.h>
16
+ #include "faiss_c.h"
17
+
18
+ #ifdef __cplusplus
19
+ extern "C" {
20
+ #endif
21
+
22
+ /// Base class of GPU-side resource provider; hides provision of
23
+ /// cuBLAS handles, CUDA streams and a temporary memory manager
24
+ FAISS_DECLARE_CLASS(GpuResources)
25
+
26
+ FAISS_DECLARE_DESTRUCTOR(GpuResources)
27
+
28
+ /// Call to pre-allocate resources for a particular device. If this is
29
+ /// not called, then resources will be allocated at the first time
30
+ /// of demand
31
+ int faiss_GpuResources_initializeForDevice(FaissGpuResources*, int);
32
+
33
+ /// Returns the cuBLAS handle that we use for the given device
34
+ int faiss_GpuResources_getBlasHandle(FaissGpuResources*, int, cublasHandle_t*);
35
+
36
+ /// Returns the stream that we order all computation on for the
37
+ /// given device
38
+ int faiss_GpuResources_getDefaultStream(FaissGpuResources*, int, cudaStream_t*);
39
+
40
+ /// Returns the available CPU pinned memory buffer
41
+ int faiss_GpuResources_getPinnedMemory(FaissGpuResources*, void**, size_t*);
42
+
43
+ /// Returns the stream on which we perform async CPU <-> GPU copies
44
+ int faiss_GpuResources_getAsyncCopyStream(FaissGpuResources*, int, cudaStream_t*);
45
+
46
+ /// Calls getBlasHandle with the current device
47
+ int faiss_GpuResources_getBlasHandleCurrentDevice(FaissGpuResources*, cublasHandle_t*);
48
+
49
+ /// Calls getDefaultStream with the current device
50
+ int faiss_GpuResources_getDefaultStreamCurrentDevice(FaissGpuResources*, cudaStream_t*);
51
+
52
+ /// Synchronizes the CPU with respect to the default stream for the
53
+ /// given device
54
+ // equivalent to cudaDeviceSynchronize(getDefaultStream(device))
55
+ int faiss_GpuResources_syncDefaultStream(FaissGpuResources*, int);
56
+
57
+ /// Calls syncDefaultStream for the current device
58
+ int faiss_GpuResources_syncDefaultStreamCurrentDevice(FaissGpuResources*);
59
+
60
+ /// Calls getAsyncCopyStream for the current device
61
+ int faiss_GpuResources_getAsyncCopyStreamCurrentDevice(FaissGpuResources*, cudaStream_t*);
62
+
63
+ #ifdef __cplusplus
64
+ }
65
+ #endif
66
+ #endif
@@ -0,0 +1,54 @@
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 "gpu/StandardGpuResources_c.h"
12
+ #include "gpu/StandardGpuResources.h"
13
+ #include "macros_impl.h"
14
+
15
+ using faiss::gpu::StandardGpuResources;
16
+
17
+ DEFINE_DESTRUCTOR(StandardGpuResources)
18
+
19
+ int faiss_StandardGpuResources_new(FaissStandardGpuResources** p_res) {
20
+ try {
21
+ auto p = new StandardGpuResources();
22
+ *p_res = reinterpret_cast<FaissStandardGpuResources*>(p);
23
+ } CATCH_AND_HANDLE
24
+ }
25
+
26
+ int faiss_StandardGpuResources_noTempMemory(FaissStandardGpuResources* res) {
27
+ try {
28
+ reinterpret_cast<StandardGpuResources*>(res)->noTempMemory();
29
+ } CATCH_AND_HANDLE
30
+ }
31
+
32
+ int faiss_StandardGpuResources_setTempMemory(FaissStandardGpuResources* res, size_t size) {
33
+ try {
34
+ reinterpret_cast<StandardGpuResources*>(res)->setTempMemory(size);
35
+ } CATCH_AND_HANDLE
36
+ }
37
+
38
+ int faiss_StandardGpuResources_setPinnedMemory(FaissStandardGpuResources* res, size_t size) {
39
+ try {
40
+ reinterpret_cast<StandardGpuResources*>(res)->setPinnedMemory(size);
41
+ } CATCH_AND_HANDLE
42
+ }
43
+
44
+ int faiss_StandardGpuResources_setDefaultStream(FaissStandardGpuResources* res, int device, cudaStream_t stream) {
45
+ try {
46
+ reinterpret_cast<StandardGpuResources*>(res)->setDefaultStream(device, stream);
47
+ } CATCH_AND_HANDLE
48
+ }
49
+
50
+ int faiss_StandardGpuResources_setDefaultNullStreamAllDevices(FaissStandardGpuResources* res) {
51
+ try {
52
+ reinterpret_cast<StandardGpuResources*>(res)->setDefaultNullStreamAllDevices();
53
+ } CATCH_AND_HANDLE
54
+ }
@@ -0,0 +1,53 @@
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_STANDARD_GPURESOURCES_C_H
12
+ #define FAISS_STANDARD_GPURESOURCES_C_H
13
+
14
+ #include <cuda_runtime_api.h>
15
+ #include "faiss_c.h"
16
+ #include "gpu/GpuResources_c.h"
17
+
18
+ #ifdef __cplusplus
19
+ extern "C" {
20
+ #endif
21
+
22
+ /// Default implementation of GpuResources that allocates a cuBLAS
23
+ /// stream and 2 streams for use, as well as temporary memory
24
+ FAISS_DECLARE_CLASS_INHERITED(StandardGpuResources, GpuResources)
25
+
26
+ FAISS_DECLARE_DESTRUCTOR(StandardGpuResources)
27
+
28
+ /// Default constructor for StandardGpuResources
29
+ int faiss_StandardGpuResources_new(FaissStandardGpuResources**);
30
+
31
+ /// Disable allocation of temporary memory; all temporary memory
32
+ /// requests will call cudaMalloc / cudaFree at the point of use
33
+ int faiss_StandardGpuResources_noTempMemory(FaissStandardGpuResources*);
34
+
35
+ /// Specify that we wish to use a certain fixed size of memory on
36
+ /// all devices as temporary memory
37
+ int faiss_StandardGpuResources_setTempMemory(FaissStandardGpuResources*, size_t size);
38
+
39
+ /// Set amount of pinned memory to allocate, for async GPU <-> CPU
40
+ /// transfers
41
+ int faiss_StandardGpuResources_setPinnedMemory(FaissStandardGpuResources*, size_t size);
42
+
43
+ /// Called to change the stream for work ordering
44
+ int faiss_StandardGpuResources_setDefaultStream(FaissStandardGpuResources*, int device, cudaStream_t stream);
45
+
46
+ /// Called to change the work ordering streams to the null stream
47
+ /// for all devices
48
+ int faiss_StandardGpuResources_setDefaultNullStreamAllDevices(FaissStandardGpuResources*);
49
+
50
+ #ifdef __cplusplus
51
+ }
52
+ #endif
53
+ #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 GPU_MACROS_IMPL_H
12
+ #define GPU_MACROS_IMPL_H
13
+ #include "../macros_impl.h"
14
+
15
+ #undef DEFINE_GETTER
16
+ #define DEFINE_GETTER(clazz, ty, name) \
17
+ ty faiss_ ## clazz ## _ ## name (const Faiss ## clazz *obj) { \
18
+ return static_cast< ty >( \
19
+ reinterpret_cast< const faiss::gpu::clazz *>(obj)-> name \
20
+ ); \
21
+ }
22
+
23
+ #undef DEFINE_SETTER
24
+ #define DEFINE_SETTER(clazz, ty, name) \
25
+ void faiss_ ## clazz ## _set_ ## name (Faiss ## clazz *obj, ty val) { \
26
+ reinterpret_cast< faiss::gpu::clazz *>(obj)-> name = val; \
27
+ }
28
+
29
+ #undef DEFINE_SETTER_STATIC
30
+ #define DEFINE_SETTER_STATIC(clazz, ty_to, ty_from, name) \
31
+ void faiss_ ## clazz ## _set_ ## name (Faiss ## clazz *obj, ty_from val) { \
32
+ reinterpret_cast< faiss::gpu::clazz *>(obj)-> name = \
33
+ static_cast< ty_to >(val); \
34
+ }
35
+
36
+ #undef DEFINE_DESTRUCTOR
37
+ #define DEFINE_DESTRUCTOR(clazz) \
38
+ void faiss_ ## clazz ## _free (Faiss ## clazz *obj) { \
39
+ delete reinterpret_cast<faiss::gpu::clazz *>(obj); \
40
+ }
41
+
42
+ #endif
@@ -0,0 +1,220 @@
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 "AuxIndexStructures_c.h"
12
+ #include "../../impl/AuxIndexStructures.h"
13
+ #include "../macros_impl.h"
14
+ #include <iostream>
15
+
16
+ using faiss::BufferList;
17
+ using faiss::IDSelector;
18
+ using faiss::IDSelectorBatch;
19
+ using faiss::IDSelectorRange;
20
+ using faiss::RangeSearchResult;
21
+ using faiss::RangeSearchPartialResult;
22
+ using faiss::RangeQueryResult;
23
+ using faiss::DistanceComputer;
24
+
25
+ DEFINE_GETTER(RangeSearchResult, size_t, nq)
26
+
27
+ int faiss_RangeSearchResult_new(FaissRangeSearchResult** p_rsr, idx_t nq) {
28
+ try {
29
+ *p_rsr = reinterpret_cast<FaissRangeSearchResult*>(
30
+ new RangeSearchResult(nq));
31
+ return 0;
32
+ } CATCH_AND_HANDLE
33
+ }
34
+
35
+ int faiss_RangeSearchResult_new_with(FaissRangeSearchResult** p_rsr, idx_t nq, int alloc_lims) {
36
+ try {
37
+ *p_rsr = reinterpret_cast<FaissRangeSearchResult*>(
38
+ new RangeSearchResult(nq, static_cast<bool>(alloc_lims)));
39
+ return 0;
40
+ } CATCH_AND_HANDLE
41
+ }
42
+
43
+ /// called when lims contains the nb of elements result entries
44
+ /// for each query
45
+ int faiss_RangeSearchResult_do_allocation(FaissRangeSearchResult* rsr) {
46
+ try {
47
+ reinterpret_cast<RangeSearchResult*>(rsr)->do_allocation();
48
+ return 0;
49
+ } CATCH_AND_HANDLE
50
+ }
51
+
52
+ DEFINE_DESTRUCTOR(RangeSearchResult)
53
+
54
+ /// getter for buffer_size
55
+ DEFINE_GETTER(RangeSearchResult, size_t, buffer_size)
56
+
57
+ /// getter for lims: size (nq + 1)
58
+ void faiss_RangeSearchResult_lims(FaissRangeSearchResult* rsr, size_t** lims) {
59
+ *lims = reinterpret_cast<RangeSearchResult*>(rsr)->lims;
60
+ }
61
+
62
+ /// getter for labels and respective distances (not sorted):
63
+ /// result for query i is labels[lims[i]:lims[i+1]]
64
+ void faiss_RangeSearchResult_labels(FaissRangeSearchResult* rsr, idx_t** labels, float** distances) {
65
+ auto sr = reinterpret_cast<RangeSearchResult*>(rsr);
66
+ *labels = sr->labels;
67
+ *distances = sr->distances;
68
+ }
69
+
70
+ DEFINE_DESTRUCTOR(IDSelector)
71
+
72
+ int faiss_IDSelector_is_member(const FaissIDSelector* sel, idx_t id) {
73
+ return reinterpret_cast<const IDSelector*>(sel)->is_member(id);
74
+ }
75
+
76
+ DEFINE_DESTRUCTOR(IDSelectorRange)
77
+
78
+ DEFINE_GETTER(IDSelectorRange, idx_t, imin)
79
+ DEFINE_GETTER(IDSelectorRange, idx_t, imax)
80
+
81
+ int faiss_IDSelectorRange_new(FaissIDSelectorRange** p_sel, idx_t imin, idx_t imax) {
82
+ try {
83
+ *p_sel = reinterpret_cast<FaissIDSelectorRange*>(
84
+ new IDSelectorRange(imin, imax)
85
+ );
86
+ return 0;
87
+ } CATCH_AND_HANDLE
88
+ }
89
+
90
+ DEFINE_GETTER(IDSelectorBatch, int, nbits)
91
+ DEFINE_GETTER(IDSelectorBatch, idx_t, mask)
92
+
93
+ int faiss_IDSelectorBatch_new(FaissIDSelectorBatch** p_sel, size_t n, const idx_t* indices) {
94
+ try {
95
+ *p_sel = reinterpret_cast<FaissIDSelectorBatch*>(
96
+ new IDSelectorBatch(n, indices)
97
+ );
98
+ return 0;
99
+ } CATCH_AND_HANDLE
100
+ }
101
+
102
+ // Below are structures used only by Index implementations
103
+
104
+ DEFINE_DESTRUCTOR(BufferList)
105
+
106
+ DEFINE_GETTER(BufferList, size_t, buffer_size)
107
+ DEFINE_GETTER(BufferList, size_t, wp)
108
+
109
+ int faiss_BufferList_append_buffer(FaissBufferList* bl) {
110
+ try {
111
+ reinterpret_cast<BufferList*>(bl)->append_buffer();
112
+ return 0;
113
+ } CATCH_AND_HANDLE
114
+ }
115
+
116
+ int faiss_BufferList_new(FaissBufferList** p_bl, size_t buffer_size) {
117
+ try {
118
+ *p_bl = reinterpret_cast<FaissBufferList*>(
119
+ new BufferList(buffer_size)
120
+ );
121
+ return 0;
122
+ } CATCH_AND_HANDLE
123
+ }
124
+
125
+ int faiss_BufferList_add(FaissBufferList* bl, idx_t id, float dis) {
126
+ try {
127
+ reinterpret_cast<BufferList*>(bl)->add(id, dis);
128
+ return 0;
129
+ } CATCH_AND_HANDLE
130
+ }
131
+
132
+ /// copy elemnts ofs:ofs+n-1 seen as linear data in the buffers to
133
+ /// tables dest_ids, dest_dis
134
+ int faiss_BufferList_copy_range(
135
+ FaissBufferList* bl, size_t ofs, size_t n, idx_t *dest_ids, float *dest_dis) {
136
+ try {
137
+ reinterpret_cast<BufferList*>(bl)->copy_range(ofs, n, dest_ids, dest_dis);
138
+ return 0;
139
+ } CATCH_AND_HANDLE
140
+ }
141
+
142
+ DEFINE_GETTER(RangeQueryResult, idx_t, qno)
143
+ DEFINE_GETTER(RangeQueryResult, size_t, nres)
144
+ DEFINE_GETTER_PERMISSIVE(RangeQueryResult, FaissRangeSearchPartialResult*, pres)
145
+
146
+ int faiss_RangeQueryResult_add(FaissRangeQueryResult* qr, float dis, idx_t id) {
147
+ try {
148
+ reinterpret_cast<RangeQueryResult*>(qr)->add(dis, id);
149
+ return 0;
150
+ } CATCH_AND_HANDLE
151
+ }
152
+
153
+ DEFINE_GETTER_PERMISSIVE(RangeSearchPartialResult, FaissRangeSearchResult*, res)
154
+
155
+ int faiss_RangeSearchPartialResult_new(
156
+ FaissRangeSearchPartialResult** p_res, FaissRangeSearchResult* res_in) {
157
+ try {
158
+ *p_res = reinterpret_cast<FaissRangeSearchPartialResult*>(
159
+ new RangeSearchPartialResult(
160
+ reinterpret_cast<RangeSearchResult*>(res_in))
161
+ );
162
+ return 0;
163
+ } CATCH_AND_HANDLE
164
+ }
165
+
166
+ int faiss_RangeSearchPartialResult_finalize(
167
+ FaissRangeSearchPartialResult* res) {
168
+ try {
169
+ reinterpret_cast<RangeSearchPartialResult*>(res)->finalize();
170
+ return 0;
171
+ } CATCH_AND_HANDLE
172
+ }
173
+
174
+ /// called by range_search before do_allocation
175
+ int faiss_RangeSearchPartialResult_set_lims(
176
+ FaissRangeSearchPartialResult* res) {
177
+ try {
178
+ reinterpret_cast<RangeSearchPartialResult*>(res)->set_lims();
179
+ return 0;
180
+ } CATCH_AND_HANDLE
181
+ }
182
+
183
+ int faiss_RangeSearchPartialResult_new_result(
184
+ FaissRangeSearchPartialResult* res, idx_t qno, FaissRangeQueryResult** qr) {
185
+
186
+ try {
187
+ auto q =
188
+ &reinterpret_cast<RangeSearchPartialResult*>(res)->new_result(qno);
189
+ if (qr) {
190
+ *qr = reinterpret_cast<FaissRangeQueryResult*>(&q);
191
+ }
192
+ return 0;
193
+ } CATCH_AND_HANDLE
194
+ }
195
+
196
+ DEFINE_DESTRUCTOR(DistanceComputer)
197
+
198
+ int faiss_DistanceComputer_set_query(FaissDistanceComputer *dc, const float *x) {
199
+ try {
200
+ reinterpret_cast<DistanceComputer*>(dc)->set_query(x);
201
+ return 0;
202
+ }
203
+ CATCH_AND_HANDLE
204
+ }
205
+
206
+ int faiss_DistanceComputer_vector_to_query_dis(FaissDistanceComputer *dc, idx_t i, float *qd) {
207
+ try {
208
+ *qd = reinterpret_cast<DistanceComputer*>(dc)->operator()(i);
209
+ return 0;
210
+ }
211
+ CATCH_AND_HANDLE
212
+ }
213
+
214
+ int faiss_DistanceComputer_symmetric_dis(FaissDistanceComputer *dc, idx_t i, idx_t j, float *vd) {
215
+ try {
216
+ *vd = reinterpret_cast<DistanceComputer*>(dc)->symmetric_dis(i, j);
217
+ return 0;
218
+ }
219
+ CATCH_AND_HANDLE
220
+ }