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
@@ -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/rabitq_simd.h>
11
9
  #include <immintrin.h>
12
10
  #include <limits>
@@ -378,9 +376,11 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512>(
378
376
  }
379
377
  sum += reduce_add_128(sum_128);
380
378
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
381
- const auto yv = *(const uint64_t*)(data + offset);
379
+ uint64_t yv;
380
+ std::memcpy(&yv, data + offset, sizeof(yv));
382
381
  for (int j = 0; j < qb; j++) {
383
- const auto qv = *(const uint64_t*)(query + j * size + offset);
382
+ uint64_t qv;
383
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
384
384
  sum += popcount64(qv & yv) << j;
385
385
  }
386
386
  }
@@ -457,10 +457,12 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
457
457
  dot_product += reduce_add_128(dot_128);
458
458
  popcount_sum += reduce_add_128(pop_128);
459
459
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
460
- const auto yv = *(const uint64_t*)(data + offset);
460
+ uint64_t yv;
461
+ std::memcpy(&yv, data + offset, sizeof(yv));
461
462
  popcount_sum += popcount64(yv);
462
463
  for (int j = 0; j < qb; j++) {
463
- const auto qv = *(const uint64_t*)(query + j * size + offset);
464
+ uint64_t qv;
465
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
464
466
  dot_product += popcount64(qv & yv) << j;
465
467
  }
466
468
  }
@@ -527,9 +529,11 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
527
529
  }
528
530
  sum += reduce_add_128(sum_128);
529
531
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
530
- const auto yv = *(const uint64_t*)(data + offset);
532
+ uint64_t yv;
533
+ std::memcpy(&yv, data + offset, sizeof(yv));
531
534
  for (int j = 0; j < qb; j++) {
532
- const auto qv = *(const uint64_t*)(query + j * size + offset);
535
+ uint64_t qv;
536
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
533
537
  sum += popcount64(qv ^ yv) << j;
534
538
  }
535
539
  }
@@ -572,7 +576,8 @@ uint64_t popcount<SIMDLevel::AVX512>(const uint8_t* data, size_t size) {
572
576
  }
573
577
  sum += reduce_add_128(sum_128);
574
578
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
575
- const auto yv = *(const uint64_t*)(data + offset);
579
+ uint64_t yv;
580
+ std::memcpy(&yv, data + offset, sizeof(yv));
576
581
  sum += popcount64(yv);
577
582
  }
578
583
  for (; offset < size; ++offset) {
@@ -623,16 +628,6 @@ namespace faiss::rabitq::multibit {
623
628
 
624
629
  namespace {
625
630
 
626
- inline float hsum_avx2(__m256 v) {
627
- __m128 hi = _mm256_extractf128_ps(v, 1);
628
- __m128 lo = _mm256_castps256_ps128(v);
629
- lo = _mm_add_ps(lo, hi);
630
- __m128 shuf = _mm_movehdup_ps(lo);
631
- lo = _mm_add_ps(lo, shuf);
632
- shuf = _mm_movehl_ps(shuf, lo);
633
- return _mm_cvtss_f32(_mm_add_ss(lo, shuf));
634
- }
635
-
636
631
  inline float ip_1exbit_avx512(
637
632
  const uint8_t* __restrict sign_bits,
638
633
  const uint8_t* __restrict ex_code,
@@ -664,64 +659,85 @@ inline float ip_1exbit_avx512(
664
659
  return result;
665
660
  }
666
661
 
667
- // AVX2+BMI2 bitplane kernel used as fallback for ex_bits >= 2.
668
- // AVX512 TU has AVX2 available. BMI2 guarded separately since
669
- // VIA Eden X4 has AVX2 without BMI2.
670
- #ifdef __BMI2__
671
- inline float ip_bitplane_avx2(
662
+ // Bit-plane kernel for ex_bits >= 2. It scores 16 dims per pass.
663
+ // Each plane acts as a mask for one weighted add. Reads can run
664
+ // past the codes into the row tail, which stays in bounds.
665
+ inline float ip_bitplane_avx512(
672
666
  const uint8_t* __restrict sign_bits,
673
667
  const uint8_t* __restrict ex_code,
674
668
  const float* __restrict rotated_q,
675
669
  size_t d,
676
670
  size_t ex_bits,
677
671
  float cb) {
678
- __m256 acc = _mm256_setzero_ps();
679
- const __m256 v_one = _mm256_set1_ps(1.0f);
680
- const __m256i bit_pos = _mm256_setr_epi32(1, 2, 4, 8, 16, 32, 64, 128);
681
- const __m256i zero = _mm256_setzero_si256();
682
- const __m256 v_cb = _mm256_set1_ps(cb);
672
+ __m512 acc = _mm512_setzero_ps();
673
+ const __m512 v_cb = _mm512_set1_ps(cb);
683
674
 
684
675
  uint64_t pext_masks[7];
685
- __m256 v_weights[8];
676
+ __m512 v_weights[8];
686
677
  for (size_t b = 0; b < ex_bits; b++) {
687
678
  uint64_t m = 0;
688
679
  for (int j = 0; j < 8; j++) {
689
680
  m |= (1ULL << (b + j * ex_bits));
690
681
  }
691
682
  pext_masks[b] = m;
692
- v_weights[b] = _mm256_set1_ps(static_cast<float>(1u << b));
683
+ v_weights[b] = _mm512_set1_ps(static_cast<float>(1u << b));
693
684
  }
694
- v_weights[ex_bits] = _mm256_set1_ps(static_cast<float>(1u << ex_bits));
685
+ v_weights[ex_bits] = _mm512_set1_ps(static_cast<float>(1u << ex_bits));
695
686
 
696
687
  size_t i = 0;
697
- for (; i + 8 <= d; i += 8) {
698
- __m256i sb_cmp = _mm256_cmpgt_epi32(
699
- _mm256_and_si256(_mm256_set1_epi32(sign_bits[i / 8]), bit_pos),
700
- zero);
701
- __m256 recon = _mm256_mul_ps(
702
- _mm256_and_ps(_mm256_castsi256_ps(sb_cmp), v_one),
703
- v_weights[ex_bits]);
688
+ for (; i + 16 <= d; i += 16) {
689
+ uint16_t sb = 0;
690
+ memcpy(&sb, sign_bits + (i / 8), sizeof(uint16_t));
691
+ __m512 recon = _mm512_maskz_mov_ps(
692
+ static_cast<__mmask16>(sb), v_weights[ex_bits]);
693
+
694
+ uint64_t lo64 = 0;
695
+ uint64_t hi64 = 0;
696
+ memcpy(&lo64, ex_code + (i / 8) * ex_bits, sizeof(uint64_t));
697
+ memcpy(&hi64, ex_code + ((i / 8) + 1) * ex_bits, sizeof(uint64_t));
698
+
699
+ for (size_t b = 0; b < ex_bits; b++) {
700
+ const uint32_t plane =
701
+ static_cast<uint32_t>(_pext_u64(lo64, pext_masks[b])) |
702
+ (static_cast<uint32_t>(_pext_u64(hi64, pext_masks[b]))
703
+ << 8);
704
+ recon = _mm512_mask_add_ps(
705
+ recon, static_cast<__mmask16>(plane), recon, v_weights[b]);
706
+ }
704
707
 
705
- uint64_t ex64 = 0;
706
- memcpy(&ex64, ex_code + (i / 8) * ex_bits, sizeof(uint64_t));
708
+ __m512 rq = _mm512_loadu_ps(rotated_q + i);
709
+ acc = _mm512_fmadd_ps(rq, _mm512_add_ps(recon, v_cb), acc);
710
+ }
711
+
712
+ // Half-width step: keeps the scalar tail under 8 dims when d is a multiple
713
+ // of 8 but not of 16 (e.g. 200, 1000). The upper 8 lanes are masked off
714
+ // throughout, and rotated_q is loaded masked so nothing is read past the
715
+ // end.
716
+ if (i + 8 <= d) {
717
+ const __mmask16 low8 = static_cast<__mmask16>(0x00ff);
718
+ __m512 recon = _mm512_maskz_mov_ps(
719
+ static_cast<__mmask16>(sign_bits[i / 8]), v_weights[ex_bits]);
720
+
721
+ uint64_t lo64 = 0;
722
+ memcpy(&lo64, ex_code + (i / 8) * ex_bits, sizeof(uint64_t));
707
723
 
708
724
  for (size_t b = 0; b < ex_bits; b++) {
709
- auto plane = static_cast<uint8_t>(_pext_u64(ex64, pext_masks[b]));
710
- __m256i p_cmp = _mm256_cmpgt_epi32(
711
- _mm256_and_si256(_mm256_set1_epi32(plane), bit_pos), zero);
712
- __m256 p_f = _mm256_and_ps(_mm256_castsi256_ps(p_cmp), v_one);
713
- recon = _mm256_fmadd_ps(p_f, v_weights[b], recon);
725
+ const uint32_t plane =
726
+ static_cast<uint32_t>(_pext_u64(lo64, pext_masks[b]));
727
+ recon = _mm512_mask_add_ps(
728
+ recon, static_cast<__mmask16>(plane), recon, v_weights[b]);
714
729
  }
715
730
 
716
- __m256 rq = _mm256_loadu_ps(rotated_q + i);
717
- acc = _mm256_fmadd_ps(rq, _mm256_add_ps(recon, v_cb), acc);
731
+ __m512 rq = _mm512_maskz_loadu_ps(low8, rotated_q + i);
732
+ acc = _mm512_fmadd_ps(
733
+ rq, _mm512_mask_add_ps(recon, low8, recon, v_cb), acc);
734
+ i += 8;
718
735
  }
719
736
 
720
- float result = hsum_avx2(acc);
737
+ float result = _mm512_reduce_add_ps(acc);
721
738
  result += ip_scalar(sign_bits, ex_code, rotated_q, i, d, ex_bits, cb);
722
739
  return result;
723
740
  }
724
- #endif // __BMI2__
725
741
 
726
742
  } // namespace
727
743
 
@@ -733,18 +749,36 @@ float compute_inner_product<SIMDLevel::AVX512>(
733
749
  size_t d,
734
750
  size_t ex_bits,
735
751
  float cb) {
752
+ if (ex_bits == 8) {
753
+ // RBQ9 has one byte per extra code: no bit-plane extraction or BMI2.
754
+ __m512 acc = _mm512_setzero_ps();
755
+ const __m512 weight = _mm512_set1_ps(256.f);
756
+ const __m512 offset = _mm512_set1_ps(cb);
757
+ size_t i = 0;
758
+ for (; i + 16 <= d; i += 16) {
759
+ uint16_t signs;
760
+ memcpy(&signs, sign_bits + i / 8, sizeof(signs));
761
+ const __m128i bytes = _mm_loadu_si128(
762
+ reinterpret_cast<const __m128i*>(ex_code + i));
763
+ __m512 recon = _mm512_cvtepi32_ps(_mm512_cvtepu8_epi32(bytes));
764
+ recon = _mm512_mask_add_ps(recon, signs, recon, weight);
765
+ acc = _mm512_fmadd_ps(
766
+ _mm512_loadu_ps(rotated_q + i),
767
+ _mm512_add_ps(recon, offset),
768
+ acc);
769
+ }
770
+ return _mm512_reduce_add_ps(acc) +
771
+ ip_scalar(sign_bits, ex_code, rotated_q, i, d, ex_bits, cb);
772
+ }
736
773
  if (ex_bits == 1) {
737
774
  return ip_1exbit_avx512(sign_bits, ex_code, rotated_q, d, cb);
738
775
  }
739
776
 
740
- #ifdef __BMI2__
741
777
  if (ex_bits <= 7) {
742
- return ip_bitplane_avx2(sign_bits, ex_code, rotated_q, d, ex_bits, cb);
778
+ return ip_bitplane_avx512(
779
+ sign_bits, ex_code, rotated_q, d, ex_bits, cb);
743
780
  }
744
- #endif
745
781
  return ip_scalar(sign_bits, ex_code, rotated_q, 0, d, ex_bits, cb);
746
782
  }
747
783
 
748
784
  } // namespace faiss::rabitq::multibit
749
-
750
- #endif // COMPILE_SIMD_AVX512
@@ -6,35 +6,30 @@
6
6
  */
7
7
 
8
8
  /**
9
- * @file rabitq_avx512_spr.cpp
9
+ * @file rabitq_avx512_vpopcnt.cpp
10
10
  *
11
- * RaBitQ SIMD kernels specialized for SIMDLevel::AVX512_SPR.
11
+ * RaBitQ SIMD kernels specialized for SIMDLevel::AVX512_VPOPCNT.
12
12
  *
13
- * Sapphire Rapids (SPR) and later Intel microarchitectures expose
14
- * AVX-512 VPOPCNTDQ (vpopcntq), which performs a per-lane 64-bit
15
- * popcount in a single instruction. This is used here to replace the
16
- * multi-step shuffle/pshufb-based popcount used by the generic AVX-512
17
- * specialization in rabitq_avx512.cpp. The popcount-heavy kernels
18
- * (bitwise_and_dot_product, bitwise_xor_dot_product, popcount) become
19
- * substantially shorter and faster on SPR+ as a result.
13
+ * AVX-512 VPOPCNTDQ performs a per-lane 64-bit popcount in a single
14
+ * instruction. It is available on CPUs including Ice Lake, Zen 4, and
15
+ * Sapphire Rapids, independently of the other SPR-only extensions. This
16
+ * replaces the multi-step shuffle-based popcount used by the generic
17
+ * AVX-512 specialization in rabitq_avx512.cpp.
20
18
  *
21
19
  * Build / dispatch behavior:
22
20
  * - faiss_avx512 (AVX-512 only, no SPR features): NOT compiled.
23
21
  * The existing AVX512 specialization in rabitq_avx512.cpp is used.
24
- * - faiss_avx512_spr (statically built for SPR+): compiled. The
25
- * SINGLE_SIMD_LEVEL is AVX512_SPR, so this specialization is
26
- * selected by static dispatch.
22
+ * - faiss_avx512_spr: compiled alongside the full SPR specialization and
23
+ * selected through the SPR -> VPOPCNT fallback.
27
24
  * - faiss with FAISS_OPT_LEVEL=dd (dynamic dispatch): compiled with
28
25
  * -mavx512vpopcntdq as a per-file flag. Selected at runtime when
29
- * SIMDConfig::level == SIMDLevel::AVX512_SPR.
26
+ * the CPU exposes AVX512_VPOPCNTDQ.
30
27
  *
31
28
  * The floating-point multi-bit inner-product kernel does not benefit
32
- * from VPOPCNTDQ, so this TU forwards compute_inner_product<SPR> to
29
+ * from VPOPCNTDQ, so this TU forwards compute_inner_product<VPOPCNT> to
33
30
  * the AVX512 implementation to avoid duplicating that code path.
34
31
  */
35
32
 
36
- #ifdef COMPILE_SIMD_AVX512_SPR
37
-
38
33
  #include <faiss/utils/popcount.h>
39
34
  #include <faiss/utils/rabitq_simd.h>
40
35
  #include <immintrin.h>
@@ -47,7 +42,7 @@
47
42
  namespace faiss::rabitq {
48
43
 
49
44
  // Forward declarations for the AVX512 specializations defined in
50
- // rabitq_avx512.cpp. They live in the same TU group on SPR builds, so
45
+ // rabitq_avx512.cpp. They live in the same TU group in supported builds, so
51
46
  // we can reuse them as a tail handler / fallback. Declaring rather
52
47
  // than redefining avoids ODR risk and keeps a single source of truth
53
48
  // for the floating-point kernel.
@@ -66,6 +61,43 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512>(
66
61
  template <>
67
62
  uint64_t popcount<SIMDLevel::AVX512>(const uint8_t* data, size_t size);
68
63
 
64
+ template <>
65
+ void bitwise_q4_batch_4<SIMDLevel::AVX512_VPOPCNT>(
66
+ const uint8_t* query,
67
+ const uint8_t* const* data,
68
+ size_t size,
69
+ BitwiseAndDotProductResult* results) {
70
+ __m512i dots[4], pops[4];
71
+ for (int i = 0; i < 4; ++i) {
72
+ dots[i] = pops[i] = _mm512_setzero_si512();
73
+ }
74
+ for (size_t off = 0; off < size; off += 64) {
75
+ const size_t count = std::min(size - off, size_t(64));
76
+ const __mmask64 mask =
77
+ count == 64 ? ~__mmask64(0) : (__mmask64(1) << count) - 1;
78
+ __m512i x[4];
79
+ for (int i = 0; i < 4; ++i) {
80
+ x[i] = _mm512_maskz_loadu_epi8(mask, data[i] + off);
81
+ pops[i] = _mm512_add_epi64(pops[i], _mm512_popcnt_epi64(x[i]));
82
+ }
83
+ for (int bit = 0; bit < 4; ++bit) {
84
+ // Load each query plane once for four independent database codes.
85
+ const __m512i q =
86
+ _mm512_maskz_loadu_epi8(mask, query + bit * size + off);
87
+ for (int i = 0; i < 4; ++i) {
88
+ const __m512i p =
89
+ _mm512_popcnt_epi64(_mm512_and_si512(q, x[i]));
90
+ dots[i] = _mm512_add_epi64(dots[i], _mm512_slli_epi64(p, bit));
91
+ }
92
+ }
93
+ }
94
+ for (int i = 0; i < 4; ++i) {
95
+ results[i] = {
96
+ static_cast<uint64_t>(_mm512_reduce_add_epi64(dots[i])),
97
+ static_cast<uint64_t>(_mm512_reduce_add_epi64(pops[i]))};
98
+ }
99
+ }
100
+
69
101
  namespace {
70
102
 
71
103
  // 512-bit popcount using AVX-512 VPOPCNTDQ (vpopcntq).
@@ -75,8 +107,8 @@ inline __m512i popcount_512_vpopcntdq(__m512i v) {
75
107
  }
76
108
 
77
109
  // 256-bit popcount using AVX-512VL VPOPCNTDQ.
78
- // AVX512VL is part of the SPR feature set, so vpopcntq is available
79
- // on 256-bit registers via _mm256_popcnt_epi64.
110
+ // Baseline AVX-512 includes AVX512VL, so VPOPCNTDQ is also available on
111
+ // 256-bit registers via _mm256_popcnt_epi64.
80
112
  inline __m256i popcount_256_vpopcntdq(__m256i v) {
81
113
  return _mm256_popcnt_epi64(v);
82
114
  }
@@ -101,7 +133,7 @@ inline uint64_t reduce_add_128(__m128i v) {
101
133
  } // namespace
102
134
 
103
135
  template <>
104
- uint64_t bitwise_and_dot_product<SIMDLevel::AVX512_SPR>(
136
+ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512_VPOPCNT>(
105
137
  const uint8_t* query,
106
138
  const uint8_t* data,
107
139
  size_t size,
@@ -167,10 +199,11 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512_SPR>(
167
199
 
168
200
  // 64-bit scalar tail.
169
201
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
170
- const auto yv = *reinterpret_cast<const uint64_t*>(data + offset);
202
+ uint64_t yv;
203
+ std::memcpy(&yv, data + offset, sizeof(yv));
171
204
  for (size_t j = 0; j < qb; j++) {
172
- const auto qv = *reinterpret_cast<const uint64_t*>(
173
- query + j * size + offset);
205
+ uint64_t qv;
206
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
174
207
  sum += static_cast<uint64_t>(popcount64(qv & yv)) << j;
175
208
  }
176
209
  }
@@ -187,7 +220,7 @@ uint64_t bitwise_and_dot_product<SIMDLevel::AVX512_SPR>(
187
220
 
188
221
  template <>
189
222
  BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
190
- SIMDLevel::AVX512_SPR>(
223
+ SIMDLevel::AVX512_VPOPCNT>(
191
224
  const uint8_t* query,
192
225
  const uint8_t* data,
193
226
  size_t size,
@@ -258,11 +291,12 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
258
291
  popcount_sum += reduce_add_128(pop_128);
259
292
 
260
293
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
261
- const auto yv = *reinterpret_cast<const uint64_t*>(data + offset);
294
+ uint64_t yv;
295
+ std::memcpy(&yv, data + offset, sizeof(yv));
262
296
  popcount_sum += popcount64(yv);
263
297
  for (size_t j = 0; j < qb; j++) {
264
- const auto qv = *reinterpret_cast<const uint64_t*>(
265
- query + j * size + offset);
298
+ uint64_t qv;
299
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
266
300
  dot_product += static_cast<uint64_t>(popcount64(qv & yv)) << j;
267
301
  }
268
302
  }
@@ -278,7 +312,7 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
278
312
  }
279
313
 
280
314
  template <>
281
- uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512_SPR>(
315
+ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512_VPOPCNT>(
282
316
  const uint8_t* query,
283
317
  const uint8_t* data,
284
318
  size_t size,
@@ -339,10 +373,11 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512_SPR>(
339
373
  sum += reduce_add_128(sum_128);
340
374
 
341
375
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
342
- const auto yv = *reinterpret_cast<const uint64_t*>(data + offset);
376
+ uint64_t yv;
377
+ std::memcpy(&yv, data + offset, sizeof(yv));
343
378
  for (size_t j = 0; j < qb; j++) {
344
- const auto qv = *reinterpret_cast<const uint64_t*>(
345
- query + j * size + offset);
379
+ uint64_t qv;
380
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
346
381
  sum += static_cast<uint64_t>(popcount64(qv ^ yv)) << j;
347
382
  }
348
383
  }
@@ -357,7 +392,7 @@ uint64_t bitwise_xor_dot_product<SIMDLevel::AVX512_SPR>(
357
392
  }
358
393
 
359
394
  template <>
360
- uint64_t popcount<SIMDLevel::AVX512_SPR>(const uint8_t* data, size_t size) {
395
+ uint64_t popcount<SIMDLevel::AVX512_VPOPCNT>(const uint8_t* data, size_t size) {
361
396
  uint64_t sum = 0;
362
397
  size_t offset = 0;
363
398
 
@@ -392,7 +427,8 @@ uint64_t popcount<SIMDLevel::AVX512_SPR>(const uint8_t* data, size_t size) {
392
427
  sum += reduce_add_128(sum_128);
393
428
 
394
429
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
395
- const auto yv = *reinterpret_cast<const uint64_t*>(data + offset);
430
+ uint64_t yv;
431
+ std::memcpy(&yv, data + offset, sizeof(yv));
396
432
  sum += popcount64(yv);
397
433
  }
398
434
  for (; offset < size; ++offset) {
@@ -419,7 +455,7 @@ float compute_inner_product<SIMDLevel::AVX512>(
419
455
  float cb);
420
456
 
421
457
  template <>
422
- float compute_inner_product<SIMDLevel::AVX512_SPR>(
458
+ float compute_inner_product<SIMDLevel::AVX512_VPOPCNT>(
423
459
  const uint8_t* __restrict sign_bits,
424
460
  const uint8_t* __restrict ex_code,
425
461
  const float* __restrict rotated_q,
@@ -431,5 +467,3 @@ float compute_inner_product<SIMDLevel::AVX512_SPR>(
431
467
  }
432
468
 
433
469
  } // namespace faiss::rabitq::multibit
434
-
435
- #endif // COMPILE_SIMD_AVX512_SPR