faiss 0.4.3 → 0.5.1

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 (186) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -0
  3. data/README.md +2 -0
  4. data/ext/faiss/index.cpp +33 -6
  5. data/ext/faiss/index_binary.cpp +17 -4
  6. data/ext/faiss/kmeans.cpp +6 -6
  7. data/lib/faiss/version.rb +1 -1
  8. data/vendor/faiss/faiss/AutoTune.cpp +2 -3
  9. data/vendor/faiss/faiss/AutoTune.h +1 -1
  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 +26 -51
  13. data/vendor/faiss/faiss/IVFlib.h +1 -1
  14. data/vendor/faiss/faiss/Index.cpp +11 -0
  15. data/vendor/faiss/faiss/Index.h +34 -11
  16. data/vendor/faiss/faiss/Index2Layer.cpp +1 -1
  17. data/vendor/faiss/faiss/Index2Layer.h +2 -2
  18. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +1 -0
  19. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +9 -4
  20. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.h +5 -1
  21. data/vendor/faiss/faiss/IndexBinary.h +7 -7
  22. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +1 -1
  23. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +8 -2
  24. data/vendor/faiss/faiss/IndexBinaryHNSW.h +1 -1
  25. data/vendor/faiss/faiss/IndexBinaryHash.cpp +3 -3
  26. data/vendor/faiss/faiss/IndexBinaryHash.h +5 -5
  27. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +7 -6
  28. data/vendor/faiss/faiss/IndexFastScan.cpp +125 -49
  29. data/vendor/faiss/faiss/IndexFastScan.h +102 -7
  30. data/vendor/faiss/faiss/IndexFlat.cpp +374 -4
  31. data/vendor/faiss/faiss/IndexFlat.h +81 -1
  32. data/vendor/faiss/faiss/IndexHNSW.cpp +93 -2
  33. data/vendor/faiss/faiss/IndexHNSW.h +58 -2
  34. data/vendor/faiss/faiss/IndexIDMap.cpp +14 -13
  35. data/vendor/faiss/faiss/IndexIDMap.h +6 -6
  36. data/vendor/faiss/faiss/IndexIVF.cpp +1 -1
  37. data/vendor/faiss/faiss/IndexIVF.h +5 -5
  38. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +1 -1
  39. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +9 -3
  40. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +3 -1
  41. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +176 -90
  42. data/vendor/faiss/faiss/IndexIVFFastScan.h +173 -18
  43. data/vendor/faiss/faiss/IndexIVFFlat.cpp +1 -0
  44. data/vendor/faiss/faiss/IndexIVFFlatPanorama.cpp +251 -0
  45. data/vendor/faiss/faiss/IndexIVFFlatPanorama.h +64 -0
  46. data/vendor/faiss/faiss/IndexIVFPQ.cpp +3 -1
  47. data/vendor/faiss/faiss/IndexIVFPQ.h +1 -1
  48. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +134 -2
  49. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +7 -1
  50. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +99 -8
  51. data/vendor/faiss/faiss/IndexIVFRaBitQ.h +4 -1
  52. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +828 -0
  53. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +252 -0
  54. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +1 -1
  55. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +1 -1
  56. data/vendor/faiss/faiss/IndexNNDescent.cpp +1 -1
  57. data/vendor/faiss/faiss/IndexNSG.cpp +1 -1
  58. data/vendor/faiss/faiss/IndexNeuralNetCodec.h +1 -1
  59. data/vendor/faiss/faiss/IndexPQ.cpp +4 -1
  60. data/vendor/faiss/faiss/IndexPQ.h +1 -1
  61. data/vendor/faiss/faiss/IndexPQFastScan.cpp +6 -2
  62. data/vendor/faiss/faiss/IndexPQFastScan.h +5 -1
  63. data/vendor/faiss/faiss/IndexPreTransform.cpp +14 -0
  64. data/vendor/faiss/faiss/IndexPreTransform.h +9 -0
  65. data/vendor/faiss/faiss/IndexRaBitQ.cpp +96 -13
  66. data/vendor/faiss/faiss/IndexRaBitQ.h +11 -2
  67. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +731 -0
  68. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +175 -0
  69. data/vendor/faiss/faiss/IndexRefine.cpp +49 -0
  70. data/vendor/faiss/faiss/IndexRefine.h +17 -0
  71. data/vendor/faiss/faiss/IndexShards.cpp +1 -1
  72. data/vendor/faiss/faiss/MatrixStats.cpp +3 -3
  73. data/vendor/faiss/faiss/MetricType.h +1 -1
  74. data/vendor/faiss/faiss/VectorTransform.h +2 -2
  75. data/vendor/faiss/faiss/clone_index.cpp +5 -1
  76. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +1 -1
  77. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +3 -1
  78. data/vendor/faiss/faiss/gpu/GpuIndex.h +11 -11
  79. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +1 -1
  80. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +1 -1
  81. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +11 -7
  82. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +1 -1
  83. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +2 -0
  84. data/vendor/faiss/faiss/gpu/test/TestGpuIcmEncoder.cpp +7 -0
  85. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +1 -1
  86. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
  87. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +1 -1
  88. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +2 -2
  89. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +1 -1
  90. data/vendor/faiss/faiss/impl/CodePacker.h +2 -2
  91. data/vendor/faiss/faiss/impl/DistanceComputer.h +77 -6
  92. data/vendor/faiss/faiss/impl/FastScanDistancePostProcessing.h +53 -0
  93. data/vendor/faiss/faiss/impl/HNSW.cpp +295 -16
  94. data/vendor/faiss/faiss/impl/HNSW.h +35 -6
  95. data/vendor/faiss/faiss/impl/IDSelector.cpp +2 -2
  96. data/vendor/faiss/faiss/impl/IDSelector.h +4 -4
  97. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +4 -4
  98. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +1 -1
  99. data/vendor/faiss/faiss/impl/LookupTableScaler.h +1 -1
  100. data/vendor/faiss/faiss/impl/NNDescent.cpp +1 -1
  101. data/vendor/faiss/faiss/impl/NNDescent.h +2 -2
  102. data/vendor/faiss/faiss/impl/NSG.cpp +1 -1
  103. data/vendor/faiss/faiss/impl/Panorama.cpp +193 -0
  104. data/vendor/faiss/faiss/impl/Panorama.h +204 -0
  105. data/vendor/faiss/faiss/impl/PanoramaStats.cpp +33 -0
  106. data/vendor/faiss/faiss/impl/PanoramaStats.h +38 -0
  107. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +5 -5
  108. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +1 -1
  109. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +1 -1
  110. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +2 -0
  111. data/vendor/faiss/faiss/impl/ProductQuantizer.h +1 -1
  112. data/vendor/faiss/faiss/impl/RaBitQStats.cpp +29 -0
  113. data/vendor/faiss/faiss/impl/RaBitQStats.h +56 -0
  114. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +294 -0
  115. data/vendor/faiss/faiss/impl/RaBitQUtils.h +330 -0
  116. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +304 -223
  117. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +72 -4
  118. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +362 -0
  119. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.h +112 -0
  120. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +1 -1
  121. data/vendor/faiss/faiss/impl/ResultHandler.h +4 -4
  122. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +7 -10
  123. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +2 -4
  124. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +7 -4
  125. data/vendor/faiss/faiss/impl/index_read.cpp +238 -10
  126. data/vendor/faiss/faiss/impl/index_write.cpp +212 -19
  127. data/vendor/faiss/faiss/impl/io.cpp +2 -2
  128. data/vendor/faiss/faiss/impl/io.h +4 -4
  129. data/vendor/faiss/faiss/impl/kmeans1d.cpp +1 -1
  130. data/vendor/faiss/faiss/impl/kmeans1d.h +1 -1
  131. data/vendor/faiss/faiss/impl/lattice_Zn.h +2 -2
  132. data/vendor/faiss/faiss/impl/mapped_io.cpp +2 -2
  133. data/vendor/faiss/faiss/impl/mapped_io.h +4 -3
  134. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +8 -1
  135. data/vendor/faiss/faiss/impl/platform_macros.h +12 -0
  136. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +30 -4
  137. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +14 -8
  138. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +5 -6
  139. data/vendor/faiss/faiss/impl/simd_result_handlers.h +55 -11
  140. data/vendor/faiss/faiss/impl/svs_io.cpp +86 -0
  141. data/vendor/faiss/faiss/impl/svs_io.h +67 -0
  142. data/vendor/faiss/faiss/impl/zerocopy_io.h +1 -1
  143. data/vendor/faiss/faiss/index_factory.cpp +217 -8
  144. data/vendor/faiss/faiss/index_factory.h +1 -1
  145. data/vendor/faiss/faiss/index_io.h +1 -1
  146. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +1 -1
  147. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  148. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +115 -1
  149. data/vendor/faiss/faiss/invlists/InvertedLists.h +46 -0
  150. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +1 -1
  151. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +1 -1
  152. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +261 -0
  153. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +117 -0
  154. data/vendor/faiss/faiss/svs/IndexSVSFlat.h +66 -0
  155. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +245 -0
  156. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +137 -0
  157. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +39 -0
  158. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +42 -0
  159. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +149 -0
  160. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +58 -0
  161. data/vendor/faiss/faiss/utils/AlignedTable.h +1 -1
  162. data/vendor/faiss/faiss/utils/Heap.cpp +2 -2
  163. data/vendor/faiss/faiss/utils/Heap.h +3 -3
  164. data/vendor/faiss/faiss/utils/NeuralNet.cpp +1 -1
  165. data/vendor/faiss/faiss/utils/NeuralNet.h +3 -3
  166. data/vendor/faiss/faiss/utils/approx_topk/approx_topk.h +2 -2
  167. data/vendor/faiss/faiss/utils/approx_topk/avx2-inl.h +2 -2
  168. data/vendor/faiss/faiss/utils/approx_topk/mode.h +1 -1
  169. data/vendor/faiss/faiss/utils/distances.cpp +0 -3
  170. data/vendor/faiss/faiss/utils/distances.h +2 -2
  171. data/vendor/faiss/faiss/utils/extra_distances-inl.h +3 -1
  172. data/vendor/faiss/faiss/utils/hamming-inl.h +2 -0
  173. data/vendor/faiss/faiss/utils/hamming.cpp +7 -6
  174. data/vendor/faiss/faiss/utils/hamming.h +1 -1
  175. data/vendor/faiss/faiss/utils/hamming_distance/common.h +1 -2
  176. data/vendor/faiss/faiss/utils/partitioning.cpp +5 -5
  177. data/vendor/faiss/faiss/utils/partitioning.h +2 -2
  178. data/vendor/faiss/faiss/utils/rabitq_simd.h +222 -336
  179. data/vendor/faiss/faiss/utils/random.cpp +1 -1
  180. data/vendor/faiss/faiss/utils/simdlib_avx2.h +1 -1
  181. data/vendor/faiss/faiss/utils/simdlib_avx512.h +1 -1
  182. data/vendor/faiss/faiss/utils/simdlib_neon.h +2 -2
  183. data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +1 -1
  184. data/vendor/faiss/faiss/utils/utils.cpp +9 -2
  185. data/vendor/faiss/faiss/utils/utils.h +2 -2
  186. metadata +29 -1
@@ -29,11 +29,13 @@
29
29
  #include <faiss/IndexIVFAdditiveQuantizer.h>
30
30
  #include <faiss/IndexIVFAdditiveQuantizerFastScan.h>
31
31
  #include <faiss/IndexIVFFlat.h>
32
+ #include <faiss/IndexIVFFlatPanorama.h>
32
33
  #include <faiss/IndexIVFIndependentQuantizer.h>
33
34
  #include <faiss/IndexIVFPQ.h>
34
35
  #include <faiss/IndexIVFPQFastScan.h>
35
36
  #include <faiss/IndexIVFPQR.h>
36
37
  #include <faiss/IndexIVFRaBitQ.h>
38
+ #include <faiss/IndexIVFRaBitQFastScan.h>
37
39
  #include <faiss/IndexIVFSpectralHash.h>
38
40
  #include <faiss/IndexLSH.h>
39
41
  #include <faiss/IndexLattice.h>
@@ -43,8 +45,16 @@
43
45
  #include <faiss/IndexPQFastScan.h>
44
46
  #include <faiss/IndexPreTransform.h>
45
47
  #include <faiss/IndexRaBitQ.h>
48
+ #include <faiss/IndexRaBitQFastScan.h>
46
49
  #include <faiss/IndexRefine.h>
47
50
  #include <faiss/IndexRowwiseMinMax.h>
51
+ #ifdef FAISS_ENABLE_SVS
52
+ #include <faiss/impl/svs_io.h>
53
+ #include <faiss/svs/IndexSVSFlat.h>
54
+ #include <faiss/svs/IndexSVSVamana.h>
55
+ #include <faiss/svs/IndexSVSVamanaLVQ.h>
56
+ #include <faiss/svs/IndexSVSVamanaLeanVec.h>
57
+ #endif
48
58
  #include <faiss/IndexScalarQuantizer.h>
49
59
  #include <faiss/MetaIndexes.h>
50
60
  #include <faiss/VectorTransform.h>
@@ -68,9 +78,10 @@ namespace faiss {
68
78
  **************************************************************/
69
79
 
70
80
  // This is a baseline functionality for reading mmapped and zerocopied vector.
71
- // * if `beforeknown_size` is defined, then a size of the vector won't be read.
81
+ // * if `beforeknown_size` is defined, then a size of the vector won't be
82
+ // read.
72
83
  // * if `size_multiplier` is defined, then a size will be multiplied by it.
73
- // * returns true is the case was handled; ownerwise, false
84
+ // * returns true is the case was handled; otherwise, false
74
85
  template <typename VectorT>
75
86
  bool read_vector_base(
76
87
  VectorT& target,
@@ -181,7 +192,7 @@ void read_vector(VectorT& target, IOReader* f) {
181
192
  // a replacement for READXBVECTOR
182
193
  template <typename VectorT>
183
194
  void read_xb_vector(VectorT& target, IOReader* f) {
184
- // size is not known beforehand, nultiply the size 4x
195
+ // size is not known beforehand, multiply the size 4x
185
196
  if (read_vector_base<VectorT>(target, f, std::nullopt, 4)) {
186
197
  return;
187
198
  }
@@ -325,6 +336,34 @@ InvertedLists* read_InvertedLists(IOReader* f, int io_flags) {
325
336
  "read_InvertedLists:"
326
337
  " WARN! inverted lists not stored with IVF object\n");
327
338
  return nullptr;
339
+ } else if (h == fourcc("ilpn") && !(io_flags & IO_FLAG_SKIP_IVF_DATA)) {
340
+ size_t nlist, code_size, n_levels;
341
+ READ1(nlist);
342
+ READ1(code_size);
343
+ READ1(n_levels);
344
+ auto ailp = new ArrayInvertedListsPanorama(nlist, code_size, n_levels);
345
+ std::vector<size_t> sizes(nlist);
346
+ read_ArrayInvertedLists_sizes(f, sizes);
347
+ for (size_t i = 0; i < nlist; i++) {
348
+ ailp->ids[i].resize(sizes[i]);
349
+ size_t num_elems =
350
+ ((sizes[i] + ArrayInvertedListsPanorama::kBatchSize - 1) /
351
+ ArrayInvertedListsPanorama::kBatchSize) *
352
+ ArrayInvertedListsPanorama::kBatchSize;
353
+ ailp->codes[i].resize(num_elems * code_size);
354
+ ailp->cum_sums[i].resize(num_elems * (n_levels + 1));
355
+ }
356
+ for (size_t i = 0; i < nlist; i++) {
357
+ size_t n = sizes[i];
358
+ if (n > 0) {
359
+ read_vector_with_known_size(
360
+ ailp->codes[i], f, ailp->codes[i].size());
361
+ read_vector_with_known_size(ailp->ids[i], f, n);
362
+ read_vector_with_known_size(
363
+ ailp->cum_sums[i], f, ailp->cum_sums[i].size());
364
+ }
365
+ }
366
+ return ailp;
328
367
  } else if (h == fourcc("ilar") && !(io_flags & IO_FLAG_SKIP_IVF_DATA)) {
329
368
  auto ails = new ArrayInvertedLists(0, 0);
330
369
  READ1(ails->nlist);
@@ -575,11 +614,19 @@ ProductQuantizer* read_ProductQuantizer(IOReader* reader) {
575
614
  return pq;
576
615
  }
577
616
 
578
- static void read_RaBitQuantizer(RaBitQuantizer* rabitq, IOReader* f) {
579
- // don't care about rabitq->centroid
617
+ static void read_RaBitQuantizer(
618
+ RaBitQuantizer* rabitq,
619
+ IOReader* f,
620
+ bool multi_bit = true) {
580
621
  READ1(rabitq->d);
581
622
  READ1(rabitq->code_size);
582
623
  READ1(rabitq->metric_type);
624
+
625
+ if (multi_bit) {
626
+ READ1(rabitq->nb_bits);
627
+ } else {
628
+ rabitq->nb_bits = 1;
629
+ }
583
630
  }
584
631
 
585
632
  void read_direct_map(DirectMap* dm, IOReader* f) {
@@ -681,6 +728,20 @@ Index* read_index(IOReader* f, int io_flags) {
681
728
  if (h == fourcc("null")) {
682
729
  // denotes a missing index, useful for some cases
683
730
  return nullptr;
731
+ } else if (h == fourcc("IxFP")) {
732
+ int d;
733
+ size_t n_levels, batch_size;
734
+ READ1(d);
735
+ READ1(n_levels);
736
+ READ1(batch_size);
737
+ IndexFlatL2Panorama* idxp =
738
+ new IndexFlatL2Panorama(d, n_levels, batch_size);
739
+ READ1(idxp->ntotal);
740
+ READ1(idxp->is_trained);
741
+ READVECTOR(idxp->codes);
742
+ READVECTOR(idxp->cum_sums);
743
+ idxp->verbose = false;
744
+ idx = idxp;
684
745
  } else if (
685
746
  h == fourcc("IxFI") || h == fourcc("IxF2") || h == fourcc("IxFl")) {
686
747
  IndexFlat* idxf;
@@ -927,6 +988,13 @@ Index* read_index(IOReader* f, int io_flags) {
927
988
  }
928
989
  read_InvertedLists(ivfl, f, io_flags);
929
990
  idx = ivfl;
991
+ } else if (h == fourcc("IwPn")) {
992
+ IndexIVFFlatPanorama* ivfp = new IndexIVFFlatPanorama();
993
+ read_ivf_header(ivfp, f);
994
+ ivfp->code_size = ivfp->d * sizeof(float);
995
+ READ1(ivfp->n_levels);
996
+ read_InvertedLists(ivfp, f, io_flags);
997
+ idx = ivfp;
930
998
  } else if (h == fourcc("IwFl")) {
931
999
  IndexIVFFlat* ivfl = new IndexIVFFlat();
932
1000
  read_ivf_header(ivfl, f);
@@ -1058,13 +1126,19 @@ Index* read_index(IOReader* f, int io_flags) {
1058
1126
  read_index_header(imiq, f);
1059
1127
  read_ProductQuantizer(&imiq->pq, f);
1060
1128
  idx = imiq;
1061
- } else if (h == fourcc("IxRF")) {
1129
+ } else if (h == fourcc("IxRF") || h == fourcc("IxRP")) {
1062
1130
  IndexRefine* idxrf = new IndexRefine();
1063
1131
  read_index_header(idxrf, f);
1064
1132
  idxrf->base_index = read_index(f, io_flags);
1065
1133
  idxrf->refine_index = read_index(f, io_flags);
1066
1134
  READ1(idxrf->k_factor);
1067
- if (dynamic_cast<IndexFlat*>(idxrf->refine_index)) {
1135
+ if (h == fourcc("IxRP")) {
1136
+ // then make a RefineFlatPanorama with it
1137
+ IndexRefine* idxrf_old = idxrf;
1138
+ idxrf = new IndexRefinePanorama();
1139
+ *idxrf = *idxrf_old;
1140
+ delete idxrf_old;
1141
+ } else if (dynamic_cast<IndexFlat*>(idxrf->refine_index)) {
1068
1142
  // then make a RefineFlat with it
1069
1143
  IndexRefine* idxrf_old = idxrf;
1070
1144
  idxrf = new IndexRefineFlat();
@@ -1099,11 +1173,15 @@ Index* read_index(IOReader* f, int io_flags) {
1099
1173
  idx = idxp;
1100
1174
  } else if (
1101
1175
  h == fourcc("IHNf") || h == fourcc("IHNp") || h == fourcc("IHNs") ||
1102
- h == fourcc("IHN2") || h == fourcc("IHNc") || h == fourcc("IHc2")) {
1176
+ h == fourcc("IHN2") || h == fourcc("IHNc") || h == fourcc("IHc2") ||
1177
+ h == fourcc("IHfP")) {
1103
1178
  IndexHNSW* idxhnsw = nullptr;
1104
1179
  if (h == fourcc("IHNf")) {
1105
1180
  idxhnsw = new IndexHNSWFlat();
1106
1181
  }
1182
+ if (h == fourcc("IHfP")) {
1183
+ idxhnsw = new IndexHNSWFlatPanorama();
1184
+ }
1107
1185
  if (h == fourcc("IHNp")) {
1108
1186
  idxhnsw = new IndexHNSWPQ();
1109
1187
  }
@@ -1120,6 +1198,15 @@ Index* read_index(IOReader* f, int io_flags) {
1120
1198
  idxhnsw = new IndexHNSWCagra();
1121
1199
  }
1122
1200
  read_index_header(idxhnsw, f);
1201
+ if (h == fourcc("IHfP")) {
1202
+ auto idx_panorama = dynamic_cast<IndexHNSWFlatPanorama*>(idxhnsw);
1203
+ size_t nlevels;
1204
+ READ1(nlevels);
1205
+ const_cast<size_t&>(idx_panorama->num_panorama_levels) = nlevels;
1206
+ const_cast<size_t&>(idx_panorama->panorama_level_width) =
1207
+ (idx_panorama->d + nlevels - 1) / nlevels;
1208
+ READVECTOR(idx_panorama->cum_sums);
1209
+ }
1123
1210
  if (h == fourcc("IHNc") || h == fourcc("IHc2")) {
1124
1211
  READ1(idxhnsw->keep_max_size_level0);
1125
1212
  auto idx_hnsw_cagra = dynamic_cast<IndexHNSWCagra*>(idxhnsw);
@@ -1132,6 +1219,7 @@ Index* read_index(IOReader* f, int io_flags) {
1132
1219
  }
1133
1220
  }
1134
1221
  read_HNSW(&idxhnsw->hnsw, f);
1222
+ idxhnsw->hnsw.is_panorama = (h == fourcc("IHfP"));
1135
1223
  idxhnsw->storage = read_index(f, io_flags);
1136
1224
  idxhnsw->own_fields = idxhnsw->storage != nullptr;
1137
1225
  if (h == fourcc("IHNp") && !(io_flags & IO_FLAG_PQ_SKIP_SDC_TABLE)) {
@@ -1224,24 +1312,162 @@ Index* read_index(IOReader* f, int io_flags) {
1224
1312
  imm->own_fields = true;
1225
1313
 
1226
1314
  idx = imm;
1315
+ } else if (h == fourcc("Irfs")) {
1316
+ IndexRaBitQFastScan* idxqfs = new IndexRaBitQFastScan();
1317
+ read_index_header(idxqfs, f);
1318
+ read_RaBitQuantizer(&idxqfs->rabitq, f, true);
1319
+ READVECTOR(idxqfs->center);
1320
+ READ1(idxqfs->qb);
1321
+ READVECTOR(idxqfs->flat_storage);
1322
+
1323
+ READ1(idxqfs->bbs);
1324
+ READ1(idxqfs->ntotal2);
1325
+ READ1(idxqfs->M2);
1326
+ READ1(idxqfs->code_size);
1327
+
1328
+ const size_t M_fastscan = (idxqfs->d + 3) / 4;
1329
+ constexpr size_t nbits_fastscan = 4;
1330
+ idxqfs->M = M_fastscan;
1331
+ idxqfs->nbits = nbits_fastscan;
1332
+ idxqfs->ksub = (1 << nbits_fastscan);
1333
+
1334
+ READVECTOR(idxqfs->codes);
1335
+ idx = idxqfs;
1227
1336
  } else if (h == fourcc("Ixrq")) {
1228
1337
  IndexRaBitQ* idxq = new IndexRaBitQ();
1229
1338
  read_index_header(idxq, f);
1230
- read_RaBitQuantizer(&idxq->rabitq, f);
1339
+ read_RaBitQuantizer(&idxq->rabitq, f, false);
1231
1340
  READVECTOR(idxq->codes);
1232
1341
  READVECTOR(idxq->center);
1233
1342
  READ1(idxq->qb);
1343
+
1344
+ // rabitq.nb_bits is already set to 1 by read_RaBitQuantizer
1345
+ idxq->code_size = idxq->rabitq.code_size;
1346
+ idx = idxq;
1347
+ } else if (h == fourcc("Ixrr")) {
1348
+ // Ixrr = multi-bit format (new)
1349
+ IndexRaBitQ* idxq = new IndexRaBitQ();
1350
+ read_index_header(idxq, f);
1351
+ read_RaBitQuantizer(&idxq->rabitq, f, true); // Reads nb_bits from file
1352
+ READVECTOR(idxq->codes);
1353
+ READVECTOR(idxq->center);
1354
+ READ1(idxq->qb);
1355
+
1234
1356
  idxq->code_size = idxq->rabitq.code_size;
1235
1357
  idx = idxq;
1236
1358
  } else if (h == fourcc("Iwrq")) {
1237
1359
  IndexIVFRaBitQ* ivrq = new IndexIVFRaBitQ();
1238
1360
  read_ivf_header(ivrq, f);
1239
- read_RaBitQuantizer(&ivrq->rabitq, f);
1361
+ read_RaBitQuantizer(&ivrq->rabitq, f, false);
1362
+ READ1(ivrq->code_size);
1363
+ READ1(ivrq->by_residual);
1364
+ READ1(ivrq->qb);
1365
+
1366
+ // rabitq.nb_bits is already set to 1 by read_RaBitQuantizer
1367
+ // Update rabitq to match nb_bits
1368
+ ivrq->rabitq.code_size =
1369
+ ivrq->rabitq.compute_code_size(ivrq->d, ivrq->rabitq.nb_bits);
1370
+ ivrq->code_size = ivrq->rabitq.code_size;
1371
+ read_InvertedLists(ivrq, f, io_flags);
1372
+ idx = ivrq;
1373
+ } else if (h == fourcc("Iwrr")) {
1374
+ // Iwrr = multi-bit format (new)
1375
+ IndexIVFRaBitQ* ivrq = new IndexIVFRaBitQ();
1376
+ read_ivf_header(ivrq, f);
1377
+ read_RaBitQuantizer(&ivrq->rabitq, f, true); // Reads nb_bits from file
1240
1378
  READ1(ivrq->code_size);
1241
1379
  READ1(ivrq->by_residual);
1242
1380
  READ1(ivrq->qb);
1381
+
1382
+ // Update rabitq to match nb_bits
1383
+ ivrq->rabitq.code_size =
1384
+ ivrq->rabitq.compute_code_size(ivrq->d, ivrq->rabitq.nb_bits);
1385
+ ivrq->code_size = ivrq->rabitq.code_size;
1243
1386
  read_InvertedLists(ivrq, f, io_flags);
1244
1387
  idx = ivrq;
1388
+ }
1389
+ #ifdef FAISS_ENABLE_SVS
1390
+ else if (
1391
+ h == fourcc("ILVQ") || h == fourcc("ISVL") || h == fourcc("ISVD")) {
1392
+ IndexSVSVamana* svs;
1393
+ if (h == fourcc("ILVQ")) {
1394
+ svs = new IndexSVSVamanaLVQ();
1395
+ } else if (h == fourcc("ISVL")) {
1396
+ svs = new IndexSVSVamanaLeanVec();
1397
+ } else if (h == fourcc("ISVD")) {
1398
+ svs = new IndexSVSVamana();
1399
+ }
1400
+
1401
+ read_index_header(svs, f);
1402
+ READ1(svs->graph_max_degree);
1403
+ READ1(svs->alpha);
1404
+ READ1(svs->search_window_size);
1405
+ READ1(svs->search_buffer_capacity);
1406
+ READ1(svs->construction_window_size);
1407
+ READ1(svs->max_candidate_pool_size);
1408
+ READ1(svs->prune_to);
1409
+ READ1(svs->use_full_search_history);
1410
+ READ1(svs->storage_kind);
1411
+ if (h == fourcc("ISVL")) {
1412
+ READ1(dynamic_cast<IndexSVSVamanaLeanVec*>(svs)->leanvec_d);
1413
+ }
1414
+
1415
+ bool initialized;
1416
+ READ1(initialized);
1417
+ if (initialized) {
1418
+ faiss::svs_io::ReaderStreambuf rbuf(f);
1419
+ std::istream is(&rbuf);
1420
+ svs->deserialize_impl(is);
1421
+ }
1422
+ if (h == fourcc("ISVL")) {
1423
+ bool trained;
1424
+ READ1(trained);
1425
+ if (trained) {
1426
+ faiss::svs_io::ReaderStreambuf rbuf(f);
1427
+ std::istream is(&rbuf);
1428
+ dynamic_cast<IndexSVSVamanaLeanVec*>(svs)
1429
+ ->deserialize_training_data(is);
1430
+ }
1431
+ }
1432
+ idx = svs;
1433
+ } else if (h == fourcc("ISVF")) {
1434
+ IndexSVSFlat* svs = new IndexSVSFlat();
1435
+ read_index_header(svs, f);
1436
+
1437
+ bool initialized;
1438
+ READ1(initialized);
1439
+ if (initialized) {
1440
+ faiss::svs_io::ReaderStreambuf rbuf(f);
1441
+ std::istream is(&rbuf);
1442
+ svs->deserialize_impl(is);
1443
+ idx = svs;
1444
+ }
1445
+ }
1446
+ #endif // FAISS_ENABLE_SVS
1447
+ else if (h == fourcc("Iwrf")) {
1448
+ IndexIVFRaBitQFastScan* ivrqfs = new IndexIVFRaBitQFastScan();
1449
+ read_ivf_header(ivrqfs, f);
1450
+ read_RaBitQuantizer(&ivrqfs->rabitq, f);
1451
+ READ1(ivrqfs->by_residual);
1452
+ READ1(ivrqfs->code_size);
1453
+ READ1(ivrqfs->bbs);
1454
+ READ1(ivrqfs->qbs2);
1455
+ READ1(ivrqfs->M2);
1456
+ READ1(ivrqfs->implem);
1457
+ READ1(ivrqfs->qb);
1458
+ READ1(ivrqfs->centered);
1459
+ READVECTOR(ivrqfs->flat_storage);
1460
+
1461
+ // Initialize FastScan base class fields
1462
+ const size_t M_fastscan = (ivrqfs->d + 3) / 4;
1463
+ constexpr size_t nbits_fastscan = 4;
1464
+ ivrqfs->M = M_fastscan;
1465
+ ivrqfs->nbits = nbits_fastscan;
1466
+ ivrqfs->ksub = (1 << nbits_fastscan);
1467
+
1468
+ read_InvertedLists(ivrqfs, f, io_flags);
1469
+ ivrqfs->init_code_packer();
1470
+ idx = ivrqfs;
1245
1471
  } else {
1246
1472
  FAISS_THROW_FMT(
1247
1473
  "Index type 0x%08x (\"%s\") not recognized",
@@ -1396,6 +1622,7 @@ IndexBinary* read_index_binary(IOReader* f, int io_flags) {
1396
1622
  IndexBinaryHNSW* idxhnsw = new IndexBinaryHNSW();
1397
1623
  read_index_binary_header(idxhnsw, f);
1398
1624
  read_HNSW(&idxhnsw->hnsw, f);
1625
+ idxhnsw->hnsw.is_panorama = false;
1399
1626
  idxhnsw->storage = read_index_binary(f, io_flags);
1400
1627
  idxhnsw->own_fields = true;
1401
1628
  idx = idxhnsw;
@@ -1406,6 +1633,7 @@ IndexBinary* read_index_binary(IOReader* f, int io_flags) {
1406
1633
  READ1(idxhnsw->base_level_only);
1407
1634
  READ1(idxhnsw->num_base_level_search_entrypoints);
1408
1635
  read_HNSW(&idxhnsw->hnsw, f);
1636
+ idxhnsw->hnsw.is_panorama = false;
1409
1637
  idxhnsw->storage = read_index_binary(f, io_flags);
1410
1638
  idxhnsw->own_fields = true;
1411
1639
  idx = idxhnsw;