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
@@ -1,5 +1,13 @@
1
1
  /*
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ * Portions 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
+ /*
9
+ * Portions Copyright 2026 Arm Limited and/or its affiliates
10
+ * <open-source-office@arm.com>
3
11
  *
4
12
  * This source code is licensed under the MIT license found in the
5
13
  * LICENSE file in the root directory of this source tree.
@@ -13,6 +21,7 @@
13
21
  #include <cstddef>
14
22
  #include <cstdio>
15
23
  #include <cstring>
24
+ #include <type_traits>
16
25
  #include <vector>
17
26
 
18
27
  #include <omp.h>
@@ -26,6 +35,7 @@
26
35
  #include <faiss/utils/distances_dispatch.h>
27
36
  #include <faiss/utils/distances_fused/distances_fused.h>
28
37
  #include <faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h>
38
+ #include <faiss/utils/utils.h>
29
39
 
30
40
  #ifndef FINTEGER
31
41
  #define FINTEGER long
@@ -361,10 +371,11 @@ void exhaustive_L2sqr_seq(
361
371
  }
362
372
  }
363
373
 
364
- /** Find the nearest neighbors for nx queries in a set of ny vectors */
365
- template <class BlockResultHandler>
366
- void exhaustive_inner_product_blas(
367
- const float* x,
374
+ /** Find the nearest neighbors for nx queries (see Fp32QueryTiles) in a set
375
+ * of ny vectors */
376
+ template <class BlockResultHandler, class QueryTiles>
377
+ void exhaustive_inner_product_blas_tiles(
378
+ QueryTiles& queries,
368
379
  const float* y,
369
380
  size_t d,
370
381
  size_t nx,
@@ -378,7 +389,8 @@ void exhaustive_inner_product_blas(
378
389
  /* block sizes */
379
390
  const size_t bs_x = distance_compute_blas_query_bs;
380
391
  const size_t bs_y = distance_compute_blas_database_bs;
381
- std::unique_ptr<float[]> ip_block(new float[bs_x * bs_y]);
392
+ std::unique_ptr<float[]> ip_block(
393
+ new float[std::min(bs_x, nx) * std::min(bs_y, ny)]);
382
394
 
383
395
  for (size_t i0 = 0; i0 < nx; i0 += bs_x) {
384
396
  size_t i1 = i0 + bs_x;
@@ -386,6 +398,8 @@ void exhaustive_inner_product_blas(
386
398
  i1 = nx;
387
399
  }
388
400
 
401
+ const float* xt = queries.tile(i0, i1, nullptr);
402
+
389
403
  res.begin_multiple(i0, i1);
390
404
 
391
405
  for (size_t j0 = 0; j0 < ny; j0 += bs_y) {
@@ -405,7 +419,7 @@ void exhaustive_inner_product_blas(
405
419
  &one,
406
420
  y + j0 * d,
407
421
  &di,
408
- x + i0 * d,
422
+ xt,
409
423
  &di,
410
424
  &zero,
411
425
  ip_block.get(),
@@ -421,29 +435,32 @@ void exhaustive_inner_product_blas(
421
435
 
422
436
  // distance correction is an operator that can be applied to transform
423
437
  // the distances
424
- template <class BlockResultHandler>
425
- void exhaustive_L2sqr_blas_default_impl(
426
- const float* x,
438
+ template <class BlockResultHandler, class QueryTiles>
439
+ void exhaustive_L2sqr_blas_tiles(
440
+ QueryTiles& queries,
427
441
  const float* y,
428
442
  size_t d,
429
443
  size_t nx,
430
444
  size_t ny,
431
445
  BlockResultHandler& res,
432
- const float* y_norms = nullptr) {
446
+ const float* y_norms = nullptr,
447
+ size_t bs_x = distance_compute_blas_query_bs,
448
+ size_t bs_y = distance_compute_blas_database_bs) {
433
449
  // BLAS does not like empty matrices
434
450
  if (nx == 0 || ny == 0) {
435
451
  return;
436
452
  }
437
453
 
438
- /* block sizes */
439
- const size_t bs_x = distance_compute_blas_query_bs;
440
- const size_t bs_y = distance_compute_blas_database_bs;
454
+ [[maybe_unused]] constexpr bool parallel_distances = std::is_same_v<
455
+ BlockResultHandler,
456
+ Top2BlockResultHandler<CMax<float, int64_t>>>;
441
457
  // const size_t bs_x = 16, bs_y = 16;
442
- std::unique_ptr<float[]> ip_block(new float[bs_x * bs_y]);
458
+ std::unique_ptr<float[]> ip_block(
459
+ new float[std::min(bs_x, nx) * std::min(bs_y, ny)]);
443
460
  std::unique_ptr<float[]> x_norms(new float[nx]);
444
461
  std::unique_ptr<float[]> del2;
445
462
 
446
- fvec_norms_L2sqr(x_norms.get(), x, d, nx);
463
+ queries.prepare_norms(nx, x_norms.get());
447
464
 
448
465
  if (!y_norms) {
449
466
  float* y_norms2 = new float[ny];
@@ -458,6 +475,8 @@ void exhaustive_L2sqr_blas_default_impl(
458
475
  i1 = nx;
459
476
  }
460
477
 
478
+ const float* xt = queries.tile(i0, i1, x_norms.get());
479
+
461
480
  res.begin_multiple(i0, i1);
462
481
 
463
482
  for (size_t j0 = 0; j0 < ny; j0 += bs_y) {
@@ -477,13 +496,15 @@ void exhaustive_L2sqr_blas_default_impl(
477
496
  &one,
478
497
  y + j0 * d,
479
498
  &di,
480
- x + i0 * d,
499
+ xt,
481
500
  &di,
482
501
  &zero,
483
502
  ip_block.get(),
484
503
  &nyi);
485
504
  }
486
- for (size_t i = i0; i < i1; i++) {
505
+ // The top-2 handler is too cheap to hide this pass: share it.
506
+ #pragma omp parallel for if (parallel_distances && i1 - i0 >= 16)
507
+ for (int64_t i = i0; i < static_cast<int64_t>(i1); i++) {
487
508
  float* ip_line = ip_block.get() + (i - i0) * (j1 - j0);
488
509
 
489
510
  for (size_t j = j0; j < j1; j++) {
@@ -510,6 +531,34 @@ void exhaustive_L2sqr_blas_default_impl(
510
531
  }
511
532
  }
512
533
 
534
+ template <class BlockResultHandler>
535
+ void exhaustive_inner_product_blas(
536
+ const float* x,
537
+ const float* y,
538
+ size_t d,
539
+ size_t nx,
540
+ size_t ny,
541
+ BlockResultHandler& res) {
542
+ Fp32QueryTiles queries{x, d};
543
+ exhaustive_inner_product_blas_tiles(queries, y, d, nx, ny, res);
544
+ }
545
+
546
+ template <class BlockResultHandler>
547
+ void exhaustive_L2sqr_blas_default_impl(
548
+ const float* x,
549
+ const float* y,
550
+ size_t d,
551
+ size_t nx,
552
+ size_t ny,
553
+ BlockResultHandler& res,
554
+ const float* y_norms = nullptr,
555
+ size_t bs_x = distance_compute_blas_query_bs,
556
+ size_t bs_y = distance_compute_blas_database_bs) {
557
+ Fp32QueryTiles queries{x, d};
558
+ exhaustive_L2sqr_blas_tiles(
559
+ queries, y, d, nx, ny, res, y_norms, bs_x, bs_y);
560
+ }
561
+
513
562
  template <class BlockResultHandler>
514
563
  void exhaustive_L2sqr_blas(
515
564
  const float* x,
@@ -518,8 +567,11 @@ void exhaustive_L2sqr_blas(
518
567
  size_t nx,
519
568
  size_t ny,
520
569
  BlockResultHandler& res,
521
- const float* y_norms = nullptr) {
522
- exhaustive_L2sqr_blas_default_impl(x, y, d, nx, ny, res, y_norms);
570
+ const float* y_norms = nullptr,
571
+ size_t bs_x = distance_compute_blas_query_bs,
572
+ size_t bs_y = distance_compute_blas_database_bs) {
573
+ exhaustive_L2sqr_blas_default_impl(
574
+ x, y, d, nx, ny, res, y_norms, bs_x, bs_y);
523
575
  }
524
576
 
525
577
  } // anonymous namespace
@@ -535,7 +587,9 @@ void exhaustive_L2sqr_blas<Top1BlockResultHandler<CMax<float, int64_t>>>(
535
587
  size_t nx,
536
588
  size_t ny,
537
589
  Top1BlockResultHandler<CMax<float, int64_t>>& res,
538
- const float* y_norms) {
590
+ const float* y_norms,
591
+ size_t bs_x,
592
+ size_t bs_y) {
539
593
  // use a faster fused kernel if available
540
594
  if (exhaustive_L2sqr_fused_cmax(x, y, d, nx, ny, res, y_norms)) {
541
595
  return;
@@ -547,15 +601,68 @@ void exhaustive_L2sqr_blas<Top1BlockResultHandler<CMax<float, int64_t>>>(
547
601
  SL == SIMDLevel::AVX2 || SL == SIMDLevel::AVX512 ||
548
602
  SL == SIMDLevel::ARM_SVE) {
549
603
  exhaustive_L2sqr_blas_cmax<SL>(
550
- x, y, d, nx, ny, res, y_norms);
604
+ x, y, d, nx, ny, res, y_norms, bs_x, bs_y);
551
605
  } else {
552
606
  exhaustive_L2sqr_blas_default_impl<
553
607
  Top1BlockResultHandler<CMax<float, int64_t>>>(
608
+ x, y, d, nx, ny, res, y_norms, bs_x, bs_y);
609
+ }
610
+ });
611
+ }
612
+
613
+ /// exhaustive_L2sqr_blas for packed IEEE binary16 queries
614
+ template <class BlockResultHandler>
615
+ void exhaustive_L2sqr_blas_fp16(
616
+ const uint16_t* x,
617
+ const float* y,
618
+ size_t d,
619
+ size_t nx,
620
+ size_t ny,
621
+ BlockResultHandler& res,
622
+ const float* y_norms) {
623
+ Fp16QueryTiles queries(x, d, nx);
624
+ exhaustive_L2sqr_blas_tiles(queries, y, d, nx, ny, res, y_norms);
625
+ }
626
+
627
+ template <>
628
+ void exhaustive_L2sqr_blas_fp16<Top1BlockResultHandler<CMax<float, int64_t>>>(
629
+ const uint16_t* x,
630
+ const float* y,
631
+ size_t d,
632
+ size_t nx,
633
+ size_t ny,
634
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
635
+ const float* y_norms) {
636
+ with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_WITH_SVE>(
637
+ [&]<SIMDLevel SL>() {
638
+ if constexpr (
639
+ SL == SIMDLevel::AVX2 || SL == SIMDLevel::AVX512 ||
640
+ SL == SIMDLevel::ARM_SVE) {
641
+ exhaustive_L2sqr_blas_cmax_fp16<SL>(
554
642
  x, y, d, nx, ny, res, y_norms);
643
+ } else {
644
+ Fp16QueryTiles queries(x, d, nx);
645
+ exhaustive_L2sqr_blas_tiles(
646
+ queries, y, d, nx, ny, res, y_norms);
555
647
  }
556
648
  });
557
649
  }
558
650
 
651
+ // BLAS is faster for the small query slices that each OpenMP worker of
652
+ // IndexIVF::search passes to the quantizer, when the total work is large.
653
+ // Measured on Neoverse-V2 with ArmPL and on Neoverse-V3 with OpenBLAS,
654
+ // both on Linux. The rule applies to all GCC and Clang aarch64 builds.
655
+ bool should_use_blas(size_t nx, [[maybe_unused]] size_t ny, size_t d) {
656
+ #ifdef __aarch64__
657
+ if (omp_in_parallel() && d >= 64 && nx >= 4 &&
658
+ nx * d * ny >=
659
+ static_cast<size_t>(distance_compute_blas_threshold) * 32) {
660
+ return true;
661
+ }
662
+ #endif
663
+ return nx * d >= static_cast<size_t>(distance_compute_blas_threshold);
664
+ }
665
+
559
666
  struct Run_search_inner_product {
560
667
  using T = void;
561
668
  template <class BlockResultHandler>
@@ -569,8 +676,7 @@ struct Run_search_inner_product {
569
676
  // per-query begin()/end() still runs and each handler writes its own
570
677
  // neutral distance and -1 label. The BLAS path instead returns early
571
678
  // on ny == 0, before the handler is initialized.
572
- if (res.sel || ny == 0 ||
573
- nx * d < static_cast<size_t>(distance_compute_blas_threshold)) {
679
+ if (res.sel || ny == 0 || !should_use_blas(nx, ny, d)) {
574
680
  exhaustive_inner_product_seq(x, y, d, nx, ny, res);
575
681
  } else {
576
682
  exhaustive_inner_product_blas(x, y, d, nx, ny, res);
@@ -589,8 +695,7 @@ struct Run_search_L2sqr {
589
695
  size_t ny,
590
696
  const float* y_norm2) {
591
697
  // See the note on ny == 0 in Run_search_inner_product.
592
- if (res.sel || ny == 0 ||
593
- nx * d < static_cast<size_t>(distance_compute_blas_threshold)) {
698
+ if (res.sel || ny == 0 || !should_use_blas(nx, ny, d)) {
594
699
  exhaustive_L2sqr_seq(x, y, d, nx, ny, res);
595
700
  } else {
596
701
  exhaustive_L2sqr_blas(x, y, d, nx, ny, res, y_norm2);
@@ -598,6 +703,58 @@ struct Run_search_L2sqr {
598
703
  }
599
704
  };
600
705
 
706
+ // Supplied score norms are authoritative: use norm expansion even for a
707
+ // small query. Keep the same BLAS/fused/SIMD result handlers as knn_L2sqr.
708
+ struct Run_search_L2sqr_with_norms {
709
+ using T = void;
710
+ template <class BlockResultHandler>
711
+ void f(BlockResultHandler& res,
712
+ const float* x,
713
+ const float* y,
714
+ size_t d,
715
+ size_t nx,
716
+ size_t ny,
717
+ const float* y_norm2,
718
+ size_t bs_x,
719
+ size_t bs_y) {
720
+ // Initialize each handler's neutral distance/-1 outputs for ny == 0.
721
+ if (ny == 0) {
722
+ exhaustive_L2sqr_seq(x, y, d, nx, ny, res);
723
+ } else {
724
+ exhaustive_L2sqr_blas(x, y, d, nx, ny, res, y_norm2, bs_x, bs_y);
725
+ }
726
+ }
727
+ };
728
+
729
+ // fp16 consumers only run the BLAS kernels, see use_fp16_blas_kernels
730
+ struct Run_search_inner_product_fp16 {
731
+ using T = void;
732
+ template <class BlockResultHandler>
733
+ void f(BlockResultHandler& res,
734
+ const uint16_t* x,
735
+ const float* y,
736
+ size_t d,
737
+ size_t nx,
738
+ size_t ny) {
739
+ Fp16QueryTiles queries(x, d, nx);
740
+ exhaustive_inner_product_blas_tiles(queries, y, d, nx, ny, res);
741
+ }
742
+ };
743
+
744
+ struct Run_search_L2sqr_fp16 {
745
+ using T = void;
746
+ template <class BlockResultHandler>
747
+ void f(BlockResultHandler& res,
748
+ const uint16_t* x,
749
+ const float* y,
750
+ size_t d,
751
+ size_t nx,
752
+ size_t ny,
753
+ const float* y_norm2) {
754
+ exhaustive_L2sqr_blas_fp16(x, y, d, nx, ny, res, y_norm2);
755
+ }
756
+ };
757
+
601
758
  } // anonymous namespace
602
759
 
603
760
  /*******************************************************
@@ -610,8 +767,23 @@ int distance_compute_blas_database_bs = 1024;
610
767
  int distance_compute_min_k_reservoir = 100;
611
768
 
612
769
  // Database-parallel KNN: parallelizes over database segments instead of
613
- // queries, for the case where nx < nthreads and the database is large.
770
+ // queries, for a large database and as many queries as its buffers allow.
614
771
  static constexpr size_t kDbParallelMinVectors = 10000;
772
+ // Bound on its per-thread result heaps and inner-product blocks when
773
+ // nx >= nthreads.
774
+ static constexpr size_t kDbParallelMaxBytes = size_t(256) << 20;
775
+ // Inner-product block of one thread, in floats.
776
+ static constexpr size_t kDbParallelBlockFloats = size_t(1) << 16;
777
+
778
+ // Database vectors per inner-product block: fewer for more queries, so that
779
+ // the block stays near kDbParallelBlockFloats.
780
+ static size_t db_parallel_block(
781
+ size_t nx,
782
+ size_t database_block_size = distance_compute_blas_database_bs) {
783
+ return std::min<size_t>(
784
+ database_block_size,
785
+ std::max<size_t>(32, kDbParallelBlockFloats / nx));
786
+ }
615
787
 
616
788
  template <class C>
617
789
  static void knn_db_parallel_impl(
@@ -623,14 +795,15 @@ static void knn_db_parallel_impl(
623
795
  size_t k,
624
796
  float* vals,
625
797
  int64_t* ids,
626
- const float* y_norms) {
798
+ const float* y_norms,
799
+ size_t database_block_size = distance_compute_blas_database_bs) {
627
800
  using T = typename C::T;
628
801
  using TI = typename C::TI;
629
802
 
630
803
  int nt = omp_get_max_threads();
631
- const size_t bs_y = distance_compute_blas_database_bs;
804
+ const size_t bs_y = db_parallel_block(nx, database_block_size);
632
805
 
633
- // Per-thread result heaps: nt threads x nx queries x k results
806
+ // Result heaps: nt segments x nx queries x k results
634
807
  std::vector<T> all_dis(static_cast<size_t>(nt) * nx * k);
635
808
  std::vector<TI> all_ids(static_cast<size_t>(nt) * nx * k);
636
809
 
@@ -649,9 +822,8 @@ static void knn_db_parallel_impl(
649
822
  }
650
823
  }
651
824
 
652
- #pragma omp parallel num_threads(nt)
653
- {
654
- int tid = omp_get_thread_num();
825
+ #pragma omp parallel for
826
+ for (int tid = 0; tid < nt; tid++) {
655
827
  size_t j_begin = static_cast<size_t>(tid) * ny / nt;
656
828
  size_t j_end = static_cast<size_t>(tid + 1) * ny / nt;
657
829
  size_t local_ny = j_end - j_begin;
@@ -734,7 +906,7 @@ static void knn_db_parallel_impl(
734
906
  }
735
907
  }
736
908
 
737
- // Merge per-thread heaps into output, parallelized over queries
909
+ // Merge per-segment heaps into output, parallelized over queries
738
910
  #pragma omp parallel for
739
911
  for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
740
912
  heap_heapify<C>(k, vals + i * k, ids + i * k);
@@ -760,16 +932,28 @@ static void knn_db_parallel_impl(
760
932
  static bool should_use_db_parallel(
761
933
  size_t nx,
762
934
  size_t ny,
763
- const IDSelector* sel) {
935
+ size_t k,
936
+ const IDSelector* sel,
937
+ size_t database_block_size = distance_compute_blas_database_bs) {
764
938
  if (sel) {
765
939
  return false;
766
940
  }
767
- int nt = omp_get_max_threads();
768
- size_t min_ny = std::max(
769
- kDbParallelMinVectors,
770
- static_cast<size_t>(nt) *
771
- static_cast<size_t>(distance_compute_blas_database_bs));
772
- return nt > 1 && nx < static_cast<size_t>(nt) && ny >= min_ny;
941
+ const size_t nt = omp_get_max_threads();
942
+ const size_t min_ny =
943
+ std::max(kDbParallelMinVectors, nt * database_block_size);
944
+ if (nt <= 1 || ny < min_ny) {
945
+ return false;
946
+ }
947
+ if (nx < nt) {
948
+ return true;
949
+ }
950
+ // Otherwise the sequential scan or BLAS runs. The sequential scan reads
951
+ // the whole database once per query per thread, and BLAS synchronizes
952
+ // all threads twice per database block.
953
+ const size_t bytes = nt *
954
+ (nx * k * (sizeof(float) + sizeof(int64_t)) +
955
+ nx * db_parallel_block(nx, database_block_size) * sizeof(float));
956
+ return bytes <= kDbParallelMaxBytes;
773
957
  }
774
958
 
775
959
  void knn_inner_product(
@@ -796,7 +980,7 @@ void knn_inner_product(
796
980
  return;
797
981
  }
798
982
 
799
- if (should_use_db_parallel(nx, ny, sel)) {
983
+ if (should_use_db_parallel(nx, ny, k, sel)) {
800
984
  knn_db_parallel_impl<CMin<float, int64_t>>(
801
985
  x, y, d, nx, ny, k, vals, ids, nullptr);
802
986
  } else {
@@ -862,7 +1046,7 @@ void knn_L2sqr(
862
1046
  return;
863
1047
  }
864
1048
 
865
- if (should_use_db_parallel(nx, ny, sel)) {
1049
+ if (should_use_db_parallel(nx, ny, k, sel)) {
866
1050
  knn_db_parallel_impl<CMax<float, int64_t>>(
867
1051
  x, y, d, nx, ny, k, vals, ids, y_norm2);
868
1052
  } else {
@@ -894,6 +1078,245 @@ void knn_L2sqr(
894
1078
  knn_L2sqr(x, y, d, nx, ny, res->k, res->val, res->ids, y_norm2, sel);
895
1079
  }
896
1080
 
1081
+ void knn_L2sqr_with_norms(
1082
+ const float* x,
1083
+ const float* y,
1084
+ size_t d,
1085
+ size_t nx,
1086
+ size_t ny,
1087
+ size_t k,
1088
+ float* vals,
1089
+ int64_t* ids,
1090
+ const float* y_norm2,
1091
+ size_t query_block_size,
1092
+ size_t database_block_size) {
1093
+ FAISS_THROW_IF_NOT(query_block_size > 0 && database_block_size > 0);
1094
+ FAISS_THROW_IF_NOT(ny == 0 || y_norm2 != nullptr);
1095
+ if (nx == 0) {
1096
+ return;
1097
+ }
1098
+ if (should_use_db_parallel(nx, ny, k, nullptr, database_block_size)) {
1099
+ knn_db_parallel_impl<CMax<float, int64_t>>(
1100
+ x, y, d, nx, ny, k, vals, ids, y_norm2, database_block_size);
1101
+ } else {
1102
+ Run_search_L2sqr_with_norms r;
1103
+ dispatch_knn_ResultHandler(
1104
+ nx,
1105
+ vals,
1106
+ ids,
1107
+ k,
1108
+ METRIC_L2,
1109
+ nullptr,
1110
+ r,
1111
+ x,
1112
+ y,
1113
+ d,
1114
+ nx,
1115
+ ny,
1116
+ y_norm2,
1117
+ query_block_size,
1118
+ database_block_size);
1119
+ }
1120
+ }
1121
+
1122
+ void accumulate_cluster_sums(
1123
+ size_t d,
1124
+ size_t n,
1125
+ size_t k,
1126
+ const float* x,
1127
+ const int64_t* labels,
1128
+ float* sums) {
1129
+ // Each thread owns a range of labels, so no two threads write one row.
1130
+ #pragma omp parallel if (n * d > 65536)
1131
+ {
1132
+ const int64_t nt = omp_get_num_threads(), rank = omp_get_thread_num();
1133
+ const int64_t a0 = k * rank / nt, a1 = k * (rank + 1) / nt;
1134
+ for (size_t i = 0; i < n; i++) {
1135
+ const int64_t a = labels[i];
1136
+ if (a >= a0 && a < a1) {
1137
+ float* row = sums + a * (d + 1);
1138
+ fvec_add(d, row, x + i * d, row);
1139
+ row[d] += 1;
1140
+ }
1141
+ }
1142
+ }
1143
+ }
1144
+
1145
+ void accumulate_removal_stats(
1146
+ size_t d,
1147
+ size_t n,
1148
+ size_t k,
1149
+ const float* x,
1150
+ const int64_t* labels,
1151
+ const float* d1,
1152
+ const float* d2,
1153
+ const float* centroids,
1154
+ const float* axes,
1155
+ float* stats) {
1156
+ // Each thread owns a range of labels, so no two threads write one row.
1157
+ #pragma omp parallel if (n * d > 65536)
1158
+ {
1159
+ const int64_t nt = omp_get_num_threads(), rank = omp_get_thread_num();
1160
+ const int64_t a0 = k * rank / nt, a1 = k * (rank + 1) / nt;
1161
+ std::vector<float> y(d);
1162
+ for (size_t i = 0; i < n; i++) {
1163
+ const int64_t a = labels[i];
1164
+ if (a < a0 || a >= a1) {
1165
+ continue;
1166
+ }
1167
+ fvec_sub(d, x + i * d, centroids + a * d, y.data());
1168
+ const float p = fvec_inner_product(y.data(), axes + a * d, d);
1169
+ float* row = stats + a * (d + 1);
1170
+ fvec_madd(d, row, p, y.data(), row);
1171
+ const float margin = d2[i] - d1[i];
1172
+ row[d] += std::isfinite(margin) ? margin : 0;
1173
+ }
1174
+ }
1175
+ }
1176
+
1177
+ void update_removal_axes(
1178
+ size_t d,
1179
+ size_t n,
1180
+ const int64_t* ids,
1181
+ float weight,
1182
+ float* batch,
1183
+ float* axis,
1184
+ float* axes) {
1185
+ #pragma omp parallel for if (n * d > 65536)
1186
+ for (int64_t i = 0; i < static_cast<int64_t>(n); i++) {
1187
+ const int64_t a = ids[i];
1188
+ float* b = batch + a * (d + 1);
1189
+ float* u = axis + a * (d + 1);
1190
+ fvec_madd(d, u, weight, b, u);
1191
+ double norm2 = 0;
1192
+ for (size_t j = 0; j < d; j++) {
1193
+ norm2 += double(u[j]) * u[j];
1194
+ }
1195
+ const double norm = std::sqrt(norm2);
1196
+ if (norm > 0) {
1197
+ for (size_t j = 0; j < d; j++) {
1198
+ axes[a * d + j] = u[j] / norm;
1199
+ }
1200
+ }
1201
+ std::fill(b, b + d + 1, 0.0f);
1202
+ }
1203
+ }
1204
+
1205
+ namespace {
1206
+
1207
+ /** Whether fp16 queries go to the BLAS kernels directly.
1208
+ *
1209
+ * Other cases widen chunks of queries and run the fp32 search: selectors,
1210
+ * sequential and database-parallel searches, and small d, where the fused
1211
+ * top-1 kernels need fp32 rows.
1212
+ */
1213
+ bool use_fp16_blas_kernels(
1214
+ size_t d,
1215
+ size_t nx,
1216
+ size_t ny,
1217
+ size_t k,
1218
+ const IDSelector* sel) {
1219
+ return !sel && ny > 0 && d > 32 &&
1220
+ nx * d >= static_cast<size_t>(distance_compute_blas_threshold) &&
1221
+ !should_use_db_parallel(nx, ny, k, sel);
1222
+ }
1223
+
1224
+ /// widens chunks of queries and calls search(x_chunk, n_chunk, i0) on them
1225
+ template <class Fp32Search>
1226
+ void search_fp16_by_chunks(
1227
+ const uint16_t* x,
1228
+ size_t d,
1229
+ size_t nx,
1230
+ const Fp32Search& search) {
1231
+ // a multiple of the query tile, so the fp32 kernels see the same tiles
1232
+ const size_t bs_x = std::max(distance_compute_blas_query_bs, 1);
1233
+ const size_t chunk = bs_x *
1234
+ std::max<size_t>(1, (size_t(8) << 20) / (bs_x * d * sizeof(float)));
1235
+ std::vector<float> buffer(std::min(nx, chunk) * d);
1236
+ for (size_t i0 = 0; i0 < nx; i0 += chunk) {
1237
+ const size_t i1 = std::min(nx, i0 + chunk);
1238
+ fp16_to_fp32((i1 - i0) * d, x + i0 * d, buffer.data());
1239
+ search(buffer.data(), i1 - i0, i0);
1240
+ }
1241
+ }
1242
+
1243
+ } // namespace
1244
+
1245
+ void knn_inner_product_fp16(
1246
+ const uint16_t* x,
1247
+ const float* y,
1248
+ size_t d,
1249
+ size_t nx,
1250
+ size_t ny,
1251
+ size_t k,
1252
+ float* vals,
1253
+ int64_t* ids,
1254
+ const IDSelector* sel) {
1255
+ if (!use_fp16_blas_kernels(d, nx, ny, k, sel)) {
1256
+ search_fp16_by_chunks(
1257
+ x, d, nx, [&](const float* xc, size_t nc, size_t i0) {
1258
+ knn_inner_product(
1259
+ xc,
1260
+ y,
1261
+ d,
1262
+ nc,
1263
+ ny,
1264
+ k,
1265
+ vals + i0 * k,
1266
+ ids + i0 * k,
1267
+ sel);
1268
+ });
1269
+ return;
1270
+ }
1271
+ Run_search_inner_product_fp16 r;
1272
+ dispatch_knn_ResultHandler(
1273
+ nx,
1274
+ vals,
1275
+ ids,
1276
+ k,
1277
+ METRIC_INNER_PRODUCT,
1278
+ nullptr,
1279
+ r,
1280
+ x,
1281
+ y,
1282
+ d,
1283
+ nx,
1284
+ ny);
1285
+ }
1286
+
1287
+ void knn_L2sqr_fp16(
1288
+ const uint16_t* x,
1289
+ const float* y,
1290
+ size_t d,
1291
+ size_t nx,
1292
+ size_t ny,
1293
+ size_t k,
1294
+ float* vals,
1295
+ int64_t* ids,
1296
+ const float* y_norm2,
1297
+ const IDSelector* sel) {
1298
+ if (!use_fp16_blas_kernels(d, nx, ny, k, sel)) {
1299
+ search_fp16_by_chunks(
1300
+ x, d, nx, [&](const float* xc, size_t nc, size_t i0) {
1301
+ knn_L2sqr(
1302
+ xc,
1303
+ y,
1304
+ d,
1305
+ nc,
1306
+ ny,
1307
+ k,
1308
+ vals + i0 * k,
1309
+ ids + i0 * k,
1310
+ y_norm2,
1311
+ sel);
1312
+ });
1313
+ return;
1314
+ }
1315
+ Run_search_L2sqr_fp16 r;
1316
+ dispatch_knn_ResultHandler(
1317
+ nx, vals, ids, k, METRIC_L2, nullptr, r, x, y, d, nx, ny, y_norm2);
1318
+ }
1319
+
897
1320
  /***************************************************************************
898
1321
  * Range search
899
1322
  ***************************************************************************/