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
@@ -15,6 +15,7 @@
15
15
  #include <faiss/impl/scalar_quantizer/similarities.h>
16
16
  #include <faiss/utils/distances.h>
17
17
  #include <faiss/utils/rabitq_simd.h>
18
+ #include <faiss/utils/simd_impl/masked_sum.h>
18
19
  #include <limits>
19
20
 
20
21
  #ifndef THE_LEVEL_TO_DISPATCH
@@ -28,34 +29,13 @@ namespace scalar_quantizer {
28
29
  // Define SL as alias for THE_LEVEL_TO_DISPATCH for use in this file
29
30
  constexpr SIMDLevel SL = THE_LEVEL_TO_DISPATCH;
30
31
 
31
- /*******************************************************************
32
- * TurboQuant SIMD kernel: masked_sum
33
- * Compute sum of arr[j] where bit j of the bitmask is set.
34
- * NONE specialization is inline; AVX2/AVX512/NEON specializations
35
- * live in sq-avx2.cpp / sq-avx512.cpp / sq-neon.cpp.
36
- *******************************************************************/
37
-
32
+ // The SPR scalar-quantizer implementation uses VPOPCNTDQ for its RaBitQ
33
+ // popcount helpers, but those helpers belong to the narrower VPOPCNT
34
+ // capability. Keep the SQ implementation at AVX512_SPR while explicitly
35
+ // reusing the independently dispatched VPOPCNT kernels.
38
36
  template <SIMDLevel SL0>
39
- float turboq_masked_sum(const float* arr, const uint8_t* bits, size_t d);
40
-
41
- template <>
42
- inline float turboq_masked_sum<SIMDLevel::NONE>(
43
- const float* arr,
44
- const uint8_t* bits,
45
- size_t d) {
46
- float result = 0;
47
- for (size_t byte_idx = 0; byte_idx < (d + 7) / 8; byte_idx++) {
48
- uint8_t b = bits[byte_idx];
49
- size_t base = byte_idx * 8;
50
- size_t end = std::min(base + 8, d);
51
- for (size_t j = base; j < end; j++) {
52
- if (b & (1 << (j - base))) {
53
- result += arr[j];
54
- }
55
- }
56
- }
57
- return result;
58
- }
37
+ inline constexpr SIMDLevel rabitq_popcount_level =
38
+ SL0 == SIMDLevel::AVX512_SPR ? SIMDLevel::AVX512_VPOPCNT : SL0;
59
39
 
60
40
  /*******************************************************************
61
41
  * Full TurboQuant DC — lives here because it needs both
@@ -65,8 +45,10 @@ inline float turboq_masked_sum<SIMDLevel::NONE>(
65
45
  *******************************************************************/
66
46
  template <int NBits, class Similarity, SIMDLevel SL2>
67
47
  struct DCTurboQuantFull : ScalarQuantizer::TurboQuantRefine::DistanceComputer {
48
+ // The AVX-512 levels share the AVX512 masked_sum specialization.
49
+ static constexpr SIMDLevel SUM_LEVEL = simd512_level_selector<SL2>::value;
68
50
  using Sim = Similarity;
69
- QuantizerTurboQuantFull<NBits, SIMDLevel::NONE> quant;
51
+ QuantizerTurboQuantFull<NBits, SL2> quant;
70
52
  std::vector<float> query;
71
53
  std::vector<float> query_proj;
72
54
  float q_norm_sq = 0;
@@ -259,27 +241,29 @@ struct DCTurboQuantFull : ScalarQuantizer::TurboQuantRefine::DistanceComputer {
259
241
  if (qb > 0) {
260
242
  // Integer popcount path for 1-bit MSE
261
243
  size_t byte_size = (d + 7) / 8;
262
- uint64_t and_result = rabitq::bitwise_and_dot_product<SL2>(
244
+ uint64_t and_result = rabitq::bitwise_and_dot_product<
245
+ rabitq_popcount_level<SL2>>(
263
246
  rearranged_q.data(), code, byte_size, qb);
264
- uint64_t pop = rabitq::popcount<SL2>(code, byte_size);
247
+ uint64_t pop = rabitq::popcount<rabitq_popcount_level<SL2>>(
248
+ code, byte_size);
265
249
  mse_dot = mse_base +
266
250
  mse_int_scale * static_cast<float>(and_result) +
267
251
  mse_popcnt_scale * static_cast<float>(pop);
268
252
  } else {
269
253
  // Float path: masked accumulation
270
- float pos_sum = turboq_masked_sum<SL2>(query.data(), code, d);
254
+ float pos_sum = masked_sum<SUM_LEVEL>(query.data(), code, d);
271
255
  mse_dot = scaled_c0 * total_q_sum + delta_centroid * pos_sum;
272
256
  }
273
257
  } else if constexpr (NBits == 3) {
274
258
  // 2-bit MSE: decompose into 3 masked sums over bit-planes.
275
259
  size_t pb = quant.mse_plane_bytes;
276
- float s0 = turboq_masked_sum<SL2>(query.data(), code, d);
277
- float s1 = turboq_masked_sum<SL2>(query.data(), code + pb, d);
260
+ float s0 = masked_sum<SUM_LEVEL>(query.data(), code, d);
261
+ float s1 = masked_sum<SUM_LEVEL>(query.data(), code + pb, d);
278
262
  for (size_t i = 0; i < pb; i++) {
279
263
  scratch_and[i] = code[i] & code[pb + i];
280
264
  }
281
265
  float s01 =
282
- turboq_masked_sum<SL2>(query.data(), scratch_and.data(), d);
266
+ masked_sum<SUM_LEVEL>(query.data(), scratch_and.data(), d);
283
267
  mse_dot = mse_multi_base + mse_coeff_s0 * s0 + mse_coeff_s1 * s1 +
284
268
  mse_coeff_s01 * s01;
285
269
  } else {
@@ -316,15 +300,17 @@ struct DCTurboQuantFull : ScalarQuantizer::TurboQuantRefine::DistanceComputer {
316
300
  float qjl_dot;
317
301
  if (qb > 0 && int_qjl) {
318
302
  size_t byte_size = (d + 7) / 8;
319
- uint64_t and_result = rabitq::bitwise_and_dot_product<SL2>(
320
- rearranged_qproj.data(), qjl_code, byte_size, qb);
321
- uint64_t pop = rabitq::popcount<SL2>(qjl_code, byte_size);
303
+ uint64_t and_result =
304
+ rabitq::bitwise_and_dot_product<rabitq_popcount_level<SL2>>(
305
+ rearranged_qproj.data(), qjl_code, byte_size, qb);
306
+ uint64_t pop = rabitq::popcount<rabitq_popcount_level<SL2>>(
307
+ qjl_code, byte_size);
322
308
  float pos_sum = qjl_popcnt_scale * static_cast<float>(pop) +
323
309
  qjl_int_scale * static_cast<float>(and_result);
324
310
  qjl_dot = qjl_coeff * gamma * (2.0f * pos_sum - total_qproj_sum);
325
311
  } else {
326
312
  float pos_sum =
327
- turboq_masked_sum<SL2>(query_proj.data(), qjl_code, d);
313
+ masked_sum<SUM_LEVEL>(query_proj.data(), qjl_code, d);
328
314
  qjl_dot = qjl_coeff * gamma * (2.0f * pos_sum - total_qproj_sum);
329
315
  }
330
316
 
@@ -342,10 +328,17 @@ struct DCTurboQuantFull : ScalarQuantizer::TurboQuantRefine::DistanceComputer {
342
328
  }
343
329
  };
344
330
 
331
+ // True for every level that runs the 512-bit kernels. AVX512_VPOPCNT is
332
+ // unreachable here until an SQ translation unit is compiled at that level;
333
+ // naming it keeps the 512-bit alignment rule correct when one is.
334
+ template <SIMDLevel SL2>
335
+ constexpr bool is_avx512_family = SL2 == SIMDLevel::AVX512 ||
336
+ SL2 == SIMDLevel::AVX512_VPOPCNT || SL2 == SIMDLevel::AVX512_SPR;
337
+
345
338
  // Returns true if dimension d is compatible with the given SIMD level
346
339
  template <SIMDLevel SL2>
347
340
  constexpr bool is_dimension_compatible(size_t d) {
348
- if constexpr (SL2 == SIMDLevel::AVX512 || SL2 == SIMDLevel::AVX512_SPR) {
341
+ if constexpr (is_avx512_family<SL2>) {
349
342
  return d % 16 == 0;
350
343
  } else if constexpr (SL2 == SIMDLevel::AVX2 || SL2 == SIMDLevel::ARM_NEON) {
351
344
  return d % 8 == 0;
@@ -354,6 +347,50 @@ constexpr bool is_dimension_compatible(size_t d) {
354
347
  }
355
348
  }
356
349
 
350
+ // Distance computers and scanners. At AVX512 the 16-bit float codecs decode
351
+ // a partial last block, so they take any d.
352
+ template <SIMDLevel SL2>
353
+ constexpr bool is_dimension_compatible(
354
+ size_t d,
355
+ ScalarQuantizer::QuantizerType qtype) {
356
+ return (SL2 == SIMDLevel::AVX512 &&
357
+ (qtype == ScalarQuantizer::QT_fp16 ||
358
+ qtype == ScalarQuantizer::QT_bf16)) ||
359
+ is_dimension_compatible<SL2>(d);
360
+ }
361
+
362
+ /// Calls f with the DCTemplate type for (Quantizer, Sim, SL2) and d. The
363
+ /// 16-bit float quantizers and the QT_8bit inner product use it. Four
364
+ /// accumulators per code hide the FMA latency at a large d. At a small d their
365
+ /// reduction costs more than it saves. At AVX512 they are faster only on AMD
366
+ /// (SIMDConfig::sq_avx512_4_accumulators).
367
+ template <class Quantizer, class Sim, SIMDLevel SL2, class F>
368
+ auto with_dc_template(size_t d, F f) {
369
+ if constexpr (SL2 == SIMDLevel::AVX2 || SL2 == SIMDLevel::AVX512) {
370
+ if (d >= 384 &&
371
+ (SL2 == SIMDLevel::AVX2 || SIMDConfig::sq_avx512_4_accumulators)) {
372
+ return f.template
373
+ operator()<DCTemplate<Quantizer, Sim, SL2, true>>();
374
+ }
375
+ }
376
+ return f.template operator()<DCTemplate<Quantizer, Sim, SL2>>();
377
+ }
378
+
379
+ /// with_dc_template for QT_8bit. Only its inner product has a four-accumulator
380
+ /// loop.
381
+ template <class Sim, SIMDLevel SL2, class F>
382
+ auto with_8bit_dc_template(size_t d, F f) {
383
+ using Quantizer = QuantizerTemplate<
384
+ Codec8bit<SL2>,
385
+ QuantizerTemplateScaling::NON_UNIFORM,
386
+ SL2>;
387
+ if constexpr (Sim::metric_type == METRIC_INNER_PRODUCT) {
388
+ return with_dc_template<Quantizer, Sim, SL2>(d, f);
389
+ } else {
390
+ return f.template operator()<DCTemplate<Quantizer, Sim, SL2>>();
391
+ }
392
+ }
393
+
357
394
  /*******************************************************************
358
395
  * sq_select_quantizer: the big switch returning SQuantizer*
359
396
  *******************************************************************/
@@ -468,10 +505,13 @@ SQDistanceComputer* select_distance_computer_body(
468
505
  const std::vector<float>& trained) {
469
506
  // Return nullptr for incompatible dimensions in SIMD cases
470
507
  if constexpr (SL2 != SIMDLevel::NONE) {
471
- if (!is_dimension_compatible<SL2>(d)) {
508
+ if (!is_dimension_compatible<SL2>(d, qtype)) {
472
509
  return nullptr;
473
510
  }
474
511
  }
512
+ auto new_dc = [&]<class DC>() -> SQDistanceComputer* {
513
+ return new DC(d, trained);
514
+ };
475
515
  switch (qtype) {
476
516
  case ScalarQuantizer::QT_8bit_uniform:
477
517
  return new DCTemplate<
@@ -492,13 +532,7 @@ SQDistanceComputer* select_distance_computer_body(
492
532
  SL2>(d, trained);
493
533
 
494
534
  case ScalarQuantizer::QT_8bit:
495
- return new DCTemplate<
496
- QuantizerTemplate<
497
- Codec8bit<SL2>,
498
- QuantizerTemplateScaling::NON_UNIFORM,
499
- SL2>,
500
- Sim,
501
- SL2>(d, trained);
535
+ return with_8bit_dc_template<Sim, SL2>(d, new_dc);
502
536
 
503
537
  case ScalarQuantizer::QT_6bit:
504
538
  return new DCTemplate<
@@ -519,19 +553,19 @@ SQDistanceComputer* select_distance_computer_body(
519
553
  SL2>(d, trained);
520
554
 
521
555
  case ScalarQuantizer::QT_fp16:
522
- return new DCTemplate<QuantizerFP16<SL2>, Sim, SL2>(d, trained);
556
+ return with_dc_template<QuantizerFP16<SL2>, Sim, SL2>(d, new_dc);
523
557
 
524
558
  case ScalarQuantizer::QT_bf16:
525
- return new DCTemplate<QuantizerBF16<SL2>, Sim, SL2>(d, trained);
559
+ return with_dc_template<QuantizerBF16<SL2>, Sim, SL2>(d, new_dc);
526
560
 
527
561
  case ScalarQuantizer::QT_8bit_direct:
528
- if constexpr (
529
- SL2 == SIMDLevel::AVX512 || SL2 == SIMDLevel::AVX512_SPR) {
562
+ if constexpr (is_avx512_family<SL2>) {
530
563
  if (d % 32 == 0) {
531
564
  return new DistanceComputerByte<Sim, SL2>(
532
565
  static_cast<int>(d), trained);
533
566
  }
534
- } else if constexpr (SL2 == SIMDLevel::AVX2) {
567
+ } else if constexpr (
568
+ SL2 == SIMDLevel::AVX2 || SL2 == SIMDLevel::ARM_NEON) {
535
569
  if (d % 16 == 0) {
536
570
  return new DistanceComputerByte<Sim, SL2>(
537
571
  static_cast<int>(d), trained);
@@ -546,12 +580,13 @@ SQDistanceComputer* select_distance_computer_body(
546
580
  return new DistanceComputerByteSigned<Sim, SL2>(
547
581
  static_cast<int>(d), trained);
548
582
  }
549
- } else if constexpr (SL2 == SIMDLevel::AVX512) {
583
+ } else if constexpr (is_avx512_family<SL2>) {
550
584
  if (d % 32 == 0) {
551
585
  return new DistanceComputerByteSigned<Sim, SL2>(
552
586
  static_cast<int>(d), trained);
553
587
  }
554
- } else if constexpr (SL2 == SIMDLevel::AVX2) {
588
+ } else if constexpr (
589
+ SL2 == SIMDLevel::AVX2 || SL2 == SIMDLevel::ARM_NEON) {
555
590
  if (d % 16 == 0) {
556
591
  return new DistanceComputerByteSigned<Sim, SL2>(
557
592
  static_cast<int>(d), trained);
@@ -685,7 +720,7 @@ InvertedListScanner* sq_select_InvertedListScanner<THE_LEVEL_TO_DISPATCH>(
685
720
  [&]<SIMDLevel SL2, class Similarity>() -> InvertedListScanner* {
686
721
  // Return nullptr for incompatible dimensions in SIMD cases
687
722
  if constexpr (SL2 != SIMDLevel::NONE) {
688
- if (!is_dimension_compatible<SL2>(d)) {
723
+ if (!is_dimension_compatible<SL2>(d, qtype)) {
689
724
  return nullptr;
690
725
  }
691
726
  }
@@ -707,13 +742,7 @@ InvertedListScanner* sq_select_InvertedListScanner<THE_LEVEL_TO_DISPATCH>(
707
742
  Similarity,
708
743
  SL2>>();
709
744
  case ScalarQuantizer::QT_8bit:
710
- return scan.template operator()<DCTemplate<
711
- QuantizerTemplate<
712
- Codec8bit<SL2>,
713
- QuantizerTemplateScaling::NON_UNIFORM,
714
- SL2>,
715
- Similarity,
716
- SL2>>();
745
+ return with_8bit_dc_template<Similarity, SL2>(d, scan);
717
746
  case ScalarQuantizer::QT_4bit:
718
747
  return scan.template operator()<DCTemplate<
719
748
  QuantizerTemplate<
@@ -731,20 +760,19 @@ InvertedListScanner* sq_select_InvertedListScanner<THE_LEVEL_TO_DISPATCH>(
731
760
  Similarity,
732
761
  SL2>>();
733
762
  case ScalarQuantizer::QT_fp16:
734
- return scan.template
735
- operator()<DCTemplate<QuantizerFP16<SL2>, Similarity, SL2>>();
763
+ return with_dc_template<QuantizerFP16<SL2>, Similarity, SL2>(
764
+ d, scan);
736
765
  case ScalarQuantizer::QT_bf16:
737
- return scan.template
738
- operator()<DCTemplate<QuantizerBF16<SL2>, Similarity, SL2>>();
766
+ return with_dc_template<QuantizerBF16<SL2>, Similarity, SL2>(
767
+ d, scan);
739
768
  case ScalarQuantizer::QT_8bit_direct:
740
- if constexpr (
741
- SL2 == SIMDLevel::AVX512 ||
742
- SL2 == SIMDLevel::AVX512_SPR) {
769
+ if constexpr (is_avx512_family<SL2>) {
743
770
  if (d % 32 == 0) {
744
771
  return scan.template
745
772
  operator()<DistanceComputerByte<Similarity, SL2>>();
746
773
  }
747
- } else if constexpr (SL2 == SIMDLevel::AVX2) {
774
+ } else if constexpr (
775
+ SL2 == SIMDLevel::AVX2 || SL2 == SIMDLevel::ARM_NEON) {
748
776
  if (d % 16 == 0) {
749
777
  return scan.template
750
778
  operator()<DistanceComputerByte<Similarity, SL2>>();
@@ -760,12 +788,13 @@ InvertedListScanner* sq_select_InvertedListScanner<THE_LEVEL_TO_DISPATCH>(
760
788
  return scan.template operator()<
761
789
  DistanceComputerByteSigned<Similarity, SL2>>();
762
790
  }
763
- } else if constexpr (SL2 == SIMDLevel::AVX512) {
791
+ } else if constexpr (is_avx512_family<SL2>) {
764
792
  if (d % 32 == 0) {
765
793
  return scan.template operator()<
766
794
  DistanceComputerByteSigned<Similarity, SL2>>();
767
795
  }
768
- } else if constexpr (SL2 == SIMDLevel::AVX2) {
796
+ } else if constexpr (
797
+ SL2 == SIMDLevel::AVX2 || SL2 == SIMDLevel::ARM_NEON) {
769
798
  if (d % 16 == 0) {
770
799
  return scan.template operator()<
771
800
  DistanceComputerByteSigned<Similarity, SL2>>();
@@ -5,11 +5,8 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- #ifdef COMPILE_SIMD_ARM_NEON
9
-
10
8
  #include <faiss/impl/simdlib/simdlib_neon.h>
11
9
 
12
- #include <algorithm>
13
10
  #include <cstring>
14
11
 
15
12
  #include <faiss/impl/scalar_quantizer/codecs.h>
@@ -17,6 +14,7 @@
17
14
  #include <faiss/impl/scalar_quantizer/quantizers.h>
18
15
  #include <faiss/impl/scalar_quantizer/scanners.h>
19
16
  #include <faiss/impl/scalar_quantizer/similarities.h>
17
+ #include <faiss/impl/scalar_quantizer/sq8_coefficients.h>
20
18
 
21
19
  namespace faiss {
22
20
 
@@ -105,14 +103,15 @@ template <>
105
103
  struct Codec8bit<SIMDLevel::ARM_NEON> : Codec8bit<SIMDLevel::NONE> {
106
104
  static FAISS_ALWAYS_INLINE simd8float32
107
105
  decode_8_components(const uint8_t* code, size_t i) {
108
- float32_t result[8] = {};
109
- for (size_t j = 0; j < 8; j++) {
110
- result[j] =
111
- Codec8bit<SIMDLevel::NONE>::decode_component(code, i + j);
112
- }
113
- float32x4_t res1 = vld1q_f32(result);
114
- float32x4_t res2 = vld1q_f32(result + 4);
115
- return simd8float32(float32x4x2_t{res1, res2});
106
+ const uint16x8_t c16 = vmovl_u8(vld1_u8(code + i));
107
+ const float32x4_t lo = vcvtq_f32_u32(vmovl_u16(vget_low_u16(c16)));
108
+ const float32x4_t hi = vcvtq_f32_u32(vmovl_u16(vget_high_u16(c16)));
109
+ const float32x4_t one_255 = vdupq_n_f32(1.f / 255.f);
110
+ const float32x4_t half_one_255 = vdupq_n_f32(0.5f / 255.f);
111
+ return simd8float32(
112
+ float32x4x2_t{
113
+ vfmaq_f32(half_one_255, lo, one_255),
114
+ vfmaq_f32(half_one_255, hi, one_255)});
116
115
  }
117
116
  };
118
117
 
@@ -618,6 +617,155 @@ struct DCTemplate<Quantizer, Similarity, SIMDLevel::ARM_NEON>
618
617
  }
619
618
  };
620
619
 
620
+ // QT_8bit + L2. With a, rmin from sq8_coefficients.h and e = q - rmin, each
621
+ // term is (e_i - a_i * c_i)^2: one fmls and one fmla per 4 dims, instead of
622
+ // separate codec, vmin/vdiff, sub, mul and add steps.
623
+ template <>
624
+ struct DCTemplate<
625
+ QuantizerTemplate<
626
+ Codec8bit<SIMDLevel::ARM_NEON>,
627
+ QuantizerTemplateScaling::NON_UNIFORM,
628
+ SIMDLevel::ARM_NEON>,
629
+ SimilarityL2<SIMDLevel::ARM_NEON>,
630
+ SIMDLevel::ARM_NEON> : SQDistanceComputer {
631
+ using Sim = SimilarityL2<SIMDLevel::ARM_NEON>;
632
+
633
+ size_t d;
634
+ std::vector<float> a, rmin, e;
635
+
636
+ DCTemplate(size_t d_in, const std::vector<float>& trained)
637
+ : d(d_in), a(d_in), rmin(d_in), e(d_in) {
638
+ assert(d % 8 == 0);
639
+ sq8::make_coefficients(trained.data(), d, a.data(), rmin.data());
640
+ }
641
+
642
+ void set_query(const float* x) final {
643
+ q = x;
644
+ for (size_t i = 0; i < d; i++) {
645
+ e[i] = x[i] - rmin[i];
646
+ }
647
+ }
648
+
649
+ // Each code has its own accumulators, so the batched results equal the
650
+ // single-code ones bit for bit.
651
+ template <size_t N>
652
+ FAISS_ALWAYS_INLINE void l2(
653
+ const uint8_t* const (&codes)[N],
654
+ float (&dis)[N]) const {
655
+ float32x4_t acc[N][2] = {};
656
+ for (size_t i = 0; i < d; i += 8) {
657
+ const float32x4_t a0 = vld1q_f32(a.data() + i);
658
+ const float32x4_t a1 = vld1q_f32(a.data() + i + 4);
659
+ const float32x4_t e0 = vld1q_f32(e.data() + i);
660
+ const float32x4_t e1 = vld1q_f32(e.data() + i + 4);
661
+ for (size_t n = 0; n < N; n++) {
662
+ const uint16x8_t c16 = vmovl_u8(vld1_u8(codes[n] + i));
663
+ const float32x4_t t0 = vfmsq_f32(
664
+ e0, a0, vcvtq_f32_u32(vmovl_u16(vget_low_u16(c16))));
665
+ const float32x4_t t1 =
666
+ vfmsq_f32(e1, a1, vcvtq_f32_u32(vmovl_high_u16(c16)));
667
+ acc[n][0] = vfmaq_f32(acc[n][0], t0, t0);
668
+ acc[n][1] = vfmaq_f32(acc[n][1], t1, t1);
669
+ }
670
+ }
671
+ for (size_t n = 0; n < N; n++) {
672
+ dis[n] = vaddvq_f32(vaddq_f32(acc[n][0], acc[n][1]));
673
+ }
674
+ }
675
+
676
+ float query_to_code(const uint8_t* code) const final {
677
+ float dis[1];
678
+ l2<1>({code}, dis);
679
+ return dis[0];
680
+ }
681
+
682
+ void query_to_codes_batch_4(
683
+ const uint8_t* code_0,
684
+ const uint8_t* code_1,
685
+ const uint8_t* code_2,
686
+ const uint8_t* code_3,
687
+ float& dis0,
688
+ float& dis1,
689
+ float& dis2,
690
+ float& dis3) const final {
691
+ float dis[4];
692
+ l2<4>({code_0, code_1, code_2, code_3}, dis);
693
+ dis0 = dis[0];
694
+ dis1 = dis[1];
695
+ dis2 = dis[2];
696
+ dis3 = dis[3];
697
+ }
698
+
699
+ float symmetric_dis(idx_t i, idx_t j) override {
700
+ const uint8_t* c1 = codes + i * code_size;
701
+ const uint8_t* c2 = codes + j * code_size;
702
+ float accu = 0;
703
+ for (size_t k = 0; k < d; k++) {
704
+ const float diff = a[k] * float(int(c1[k]) - int(c2[k]));
705
+ accu += diff * diff;
706
+ }
707
+ return accu;
708
+ }
709
+ };
710
+
711
+ // Byte-domain kernels for QT_8bit_direct{,_signed}. The dispatch only
712
+ // selects them when d % 16 == 0, so no loop needs a tail.
713
+
714
+ namespace {
715
+
716
+ // The accumulator stays unsigned: a vmull_u8 square reaches 255*255 = 65025,
717
+ // which an int16 lane would read as negative.
718
+ FAISS_ALWAYS_INLINE int neon_byte_l2sqr(
719
+ const uint8_t* code1,
720
+ const uint8_t* code2,
721
+ int d) {
722
+ uint32x4_t accu = vdupq_n_u32(0);
723
+ for (int i = 0; i < d; i += 16) {
724
+ const uint8x16_t diff =
725
+ vabdq_u8(vld1q_u8(code1 + i), vld1q_u8(code2 + i));
726
+ accu = vpadalq_u16(
727
+ accu, vmull_u8(vget_low_u8(diff), vget_low_u8(diff)));
728
+ accu = vpadalq_u16(
729
+ accu, vmull_u8(vget_high_u8(diff), vget_high_u8(diff)));
730
+ }
731
+ return static_cast<int>(vaddvq_u32(accu));
732
+ }
733
+
734
+ FAISS_ALWAYS_INLINE int neon_byte_ip(
735
+ const uint8_t* code1,
736
+ const uint8_t* code2,
737
+ int d) {
738
+ uint32x4_t accu = vdupq_n_u32(0);
739
+ for (int i = 0; i < d; i += 16) {
740
+ const uint8x16_t c1 = vld1q_u8(code1 + i);
741
+ const uint8x16_t c2 = vld1q_u8(code2 + i);
742
+ accu = vpadalq_u16(accu, vmull_u8(vget_low_u8(c1), vget_low_u8(c2)));
743
+ accu = vpadalq_u16(accu, vmull_u8(vget_high_u8(c1), vget_high_u8(c2)));
744
+ }
745
+ return static_cast<int>(vaddvq_u32(accu));
746
+ }
747
+
748
+ // The codes store value + 128. For x in 0 to 255, x ^ 0x80 read as int8 is
749
+ // exactly x - 128, which is how the bias comes off before vmull_s8.
750
+ FAISS_ALWAYS_INLINE int neon_byte_ip_unbias(
751
+ const uint8_t* code1,
752
+ const uint8_t* code2,
753
+ int d) {
754
+ const uint8x16_t bias = vdupq_n_u8(0x80);
755
+ int32x4_t accu = vdupq_n_s32(0);
756
+ for (int i = 0; i < d; i += 16) {
757
+ const int8x16_t c1 =
758
+ vreinterpretq_s8_u8(veorq_u8(vld1q_u8(code1 + i), bias));
759
+ const int8x16_t c2 =
760
+ vreinterpretq_s8_u8(veorq_u8(vld1q_u8(code2 + i), bias));
761
+ accu = vpadalq_s16(accu, vmull_s8(vget_low_s8(c1), vget_low_s8(c2)));
762
+ accu = vpadalq_s16(accu, vmull_s8(vget_high_s8(c1), vget_high_s8(c2)));
763
+ }
764
+ return static_cast<int>(vaddvq_s32(accu));
765
+ }
766
+
767
+ } // namespace
768
+
621
769
  template <class Similarity>
622
770
  struct DistanceComputerByte<Similarity, SIMDLevel::ARM_NEON>
623
771
  : SQDistanceComputer {
@@ -626,20 +774,17 @@ struct DistanceComputerByte<Similarity, SIMDLevel::ARM_NEON>
626
774
  int d;
627
775
  std::vector<uint8_t> tmp;
628
776
 
629
- DistanceComputerByte(int d, const std::vector<float>&) : d(d), tmp(d) {}
777
+ DistanceComputerByte(int d, const std::vector<float>&) : d(d), tmp(d) {
778
+ FAISS_THROW_IF_NOT(d % 16 == 0);
779
+ }
630
780
 
631
781
  int compute_code_distance(const uint8_t* code1, const uint8_t* code2)
632
782
  const {
633
- int accu = 0;
634
- for (int i = 0; i < d; i++) {
635
- if (Sim::metric_type == METRIC_INNER_PRODUCT) {
636
- accu += int(code1[i]) * code2[i];
637
- } else {
638
- int diff = int(code1[i]) - code2[i];
639
- accu += diff * diff;
640
- }
783
+ if constexpr (Sim::metric_type == METRIC_INNER_PRODUCT) {
784
+ return neon_byte_ip(code1, code2, d);
785
+ } else {
786
+ return neon_byte_l2sqr(code1, code2, d);
641
787
  }
642
- return accu;
643
788
  }
644
789
 
645
790
  void set_query(const float* x) final {
@@ -663,93 +808,52 @@ struct DistanceComputerByte<Similarity, SIMDLevel::ARM_NEON>
663
808
  }
664
809
  };
665
810
 
666
- /**********************************************************
667
- * TurboQuant masked_sum NEON specialization (scalar fallback)
668
- **********************************************************/
811
+ template <class Similarity>
812
+ struct DistanceComputerByteSigned<Similarity, SIMDLevel::ARM_NEON>
813
+ : SQDistanceComputer {
814
+ using Sim = Similarity;
669
815
 
670
- template <SIMDLevel SL0>
671
- float turboq_masked_sum(const float* arr, const uint8_t* bits, size_t d);
816
+ int d;
817
+ std::vector<uint8_t> tmp;
672
818
 
673
- template <>
674
- float turboq_masked_sum<SIMDLevel::ARM_NEON>(
675
- const float* arr,
676
- const uint8_t* bits,
677
- size_t d) {
678
- float result = 0;
679
- for (size_t byte_idx = 0; byte_idx < (d + 7) / 8; byte_idx++) {
680
- uint8_t b = bits[byte_idx];
681
- size_t base = byte_idx * 8;
682
- size_t end = std::min(base + 8, d);
683
- for (size_t j = base; j < end; j++) {
684
- if (b & (1 << (j - base))) {
685
- result += arr[j];
686
- }
687
- }
819
+ DistanceComputerByteSigned(int d, const std::vector<float>&)
820
+ : d(d), tmp(d) {
821
+ FAISS_THROW_IF_NOT(d % 16 == 0);
688
822
  }
689
- return result;
690
- }
691
-
692
- } // namespace scalar_quantizer
693
- } // namespace faiss
694
-
695
- #define THE_LEVEL_TO_DISPATCH SIMDLevel::ARM_NEON
696
- #include <faiss/impl/scalar_quantizer/sq-dispatch.h>
697
-
698
- #ifdef COMPILE_SIMD_ARM_SVE
699
823
 
700
- // ARM_SVE: SVE is a superset of NEON. Forward to the NEON implementation
701
- // until a dedicated SVE specialization is written.
824
+ int compute_code_distance(const uint8_t* code1, const uint8_t* code2)
825
+ const {
826
+ if constexpr (Sim::metric_type == METRIC_INNER_PRODUCT) {
827
+ return neon_byte_ip_unbias(code1, code2, d);
828
+ } else {
829
+ // The bias cancels in the difference.
830
+ return neon_byte_l2sqr(code1, code2, d);
831
+ }
832
+ }
702
833
 
703
- namespace faiss {
704
- namespace scalar_quantizer {
834
+ void set_query(const float* x) final {
835
+ for (int i = 0; i < d; i++) {
836
+ tmp[i] = uint8_t(int(x[i]) + 128);
837
+ }
838
+ }
705
839
 
706
- // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
707
- template <>
708
- ScalarQuantizer::SQuantizer* sq_select_quantizer<SIMDLevel::ARM_SVE>(
709
- QuantizerType qtype,
710
- size_t d,
711
- const std::vector<float>& trained) {
712
- return sq_select_quantizer<SIMDLevel::ARM_NEON>(qtype, d, trained);
713
- }
840
+ int compute_distance(const float* x, const uint8_t* code) {
841
+ set_query(x);
842
+ return compute_code_distance(tmp.data(), code);
843
+ }
714
844
 
715
- // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
716
- template <>
717
- SQDistanceComputer* sq_select_distance_computer<SIMDLevel::ARM_SVE>(
718
- MetricType metric,
719
- ScalarQuantizer::QuantizerType qtype,
720
- size_t d,
721
- const std::vector<float>& trained) {
722
- return sq_select_distance_computer<SIMDLevel::ARM_NEON>(
723
- metric, qtype, d, trained);
724
- }
845
+ float symmetric_dis(idx_t i, idx_t j) override {
846
+ return compute_code_distance(
847
+ codes + i * code_size, codes + j * code_size);
848
+ }
725
849
 
726
- // NOLINTNEXTLINE(facebook-hte-MisplacedTemplateSpecialization)
727
- template <>
728
- InvertedListScanner* sq_select_InvertedListScanner<SIMDLevel::ARM_SVE>(
729
- QuantizerType qtype,
730
- MetricType mt,
731
- size_t d,
732
- size_t code_size,
733
- const std::vector<float>& trained,
734
- const Index* quantizer,
735
- bool store_pairs,
736
- const IDSelector* sel,
737
- bool by_residual) {
738
- return sq_select_InvertedListScanner<SIMDLevel::ARM_NEON>(
739
- qtype,
740
- mt,
741
- d,
742
- code_size,
743
- trained,
744
- quantizer,
745
- store_pairs,
746
- sel,
747
- by_residual);
748
- }
850
+ float query_to_code(const uint8_t* code) const final {
851
+ return compute_code_distance(tmp.data(), code);
852
+ }
853
+ };
749
854
 
750
855
  } // namespace scalar_quantizer
751
856
  } // namespace faiss
752
857
 
753
- #endif // COMPILE_SIMD_ARM_SVE
754
-
755
- #endif // COMPILE_SIMD_ARM_NEON
858
+ #define THE_LEVEL_TO_DISPATCH SIMDLevel::ARM_NEON
859
+ #include <faiss/impl/scalar_quantizer/sq-dispatch.h>