faiss 0.6.3 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (255) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/ext/faiss/extconf.rb +1 -1
  4. data/lib/faiss/version.rb +1 -1
  5. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  6. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  7. data/vendor/faiss/faiss/Clustering.h +37 -0
  8. data/vendor/faiss/faiss/IVFlib.cpp +99 -0
  9. data/vendor/faiss/faiss/IVFlib.h +15 -0
  10. data/vendor/faiss/faiss/Index.cpp +44 -0
  11. data/vendor/faiss/faiss/Index.h +18 -13
  12. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  13. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  14. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +23 -2
  15. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +94 -144
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.h +18 -5
  17. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -14
  18. data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +33 -17
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +64 -4
  22. data/vendor/faiss/faiss/IndexFlat.h +33 -2
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +755 -99
  25. data/vendor/faiss/faiss/IndexHNSW.h +69 -7
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +43 -6
  27. data/vendor/faiss/faiss/IndexIDMap.h +7 -0
  28. data/vendor/faiss/faiss/IndexIVF.cpp +297 -32
  29. data/vendor/faiss/faiss/IndexIVF.h +53 -1
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +46 -14
  31. data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +16 -0
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +9 -1
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -0
  37. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  38. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +50 -14
  39. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -2
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +855 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +0 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +6 -3
  46. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +10 -4
  47. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  48. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  49. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  50. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  51. data/vendor/faiss/faiss/IndexShards.cpp +13 -3
  52. data/vendor/faiss/faiss/IndexShards.h +5 -0
  53. data/vendor/faiss/faiss/IndexShardsIVF.cpp +4 -3
  54. data/vendor/faiss/faiss/SuperKMeans.cpp +87 -22
  55. data/vendor/faiss/faiss/SuperKMeans.h +12 -6
  56. data/vendor/faiss/faiss/VectorTransform.cpp +40 -1
  57. data/vendor/faiss/faiss/VectorTransform.h +3 -0
  58. data/vendor/faiss/faiss/clone_index.cpp +27 -0
  59. data/vendor/faiss/faiss/gpu/Dataset.h +112 -0
  60. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  61. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  62. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +4 -5
  63. data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
  64. data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -18
  65. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +26 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +109 -63
  67. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  68. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  69. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  70. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  71. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  72. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  73. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  74. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +11 -0
  75. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +92 -0
  76. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +4 -0
  77. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  78. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +38 -0
  79. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +106 -0
  80. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +10 -0
  81. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
  82. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -0
  83. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  84. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  85. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  86. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +287 -134
  87. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  88. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  89. data/vendor/faiss/faiss/impl/HNSW.cpp +465 -125
  90. data/vendor/faiss/faiss/impl/HNSW.h +118 -7
  91. data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
  92. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  93. data/vendor/faiss/faiss/impl/NNDescent.cpp +12 -4
  94. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  95. data/vendor/faiss/faiss/impl/Panorama.h +14 -4
  96. data/vendor/faiss/faiss/impl/PiPNN.cpp +822 -0
  97. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  98. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +58 -15
  99. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +1 -1
  100. data/vendor/faiss/faiss/impl/RaBitQUtils.h +1 -1
  101. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +150 -60
  102. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +62 -4
  103. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +2 -2
  104. data/vendor/faiss/faiss/impl/ResultHandler.h +9 -3
  105. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +61 -30
  106. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +7 -16
  107. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  108. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  109. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  110. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +89 -13
  111. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  112. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  113. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  114. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  115. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -1
  116. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  117. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  118. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  119. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +18 -129
  120. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  121. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +5 -0
  122. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +0 -4
  123. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +0 -4
  124. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +0 -88
  125. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  127. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +22 -1
  128. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -2
  129. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +41 -23
  130. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/index_read.cpp +302 -29
  133. data/vendor/faiss/faiss/impl/index_write.cpp +126 -26
  134. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  135. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  136. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  137. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  138. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  139. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  140. data/vendor/faiss/faiss/impl/platform_macros.h +45 -0
  141. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  142. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +152 -43
  143. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  144. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  145. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  146. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  147. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  148. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  149. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +23 -4
  150. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  151. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  152. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  153. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  154. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  155. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  156. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  157. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +0 -4
  158. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +0 -4
  159. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  160. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  161. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +6 -1
  162. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +7 -3
  163. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  164. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +59 -11
  165. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +232 -79
  166. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +482 -103
  167. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +2 -149
  168. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +3 -698
  169. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +101 -72
  170. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +203 -99
  171. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  172. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  173. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  174. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  175. data/vendor/faiss/faiss/impl/simd_dispatch.h +47 -22
  176. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  177. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  178. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  179. data/vendor/faiss/faiss/index_factory.cpp +84 -1
  180. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +10 -5
  181. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +10 -4
  182. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +16 -4
  183. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +11 -3
  184. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
  185. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
  186. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +9 -2
  187. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
  188. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  189. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  190. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  191. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  192. data/vendor/faiss/faiss/utils/distances.cpp +246 -28
  193. data/vendor/faiss/faiss/utils/distances.h +34 -0
  194. data/vendor/faiss/faiss/utils/distances_dispatch.h +36 -28
  195. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  196. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -15
  197. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  198. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  199. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  200. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  201. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  202. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  203. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  204. data/vendor/faiss/faiss/utils/hamming.cpp +15 -9
  205. data/vendor/faiss/faiss/utils/hamming.h +10 -1
  206. data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
  207. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  208. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  209. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +20 -0
  210. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +34 -6
  211. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +217 -0
  212. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  213. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +143 -1
  214. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  215. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  216. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  217. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  218. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  219. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  220. data/vendor/faiss/faiss/utils/rabitq_simd.h +51 -16
  221. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  222. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +231 -35
  223. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +64 -5
  224. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +64 -5
  225. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +428 -108
  226. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  227. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +93 -0
  228. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  229. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  230. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  231. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  232. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  233. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  234. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  235. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  236. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  237. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +54 -15
  238. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +89 -55
  239. data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +70 -36
  240. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  241. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  242. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +2 -7
  243. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
  244. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +30 -0
  247. data/vendor/faiss/faiss/utils/simd_levels.cpp +191 -70
  248. data/vendor/faiss/faiss/utils/simd_levels.h +100 -16
  249. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  250. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  251. data/vendor/faiss/faiss/utils/utils.cpp +80 -0
  252. data/vendor/faiss/faiss/utils/utils.h +24 -0
  253. metadata +38 -5
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_spr.h +0 -171
@@ -60,6 +60,19 @@ void round_tab(const float* tab, size_t n, float a, float bi, T* tab_out) {
60
60
 
61
61
  } // anonymous namespace
62
62
 
63
+ float fastscan_lut_scale(
64
+ float max_column_span,
65
+ float max_sum_span,
66
+ size_t n_rounded_terms) {
67
+ const float headroom = 0.5f * float(n_rounded_terms);
68
+ const float sum_budget = 65535.0f - headroom;
69
+ const float a = std::min(
70
+ max_column_span > 0 ? 255.0f / max_column_span : HUGE_VALF,
71
+ (max_sum_span > 0 && sum_budget > 0) ? sum_budget / max_sum_span
72
+ : HUGE_VALF);
73
+ return std::isfinite(a) ? a : 0.0f;
74
+ }
75
+
63
76
  void round_uint8_per_column(
64
77
  float* tab,
65
78
  size_t n,
@@ -67,6 +80,7 @@ void round_uint8_per_column(
67
80
  float* a_out,
68
81
  float* b_out) {
69
82
  float max_span = 0;
83
+ float total_span = 0;
70
84
  std::vector<float> mins(n);
71
85
  for (size_t i = 0; i < n; i++) {
72
86
  mins[i] = tab_min(tab + i * d, d);
@@ -74,8 +88,9 @@ void round_uint8_per_column(
74
88
  if (span > max_span) {
75
89
  max_span = span;
76
90
  }
91
+ total_span += span;
77
92
  }
78
- float a = max_span > 0 ? 255.0f / max_span : 0.0f;
93
+ float a = fastscan_lut_scale(max_span, total_span, n);
79
94
  float b = 0;
80
95
  for (size_t i = 0; i < n; i++) {
81
96
  b += mins[i];
@@ -97,6 +112,7 @@ void round_uint8_per_column_multi(
97
112
  float* a_out,
98
113
  float* b_out) {
99
114
  float max_span = 0;
115
+ float total_span = 0;
100
116
  std::vector<float> mins(n);
101
117
  for (size_t i = 0; i < n; i++) {
102
118
  float min_i = HUGE_VAL;
@@ -110,8 +126,9 @@ void round_uint8_per_column_multi(
110
126
  if (span > max_span) {
111
127
  max_span = span;
112
128
  }
129
+ total_span += span;
113
130
  }
114
- float a = max_span > 0 ? 255.0f / max_span : 0.0f;
131
+ float a = fastscan_lut_scale(max_span, total_span, n);
115
132
  float b = 0;
116
133
  for (size_t i = 0; i < n; i++) {
117
134
  b += mins[i];
@@ -155,12 +172,7 @@ void quantize_LUT_and_bias(
155
172
  max_span_dis += span;
156
173
  b += mins[i];
157
174
  }
158
- a = std::min(
159
- max_span_LUT > 0 ? 255.0f / max_span_LUT : HUGE_VALF,
160
- max_span_dis > 0 ? 65535.0f / max_span_dis : HUGE_VALF);
161
- if (!std::isfinite(a)) {
162
- a = 0.0f;
163
- }
175
+ a = fastscan_lut_scale(max_span_LUT, max_span_dis, M);
164
176
 
165
177
  for (size_t i = 0; i < M; i++) {
166
178
  round_tab(LUT + i * ksub, ksub, a, mins[i], LUTq + i * ksub);
@@ -180,12 +192,7 @@ void quantize_LUT_and_bias(
180
192
  max_span_dis += span;
181
193
  b += mins[i];
182
194
  }
183
- a = std::min(
184
- max_span_LUT > 0 ? 255.0f / max_span_LUT : HUGE_VALF,
185
- max_span_dis > 0 ? 65535.0f / max_span_dis : HUGE_VALF);
186
- if (!std::isfinite(a)) {
187
- a = 0.0f;
188
- }
195
+ a = fastscan_lut_scale(max_span_LUT, max_span_dis, M);
189
196
  b += bias_min;
190
197
 
191
198
  for (size_t i = 0; i < M; i++) {
@@ -219,12 +226,7 @@ void quantize_LUT_and_bias(
219
226
  b = std::min(b, b2j);
220
227
  }
221
228
 
222
- a = std::min(
223
- max_span_LUT > 0 ? 255.0f / max_span_LUT : HUGE_VALF,
224
- max_span_dis > 0 ? 65535.0f / max_span_dis : HUGE_VALF);
225
- if (!std::isfinite(a)) {
226
- a = 0.0f;
227
- }
229
+ a = fastscan_lut_scale(max_span_LUT, max_span_dis, M);
228
230
 
229
231
  ij = 0;
230
232
  size_t ij_2 = 0;
@@ -266,13 +268,15 @@ void quantize_LUT_and_bias(
266
268
  }
267
269
 
268
270
  float max_span = -HUGE_VAL;
271
+ float total_span = 0;
269
272
  b = 0;
270
273
  for (size_t i = 0; i < M; i++) {
271
274
  float span = maxs[i] - mins[i];
272
275
  max_span = std::max(max_span, span);
276
+ total_span += span;
273
277
  b += mins[i];
274
278
  }
275
- a = max_span > 0 ? 255.0f / max_span : 0.0f;
279
+ a = fastscan_lut_scale(max_span, total_span, M);
276
280
  ij = 0;
277
281
  size_t ij_2 = 0;
278
282
  for (size_t j = 0; j < nprobe; j++) {
@@ -321,12 +325,7 @@ void aq_quantize_LUT_and_bias(
321
325
  max_span_dis += (i >= M - M_norm ? span * norm_scale : span);
322
326
  b += mins[i];
323
327
  }
324
- a = std::min(
325
- max_span_LUT > 0 ? 255.0f / max_span_LUT : HUGE_VALF,
326
- max_span_dis > 0 ? 65535.0f / max_span_dis : HUGE_VALF);
327
- if (!std::isfinite(a)) {
328
- a = 0.0f;
329
- }
328
+ a = fastscan_lut_scale(max_span_LUT, max_span_dis, M);
330
329
  b += bias_min;
331
330
 
332
331
  for (size_t i = 0; i < M; i++) {
@@ -7,6 +7,8 @@
7
7
 
8
8
  #pragma once
9
9
 
10
+ #include <cstddef>
11
+
10
12
  #include <cstdint>
11
13
  #include <cstdio>
12
14
 
@@ -25,6 +27,15 @@ namespace faiss {
25
27
 
26
28
  namespace quantize_lut {
27
29
 
30
+ /** Scale for a fast scan LUT, bounding both the uint8 entry and the uint16
31
+ * sum the kernel accumulates. `n_rounded_terms` is how many rounded values
32
+ * land in one accumulator: the columns, plus one for a rounded bias term.
33
+ */
34
+ float fastscan_lut_scale(
35
+ float max_column_span,
36
+ float max_sum_span,
37
+ size_t n_rounded_terms);
38
+
28
39
  /* affine quantizer, a and b are the affine coefficients, marginalize over d
29
40
  *
30
41
  * @param tab input/output, size (n, d)
@@ -53,6 +53,28 @@ BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount(
53
53
  size_t size,
54
54
  size_t qb);
55
55
 
56
+ /// Four independent AND-dot-product/popcount pairs for a four-plane query.
57
+ /// Query contains 4 * size bytes; each data pointer contains `size` bytes.
58
+ /// Byte-aligned buffers and arbitrary byte counts are supported.
59
+ template <SIMDLevel SL = SINGLE_SIMD_LEVEL>
60
+ inline void bitwise_q4_batch_4(
61
+ const uint8_t* query,
62
+ const uint8_t* const* data,
63
+ size_t size,
64
+ BitwiseAndDotProductResult* results) {
65
+ for (size_t i = 0; i < 4; ++i) {
66
+ results[i] = bitwise_and_dot_product_with_popcount<SL>(
67
+ query, data[i], size, 4);
68
+ }
69
+ }
70
+
71
+ template <>
72
+ void bitwise_q4_batch_4<SIMDLevel::AVX512_VPOPCNT>(
73
+ const uint8_t* query,
74
+ const uint8_t* const* data,
75
+ size_t size,
76
+ BitwiseAndDotProductResult* results);
77
+
56
78
  /**
57
79
  * Compute dot product between query and binary data using popcount on XOR.
58
80
  *
@@ -140,9 +162,11 @@ inline uint64_t bitwise_and_dot_product<SIMDLevel::NONE>(
140
162
  uint64_t sum = 0;
141
163
  size_t offset = 0;
142
164
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
143
- const auto yv = *(const uint64_t*)(data + offset);
165
+ uint64_t yv;
166
+ std::memcpy(&yv, data + offset, sizeof(yv));
144
167
  for (int j = 0; j < qb; j++) {
145
- const auto qv = *(const uint64_t*)(query + j * size + offset);
168
+ uint64_t qv;
169
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
146
170
  sum += popcount64(qv & yv) << j;
147
171
  }
148
172
  }
@@ -167,10 +191,12 @@ inline BitwiseAndDotProductResult bitwise_and_dot_product_with_popcount<
167
191
  uint64_t popcount_sum = 0;
168
192
  size_t offset = 0;
169
193
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
170
- const auto yv = *(const uint64_t*)(data + offset);
194
+ uint64_t yv;
195
+ std::memcpy(&yv, data + offset, sizeof(yv));
171
196
  popcount_sum += popcount64(yv);
172
197
  for (int j = 0; j < qb; j++) {
173
- const auto qv = *(const uint64_t*)(query + j * size + offset);
198
+ uint64_t qv;
199
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
174
200
  dot_product += popcount64(qv & yv) << j;
175
201
  }
176
202
  }
@@ -194,9 +220,11 @@ inline uint64_t bitwise_xor_dot_product<SIMDLevel::NONE>(
194
220
  uint64_t sum = 0;
195
221
  size_t offset = 0;
196
222
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
197
- const auto yv = *(const uint64_t*)(data + offset);
223
+ uint64_t yv;
224
+ std::memcpy(&yv, data + offset, sizeof(yv));
198
225
  for (int j = 0; j < qb; j++) {
199
- const auto qv = *(const uint64_t*)(query + j * size + offset);
226
+ uint64_t qv;
227
+ std::memcpy(&qv, query + j * size + offset, sizeof(qv));
200
228
  sum += popcount64(qv ^ yv) << j;
201
229
  }
202
230
  }
@@ -215,7 +243,8 @@ inline uint64_t popcount<SIMDLevel::NONE>(const uint8_t* data, size_t size) {
215
243
  uint64_t sum = 0;
216
244
  size_t offset = 0;
217
245
  for (size_t step = 64 / 8; offset + step <= size; offset += step) {
218
- const auto yv = *(const uint64_t*)(data + offset);
246
+ uint64_t yv;
247
+ std::memcpy(&yv, data + offset, sizeof(yv));
219
248
  sum += popcount64(yv);
220
249
  }
221
250
  for (; offset < size; ++offset) {
@@ -332,16 +361,12 @@ inline void quantize_query_values<SIMDLevel::NONE>(
332
361
  /*********************************************************
333
362
  * Multi-bit RaBitQ inner product kernels.
334
363
  *
335
- * Compute: sum_i rotated_q[i] * ((sign_bit_i << ex_bits) + ex_code_val_i + cb)
364
+ * Score = sum_i rotated_q[i] * ((sign_i << ex_bits) + ex_i + cb)
336
365
  *
337
- * Strategy:
338
- * ex_bits == 1: Specialized kernel — both sign_bits and ex_code are
339
- * 1-bit-per-dim packed, enabling direct bit→mask→float
340
- * conversion with zero per-element extraction.
341
- * ex_bits >= 2: Bit-plane decomposition (BMI2 required) — PEXT extracts
342
- * each bit plane in one instruction, then the same
343
- * bit→mask→float kernel computes each plane's dot product.
344
- * Fallback: Scalar extraction via 64-bit window read + shift + mask.
366
+ * ex_bits == 1: bits map straight to floats. No extraction.
367
+ * ex_bits 2..7: BMI2 PEXT extracts each bit plane.
368
+ * ex_bits == 8: whole bytes. No extraction and no BMI2.
369
+ * Fallback: scalar math reads one code at a time.
345
370
  *********************************************************/
346
371
  namespace faiss::rabitq::multibit {
347
372
 
@@ -357,6 +382,16 @@ inline float ip_scalar(
357
382
  size_t ex_bits,
358
383
  float cb) {
359
384
  float result = 0.0f;
385
+ if (ex_bits == 8) {
386
+ // RBQ9 is byte-aligned, including the last dimension. Do not require
387
+ // trailing factor bytes for the scalar reference or SIMD tail.
388
+ for (size_t i = start; i < d; ++i) {
389
+ const int sb = (sign_bits[i / 8] >> (i % 8)) & 1;
390
+ result += rotated_q[i] *
391
+ (static_cast<float>((sb << 8) + ex_code[i]) + cb);
392
+ }
393
+ return result;
394
+ }
360
395
  const int sign_shift = static_cast<int>(ex_bits);
361
396
  const uint64_t code_mask = (1ULL << ex_bits) - 1;
362
397
  for (size_t i = start; i < d; i++) {
@@ -6,8 +6,7 @@
6
6
  */
7
7
 
8
8
  #include <faiss/utils/distances.h>
9
-
10
- #ifdef __aarch64__
9
+ #include <faiss/utils/simd_impl/masked_sum.h>
11
10
 
12
11
  #include <arm_neon.h>
13
12
  #include <limits>
@@ -41,6 +40,14 @@ void fvec_madd<SIMDLevel::ARM_NEON>(
41
40
  c[i] = a[i] + bf * b[i];
42
41
  }
43
42
 
43
+ template <>
44
+ float fvec_masked_sum<SIMDLevel::ARM_NEON>(
45
+ const float* x,
46
+ const uint8_t* bits,
47
+ size_t d) {
48
+ return masked_sum<SIMDLevel::ARM_NEON>(x, bits, d);
49
+ }
50
+
44
51
  template <>
45
52
  void fvec_L2sqr_ny_transposed<SIMDLevel::ARM_NEON>(
46
53
  float* dis,
@@ -150,5 +157,3 @@ int fvec_madd_and_argmin<SIMDLevel::ARM_NEON>(
150
157
  }
151
158
 
152
159
  } // namespace faiss
153
-
154
- #endif // __aarch64__
@@ -129,6 +129,22 @@ struct ElementOpIP {
129
129
  }
130
130
  };
131
131
 
132
+ struct ElementOpL2 {
133
+ static svfloat32_t op(svbool_t pg, svfloat32_t x, svfloat32_t y) {
134
+ const svfloat32_t diff = svsub_f32_x(pg, x, y);
135
+ return svmul_f32_x(pg, diff, diff);
136
+ }
137
+
138
+ static svfloat32_t merge(
139
+ svbool_t pg,
140
+ svfloat32_t z,
141
+ svfloat32_t x,
142
+ svfloat32_t y) {
143
+ const svfloat32_t diff = svsub_f32_x(pg, x, y);
144
+ return svmla_f32_x(pg, z, diff, diff);
145
+ }
146
+ };
147
+
132
148
  template <typename ElementOp>
133
149
  void fvec_op_ny_sve_d1(float* dis, const float* x, const float* y, size_t ny) {
134
150
  const size_t lanes = svcntw();
@@ -513,13 +529,87 @@ void fvec_L2sqr_ny<SIMDLevel::ARM_SVE>(
513
529
  const float* y,
514
530
  size_t d,
515
531
  size_t ny) {
516
- // Use autovectorized L2sqr in a loop
517
- for (size_t i = 0; i < ny; i++) {
518
- dis[i] = fvec_L2sqr<SIMDLevel::ARM_SVE>(x, y, d);
519
- y += d;
532
+ const size_t lanes = static_cast<size_t>(svcntw());
533
+
534
+ switch (d) {
535
+ case 1:
536
+ fvec_op_ny_sve_d1<ElementOpL2>(dis, x, y, ny);
537
+ break;
538
+
539
+ case 2:
540
+ fvec_op_ny_sve_d2<ElementOpL2>(dis, x, y, ny);
541
+ break;
542
+
543
+ case 4:
544
+ fvec_op_ny_sve_d4<ElementOpL2>(dis, x, y, ny);
545
+ break;
546
+
547
+ case 8:
548
+ fvec_op_ny_sve_d8<ElementOpL2>(dis, x, y, ny);
549
+ break;
550
+
551
+ default:
552
+ if (d == lanes)
553
+ fvec_op_ny_sve_lanes1<ElementOpL2>(dis, x, y, ny);
554
+ else if (d == lanes * 2)
555
+ fvec_op_ny_sve_lanes2<ElementOpL2>(dis, x, y, ny);
556
+ else if (d == lanes * 3)
557
+ fvec_op_ny_sve_lanes3<ElementOpL2>(dis, x, y, ny);
558
+ else if (d == lanes * 4)
559
+ fvec_op_ny_sve_lanes4<ElementOpL2>(dis, x, y, ny);
560
+ else {
561
+ // Fallback: use autovectorized L2sqr
562
+ for (size_t i = 0; i < ny; i++) {
563
+ dis[i] = fvec_L2sqr<SIMDLevel::ARM_SVE>(x, y, d);
564
+ y += d;
565
+ }
566
+ }
567
+ break;
520
568
  }
521
569
  }
522
570
 
571
+ namespace {
572
+
573
+ /// Low-dimensional L2sqr nearest (D in {2,4,8}). The data is row-major
574
+ /// (centroid c is y[c*D .. c*D+D]). Each SVE lane tracks one centroid across
575
+ /// batches, keeping the lane-local minimum and index in registers. The scratch
576
+ /// buffer is not written, matching the AVX2/AVX512 D2/D4/D8 implementations.
577
+ template <int D>
578
+ size_t fvec_L2sqr_ny_nearest_lowdim(
579
+ float* /*distances_tmp_buffer*/,
580
+ const float* x,
581
+ const float* y,
582
+ size_t ny) {
583
+ const size_t lanes = svcntw();
584
+ svfloat32_t global_mins = svdup_n_f32(HUGE_VALF);
585
+ svuint32_t global_ids = svdup_n_u32(0);
586
+ svuint32_t current_ids = svindex_u32(0, 1);
587
+
588
+ for (size_t c = 0; c < ny; c += lanes) {
589
+ const svbool_t pg = svwhilelt_b32_u64(c, ny);
590
+ svfloat32_t distances = svdup_n_f32(0.0f);
591
+ for (uint32_t j = 0; j < D; ++j) {
592
+ const svuint32_t offsets = svindex_u32(j, D);
593
+ const svfloat32_t yv =
594
+ svld1_gather_u32index_f32(pg, y + c * D, offsets);
595
+ const svfloat32_t diff = svsub_n_f32_x(pg, yv, x[j]);
596
+ distances = svmla_f32_m(pg, distances, diff, diff);
597
+ }
598
+
599
+ const svbool_t closer = svcmplt_f32(pg, distances, global_mins);
600
+ global_mins = svsel_f32(closer, distances, global_mins);
601
+ global_ids = svsel_u32(closer, current_ids, global_ids);
602
+ current_ids =
603
+ svadd_n_u32_x(pg, current_ids, static_cast<uint32_t>(lanes));
604
+ }
605
+
606
+ const svbool_t all = svptrue_b32();
607
+ const float global_min = svminv_f32(all, global_mins);
608
+ return svminv_u32(svcmpeq_n_f32(all, global_mins, global_min), global_ids);
609
+ }
610
+
611
+ } // namespace
612
+
523
613
  template <>
524
614
  size_t fvec_L2sqr_ny_nearest<SIMDLevel::ARM_SVE>(
525
615
  float* distances_tmp_buffer,
@@ -527,14 +617,52 @@ size_t fvec_L2sqr_ny_nearest<SIMDLevel::ARM_SVE>(
527
617
  const float* y,
528
618
  size_t d,
529
619
  size_t ny) {
530
- fvec_L2sqr_ny<SIMDLevel::ARM_SVE>(distances_tmp_buffer, x, y, d, ny);
620
+ switch (d) {
621
+ case 2:
622
+ return fvec_L2sqr_ny_nearest_lowdim<2>(
623
+ distances_tmp_buffer, x, y, ny);
624
+ case 4:
625
+ return fvec_L2sqr_ny_nearest_lowdim<4>(
626
+ distances_tmp_buffer, x, y, ny);
627
+ case 8:
628
+ return fvec_L2sqr_ny_nearest_lowdim<8>(
629
+ distances_tmp_buffer, x, y, ny);
630
+ }
631
+
632
+ const size_t lanes = static_cast<size_t>(svcntw());
531
633
 
532
634
  size_t nearest_idx = 0;
533
635
  float min_dis = HUGE_VALF;
534
636
 
535
- for (size_t i = 0; i < ny; i++) {
536
- if (distances_tmp_buffer[i] < min_dis) {
537
- min_dis = distances_tmp_buffer[i];
637
+ for (size_t i = 0; i < ny; ++i) {
638
+ const float* yi = y + i * d;
639
+ size_t j = 0;
640
+
641
+ svfloat32_t accv = svdup_n_f32(0.0f);
642
+
643
+ for (; j + lanes <= d; j += lanes) {
644
+ const svbool_t pg = svptrue_b32();
645
+ const svfloat32_t xv = svld1_f32(pg, x + j);
646
+ const svfloat32_t yv = svld1_f32(pg, yi + j);
647
+ const svfloat32_t diff = svsub_f32_x(pg, xv, yv);
648
+ accv = svmla_f32_x(pg, accv, diff, diff);
649
+ }
650
+
651
+ if (j < d) {
652
+ const svbool_t pg = svwhilelt_b32_u64(j, d);
653
+ const svfloat32_t xv = svld1_f32(pg, x + j);
654
+ const svfloat32_t yv = svld1_f32(pg, yi + j);
655
+ const svfloat32_t diff = svsub_f32_x(pg, xv, yv);
656
+ // Merging predication: lanes outside the tail keep the partial
657
+ // sums accumulated above, which the reduction below still adds in.
658
+ // `_x` would leave them unspecified.
659
+ accv = svmla_f32_m(pg, accv, diff, diff);
660
+ }
661
+
662
+ const float dist = svaddv_f32(svptrue_b32(), accv);
663
+ distances_tmp_buffer[i] = dist;
664
+ if (dist < min_dis) {
665
+ min_dis = dist;
538
666
  nearest_idx = i;
539
667
  }
540
668
  }
@@ -542,7 +670,6 @@ size_t fvec_L2sqr_ny_nearest<SIMDLevel::ARM_SVE>(
542
670
  return nearest_idx;
543
671
  }
544
672
 
545
- FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
546
673
  template <>
547
674
  void fvec_L2sqr_ny_transposed<SIMDLevel::ARM_SVE>(
548
675
  float* dis,
@@ -552,22 +679,26 @@ void fvec_L2sqr_ny_transposed<SIMDLevel::ARM_SVE>(
552
679
  size_t d,
553
680
  size_t d_offset,
554
681
  size_t ny) {
555
- float x_sqlen = 0;
556
- FAISS_PRAGMA_IMPRECISE_LOOP
557
- for (size_t j = 0; j < d; j++) {
558
- x_sqlen += x[j] * x[j];
559
- }
682
+ const size_t lanes = static_cast<size_t>(svcntw());
683
+ const float x_sq = fvec_norm_L2sqr(x, d);
684
+
685
+ for (size_t k = 0; k < ny; k += lanes) {
686
+ svbool_t pg = svwhilelt_b32_u64(k, ny);
687
+ svfloat32_t acc = svdup_n_f32(0.0f);
560
688
 
561
- for (size_t i = 0; i < ny; i++) {
562
- float dp = 0;
563
- FAISS_PRAGMA_IMPRECISE_LOOP
564
- for (size_t j = 0; j < d; j++) {
565
- dp += x[j] * y[i + j * d_offset];
689
+ for (size_t j = 0; j < d; ++j) {
690
+ svfloat32_t ychunk = svld1_f32(pg, y + j * d_offset + k);
691
+ svfloat32_t xj = svdup_n_f32(x[j]);
692
+ acc = svmla_f32_x(pg, acc, xj, ychunk);
566
693
  }
567
- dis[i] = x_sqlen + y_sqlen[i] - 2 * dp;
694
+
695
+ svfloat32_t ysq = svld1_f32(pg, y_sqlen + k);
696
+ svfloat32_t two_acc = svmul_f32_x(pg, acc, svdup_n_f32(2.0f));
697
+ svfloat32_t sum = svadd_f32_x(pg, svdup_n_f32(x_sq), ysq);
698
+ svfloat32_t res = svsub_f32_x(pg, sum, two_acc);
699
+ svst1_f32(pg, dis + k, res);
568
700
  }
569
701
  }
570
- FAISS_PRAGMA_IMPRECISE_FUNCTION_END
571
702
 
572
703
  template <>
573
704
  size_t fvec_L2sqr_ny_nearest_y_transposed<SIMDLevel::ARM_SVE>(
@@ -578,25 +709,60 @@ size_t fvec_L2sqr_ny_nearest_y_transposed<SIMDLevel::ARM_SVE>(
578
709
  size_t d,
579
710
  size_t d_offset,
580
711
  size_t ny) {
581
- fvec_L2sqr_ny_transposed<SIMDLevel::ARM_SVE>(
582
- distances_tmp_buffer, x, y, y_sqlen, d, d_offset, ny);
712
+ const size_t lanes = svcntw();
713
+ const float x_sq = fvec_norm_L2sqr(x, d);
583
714
 
584
- size_t nearest_idx = 0;
585
- float min_dis = HUGE_VALF;
715
+ size_t current_min_idx = 0;
716
+ svfloat32_t current_min_v = svdup_n_f32(HUGE_VALF);
586
717
 
587
- for (size_t i = 0; i < ny; i++) {
588
- if (distances_tmp_buffer[i] < min_dis) {
589
- min_dis = distances_tmp_buffer[i];
590
- nearest_idx = i;
718
+ // Max SVE vector length is 2048 bits → 64 fp32 lanes
719
+ float tmp_buf[64];
720
+
721
+ for (size_t k = 0; k < ny; k += lanes) {
722
+ svbool_t pg = svwhilelt_b32_u64(k, ny);
723
+ svfloat32_t acc = svdup_n_f32(0.0f);
724
+
725
+ for (size_t j = 0; j < d; ++j) {
726
+ svfloat32_t ychunk = svld1_f32(pg, y + j * d_offset + k);
727
+ svfloat32_t xj = svdup_n_f32(x[j]);
728
+ acc = svmla_f32_x(pg, acc, xj, ychunk);
729
+ }
730
+
731
+ svfloat32_t ysq = svld1_f32(pg, y_sqlen + k);
732
+ svfloat32_t two_acc = svmul_f32_x(pg, acc, svdup_n_f32(2.0f));
733
+ svfloat32_t sum = svadd_f32_x(pg, svdup_n_f32(x_sq), ysq);
734
+ svfloat32_t res = svsub_f32_x(pg, sum, two_acc);
735
+
736
+ svst1_f32(pg, distances_tmp_buffer + k, res);
737
+
738
+ svbool_t less_mask = svcmplt_f32(pg, res, current_min_v);
739
+
740
+ if (svptest_any(pg, less_mask)) {
741
+ float vec_min = svminv_f32(pg, res);
742
+
743
+ current_min_v =
744
+ svmin_f32_x(pg, current_min_v, svdup_n_f32(vec_min));
745
+
746
+ svst1_f32(pg, tmp_buf, res);
747
+
748
+ size_t cnt = (size_t)svcntw();
749
+ for (size_t lane = 0; lane < cnt && (k + lane) < ny; ++lane) {
750
+ if (tmp_buf[lane] == vec_min) {
751
+ current_min_idx = k + lane;
752
+ break;
753
+ }
754
+ }
591
755
  }
592
756
  }
593
757
 
594
- return nearest_idx;
758
+ return current_min_idx;
595
759
  }
596
760
 
597
- template <>
598
- void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
599
- const float* x,
761
+ namespace {
762
+
763
+ template <class QueryTiles>
764
+ void exhaustive_L2sqr_blas_cmax_sve(
765
+ QueryTiles& queries,
600
766
  const float* y,
601
767
  size_t d,
602
768
  size_t nx,
@@ -615,7 +781,7 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
615
781
  std::unique_ptr<float[]> x_norms(new float[nx]);
616
782
  std::unique_ptr<float[]> del2;
617
783
 
618
- fvec_norms_L2sqr(x_norms.get(), x, d, nx);
784
+ queries.prepare_norms(nx, x_norms.get());
619
785
 
620
786
  const size_t lanes = svcntw();
621
787
 
@@ -631,6 +797,8 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
631
797
  if (i1 > nx)
632
798
  i1 = nx;
633
799
 
800
+ const float* xt = queries.tile(i0, i1, x_norms.get());
801
+
634
802
  res.begin_multiple(i0, i1);
635
803
 
636
804
  for (size_t j0 = 0; j0 < ny; j0 += bs_y) {
@@ -649,7 +817,7 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
649
817
  &one,
650
818
  y + j0 * d,
651
819
  &di,
652
- x + i0 * d,
820
+ xt,
653
821
  &di,
654
822
  &zero,
655
823
  ip_block.get(),
@@ -774,4 +942,32 @@ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
774
942
  }
775
943
  }
776
944
 
945
+ } // namespace
946
+
947
+ template <>
948
+ void exhaustive_L2sqr_blas_cmax<SIMDLevel::ARM_SVE>(
949
+ const float* x,
950
+ const float* y,
951
+ size_t d,
952
+ size_t nx,
953
+ size_t ny,
954
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
955
+ const float* y_norms) {
956
+ Fp32QueryTiles queries{x, d};
957
+ exhaustive_L2sqr_blas_cmax_sve(queries, y, d, nx, ny, res, y_norms);
958
+ }
959
+
960
+ template <>
961
+ void exhaustive_L2sqr_blas_cmax_fp16<SIMDLevel::ARM_SVE>(
962
+ const uint16_t* x,
963
+ const float* y,
964
+ size_t d,
965
+ size_t nx,
966
+ size_t ny,
967
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
968
+ const float* y_norms) {
969
+ Fp16QueryTiles queries(x, d, nx);
970
+ exhaustive_L2sqr_blas_cmax_sve(queries, y, d, nx, ny, res, y_norms);
971
+ }
972
+
777
973
  } // namespace faiss