faiss 0.6.4 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/ext/faiss/extconf.rb +2 -1
  4. data/ext/faiss/index.cpp +42 -0
  5. data/lib/faiss/version.rb +1 -1
  6. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  7. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  8. data/vendor/faiss/faiss/Clustering.h +29 -0
  9. data/vendor/faiss/faiss/IVFlib.cpp +147 -51
  10. data/vendor/faiss/faiss/IVFlib.h +21 -2
  11. data/vendor/faiss/faiss/Index.cpp +44 -0
  12. data/vendor/faiss/faiss/Index.h +18 -13
  13. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  14. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  15. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +85 -4
  17. data/vendor/faiss/faiss/IndexBinaryHNSW.h +19 -0
  18. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -11
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +34 -18
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +57 -1
  22. data/vendor/faiss/faiss/IndexFlat.h +31 -0
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +461 -13
  25. data/vendor/faiss/faiss/IndexHNSW.h +40 -5
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +45 -0
  27. data/vendor/faiss/faiss/IndexIDMap.h +15 -3
  28. data/vendor/faiss/faiss/IndexIVF.cpp +349 -68
  29. data/vendor/faiss/faiss/IndexIVF.h +90 -2
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +51 -29
  31. data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +22 -6
  34. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +2 -1
  35. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  36. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +78 -58
  37. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +7 -13
  38. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
  39. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +860 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
  46. data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
  47. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +7 -4
  48. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +12 -6
  49. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  50. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  51. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  52. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  53. data/vendor/faiss/faiss/IndexShards.cpp +10 -0
  54. data/vendor/faiss/faiss/IndexShards.h +5 -0
  55. data/vendor/faiss/faiss/SuperKMeans.cpp +59 -17
  56. data/vendor/faiss/faiss/SuperKMeans.h +9 -4
  57. data/vendor/faiss/faiss/VectorTransform.cpp +159 -11
  58. data/vendor/faiss/faiss/VectorTransform.h +24 -1
  59. data/vendor/faiss/faiss/clone_index.cpp +24 -0
  60. data/vendor/faiss/faiss/factory_tools.cpp +19 -1
  61. data/vendor/faiss/faiss/gpu/Dataset.h +164 -0
  62. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  63. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  64. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +3 -0
  65. data/vendor/faiss/faiss/gpu/GpuIndex.h +4 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +32 -13
  67. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +51 -22
  68. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  69. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  70. data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
  71. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
  72. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  73. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  74. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  75. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
  76. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  77. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  78. data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
  79. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +12 -1
  80. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +93 -1
  81. data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
  82. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +5 -1
  83. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  84. data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
  85. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
  86. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +39 -1
  87. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +107 -1
  88. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +11 -1
  89. data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
  90. data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
  91. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
  92. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
  93. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +3 -1
  94. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  95. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  96. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  97. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +267 -136
  98. data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
  99. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  100. data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
  101. data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
  102. data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
  103. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  104. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  105. data/vendor/faiss/faiss/impl/HNSW.cpp +48 -12
  106. data/vendor/faiss/faiss/impl/HNSW.h +74 -2
  107. data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
  108. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  109. data/vendor/faiss/faiss/impl/NNDescent.cpp +2 -1
  110. data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
  111. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  112. data/vendor/faiss/faiss/impl/Panorama.h +3 -2
  113. data/vendor/faiss/faiss/impl/PiPNN.cpp +824 -0
  114. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  115. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
  116. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +54 -11
  117. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
  118. data/vendor/faiss/faiss/impl/RaBitQUtils.h +30 -1
  119. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +91 -66
  120. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +15 -0
  121. data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
  122. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -0
  123. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +1 -6
  124. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  125. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  127. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
  128. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  129. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  130. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -0
  133. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  134. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  135. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  136. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +17 -129
  137. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  138. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +73 -6
  139. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
  140. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -4
  141. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -4
  142. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +10 -85
  143. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  144. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  145. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +106 -1
  146. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -6
  147. data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
  148. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +105 -78
  149. data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
  150. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  151. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  152. data/vendor/faiss/faiss/impl/index_read.cpp +318 -9
  153. data/vendor/faiss/faiss/impl/index_write.cpp +124 -5
  154. data/vendor/faiss/faiss/impl/io.cpp +1 -0
  155. data/vendor/faiss/faiss/impl/io_macros.h +2 -2
  156. data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
  157. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
  158. data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
  159. data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
  160. data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
  161. data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
  162. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
  163. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
  164. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
  165. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
  166. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
  167. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
  168. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  169. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  170. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  171. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  172. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  173. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  174. data/vendor/faiss/faiss/impl/platform_macros.h +91 -27
  175. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  176. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +84 -4
  177. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
  178. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  179. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  180. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  181. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  182. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  183. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  184. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +19 -0
  185. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  186. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  187. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  188. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  189. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  190. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  191. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  192. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
  193. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -6
  194. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -5
  195. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  196. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  197. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
  198. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +5 -1
  199. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  200. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +85 -37
  201. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +262 -110
  202. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +503 -122
  203. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +10 -155
  204. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +7 -701
  205. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +66 -57
  206. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +133 -127
  207. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  208. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  209. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  210. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  211. data/vendor/faiss/faiss/impl/simd_dispatch.h +7 -0
  212. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  213. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
  214. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  215. data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
  216. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  217. data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
  218. data/vendor/faiss/faiss/index_factory.cpp +106 -6
  219. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  220. data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
  221. data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
  222. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +15 -10
  223. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
  224. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
  225. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
  226. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
  227. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
  228. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
  229. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
  230. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
  231. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
  232. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +3 -2
  233. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  234. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  235. data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
  236. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  237. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  238. data/vendor/faiss/faiss/utils/distances.cpp +466 -43
  239. data/vendor/faiss/faiss/utils/distances.h +179 -2
  240. data/vendor/faiss/faiss/utils/distances_dispatch.h +6 -4
  241. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  242. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +0 -14
  243. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  244. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  247. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  248. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  249. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  250. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  251. data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
  252. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  253. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +0 -4
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +33 -5
  256. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +15 -30
  257. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  258. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +2 -1
  259. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  260. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  261. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  262. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  263. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  264. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  265. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  266. data/vendor/faiss/faiss/utils/rabitq_simd.h +171 -22
  267. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  268. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +81 -9
  269. data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
  270. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +108 -9
  271. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +111 -12
  272. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +353 -71
  273. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  274. data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
  275. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +106 -0
  276. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  277. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  278. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  279. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  280. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  281. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  282. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  283. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  284. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  285. data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
  286. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +185 -51
  287. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +194 -66
  288. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512_vpopcnt.cpp +51 -14
  289. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  290. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  291. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  292. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  293. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +0 -4
  294. data/vendor/faiss/faiss/utils/simd_levels.cpp +22 -6
  295. data/vendor/faiss/faiss/utils/simd_levels.h +74 -1
  296. data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
  297. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  298. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  299. data/vendor/faiss/faiss/utils/utils.cpp +84 -0
  300. data/vendor/faiss/faiss/utils/utils.h +24 -0
  301. metadata +52 -1
@@ -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>
@@ -70,6 +75,53 @@ struct WrappedSearchResult {
70
75
  }
71
76
  };
72
77
 
78
+ /** Below this many codes, pq_distances_8bit_by_block is slower than the
79
+ * per-code kernel: each sum in memory waits for the store of the previous
80
+ * group of sub-quantizers. */
81
+ constexpr size_t kPQBlockMinCodes = 16;
82
+
83
+ /** For each sub-quantizer, a block of codes reads one 1 KB table slice, which
84
+ * stays in L1; code by code, a table larger than L1 makes every lookup an L1
85
+ * miss. SL as in PQCodeDistanceScalar. */
86
+ template <SIMDLevel SL>
87
+ void pq_distances_8bit_by_block(
88
+ size_t M,
89
+ const float* sim_table,
90
+ float dis0,
91
+ size_t n,
92
+ const uint8_t* codes,
93
+ float* distances) {
94
+ constexpr size_t kBlock = 64;
95
+ constexpr size_t ksub = PQCodeDistanceScalar8<SL>::ksub;
96
+ for (size_t j0 = 0; j0 < n; j0 += kBlock) {
97
+ const size_t nb = std::min(kBlock, n - j0);
98
+ const uint8_t* block = codes + j0 * M;
99
+ float* acc = distances + j0;
100
+ std::fill(acc, acc + nb, 0.0f);
101
+ const float* tab = sim_table;
102
+ size_t m = 0;
103
+ for (; m + 4 <= M; m += 4, tab += 4 * ksub) {
104
+ const uint8_t* c = block + m;
105
+ for (size_t b = 0; b < nb; b++) {
106
+ uint32_t w;
107
+ memcpy(&w, c + b * M, sizeof(w));
108
+ acc[b] += (tab[w & 255] + tab[ksub + ((w >> 8) & 255)]) +
109
+ (tab[2 * ksub + ((w >> 16) & 255)] +
110
+ tab[3 * ksub + (w >> 24)]);
111
+ }
112
+ }
113
+ for (; m < M; m++, tab += ksub) {
114
+ const uint8_t* c = block + m;
115
+ for (size_t b = 0; b < nb; b++) {
116
+ acc[b] += tab[c[b * M]];
117
+ }
118
+ }
119
+ for (size_t b = 0; b < nb; b++) {
120
+ acc[b] += dis0;
121
+ }
122
+ }
123
+ }
124
+
73
125
  /*****************************************************
74
126
  * Scaning the codes.
75
127
  * The scanning functions call their favorite precompute_*
@@ -411,10 +463,8 @@ struct IVFPQScannerT : QueryTables {
411
463
  }
412
464
  }
413
465
 
414
- #pragma omp critical
415
- {
416
- indexIVFPQ_stats.n_hamming_pass += n_hamming_pass;
417
- }
466
+ detail::atomic_fetch_add_relaxed(
467
+ indexIVFPQ_stats.n_hamming_pass, n_hamming_pass);
418
468
  }
419
469
 
420
470
  template <class SearchResultType>
@@ -487,6 +537,29 @@ struct IVFPQScanner : IVFPQScannerT<idx_t, METRIC_TYPE, PQCodeDist>,
487
537
  this->pq.M, this->pq.nbits, this->sim_table, code);
488
538
  }
489
539
 
540
+ void distances_to_codes(size_t n, const uint8_t* codes, float* distances)
541
+ const override {
542
+ // Where gathers are fast (Intel), the block kernel is faster at
543
+ // M <= 64, and the per-code gather kernels are faster at M = 128
544
+ // unless all cores are busy.
545
+ if constexpr (
546
+ has_scalar_8bit_kernel<THE_SIMD_LEVEL>() &&
547
+ std::is_same_v<typename PQCodeDist::PQDecoder, PQDecoder8>) {
548
+ if (precompute_mode == 2 && n >= kPQBlockMinCodes &&
549
+ (SIMDConfig::scalar_gather || this->pq.M <= 64)) {
550
+ pq_distances_8bit_by_block<THE_SIMD_LEVEL>(
551
+ this->pq.M,
552
+ this->sim_table,
553
+ this->dis0,
554
+ n,
555
+ codes,
556
+ distances);
557
+ return;
558
+ }
559
+ }
560
+ InvertedListScanner::distances_to_codes(n, codes, distances);
561
+ }
562
+
490
563
  size_t scan_codes(
491
564
  size_t ncode,
492
565
  const uint8_t* codes,
@@ -560,6 +633,13 @@ InvertedListScanner* make_IVFPQInvertedListScanner<THE_SIMD_LEVEL>(
560
633
 
561
634
  auto with_decoder = [&]<bool use_sel>() -> InvertedListScanner* {
562
635
  if (ivfpq.pq.nbits == 8) {
636
+ if constexpr (has_scalar_8bit_kernel<THE_SIMD_LEVEL>()) {
637
+ if (use_scalar_8bit_kernel<THE_SIMD_LEVEL>(ivfpq.pq.M)) {
638
+ return make.template operator()<
639
+ PQCodeDistanceScalar8Dist<THE_SIMD_LEVEL>,
640
+ use_sel>();
641
+ }
642
+ }
563
643
  return make.template
564
644
  operator()<PQCodeDistance<PQDecoder8, THE_SIMD_LEVEL>, use_sel>();
565
645
  } else if (ivfpq.pq.nbits == 16) {
@@ -67,33 +67,33 @@ void QueryTables::init_query_L2() {
67
67
  }
68
68
 
69
69
  float QueryTables::precompute_list_tables() {
70
- float dis0 = 0;
70
+ float local_dis0 = 0;
71
71
  uint64_t t0;
72
72
  TIC;
73
73
  if (by_residual) {
74
74
  if (metric_type == METRIC_INNER_PRODUCT) {
75
- dis0 = precompute_list_tables_IP();
75
+ local_dis0 = precompute_list_tables_IP();
76
76
  } else {
77
- dis0 = precompute_list_tables_L2();
77
+ local_dis0 = precompute_list_tables_L2();
78
78
  }
79
79
  }
80
80
  init_list_cycles += TOC;
81
- return dis0;
81
+ return local_dis0;
82
82
  }
83
83
 
84
84
  float QueryTables::precompute_list_table_pointers() {
85
- float dis0 = 0;
85
+ float local_dis0 = 0;
86
86
  uint64_t t0;
87
87
  TIC;
88
88
  if (by_residual) {
89
89
  if (metric_type == METRIC_INNER_PRODUCT) {
90
90
  FAISS_THROW_MSG("not implemented");
91
91
  } else {
92
- dis0 = precompute_list_table_pointers_L2();
92
+ local_dis0 = precompute_list_table_pointers_L2();
93
93
  }
94
94
  }
95
95
  init_list_cycles += TOC;
96
- return dis0;
96
+ return local_dis0;
97
97
  }
98
98
 
99
99
  void QueryTables::init_list(idx_t list_no, float coarse_dis_in, int mode) {
@@ -112,7 +112,7 @@ float QueryTables::precompute_list_tables_IP() {
112
112
  // and dis0, the initial value
113
113
  ivfpq.quantizer->reconstruct(key, decoded_vec);
114
114
  // decoded_vec = centroid
115
- float dis0 = fvec_inner_product_dispatch(qi, decoded_vec, d);
115
+ float local_dis0 = fvec_inner_product_dispatch(qi, decoded_vec, d);
116
116
 
117
117
  if (polysemous_ht) {
118
118
  for (int i = 0; i < d; i++) {
@@ -120,11 +120,11 @@ float QueryTables::precompute_list_tables_IP() {
120
120
  }
121
121
  pq.compute_code(residual_vec, q_code.data());
122
122
  }
123
- return dis0;
123
+ return local_dis0;
124
124
  }
125
125
 
126
126
  float QueryTables::precompute_list_tables_L2() {
127
- float dis0 = 0;
127
+ float local_dis0 = 0;
128
128
 
129
129
  if (use_precomputed_table == 0 || use_precomputed_table == -1) {
130
130
  ivfpq.quantizer->compute_residual(qi, residual_vec, key);
@@ -135,7 +135,7 @@ float QueryTables::precompute_list_tables_L2() {
135
135
  }
136
136
 
137
137
  } else if (use_precomputed_table == 1) {
138
- dis0 = coarse_dis;
138
+ local_dis0 = coarse_dis;
139
139
 
140
140
  fvec_madd_dispatch(
141
141
  pq.M * pq.ksub,
@@ -150,7 +150,7 @@ float QueryTables::precompute_list_tables_L2() {
150
150
  }
151
151
 
152
152
  } else if (use_precomputed_table == 2) {
153
- dis0 = coarse_dis;
153
+ local_dis0 = coarse_dis;
154
154
 
155
155
  const MultiIndexQuantizer* miq =
156
156
  dynamic_cast<const MultiIndexQuantizer*>(ivfpq.quantizer);
@@ -188,14 +188,14 @@ float QueryTables::precompute_list_tables_L2() {
188
188
  }
189
189
  }
190
190
 
191
- return dis0;
191
+ return local_dis0;
192
192
  }
193
193
 
194
194
  float QueryTables::precompute_list_table_pointers_L2() {
195
- float dis0 = 0;
195
+ float local_dis0 = 0;
196
196
 
197
197
  if (use_precomputed_table == 1) {
198
- dis0 = coarse_dis;
198
+ local_dis0 = coarse_dis;
199
199
 
200
200
  const float* s = ivfpq.precomputed_table.data() + key * pq.ksub * pq.M;
201
201
  for (size_t m = 0; m < pq.M; m++) {
@@ -203,7 +203,7 @@ float QueryTables::precompute_list_table_pointers_L2() {
203
203
  s += pq.ksub;
204
204
  }
205
205
  } else if (use_precomputed_table == 2) {
206
- dis0 = coarse_dis;
206
+ local_dis0 = coarse_dis;
207
207
 
208
208
  const MultiIndexQuantizer* miq =
209
209
  dynamic_cast<const MultiIndexQuantizer*>(ivfpq.quantizer);
@@ -237,7 +237,7 @@ float QueryTables::precompute_list_table_pointers_L2() {
237
237
  // avoid with the tables pointers.
238
238
  }
239
239
 
240
- return dis0;
240
+ return local_dis0;
241
241
  }
242
242
 
243
243
  } // namespace pq_code_distance
@@ -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 <>
@@ -40,11 +40,16 @@ void pq_scan_8bit(
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
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
  }
@@ -61,6 +66,20 @@ void pq_code_distance_8bit_four(
61
66
  float& result2,
62
67
  float& result3) {
63
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
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ /* 8-bit product-quantizer code distance for RISC-V Vector.
11
+ *
12
+ * Free-standing (no exceptions, heap or threads), so a freestanding RISC-V
13
+ * target can compile it. pq_code_distance/rvv.cpp calls it; no Buck target
14
+ * here builds that RISC-V CPU path.
15
+ *
16
+ * PQDecoder8 fixes nbits = 8, so ksub = 256 and the scalar loop is
17
+ *
18
+ * for (m = 0; m < M; m++) result += sim_table[m * 256 + code[m]];
19
+ *
20
+ * That is a gather, not a contiguous read. RVV issues one vluxei32 per strip:
21
+ * build the element offset m * 256 + code[m] across the lanes, scale it to
22
+ * bytes, then index sim_table.
23
+ *
24
+ * The offset stays inside uint32 while M * 256 * 4 bytes does. At M = 1024
25
+ * that is 1 MiB, so no PQ shape in Faiss overflows it.
26
+ *
27
+ * The accumulator is LMUL 4, as in utils/simd_impl/distances_rvv_kernel.h.
28
+ * The lanes add the M terms in a different order than the scalar loop, so
29
+ * the last bits can differ.
30
+ */
31
+
32
+ #include <cstddef>
33
+ #include <cstdint>
34
+
35
+ #include <riscv_vector.h>
36
+
37
+ namespace faiss {
38
+ namespace pq_rvv_kernel {
39
+
40
+ /// Element offsets into sim_table for subquantizers [m, m + vl).
41
+ inline vuint32m4_t row_offsets(size_t m, size_t vl) {
42
+ // (m + lane) * 256
43
+ vuint32m4_t row = __riscv_vadd_vx_u32m4(__riscv_vid_v_u32m4(vl), m, vl);
44
+ return __riscv_vsll_vx_u32m4(row, 8, vl);
45
+ }
46
+
47
+ /// Byte offsets for one code, given the precomputed row offsets.
48
+ inline vuint32m4_t byte_offsets(
49
+ vuint32m4_t row_off,
50
+ const uint8_t* code,
51
+ size_t m,
52
+ size_t vl) {
53
+ vuint8m1_t cb = __riscv_vle8_v_u8m1(code + m, vl);
54
+ vuint32m4_t idx = __riscv_vzext_vf4_u32m4(cb, vl);
55
+ // (row + code) * sizeof(float)
56
+ return __riscv_vsll_vx_u32m4(
57
+ __riscv_vadd_vv_u32m4(row_off, idx, vl), 2, vl);
58
+ }
59
+
60
+ inline float reduce(vfloat32m4_t acc, size_t vlmax) {
61
+ vfloat32m1_t zero = __riscv_vfmv_v_f_f32m1(0.0f, 1);
62
+ vfloat32m1_t sum = __riscv_vfredusum_vs_f32m4_f32m1(acc, zero, vlmax);
63
+ return __riscv_vfmv_f_s_f32m1_f32(sum);
64
+ }
65
+
66
+ /// Distance between a query and one 8-bit PQ code.
67
+ /// @param sim_table M * 256 floats, laid out (M, ksub)
68
+ inline float distance_8bit(
69
+ size_t M,
70
+ const float* sim_table,
71
+ const uint8_t* code) {
72
+ const size_t vlmax = __riscv_vsetvlmax_e32m4();
73
+ vfloat32m4_t acc = __riscv_vfmv_v_f_f32m4(0.0f, vlmax);
74
+
75
+ size_t m = 0;
76
+ while (m < M) {
77
+ const size_t vl = __riscv_vsetvl_e32m4(M - m);
78
+ vuint32m4_t off = byte_offsets(row_offsets(m, vl), code, m, vl);
79
+ vfloat32m4_t v = __riscv_vluxei32_v_f32m4(sim_table, off, vl);
80
+ // Tail-undisturbed: lanes past vl keep earlier partial sums, and the
81
+ // final reduction covers vlmax.
82
+ acc = __riscv_vfadd_vv_f32m4_tu(acc, acc, v, vl);
83
+ m += vl;
84
+ }
85
+ return reduce(acc, vlmax);
86
+ }
87
+
88
+ } // namespace pq_rvv_kernel
89
+ } // namespace faiss