faiss 0.6.3 → 0.6.5

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 (255) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/ext/faiss/extconf.rb +1 -1
  4. data/lib/faiss/version.rb +1 -1
  5. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  6. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  7. data/vendor/faiss/faiss/Clustering.h +37 -0
  8. data/vendor/faiss/faiss/IVFlib.cpp +99 -0
  9. data/vendor/faiss/faiss/IVFlib.h +15 -0
  10. data/vendor/faiss/faiss/Index.cpp +44 -0
  11. data/vendor/faiss/faiss/Index.h +18 -13
  12. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  13. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  14. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +23 -2
  15. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +94 -144
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.h +18 -5
  17. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -14
  18. data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +33 -17
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +64 -4
  22. data/vendor/faiss/faiss/IndexFlat.h +33 -2
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +755 -99
  25. data/vendor/faiss/faiss/IndexHNSW.h +69 -7
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +43 -6
  27. data/vendor/faiss/faiss/IndexIDMap.h +7 -0
  28. data/vendor/faiss/faiss/IndexIVF.cpp +297 -32
  29. data/vendor/faiss/faiss/IndexIVF.h +53 -1
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +46 -14
  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 +16 -0
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +9 -1
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -0
  37. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  38. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +50 -14
  39. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -2
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +855 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +0 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +6 -3
  46. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +10 -4
  47. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  48. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  49. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  50. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  51. data/vendor/faiss/faiss/IndexShards.cpp +13 -3
  52. data/vendor/faiss/faiss/IndexShards.h +5 -0
  53. data/vendor/faiss/faiss/IndexShardsIVF.cpp +4 -3
  54. data/vendor/faiss/faiss/SuperKMeans.cpp +87 -22
  55. data/vendor/faiss/faiss/SuperKMeans.h +12 -6
  56. data/vendor/faiss/faiss/VectorTransform.cpp +40 -1
  57. data/vendor/faiss/faiss/VectorTransform.h +3 -0
  58. data/vendor/faiss/faiss/clone_index.cpp +27 -0
  59. data/vendor/faiss/faiss/gpu/Dataset.h +112 -0
  60. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  61. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  62. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +4 -5
  63. data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
  64. data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -18
  65. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +26 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +109 -63
  67. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  68. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  69. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  70. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  71. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  72. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  73. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  74. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +11 -0
  75. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +92 -0
  76. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +4 -0
  77. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  78. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +38 -0
  79. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +106 -0
  80. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +10 -0
  81. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
  82. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -0
  83. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  84. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  85. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  86. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +287 -134
  87. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  88. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  89. data/vendor/faiss/faiss/impl/HNSW.cpp +465 -125
  90. data/vendor/faiss/faiss/impl/HNSW.h +118 -7
  91. data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
  92. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  93. data/vendor/faiss/faiss/impl/NNDescent.cpp +12 -4
  94. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  95. data/vendor/faiss/faiss/impl/Panorama.h +14 -4
  96. data/vendor/faiss/faiss/impl/PiPNN.cpp +822 -0
  97. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  98. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +58 -15
  99. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +1 -1
  100. data/vendor/faiss/faiss/impl/RaBitQUtils.h +1 -1
  101. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +150 -60
  102. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +62 -4
  103. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +2 -2
  104. data/vendor/faiss/faiss/impl/ResultHandler.h +9 -3
  105. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +61 -30
  106. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +7 -16
  107. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  108. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  109. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  110. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +89 -13
  111. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  112. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  113. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  114. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  115. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -1
  116. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  117. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  118. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  119. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +18 -129
  120. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  121. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +5 -0
  122. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +0 -4
  123. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +0 -4
  124. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +0 -88
  125. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  127. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +22 -1
  128. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -2
  129. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +41 -23
  130. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/index_read.cpp +302 -29
  133. data/vendor/faiss/faiss/impl/index_write.cpp +126 -26
  134. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  135. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  136. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  137. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  138. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  139. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  140. data/vendor/faiss/faiss/impl/platform_macros.h +45 -0
  141. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  142. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +152 -43
  143. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  144. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  145. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  146. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  147. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  148. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  149. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +23 -4
  150. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  151. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  152. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  153. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  154. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  155. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  156. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  157. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +0 -4
  158. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +0 -4
  159. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  160. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  161. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +6 -1
  162. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +7 -3
  163. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  164. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +59 -11
  165. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +232 -79
  166. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +482 -103
  167. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +2 -149
  168. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +3 -698
  169. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +101 -72
  170. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +203 -99
  171. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  172. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  173. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  174. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  175. data/vendor/faiss/faiss/impl/simd_dispatch.h +47 -22
  176. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  177. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  178. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  179. data/vendor/faiss/faiss/index_factory.cpp +84 -1
  180. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +10 -5
  181. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +10 -4
  182. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +16 -4
  183. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +11 -3
  184. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
  185. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
  186. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +9 -2
  187. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
  188. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  189. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  190. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  191. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  192. data/vendor/faiss/faiss/utils/distances.cpp +246 -28
  193. data/vendor/faiss/faiss/utils/distances.h +34 -0
  194. data/vendor/faiss/faiss/utils/distances_dispatch.h +36 -28
  195. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  196. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -15
  197. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  198. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  199. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  200. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  201. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  202. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  203. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  204. data/vendor/faiss/faiss/utils/hamming.cpp +15 -9
  205. data/vendor/faiss/faiss/utils/hamming.h +10 -1
  206. data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
  207. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  208. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  209. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +20 -0
  210. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +34 -6
  211. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +217 -0
  212. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  213. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +143 -1
  214. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  215. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  216. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  217. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  218. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  219. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  220. data/vendor/faiss/faiss/utils/rabitq_simd.h +51 -16
  221. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  222. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +231 -35
  223. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +64 -5
  224. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +64 -5
  225. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +428 -108
  226. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  227. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +93 -0
  228. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  229. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  230. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  231. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  232. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  233. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  234. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  235. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  236. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  237. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +54 -15
  238. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +89 -55
  239. data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +70 -36
  240. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  241. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  242. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +2 -7
  243. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
  244. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +30 -0
  247. data/vendor/faiss/faiss/utils/simd_levels.cpp +191 -70
  248. data/vendor/faiss/faiss/utils/simd_levels.h +100 -16
  249. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  250. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  251. data/vendor/faiss/faiss/utils/utils.cpp +80 -0
  252. data/vendor/faiss/faiss/utils/utils.h +24 -0
  253. metadata +38 -5
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_spr.h +0 -171
@@ -7,8 +7,15 @@
7
7
 
8
8
  #include <faiss/utils/simd_levels.h>
9
9
 
10
+ #include <cstdint>
11
+ #include <cstdio>
10
12
  #include <cstdlib>
11
13
 
14
+ #if defined(_MSC_VER)
15
+ // __cpuidex, _xgetbv
16
+ #include <intrin.h>
17
+ #endif
18
+
12
19
  #include <faiss/impl/FaissAssert.h>
13
20
  #include <faiss/impl/simd_dispatch.h>
14
21
 
@@ -23,6 +30,18 @@ uint64_t SIMDConfig::supported_simd_levels = 0;
23
30
  // Microarchitecture flags (x86). Default false; set by
24
31
  // detect_x86_uarch_flags() at load time.
25
32
  bool SIMDConfig::avx512_split = false;
33
+ bool SIMDConfig::scalar_gather = false;
34
+ bool SIMDConfig::sq_avx512_4_accumulators = false;
35
+
36
+ // Resolved here rather than in the header so that dependents, which never see
37
+ // FAISS_ENABLE_DD, still get the answer for the faiss they link against.
38
+ bool SIMDConfig::has_dynamic_dispatch() {
39
+ #ifdef FAISS_ENABLE_DD
40
+ return true;
41
+ #else
42
+ return false;
43
+ #endif
44
+ }
26
45
 
27
46
  // ARM SVE runtime detection
28
47
  #if defined(__aarch64__) || defined(_M_ARM64)
@@ -57,49 +76,118 @@ static bool has_sve() {
57
76
  }
58
77
  #endif
59
78
 
79
+ namespace {
80
+
81
+ #if defined(__x86_64__) || defined(_M_X64)
82
+
83
+ // MSVC and clang-cl do not support GNU-style
84
+ // 64-bit inline assembly, MSVC defines _M_X64 instead of __x86_64__
85
+
86
+ #if defined(_MSC_VER)
87
+
88
+ [[maybe_unused]] void cpuid_count(
89
+ unsigned int leaf,
90
+ unsigned int subleaf,
91
+ unsigned int regs[4]) {
92
+ int r[4];
93
+ __cpuidex(r, static_cast<int>(leaf), static_cast<int>(subleaf));
94
+ for (int i = 0; i < 4; i++) {
95
+ regs[i] = static_cast<unsigned int>(r[i]);
96
+ }
97
+ }
98
+
99
+ [[maybe_unused]] uint64_t xgetbv0() {
100
+ return static_cast<uint64_t>(_xgetbv(0));
101
+ }
102
+
103
+ #else // GCC / Clang
104
+
105
+ [[maybe_unused]] void cpuid_count(
106
+ unsigned int leaf,
107
+ unsigned int subleaf,
108
+ unsigned int regs[4]) {
109
+ asm volatile("cpuid"
110
+ : "=a"(regs[0]), "=b"(regs[1]), "=c"(regs[2]), "=d"(regs[3])
111
+ : "a"(leaf), "c"(subleaf));
112
+ }
113
+
114
+ [[maybe_unused]] uint64_t xgetbv0() {
115
+ unsigned int eax, edx;
116
+ asm volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
117
+ return eax | (static_cast<uint64_t>(edx) << 32);
118
+ }
119
+
120
+ #endif // _MSC_VER
121
+
60
122
  // Detect x86 microarchitecture flags used for kernel routing. Uses raw
61
123
  // cpuid so it is safe to run on any CPU regardless of compiled SIMD level.
62
- #if defined(__x86_64__)
63
- namespace {
64
124
  void detect_x86_uarch_flags() {
65
- unsigned int eax, ebx, ecx, edx;
125
+ unsigned int regs[4];
66
126
 
67
127
  // Vendor string (CPUID.0): "AuthenticAMD" is EBX="Auth", EDX="enti",
68
128
  // ECX="cAMD".
69
- eax = 0;
70
- ecx = 0;
71
- asm volatile("cpuid"
72
- : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
73
- : "a"(eax), "c"(ecx));
74
- const bool is_amd =
75
- ebx == 0x68747541u && edx == 0x69746e65u && ecx == 0x444d4163u;
129
+ cpuid_count(0, 0, regs);
130
+ const bool is_amd = regs[1] == 0x68747541u && regs[3] == 0x69746e65u &&
131
+ regs[2] == 0x444d4163u;
76
132
 
77
133
  // Family/model (CPUID.1 EAX).
78
- eax = 1;
79
- ecx = 0;
80
- asm volatile("cpuid"
81
- : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
82
- : "a"(eax), "c"(ecx));
83
- const unsigned int base_family = (eax >> 8) & 0xfu;
134
+ cpuid_count(1, 0, regs);
135
+ const unsigned int eax1 = regs[0];
136
+ const unsigned int base_family = (eax1 >> 8) & 0xfu;
84
137
  const unsigned int display_family =
85
- base_family + (base_family == 0xfu ? ((eax >> 20) & 0xffu) : 0u);
138
+ base_family + (base_family == 0xfu ? ((eax1 >> 20) & 0xffu) : 0u);
86
139
  // AMD Zen 4 / Zen 4c (Bergamo) is family 0x19 and splits AVX-512.
87
140
  // (Zen 5, family 0x1A, has a native 512-bit datapath and is excluded.)
88
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;
89
149
  }
90
- } // namespace
91
- #else
92
- namespace {
150
+
151
+ #else // Not x86-64
152
+
93
153
  void detect_x86_uarch_flags() {}
154
+
155
+ #endif // defined(__x86_64__) || defined(_M_X64)
156
+
94
157
  } // namespace
158
+
159
+ /// Must mirror the case labels in with_selected_simd_levels. A static build
160
+ /// defines a COMPILE_SIMD_* macro for every level whose sources it compiles,
161
+ /// not only for SINGLE_SIMD_LEVEL, so this reports a correct set in both
162
+ /// modes and the result is not a single level.
163
+ uint64_t compiled_simd_levels() {
164
+ uint64_t mask = uint64_t(1) << static_cast<int>(SIMDLevel::NONE);
165
+ #ifdef COMPILE_SIMD_AVX2
166
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::AVX2);
167
+ #endif
168
+ #ifdef COMPILE_SIMD_AVX512
169
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::AVX512);
170
+ #endif
171
+ #ifdef COMPILE_SIMD_AVX512_VPOPCNT
172
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::AVX512_VPOPCNT);
173
+ #endif
174
+ #ifdef COMPILE_SIMD_AVX512_SPR
175
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::AVX512_SPR);
176
+ #endif
177
+ #ifdef COMPILE_SIMD_ARM_NEON
178
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::ARM_NEON);
179
+ #endif
180
+ #ifdef COMPILE_SIMD_ARM_SVE
181
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::ARM_SVE);
182
+ #endif
183
+ #ifdef COMPILE_SIMD_RISCV_RVV
184
+ mask |= uint64_t(1) << static_cast<int>(SIMDLevel::RISCV_RVV);
95
185
  #endif
186
+ return mask;
187
+ }
96
188
 
97
189
  #ifdef FAISS_ENABLE_DD
98
190
 
99
- // =============================================================================
100
- // Dynamic Dispatch (DD) mode implementation
101
- // =============================================================================
102
-
103
191
  // Static initializer to run constructor at load time
104
192
  // NOLINTNEXTLINE(facebook-avoid-non-const-global-variables)
105
193
  static SIMDConfig simd_config_initializer;
@@ -112,7 +200,24 @@ SIMDConfig::SIMDConfig(const char** faiss_simd_level_env) {
112
200
  if (!env_var) {
113
201
  level = auto_detect_simd_level();
114
202
  } else {
115
- level = to_simd_level(env_var);
203
+ // The microarchitecture flags describe the CPU, not the forced level.
204
+ detect_x86_uarch_flags();
205
+ // Forcing a level the CPU lacks is allowed. Forcing one the binary
206
+ // does not hold is not: dispatch would fall to NONE and skip every
207
+ // level between. Walk down to the nearest compiled level instead.
208
+ const uint64_t compiled = compiled_simd_levels();
209
+ const SIMDLevel requested = to_simd_level(env_var);
210
+ level = requested;
211
+ while (((compiled >> static_cast<int>(level)) & 1) == 0) {
212
+ level = get_simd_fallback(level);
213
+ }
214
+ if (level != requested) {
215
+ fprintf(stderr,
216
+ "faiss: FAISS_SIMD_LEVEL=%s is not compiled into this "
217
+ "build, using %s instead\n",
218
+ to_string(requested).c_str(),
219
+ to_string(level).c_str());
220
+ }
116
221
  supported_simd_levels = (1 << static_cast<int>(level));
117
222
  }
118
223
  supported_simd_levels |= (1 << static_cast<int>(SIMDLevel::NONE));
@@ -144,80 +249,86 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
144
249
 
145
250
  detect_x86_uarch_flags();
146
251
 
147
- #if defined(__x86_64__) && \
252
+ #if (defined(__x86_64__) || defined(_M_X64)) && \
148
253
  (defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_AVX512))
149
- unsigned int eax, ebx, ecx, edx;
254
+ unsigned int regs[4];
150
255
 
151
- eax = 1;
152
- ecx = 0;
153
- asm volatile("cpuid"
154
- : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
155
- : "a"(eax), "c"(ecx));
256
+ cpuid_count(1, 0, regs);
257
+ unsigned int ecx1 = regs[2];
156
258
 
157
- bool has_avx = (ecx & (1 << 28)) != 0;
259
+ bool has_avx = (ecx1 & (1 << 28)) != 0;
158
260
 
159
261
  bool has_xsave_osxsave =
160
- (ecx & ((1 << 26) | (1 << 27))) == ((1 << 26) | (1 << 27));
262
+ (ecx1 & ((1 << 26) | (1 << 27))) == ((1 << 26) | (1 << 27));
161
263
 
162
264
  bool avx_supported = false;
163
265
  if (has_avx && has_xsave_osxsave) {
164
- unsigned int xcr0;
165
- asm volatile("xgetbv" : "=a"(xcr0), "=d"(edx) : "c"(0));
166
- avx_supported = (xcr0 & 6) == 6;
266
+ avx_supported = (xgetbv0() & 6) == 6;
167
267
  }
168
268
 
169
269
  if (avx_supported) {
170
- eax = 7;
171
- ecx = 0;
172
- asm volatile("cpuid"
173
- : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
174
- : "a"(eax), "c"(ecx));
175
- // Save EDX before xgetbv clobbers it — needed for
176
- // AVX512_FP16 check (bit 23) in the SPR detection below.
177
- unsigned int cpuid7_edx = edx;
178
-
179
- unsigned int xcr0;
180
- asm volatile("xgetbv" : "=a"(xcr0), "=d"(edx) : "c"(0));
270
+ cpuid_count(7, 0, regs);
271
+ unsigned int ebx7 = regs[1];
272
+ // EDX of CPUID leaf 7 subleaf 0 carries AVX512_FP16 (bit 23),
273
+ // needed for the SPR detection below. Kept in a local so a later
274
+ // xgetbv cannot clobber it.
275
+ unsigned int cpuid7_edx = regs[3];
276
+ // Leaf 7 subleaf 0, not leaf 1: leaf 1 ECX holds unrelated bits at
277
+ // these positions.
278
+ unsigned int ecx7 = regs[2];
279
+ [[maybe_unused]] bool has_avx512_vnni = (ecx7 & (1 << 11)) != 0;
280
+ [[maybe_unused]] bool has_avx512_vpopcntdq = (ecx7 & (1 << 14)) != 0;
281
+ // Bit 12 = AVX512_BITALG, needed for the byte-wise popcount kernels.
282
+ [[maybe_unused]] bool has_avx512_bitalg = (ecx7 & (1 << 12)) != 0;
283
+
284
+ uint64_t xcr0 = xgetbv0();
181
285
 
182
286
  #if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_AVX512)
183
- bool has_avx2 = (ebx & (1 << 5)) != 0;
184
- if (has_avx2) {
287
+ bool has_avx2 = (ebx7 & (1 << 5)) != 0;
288
+ // The AVX2 and higher SIMD files compile with -mbmi2.
289
+ bool has_bmi2 = (ebx7 & (1 << 8)) != 0;
290
+ if (has_avx2 && has_bmi2) {
185
291
  supported_simd_levels |= (1 << static_cast<int>(SIMDLevel::AVX2));
186
292
  detected_level = SIMDLevel::AVX2;
187
293
  }
188
294
 
189
295
  #if defined(COMPILE_SIMD_AVX512)
190
- bool cpu_has_avx512f = (ebx & (1 << 16)) != 0;
296
+ bool cpu_has_avx512f = (ebx7 & (1 << 16)) != 0;
191
297
  bool os_supports_avx512 = (xcr0 & 0xE0) == 0xE0;
192
- bool has_avx512f = cpu_has_avx512f && os_supports_avx512;
298
+ bool has_avx512f = cpu_has_avx512f && os_supports_avx512 && has_bmi2;
193
299
  if (has_avx512f) {
194
- bool has_avx512cd = (ebx & (1 << 28)) != 0;
195
- bool has_avx512vl = (ebx & (1 << 31)) != 0;
196
- bool has_avx512dq = (ebx & (1 << 17)) != 0;
197
- bool has_avx512bw = (ebx & (1 << 30)) != 0;
300
+ bool has_avx512cd = (ebx7 & (1 << 28)) != 0;
301
+ bool has_avx512vl = (ebx7 & (1 << 31)) != 0;
302
+ bool has_avx512dq = (ebx7 & (1 << 17)) != 0;
303
+ bool has_avx512bw = (ebx7 & (1 << 30)) != 0;
198
304
  if (has_avx512bw && has_avx512cd && has_avx512vl && has_avx512dq) {
199
305
  detected_level = SIMDLevel::AVX512;
200
306
  supported_simd_levels |=
201
307
  (1 << static_cast<int>(SIMDLevel::AVX512));
202
308
 
309
+ #if defined(COMPILE_SIMD_AVX512_VPOPCNT)
310
+ if (has_avx512_vpopcntdq && has_avx512_bitalg) {
311
+ detected_level = SIMDLevel::AVX512_VPOPCNT;
312
+ supported_simd_levels |=
313
+ (1 << static_cast<int>(SIMDLevel::AVX512_VPOPCNT));
314
+ }
315
+ #endif
316
+
203
317
  #if defined(COMPILE_SIMD_AVX512_SPR)
204
318
  // Check for Sapphire Rapids features.
205
- // The SPR code path is compiled with -mavx512fp16, so we
206
- // must verify both AVX512_BF16 and AVX512_FP16 before
207
- // dispatching to it. AMD Zen 4 (bergamo) has BF16 but
208
- // not FP16 — using SPR code there causes SIGILL.
319
+ // The SPR code path is compiled with AVX512_VNNI, BF16,
320
+ // FP16 and VPOPCNTDQ, and falls back to the VPOPCNT kernels,
321
+ // which need BITALG. All five features are required.
322
+ // AMD Zen 4 has VPOPCNTDQ and BF16 but not FP16, and must
323
+ // remain on the AVX512_VPOPCNT level.
209
324
  // CPUID EAX=7, ECX=1: EAX bit 5 = AVX512_BF16
210
325
  // CPUID EAX=7, ECX=0: EDX bit 23 = AVX512_FP16
211
326
  // (Linux: X86_FEATURE_AVX512_FP16 = 18*32+23)
212
327
  bool has_avx512_fp16 = (cpuid7_edx & (1 << 23)) != 0;
213
- unsigned int eax1, ebx1, ecx1, edx1;
214
- eax1 = 7;
215
- ecx1 = 1;
216
- asm volatile("cpuid"
217
- : "=a"(eax1), "=b"(ebx1), "=c"(ecx1), "=d"(edx1)
218
- : "a"(eax1), "c"(ecx1));
219
- bool has_avx512_bf16 = (eax1 & (1 << 5)) != 0;
220
- if (has_avx512_bf16 && has_avx512_fp16) {
328
+ cpuid_count(7, 1, regs);
329
+ const bool has_avx512_bf16 = (regs[0] & (1 << 5)) != 0;
330
+ if (has_avx512_vnni && has_avx512_vpopcntdq &&
331
+ has_avx512_bitalg && has_avx512_bf16 && has_avx512_fp16) {
221
332
  detected_level = SIMDLevel::AVX512_SPR;
222
333
  supported_simd_levels |=
223
334
  (1 << static_cast<int>(SIMDLevel::AVX512_SPR));
@@ -228,7 +339,7 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
228
339
  #endif // defined(COMPILE_SIMD_AVX512)
229
340
  #endif // defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_AVX512)
230
341
  }
231
- #endif // defined(__x86_64__) && ...
342
+ #endif // defined(__x86_64__) || defined(_M_X64)
232
343
 
233
344
  #ifdef COMPILE_SIMD_ARM_NEON
234
345
  // ARM NEON is standard on aarch64
@@ -244,7 +355,10 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
244
355
  #endif
245
356
 
246
357
  #if defined(__riscv) && defined(COMPILE_SIMD_RISCV_RVV)
247
- // 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.
248
362
  supported_simd_levels |= (1 << static_cast<int>(SIMDLevel::RISCV_RVV));
249
363
  detected_level = SIMDLevel::RISCV_RVV;
250
364
  #endif
@@ -311,6 +425,8 @@ SIMDLevel SIMDConfig::auto_detect_simd_level() {
311
425
  // In static mode, return the compiled-in level
312
426
  #if defined(COMPILE_SIMD_AVX512_SPR)
313
427
  return SIMDLevel::AVX512_SPR;
428
+ #elif defined(COMPILE_SIMD_AVX512_VPOPCNT)
429
+ return SIMDLevel::AVX512_VPOPCNT;
314
430
  #elif defined(COMPILE_SIMD_AVX512)
315
431
  return SIMDLevel::AVX512;
316
432
  #elif defined(COMPILE_SIMD_AVX2)
@@ -345,6 +461,8 @@ std::string to_string(SIMDLevel level) {
345
461
  return "AVX2";
346
462
  case SIMDLevel::AVX512:
347
463
  return "AVX512";
464
+ case SIMDLevel::AVX512_VPOPCNT:
465
+ return "AVX512_VPOPCNT";
348
466
  case SIMDLevel::AVX512_SPR:
349
467
  return "AVX512_SPR";
350
468
  case SIMDLevel::ARM_NEON:
@@ -369,6 +487,9 @@ SIMDLevel to_simd_level(const std::string& level_str) {
369
487
  if (level_str == "AVX512") {
370
488
  return SIMDLevel::AVX512;
371
489
  }
490
+ if (level_str == "AVX512_VPOPCNT") {
491
+ return SIMDLevel::AVX512_VPOPCNT;
492
+ }
372
493
  if (level_str == "AVX512_SPR") {
373
494
  return SIMDLevel::AVX512_SPR;
374
495
  }
@@ -21,16 +21,84 @@ enum class SIMDLevel {
21
21
  // x86
22
22
  AVX2,
23
23
  AVX512,
24
- AVX512_SPR, // Sapphire Rapids: AVX512 + BF16 + FP16 + VNNI
24
+ AVX512_SPR, // Sapphire Rapids: AVX512_VPOPCNT + BF16 + FP16 + VNNI
25
25
  // arm & aarch64
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,
34
+
35
+ // Appended to preserve the numeric values of the existing public enum.
36
+ // AVX-512 core features plus AVX512_VPOPCNTDQ and AVX512_BITALG
37
+ // (Ice Lake, Zen 4, etc.).
38
+ AVX512_VPOPCNT,
30
39
 
31
40
  COUNT
32
41
  };
33
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
+
34
102
  /***************************************************************
35
103
  * SINGLE_SIMD_LEVEL: the SIMD level for code without explicit SL context.
36
104
  *
@@ -52,6 +120,8 @@ inline constexpr SIMDLevel SINGLE_SIMD_LEVEL = SIMDLevel::NONE;
52
120
  #else
53
121
  #if defined(COMPILE_SIMD_AVX512_SPR)
54
122
  inline constexpr SIMDLevel SINGLE_SIMD_LEVEL = SIMDLevel::AVX512_SPR;
123
+ #elif defined(COMPILE_SIMD_AVX512_VPOPCNT)
124
+ inline constexpr SIMDLevel SINGLE_SIMD_LEVEL = SIMDLevel::AVX512_VPOPCNT;
55
125
  #elif defined(COMPILE_SIMD_AVX512)
56
126
  inline constexpr SIMDLevel SINGLE_SIMD_LEVEL = SIMDLevel::AVX512;
57
127
  #elif defined(COMPILE_SIMD_AVX2)
@@ -71,7 +141,7 @@ inline constexpr SIMDLevel SINGLE_SIMD_LEVEL = SIMDLevel::NONE;
71
141
  * Helper to select the appropriate 256-bit SIMD level.
72
142
  *
73
143
  * For 256-bit SIMD types (simd16uint16, simd32uint8, etc.), maps:
74
- * AVX512/AVX512_SPR → AVX2 (256-bit ops use AVX2 instructions)
144
+ * AVX512/AVX512_VPOPCNT/AVX512_SPR → AVX2
75
145
  * AVX2 → AVX2
76
146
  * ARM_NEON/ARM_SVE → ARM_NEON
77
147
  * NONE → NONE
@@ -79,7 +149,8 @@ inline constexpr SIMDLevel SINGLE_SIMD_LEVEL = SIMDLevel::NONE;
79
149
  template <SIMDLevel SL>
80
150
  struct simd256_level_selector {
81
151
  static constexpr SIMDLevel value =
82
- (SL == SIMDLevel::AVX512 || SL == SIMDLevel::AVX512_SPR)
152
+ (SL == SIMDLevel::AVX512 || SL == SIMDLevel::AVX512_VPOPCNT ||
153
+ SL == SIMDLevel::AVX512_SPR)
83
154
  ? SIMDLevel::AVX2
84
155
  : (SL == SIMDLevel::ARM_SVE ? SIMDLevel::ARM_NEON
85
156
  : SL == SIMDLevel::RISCV_RVV ? SIMDLevel::NONE
@@ -96,13 +167,14 @@ inline constexpr SIMDLevel SINGLE_SIMD_LEVEL_256 =
96
167
  * Helper to select the appropriate 512-bit SIMD level.
97
168
  *
98
169
  * For 512-bit SIMD types (simd32uint16, simd64uint8, etc.), maps:
99
- * AVX512_SPR → AVX512 (512-bit ops share the same instructions)
170
+ * AVX512_VPOPCNT/AVX512_SPR → AVX512 for generic 512-bit operations
100
171
  * AVX512 → AVX512
101
172
  * NONE → NONE
102
173
  ***************************************************************/
103
174
  template <SIMDLevel SL>
104
175
  struct simd512_level_selector {
105
- static constexpr SIMDLevel value = (SL == SIMDLevel::AVX512_SPR)
176
+ static constexpr SIMDLevel value =
177
+ (SL == SIMDLevel::AVX512_SPR || SL == SIMDLevel::AVX512_VPOPCNT)
106
178
  ? SIMDLevel::AVX512
107
179
  : (SL == SIMDLevel::RISCV_RVV) ? SIMDLevel::NONE
108
180
  : SL;
@@ -124,14 +196,21 @@ constexpr int simd_width() {
124
196
  static_assert(
125
197
  SL != SIMDLevel::RISCV_RVV,
126
198
  "simd_width<RISCV_RVV> is not supported: RVV is variable-width");
127
- if constexpr (SL == SIMDLevel::AVX512 || SL == SIMDLevel::AVX512_SPR)
199
+ if constexpr (
200
+ SL == SIMDLevel::AVX512 || SL == SIMDLevel::AVX512_VPOPCNT ||
201
+ SL == SIMDLevel::AVX512_SPR) {
128
202
  return 16;
129
- else if constexpr (SL == SIMDLevel::AVX2 || SL == SIMDLevel::ARM_NEON)
203
+ } else if constexpr (SL == SIMDLevel::AVX2 || SL == SIMDLevel::ARM_NEON) {
130
204
  return 8;
131
- else
205
+ } else {
132
206
  return 1;
207
+ }
133
208
  }
134
209
 
210
+ /// Bitmask (1 << SIMDLevel) of the levels this binary holds code for,
211
+ /// whether or not the CPU supports them.
212
+ uint64_t compiled_simd_levels();
213
+
135
214
  /// Convert SIMDLevel to string. Throws FaissException for invalid level.
136
215
  std::string to_string(SIMDLevel level);
137
216
 
@@ -167,15 +246,20 @@ struct FAISS_API SIMDConfig {
167
246
  /// 256-bit kernel.
168
247
  static bool avx512_split;
169
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
+
170
259
  static SIMDLevel auto_detect_simd_level();
171
260
 
172
- static constexpr bool has_dynamic_dispatch() {
173
- #ifdef FAISS_ENABLE_DD
174
- return true;
175
- #else
176
- return false;
177
- #endif
178
- }
261
+ /// Whether this faiss build dispatches SIMD at runtime.
262
+ static bool has_dynamic_dispatch();
179
263
 
180
264
  SIMDConfig(const char** faiss_simd_level_env = nullptr);
181
265
 
@@ -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