faiss 0.1.2 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (274) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +16 -4
  5. data/ext/faiss/ext.cpp +11 -307
  6. data/ext/faiss/extconf.rb +3 -3
  7. data/ext/faiss/index.cpp +156 -0
  8. data/ext/faiss/index_binary.cpp +81 -0
  9. data/ext/faiss/kmeans.cpp +36 -0
  10. data/ext/faiss/pca_matrix.cpp +34 -0
  11. data/ext/faiss/product_quantizer.cpp +54 -0
  12. data/ext/faiss/utils.cpp +40 -0
  13. data/ext/faiss/utils.h +16 -0
  14. data/lib/faiss/version.rb +1 -1
  15. data/vendor/faiss/{AutoTune.cpp → faiss/AutoTune.cpp} +44 -41
  16. data/vendor/faiss/{AutoTune.h → faiss/AutoTune.h} +6 -3
  17. data/vendor/faiss/{Clustering.cpp → faiss/Clustering.cpp} +29 -24
  18. data/vendor/faiss/{Clustering.h → faiss/Clustering.h} +0 -0
  19. data/vendor/faiss/{IVFlib.cpp → faiss/IVFlib.cpp} +86 -11
  20. data/vendor/faiss/{IVFlib.h → faiss/IVFlib.h} +26 -8
  21. data/vendor/faiss/{Index.cpp → faiss/Index.cpp} +3 -4
  22. data/vendor/faiss/{Index.h → faiss/Index.h} +3 -3
  23. data/vendor/faiss/{Index2Layer.cpp → faiss/Index2Layer.cpp} +12 -11
  24. data/vendor/faiss/{Index2Layer.h → faiss/Index2Layer.h} +0 -0
  25. data/vendor/faiss/{IndexBinary.cpp → faiss/IndexBinary.cpp} +5 -5
  26. data/vendor/faiss/{IndexBinary.h → faiss/IndexBinary.h} +1 -1
  27. data/vendor/faiss/{IndexBinaryFlat.cpp → faiss/IndexBinaryFlat.cpp} +0 -0
  28. data/vendor/faiss/{IndexBinaryFlat.h → faiss/IndexBinaryFlat.h} +0 -0
  29. data/vendor/faiss/{IndexBinaryFromFloat.cpp → faiss/IndexBinaryFromFloat.cpp} +1 -0
  30. data/vendor/faiss/{IndexBinaryFromFloat.h → faiss/IndexBinaryFromFloat.h} +0 -0
  31. data/vendor/faiss/{IndexBinaryHNSW.cpp → faiss/IndexBinaryHNSW.cpp} +1 -2
  32. data/vendor/faiss/{IndexBinaryHNSW.h → faiss/IndexBinaryHNSW.h} +0 -0
  33. data/vendor/faiss/{IndexBinaryHash.cpp → faiss/IndexBinaryHash.cpp} +9 -10
  34. data/vendor/faiss/{IndexBinaryHash.h → faiss/IndexBinaryHash.h} +2 -1
  35. data/vendor/faiss/{IndexBinaryIVF.cpp → faiss/IndexBinaryIVF.cpp} +11 -18
  36. data/vendor/faiss/{IndexBinaryIVF.h → faiss/IndexBinaryIVF.h} +1 -1
  37. data/vendor/faiss/{IndexFlat.cpp → faiss/IndexFlat.cpp} +0 -148
  38. data/vendor/faiss/{IndexFlat.h → faiss/IndexFlat.h} +0 -51
  39. data/vendor/faiss/{IndexHNSW.cpp → faiss/IndexHNSW.cpp} +67 -37
  40. data/vendor/faiss/{IndexHNSW.h → faiss/IndexHNSW.h} +0 -0
  41. data/vendor/faiss/{IndexIVF.cpp → faiss/IndexIVF.cpp} +244 -74
  42. data/vendor/faiss/{IndexIVF.h → faiss/IndexIVF.h} +26 -15
  43. data/vendor/faiss/{IndexIVFFlat.cpp → faiss/IndexIVFFlat.cpp} +9 -8
  44. data/vendor/faiss/{IndexIVFFlat.h → faiss/IndexIVFFlat.h} +2 -1
  45. data/vendor/faiss/{IndexIVFPQ.cpp → faiss/IndexIVFPQ.cpp} +48 -23
  46. data/vendor/faiss/{IndexIVFPQ.h → faiss/IndexIVFPQ.h} +27 -9
  47. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1116 -0
  48. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +166 -0
  49. data/vendor/faiss/{IndexIVFPQR.cpp → faiss/IndexIVFPQR.cpp} +11 -10
  50. data/vendor/faiss/{IndexIVFPQR.h → faiss/IndexIVFPQR.h} +2 -1
  51. data/vendor/faiss/{IndexIVFSpectralHash.cpp → faiss/IndexIVFSpectralHash.cpp} +2 -3
  52. data/vendor/faiss/{IndexIVFSpectralHash.h → faiss/IndexIVFSpectralHash.h} +0 -0
  53. data/vendor/faiss/{IndexLSH.cpp → faiss/IndexLSH.cpp} +0 -0
  54. data/vendor/faiss/{IndexLSH.h → faiss/IndexLSH.h} +0 -0
  55. data/vendor/faiss/{IndexLattice.cpp → faiss/IndexLattice.cpp} +0 -0
  56. data/vendor/faiss/{IndexLattice.h → faiss/IndexLattice.h} +0 -0
  57. data/vendor/faiss/{IndexPQ.cpp → faiss/IndexPQ.cpp} +40 -24
  58. data/vendor/faiss/{IndexPQ.h → faiss/IndexPQ.h} +3 -1
  59. data/vendor/faiss/faiss/IndexPQFastScan.cpp +536 -0
  60. data/vendor/faiss/faiss/IndexPQFastScan.h +111 -0
  61. data/vendor/faiss/{IndexPreTransform.cpp → faiss/IndexPreTransform.cpp} +47 -0
  62. data/vendor/faiss/{IndexPreTransform.h → faiss/IndexPreTransform.h} +2 -0
  63. data/vendor/faiss/faiss/IndexRefine.cpp +256 -0
  64. data/vendor/faiss/faiss/IndexRefine.h +73 -0
  65. data/vendor/faiss/{IndexReplicas.cpp → faiss/IndexReplicas.cpp} +102 -10
  66. data/vendor/faiss/{IndexReplicas.h → faiss/IndexReplicas.h} +6 -0
  67. data/vendor/faiss/{IndexScalarQuantizer.cpp → faiss/IndexScalarQuantizer.cpp} +5 -5
  68. data/vendor/faiss/{IndexScalarQuantizer.h → faiss/IndexScalarQuantizer.h} +1 -1
  69. data/vendor/faiss/{IndexShards.cpp → faiss/IndexShards.cpp} +37 -12
  70. data/vendor/faiss/{IndexShards.h → faiss/IndexShards.h} +3 -4
  71. data/vendor/faiss/{MatrixStats.cpp → faiss/MatrixStats.cpp} +0 -0
  72. data/vendor/faiss/{MatrixStats.h → faiss/MatrixStats.h} +0 -0
  73. data/vendor/faiss/{MetaIndexes.cpp → faiss/MetaIndexes.cpp} +5 -3
  74. data/vendor/faiss/{MetaIndexes.h → faiss/MetaIndexes.h} +0 -0
  75. data/vendor/faiss/{MetricType.h → faiss/MetricType.h} +0 -0
  76. data/vendor/faiss/{VectorTransform.cpp → faiss/VectorTransform.cpp} +4 -3
  77. data/vendor/faiss/{VectorTransform.h → faiss/VectorTransform.h} +0 -0
  78. data/vendor/faiss/{clone_index.cpp → faiss/clone_index.cpp} +0 -0
  79. data/vendor/faiss/{clone_index.h → faiss/clone_index.h} +0 -0
  80. data/vendor/faiss/{gpu → faiss/gpu}/GpuAutoTune.cpp +0 -0
  81. data/vendor/faiss/{gpu → faiss/gpu}/GpuAutoTune.h +0 -0
  82. data/vendor/faiss/{gpu → faiss/gpu}/GpuCloner.cpp +14 -14
  83. data/vendor/faiss/{gpu → faiss/gpu}/GpuCloner.h +6 -7
  84. data/vendor/faiss/{gpu → faiss/gpu}/GpuClonerOptions.cpp +0 -0
  85. data/vendor/faiss/{gpu → faiss/gpu}/GpuClonerOptions.h +0 -0
  86. data/vendor/faiss/{gpu → faiss/gpu}/GpuDistance.h +13 -5
  87. data/vendor/faiss/{gpu → faiss/gpu}/GpuFaissAssert.h +0 -0
  88. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndex.h +18 -17
  89. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexBinaryFlat.h +15 -8
  90. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexFlat.h +46 -21
  91. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVF.h +20 -4
  92. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVFFlat.h +32 -5
  93. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVFPQ.h +43 -16
  94. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVFScalarQuantizer.h +33 -5
  95. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndicesOptions.h +0 -0
  96. data/vendor/faiss/faiss/gpu/GpuResources.cpp +200 -0
  97. data/vendor/faiss/faiss/gpu/GpuResources.h +268 -0
  98. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +605 -0
  99. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +204 -0
  100. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +547 -0
  101. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +51 -0
  102. data/vendor/faiss/{gpu → faiss/gpu}/impl/RemapIndices.cpp +3 -3
  103. data/vendor/faiss/{gpu → faiss/gpu}/impl/RemapIndices.h +3 -2
  104. data/vendor/faiss/{gpu → faiss/gpu}/perf/IndexWrapper-inl.h +1 -1
  105. data/vendor/faiss/{gpu → faiss/gpu}/perf/IndexWrapper.h +1 -1
  106. data/vendor/faiss/{gpu → faiss/gpu}/perf/PerfClustering.cpp +1 -1
  107. data/vendor/faiss/{gpu → faiss/gpu}/perf/PerfIVFPQAdd.cpp +0 -0
  108. data/vendor/faiss/{gpu → faiss/gpu}/perf/WriteIndex.cpp +0 -0
  109. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +274 -0
  110. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexBinaryFlat.cpp +0 -0
  111. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexFlat.cpp +1 -1
  112. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexIVFFlat.cpp +7 -2
  113. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexIVFPQ.cpp +146 -53
  114. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +231 -0
  115. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuMemoryException.cpp +0 -0
  116. data/vendor/faiss/{gpu → faiss/gpu}/test/TestUtils.cpp +4 -2
  117. data/vendor/faiss/{gpu → faiss/gpu}/test/TestUtils.h +33 -0
  118. data/vendor/faiss/{gpu → faiss/gpu}/test/demo_ivfpq_indexing_gpu.cpp +7 -5
  119. data/vendor/faiss/{gpu → faiss/gpu}/utils/DeviceUtils.h +1 -1
  120. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +214 -0
  121. data/vendor/faiss/{gpu → faiss/gpu}/utils/StackDeviceMemory.h +25 -40
  122. data/vendor/faiss/{gpu → faiss/gpu}/utils/StaticUtils.h +6 -0
  123. data/vendor/faiss/{gpu → faiss/gpu}/utils/Timer.cpp +5 -6
  124. data/vendor/faiss/{gpu → faiss/gpu}/utils/Timer.h +2 -2
  125. data/vendor/faiss/{impl → faiss/impl}/AuxIndexStructures.cpp +1 -0
  126. data/vendor/faiss/{impl → faiss/impl}/AuxIndexStructures.h +8 -5
  127. data/vendor/faiss/{impl → faiss/impl}/FaissAssert.h +1 -0
  128. data/vendor/faiss/{impl → faiss/impl}/FaissException.cpp +26 -0
  129. data/vendor/faiss/{impl → faiss/impl}/FaissException.h +4 -0
  130. data/vendor/faiss/{impl → faiss/impl}/HNSW.cpp +28 -30
  131. data/vendor/faiss/{impl → faiss/impl}/HNSW.h +19 -11
  132. data/vendor/faiss/{impl → faiss/impl}/PolysemousTraining.cpp +1 -1
  133. data/vendor/faiss/{impl → faiss/impl}/PolysemousTraining.h +5 -5
  134. data/vendor/faiss/{impl → faiss/impl}/ProductQuantizer-inl.h +0 -1
  135. data/vendor/faiss/{impl → faiss/impl}/ProductQuantizer.cpp +31 -21
  136. data/vendor/faiss/{impl → faiss/impl}/ProductQuantizer.h +2 -0
  137. data/vendor/faiss/faiss/impl/ResultHandler.h +452 -0
  138. data/vendor/faiss/{impl → faiss/impl}/ScalarQuantizer.cpp +90 -56
  139. data/vendor/faiss/{impl → faiss/impl}/ScalarQuantizer.h +7 -1
  140. data/vendor/faiss/{impl → faiss/impl}/ThreadedIndex-inl.h +0 -0
  141. data/vendor/faiss/{impl → faiss/impl}/ThreadedIndex.h +0 -0
  142. data/vendor/faiss/{impl → faiss/impl}/index_read.cpp +79 -128
  143. data/vendor/faiss/{impl → faiss/impl}/index_write.cpp +37 -63
  144. data/vendor/faiss/{impl → faiss/impl}/io.cpp +47 -11
  145. data/vendor/faiss/{impl → faiss/impl}/io.h +29 -10
  146. data/vendor/faiss/faiss/impl/io_macros.h +57 -0
  147. data/vendor/faiss/{impl → faiss/impl}/lattice_Zn.cpp +38 -36
  148. data/vendor/faiss/{impl → faiss/impl}/lattice_Zn.h +3 -3
  149. data/vendor/faiss/faiss/impl/platform_macros.h +68 -0
  150. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +272 -0
  151. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +169 -0
  152. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +180 -0
  153. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +354 -0
  154. data/vendor/faiss/faiss/impl/simd_result_handlers.h +559 -0
  155. data/vendor/faiss/{index_factory.cpp → faiss/index_factory.cpp} +145 -19
  156. data/vendor/faiss/{index_factory.h → faiss/index_factory.h} +0 -0
  157. data/vendor/faiss/{index_io.h → faiss/index_io.h} +8 -1
  158. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +151 -0
  159. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +76 -0
  160. data/vendor/faiss/{DirectMap.cpp → faiss/invlists/DirectMap.cpp} +1 -1
  161. data/vendor/faiss/{DirectMap.h → faiss/invlists/DirectMap.h} +1 -1
  162. data/vendor/faiss/{InvertedLists.cpp → faiss/invlists/InvertedLists.cpp} +74 -3
  163. data/vendor/faiss/{InvertedLists.h → faiss/invlists/InvertedLists.h} +33 -1
  164. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +107 -0
  165. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +63 -0
  166. data/vendor/faiss/{OnDiskInvertedLists.cpp → faiss/invlists/OnDiskInvertedLists.cpp} +161 -8
  167. data/vendor/faiss/{OnDiskInvertedLists.h → faiss/invlists/OnDiskInvertedLists.h} +31 -8
  168. data/vendor/faiss/faiss/python/python_callbacks.cpp +112 -0
  169. data/vendor/faiss/faiss/python/python_callbacks.h +52 -0
  170. data/vendor/faiss/faiss/utils/AlignedTable.h +141 -0
  171. data/vendor/faiss/{utils → faiss/utils}/Heap.cpp +7 -9
  172. data/vendor/faiss/{utils → faiss/utils}/Heap.h +61 -52
  173. data/vendor/faiss/{utils → faiss/utils}/WorkerThread.cpp +0 -0
  174. data/vendor/faiss/{utils → faiss/utils}/WorkerThread.h +0 -0
  175. data/vendor/faiss/{utils → faiss/utils}/distances.cpp +189 -329
  176. data/vendor/faiss/{utils → faiss/utils}/distances.h +30 -21
  177. data/vendor/faiss/{utils → faiss/utils}/distances_simd.cpp +282 -54
  178. data/vendor/faiss/{utils → faiss/utils}/extra_distances.cpp +9 -9
  179. data/vendor/faiss/{utils → faiss/utils}/extra_distances.h +0 -0
  180. data/vendor/faiss/{utils → faiss/utils}/hamming-inl.h +5 -7
  181. data/vendor/faiss/{utils → faiss/utils}/hamming.cpp +11 -13
  182. data/vendor/faiss/{utils → faiss/utils}/hamming.h +4 -3
  183. data/vendor/faiss/faiss/utils/ordered_key_value.h +98 -0
  184. data/vendor/faiss/faiss/utils/partitioning.cpp +1256 -0
  185. data/vendor/faiss/faiss/utils/partitioning.h +69 -0
  186. data/vendor/faiss/faiss/utils/quantize_lut.cpp +277 -0
  187. data/vendor/faiss/faiss/utils/quantize_lut.h +80 -0
  188. data/vendor/faiss/{utils → faiss/utils}/random.cpp +5 -5
  189. data/vendor/faiss/{utils → faiss/utils}/random.h +0 -0
  190. data/vendor/faiss/faiss/utils/simdlib.h +31 -0
  191. data/vendor/faiss/faiss/utils/simdlib_avx2.h +461 -0
  192. data/vendor/faiss/faiss/utils/simdlib_emulated.h +589 -0
  193. data/vendor/faiss/{utils → faiss/utils}/utils.cpp +27 -28
  194. data/vendor/faiss/{utils → faiss/utils}/utils.h +4 -0
  195. metadata +195 -285
  196. data/vendor/faiss/c_api/AutoTune_c.cpp +0 -83
  197. data/vendor/faiss/c_api/AutoTune_c.h +0 -64
  198. data/vendor/faiss/c_api/Clustering_c.cpp +0 -145
  199. data/vendor/faiss/c_api/Clustering_c.h +0 -123
  200. data/vendor/faiss/c_api/IndexFlat_c.cpp +0 -140
  201. data/vendor/faiss/c_api/IndexFlat_c.h +0 -115
  202. data/vendor/faiss/c_api/IndexIVFFlat_c.cpp +0 -64
  203. data/vendor/faiss/c_api/IndexIVFFlat_c.h +0 -58
  204. data/vendor/faiss/c_api/IndexIVF_c.cpp +0 -99
  205. data/vendor/faiss/c_api/IndexIVF_c.h +0 -142
  206. data/vendor/faiss/c_api/IndexLSH_c.cpp +0 -37
  207. data/vendor/faiss/c_api/IndexLSH_c.h +0 -40
  208. data/vendor/faiss/c_api/IndexPreTransform_c.cpp +0 -21
  209. data/vendor/faiss/c_api/IndexPreTransform_c.h +0 -32
  210. data/vendor/faiss/c_api/IndexShards_c.cpp +0 -44
  211. data/vendor/faiss/c_api/IndexShards_c.h +0 -42
  212. data/vendor/faiss/c_api/Index_c.cpp +0 -105
  213. data/vendor/faiss/c_api/Index_c.h +0 -183
  214. data/vendor/faiss/c_api/MetaIndexes_c.cpp +0 -49
  215. data/vendor/faiss/c_api/MetaIndexes_c.h +0 -49
  216. data/vendor/faiss/c_api/clone_index_c.cpp +0 -23
  217. data/vendor/faiss/c_api/clone_index_c.h +0 -32
  218. data/vendor/faiss/c_api/error_c.h +0 -42
  219. data/vendor/faiss/c_api/error_impl.cpp +0 -27
  220. data/vendor/faiss/c_api/error_impl.h +0 -16
  221. data/vendor/faiss/c_api/faiss_c.h +0 -58
  222. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +0 -96
  223. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.h +0 -56
  224. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +0 -52
  225. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.h +0 -68
  226. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +0 -17
  227. data/vendor/faiss/c_api/gpu/GpuIndex_c.h +0 -30
  228. data/vendor/faiss/c_api/gpu/GpuIndicesOptions_c.h +0 -38
  229. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +0 -86
  230. data/vendor/faiss/c_api/gpu/GpuResources_c.h +0 -66
  231. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +0 -54
  232. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.h +0 -53
  233. data/vendor/faiss/c_api/gpu/macros_impl.h +0 -42
  234. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.cpp +0 -220
  235. data/vendor/faiss/c_api/impl/AuxIndexStructures_c.h +0 -149
  236. data/vendor/faiss/c_api/index_factory_c.cpp +0 -26
  237. data/vendor/faiss/c_api/index_factory_c.h +0 -30
  238. data/vendor/faiss/c_api/index_io_c.cpp +0 -42
  239. data/vendor/faiss/c_api/index_io_c.h +0 -50
  240. data/vendor/faiss/c_api/macros_impl.h +0 -110
  241. data/vendor/faiss/demos/demo_imi_flat.cpp +0 -151
  242. data/vendor/faiss/demos/demo_imi_pq.cpp +0 -199
  243. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +0 -146
  244. data/vendor/faiss/demos/demo_sift1M.cpp +0 -252
  245. data/vendor/faiss/demos/demo_weighted_kmeans.cpp +0 -185
  246. data/vendor/faiss/gpu/GpuResources.cpp +0 -52
  247. data/vendor/faiss/gpu/GpuResources.h +0 -73
  248. data/vendor/faiss/gpu/StandardGpuResources.cpp +0 -303
  249. data/vendor/faiss/gpu/StandardGpuResources.h +0 -114
  250. data/vendor/faiss/gpu/utils/DeviceMemory.cpp +0 -77
  251. data/vendor/faiss/gpu/utils/DeviceMemory.h +0 -71
  252. data/vendor/faiss/gpu/utils/MemorySpace.cpp +0 -89
  253. data/vendor/faiss/gpu/utils/MemorySpace.h +0 -44
  254. data/vendor/faiss/gpu/utils/StackDeviceMemory.cpp +0 -239
  255. data/vendor/faiss/misc/test_blas.cpp +0 -84
  256. data/vendor/faiss/tests/test_binary_flat.cpp +0 -64
  257. data/vendor/faiss/tests/test_dealloc_invlists.cpp +0 -183
  258. data/vendor/faiss/tests/test_ivfpq_codec.cpp +0 -67
  259. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +0 -98
  260. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +0 -566
  261. data/vendor/faiss/tests/test_merge.cpp +0 -257
  262. data/vendor/faiss/tests/test_omp_threads.cpp +0 -14
  263. data/vendor/faiss/tests/test_ondisk_ivf.cpp +0 -220
  264. data/vendor/faiss/tests/test_pairs_decoding.cpp +0 -189
  265. data/vendor/faiss/tests/test_params_override.cpp +0 -231
  266. data/vendor/faiss/tests/test_pq_encoding.cpp +0 -98
  267. data/vendor/faiss/tests/test_sliding_ivf.cpp +0 -240
  268. data/vendor/faiss/tests/test_threaded_index.cpp +0 -253
  269. data/vendor/faiss/tests/test_transfer_invlists.cpp +0 -159
  270. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +0 -98
  271. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +0 -81
  272. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +0 -93
  273. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +0 -119
  274. data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +0 -100
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61a0b7a7d20933b60a9e0e213016b77f10eae5bb86ecf6825f5f5661f31f5d7d
4
- data.tar.gz: 967378ee774a35e3a639b1d902648ebf4177ffb7ed43e7228e8432f284f397c1
3
+ metadata.gz: f6fb14299c0b91d6ddaf74ca2fa2d6895e8621249a5351683b109f35866ab541
4
+ data.tar.gz: 2ec9d11faf8f3f1af7790d1f39e31cb92d8a79d2539fa7c935dbda63f3052d8c
5
5
  SHA512:
6
- metadata.gz: 32747a4d4a3d40f15e9802280d894b2270d4b78ac0a10859442d0fd3c7ae27a55032a92e072756cb4046964c9d53afcc9586ac954e2b3cf63d057d0a3652e5a8
7
- data.tar.gz: ca5005286253b7dea1546160ffb00c4b91a8b926512fbcfb7db594171435249e88408c982dc53fed4d90f87e68979bd9c2a2c1975f94495ca77c9ac878b22c1c
6
+ metadata.gz: 5afa3a7ca6947fad6f5a4f7693b524411875d212079621bc72bda73df36fdf090fcb566b83248302a83ecb38ee04189c57c812eaad4ffc99f94840eaf3abfd28
7
+ data.tar.gz: c29cf727d683f82e530ba1788630ae546ddb4470f3145f0a4dd1b6afe31555bb9363dfa52cc2a5e005a077f6e2aae24b3dc1aa44d1ff9c50fc9269987df0ec87
data/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ ## 0.1.7 (2021-03-26)
2
+
3
+ - Added `IndexScalarQuantizer` and `IndexIVFScalarQuantizer`
4
+
5
+ ## 0.1.6 (2021-03-21)
6
+
7
+ - Added support for inner product to `IndexIVFFlat` and `IndexIVFPQ`
8
+ - Added `ParameterSpace`
9
+ - Added `nprobe=` method
10
+
11
+ ## 0.1.5 (2021-03-14)
12
+
13
+ - Added `--with-optflags` option
14
+ - Added support for inner product to `IndexHNSWFlat`
15
+
16
+ ## 0.1.4 (2021-02-04)
17
+
18
+ - Updated Faiss to 1.7.0
19
+
20
+ ## 0.1.3 (2020-10-22)
21
+
22
+ - Updated Faiss to 1.6.4
23
+
1
24
  ## 0.1.2 (2020-08-17)
2
25
 
3
26
  - Updated Faiss to 1.6.3
data/LICENSE.txt CHANGED
@@ -1,7 +1,7 @@
1
1
  MIT License
2
2
 
3
3
  Copyright (c) Facebook, Inc. and its affiliates.
4
- Copyright (c) 2020 Andrew Kane
4
+ Copyright (c) 2020-2021 Andrew Kane
5
5
 
6
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Learn more about [Faiss](https://engineering.fb.com/data-infrastructure/faiss-a-library-for-efficient-similarity-search/)
6
6
 
7
- [![Build Status](https://travis-ci.org/ankane/faiss.svg?branch=master)](https://travis-ci.org/ankane/faiss)
7
+ [![Build Status](https://github.com/ankane/faiss/workflows/build/badge.svg?branch=master)](https://github.com/ankane/faiss/actions)
8
8
 
9
9
  ## Installation
10
10
 
@@ -24,7 +24,7 @@ Add this line to your application’s Gemfile:
24
24
  gem 'faiss'
25
25
  ```
26
26
 
27
- Faiss is not available for Windows
27
+ It can take a few minutes to compile the gem. Faiss is not available for Windows.
28
28
 
29
29
  ## Getting Started
30
30
 
@@ -97,12 +97,24 @@ Locality-sensitive hashing
97
97
  Faiss::IndexLSH.new(d, nbits)
98
98
  ```
99
99
 
100
+ Scalar quantizer (SQ) in flat mode
101
+
102
+ ```rb
103
+ Faiss::IndexScalarQuantizer.new(d, qtype)
104
+ ```
105
+
100
106
  Product quantizer (PQ) in flat mode
101
107
 
102
108
  ```rb
103
109
  Faiss::IndexPQ.new(d, m, nbits)
104
110
  ```
105
111
 
112
+ IVF and scalar quantizer
113
+
114
+ ```rb
115
+ Faiss::IndexIVFScalarQuantizer.new(quantizer, d, nlists, qtype)
116
+ ```
117
+
106
118
  IVFADC (coarse quantizer+PQ on residuals)
107
119
 
108
120
  ```rb
@@ -200,10 +212,10 @@ Data can be an array of arrays
200
212
  [[1, 2, 3], [4, 5, 6]]
201
213
  ```
202
214
 
203
- Or a Numo NArray
215
+ Or a Numo array
204
216
 
205
217
  ```ruby
206
- Numo::SFloat.new(3, 2).seq
218
+ Numo::NArray.cast([[1, 2, 3], [4, 5, 6]])
207
219
  ```
208
220
 
209
221
  ## History
data/ext/faiss/ext.cpp CHANGED
@@ -1,315 +1,19 @@
1
- #include <faiss/IndexFlat.h>
2
- #include <faiss/IndexHNSW.h>
3
- #include <faiss/IndexIVFFlat.h>
4
- #include <faiss/IndexLSH.h>
5
- #include <faiss/IndexScalarQuantizer.h>
6
- #include <faiss/IndexPQ.h>
7
- #include <faiss/IndexIVFPQ.h>
8
- #include <faiss/IndexIVFPQR.h>
9
-
10
- #include <faiss/IndexBinaryFlat.h>
11
- #include <faiss/IndexBinaryIVF.h>
12
- #include <faiss/index_factory.h>
13
- #include <faiss/index_io.h>
14
-
15
- #include <faiss/Clustering.h>
16
- #include <faiss/VectorTransform.h>
17
-
18
- #include <rice/Array.hpp>
19
- #include <rice/Class.hpp>
20
- #include <rice/Constructor.hpp>
21
1
  #include <rice/Module.hpp>
22
2
 
23
- float* float_array(Rice::Object o)
24
- {
25
- Rice::String s = o.call("to_binary");
26
- return (float*) s.c_str();
27
- }
28
-
29
- uint8_t* uint8_array(Rice::Object o)
30
- {
31
- Rice::String s = o.call("to_binary");
32
- return (uint8_t*) s.c_str();
33
- }
34
-
35
- // TODO return Numo::SFloat
36
- Rice::String result(float* ptr, int64_t length)
37
- {
38
- return Rice::String(std::string((char*) ptr, length * sizeof(float)));
39
- }
40
-
41
- // TODO return Numo::UInt8
42
- Rice::String result(uint8_t* ptr, int64_t length)
43
- {
44
- return Rice::String(std::string((char*) ptr, length * sizeof(uint8_t)));
45
- }
46
-
47
- // TODO return Numo::Int32
48
- Rice::String result(int32_t* ptr, int64_t length)
49
- {
50
- return Rice::String(std::string((char*) ptr, length * sizeof(int32_t)));
51
- }
52
-
53
- // TODO return Numo::Int64
54
- Rice::String result(int64_t* ptr, int64_t length)
55
- {
56
- return Rice::String(std::string((char*) ptr, length * sizeof(int64_t)));
57
- }
3
+ void init_index(Rice::Module& m);
4
+ void init_index_binary(Rice::Module& m);
5
+ void init_kmeans(Rice::Module& m);
6
+ void init_pca_matrix(Rice::Module& m);
7
+ void init_product_quantizer(Rice::Module& m);
58
8
 
59
9
  extern "C"
60
10
  void Init_ext()
61
11
  {
62
- Rice::Module rb_mFaiss = Rice::define_module("Faiss")
63
- .define_singleton_method(
64
- "index_binary_factory",
65
- *[](int d, const char *description) {
66
- return faiss::index_binary_factory(d, description);
67
- });
68
-
69
- Rice::define_class_under<faiss::Index>(rb_mFaiss, "Index")
70
- .define_method(
71
- "d",
72
- *[](faiss::Index &self) {
73
- return self.d;
74
- })
75
- .define_method(
76
- "trained?",
77
- *[](faiss::Index &self) {
78
- return self.is_trained;
79
- })
80
- .define_method(
81
- "ntotal",
82
- *[](faiss::Index &self) {
83
- return self.ntotal;
84
- })
85
- .define_method(
86
- "_train",
87
- *[](faiss::Index &self, int64_t n, Rice::Object o) {
88
- const float *x = float_array(o);
89
- self.train(n, x);
90
- })
91
- .define_method(
92
- "_add",
93
- *[](faiss::Index &self, int64_t n, Rice::Object o) {
94
- const float *x = float_array(o);
95
- self.add(n, x);
96
- })
97
- .define_method(
98
- "_search",
99
- *[](faiss::Index &self, int64_t n, Rice::Object o, int64_t k) {
100
- const float *x = float_array(o);
101
- float *distances = new float[k * n];
102
- int64_t *labels = new int64_t[k * n];
103
-
104
- self.search(n, x, k, distances, labels);
105
-
106
- auto dstr = result(distances, k * n);
107
- auto lstr = result(labels, k * n);
108
-
109
- Rice::Array ret;
110
- ret.push(dstr);
111
- ret.push(lstr);
112
- return ret;
113
- })
114
- .define_method(
115
- "save",
116
- *[](faiss::Index &self, const char *fname) {
117
- faiss::write_index(&self, fname);
118
- })
119
- .define_singleton_method(
120
- "load",
121
- *[](const char *fname) {
122
- return faiss::read_index(fname);
123
- });
124
-
125
- Rice::define_class_under<faiss::IndexBinary>(rb_mFaiss, "IndexBinary")
126
- .define_method(
127
- "d",
128
- *[](faiss::Index &self) {
129
- return self.d;
130
- })
131
- .define_method(
132
- "trained?",
133
- *[](faiss::IndexBinary &self) {
134
- return self.is_trained;
135
- })
136
- .define_method(
137
- "ntotal",
138
- *[](faiss::IndexBinary &self) {
139
- return self.ntotal;
140
- })
141
- .define_method(
142
- "_train",
143
- *[](faiss::IndexBinary &self, int64_t n, Rice::Object o) {
144
- const uint8_t *x = uint8_array(o);
145
- self.train(n, x);
146
- })
147
- .define_method(
148
- "_add",
149
- *[](faiss::IndexBinary &self, int64_t n, Rice::Object o) {
150
- const uint8_t *x = uint8_array(o);
151
- self.add(n, x);
152
- })
153
- .define_method(
154
- "_search",
155
- *[](faiss::IndexBinary &self, int64_t n, Rice::Object o, int64_t k) {
156
- const uint8_t *x = uint8_array(o);
157
- int32_t *distances = new int32_t[k * n];
158
- int64_t *labels = new int64_t[k * n];
159
-
160
- self.search(n, x, k, distances, labels);
161
-
162
- auto dstr = result(distances, k * n);
163
- auto lstr = result(labels, k * n);
164
-
165
- Rice::Array ret;
166
- ret.push(dstr);
167
- ret.push(lstr);
168
- return ret;
169
- })
170
- .define_method(
171
- "save",
172
- *[](faiss::IndexBinary &self, const char *fname) {
173
- faiss::write_index_binary(&self, fname);
174
- })
175
- .define_singleton_method(
176
- "load",
177
- *[](const char *fname) {
178
- return faiss::read_index_binary(fname);
179
- });
180
-
181
- Rice::define_class_under<faiss::IndexFlatL2, faiss::Index>(rb_mFaiss, "IndexFlatL2")
182
- .define_constructor(Rice::Constructor<faiss::IndexFlatL2, int64_t>());
183
-
184
- Rice::define_class_under<faiss::IndexFlatIP, faiss::Index>(rb_mFaiss, "IndexFlatIP")
185
- .define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
186
-
187
- Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(rb_mFaiss, "IndexHNSWFlat")
188
- .define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int>());
189
-
190
- Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(rb_mFaiss, "IndexIVFFlat")
191
- .define_constructor(Rice::Constructor<faiss::IndexIVFFlat, faiss::Index*, size_t, size_t>());
192
-
193
- Rice::define_class_under<faiss::IndexLSH, faiss::Index>(rb_mFaiss, "IndexLSH")
194
- .define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
195
-
196
- Rice::define_class_under<faiss::IndexScalarQuantizer, faiss::Index>(rb_mFaiss, "IndexScalarQuantizer")
197
- .define_constructor(Rice::Constructor<faiss::IndexScalarQuantizer, int, faiss::ScalarQuantizer::QuantizerType>());
198
-
199
- Rice::define_class_under<faiss::IndexPQ, faiss::Index>(rb_mFaiss, "IndexPQ")
200
- .define_constructor(Rice::Constructor<faiss::IndexPQ, int, size_t, size_t>());
201
-
202
- Rice::define_class_under<faiss::IndexIVFScalarQuantizer, faiss::Index>(rb_mFaiss, "IndexIVFScalarQuantizer")
203
- .define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
204
-
205
- Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(rb_mFaiss, "IndexIVFPQ")
206
- .define_constructor(Rice::Constructor<faiss::IndexIVFPQ, faiss::Index*, size_t, size_t, size_t, size_t>());
207
-
208
- Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(rb_mFaiss, "IndexIVFPQR")
209
- .define_constructor(Rice::Constructor<faiss::IndexIVFPQR, faiss::Index*, size_t, size_t, size_t, size_t, size_t, size_t>());
210
-
211
- Rice::define_class_under<faiss::IndexBinaryFlat, faiss::IndexBinary>(rb_mFaiss, "IndexBinaryFlat")
212
- .define_constructor(Rice::Constructor<faiss::IndexBinaryFlat, int64_t>());
213
-
214
- Rice::define_class_under<faiss::IndexBinaryIVF, faiss::IndexBinary>(rb_mFaiss, "IndexBinaryIVF")
215
- .define_constructor(Rice::Constructor<faiss::IndexBinaryIVF, faiss::IndexBinary*, size_t, size_t>());
216
-
217
- Rice::define_class_under<faiss::Clustering>(rb_mFaiss, "Kmeans")
218
- .define_constructor(Rice::Constructor<faiss::Clustering, int, int>())
219
- .define_method(
220
- "d",
221
- *[](faiss::Clustering &self) {
222
- return self.d;
223
- })
224
- .define_method(
225
- "k",
226
- *[](faiss::Clustering &self) {
227
- return self.k;
228
- })
229
- .define_method(
230
- "_centroids",
231
- *[](faiss::Clustering &self) {
232
- float *centroids = new float[self.k * self.d];
233
- for (size_t i = 0; i < self.centroids.size(); i++) {
234
- centroids[i] = self.centroids[i];
235
- }
236
- return result(centroids, self.k * self.d);
237
- })
238
- .define_method(
239
- "_train",
240
- *[](faiss::Clustering &self, int64_t n, Rice::Object o, faiss::Index & index) {
241
- const float *x = float_array(o);
242
- self.train(n, x, index);
243
- });
244
-
245
- Rice::define_class_under<faiss::PCAMatrix>(rb_mFaiss, "PCAMatrix")
246
- .define_constructor(Rice::Constructor<faiss::PCAMatrix, int, int>())
247
- .define_method(
248
- "d_in",
249
- *[](faiss::PCAMatrix &self) {
250
- return self.d_in;
251
- })
252
- .define_method(
253
- "d_out",
254
- *[](faiss::PCAMatrix &self) {
255
- return self.d_out;
256
- })
257
- .define_method(
258
- "_train",
259
- *[](faiss::PCAMatrix &self, int64_t n, Rice::Object o) {
260
- const float *x = float_array(o);
261
- self.train(n, x);
262
- })
263
- .define_method(
264
- "_apply",
265
- *[](faiss::PCAMatrix &self, int64_t n, Rice::Object o) {
266
- const float *x = float_array(o);
267
- float* res = self.apply(n, x);
268
- return result(res, n * self.d_out);
269
- });
12
+ auto m = Rice::define_module("Faiss");
270
13
 
271
- Rice::define_class_under<faiss::ProductQuantizer>(rb_mFaiss, "ProductQuantizer")
272
- .define_constructor(Rice::Constructor<faiss::ProductQuantizer, size_t, size_t, size_t>())
273
- .define_method(
274
- "d",
275
- *[](faiss::ProductQuantizer &self) {
276
- return self.d;
277
- })
278
- .define_method(
279
- "m",
280
- *[](faiss::ProductQuantizer &self) {
281
- return self.M;
282
- })
283
- .define_method(
284
- "_train",
285
- *[](faiss::ProductQuantizer &self, int n, Rice::Object o) {
286
- const float *x = float_array(o);
287
- self.train(n, x);
288
- })
289
- .define_method(
290
- "_compute_codes",
291
- *[](faiss::ProductQuantizer &self, int n, Rice::Object o) {
292
- const float *x = float_array(o);
293
- uint8_t *codes = new uint8_t[n * self.M];
294
- self.compute_codes(x, codes, n);
295
- return result(codes, n * self.M);
296
- })
297
- .define_method(
298
- "_decode",
299
- *[](faiss::ProductQuantizer &self, int n, Rice::Object o) {
300
- const uint8_t *codes = uint8_array(o);
301
- float *x = new float[n * self.d];
302
- self.decode(codes, x, n);
303
- return result(x, n * self.d);
304
- })
305
- .define_method(
306
- "save",
307
- *[](faiss::ProductQuantizer &self, const char *fname) {
308
- faiss::write_ProductQuantizer(&self, fname);
309
- })
310
- .define_singleton_method(
311
- "load",
312
- *[](const char *fname) {
313
- return faiss::read_ProductQuantizer(fname);
314
- });
14
+ init_index(m);
15
+ init_index_binary(m);
16
+ init_kmeans(m);
17
+ init_pca_matrix(m);
18
+ init_product_quantizer(m);
315
19
  }
data/ext/faiss/extconf.rb CHANGED
@@ -4,12 +4,12 @@ abort "BLAS not found" unless have_library("blas")
4
4
  abort "LAPACK not found" unless have_library("lapack")
5
5
  abort "OpenMP not found" unless have_library("omp") || have_library("gomp")
6
6
 
7
- $CXXFLAGS << " -std=c++11 -march=native -DFINTEGER=int"
7
+ $CXXFLAGS << " -std=c++11 -DFINTEGER=int " << with_config("optflags", "-march=native")
8
8
 
9
9
  ext = File.expand_path(".", __dir__)
10
- vendor = File.expand_path("../../vendor", __dir__)
10
+ vendor = File.expand_path("../../vendor/faiss", __dir__)
11
11
 
12
- $srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/impl,#{vendor}/faiss/utils}/*.{cpp}"]
12
+ $srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/{impl,invlists,utils}}/*.{cpp}"]
13
13
  $objs = $srcs.map { |v| v.sub(/cpp\z/, "o") }
14
14
  $INCFLAGS << " -I#{vendor}"
15
15
  $VPATH << vendor
@@ -0,0 +1,156 @@
1
+ #include <faiss/Index.h>
2
+ #include <faiss/IndexFlat.h>
3
+ #include <faiss/IndexHNSW.h>
4
+ #include <faiss/IndexIVFFlat.h>
5
+ #include <faiss/IndexLSH.h>
6
+ #include <faiss/IndexScalarQuantizer.h>
7
+ #include <faiss/IndexPQ.h>
8
+ #include <faiss/IndexIVFPQ.h>
9
+ #include <faiss/IndexIVFPQR.h>
10
+ #include <faiss/index_io.h>
11
+ #include <faiss/AutoTune.h>
12
+
13
+ #include <rice/Array.hpp>
14
+ #include <rice/Constructor.hpp>
15
+ #include <rice/Module.hpp>
16
+
17
+ #include "utils.h"
18
+
19
+ template<>
20
+ faiss::MetricType from_ruby<faiss::MetricType>(Rice::Object x)
21
+ {
22
+ auto s = x.to_s().str();
23
+ if (s == "inner_product") {
24
+ return faiss::METRIC_INNER_PRODUCT;
25
+ } else if (s == "l2") {
26
+ return faiss::METRIC_L2;
27
+ } else {
28
+ // TODO throw argument error
29
+ throw std::runtime_error("Invalid metric: " + s);
30
+ }
31
+ }
32
+
33
+ template<>
34
+ faiss::ScalarQuantizer::QuantizerType from_ruby<faiss::ScalarQuantizer::QuantizerType>(Rice::Object x)
35
+ {
36
+ auto s = x.to_s().str();
37
+ if (s == "qt_8bit") {
38
+ return faiss::ScalarQuantizer::QT_8bit;
39
+ } else if (s == "qt_4bit") {
40
+ return faiss::ScalarQuantizer::QT_4bit;
41
+ } else if (s == "qt_8bit_uniform") {
42
+ return faiss::ScalarQuantizer::QT_8bit_uniform;
43
+ } else if (s == "qt_4bit_uniform") {
44
+ return faiss::ScalarQuantizer::QT_4bit_uniform;
45
+ } else if (s == "qt_fp16") {
46
+ return faiss::ScalarQuantizer::QT_fp16;
47
+ } else if (s == "qt_8bit_direct") {
48
+ return faiss::ScalarQuantizer::QT_8bit_direct;
49
+ } else if (s == "qt_6bit") {
50
+ return faiss::ScalarQuantizer::QT_6bit;
51
+ } else {
52
+ // TODO throw argument error
53
+ throw std::runtime_error("Invalid quantizer type: " + s);
54
+ }
55
+ }
56
+
57
+ void init_index(Rice::Module& m) {
58
+ Rice::define_class_under<faiss::Index>(m, "Index")
59
+ .define_method(
60
+ "d",
61
+ *[](faiss::Index &self) {
62
+ return self.d;
63
+ })
64
+ .define_method(
65
+ "trained?",
66
+ *[](faiss::Index &self) {
67
+ return self.is_trained;
68
+ })
69
+ .define_method(
70
+ "ntotal",
71
+ *[](faiss::Index &self) {
72
+ return self.ntotal;
73
+ })
74
+ .define_method(
75
+ "_train",
76
+ *[](faiss::Index &self, int64_t n, Rice::Object o) {
77
+ const float *x = float_array(o);
78
+ self.train(n, x);
79
+ })
80
+ .define_method(
81
+ "_add",
82
+ *[](faiss::Index &self, int64_t n, Rice::Object o) {
83
+ const float *x = float_array(o);
84
+ self.add(n, x);
85
+ })
86
+ .define_method(
87
+ "_search",
88
+ *[](faiss::Index &self, int64_t n, Rice::Object o, int64_t k) {
89
+ const float *x = float_array(o);
90
+ float *distances = new float[k * n];
91
+ int64_t *labels = new int64_t[k * n];
92
+
93
+ self.search(n, x, k, distances, labels);
94
+
95
+ auto dstr = result(distances, k * n);
96
+ auto lstr = result(labels, k * n);
97
+
98
+ Rice::Array ret;
99
+ ret.push(dstr);
100
+ ret.push(lstr);
101
+ return ret;
102
+ })
103
+ .define_method(
104
+ "nprobe=",
105
+ *[](faiss::Index &self, double val) {
106
+ faiss::ParameterSpace().set_index_parameter(&self, "nprobe", val);
107
+ })
108
+ .define_method(
109
+ "save",
110
+ *[](faiss::Index &self, const char *fname) {
111
+ faiss::write_index(&self, fname);
112
+ })
113
+ .define_singleton_method(
114
+ "load",
115
+ *[](const char *fname) {
116
+ return faiss::read_index(fname);
117
+ });
118
+
119
+ Rice::define_class_under<faiss::IndexFlatL2, faiss::Index>(m, "IndexFlatL2")
120
+ .define_constructor(Rice::Constructor<faiss::IndexFlatL2, int64_t>());
121
+
122
+ Rice::define_class_under<faiss::IndexFlatIP, faiss::Index>(m, "IndexFlatIP")
123
+ .define_constructor(Rice::Constructor<faiss::IndexFlatIP, int64_t>());
124
+
125
+ Rice::define_class_under<faiss::IndexHNSWFlat, faiss::Index>(m, "IndexHNSWFlat")
126
+ .define_constructor(Rice::Constructor<faiss::IndexHNSWFlat, int, int, faiss::MetricType>(), (Rice::Arg("d"), Rice::Arg("M"), Rice::Arg("metric") = faiss::METRIC_L2));
127
+
128
+ Rice::define_class_under<faiss::IndexIVFFlat, faiss::Index>(m, "IndexIVFFlat")
129
+ .define_constructor(Rice::Constructor<faiss::IndexIVFFlat, faiss::Index*, size_t, size_t, faiss::MetricType>(), (Rice::Arg("quantizer"), Rice::Arg("d"), Rice::Arg("nlist"), Rice::Arg("metric") = faiss::METRIC_L2));
130
+
131
+ Rice::define_class_under<faiss::IndexLSH, faiss::Index>(m, "IndexLSH")
132
+ .define_constructor(Rice::Constructor<faiss::IndexLSH, int64_t, int>());
133
+
134
+ Rice::define_class_under<faiss::IndexScalarQuantizer, faiss::Index>(m, "IndexScalarQuantizer")
135
+ .define_constructor(Rice::Constructor<faiss::IndexScalarQuantizer, int, faiss::ScalarQuantizer::QuantizerType>());
136
+
137
+ Rice::define_class_under<faiss::IndexPQ, faiss::Index>(m, "IndexPQ")
138
+ .define_constructor(Rice::Constructor<faiss::IndexPQ, int, size_t, size_t>());
139
+
140
+ Rice::define_class_under<faiss::IndexIVFScalarQuantizer, faiss::Index>(m, "IndexIVFScalarQuantizer")
141
+ .define_constructor(Rice::Constructor<faiss::IndexIVFScalarQuantizer, faiss::Index*, size_t, size_t, faiss::ScalarQuantizer::QuantizerType>());
142
+
143
+ Rice::define_class_under<faiss::IndexIVFPQ, faiss::Index>(m, "IndexIVFPQ")
144
+ .define_constructor(Rice::Constructor<faiss::IndexIVFPQ, faiss::Index*, size_t, size_t, size_t, size_t, faiss::MetricType>(), (Rice::Arg("quantizer"), Rice::Arg("d"), Rice::Arg("nlist"), Rice::Arg("M"), Rice::Arg("nbits_per_idx"), Rice::Arg("metric") = faiss::METRIC_L2));
145
+
146
+ Rice::define_class_under<faiss::IndexIVFPQR, faiss::Index>(m, "IndexIVFPQR")
147
+ .define_constructor(Rice::Constructor<faiss::IndexIVFPQR, faiss::Index*, size_t, size_t, size_t, size_t, size_t, size_t>());
148
+
149
+ Rice::define_class_under<faiss::ParameterSpace>(m, "ParameterSpace")
150
+ .define_constructor(Rice::Constructor<faiss::ParameterSpace>())
151
+ .define_method(
152
+ "set_index_parameter",
153
+ *[](faiss::ParameterSpace& self, faiss::Index* index, const std::string& name, double val) {
154
+ self.set_index_parameter(index, name, val);
155
+ });
156
+ }