faiss 0.3.2 → 0.3.4

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 (295) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE.txt +1 -1
  4. data/ext/faiss/extconf.rb +1 -1
  5. data/ext/faiss/index.cpp +10 -14
  6. data/ext/faiss/numo.hpp +957 -833
  7. data/lib/faiss/version.rb +1 -1
  8. data/vendor/faiss/faiss/AutoTune.cpp +2 -2
  9. data/vendor/faiss/faiss/AutoTune.h +2 -2
  10. data/vendor/faiss/faiss/Clustering.cpp +2 -2
  11. data/vendor/faiss/faiss/Clustering.h +2 -2
  12. data/vendor/faiss/faiss/IVFlib.cpp +2 -2
  13. data/vendor/faiss/faiss/IVFlib.h +2 -2
  14. data/vendor/faiss/faiss/Index.cpp +6 -2
  15. data/vendor/faiss/faiss/Index.h +10 -3
  16. data/vendor/faiss/faiss/Index2Layer.cpp +2 -2
  17. data/vendor/faiss/faiss/Index2Layer.h +2 -2
  18. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +7 -7
  19. data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +2 -2
  20. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +14 -16
  21. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.h +2 -2
  22. data/vendor/faiss/faiss/IndexBinary.cpp +13 -2
  23. data/vendor/faiss/faiss/IndexBinary.h +8 -2
  24. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +2 -3
  25. data/vendor/faiss/faiss/IndexBinaryFlat.h +2 -2
  26. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +2 -2
  27. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +2 -2
  28. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +2 -7
  29. data/vendor/faiss/faiss/IndexBinaryHNSW.h +2 -2
  30. data/vendor/faiss/faiss/IndexBinaryHash.cpp +2 -3
  31. data/vendor/faiss/faiss/IndexBinaryHash.h +2 -2
  32. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +2 -2
  33. data/vendor/faiss/faiss/IndexBinaryIVF.h +2 -2
  34. data/vendor/faiss/faiss/IndexFastScan.cpp +10 -14
  35. data/vendor/faiss/faiss/IndexFastScan.h +11 -2
  36. data/vendor/faiss/faiss/IndexFlat.cpp +2 -3
  37. data/vendor/faiss/faiss/IndexFlat.h +2 -2
  38. data/vendor/faiss/faiss/IndexFlatCodes.cpp +11 -2
  39. data/vendor/faiss/faiss/IndexFlatCodes.h +5 -2
  40. data/vendor/faiss/faiss/IndexHNSW.cpp +13 -6
  41. data/vendor/faiss/faiss/IndexHNSW.h +2 -2
  42. data/vendor/faiss/faiss/IndexIDMap.cpp +19 -3
  43. data/vendor/faiss/faiss/IndexIDMap.h +5 -2
  44. data/vendor/faiss/faiss/IndexIVF.cpp +2 -3
  45. data/vendor/faiss/faiss/IndexIVF.h +5 -4
  46. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +6 -7
  47. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +2 -2
  48. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +3 -14
  49. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +2 -4
  50. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +71 -34
  51. data/vendor/faiss/faiss/IndexIVFFastScan.h +19 -2
  52. data/vendor/faiss/faiss/IndexIVFFlat.cpp +2 -2
  53. data/vendor/faiss/faiss/IndexIVFFlat.h +2 -2
  54. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +2 -2
  55. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +2 -2
  56. data/vendor/faiss/faiss/IndexIVFPQ.cpp +2 -3
  57. data/vendor/faiss/faiss/IndexIVFPQ.h +2 -2
  58. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +7 -33
  59. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +2 -4
  60. data/vendor/faiss/faiss/IndexIVFPQR.cpp +2 -2
  61. data/vendor/faiss/faiss/IndexIVFPQR.h +2 -2
  62. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +2 -3
  63. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +2 -2
  64. data/vendor/faiss/faiss/IndexLSH.cpp +2 -3
  65. data/vendor/faiss/faiss/IndexLSH.h +2 -2
  66. data/vendor/faiss/faiss/IndexLattice.cpp +2 -2
  67. data/vendor/faiss/faiss/IndexLattice.h +2 -2
  68. data/vendor/faiss/faiss/IndexNNDescent.cpp +2 -2
  69. data/vendor/faiss/faiss/IndexNNDescent.h +2 -2
  70. data/vendor/faiss/faiss/IndexNSG.cpp +2 -5
  71. data/vendor/faiss/faiss/IndexNSG.h +2 -2
  72. data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +2 -2
  73. data/vendor/faiss/faiss/IndexNeuralNetCodec.h +2 -2
  74. data/vendor/faiss/faiss/IndexPQ.cpp +26 -26
  75. data/vendor/faiss/faiss/IndexPQ.h +2 -2
  76. data/vendor/faiss/faiss/IndexPQFastScan.cpp +2 -5
  77. data/vendor/faiss/faiss/IndexPQFastScan.h +2 -11
  78. data/vendor/faiss/faiss/IndexPreTransform.cpp +2 -2
  79. data/vendor/faiss/faiss/IndexPreTransform.h +2 -2
  80. data/vendor/faiss/faiss/IndexRefine.cpp +41 -4
  81. data/vendor/faiss/faiss/IndexRefine.h +9 -2
  82. data/vendor/faiss/faiss/IndexReplicas.cpp +2 -2
  83. data/vendor/faiss/faiss/IndexReplicas.h +2 -2
  84. data/vendor/faiss/faiss/IndexRowwiseMinMax.cpp +2 -2
  85. data/vendor/faiss/faiss/IndexRowwiseMinMax.h +2 -2
  86. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +2 -3
  87. data/vendor/faiss/faiss/IndexScalarQuantizer.h +2 -2
  88. data/vendor/faiss/faiss/IndexShards.cpp +2 -2
  89. data/vendor/faiss/faiss/IndexShards.h +2 -2
  90. data/vendor/faiss/faiss/IndexShardsIVF.cpp +2 -2
  91. data/vendor/faiss/faiss/IndexShardsIVF.h +2 -2
  92. data/vendor/faiss/faiss/MatrixStats.cpp +2 -2
  93. data/vendor/faiss/faiss/MatrixStats.h +2 -2
  94. data/vendor/faiss/faiss/MetaIndexes.cpp +2 -3
  95. data/vendor/faiss/faiss/MetaIndexes.h +2 -2
  96. data/vendor/faiss/faiss/MetricType.h +2 -2
  97. data/vendor/faiss/faiss/VectorTransform.cpp +2 -2
  98. data/vendor/faiss/faiss/VectorTransform.h +2 -2
  99. data/vendor/faiss/faiss/clone_index.cpp +2 -2
  100. data/vendor/faiss/faiss/clone_index.h +2 -2
  101. data/vendor/faiss/faiss/cppcontrib/SaDecodeKernels.h +2 -2
  102. data/vendor/faiss/faiss/cppcontrib/detail/CoarseBitType.h +2 -2
  103. data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +2 -2
  104. data/vendor/faiss/faiss/cppcontrib/factory_tools.cpp +44 -4
  105. data/vendor/faiss/faiss/cppcontrib/factory_tools.h +7 -2
  106. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-avx2-inl.h +2 -2
  107. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +2 -2
  108. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-neon-inl.h +2 -2
  109. data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMax-inl.h +2 -2
  110. data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMaxFP16-inl.h +2 -2
  111. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-avx2-inl.h +2 -2
  112. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-inl.h +2 -2
  113. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-neon-inl.h +2 -2
  114. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +2 -5
  115. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +2 -2
  116. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +13 -13
  117. data/vendor/faiss/faiss/gpu/GpuCloner.h +2 -2
  118. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +6 -6
  119. data/vendor/faiss/faiss/gpu/GpuDistance.h +11 -7
  120. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +2 -2
  121. data/vendor/faiss/faiss/gpu/GpuIcmEncoder.h +2 -2
  122. data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -7
  123. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +2 -2
  124. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +6 -3
  125. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +2 -2
  126. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +2 -2
  127. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +2 -2
  128. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +3 -3
  129. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +2 -2
  130. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +2 -2
  131. data/vendor/faiss/faiss/gpu/GpuResources.cpp +7 -2
  132. data/vendor/faiss/faiss/gpu/GpuResources.h +11 -4
  133. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +51 -21
  134. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +15 -5
  135. data/vendor/faiss/faiss/gpu/impl/IndexUtils.h +2 -2
  136. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +2 -2
  137. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +2 -2
  138. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +2 -2
  139. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +2 -2
  140. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +2 -2
  141. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +2 -2
  142. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +2 -2
  143. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +2 -3
  144. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +2 -2
  145. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +2 -2
  146. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +2 -2
  147. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +54 -54
  148. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +80 -78
  149. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +51 -51
  150. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +2 -2
  151. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +3 -3
  152. data/vendor/faiss/faiss/gpu/test/TestGpuResidualQuantizer.cpp +70 -0
  153. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +74 -4
  154. data/vendor/faiss/faiss/gpu/test/TestUtils.h +2 -2
  155. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +2 -2
  156. data/vendor/faiss/faiss/gpu/utils/{RaftUtils.h → CuvsUtils.h} +12 -11
  157. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +2 -2
  158. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +2 -2
  159. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +2 -2
  160. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +2 -2
  161. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +2 -2
  162. data/vendor/faiss/faiss/gpu/utils/Timer.h +2 -2
  163. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +79 -11
  164. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +17 -5
  165. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -2
  166. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +2 -2
  167. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  168. data/vendor/faiss/faiss/impl/CodePacker.h +2 -2
  169. data/vendor/faiss/faiss/impl/DistanceComputer.h +2 -2
  170. data/vendor/faiss/faiss/impl/FaissAssert.h +2 -2
  171. data/vendor/faiss/faiss/impl/FaissException.cpp +2 -2
  172. data/vendor/faiss/faiss/impl/FaissException.h +2 -3
  173. data/vendor/faiss/faiss/impl/HNSW.cpp +24 -19
  174. data/vendor/faiss/faiss/impl/HNSW.h +12 -2
  175. data/vendor/faiss/faiss/impl/IDSelector.cpp +2 -2
  176. data/vendor/faiss/faiss/impl/IDSelector.h +2 -2
  177. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +2 -2
  178. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +2 -2
  179. data/vendor/faiss/faiss/impl/LookupTableScaler.h +2 -2
  180. data/vendor/faiss/faiss/impl/NNDescent.cpp +2 -2
  181. data/vendor/faiss/faiss/impl/NNDescent.h +2 -2
  182. data/vendor/faiss/faiss/impl/NSG.cpp +27 -21
  183. data/vendor/faiss/faiss/impl/NSG.h +20 -8
  184. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +2 -2
  185. data/vendor/faiss/faiss/impl/PolysemousTraining.h +2 -2
  186. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +2 -4
  187. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +2 -2
  188. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +2 -2
  189. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +2 -2
  190. data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -2
  191. data/vendor/faiss/faiss/impl/Quantizer.h +2 -2
  192. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +2 -36
  193. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +3 -13
  194. data/vendor/faiss/faiss/impl/ResultHandler.h +2 -2
  195. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +2 -2
  196. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +2 -2
  197. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +2 -2
  198. data/vendor/faiss/faiss/impl/ThreadedIndex.h +2 -2
  199. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +2 -2
  200. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx512.h +2 -2
  201. data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +2 -2
  202. data/vendor/faiss/faiss/impl/code_distance/code_distance-sve.h +440 -0
  203. data/vendor/faiss/faiss/impl/code_distance/code_distance.h +55 -2
  204. data/vendor/faiss/faiss/impl/index_read.cpp +2 -5
  205. data/vendor/faiss/faiss/impl/index_read_utils.h +2 -2
  206. data/vendor/faiss/faiss/impl/index_write.cpp +2 -6
  207. data/vendor/faiss/faiss/impl/io.cpp +2 -2
  208. data/vendor/faiss/faiss/impl/io.h +2 -2
  209. data/vendor/faiss/faiss/impl/io_macros.h +2 -9
  210. data/vendor/faiss/faiss/impl/kmeans1d.cpp +2 -3
  211. data/vendor/faiss/faiss/impl/kmeans1d.h +2 -2
  212. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +2 -3
  213. data/vendor/faiss/faiss/impl/lattice_Zn.h +2 -2
  214. data/vendor/faiss/faiss/impl/platform_macros.h +12 -2
  215. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +2 -2
  216. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +20 -2
  217. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +2 -2
  218. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +2 -2
  219. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +3 -3
  220. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +2 -2
  221. data/vendor/faiss/faiss/impl/simd_result_handlers.h +18 -18
  222. data/vendor/faiss/faiss/index_factory.cpp +20 -21
  223. data/vendor/faiss/faiss/index_factory.h +2 -2
  224. data/vendor/faiss/faiss/index_io.h +2 -2
  225. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +2 -2
  226. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +2 -2
  227. data/vendor/faiss/faiss/invlists/DirectMap.cpp +2 -2
  228. data/vendor/faiss/faiss/invlists/DirectMap.h +2 -2
  229. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +2 -2
  230. data/vendor/faiss/faiss/invlists/InvertedLists.h +2 -2
  231. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +2 -2
  232. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +2 -2
  233. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +2 -3
  234. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +2 -2
  235. data/vendor/faiss/faiss/python/python_callbacks.cpp +2 -2
  236. data/vendor/faiss/faiss/python/python_callbacks.h +2 -2
  237. data/vendor/faiss/faiss/utils/AlignedTable.h +5 -3
  238. data/vendor/faiss/faiss/utils/Heap.cpp +2 -2
  239. data/vendor/faiss/faiss/utils/Heap.h +2 -2
  240. data/vendor/faiss/faiss/utils/NeuralNet.cpp +11 -7
  241. data/vendor/faiss/faiss/utils/NeuralNet.h +2 -2
  242. data/vendor/faiss/faiss/utils/WorkerThread.cpp +2 -2
  243. data/vendor/faiss/faiss/utils/WorkerThread.h +2 -2
  244. data/vendor/faiss/faiss/utils/approx_topk/approx_topk.h +2 -2
  245. data/vendor/faiss/faiss/utils/approx_topk/avx2-inl.h +2 -2
  246. data/vendor/faiss/faiss/utils/approx_topk/generic.h +2 -2
  247. data/vendor/faiss/faiss/utils/approx_topk/mode.h +2 -2
  248. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +2 -2
  249. data/vendor/faiss/faiss/utils/bf16.h +2 -2
  250. data/vendor/faiss/faiss/utils/distances.cpp +191 -2
  251. data/vendor/faiss/faiss/utils/distances.h +3 -3
  252. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -2
  253. data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
  254. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
  255. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +2 -2
  256. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +2 -2
  257. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +2 -2
  258. data/vendor/faiss/faiss/utils/distances_simd.cpp +502 -3
  259. data/vendor/faiss/faiss/utils/extra_distances-inl.h +2 -2
  260. data/vendor/faiss/faiss/utils/extra_distances.cpp +2 -3
  261. data/vendor/faiss/faiss/utils/extra_distances.h +2 -2
  262. data/vendor/faiss/faiss/utils/fp16-arm.h +2 -2
  263. data/vendor/faiss/faiss/utils/fp16-fp16c.h +2 -2
  264. data/vendor/faiss/faiss/utils/fp16-inl.h +2 -2
  265. data/vendor/faiss/faiss/utils/fp16.h +2 -2
  266. data/vendor/faiss/faiss/utils/hamming-inl.h +2 -2
  267. data/vendor/faiss/faiss/utils/hamming.cpp +2 -3
  268. data/vendor/faiss/faiss/utils/hamming.h +2 -2
  269. data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +2 -2
  270. data/vendor/faiss/faiss/utils/hamming_distance/avx512-inl.h +490 -0
  271. data/vendor/faiss/faiss/utils/hamming_distance/common.h +2 -2
  272. data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +2 -2
  273. data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +5 -2
  274. data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +5 -5
  275. data/vendor/faiss/faiss/utils/ordered_key_value.h +2 -2
  276. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  277. data/vendor/faiss/faiss/utils/partitioning.h +2 -2
  278. data/vendor/faiss/faiss/utils/prefetch.h +2 -2
  279. data/vendor/faiss/faiss/utils/quantize_lut.cpp +2 -2
  280. data/vendor/faiss/faiss/utils/quantize_lut.h +2 -2
  281. data/vendor/faiss/faiss/utils/random.cpp +2 -2
  282. data/vendor/faiss/faiss/utils/random.h +2 -2
  283. data/vendor/faiss/faiss/utils/simdlib.h +2 -2
  284. data/vendor/faiss/faiss/utils/simdlib_avx2.h +2 -2
  285. data/vendor/faiss/faiss/utils/simdlib_avx512.h +2 -2
  286. data/vendor/faiss/faiss/utils/simdlib_emulated.h +2 -2
  287. data/vendor/faiss/faiss/utils/simdlib_neon.h +2 -2
  288. data/vendor/faiss/faiss/utils/simdlib_ppc64.h +2 -2
  289. data/vendor/faiss/faiss/utils/sorting.cpp +2 -2
  290. data/vendor/faiss/faiss/utils/sorting.h +2 -2
  291. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +2 -2
  292. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +2 -2
  293. data/vendor/faiss/faiss/utils/utils.cpp +7 -7
  294. data/vendor/faiss/faiss/utils/utils.h +4 -3
  295. metadata +9 -10
@@ -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.
@@ -24,7 +24,6 @@
24
24
  #include <faiss/utils/Heap.h>
25
25
  #include <faiss/utils/distances.h>
26
26
  #include <faiss/utils/hamming.h>
27
- #include <faiss/utils/utils.h>
28
27
 
29
28
  extern "C" {
30
29
 
@@ -152,6 +151,40 @@ void AdditiveQuantizer::train_norm(size_t n, const float* norms) {
152
151
  }
153
152
  }
154
153
 
154
+ void AdditiveQuantizer::compute_codebook_tables() {
155
+ centroid_norms.resize(total_codebook_size);
156
+ fvec_norms_L2sqr(
157
+ centroid_norms.data(), codebooks.data(), d, total_codebook_size);
158
+ size_t cross_table_size = 0;
159
+ for (int m = 0; m < M; m++) {
160
+ size_t K = (size_t)1 << nbits[m];
161
+ cross_table_size += K * codebook_offsets[m];
162
+ }
163
+ codebook_cross_products.resize(cross_table_size);
164
+ size_t ofs = 0;
165
+ for (int m = 1; m < M; m++) {
166
+ FINTEGER ki = (size_t)1 << nbits[m];
167
+ FINTEGER kk = codebook_offsets[m];
168
+ FINTEGER di = d;
169
+ float zero = 0, one = 1;
170
+ assert(ofs + ki * kk <= cross_table_size);
171
+ sgemm_("Transposed",
172
+ "Not transposed",
173
+ &ki,
174
+ &kk,
175
+ &di,
176
+ &one,
177
+ codebooks.data() + d * kk,
178
+ &di,
179
+ codebooks.data(),
180
+ &di,
181
+ &zero,
182
+ codebook_cross_products.data() + ofs,
183
+ &ki);
184
+ ofs += ki * kk;
185
+ }
186
+ }
187
+
155
188
  namespace {
156
189
 
157
190
  // TODO
@@ -471,7 +504,6 @@ namespace {
471
504
  float accumulate_IPs(
472
505
  const AdditiveQuantizer& aq,
473
506
  BitstringReader& bs,
474
- const uint8_t* codes,
475
507
  const float* LUT) {
476
508
  float accu = 0;
477
509
  for (int m = 0; m < aq.M; m++) {
@@ -483,6 +515,29 @@ float accumulate_IPs(
483
515
  return accu;
484
516
  }
485
517
 
518
+ float compute_norm_from_LUT(const AdditiveQuantizer& aq, BitstringReader& bs) {
519
+ float accu = 0;
520
+ std::vector<int> idx(aq.M);
521
+ const float* c = aq.codebook_cross_products.data();
522
+ for (int m = 0; m < aq.M; m++) {
523
+ size_t nbit = aq.nbits[m];
524
+ int i = bs.read(nbit);
525
+ size_t K = 1 << nbit;
526
+ idx[m] = i;
527
+
528
+ accu += aq.centroid_norms[aq.codebook_offsets[m] + i];
529
+
530
+ for (int l = 0; l < m; l++) {
531
+ int j = idx[l];
532
+ accu += 2 * c[j * K + i];
533
+ c += (1 << aq.nbits[l]) * K;
534
+ }
535
+ }
536
+ // FAISS_THROW_IF_NOT(c == aq.codebook_cross_products.data() +
537
+ // aq.codebook_cross_products.size());
538
+ return accu;
539
+ }
540
+
486
541
  } // anonymous namespace
487
542
 
488
543
  template <>
@@ -491,7 +546,7 @@ float AdditiveQuantizer::
491
546
  const uint8_t* codes,
492
547
  const float* LUT) const {
493
548
  BitstringReader bs(codes, code_size);
494
- return accumulate_IPs(*this, bs, codes, LUT);
549
+ return accumulate_IPs(*this, bs, LUT);
495
550
  }
496
551
 
497
552
  template <>
@@ -500,7 +555,7 @@ float AdditiveQuantizer::
500
555
  const uint8_t* codes,
501
556
  const float* LUT) const {
502
557
  BitstringReader bs(codes, code_size);
503
- return -accumulate_IPs(*this, bs, codes, LUT);
558
+ return -accumulate_IPs(*this, bs, LUT);
504
559
  }
505
560
 
506
561
  template <>
@@ -509,7 +564,7 @@ float AdditiveQuantizer::
509
564
  const uint8_t* codes,
510
565
  const float* LUT) const {
511
566
  BitstringReader bs(codes, code_size);
512
- float accu = accumulate_IPs(*this, bs, codes, LUT);
567
+ float accu = accumulate_IPs(*this, bs, LUT);
513
568
  uint32_t norm_i = bs.read(32);
514
569
  float norm2;
515
570
  memcpy(&norm2, &norm_i, 4);
@@ -522,7 +577,7 @@ float AdditiveQuantizer::
522
577
  const uint8_t* codes,
523
578
  const float* LUT) const {
524
579
  BitstringReader bs(codes, code_size);
525
- float accu = accumulate_IPs(*this, bs, codes, LUT);
580
+ float accu = accumulate_IPs(*this, bs, LUT);
526
581
  uint32_t norm_i = bs.read(8);
527
582
  float norm2 = decode_qcint(norm_i);
528
583
  return norm2 - 2 * accu;
@@ -534,7 +589,7 @@ float AdditiveQuantizer::
534
589
  const uint8_t* codes,
535
590
  const float* LUT) const {
536
591
  BitstringReader bs(codes, code_size);
537
- float accu = accumulate_IPs(*this, bs, codes, LUT);
592
+ float accu = accumulate_IPs(*this, bs, LUT);
538
593
  uint32_t norm_i = bs.read(4);
539
594
  float norm2 = decode_qcint(norm_i);
540
595
  return norm2 - 2 * accu;
@@ -546,7 +601,7 @@ float AdditiveQuantizer::
546
601
  const uint8_t* codes,
547
602
  const float* LUT) const {
548
603
  BitstringReader bs(codes, code_size);
549
- float accu = accumulate_IPs(*this, bs, codes, LUT);
604
+ float accu = accumulate_IPs(*this, bs, LUT);
550
605
  uint32_t norm_i = bs.read(8);
551
606
  float norm2 = decode_qint8(norm_i, norm_min, norm_max);
552
607
  return norm2 - 2 * accu;
@@ -558,10 +613,23 @@ float AdditiveQuantizer::
558
613
  const uint8_t* codes,
559
614
  const float* LUT) const {
560
615
  BitstringReader bs(codes, code_size);
561
- float accu = accumulate_IPs(*this, bs, codes, LUT);
616
+ float accu = accumulate_IPs(*this, bs, LUT);
562
617
  uint32_t norm_i = bs.read(4);
563
618
  float norm2 = decode_qint4(norm_i, norm_min, norm_max);
564
619
  return norm2 - 2 * accu;
565
620
  }
566
621
 
622
+ template <>
623
+ float AdditiveQuantizer::
624
+ compute_1_distance_LUT<false, AdditiveQuantizer::ST_norm_from_LUT>(
625
+ const uint8_t* codes,
626
+ const float* LUT) const {
627
+ FAISS_THROW_IF_NOT(codebook_cross_products.size() > 0);
628
+ BitstringReader bs(codes, code_size);
629
+ float accu = accumulate_IPs(*this, bs, LUT);
630
+ BitstringReader bs2(codes, code_size);
631
+ float norm2 = compute_norm_from_LUT(*this, bs2);
632
+ return norm2 - 2 * accu;
633
+ }
634
+
567
635
  } // namespace faiss
@@ -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.
@@ -29,6 +29,8 @@ struct AdditiveQuantizer : Quantizer {
29
29
  std::vector<float> codebooks; ///< codebooks
30
30
 
31
31
  // derived values
32
+ /// codebook #1 is stored in rows codebook_offsets[i]:codebook_offsets[i+1]
33
+ /// in the codebooks table of size total_codebook_size by d
32
34
  std::vector<uint64_t> codebook_offsets;
33
35
  size_t tot_bits = 0; ///< total number of bits (indexes + norms)
34
36
  size_t norm_bits = 0; ///< bits allocated for the norms
@@ -38,9 +40,19 @@ struct AdditiveQuantizer : Quantizer {
38
40
  bool verbose = false; ///< verbose during training?
39
41
  bool is_trained = false; ///< is trained or not
40
42
 
41
- IndexFlat1D qnorm; ///< store and search norms
42
- std::vector<float> norm_tabs; ///< store norms of codebook entries for 4-bit
43
- ///< fastscan search
43
+ /// auxiliary data for ST_norm_lsq2x4 and ST_norm_rq2x4
44
+ /// store norms of codebook entries for 4-bit fastscan
45
+ std::vector<float> norm_tabs;
46
+ IndexFlat1D qnorm; ///< store and search norms
47
+
48
+ void compute_codebook_tables();
49
+
50
+ /// norms of all codebook entries (size total_codebook_size)
51
+ std::vector<float> centroid_norms;
52
+
53
+ /// dot products of all codebook entries with the previous codebooks
54
+ /// size sum(codebook_offsets[m] * 2^nbits[m], m=0..M-1)
55
+ std::vector<float> codebook_cross_products;
44
56
 
45
57
  /// norms and distance matrixes with beam search can get large, so use this
46
58
  /// to control for the amount of memory that can be allocated
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -1,6 +1,5 @@
1
-
2
- /**
3
- * Copyright (c) Facebook, Inc. and its affiliates.
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
3
  *
5
4
  * This source code is licensed under the MIT license found in the
6
5
  * LICENSE file in the root directory of this source tree.
@@ -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.
@@ -166,10 +166,10 @@ void HNSW::print_neighbor_stats(int level) const {
166
166
  }
167
167
 
168
168
  void HNSW::fill_with_random_links(size_t n) {
169
- int max_level = prepare_level_tab(n);
169
+ int max_level_2 = prepare_level_tab(n);
170
170
  RandomGenerator rng2(456);
171
171
 
172
- for (int level = max_level - 1; level >= 0; --level) {
172
+ for (int level = max_level_2 - 1; level >= 0; --level) {
173
173
  std::vector<int> elts;
174
174
  for (int i = 0; i < n; i++) {
175
175
  if (levels[i] > level) {
@@ -210,16 +210,16 @@ int HNSW::prepare_level_tab(size_t n, bool preset_levels) {
210
210
  }
211
211
  }
212
212
 
213
- int max_level = 0;
213
+ int max_level_2 = 0;
214
214
  for (int i = 0; i < n; i++) {
215
215
  int pt_level = levels[i + n0] - 1;
216
- if (pt_level > max_level)
217
- max_level = pt_level;
216
+ if (pt_level > max_level_2)
217
+ max_level_2 = pt_level;
218
218
  offsets.push_back(offsets.back() + cum_nb_neighbors(pt_level + 1));
219
219
  }
220
220
  neighbors.resize(offsets.back(), -1);
221
221
 
222
- return max_level;
222
+ return max_level_2;
223
223
  }
224
224
 
225
225
  /** Enumerate vertices from nearest to farthest from query, keep a
@@ -351,6 +351,8 @@ void add_link(
351
351
  }
352
352
  }
353
353
 
354
+ } // namespace
355
+
354
356
  /// search neighbors on a single level, starting from an entry point
355
357
  void search_neighbors_to_add(
356
358
  HNSW& hnsw,
@@ -359,10 +361,8 @@ void search_neighbors_to_add(
359
361
  int entry_point,
360
362
  float d_entry_point,
361
363
  int level,
362
- VisitedTable& vt) {
363
- // selects a version
364
- const bool reference_version = false;
365
-
364
+ VisitedTable& vt,
365
+ bool reference_version) {
366
366
  // top is nearest candidate
367
367
  std::priority_queue<NodeDistFarther> candidates;
368
368
 
@@ -385,7 +385,14 @@ void search_neighbors_to_add(
385
385
  size_t begin, end;
386
386
  hnsw.neighbor_range(currNode, level, &begin, &end);
387
387
 
388
- // select a version, based on a flag
388
+ // The reference version is not used, but kept here because:
389
+ // 1. It is easier to switch back if the optimized version has a problem
390
+ // 2. It serves as a starting point for new optimizations
391
+ // 3. It helps understand the code
392
+ // 4. It ensures the reference version is still compilable if the
393
+ // optimized version changes
394
+ // The reference and the optimized versions' results are compared in
395
+ // test_hnsw.cpp
389
396
  if (reference_version) {
390
397
  // a reference version
391
398
  for (size_t i = begin; i < end; i++) {
@@ -470,8 +477,6 @@ void search_neighbors_to_add(
470
477
  vt.advance();
471
478
  }
472
479
 
473
- } // namespace
474
-
475
480
  /// Finds neighbors and builds links with them, starting from an entry
476
481
  /// point. The own neighbor list is assumed to be locked.
477
482
  void HNSW::add_links_starting_from(
@@ -493,17 +498,17 @@ void HNSW::add_links_starting_from(
493
498
 
494
499
  ::faiss::shrink_neighbor_list(ptdis, link_targets, M, keep_max_size_level0);
495
500
 
496
- std::vector<storage_idx_t> neighbors;
497
- neighbors.reserve(link_targets.size());
501
+ std::vector<storage_idx_t> neighbors_to_add;
502
+ neighbors_to_add.reserve(link_targets.size());
498
503
  while (!link_targets.empty()) {
499
504
  storage_idx_t other_id = link_targets.top().id;
500
505
  add_link(*this, ptdis, pt_id, other_id, level, keep_max_size_level0);
501
- neighbors.push_back(other_id);
506
+ neighbors_to_add.push_back(other_id);
502
507
  link_targets.pop();
503
508
  }
504
509
 
505
510
  omp_unset_lock(&locks[pt_id]);
506
- for (storage_idx_t other_id : neighbors) {
511
+ for (storage_idx_t other_id : neighbors_to_add) {
507
512
  omp_set_lock(&locks[other_id]);
508
513
  add_link(*this, ptdis, other_id, pt_id, level, keep_max_size_level0);
509
514
  omp_unset_lock(&locks[other_id]);
@@ -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.
@@ -281,4 +281,14 @@ std::priority_queue<HNSW::Node> search_from_candidate_unbounded(
281
281
  VisitedTable* vt,
282
282
  HNSWStats& stats);
283
283
 
284
+ void search_neighbors_to_add(
285
+ HNSW& hnsw,
286
+ DistanceComputer& qdis,
287
+ std::priority_queue<HNSW::NodeDistCloser>& results,
288
+ int entry_point,
289
+ float d_entry_point,
290
+ int level,
291
+ VisitedTable& vt,
292
+ bool reference_version = false);
293
+
284
294
  } // namespace faiss
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -1,12 +1,10 @@
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
-
10
8
  #include <faiss/impl/NSG.h>
11
9
 
12
10
  #include <algorithm>
@@ -18,14 +16,16 @@
18
16
 
19
17
  namespace faiss {
20
18
 
21
- namespace nsg {
22
-
23
19
  namespace {
24
20
 
21
+ using LockGuard = std::lock_guard<std::mutex>;
22
+
25
23
  // It needs to be smaller than 0
26
24
  constexpr int EMPTY_ID = -1;
27
25
 
28
- } // namespace
26
+ } // anonymous namespace
27
+
28
+ namespace nsg {
29
29
 
30
30
  DistanceComputer* storage_distance_computer(const Index* storage) {
31
31
  if (is_similarity_metric(storage->metric_type)) {
@@ -35,14 +35,8 @@ DistanceComputer* storage_distance_computer(const Index* storage) {
35
35
  }
36
36
  }
37
37
 
38
- } // namespace nsg
39
-
40
- using namespace nsg;
41
-
42
- using LockGuard = std::lock_guard<std::mutex>;
43
-
44
38
  struct Neighbor {
45
- int id;
39
+ int32_t id;
46
40
  float distance;
47
41
  bool flag;
48
42
 
@@ -56,7 +50,7 @@ struct Neighbor {
56
50
  };
57
51
 
58
52
  struct Node {
59
- int id;
53
+ int32_t id;
60
54
  float distance;
61
55
 
62
56
  Node() = default;
@@ -65,6 +59,11 @@ struct Node {
65
59
  inline bool operator<(const Node& other) const {
66
60
  return distance < other.distance;
67
61
  }
62
+
63
+ // to keep the compiler happy
64
+ inline bool operator<(int other) const {
65
+ return id < other;
66
+ }
68
67
  };
69
68
 
70
69
  inline int insert_into_pool(Neighbor* addr, int K, Neighbor nn) {
@@ -106,6 +105,10 @@ inline int insert_into_pool(Neighbor* addr, int K, Neighbor nn) {
106
105
  return right;
107
106
  }
108
107
 
108
+ } // namespace nsg
109
+
110
+ using namespace nsg;
111
+
109
112
  NSG::NSG(int R) : R(R), rng(0x0903) {
110
113
  L = R + 32;
111
114
  C = R + 100;
@@ -253,9 +256,11 @@ void NSG::search_on_graph(
253
256
  std::vector<int> init_ids(pool_size);
254
257
 
255
258
  int num_ids = 0;
256
- for (int i = 0; i < init_ids.size() && i < graph.K; i++) {
257
- int id = (int)graph.at(ep, i);
258
- if (id < 0 || id >= ntotal) {
259
+ std::vector<index_t> neighbors(graph.K);
260
+ size_t nneigh = graph.get_neighbors(ep, neighbors.data());
261
+ for (int i = 0; i < init_ids.size() && i < nneigh; i++) {
262
+ int id = (int)neighbors[i];
263
+ if (id >= ntotal) {
259
264
  continue;
260
265
  }
261
266
 
@@ -296,9 +301,10 @@ void NSG::search_on_graph(
296
301
  retset[k].flag = false;
297
302
  int n = retset[k].id;
298
303
 
299
- for (int m = 0; m < graph.K; m++) {
300
- int id = (int)graph.at(n, m);
301
- if (id < 0 || id > ntotal || vt.get(id)) {
304
+ size_t nneigh_for_n = graph.get_neighbors(n, neighbors.data());
305
+ for (int m = 0; m < nneigh_for_n; m++) {
306
+ int id = neighbors[m];
307
+ if (id > ntotal || vt.get(id)) {
302
308
  continue;
303
309
  }
304
310
  vt.set(id);
@@ -1,12 +1,10 @@
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
-
10
8
  #pragma once
11
9
 
12
10
  #include <memory>
@@ -40,11 +38,12 @@ namespace faiss {
40
38
  */
41
39
 
42
40
  struct DistanceComputer; // from AuxIndexStructures
43
- struct Neighbor;
44
- struct Node;
45
41
 
46
42
  namespace nsg {
47
43
 
44
+ struct Neighbor;
45
+ struct Node;
46
+
48
47
  /***********************************************************
49
48
  * Graph structure to store a graph.
50
49
  *
@@ -75,7 +74,7 @@ struct Graph {
75
74
  }
76
75
 
77
76
  // release the allocated memory if needed
78
- ~Graph() {
77
+ virtual ~Graph() {
79
78
  if (own_fields) {
80
79
  delete[] data;
81
80
  }
@@ -90,6 +89,17 @@ struct Graph {
90
89
  inline node_t& at(int i, int j) {
91
90
  return data[i * K + j];
92
91
  }
92
+
93
+ // get all neighbors of node i (used during search only)
94
+ virtual size_t get_neighbors(int i, node_t* neighbors) const {
95
+ for (int j = 0; j < K; j++) {
96
+ if (data[i * K + j] < 0) {
97
+ return j;
98
+ }
99
+ neighbors[j] = data[i * K + j];
100
+ }
101
+ return K;
102
+ }
93
103
  };
94
104
 
95
105
  DistanceComputer* storage_distance_computer(const Index* storage);
@@ -99,6 +109,8 @@ DistanceComputer* storage_distance_computer(const Index* storage);
99
109
  struct NSG {
100
110
  /// internal storage of vectors (32 bits: this is expensive)
101
111
  using storage_idx_t = int32_t;
112
+ using Node = nsg::Node;
113
+ using Neighbor = nsg::Neighbor;
102
114
 
103
115
  int ntotal = 0; ///< nb of nodes
104
116
 
@@ -112,7 +124,7 @@ struct NSG {
112
124
 
113
125
  int enterpoint; ///< enterpoint
114
126
 
115
- std::shared_ptr<nsg::Graph<int>> final_graph; ///< NSG graph structure
127
+ std::shared_ptr<nsg::Graph<int32_t>> final_graph; ///< NSG graph structure
116
128
 
117
129
  bool is_built = false; ///< NSG is built or not
118
130
 
@@ -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.