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
@@ -7,6 +7,12 @@
7
7
 
8
8
  #pragma once
9
9
 
10
+ #include <faiss/impl/scalar_quantizer/codecs.h>
11
+ #include <faiss/impl/scalar_quantizer/distance_computers.h>
12
+ #include <faiss/impl/scalar_quantizer/quantizers.h>
13
+ #include <faiss/impl/scalar_quantizer/similarities.h>
14
+ #include <faiss/impl/scalar_quantizer/sq8_coefficients.h>
15
+ #include <faiss/impl/simdlib/simdlib_avx512.h>
10
16
  #include <cstring>
11
17
 
12
18
  namespace faiss {
@@ -14,11 +20,6 @@ namespace faiss {
14
20
  namespace scalar_quantizer {
15
21
 
16
22
  using simd16float32 = faiss::simd16float32_tpl<SIMDLevel::AVX512>;
17
- using simd512bit = faiss::simd512bit_tpl<SIMDLevel::AVX512>;
18
-
19
- /**********************************************************
20
- * TurboQuant bit-unpacking helpers
21
- **********************************************************/
22
23
 
23
24
  namespace {
24
25
 
@@ -100,7 +101,7 @@ FAISS_ALWAYS_INLINE __m512i unpack_16x4bit_to_u32(const uint8_t* code, int i) {
100
101
  **********************************************************/
101
102
 
102
103
  template <>
103
- struct Codec8bit<SIMDLevel::AVX512> : Codec8bit<SIMDLevel::NONE> {
104
+ struct Codec8bit<THE_LEVEL_TO_DISPATCH> : Codec8bit<SIMDLevel::NONE> {
104
105
  static FAISS_ALWAYS_INLINE simd16float32
105
106
  decode_16_components(const uint8_t* code, size_t i) {
106
107
  const __m128i c16 = _mm_loadu_si128((__m128i*)(code + i));
@@ -113,7 +114,7 @@ struct Codec8bit<SIMDLevel::AVX512> : Codec8bit<SIMDLevel::NONE> {
113
114
  };
114
115
 
115
116
  template <>
116
- struct Codec4bit<SIMDLevel::AVX512> : Codec4bit<SIMDLevel::NONE> {
117
+ struct Codec4bit<THE_LEVEL_TO_DISPATCH> : Codec4bit<SIMDLevel::NONE> {
117
118
  static FAISS_ALWAYS_INLINE simd16float32
118
119
  decode_16_components(const uint8_t* code, size_t i) {
119
120
  uint64_t c8 = *(uint64_t*)(code + (i >> 1));
@@ -135,7 +136,7 @@ struct Codec4bit<SIMDLevel::AVX512> : Codec4bit<SIMDLevel::NONE> {
135
136
  };
136
137
 
137
138
  template <>
138
- struct Codec6bit<SIMDLevel::AVX512> : Codec6bit<SIMDLevel::NONE> {
139
+ struct Codec6bit<THE_LEVEL_TO_DISPATCH> : Codec6bit<SIMDLevel::NONE> {
139
140
  static FAISS_ALWAYS_INLINE simd16float32
140
141
  decode_16_components(const uint8_t* code, size_t i) {
141
142
  // pure AVX512 implementation (not necessarily the fastest).
@@ -152,10 +153,10 @@ struct Codec6bit<SIMDLevel::AVX512> : Codec6bit<SIMDLevel::NONE> {
152
153
  // 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
153
154
  // 00 01 02 03
154
155
  const __m256i shuffle_mask = _mm256_setr_epi16(
155
- 0xFF00, 0x0100, 0x0201, 0xFF02,
156
- 0xFF03, 0x0403, 0x0504, 0xFF05,
157
- 0xFF06, 0x0706, 0x0807, 0xFF08,
158
- 0xFF09, 0x0A09, 0x0B0A, 0xFF0B);
156
+ short(0xFF00), 0x0100, 0x0201, short(0xFF02),
157
+ short(0xFF03), 0x0403, 0x0504, short(0xFF05),
158
+ short(0xFF06), 0x0706, 0x0807, short(0xFF08),
159
+ short(0xFF09), 0x0A09, 0x0B0A, short(0xFF0B));
159
160
  const __m256i shuffled = _mm256_shuffle_epi8(bit_6v_256, shuffle_mask);
160
161
 
161
162
  // 0: xxxxxxxx xx543210
@@ -192,17 +193,17 @@ template <class Codec>
192
193
  struct QuantizerTemplate<
193
194
  Codec,
194
195
  scalar_quantizer::QuantizerTemplateScaling::UNIFORM,
195
- SIMDLevel::AVX512>
196
+ THE_LEVEL_TO_DISPATCH>
196
197
  : QuantizerTemplate<
197
198
  Codec,
198
199
  scalar_quantizer::QuantizerTemplateScaling::UNIFORM,
199
200
  SIMDLevel::NONE> {
200
- QuantizerTemplate(size_t d, const std::vector<float>& trained)
201
+ QuantizerTemplate(size_t d_in, const std::vector<float>& trained)
201
202
  : QuantizerTemplate<
202
203
  Codec,
203
204
  scalar_quantizer::QuantizerTemplateScaling::UNIFORM,
204
- SIMDLevel::NONE>(d, trained) {
205
- assert(d % 16 == 0);
205
+ SIMDLevel::NONE>(d_in, trained) {
206
+ assert(d_in % 16 == 0);
206
207
  }
207
208
 
208
209
  FAISS_ALWAYS_INLINE simd16float32
@@ -211,23 +212,29 @@ struct QuantizerTemplate<
211
212
  return simd16float32(_mm512_fmadd_ps(
212
213
  xi, _mm512_set1_ps(this->vdiff), _mm512_set1_ps(this->vmin)));
213
214
  }
215
+
216
+ /// Raw codec decode without denormalization
217
+ FAISS_ALWAYS_INLINE simd16float32
218
+ decode_16_raw(const uint8_t* code, int i) const {
219
+ return Codec::decode_16_components(code, i);
220
+ }
214
221
  };
215
222
 
216
223
  template <class Codec>
217
224
  struct QuantizerTemplate<
218
225
  Codec,
219
226
  scalar_quantizer::QuantizerTemplateScaling::NON_UNIFORM,
220
- SIMDLevel::AVX512>
227
+ THE_LEVEL_TO_DISPATCH>
221
228
  : QuantizerTemplate<
222
229
  Codec,
223
230
  scalar_quantizer::QuantizerTemplateScaling::NON_UNIFORM,
224
231
  SIMDLevel::NONE> {
225
- QuantizerTemplate(size_t d, const std::vector<float>& trained)
232
+ QuantizerTemplate(size_t d_in, const std::vector<float>& trained)
226
233
  : QuantizerTemplate<
227
234
  Codec,
228
235
  scalar_quantizer::QuantizerTemplateScaling::NON_UNIFORM,
229
- SIMDLevel::NONE>(d, trained) {
230
- assert(d % 16 == 0);
236
+ SIMDLevel::NONE>(d_in, trained) {
237
+ assert(d_in % 16 == 0);
231
238
  }
232
239
 
233
240
  FAISS_ALWAYS_INLINE simd16float32
@@ -244,40 +251,87 @@ struct QuantizerTemplate<
244
251
  * Lloyd-Max scalar quantizer
245
252
  **********************************************************/
246
253
 
247
- #define DEFINE_LLOYD_MAX_AVX512_SPECIALIZATION(NBITS, INDEX_EXPR) \
248
- template <> \
249
- struct QuantizerLloydMax<NBITS, SIMDLevel::AVX512> \
250
- : QuantizerLloydMax<NBITS, SIMDLevel::NONE> { \
251
- using Base = QuantizerLloydMax<NBITS, SIMDLevel::NONE>; \
252
- \
253
- QuantizerLloydMax(size_t d, const std::vector<float>& trained) \
254
- : Base(d, trained) { \
255
- assert(d % 16 == 0); \
256
- } \
257
- \
258
- FAISS_ALWAYS_INLINE simd16float32 \
259
- reconstruct_16_components(const uint8_t* code, int i) const { \
260
- const __m512i indices = (INDEX_EXPR); \
261
- return simd16float32(_mm512_i32gather_ps( \
262
- indices, this->centroids, sizeof(float))); \
263
- } \
264
- }
265
-
266
- DEFINE_LLOYD_MAX_AVX512_SPECIALIZATION(1, unpack_16x1bit_to_u32(code, i));
267
- DEFINE_LLOYD_MAX_AVX512_SPECIALIZATION(2, unpack_16x2bit_to_u32(code, i));
268
- DEFINE_LLOYD_MAX_AVX512_SPECIALIZATION(3, unpack_16x3bit_to_u32(code, i));
269
- DEFINE_LLOYD_MAX_AVX512_SPECIALIZATION(4, unpack_16x4bit_to_u32(code, i));
270
-
271
- #undef DEFINE_LLOYD_MAX_AVX512_SPECIALIZATION
254
+ // 1-bit Lloyd-Max AVX512: 16 comparisons → 2 bytes via mask compare.
255
+ // NLT_UQ is !(x < b), matching the scalar upper_bound tie-break.
256
+ template <>
257
+ struct QuantizerLloydMax<1, THE_LEVEL_TO_DISPATCH>
258
+ : QuantizerLloydMaxBase<1, THE_LEVEL_TO_DISPATCH> {
259
+ using Base = QuantizerLloydMaxBase<1, THE_LEVEL_TO_DISPATCH>;
260
+
261
+ QuantizerLloydMax(size_t d_in, const std::vector<float>& trained)
262
+ : Base(d_in, trained) {
263
+ assert(d_in % 16 == 0);
264
+ }
265
+
266
+ FAISS_ALWAYS_INLINE simd16float32
267
+ reconstruct_16_components(const uint8_t* code, int i) const {
268
+ return simd16float32(_mm512_i32gather_ps(
269
+ unpack_16x1bit_to_u32(code, i),
270
+ this->centroids,
271
+ sizeof(float)));
272
+ }
273
+
274
+ void encode_vector(const float* x, uint8_t* code) const final {
275
+ __m512 boundary = _mm512_set1_ps(this->boundaries[0]);
276
+ for (size_t i = 0; i < this->d; i += 16) {
277
+ __m512 vals = _mm512_loadu_ps(x + i);
278
+ __mmask16 mask = _mm512_cmp_ps_mask(vals, boundary, _CMP_NLT_UQ);
279
+ uint16_t bits = _cvtmask16_u32(mask);
280
+ memcpy(code + i / 8, &bits, sizeof(uint16_t));
281
+ }
282
+ }
283
+
284
+ void decode_vector(const uint8_t* code, float* x) const final {
285
+ for (size_t i = 0; i < this->d; i += 16) {
286
+ simd16float32 xi =
287
+ reconstruct_16_components(code, static_cast<int>(i));
288
+ _mm512_storeu_ps(x + i, xi.f);
289
+ }
290
+ }
291
+ };
272
292
 
293
+ // 2-4 bit Lloyd-Max AVX512: decode via gather, encode stays scalar.
294
+ #define DEFINE_LLOYD_MAX_AVX512_MULTIBIT(NBITS, UNPACK_EXPR) \
295
+ template <> \
296
+ struct QuantizerLloydMax<NBITS, THE_LEVEL_TO_DISPATCH> \
297
+ : QuantizerLloydMaxBase<NBITS, THE_LEVEL_TO_DISPATCH> { \
298
+ using Base = QuantizerLloydMaxBase<NBITS, THE_LEVEL_TO_DISPATCH>; \
299
+ \
300
+ QuantizerLloydMax(size_t d_in, const std::vector<float>& trained) \
301
+ : Base(d_in, trained) { \
302
+ assert(d_in % 16 == 0); \
303
+ } \
304
+ \
305
+ FAISS_ALWAYS_INLINE simd16float32 \
306
+ reconstruct_16_components(const uint8_t* code, int i) const { \
307
+ return simd16float32(_mm512_i32gather_ps( \
308
+ (UNPACK_EXPR), this->centroids, sizeof(float))); \
309
+ } \
310
+ \
311
+ void decode_vector(const uint8_t* code, float* x) const final { \
312
+ for (size_t i = 0; i < this->d; i += 16) { \
313
+ simd16float32 xi = \
314
+ reconstruct_16_components(code, static_cast<int>(i)); \
315
+ _mm512_storeu_ps(x + i, xi.f); \
316
+ } \
317
+ } \
318
+ }
319
+
320
+ DEFINE_LLOYD_MAX_AVX512_MULTIBIT(2, unpack_16x2bit_to_u32(code, i));
321
+ DEFINE_LLOYD_MAX_AVX512_MULTIBIT(3, unpack_16x3bit_to_u32(code, i));
322
+ DEFINE_LLOYD_MAX_AVX512_MULTIBIT(4, unpack_16x4bit_to_u32(code, i));
323
+
324
+ #undef DEFINE_LLOYD_MAX_AVX512_MULTIBIT
325
+
326
+ // 8-bit Lloyd-Max AVX512
273
327
  template <>
274
- struct QuantizerLloydMax<8, SIMDLevel::AVX512>
275
- : QuantizerLloydMax<8, SIMDLevel::NONE> {
276
- using Base = QuantizerLloydMax<8, SIMDLevel::NONE>;
328
+ struct QuantizerLloydMax<8, THE_LEVEL_TO_DISPATCH>
329
+ : QuantizerLloydMaxBase<8, THE_LEVEL_TO_DISPATCH> {
330
+ using Base = QuantizerLloydMaxBase<8, THE_LEVEL_TO_DISPATCH>;
277
331
 
278
- QuantizerLloydMax(size_t d, const std::vector<float>& trained)
279
- : Base(d, trained) {
280
- assert(d % 16 == 0);
332
+ QuantizerLloydMax(size_t d_in, const std::vector<float>& trained)
333
+ : Base(d_in, trained) {
334
+ assert(d_in % 16 == 0);
281
335
  }
282
336
 
283
337
  FAISS_ALWAYS_INLINE simd16float32
@@ -288,6 +342,14 @@ struct QuantizerLloydMax<8, SIMDLevel::AVX512>
288
342
  return simd16float32(
289
343
  _mm512_i32gather_ps(indices, this->centroids, sizeof(float)));
290
344
  }
345
+
346
+ void decode_vector(const uint8_t* code, float* x) const final {
347
+ for (size_t i = 0; i < this->d; i += 16) {
348
+ simd16float32 xi =
349
+ reconstruct_16_components(code, static_cast<int>(i));
350
+ _mm512_storeu_ps(x + i, xi.f);
351
+ }
352
+ }
291
353
  };
292
354
 
293
355
  /**********************************************************
@@ -295,17 +357,25 @@ struct QuantizerLloydMax<8, SIMDLevel::AVX512>
295
357
  **********************************************************/
296
358
 
297
359
  template <>
298
- struct QuantizerFP16<SIMDLevel::AVX512> : QuantizerFP16<SIMDLevel::NONE> {
299
- QuantizerFP16(size_t d, const std::vector<float>& trained)
300
- : QuantizerFP16<SIMDLevel::NONE>(d, trained) {
301
- assert(d % 16 == 0);
302
- }
360
+ struct QuantizerFP16<THE_LEVEL_TO_DISPATCH>
361
+ : QuantizerFP16Base<THE_LEVEL_TO_DISPATCH> {
362
+ using QuantizerFP16Base<THE_LEVEL_TO_DISPATCH>::QuantizerFP16Base;
303
363
 
304
364
  FAISS_ALWAYS_INLINE simd16float32
305
365
  reconstruct_16_components(const uint8_t* code, int i) const {
306
366
  __m256i codei = _mm256_loadu_si256((const __m256i*)(code + 2 * i));
307
367
  return simd16float32(_mm512_cvtph_ps(codei));
308
368
  }
369
+
370
+ /// Components i .. i + 15 selected by mask, the others 0. Reads no byte
371
+ /// past the selected components.
372
+ FAISS_ALWAYS_INLINE simd16float32 reconstruct_16_components_masked(
373
+ const uint8_t* code,
374
+ int i,
375
+ __mmask16 mask) const {
376
+ __m256i codei = _mm256_maskz_loadu_epi16(mask, code + 2 * i);
377
+ return simd16float32(_mm512_cvtph_ps(codei));
378
+ }
309
379
  };
310
380
 
311
381
  /**********************************************************
@@ -313,15 +383,26 @@ struct QuantizerFP16<SIMDLevel::AVX512> : QuantizerFP16<SIMDLevel::NONE> {
313
383
  **********************************************************/
314
384
 
315
385
  template <>
316
- struct QuantizerBF16<SIMDLevel::AVX512> : QuantizerBF16<SIMDLevel::NONE> {
317
- QuantizerBF16(size_t d, const std::vector<float>& trained)
318
- : QuantizerBF16<SIMDLevel::NONE>(d, trained) {
319
- assert(d % 16 == 0);
320
- }
386
+ struct QuantizerBF16<THE_LEVEL_TO_DISPATCH>
387
+ : QuantizerBF16Base<THE_LEVEL_TO_DISPATCH> {
388
+ using QuantizerBF16Base<THE_LEVEL_TO_DISPATCH>::QuantizerBF16Base;
321
389
 
322
390
  FAISS_ALWAYS_INLINE simd16float32
323
391
  reconstruct_16_components(const uint8_t* code, int i) const {
324
392
  __m256i code_256i = _mm256_loadu_si256((const __m256i*)(code + 2 * i));
393
+ return widen(code_256i);
394
+ }
395
+
396
+ /// Components i .. i + 15 selected by mask, the others 0. Reads no byte
397
+ /// past the selected components.
398
+ FAISS_ALWAYS_INLINE simd16float32 reconstruct_16_components_masked(
399
+ const uint8_t* code,
400
+ int i,
401
+ __mmask16 mask) const {
402
+ return widen(_mm256_maskz_loadu_epi16(mask, code + 2 * i));
403
+ }
404
+
405
+ static FAISS_ALWAYS_INLINE simd16float32 widen(__m256i code_256i) {
325
406
  __m512i code_512i = _mm512_cvtepu16_epi32(code_256i);
326
407
  code_512i = _mm512_slli_epi32(code_512i, 16);
327
408
  return simd16float32(_mm512_castsi512_ps(code_512i));
@@ -333,11 +414,11 @@ struct QuantizerBF16<SIMDLevel::AVX512> : QuantizerBF16<SIMDLevel::NONE> {
333
414
  **********************************************************/
334
415
 
335
416
  template <>
336
- struct Quantizer8bitDirect<SIMDLevel::AVX512>
337
- : Quantizer8bitDirect<SIMDLevel::NONE> {
338
- Quantizer8bitDirect(size_t d, const std::vector<float>& trained)
339
- : Quantizer8bitDirect<SIMDLevel::NONE>(d, trained) {
340
- assert(d % 16 == 0);
417
+ struct Quantizer8bitDirect<THE_LEVEL_TO_DISPATCH>
418
+ : Quantizer8bitDirectBase<THE_LEVEL_TO_DISPATCH> {
419
+ Quantizer8bitDirect(size_t d_in, const std::vector<float>& trained)
420
+ : Quantizer8bitDirectBase<THE_LEVEL_TO_DISPATCH>(d_in, trained) {
421
+ assert(d_in % 16 == 0);
341
422
  }
342
423
 
343
424
  FAISS_ALWAYS_INLINE simd16float32
@@ -353,11 +434,13 @@ struct Quantizer8bitDirect<SIMDLevel::AVX512>
353
434
  **********************************************************/
354
435
 
355
436
  template <>
356
- struct Quantizer8bitDirectSigned<SIMDLevel::AVX512>
357
- : Quantizer8bitDirectSigned<SIMDLevel::NONE> {
358
- Quantizer8bitDirectSigned(size_t d, const std::vector<float>& trained)
359
- : Quantizer8bitDirectSigned<SIMDLevel::NONE>(d, trained) {
360
- assert(d % 16 == 0);
437
+ struct Quantizer8bitDirectSigned<THE_LEVEL_TO_DISPATCH>
438
+ : Quantizer8bitDirectSignedBase<THE_LEVEL_TO_DISPATCH> {
439
+ Quantizer8bitDirectSigned(size_t d_in, const std::vector<float>& trained)
440
+ : Quantizer8bitDirectSignedBase<THE_LEVEL_TO_DISPATCH>(
441
+ d_in,
442
+ trained) {
443
+ assert(d_in % 16 == 0);
361
444
  }
362
445
 
363
446
  FAISS_ALWAYS_INLINE simd16float32
@@ -375,14 +458,14 @@ struct Quantizer8bitDirectSigned<SIMDLevel::AVX512>
375
458
  **********************************************************/
376
459
 
377
460
  template <>
378
- struct SimilarityL2<SIMDLevel::AVX512> {
461
+ struct SimilarityL2<THE_LEVEL_TO_DISPATCH> {
379
462
  static constexpr int simdwidth = 16;
380
- static constexpr SIMDLevel simd_level = SIMDLevel::AVX512;
463
+ static constexpr SIMDLevel simd_level = THE_LEVEL_TO_DISPATCH;
381
464
  static constexpr MetricType metric_type = METRIC_L2;
382
465
 
383
466
  const float *y, *yi;
384
467
 
385
- explicit SimilarityL2(const float* y) : y(y), yi(nullptr) {}
468
+ explicit SimilarityL2(const float* y_in) : y(y_in), yi(nullptr) {}
386
469
 
387
470
  simd16float32 accu16;
388
471
 
@@ -405,20 +488,43 @@ struct SimilarityL2<SIMDLevel::AVX512> {
405
488
  accu16 = accu16 + tmp * tmp;
406
489
  }
407
490
 
491
+ FAISS_ALWAYS_INLINE void fmadd_16_components_2(
492
+ simd16float32 x,
493
+ simd16float32 y_2) {
494
+ simd16float32 tmp = y_2 - x;
495
+ accu16 = fmadd(tmp, tmp, accu16);
496
+ }
497
+
408
498
  FAISS_ALWAYS_INLINE float result_16() {
409
499
  return horizontal_add(accu16);
410
500
  }
501
+
502
+ static void adjust_query_for_raw_decode(
503
+ const float* x,
504
+ float* q_adj,
505
+ size_t d,
506
+ float vmin,
507
+ float vdiff,
508
+ float& scale_factor,
509
+ float& bias) {
510
+ float inv_vdiff = (vdiff != 0) ? 1.0f / vdiff : 0.0f;
511
+ for (size_t i = 0; i < d; i++) {
512
+ q_adj[i] = (x[i] - vmin) * inv_vdiff;
513
+ }
514
+ scale_factor = vdiff * vdiff;
515
+ bias = 0;
516
+ }
411
517
  };
412
518
 
413
519
  template <>
414
- struct SimilarityIP<SIMDLevel::AVX512> {
520
+ struct SimilarityIP<THE_LEVEL_TO_DISPATCH> {
415
521
  static constexpr int simdwidth = 16;
416
- static constexpr SIMDLevel simd_level = SIMDLevel::AVX512;
522
+ static constexpr SIMDLevel simd_level = THE_LEVEL_TO_DISPATCH;
417
523
  static constexpr MetricType metric_type = METRIC_INNER_PRODUCT;
418
524
 
419
525
  const float *y, *yi;
420
526
 
421
- explicit SimilarityIP(const float* y) : y(y), yi(nullptr) {}
527
+ explicit SimilarityIP(const float* y_in) : y(y_in), yi(nullptr) {}
422
528
 
423
529
  simd16float32 accu16;
424
530
 
@@ -439,32 +545,144 @@ struct SimilarityIP<SIMDLevel::AVX512> {
439
545
  accu16 = accu16 + x1 * x2;
440
546
  }
441
547
 
548
+ FAISS_ALWAYS_INLINE void fmadd_16_components_2(
549
+ simd16float32 x1,
550
+ simd16float32 x2) {
551
+ accu16 = fmadd(x1, x2, accu16);
552
+ }
553
+
442
554
  FAISS_ALWAYS_INLINE float result_16() {
443
555
  return horizontal_add(accu16);
444
556
  }
557
+
558
+ static void adjust_query_for_raw_decode(
559
+ const float* x,
560
+ float* q_adj,
561
+ size_t d,
562
+ float vmin,
563
+ float vdiff,
564
+ float& scale_factor,
565
+ float& bias) {
566
+ float sum_q = 0;
567
+ for (size_t i = 0; i < d; i++) {
568
+ q_adj[i] = x[i];
569
+ sum_q += x[i];
570
+ }
571
+ scale_factor = vdiff;
572
+ bias = vmin * sum_q;
573
+ }
445
574
  };
446
575
 
447
576
  /**********************************************************
448
577
  * Distance Computers
449
578
  **********************************************************/
450
579
 
451
- template <class Quantizer, class Similarity>
452
- struct DCTemplate<Quantizer, Similarity, SIMDLevel::AVX512>
580
+ namespace {
581
+
582
+ /// 16 lanes to 4: lanes j, j + 4, j + 8 and j + 12 summed into lane j.
583
+ FAISS_ALWAYS_INLINE __m128 reduce_16_to_4(__m512 a) {
584
+ const __m256 hi =
585
+ _mm256_castpd_ps(_mm512_extractf64x4_pd(_mm512_castps_pd(a), 1));
586
+ const __m256 s8 = _mm256_add_ps(_mm512_castps512_ps256(a), hi);
587
+ return _mm_add_ps(_mm256_castps256_ps128(s8), _mm256_extractf128_ps(s8, 1));
588
+ }
589
+
590
+ } // namespace
591
+
592
+ template <class Quantizer, class Similarity, bool four_acc>
593
+ struct DCTemplate<Quantizer, Similarity, THE_LEVEL_TO_DISPATCH, four_acc>
453
594
  : SQDistanceComputer {
454
595
  using Sim = Similarity;
455
596
 
456
597
  Quantizer quant;
457
598
 
599
+ // Pre-adjusted query buffer for uniform quantizers
600
+ std::vector<float> q_adj;
601
+ float scale_factor = 0;
602
+ float bias = 0;
603
+
604
+ static constexpr bool has_decode_raw() {
605
+ return requires(const Quantizer& q, const uint8_t* c, int i) {
606
+ { q.decode_16_raw(c, i) };
607
+ };
608
+ }
609
+
458
610
  DCTemplate(size_t d, const std::vector<float>& trained)
459
- : quant(d, trained) {}
611
+ : quant(d, trained) {
612
+ if constexpr (has_decode_raw()) {
613
+ q_adj.resize(d);
614
+ }
615
+ }
616
+
617
+ /// The quantizer can decode a partial block, so d need not be a
618
+ /// multiple of 16.
619
+ static constexpr bool has_masked_tail() {
620
+ return requires(const Quantizer& q, const uint8_t* c) {
621
+ { q.reconstruct_16_components_masked(c, 0, __mmask16(0)) };
622
+ };
623
+ }
624
+
625
+ /// Adds the partial last block, from component i, of a d that is not a
626
+ /// multiple of 16.
627
+ FAISS_ALWAYS_INLINE void add_tail(
628
+ Similarity& sim,
629
+ const float* x,
630
+ const uint8_t* code,
631
+ size_t i) const {
632
+ if constexpr (has_masked_tail()) {
633
+ if (i < quant.d) {
634
+ const __mmask16 mask = __mmask16((1u << (quant.d - i)) - 1);
635
+ sim.fmadd_16_components_2(
636
+ quant.reconstruct_16_components_masked(
637
+ code, static_cast<int>(i), mask),
638
+ simd16float32(_mm512_maskz_loadu_ps(mask, x + i)));
639
+ }
640
+ }
641
+ }
642
+
643
+ /// Similarity terms of one code with four accumulators.
644
+ FAISS_ALWAYS_INLINE __m512
645
+ accumulate_4(const float* x, const uint8_t* code) const {
646
+ struct Acc : Similarity {
647
+ Acc() : Similarity(nullptr) {}
648
+ };
649
+ Acc s[4];
650
+ for (int j = 0; j < 4; j++) {
651
+ s[j].begin_16();
652
+ }
653
+ const size_t d = quant.d;
654
+ size_t i = 0;
655
+ for (; i + 64 <= d; i += 64) {
656
+ for (int j = 0; j < 4; j++) {
657
+ s[j].fmadd_16_components_2(
658
+ quant.reconstruct_16_components(
659
+ code, static_cast<int>(i + 16 * j)),
660
+ simd16float32(x + i + 16 * j));
661
+ }
662
+ }
663
+ for (; i + 16 <= d; i += 16) {
664
+ s[0].fmadd_16_components_2(
665
+ quant.reconstruct_16_components(code, static_cast<int>(i)),
666
+ simd16float32(x + i));
667
+ }
668
+ add_tail(s[0], x, code, i);
669
+ return _mm512_add_ps(
670
+ _mm512_add_ps(s[0].accu16.f, s[1].accu16.f),
671
+ _mm512_add_ps(s[2].accu16.f, s[3].accu16.f));
672
+ }
460
673
 
461
674
  float compute_distance(const float* x, const uint8_t* code) const {
675
+ if constexpr (four_acc) {
676
+ return reduce_4(reduce_16_to_4(accumulate_4(x, code)));
677
+ }
462
678
  Similarity sim(x);
463
679
  sim.begin_16();
464
- for (size_t i = 0; i < quant.d; i += 16) {
680
+ const size_t d16 = quant.d & ~size_t(15);
681
+ for (size_t i = 0; i < d16; i += 16) {
465
682
  simd16float32 xi = quant.reconstruct_16_components(code, i);
466
683
  sim.add_16_components(xi);
467
684
  }
685
+ add_tail(sim, x, code, d16);
468
686
  return sim.result_16();
469
687
  }
470
688
 
@@ -472,16 +690,46 @@ struct DCTemplate<Quantizer, Similarity, SIMDLevel::AVX512>
472
690
  const {
473
691
  Similarity sim(nullptr);
474
692
  sim.begin_16();
475
- for (size_t i = 0; i < quant.d; i += 16) {
693
+ const size_t d16 = quant.d & ~size_t(15);
694
+ for (size_t i = 0; i < d16; i += 16) {
476
695
  simd16float32 x1 = quant.reconstruct_16_components(code1, i);
477
696
  simd16float32 x2 = quant.reconstruct_16_components(code2, i);
478
697
  sim.add_16_components_2(x1, x2);
479
698
  }
699
+ if constexpr (has_masked_tail()) {
700
+ if (d16 < quant.d) {
701
+ const __mmask16 mask = __mmask16((1u << (quant.d - d16)) - 1);
702
+ const int i = static_cast<int>(d16);
703
+ sim.add_16_components_2(
704
+ quant.reconstruct_16_components_masked(code1, i, mask),
705
+ quant.reconstruct_16_components_masked(code2, i, mask));
706
+ }
707
+ }
480
708
  return sim.result_16();
481
709
  }
482
710
 
483
711
  void set_query(const float* x) final {
484
712
  q = x;
713
+ if constexpr (has_decode_raw()) {
714
+ Sim::adjust_query_for_raw_decode(
715
+ x,
716
+ q_adj.data(),
717
+ quant.d,
718
+ quant.vmin,
719
+ quant.vdiff,
720
+ scale_factor,
721
+ bias);
722
+ }
723
+ }
724
+
725
+ float query_to_code_predecoded(const uint8_t* code) const {
726
+ Similarity sim(q_adj.data());
727
+ sim.begin_16();
728
+ for (size_t i = 0; i < quant.d; i += 16) {
729
+ simd16float32 xi = quant.decode_16_raw(code, i);
730
+ sim.add_16_components(xi);
731
+ }
732
+ return bias + scale_factor * sim.result_16();
485
733
  }
486
734
 
487
735
  float symmetric_dis(idx_t i, idx_t j) override {
@@ -490,64 +738,197 @@ struct DCTemplate<Quantizer, Similarity, SIMDLevel::AVX512>
490
738
  }
491
739
 
492
740
  float query_to_code(const uint8_t* code) const final {
493
- return compute_distance(q, code);
741
+ if constexpr (has_decode_raw()) {
742
+ return query_to_code_predecoded(code);
743
+ } else {
744
+ return compute_distance(q, code);
745
+ }
494
746
  }
495
- };
496
747
 
497
- template <class Similarity>
498
- struct DistanceComputerByte<Similarity, SIMDLevel::AVX512>
499
- : SQDistanceComputer {
500
- using Sim = Similarity;
748
+ void query_to_codes_batch_4(
749
+ const uint8_t* code_0,
750
+ const uint8_t* code_1,
751
+ const uint8_t* code_2,
752
+ const uint8_t* code_3,
753
+ float& dis0,
754
+ float& dis1,
755
+ float& dis2,
756
+ float& dis3) const final {
757
+ if constexpr (four_acc) {
758
+ alignas(16) float dis[4];
759
+ _mm_store_ps(
760
+ dis,
761
+ reduce_4x4(
762
+ reduce_16_to_4(accumulate_4(q, code_0)),
763
+ reduce_16_to_4(accumulate_4(q, code_1)),
764
+ reduce_16_to_4(accumulate_4(q, code_2)),
765
+ reduce_16_to_4(accumulate_4(q, code_3))));
766
+ dis0 = dis[0];
767
+ dis1 = dis[1];
768
+ dis2 = dis[2];
769
+ dis3 = dis[3];
770
+ return;
771
+ }
772
+ Similarity sim0(q);
773
+ Similarity sim1(q);
774
+ Similarity sim2(q);
775
+ Similarity sim3(q);
776
+
777
+ sim0.begin_16();
778
+ sim1.begin_16();
779
+ sim2.begin_16();
780
+ sim3.begin_16();
781
+
782
+ const size_t d16 = quant.d & ~size_t(15);
783
+ for (size_t i = 0; i < d16; i += 16) {
784
+ simd16float32 xi0 = quant.reconstruct_16_components(code_0, i);
785
+ simd16float32 xi1 = quant.reconstruct_16_components(code_1, i);
786
+ simd16float32 xi2 = quant.reconstruct_16_components(code_2, i);
787
+ simd16float32 xi3 = quant.reconstruct_16_components(code_3, i);
788
+ sim0.add_16_components(xi0);
789
+ sim1.add_16_components(xi1);
790
+ sim2.add_16_components(xi2);
791
+ sim3.add_16_components(xi3);
792
+ }
793
+ add_tail(sim0, q, code_0, d16);
794
+ add_tail(sim1, q, code_1, d16);
795
+ add_tail(sim2, q, code_2, d16);
796
+ add_tail(sim3, q, code_3, d16);
501
797
 
502
- int d;
503
- std::vector<uint8_t> tmp;
798
+ dis0 = sim0.result_16();
799
+ dis1 = sim1.result_16();
800
+ dis2 = sim2.result_16();
801
+ dis3 = sim3.result_16();
802
+ }
803
+ };
504
804
 
505
- DistanceComputerByte(int d, const std::vector<float>&) : d(d), tmp(d) {}
805
+ // QT_8bit + IP: the precomputed form of the AVX2 specialization, 16 dims per
806
+ // convert and FMA.
807
+ template <bool four_acc>
808
+ struct DCTemplate<
809
+ QuantizerTemplate<
810
+ Codec8bit<THE_LEVEL_TO_DISPATCH>,
811
+ QuantizerTemplateScaling::NON_UNIFORM,
812
+ THE_LEVEL_TO_DISPATCH>,
813
+ SimilarityIP<THE_LEVEL_TO_DISPATCH>,
814
+ THE_LEVEL_TO_DISPATCH,
815
+ four_acc> : SQDistanceComputer {
816
+ using Quantizer = QuantizerTemplate<
817
+ Codec8bit<THE_LEVEL_TO_DISPATCH>,
818
+ QuantizerTemplateScaling::NON_UNIFORM,
819
+ THE_LEVEL_TO_DISPATCH>;
820
+ using Sim = SimilarityIP<THE_LEVEL_TO_DISPATCH>;
506
821
 
507
- int compute_code_distance(const uint8_t* code1, const uint8_t* code2)
508
- const {
509
- // compute 16 lanes of 32-bit products (16-bytes) at once for
510
- // the supported metrics
511
- __m512i accu = _mm512_setzero_si512();
512
- constexpr int kLanes = 16;
513
- for (int i = 0; i < d; i += kLanes) {
514
- __m128i c1 = _mm_loadu_si128((__m128i*)(code1 + i));
515
- __m128i c2 = _mm_loadu_si128((__m128i*)(code2 + i));
516
- __m512i c1i = _mm512_cvtepu8_epi32(c1);
517
- __m512i c2i = _mm512_cvtepu8_epi32(c2);
518
-
519
- __m512i v;
520
- if (Sim::metric_type == METRIC_INNER_PRODUCT) {
521
- v = _mm512_mullo_epi32(c1i, c2i);
522
- } else {
523
- __m512i diff = _mm512_sub_epi32(c1i, c2i);
524
- v = _mm512_mullo_epi32(diff, diff);
525
- }
526
- accu = _mm512_add_epi32(accu, v);
527
- }
528
- return _mm512_reduce_add_epi32(accu);
822
+ Quantizer quant;
823
+ std::vector<float> a, rmin, qa;
824
+ float bias = 0;
825
+
826
+ DCTemplate(size_t d, const std::vector<float>& trained)
827
+ : quant(d, trained), a(d), rmin(d), qa(d) {
828
+ sq8::make_coefficients(trained.data(), d, a.data(), rmin.data());
529
829
  }
530
830
 
531
831
  void set_query(const float* x) final {
532
- for (int i = 0; i < d; i++) {
533
- tmp[i] = int(x[i]);
832
+ q = x;
833
+ bias = 0;
834
+ for (size_t i = 0; i < quant.d; i++) {
835
+ qa[i] = x[i] * a[i];
836
+ bias += x[i] * rmin[i];
534
837
  }
535
838
  }
536
839
 
537
- int compute_distance(const float* x, const uint8_t* code) {
538
- set_query(x);
539
- return compute_code_distance(tmp.data(), code);
840
+ static FAISS_ALWAYS_INLINE __m512 codes_16(const uint8_t* code) {
841
+ return _mm512_cvtepi32_ps(
842
+ _mm512_cvtepu8_epi32(_mm_loadu_si128((const __m128i*)code)));
540
843
  }
541
844
 
542
- float symmetric_dis(idx_t i, idx_t j) override {
543
- return compute_code_distance(
544
- codes + i * code_size, codes + j * code_size);
845
+ /// sum_i qa[i] * code[i] of N codes, in 4 lanes each. The N codes share
846
+ /// each qa load. With four_acc, dims i + 16 * j of each 64 go to
847
+ /// accumulator j of the code, so a code sums the same way for any N.
848
+ template <size_t N>
849
+ FAISS_ALWAYS_INLINE void accumulate(
850
+ const uint8_t* const* code_ptrs,
851
+ __m128* sums) const {
852
+ constexpr size_t n_acc = four_acc ? 4 : 1;
853
+ __m512 acc[N][n_acc];
854
+ for (size_t n = 0; n < N; n++) {
855
+ for (size_t j = 0; j < n_acc; j++) {
856
+ acc[n][j] = _mm512_setzero_ps();
857
+ }
858
+ }
859
+ const size_t d = quant.d;
860
+ size_t i = 0;
861
+ if constexpr (four_acc) {
862
+ for (; i + 64 <= d; i += 64) {
863
+ for (size_t j = 0; j < 4; j++) {
864
+ const __m512 qa16 = _mm512_loadu_ps(qa.data() + i + 16 * j);
865
+ for (size_t n = 0; n < N; n++) {
866
+ acc[n][j] = _mm512_fmadd_ps(
867
+ qa16,
868
+ codes_16(code_ptrs[n] + i + 16 * j),
869
+ acc[n][j]);
870
+ }
871
+ }
872
+ }
873
+ }
874
+ for (; i < d; i += 16) {
875
+ const __m512 qa16 = _mm512_loadu_ps(qa.data() + i);
876
+ for (size_t n = 0; n < N; n++) {
877
+ acc[n][0] = _mm512_fmadd_ps(
878
+ qa16, codes_16(code_ptrs[n] + i), acc[n][0]);
879
+ }
880
+ }
881
+ for (size_t n = 0; n < N; n++) {
882
+ __m512 sum = acc[n][0];
883
+ if constexpr (four_acc) {
884
+ sum = _mm512_add_ps(
885
+ _mm512_add_ps(acc[n][0], acc[n][1]),
886
+ _mm512_add_ps(acc[n][2], acc[n][3]));
887
+ }
888
+ sums[n] = reduce_16_to_4(sum);
889
+ }
545
890
  }
546
891
 
547
892
  float query_to_code(const uint8_t* code) const final {
548
- return compute_code_distance(tmp.data(), code);
893
+ __m128 sum;
894
+ accumulate<1>(&code, &sum);
895
+ return bias + reduce_4(sum);
896
+ }
897
+
898
+ void query_to_codes_batch_4(
899
+ const uint8_t* code_0,
900
+ const uint8_t* code_1,
901
+ const uint8_t* code_2,
902
+ const uint8_t* code_3,
903
+ float& dis0,
904
+ float& dis1,
905
+ float& dis2,
906
+ float& dis3) const final {
907
+ const uint8_t* code_ptrs[4] = {code_0, code_1, code_2, code_3};
908
+ __m128 sums[4];
909
+ accumulate<4>(code_ptrs, sums);
910
+ alignas(16) float dis[4];
911
+ _mm_store_ps(dis, reduce_4x4(sums[0], sums[1], sums[2], sums[3]));
912
+ dis0 = bias + dis[0];
913
+ dis1 = bias + dis[1];
914
+ dis2 = bias + dis[2];
915
+ dis3 = bias + dis[3];
916
+ }
917
+
918
+ float symmetric_dis(idx_t i, idx_t j) override {
919
+ const uint8_t* c1 = codes + i * code_size;
920
+ const uint8_t* c2 = codes + j * code_size;
921
+ Sim sim(nullptr);
922
+ sim.begin_16();
923
+ for (size_t k = 0; k < quant.d; k += 16) {
924
+ simd16float32 x1 = quant.reconstruct_16_components(c1, k);
925
+ simd16float32 x2 = quant.reconstruct_16_components(c2, k);
926
+ sim.add_16_components_2(x1, x2);
927
+ }
928
+ return sim.result_16();
549
929
  }
550
930
  };
551
931
 
552
932
  } // namespace scalar_quantizer
933
+
553
934
  } // namespace faiss