faiss 0.6.4 → 0.6.6

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 (301) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/ext/faiss/extconf.rb +2 -1
  4. data/ext/faiss/index.cpp +42 -0
  5. data/lib/faiss/version.rb +1 -1
  6. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  7. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  8. data/vendor/faiss/faiss/Clustering.h +29 -0
  9. data/vendor/faiss/faiss/IVFlib.cpp +147 -51
  10. data/vendor/faiss/faiss/IVFlib.h +21 -2
  11. data/vendor/faiss/faiss/Index.cpp +44 -0
  12. data/vendor/faiss/faiss/Index.h +18 -13
  13. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  14. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  15. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +85 -4
  17. data/vendor/faiss/faiss/IndexBinaryHNSW.h +19 -0
  18. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -11
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +34 -18
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +57 -1
  22. data/vendor/faiss/faiss/IndexFlat.h +31 -0
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +461 -13
  25. data/vendor/faiss/faiss/IndexHNSW.h +40 -5
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +45 -0
  27. data/vendor/faiss/faiss/IndexIDMap.h +15 -3
  28. data/vendor/faiss/faiss/IndexIVF.cpp +349 -68
  29. data/vendor/faiss/faiss/IndexIVF.h +90 -2
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +51 -29
  31. data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +22 -6
  34. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +2 -1
  35. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  36. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +78 -58
  37. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +7 -13
  38. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
  39. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +860 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
  46. data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
  47. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +7 -4
  48. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +12 -6
  49. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  50. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  51. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  52. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  53. data/vendor/faiss/faiss/IndexShards.cpp +10 -0
  54. data/vendor/faiss/faiss/IndexShards.h +5 -0
  55. data/vendor/faiss/faiss/SuperKMeans.cpp +59 -17
  56. data/vendor/faiss/faiss/SuperKMeans.h +9 -4
  57. data/vendor/faiss/faiss/VectorTransform.cpp +159 -11
  58. data/vendor/faiss/faiss/VectorTransform.h +24 -1
  59. data/vendor/faiss/faiss/clone_index.cpp +24 -0
  60. data/vendor/faiss/faiss/factory_tools.cpp +19 -1
  61. data/vendor/faiss/faiss/gpu/Dataset.h +164 -0
  62. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  63. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  64. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +3 -0
  65. data/vendor/faiss/faiss/gpu/GpuIndex.h +4 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +32 -13
  67. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +51 -22
  68. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  69. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  70. data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
  71. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
  72. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  73. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  74. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  75. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
  76. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  77. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  78. data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
  79. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +12 -1
  80. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +93 -1
  81. data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
  82. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +5 -1
  83. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  84. data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
  85. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
  86. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +39 -1
  87. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +107 -1
  88. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +11 -1
  89. data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
  90. data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
  91. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
  92. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
  93. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +3 -1
  94. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  95. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  96. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  97. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +267 -136
  98. data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
  99. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  100. data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
  101. data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
  102. data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
  103. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  104. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  105. data/vendor/faiss/faiss/impl/HNSW.cpp +48 -12
  106. data/vendor/faiss/faiss/impl/HNSW.h +74 -2
  107. data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
  108. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  109. data/vendor/faiss/faiss/impl/NNDescent.cpp +2 -1
  110. data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
  111. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  112. data/vendor/faiss/faiss/impl/Panorama.h +3 -2
  113. data/vendor/faiss/faiss/impl/PiPNN.cpp +824 -0
  114. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  115. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
  116. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +54 -11
  117. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
  118. data/vendor/faiss/faiss/impl/RaBitQUtils.h +30 -1
  119. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +91 -66
  120. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +15 -0
  121. data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
  122. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -0
  123. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +1 -6
  124. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  125. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  127. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
  128. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  129. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  130. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -0
  133. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  134. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  135. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  136. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +17 -129
  137. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  138. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +73 -6
  139. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
  140. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -4
  141. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -4
  142. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +10 -85
  143. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  144. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  145. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +106 -1
  146. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -6
  147. data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
  148. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +105 -78
  149. data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
  150. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  151. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  152. data/vendor/faiss/faiss/impl/index_read.cpp +318 -9
  153. data/vendor/faiss/faiss/impl/index_write.cpp +124 -5
  154. data/vendor/faiss/faiss/impl/io.cpp +1 -0
  155. data/vendor/faiss/faiss/impl/io_macros.h +2 -2
  156. data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
  157. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
  158. data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
  159. data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
  160. data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
  161. data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
  162. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
  163. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
  164. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
  165. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
  166. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
  167. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
  168. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  169. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  170. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  171. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  172. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  173. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  174. data/vendor/faiss/faiss/impl/platform_macros.h +91 -27
  175. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  176. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +84 -4
  177. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
  178. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  179. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  180. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  181. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  182. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  183. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  184. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +19 -0
  185. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  186. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  187. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  188. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  189. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  190. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  191. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  192. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
  193. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -6
  194. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -5
  195. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  196. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  197. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
  198. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +5 -1
  199. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  200. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +85 -37
  201. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +262 -110
  202. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +503 -122
  203. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +10 -155
  204. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +7 -701
  205. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +66 -57
  206. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +133 -127
  207. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  208. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  209. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  210. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  211. data/vendor/faiss/faiss/impl/simd_dispatch.h +7 -0
  212. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  213. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
  214. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  215. data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
  216. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  217. data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
  218. data/vendor/faiss/faiss/index_factory.cpp +106 -6
  219. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  220. data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
  221. data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
  222. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +15 -10
  223. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
  224. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
  225. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
  226. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
  227. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
  228. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
  229. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
  230. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
  231. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
  232. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +3 -2
  233. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  234. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  235. data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
  236. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  237. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  238. data/vendor/faiss/faiss/utils/distances.cpp +466 -43
  239. data/vendor/faiss/faiss/utils/distances.h +179 -2
  240. data/vendor/faiss/faiss/utils/distances_dispatch.h +6 -4
  241. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  242. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +0 -14
  243. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  244. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  247. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  248. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  249. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  250. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  251. data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
  252. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  253. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +0 -4
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +33 -5
  256. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +15 -30
  257. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  258. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +2 -1
  259. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  260. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  261. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  262. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  263. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  264. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  265. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  266. data/vendor/faiss/faiss/utils/rabitq_simd.h +171 -22
  267. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  268. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +81 -9
  269. data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
  270. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +108 -9
  271. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +111 -12
  272. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +353 -71
  273. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  274. data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
  275. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +106 -0
  276. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  277. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  278. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  279. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  280. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  281. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  282. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  283. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  284. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  285. data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
  286. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +185 -51
  287. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +194 -66
  288. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512_vpopcnt.cpp +51 -14
  289. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  290. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  291. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  292. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  293. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +0 -4
  294. data/vendor/faiss/faiss/utils/simd_levels.cpp +22 -6
  295. data/vendor/faiss/faiss/utils/simd_levels.h +74 -1
  296. data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
  297. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  298. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  299. data/vendor/faiss/faiss/utils/utils.cpp +84 -0
  300. data/vendor/faiss/faiss/utils/utils.h +24 -0
  301. metadata +52 -1
@@ -0,0 +1,121 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ #include <faiss/invlists/DiskIOBackend.h>
9
+
10
+ #include <libaio.h>
11
+ #include <sys/types.h>
12
+ #include <unistd.h>
13
+
14
+ #include <algorithm>
15
+ #include <cerrno>
16
+ #include <cstring>
17
+ #include <ctime>
18
+ #include <vector>
19
+
20
+ #include <faiss/impl/FaissAssert.h>
21
+
22
+ namespace faiss {
23
+
24
+ DiskIOBackend::DiskIOBackend(int max_events_in) {
25
+ FAISS_THROW_IF_NOT(max_events_in > 0);
26
+ const int ret = io_setup(max_events_in, &ctx);
27
+ FAISS_THROW_IF_NOT_FMT(
28
+ ret == 0,
29
+ "io_setup failed: %s%s",
30
+ strerror(-ret),
31
+ ret == -EAGAIN ? " (too many libaio contexts, see fs.aio-max-nr)"
32
+ : "");
33
+ pid = getpid();
34
+ max_events = max_events_in;
35
+ iocbs.resize(max_events);
36
+ iocb_ptrs.resize(max_events);
37
+ events.resize(max_events);
38
+ }
39
+
40
+ DiskIOBackend::~DiskIOBackend() {
41
+ // blocks until the reads in flight are done, so the caller can free their
42
+ // buffers afterwards. The context of a parent process is not ours to
43
+ // destroy.
44
+ if (owned_by_this_process()) {
45
+ io_destroy(ctx);
46
+ }
47
+ }
48
+
49
+ void DiskIOBackend::stage(
50
+ int fd,
51
+ void* buf,
52
+ size_t len,
53
+ size_t offset,
54
+ void* user_data) {
55
+ FAISS_THROW_IF_NOT_MSG(
56
+ n_staged < max_events, "DiskIOBackend: too many reads staged");
57
+ iocb* cb = &iocbs[n_staged];
58
+ io_prep_pread(cb, fd, buf, len, static_cast<long long>(offset));
59
+ cb->data = user_data;
60
+ iocb_ptrs[n_staged] = cb;
61
+ n_staged++;
62
+ }
63
+
64
+ void DiskIOBackend::submit() {
65
+ int done = 0;
66
+ while (done < n_staged) {
67
+ const int ret =
68
+ io_submit(ctx, n_staged - done, iocb_ptrs.data() + done);
69
+ if (ret == -EINTR || ret == -EAGAIN) {
70
+ continue; // transient, the queue is sized for all reads in flight
71
+ }
72
+ FAISS_THROW_IF_NOT_FMT(
73
+ ret > 0,
74
+ "io_submit failed: %s",
75
+ ret < 0 ? strerror(-ret) : "no read accepted");
76
+ done += ret;
77
+ n_in_flight += ret;
78
+ }
79
+ n_staged = 0;
80
+ }
81
+
82
+ int DiskIOBackend::reap(void** user_data, long* res, int max, int min_nr) {
83
+ max = std::min(max, max_events);
84
+ min_nr = std::min(min_nr, std::min(max, n_in_flight));
85
+ struct timespec no_wait = {0, 0};
86
+ const int ret = io_getevents(
87
+ ctx, min_nr, max, events.data(), min_nr > 0 ? nullptr : &no_wait);
88
+ if (ret == -EINTR) {
89
+ return 0;
90
+ }
91
+ FAISS_THROW_IF_NOT_FMT(ret >= 0, "io_getevents failed: %s", strerror(-ret));
92
+ n_in_flight -= ret;
93
+ for (int i = 0; i < ret; i++) {
94
+ user_data[i] = events[i].data;
95
+ // bytes read or a negative errno, in an unsigned field
96
+ res[i] = static_cast<long>(events[i].res);
97
+ }
98
+ return ret;
99
+ }
100
+
101
+ bool DiskIOBackend::wait_all() noexcept {
102
+ n_staged = 0;
103
+ while (n_in_flight > 0) {
104
+ const int ret = io_getevents(
105
+ ctx, n_in_flight, max_events, events.data(), nullptr);
106
+ if (ret == -EINTR) {
107
+ continue;
108
+ }
109
+ if (ret < 0) {
110
+ return false;
111
+ }
112
+ n_in_flight -= ret;
113
+ }
114
+ return true;
115
+ }
116
+
117
+ bool DiskIOBackend::owned_by_this_process() const {
118
+ return pid == getpid();
119
+ }
120
+
121
+ } // namespace faiss
@@ -0,0 +1,71 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ #pragma once
9
+
10
+ #include <libaio.h>
11
+ #include <sys/types.h>
12
+
13
+ #include <cstddef>
14
+ #include <vector>
15
+
16
+ namespace faiss {
17
+
18
+ /** Asynchronous reads with Linux libaio, for one thread.
19
+ *
20
+ * Reads are staged with stage(), sent to the kernel together with submit(),
21
+ * and collected with reap(). With a file opened with
22
+ * O_DIRECT, the buffer address, offset and length of every read must be
23
+ * multiples of the device sector size.
24
+ */
25
+ struct DiskIOBackend {
26
+ io_context_t ctx = nullptr;
27
+ pid_t pid = 0; ///< process that created ctx
28
+ int max_events = 0; ///< at most max_events reads can be in flight
29
+ int n_staged = 0; ///< reads staged for the next submit()
30
+ int n_in_flight = 0; ///< submitted and not reaped
31
+ std::vector<iocb> iocbs;
32
+ std::vector<iocb*> iocb_ptrs;
33
+ std::vector<io_event> events;
34
+
35
+ explicit DiskIOBackend(int max_events);
36
+ ~DiskIOBackend();
37
+
38
+ DiskIOBackend(const DiskIOBackend&) = delete;
39
+ DiskIOBackend& operator=(const DiskIOBackend&) = delete;
40
+
41
+ /// add a read of len bytes at offset into buf to the next submit();
42
+ /// user_data identifies it in reap()
43
+ void stage(int fd, void* buf, size_t len, size_t offset, void* user_data);
44
+
45
+ /// send the staged reads to the kernel
46
+ void submit();
47
+
48
+ /** Collect up to max completed reads, waiting until at least min_nr
49
+ * have completed (without waiting if min_nr is 0). min_nr is capped by
50
+ * max and by the reads in flight, and a signal can end the wait early.
51
+ *
52
+ * @param user_data output, the user_data of each completed read
53
+ * @param res output, the bytes read, or a negative errno
54
+ * @return number of completed reads
55
+ */
56
+ int reap(void** user_data, long* res, int max, int min_nr = 0);
57
+
58
+ /** Drop the staged reads and wait for the submitted ones to complete,
59
+ * ignoring their results, so that their buffers can be freed and the
60
+ * context reused.
61
+ *
62
+ * @return false if waiting failed: reads may still be in flight
63
+ */
64
+ bool wait_all() noexcept;
65
+
66
+ /// false in a process forked after the context was created: the context
67
+ /// belongs to the parent process and cannot be used
68
+ bool owned_by_this_process() const;
69
+ };
70
+
71
+ } // namespace faiss
@@ -127,7 +127,9 @@ size_t InvertedLists::copy_subset_to(
127
127
  } else if (subset_type == SUBSET_TYPE_ID_MOD) {
128
128
  for (size_t i = 0; i < n; i++) {
129
129
  idx_t id = ids_in[i];
130
- if (id % a1 == a2) {
130
+ // C++ gives a negative remainder, so a plain `id % a1` never
131
+ // matches for a negative id. Fold it into [0, a1) instead.
132
+ if (((id % a1) + a1) % a1 == a2) {
131
133
  oivf.add_entry(
132
134
  list_no,
133
135
  get_single_id(list_no, i),
@@ -296,23 +298,24 @@ size_t ArrayInvertedLists::add_entries(
296
298
  }
297
299
 
298
300
  size_t ArrayInvertedLists::list_size(size_t list_no) const {
299
- assert(list_no < nlist);
301
+ FAISS_THROW_IF_NOT(list_no < nlist);
300
302
  return ids[list_no].size();
301
303
  }
302
304
 
303
305
  bool ArrayInvertedLists::is_empty(size_t list_no, void* inverted_list_context)
304
306
  const {
305
307
  FAISS_THROW_IF_NOT(inverted_list_context == nullptr);
308
+ FAISS_THROW_IF_NOT(list_no < nlist);
306
309
  return ids[list_no].size() == 0;
307
310
  }
308
311
 
309
312
  const uint8_t* ArrayInvertedLists::get_codes(size_t list_no) const {
310
- assert(list_no < nlist);
313
+ FAISS_THROW_IF_NOT(list_no < nlist);
311
314
  return codes[list_no].data();
312
315
  }
313
316
 
314
317
  const idx_t* ArrayInvertedLists::get_ids(size_t list_no) const {
315
- assert(list_no < nlist);
318
+ FAISS_THROW_IF_NOT(list_no < nlist);
316
319
  return ids[list_no].data();
317
320
  }
318
321
 
@@ -929,10 +932,12 @@ void StopWordsInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist_in)
929
932
  * CappedInvertedLists implementation
930
933
  ******************************************/
931
934
 
932
- CappedInvertedLists::CappedInvertedLists(InvertedLists* il0, size_t maxsize)
933
- : InvertedLists(il0->nlist, il0->code_size),
934
- il0(il0),
935
- maxsize(maxsize) {}
935
+ CappedInvertedLists::CappedInvertedLists(
936
+ InvertedLists* il0_in,
937
+ size_t maxsize_in)
938
+ : InvertedLists(il0_in->nlist, il0_in->code_size),
939
+ il0(il0_in),
940
+ maxsize(maxsize_in) {}
936
941
 
937
942
  size_t CappedInvertedLists::list_size(size_t list_no) const {
938
943
  size_t sz = il0->list_size(list_no);
@@ -972,9 +977,9 @@ const uint8_t* CappedInvertedLists::get_single_code(
972
977
  return il0->get_single_code(list_no, offset);
973
978
  }
974
979
 
975
- void CappedInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist)
980
+ void CappedInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist_in)
976
981
  const {
977
- il0->prefetch_lists(list_nos, nlist);
982
+ il0->prefetch_lists(list_nos, nlist_in);
978
983
  }
979
984
 
980
985
  size_t CappedInvertedLists::add_entries(
@@ -17,6 +17,10 @@
17
17
  #include <faiss/invlists/OnDiskInvertedLists.h>
18
18
  #endif // !_WIN32
19
19
 
20
+ #ifdef FAISS_ENABLE_DISK_IVF
21
+ #include <faiss/invlists/RaBitQSSDInvertedLists.h>
22
+ #endif
23
+
20
24
  namespace faiss {
21
25
 
22
26
  /**********************************************************
@@ -35,6 +39,9 @@ struct IOHookTable : std::vector<InvertedListsIOHook*> {
35
39
  IOHookTable() {
36
40
  #ifndef _WIN32
37
41
  push_back(new OnDiskInvertedListsIOHook());
42
+ #endif
43
+ #ifdef FAISS_ENABLE_DISK_IVF
44
+ push_back(new RaBitQSSDInvertedListsIOHook());
38
45
  #endif
39
46
  push_back(new BlockInvertedListsIOHook());
40
47
  }
@@ -90,6 +97,25 @@ void InvertedListsIOHook::print_callbacks() {
90
97
  }
91
98
  }
92
99
 
100
+ std::string ondisk_same_dir_filename(IOReader* f, const std::string& filename) {
101
+ FileIOReader* reader = dynamic_cast<FileIOReader*>(f);
102
+ FAISS_THROW_IF_NOT_MSG(
103
+ reader,
104
+ "IO_FLAG_ONDISK_SAME_DIR only supported "
105
+ "when reading from file");
106
+ std::string dirname = "./";
107
+ size_t slash = reader->name.find_last_of('/');
108
+ if (slash != std::string::npos) {
109
+ dirname = reader->name.substr(0, slash + 1);
110
+ }
111
+ std::string basename = filename;
112
+ slash = basename.find_last_of('/');
113
+ if (slash != std::string::npos) {
114
+ basename = basename.substr(slash + 1);
115
+ }
116
+ return dirname + basename;
117
+ }
118
+
93
119
  InvertedLists* InvertedListsIOHook::read_ArrayInvertedLists(
94
120
  IOReader*,
95
121
  int,
@@ -59,4 +59,9 @@ struct InvertedListsIOHook {
59
59
  static InvertedListsIOHook* lookup_classname(const std::string& classname);
60
60
  };
61
61
 
62
+ /** For IO_FLAG_ONDISK_SAME_DIR: the path of a data file with the base name of
63
+ * filename, in the directory of the index file f is reading from.
64
+ */
65
+ std::string ondisk_same_dir_filename(IOReader* f, const std::string& filename);
66
+
62
67
  } // namespace faiss
@@ -11,6 +11,7 @@
11
11
 
12
12
  #include <pthread.h>
13
13
 
14
+ #include <cerrno>
14
15
  #include <memory>
15
16
  #include <unordered_set>
16
17
 
@@ -152,22 +153,22 @@ struct OnDiskInvertedLists::OngoingPrefetch {
152
153
  if (list_no == -1) {
153
154
  return false;
154
155
  }
155
- const OnDiskInvertedLists* od = pf->od;
156
- od->locks->lock_1(list_no);
157
- size_t n = od->list_size(list_no);
158
- const idx_t* idx = od->get_ids(list_no);
159
- const uint8_t* codes = od->get_codes(list_no);
156
+ const OnDiskInvertedLists* invlists = pf->od;
157
+ invlists->locks->lock_1(static_cast<int>(list_no));
158
+ size_t n = invlists->list_size(list_no);
159
+ const idx_t* idx = invlists->get_ids(list_no);
160
+ const uint8_t* codes = invlists->get_codes(list_no);
160
161
  int cs = 0;
161
162
  for (size_t i = 0; i < n; i++) {
162
163
  cs += idx[i];
163
164
  }
164
165
  const idx_t* codes8 = (const idx_t*)codes;
165
- size_t n8 = n * od->code_size / 8;
166
+ size_t n8 = n * invlists->code_size / 8;
166
167
 
167
168
  for (size_t i = 0; i < n8; i++) {
168
169
  cs += codes8[i];
169
170
  }
170
- od->locks->unlock_1(list_no);
171
+ invlists->locks->unlock_1(static_cast<int>(list_no));
171
172
 
172
173
  global_cs += cs & 1;
173
174
  return true;
@@ -737,23 +738,7 @@ InvertedLists* OnDiskInvertedListsIOHook::read(IOReader* f, int io_flags)
737
738
  od->filename.assign(x.begin(), x.end());
738
739
 
739
740
  if (io_flags & IO_FLAG_ONDISK_SAME_DIR) {
740
- FileIOReader* reader = dynamic_cast<FileIOReader*>(f);
741
- FAISS_THROW_IF_NOT_MSG(
742
- reader,
743
- "IO_FLAG_ONDISK_SAME_DIR only supported "
744
- "when reading from file");
745
- std::string indexname = reader->name;
746
- std::string dirname = "./";
747
- size_t slash = indexname.find_last_of('/');
748
- if (slash != std::string::npos) {
749
- dirname = indexname.substr(0, slash + 1);
750
- }
751
- std::string filename = od->filename;
752
- slash = filename.find_last_of('/');
753
- if (slash != std::string::npos) {
754
- filename = filename.substr(slash + 1);
755
- }
756
- filename = dirname + filename;
741
+ std::string filename = ondisk_same_dir_filename(f, od->filename);
757
742
  printf("IO_FLAG_ONDISK_SAME_DIR: "
758
743
  "updating ondisk filename from %s to %s\n",
759
744
  od->filename.c_str(),