faiss 0.6.3 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (255) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/ext/faiss/extconf.rb +1 -1
  4. data/lib/faiss/version.rb +1 -1
  5. data/vendor/faiss/faiss/AutoTune.cpp +175 -0
  6. data/vendor/faiss/faiss/Clustering.cpp +150 -60
  7. data/vendor/faiss/faiss/Clustering.h +37 -0
  8. data/vendor/faiss/faiss/IVFlib.cpp +99 -0
  9. data/vendor/faiss/faiss/IVFlib.h +15 -0
  10. data/vendor/faiss/faiss/Index.cpp +44 -0
  11. data/vendor/faiss/faiss/Index.h +18 -13
  12. data/vendor/faiss/faiss/IndexBinary.cpp +8 -0
  13. data/vendor/faiss/faiss/IndexBinary.h +8 -0
  14. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +23 -2
  15. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +94 -144
  16. data/vendor/faiss/faiss/IndexBinaryHNSW.h +18 -5
  17. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +40 -14
  18. data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +33 -17
  20. data/vendor/faiss/faiss/IndexFastScan.h +6 -3
  21. data/vendor/faiss/faiss/IndexFlat.cpp +64 -4
  22. data/vendor/faiss/faiss/IndexFlat.h +33 -2
  23. data/vendor/faiss/faiss/IndexFlatCodes.cpp +6 -1
  24. data/vendor/faiss/faiss/IndexHNSW.cpp +755 -99
  25. data/vendor/faiss/faiss/IndexHNSW.h +69 -7
  26. data/vendor/faiss/faiss/IndexIDMap.cpp +43 -6
  27. data/vendor/faiss/faiss/IndexIDMap.h +7 -0
  28. data/vendor/faiss/faiss/IndexIVF.cpp +297 -32
  29. data/vendor/faiss/faiss/IndexIVF.h +53 -1
  30. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +46 -14
  31. data/vendor/faiss/faiss/IndexIVFFastScan.h +4 -2
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +33 -0
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +16 -0
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +9 -1
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -0
  37. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +3 -20
  38. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +50 -14
  39. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -2
  40. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +855 -0
  41. data/vendor/faiss/faiss/IndexIVFSQFastScan.h +145 -0
  42. data/vendor/faiss/faiss/IndexNNDescent.cpp +0 -3
  43. data/vendor/faiss/faiss/IndexPiPNN.cpp +201 -0
  44. data/vendor/faiss/faiss/IndexPiPNN.h +83 -0
  45. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +6 -3
  46. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +10 -4
  47. data/vendor/faiss/faiss/IndexReplicas.cpp +20 -0
  48. data/vendor/faiss/faiss/IndexReplicas.h +9 -0
  49. data/vendor/faiss/faiss/IndexSQFastScan.cpp +648 -0
  50. data/vendor/faiss/faiss/IndexSQFastScan.h +129 -0
  51. data/vendor/faiss/faiss/IndexShards.cpp +13 -3
  52. data/vendor/faiss/faiss/IndexShards.h +5 -0
  53. data/vendor/faiss/faiss/IndexShardsIVF.cpp +4 -3
  54. data/vendor/faiss/faiss/SuperKMeans.cpp +87 -22
  55. data/vendor/faiss/faiss/SuperKMeans.h +12 -6
  56. data/vendor/faiss/faiss/VectorTransform.cpp +40 -1
  57. data/vendor/faiss/faiss/VectorTransform.h +3 -0
  58. data/vendor/faiss/faiss/clone_index.cpp +27 -0
  59. data/vendor/faiss/faiss/gpu/Dataset.h +112 -0
  60. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +16 -13
  61. data/vendor/faiss/faiss/gpu/GpuCloner.h +4 -0
  62. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +4 -5
  63. data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
  64. data/vendor/faiss/faiss/gpu/GpuIndex.h +8 -18
  65. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +26 -13
  66. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +109 -63
  67. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -0
  68. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +6 -0
  69. data/vendor/faiss/faiss/gpu/test/TestGpuDatasetHeader.cpp +18 -0
  70. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +215 -0
  71. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +98 -0
  72. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +11 -0
  73. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +1 -1
  74. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +11 -0
  75. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +92 -0
  76. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +4 -0
  77. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +69 -0
  78. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +38 -0
  79. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +106 -0
  80. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +10 -0
  81. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
  82. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -0
  83. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -6
  84. data/vendor/faiss/faiss/impl/ClusteringHelpers.cpp +134 -45
  85. data/vendor/faiss/faiss/impl/ClusteringHelpers.h +36 -0
  86. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +287 -134
  87. data/vendor/faiss/faiss/impl/DistanceComputer.cpp +63 -0
  88. data/vendor/faiss/faiss/impl/DistanceComputer.h +3 -29
  89. data/vendor/faiss/faiss/impl/HNSW.cpp +465 -125
  90. data/vendor/faiss/faiss/impl/HNSW.h +118 -7
  91. data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
  92. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +6 -19
  93. data/vendor/faiss/faiss/impl/NNDescent.cpp +12 -4
  94. data/vendor/faiss/faiss/impl/NSG.cpp +3 -2
  95. data/vendor/faiss/faiss/impl/Panorama.h +14 -4
  96. data/vendor/faiss/faiss/impl/PiPNN.cpp +822 -0
  97. data/vendor/faiss/faiss/impl/PiPNN.h +122 -0
  98. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +58 -15
  99. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +1 -1
  100. data/vendor/faiss/faiss/impl/RaBitQUtils.h +1 -1
  101. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +150 -60
  102. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +62 -4
  103. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +2 -2
  104. data/vendor/faiss/faiss/impl/ResultHandler.h +9 -3
  105. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +61 -30
  106. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +7 -16
  107. data/vendor/faiss/faiss/impl/approx_topk/avx2.cpp +0 -4
  108. data/vendor/faiss/faiss/impl/approx_topk/neon.cpp +0 -4
  109. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryHash_impl.h +5 -2
  110. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +89 -13
  111. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +0 -4
  112. data/vendor/faiss/faiss/impl/binary_hamming/avx512.cpp +0 -4
  113. data/vendor/faiss/faiss/impl/binary_hamming/neon.cpp +0 -4
  114. data/vendor/faiss/faiss/impl/binary_hamming/rvv.cpp +0 -4
  115. data/vendor/faiss/faiss/impl/expanded_scanners.h +93 -1
  116. data/vendor/faiss/faiss/impl/fast_scan/FastScanDistancePostProcessing.h +4 -0
  117. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops.h +61 -64
  118. data/vendor/faiss/faiss/impl/fast_scan/accumulate_loops_512.h +13 -14
  119. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +18 -129
  120. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +25 -19
  121. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +5 -0
  122. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +0 -4
  123. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +0 -4
  124. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +0 -88
  125. data/vendor/faiss/faiss/impl/fast_scan/impl-riscv.cpp +0 -4
  126. data/vendor/faiss/faiss/impl/fast_scan/kernels_neon.h +155 -0
  127. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +22 -1
  128. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +3 -2
  129. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +41 -23
  130. data/vendor/faiss/faiss/impl/hnsw/avx2.cpp +0 -4
  131. data/vendor/faiss/faiss/impl/hnsw/avx512.cpp +0 -4
  132. data/vendor/faiss/faiss/impl/index_read.cpp +302 -29
  133. data/vendor/faiss/faiss/impl/index_write.cpp +126 -26
  134. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +159 -0
  135. data/vendor/faiss/faiss/impl/pipnn/HashPrune.h +69 -0
  136. data/vendor/faiss/faiss/impl/pipnn/Partition.cpp +587 -0
  137. data/vendor/faiss/faiss/impl/pipnn/Partition.h +93 -0
  138. data/vendor/faiss/faiss/impl/pipnn/kernels.cpp +326 -0
  139. data/vendor/faiss/faiss/impl/pipnn/kernels.h +92 -0
  140. data/vendor/faiss/faiss/impl/platform_macros.h +45 -0
  141. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +0 -4
  142. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +152 -43
  143. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +6 -0
  144. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +0 -4
  145. data/vendor/faiss/faiss/impl/pq_code_distance/avx512.cpp +0 -4
  146. data/vendor/faiss/faiss/impl/pq_code_distance/neon.cpp +0 -4
  147. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx2.h +4 -0
  148. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-avx512.h +4 -1
  149. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +23 -4
  150. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.h +6 -6
  151. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-inl.h +131 -6
  152. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-sve.cpp +0 -4
  153. data/vendor/faiss/faiss/impl/pq_code_distance/pq_rvv_kernel.h +89 -0
  154. data/vendor/faiss/faiss/impl/pq_code_distance/pq_scan_impl.h +21 -10
  155. data/vendor/faiss/faiss/impl/pq_code_distance/rvv.cpp +45 -18
  156. data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +2 -1
  157. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +0 -4
  158. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +0 -4
  159. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +0 -4
  160. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +0 -4
  161. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +6 -1
  162. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +7 -3
  163. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +28 -52
  164. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +59 -11
  165. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +232 -79
  166. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +482 -103
  167. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +2 -149
  168. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +3 -698
  169. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +101 -72
  170. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +203 -99
  171. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-rvv.cpp +3185 -144
  172. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_coefficients.h +48 -0
  173. data/vendor/faiss/faiss/impl/scalar_quantizer/sq8_rvv_kernel.h +546 -0
  174. data/vendor/faiss/faiss/impl/scalar_quantizer/training.cpp +2 -2
  175. data/vendor/faiss/faiss/impl/simd_dispatch.h +47 -22
  176. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx2.h +14 -0
  177. data/vendor/faiss/faiss/impl/simdlib/simdlib_dispatch.h +7 -8
  178. data/vendor/faiss/faiss/impl/sq_fastscan_utils.h +232 -0
  179. data/vendor/faiss/faiss/index_factory.cpp +84 -1
  180. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +10 -5
  181. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +10 -4
  182. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +16 -4
  183. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +11 -3
  184. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
  185. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
  186. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +9 -2
  187. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
  188. data/vendor/faiss/faiss/utils/AlignedTable.h +6 -1
  189. data/vendor/faiss/faiss/utils/Heap.cpp +4 -3
  190. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +16 -5
  191. data/vendor/faiss/faiss/utils/bf16.h +36 -0
  192. data/vendor/faiss/faiss/utils/distances.cpp +246 -28
  193. data/vendor/faiss/faiss/utils/distances.h +34 -0
  194. data/vendor/faiss/faiss/utils/distances_dispatch.h +36 -28
  195. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +2 -5
  196. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -15
  197. data/vendor/faiss/faiss/utils/distances_fused/rvv.cpp +25 -0
  198. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +0 -4
  199. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based_neon.cpp +0 -4
  200. data/vendor/faiss/faiss/utils/distances_fused/simdlib_kernel-inl.h +2 -1
  201. data/vendor/faiss/faiss/utils/distances_simd.cpp +9 -0
  202. data/vendor/faiss/faiss/utils/fp16-fp16c.h +4 -0
  203. data/vendor/faiss/faiss/utils/fp16-inl.h +26 -52
  204. data/vendor/faiss/faiss/utils/hamming.cpp +15 -9
  205. data/vendor/faiss/faiss/utils/hamming.h +10 -1
  206. data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
  207. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx2.cpp +0 -4
  208. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512.cpp +0 -4
  209. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +20 -0
  210. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +34 -6
  211. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_vpopcnt.h +217 -0
  212. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-neon.h +0 -4
  213. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +143 -1
  214. data/vendor/faiss/faiss/utils/hamming_distance/hamming_neon.cpp +0 -4
  215. data/vendor/faiss/faiss/utils/hamming_distance/hamming_rvv.cpp +0 -4
  216. data/vendor/faiss/faiss/utils/pq_code_distance.h +8 -4
  217. data/vendor/faiss/faiss/utils/prefetch.h +1 -26
  218. data/vendor/faiss/faiss/utils/quantize_lut.cpp +26 -27
  219. data/vendor/faiss/faiss/utils/quantize_lut.h +11 -0
  220. data/vendor/faiss/faiss/utils/rabitq_simd.h +51 -16
  221. data/vendor/faiss/faiss/utils/simd_impl/distances_aarch64.cpp +9 -4
  222. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +231 -35
  223. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +64 -5
  224. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +64 -5
  225. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +428 -108
  226. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv_kernel.h +68 -0
  227. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +93 -0
  228. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels.h +76 -0
  229. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx2.cpp +52 -0
  230. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_avx512.cpp +52 -0
  231. data/vendor/faiss/faiss/utils/simd_impl/fp16_kernels_neon.cpp +48 -0
  232. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx2.h +44 -0
  233. data/vendor/faiss/faiss/utils/simd_impl/masked_sum-avx512.h +43 -0
  234. data/vendor/faiss/faiss/utils/simd_impl/masked_sum.h +38 -0
  235. data/vendor/faiss/faiss/utils/simd_impl/partitioning_avx2.cpp +0 -4
  236. data/vendor/faiss/faiss/utils/simd_impl/partitioning_neon.cpp +0 -4
  237. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +54 -15
  238. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +89 -55
  239. data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +70 -36
  240. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +297 -10
  241. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +0 -4
  242. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +2 -7
  243. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
  244. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx2.cpp +0 -4
  245. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_avx512.cpp +0 -4
  246. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +30 -0
  247. data/vendor/faiss/faiss/utils/simd_levels.cpp +191 -70
  248. data/vendor/faiss/faiss/utils/simd_levels.h +100 -16
  249. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +0 -4
  250. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +0 -4
  251. data/vendor/faiss/faiss/utils/utils.cpp +80 -0
  252. data/vendor/faiss/faiss/utils/utils.h +24 -0
  253. metadata +38 -5
  254. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
  255. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512_spr.h +0 -171
@@ -45,6 +45,7 @@
45
45
  #include <faiss/IndexIVFPQR.h>
46
46
  #include <faiss/IndexIVFRaBitQ.h>
47
47
  #include <faiss/IndexIVFRaBitQFastScan.h>
48
+ #include <faiss/IndexIVFSQFastScan.h>
48
49
  #include <faiss/IndexIVFSpectralHash.h>
49
50
  #include <faiss/IndexLSH.h>
50
51
  #include <faiss/IndexLattice.h>
@@ -52,11 +53,13 @@
52
53
  #include <faiss/IndexNSG.h>
53
54
  #include <faiss/IndexPQ.h>
54
55
  #include <faiss/IndexPQFastScan.h>
56
+ #include <faiss/IndexPiPNN.h>
55
57
  #include <faiss/IndexPreTransform.h>
56
58
  #include <faiss/IndexRaBitQ.h>
57
59
  #include <faiss/IndexRaBitQFastScan.h>
58
60
  #include <faiss/IndexRefine.h>
59
61
  #include <faiss/IndexRowwiseMinMax.h>
62
+ #include <faiss/IndexSQFastScan.h>
60
63
  #ifdef FAISS_ENABLE_SVS
61
64
  #include <faiss/impl/svs_io.h>
62
65
  #include <faiss/svs/IndexSVSFlat.h>
@@ -545,7 +548,7 @@ std::unique_ptr<InvertedLists> read_InvertedLists_up(
545
548
  READ1(n_levels);
546
549
  FAISS_THROW_IF_NOT_FMT(
547
550
  n_levels > 0, "invalid ilpn n_levels %zd", n_levels);
548
- constexpr size_t bs = Panorama::kDefaultBatchSize;
551
+ constexpr size_t bs = Panorama::kLegacyBatchSize;
549
552
  auto ailp = std::make_unique<ArrayInvertedListsPanorama>(
550
553
  nlist, code_size, n_levels, bs);
551
554
  std::vector<size_t> sizes(nlist);
@@ -765,6 +768,10 @@ void read_ProductQuantizer(ProductQuantizer* pq, IOReader* f) {
765
768
  pq->d * pq->ksub);
766
769
  }
767
770
 
771
+ static void validate_codebooks_size(
772
+ const AdditiveQuantizer& aq,
773
+ size_t effective_d);
774
+
768
775
  static void read_ResidualQuantizer_old(ResidualQuantizer& rq, IOReader* f) {
769
776
  READ1(rq.d);
770
777
  FAISS_THROW_IF_NOT_FMT(
@@ -786,6 +793,7 @@ static void read_ResidualQuantizer_old(ResidualQuantizer& rq, IOReader* f) {
786
793
  READ1(rq.norm_min);
787
794
  READ1(rq.norm_max);
788
795
  rq.set_derived_values();
796
+ validate_codebooks_size(rq, rq.d);
789
797
  }
790
798
 
791
799
  static void read_AdditiveQuantizer(AdditiveQuantizer& aq, IOReader* f) {
@@ -821,6 +829,10 @@ static void read_AdditiveQuantizer(AdditiveQuantizer& aq, IOReader* f) {
821
829
  }
822
830
 
823
831
  aq.set_derived_values();
832
+ FAISS_THROW_IF_NOT_FMT(
833
+ aq.code_size > 0,
834
+ "invalid AdditiveQuantizer: nbits sum to 0 bits, code_size %zd",
835
+ aq.code_size);
824
836
 
825
837
  // Sanity-check codebooks size without knowing the effective dimension.
826
838
  // codebooks stores effective_d * total_codebook_size floats, so its
@@ -843,6 +855,9 @@ static void read_AdditiveQuantizer(AdditiveQuantizer& aq, IOReader* f) {
843
855
  static void validate_codebooks_size(
844
856
  const AdditiveQuantizer& aq,
845
857
  size_t effective_d) {
858
+ if (!aq.is_trained && aq.codebooks.empty()) {
859
+ return;
860
+ }
846
861
  size_t required = mul_no_overflow(
847
862
  effective_d, aq.total_codebook_size, "codebooks validation");
848
863
  FAISS_THROW_IF_NOT_FMT(
@@ -1160,8 +1175,7 @@ void read_ScalarQuantizer(
1160
1175
  }
1161
1176
  }
1162
1177
 
1163
- // TurboQ full types: extract seed and qjl_type from trained,
1164
- // regenerate projection matrix.
1178
+ // TurboQ full types: extract seed and qjl_type from trained.
1165
1179
  if (ScalarQuantizer::TurboQuantRefine::is_turboq_full(ivsc->qtype) &&
1166
1180
  ivsc->trained.size() >= 3) {
1167
1181
  size_t n = ivsc->trained.size();
@@ -1170,7 +1184,6 @@ void read_ScalarQuantizer(
1170
1184
  ivsc->turboq_refine.seed =
1171
1185
  ScalarQuantizer::TurboQuantRefine::unpack_seed(
1172
1186
  ivsc->trained[n - 3], ivsc->trained[n - 2]);
1173
- ivsc->turboq_refine.init_projection(ivsc->d);
1174
1187
  }
1175
1188
  }
1176
1189
 
@@ -1357,6 +1370,59 @@ static void read_NSG(NSG& nsg, IOReader* f) {
1357
1370
  FAISS_CHECK_RANGE(nsg.enterpoint, 0, N);
1358
1371
  }
1359
1372
 
1373
+ static void read_PiPNN(PiPNN& pipnn, idx_t index_ntotal, IOReader* f) {
1374
+ READ1(pipnn.R);
1375
+ READ1(pipnn.leaf_k);
1376
+ READ1(pipnn.hash_bits);
1377
+ READ1(pipnn.reservoir_size);
1378
+ READ1(pipnn.c_max);
1379
+ READ1(pipnn.c_min);
1380
+ READ1(pipnn.leader_cap);
1381
+ // Former replicas field: build metadata that search does not need.
1382
+ int replicas;
1383
+ READ1(replicas);
1384
+ READ1(pipnn.leader_fraction);
1385
+ READ1(pipnn.alpha);
1386
+ READVECTOR(pipnn.fanout);
1387
+ READ1(pipnn.seed);
1388
+ READ1_BOOL(pipnn.ip_partition_by_angle);
1389
+ READ1(pipnn.search_L);
1390
+ pipnn.check_params();
1391
+ READ1(pipnn.ntotal);
1392
+ READ1(pipnn.entry_point);
1393
+ // Checked before the graph is allocated; ntotal == 0 means unbuilt.
1394
+ if (pipnn.ntotal != 0) {
1395
+ FAISS_THROW_IF_NOT_FMT(
1396
+ pipnn.ntotal == index_ntotal,
1397
+ "PiPNN ntotal %" PRId64 " != index ntotal %" PRId64,
1398
+ pipnn.ntotal,
1399
+ index_ntotal);
1400
+ FAISS_THROW_IF_NOT_FMT(
1401
+ pipnn.ntotal <= INT32_MAX,
1402
+ "built PiPNN ntotal %" PRId64 " exceeds INT32_MAX",
1403
+ pipnn.ntotal);
1404
+ FAISS_CHECK_RANGE(pipnn.entry_point, 0, pipnn.ntotal);
1405
+ }
1406
+ const size_t graph_size = mul_no_overflow(
1407
+ (size_t)pipnn.ntotal, (size_t)pipnn.R, "PiPNN graph");
1408
+ // Views (IO_FLAG_MMAP_IFC, zero-copy) skip the scan: it would fault in
1409
+ // the whole graph. Search bounds-checks every neighbour id instead.
1410
+ if (!read_vector_base(pipnn.graph, f, graph_size, std::nullopt)) {
1411
+ FAISS_THROW_IF_NOT(
1412
+ graph_size <
1413
+ get_deserialization_vector_byte_limit() / sizeof(int32_t));
1414
+ pipnn.graph.resize(graph_size);
1415
+ READANDCHECK(pipnn.graph.data(), graph_size);
1416
+ for (int32_t id : pipnn.graph) {
1417
+ FAISS_CHECK_RANGE(id, -1, pipnn.ntotal);
1418
+ }
1419
+ }
1420
+ read_vector(pipnn.next_copy, f);
1421
+ FAISS_THROW_IF_NOT(
1422
+ pipnn.next_copy.size() == 0 ||
1423
+ pipnn.next_copy.size() == size_t(pipnn.ntotal));
1424
+ }
1425
+
1360
1426
  static void read_NNDescent(NNDescent& nnd, IOReader* f) {
1361
1427
  READ1(nnd.ntotal);
1362
1428
  READ1(nnd.d);
@@ -1688,6 +1754,10 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
1688
1754
  int d;
1689
1755
  size_t n_levels, batch_size;
1690
1756
  READ1(d);
1757
+ // This branch does not go through read_index_header, so the range
1758
+ // check that guards `d` for every other index type has to be
1759
+ // repeated here.
1760
+ FAISS_CHECK_RANGE(d, 0, (1 << 20) + 1);
1691
1761
  READ1(n_levels);
1692
1762
  FAISS_THROW_IF_NOT_FMT(n_levels > 0, "invalid n_levels %zd", n_levels);
1693
1763
  READ1(batch_size);
@@ -1702,24 +1772,36 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
1702
1772
  d, n_levels, batch_size);
1703
1773
  }
1704
1774
  READ1(idxp->ntotal);
1775
+ FAISS_THROW_IF_NOT_FMT(
1776
+ idxp->ntotal >= 0,
1777
+ "invalid ntotal %" PRId64 " read from IxFP index",
1778
+ (int64_t)idxp->ntotal);
1705
1779
  READ1_BOOL(idxp->is_trained);
1706
1780
  READVECTOR(idxp->codes);
1707
1781
  READVECTOR(idxp->cum_sums);
1708
- size_t num_slots = mul_no_overflow(
1709
- ((size_t)idxp->ntotal + idxp->batch_size - 1) /
1710
- idxp->batch_size,
1711
- idxp->batch_size,
1782
+ const size_t ntotal = (size_t)idxp->ntotal;
1783
+ const size_t num_batches =
1784
+ ntotal / batch_size + (ntotal % batch_size != 0);
1785
+ const size_t num_slots = mul_no_overflow(
1786
+ num_batches,
1787
+ batch_size,
1712
1788
  "IndexFlatPanorama num_batches*batch_size");
1713
- FAISS_THROW_IF_NOT(
1714
- idxp->codes.size() ==
1715
- mul_no_overflow(
1716
- num_slots, idxp->code_size, "IndexFlatPanorama codes"));
1717
- FAISS_THROW_IF_NOT(
1718
- idxp->cum_sums.size() ==
1719
- mul_no_overflow(
1720
- num_slots,
1721
- idxp->pano.n_levels + 1,
1722
- "IndexFlatPanorama cum_sums"));
1789
+ const size_t expected_codes_size = mul_no_overflow(
1790
+ num_slots, idxp->code_size, "IndexFlatPanorama codes");
1791
+ FAISS_THROW_IF_NOT_FMT(
1792
+ idxp->codes.size() == expected_codes_size,
1793
+ "IndexFlatPanorama codes size mismatch: got %zu, expected %zu",
1794
+ idxp->codes.size(),
1795
+ expected_codes_size);
1796
+ const size_t expected_cum_sums_size = mul_no_overflow(
1797
+ num_slots,
1798
+ idxp->pano.n_levels + 1,
1799
+ "IndexFlatPanorama cum_sums");
1800
+ FAISS_THROW_IF_NOT_FMT(
1801
+ idxp->cum_sums.size() == expected_cum_sums_size,
1802
+ "IndexFlatPanorama cum_sums size mismatch: got %zu, expected %zu",
1803
+ idxp->cum_sums.size(),
1804
+ expected_cum_sums_size);
1723
1805
  idxp->verbose = false;
1724
1806
  idx = std::move(idxp);
1725
1807
  } else if (
@@ -1823,6 +1905,14 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
1823
1905
  } else {
1824
1906
  read_ResidualQuantizer(idxr->rq, f, io_flags);
1825
1907
  }
1908
+ FAISS_THROW_IF_NOT_MSG(
1909
+ idxr->rq.is_trained == idxr->is_trained,
1910
+ "IndexResidualQuantizer is_trained does not match "
1911
+ "ResidualQuantizer is_trained");
1912
+ FAISS_THROW_IF_NOT_MSG(
1913
+ idxr->ntotal == 0 || idxr->rq.is_trained,
1914
+ "IndexResidualQuantizer with ntotal > 0 must have trained "
1915
+ "ResidualQuantizer");
1826
1916
  validate_aq_dimension_match(
1827
1917
  idxr->rq, idxr->d, "IndexResidualQuantizer");
1828
1918
  READ1(idxr->code_size);
@@ -2136,7 +2226,7 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2136
2226
  read_ivf_header(ivfp.get(), f);
2137
2227
  ivfp->code_size = ivfp->d * sizeof(float);
2138
2228
  READ1(ivfp->n_levels);
2139
- ivfp->batch_size = Panorama::kDefaultBatchSize;
2229
+ ivfp->batch_size = Panorama::kLegacyBatchSize;
2140
2230
  read_InvertedLists(*ivfp, f, io_flags);
2141
2231
  idx = std::move(ivfp);
2142
2232
  } else if (h == fourcc("IwP2")) {
@@ -2163,6 +2253,12 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2163
2253
  read_ScalarQuantizer(&idxs->sq, f, *idxs);
2164
2254
  read_vector(idxs->codes, f);
2165
2255
  idxs->code_size = idxs->sq.code_size;
2256
+ FAISS_THROW_IF_NOT(
2257
+ idxs->codes.size() ==
2258
+ mul_no_overflow(
2259
+ (size_t)idxs->ntotal,
2260
+ idxs->code_size,
2261
+ "IndexScalarQuantizer codes"));
2166
2262
  idx = std::move(idxs);
2167
2263
  } else if (h == fourcc("IxLa")) {
2168
2264
  int d, nsq, scale_nbit, r2;
@@ -2374,8 +2470,18 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2374
2470
  // Validate transform chain dimension consistency:
2375
2471
  // chain[0].d_in must equal the outer index d, consecutive
2376
2472
  // transforms must have matching d_out/d_in, and the last
2377
- // transform's d_out must equal the sub-index d.
2378
- if (nt > 0) {
2473
+ // transform's d_out must equal the sub-index d. With an empty
2474
+ // chain the sub-index d must equal the outer d directly, because
2475
+ // reconstruct() then hands the caller's buffer -- sized from the
2476
+ // outer d -- straight to the sub-index.
2477
+ if (nt == 0 && ixpt->index) {
2478
+ FAISS_THROW_IF_NOT_FMT(
2479
+ ixpt->index->d == ixpt->d,
2480
+ "IndexPreTransform empty chain: sub-index d=%d "
2481
+ "!= index d=%d",
2482
+ ixpt->index->d,
2483
+ ixpt->d);
2484
+ } else if (nt > 0) {
2379
2485
  FAISS_THROW_IF_NOT_FMT(
2380
2486
  ixpt->chain[0]->d_in == ixpt->d,
2381
2487
  "IndexPreTransform chain[0] d_in=%d != index d=%d",
@@ -2497,7 +2603,7 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2497
2603
  } else if (
2498
2604
  h == fourcc("IHNf") || h == fourcc("IHNp") || h == fourcc("IHNs") ||
2499
2605
  h == fourcc("IHN2") || h == fourcc("IHNc") || h == fourcc("IHc2") ||
2500
- h == fourcc("IHfP") || h == fourcc("IH00")) {
2606
+ h == fourcc("IHfP") || h == fourcc("IHNr") || h == fourcc("IH00")) {
2501
2607
  std::unique_ptr<IndexHNSW> idxhnsw;
2502
2608
  if (h == fourcc("IH00")) {
2503
2609
  idxhnsw = std::make_unique<IndexHNSW>();
@@ -2515,6 +2621,8 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2515
2621
  idxhnsw = std::make_unique<IndexHNSWCagra>();
2516
2622
  } else if (h == fourcc("IHc2")) {
2517
2623
  idxhnsw = std::make_unique<IndexHNSWCagra>();
2624
+ } else if (h == fourcc("IHNr")) {
2625
+ idxhnsw = std::make_unique<IndexHNSWRaBitQ>();
2518
2626
  }
2519
2627
  read_index_header(*idxhnsw, f);
2520
2628
  if (h == fourcc("IHfP")) {
@@ -2525,10 +2633,21 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2525
2633
  "dynamic_cast to IndexHNSWFlatPanorama failed");
2526
2634
  size_t nlevels;
2527
2635
  READ1(nlevels);
2636
+ FAISS_THROW_IF_NOT_FMT(
2637
+ nlevels > 0, "invalid IHfP n_levels %zd", nlevels);
2528
2638
  const_cast<size_t&>(idx_panorama->num_panorama_levels) = nlevels;
2529
2639
  const_cast<Panorama&>(idx_panorama->pano) =
2530
2640
  Panorama(idx_panorama->d * sizeof(float), nlevels, 1);
2531
2641
  READVECTOR(idx_panorama->cum_sums);
2642
+ // Both the search path and get_cum_sum() index cum_sums at
2643
+ // stride pano.n_levels + 1, which may be lower than the
2644
+ // serialized nlevels if set_derived_values() truncated it.
2645
+ FAISS_THROW_IF_NOT(
2646
+ idx_panorama->cum_sums.size() ==
2647
+ mul_no_overflow(
2648
+ (size_t)idx_panorama->ntotal,
2649
+ idx_panorama->pano.n_levels + 1,
2650
+ "IndexHNSWFlatPanorama cum_sums"));
2532
2651
  } else if (h == fourcc("IHNc") || h == fourcc("IHc2")) {
2533
2652
  READ1_BOOL(idxhnsw->keep_max_size_level0);
2534
2653
  auto idx_hnsw_cagra = dynamic_cast<IndexHNSWCagra*>(idxhnsw.get());
@@ -2549,7 +2668,8 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2549
2668
  "HNSW levels size %zu != index ntotal %" PRId64,
2550
2669
  idxhnsw->hnsw.levels.size(),
2551
2670
  idxhnsw->ntotal);
2552
- idxhnsw->hnsw.is_panorama = (h == fourcc("IHfP"));
2671
+ idxhnsw->hnsw.search_method =
2672
+ h == fourcc("IHfP") ? HNSW::SM_PANORAMA : HNSW::SM_DEFAULT;
2553
2673
  // `HNSW::is_similarity` is intentionally not serialized, so we
2554
2674
  // re-derive it here from the persisted metric type. Without this,
2555
2675
  // a saved IP/similarity index would come back configured as a
@@ -2571,6 +2691,57 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2571
2691
  idxhnsw->storage->d,
2572
2692
  idxhnsw->d);
2573
2693
  }
2694
+ if (h == fourcc("IHNr")) {
2695
+ auto* idx_rabitq = dynamic_cast<IndexHNSWRaBitQ*>(idxhnsw.get());
2696
+ FAISS_THROW_IF_NOT_MSG(
2697
+ idx_rabitq, "IHNr must deserialize to an IndexHNSWRaBitQ");
2698
+ FAISS_THROW_IF_NOT_MSG(
2699
+ idxhnsw->metric_type == METRIC_L2,
2700
+ "IndexHNSWRaBitQ supports only the L2 metric");
2701
+ bool staged;
2702
+ READ1_BOOL(staged);
2703
+ idxhnsw->hnsw.search_method =
2704
+ staged ? HNSW::SM_RABITQ : HNSW::SM_DEFAULT;
2705
+ if (idxhnsw->storage) {
2706
+ auto* rq = dynamic_cast<IndexRaBitQ*>(idxhnsw->storage);
2707
+ FAISS_THROW_IF_NOT_MSG(
2708
+ rq, "IndexHNSWRaBitQ storage must be an IndexRaBitQ");
2709
+ FAISS_THROW_IF_NOT_MSG(
2710
+ rq->metric_type == idxhnsw->metric_type &&
2711
+ rq->rabitq.metric_type == idxhnsw->metric_type,
2712
+ "IndexHNSWRaBitQ storage metric mismatch");
2713
+ FAISS_THROW_IF_NOT_MSG(
2714
+ rq->is_trained == idxhnsw->is_trained,
2715
+ "IndexHNSWRaBitQ storage training state mismatch");
2716
+ FAISS_THROW_IF_NOT_FMT(
2717
+ rq->rabitq.nb_bits >= 1 && rq->rabitq.nb_bits <= 9,
2718
+ "invalid RaBitQ nb_bits=%zu",
2719
+ rq->rabitq.nb_bits);
2720
+ const size_t expected_code_size =
2721
+ rq->rabitq.compute_code_size(rq->d, rq->rabitq.nb_bits);
2722
+ validate_code_size_match(
2723
+ rq->rabitq.code_size,
2724
+ expected_code_size,
2725
+ "IndexHNSWRaBitQ quantizer");
2726
+ validate_code_size_match(
2727
+ rq->code_size,
2728
+ expected_code_size,
2729
+ "IndexHNSWRaBitQ storage");
2730
+ FAISS_THROW_IF_NOT(
2731
+ rq->codes.size() ==
2732
+ mul_no_overflow(
2733
+ static_cast<size_t>(rq->ntotal),
2734
+ rq->code_size,
2735
+ "IndexHNSWRaBitQ codes"));
2736
+ FAISS_THROW_IF_NOT_MSG(
2737
+ !rq->is_trained ||
2738
+ rq->center.size() == static_cast<size_t>(rq->d),
2739
+ "IndexHNSWRaBitQ center size mismatch");
2740
+ FAISS_THROW_IF_NOT_MSG(
2741
+ staged == (rq->rabitq.nb_bits >= 2),
2742
+ "IndexHNSWRaBitQ staged-search metadata mismatch");
2743
+ }
2744
+ }
2574
2745
  if (h == fourcc("IHN2")) {
2575
2746
  FAISS_THROW_IF_NOT_MSG(
2576
2747
  idxhnsw->storage,
@@ -2661,6 +2832,66 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2661
2832
  idxnnd->d);
2662
2833
  }
2663
2834
  idx = std::move(idxnnd);
2835
+ } else if (h == fourcc("IPNf") || h == fourcc("IPNs")) {
2836
+ std::unique_ptr<IndexPiPNN> idxpipnn;
2837
+ if (h == fourcc("IPNf")) {
2838
+ idxpipnn = std::make_unique<IndexPiPNNFlat>();
2839
+ } else {
2840
+ idxpipnn = std::make_unique<IndexPiPNNSQ>();
2841
+ }
2842
+ read_index_header(*idxpipnn, f);
2843
+ READ1_BOOL(idxpipnn->defer_build);
2844
+ read_PiPNN(idxpipnn->pipnn, idxpipnn->ntotal, f);
2845
+ idxpipnn->storage = read_index(f, io_flags);
2846
+ idxpipnn->own_fields = true;
2847
+ FAISS_THROW_IF_NOT_MSG(
2848
+ idxpipnn->storage, "IndexPiPNN requires non-null storage");
2849
+ IndexPiPNN::check_storage_supported(*idxpipnn->storage);
2850
+ FAISS_THROW_IF_NOT_FMT(
2851
+ idxpipnn->storage->ntotal == idxpipnn->ntotal,
2852
+ "PiPNN storage ntotal %" PRId64 " != index ntotal %" PRId64,
2853
+ idxpipnn->storage->ntotal,
2854
+ idxpipnn->ntotal);
2855
+ FAISS_THROW_IF_NOT_FMT(
2856
+ idxpipnn->storage->d == idxpipnn->d,
2857
+ "PiPNN storage d %d != index d %d",
2858
+ idxpipnn->storage->d,
2859
+ idxpipnn->d);
2860
+ FAISS_THROW_IF_NOT_FMT(
2861
+ idxpipnn->storage->metric_type == idxpipnn->metric_type,
2862
+ "PiPNN storage metric %d != index metric %d",
2863
+ (int)idxpipnn->storage->metric_type,
2864
+ (int)idxpipnn->metric_type);
2865
+ idx = std::move(idxpipnn);
2866
+ } else if (h == fourcc("ISfs")) {
2867
+ auto idxsqfs = std::make_unique<IndexSQFastScan>();
2868
+ read_index_header(*idxsqfs, f);
2869
+ read_ScalarQuantizer(&idxsqfs->sq, f, *idxsqfs);
2870
+ READ1(idxsqfs->implem);
2871
+ READ1(idxsqfs->bbs);
2872
+ READ1(idxsqfs->qbs);
2873
+ FAISS_THROW_IF_NOT_MSG(idxsqfs->qbs >= 0, "qbs must be non-negative");
2874
+ READ1(idxsqfs->ntotal2);
2875
+ READ1(idxsqfs->M2);
2876
+ READVECTOR(idxsqfs->codes);
2877
+ READ1(idxsqfs->rerank_factor);
2878
+ READVECTOR(idxsqfs->lo_codes);
2879
+
2880
+ // Restore FastScan base-class fields from the SQ
2881
+ idxsqfs->M = idxsqfs->sq.d;
2882
+ idxsqfs->nbits = 4;
2883
+ idxsqfs->ksub = 16;
2884
+ idxsqfs->code_size = idxsqfs->M2 / 2;
2885
+
2886
+ validate_fastscan_fields(
2887
+ idxsqfs->M,
2888
+ idxsqfs->M2,
2889
+ idxsqfs->ksub,
2890
+ idxsqfs->bbs,
2891
+ "IndexSQFastScan");
2892
+
2893
+ idx = std::move(idxsqfs);
2894
+
2664
2895
  } else if (h == fourcc("IPfs")) {
2665
2896
  auto idxpqfs = std::make_unique<IndexPQFastScan>();
2666
2897
  read_index_header(*idxpqfs, f);
@@ -2688,6 +2919,46 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2688
2919
 
2689
2920
  idx = std::move(idxpqfs);
2690
2921
 
2922
+ } else if (h == fourcc("IwSf")) {
2923
+ auto ivfsqfs = std::make_unique<IndexIVFSQFastScan>();
2924
+ read_ivf_header(ivfsqfs.get(), f);
2925
+ READ1_BOOL(ivfsqfs->by_residual);
2926
+ READ1(ivfsqfs->code_size);
2927
+ READ1(ivfsqfs->bbs);
2928
+ READ1(ivfsqfs->M2);
2929
+ READ1(ivfsqfs->implem);
2930
+ READ1(ivfsqfs->rerank_factor);
2931
+ read_ScalarQuantizer(&ivfsqfs->sq, f, *ivfsqfs);
2932
+ read_InvertedLists(*ivfsqfs, f, io_flags);
2933
+
2934
+ ivfsqfs->M = ivfsqfs->d;
2935
+ ivfsqfs->nbits = 4;
2936
+ ivfsqfs->ksub = 16;
2937
+ ivfsqfs->init_code_packer();
2938
+
2939
+ bool has_orig;
2940
+ READ1(has_orig);
2941
+ if (has_orig) {
2942
+ ivfsqfs->lo_codes_invlists = read_InvertedLists(f, io_flags);
2943
+ // Rebuild direct_map from lo_codes_invlists for reranking
2944
+ ivfsqfs->direct_map.set_type(
2945
+ DirectMap::Hashtable, ivfsqfs->invlists, 0);
2946
+ for (size_t list_no = 0; list_no < ivfsqfs->nlist; list_no++) {
2947
+ size_t list_size =
2948
+ ivfsqfs->lo_codes_invlists->list_size(list_no);
2949
+ if (list_size == 0) {
2950
+ continue;
2951
+ }
2952
+ InvertedLists::ScopedIds ids(
2953
+ ivfsqfs->lo_codes_invlists, list_no);
2954
+ for (size_t j = 0; j < list_size; j++) {
2955
+ ivfsqfs->direct_map.add_single_id(ids[j], list_no, j);
2956
+ }
2957
+ }
2958
+ }
2959
+
2960
+ idx = std::move(ivfsqfs);
2961
+
2691
2962
  } else if (h == fourcc("IwPf")) {
2692
2963
  auto ivpq = std::make_unique<IndexIVFPQFastScan>();
2693
2964
  read_ivf_header(ivpq.get(), f);
@@ -2838,7 +3109,7 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2838
3109
  auto idxq = std::make_unique<IndexRaBitQ>();
2839
3110
  read_index_header(*idxq, f);
2840
3111
  read_RaBitQuantizer(idxq->rabitq, f, idxq->d, false);
2841
- READVECTOR(idxq->codes);
3112
+ read_vector(idxq->codes, f);
2842
3113
  READVECTOR(idxq->center);
2843
3114
  READ1(idxq->qb);
2844
3115
  // qb=0: Not quantized - direct distance computation on given float32s.
@@ -2857,7 +3128,7 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2857
3128
  read_index_header(*idxq, f);
2858
3129
  read_RaBitQuantizer(
2859
3130
  idxq->rabitq, f, idxq->d, true); // Reads nb_bits from file
2860
- READVECTOR(idxq->codes);
3131
+ read_vector(idxq->codes, f);
2861
3132
  READVECTOR(idxq->center);
2862
3133
  READ1(idxq->qb);
2863
3134
  // qb=0: Not quantized - direct distance computation on given float32s.
@@ -3052,7 +3323,8 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
3052
3323
  bool initialized;
3053
3324
  READ1_BOOL(initialized);
3054
3325
  if (initialized) {
3055
- faiss::svs_io::ReaderStreambuf rbuf(f);
3326
+ faiss::svs_io::ReaderStreambuf rbuf(
3327
+ f, get_deserialization_vector_byte_limit());
3056
3328
  std::istream is(&rbuf);
3057
3329
  svs_ivf->deserialize_impl(is);
3058
3330
  }
@@ -3060,7 +3332,8 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
3060
3332
  bool trained;
3061
3333
  READ1_BOOL(trained);
3062
3334
  if (trained) {
3063
- faiss::svs_io::ReaderStreambuf rbuf(f);
3335
+ faiss::svs_io::ReaderStreambuf rbuf(
3336
+ f, get_deserialization_vector_byte_limit());
3064
3337
  std::istream is(&rbuf);
3065
3338
  auto* leanvec =
3066
3339
  dynamic_cast<IndexSVSIVFLeanVec*>(svs_ivf.get());
@@ -3390,7 +3663,7 @@ std::unique_ptr<IndexBinary> read_index_binary_up(IOReader* f, int io_flags) {
3390
3663
  auto idxhnsw = std::make_unique<IndexBinaryHNSW>();
3391
3664
  read_index_binary_header(*idxhnsw, f);
3392
3665
  read_HNSW(idxhnsw->hnsw, f);
3393
- idxhnsw->hnsw.is_panorama = false;
3666
+ idxhnsw->hnsw.search_method = HNSW::SM_DEFAULT;
3394
3667
  FAISS_THROW_IF_NOT_FMT(
3395
3668
  idxhnsw->hnsw.levels.size() == (size_t)idxhnsw->ntotal,
3396
3669
  "IndexBinaryHNSW HNSW levels size %zu != ntotal %" PRId64,
@@ -3414,7 +3687,7 @@ std::unique_ptr<IndexBinary> read_index_binary_up(IOReader* f, int io_flags) {
3414
3687
  READ1_BOOL(idxhnsw->base_level_only);
3415
3688
  READ1(idxhnsw->num_base_level_search_entrypoints);
3416
3689
  read_HNSW(idxhnsw->hnsw, f);
3417
- idxhnsw->hnsw.is_panorama = false;
3690
+ idxhnsw->hnsw.search_method = HNSW::SM_DEFAULT;
3418
3691
  FAISS_THROW_IF_NOT_FMT(
3419
3692
  idxhnsw->hnsw.levels.size() == (size_t)idxhnsw->ntotal,
3420
3693
  "IndexBinaryHNSWCagra HNSW levels size %zu != ntotal %" PRId64,