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
@@ -11,9 +11,14 @@
11
11
  #error "THE_SIMD_LEVEL must be defined before including IVFPQScanner_impl.h"
12
12
  #endif
13
13
 
14
+ #include <algorithm>
15
+ #include <cstring>
16
+ #include <type_traits>
17
+
14
18
  #include <faiss/IndexIVFPQ.h>
15
19
  #include <faiss/impl/IDSelector.h>
16
20
  #include <faiss/impl/ResultHandler.h>
21
+ #include <faiss/impl/platform_macros.h>
17
22
  #include <faiss/impl/pq_code_distance/IVFPQ_QueryTables.h>
18
23
  #include <faiss/impl/pq_code_distance/pq_code_distance-inl.h>
19
24
  #include <faiss/impl/simd_dispatch.h>
@@ -29,19 +34,31 @@ struct WrappedSearchResult {
29
34
  ResultHandler& res;
30
35
  size_t nup = 0;
31
36
  idx_t list_no;
32
-
37
+ size_t list_size;
33
38
  const idx_t* ids;
34
39
  const IDSelector* sel;
40
+ IDSelectorContextDispatch dispatch;
35
41
 
36
42
  WrappedSearchResult(
37
43
  idx_t list_no_in,
44
+ size_t list_size_in,
38
45
  const idx_t* ids_in,
39
46
  const IDSelector* sel_in,
40
47
  ResultHandler& res_in)
41
- : res(res_in), list_no(list_no_in), ids(ids_in), sel(sel_in) {}
48
+ : res(res_in),
49
+ list_no(list_no_in),
50
+ list_size(list_size_in),
51
+ ids(ids_in),
52
+ sel(sel_in),
53
+ // A selector implies real ids, so ids==nullptr iff store_pairs;
54
+ // that disables the context path exactly when ids[] is synthetic.
55
+ dispatch(sel_in, /*store_pairs=*/ids_in == nullptr) {}
42
56
 
43
57
  inline bool skip_entry(idx_t j) {
44
- return use_sel && !sel->is_member(ids[j]);
58
+ return use_sel &&
59
+ !dispatch.is_member(
60
+ ids[j],
61
+ IDScanContext{ids, list_size, static_cast<size_t>(j)});
45
62
  }
46
63
 
47
64
  inline void add(idx_t j, float dis) {
@@ -58,6 +75,48 @@ struct WrappedSearchResult {
58
75
  }
59
76
  };
60
77
 
78
+ /** For each sub-quantizer, a block of codes reads one 1 KB table slice, which
79
+ * stays in L1; code by code, a table larger than L1 makes every lookup an L1
80
+ * miss. SL as in PQCodeDistanceScalar. */
81
+ template <SIMDLevel SL>
82
+ void pq_distances_8bit_by_block(
83
+ size_t M,
84
+ const float* sim_table,
85
+ float dis0,
86
+ size_t n,
87
+ const uint8_t* codes,
88
+ float* distances) {
89
+ constexpr size_t kBlock = 64;
90
+ constexpr size_t ksub = PQCodeDistanceScalar8<SL>::ksub;
91
+ for (size_t j0 = 0; j0 < n; j0 += kBlock) {
92
+ const size_t nb = std::min(kBlock, n - j0);
93
+ const uint8_t* block = codes + j0 * M;
94
+ float* acc = distances + j0;
95
+ std::fill(acc, acc + nb, 0.0f);
96
+ const float* tab = sim_table;
97
+ size_t m = 0;
98
+ for (; m + 4 <= M; m += 4, tab += 4 * ksub) {
99
+ const uint8_t* c = block + m;
100
+ for (size_t b = 0; b < nb; b++) {
101
+ uint32_t w;
102
+ memcpy(&w, c + b * M, sizeof(w));
103
+ acc[b] += (tab[w & 255] + tab[ksub + ((w >> 8) & 255)]) +
104
+ (tab[2 * ksub + ((w >> 16) & 255)] +
105
+ tab[3 * ksub + (w >> 24)]);
106
+ }
107
+ }
108
+ for (; m < M; m++, tab += ksub) {
109
+ const uint8_t* c = block + m;
110
+ for (size_t b = 0; b < nb; b++) {
111
+ acc[b] += tab[c[b * M]];
112
+ }
113
+ }
114
+ for (size_t b = 0; b < nb; b++) {
115
+ acc[b] += dis0;
116
+ }
117
+ }
118
+ }
119
+
61
120
  /*****************************************************
62
121
  * Scaning the codes.
63
122
  * The scanning functions call their favorite precompute_*
@@ -204,41 +263,41 @@ struct IVFPQScannerT : QueryTables {
204
263
  }
205
264
  }
206
265
 
266
+ float on_the_fly_dis0 = 0;
267
+
268
+ void prepare_on_the_fly_distance() {
269
+ on_the_fly_dis0 = 0;
270
+ if (!by_residual) {
271
+ return;
272
+ }
273
+ if (METRIC_TYPE == METRIC_INNER_PRODUCT) {
274
+ ivfpq.quantizer->reconstruct(key, residual_vec);
275
+ on_the_fly_dis0 = fvec_inner_product_dispatch(residual_vec, qi, d);
276
+ } else {
277
+ ivfpq.quantizer->compute_residual(qi, residual_vec, key);
278
+ }
279
+ }
280
+
281
+ float distance_to_code_on_the_fly(const uint8_t* code) const {
282
+ pq.decode(code, decoded_vec);
283
+ if (METRIC_TYPE == METRIC_INNER_PRODUCT) {
284
+ return on_the_fly_dis0 +
285
+ fvec_inner_product_dispatch(decoded_vec, qi, d);
286
+ }
287
+ const float* dvec = by_residual ? residual_vec : qi;
288
+ return fvec_L2sqr_dispatch(decoded_vec, dvec, d);
289
+ }
290
+
207
291
  /// nothing is precomputed: access residuals on-the-fly
208
292
  template <class SearchResultType>
209
293
  void scan_on_the_fly_dist(
210
294
  size_t ncode,
211
295
  const uint8_t* codes,
212
296
  SearchResultType& res) const {
213
- const float* dvec;
214
- float local_dis0 = 0;
215
- if (by_residual) {
216
- if (METRIC_TYPE == METRIC_INNER_PRODUCT) {
217
- ivfpq.quantizer->reconstruct(key, residual_vec);
218
- local_dis0 = fvec_inner_product_dispatch(residual_vec, qi, d);
219
- } else {
220
- ivfpq.quantizer->compute_residual(qi, residual_vec, key);
221
- }
222
- dvec = residual_vec;
223
- } else {
224
- dvec = qi;
225
- local_dis0 = 0;
226
- }
227
-
228
297
  for (size_t j = 0; j < ncode; j++, codes += pq.code_size) {
229
- if (res.skip_entry(j)) {
230
- continue;
298
+ if (!res.skip_entry(j)) {
299
+ res.add(j, distance_to_code_on_the_fly(codes));
231
300
  }
232
- pq.decode(codes, decoded_vec);
233
-
234
- float dis;
235
- if (METRIC_TYPE == METRIC_INNER_PRODUCT) {
236
- dis = local_dis0 +
237
- fvec_inner_product_dispatch(decoded_vec, qi, d);
238
- } else {
239
- dis = fvec_L2sqr_dispatch(decoded_vec, dvec, d);
240
- }
241
- res.add(j, dis);
242
301
  }
243
302
  }
244
303
 
@@ -399,10 +458,8 @@ struct IVFPQScannerT : QueryTables {
399
458
  }
400
459
  }
401
460
 
402
- #pragma omp critical
403
- {
404
- indexIVFPQ_stats.n_hamming_pass += n_hamming_pass;
405
- }
461
+ detail::atomic_fetch_add_relaxed(
462
+ indexIVFPQ_stats.n_hamming_pass, n_hamming_pass);
406
463
  }
407
464
 
408
465
  template <class SearchResultType>
@@ -426,7 +483,6 @@ struct IVFPQScannerT : QueryTables {
426
483
  *
427
484
  * precompute_mode is how much we precompute (2 = precompute distance tables,
428
485
  * 1 = precompute pointers to distances, 0 = compute distances one by one).
429
- * Currently only 2 is supported
430
486
  *
431
487
  * use_sel: store or ignore the IDSelector
432
488
  */
@@ -451,20 +507,52 @@ struct IVFPQScanner : IVFPQScannerT<idx_t, METRIC_TYPE, PQCodeDist>,
451
507
  }
452
508
 
453
509
  void set_query(const float* query) override {
454
- this->init_query(query);
510
+ if (precompute_mode == 0) {
511
+ this->qi = query;
512
+ } else {
513
+ this->init_query(query);
514
+ }
455
515
  }
456
516
 
457
517
  void set_list(idx_t list_no_in, float coarse_dis_in) override {
458
518
  this->list_no = list_no_in;
459
519
  this->init_list(list_no_in, coarse_dis_in, precompute_mode);
520
+ if (precompute_mode == 0) {
521
+ this->prepare_on_the_fly_distance();
522
+ }
460
523
  }
461
524
 
462
525
  float distance_to_code(const uint8_t* code) const override {
526
+ if (precompute_mode == 0) {
527
+ return this->distance_to_code_on_the_fly(code);
528
+ }
463
529
  FAISS_THROW_IF_NOT(precompute_mode == 2);
464
- float dis = this->dis0 +
530
+ return this->dis0 +
465
531
  PQCodeDist::distance_single_code(
466
- this->pq.M, this->pq.nbits, this->sim_table, code);
467
- return dis;
532
+ this->pq.M, this->pq.nbits, this->sim_table, code);
533
+ }
534
+
535
+ void distances_to_codes(size_t n, const uint8_t* codes, float* distances)
536
+ const override {
537
+ // Where gathers are fast (Intel), the block kernel is faster at
538
+ // M <= 64, and the per-code gather kernels are faster at M = 128
539
+ // unless all cores are busy.
540
+ if constexpr (
541
+ has_scalar_8bit_kernel<THE_SIMD_LEVEL>() &&
542
+ std::is_same_v<typename PQCodeDist::PQDecoder, PQDecoder8>) {
543
+ if (precompute_mode == 2 &&
544
+ (SIMDConfig::scalar_gather || this->pq.M <= 64)) {
545
+ pq_distances_8bit_by_block<THE_SIMD_LEVEL>(
546
+ this->pq.M,
547
+ this->sim_table,
548
+ this->dis0,
549
+ n,
550
+ codes,
551
+ distances);
552
+ return;
553
+ }
554
+ }
555
+ InvertedListScanner::distances_to_codes(n, codes, distances);
468
556
  }
469
557
 
470
558
  size_t scan_codes(
@@ -474,6 +562,7 @@ struct IVFPQScanner : IVFPQScannerT<idx_t, METRIC_TYPE, PQCodeDist>,
474
562
  ResultHandler& handler) const override {
475
563
  WrappedSearchResult<C, use_sel> res(
476
564
  this->key,
565
+ ncode,
477
566
  this->store_pairs ? nullptr : ids,
478
567
  this->sel,
479
568
  handler);
@@ -498,27 +587,40 @@ template <SIMDLevel SL>
498
587
  InvertedListScanner* make_IVFPQInvertedListScanner(
499
588
  const IndexIVFPQ& ivfpq,
500
589
  bool store_pairs,
501
- const IDSelector* sel);
590
+ const IDSelector* sel,
591
+ IndexIVFPQ::ScannerMode mode);
592
+
593
+ template <SIMDLevel SL>
594
+ inline InvertedListScanner* make_IVFPQInvertedListScanner(
595
+ const IndexIVFPQ& ivfpq,
596
+ bool store_pairs,
597
+ const IDSelector* sel) {
598
+ return make_IVFPQInvertedListScanner<SL>(
599
+ ivfpq, store_pairs, sel, IndexIVFPQ::ScannerMode::Precomputed);
600
+ }
502
601
 
503
602
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
504
603
  template <>
505
604
  InvertedListScanner* make_IVFPQInvertedListScanner<THE_SIMD_LEVEL>(
506
605
  const IndexIVFPQ& ivfpq,
507
606
  bool store_pairs,
508
- const IDSelector* sel) {
607
+ const IDSelector* sel,
608
+ IndexIVFPQ::ScannerMode mode) {
609
+ const int precompute_mode =
610
+ mode == IndexIVFPQ::ScannerMode::Precomputed ? 2 : 0;
509
611
  auto make = [&]<class PQCodeDist, bool use_sel>() -> InvertedListScanner* {
510
612
  if (ivfpq.metric_type == METRIC_INNER_PRODUCT) {
511
613
  return new IVFPQScanner<
512
614
  METRIC_INNER_PRODUCT,
513
615
  CMin<float, idx_t>,
514
616
  PQCodeDist,
515
- use_sel>(ivfpq, store_pairs, 2, sel);
617
+ use_sel>(ivfpq, store_pairs, precompute_mode, sel);
516
618
  } else if (ivfpq.metric_type == METRIC_L2) {
517
619
  return new IVFPQScanner<
518
620
  METRIC_L2,
519
621
  CMax<float, idx_t>,
520
622
  PQCodeDist,
521
- use_sel>(ivfpq, store_pairs, 2, sel);
623
+ use_sel>(ivfpq, store_pairs, precompute_mode, sel);
522
624
  } else {
523
625
  FAISS_THROW_MSG("unsupported metric type");
524
626
  }
@@ -526,6 +628,13 @@ InvertedListScanner* make_IVFPQInvertedListScanner<THE_SIMD_LEVEL>(
526
628
 
527
629
  auto with_decoder = [&]<bool use_sel>() -> InvertedListScanner* {
528
630
  if (ivfpq.pq.nbits == 8) {
631
+ if constexpr (has_scalar_8bit_kernel<THE_SIMD_LEVEL>()) {
632
+ if (use_scalar_8bit_kernel<THE_SIMD_LEVEL>(ivfpq.pq.M)) {
633
+ return make.template operator()<
634
+ PQCodeDistanceScalar8Dist<THE_SIMD_LEVEL>,
635
+ use_sel>();
636
+ }
637
+ }
529
638
  return make.template
530
639
  operator()<PQCodeDistance<PQDecoder8, THE_SIMD_LEVEL>, use_sel>();
531
640
  } else if (ivfpq.pq.nbits == 16) {
@@ -102,6 +102,12 @@ template <>
102
102
  FlatCodesDistanceComputer* get_PQFlatCodesDistanceComputer<THE_SIMD_LEVEL>(
103
103
  const IndexPQ& index) {
104
104
  if (index.pq.nbits == 8) {
105
+ if constexpr (has_scalar_8bit_kernel<THE_SIMD_LEVEL>()) {
106
+ if (use_scalar_8bit_kernel<THE_SIMD_LEVEL>(index.pq.M)) {
107
+ return new PQDistanceComputer<
108
+ PQCodeDistanceScalar8Dist<THE_SIMD_LEVEL>>(index);
109
+ }
110
+ }
105
111
  return new PQDistanceComputer<
106
112
  PQCodeDistance<PQDecoder8, THE_SIMD_LEVEL>>(index);
107
113
  } else if (index.pq.nbits == 16) {
@@ -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
  #define THE_SIMD_LEVEL SIMDLevel::AVX2
11
9
 
12
10
  // NOLINTNEXTLINE(facebook-hte-InlineHeader,facebook-unused-include-check)
@@ -19,5 +17,3 @@
19
17
  #include <faiss/impl/pq_code_distance/PQDistanceComputer_impl.h>
20
18
  // NOLINTNEXTLINE(facebook-hte-InlineHeader,facebook-unused-include-check)
21
19
  #include <faiss/impl/pq_code_distance/IVFPQScanner_impl.h>
22
-
23
- #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
  #define THE_SIMD_LEVEL SIMDLevel::AVX512
11
9
 
12
10
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
@@ -19,5 +17,3 @@
19
17
  #include <faiss/impl/pq_code_distance/PQDistanceComputer_impl.h>
20
18
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
21
19
  #include <faiss/impl/pq_code_distance/IVFPQScanner_impl.h>
22
-
23
- #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_NEON
9
-
10
8
  #define THE_SIMD_LEVEL SIMDLevel::ARM_NEON
11
9
 
12
10
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
@@ -19,5 +17,3 @@
19
17
  #include <faiss/impl/pq_code_distance/PQDistanceComputer_impl.h>
20
18
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
21
19
  #include <faiss/impl/pq_code_distance/IVFPQScanner_impl.h>
22
-
23
- #endif // COMPILE_SIMD_ARM_NEON
@@ -160,6 +160,10 @@ inline void distance_four_codes_avx2_pqdecoder8_m8(
160
160
  namespace faiss {
161
161
  namespace pq_code_distance {
162
162
 
163
+ // On AMD Zen 3/4/5 (SIMDConfig::scalar_gather) the scalar kernel is 0-48%
164
+ // faster than these gathers at M = 8-384; on Intel the gathers are faster.
165
+ // The callers select it with use_scalar_8bit_kernel().
166
+
163
167
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
164
168
  template <>
165
169
  float pq_code_distance_8bit_single_impl<SIMDLevel::AVX2>(
@@ -18,7 +18,10 @@ namespace pq_code_distance {
18
18
  // the AVX2 version, which is somewhat unexpected.
19
19
  // This version is kept for completeness.
20
20
  //
21
- // TODO: test for AMD CPUs.
21
+ // On AMD Zen 4/5 (SIMDConfig::scalar_gather) the scalar kernel is up to 48%
22
+ // faster at M >= 16. Below M = 16 this kernel has no gather and its loop is
23
+ // as fast or faster, so it is kept there. The callers select the scalar
24
+ // kernel with use_scalar_8bit_kernel().
22
25
 
23
26
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
24
27
  template <>
@@ -7,7 +7,7 @@
7
7
 
8
8
  // This TU provides non-templated PQ code distance dispatch wrappers
9
9
  // (pq_code_distance_8bit_single, pq_code_distance_8bit_four) declared
10
- // in pq_code_distance-inl.h. These use with_simd_level to route to the
10
+ // in pq_code_distance-inl.h. These use with_simd_level_with_sve to route to the
11
11
  // best available SIMD implementation via pq_code_distance_8bit_*_impl
12
12
  // function template specializations.
13
13
  //
@@ -34,17 +34,22 @@ void pq_scan_8bit(
34
34
  float* heap_dis,
35
35
  int64_t* heap_ids,
36
36
  bool max_heap) {
37
- with_simd_level([&]<SIMDLevel SL>() {
37
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
38
38
  pq_scan_8bit_impl<SL>(
39
39
  M, dis_table, codes, ncodes, k, heap_dis, heap_ids, max_heap);
40
40
  });
41
41
  }
42
42
 
43
+ // NONE: only the files of level SL may instantiate PQCodeDistanceScalar8<SL>.
43
44
  float pq_code_distance_8bit_single(
44
45
  size_t M,
45
46
  const float* sim_table,
46
47
  const uint8_t* code) {
47
- return with_simd_level([&]<SIMDLevel SL>() {
48
+ return with_simd_level_with_sve([&]<SIMDLevel SL>() {
49
+ if (use_scalar_8bit_kernel<SL>(M)) {
50
+ return PQCodeDistanceScalar8<SIMDLevel::NONE>::distance_single_code(
51
+ M, sim_table, code);
52
+ }
48
53
  return pq_code_distance_8bit_single_impl<SL>(M, sim_table, code);
49
54
  });
50
55
  }
@@ -60,7 +65,21 @@ void pq_code_distance_8bit_four(
60
65
  float& result1,
61
66
  float& result2,
62
67
  float& result3) {
63
- with_simd_level([&]<SIMDLevel SL>() {
68
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
69
+ if (use_scalar_8bit_kernel<SL>(M)) {
70
+ PQCodeDistanceScalar8<SIMDLevel::NONE>::distance_four_codes(
71
+ M,
72
+ sim_table,
73
+ code0,
74
+ code1,
75
+ code2,
76
+ code3,
77
+ result0,
78
+ result1,
79
+ result2,
80
+ result3);
81
+ return;
82
+ }
64
83
  pq_code_distance_8bit_four_impl<SL>(
65
84
  M,
66
85
  sim_table,
@@ -20,8 +20,8 @@ inline float pq_code_distance_8bit_single_impl<SIMDLevel::NONE>(
20
20
  size_t M,
21
21
  const float* sim_table,
22
22
  const uint8_t* code) {
23
- return PQCodeDistanceScalar<PQDecoder8>::distance_single_code(
24
- M, 8, sim_table, code);
23
+ return PQCodeDistanceScalar<PQDecoder8, SIMDLevel::NONE>::
24
+ distance_single_code(M, 8, sim_table, code);
25
25
  }
26
26
 
27
27
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
@@ -37,7 +37,7 @@ inline void pq_code_distance_8bit_four_impl<SIMDLevel::NONE>(
37
37
  float& result1,
38
38
  float& result2,
39
39
  float& result3) {
40
- PQCodeDistanceScalar<PQDecoder8>::distance_four_codes(
40
+ PQCodeDistanceScalar<PQDecoder8, SIMDLevel::NONE>::distance_four_codes(
41
41
  M,
42
42
  8,
43
43
  sim_table,
@@ -60,8 +60,8 @@ inline float pq_code_distance_8bit_single_impl<SIMDLevel::ARM_NEON>(
60
60
  size_t M,
61
61
  const float* sim_table,
62
62
  const uint8_t* code) {
63
- return PQCodeDistanceScalar<PQDecoder8>::distance_single_code(
64
- M, 8, sim_table, code);
63
+ return PQCodeDistanceScalar<PQDecoder8, SIMDLevel::ARM_NEON>::
64
+ distance_single_code(M, 8, sim_table, code);
65
65
  }
66
66
 
67
67
  // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
@@ -77,7 +77,7 @@ inline void pq_code_distance_8bit_four_impl<SIMDLevel::ARM_NEON>(
77
77
  float& result1,
78
78
  float& result2,
79
79
  float& result3) {
80
- PQCodeDistanceScalar<PQDecoder8>::distance_four_codes(
80
+ PQCodeDistanceScalar<PQDecoder8, SIMDLevel::ARM_NEON>::distance_four_codes(
81
81
  M,
82
82
  8,
83
83
  sim_table,
@@ -23,6 +23,7 @@
23
23
  #include <faiss/impl/ProductQuantizer.h>
24
24
  #include <faiss/impl/platform_macros.h>
25
25
  #include <faiss/impl/simd_dispatch.h>
26
+ #include <faiss/utils/simd_levels.h>
26
27
 
27
28
  namespace faiss {
28
29
  namespace pq_code_distance {
@@ -62,10 +63,14 @@ namespace pq_code_distance {
62
63
  *********************************************************************/
63
64
 
64
65
  /// Scalar PQ code distance implementation.
65
- /// Templated only on decoder type, independent of SIMD level.
66
66
  /// Used directly by non-PQDecoder8 decoders (PQDecoder16,
67
67
  /// PQDecoderGeneric) and as fallback for PQDecoder8 at NONE/NEON.
68
- template <typename PQDecoderT>
68
+ // SL keeps each SIMD-level TU's instantiation a distinct symbol. Without it
69
+ // the linker merges them and may keep one compiled with -mavx512*, which
70
+ // then runs on the AVX2 path and raises SIGILL on CPUs without AVX512.
71
+ // The default keeps callers outside faiss that name only the decoder (e.g.
72
+ // unicorn/features/encoders/PQFSTable.h) compiling; faiss passes SL itself.
73
+ template <typename PQDecoderT, SIMDLevel SL = SIMDLevel::NONE>
69
74
  struct PQCodeDistanceScalar {
70
75
  using PQDecoder = PQDecoderT;
71
76
 
@@ -124,6 +129,125 @@ struct PQCodeDistanceScalar {
124
129
  }
125
130
  };
126
131
 
132
+ /// 8-bit scalar kernel with four partial sums (one running sum serializes on
133
+ /// add latency). Used instead of the gather kernels where
134
+ /// SIMDConfig::scalar_gather is set. Both functions use the same per-code
135
+ /// summation order (partial sum m % 4, tail into the first), so single-code
136
+ /// and four-code results are bit-identical. SL as in PQCodeDistanceScalar.
137
+ template <SIMDLevel SL>
138
+ struct PQCodeDistanceScalar8 {
139
+ static constexpr size_t ksub = 256;
140
+
141
+ static float distance_single_code(
142
+ size_t M,
143
+ const float* tab,
144
+ const uint8_t* code) {
145
+ float a[4] = {};
146
+ size_t m = 0;
147
+ for (; m + 4 <= M; m += 4, tab += 4 * ksub) {
148
+ for (size_t j = 0; j < 4; j++) {
149
+ a[j] += tab[j * ksub + code[m + j]];
150
+ }
151
+ }
152
+ for (; m < M; m++, tab += ksub) {
153
+ a[0] += tab[code[m]];
154
+ }
155
+ return (a[0] + a[1]) + (a[2] + a[3]);
156
+ }
157
+
158
+ static void distance_four_codes(
159
+ size_t M,
160
+ const float* tab,
161
+ const uint8_t* __restrict code0,
162
+ const uint8_t* __restrict code1,
163
+ const uint8_t* __restrict code2,
164
+ const uint8_t* __restrict code3,
165
+ float& result0,
166
+ float& result1,
167
+ float& result2,
168
+ float& result3) {
169
+ const uint8_t* codes[4] = {code0, code1, code2, code3};
170
+ float a[4][4] = {};
171
+ size_t m = 0;
172
+ for (; m + 4 <= M; m += 4, tab += 4 * ksub) {
173
+ for (size_t j = 0; j < 4; j++) {
174
+ for (size_t c = 0; c < 4; c++) {
175
+ a[c][j] += tab[j * ksub + codes[c][m + j]];
176
+ }
177
+ }
178
+ }
179
+ for (; m < M; m++, tab += ksub) {
180
+ for (size_t c = 0; c < 4; c++) {
181
+ a[c][0] += tab[codes[c][m]];
182
+ }
183
+ }
184
+ float* out[4] = {&result0, &result1, &result2, &result3};
185
+ for (size_t c = 0; c < 4; c++) {
186
+ *out[c] = (a[c][0] + a[c][1]) + (a[c][2] + a[c][3]);
187
+ }
188
+ }
189
+ };
190
+
191
+ /// SIMDConfig::scalar_gather is set and the gather kernel of level SL is
192
+ /// slower than PQCodeDistanceScalar8: the AVX2 kernel at any M, the AVX-512
193
+ /// kernel at M >= 16. The callers check this once per scan, not per code.
194
+ template <SIMDLevel SL>
195
+ inline bool use_scalar_8bit_kernel(size_t M) {
196
+ if constexpr (SL == SIMDLevel::AVX2) {
197
+ return SIMDConfig::scalar_gather;
198
+ } else if constexpr (SL == SIMDLevel::AVX512) {
199
+ return SIMDConfig::scalar_gather && M >= 16;
200
+ } else {
201
+ return false;
202
+ }
203
+ }
204
+
205
+ template <SIMDLevel SL>
206
+ constexpr bool has_scalar_8bit_kernel() {
207
+ return SL == SIMDLevel::AVX2 || SL == SIMDLevel::AVX512;
208
+ }
209
+
210
+ /// PQCodeDistance interface of PQCodeDistanceScalar8.
211
+ template <SIMDLevel SL>
212
+ struct PQCodeDistanceScalar8Dist {
213
+ using PQDecoder = PQDecoder8;
214
+ static constexpr SIMDLevel simd_level = SL;
215
+
216
+ static float distance_single_code(
217
+ size_t M,
218
+ size_t /* nbits */,
219
+ const float* sim_table,
220
+ const uint8_t* code) {
221
+ return PQCodeDistanceScalar8<SL>::distance_single_code(
222
+ M, sim_table, code);
223
+ }
224
+
225
+ static void distance_four_codes(
226
+ size_t M,
227
+ size_t /* nbits */,
228
+ const float* sim_table,
229
+ const uint8_t* __restrict code0,
230
+ const uint8_t* __restrict code1,
231
+ const uint8_t* __restrict code2,
232
+ const uint8_t* __restrict code3,
233
+ float& result0,
234
+ float& result1,
235
+ float& result2,
236
+ float& result3) {
237
+ PQCodeDistanceScalar8<SL>::distance_four_codes(
238
+ M,
239
+ sim_table,
240
+ code0,
241
+ code1,
242
+ code2,
243
+ code3,
244
+ result0,
245
+ result1,
246
+ result2,
247
+ result3);
248
+ }
249
+ };
250
+
127
251
  /*********************************************************************
128
252
  * Dispatch bridge — function templates for PQDecoder8 SIMD dispatch.
129
253
  *
@@ -168,7 +292,7 @@ struct PQCodeDistance {
168
292
  if constexpr (std::is_same_v<PQDecoderT, PQDecoder8>) {
169
293
  return pq_code_distance_8bit_single_impl<SL>(M, sim_table, code);
170
294
  } else {
171
- return PQCodeDistanceScalar<PQDecoderT>::distance_single_code(
295
+ return PQCodeDistanceScalar<PQDecoderT, SL>::distance_single_code(
172
296
  M, nbits, sim_table, code);
173
297
  }
174
298
  }
@@ -198,7 +322,7 @@ struct PQCodeDistance {
198
322
  result2,
199
323
  result3);
200
324
  } else {
201
- PQCodeDistanceScalar<PQDecoderT>::distance_four_codes(
325
+ PQCodeDistanceScalar<PQDecoderT, SL>::distance_four_codes(
202
326
  M,
203
327
  nbits,
204
328
  sim_table,
@@ -250,8 +374,9 @@ FAISS_API void pq_code_distance_8bit_four(
250
374
  *
251
375
  * Scans all ncodes PQ codes against a precomputed distance table,
252
376
  * maintaining a k-nearest-neighbor heap. Uses the SIMD PQ distance
253
- * kernels (AVX2 gathers, etc.) for the inner loop, with the SIMD
254
- * gathers inlined into the scan loop in each per-SIMD TU.
377
+ * kernels (AVX2 gathers, etc.), or PQCodeDistanceScalar8 where
378
+ * use_scalar_8bit_kernel() selects it, inlined into the scan loop in
379
+ * each per-SIMD TU.
255
380
  *
256
381
  * Definitions are in pq_scan_impl.h (per-SIMD TUs) and
257
382
  * pq_code_distance-generic.cpp (dispatch wrapper).
@@ -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 <arm_sve.h>
11
9
 
12
10
  #include <faiss/impl/pq_code_distance/pq_code_distance-inl.h>
@@ -351,5 +349,3 @@ void pq_code_distance_8bit_four_impl<SIMDLevel::ARM_SVE>(
351
349
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
352
350
  #include <faiss/impl/pq_code_distance/pq_scan_impl.h>
353
351
  #undef THE_SIMD_LEVEL
354
-
355
- #endif // COMPILE_SIMD_ARM_SVE