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
@@ -67,11 +67,13 @@ IndexSVSVamana::IndexSVSVamana(
67
67
  size_t degree,
68
68
  MetricType metric,
69
69
  SVSStorageKind storage,
70
- bool is_static)
70
+ bool is_static,
71
+ bool store_vectors)
71
72
  : Index(d, metric),
72
73
  graph_max_degree{degree},
73
74
  is_static{is_static},
74
- storage_kind{storage} {
75
+ storage_kind{storage},
76
+ store_vectors{store_vectors} {
75
77
  prune_to = graph_max_degree < 4 ? graph_max_degree : graph_max_degree - 4;
76
78
  alpha = metric == METRIC_L2 ? 1.2f : 0.95f;
77
79
 
@@ -124,6 +126,14 @@ IndexSVSVamana::~IndexSVSVamana() {
124
126
  }
125
127
 
126
128
  void IndexSVSVamana::add(idx_t n, const float* x) {
129
+ // Opting out after data has been added would leave stored_vectors
130
+ // misaligned with the ids in the index, so release it instead of growing.
131
+ if (!store_vectors && stored_vectors_valid) {
132
+ stored_vectors.clear();
133
+ stored_vectors.shrink_to_fit();
134
+ stored_vectors_valid = false;
135
+ }
136
+
127
137
  if (is_static) {
128
138
  FAISS_THROW_IF_MSG(
129
139
  impl,
@@ -167,7 +177,8 @@ void IndexSVSVamana::reconstruct(idx_t key, float* recons) const {
167
177
  FAISS_THROW_IF_NOT_MSG(
168
178
  stored_vectors_valid && !stored_vectors.empty(),
169
179
  "IndexSVSVamana::reconstruct: stored_vectors unavailable "
170
- "(invalidated by remove_ids or not restored after deserialization)");
180
+ "(store_vectors disabled, invalidated by remove_ids, or not "
181
+ "restored after deserialization)");
171
182
  std::memcpy(recons, stored_vectors.data() + key * d, sizeof(float) * d);
172
183
  }
173
184
 
@@ -187,7 +198,8 @@ void IndexSVSVamana::reset() {
187
198
  }
188
199
  }
189
200
  stored_vectors.clear();
190
- stored_vectors_valid = true;
201
+ stored_vectors.shrink_to_fit();
202
+ stored_vectors_valid = store_vectors;
191
203
  mmap_owner.reset(); // Release the memory mapping
192
204
  is_trained = false;
193
205
  ntotal = 0;
@@ -90,8 +90,9 @@ inline svs_runtime::StorageKind to_svs_storage_kind(SVSStorageKind kind) {
90
90
  }
91
91
 
92
92
  struct IndexSVSVamana : Index {
93
- size_t graph_max_degree;
94
- size_t prune_to;
93
+ // Same defaults as IndexSVSVamana(d, /*degree=*/64).
94
+ size_t graph_max_degree = 64;
95
+ size_t prune_to = 60;
95
96
  float alpha = 1.2;
96
97
  size_t search_window_size = 10;
97
98
  size_t search_buffer_capacity = 10;
@@ -111,7 +112,8 @@ struct IndexSVSVamana : Index {
111
112
  size_t degree,
112
113
  MetricType metric = METRIC_L2,
113
114
  SVSStorageKind storage = SVSStorageKind::SVS_FP32,
114
- bool is_static = false);
115
+ bool is_static = false,
116
+ bool store_vectors = true);
115
117
 
116
118
  ~IndexSVSVamana() override;
117
119
 
@@ -164,6 +166,12 @@ struct IndexSVSVamana : Index {
164
166
  std::vector<float> stored_vectors;
165
167
  bool stored_vectors_valid{true};
166
168
 
169
+ // Set to false before the first add() to skip the stored_vectors copy,
170
+ // saving ntotal * d * 4 bytes at the cost of reconstruct() support and
171
+ // hence of use as an IVF coarse quantizer. Clearing it after vectors have
172
+ // been added drops the copy, which can no longer be aligned with the ids.
173
+ bool store_vectors{true};
174
+
167
175
  protected:
168
176
  /* Initializes the implementation. For static indexes the data is consumed
169
177
  at build time; for dynamic indexes n/x are ignored and add() populates
@@ -34,7 +34,9 @@ IndexSVSVamanaLVQ::IndexSVSVamanaLVQ(
34
34
  size_t degree,
35
35
  MetricType metric,
36
36
  SVSStorageKind storage,
37
- bool is_static)
38
- : IndexSVSVamana(d, degree, metric, storage, is_static) {}
37
+ bool is_static,
38
+ bool store_vectors)
39
+ : IndexSVSVamana(d, degree, metric, storage, is_static, store_vectors) {
40
+ }
39
41
 
40
42
  } // namespace faiss
@@ -35,7 +35,8 @@ struct IndexSVSVamanaLVQ : IndexSVSVamana {
35
35
  size_t degree,
36
36
  MetricType metric = METRIC_L2,
37
37
  SVSStorageKind storage = SVSStorageKind::SVS_LVQ4x0,
38
- bool is_static = false);
38
+ bool is_static = false,
39
+ bool store_vectors = true);
39
40
 
40
41
  ~IndexSVSVamanaLVQ() override = default;
41
42
  };
@@ -44,8 +44,15 @@ IndexSVSVamanaLeanVec::IndexSVSVamanaLeanVec(
44
44
  MetricType metric,
45
45
  size_t leanvec_dims,
46
46
  SVSStorageKind storage_kind,
47
- bool is_static)
48
- : IndexSVSVamana(d, degree, metric, storage_kind, is_static) {
47
+ bool is_static,
48
+ bool store_vectors)
49
+ : IndexSVSVamana(
50
+ d,
51
+ degree,
52
+ metric,
53
+ storage_kind,
54
+ is_static,
55
+ store_vectors) {
49
56
  is_trained = false;
50
57
  leanvec_d = leanvec_dims == 0 ? d / 2 : leanvec_dims;
51
58
  }
@@ -36,7 +36,8 @@ struct IndexSVSVamanaLeanVec : IndexSVSVamana {
36
36
  MetricType metric = METRIC_L2,
37
37
  size_t leanvec_dims = 0,
38
38
  SVSStorageKind storage = SVSStorageKind::SVS_LeanVec4x4,
39
- bool is_static = false);
39
+ bool is_static = false,
40
+ bool store_vectors = true);
40
41
 
41
42
  ~IndexSVSVamanaLeanVec() override;
42
43
 
@@ -117,6 +117,8 @@ template <class T, int A = 32>
117
117
  struct AlignedTable {
118
118
  AlignedTableTightAlloc<T, A> tab;
119
119
  size_t numel = 0;
120
+ // If true, resize() reallocates only when n is larger than the capacity.
121
+ bool keep_capacity = false;
120
122
 
121
123
  static size_t round_capacity(size_t n) {
122
124
  if (n == 0) {
@@ -141,7 +143,10 @@ struct AlignedTable {
141
143
  }
142
144
 
143
145
  void resize(size_t n) {
144
- tab.resize(round_capacity(n));
146
+ const size_t capacity = round_capacity(n);
147
+ if (!keep_capacity || capacity > tab.numel) {
148
+ tab.resize(capacity);
149
+ }
145
150
  numel = n;
146
151
  }
147
152
 
@@ -194,10 +194,11 @@ void merge_knn_results(
194
194
  const idx_t* I_in = all_labels + i * k;
195
195
  int heap_size = 0;
196
196
 
197
- // push the first element of each shard (if not -1)
197
+ // push the first element of each shard (if not -1). A caller id
198
+ // may be negative, so only -1 marks a missing result.
198
199
  for (long s = 0; s < static_cast<long>(nshard); s++) {
199
200
  pointer[s] = 0;
200
- if (I_in[stride * s] >= 0) {
201
+ if (I_in[stride * s] != -1) {
201
202
  heap_push<C>(
202
203
  ++heap_size,
203
204
  heap_vals,
@@ -221,7 +222,7 @@ void merge_knn_results(
221
222
  // pop from shard, advance pointer for this shard
222
223
  heap_pop<C>(heap_size--, heap_vals, shard_ids);
223
224
  p++;
224
- if (static_cast<size_t>(p) < k && I_in[stride * s + p] >= 0) {
225
+ if (static_cast<size_t>(p) < k && I_in[stride * s + p] != -1) {
225
226
  heap_push<C>(
226
227
  ++heap_size,
227
228
  heap_vals,
@@ -21,19 +21,30 @@ using simd8uint32 = simd8uint32_tpl<SINGLE_SIMD_LEVEL_256>;
21
21
  using simd16uint16 = simd16uint16_tpl<SINGLE_SIMD_LEVEL_256>;
22
22
 
23
23
  // HeapWithBucketsForHamming32 uses simd8uint32 under the
24
- // hood.
25
-
26
- template <typename C, uint32_t NBUCKETS, uint32_t N, typename HammingComputerT>
24
+ // hood. SL is the SIMD level of the file that uses it, so each level compiles
25
+ // its own copy under its own name.
26
+
27
+ template <
28
+ typename C,
29
+ uint32_t NBUCKETS,
30
+ uint32_t N,
31
+ typename HammingComputerT,
32
+ SIMDLevel SL>
27
33
  struct HeapWithBucketsForHamming32 {
28
34
  // this case was not implemented yet.
29
35
  };
30
36
 
31
- template <uint32_t NBUCKETS, uint32_t N, typename HammingComputerT>
37
+ template <
38
+ uint32_t NBUCKETS,
39
+ uint32_t N,
40
+ typename HammingComputerT,
41
+ SIMDLevel SL>
32
42
  struct HeapWithBucketsForHamming32<
33
43
  CMax<int, int64_t>,
34
44
  NBUCKETS,
35
45
  N,
36
- HammingComputerT> {
46
+ HammingComputerT,
47
+ SL> {
37
48
  static constexpr uint32_t NBUCKETS_8 = NBUCKETS / 8;
38
49
  static_assert(
39
50
  (NBUCKETS) > 0 && ((NBUCKETS % 8) == 0),
@@ -14,6 +14,10 @@
14
14
  #include <immintrin.h>
15
15
  #endif
16
16
 
17
+ #if defined(__riscv_vector)
18
+ #include <riscv_vector.h>
19
+ #endif
20
+
17
21
  namespace faiss {
18
22
 
19
23
  namespace {
@@ -38,6 +42,8 @@ inline float decode_bf16(const uint16_t v) {
38
42
  return fp.as_f32;
39
43
  }
40
44
 
45
+ namespace {
46
+
41
47
  inline void encode_bf16_simd(const float* src, uint16_t* dst, size_t n) {
42
48
  size_t i = 0;
43
49
  #ifdef __AVX512BF16__
@@ -61,10 +67,40 @@ inline void decode_bf16_simd(const uint16_t* src, float* dst, size_t n) {
61
67
  w = _mm512_slli_epi32(w, 16);
62
68
  _mm512_storeu_ps(dst + i, _mm512_castsi512_ps(w));
63
69
  }
70
+ #elif defined(__riscv_vector)
71
+ if (n == 1) {
72
+ dst[0] = decode_bf16(src[0]);
73
+ return;
74
+ }
75
+ if (n < 16) {
76
+ for (; i < n; ++i) {
77
+ dst[i] = decode_bf16(src[i]);
78
+ }
79
+ return;
80
+ }
81
+ const size_t vlmax = __riscv_vsetvlmax_e16m1();
82
+ for (; i + vlmax <= n; i += vlmax) {
83
+ vuint16m1_t v = __riscv_vle16_v_u16m1(src + i, vlmax);
84
+ vuint32m2_t w = __riscv_vzext_vf2_u32m2(v, vlmax);
85
+ w = __riscv_vsll_vx_u32m2(w, 16, vlmax);
86
+ __riscv_vse32_v_f32m2(
87
+ dst + i, __riscv_vreinterpret_v_u32m2_f32m2(w), vlmax);
88
+ }
89
+ if (i < n) {
90
+ const size_t vl = __riscv_vsetvl_e16m1(n - i);
91
+ vuint16m1_t v = __riscv_vle16_v_u16m1(src + i, vl);
92
+ vuint32m2_t w = __riscv_vzext_vf2_u32m2(v, vl);
93
+ w = __riscv_vsll_vx_u32m2(w, 16, vl);
94
+ __riscv_vse32_v_f32m2(
95
+ dst + i, __riscv_vreinterpret_v_u32m2_f32m2(w), vl);
96
+ i += vl;
97
+ }
64
98
  #endif
65
99
  for (; i < n; i++) {
66
100
  dst[i] = decode_bf16(src[i]);
67
101
  }
68
102
  }
69
103
 
104
+ } // namespace
105
+
70
106
  } // namespace faiss
@@ -26,6 +26,7 @@
26
26
  #include <faiss/utils/distances_dispatch.h>
27
27
  #include <faiss/utils/distances_fused/distances_fused.h>
28
28
  #include <faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h>
29
+ #include <faiss/utils/utils.h>
29
30
 
30
31
  #ifndef FINTEGER
31
32
  #define FINTEGER long
@@ -361,10 +362,11 @@ void exhaustive_L2sqr_seq(
361
362
  }
362
363
  }
363
364
 
364
- /** Find the nearest neighbors for nx queries in a set of ny vectors */
365
- template <class BlockResultHandler>
366
- void exhaustive_inner_product_blas(
367
- const float* x,
365
+ /** Find the nearest neighbors for nx queries (see Fp32QueryTiles) in a set
366
+ * of ny vectors */
367
+ template <class BlockResultHandler, class QueryTiles>
368
+ void exhaustive_inner_product_blas_tiles(
369
+ QueryTiles& queries,
368
370
  const float* y,
369
371
  size_t d,
370
372
  size_t nx,
@@ -386,6 +388,8 @@ void exhaustive_inner_product_blas(
386
388
  i1 = nx;
387
389
  }
388
390
 
391
+ const float* xt = queries.tile(i0, i1, nullptr);
392
+
389
393
  res.begin_multiple(i0, i1);
390
394
 
391
395
  for (size_t j0 = 0; j0 < ny; j0 += bs_y) {
@@ -405,7 +409,7 @@ void exhaustive_inner_product_blas(
405
409
  &one,
406
410
  y + j0 * d,
407
411
  &di,
408
- x + i0 * d,
412
+ xt,
409
413
  &di,
410
414
  &zero,
411
415
  ip_block.get(),
@@ -421,9 +425,9 @@ void exhaustive_inner_product_blas(
421
425
 
422
426
  // distance correction is an operator that can be applied to transform
423
427
  // the distances
424
- template <class BlockResultHandler>
425
- void exhaustive_L2sqr_blas_default_impl(
426
- const float* x,
428
+ template <class BlockResultHandler, class QueryTiles>
429
+ void exhaustive_L2sqr_blas_tiles(
430
+ QueryTiles& queries,
427
431
  const float* y,
428
432
  size_t d,
429
433
  size_t nx,
@@ -443,7 +447,7 @@ void exhaustive_L2sqr_blas_default_impl(
443
447
  std::unique_ptr<float[]> x_norms(new float[nx]);
444
448
  std::unique_ptr<float[]> del2;
445
449
 
446
- fvec_norms_L2sqr(x_norms.get(), x, d, nx);
450
+ queries.prepare_norms(nx, x_norms.get());
447
451
 
448
452
  if (!y_norms) {
449
453
  float* y_norms2 = new float[ny];
@@ -458,6 +462,8 @@ void exhaustive_L2sqr_blas_default_impl(
458
462
  i1 = nx;
459
463
  }
460
464
 
465
+ const float* xt = queries.tile(i0, i1, x_norms.get());
466
+
461
467
  res.begin_multiple(i0, i1);
462
468
 
463
469
  for (size_t j0 = 0; j0 < ny; j0 += bs_y) {
@@ -477,7 +483,7 @@ void exhaustive_L2sqr_blas_default_impl(
477
483
  &one,
478
484
  y + j0 * d,
479
485
  &di,
480
- x + i0 * d,
486
+ xt,
481
487
  &di,
482
488
  &zero,
483
489
  ip_block.get(),
@@ -510,6 +516,31 @@ void exhaustive_L2sqr_blas_default_impl(
510
516
  }
511
517
  }
512
518
 
519
+ template <class BlockResultHandler>
520
+ void exhaustive_inner_product_blas(
521
+ const float* x,
522
+ const float* y,
523
+ size_t d,
524
+ size_t nx,
525
+ size_t ny,
526
+ BlockResultHandler& res) {
527
+ Fp32QueryTiles queries{x, d};
528
+ exhaustive_inner_product_blas_tiles(queries, y, d, nx, ny, res);
529
+ }
530
+
531
+ template <class BlockResultHandler>
532
+ void exhaustive_L2sqr_blas_default_impl(
533
+ const float* x,
534
+ const float* y,
535
+ size_t d,
536
+ size_t nx,
537
+ size_t ny,
538
+ BlockResultHandler& res,
539
+ const float* y_norms = nullptr) {
540
+ Fp32QueryTiles queries{x, d};
541
+ exhaustive_L2sqr_blas_tiles(queries, y, d, nx, ny, res, y_norms);
542
+ }
543
+
513
544
  template <class BlockResultHandler>
514
545
  void exhaustive_L2sqr_blas(
515
546
  const float* x,
@@ -541,17 +572,57 @@ void exhaustive_L2sqr_blas<Top1BlockResultHandler<CMax<float, int64_t>>>(
541
572
  return;
542
573
  }
543
574
 
544
- with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_A1>([&]<SIMDLevel SL>() {
545
- if constexpr (
546
- SL == SIMDLevel::AVX2 || SL == SIMDLevel::AVX512 ||
547
- SL == SIMDLevel::ARM_SVE) {
548
- exhaustive_L2sqr_blas_cmax<SL>(x, y, d, nx, ny, res, y_norms);
549
- } else {
550
- exhaustive_L2sqr_blas_default_impl<
551
- Top1BlockResultHandler<CMax<float, int64_t>>>(
552
- x, y, d, nx, ny, res, y_norms);
553
- }
554
- });
575
+ with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_WITH_SVE>(
576
+ [&]<SIMDLevel SL>() {
577
+ if constexpr (
578
+ SL == SIMDLevel::AVX2 || SL == SIMDLevel::AVX512 ||
579
+ SL == SIMDLevel::ARM_SVE) {
580
+ exhaustive_L2sqr_blas_cmax<SL>(
581
+ x, y, d, nx, ny, res, y_norms);
582
+ } else {
583
+ exhaustive_L2sqr_blas_default_impl<
584
+ Top1BlockResultHandler<CMax<float, int64_t>>>(
585
+ x, y, d, nx, ny, res, y_norms);
586
+ }
587
+ });
588
+ }
589
+
590
+ /// exhaustive_L2sqr_blas for packed IEEE binary16 queries
591
+ template <class BlockResultHandler>
592
+ void exhaustive_L2sqr_blas_fp16(
593
+ const uint16_t* x,
594
+ const float* y,
595
+ size_t d,
596
+ size_t nx,
597
+ size_t ny,
598
+ BlockResultHandler& res,
599
+ const float* y_norms) {
600
+ Fp16QueryTiles queries(x, d, nx);
601
+ exhaustive_L2sqr_blas_tiles(queries, y, d, nx, ny, res, y_norms);
602
+ }
603
+
604
+ template <>
605
+ void exhaustive_L2sqr_blas_fp16<Top1BlockResultHandler<CMax<float, int64_t>>>(
606
+ const uint16_t* x,
607
+ const float* y,
608
+ size_t d,
609
+ size_t nx,
610
+ size_t ny,
611
+ Top1BlockResultHandler<CMax<float, int64_t>>& res,
612
+ const float* y_norms) {
613
+ with_selected_simd_levels<AVAILABLE_SIMD_LEVELS_BASE_WITH_SVE>(
614
+ [&]<SIMDLevel SL>() {
615
+ if constexpr (
616
+ SL == SIMDLevel::AVX2 || SL == SIMDLevel::AVX512 ||
617
+ SL == SIMDLevel::ARM_SVE) {
618
+ exhaustive_L2sqr_blas_cmax_fp16<SL>(
619
+ x, y, d, nx, ny, res, y_norms);
620
+ } else {
621
+ Fp16QueryTiles queries(x, d, nx);
622
+ exhaustive_L2sqr_blas_tiles(
623
+ queries, y, d, nx, ny, res, y_norms);
624
+ }
625
+ });
555
626
  }
556
627
 
557
628
  struct Run_search_inner_product {
@@ -563,7 +634,11 @@ struct Run_search_inner_product {
563
634
  size_t d,
564
635
  size_t nx,
565
636
  size_t ny) {
566
- if (res.sel ||
637
+ // ny == 0 goes to the sequential path: it guards only on nx, so its
638
+ // per-query begin()/end() still runs and each handler writes its own
639
+ // neutral distance and -1 label. The BLAS path instead returns early
640
+ // on ny == 0, before the handler is initialized.
641
+ if (res.sel || ny == 0 ||
567
642
  nx * d < static_cast<size_t>(distance_compute_blas_threshold)) {
568
643
  exhaustive_inner_product_seq(x, y, d, nx, ny, res);
569
644
  } else {
@@ -582,7 +657,8 @@ struct Run_search_L2sqr {
582
657
  size_t nx,
583
658
  size_t ny,
584
659
  const float* y_norm2) {
585
- if (res.sel ||
660
+ // See the note on ny == 0 in Run_search_inner_product.
661
+ if (res.sel || ny == 0 ||
586
662
  nx * d < static_cast<size_t>(distance_compute_blas_threshold)) {
587
663
  exhaustive_L2sqr_seq(x, y, d, nx, ny, res);
588
664
  } else {
@@ -591,6 +667,35 @@ struct Run_search_L2sqr {
591
667
  }
592
668
  };
593
669
 
670
+ // fp16 consumers only run the BLAS kernels, see use_fp16_blas_kernels
671
+ struct Run_search_inner_product_fp16 {
672
+ using T = void;
673
+ template <class BlockResultHandler>
674
+ void f(BlockResultHandler& res,
675
+ const uint16_t* x,
676
+ const float* y,
677
+ size_t d,
678
+ size_t nx,
679
+ size_t ny) {
680
+ Fp16QueryTiles queries(x, d, nx);
681
+ exhaustive_inner_product_blas_tiles(queries, y, d, nx, ny, res);
682
+ }
683
+ };
684
+
685
+ struct Run_search_L2sqr_fp16 {
686
+ using T = void;
687
+ template <class BlockResultHandler>
688
+ void f(BlockResultHandler& res,
689
+ const uint16_t* x,
690
+ const float* y,
691
+ size_t d,
692
+ size_t nx,
693
+ size_t ny,
694
+ const float* y_norm2) {
695
+ exhaustive_L2sqr_blas_fp16(x, y, d, nx, ny, res, y_norm2);
696
+ }
697
+ };
698
+
594
699
  } // anonymous namespace
595
700
 
596
701
  /*******************************************************
@@ -623,7 +728,7 @@ static void knn_db_parallel_impl(
623
728
  int nt = omp_get_max_threads();
624
729
  const size_t bs_y = distance_compute_blas_database_bs;
625
730
 
626
- // Per-thread result heaps: nt threads x nx queries x k results
731
+ // Result heaps: nt segments x nx queries x k results
627
732
  std::vector<T> all_dis(static_cast<size_t>(nt) * nx * k);
628
733
  std::vector<TI> all_ids(static_cast<size_t>(nt) * nx * k);
629
734
 
@@ -642,9 +747,8 @@ static void knn_db_parallel_impl(
642
747
  }
643
748
  }
644
749
 
645
- #pragma omp parallel num_threads(nt)
646
- {
647
- int tid = omp_get_thread_num();
750
+ #pragma omp parallel for
751
+ for (int tid = 0; tid < nt; tid++) {
648
752
  size_t j_begin = static_cast<size_t>(tid) * ny / nt;
649
753
  size_t j_end = static_cast<size_t>(tid + 1) * ny / nt;
650
754
  size_t local_ny = j_end - j_begin;
@@ -727,7 +831,7 @@ static void knn_db_parallel_impl(
727
831
  }
728
832
  }
729
833
 
730
- // Merge per-thread heaps into output, parallelized over queries
834
+ // Merge per-segment heaps into output, parallelized over queries
731
835
  #pragma omp parallel for
732
836
  for (int64_t i = 0; i < static_cast<int64_t>(nx); i++) {
733
837
  heap_heapify<C>(k, vals + i * k, ids + i * k);
@@ -887,6 +991,120 @@ void knn_L2sqr(
887
991
  knn_L2sqr(x, y, d, nx, ny, res->k, res->val, res->ids, y_norm2, sel);
888
992
  }
889
993
 
994
+ namespace {
995
+
996
+ /** Whether fp16 queries go to the BLAS kernels directly.
997
+ *
998
+ * Other cases widen chunks of queries and run the fp32 search: selectors,
999
+ * sequential and database-parallel searches, and small d, where the fused
1000
+ * top-1 kernels need fp32 rows.
1001
+ */
1002
+ bool use_fp16_blas_kernels(
1003
+ size_t d,
1004
+ size_t nx,
1005
+ size_t ny,
1006
+ const IDSelector* sel) {
1007
+ return !sel && ny > 0 && d > 32 &&
1008
+ nx * d >= static_cast<size_t>(distance_compute_blas_threshold) &&
1009
+ !should_use_db_parallel(nx, ny, sel);
1010
+ }
1011
+
1012
+ /// widens chunks of queries and calls search(x_chunk, n_chunk, i0) on them
1013
+ template <class Fp32Search>
1014
+ void search_fp16_by_chunks(
1015
+ const uint16_t* x,
1016
+ size_t d,
1017
+ size_t nx,
1018
+ const Fp32Search& search) {
1019
+ // a multiple of the query tile, so the fp32 kernels see the same tiles
1020
+ const size_t bs_x = std::max(distance_compute_blas_query_bs, 1);
1021
+ const size_t chunk = bs_x *
1022
+ std::max<size_t>(1, (size_t(8) << 20) / (bs_x * d * sizeof(float)));
1023
+ std::vector<float> buffer(std::min(nx, chunk) * d);
1024
+ for (size_t i0 = 0; i0 < nx; i0 += chunk) {
1025
+ const size_t i1 = std::min(nx, i0 + chunk);
1026
+ fp16_to_fp32((i1 - i0) * d, x + i0 * d, buffer.data());
1027
+ search(buffer.data(), i1 - i0, i0);
1028
+ }
1029
+ }
1030
+
1031
+ } // namespace
1032
+
1033
+ void knn_inner_product_fp16(
1034
+ const uint16_t* x,
1035
+ const float* y,
1036
+ size_t d,
1037
+ size_t nx,
1038
+ size_t ny,
1039
+ size_t k,
1040
+ float* vals,
1041
+ int64_t* ids,
1042
+ const IDSelector* sel) {
1043
+ if (!use_fp16_blas_kernels(d, nx, ny, sel)) {
1044
+ search_fp16_by_chunks(
1045
+ x, d, nx, [&](const float* xc, size_t nc, size_t i0) {
1046
+ knn_inner_product(
1047
+ xc,
1048
+ y,
1049
+ d,
1050
+ nc,
1051
+ ny,
1052
+ k,
1053
+ vals + i0 * k,
1054
+ ids + i0 * k,
1055
+ sel);
1056
+ });
1057
+ return;
1058
+ }
1059
+ Run_search_inner_product_fp16 r;
1060
+ dispatch_knn_ResultHandler(
1061
+ nx,
1062
+ vals,
1063
+ ids,
1064
+ k,
1065
+ METRIC_INNER_PRODUCT,
1066
+ nullptr,
1067
+ r,
1068
+ x,
1069
+ y,
1070
+ d,
1071
+ nx,
1072
+ ny);
1073
+ }
1074
+
1075
+ void knn_L2sqr_fp16(
1076
+ const uint16_t* x,
1077
+ const float* y,
1078
+ size_t d,
1079
+ size_t nx,
1080
+ size_t ny,
1081
+ size_t k,
1082
+ float* vals,
1083
+ int64_t* ids,
1084
+ const float* y_norm2,
1085
+ const IDSelector* sel) {
1086
+ if (!use_fp16_blas_kernels(d, nx, ny, sel)) {
1087
+ search_fp16_by_chunks(
1088
+ x, d, nx, [&](const float* xc, size_t nc, size_t i0) {
1089
+ knn_L2sqr(
1090
+ xc,
1091
+ y,
1092
+ d,
1093
+ nc,
1094
+ ny,
1095
+ k,
1096
+ vals + i0 * k,
1097
+ ids + i0 * k,
1098
+ y_norm2,
1099
+ sel);
1100
+ });
1101
+ return;
1102
+ }
1103
+ Run_search_L2sqr_fp16 r;
1104
+ dispatch_knn_ResultHandler(
1105
+ nx, vals, ids, k, METRIC_L2, nullptr, r, x, y, d, nx, ny, y_norm2);
1106
+ }
1107
+
890
1108
  /***************************************************************************
891
1109
  * Range search
892
1110
  ***************************************************************************/