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
@@ -497,7 +497,6 @@ void IndexIVF::search(
497
497
  #pragma omp parallel for if (nt > 1)
498
498
  for (idx_t slice = 0; slice < nt; slice++) {
499
499
  try {
500
- IndexIVFStats local_stats;
501
500
  idx_t i0 = n * slice / nt;
502
501
  idx_t i1 = n * (slice + 1) / nt;
503
502
  if (i1 > i0) {
@@ -516,13 +515,17 @@ void IndexIVF::search(
516
515
  omp_rethrow_if_exception(ex);
517
516
 
518
517
  // collect stats
518
+ IndexIVFStats search_stats;
519
519
  for (idx_t slice = 0; slice < nt; slice++) {
520
- indexIVF_stats.add(stats[slice]);
520
+ search_stats.add(stats[slice]);
521
521
  }
522
+ indexIVF_stats.add(search_stats);
522
523
  } else {
523
524
  // handle parallelization at level below (or don't run in parallel at
524
525
  // all)
525
- sub_search_func(n, x, distances, labels, &indexIVF_stats);
526
+ IndexIVFStats search_stats;
527
+ sub_search_func(n, x, distances, labels, &search_stats);
528
+ indexIVF_stats.add(search_stats);
526
529
  }
527
530
  }
528
531
 
@@ -617,6 +620,10 @@ void IndexIVF::search_preassigned(
617
620
  try {
618
621
  std::unique_ptr<InvertedListScanner> scanner(
619
622
  get_InvertedListScanner(store_pairs, sel, params));
623
+ // max_codes counts codes passing the selector, so test it first.
624
+ scanner->sel_after_distance = params &&
625
+ params->sel_after_distance &&
626
+ cur_max_codes == unlimited_list_size;
620
627
 
621
628
  /*****************************************************
622
629
  * Depending on parallel_mode, there are two possible ways
@@ -886,13 +893,7 @@ void IndexIVF::search_preassigned(
886
893
 
887
894
  omp_rethrow_if_exception(ex);
888
895
 
889
- if (ivf_stats == nullptr) {
890
- ivf_stats = &indexIVF_stats;
891
- }
892
- ivf_stats->nq += n;
893
- ivf_stats->nlist += nlistv;
894
- ivf_stats->ndis += ndis;
895
- ivf_stats->nheap_updates += nheap;
896
+ add_search_stats(n, nlistv, ndis, nheap, ivf_stats);
896
897
  }
897
898
 
898
899
  void IndexIVF::range_search(
@@ -918,23 +919,15 @@ void IndexIVF::range_search(
918
919
  double t0 = getmillisecs();
919
920
  quantizer->search(
920
921
  nx, x, cur_nprobe, coarse_dis.get(), keys.get(), quantizer_params);
921
- indexIVF_stats.quantization_time += getmillisecs() - t0;
922
+ double quantization_time = getmillisecs() - t0;
922
923
 
923
924
  t0 = getmillisecs();
924
925
  invlists->prefetch_lists(keys.get(), static_cast<int>(nx * cur_nprobe));
925
926
 
926
927
  range_search_preassigned(
927
- nx,
928
- x,
929
- radius,
930
- keys.get(),
931
- coarse_dis.get(),
932
- result,
933
- false,
934
- params,
935
- &indexIVF_stats);
928
+ nx, x, radius, keys.get(), coarse_dis.get(), result, false, params);
936
929
 
937
- indexIVF_stats.search_time += getmillisecs() - t0;
930
+ add_search_times(quantization_time, getmillisecs() - t0);
938
931
  }
939
932
 
940
933
  void IndexIVF::range_search_preassigned(
@@ -1115,12 +1108,7 @@ void IndexIVF::range_search_preassigned(
1115
1108
 
1116
1109
  omp_rethrow_if_exception(ex);
1117
1110
 
1118
- if (stats == nullptr) {
1119
- stats = &indexIVF_stats;
1120
- }
1121
- stats->nq += nx;
1122
- stats->nlist += nlistv;
1123
- stats->ndis += ndis;
1111
+ add_search_stats(nx, nlistv, ndis, 0, stats);
1124
1112
  }
1125
1113
 
1126
1114
  void IndexIVF::search1(
@@ -1143,7 +1131,7 @@ void IndexIVF::search1(
1143
1131
  double t0 = getmillisecs();
1144
1132
  quantizer->search(
1145
1133
  nx, x, cur_nprobe, coarse_dis.get(), keys.get(), quantizer_params);
1146
- indexIVF_stats.quantization_time += getmillisecs() - t0;
1134
+ double quantization_time = getmillisecs() - t0;
1147
1135
 
1148
1136
  t0 = getmillisecs();
1149
1137
  invlists->prefetch_lists(keys.get(), static_cast<int>(nx * cur_nprobe));
@@ -1171,7 +1159,7 @@ void IndexIVF::search1(
1171
1159
  scanner->scan_codes(list_size, scodes.get(), ids.get(), handler);
1172
1160
  }
1173
1161
 
1174
- indexIVF_stats.search_time += getmillisecs() - t0;
1162
+ add_search_times(quantization_time, getmillisecs() - t0);
1175
1163
  }
1176
1164
 
1177
1165
  InvertedListScanner* IndexIVF::get_InvertedListScanner(
@@ -1273,7 +1261,13 @@ void IndexIVF::search_and_reconstruct(
1273
1261
  std::unique_ptr<idx_t[]> idx(new idx_t[n * cur_nprobe]);
1274
1262
  std::unique_ptr<float[]> coarse_dis(new float[n * cur_nprobe]);
1275
1263
 
1276
- quantizer->search(n, x, cur_nprobe, coarse_dis.get(), idx.get());
1264
+ quantizer->search(
1265
+ n,
1266
+ x,
1267
+ cur_nprobe,
1268
+ coarse_dis.get(),
1269
+ idx.get(),
1270
+ params ? params->quantizer_params : nullptr);
1277
1271
 
1278
1272
  invlists->prefetch_lists(idx.get(), static_cast<int>(n * cur_nprobe));
1279
1273
 
@@ -1322,6 +1316,9 @@ void IndexIVF::search_and_return_codes(
1322
1316
  params = dynamic_cast<const IVFSearchParameters*>(params_in);
1323
1317
  FAISS_THROW_IF_NOT_MSG(params, "IndexIVF params have incorrect type");
1324
1318
  }
1319
+ FAISS_THROW_IF_MSG(
1320
+ invlists->code_size == InvertedLists::INVALID_CODE_SIZE,
1321
+ "search_and_return_codes not supported for these inverted lists");
1325
1322
  const size_t cur_nprobe =
1326
1323
  std::min(nlist, params ? params->nprobe : this->nprobe);
1327
1324
  FAISS_THROW_IF_NOT(cur_nprobe > 0);
@@ -1329,7 +1326,13 @@ void IndexIVF::search_and_return_codes(
1329
1326
  std::unique_ptr<idx_t[]> idx(new idx_t[n * cur_nprobe]);
1330
1327
  std::unique_ptr<float[]> coarse_dis(new float[n * cur_nprobe]);
1331
1328
 
1332
- quantizer->search(n, x, cur_nprobe, coarse_dis.get(), idx.get());
1329
+ quantizer->search(
1330
+ n,
1331
+ x,
1332
+ cur_nprobe,
1333
+ coarse_dis.get(),
1334
+ idx.get(),
1335
+ params ? params->quantizer_params : nullptr);
1333
1336
 
1334
1337
  invlists->prefetch_lists(idx.get(), static_cast<int>(n * cur_nprobe));
1335
1338
 
@@ -1670,16 +1673,44 @@ void IndexIVFStats::reset() {
1670
1673
  }
1671
1674
 
1672
1675
  void IndexIVFStats::add(const IndexIVFStats& other) {
1673
- nq += other.nq;
1674
- nlist += other.nlist;
1675
- ndis += other.ndis;
1676
- nheap_updates += other.nheap_updates;
1677
- quantization_time += other.quantization_time;
1678
- search_time += other.search_time;
1676
+ detail::atomic_fetch_add_relaxed(nq, other.nq);
1677
+ detail::atomic_fetch_add_relaxed(nlist, other.nlist);
1678
+ detail::atomic_fetch_add_relaxed(ndis, other.ndis);
1679
+ detail::atomic_fetch_add_relaxed(nheap_updates, other.nheap_updates);
1680
+ detail::atomic_fetch_add_relaxed(
1681
+ quantization_time, other.quantization_time);
1682
+ detail::atomic_fetch_add_relaxed(search_time, other.search_time);
1679
1683
  }
1680
1684
 
1681
1685
  IndexIVFStats indexIVF_stats;
1682
1686
 
1687
+ void add_search_stats(
1688
+ size_t nq,
1689
+ size_t nlist,
1690
+ size_t ndis,
1691
+ size_t nheap_updates,
1692
+ IndexIVFStats* stats) {
1693
+ if (stats == nullptr) {
1694
+ stats = &indexIVF_stats;
1695
+ }
1696
+ detail::atomic_fetch_add_relaxed(stats->nq, nq);
1697
+ detail::atomic_fetch_add_relaxed(stats->nlist, nlist);
1698
+ detail::atomic_fetch_add_relaxed(stats->ndis, ndis);
1699
+ detail::atomic_fetch_add_relaxed(stats->nheap_updates, nheap_updates);
1700
+ }
1701
+
1702
+ void add_search_times(
1703
+ double quantization_time,
1704
+ double search_time,
1705
+ IndexIVFStats* stats) {
1706
+ if (stats == nullptr) {
1707
+ stats = &indexIVF_stats;
1708
+ }
1709
+ detail::atomic_fetch_add_relaxed(
1710
+ stats->quantization_time, quantization_time);
1711
+ detail::atomic_fetch_add_relaxed(stats->search_time, search_time);
1712
+ }
1713
+
1683
1714
  /*************************************************************************
1684
1715
  * InvertedListScanner
1685
1716
  *************************************************************************/
@@ -115,6 +115,20 @@ struct SearchParametersIVF : SearchParameters {
115
115
  /// uses implementation 10 for this option.
116
116
  size_t max_empty_result_buckets = 0;
117
117
 
118
+ /// k-NN only: test the IDSelector only on codes whose distance would enter
119
+ /// the result heap. Faster when the selector costs more than a distance
120
+ /// and accepts most codes; slower when it rejects most codes.
121
+ ///
122
+ /// is_member must return the same answer for an id during the whole
123
+ /// search. Then the results are the same with or without this option, so
124
+ /// a scan that does not implement it ignores it. With it,
125
+ /// IndexIVFStats::ndis counts every scanned code.
126
+ ///
127
+ /// Only the batched IVF-SQ scan implements it, and only with unlimited
128
+ /// max_codes. The uniform, TurboQuant and 0-bit SQ types and the other
129
+ /// IVF indexes test the selector first.
130
+ bool sel_after_distance = false;
131
+
118
132
  SearchParameters* quantizer_params = nullptr;
119
133
  /// context object to pass to InvertedLists
120
134
  void* inverted_list_context = nullptr;
@@ -456,7 +470,7 @@ struct IndexIVF : Index, IndexIVFInterface {
456
470
  * include the list ids in the code (in this case add
457
471
  * ceil(log8(nlist)) to the code size)
458
472
  */
459
- void search_and_return_codes(
473
+ virtual void search_and_return_codes(
460
474
  idx_t n,
461
475
  const float* x,
462
476
  idx_t k,
@@ -549,6 +563,9 @@ struct InvertedListScanner {
549
563
  /// search in this subset of ids
550
564
  const IDSelector* sel;
551
565
 
566
+ /// see SearchParametersIVF::sel_after_distance
567
+ bool sel_after_distance = false;
568
+
552
569
  InvertedListScanner(
553
570
  bool store_pairs_in = false,
554
571
  const IDSelector* sel_in = nullptr)
@@ -644,12 +661,31 @@ struct IndexIVFStats {
644
661
  reset();
645
662
  }
646
663
  void reset();
664
+ /** Adds the counters of other to this one. The adds are atomic, so
665
+ * several threads can add to the same IndexIVFStats at once. This is
666
+ * what makes concurrent searches safe on the global indexIVF_stats. */
647
667
  void add(const IndexIVFStats& other);
648
668
  };
649
669
 
650
670
  // global var that collects them all
651
671
  FAISS_API extern IndexIVFStats indexIVF_stats;
652
672
 
673
+ /** Adds the counters of one search call to stats, or to indexIVF_stats if
674
+ * stats is null. The adds are atomic, as in IndexIVFStats::add. */
675
+ void add_search_stats(
676
+ size_t nq,
677
+ size_t nlist,
678
+ size_t ndis,
679
+ size_t nheap_updates = 0,
680
+ IndexIVFStats* stats = nullptr);
681
+
682
+ /** Adds the times of one search call to stats, or to indexIVF_stats if
683
+ * stats is null. The adds are atomic, as in IndexIVFStats::add. */
684
+ void add_search_times(
685
+ double quantization_time,
686
+ double search_time,
687
+ IndexIVFStats* stats = nullptr);
688
+
653
689
  } // namespace faiss
654
690
 
655
691
  #endif
@@ -722,9 +722,7 @@ void IndexIVFFastScan::search_dispatch_implem(
722
722
  }
723
723
  }
724
724
  }
725
- indexIVF_stats.nq += n;
726
- indexIVF_stats.ndis += ndis;
727
- indexIVF_stats.nlist += nlist_visited;
725
+ add_search_stats(n, nlist_visited, ndis);
728
726
  } else {
729
727
  FAISS_THROW_FMT("implem %d does not exist", implem);
730
728
  }
@@ -869,9 +867,7 @@ void IndexIVFFastScan::range_search_dispatch_implem(
869
867
  }
870
868
  }
871
869
 
872
- indexIVF_stats.nq += n;
873
- indexIVF_stats.ndis += ndis;
874
- indexIVF_stats.nlist += nlist_visited;
870
+ add_search_stats(n, nlist_visited, ndis);
875
871
  }
876
872
 
877
873
  template <class C>
@@ -940,9 +936,7 @@ void IndexIVFFastScan::search_implem_1(
940
936
  }
941
937
  heap_reorder<C>(k, heap_dis, heap_ids);
942
938
  }
943
- indexIVF_stats.nq += n;
944
- indexIVF_stats.ndis += ndis;
945
- indexIVF_stats.nlist += nlist_visited;
939
+ add_search_stats(n, nlist_visited, ndis);
946
940
  }
947
941
 
948
942
  template <class C>
@@ -1026,9 +1020,7 @@ void IndexIVFFastScan::search_implem_2(
1026
1020
  }
1027
1021
  }
1028
1022
  }
1029
- indexIVF_stats.nq += n;
1030
- indexIVF_stats.ndis += ndis;
1031
- indexIVF_stats.nlist += nlist_visited;
1023
+ add_search_stats(n, nlist_visited, ndis);
1032
1024
  }
1033
1025
 
1034
1026
  void IndexIVFFastScan::search_implem_10(
@@ -1597,9 +1589,7 @@ void IndexIVFFastScan::search_implem_14(
1597
1589
  }
1598
1590
  }
1599
1591
 
1600
- indexIVF_stats.nq += n;
1601
- indexIVF_stats.ndis += ndis;
1602
- indexIVF_stats.nlist += nlist_visited;
1592
+ add_search_stats(n, nlist_visited, ndis);
1603
1593
  }
1604
1594
 
1605
1595
  void IndexIVFFastScan::reconstruct_from_offset(
@@ -78,10 +78,10 @@ struct IVFFlatScanner : InvertedListScanner {
78
78
  using C = typename VectorDistance::C;
79
79
 
80
80
  IVFFlatScanner(
81
- const VectorDistance& vd,
82
- bool store_pairs,
83
- const IDSelector* sel)
84
- : InvertedListScanner(store_pairs, sel), vd(vd) {
81
+ const VectorDistance& vd_in,
82
+ bool store_pairs_in,
83
+ const IDSelector* sel_in)
84
+ : InvertedListScanner(store_pairs_in, sel_in), vd(vd_in) {
85
85
  keep_max = vd.is_similarity;
86
86
  code_size = vd.d * sizeof(float);
87
87
  }
@@ -91,8 +91,8 @@ struct IVFFlatScanner : InvertedListScanner {
91
91
  this->xi = query;
92
92
  }
93
93
 
94
- void set_list(idx_t list_no, float /* coarse_dis */) override {
95
- this->list_no = list_no;
94
+ void set_list(idx_t list_no_in, float /* coarse_dis */) override {
95
+ list_no = list_no_in;
96
96
  }
97
97
 
98
98
  float distance_to_code(const uint8_t* code) const final;
@@ -368,7 +368,7 @@ struct IVFPQFastScanScanner : InvertedListScanner {
368
368
  // implemented for all vector distances, although only L2 and IP are
369
369
  // supported by FastScan
370
370
  with_VectorDistance(pq.dsub, index.metric_type, 0.0, [&](auto vd) {
371
- int m;
371
+ size_t m;
372
372
  for (m = 0; m + 1 < pq.M; m += 2) {
373
373
  const float* cent;
374
374
  uint8_t c = *code++;
@@ -35,6 +35,7 @@ using rabitq_utils::QueryFactorsData;
35
35
  using rabitq_utils::round_nonnegative_to_uint16;
36
36
  using rabitq_utils::SignBitFactors;
37
37
  using rabitq_utils::SignBitFactorsWithError;
38
+ using rabitq_utils::write_subset_sum_lut;
38
39
 
39
40
  inline size_t roundup(size_t a, size_t b) {
40
41
  return (a + b - 1) / b * b;
@@ -368,32 +369,6 @@ bool IndexIVFRaBitQFastScan::lookup_table_is_3d() const {
368
369
  return true;
369
370
  }
370
371
 
371
- // out[code] = base + sum of v_i for each set bit in code.
372
- inline void write_subset_sum_lut(
373
- float* out,
374
- float base,
375
- float v0,
376
- float v1,
377
- float v2,
378
- float v3) {
379
- out[0] = base;
380
- out[1] = base + v0;
381
- out[2] = base + v1;
382
- out[3] = base + v0 + v1;
383
- out[4] = base + v2;
384
- out[5] = base + v0 + v2;
385
- out[6] = base + v1 + v2;
386
- out[7] = base + v0 + v1 + v2;
387
- out[8] = base + v3;
388
- out[9] = base + v0 + v3;
389
- out[10] = base + v1 + v3;
390
- out[11] = base + v0 + v1 + v3;
391
- out[12] = base + v2 + v3;
392
- out[13] = base + v0 + v2 + v3;
393
- out[14] = base + v1 + v2 + v3;
394
- out[15] = base + v0 + v1 + v2 + v3;
395
- }
396
-
397
372
  // Computes lookup table for residual vectors in RaBitQ FastScan format
398
373
  void IndexIVFRaBitQFastScan::compute_residual_LUT(
399
374
  const float* query,
@@ -753,6 +728,28 @@ void IndexIVFRaBitQFastScan::compute_LUT_uint8(
753
728
  }
754
729
  }
755
730
 
731
+ namespace {
732
+
733
+ /// Decode a FastScan code to a RaBitQ residual vector of dimension d.
734
+ void decode_fastscan_to_residual(
735
+ size_t d,
736
+ const uint8_t* fastscan_code,
737
+ float* residual,
738
+ float dp_multiplier) {
739
+ memset(residual, 0, sizeof(float) * d);
740
+
741
+ const float inv_d_sqrt = (d == 0) ? 1.0f : (1.0f / std::sqrt((float)d));
742
+
743
+ for (size_t j = 0; j < d; j++) {
744
+ bool bit_value = rabitq_utils::extract_bit_fastscan(fastscan_code, j);
745
+
746
+ float bit_as_float = bit_value ? 1.0f : 0.0f;
747
+ residual[j] = (bit_as_float - 0.5f) * dp_multiplier * 2 * inv_d_sqrt;
748
+ }
749
+ }
750
+
751
+ } // namespace
752
+
756
753
  void IndexIVFRaBitQFastScan::reconstruct_from_offset(
757
754
  int64_t list_no,
758
755
  int64_t offset,
@@ -800,7 +797,7 @@ void IndexIVFRaBitQFastScan::reconstruct_from_offset(
800
797
  // Decode residual directly using dp_multiplier
801
798
  std::vector<float> residual(d);
802
799
  decode_fastscan_to_residual(
803
- fastscan_code.data(), residual.data(), dp_multiplier);
800
+ d, fastscan_code.data(), residual.data(), dp_multiplier);
804
801
 
805
802
  // Reconstruct: x = centroid + residual
806
803
  for (size_t j = 0; j < static_cast<size_t>(d); j++) {
@@ -838,7 +835,10 @@ void IndexIVFRaBitQFastScan::sa_decode(idx_t n, const uint8_t* bytes, float* x)
838
835
  *reinterpret_cast<const SignBitFactors*>(factors_ptr);
839
836
 
840
837
  decode_fastscan_to_residual(
841
- fastscan_code, residual.data(), base_factors.dp_multiplier);
838
+ d,
839
+ fastscan_code,
840
+ residual.data(),
841
+ base_factors.dp_multiplier);
842
842
 
843
843
  for (size_t j = 0; j < static_cast<size_t>(d); j++) {
844
844
  x_i[j] = centroid[j] + residual[j];
@@ -849,22 +849,6 @@ void IndexIVFRaBitQFastScan::sa_decode(idx_t n, const uint8_t* bytes, float* x)
849
849
  }
850
850
  }
851
851
 
852
- void IndexIVFRaBitQFastScan::decode_fastscan_to_residual(
853
- const uint8_t* fastscan_code,
854
- float* residual,
855
- float dp_multiplier) const {
856
- memset(residual, 0, sizeof(float) * d);
857
-
858
- const float inv_d_sqrt = (d == 0) ? 1.0f : (1.0f / std::sqrt((float)d));
859
-
860
- for (size_t j = 0; j < static_cast<size_t>(d); j++) {
861
- bool bit_value = rabitq_utils::extract_bit_fastscan(fastscan_code, j);
862
-
863
- float bit_as_float = bit_value ? 1.0f : 0.0f;
864
- residual[j] = (bit_as_float - 0.5f) * dp_multiplier * 2 * inv_d_sqrt;
865
- }
866
- }
867
-
868
852
  std::unique_ptr<FastScanCodeScanner> IndexIVFRaBitQFastScan::make_knn_scanner(
869
853
  bool is_max,
870
854
  idx_t n,
@@ -135,15 +135,6 @@ struct IndexIVFRaBitQFastScan : IndexIVFFastScan {
135
135
  std::vector<uint8_t>& rotated_qq,
136
136
  float* rotated_q_out) const;
137
137
 
138
- private:
139
- /// Decode FastScan code to RaBitQ residual vector with explicit
140
- /// dp_multiplier
141
- void decode_fastscan_to_residual(
142
- const uint8_t* fastscan_code,
143
- float* residual,
144
- float dp_multiplier) const;
145
-
146
- public:
147
138
  /// Implementation methods for IVFRaBitQFastScan specialization
148
139
  bool lookup_table_is_3d() const override;
149
140
 
@@ -205,10 +196,10 @@ IVFRaBitQHeapHandler<C, SL>::IVFRaBitQHeapHandler(
205
196
  size_t k_val,
206
197
  float* distances,
207
198
  int64_t* labels,
208
- const IDSelector* sel,
199
+ const IDSelector* sel_in,
209
200
  const FastScanDistancePostProcessing* ctx,
210
201
  bool multibit)
211
- : ResultHandlerCompare<C, true, SL>(nq_val, 0, sel),
202
+ : ResultHandlerCompare<C, true, SL>(nq_val, 0, sel_in),
212
203
  index(idx),
213
204
  heap_distances(distances),
214
205
  heap_labels(labels),