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
@@ -5,14 +5,19 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ #include <faiss/Clustering.h>
8
9
  #include <faiss/IndexFlat.h>
9
10
  #include <faiss/gpu/GpuIndexFlat.h>
11
+ #include <faiss/gpu/GpuIndexIVFFlat.h>
10
12
  #include <faiss/gpu/StandardGpuResources.h>
11
13
  #include <faiss/gpu/impl/IndexUtils.h>
12
14
  #include <faiss/gpu/test/TestUtils.h>
13
15
  #include <faiss/gpu/utils/DeviceUtils.h>
14
16
  #include <faiss/impl/IDSelector.h>
17
+ #include <faiss/utils/fp16.h>
15
18
  #include <gtest/gtest.h>
19
+ #include <algorithm>
20
+ #include <cstdint>
16
21
  #include <sstream>
17
22
  #include <unordered_map>
18
23
  #include <vector>
@@ -807,6 +812,216 @@ TEST(TestCuvsGpuIndexFlat, IDSelector_IP) {
807
812
  }
808
813
  #endif
809
814
 
815
+ // Packs float vectors as IEEE binary16, the Float16 input of search_ex
816
+ std::vector<uint16_t> toFp16(const std::vector<float>& v) {
817
+ std::vector<uint16_t> out(v.size());
818
+ std::transform(v.begin(), v.end(), out.begin(), [](float f) {
819
+ return faiss::encode_fp16(f);
820
+ });
821
+ return out;
822
+ }
823
+
824
+ std::vector<float> fromFp16(const std::vector<uint16_t>& v) {
825
+ std::vector<float> out(v.size());
826
+ std::transform(v.begin(), v.end(), out.begin(), [](uint16_t h) {
827
+ return faiss::decode_fp16(h);
828
+ });
829
+ return out;
830
+ }
831
+
832
+ // A copy of host data on a GPU device
833
+ struct DeviceCopy {
834
+ DeviceCopy(int device, const void* src, size_t bytes) {
835
+ faiss::gpu::DeviceScope scope(device);
836
+ CUDA_VERIFY(cudaMalloc(&data, bytes));
837
+ CUDA_VERIFY(cudaMemcpy(data, src, bytes, cudaMemcpyHostToDevice));
838
+ }
839
+
840
+ DeviceCopy(const DeviceCopy&) = delete;
841
+ DeviceCopy& operator=(const DeviceCopy&) = delete;
842
+
843
+ ~DeviceCopy() {
844
+ CUDA_VERIFY(cudaFree(data));
845
+ }
846
+
847
+ void* data = nullptr;
848
+ };
849
+
850
+ // search_ex on Float16 queries must return exactly what search returns on the
851
+ // float32 queries rounded to fp16, as the rounded values are what the index
852
+ // computes with, whether the queries are on the host (paged or not) or on the
853
+ // device
854
+ void testSearchExFloat16(faiss::gpu::GpuIndex& index) {
855
+ int numQuery = faiss::gpu::randVal(1, 512);
856
+ int k = std::min(faiss::gpu::randVal(1, 50), int(index.ntotal));
857
+
858
+ auto queries = faiss::gpu::randVecs(numQuery, index.d);
859
+ auto queries16 = toFp16(queries);
860
+ auto roundedQueries = fromFp16(queries16);
861
+
862
+ std::vector<float> refDistances(numQuery * k);
863
+ std::vector<faiss::idx_t> refLabels(numQuery * k);
864
+ index.search(
865
+ numQuery,
866
+ roundedQueries.data(),
867
+ k,
868
+ refDistances.data(),
869
+ refLabels.data());
870
+
871
+ auto expectSearchExMatches = [&](const void* x, const std::string& where) {
872
+ std::vector<float> distances(numQuery * k, 0);
873
+ std::vector<faiss::idx_t> labels(numQuery * k, -1);
874
+ index.search_ex(
875
+ numQuery,
876
+ x,
877
+ faiss::NumericType::Float16,
878
+ k,
879
+ distances.data(),
880
+ labels.data());
881
+
882
+ EXPECT_EQ(labels, refLabels) << where;
883
+ EXPECT_EQ(distances, refDistances) << where;
884
+ };
885
+
886
+ expectSearchExMatches(queries16.data(), "host queries");
887
+
888
+ auto minPagingSize = index.getMinPagingSize();
889
+ index.setMinPagingSize(0);
890
+ expectSearchExMatches(queries16.data(), "paged host queries");
891
+ index.setMinPagingSize(minPagingSize);
892
+
893
+ DeviceCopy queries16Device(
894
+ index.getDevice(),
895
+ queries16.data(),
896
+ queries16.size() * sizeof(uint16_t));
897
+ expectSearchExMatches(queries16Device.data, "device queries");
898
+ }
899
+
900
+ // Covers both float32 storage (queries widened on the device) and float16
901
+ // storage (queries searched natively)
902
+ template <typename GpuIndexFlatT>
903
+ void testFlatSearchExFloat16() {
904
+ faiss::gpu::StandardGpuResources res;
905
+ res.noTempMemory();
906
+
907
+ int dim = faiss::gpu::randVal(50, 400);
908
+ int numVecs = faiss::gpu::randVal(1000, 5000);
909
+ auto vecs = faiss::gpu::randVecs(numVecs, dim);
910
+
911
+ for (bool useFloat16 : {false, true}) {
912
+ faiss::gpu::GpuIndexFlatConfig config;
913
+ config.device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
914
+ config.useFloat16 = useFloat16;
915
+
916
+ GpuIndexFlatT gpuIndex(&res, dim, config);
917
+ gpuIndex.add(numVecs, vecs.data());
918
+
919
+ SCOPED_TRACE(useFloat16 ? "useFloat16" : "float32 storage");
920
+ testSearchExFloat16(gpuIndex);
921
+ }
922
+ }
923
+
924
+ TEST(TestGpuIndexFlat, SearchExFloat16_L2) {
925
+ testFlatSearchExFloat16<faiss::gpu::GpuIndexFlatL2>();
926
+ }
927
+
928
+ TEST(TestGpuIndexFlat, SearchExFloat16_IP) {
929
+ testFlatSearchExFloat16<faiss::gpu::GpuIndexFlatIP>();
930
+ }
931
+
932
+ // Indexes without a native fp16 search widen the queries on the device
933
+ TEST(TestGpuIndexFlat, SearchExFloat16_IVFFlatDefault) {
934
+ faiss::gpu::StandardGpuResources res;
935
+ res.noTempMemory();
936
+
937
+ int dim = 64;
938
+ int numVecs = 5000;
939
+ auto vecs = faiss::gpu::randVecs(numVecs, dim);
940
+
941
+ faiss::gpu::GpuIndexIVFFlatConfig config;
942
+ config.device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
943
+
944
+ int numLists = 32;
945
+ faiss::gpu::GpuIndexIVFFlat gpuIndex(
946
+ &res, dim, numLists, faiss::METRIC_L2, config);
947
+ gpuIndex.nprobe = 4;
948
+ gpuIndex.train(numVecs, vecs.data());
949
+ gpuIndex.add(numVecs, vecs.data());
950
+
951
+ testSearchExFloat16(gpuIndex);
952
+ }
953
+
954
+ // k-means on fp16 data with a GPU assignment index must match k-means on the
955
+ // float32 rounding of that data with the same kind of index
956
+ TEST(TestGpuIndexFlat, ClusteringTrainExFloat16) {
957
+ faiss::gpu::StandardGpuResources res;
958
+ res.noTempMemory();
959
+
960
+ int dim = 32;
961
+ int numCentroids = 20;
962
+ // Fits in a single decode block, so that both runs search the same batches
963
+ int numVecs = 2000;
964
+ auto vecs = faiss::gpu::randVecs(numVecs, dim);
965
+ auto vecs16 = toFp16(vecs);
966
+ auto roundedVecs = fromFp16(vecs16);
967
+
968
+ faiss::ClusteringParameters cp;
969
+ cp.niter = 10;
970
+
971
+ for (bool useFloat16 : {false, true}) {
972
+ faiss::gpu::GpuIndexFlatConfig config;
973
+ config.device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
974
+ config.useFloat16 = useFloat16;
975
+
976
+ faiss::gpu::GpuIndexFlatL2 refIndex(&res, dim, config);
977
+ faiss::Clustering refClustering(dim, numCentroids, cp);
978
+ refClustering.train(numVecs, roundedVecs.data(), refIndex);
979
+
980
+ faiss::gpu::GpuIndexFlatL2 gpuIndex(&res, dim, config);
981
+ faiss::Clustering clustering(dim, numCentroids, cp);
982
+ clustering.train_ex(
983
+ numVecs, vecs16.data(), faiss::NumericType::Float16, gpuIndex);
984
+
985
+ ASSERT_EQ(clustering.centroids.size(), refClustering.centroids.size());
986
+ for (size_t i = 0; i < clustering.centroids.size(); ++i) {
987
+ EXPECT_NEAR(
988
+ clustering.centroids[i], refClustering.centroids[i], 1e-5f)
989
+ << "useFloat16 " << useFloat16 << " element " << i;
990
+ }
991
+ }
992
+ }
993
+
994
+ // add and reconstruct_n copy the whole vector set between host and device in
995
+ // one copy. On ROCm, a pageable copy of more than 64 KiB goes through 16 MiB
996
+ // pinned chunks: 17 vectors take one chunk, and 10,500 vectors take two full
997
+ // chunks and a partial one. Each size has its own resources, so the second
998
+ // size reuses the pinned chunks after the streams of the first are destroyed.
999
+ TEST(TestGpuIndexFlat, LargeHostCopyRoundTrip) {
1000
+ int dim = 1000;
1001
+ int device = faiss::gpu::randVal(0, faiss::gpu::getNumDevices() - 1);
1002
+
1003
+ for (int numVecs : {17, 10500}) {
1004
+ faiss::gpu::StandardGpuResources res;
1005
+ res.noTempMemory();
1006
+
1007
+ faiss::gpu::GpuIndexFlatConfig config;
1008
+ config.device = device;
1009
+
1010
+ faiss::gpu::GpuIndexFlatL2 gpuIndex(&res, dim, config);
1011
+ std::vector<float> vecs = faiss::gpu::randVecs(numVecs, dim);
1012
+ gpuIndex.add(numVecs, vecs.data());
1013
+
1014
+ std::vector<float> out(vecs.size());
1015
+ gpuIndex.reconstruct_n(0, numVecs, out.data());
1016
+
1017
+ // Report the first difference; printing millions of values is useless
1018
+ size_t firstDiff =
1019
+ std::mismatch(out.begin(), out.end(), vecs.begin()).first -
1020
+ out.begin();
1021
+ EXPECT_EQ(firstDiff, out.size()) << "numVecs " << numVecs;
1022
+ }
1023
+ }
1024
+
810
1025
  int main(int argc, char** argv) {
811
1026
  testing::InitGoogleTest(&argc, argv);
812
1027
 
@@ -31,8 +31,11 @@
31
31
  #include <faiss/impl/IDSelector.h>
32
32
  #include <faiss/utils/distances.h>
33
33
  #include <gtest/gtest.h>
34
+ #include <algorithm>
34
35
  #include <cmath>
36
+ #include <random>
35
37
  #include <sstream>
38
+ #include <unordered_set>
36
39
  #include <vector>
37
40
 
38
41
  // FIXME: figure out a better way to test fp16
@@ -472,6 +475,101 @@ TEST(TestGpuIndexIVFFlat, Float32_32_CopyFrom) {
472
475
  #endif
473
476
  }
474
477
 
478
+ #if defined USE_NVIDIA_CUVS
479
+ class TestCuvsGpuIndexIVFFlatCopyFrom
480
+ : public testing::TestWithParam<faiss::MetricType> {};
481
+
482
+ TEST_P(TestCuvsGpuIndexIVFFlatCopyFrom, SearchLargeK) {
483
+ constexpr int numVecs = 16384;
484
+ constexpr int dim = 128;
485
+ constexpr int numCentroids = 32;
486
+ const auto metric = GetParam();
487
+ std::mt19937 rng(42);
488
+ std::uniform_int_distribution<int> random(-30, 30);
489
+ std::vector<float> vecs(numVecs * dim);
490
+ for (auto& value : vecs) {
491
+ value = random(rng);
492
+ }
493
+
494
+ faiss::IndexFlat quantizer(dim, metric);
495
+ faiss::IndexIVFFlat cpuIndex(&quantizer, dim, numCentroids, metric);
496
+ cpuIndex.cp.niter = 5;
497
+ cpuIndex.train(numVecs, vecs.data());
498
+ cpuIndex.add(numVecs, vecs.data());
499
+ cpuIndex.nprobe = numCentroids;
500
+
501
+ faiss::gpu::StandardGpuResources res;
502
+ faiss::gpu::GpuIndexIVFFlatConfig config;
503
+ config.use_cuvs = true;
504
+ config.indicesOptions = faiss::gpu::INDICES_64_BIT;
505
+ faiss::gpu::GpuIndexIVFFlat gpuIndex(
506
+ &res, dim, numCentroids, metric, config);
507
+ gpuIndex.copyFrom(&cpuIndex);
508
+
509
+ faiss::IndexFlat exact(dim, metric);
510
+ exact.add(numVecs, vecs.data());
511
+ // k > 256 takes the cuVS path that sizes its workspace from the
512
+ // accumulated list sizes. Cloning must populate those sizes first.
513
+ for (int k : {256, 257, 500, 3500}) {
514
+ SCOPED_TRACE(testing::Message() << "k=" << k);
515
+ for (int numQueries : {1, 4}) {
516
+ SCOPED_TRACE(testing::Message() << "numQueries=" << numQueries);
517
+ std::vector<float> expected(numQueries * k);
518
+ std::vector<float> distances(numQueries * k);
519
+ std::vector<faiss::idx_t> expectedIds(numQueries * k);
520
+ std::vector<faiss::idx_t> ids(numQueries * k);
521
+ exact.search(
522
+ numQueries,
523
+ vecs.data(),
524
+ k,
525
+ expected.data(),
526
+ expectedIds.data());
527
+ for (int repeat = 0; repeat < 2; ++repeat) {
528
+ gpuIndex.search(
529
+ numQueries,
530
+ vecs.data(),
531
+ k,
532
+ distances.data(),
533
+ ids.data());
534
+ for (int q = 0; q < numQueries; ++q) {
535
+ std::unordered_set<faiss::idx_t> uniqueIds;
536
+ for (int i = q * k; i < (q + 1) * k; ++i) {
537
+ ASSERT_GE(ids[i], 0);
538
+ ASSERT_LT(ids[i], numVecs);
539
+ EXPECT_TRUE(uniqueIds.insert(ids[i]).second);
540
+ float score = 0;
541
+ for (int j = 0; j < dim; ++j) {
542
+ float a = vecs[q * dim + j];
543
+ float b = vecs[ids[i] * dim + j];
544
+ score += metric == faiss::METRIC_L2
545
+ ? (a - b) * (a - b)
546
+ : a * b;
547
+ }
548
+ EXPECT_FLOAT_EQ(score, distances[i]);
549
+ }
550
+ // cuVS need not return sorted results, and tied scores
551
+ // may select different IDs at the top-k boundary.
552
+ std::vector<float> actual(
553
+ distances.begin() + q * k,
554
+ distances.begin() + (q + 1) * k);
555
+ auto begin = expected.begin() + q * k;
556
+ std::sort(begin, begin + k);
557
+ std::sort(actual.begin(), actual.end());
558
+ for (int i = 0; i < k; ++i) {
559
+ EXPECT_FLOAT_EQ(begin[i], actual[i]);
560
+ }
561
+ }
562
+ }
563
+ }
564
+ }
565
+ }
566
+
567
+ INSTANTIATE_TEST_SUITE_P(
568
+ Cuvs,
569
+ TestCuvsGpuIndexIVFFlatCopyFrom,
570
+ testing::Values(faiss::METRIC_L2, faiss::METRIC_INNER_PRODUCT));
571
+ #endif
572
+
475
573
  TEST(TestGpuIndexIVFFlat, Float32_negative) {
476
574
  Options opt;
477
575
 
@@ -63,6 +63,17 @@ size_t getMaxSharedMemPerBlockCurrentDevice();
63
63
  /// a device (deviceId >= 0) or the host (-1).
64
64
  int getDeviceForAddress(const void* p);
65
65
 
66
+ /// cudaMemcpyAsync, except that on ROCm a copy of more than 64 KiB to or from
67
+ /// pageable host memory goes through pinned staging buffers. As with CUDA's
68
+ /// pageable copies, a host-to-device copy returns once the source can be
69
+ /// reused, and a device-to-host copy returns once the data is on the host.
70
+ void memcpyHostDeviceAsync(
71
+ void* dst,
72
+ const void* src,
73
+ size_t bytes,
74
+ cudaMemcpyKind kind,
75
+ cudaStream_t stream);
76
+
66
77
  /// Does the given device support full unified memory sharing host
67
78
  /// memory?
68
79
  bool getFullUnifiedMemSupport(int device);
@@ -145,7 +145,7 @@ void StackDeviceMemory::Stack::returnAlloc(
145
145
  }
146
146
 
147
147
  head_ = p;
148
- lastUsers_.push_back(Range(p, p + size, stream));
148
+ lastUsers_.emplace_back(p, p + size, stream);
149
149
  }
150
150
 
151
151
  std::string StackDeviceMemory::Stack::toString() const {
@@ -11,6 +11,7 @@
11
11
  #pragma once
12
12
 
13
13
  #include <faiss/Index.h>
14
+ #include <faiss/IndexBinary.h>
14
15
 
15
16
  namespace faiss {
16
17
  namespace gpu_metal {
@@ -31,5 +32,15 @@ faiss::Index* index_cpu_to_metal_gpu(
31
32
  /// Caller owns the returned index.
32
33
  faiss::Index* index_metal_gpu_to_cpu(const faiss::Index* index);
33
34
 
35
+ /// Clone IndexBinaryFlat to Metal. device must be 0; caller owns the result.
36
+ faiss::IndexBinary* index_binary_cpu_to_metal_gpu(
37
+ StandardMetalResources* res,
38
+ int device,
39
+ const faiss::IndexBinary* index);
40
+
41
+ /// Clone MetalIndexBinaryFlat to CPU. Caller owns the returned index.
42
+ faiss::IndexBinary* index_binary_metal_gpu_to_cpu(
43
+ const faiss::IndexBinary* index);
44
+
34
45
  } // namespace gpu_metal
35
46
  } // namespace faiss
@@ -83,5 +83,97 @@ bool runMetalIVFFlatFullSearch(
83
83
  bool centroidsAreFP16 = false,
84
84
  bool waitForCompletion = true);
85
85
 
86
+ /// Full IVF-PQ search on the GPU: builds per-(query, probe) PQ lookup tables,
87
+ /// scans the assigned inverted lists with 8-bit ADC, and merges the per-list
88
+ /// top-k into the final top-k. Coarse quantization is performed on the CPU and
89
+ /// the assignments are passed in via @p coarseAssign. @p coarseDist carries
90
+ /// the per-(query, probe) coarse scores, added for inner product (the IP
91
+ /// tables hold <q_m, pq_m> only); unused for L2.
92
+ bool runMetalIVFPQFullSearch(
93
+ id<MTLDevice> device,
94
+ id<MTLCommandQueue> queue,
95
+ id<MTLBuffer> queries,
96
+ id<MTLBuffer> coarseAssign,
97
+ id<MTLBuffer> coarseDist,
98
+ id<MTLBuffer> coarseCentroids,
99
+ id<MTLBuffer> pqCentroids,
100
+ id<MTLBuffer> lookupTable,
101
+ id<MTLBuffer> codes,
102
+ id<MTLBuffer> ids,
103
+ id<MTLBuffer> listOffset,
104
+ id<MTLBuffer> listLength,
105
+ int nq,
106
+ int d,
107
+ int M,
108
+ int k,
109
+ int nprobe,
110
+ int nlist,
111
+ int avgListLen,
112
+ bool lookupFp16,
113
+ bool isL2,
114
+ id<MTLBuffer> outDistances,
115
+ id<MTLBuffer> outIndices,
116
+ id<MTLBuffer> perListDistBuf,
117
+ id<MTLBuffer> perListIdxBuf,
118
+ bool waitForCompletion = true);
119
+
120
+ /// Build the query-independent precomputed term of the IVFPQ L2 distance
121
+ /// decomposition on the GPU: term2[l][m][c] = ||pq[m][c]||^2 +
122
+ /// 2 <coarse_l[m], pq[m][c]>, laid out as nlist * M * 256 floats. Run once
123
+ /// per trained index; consumed by runMetalIVFPQPrecompSearch.
124
+ bool runMetalIVFPQPrecomputeTerm2(
125
+ id<MTLDevice> device,
126
+ id<MTLCommandQueue> queue,
127
+ id<MTLBuffer> coarseCentroids,
128
+ id<MTLBuffer> pqCentroids,
129
+ id<MTLBuffer> outTerm2,
130
+ int nlist,
131
+ int d,
132
+ int M);
133
+
134
+ /// IVF-PQ search using the precomputed-table decomposition: a per-query
135
+ /// M*256 term is built once per batch (instead of a full residual LUT per
136
+ /// (query, probe)), combined with @p term2 in threadgroup memory by the scan
137
+ /// kernel, and the coarse distance is added as the constant ||x - c||^2 term.
138
+ /// The scan keeps an exact running top-k over arbitrarily long lists and the
139
+ /// merge runs in rounds, so there is no list-length or nprobe*k cap; the
140
+ /// remaining requirements are k <= 512, M <= 64 and d/M <= 256.
141
+ /// onTheFly evaluates encountered codes directly (d <= 256), using the
142
+ /// supplied coarse centroids; shortLists requires every list to have <= 128
143
+ /// entries and selects a smaller threadgroup and scratch allocation.
144
+ /// @p term2 may be nil for inner product (qterm is the whole table);
145
+ /// @p useDis0 adds the coarse distance (required for L2, by_residual for IP).
146
+ bool runMetalIVFPQPrecompSearch(
147
+ id<MTLDevice> device,
148
+ id<MTLCommandQueue> queue,
149
+ id<MTLBuffer> queries,
150
+ id<MTLBuffer> coarseAssign,
151
+ id<MTLBuffer> coarseDist,
152
+ id<MTLBuffer> term2,
153
+ id<MTLBuffer> qtermScratch,
154
+ id<MTLBuffer> pqCentroids,
155
+ id<MTLBuffer> codes,
156
+ id<MTLBuffer> ids,
157
+ id<MTLBuffer> listOffset,
158
+ id<MTLBuffer> listLength,
159
+ int nq,
160
+ int d,
161
+ int M,
162
+ int k,
163
+ int nprobe,
164
+ bool isL2,
165
+ bool useDis0,
166
+ id<MTLBuffer> outDistances,
167
+ id<MTLBuffer> outIndices,
168
+ id<MTLBuffer> perListDistBuf,
169
+ id<MTLBuffer> perListIdxBuf,
170
+ id<MTLBuffer> mergeScratchDistBuf,
171
+ id<MTLBuffer> mergeScratchIdxBuf,
172
+ bool waitForCompletion = true,
173
+ id<MTLBuffer> coarseCentroids = nil,
174
+ bool onTheFly = false,
175
+ bool shortLists = false,
176
+ bool compactMerge = true);
177
+
86
178
  } // namespace gpu_metal
87
179
  } // namespace faiss
@@ -24,6 +24,10 @@ struct MetalIndexConfig {
24
24
 
25
25
  bool useFloat16CoarseQuantizer = false;
26
26
 
27
+ /// Build the IVF-PQ distance lookup tables in half precision. Halves LUT
28
+ /// bandwidth during the GPU scan at a small accuracy cost.
29
+ bool useFloat16 = false;
30
+
27
31
  faiss::gpu::IndicesOptions indicesOptions = faiss::gpu::INDICES_64_BIT;
28
32
 
29
33
  bool interleavedLayout = true;
@@ -0,0 +1,69 @@
1
+ // @lint-ignore-every LICENSELINT
2
+ /**
3
+ * Copyright (c) Meta Platforms, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #pragma once
10
+
11
+ #include <faiss/IndexBinaryFlat.h>
12
+ #include <faiss/gpu_metal/MetalResources.h>
13
+ #include <memory>
14
+
15
+ namespace faiss {
16
+ namespace gpu_metal {
17
+
18
+ /// Exhaustive Hamming search over GPU-resident binary vectors.
19
+ /// Every positive dimension divisible by 8 is supported. GPU search is exact
20
+ /// for k <= 512, with ties ordered by increasing label. Larger k, selectors,
21
+ /// and counts exceeding uint32_t use the CPU copy. GPU failures throw rather
22
+ /// than silently falling back. Range search and remove_ids are not supported.
23
+ /// As with other Metal indexes, callers must serialize mutation and search.
24
+ class MetalIndexBinaryFlat : public faiss::IndexBinary {
25
+ public:
26
+ static constexpr int kMaxGpuK = 512;
27
+
28
+ MetalIndexBinaryFlat(std::shared_ptr<MetalResources> resources, int d);
29
+ MetalIndexBinaryFlat(
30
+ std::shared_ptr<MetalResources> resources,
31
+ const faiss::IndexBinaryFlat* index);
32
+ ~MetalIndexBinaryFlat() override;
33
+
34
+ MetalIndexBinaryFlat(const MetalIndexBinaryFlat&) = delete;
35
+ MetalIndexBinaryFlat& operator=(const MetalIndexBinaryFlat&) = delete;
36
+
37
+ void add(idx_t n, const uint8_t* x) override;
38
+ void reset() override;
39
+ void search(
40
+ idx_t n,
41
+ const uint8_t* x,
42
+ idx_t k,
43
+ int32_t* distances,
44
+ idx_t* labels,
45
+ const SearchParameters* params = nullptr) const override;
46
+ void reconstruct(idx_t key, uint8_t* recons) const override;
47
+
48
+ void copyFrom(const faiss::IndexBinaryFlat* index);
49
+ void copyTo(faiss::IndexBinaryFlat* index) const;
50
+
51
+ std::shared_ptr<MetalResources> getResources() const {
52
+ return resources_;
53
+ }
54
+
55
+ private:
56
+ std::shared_ptr<MetalResources> resources_;
57
+ faiss::IndexBinaryFlat cpuIndex_;
58
+ id<MTLBuffer> vectorsBuf_ = nil;
59
+ // Search is synchronous and callers serialize access, so scratch buffers
60
+ // can be reused after waitUntilCompleted. ARC releases them on destruction.
61
+ mutable id<MTLBuffer> queryBuf_ = nil;
62
+ mutable id<MTLBuffer> distanceBuf_ = nil;
63
+ mutable id<MTLBuffer> labelBuf_ = nil;
64
+
65
+ id<MTLBuffer> upload(const uint8_t* data, size_t bytes) const;
66
+ };
67
+
68
+ } // namespace gpu_metal
69
+ } // namespace faiss
@@ -75,13 +75,51 @@ class MetalIndexIVFPQ : public MetalIndex {
75
75
  std::unique_ptr<faiss::IndexIVFPQ> cpuIndex_;
76
76
  std::unique_ptr<MetalIVFPQImpl> gpuIvf_;
77
77
 
78
+ // Reusable GPU scratch buffers for the search path, grown on demand.
79
+ mutable id<MTLBuffer> searchQueriesBuf_ = nil;
80
+ mutable id<MTLBuffer> searchCoarseBuf_ = nil;
81
+ mutable id<MTLBuffer> searchOutDistBuf_ = nil;
82
+ mutable id<MTLBuffer> searchOutIdxBuf_ = nil;
83
+ mutable id<MTLBuffer> searchPerListDistBuf_ = nil;
84
+ mutable id<MTLBuffer> searchPerListIdxBuf_ = nil;
85
+ mutable id<MTLBuffer> lookupTableBuf_ = nil;
86
+ mutable id<MTLBuffer> lookupTableHalfBuf_ = nil;
87
+ mutable id<MTLBuffer> searchCoarseDistBuf_ = nil;
88
+ mutable id<MTLBuffer> searchQTermBuf_ = nil;
89
+ mutable id<MTLBuffer> searchMergeDistBuf_ = nil;
90
+ mutable id<MTLBuffer> searchMergeIdxBuf_ = nil;
91
+ mutable size_t searchQueriesCap_ = 0;
92
+ mutable size_t searchCoarseCap_ = 0;
93
+ mutable size_t searchOutDistCap_ = 0;
94
+ mutable size_t searchOutIdxCap_ = 0;
95
+ mutable size_t searchPerListDistCap_ = 0;
96
+ mutable size_t searchPerListIdxCap_ = 0;
97
+ mutable size_t lookupTableCap_ = 0;
98
+ mutable size_t lookupTableHalfCap_ = 0;
99
+ mutable size_t searchCoarseDistCap_ = 0;
100
+ mutable size_t searchQTermCap_ = 0;
101
+ mutable size_t searchMergeDistCap_ = 0;
102
+ mutable size_t searchMergeIdxCap_ = 0;
103
+
104
+ // Coarse centroids mirrored on the GPU for the L2 lookup-table build.
105
+ mutable id<MTLBuffer> centroidBuf_ = nil;
106
+
107
+ // Query-independent term of the L2 precomputed-table decomposition
108
+ // (nlist * M * 256 floats), built once per trained index on the GPU.
109
+ mutable id<MTLBuffer> term2Buf_ = nil;
110
+
78
111
  void verifyPQSettings_() const;
112
+ void precomputeTerm2_() const;
79
113
 
80
114
  void encodeResidualAndAppend_(
81
115
  idx_t n,
82
116
  const float* x,
83
117
  const idx_t* list_nos,
84
118
  const idx_t* xids);
119
+
120
+ void ensureSearchBuf_(id<MTLBuffer>& buf, size_t& cap, size_t needed) const;
121
+ void uploadCentroids_() const;
122
+ void uploadPQCentroids_() const;
85
123
  };
86
124
 
87
125
  } // namespace gpu_metal