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
@@ -17,6 +17,7 @@
17
17
  #include <limits>
18
18
  #include <memory>
19
19
 
20
+ #include <faiss/IndexBinaryHNSW.h>
20
21
  #include <faiss/IndexFlat.h>
21
22
  #include <faiss/IndexLSH.h>
22
23
  #include <faiss/impl/AuxIndexStructures.h>
@@ -192,7 +193,7 @@ void IndexBinaryIVF::reconstruct_n(idx_t i0, idx_t ni, uint8_t* recons) const {
192
193
  continue;
193
194
  }
194
195
 
195
- uint8_t* reconstructed = recons + (id - i0) * d;
196
+ uint8_t* reconstructed = recons + (id - i0) * code_size;
196
197
  reconstruct_from_offset(list_no, offset, reconstructed);
197
198
  }
198
199
  }
@@ -233,10 +234,10 @@ void IndexBinaryIVF::search_and_reconstruct(
233
234
  for (idx_t j = 0; j < k; ++j) {
234
235
  idx_t ij = i * k + j;
235
236
  idx_t key = labels[ij];
236
- uint8_t* reconstructed = recons + ij * d;
237
+ uint8_t* reconstructed = recons + ij * code_size;
237
238
  if (key < 0) {
238
239
  // Fill with NaNs
239
- memset(reconstructed, -1, sizeof(*reconstructed) * d);
240
+ memset(reconstructed, -1, code_size);
240
241
  } else {
241
242
  int list_no = key >> 32;
242
243
  int offset = key & 0xffffffff;
@@ -254,6 +255,21 @@ void IndexBinaryIVF::reconstruct_from_offset(
254
255
  idx_t list_no,
255
256
  idx_t offset,
256
257
  uint8_t* recons) const {
258
+ FAISS_THROW_IF_NOT_FMT(
259
+ list_no >= 0 &&
260
+ static_cast<uint64_t>(list_no) <
261
+ static_cast<uint64_t>(nlist),
262
+ "Invalid list_no %" PRId64 " (nlist=%zd)",
263
+ list_no,
264
+ nlist);
265
+ FAISS_THROW_IF_NOT_FMT(
266
+ offset >= 0 &&
267
+ static_cast<uint64_t>(offset) <
268
+ static_cast<uint64_t>(invlists->list_size(list_no)),
269
+ "Invalid offset %" PRId64 " for list %" PRId64 " (size=%zd)",
270
+ offset,
271
+ list_no,
272
+ invlists->list_size(list_no));
257
273
  memcpy(recons,
258
274
  InvertedLists::ScopedCodes(invlists, list_no, offset).get(),
259
275
  code_size);
@@ -308,6 +324,24 @@ void IndexBinaryIVF::train(idx_t n, const uint8_t* x) {
308
324
 
309
325
  quantizer->add(clus.k, x_b.get());
310
326
  quantizer->is_trained = true;
327
+
328
+ auto* hnsw = dynamic_cast<IndexBinaryHNSW*>(quantizer);
329
+ if (hnsw && hnsw->level_0_link_samples_per_node > 0) {
330
+ const idx_t samples = std::min<idx_t>(
331
+ n, idx_t(hnsw->level_0_link_samples_per_node) * nlist);
332
+ const HNSWLevel0LinkStats stats =
333
+ hnsw->add_level_0_links_from_sample(samples, x);
334
+ if (verbose) {
335
+ printf("HNSW quantizer level-0 links from %" PRId64
336
+ " samples: %zd sinks found, %zd remaining, %zd links "
337
+ "added, %zd replaced\n",
338
+ samples,
339
+ stats.n_sinks_found,
340
+ stats.n_sinks_remaining,
341
+ stats.n_links_added,
342
+ stats.n_links_replaced);
343
+ }
344
+ }
311
345
  }
312
346
 
313
347
  is_trained = true;
@@ -348,10 +382,6 @@ void IndexBinaryIVF::replace_invlists(InvertedLists* il, bool own) {
348
382
  own_invlists = own;
349
383
  }
350
384
 
351
- // IVFBinaryScannerL2, search_knn_hamming_count, BlockSearch,
352
- // BlockSearchVariableK, search_knn_hamming_per_invlist are now in
353
- // impl/binary_hamming/IndexBinaryIVF_impl.h (compiled per-ISA)
354
-
355
385
  namespace {
356
386
 
357
387
  void search_knn_hamming_heap(
@@ -461,13 +491,9 @@ void search_knn_hamming_heap(
461
491
 
462
492
  } // anonymous namespace
463
493
 
464
- // The remaining template code (search_knn_hamming_count,
465
- // search_knn_hamming_per_invlist, etc.) has been moved to
466
- // impl/binary_hamming/IndexBinaryIVF_impl.h
467
-
468
494
  BinaryInvertedListScanner* IndexBinaryIVF::get_InvertedListScanner(
469
495
  bool store_pairs) const {
470
- return with_simd_level([&]<SIMDLevel SL>() {
496
+ return with_simd_level_with_vpopcnt([&]<SIMDLevel SL>() {
471
497
  return make_binary_ivf_scanner_fixSL<SL>(code_size, store_pairs);
472
498
  });
473
499
  }
@@ -483,7 +509,7 @@ void IndexBinaryIVF::search_preassigned(
483
509
  bool store_pairs,
484
510
  const IVFSearchParameters* params) const {
485
511
  if (per_invlist_search) {
486
- with_simd_level([&]<SIMDLevel SL>() {
512
+ with_simd_level_with_vpopcnt([&]<SIMDLevel SL>() {
487
513
  search_knn_hamming_per_invlist_fixSL<SL>(
488
514
  code_size,
489
515
  this,
@@ -501,7 +527,7 @@ void IndexBinaryIVF::search_preassigned(
501
527
  search_knn_hamming_heap(
502
528
  this, n, x, k, cidx, cdis, dis, idx, store_pairs, params);
503
529
  } else {
504
- with_simd_level([&]<SIMDLevel SL>() {
530
+ with_simd_level_with_vpopcnt([&]<SIMDLevel SL>() {
505
531
  search_knn_hamming_count_fixSL<SL>(
506
532
  code_size,
507
533
  store_pairs,
@@ -232,7 +232,14 @@ struct BinaryInvertedListScanner {
232
232
  virtual uint32_t distance_to_code(const uint8_t* code) const = 0;
233
233
 
234
234
  /** compute the distances to codes. (distances, labels) should be
235
- * organized as a min- or max-heap
235
+ * organized as a max-heap: the scan accepts a code whose distance is
236
+ * below distances[0] and replaces the top. A min-heap caller reads its
237
+ * neutral value as a huge unsigned bound and gets wrong results.
238
+
239
+ *
240
+ * The heap top is the only bound. To keep the k nearest codes inside a
241
+ * radius, seed every heap slot with that radius instead of the neutral
242
+ * value; an unfilled slot keeps its label of -1.
236
243
  *
237
244
  * @param n number of codes to scan
238
245
  * @param codes codes to scan (n * code_size)
@@ -55,7 +55,7 @@ IndexFastScan::IndexFastScan()
55
55
 
56
56
  void IndexFastScan::reset() {
57
57
  codes.resize(0);
58
- ntotal = 0;
58
+ ntotal = ntotal2 = 0;
59
59
  }
60
60
 
61
61
  void IndexFastScan::add(idx_t n, const float* x) {
@@ -267,15 +267,16 @@ void IndexFastScan::search(
267
267
  float* distances,
268
268
  idx_t* labels,
269
269
  const SearchParameters* params) const {
270
- FAISS_THROW_IF_MSG(params, "search params not supported for this index");
271
270
  FAISS_THROW_IF_NOT(k > 0);
271
+ const IDSelector* sel = params ? params->sel : nullptr;
272
272
 
273
273
  FastScanDistancePostProcessing empty_context{};
274
274
  if (metric_type == METRIC_L2) {
275
- search_dispatch_implem<true>(n, x, k, distances, labels, empty_context);
275
+ search_dispatch_implem<true>(
276
+ n, x, k, distances, labels, empty_context, sel);
276
277
  } else {
277
278
  search_dispatch_implem<false>(
278
- n, x, k, distances, labels, empty_context);
279
+ n, x, k, distances, labels, empty_context, sel);
279
280
  }
280
281
  }
281
282
 
@@ -286,7 +287,8 @@ void IndexFastScan::search_dispatch_implem(
286
287
  idx_t k,
287
288
  float* distances,
288
289
  idx_t* labels,
289
- const FastScanDistancePostProcessing& context) const {
290
+ const FastScanDistancePostProcessing& context,
291
+ const IDSelector* sel) const {
290
292
  using Cfloat = typename std::conditional<
291
293
  is_max,
292
294
  CMax<float, int64_t>,
@@ -317,6 +319,10 @@ void IndexFastScan::search_dispatch_implem(
317
319
  FAISS_THROW_MSG("not implemented");
318
320
  } else if (implem == 2 || implem == 3 || implem == 4) {
319
321
  FAISS_THROW_IF_NOT(orig_codes);
322
+ FAISS_THROW_IF_MSG(
323
+ sel,
324
+ "IDSelector is not supported by implem 2/3/4; leave implem at "
325
+ "0 so a selector-aware kernel is chosen");
320
326
  search_implem_234<Cfloat>(n, x, k, distances, labels, context);
321
327
  } else if (impl >= 12 && impl <= 15) {
322
328
  FAISS_THROW_IF_NOT(ntotal < INT_MAX);
@@ -328,9 +334,11 @@ void IndexFastScan::search_dispatch_implem(
328
334
  // - OpenMP disabled (omp_get_max_threads() = 1)
329
335
  if (nt < 2) {
330
336
  if (impl == 12 || impl == 13) {
331
- search_implem_12<C>(n, x, k, distances, labels, impl, context);
337
+ search_implem_12<C>(
338
+ n, x, k, distances, labels, impl, context, sel);
332
339
  } else {
333
- search_implem_14<C>(n, x, k, distances, labels, impl, context);
340
+ search_implem_14<C>(
341
+ n, x, k, distances, labels, impl, context, sel);
334
342
  }
335
343
  } else {
336
344
  // explicitly slice over threads
@@ -355,7 +363,8 @@ void IndexFastScan::search_dispatch_implem(
355
363
  dis_i,
356
364
  lab_i,
357
365
  impl,
358
- thread_context);
366
+ thread_context,
367
+ sel);
359
368
  } else {
360
369
  search_implem_14<C>(
361
370
  i1 - i0,
@@ -364,7 +373,8 @@ void IndexFastScan::search_dispatch_implem(
364
373
  dis_i,
365
374
  lab_i,
366
375
  impl,
367
- thread_context);
376
+ thread_context,
377
+ sel);
368
378
  }
369
379
  }
370
380
  }
@@ -440,7 +450,8 @@ void IndexFastScan::search_implem_12(
440
450
  float* distances,
441
451
  idx_t* labels,
442
452
  int impl,
443
- const FastScanDistancePostProcessing& context) const {
453
+ const FastScanDistancePostProcessing& context,
454
+ const IDSelector* sel) const {
444
455
  FAISS_THROW_IF_NOT(bbs == 32);
445
456
 
446
457
  // handle qbs2 blocking by recursive call
@@ -460,7 +471,8 @@ void IndexFastScan::search_implem_12(
460
471
  distances + i0 * k,
461
472
  labels + i0 * k,
462
473
  impl,
463
- sub_context);
474
+ sub_context,
475
+ sel);
464
476
  }
465
477
  return;
466
478
  }
@@ -492,7 +504,7 @@ void IndexFastScan::search_implem_12(
492
504
  FAISS_THROW_IF_NOT(LUT_nq == n);
493
505
 
494
506
  auto scanner = make_knn_scanner(
495
- C::is_max, n, k, ntotal, distances, labels, nullptr, impl, context);
507
+ C::is_max, n, k, ntotal, distances, labels, sel, impl, context);
496
508
  auto* rh = scanner->handler();
497
509
  rh->normalizers = normalizers.get();
498
510
  // Note: skip & 2 previously set handler->disable (run kernel,
@@ -525,7 +537,8 @@ void IndexFastScan::search_implem_14(
525
537
  float* distances,
526
538
  idx_t* labels,
527
539
  int impl,
528
- const FastScanDistancePostProcessing& context) const {
540
+ const FastScanDistancePostProcessing& context,
541
+ const IDSelector* sel) const {
529
542
  FAISS_THROW_IF_NOT(bbs % 32 == 0);
530
543
 
531
544
  // The accumulate loop dispatch table only instantiates certain
@@ -553,7 +566,8 @@ void IndexFastScan::search_implem_14(
553
566
  distances + i0 * k,
554
567
  labels + i0 * k,
555
568
  impl,
556
- sub_context);
569
+ sub_context,
570
+ sel);
557
571
  }
558
572
  return;
559
573
  }
@@ -577,7 +591,7 @@ void IndexFastScan::search_implem_14(
577
591
  LUT.get());
578
592
 
579
593
  auto scanner = make_knn_scanner(
580
- C::is_max, n, k, ntotal, distances, labels, nullptr, impl, context);
594
+ C::is_max, n, k, ntotal, distances, labels, sel, impl, context);
581
595
  auto* rh = scanner->handler();
582
596
  rh->normalizers = normalizers.get();
583
597
  // Note: skip & 2 previously set handler->disable (run kernel,
@@ -606,7 +620,8 @@ template void IndexFastScan::search_dispatch_implem<true>(
606
620
  idx_t k,
607
621
  float* distances,
608
622
  idx_t* labels,
609
- const FastScanDistancePostProcessing& context) const;
623
+ const FastScanDistancePostProcessing& context,
624
+ const IDSelector* sel) const;
610
625
 
611
626
  template void IndexFastScan::search_dispatch_implem<false>(
612
627
  idx_t n,
@@ -614,7 +629,8 @@ template void IndexFastScan::search_dispatch_implem<false>(
614
629
  idx_t k,
615
630
  float* distances,
616
631
  idx_t* labels,
617
- const FastScanDistancePostProcessing& context) const;
632
+ const FastScanDistancePostProcessing& context,
633
+ const IDSelector* sel) const;
618
634
 
619
635
  void IndexFastScan::reconstruct(idx_t key, float* recons) const {
620
636
  FAISS_THROW_IF_NOT_FMT(
@@ -155,7 +155,8 @@ struct IndexFastScan : Index {
155
155
  idx_t k,
156
156
  float* distances,
157
157
  idx_t* labels,
158
- const FastScanDistancePostProcessing& context) const;
158
+ const FastScanDistancePostProcessing& context,
159
+ const IDSelector* sel = nullptr) const;
159
160
 
160
161
  template <class Cfloat>
161
162
  void search_implem_234(
@@ -174,7 +175,8 @@ struct IndexFastScan : Index {
174
175
  float* distances,
175
176
  idx_t* labels,
176
177
  int impl,
177
- const FastScanDistancePostProcessing& context) const;
178
+ const FastScanDistancePostProcessing& context,
179
+ const IDSelector* sel = nullptr) const;
178
180
 
179
181
  template <class C>
180
182
  void search_implem_14(
@@ -184,7 +186,8 @@ struct IndexFastScan : Index {
184
186
  float* distances,
185
187
  idx_t* labels,
186
188
  int impl,
187
- const FastScanDistancePostProcessing& context) const;
189
+ const FastScanDistancePostProcessing& context,
190
+ const IDSelector* sel = nullptr) const;
188
191
 
189
192
  /** Reconstruct a vector from its code
190
193
  *
@@ -59,6 +59,40 @@ void IndexFlat::search(
59
59
  }
60
60
  }
61
61
 
62
+ void IndexFlat::search_ex(
63
+ idx_t n,
64
+ const void* x,
65
+ NumericType numeric_type,
66
+ idx_t k,
67
+ float* distances,
68
+ idx_t* labels,
69
+ const SearchParameters* params) const {
70
+ if (numeric_type != NumericType::Float16 ||
71
+ (metric_type != METRIC_L2 && metric_type != METRIC_INNER_PRODUCT)) {
72
+ Index::search_ex(n, x, numeric_type, k, distances, labels, params);
73
+ return;
74
+ }
75
+ FAISS_THROW_IF_NOT(k > 0);
76
+ const IDSelector* sel = params ? params->sel : nullptr;
77
+ const uint16_t* x16 = static_cast<const uint16_t*>(x);
78
+ if (metric_type == METRIC_INNER_PRODUCT) {
79
+ knn_inner_product_fp16(
80
+ x16, get_xb(), d, n, ntotal, k, distances, labels, sel);
81
+ } else {
82
+ knn_L2sqr_fp16(
83
+ x16,
84
+ get_xb(),
85
+ d,
86
+ n,
87
+ ntotal,
88
+ k,
89
+ distances,
90
+ labels,
91
+ nullptr,
92
+ sel);
93
+ }
94
+ }
95
+
62
96
  void IndexFlat::range_search(
63
97
  idx_t n,
64
98
  const float* x,
@@ -270,9 +304,11 @@ struct FlatIPDis : FlatCodesDistanceComputer {
270
304
  FlatCodesDistanceComputer* IndexFlat::get_FlatCodesDistanceComputer() const {
271
305
  FlatCodesDistanceComputer* dc = nullptr;
272
306
  if (metric_type == METRIC_L2) {
273
- with_simd_level([&]<SIMDLevel SL>() { dc = new FlatL2Dis<SL>(*this); });
307
+ with_simd_level_with_sve(
308
+ [&]<SIMDLevel SL>() { dc = new FlatL2Dis<SL>(*this); });
274
309
  } else if (metric_type == METRIC_INNER_PRODUCT) {
275
- with_simd_level([&]<SIMDLevel SL>() { dc = new FlatIPDis<SL>(*this); });
310
+ with_simd_level_with_sve(
311
+ [&]<SIMDLevel SL>() { dc = new FlatIPDis<SL>(*this); });
276
312
  } else {
277
313
  dc = get_extra_distance_computer(d, metric_type, metric_arg, get_xb());
278
314
  }
@@ -404,7 +440,7 @@ FlatCodesDistanceComputer* IndexFlatL2::get_FlatCodesDistanceComputer() const {
404
440
  if (metric_type == METRIC_L2) {
405
441
  if (!cached_l2norms.empty()) {
406
442
  FlatCodesDistanceComputer* dc = nullptr;
407
- with_simd_level([&]<SIMDLevel SL>() {
443
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
408
444
  dc = new FlatL2WithNormsDis<SL>(*this);
409
445
  });
410
446
  return dc;
@@ -444,6 +480,17 @@ void IndexFlat1D::reset() {
444
480
  perm.clear();
445
481
  }
446
482
 
483
+ void IndexFlat1D::search_ex(
484
+ idx_t n,
485
+ const void* x,
486
+ NumericType numeric_type,
487
+ idx_t k,
488
+ float* distances,
489
+ idx_t* labels,
490
+ const SearchParameters* params) const {
491
+ Index::search_ex(n, x, numeric_type, k, distances, labels, params);
492
+ }
493
+
447
494
  void IndexFlat1D::search(
448
495
  idx_t n,
449
496
  const float* x,
@@ -668,6 +715,17 @@ void IndexFlatPanorama::add(idx_t n, const float* x) {
668
715
  pano.compute_cumulative_sums(cum_sums.data(), offset, n, x);
669
716
  }
670
717
 
718
+ void IndexFlatPanorama::search_ex(
719
+ idx_t n,
720
+ const void* x,
721
+ NumericType numeric_type,
722
+ idx_t k,
723
+ float* distances,
724
+ idx_t* labels,
725
+ const SearchParameters* params) const {
726
+ Index::search_ex(n, x, numeric_type, k, distances, labels, params);
727
+ }
728
+
671
729
  void IndexFlatPanorama::search(
672
730
  idx_t n,
673
731
  const float* x,
@@ -704,10 +762,12 @@ void IndexFlatPanorama::reset() {
704
762
  }
705
763
 
706
764
  void IndexFlatPanorama::reconstruct(idx_t key, float* recons) const {
765
+ FAISS_THROW_IF_NOT(key >= 0 && key < ntotal);
707
766
  pano.reconstruct(key, recons, codes.data());
708
767
  }
709
768
 
710
769
  void IndexFlatPanorama::reconstruct_n(idx_t i, idx_t n, float* recons) const {
770
+ FAISS_THROW_IF_NOT(i >= 0 && i <= ntotal && n >= 0 && n <= ntotal - i);
711
771
  Index::reconstruct_n(i, n, recons);
712
772
  }
713
773
 
@@ -784,7 +844,7 @@ void IndexFlatPanorama::search_subset(
784
844
  idx_t k,
785
845
  float* distances,
786
846
  idx_t* labels) const {
787
- with_simd_level([&]<SIMDLevel SL>() {
847
+ with_simd_level_with_sve([&]<SIMDLevel SL>() {
788
848
  with_metric_type(metric_type, [&]<MetricType M>() {
789
849
  constexpr bool is_sim = is_similarity_metric(M);
790
850
  using C = std::conditional_t<
@@ -31,6 +31,17 @@ struct IndexFlat : IndexFlatCodes {
31
31
  idx_t* labels,
32
32
  const SearchParameters* params = nullptr) const override;
33
33
 
34
+ /// Float16 queries with METRIC_L2 / METRIC_INNER_PRODUCT are searched
35
+ /// natively (knn_L2sqr_fp16 / knn_inner_product_fp16).
36
+ void search_ex(
37
+ idx_t n,
38
+ const void* x,
39
+ NumericType numeric_type,
40
+ idx_t k,
41
+ float* distances,
42
+ idx_t* labels,
43
+ const SearchParameters* params = nullptr) const override;
44
+
34
45
  void range_search(
35
46
  idx_t n,
36
47
  const float* x,
@@ -135,6 +146,16 @@ struct IndexFlatPanorama : IndexFlat {
135
146
  idx_t* labels,
136
147
  const SearchParameters* params = nullptr) const override;
137
148
 
149
+ /// widens Float16 queries for search() (Index::search_ex default)
150
+ void search_ex(
151
+ idx_t n,
152
+ const void* x,
153
+ NumericType numeric_type,
154
+ idx_t k,
155
+ float* distances,
156
+ idx_t* labels,
157
+ const SearchParameters* params = nullptr) const override;
158
+
138
159
  void range_search(
139
160
  idx_t n,
140
161
  const float* x,
@@ -176,7 +197,7 @@ struct IndexFlatL2Panorama : IndexFlatPanorama {
176
197
  explicit IndexFlatL2Panorama(
177
198
  idx_t d_in,
178
199
  size_t n_levels_in,
179
- size_t batch_size_in = 512)
200
+ size_t batch_size_in = Panorama::kDefaultBatchSize)
180
201
  : IndexFlatPanorama(d_in, METRIC_L2, n_levels_in, batch_size_in) {}
181
202
  };
182
203
 
@@ -189,7 +210,7 @@ struct IndexFlatIPPanorama : IndexFlatPanorama {
189
210
  explicit IndexFlatIPPanorama(
190
211
  idx_t d_in,
191
212
  size_t n_levels_in,
192
- size_t batch_size_in = 512)
213
+ size_t batch_size_in = Panorama::kDefaultBatchSize)
193
214
  : IndexFlatPanorama(
194
215
  d_in,
195
216
  METRIC_INNER_PRODUCT,
@@ -221,6 +242,16 @@ struct IndexFlat1D : IndexFlatL2 {
221
242
  float* distances,
222
243
  idx_t* labels,
223
244
  const SearchParameters* params = nullptr) const override;
245
+
246
+ /// widens Float16 queries for search() (Index::search_ex default)
247
+ void search_ex(
248
+ idx_t n,
249
+ const void* x,
250
+ NumericType numeric_type,
251
+ idx_t k,
252
+ float* distances,
253
+ idx_t* labels,
254
+ const SearchParameters* params = nullptr) const override;
224
255
  };
225
256
 
226
257
  } // namespace faiss
@@ -45,7 +45,12 @@ void IndexFlatCodes::add_sa_codes(
45
45
  }
46
46
 
47
47
  void IndexFlatCodes::reset() {
48
- codes.clear();
48
+ if (codes.is_owned) {
49
+ codes.clear();
50
+ } else {
51
+ // a view (mmap / zero-copy load) cannot be cleared in place
52
+ codes = MaybeOwnedVector<uint8_t>();
53
+ }
49
54
  ntotal = 0;
50
55
  }
51
56