faiss 0.6.4 → 0.6.6

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 (301) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/ext/faiss/extconf.rb +2 -1
  4. data/ext/faiss/index.cpp +42 -0
  5. data/lib/faiss/version.rb +1 -1
  6. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  7. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  8. data/vendor/faiss/faiss/Clustering.h +29 -0
  9. data/vendor/faiss/faiss/IVFlib.cpp +147 -51
  10. data/vendor/faiss/faiss/IVFlib.h +21 -2
  11. data/vendor/faiss/faiss/Index.cpp +44 -0
  12. data/vendor/faiss/faiss/Index.h +18 -13
  13. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  14. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  15. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +85 -4
  17. data/vendor/faiss/faiss/IndexBinaryHNSW.h +19 -0
  18. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -11
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +34 -18
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +57 -1
  22. data/vendor/faiss/faiss/IndexFlat.h +31 -0
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +461 -13
  25. data/vendor/faiss/faiss/IndexHNSW.h +40 -5
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +45 -0
  27. data/vendor/faiss/faiss/IndexIDMap.h +15 -3
  28. data/vendor/faiss/faiss/IndexIVF.cpp +349 -68
  29. data/vendor/faiss/faiss/IndexIVF.h +90 -2
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +51 -29
  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 +22 -6
  34. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +2 -1
  35. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  36. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +78 -58
  37. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +7 -13
  38. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
  39. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +860 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
  46. data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
  47. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +7 -4
  48. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +12 -6
  49. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  50. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  51. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  52. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  53. data/vendor/faiss/faiss/IndexShards.cpp +10 -0
  54. data/vendor/faiss/faiss/IndexShards.h +5 -0
  55. data/vendor/faiss/faiss/SuperKMeans.cpp +59 -17
  56. data/vendor/faiss/faiss/SuperKMeans.h +9 -4
  57. data/vendor/faiss/faiss/VectorTransform.cpp +159 -11
  58. data/vendor/faiss/faiss/VectorTransform.h +24 -1
  59. data/vendor/faiss/faiss/clone_index.cpp +24 -0
  60. data/vendor/faiss/faiss/factory_tools.cpp +19 -1
  61. data/vendor/faiss/faiss/gpu/Dataset.h +164 -0
  62. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  63. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  64. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +3 -0
  65. data/vendor/faiss/faiss/gpu/GpuIndex.h +4 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +32 -13
  67. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +51 -22
  68. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  69. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  70. data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
  71. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
  72. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  73. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  74. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  75. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
  76. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  77. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  78. data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
  79. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +12 -1
  80. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +93 -1
  81. data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
  82. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +5 -1
  83. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  84. data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
  85. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
  86. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +39 -1
  87. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +107 -1
  88. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +11 -1
  89. data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
  90. data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
  91. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
  92. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
  93. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +3 -1
  94. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  95. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  96. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  97. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +267 -136
  98. data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
  99. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  100. data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
  101. data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
  102. data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
  103. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  104. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  105. data/vendor/faiss/faiss/impl/HNSW.cpp +48 -12
  106. data/vendor/faiss/faiss/impl/HNSW.h +74 -2
  107. data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
  108. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  109. data/vendor/faiss/faiss/impl/NNDescent.cpp +2 -1
  110. data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
  111. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  112. data/vendor/faiss/faiss/impl/Panorama.h +3 -2
  113. data/vendor/faiss/faiss/impl/PiPNN.cpp +824 -0
  114. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  115. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
  116. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +54 -11
  117. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
  118. data/vendor/faiss/faiss/impl/RaBitQUtils.h +30 -1
  119. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +91 -66
  120. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +15 -0
  121. data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
  122. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +29 -0
  123. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +1 -6
  124. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  125. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  127. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
  128. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  129. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  130. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -0
  133. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  134. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  135. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  136. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +17 -129
  137. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  138. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +73 -6
  139. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
  140. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -4
  141. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -4
  142. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +10 -85
  143. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  144. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  145. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +106 -1
  146. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -6
  147. data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
  148. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +105 -78
  149. data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
  150. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  151. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  152. data/vendor/faiss/faiss/impl/index_read.cpp +318 -9
  153. data/vendor/faiss/faiss/impl/index_write.cpp +124 -5
  154. data/vendor/faiss/faiss/impl/io.cpp +1 -0
  155. data/vendor/faiss/faiss/impl/io_macros.h +2 -2
  156. data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
  157. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
  158. data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
  159. data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
  160. data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
  161. data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
  162. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
  163. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
  164. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
  165. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
  166. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
  167. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
  168. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  169. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  170. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  171. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  172. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  173. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  174. data/vendor/faiss/faiss/impl/platform_macros.h +91 -27
  175. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  176. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +84 -4
  177. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
  178. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  179. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  180. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  181. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  182. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  183. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  184. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +19 -0
  185. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  186. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  187. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  188. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  189. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  190. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  191. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  192. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
  193. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -6
  194. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -5
  195. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  196. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  197. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
  198. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +5 -1
  199. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  200. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +85 -37
  201. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +262 -110
  202. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +503 -122
  203. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +10 -155
  204. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +7 -701
  205. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +66 -57
  206. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +133 -127
  207. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  208. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  209. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  210. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  211. data/vendor/faiss/faiss/impl/simd_dispatch.h +7 -0
  212. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  213. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
  214. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  215. data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
  216. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  217. data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
  218. data/vendor/faiss/faiss/index_factory.cpp +106 -6
  219. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  220. data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
  221. data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
  222. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +15 -10
  223. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
  224. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
  225. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
  226. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
  227. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
  228. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
  229. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
  230. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
  231. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
  232. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +3 -2
  233. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  234. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  235. data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
  236. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  237. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  238. data/vendor/faiss/faiss/utils/distances.cpp +466 -43
  239. data/vendor/faiss/faiss/utils/distances.h +179 -2
  240. data/vendor/faiss/faiss/utils/distances_dispatch.h +6 -4
  241. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  242. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +0 -14
  243. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  244. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  247. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  248. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  249. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  250. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  251. data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
  252. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  253. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +0 -4
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +33 -5
  256. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +15 -30
  257. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  258. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +2 -1
  259. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  260. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  261. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  262. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  263. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  264. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  265. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  266. data/vendor/faiss/faiss/utils/rabitq_simd.h +171 -22
  267. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  268. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +81 -9
  269. data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
  270. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +108 -9
  271. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +111 -12
  272. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +353 -71
  273. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  274. data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
  275. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +106 -0
  276. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  277. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  278. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  279. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  280. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  281. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  282. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  283. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  284. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  285. data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
  286. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +185 -51
  287. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +194 -66
  288. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512_vpopcnt.cpp +51 -14
  289. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  290. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  291. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  292. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  293. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +0 -4
  294. data/vendor/faiss/faiss/utils/simd_levels.cpp +22 -6
  295. data/vendor/faiss/faiss/utils/simd_levels.h +74 -1
  296. data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
  297. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  298. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  299. data/vendor/faiss/faiss/utils/utils.cpp +84 -0
  300. data/vendor/faiss/faiss/utils/utils.h +24 -0
  301. metadata +52 -1
@@ -20,10 +20,12 @@
20
20
  #include <faiss/IndexIVFEDEN.h>
21
21
  #include <faiss/IndexIVFFlat.h>
22
22
  #include <faiss/IndexIVFPQFastScan.h>
23
+ #include <faiss/IndexIVFPQR.h>
23
24
  #include <faiss/IndexIVFRaBitQ.h>
24
25
  #include <faiss/IndexLSH.h>
25
26
  #include <faiss/IndexNSG.h>
26
27
  #include <faiss/IndexPQFastScan.h>
28
+ #include <faiss/IndexPiPNN.h>
27
29
  #include <faiss/IndexPreTransform.h>
28
30
  #include <faiss/IndexRaBitQ.h>
29
31
  #include <faiss/IndexRefine.h>
@@ -124,6 +126,11 @@ std::string reverse_index_factory(const faiss::Index* index) {
124
126
  dynamic_cast<const faiss::IndexIVFScalarQuantizer*>(
125
127
  ivf_index)) {
126
128
  return prefix + "," + sq_types.at(sq_index->sq.qtype);
129
+ } else if (
130
+ const faiss::IndexIVFPQR* ivfpqr_index =
131
+ dynamic_cast<const faiss::IndexIVFPQR*>(ivf_index)) {
132
+ return prefix + ",PQ" + std::to_string(ivfpqr_index->pq.M) + "+" +
133
+ std::to_string(ivfpqr_index->refine_pq.M);
127
134
  } else if (
128
135
  const faiss::IndexIVFPQ* ivfpq_index =
129
136
  dynamic_cast<const faiss::IndexIVFPQ*>(ivf_index)) {
@@ -178,12 +185,23 @@ std::string reverse_index_factory(const faiss::Index* index) {
178
185
  } else if (
179
186
  const faiss::IndexHNSW* hnsw_index =
180
187
  dynamic_cast<const faiss::IndexHNSW*>(index)) {
181
- return "HNSW" + std::to_string(get_hnsw_M(hnsw_index));
188
+ std::string hnsw_prefix =
189
+ "HNSW" + std::to_string(get_hnsw_M(hnsw_index));
190
+ const faiss::Index* storage = hnsw_index->storage;
191
+ if (!storage || dynamic_cast<const faiss::IndexFlat*>(storage)) {
192
+ return hnsw_prefix;
193
+ }
194
+ return hnsw_prefix + "," + reverse_index_factory(storage);
182
195
  } else if (
183
196
  const faiss::IndexNSG* nsg_index =
184
197
  dynamic_cast<const faiss::IndexNSG*>(index)) {
185
198
  return "NSG" + std::to_string(nsg_index->nsg.R) + "," +
186
199
  reverse_index_factory(nsg_index->storage);
200
+ } else if (
201
+ const faiss::IndexPiPNN* pipnn_index =
202
+ dynamic_cast<const faiss::IndexPiPNN*>(index)) {
203
+ return "PiPNN" + std::to_string(pipnn_index->pipnn.R) + "," +
204
+ reverse_index_factory(pipnn_index->storage);
187
205
  } else if (
188
206
  const faiss::IndexRefine* refine_index =
189
207
  dynamic_cast<const faiss::IndexRefine*>(index)) {
@@ -0,0 +1,164 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include <faiss/Index.h>
11
+
12
+ #include <cstdint>
13
+ #include <memory>
14
+
15
+ namespace faiss {
16
+ namespace gpu {
17
+ class GpuResourcesProvider;
18
+
19
+ enum class BBQCodeLayout {
20
+ Packed1Bit,
21
+ Transposed2Bit,
22
+ Transposed4Bit,
23
+ Packed4Bit,
24
+ Packed7Bit,
25
+ Packed8Bit
26
+ };
27
+
28
+ /** Precomputed BBQ codes and corrections in caller-owned device buffers.
29
+ *
30
+ * Codes are packed rows in codeLayout with a 4-byte aligned base pointer. Each
31
+ * correction array has one entry per row; centroid has dim entries of the
32
+ * Dataset numeric type. Corrections must have been generated for metric. All
33
+ * buffers must outlive uses of the Dataset.
34
+ */
35
+ struct BBQQuantizer {
36
+ const uint8_t* codes = nullptr;
37
+ const float* lowerIntervals = nullptr;
38
+ const float* upperIntervals = nullptr;
39
+ const float* additionalCorrections = nullptr;
40
+ const int32_t* quantizedComponentSums = nullptr;
41
+ const void* centroid = nullptr;
42
+ const float* dequantDelta = nullptr;
43
+ const float* dequantSumDelta = nullptr;
44
+ const float* rowNorm = nullptr;
45
+ BBQCodeLayout codeLayout = BBQCodeLayout::Packed1Bit;
46
+ MetricType metric = METRIC_L2;
47
+ float centroidNormSquared = 0;
48
+ };
49
+
50
+ /** Caller-controlled vector storage for cuVS CAGRA.
51
+ *
52
+ * Dense dimensions and row strides are in elements, with contiguous columns.
53
+ * Binary vectors use UInt8, and their dimension is the number of bytes, not
54
+ * bits. Views never own or copy their allocation. An owning Dataset controls
55
+ * the allocation's lifetime; CAGRA always borrows it. Keep the backing
56
+ * allocation alive and unchanged until all indexes using it and queued GPU work
57
+ * finish. Moving an owning Dataset preserves its allocation and existing
58
+ * borrowed views.
59
+ */
60
+ class Dataset {
61
+ public:
62
+ enum class Layout { Standard, Padded, BBQ };
63
+
64
+ ~Dataset();
65
+ Dataset(Dataset&&) noexcept;
66
+ Dataset& operator=(Dataset&&) noexcept;
67
+ Dataset(const Dataset&) = delete;
68
+ Dataset& operator=(const Dataset&) = delete;
69
+
70
+ /// Borrow host storage. rowStride must be at least dim.
71
+ static Dataset viewHost(
72
+ const void* data,
73
+ idx_t numRows,
74
+ int dim,
75
+ NumericType numericType,
76
+ idx_t rowStride);
77
+
78
+ /// Borrow storage accessible on device. A Standard view can also be
79
+ /// CAGRA-compatible when its physical stride and alignment satisfy padding.
80
+ /// When used by CAGRA, elements between dim and rowStride must be zero.
81
+ static Dataset viewDevice(
82
+ const void* data,
83
+ idx_t numRows,
84
+ int dim,
85
+ NumericType numericType,
86
+ idx_t rowStride,
87
+ int device);
88
+
89
+ /// Borrow CAGRA-compatible device storage without copying. The base address
90
+ /// must be 16-byte aligned and rowStride must equal dim rounded up to a
91
+ /// 16-byte row width. The caller must zero elements between dim and
92
+ /// rowStride, including for compatible Standard views used by CAGRA.
93
+ static Dataset viewDevicePadded(
94
+ const void* data,
95
+ idx_t numRows,
96
+ int dim,
97
+ NumericType numericType,
98
+ idx_t rowStride,
99
+ int device);
100
+
101
+ /// Borrow one symmetric or two asymmetric BBQ quantizers without copying
102
+ /// their buffers. Both quantizers describe the same vectors, numeric type,
103
+ /// and metric. BBQ supports NN-descent graph construction; attach dense
104
+ /// padded storage with CAGRA updateDataset before searching.
105
+ static Dataset viewDeviceBBQ(
106
+ const BBQQuantizer& source,
107
+ idx_t numRows,
108
+ int dim,
109
+ NumericType numericType,
110
+ int device,
111
+ const BBQQuantizer* secondary = nullptr);
112
+
113
+ /// Create padded device storage using cuVS make_device_padded_dataset.
114
+ /// Device/managed input whose stride already matches the required padding
115
+ /// is rejected; use a view for that storage. Copies logical columns and
116
+ /// zeroes padding asynchronously on the resource's stream. Source data must
117
+ /// be ready on that stream and stay alive and unchanged until copying
118
+ /// finishes. Order subsequent use on that stream. Retains the resources
119
+ /// needed to release the allocation.
120
+ static Dataset makeDevicePadded(
121
+ GpuResourcesProvider* resources,
122
+ const Dataset& source,
123
+ int device);
124
+
125
+ /// Dense storage only; BBQ has separate code and correction buffers.
126
+ const void* getData() const;
127
+ idx_t getNumRows() const;
128
+ int getDim() const;
129
+ /// Dense storage only, in elements.
130
+ idx_t getRowStride() const;
131
+ NumericType getNumericType() const;
132
+ int getDevice() const; // -1 for host storage
133
+ Layout getLayout() const;
134
+ bool isDevice() const;
135
+ bool ownsData() const;
136
+
137
+ /// Reports dense device residency, alignment, and exact required row
138
+ /// stride. Returns false for BBQ. Does not inspect padding values; zero
139
+ /// padding is a caller contract.
140
+ bool isCagraCompatible() const;
141
+
142
+ /// Create a non-owning view of the same storage by copying its metadata.
143
+ /// Does not retain ownership of the backing allocation.
144
+ Dataset view() const;
145
+
146
+ /// Copy dense logical columns into packed host storage. destination must
147
+ /// have numRows * dim * sizeof(element) bytes. Completes all device work
148
+ /// using the source device before copying and returns with the host data
149
+ /// ready.
150
+ void copyToHost(void* destination) const;
151
+
152
+ private:
153
+ template <typename>
154
+ friend class CuvsCagra;
155
+
156
+ struct Impl;
157
+ explicit Dataset(std::unique_ptr<Impl> impl);
158
+ const Impl& getImpl() const;
159
+ const BBQQuantizer* getBBQQuantizer(bool secondary) const;
160
+ std::unique_ptr<Impl> impl_;
161
+ };
162
+
163
+ } // namespace gpu
164
+ } // namespace faiss
@@ -14,7 +14,7 @@
14
14
 
15
15
  #include <faiss/IndexBinaryFlat.h>
16
16
  #include <faiss/IndexFlat.h>
17
- #if defined(USE_NVIDIA_CUVS) && !defined(FAISS_CUVS_NO_CAGRA)
17
+ #if defined USE_NVIDIA_CUVS
18
18
  #include <faiss/IndexBinaryHNSW.h>
19
19
  #include <faiss/IndexHNSW.h>
20
20
  #endif
@@ -28,7 +28,7 @@
28
28
  #include <faiss/MetaIndexes.h>
29
29
  #include <faiss/gpu/GpuIndex.h>
30
30
  #include <faiss/gpu/GpuIndexBinaryFlat.h>
31
- #if defined(USE_NVIDIA_CUVS) && !defined(FAISS_CUVS_NO_CAGRA)
31
+ #if defined USE_NVIDIA_CUVS
32
32
  #include <faiss/gpu/GpuIndexBinaryCagra.h>
33
33
  #include <faiss/gpu/GpuIndexCagra.h>
34
34
  #endif
@@ -92,7 +92,7 @@ Index* ToCPUCloner::clone_Index(const Index* index) {
92
92
  // (inverse op of ToGpuClonerMultiple)
93
93
 
94
94
  }
95
- #if defined(USE_NVIDIA_CUVS) && !defined(FAISS_CUVS_NO_CAGRA)
95
+ #if defined USE_NVIDIA_CUVS
96
96
  else if (auto icg = dynamic_cast<const GpuIndexCagra*>(index)) {
97
97
  IndexHNSWCagra* res = new IndexHNSWCagra();
98
98
  if (icg->get_numeric_type() != faiss::NumericType::Float32) {
@@ -171,6 +171,7 @@ Index* ToGpuCloner::clone_Index(const Index* index) {
171
171
  config.flatConfig.useFloat16 = useFloat16CoarseQuantizer;
172
172
  config.use_cuvs = use_cuvs;
173
173
  config.allowCpuCoarseQuantizer = allowCpuCoarseQuantizer;
174
+ config.deterministic_tie_break = deterministic_tie_break;
174
175
 
175
176
  GpuIndexIVFFlat* res = new GpuIndexIVFFlat(
176
177
  provider, ifl->d, ifl->nlist, ifl->metric_type, config);
@@ -189,6 +190,7 @@ Index* ToGpuCloner::clone_Index(const Index* index) {
189
190
  config.flatConfig.useFloat16 = useFloat16CoarseQuantizer;
190
191
  config.use_cuvs = use_cuvs;
191
192
  config.allowCpuCoarseQuantizer = allowCpuCoarseQuantizer;
193
+ config.deterministic_tie_break = deterministic_tie_break;
192
194
 
193
195
  GpuIndexIVFScalarQuantizer* res = new GpuIndexIVFScalarQuantizer(
194
196
  provider,
@@ -224,6 +226,7 @@ Index* ToGpuCloner::clone_Index(const Index* index) {
224
226
  config.use_cuvs = use_cuvs;
225
227
  config.interleavedLayout = use_cuvs;
226
228
  config.allowCpuCoarseQuantizer = allowCpuCoarseQuantizer;
229
+ config.deterministic_tie_break = deterministic_tie_break;
227
230
 
228
231
  GpuIndexIVFPQ* res = new GpuIndexIVFPQ(provider, ipq, config);
229
232
 
@@ -233,14 +236,14 @@ Index* ToGpuCloner::clone_Index(const Index* index) {
233
236
 
234
237
  return res;
235
238
  }
236
- #if defined(USE_NVIDIA_CUVS) && !defined(FAISS_CUVS_NO_CAGRA)
239
+ #if defined USE_NVIDIA_CUVS
237
240
  else if (auto icg = dynamic_cast<const faiss::IndexHNSWCagra*>(index)) {
238
241
  GpuIndexCagraConfig config;
239
242
  config.device = device;
240
- GpuIndexCagra* res =
241
- new GpuIndexCagra(provider, icg->d, icg->metric_type, config);
242
- res->copyFrom_ex(icg, icg->get_numeric_type());
243
- return res;
243
+ auto res = std::make_unique<GpuIndexCagra>(
244
+ provider, icg->d, icg->metric_type, config);
245
+ res->copyFrom(icg);
246
+ return res.release();
244
247
  }
245
248
  #endif
246
249
  else {
@@ -533,7 +536,7 @@ faiss::IndexBinary* index_binary_gpu_to_cpu(
533
536
  ii->copyTo(ret);
534
537
  return ret;
535
538
  }
536
- #if defined(USE_NVIDIA_CUVS) && !defined(FAISS_CUVS_NO_CAGRA)
539
+ #if defined USE_NVIDIA_CUVS
537
540
  else if (auto ii = dynamic_cast<const GpuIndexBinaryCagra*>(gpu_index)) {
538
541
  IndexBinaryHNSWCagra* ret = new IndexBinaryHNSWCagra();
539
542
  ii->copyTo(ret);
@@ -555,15 +558,15 @@ faiss::IndexBinary* index_binary_cpu_to_gpu(
555
558
  config.device = device;
556
559
  return new GpuIndexBinaryFlat(provider, ii, config);
557
560
  }
558
- #if defined(USE_NVIDIA_CUVS) && !defined(FAISS_CUVS_NO_CAGRA)
561
+ #if defined USE_NVIDIA_CUVS
559
562
  else if (
560
563
  auto ii = dynamic_cast<const faiss::IndexBinaryHNSWCagra*>(index)) {
561
564
  GpuIndexCagraConfig config;
562
565
  config.device = device;
563
- GpuIndexBinaryCagra* res =
564
- new GpuIndexBinaryCagra(provider, ii->d, config);
566
+ auto res =
567
+ std::make_unique<GpuIndexBinaryCagra>(provider, ii->d, config);
565
568
  res->copyFrom(ii);
566
- return res;
569
+ return res.release();
567
570
  }
568
571
  #endif
569
572
  else {
@@ -70,6 +70,8 @@ struct ToGpuClonerMultiple : faiss::Cloner, GpuMultipleClonerOptions {
70
70
  faiss::Index* index_gpu_to_cpu(const faiss::Index* gpu_index);
71
71
 
72
72
  /// converts any CPU index that can be converted to GPU
73
+ /// CAGRA clones contain only the graph. Call updateDataset with caller-owned,
74
+ /// compatible device storage before searching.
73
75
  faiss::Index* index_cpu_to_gpu(
74
76
  GpuResourcesProvider* provider,
75
77
  int device,
@@ -105,6 +107,8 @@ faiss::IndexBinary* index_binary_gpu_to_cpu(
105
107
  const faiss::IndexBinary* gpu_index);
106
108
 
107
109
  /// converts any CPU index that can be converted to GPU
110
+ /// CAGRA clones contain only the graph. Call updateDataset with caller-owned,
111
+ /// compatible device storage before searching.
108
112
  faiss::IndexBinary* index_binary_cpu_to_gpu(
109
113
  GpuResourcesProvider* provider,
110
114
  int device,
@@ -42,6 +42,9 @@ struct GpuClonerOptions {
42
42
  /// Use the cuVS implementation. Opt-in: see GpuIndexConfig::use_cuvs.
43
43
  bool use_cuvs = false;
44
44
 
45
+ /// IVF indexes only: see GpuIndexIVFConfig::deterministic_tie_break.
46
+ bool deterministic_tie_break = false;
47
+
45
48
  /// This flag controls the CPU fallback logic for coarse quantizer
46
49
  /// component of the index. When set to false (default), the cloner will
47
50
  /// throw an exception for indices not implemented on GPU. When set to
@@ -213,6 +213,9 @@ class GpuIndex : public faiss::Index {
213
213
  idx_t* labels,
214
214
  const SearchParameters* params) const = 0;
215
215
 
216
+ /// Search with queries of the given numeric type; all data is guaranteed
217
+ /// to be resident on our device. The default handles Float32 and widens
218
+ /// Float16 queries to float32 on the device before calling searchImpl_
216
219
  virtual void searchImpl_ex_(
217
220
  idx_t n,
218
221
  const void* x,
@@ -220,19 +223,7 @@ class GpuIndex : public faiss::Index {
220
223
  int k,
221
224
  float* distances,
222
225
  idx_t* labels,
223
- const SearchParameters* params) const {
224
- if (numeric_type == NumericType::Float32) {
225
- searchImpl_(
226
- n,
227
- static_cast<const float*>(x),
228
- k,
229
- distances,
230
- labels,
231
- params);
232
- } else {
233
- FAISS_THROW_MSG("GpuIndex::searchImpl_: unsupported numeric type");
234
- }
235
- }
226
+ const SearchParameters* params) const;
236
227
 
237
228
  private:
238
229
  /// Calls addImpl_ for a single page of GPU-resident data
@@ -33,6 +33,7 @@ namespace faiss {
33
33
  namespace gpu {
34
34
 
35
35
  class BinaryCuvsCagra;
36
+ class Dataset;
36
37
 
37
38
  struct GpuIndexBinaryCagra : public IndexBinary {
38
39
  public:
@@ -49,22 +50,40 @@ struct GpuIndexBinaryCagra : public IndexBinary {
49
50
  /// stream and handle resources on the GPU
50
51
  std::shared_ptr<GpuResources> getResources();
51
52
 
52
- /// Trains CAGRA based on the given vector data and add them along with ids.
53
- /// NB: The use of the add function here is to build the CAGRA graph on
54
- /// the base dataset. Use this function when you want to add vectors with
55
- /// ids. Ref: https://github.com/facebookresearch/faiss/issues/4107
53
+ /// Pointer-based construction is unsupported. Use a caller-owned Dataset.
56
54
  void add(idx_t n, const uint8_t* x) override;
57
-
58
- /// Trains CAGRA based on the given vector data.
59
- /// NB: The use of the train function here is to build the CAGRA graph on
60
- /// the base dataset and is currently the only function to add the full set
61
- /// of vectors (without IDs) to the index. There is no external quantizer to
62
- /// be trained here.
63
55
  void train(idx_t n, const uint8_t* x) override;
64
56
 
65
- /// Initialize ourselves from the given CPU index; will overwrite
66
- /// all data in ourselves
67
- void copyFrom(const faiss::IndexBinaryHNSWCagra* index);
57
+ /// Build the full graph, borrowing UInt8 vectors with dim == d / 8.
58
+ /// The backing storage must remain alive and unchanged until all index
59
+ /// operations have completed. Does nothing if already trained; call reset()
60
+ /// before building again.
61
+ void train(const Dataset& dataset);
62
+ void add(const Dataset& dataset);
63
+ void train_dataset(const Dataset& dataset) override {
64
+ train(dataset);
65
+ }
66
+ void add_dataset(const Dataset& dataset) override {
67
+ add(dataset);
68
+ }
69
+
70
+ /// Rebind search storage without changing the graph. The vectors and their
71
+ /// ordering must match the training data. Requires compatible device data;
72
+ /// any padding must be zero and the caller retains ownership. Previous
73
+ /// work on the index stream completes before rebinding.
74
+ void updateDataset(const Dataset& dataset);
75
+ bool isSearchReady() const;
76
+
77
+ /// Copy the CPU graph. Without a Dataset, import only the graph and
78
+ /// metadata; attach vectors with updateDataset before search or copyTo.
79
+ /// If provided, dataset must describe writable, compatible storage on this
80
+ /// device with the CPU index's shape and UInt8 type. Copy CPU vectors into
81
+ /// that allocation, zero padding, and borrow it for search. The caller owns
82
+ /// the allocation and must keep it alive through index use. Copies complete
83
+ /// before return, so the CPU index can be released in either case.
84
+ void copyFrom(
85
+ const faiss::IndexBinaryHNSWCagra* index,
86
+ Dataset* dataset = nullptr);
68
87
 
69
88
  /// Copy ourselves to the given CPU index; will overwrite all data
70
89
  /// in the index instance
@@ -27,6 +27,11 @@
27
27
  #include <faiss/gpu/GpuIndex.h>
28
28
  #include <faiss/gpu/GpuIndexIVFPQ.h>
29
29
 
30
+ #ifndef SWIG
31
+ #include <faiss/gpu/Dataset.h>
32
+ #include <optional>
33
+ #endif
34
+
30
35
  #include <variant>
31
36
  #include <vector>
32
37
  #include "faiss/Index.h"
@@ -216,7 +221,6 @@ struct GpuIndexCagraConfig : public GpuIndexConfig {
216
221
  std::shared_ptr<IVFPQBuildCagraConfig> ivf_pq_params{nullptr};
217
222
  std::shared_ptr<IVFPQSearchCagraConfig> ivf_pq_search_params{nullptr};
218
223
  float refine_rate = 2.0f;
219
- bool store_dataset = true;
220
224
 
221
225
  /// Whether to use MST optimization to guarantee graph connectivity.
222
226
  bool guarantee_connectivity = false;
@@ -309,28 +313,49 @@ struct GpuIndexCagra : public GpuIndex {
309
313
  faiss::MetricType metric = faiss::METRIC_L2,
310
314
  GpuIndexCagraConfig config = GpuIndexCagraConfig());
311
315
 
312
- /// Trains CAGRA based on the given vector data and add them along with ids.
313
- /// NB: The use of the add function here is to build the CAGRA graph on
314
- /// the base dataset. Use this function when you want to add vectors with
315
- /// ids. Ref: https://github.com/facebookresearch/faiss/issues/4107
316
+ ~GpuIndexCagra() override;
317
+
318
+ /// Build the full graph while borrowing dataset storage. The backing
319
+ /// allocation must remain alive and unchanged through index use and all
320
+ /// outstanding GPU work. Does nothing if already trained; call reset()
321
+ /// before building again.
322
+ void train(const Dataset& dataset);
323
+ void train_dataset(const Dataset& dataset) override {
324
+ train(dataset);
325
+ }
326
+
327
+ /// Equivalent to train(dataset); incremental insertion is unsupported.
328
+ void add(const Dataset& dataset);
329
+ void add_dataset(const Dataset& dataset) override {
330
+ add(dataset);
331
+ }
332
+
333
+ /// Attach search-compatible device storage without changing the graph.
334
+ /// Shape, dtype and vector ordering must match the trained dataset. The
335
+ /// values must be the same; this method does not rebuild for changed data.
336
+ /// Previous work on this index's stream completes before rebinding.
337
+ void updateDataset(const Dataset& dataset);
338
+ bool isSearchReady() const;
339
+
340
+ /// Copy the CPU graph and metadata. With no dataset, import only the
341
+ /// graph: no vector storage is retained and GPU search is unavailable.
342
+ /// When supplied, dataset must describe writable CAGRA-compatible device
343
+ /// storage with matching shape and dtype on this index's device. Its
344
+ /// contents are overwritten with CPU vectors and its padding is zeroed.
345
+ /// The index borrows that allocation and becomes search-ready. The caller
346
+ /// retains ownership and must keep the allocation alive through index use
347
+ /// and queued GPU work. Copies finish before return, so the CPU index may
348
+ /// then be released in either mode.
349
+ void copyFrom(
350
+ const faiss::IndexHNSWCagra* index,
351
+ Dataset* dataset = nullptr);
352
+
353
+ /// Legacy pointer entry points throw: use the Dataset overloads.
316
354
  void add(idx_t n, const float* x) override;
317
355
  void add_ex(idx_t n, const void* x, NumericType numeric_type) override;
318
-
319
- /// Trains CAGRA based on the given vector data.
320
- /// NB: The use of the train function here is to build the CAGRA graph on
321
- /// the base dataset and is currently the only function to add the full set
322
- /// of vectors (without IDs) to the index. There is no external quantizer to
323
- /// be trained here.
324
356
  void train(idx_t n, const float* x) override;
325
357
  void train_ex(idx_t n, const void* x, NumericType numeric_type) override;
326
358
 
327
- /// Initialize ourselves from the given CPU index; will overwrite
328
- /// all data in ourselves
329
- void copyFrom(const faiss::IndexHNSWCagra* index);
330
- void copyFrom_ex(
331
- const faiss::IndexHNSWCagra* index,
332
- NumericType numeric_type);
333
-
334
359
  /// Copy ourselves to the given CPU index; will overwrite all data
335
360
  /// in the index instance
336
361
  void copyTo(faiss::IndexHNSWCagra* index) const;
@@ -372,7 +397,9 @@ struct GpuIndexCagra : public GpuIndex {
372
397
  /// more than one device: cuVS `all_neighbors` knn graph construction over
373
398
  /// overlapping clusters, followed by graph pruning. Leaves the result in
374
399
  /// merged_knngraph_ for copyTo(); index_ stays empty.
375
- void trainAllNeighbors_(idx_t n, const float* x);
400
+ #ifndef SWIG
401
+ void trainAllNeighbors_(const Dataset& dataset);
402
+ #endif
376
403
 
377
404
  /// Populate HNSW levels >= 1 of `index` by building a CAGRA graph over
378
405
  /// each level's node subset on the GPU. Requires the level table to be
@@ -385,9 +412,9 @@ struct GpuIndexCagra : public GpuIndex {
385
412
  /// Our configuration options
386
413
  const GpuIndexCagraConfig cagraConfig_;
387
414
 
388
- faiss::NumericType numeric_type_;
415
+ faiss::NumericType numeric_type_ = NumericType::Float32;
389
416
 
390
- /// Instance that we own; contains the inverted lists
417
+ /// Instance that owns the CAGRA graph and borrows dataset storage
391
418
  std::variant<
392
419
  std::monostate,
393
420
  std::shared_ptr<CuvsCagra<float>>,
@@ -398,7 +425,9 @@ struct GpuIndexCagra : public GpuIndex {
398
425
  /// Multi-GPU state: populated by trainAllNeighbors_(), used by copyTo()
399
426
  std::vector<idx_t> merged_knngraph_;
400
427
  idx_t merged_knngraph_degree_ = 0;
401
- const float* multi_gpu_dataset_ = nullptr;
428
+ #ifndef SWIG
429
+ std::optional<Dataset> multiGpuDataset_;
430
+ #endif
402
431
  };
403
432
 
404
433
  } // namespace gpu
@@ -131,6 +131,17 @@ class GpuIndexFlat : public GpuIndex {
131
131
  idx_t* labels,
132
132
  const SearchParameters* params) const override;
133
133
 
134
+ /// Called from GpuIndex for search; Float16 queries are searched without
135
+ /// conversion against float16 storage
136
+ void searchImpl_ex_(
137
+ idx_t n,
138
+ const void* x,
139
+ NumericType numeric_type,
140
+ int k,
141
+ float* distances,
142
+ idx_t* labels,
143
+ const SearchParameters* params) const override;
144
+
134
145
  protected:
135
146
  /// Our configuration options
136
147
  const GpuIndexFlatConfig flatConfig_;
@@ -32,6 +32,12 @@ struct GpuIndexIVFConfig : public GpuIndexConfig {
32
32
  /// throw an exception for indices not implemented on GPU. When set to
33
33
  /// true, it will fallback to a CPU implementation.
34
34
  bool allowCpuCoarseQuantizer = false;
35
+
36
+ /// Break a distance tie on the smaller id, so a search repeats. Honoured
37
+ /// by GpuIndexIVFFlat and GpuIndexIVFScalarQuantizer with the interleaved
38
+ /// layout for k up to 1024, and by the cuVS GpuIndexIVFScalarQuantizer
39
+ /// and GpuIndexIVFPQ for k and nprobe up to 256.
40
+ bool deterministic_tie_break = false;
35
41
  };
36
42
 
37
43
  /// Base class of all GPU IVF index types. This (for now) deliberately does not