faiss 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (293) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE.txt +1 -1
  4. data/lib/faiss/version.rb +1 -1
  5. data/vendor/faiss/faiss/AutoTune.cpp +2 -2
  6. data/vendor/faiss/faiss/AutoTune.h +3 -3
  7. data/vendor/faiss/faiss/Clustering.cpp +37 -6
  8. data/vendor/faiss/faiss/Clustering.h +12 -3
  9. data/vendor/faiss/faiss/IVFlib.cpp +6 -3
  10. data/vendor/faiss/faiss/IVFlib.h +2 -2
  11. data/vendor/faiss/faiss/Index.cpp +6 -2
  12. data/vendor/faiss/faiss/Index.h +30 -8
  13. data/vendor/faiss/faiss/Index2Layer.cpp +2 -2
  14. data/vendor/faiss/faiss/Index2Layer.h +2 -2
  15. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +7 -7
  16. data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +2 -2
  17. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +14 -16
  18. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.h +2 -2
  19. data/vendor/faiss/faiss/IndexBinary.cpp +13 -2
  20. data/vendor/faiss/faiss/IndexBinary.h +8 -2
  21. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +2 -3
  22. data/vendor/faiss/faiss/IndexBinaryFlat.h +2 -2
  23. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +2 -2
  24. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +2 -2
  25. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +2 -7
  26. data/vendor/faiss/faiss/IndexBinaryHNSW.h +3 -3
  27. data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -3
  28. data/vendor/faiss/faiss/IndexBinaryHash.h +2 -2
  29. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +3 -3
  30. data/vendor/faiss/faiss/IndexBinaryIVF.h +2 -2
  31. data/vendor/faiss/faiss/IndexFastScan.cpp +32 -18
  32. data/vendor/faiss/faiss/IndexFastScan.h +11 -2
  33. data/vendor/faiss/faiss/IndexFlat.cpp +13 -10
  34. data/vendor/faiss/faiss/IndexFlat.h +2 -2
  35. data/vendor/faiss/faiss/IndexFlatCodes.cpp +170 -7
  36. data/vendor/faiss/faiss/IndexFlatCodes.h +25 -5
  37. data/vendor/faiss/faiss/IndexHNSW.cpp +156 -96
  38. data/vendor/faiss/faiss/IndexHNSW.h +54 -5
  39. data/vendor/faiss/faiss/IndexIDMap.cpp +19 -3
  40. data/vendor/faiss/faiss/IndexIDMap.h +5 -2
  41. data/vendor/faiss/faiss/IndexIVF.cpp +5 -6
  42. data/vendor/faiss/faiss/IndexIVF.h +13 -4
  43. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +21 -7
  44. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +5 -2
  45. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +3 -14
  46. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +2 -4
  47. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +201 -91
  48. data/vendor/faiss/faiss/IndexIVFFastScan.h +33 -9
  49. data/vendor/faiss/faiss/IndexIVFFlat.cpp +2 -2
  50. data/vendor/faiss/faiss/IndexIVFFlat.h +2 -2
  51. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +2 -2
  52. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +2 -2
  53. data/vendor/faiss/faiss/IndexIVFPQ.cpp +3 -6
  54. data/vendor/faiss/faiss/IndexIVFPQ.h +2 -2
  55. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +7 -14
  56. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +2 -4
  57. data/vendor/faiss/faiss/IndexIVFPQR.cpp +2 -2
  58. data/vendor/faiss/faiss/IndexIVFPQR.h +2 -2
  59. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +2 -3
  60. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +2 -2
  61. data/vendor/faiss/faiss/IndexLSH.cpp +2 -3
  62. data/vendor/faiss/faiss/IndexLSH.h +2 -2
  63. data/vendor/faiss/faiss/IndexLattice.cpp +3 -21
  64. data/vendor/faiss/faiss/IndexLattice.h +5 -24
  65. data/vendor/faiss/faiss/IndexNNDescent.cpp +2 -31
  66. data/vendor/faiss/faiss/IndexNNDescent.h +3 -3
  67. data/vendor/faiss/faiss/IndexNSG.cpp +2 -5
  68. data/vendor/faiss/faiss/IndexNSG.h +3 -3
  69. data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +56 -0
  70. data/vendor/faiss/faiss/IndexNeuralNetCodec.h +49 -0
  71. data/vendor/faiss/faiss/IndexPQ.cpp +26 -26
  72. data/vendor/faiss/faiss/IndexPQ.h +2 -2
  73. data/vendor/faiss/faiss/IndexPQFastScan.cpp +2 -5
  74. data/vendor/faiss/faiss/IndexPQFastScan.h +2 -11
  75. data/vendor/faiss/faiss/IndexPreTransform.cpp +2 -2
  76. data/vendor/faiss/faiss/IndexPreTransform.h +3 -3
  77. data/vendor/faiss/faiss/IndexRefine.cpp +46 -9
  78. data/vendor/faiss/faiss/IndexRefine.h +9 -2
  79. data/vendor/faiss/faiss/IndexReplicas.cpp +2 -2
  80. data/vendor/faiss/faiss/IndexReplicas.h +2 -2
  81. data/vendor/faiss/faiss/IndexRowwiseMinMax.cpp +2 -2
  82. data/vendor/faiss/faiss/IndexRowwiseMinMax.h +2 -2
  83. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +5 -4
  84. data/vendor/faiss/faiss/IndexScalarQuantizer.h +2 -2
  85. data/vendor/faiss/faiss/IndexShards.cpp +2 -2
  86. data/vendor/faiss/faiss/IndexShards.h +2 -2
  87. data/vendor/faiss/faiss/IndexShardsIVF.cpp +2 -2
  88. data/vendor/faiss/faiss/IndexShardsIVF.h +2 -2
  89. data/vendor/faiss/faiss/MatrixStats.cpp +2 -2
  90. data/vendor/faiss/faiss/MatrixStats.h +2 -2
  91. data/vendor/faiss/faiss/MetaIndexes.cpp +2 -3
  92. data/vendor/faiss/faiss/MetaIndexes.h +2 -2
  93. data/vendor/faiss/faiss/MetricType.h +9 -4
  94. data/vendor/faiss/faiss/VectorTransform.cpp +2 -2
  95. data/vendor/faiss/faiss/VectorTransform.h +2 -2
  96. data/vendor/faiss/faiss/clone_index.cpp +2 -2
  97. data/vendor/faiss/faiss/clone_index.h +2 -2
  98. data/vendor/faiss/faiss/cppcontrib/SaDecodeKernels.h +2 -2
  99. data/vendor/faiss/faiss/cppcontrib/detail/CoarseBitType.h +2 -2
  100. data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +97 -19
  101. data/vendor/faiss/faiss/cppcontrib/factory_tools.cpp +192 -0
  102. data/vendor/faiss/faiss/cppcontrib/factory_tools.h +29 -0
  103. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-avx2-inl.h +2 -2
  104. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +85 -32
  105. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-neon-inl.h +2 -2
  106. data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMax-inl.h +2 -2
  107. data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMaxFP16-inl.h +2 -2
  108. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-avx2-inl.h +2 -2
  109. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-inl.h +2 -2
  110. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-neon-inl.h +2 -2
  111. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +2 -5
  112. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +2 -2
  113. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +45 -13
  114. data/vendor/faiss/faiss/gpu/GpuCloner.h +2 -2
  115. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +12 -6
  116. data/vendor/faiss/faiss/gpu/GpuDistance.h +11 -7
  117. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +3 -3
  118. data/vendor/faiss/faiss/gpu/GpuIcmEncoder.h +2 -2
  119. data/vendor/faiss/faiss/gpu/GpuIndex.h +10 -15
  120. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +2 -2
  121. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +285 -0
  122. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +2 -2
  123. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +8 -2
  124. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +4 -2
  125. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +3 -3
  126. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +2 -2
  127. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +2 -2
  128. data/vendor/faiss/faiss/gpu/GpuResources.cpp +7 -2
  129. data/vendor/faiss/faiss/gpu/GpuResources.h +11 -4
  130. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +66 -11
  131. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +15 -5
  132. data/vendor/faiss/faiss/gpu/impl/IndexUtils.h +2 -2
  133. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +28 -23
  134. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +2 -2
  135. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +2 -2
  136. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +2 -2
  137. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +2 -2
  138. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +2 -2
  139. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +8 -2
  140. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +2 -3
  141. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +2 -2
  142. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +10 -7
  143. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +2 -2
  144. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +54 -54
  145. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +144 -77
  146. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +51 -51
  147. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +2 -2
  148. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +3 -3
  149. data/vendor/faiss/faiss/gpu/test/TestGpuResidualQuantizer.cpp +70 -0
  150. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +74 -4
  151. data/vendor/faiss/faiss/gpu/test/TestUtils.h +2 -2
  152. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +3 -3
  153. data/vendor/faiss/faiss/gpu/utils/{RaftUtils.h → CuvsUtils.h} +12 -11
  154. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +8 -2
  155. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +2 -2
  156. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +2 -2
  157. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +2 -2
  158. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +6 -3
  159. data/vendor/faiss/faiss/gpu/utils/Timer.h +3 -3
  160. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +79 -11
  161. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +17 -5
  162. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +27 -2
  163. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +11 -3
  164. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  165. data/vendor/faiss/faiss/impl/CodePacker.h +2 -2
  166. data/vendor/faiss/faiss/impl/DistanceComputer.h +48 -2
  167. data/vendor/faiss/faiss/impl/FaissAssert.h +6 -4
  168. data/vendor/faiss/faiss/impl/FaissException.cpp +2 -2
  169. data/vendor/faiss/faiss/impl/FaissException.h +2 -3
  170. data/vendor/faiss/faiss/impl/HNSW.cpp +378 -205
  171. data/vendor/faiss/faiss/impl/HNSW.h +55 -24
  172. data/vendor/faiss/faiss/impl/IDSelector.cpp +2 -2
  173. data/vendor/faiss/faiss/impl/IDSelector.h +2 -2
  174. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +10 -10
  175. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +2 -2
  176. data/vendor/faiss/faiss/impl/LookupTableScaler.h +36 -2
  177. data/vendor/faiss/faiss/impl/NNDescent.cpp +15 -10
  178. data/vendor/faiss/faiss/impl/NNDescent.h +2 -2
  179. data/vendor/faiss/faiss/impl/NSG.cpp +26 -49
  180. data/vendor/faiss/faiss/impl/NSG.h +20 -8
  181. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +2 -2
  182. data/vendor/faiss/faiss/impl/PolysemousTraining.h +2 -2
  183. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +2 -4
  184. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +2 -2
  185. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +2 -2
  186. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +3 -2
  187. data/vendor/faiss/faiss/impl/ProductQuantizer.h +7 -3
  188. data/vendor/faiss/faiss/impl/Quantizer.h +2 -2
  189. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +2 -36
  190. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +3 -13
  191. data/vendor/faiss/faiss/impl/ResultHandler.h +153 -34
  192. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +721 -104
  193. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +5 -2
  194. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +2 -2
  195. data/vendor/faiss/faiss/impl/ThreadedIndex.h +2 -2
  196. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +7 -2
  197. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx512.h +248 -0
  198. data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +2 -2
  199. data/vendor/faiss/faiss/impl/code_distance/code_distance-sve.h +440 -0
  200. data/vendor/faiss/faiss/impl/code_distance/code_distance.h +55 -2
  201. data/vendor/faiss/faiss/impl/index_read.cpp +31 -20
  202. data/vendor/faiss/faiss/impl/index_read_utils.h +37 -0
  203. data/vendor/faiss/faiss/impl/index_write.cpp +30 -16
  204. data/vendor/faiss/faiss/impl/io.cpp +15 -7
  205. data/vendor/faiss/faiss/impl/io.h +6 -6
  206. data/vendor/faiss/faiss/impl/io_macros.h +8 -9
  207. data/vendor/faiss/faiss/impl/kmeans1d.cpp +2 -3
  208. data/vendor/faiss/faiss/impl/kmeans1d.h +2 -2
  209. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +2 -3
  210. data/vendor/faiss/faiss/impl/lattice_Zn.h +2 -2
  211. data/vendor/faiss/faiss/impl/platform_macros.h +34 -2
  212. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +13 -2
  213. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +20 -2
  214. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +3 -3
  215. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +450 -3
  216. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +8 -8
  217. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +3 -3
  218. data/vendor/faiss/faiss/impl/simd_result_handlers.h +151 -67
  219. data/vendor/faiss/faiss/index_factory.cpp +51 -34
  220. data/vendor/faiss/faiss/index_factory.h +2 -2
  221. data/vendor/faiss/faiss/index_io.h +14 -7
  222. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +30 -10
  223. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +5 -2
  224. data/vendor/faiss/faiss/invlists/DirectMap.cpp +11 -3
  225. data/vendor/faiss/faiss/invlists/DirectMap.h +2 -2
  226. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +57 -19
  227. data/vendor/faiss/faiss/invlists/InvertedLists.h +20 -11
  228. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +2 -2
  229. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +2 -2
  230. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +23 -9
  231. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +4 -3
  232. data/vendor/faiss/faiss/python/python_callbacks.cpp +5 -5
  233. data/vendor/faiss/faiss/python/python_callbacks.h +2 -2
  234. data/vendor/faiss/faiss/utils/AlignedTable.h +5 -3
  235. data/vendor/faiss/faiss/utils/Heap.cpp +2 -2
  236. data/vendor/faiss/faiss/utils/Heap.h +107 -2
  237. data/vendor/faiss/faiss/utils/NeuralNet.cpp +346 -0
  238. data/vendor/faiss/faiss/utils/NeuralNet.h +147 -0
  239. data/vendor/faiss/faiss/utils/WorkerThread.cpp +2 -2
  240. data/vendor/faiss/faiss/utils/WorkerThread.h +2 -2
  241. data/vendor/faiss/faiss/utils/approx_topk/approx_topk.h +2 -2
  242. data/vendor/faiss/faiss/utils/approx_topk/avx2-inl.h +2 -2
  243. data/vendor/faiss/faiss/utils/approx_topk/generic.h +2 -2
  244. data/vendor/faiss/faiss/utils/approx_topk/mode.h +2 -2
  245. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +2 -2
  246. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  247. data/vendor/faiss/faiss/utils/distances.cpp +249 -90
  248. data/vendor/faiss/faiss/utils/distances.h +8 -8
  249. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -2
  250. data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
  251. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
  252. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +2 -2
  253. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +2 -2
  254. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +2 -2
  255. data/vendor/faiss/faiss/utils/distances_simd.cpp +1543 -56
  256. data/vendor/faiss/faiss/utils/extra_distances-inl.h +72 -2
  257. data/vendor/faiss/faiss/utils/extra_distances.cpp +87 -140
  258. data/vendor/faiss/faiss/utils/extra_distances.h +5 -4
  259. data/vendor/faiss/faiss/utils/fp16-arm.h +2 -2
  260. data/vendor/faiss/faiss/utils/fp16-fp16c.h +2 -2
  261. data/vendor/faiss/faiss/utils/fp16-inl.h +2 -2
  262. data/vendor/faiss/faiss/utils/fp16.h +2 -2
  263. data/vendor/faiss/faiss/utils/hamming-inl.h +2 -2
  264. data/vendor/faiss/faiss/utils/hamming.cpp +3 -4
  265. data/vendor/faiss/faiss/utils/hamming.h +2 -2
  266. data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +2 -2
  267. data/vendor/faiss/faiss/utils/hamming_distance/avx512-inl.h +490 -0
  268. data/vendor/faiss/faiss/utils/hamming_distance/common.h +2 -2
  269. data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +6 -3
  270. data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +7 -3
  271. data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +5 -5
  272. data/vendor/faiss/faiss/utils/ordered_key_value.h +2 -2
  273. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  274. data/vendor/faiss/faiss/utils/partitioning.h +2 -2
  275. data/vendor/faiss/faiss/utils/prefetch.h +2 -2
  276. data/vendor/faiss/faiss/utils/quantize_lut.cpp +2 -2
  277. data/vendor/faiss/faiss/utils/quantize_lut.h +2 -2
  278. data/vendor/faiss/faiss/utils/random.cpp +45 -2
  279. data/vendor/faiss/faiss/utils/random.h +27 -2
  280. data/vendor/faiss/faiss/utils/simdlib.h +12 -3
  281. data/vendor/faiss/faiss/utils/simdlib_avx2.h +2 -2
  282. data/vendor/faiss/faiss/utils/simdlib_avx512.h +296 -0
  283. data/vendor/faiss/faiss/utils/simdlib_emulated.h +2 -2
  284. data/vendor/faiss/faiss/utils/simdlib_neon.h +7 -4
  285. data/vendor/faiss/faiss/utils/simdlib_ppc64.h +1084 -0
  286. data/vendor/faiss/faiss/utils/sorting.cpp +2 -2
  287. data/vendor/faiss/faiss/utils/sorting.h +2 -2
  288. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +2 -2
  289. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +176 -0
  290. data/vendor/faiss/faiss/utils/utils.cpp +17 -10
  291. data/vendor/faiss/faiss/utils/utils.h +7 -3
  292. metadata +22 -11
  293. data/vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h +0 -102
@@ -0,0 +1,440 @@
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
+ #ifdef __ARM_FEATURE_SVE
11
+
12
+ #include <arm_sve.h>
13
+
14
+ #include <tuple>
15
+ #include <type_traits>
16
+
17
+ #include <faiss/impl/code_distance/code_distance-generic.h>
18
+
19
+ namespace faiss {
20
+
21
+ template <typename PQDecoderT>
22
+ std::enable_if_t<!std::is_same_v<PQDecoderT, PQDecoder8>, float> inline distance_single_code_sve(
23
+ // the product quantizer
24
+ const size_t M,
25
+ // number of bits per quantization index
26
+ const size_t nbits,
27
+ // precomputed distances, layout (M, ksub)
28
+ const float* sim_table,
29
+ const uint8_t* code) {
30
+ // default implementation
31
+ return distance_single_code_generic<PQDecoderT>(M, nbits, sim_table, code);
32
+ }
33
+
34
+ static inline void distance_codes_kernel(
35
+ svbool_t pg,
36
+ svuint32_t idx1,
37
+ svuint32_t offsets_0,
38
+ const float* tab,
39
+ svfloat32_t& partialSum) {
40
+ // add offset
41
+ const auto indices_to_read_from = svadd_u32_x(pg, idx1, offsets_0);
42
+
43
+ // gather values, similar to some operations of tab[index]
44
+ const auto collected =
45
+ svld1_gather_u32index_f32(pg, tab, indices_to_read_from);
46
+
47
+ // collect partial sum
48
+ partialSum = svadd_f32_m(pg, partialSum, collected);
49
+ }
50
+
51
+ static float distance_single_code_sve_for_small_m(
52
+ // the product quantizer
53
+ const size_t M,
54
+ // precomputed distances, layout (M, ksub)
55
+ const float* sim_table,
56
+ // codes
57
+ const uint8_t* __restrict code) {
58
+ constexpr size_t nbits = 8u;
59
+
60
+ const size_t ksub = 1 << nbits;
61
+
62
+ const auto offsets_0 = svindex_u32(0, static_cast<uint32_t>(ksub));
63
+
64
+ // loop
65
+ const auto pg = svwhilelt_b32_u64(0, M);
66
+
67
+ auto mm1 = svld1ub_u32(pg, code);
68
+ mm1 = svadd_u32_x(pg, mm1, offsets_0);
69
+ const auto collected0 = svld1_gather_u32index_f32(pg, sim_table, mm1);
70
+ return svaddv_f32(pg, collected0);
71
+ }
72
+
73
+ template <typename PQDecoderT>
74
+ std::enable_if_t<std::is_same_v<PQDecoderT, PQDecoder8>, float> inline distance_single_code_sve(
75
+ // the product quantizer
76
+ const size_t M,
77
+ // number of bits per quantization index
78
+ const size_t nbits,
79
+ // precomputed distances, layout (M, ksub)
80
+ const float* sim_table,
81
+ const uint8_t* code) {
82
+ if (M <= svcntw())
83
+ return distance_single_code_sve_for_small_m(M, sim_table, code);
84
+
85
+ const float* tab = sim_table;
86
+
87
+ const size_t ksub = 1 << nbits;
88
+
89
+ const auto offsets_0 = svindex_u32(0, static_cast<uint32_t>(ksub));
90
+
91
+ // accumulators of partial sums
92
+ auto partialSum = svdup_n_f32(0.f);
93
+
94
+ const auto lanes = svcntb();
95
+ const auto quad_lanes = lanes / 4;
96
+
97
+ // loop
98
+ for (std::size_t m = 0; m < M;) {
99
+ const auto pg = svwhilelt_b8_u64(m, M);
100
+
101
+ const auto mm1 = svld1_u8(pg, code + m);
102
+ {
103
+ const auto mm1lo = svunpklo_u16(mm1);
104
+ const auto pglo = svunpklo_b(pg);
105
+
106
+ {
107
+ // convert uint8 values to uint32 values
108
+ const auto idx1 = svunpklo_u32(mm1lo);
109
+ const auto pglolo = svunpklo_b(pglo);
110
+
111
+ distance_codes_kernel(pglolo, idx1, offsets_0, tab, partialSum);
112
+ tab += ksub * quad_lanes;
113
+ }
114
+
115
+ m += quad_lanes;
116
+ if (m >= M)
117
+ break;
118
+
119
+ {
120
+ // convert uint8 values to uint32 values
121
+ const auto idx1 = svunpkhi_u32(mm1lo);
122
+ const auto pglohi = svunpkhi_b(pglo);
123
+
124
+ distance_codes_kernel(pglohi, idx1, offsets_0, tab, partialSum);
125
+ tab += ksub * quad_lanes;
126
+ }
127
+
128
+ m += quad_lanes;
129
+ if (m >= M)
130
+ break;
131
+ }
132
+
133
+ {
134
+ const auto mm1hi = svunpkhi_u16(mm1);
135
+ const auto pghi = svunpkhi_b(pg);
136
+
137
+ {
138
+ // convert uint8 values to uint32 values
139
+ const auto idx1 = svunpklo_u32(mm1hi);
140
+ const auto pghilo = svunpklo_b(pghi);
141
+
142
+ distance_codes_kernel(pghilo, idx1, offsets_0, tab, partialSum);
143
+ tab += ksub * quad_lanes;
144
+ }
145
+
146
+ m += quad_lanes;
147
+ if (m >= M)
148
+ break;
149
+
150
+ {
151
+ // convert uint8 values to uint32 values
152
+ const auto idx1 = svunpkhi_u32(mm1hi);
153
+ const auto pghihi = svunpkhi_b(pghi);
154
+
155
+ distance_codes_kernel(pghihi, idx1, offsets_0, tab, partialSum);
156
+ tab += ksub * quad_lanes;
157
+ }
158
+
159
+ m += quad_lanes;
160
+ }
161
+ }
162
+
163
+ return svaddv_f32(svptrue_b32(), partialSum);
164
+ }
165
+
166
+ template <typename PQDecoderT>
167
+ std::enable_if_t<!std::is_same_v<PQDecoderT, PQDecoder8>, void>
168
+ distance_four_codes_sve(
169
+ // the product quantizer
170
+ const size_t M,
171
+ // number of bits per quantization index
172
+ const size_t nbits,
173
+ // precomputed distances, layout (M, ksub)
174
+ const float* sim_table,
175
+ // codes
176
+ const uint8_t* __restrict code0,
177
+ const uint8_t* __restrict code1,
178
+ const uint8_t* __restrict code2,
179
+ const uint8_t* __restrict code3,
180
+ // computed distances
181
+ float& result0,
182
+ float& result1,
183
+ float& result2,
184
+ float& result3) {
185
+ distance_four_codes_generic<PQDecoderT>(
186
+ M,
187
+ nbits,
188
+ sim_table,
189
+ code0,
190
+ code1,
191
+ code2,
192
+ code3,
193
+ result0,
194
+ result1,
195
+ result2,
196
+ result3);
197
+ }
198
+
199
+ static void distance_four_codes_sve_for_small_m(
200
+ // the product quantizer
201
+ const size_t M,
202
+ // precomputed distances, layout (M, ksub)
203
+ const float* sim_table,
204
+ // codes
205
+ const uint8_t* __restrict code0,
206
+ const uint8_t* __restrict code1,
207
+ const uint8_t* __restrict code2,
208
+ const uint8_t* __restrict code3,
209
+ // computed distances
210
+ float& result0,
211
+ float& result1,
212
+ float& result2,
213
+ float& result3) {
214
+ constexpr size_t nbits = 8u;
215
+
216
+ const size_t ksub = 1 << nbits;
217
+
218
+ const auto offsets_0 = svindex_u32(0, static_cast<uint32_t>(ksub));
219
+
220
+ const auto quad_lanes = svcntw();
221
+
222
+ // loop
223
+ const auto pg = svwhilelt_b32_u64(0, M);
224
+
225
+ auto mm10 = svld1ub_u32(pg, code0);
226
+ auto mm11 = svld1ub_u32(pg, code1);
227
+ auto mm12 = svld1ub_u32(pg, code2);
228
+ auto mm13 = svld1ub_u32(pg, code3);
229
+ mm10 = svadd_u32_x(pg, mm10, offsets_0);
230
+ mm11 = svadd_u32_x(pg, mm11, offsets_0);
231
+ mm12 = svadd_u32_x(pg, mm12, offsets_0);
232
+ mm13 = svadd_u32_x(pg, mm13, offsets_0);
233
+ const auto collected0 = svld1_gather_u32index_f32(pg, sim_table, mm10);
234
+ const auto collected1 = svld1_gather_u32index_f32(pg, sim_table, mm11);
235
+ const auto collected2 = svld1_gather_u32index_f32(pg, sim_table, mm12);
236
+ const auto collected3 = svld1_gather_u32index_f32(pg, sim_table, mm13);
237
+ result0 = svaddv_f32(pg, collected0);
238
+ result1 = svaddv_f32(pg, collected1);
239
+ result2 = svaddv_f32(pg, collected2);
240
+ result3 = svaddv_f32(pg, collected3);
241
+ }
242
+
243
+ // Combines 4 operations of distance_single_code()
244
+ template <typename PQDecoderT>
245
+ std::enable_if_t<std::is_same_v<PQDecoderT, PQDecoder8>, void>
246
+ distance_four_codes_sve(
247
+ // the product quantizer
248
+ const size_t M,
249
+ // number of bits per quantization index
250
+ const size_t nbits,
251
+ // precomputed distances, layout (M, ksub)
252
+ const float* sim_table,
253
+ // codes
254
+ const uint8_t* __restrict code0,
255
+ const uint8_t* __restrict code1,
256
+ const uint8_t* __restrict code2,
257
+ const uint8_t* __restrict code3,
258
+ // computed distances
259
+ float& result0,
260
+ float& result1,
261
+ float& result2,
262
+ float& result3) {
263
+ if (M <= svcntw()) {
264
+ distance_four_codes_sve_for_small_m(
265
+ M,
266
+ sim_table,
267
+ code0,
268
+ code1,
269
+ code2,
270
+ code3,
271
+ result0,
272
+ result1,
273
+ result2,
274
+ result3);
275
+ return;
276
+ }
277
+
278
+ const float* tab = sim_table;
279
+
280
+ const size_t ksub = 1 << nbits;
281
+
282
+ const auto offsets_0 = svindex_u32(0, static_cast<uint32_t>(ksub));
283
+
284
+ // accumulators of partial sums
285
+ auto partialSum0 = svdup_n_f32(0.f);
286
+ auto partialSum1 = svdup_n_f32(0.f);
287
+ auto partialSum2 = svdup_n_f32(0.f);
288
+ auto partialSum3 = svdup_n_f32(0.f);
289
+
290
+ const auto lanes = svcntb();
291
+ const auto quad_lanes = lanes / 4;
292
+
293
+ // loop
294
+ for (std::size_t m = 0; m < M;) {
295
+ const auto pg = svwhilelt_b8_u64(m, M);
296
+
297
+ const auto mm10 = svld1_u8(pg, code0 + m);
298
+ const auto mm11 = svld1_u8(pg, code1 + m);
299
+ const auto mm12 = svld1_u8(pg, code2 + m);
300
+ const auto mm13 = svld1_u8(pg, code3 + m);
301
+ {
302
+ const auto mm10lo = svunpklo_u16(mm10);
303
+ const auto mm11lo = svunpklo_u16(mm11);
304
+ const auto mm12lo = svunpklo_u16(mm12);
305
+ const auto mm13lo = svunpklo_u16(mm13);
306
+ const auto pglo = svunpklo_b(pg);
307
+
308
+ {
309
+ const auto pglolo = svunpklo_b(pglo);
310
+ {
311
+ const auto idx1 = svunpklo_u32(mm10lo);
312
+ distance_codes_kernel(
313
+ pglolo, idx1, offsets_0, tab, partialSum0);
314
+ }
315
+ {
316
+ const auto idx1 = svunpklo_u32(mm11lo);
317
+ distance_codes_kernel(
318
+ pglolo, idx1, offsets_0, tab, partialSum1);
319
+ }
320
+ {
321
+ const auto idx1 = svunpklo_u32(mm12lo);
322
+ distance_codes_kernel(
323
+ pglolo, idx1, offsets_0, tab, partialSum2);
324
+ }
325
+ {
326
+ const auto idx1 = svunpklo_u32(mm13lo);
327
+ distance_codes_kernel(
328
+ pglolo, idx1, offsets_0, tab, partialSum3);
329
+ }
330
+ tab += ksub * quad_lanes;
331
+ }
332
+
333
+ m += quad_lanes;
334
+ if (m >= M)
335
+ break;
336
+
337
+ {
338
+ const auto pglohi = svunpkhi_b(pglo);
339
+ {
340
+ const auto idx1 = svunpkhi_u32(mm10lo);
341
+ distance_codes_kernel(
342
+ pglohi, idx1, offsets_0, tab, partialSum0);
343
+ }
344
+ {
345
+ const auto idx1 = svunpkhi_u32(mm11lo);
346
+ distance_codes_kernel(
347
+ pglohi, idx1, offsets_0, tab, partialSum1);
348
+ }
349
+ {
350
+ const auto idx1 = svunpkhi_u32(mm12lo);
351
+ distance_codes_kernel(
352
+ pglohi, idx1, offsets_0, tab, partialSum2);
353
+ }
354
+ {
355
+ const auto idx1 = svunpkhi_u32(mm13lo);
356
+ distance_codes_kernel(
357
+ pglohi, idx1, offsets_0, tab, partialSum3);
358
+ }
359
+ tab += ksub * quad_lanes;
360
+ }
361
+
362
+ m += quad_lanes;
363
+ if (m >= M)
364
+ break;
365
+ }
366
+
367
+ {
368
+ const auto mm10hi = svunpkhi_u16(mm10);
369
+ const auto mm11hi = svunpkhi_u16(mm11);
370
+ const auto mm12hi = svunpkhi_u16(mm12);
371
+ const auto mm13hi = svunpkhi_u16(mm13);
372
+ const auto pghi = svunpkhi_b(pg);
373
+
374
+ {
375
+ const auto pghilo = svunpklo_b(pghi);
376
+ {
377
+ const auto idx1 = svunpklo_u32(mm10hi);
378
+ distance_codes_kernel(
379
+ pghilo, idx1, offsets_0, tab, partialSum0);
380
+ }
381
+ {
382
+ const auto idx1 = svunpklo_u32(mm11hi);
383
+ distance_codes_kernel(
384
+ pghilo, idx1, offsets_0, tab, partialSum1);
385
+ }
386
+ {
387
+ const auto idx1 = svunpklo_u32(mm12hi);
388
+ distance_codes_kernel(
389
+ pghilo, idx1, offsets_0, tab, partialSum2);
390
+ }
391
+ {
392
+ const auto idx1 = svunpklo_u32(mm13hi);
393
+ distance_codes_kernel(
394
+ pghilo, idx1, offsets_0, tab, partialSum3);
395
+ }
396
+ tab += ksub * quad_lanes;
397
+ }
398
+
399
+ m += quad_lanes;
400
+ if (m >= M)
401
+ break;
402
+
403
+ {
404
+ const auto pghihi = svunpkhi_b(pghi);
405
+ {
406
+ const auto idx1 = svunpkhi_u32(mm10hi);
407
+ distance_codes_kernel(
408
+ pghihi, idx1, offsets_0, tab, partialSum0);
409
+ }
410
+ {
411
+ const auto idx1 = svunpkhi_u32(mm11hi);
412
+ distance_codes_kernel(
413
+ pghihi, idx1, offsets_0, tab, partialSum1);
414
+ }
415
+ {
416
+ const auto idx1 = svunpkhi_u32(mm12hi);
417
+ distance_codes_kernel(
418
+ pghihi, idx1, offsets_0, tab, partialSum2);
419
+ }
420
+ {
421
+ const auto idx1 = svunpkhi_u32(mm13hi);
422
+ distance_codes_kernel(
423
+ pghihi, idx1, offsets_0, tab, partialSum3);
424
+ }
425
+ tab += ksub * quad_lanes;
426
+ }
427
+
428
+ m += quad_lanes;
429
+ }
430
+ }
431
+
432
+ result0 = svaddv_f32(svptrue_b32(), partialSum0);
433
+ result1 = svaddv_f32(svptrue_b32(), partialSum1);
434
+ result2 = svaddv_f32(svptrue_b32(), partialSum2);
435
+ result3 = svaddv_f32(svptrue_b32(), partialSum3);
436
+ }
437
+
438
+ } // namespace faiss
439
+
440
+ #endif
@@ -1,5 +1,5 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -77,6 +77,59 @@ inline void distance_four_codes(
77
77
 
78
78
  } // namespace faiss
79
79
 
80
+ #elif defined(__ARM_FEATURE_SVE)
81
+
82
+ #include <faiss/impl/code_distance/code_distance-sve.h>
83
+
84
+ namespace faiss {
85
+
86
+ template <typename PQDecoderT>
87
+ inline float distance_single_code(
88
+ // the product quantizer
89
+ const size_t M,
90
+ // number of bits per quantization index
91
+ const size_t nbits,
92
+ // precomputed distances, layout (M, ksub)
93
+ const float* sim_table,
94
+ // the code
95
+ const uint8_t* code) {
96
+ return distance_single_code_sve<PQDecoderT>(M, nbits, sim_table, code);
97
+ }
98
+
99
+ template <typename PQDecoderT>
100
+ inline void distance_four_codes(
101
+ // the product quantizer
102
+ const size_t M,
103
+ // number of bits per quantization index
104
+ const size_t nbits,
105
+ // precomputed distances, layout (M, ksub)
106
+ const float* sim_table,
107
+ // codes
108
+ const uint8_t* __restrict code0,
109
+ const uint8_t* __restrict code1,
110
+ const uint8_t* __restrict code2,
111
+ const uint8_t* __restrict code3,
112
+ // computed distances
113
+ float& result0,
114
+ float& result1,
115
+ float& result2,
116
+ float& result3) {
117
+ distance_four_codes_sve<PQDecoderT>(
118
+ M,
119
+ nbits,
120
+ sim_table,
121
+ code0,
122
+ code1,
123
+ code2,
124
+ code3,
125
+ result0,
126
+ result1,
127
+ result2,
128
+ result3);
129
+ }
130
+
131
+ } // namespace faiss
132
+
80
133
  #else
81
134
 
82
135
  #include <faiss/impl/code_distance/code_distance-generic.h>
@@ -1,12 +1,11 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- // -*- c++ -*-
9
-
8
+ #include <faiss/impl/index_read_utils.h>
10
9
  #include <faiss/index_io.h>
11
10
 
12
11
  #include <faiss/impl/io_macros.h>
@@ -14,9 +13,6 @@
14
13
  #include <cstdio>
15
14
  #include <cstdlib>
16
15
 
17
- #include <sys/stat.h>
18
- #include <sys/types.h>
19
-
20
16
  #include <faiss/impl/FaissAssert.h>
21
17
  #include <faiss/impl/io.h>
22
18
  #include <faiss/impl/io_macros.h>
@@ -63,7 +59,7 @@ namespace faiss {
63
59
  * Read
64
60
  **************************************************************/
65
61
 
66
- static void read_index_header(Index* idx, IOReader* f) {
62
+ void read_index_header(Index* idx, IOReader* f) {
67
63
  READ1(idx->d);
68
64
  READ1(idx->ntotal);
69
65
  idx_t dummy;
@@ -232,7 +228,7 @@ InvertedLists* read_InvertedLists(IOReader* f, int io_flags) {
232
228
  }
233
229
  }
234
230
 
235
- static void read_InvertedLists(IndexIVF* ivf, IOReader* f, int io_flags) {
231
+ void read_InvertedLists(IndexIVF* ivf, IOReader* f, int io_flags) {
236
232
  InvertedLists* ils = read_InvertedLists(f, io_flags);
237
233
  if (ils) {
238
234
  FAISS_THROW_IF_NOT(ils->nlist == ivf->nlist);
@@ -244,7 +240,7 @@ static void read_InvertedLists(IndexIVF* ivf, IOReader* f, int io_flags) {
244
240
  ivf->own_invlists = true;
245
241
  }
246
242
 
247
- static void read_ProductQuantizer(ProductQuantizer* pq, IOReader* f) {
243
+ void read_ProductQuantizer(ProductQuantizer* pq, IOReader* f) {
248
244
  READ1(pq->d);
249
245
  READ1(pq->M);
250
246
  READ1(pq->nbits);
@@ -354,7 +350,7 @@ static void read_ProductLocalSearchQuantizer(
354
350
  }
355
351
  }
356
352
 
357
- static void read_ScalarQuantizer(ScalarQuantizer* ivsc, IOReader* f) {
353
+ void read_ScalarQuantizer(ScalarQuantizer* ivsc, IOReader* f) {
358
354
  READ1(ivsc->qtype);
359
355
  READ1(ivsc->rangestat);
360
356
  READ1(ivsc->rangestat_arg);
@@ -375,7 +371,10 @@ static void read_HNSW(HNSW* hnsw, IOReader* f) {
375
371
  READ1(hnsw->max_level);
376
372
  READ1(hnsw->efConstruction);
377
373
  READ1(hnsw->efSearch);
378
- READ1(hnsw->upper_beam);
374
+
375
+ // // deprecated field
376
+ // READ1(hnsw->upper_beam);
377
+ READ1_DUMMY(int)
379
378
  }
380
379
 
381
380
  static void read_NSG(NSG* nsg, IOReader* f) {
@@ -440,7 +439,7 @@ ProductQuantizer* read_ProductQuantizer(IOReader* reader) {
440
439
  return pq;
441
440
  }
442
441
 
443
- static void read_direct_map(DirectMap* dm, IOReader* f) {
442
+ void read_direct_map(DirectMap* dm, IOReader* f) {
444
443
  char maintain_direct_map;
445
444
  READ1(maintain_direct_map);
446
445
  dm->type = (DirectMap::Type)maintain_direct_map;
@@ -456,10 +455,10 @@ static void read_direct_map(DirectMap* dm, IOReader* f) {
456
455
  }
457
456
  }
458
457
 
459
- static void read_ivf_header(
458
+ void read_ivf_header(
460
459
  IndexIVF* ivf,
461
460
  IOReader* f,
462
- std::vector<std::vector<idx_t>>* ids = nullptr) {
461
+ std::vector<std::vector<idx_t>>* ids) {
463
462
  read_index_header(ivf, f);
464
463
  READ1(ivf->nlist);
465
464
  READ1(ivf->nprobe);
@@ -474,7 +473,7 @@ static void read_ivf_header(
474
473
  }
475
474
 
476
475
  // used for legacy formats
477
- static ArrayInvertedLists* set_array_invlist(
476
+ ArrayInvertedLists* set_array_invlist(
478
477
  IndexIVF* ivf,
479
478
  std::vector<std::vector<idx_t>>& ids) {
480
479
  ArrayInvertedLists* ail =
@@ -531,7 +530,11 @@ Index* read_index(IOReader* f, int io_flags) {
531
530
  Index* idx = nullptr;
532
531
  uint32_t h;
533
532
  READ1(h);
534
- if (h == fourcc("IxFI") || h == fourcc("IxF2") || h == fourcc("IxFl")) {
533
+ if (h == fourcc("null")) {
534
+ // denotes a missing index, useful for some cases
535
+ return nullptr;
536
+ } else if (
537
+ h == fourcc("IxFI") || h == fourcc("IxF2") || h == fourcc("IxFl")) {
535
538
  IndexFlat* idxf;
536
539
  if (h == fourcc("IxFI")) {
537
540
  idxf = new IndexFlatIP();
@@ -948,7 +951,7 @@ Index* read_index(IOReader* f, int io_flags) {
948
951
  idx = idxp;
949
952
  } else if (
950
953
  h == fourcc("IHNf") || h == fourcc("IHNp") || h == fourcc("IHNs") ||
951
- h == fourcc("IHN2")) {
954
+ h == fourcc("IHN2") || h == fourcc("IHNc")) {
952
955
  IndexHNSW* idxhnsw = nullptr;
953
956
  if (h == fourcc("IHNf"))
954
957
  idxhnsw = new IndexHNSWFlat();
@@ -958,11 +961,19 @@ Index* read_index(IOReader* f, int io_flags) {
958
961
  idxhnsw = new IndexHNSWSQ();
959
962
  if (h == fourcc("IHN2"))
960
963
  idxhnsw = new IndexHNSW2Level();
964
+ if (h == fourcc("IHNc"))
965
+ idxhnsw = new IndexHNSWCagra();
961
966
  read_index_header(idxhnsw, f);
967
+ if (h == fourcc("IHNc")) {
968
+ READ1(idxhnsw->keep_max_size_level0);
969
+ auto idx_hnsw_cagra = dynamic_cast<IndexHNSWCagra*>(idxhnsw);
970
+ READ1(idx_hnsw_cagra->base_level_only);
971
+ READ1(idx_hnsw_cagra->num_base_level_search_entrypoints);
972
+ }
962
973
  read_HNSW(&idxhnsw->hnsw, f);
963
974
  idxhnsw->storage = read_index(f, io_flags);
964
- idxhnsw->own_fields = true;
965
- if (h == fourcc("IHNp")) {
975
+ idxhnsw->own_fields = idxhnsw->storage != nullptr;
976
+ if (h == fourcc("IHNp") && !(io_flags & IO_FLAG_PQ_SKIP_SDC_TABLE)) {
966
977
  dynamic_cast<IndexPQ*>(idxhnsw->storage)->pq.compute_sdc_table();
967
978
  }
968
979
  idx = idxhnsw;
@@ -0,0 +1,37 @@
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
+ // Utils for index_read
9
+
10
+ #ifndef FAISS_INDEX_READ_UTILS_H
11
+ #define FAISS_INDEX_READ_UTILS_H
12
+
13
+ #include <faiss/IndexIVF.h>
14
+ #include <faiss/impl/io.h>
15
+
16
+ #pragma once
17
+
18
+ namespace faiss {
19
+ struct ProductQuantizer;
20
+ struct ScalarQuantizer;
21
+
22
+ void read_index_header(Index* idx, IOReader* f);
23
+ void read_direct_map(DirectMap* dm, IOReader* f);
24
+ void read_ivf_header(
25
+ IndexIVF* ivf,
26
+ IOReader* f,
27
+ std::vector<std::vector<idx_t>>* ids = nullptr);
28
+ void read_InvertedLists(IndexIVF* ivf, IOReader* f, int io_flags);
29
+ ArrayInvertedLists* set_array_invlist(
30
+ IndexIVF* ivf,
31
+ std::vector<std::vector<idx_t>>& ids);
32
+ void read_ProductQuantizer(ProductQuantizer* pq, IOReader* f);
33
+ void read_ScalarQuantizer(ScalarQuantizer* ivsc, IOReader* f);
34
+
35
+ } // namespace faiss
36
+
37
+ #endif