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
@@ -7,17 +7,78 @@
7
7
 
8
8
  #include <faiss/utils/rabitq_simd.h>
9
9
 
10
- #ifdef COMPILE_SIMD_ARM_NEON
10
+ #include <arm_neon.h>
11
+
12
+ #include <array>
13
+
14
+ #include <faiss/impl/FaissAssert.h>
11
15
 
12
16
  namespace faiss::rabitq {
13
17
 
18
+ namespace {
19
+
20
+ // One accumulator per query bit plane. RaBitQuantizer caps qb at 8, so the
21
+ // accumulator arrays below are fixed size rather than heap allocated.
22
+ constexpr size_t kMaxQueryBits = 8;
23
+
24
+ inline uint32x4_t accumulate_popcount(uint32x4_t acc, uint8x16_t value) {
25
+ const uint16x8_t pair_counts = vpaddlq_u8(vcntq_u8(value));
26
+ return vpadalq_u16(acc, pair_counts);
27
+ }
28
+
29
+ inline uint64_t reduce_popcount(uint32x4_t acc) {
30
+ return vaddlvq_u32(acc);
31
+ }
32
+
33
+ } // namespace
34
+
14
35
  template <>
15
36
  uint64_t bitwise_and_dot_product<SIMDLevel::ARM_NEON>(
16
37
  const uint8_t* query,
17
38
  const uint8_t* data,
18
39
  size_t size,
19
40
  size_t qb) {
20
- return bitwise_and_dot_product<SIMDLevel::NONE>(query, data, size, qb);
41
+ FAISS_THROW_IF_NOT(qb <= kMaxQueryBits);
42
+ std::array<uint32x4_t, kMaxQueryBits> accumulators;
43
+ for (size_t j = 0; j < qb; j++) {
44
+ accumulators[j] = vdupq_n_u32(0);
45
+ }
46
+
47
+ size_t offset = 0;
48
+ for (; offset + 16 <= size; offset += 16) {
49
+ const uint8x16_t value = vld1q_u8(data + offset);
50
+ for (size_t j = 0; j < qb; j++) {
51
+ const uint8x16_t query_value = vld1q_u8(query + j * size + offset);
52
+ accumulators[j] = accumulate_popcount(
53
+ accumulators[j], vandq_u8(query_value, value));
54
+ }
55
+ }
56
+
57
+ uint64_t result = 0;
58
+ for (size_t j = 0; j < qb; j++) {
59
+ result += reduce_popcount(accumulators[j]) << j;
60
+ }
61
+ for (; offset + 8 <= size; offset += 8) {
62
+ uint64_t value;
63
+ memcpy(&value, data + offset, sizeof(value));
64
+ for (size_t j = 0; j < qb; j++) {
65
+ uint64_t query_value;
66
+ memcpy(&query_value,
67
+ query + j * size + offset,
68
+ sizeof(query_value));
69
+ result += static_cast<uint64_t>(popcount64(query_value & value))
70
+ << j;
71
+ }
72
+ }
73
+ for (; offset < size; offset++) {
74
+ const uint8_t value = data[offset];
75
+ for (size_t j = 0; j < qb; j++) {
76
+ result += static_cast<uint64_t>(
77
+ popcount32(query[j * size + offset] & value))
78
+ << j;
79
+ }
80
+ }
81
+ return result;
21
82
  }
22
83
 
23
84
  template <>
@@ -27,8 +88,52 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
27
88
  const uint8_t* data,
28
89
  size_t size,
29
90
  size_t qb) {
30
- return bitwise_and_dot_product_with_popcount<SIMDLevel::NONE>(
31
- query, data, size, qb);
91
+ FAISS_THROW_IF_NOT(qb <= kMaxQueryBits);
92
+ std::array<uint32x4_t, kMaxQueryBits> dot_accumulators;
93
+ for (size_t j = 0; j < qb; j++) {
94
+ dot_accumulators[j] = vdupq_n_u32(0);
95
+ }
96
+ uint32x4_t popcount_accumulator = vdupq_n_u32(0);
97
+
98
+ size_t offset = 0;
99
+ for (; offset + 16 <= size; offset += 16) {
100
+ const uint8x16_t value = vld1q_u8(data + offset);
101
+ popcount_accumulator = accumulate_popcount(popcount_accumulator, value);
102
+ for (size_t j = 0; j < qb; j++) {
103
+ const uint8x16_t query_value = vld1q_u8(query + j * size + offset);
104
+ dot_accumulators[j] = accumulate_popcount(
105
+ dot_accumulators[j], vandq_u8(query_value, value));
106
+ }
107
+ }
108
+
109
+ uint64_t dot_product = 0;
110
+ for (size_t j = 0; j < qb; j++) {
111
+ dot_product += reduce_popcount(dot_accumulators[j]) << j;
112
+ }
113
+ uint64_t popcount_sum = reduce_popcount(popcount_accumulator);
114
+ for (; offset + 8 <= size; offset += 8) {
115
+ uint64_t value;
116
+ memcpy(&value, data + offset, sizeof(value));
117
+ popcount_sum += popcount64(value);
118
+ for (size_t j = 0; j < qb; j++) {
119
+ uint64_t query_value;
120
+ memcpy(&query_value,
121
+ query + j * size + offset,
122
+ sizeof(query_value));
123
+ dot_product +=
124
+ static_cast<uint64_t>(popcount64(query_value & value)) << j;
125
+ }
126
+ }
127
+ for (; offset < size; offset++) {
128
+ const uint8_t value = data[offset];
129
+ popcount_sum += popcount32(value);
130
+ for (size_t j = 0; j < qb; j++) {
131
+ dot_product += static_cast<uint64_t>(
132
+ popcount32(query[j * size + offset] & value))
133
+ << j;
134
+ }
135
+ }
136
+ return {dot_product, popcount_sum};
32
137
  }
33
138
 
34
139
  template <>
@@ -37,18 +142,185 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::ARM_NEON>(
37
142
  const uint8_t* data,
38
143
  size_t size,
39
144
  size_t qb) {
40
- return bitwise_xor_dot_product<SIMDLevel::NONE>(query, data, size, qb);
145
+ FAISS_THROW_IF_NOT(qb <= kMaxQueryBits);
146
+ std::array<uint32x4_t, kMaxQueryBits> accumulators;
147
+ for (size_t j = 0; j < qb; j++) {
148
+ accumulators[j] = vdupq_n_u32(0);
149
+ }
150
+
151
+ size_t offset = 0;
152
+ for (; offset + 16 <= size; offset += 16) {
153
+ const uint8x16_t value = vld1q_u8(data + offset);
154
+ for (size_t j = 0; j < qb; j++) {
155
+ const uint8x16_t query_value = vld1q_u8(query + j * size + offset);
156
+ accumulators[j] = accumulate_popcount(
157
+ accumulators[j], veorq_u8(query_value, value));
158
+ }
159
+ }
160
+
161
+ uint64_t result = 0;
162
+ for (size_t j = 0; j < qb; j++) {
163
+ result += reduce_popcount(accumulators[j]) << j;
164
+ }
165
+ for (; offset + 8 <= size; offset += 8) {
166
+ uint64_t value;
167
+ memcpy(&value, data + offset, sizeof(value));
168
+ for (size_t j = 0; j < qb; j++) {
169
+ uint64_t query_value;
170
+ memcpy(&query_value,
171
+ query + j * size + offset,
172
+ sizeof(query_value));
173
+ result += static_cast<uint64_t>(popcount64(query_value ^ value))
174
+ << j;
175
+ }
176
+ }
177
+ for (; offset < size; offset++) {
178
+ const uint8_t value = data[offset];
179
+ for (size_t j = 0; j < qb; j++) {
180
+ result += static_cast<uint64_t>(
181
+ popcount32(query[j * size + offset] ^ value))
182
+ << j;
183
+ }
184
+ }
185
+ return result;
41
186
  }
42
187
 
43
188
  template <>
44
189
  uint64_t popcount<SIMDLevel::ARM_NEON>(const uint8_t* data, size_t size) {
45
- return popcount<SIMDLevel::NONE>(data, size);
190
+ uint32x4_t accumulator = vdupq_n_u32(0);
191
+ size_t offset = 0;
192
+ for (; offset + 16 <= size; offset += 16) {
193
+ accumulator = accumulate_popcount(accumulator, vld1q_u8(data + offset));
194
+ }
195
+
196
+ uint64_t result = reduce_popcount(accumulator);
197
+ for (; offset + 8 <= size; offset += 8) {
198
+ uint64_t value;
199
+ memcpy(&value, data + offset, sizeof(value));
200
+ result += popcount64(value);
201
+ }
202
+ for (; offset < size; offset++) {
203
+ result += popcount32(data[offset]);
204
+ }
205
+ return result;
46
206
  }
47
207
 
48
208
  } // namespace faiss::rabitq
49
209
 
50
210
  namespace faiss::rabitq::multibit {
51
211
 
212
+ namespace {
213
+
214
+ inline float horizontal_sum(float32x4_t low, float32x4_t high) {
215
+ return vaddvq_f32(vaddq_f32(low, high));
216
+ }
217
+
218
+ inline float ip_1exbit_neon(
219
+ const uint8_t* __restrict sign_bits,
220
+ const uint8_t* __restrict ex_code,
221
+ const float* __restrict rotated_q,
222
+ size_t d,
223
+ float cb) {
224
+ const uint32x4_t low_bit_positions = {1, 2, 4, 8};
225
+ const uint32x4_t high_bit_positions = {16, 32, 64, 128};
226
+ const uint32x4_t sign_weight = vdupq_n_u32(2);
227
+ const uint32x4_t ex_weight = vdupq_n_u32(1);
228
+ const float32x4_t bias = vdupq_n_f32(cb);
229
+ float32x4_t low_accumulator = vdupq_n_f32(0.0f);
230
+ float32x4_t high_accumulator = vdupq_n_f32(0.0f);
231
+
232
+ size_t i = 0;
233
+ for (; i + 8 <= d; i += 8) {
234
+ const uint32x4_t sign = vdupq_n_u32(sign_bits[i / 8]);
235
+ const uint32x4_t extra = vdupq_n_u32(ex_code[i / 8]);
236
+
237
+ const uint32x4_t low_values = vaddq_u32(
238
+ vandq_u32(vtstq_u32(sign, low_bit_positions), sign_weight),
239
+ vandq_u32(vtstq_u32(extra, low_bit_positions), ex_weight));
240
+ const uint32x4_t high_values = vaddq_u32(
241
+ vandq_u32(vtstq_u32(sign, high_bit_positions), sign_weight),
242
+ vandq_u32(vtstq_u32(extra, high_bit_positions), ex_weight));
243
+
244
+ const float32x4_t low_reconstruction =
245
+ vaddq_f32(vcvtq_f32_u32(low_values), bias);
246
+ const float32x4_t high_reconstruction =
247
+ vaddq_f32(vcvtq_f32_u32(high_values), bias);
248
+ low_accumulator = vfmaq_f32(
249
+ low_accumulator, vld1q_f32(rotated_q + i), low_reconstruction);
250
+ high_accumulator = vfmaq_f32(
251
+ high_accumulator,
252
+ vld1q_f32(rotated_q + i + 4),
253
+ high_reconstruction);
254
+ }
255
+
256
+ float result = horizontal_sum(low_accumulator, high_accumulator);
257
+ result += ip_scalar(sign_bits, ex_code, rotated_q, i, d, 1, cb);
258
+ return result;
259
+ }
260
+
261
+ template <size_t ExBits>
262
+ inline float ip_multibit_neon(
263
+ const uint8_t* __restrict sign_bits,
264
+ const uint8_t* __restrict ex_code,
265
+ const float* __restrict rotated_q,
266
+ size_t d,
267
+ float cb) {
268
+ static_assert(ExBits >= 2 && ExBits <= 7);
269
+ const uint32x4_t low_bit_positions = {1, 2, 4, 8};
270
+ const uint32x4_t high_bit_positions = {16, 32, 64, 128};
271
+ const uint32x4_t sign_weight =
272
+ vdupq_n_u32(static_cast<uint32_t>(1u << ExBits));
273
+ const uint64x2_t code_mask =
274
+ vdupq_n_u64(static_cast<uint64_t>((1u << ExBits) - 1));
275
+ constexpr int64_t bit_width = static_cast<int64_t>(ExBits);
276
+ const int64x2_t shifts_01 = {0, -bit_width};
277
+ const int64x2_t shifts_23 = {-2 * bit_width, -3 * bit_width};
278
+ const int64x2_t shifts_45 = {-4 * bit_width, -5 * bit_width};
279
+ const int64x2_t shifts_67 = {-6 * bit_width, -7 * bit_width};
280
+ const float32x4_t bias = vdupq_n_f32(cb);
281
+ float32x4_t low_accumulator = vdupq_n_f32(0.0f);
282
+ float32x4_t high_accumulator = vdupq_n_f32(0.0f);
283
+
284
+ size_t i = 0;
285
+ for (; i + 8 <= d; i += 8) {
286
+ uint64_t packed_codes = 0;
287
+ memcpy(&packed_codes, ex_code + (i / 8) * ExBits, ExBits);
288
+ const uint64x2_t packed = vdupq_n_u64(packed_codes);
289
+
290
+ const uint32x4_t low_codes = vcombine_u32(
291
+ vmovn_u64(vandq_u64(vshlq_u64(packed, shifts_01), code_mask)),
292
+ vmovn_u64(vandq_u64(vshlq_u64(packed, shifts_23), code_mask)));
293
+ const uint32x4_t high_codes = vcombine_u32(
294
+ vmovn_u64(vandq_u64(vshlq_u64(packed, shifts_45), code_mask)),
295
+ vmovn_u64(vandq_u64(vshlq_u64(packed, shifts_67), code_mask)));
296
+
297
+ const uint32x4_t sign = vdupq_n_u32(sign_bits[i / 8]);
298
+ const uint32x4_t low_values = vaddq_u32(
299
+ low_codes,
300
+ vandq_u32(vtstq_u32(sign, low_bit_positions), sign_weight));
301
+ const uint32x4_t high_values = vaddq_u32(
302
+ high_codes,
303
+ vandq_u32(vtstq_u32(sign, high_bit_positions), sign_weight));
304
+
305
+ const float32x4_t low_reconstruction =
306
+ vaddq_f32(vcvtq_f32_u32(low_values), bias);
307
+ const float32x4_t high_reconstruction =
308
+ vaddq_f32(vcvtq_f32_u32(high_values), bias);
309
+ low_accumulator = vfmaq_f32(
310
+ low_accumulator, vld1q_f32(rotated_q + i), low_reconstruction);
311
+ high_accumulator = vfmaq_f32(
312
+ high_accumulator,
313
+ vld1q_f32(rotated_q + i + 4),
314
+ high_reconstruction);
315
+ }
316
+
317
+ float result = horizontal_sum(low_accumulator, high_accumulator);
318
+ result += ip_scalar(sign_bits, ex_code, rotated_q, i, d, ExBits, cb);
319
+ return result;
320
+ }
321
+
322
+ } // namespace
323
+
52
324
  template <>
53
325
  float compute_inner_product<SIMDLevel::ARM_NEON>(
54
326
  const uint8_t* __restrict sign_bits,
@@ -57,10 +329,25 @@ float compute_inner_product<SIMDLevel::ARM_NEON>(
57
329
  size_t d,
58
330
  size_t ex_bits,
59
331
  float cb) {
60
- return compute_inner_product<SIMDLevel::NONE>(
61
- sign_bits, ex_code, rotated_q, d, ex_bits, cb);
332
+ if (ex_bits == 1) {
333
+ return ip_1exbit_neon(sign_bits, ex_code, rotated_q, d, cb);
334
+ }
335
+ switch (ex_bits) {
336
+ case 2:
337
+ return ip_multibit_neon<2>(sign_bits, ex_code, rotated_q, d, cb);
338
+ case 3:
339
+ return ip_multibit_neon<3>(sign_bits, ex_code, rotated_q, d, cb);
340
+ case 4:
341
+ return ip_multibit_neon<4>(sign_bits, ex_code, rotated_q, d, cb);
342
+ case 5:
343
+ return ip_multibit_neon<5>(sign_bits, ex_code, rotated_q, d, cb);
344
+ case 6:
345
+ return ip_multibit_neon<6>(sign_bits, ex_code, rotated_q, d, cb);
346
+ case 7:
347
+ return ip_multibit_neon<7>(sign_bits, ex_code, rotated_q, d, cb);
348
+ default:
349
+ return ip_scalar(sign_bits, ex_code, rotated_q, 0, d, ex_bits, cb);
350
+ }
62
351
  }
63
352
 
64
353
  } // namespace faiss::rabitq::multibit
65
-
66
- #endif // COMPILE_SIMD_ARM_NEON
@@ -9,8 +9,6 @@
9
9
 
10
10
  #include <faiss/utils/rabitq_simd.h>
11
11
 
12
- #ifdef COMPILE_SIMD_RISCV_RVV
13
-
14
12
  #include <riscv_vector.h>
15
13
 
16
14
  namespace faiss::rabitq {
@@ -188,5 +186,3 @@ compute_inner_product<SIMDLevel::RISCV_RVV>(
188
186
  }
189
187
 
190
188
  } // namespace faiss::rabitq::multibit
191
-
192
- #endif // COMPILE_SIMD_RISCV_RVV
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_AVX2
9
-
10
8
  #include <faiss/utils/simd_impl/super_kmeans_kernels.h>
11
9
 
12
10
  #include <immintrin.h>
@@ -53,5 +51,3 @@ float block_l2<SIMDLevel::AVX2>(const float* x, const float* y, int n) {
53
51
 
54
52
  } // namespace detail
55
53
  } // namespace faiss
56
-
57
- #endif // COMPILE_SIMD_AVX2
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_AVX512
9
-
10
8
  #include <faiss/utils/simd_impl/super_kmeans_kernels.h>
11
9
 
12
10
  #include <immintrin.h>
@@ -41,5 +39,3 @@ float block_l2<SIMDLevel::AVX512>(const float* x, const float* y, int n) {
41
39
 
42
40
  } // namespace detail
43
41
  } // namespace faiss
44
-
45
- #endif // COMPILE_SIMD_AVX512
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_ARM_SVE
9
-
10
8
  #include <faiss/utils/simd_impl/super_kmeans_kernels.h>
11
9
 
12
10
  #include <arm_sve.h>
@@ -30,5 +28,3 @@ float block_l2<SIMDLevel::ARM_SVE>(const float* x, const float* y, int n) {
30
28
 
31
29
  } // namespace detail
32
30
  } // namespace faiss
33
-
34
- #endif // COMPILE_SIMD_ARM_SVE
@@ -30,6 +30,8 @@ uint64_t SIMDConfig::supported_simd_levels = 0;
30
30
  // Microarchitecture flags (x86). Default false; set by
31
31
  // detect_x86_uarch_flags() at load time.
32
32
  bool SIMDConfig::avx512_split = false;
33
+ bool SIMDConfig::scalar_gather = false;
34
+ bool SIMDConfig::sq_avx512_4_accumulators = false;
33
35
 
34
36
  // Resolved here rather than in the header so that dependents, which never see
35
37
  // FAISS_ENABLE_DD, still get the answer for the faiss they link against.
@@ -50,19 +52,19 @@ bool SIMDConfig::has_dynamic_dispatch() {
50
52
  #define HWCAP_SVE (1 << 22)
51
53
  #endif
52
54
 
53
- static bool has_sve() {
55
+ [[maybe_unused]] static bool has_sve() {
54
56
  return (getauxval(AT_HWCAP) & HWCAP_SVE) != 0;
55
57
  }
56
58
 
57
59
  #elif defined(__APPLE__)
58
60
  // Apple Silicon does NOT support SVE
59
- static bool has_sve() {
61
+ [[maybe_unused]] static bool has_sve() {
60
62
  return false;
61
63
  }
62
64
 
63
65
  #else
64
66
  // Other aarch64 platforms: conservatively report no SVE
65
- static bool has_sve() {
67
+ [[maybe_unused]] static bool has_sve() {
66
68
  return false;
67
69
  }
68
70
 
@@ -137,6 +139,13 @@ void detect_x86_uarch_flags() {
137
139
  // AMD Zen 4 / Zen 4c (Bergamo) is family 0x19 and splits AVX-512.
138
140
  // (Zen 5, family 0x1A, has a native 512-bit datapath and is excluded.)
139
141
  SIMDConfig::avx512_split = is_amd && display_family == 0x19u;
142
+ // Measured on Milan, Bergamo (0x19) and Turin (0x1A); other AMD families
143
+ // keep the gather kernels.
144
+ SIMDConfig::scalar_gather =
145
+ is_amd && (display_family == 0x19u || display_family == 0x1Au);
146
+ // Measured on Bergamo (0x19) and Turin (0x1A). Intel Skylake and Sapphire
147
+ // Rapids are faster with one accumulator.
148
+ SIMDConfig::sq_avx512_4_accumulators = SIMDConfig::scalar_gather;
140
149
  }
141
150
 
142
151
  #else // Not x86-64
@@ -191,6 +200,8 @@ SIMDConfig::SIMDConfig(const char** faiss_simd_level_env) {
191
200
  if (!env_var) {
192
201
  level = auto_detect_simd_level();
193
202
  } else {
203
+ // The microarchitecture flags describe the CPU, not the forced level.
204
+ detect_x86_uarch_flags();
194
205
  // Forcing a level the CPU lacks is allowed. Forcing one the binary
195
206
  // does not hold is not: dispatch would fall to NONE and skip every
196
207
  // level between. Walk down to the nearest compiled level instead.
@@ -274,7 +285,9 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
274
285
 
275
286
  #if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_AVX512)
276
287
  bool has_avx2 = (ebx7 & (1 << 5)) != 0;
277
- if (has_avx2) {
288
+ // The AVX2 and higher SIMD files compile with -mbmi2.
289
+ bool has_bmi2 = (ebx7 & (1 << 8)) != 0;
290
+ if (has_avx2 && has_bmi2) {
278
291
  supported_simd_levels |= (1 << static_cast<int>(SIMDLevel::AVX2));
279
292
  detected_level = SIMDLevel::AVX2;
280
293
  }
@@ -282,7 +295,7 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
282
295
  #if defined(COMPILE_SIMD_AVX512)
283
296
  bool cpu_has_avx512f = (ebx7 & (1 << 16)) != 0;
284
297
  bool os_supports_avx512 = (xcr0 & 0xE0) == 0xE0;
285
- bool has_avx512f = cpu_has_avx512f && os_supports_avx512;
298
+ bool has_avx512f = cpu_has_avx512f && os_supports_avx512 && has_bmi2;
286
299
  if (has_avx512f) {
287
300
  bool has_avx512cd = (ebx7 & (1 << 28)) != 0;
288
301
  bool has_avx512vl = (ebx7 & (1 << 31)) != 0;
@@ -342,7 +355,10 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
342
355
  #endif
343
356
 
344
357
  #if defined(__riscv) && defined(COMPILE_SIMD_RISCV_RVV)
345
- // RVV is always available on RISC-V builds compiled with rv64gcv.
358
+ // RVV is always available on RISC-V builds compiled with
359
+ // rv64gcv_zvfhmin: that ISA (including Zvfhmin, used by the QT_fp16
360
+ // kernels) is the minimum requirement to run such binaries at all,
361
+ // so no runtime feature check is needed beyond the build contract.
346
362
  supported_simd_levels |= (1 << static_cast<int>(SIMDLevel::RISCV_RVV));
347
363
  detected_level = SIMDLevel::RISCV_RVV;
348
364
  #endif
@@ -26,7 +26,11 @@ enum class SIMDLevel {
26
26
  ARM_NEON,
27
27
  ARM_SVE, // Scalable Vector Extension (ARMv8.2+)
28
28
  // riscv
29
- RISCV_RVV, // RISC-V Vector Extension (rv64gcv)
29
+ // RISC-V Vector Extension. The minimum ISA for this level is rv64gcv
30
+ // plus Zvfhmin (FP16 vector conversion), which the QT_fp16 kernels
31
+ // require; RVV translation units are compiled with
32
+ // -march=rv64gcv_zvfhmin.
33
+ RISCV_RVV,
30
34
 
31
35
  // Appended to preserve the numeric values of the existing public enum.
32
36
  // AVX-512 core features plus AVX512_VPOPCNTDQ and AVX512_BITALG
@@ -36,6 +40,65 @@ enum class SIMDLevel {
36
40
  COUNT
37
41
  };
38
42
 
43
+ /***************************************************************
44
+ * FAISS_SIMD_TU(L): 1 if the library contains SIMD level L
45
+ * (COMPILE_SIMD_L) and the current translation unit can use the
46
+ * instructions of L (compiler macro), else 0. Use it to guard level
47
+ * code in headers that common files also include, for example
48
+ * `#if FAISS_SIMD_TU(AVX512)`. It is function-like, so a header that
49
+ * uses it without including this file fails to compile.
50
+ ***************************************************************/
51
+ #ifdef SWIG
52
+ #define FAISS_SIMD_TU(level) 0
53
+ #else
54
+
55
+ #if defined(COMPILE_SIMD_AVX2) && defined(__AVX2__)
56
+ #define FAISS_SIMD_TU_AVX2 1
57
+ #else
58
+ #define FAISS_SIMD_TU_AVX2 0
59
+ #endif
60
+
61
+ #if defined(COMPILE_SIMD_AVX512) && defined(__AVX512F__)
62
+ #define FAISS_SIMD_TU_AVX512 1
63
+ #else
64
+ #define FAISS_SIMD_TU_AVX512 0
65
+ #endif
66
+
67
+ #if defined(COMPILE_SIMD_AVX512_VPOPCNT) && defined(__AVX512VPOPCNTDQ__)
68
+ #define FAISS_SIMD_TU_AVX512_VPOPCNT 1
69
+ #else
70
+ #define FAISS_SIMD_TU_AVX512_VPOPCNT 0
71
+ #endif
72
+
73
+ #if defined(COMPILE_SIMD_AVX512_SPR) && defined(__AVX512FP16__)
74
+ #define FAISS_SIMD_TU_AVX512_SPR 1
75
+ #else
76
+ #define FAISS_SIMD_TU_AVX512_SPR 0
77
+ #endif
78
+
79
+ // __aarch64__ is not defined by MSVC, which cannot compile the NEON code.
80
+ #if defined(COMPILE_SIMD_ARM_NEON) && defined(__aarch64__)
81
+ #define FAISS_SIMD_TU_ARM_NEON 1
82
+ #else
83
+ #define FAISS_SIMD_TU_ARM_NEON 0
84
+ #endif
85
+
86
+ #if defined(COMPILE_SIMD_ARM_SVE) && defined(__ARM_FEATURE_SVE)
87
+ #define FAISS_SIMD_TU_ARM_SVE 1
88
+ #else
89
+ #define FAISS_SIMD_TU_ARM_SVE 0
90
+ #endif
91
+
92
+ #if defined(COMPILE_SIMD_RISCV_RVV) && defined(__riscv_vector)
93
+ #define FAISS_SIMD_TU_RISCV_RVV 1
94
+ #else
95
+ #define FAISS_SIMD_TU_RISCV_RVV 0
96
+ #endif
97
+
98
+ #define FAISS_SIMD_TU(level) FAISS_SIMD_TU_##level
99
+
100
+ #endif // SWIG
101
+
39
102
  /***************************************************************
40
103
  * SINGLE_SIMD_LEVEL: the SIMD level for code without explicit SL context.
41
104
  *
@@ -183,6 +246,16 @@ struct FAISS_API SIMDConfig {
183
246
  /// 256-bit kernel.
184
247
  static bool avx512_split;
185
248
 
249
+ /// Scalar table lookups beat gathers (AMD Zen 3, Zen 4 and Zen 5,
250
+ /// families 0x19 and 0x1A), so the 8-bit PQ code-distance kernels use
251
+ /// the scalar path instead of gathers.
252
+ static bool scalar_gather;
253
+
254
+ /// Same CPUs as scalar_gather. The AVX-512 QT_fp16, QT_bf16 and QT_8bit
255
+ /// inner-product distance loops use four accumulators per code at a
256
+ /// large d.
257
+ static bool sq_avx512_4_accumulators;
258
+
186
259
  static SIMDLevel auto_detect_simd_level();
187
260
 
188
261
  /// Whether this faiss build dispatches SIMD at runtime.
@@ -252,7 +252,7 @@ void bucket_sort_parallel(
252
252
  uint64_t vmax,
253
253
  int64_t* lims,
254
254
  int64_t* perm,
255
- int nt_in) {
255
+ [[maybe_unused]] int nt_in) {
256
256
  memset(lims, 0, sizeof(*lims) * (vmax + 1));
257
257
  #pragma omp parallel num_threads(nt_in)
258
258
  {
@@ -368,7 +368,7 @@ void bucket_sort_inplace_ref(
368
368
  if (row >= 0) {
369
369
  ptrs[bucket_no] += 1;
370
370
  }
371
- assert(idx < lims[bucket_no + 1]);
371
+ assert(idx < static_cast<size_t>(lims[bucket_no + 1]));
372
372
  TI next_bucket_no = vals[idx];
373
373
  vals[idx] = row;
374
374
  if (next_bucket_no != -1) {
@@ -391,7 +391,7 @@ void bucket_sort_inplace_ref(
391
391
  }
392
392
 
393
393
  for (size_t i = 0; i < static_cast<size_t>(nbucket); i++) {
394
- assert(ptrs[i] == lims[i + 1]);
394
+ assert(ptrs[i] == static_cast<size_t>(lims[i + 1]));
395
395
  }
396
396
  double t3 = getmillisecs();
397
397
  if (bucket_sort_verbose) {
@@ -758,8 +758,9 @@ void hashtable_int64_to_int64_add(
758
758
  i < static_cast<size_t>(lims[bucket + 1]);
759
759
  i++) {
760
760
  int64_t j = perm[i];
761
- assert(bucket_no[j] == bucket);
762
- assert(hk[j] >= k0 && hk[j] < k1);
761
+ assert(bucket_no[j] == static_cast<uint64_t>(bucket));
762
+ assert(static_cast<size_t>(hk[j]) >= k0 &&
763
+ static_cast<size_t>(hk[j]) < k1);
763
764
  size_t slot = hk[j];
764
765
  for (;;) {
765
766
  if (tab[slot * 2] == -1) { // found!
@@ -10,8 +10,6 @@
10
10
  // This file contains transposing kernels for AVX2 for
11
11
  // tiny float/int32 matrices, such as 8x2.
12
12
 
13
- #ifdef __AVX2__
14
-
15
13
  #include <immintrin.h>
16
14
 
17
15
  namespace faiss {
@@ -161,5 +159,3 @@ inline void transpose_8x8(
161
159
  }
162
160
 
163
161
  } // namespace faiss
164
-
165
- #endif
@@ -10,8 +10,6 @@
10
10
  // This file contains transposing kernels for AVX512 for // tiny float/int32
11
11
  // matrices, such as 16x2.
12
12
 
13
- #ifdef __AVX512F__
14
-
15
13
  #include <immintrin.h>
16
14
 
17
15
  namespace faiss {
@@ -172,5 +170,3 @@ inline void transpose_16x8(
172
170
  }
173
171
 
174
172
  } // namespace faiss
175
-
176
- #endif