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.
@@ -369,7 +377,9 @@ void pairwise_indexed_inner_product(
369
377
  * KNN functions
370
378
  ***************************************************************************/
371
379
 
372
- // threshold on nx * d above which we switch to BLAS to compute distances
380
+ // threshold on nx * d above which we switch to BLAS to compute distances.
381
+ // On Arm, inside an OpenMP parallel region, BLAS also wins when d >= 64,
382
+ // nx >= 4, and nx * d * ny >= 32 * distance_compute_blas_threshold.
373
383
  FAISS_API extern int distance_compute_blas_threshold;
374
384
 
375
385
  // block sizes for BLAS distance computations
@@ -455,6 +465,67 @@ void knn_L2sqr(
455
465
  const float* y_norm2 = nullptr,
456
466
  const IDSelector* sel = nullptr);
457
467
 
468
+ /** Search using authoritative FP32 database score norms and per-call blocks.
469
+ *
470
+ * Scores use the existing norm-expansion kernels: ||x||^2 + y_norm2[j]
471
+ * - 2 * x.y[j], with the existing nonnegative clamp. y_norm2 may include a
472
+ * bias/offset or +infinity mask; returned scores need not be plain L2
473
+ * distances. The caller is responsible for their interpretation.
474
+ *
475
+ * Uses the same top-k handlers, fused/SIMD BLAS and database-parallel paths
476
+ * as knn_L2sqr with forced norm expansion, without reading/writing the BLAS
477
+ * threshold or block globals. Result-handler selection (including
478
+ * distance_compute_min_k_reservoir) and the OpenMP thread count follow the
479
+ * normal knn_L2sqr policies. Blocks must be positive; y_norm2 must contain
480
+ * ny entries for a nonempty database. No selector is supported.
481
+ *
482
+ * Outputs have nx * k entries, sorted by score. With ny == 0, each entry
483
+ * has the existing neutral distance and -1 ID; nx == 0 writes no outputs.
484
+ * Other input/output storage requirements are the same as knn_L2sqr.
485
+ */
486
+ void knn_L2sqr_with_norms(
487
+ const float* x,
488
+ const float* y,
489
+ size_t d,
490
+ size_t nx,
491
+ size_t ny,
492
+ size_t k,
493
+ float* distances,
494
+ int64_t* indexes,
495
+ const float* y_norm2,
496
+ size_t query_block_size,
497
+ size_t database_block_size);
498
+
499
+ /** knn_inner_product for packed IEEE binary16 queries (x: nx * d uint16_t).
500
+ *
501
+ * The BLAS kernels widen each query tile once and reuse it for all database
502
+ * tiles, so there is no fp32 copy of the queries. Results are the same as
503
+ * knn_inner_product on the widened queries.
504
+ */
505
+ void knn_inner_product_fp16(
506
+ const uint16_t* x,
507
+ const float* y,
508
+ size_t d,
509
+ size_t nx,
510
+ size_t ny,
511
+ size_t k,
512
+ float* distances,
513
+ int64_t* indexes,
514
+ const IDSelector* sel = nullptr);
515
+
516
+ /// knn_L2sqr for packed IEEE binary16 queries, see knn_inner_product_fp16
517
+ void knn_L2sqr_fp16(
518
+ const uint16_t* x,
519
+ const float* y,
520
+ size_t d,
521
+ size_t nx,
522
+ size_t ny,
523
+ size_t k,
524
+ float* distances,
525
+ int64_t* indexes,
526
+ const float* y_norm2 = nullptr,
527
+ const IDSelector* sel = nullptr);
528
+
458
529
  /** Find the max inner product neighbors for nx queries in a set of ny vectors
459
530
  * indexed by ids. May be useful for re-ranking a pre-selected vector list
460
531
  *
@@ -569,6 +640,108 @@ void compute_PQ_dis_tables_dsub2(
569
640
  * Misc matrix and vector manipulation functions
570
641
  ***************************************************************************/
571
642
 
643
+ /** Per-cluster sums and counts of labeled vectors.
644
+ *
645
+ * For each i < n, adds x[i] to sums[labels[i], 0:d] and 1 to
646
+ * sums[labels[i], d]. The result equals numpy.add.at(sums[:, :d], labels, x)
647
+ * and numpy.add.at(sums[:, d], labels, 1) bit for bit: the rows of each
648
+ * cluster are added in increasing i, whatever the number of threads.
649
+ * Unlike numpy.add.at, which is single-threaded, it runs with OpenMP when
650
+ * n * d > 65536: each thread owns a contiguous range of labels and scans all
651
+ * rows, so no two threads write the same row of sums and no reduction is
652
+ * needed.
653
+ *
654
+ * All arrays are contiguous and row-major; x and sums are float32, labels
655
+ * int64; sums must not overlap x or labels.
656
+ *
657
+ * @param d dimension of the vectors
658
+ * @param n number of vectors
659
+ * @param k number of clusters
660
+ * @param x input vectors, size n * d
661
+ * @param labels input cluster of each vector, size n, in [0, k)
662
+ * @param sums in/out sums and counts, size k * (d + 1): row a holds the
663
+ * d components of the sum and then the count; results are
664
+ * added to its contents
665
+ */
666
+ void accumulate_cluster_sums(
667
+ size_t d,
668
+ size_t n,
669
+ size_t k,
670
+ const float* x,
671
+ const int64_t* labels,
672
+ float* sums);
673
+
674
+ /** Per-cluster removal costs and Oja long-axis terms of an assignment.
675
+ *
676
+ * Used by e-means' cost-based revival. For each i < n, with a = labels[i],
677
+ * y = x[i] - centroids[a] and p = <y, axes[a]>, adds p * y to
678
+ * stats[a, 0:d] and the removal cost d2[i] - d1[i] to stats[a, d] (zero is
679
+ * added when the computed difference is not finite). The rows of each
680
+ * cluster are added in increasing i, so the result does not depend on the
681
+ * number of threads. OpenMP as in accumulate_cluster_sums: each thread owns
682
+ * a range of labels.
683
+ *
684
+ * All arrays are contiguous and row-major, float32 except the int64
685
+ * labels; stats must not overlap any input.
686
+ *
687
+ * @param d dimension of the vectors
688
+ * @param n number of vectors
689
+ * @param k number of clusters
690
+ * @param x input vectors, size n * d
691
+ * @param labels input nearest centroid of each vector, size n, in [0, k)
692
+ * @param d1 input squared distance to the nearest centroid, size n
693
+ * @param d2 input squared distance to the second-nearest centroid,
694
+ * size n
695
+ * @param centroids input centroids used for the assignment, size k * d
696
+ * @param axes input unit long axis of each cluster, size k * d
697
+ * @param stats in/out per-cluster sums, size k * (d + 1): row a holds
698
+ * the sum of p * y and then the sum of d2 - d1; results
699
+ * are added to its contents
700
+ */
701
+ void accumulate_removal_stats(
702
+ size_t d,
703
+ size_t n,
704
+ size_t k,
705
+ const float* x,
706
+ const int64_t* labels,
707
+ const float* d1,
708
+ const float* d2,
709
+ const float* centroids,
710
+ const float* axes,
711
+ float* stats);
712
+
713
+ /** Oja update of the cluster axes from accumulate_removal_stats sums.
714
+ *
715
+ * For each cluster a = ids[i], i < n: u = axis[a, 0:d] += weight *
716
+ * batch[a, 0:d]; if |u| > 0 (computed in double), axes[a] = u / |u|;
717
+ * batch[a] is then zeroed. Clusters are independent; OpenMP over ids when
718
+ * n * d > 65536. Rows not in ids keep their contents, and so does axes[a]
719
+ * when |u| = 0.
720
+ *
721
+ * All arrays are contiguous and row-major, float32 except the int64 ids;
722
+ * batch, axis and axes have k rows (k is the number of clusters they were
723
+ * allocated for) and must not overlap each other or ids.
724
+ *
725
+ * @param d dimension of the vectors
726
+ * @param n number of clusters to update
727
+ * @param ids input clusters to update, size n, distinct, each in [0, k)
728
+ * @param weight input weight of the batch sums
729
+ * @param batch in/out batch sums, k rows of size d + 1 (as stats in
730
+ * accumulate_removal_stats); rows ids are zeroed
731
+ * @param axis in/out running sums u, k rows of size d + 1 (component d
732
+ * is not used)
733
+ * @param axes out unit axes, k rows of size d; rows ids with |u| > 0 are
734
+ * overwritten
735
+ */
736
+ void update_removal_axes(
737
+ size_t d,
738
+ size_t n,
739
+ const int64_t* ids,
740
+ float weight,
741
+ float* batch,
742
+ float* axis,
743
+ float* axes);
744
+
572
745
  /** compute c := a + bf * b for a, b and c tables
573
746
  *
574
747
  * @param n size of the tables
@@ -600,4 +773,8 @@ int fvec_madd_and_argmin(
600
773
  const float* b,
601
774
  float* c);
602
775
 
776
+ /// sum of x[j] for every j < d whose bit is set in bits (LSB-first per byte)
777
+ template <SIMDLevel>
778
+ float fvec_masked_sum(const float* x, const uint8_t* bits, size_t d);
779
+
603
780
  } // namespace faiss
@@ -184,7 +184,7 @@ inline void fvec_sub_dispatch(
184
184
  const float* a,
185
185
  const float* b,
186
186
  float* c) {
187
- with_simd_level_256bit(
187
+ with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_NO_AVX512>(
188
188
  [&]<SIMDLevel level>() { fvec_sub<level>(d, a, b, c); });
189
189
  }
190
190
 
@@ -193,7 +193,7 @@ inline void fvec_add_dispatch(
193
193
  const float* a,
194
194
  const float* b,
195
195
  float* c) {
196
- with_simd_level_256bit(
196
+ with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_NO_AVX512>(
197
197
  [&]<SIMDLevel level>() { fvec_add<level>(d, a, b, c); });
198
198
  }
199
199
 
@@ -202,7 +202,7 @@ inline void fvec_add_scalar_dispatch(
202
202
  const float* a,
203
203
  float b,
204
204
  float* c) {
205
- with_simd_level_256bit(
205
+ with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_NO_AVX512>(
206
206
  [&]<SIMDLevel level>() { fvec_add<level>(d, a, b, c); });
207
207
  }
208
208
 
@@ -214,7 +214,9 @@ inline void compute_PQ_dis_tables_dsub2_dispatch(
214
214
  const float* x,
215
215
  bool is_inner_product,
216
216
  float* dis_tables) {
217
- with_simd_level_256bit([&]<SIMDLevel level>() {
217
+ constexpr int dsub2_mask =
218
+ AVAILABLE_SIMD_LEVELS_AVX2_NEON | (1 << int(SIMDLevel::RISCV_RVV));
219
+ with_selected_simd_levels<dsub2_mask>([&]<SIMDLevel level>() {
218
220
  compute_PQ_dis_tables_dsub2<level>(
219
221
  d, ksub, centroids, nx, x, is_inner_product, dis_tables);
220
222
  });
@@ -7,10 +7,9 @@
7
7
 
8
8
  // -*- c++ -*-
9
9
 
10
+ #include <faiss/impl/platform_macros.h>
10
11
  #include <faiss/utils/distances_fused/distances_fused.h>
11
12
 
12
- #ifdef COMPILE_SIMD_AVX512
13
-
14
13
  #include <immintrin.h>
15
14
 
16
15
  namespace faiss {
@@ -264,7 +263,7 @@ void exhaustive_L2sqr_fused_cmax(
264
263
 
265
264
  const idx_t nx_p = (nx / NX_POINTS_PER_LOOP) * NX_POINTS_PER_LOOP;
266
265
  // the main loop.
267
- #pragma omp parallel for schedule(dynamic)
266
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
268
267
  for (idx_t i = 0; i < nx_p; i += NX_POINTS_PER_LOOP) {
269
268
  kernel<DIM, NX_POINTS_PER_LOOP, NY_POINTS_PER_LOOP>(
270
269
  x, y, y_transposed.data(), ny, res, y_norms, i);
@@ -343,5 +342,3 @@ bool exhaustive_L2sqr_fused_cmax<SIMDLevel::AVX512>(
343
342
  }
344
343
 
345
344
  } // namespace faiss
346
-
347
- #endif // COMPILE_SIMD_AVX512
@@ -24,20 +24,6 @@ bool exhaustive_L2sqr_fused_cmax<SIMDLevel::NONE>(
24
24
  return false;
25
25
  }
26
26
 
27
- #ifdef COMPILE_SIMD_RISCV_RVV
28
- template <>
29
- bool exhaustive_L2sqr_fused_cmax<SIMDLevel::RISCV_RVV>(
30
- const float*,
31
- const float*,
32
- size_t,
33
- size_t,
34
- size_t,
35
- Top1BlockResultHandler<CMax<float, int64_t>>&,
36
- const float*) {
37
- return false;
38
- }
39
- #endif // COMPILE_SIMD_RISCV_RVV
40
-
41
27
  bool exhaustive_L2sqr_fused_cmax(
42
28
  const float* x,
43
29
  const float* y,
@@ -0,0 +1,25 @@
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
+ #include <faiss/utils/distances_fused/distances_fused.h>
9
+
10
+ namespace faiss {
11
+
12
+ // Scalar fallback: no fused kernel available.
13
+ template <>
14
+ bool exhaustive_L2sqr_fused_cmax<SIMDLevel::RISCV_RVV>(
15
+ const float*,
16
+ const float*,
17
+ size_t,
18
+ size_t,
19
+ size_t,
20
+ Top1BlockResultHandler<CMax<float, int64_t>>&,
21
+ const float*) {
22
+ return false;
23
+ }
24
+
25
+ } // namespace faiss
@@ -7,8 +7,6 @@
7
7
 
8
8
  // AVX2 compilation unit for the simdlib-based fused distance kernel.
9
9
 
10
- #ifdef COMPILE_SIMD_AVX2
11
-
12
10
  #include <faiss/impl/simdlib/simdlib_avx2.h>
13
11
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
14
12
  #include <faiss/utils/distances_fused/simdlib_kernel-inl.h>
@@ -71,5 +69,3 @@ bool exhaustive_L2sqr_fused_cmax<SIMDLevel::AVX2>(
71
69
  }
72
70
 
73
71
  } // namespace faiss
74
-
75
- #endif // COMPILE_SIMD_AVX2
@@ -7,8 +7,6 @@
7
7
 
8
8
  // ARM NEON compilation unit for the simdlib-based fused distance kernel.
9
9
 
10
- #ifdef COMPILE_SIMD_ARM_NEON
11
-
12
10
  #include <faiss/impl/simdlib/simdlib_neon.h>
13
11
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
14
12
  #include <faiss/utils/distances_fused/simdlib_kernel-inl.h>
@@ -53,5 +51,3 @@ bool exhaustive_L2sqr_fused_cmax<SIMDLevel::ARM_NEON>(
53
51
  }
54
52
 
55
53
  } // namespace faiss
56
-
57
- #endif // COMPILE_SIMD_ARM_NEON
@@ -14,6 +14,7 @@
14
14
 
15
15
  #pragma once
16
16
 
17
+ #include <faiss/impl/platform_macros.h>
17
18
  #include <faiss/utils/distances_fused/distances_fused.h>
18
19
 
19
20
  namespace faiss {
@@ -267,7 +268,7 @@ void exhaustive_L2sqr_fused_cmax(
267
268
 
268
269
  const size_t nx_p = (nx / NX_POINTS_PER_LOOP) * NX_POINTS_PER_LOOP;
269
270
  // the main loop.
270
- #pragma omp parallel for schedule(dynamic)
271
+ #pragma omp parallel for schedule(FAISS_OMP_DYNAMIC)
271
272
  for (int64_t i = 0; i < static_cast<int64_t>(nx_p);
272
273
  i += NX_POINTS_PER_LOOP) {
273
274
  kernel<DIM, NX_POINTS_PER_LOOP, NY_POINTS_PER_LOOP, SL>(
@@ -8,6 +8,7 @@
8
8
  // -*- c++ -*-
9
9
 
10
10
  #include <faiss/utils/distances.h>
11
+ #include <faiss/utils/simd_impl/masked_sum.h>
11
12
 
12
13
  #include <cmath>
13
14
  #include <cstring>
@@ -42,6 +43,14 @@ void fvec_madd<SIMDLevel::NONE>(
42
43
  }
43
44
  }
44
45
 
46
+ template <>
47
+ float fvec_masked_sum<SIMDLevel::NONE>(
48
+ const float* x,
49
+ const uint8_t* bits,
50
+ size_t d) {
51
+ return masked_sum<SIMDLevel::NONE>(x, bits, d);
52
+ }
53
+
45
54
  template <>
46
55
  void fvec_L2sqr_ny_transposed<SIMDLevel::NONE>(
47
56
  float* dis,
@@ -112,7 +112,8 @@ struct VectorDistance {
112
112
  size_t d;
113
113
  float metric_arg;
114
114
 
115
- VectorDistance(size_t d, float metric_arg) : d(d), metric_arg(metric_arg) {}
115
+ VectorDistance(size_t d_in, float metric_arg_in)
116
+ : d(d_in), metric_arg(metric_arg_in) {}
116
117
 
117
118
  static constexpr MetricType metric = mt;
118
119
  static constexpr bool is_similarity = is_similarity_metric(mt);
@@ -12,6 +12,8 @@
12
12
 
13
13
  namespace faiss {
14
14
 
15
+ namespace {
16
+
15
17
  inline uint16_t encode_fp16(float x) {
16
18
  __m128 xf = _mm_set1_ps(x);
17
19
  __m128i xi =
@@ -25,4 +27,6 @@ inline float decode_fp16(uint16_t x) {
25
27
  return _mm_cvtss_f32(xf);
26
28
  }
27
29
 
30
+ } // namespace
31
+
28
32
  } // namespace faiss
@@ -27,63 +27,35 @@ inline uint32_t intbits(float f) {
27
27
  return *(uint32_t*)fptr;
28
28
  }
29
29
 
30
- } // namespace
31
-
32
30
  inline uint16_t encode_fp16(float f) {
33
- // via Fabian "ryg" Giesen.
34
- // https://gist.github.com/2156668
35
- uint32_t sign_mask = 0x80000000u;
36
- int32_t o;
31
+ // Rounds to nearest even, as the F16C and Arm conversions do, so that
32
+ // every SIMD level encodes the same bits. Via Fabian "ryg" Giesen
33
+ // (float_to_half_fast3_rtne): https://gist.github.com/2156668
34
+ const uint32_t f32infty = 255u << 23;
35
+ const uint32_t f16max = (127u + 16u) << 23;
36
+ const uint32_t denorm_magic = ((127u - 15u) + (23u - 10u) + 1u) << 23;
37
+ const uint32_t sign_mask = 0x80000000u;
37
38
 
38
39
  uint32_t fint = intbits(f);
39
- uint32_t sign = fint & sign_mask;
40
+ const uint32_t sign = fint & sign_mask;
40
41
  fint ^= sign;
41
42
 
42
- // NOTE all the integer compares in this function can be safely
43
- // compiled into signed compares since all operands are below
44
- // 0x80000000. Important if you want fast straight SSE2 code (since
45
- // there's no unsigned PCMPGTD).
46
-
47
- // Inf or NaN (all exponent bits set)
48
- // NaN->qNaN and Inf->Inf
49
- // unconditional assignment here, will override with right value for
50
- // the regular case below.
51
- uint32_t f32infty = 255u << 23;
52
- o = (fint > f32infty) ? 0x7e00u : 0x7c00u;
53
-
54
- // (De)normalized number or zero
55
- // update fint unconditionally to save the blending; we don't need it
56
- // anymore for the Inf/NaN case anyway.
57
-
58
- const uint32_t round_mask = ~0xfffu;
59
- const uint32_t magic = 15u << 23;
60
-
61
- // Shift exponent down, denormalize if necessary.
62
- // NOTE This represents half-float denormals using single
63
- // precision denormals. The main reason to do this is that
64
- // there's no shift with per-lane variable shifts in SSE*, which
65
- // we'd otherwise need. It has some funky side effects though:
66
- // - This conversion will actually respect the FTZ (Flush To Zero)
67
- // flag in MXCSR - if it's set, no half-float denormals will be
68
- // generated. I'm honestly not sure whether this is good or
69
- // bad. It's definitely interesting.
70
- // - If the underlying HW doesn't support denormals (not an issue
71
- // with Intel CPUs, but might be a problem on GPUs or PS3 SPUs),
72
- // you will always get flush-to-zero behavior. This is bad,
73
- // unless you're on a CPU where you don't care.
74
- // - Denormals tend to be slow. FP32 denormals are rare in
75
- // practice outside of things like recursive filters in DSP -
76
- // not a typical half-float application. Whether FP16 denormals
77
- // are rare in practice, I don't know. Whatever slow path your
78
- // HW may or may not have for denormals, this may well hit it.
79
- float fscale = floatbits(fint & round_mask) * floatbits(magic);
80
- fscale = std::min(fscale, floatbits((31u << 23) - 0x1000u));
81
- int32_t fint2 = intbits(fscale) - round_mask;
82
-
83
- if (fint < f32infty)
84
- o = fint2 >> 13; // Take the bits!
85
-
86
- return (o | (sign >> 16));
43
+ uint32_t o;
44
+ if (fint >= f16max) {
45
+ // Inf->Inf. NaN->qNaN with the top bits of the payload, as F16C.
46
+ o = (fint > f32infty) ? 0x7e00u | ((fint >> 13) & 0x3ffu) : 0x7c00u;
47
+ } else if (fint < (113u << 23)) {
48
+ // The result is subnormal or zero. Adding a magic value aligns the 10
49
+ // mantissa bits at the bottom of the float, and the float addition
50
+ // rounds to nearest even.
51
+ o = intbits(floatbits(fint) + floatbits(denorm_magic)) - denorm_magic;
52
+ } else {
53
+ const uint32_t mant_odd = (fint >> 13) & 1;
54
+ // rebias the exponent, and round to nearest even
55
+ fint += ((15u - 127u) << 23) + 0xfffu + mant_odd;
56
+ o = fint >> 13;
57
+ }
58
+ return static_cast<uint16_t>(o | (sign >> 16));
87
59
  }
88
60
 
89
61
  inline float decode_fp16(uint16_t h) {
@@ -105,4 +77,6 @@ inline float decode_fp16(uint16_t h) {
105
77
  return floatbits(((exp == shifted_exp) ? infnan_val : reg_val) | sign_bit);
106
78
  }
107
79
 
80
+ } // namespace
81
+
108
82
  } // namespace faiss
@@ -42,6 +42,9 @@ inline BitstringReader::BitstringReader(
42
42
  : code(code_in), code_size(code_size_in), i(0) {}
43
43
 
44
44
  inline uint64_t BitstringReader::read(int nbit) {
45
+ if (nbit <= 0) {
46
+ return 0;
47
+ }
45
48
  assert(code_size * 8 >= nbit + i);
46
49
  // nb of available bits in i / 8
47
50
  int na = 8 - (i & 7);
@@ -5,12 +5,8 @@
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
  // NOLINTBEGIN(facebook-hte-InlineHeader,facebook-unused-include-check)
12
10
  #include <faiss/utils/hamming_distance/hamming_computer-avx2.h>
13
11
  #include <faiss/utils/hamming_distance/hamming_impl.h>
14
12
  // NOLINTEND(facebook-hte-InlineHeader,facebook-unused-include-check)
15
-
16
- #endif // COMPILE_SIMD_AVX2
@@ -5,11 +5,7 @@
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
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
12
10
  #include <faiss/utils/hamming_distance/hamming_computer-avx512.h>
13
11
  #include <faiss/utils/hamming_distance/hamming_impl.h>
14
-
15
- #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_AVX512_VPOPCNT
9
-
10
8
  #define THE_SIMD_LEVEL SIMDLevel::AVX512_VPOPCNT
11
9
  #include <faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h>
12
10
  #include <faiss/utils/hamming_distance/hamming_impl.h>
@@ -20,5 +18,3 @@
20
18
  // Fails the build if the batch path stops being selected for this computer.
21
19
  static_assert(faiss::has_hamming_batch<
22
20
  faiss::HammingComputer20_tpl<faiss::SIMDLevel::AVX512_VPOPCNT>>);
23
-
24
- #endif // COMPILE_SIMD_AVX512_VPOPCNT
@@ -103,12 +103,40 @@ struct HammingComputerDefault_tpl<SIMDLevel::AVX512> {
103
103
  remainder8 = code_size % 8;
104
104
  }
105
105
 
106
+ /// No VPOPCNTDQ at this level: count the bits of each nibble with a
107
+ /// 16-entry table (vpshufb), then sum the bytes per 64-bit lane (vpsadbw).
108
+ /// The partial last block is a masked load, so no byte past the code is
109
+ /// read.
106
110
  int hamming(const uint8_t* b8) const {
107
- const uint64_t* a64 = reinterpret_cast<const uint64_t*>(a8);
108
- const uint64_t* b64 = reinterpret_cast<const uint64_t*>(b8);
109
-
110
- return hamming_popcount_tail(
111
- a64, b64, 0, quotient8, a8, b8, remainder8);
111
+ const int code_size = quotient8 * 8 + remainder8;
112
+ const __m512i lut = _mm512_broadcast_i32x4(
113
+ _mm_setr_epi8(0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4));
114
+ const __m512i low4 = _mm512_set1_epi8(0x0f);
115
+ const auto count = [&](__m512i x) {
116
+ const __m512i lo = _mm512_and_si512(x, low4);
117
+ const __m512i hi = _mm512_and_si512(_mm512_srli_epi16(x, 4), low4);
118
+ const __m512i c = _mm512_add_epi8(
119
+ _mm512_shuffle_epi8(lut, lo), _mm512_shuffle_epi8(lut, hi));
120
+ return _mm512_sad_epu8(c, _mm512_setzero_si512());
121
+ };
122
+ __m512i accu = _mm512_setzero_si512();
123
+ int i = 0;
124
+ for (; i + 64 <= code_size; i += 64) {
125
+ accu = _mm512_add_epi64(
126
+ accu,
127
+ count(_mm512_xor_si512(
128
+ _mm512_loadu_si512(a8 + i),
129
+ _mm512_loadu_si512(b8 + i))));
130
+ }
131
+ if (i < code_size) {
132
+ const __mmask64 mask = _bzhi_u64(~uint64_t(0), code_size - i);
133
+ accu = _mm512_add_epi64(
134
+ accu,
135
+ count(_mm512_xor_si512(
136
+ _mm512_maskz_loadu_epi8(mask, a8 + i),
137
+ _mm512_maskz_loadu_epi8(mask, b8 + i))));
138
+ }
139
+ return static_cast<int>(_mm512_reduce_add_epi64(accu));
112
140
  }
113
141
 
114
142
  inline int get_code_size() const {