faiss 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/ext/faiss/extconf.rb +1 -0
  4. data/ext/faiss/index.cpp +42 -0
  5. data/lib/faiss/version.rb +1 -1
  6. data/vendor/faiss/faiss/IVFlib.cpp +48 -51
  7. data/vendor/faiss/faiss/IVFlib.h +6 -2
  8. data/vendor/faiss/faiss/Index.cpp +2 -2
  9. data/vendor/faiss/faiss/Index.h +3 -3
  10. data/vendor/faiss/faiss/IndexBinary.cpp +2 -2
  11. data/vendor/faiss/faiss/IndexBinary.h +2 -2
  12. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
  13. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +6 -3
  14. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +6 -11
  15. data/vendor/faiss/faiss/IndexFastScan.cpp +1 -1
  16. data/vendor/faiss/faiss/IndexFlat.cpp +1 -1
  17. data/vendor/faiss/faiss/IndexHNSW.cpp +12 -10
  18. data/vendor/faiss/faiss/IndexHNSW.h +2 -5
  19. data/vendor/faiss/faiss/IndexIDMap.cpp +17 -4
  20. data/vendor/faiss/faiss/IndexIDMap.h +10 -5
  21. data/vendor/faiss/faiss/IndexIVF.cpp +68 -37
  22. data/vendor/faiss/faiss/IndexIVF.h +37 -1
  23. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -15
  24. data/vendor/faiss/faiss/IndexIVFFlat.h +6 -6
  25. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -1
  26. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +28 -44
  27. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +2 -11
  28. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
  29. data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
  30. data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +7 -2
  31. data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -0
  32. data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
  33. data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
  34. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -1
  35. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +2 -2
  36. data/vendor/faiss/faiss/SuperKMeans.cpp +2 -2
  37. data/vendor/faiss/faiss/VectorTransform.cpp +156 -9
  38. data/vendor/faiss/faiss/VectorTransform.h +24 -1
  39. data/vendor/faiss/faiss/factory_tools.cpp +19 -1
  40. data/vendor/faiss/faiss/gpu/Dataset.h +66 -14
  41. data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +8 -2
  42. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +8 -2
  43. data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
  44. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
  45. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
  46. data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
  47. data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +1 -1
  48. data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +1 -1
  49. data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
  50. data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +1 -1
  51. data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +1 -1
  52. data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
  53. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
  54. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +1 -1
  55. data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +1 -1
  56. data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +1 -1
  57. data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
  58. data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
  59. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
  60. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
  61. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
  62. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +31 -50
  63. data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
  64. data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
  65. data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
  66. data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
  67. data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
  68. data/vendor/faiss/faiss/impl/HNSW.cpp +2 -2
  69. data/vendor/faiss/faiss/impl/HNSW.h +2 -2
  70. data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
  71. data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
  72. data/vendor/faiss/faiss/impl/PiPNN.cpp +4 -2
  73. data/vendor/faiss/faiss/impl/PiPNN.h +1 -1
  74. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
  75. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
  76. data/vendor/faiss/faiss/impl/RaBitQUtils.h +29 -0
  77. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +14 -13
  78. data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
  79. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
  80. data/vendor/faiss/faiss/impl/expanded_scanners.h +29 -24
  81. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +68 -6
  82. data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
  83. data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -0
  84. data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -0
  85. data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +13 -0
  86. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +84 -0
  87. data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +0 -4
  88. data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
  89. data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +64 -55
  90. data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
  91. data/vendor/faiss/faiss/impl/index_read.cpp +146 -9
  92. data/vendor/faiss/faiss/impl/index_write.cpp +29 -5
  93. data/vendor/faiss/faiss/impl/io.cpp +1 -0
  94. data/vendor/faiss/faiss/impl/io_macros.h +2 -2
  95. data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
  96. data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
  97. data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
  98. data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
  99. data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
  100. data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
  101. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
  102. data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
  103. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
  104. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
  105. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
  106. data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
  107. data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +1 -1
  108. data/vendor/faiss/faiss/impl/platform_macros.h +47 -28
  109. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +6 -1
  110. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
  111. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
  112. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -2
  113. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -1
  114. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
  115. data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +26 -26
  116. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +48 -45
  117. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +34 -32
  118. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +8 -6
  119. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +4 -3
  120. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +34 -33
  121. data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
  122. data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
  123. data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
  124. data/vendor/faiss/faiss/index_factory.cpp +30 -6
  125. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  126. data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
  127. data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
  128. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +12 -9
  129. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
  130. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
  131. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
  132. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
  133. data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
  134. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
  135. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
  136. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
  137. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
  138. data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
  139. data/vendor/faiss/faiss/utils/distances.cpp +246 -34
  140. data/vendor/faiss/faiss/utils/distances.h +145 -2
  141. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  142. data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
  143. data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
  144. data/vendor/faiss/faiss/utils/rabitq_simd.h +120 -6
  145. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +47 -7
  146. data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
  147. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +47 -7
  148. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +50 -10
  149. data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
  150. data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +13 -0
  151. data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
  152. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +131 -36
  153. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +156 -45
  154. data/vendor/faiss/faiss/utils/simd_levels.cpp +3 -3
  155. data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
  156. data/vendor/faiss/faiss/utils/utils.cpp +4 -0
  157. metadata +20 -1
@@ -113,8 +113,8 @@ struct SIMDResultHandlerToFloat {
113
113
  size_t scan_cnt = 0; // scanned vector number (except filtered)
114
114
 
115
115
  SIMDResultHandlerToFloat() = default;
116
- SIMDResultHandlerToFloat(size_t nq, size_t ntotal)
117
- : nq(nq), ntotal(ntotal) {}
116
+ SIMDResultHandlerToFloat(size_t nq_in, size_t ntotal_in)
117
+ : nq(nq_in), ntotal(ntotal_in) {}
118
118
 
119
119
  virtual void begin(const float* norms) {
120
120
  normalizers = norms;
@@ -201,7 +201,8 @@ struct StoreResultHandler : SIMDResultHandler<SL> {
201
201
  size_t i0 = 0;
202
202
  size_t j0 = 0;
203
203
 
204
- StoreResultHandler(uint16_t* data, size_t ld) : data(data), ld(ld) {}
204
+ StoreResultHandler(uint16_t* data_in, size_t ld_in)
205
+ : data(data_in), ld(ld_in) {}
205
206
 
206
207
  void handle(size_t q, size_t b, simd16uint16 d0, simd16uint16 d1) final {
207
208
  size_t ofs = (q + i0) * ld + j0 + b * 32;
@@ -237,7 +238,7 @@ struct FixedStorageHandler : SIMDResultHandler<SL> {
237
238
  dis[q + i0][2 * b + 1] = d1;
238
239
  }
239
240
 
240
- void set_block_origin(size_t i0_in, size_t j0_in) final {
241
+ void set_block_origin(size_t i0_in, [[maybe_unused]] size_t j0_in) final {
241
242
  this->i0 = i0_in;
242
243
  assert(j0_in == 0);
243
244
  }
@@ -265,8 +266,11 @@ struct ResultHandlerCompare : SIMDResultHandler<SL> {
265
266
 
266
267
  const IDSelector* sel;
267
268
 
268
- ResultHandlerCompare(size_t nq, size_t ntotal, const IDSelector* sel_in)
269
- : SIMDResultHandler<SL>(nq, ntotal), sel{sel_in} {
269
+ ResultHandlerCompare(
270
+ size_t nq_in,
271
+ size_t ntotal_in,
272
+ const IDSelector* sel_in)
273
+ : SIMDResultHandler<SL>(nq_in, ntotal_in), sel{sel_in} {
270
274
  this->is_CMax = C::is_max;
271
275
  this->sizeof_ids = sizeof(typename C::TI);
272
276
  this->with_fields = with_id_map;
@@ -350,13 +354,16 @@ struct SingleResultHandler : ResultHandlerCompare<C, with_id_map, SL> {
350
354
  int64_t* ids;
351
355
 
352
356
  SingleResultHandler(
353
- size_t nq,
354
- size_t ntotal,
355
- float* dis,
356
- int64_t* ids,
357
+ size_t nq_in,
358
+ size_t ntotal_in,
359
+ float* dis_in,
360
+ int64_t* ids_in,
357
361
  const IDSelector* sel_in)
358
- : RHC(nq, ntotal, sel_in), idis(nq), dis(dis), ids(ids) {
359
- for (size_t i = 0; i < nq; i++) {
362
+ : RHC(nq_in, ntotal_in, sel_in),
363
+ idis(nq_in),
364
+ dis(dis_in),
365
+ ids(ids_in) {
366
+ for (size_t i = 0; i < nq_in; i++) {
360
367
  ids[i] = -1;
361
368
  idis[i] = C::neutral();
362
369
  }
@@ -447,19 +454,19 @@ struct HeapHandler : ResultHandlerCompare<C, with_id_map, SL> {
447
454
  size_t nup = 0; // number of heap updates
448
455
 
449
456
  HeapHandler(
450
- size_t nq,
451
- size_t ntotal,
452
- int64_t k,
453
- float* dis,
454
- int64_t* ids,
457
+ size_t nq_in,
458
+ size_t ntotal_in,
459
+ int64_t k_in,
460
+ float* dis_in,
461
+ int64_t* ids_in,
455
462
  const IDSelector* sel_in,
456
- const float* normalizers = nullptr)
457
- : RHC(nq, ntotal, sel_in),
458
- idis(nq * k, threshold_idis(dis, normalizers)),
459
- iids(nq * k, -1),
460
- dis(dis),
461
- ids(ids),
462
- k(k) {}
463
+ const float* normalizers_in = nullptr)
464
+ : RHC(nq_in, ntotal_in, sel_in),
465
+ idis(nq_in * k_in, threshold_idis(dis_in, normalizers_in)),
466
+ iids(nq_in * k_in, -1),
467
+ dis(dis_in),
468
+ ids(ids_in),
469
+ k(k_in) {}
463
470
 
464
471
  static uint16_t threshold_idis(float* dis_in, const float* normalizers) {
465
472
  if (dis_in[0] == std::numeric_limits<float>::max()) {
@@ -561,7 +568,7 @@ struct HeapHandler : ResultHandlerCompare<C, with_id_map, SL> {
561
568
  one_a = a != 0 ? 1 / a : 0;
562
569
  b = normalizers[2 * q + 1];
563
570
  }
564
- for (int j = 0; j < k; j++) {
571
+ for (size_t j = 0; j < k; j++) {
565
572
  heap_dis[j] = heap_dis_in[j] * one_a + b;
566
573
  heap_ids[j] = heap_ids_in[j];
567
574
  }
@@ -607,21 +614,21 @@ struct ReservoirHandler : ResultHandlerCompare<C, with_id_map, SL> {
607
614
  std::vector<Reservoir> reservoirs;
608
615
 
609
616
  ReservoirHandler(
610
- size_t nq,
611
- size_t ntotal,
617
+ size_t nq_in,
618
+ size_t ntotal_in,
612
619
  size_t k,
613
620
  size_t cap,
614
- float* dis,
615
- int64_t* ids,
621
+ float* dis_in,
622
+ int64_t* ids_in,
616
623
  const IDSelector* sel_in)
617
- : RHC(nq, ntotal, sel_in),
624
+ : RHC(nq_in, ntotal_in, sel_in),
618
625
  capacity((cap + 15) & ~15),
619
- dis(dis),
620
- ids(ids) {
626
+ dis(dis_in),
627
+ ids(ids_in) {
621
628
  assert(capacity % 16 == 0);
622
- all_ids.resize(nq * capacity);
623
- all_vals.resize(nq * capacity);
624
- for (size_t q = 0; q < nq; q++) {
629
+ all_ids.resize(nq_in * capacity);
630
+ all_vals.resize(nq_in * capacity);
631
+ for (size_t q = 0; q < nq_in; q++) {
625
632
  reservoirs.emplace_back(
626
633
  k,
627
634
  capacity,
@@ -748,18 +755,20 @@ struct RangeHandler : ResultHandlerCompare<C, with_id_map, SL> {
748
755
  std::vector<Triplet> triplets;
749
756
 
750
757
  RangeHandler(
751
- RangeSearchResult& rres,
752
- float radius,
753
- size_t ntotal,
758
+ RangeSearchResult& rres_in,
759
+ float radius_in,
760
+ size_t ntotal_in,
754
761
  const IDSelector* sel_in)
755
- : RHC(rres.nq, ntotal, sel_in), rres(rres), radius(radius) {
762
+ : RHC(rres_in.nq, ntotal_in, sel_in),
763
+ rres(rres_in),
764
+ radius(radius_in) {
756
765
  thresholds.resize(nq);
757
766
  n_per_query.resize(nq + 1);
758
767
  }
759
768
 
760
769
  void begin(const float* norms) override {
761
770
  normalizers = norms;
762
- for (int q = 0; q < nq; ++q) {
771
+ for (size_t q = 0; q < nq; ++q) {
763
772
  thresholds[q] =
764
773
  int(normalizers[2 * q] * (radius - normalizers[2 * q + 1]));
765
774
  }
@@ -821,7 +830,7 @@ struct RangeHandler : ResultHandlerCompare<C, with_id_map, SL> {
821
830
  memmove(rres.lims + 1, rres.lims, sizeof(*rres.lims) * rres.nq);
822
831
  rres.lims[0] = 0;
823
832
 
824
- for (int q = 0; q < nq; q++) {
833
+ for (size_t q = 0; q < nq; q++) {
825
834
  float a = normalizers[2 * q];
826
835
  float one_a = a != 0 ? 1 / a : 0;
827
836
  float b = normalizers[2 * q + 1];
@@ -849,20 +858,20 @@ struct PartialRangeHandler : RangeHandler<C, with_id_map, SL> {
849
858
  RangeSearchPartialResult& pres;
850
859
 
851
860
  PartialRangeHandler(
852
- RangeSearchPartialResult& pres,
853
- float radius,
854
- size_t ntotal,
855
- size_t q0,
861
+ RangeSearchPartialResult& pres_in,
862
+ float radius_in,
863
+ size_t ntotal_in,
864
+ size_t q0_in,
856
865
  size_t q1,
857
866
  const IDSelector* sel_in)
858
867
  : RangeHandler<C, with_id_map, SL>(
859
- *pres.res,
860
- radius,
861
- ntotal,
868
+ *pres_in.res,
869
+ radius_in,
870
+ ntotal_in,
862
871
  sel_in),
863
- pres(pres) {
864
- nq = q1 - q0;
865
- this->q0 = q0;
872
+ pres(pres_in) {
873
+ nq = q1 - q0_in;
874
+ this->q0 = q0_in;
866
875
  }
867
876
 
868
877
  // shift left n_per_query
@@ -876,7 +885,7 @@ struct PartialRangeHandler : RangeHandler<C, with_id_map, SL> {
876
885
  // commit to partial result instead of full RangeResult
877
886
  void end() override {
878
887
  std::vector<typename RHC::Triplet> sorted_triplets(triplets.size());
879
- for (int q = 0; q < nq; q++) {
888
+ for (size_t q = 0; q < nq; q++) {
880
889
  n_per_query[q + 1] += n_per_query[q];
881
890
  }
882
891
  shift_n_per_query();
@@ -888,7 +897,7 @@ struct PartialRangeHandler : RangeHandler<C, with_id_map, SL> {
888
897
 
889
898
  size_t* lims = n_per_query.data();
890
899
 
891
- for (int q = 0; q < nq; q++) {
900
+ for (size_t q = 0; q < nq; q++) {
892
901
  float a = normalizers[2 * q];
893
902
  float one_a = a != 0 ? 1 / a : 0;
894
903
  float b = normalizers[2 * q + 1];
@@ -927,9 +936,9 @@ struct SingleQueryResultCollectHandler
927
936
 
928
937
  SingleQueryResultCollectHandler(
929
938
  std::vector<std::pair<TI, float>>& res,
930
- size_t ntotal,
939
+ size_t ntotal_in,
931
940
  const IDSelector* sel_in)
932
- : RHC(1, ntotal, sel_in), collect(res) {
941
+ : RHC(1, ntotal_in, sel_in), collect(res) {
933
942
  this->q_map = &q_id;
934
943
  }
935
944
 
@@ -50,12 +50,11 @@ class LockVector {
50
50
  omp_unset_lock(&data_[i]);
51
51
  }
52
52
 
53
- bool try_lock(size_t i) {
53
+ bool try_lock([[maybe_unused]] size_t i) {
54
54
  FAISS_CHECK_RANGE_DEBUG(i, 0, size_);
55
55
  return omp_test_lock(&data_[i]);
56
56
  }
57
57
 
58
- private:
59
58
  omp_lock_t* data_ = nullptr;
60
59
  size_t size_ = 0;
61
60
  size_t capacity_ = 0;
@@ -10,6 +10,7 @@
10
10
 
11
11
  #include <faiss/impl/io_macros.h>
12
12
 
13
+ #include <cerrno>
13
14
  #include <cinttypes>
14
15
  #include <cmath>
15
16
  #include <cstdio>
@@ -36,6 +37,9 @@
36
37
  #include <faiss/IndexIVF.h>
37
38
  #include <faiss/IndexIVFAdditiveQuantizer.h>
38
39
  #include <faiss/IndexIVFAdditiveQuantizerFastScan.h>
40
+ #ifdef FAISS_ENABLE_DISK_IVF
41
+ #include <faiss/IndexIVFRaBitQSSD.h>
42
+ #endif
39
43
  #include <faiss/IndexIVFEDEN.h>
40
44
  #include <faiss/IndexIVFFlat.h>
41
45
  #include <faiss/IndexIVFFlatPanorama.h>
@@ -380,8 +384,9 @@ std::unique_ptr<VectorTransform> read_VectorTransform_up(IOReader* f) {
380
384
  itqt->pca_then_itq = *pi;
381
385
  }
382
386
  vt = std::move(itqt);
383
- } else if (h == fourcc("HRot")) {
387
+ } else if (h == fourcc("HRot") || h == fourcc("HR64")) {
384
388
  auto hr = std::make_unique<HadamardRotation>();
389
+ hr->pad_to_power_of_2 = h == fourcc("HRot");
385
390
  READ1(hr->seed);
386
391
  vt = std::move(hr);
387
392
  } else {
@@ -449,6 +454,27 @@ std::unique_ptr<VectorTransform> read_VectorTransform_up(IOReader* f) {
449
454
  "HadamardRotation d_out exceeds deserialization byte limit");
450
455
  hr->init(hr->seed);
451
456
  }
457
+ if (h == fourcc("HR64")) {
458
+ auto* hr = dynamic_cast<HadamardRotation*>(vt.get());
459
+ FAISS_THROW_IF_NOT_MSG(hr, "dynamic_cast to HadamardRotation failed");
460
+ FAISS_THROW_IF_NOT_FMT(
461
+ vt->d_in > 0,
462
+ "invalid HadamardRotation d_in=%d (must be > 0)",
463
+ vt->d_in);
464
+ size_t p = (static_cast<size_t>(vt->d_in) + 63) / 64 * 64;
465
+ FAISS_THROW_IF_NOT_FMT(
466
+ static_cast<size_t>(vt->d_out) == p,
467
+ "invalid HadamardRotation d_out %d for d_in %d"
468
+ " (d_out must be the smallest multiple of 64 >= d_in)",
469
+ vt->d_out,
470
+ vt->d_in);
471
+ // Up to four sign-flip vectors of d_out floats.
472
+ FAISS_THROW_IF_NOT_MSG(
473
+ p <= get_deserialization_vector_byte_limit() /
474
+ (4 * sizeof(float)),
475
+ "HadamardRotation d_out exceeds deserialization byte limit");
476
+ hr->init(hr->seed);
477
+ }
452
478
  if (h == fourcc("RmDT")) {
453
479
  auto* rdt = dynamic_cast<RemapDimensionsTransform*>(vt.get());
454
480
  FAISS_THROW_IF_NOT_MSG(
@@ -1302,8 +1328,8 @@ static void validate_HNSW(const HNSW& hnsw) {
1302
1328
  static void read_HNSW(HNSW& hnsw, IOReader* f) {
1303
1329
  READVECTOR(hnsw.assign_probas);
1304
1330
  READVECTOR(hnsw.cum_nneighbor_per_level);
1305
- READVECTOR(hnsw.levels);
1306
- READVECTOR(hnsw.offsets);
1331
+ read_vector(hnsw.levels, f);
1332
+ read_vector(hnsw.offsets, f);
1307
1333
  read_vector(hnsw.neighbors, f);
1308
1334
 
1309
1335
  READ1(hnsw.entry_point);
@@ -1504,6 +1530,11 @@ static void read_RaBitQuantizer(
1504
1530
  "RaBitQuantizer dimension mismatch: rabitq.d=%zu vs index d=%d",
1505
1531
  rabitq.d,
1506
1532
  expected_d);
1533
+
1534
+ FAISS_THROW_IF_NOT_FMT(
1535
+ rabitq.nb_bits >= 1 && rabitq.nb_bits <= 9,
1536
+ "RaBitQuantizer nb_bits=%zu out of range [1, 9]",
1537
+ rabitq.nb_bits);
1507
1538
  }
1508
1539
 
1509
1540
  static void read_EDENScalarQuantizer(
@@ -2552,9 +2583,9 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
2552
2583
  idxmap->index = read_index(f, io_flags);
2553
2584
  FAISS_THROW_IF_NOT_MSG(idxmap->index, "IndexIDMap inner index is null");
2554
2585
  idxmap->own_fields = true;
2555
- READVECTOR(idxmap->id_map);
2586
+ read_vector(idxmap->id_map, f);
2556
2587
  FAISS_THROW_IF_NOT_FMT(
2557
- idxmap->id_map.size() == idxmap->ntotal,
2588
+ idxmap->id_map.size() == static_cast<size_t>(idxmap->ntotal),
2558
2589
  "IndexIDMap id_map size (%" PRId64
2559
2590
  ") does not match ntotal (%" PRId64 ")",
2560
2591
  int64_t(idxmap->id_map.size()),
@@ -3109,8 +3140,25 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
3109
3140
  auto idxq = std::make_unique<IndexRaBitQ>();
3110
3141
  read_index_header(*idxq, f);
3111
3142
  read_RaBitQuantizer(idxq->rabitq, f, idxq->d, false);
3143
+ // Update rabitq to match nb_bits
3144
+ idxq->rabitq.code_size =
3145
+ idxq->rabitq.compute_code_size(idxq->d, idxq->rabitq.nb_bits);
3112
3146
  read_vector(idxq->codes, f);
3113
3147
  READVECTOR(idxq->center);
3148
+ const size_t expected_codes_size = mul_no_overflow(
3149
+ static_cast<size_t>(idxq->ntotal),
3150
+ idxq->rabitq.code_size,
3151
+ "IndexRaBitQ codes");
3152
+ FAISS_THROW_IF_NOT_FMT(
3153
+ idxq->codes.size() == expected_codes_size,
3154
+ "IndexRaBitQ codes size mismatch: got %zu, expected %zu",
3155
+ idxq->codes.size(),
3156
+ expected_codes_size);
3157
+ FAISS_THROW_IF_NOT_FMT(
3158
+ idxq->center.size() == static_cast<size_t>(idxq->d),
3159
+ "IndexRaBitQ center size mismatch: got %zu, expected %d",
3160
+ idxq->center.size(),
3161
+ idxq->d);
3114
3162
  READ1(idxq->qb);
3115
3163
  // qb=0: Not quantized - direct distance computation on given float32s.
3116
3164
  // qb>0 && qb<=8: Scalar-quantized with qb bits of precision.
@@ -3128,8 +3176,25 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
3128
3176
  read_index_header(*idxq, f);
3129
3177
  read_RaBitQuantizer(
3130
3178
  idxq->rabitq, f, idxq->d, true); // Reads nb_bits from file
3179
+ // Update rabitq to match nb_bits
3180
+ idxq->rabitq.code_size =
3181
+ idxq->rabitq.compute_code_size(idxq->d, idxq->rabitq.nb_bits);
3131
3182
  read_vector(idxq->codes, f);
3132
3183
  READVECTOR(idxq->center);
3184
+ const size_t expected_codes_size = mul_no_overflow(
3185
+ static_cast<size_t>(idxq->ntotal),
3186
+ idxq->rabitq.code_size,
3187
+ "IndexRaBitQ codes");
3188
+ FAISS_THROW_IF_NOT_FMT(
3189
+ idxq->codes.size() == expected_codes_size,
3190
+ "IndexRaBitQ codes size mismatch: got %zu, expected %zu",
3191
+ idxq->codes.size(),
3192
+ expected_codes_size);
3193
+ FAISS_THROW_IF_NOT_FMT(
3194
+ idxq->center.size() == static_cast<size_t>(idxq->d),
3195
+ "IndexRaBitQ center size mismatch: got %zu, expected %d",
3196
+ idxq->center.size(),
3197
+ idxq->d);
3133
3198
  READ1(idxq->qb);
3134
3199
  // qb=0: Not quantized - direct distance computation on given float32s.
3135
3200
  // qb>0 && qb<=8: Scalar-quantized with qb bits of precision.
@@ -3184,6 +3249,72 @@ std::unique_ptr<Index> read_index_up(IOReader* f, int io_flags) {
3184
3249
  read_InvertedLists(*ivrq, f, io_flags);
3185
3250
  idx = std::move(ivrq);
3186
3251
  }
3252
+ #ifdef FAISS_ENABLE_DISK_IVF
3253
+ else if (h == fourcc("IwDr")) {
3254
+ auto ivdr = std::make_unique<IndexIVFRaBitQSSD>();
3255
+ read_ivf_header(ivdr.get(), f);
3256
+ read_RaBitQuantizer(ivdr->rabitq, f, ivdr->d, true);
3257
+ FAISS_THROW_IF_NOT_MSG(
3258
+ ivdr->rabitq.metric_type == ivdr->metric_type,
3259
+ "IndexIVFRaBitQSSD: metric mismatch");
3260
+ FAISS_THROW_IF_NOT_MSG(
3261
+ ivdr->metric_type == METRIC_L2 ||
3262
+ ivdr->metric_type == METRIC_INNER_PRODUCT,
3263
+ "IndexIVFRaBitQSSD: unsupported metric");
3264
+ FAISS_THROW_IF_NOT_FMT(
3265
+ ivdr->rabitq.nb_bits >= 2 && ivdr->rabitq.nb_bits <= 9,
3266
+ "invalid IndexIVFRaBitQSSD nb_bits=%zu",
3267
+ ivdr->rabitq.nb_bits);
3268
+ ivdr->rabitq.code_size =
3269
+ ivdr->rabitq.compute_code_size(ivdr->d, ivdr->rabitq.nb_bits);
3270
+ ivdr->code_size = ivdr->rabitq.code_size;
3271
+ READ1(ivdr->mem_dim);
3272
+ FAISS_THROW_IF_NOT_MSG(
3273
+ ivdr->d > 0 && ivdr->d % 64 == 0,
3274
+ "IndexIVFRaBitQSSD: d must be a positive multiple of 64");
3275
+ FAISS_THROW_IF_NOT_MSG(
3276
+ ivdr->mem_dim >= 64 && ivdr->mem_dim <= size_t(ivdr->d) &&
3277
+ ivdr->mem_dim % 64 == 0,
3278
+ "IndexIVFRaBitQSSD: mem_dim must be a multiple of 64 in [64, d]");
3279
+ READ1_BOOL(ivdr->use_hacc);
3280
+ READ1(ivdr->drain_alpha);
3281
+ READ1(ivdr->qd_per_thread);
3282
+ FAISS_THROW_IF_NOT_FMT(
3283
+ ivdr->drain_alpha >= 0 && ivdr->drain_alpha <= 1,
3284
+ "invalid IndexIVFRaBitQSSD drain_alpha=%g",
3285
+ ivdr->drain_alpha);
3286
+ FAISS_THROW_IF_NOT_FMT(
3287
+ ivdr->qd_per_thread >= 1 &&
3288
+ ivdr->qd_per_thread <=
3289
+ IndexIVFRaBitQSSD::kMaxReadsInFlight,
3290
+ "invalid IndexIVFRaBitQSSD qd_per_thread=%zu",
3291
+ ivdr->qd_per_thread);
3292
+ READVECTOR(ivdr->centroid_norms);
3293
+ FAISS_THROW_IF_NOT(
3294
+ ivdr->centroid_norms.empty() ||
3295
+ ivdr->centroid_norms.size() == ivdr->nlist);
3296
+ READVECTOR(ivdr->centroid_scales);
3297
+ FAISS_THROW_IF_NOT(
3298
+ ivdr->centroid_scales.empty() ||
3299
+ ivdr->centroid_scales.size() == ivdr->nlist);
3300
+ read_InvertedLists(*ivdr, f, io_flags);
3301
+ if (ivdr->invlists) {
3302
+ // checks the type and the layout of the inverted lists
3303
+ ivdr->get_ssd_invlists();
3304
+ // the search accepts empty centroid_norms when ntotal is 0. ntotal
3305
+ // can exceed the list sizes: add() counts the vectors it skips
3306
+ // (coarse list -1).
3307
+ FAISS_THROW_IF_NOT_MSG(
3308
+ size_t(ivdr->ntotal) >= ivdr->invlists->compute_ntotal(),
3309
+ "IndexIVFRaBitQSSD: the inverted lists hold more than ntotal vectors");
3310
+ } else {
3311
+ FAISS_THROW_IF_NOT_MSG(
3312
+ ivdr->ntotal == 0,
3313
+ "IndexIVFRaBitQSSD: non-empty index needs inverted lists");
3314
+ }
3315
+ idx = std::move(ivdr);
3316
+ }
3317
+ #endif // FAISS_ENABLE_DISK_IVF
3187
3318
  #ifdef FAISS_ENABLE_SVS
3188
3319
  else if (
3189
3320
  h == fourcc("ILVQ") || h == fourcc("ISVL") || h == fourcc("ISVD") ||
@@ -3482,7 +3613,8 @@ static void read_InvertedLists(IndexBinaryIVF& ivf, IOReader* f, int io_flags) {
3482
3613
  auto ils = read_InvertedLists_up(f, io_flags);
3483
3614
  FAISS_THROW_IF_NOT(
3484
3615
  !ils ||
3485
- (ils->nlist == ivf.nlist && ils->code_size == ivf.code_size));
3616
+ (ils->nlist == ivf.nlist &&
3617
+ ils->code_size == static_cast<size_t>(ivf.code_size)));
3486
3618
  ivf.invlists = ils.release();
3487
3619
  ivf.own_invlists = true;
3488
3620
  }
@@ -3593,6 +3725,10 @@ static void read_binary_multi_hash_map(
3593
3725
  int id_bits;
3594
3726
  size_t sz;
3595
3727
  READ1(id_bits);
3728
+ FAISS_THROW_IF_NOT_FMT(
3729
+ id_bits >= 0 && id_bits <= 64,
3730
+ "multi hash map: invalid id_bits=%d (must be in [0, 64])",
3731
+ id_bits);
3596
3732
  READ1(sz);
3597
3733
  FAISS_CHECK_DESERIALIZATION_LOOP_LIMIT(sz, "multi hash map sz");
3598
3734
  std::vector<uint8_t> buf;
@@ -3712,9 +3848,9 @@ std::unique_ptr<IndexBinary> read_index_binary_up(IOReader* f, int io_flags) {
3712
3848
  read_index_binary_header(*idxmap, f);
3713
3849
  idxmap->index = read_index_binary(f, io_flags);
3714
3850
  idxmap->own_fields = true;
3715
- READVECTOR(idxmap->id_map);
3851
+ read_vector(idxmap->id_map, f);
3716
3852
  FAISS_THROW_IF_NOT_FMT(
3717
- idxmap->id_map.size() == idxmap->ntotal,
3853
+ idxmap->id_map.size() == static_cast<size_t>(idxmap->ntotal),
3718
3854
  "IndexBinaryIDMap id_map size (%" PRId64
3719
3855
  ") does not match ntotal (%" PRId64 ")",
3720
3856
  int64_t(idxmap->id_map.size()),
@@ -3738,7 +3874,8 @@ std::unique_ptr<IndexBinary> read_index_binary_up(IOReader* f, int io_flags) {
3738
3874
  "invalid IndexBinaryHash b=%d (must be > 0)",
3739
3875
  idxh->b);
3740
3876
  FAISS_THROW_IF_NOT_FMT(
3741
- static_cast<size_t>(idxh->b) <= idxh->code_size * 8,
3877
+ static_cast<int64_t>(idxh->b) <=
3878
+ static_cast<int64_t>(idxh->code_size) * 8,
3742
3879
  "IndexBinaryHash b=%d exceeds code_size=%d bits",
3743
3880
  idxh->b,
3744
3881
  idxh->code_size);
@@ -31,6 +31,9 @@
31
31
  #include <faiss/IndexIVF.h>
32
32
  #include <faiss/IndexIVFAdditiveQuantizer.h>
33
33
  #include <faiss/IndexIVFAdditiveQuantizerFastScan.h>
34
+ #ifdef FAISS_ENABLE_DISK_IVF
35
+ #include <faiss/IndexIVFRaBitQSSD.h>
36
+ #endif
34
37
  #include <faiss/IndexIVFEDEN.h>
35
38
  #include <faiss/IndexIVFFlat.h>
36
39
  #include <faiss/IndexIVFFlatPanorama.h>
@@ -116,7 +119,8 @@ static void write_index_header(const Index* idx, IOWriter* f) {
116
119
  void write_VectorTransform(const VectorTransform* vt, IOWriter* f) {
117
120
  if (const HadamardRotation* hr =
118
121
  dynamic_cast<const HadamardRotation*>(vt)) {
119
- uint32_t h = fourcc("HRot");
122
+ // HR64: d_out padded to a multiple of 64 rather than a power of 2.
123
+ uint32_t h = fourcc(hr->pad_to_power_of_2 ? "HRot" : "HR64");
120
124
  WRITE1(h);
121
125
  WRITE1(hr->seed);
122
126
  } else if (
@@ -1167,6 +1171,23 @@ void write_index(const Index* idx, IOWriter* f, int io_flags) {
1167
1171
  WRITE1(ivrq->qb);
1168
1172
  write_InvertedLists(ivrq->invlists, f);
1169
1173
  }
1174
+ #ifdef FAISS_ENABLE_DISK_IVF
1175
+ else if (
1176
+ const IndexIVFRaBitQSSD* ivdr =
1177
+ dynamic_cast<const IndexIVFRaBitQSSD*>(idx)) {
1178
+ uint32_t h = fourcc("IwDr");
1179
+ WRITE1(h);
1180
+ write_ivf_header(ivdr, f);
1181
+ write_RaBitQuantizer(&ivdr->rabitq, f, true);
1182
+ WRITE1(ivdr->mem_dim);
1183
+ WRITE1(ivdr->use_hacc);
1184
+ WRITE1(ivdr->drain_alpha);
1185
+ WRITE1(ivdr->qd_per_thread);
1186
+ WRITEVECTOR(ivdr->centroid_norms);
1187
+ WRITEVECTOR(ivdr->centroid_scales);
1188
+ write_InvertedLists(ivdr->invlists, f);
1189
+ }
1190
+ #endif // FAISS_ENABLE_DISK_IVF
1170
1191
  #ifdef FAISS_ENABLE_SVS
1171
1192
  else if (
1172
1193
  const IndexSVSVamana* svs =
@@ -1228,19 +1249,20 @@ void write_index(const Index* idx, IOWriter* f, int io_flags) {
1228
1249
  WRITEVECTOR(svs->stored_vectors);
1229
1250
  }
1230
1251
  } else if (
1231
- const IndexSVSFlat* svs = dynamic_cast<const IndexSVSFlat*>(idx)) {
1252
+ const IndexSVSFlat* svs_flat =
1253
+ dynamic_cast<const IndexSVSFlat*>(idx)) {
1232
1254
  uint32_t h = fourcc("ISVF");
1233
1255
  WRITE1(h);
1234
1256
  write_index_header(idx, f);
1235
1257
 
1236
- bool initialized = (svs->impl != nullptr);
1258
+ bool initialized = (svs_flat->impl != nullptr);
1237
1259
  WRITE1(initialized);
1238
1260
  if (initialized) {
1239
1261
  // Wrap SVS I/O and stream to IOWriter
1240
1262
  faiss::BufferedIOWriter bwr(f);
1241
1263
  faiss::svs_io::WriterStreambuf wbuf(&bwr);
1242
1264
  std::ostream os(&wbuf);
1243
- svs->serialize_impl(os);
1265
+ svs_flat->serialize_impl(os);
1244
1266
  os.flush();
1245
1267
  }
1246
1268
  } else if (
@@ -1398,8 +1420,10 @@ static void write_binary_multi_hash_map(
1398
1420
  int b,
1399
1421
  size_t ntotal,
1400
1422
  IOWriter* f) {
1423
+ // id_bits is also used to store the bucket sizes, which can be equal
1424
+ // to ntotal, so it must be able to represent ntotal itself.
1401
1425
  int id_bits = 0;
1402
- while ((ntotal > (size_t(1) << id_bits))) {
1426
+ while (ntotal >= (size_t(1) << id_bits)) {
1403
1427
  id_bits++;
1404
1428
  }
1405
1429
  WRITE1(id_bits);
@@ -9,6 +9,7 @@
9
9
 
10
10
  #include <algorithm>
11
11
  #include <cassert>
12
+ #include <cerrno>
12
13
  #include <cstring>
13
14
 
14
15
  #include <faiss/impl/FaissAssert.h>
@@ -7,6 +7,8 @@
7
7
 
8
8
  #pragma once
9
9
 
10
+ #include <cerrno>
11
+
10
12
  #include <faiss/impl/maybe_owned_vector.h>
11
13
 
12
14
  /*************************************************************
@@ -73,7 +75,6 @@ size_t get_deserialization_vector_byte_limit();
73
75
  size_t size; \
74
76
  READANDCHECK(&size, 1); \
75
77
  FAISS_THROW_IF_NOT( \
76
- size >= 0 && \
77
78
  size < (faiss::get_deserialization_vector_byte_limit() / \
78
79
  sizeof(*(vec).data()))); \
79
80
  FAISS_THROW_IF_NOT_FMT( \
@@ -122,7 +123,6 @@ size_t get_deserialization_vector_byte_limit();
122
123
  size_t size; \
123
124
  READANDCHECK(&size, 1); \
124
125
  FAISS_THROW_IF_NOT( \
125
- size >= 0 && \
126
126
  size < (faiss::get_deserialization_vector_byte_limit() / \
127
127
  (4 * sizeof(*(vec).data())))); \
128
128
  size *= 4; \
@@ -20,6 +20,7 @@
20
20
 
21
21
  #endif
22
22
 
23
+ #include <cerrno>
23
24
  #include <cstring>
24
25
 
25
26
  #include <faiss/impl/FaissAssert.h>
@@ -295,7 +296,8 @@ size_t MappedFileIOReader::mmap(void** ptr, size_t size, size_t nitems) {
295
296
  actual_size = mmap_owner->size() - pos;
296
297
  }
297
298
 
298
- size_t actual_nitems = (actual_size + size - 1) / size;
299
+ // Whole items only, so that reading a truncated vector fails.
300
+ size_t actual_nitems = actual_size / size;
299
301
  if (actual_nitems == 0) {
300
302
  return 0;
301
303
  }