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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7ff5a38fc140c730f7dfca4bbbfa2e2e4980fd63bf9f069ebbb9eb8deb4d50c
4
- data.tar.gz: 919723a22c26f62267c9be7e34250ef7a30a5da682679634dd64471eef5187c4
3
+ metadata.gz: 63159143b567318220567ff9005d0e186d430be9982ca82cb34d722917974c7c
4
+ data.tar.gz: aef8b8c9871639565f7b94806dea2d3252c00c0826153330650edebb30e93cfa
5
5
  SHA512:
6
- metadata.gz: 28e32877ea44804c5f73fcae9b8df462a024c56198156c0928a7ac2405d8cb4263e266bcc1fba2f88c41e3d62ba626fc55a4a228fe06e2d4e24421bb6e02b61b
7
- data.tar.gz: 56cfa780d1d5361e3b43937c1b392a26528e4f3bd8166b0945c9cda63b32942e7001b7703e5705ba158a70ed92da69ef6983d1bbacd94362a9167403874aeb1e
6
+ metadata.gz: 1070fcb421036b5bdac863bfb10f4d2d90382a3aa0181f5dc0e0a3809b7b60a487770d8259d471f415c88bf3c47914d46ccf9468136e71e9f0b27e489ed018b7
7
+ data.tar.gz: 4b40070716f9692cdeb813ffcf385bd0f157cb6667f6963bbec6b82fe071160dea2beb24a78b9381661c7b67d6c7d1ad0728649e6f232ae934033fd10ecc0d4f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.6.6 (2026-12-04)
2
+
3
+ - Updated Faiss to 1.16.1
4
+ - Added `CVar` module
5
+
1
6
  ## 0.6.5 (2026-11-10)
2
7
 
3
8
  - Updated Faiss to 1.16.0
data/ext/faiss/extconf.rb CHANGED
@@ -34,6 +34,7 @@ vendor = File.expand_path("../../vendor/faiss", __dir__)
34
34
 
35
35
  $srcs = Dir["{#{ext},#{vendor}/faiss,#{vendor}/faiss/{impl,invlists,utils}/**}/*.{cpp}"]
36
36
  $srcs.reject! { |v| v.match?(/avx2|avx512|neon|sve|rvv|simdlib_based|distances_aarch64/) }
37
+ $srcs.reject! { |v| v.match?(/IndexIVFRaBitQSSD|DiskIOBackend|RaBitQSSDInvertedLists/) }
37
38
  $objs = $srcs.map { |v| v.sub(/cpp\z/, "o") }
38
39
  abort "Faiss not found" unless find_header("faiss/Index.h", vendor)
39
40
  $VPATH << vendor
data/ext/faiss/index.cpp CHANGED
@@ -300,4 +300,46 @@ void init_index(Rice::Module& m) {
300
300
  std::ranges::copy(self.id_map, ids.write_ptr());
301
301
  return ids;
302
302
  });
303
+
304
+ Rice::define_module_under(m, "CVar")
305
+ .define_singleton_function(
306
+ "distance_compute_blas_threshold",
307
+ []() {
308
+ return faiss::distance_compute_blas_threshold;
309
+ })
310
+ .define_singleton_function(
311
+ "distance_compute_blas_threshold=",
312
+ [](int value) {
313
+ return faiss::distance_compute_blas_threshold = value;
314
+ })
315
+ .define_singleton_function(
316
+ "distance_compute_blas_query_bs",
317
+ []() {
318
+ return faiss::distance_compute_blas_query_bs;
319
+ })
320
+ .define_singleton_function(
321
+ "distance_compute_blas_query_bs=",
322
+ [](int value) {
323
+ return faiss::distance_compute_blas_query_bs = value;
324
+ })
325
+ .define_singleton_function(
326
+ "distance_compute_blas_database_bs",
327
+ []() {
328
+ return faiss::distance_compute_blas_database_bs;
329
+ })
330
+ .define_singleton_function(
331
+ "distance_compute_blas_database_bs=",
332
+ [](int value) {
333
+ return faiss::distance_compute_blas_database_bs = value;
334
+ })
335
+ .define_singleton_function(
336
+ "distance_compute_min_k_reservoir",
337
+ []() {
338
+ return faiss::distance_compute_min_k_reservoir;
339
+ })
340
+ .define_singleton_function(
341
+ "distance_compute_min_k_reservoir=",
342
+ [](int value) {
343
+ return faiss::distance_compute_min_k_reservoir = value;
344
+ });
303
345
  }
data/lib/faiss/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Faiss
2
- VERSION = "0.6.5"
2
+ VERSION = "0.6.6"
3
3
  end
@@ -660,35 +660,58 @@ int64_t DefaultShardingFunction::operator()(int64_t i, int64_t shard_count) {
660
660
  return i % shard_count;
661
661
  }
662
662
 
663
+ namespace {
664
+
665
+ /// Returns the IndexIVF of an index that shard_ivf_index_centroids accepts:
666
+ /// an IndexIVF, or an IndexPreTransform whose sub-index is an IndexIVF.
667
+ /// Returns nullptr for any other index.
668
+ IndexIVF* get_shardable_ivf(Index* index) {
669
+ auto* pt = dynamic_cast<IndexPreTransform*>(index);
670
+ return dynamic_cast<IndexIVF*>(pt ? pt->index : index);
671
+ }
672
+
673
+ template <typename QuantizerType>
674
+ void check_sharding_args(
675
+ const QuantizerType* quantizer,
676
+ const std::string& filename_template) {
677
+ FAISS_THROW_IF_MSG(quantizer->ntotal == 0, "No centroids to shard.");
678
+ FAISS_THROW_IF_MSG(
679
+ filename_template.find("%d") == std::string::npos,
680
+ "Invalid filename_template. Must contain format specifier for shard count.");
681
+ }
682
+
683
+ } // namespace
684
+
663
685
  void handle_ivf(
664
- faiss::IndexIVF* index,
686
+ faiss::Index* index,
665
687
  int64_t shard_count,
666
688
  const std::string& filename_template,
667
689
  ShardingFunction* sharding_function,
668
690
  bool generate_ids) {
691
+ const faiss::Index* quantizer = get_shardable_ivf(index)->quantizer;
669
692
  // assign centroids to each sharded Index based on sharding_function, and
670
693
  // add them to the quantizer of each sharded index
671
694
  std::vector<std::vector<float>> sharded_centroids(shard_count);
672
695
  std::vector<std::vector<idx_t>> xids(shard_count);
673
- for (int64_t i = 0; i < index->quantizer->ntotal; i++) {
696
+ for (int64_t i = 0; i < quantizer->ntotal; i++) {
674
697
  int64_t shard_id = (*sharding_function)(i, shard_count);
675
698
  // Since the quantizer does not natively support add_with_ids, we simply
676
699
  // generate them.
677
700
  xids[shard_id].push_back(i);
678
- float* reconstructed = new float[index->quantizer->d];
679
- index->quantizer->reconstruct(i, reconstructed);
701
+ float* reconstructed = new float[quantizer->d];
702
+ quantizer->reconstruct(i, reconstructed);
680
703
  sharded_centroids[shard_id].insert(
681
704
  sharded_centroids[shard_id].end(),
682
705
  &reconstructed[0],
683
- &reconstructed[index->quantizer->d]);
706
+ &reconstructed[quantizer->d]);
684
707
  delete[] reconstructed;
685
708
  }
686
709
 
687
- auto clone = static_cast<faiss::IndexIVF*>(faiss::clone_index(index));
688
- clone->quantizer->reset();
710
+ std::unique_ptr<faiss::Index> clone(faiss::clone_index(index));
711
+ get_shardable_ivf(clone.get())->quantizer->reset();
689
712
  for (int64_t i = 0; i < shard_count; i++) {
690
- auto sharded_index =
691
- static_cast<faiss::IndexIVF*>(faiss::clone_index(clone));
713
+ std::unique_ptr<faiss::Index> sharded(faiss::clone_index(clone.get()));
714
+ faiss::IndexIVF* sharded_index = get_shardable_ivf(sharded.get());
692
715
  if (generate_ids) {
693
716
  // Assume the quantizer does not natively support add_with_ids.
694
717
  // The IDMap wraps the cloned quantizer; own_fields makes it delete
@@ -698,12 +721,12 @@ void handle_ivf(
698
721
  id_quantizer->own_fields = true;
699
722
  sharded_index->quantizer = id_quantizer;
700
723
  sharded_index->quantizer->add_with_ids(
701
- sharded_centroids[i].size() / index->quantizer->d,
724
+ sharded_centroids[i].size() / quantizer->d,
702
725
  sharded_centroids[i].data(),
703
726
  xids[i].data());
704
727
  } else {
705
728
  sharded_index->quantizer->add(
706
- sharded_centroids[i].size() / index->quantizer->d,
729
+ sharded_centroids[i].size() / quantizer->d,
707
730
  sharded_centroids[i].data());
708
731
  }
709
732
  char fname[256];
@@ -715,10 +738,8 @@ void handle_ivf(
715
738
  #if defined(__GNUC__) || defined(__clang__)
716
739
  #pragma GCC diagnostic pop
717
740
  #endif
718
- faiss::write_index(sharded_index, fname);
719
- delete sharded_index;
741
+ faiss::write_index(sharded.get(), fname);
720
742
  }
721
- delete clone;
722
743
  }
723
744
 
724
745
  void handle_binary_ivf(
@@ -785,51 +806,24 @@ void handle_binary_ivf(
785
806
  delete clone;
786
807
  }
787
808
 
788
- template <typename IndexType>
789
- void sharding_helper(
790
- IndexType* index,
809
+ void shard_ivf_index_centroids(
810
+ faiss::Index* index,
791
811
  int64_t shard_count,
792
812
  const std::string& filename_template,
793
813
  ShardingFunction* sharding_function,
794
814
  bool generate_ids) {
795
- FAISS_THROW_IF_MSG(index->quantizer->ntotal == 0, "No centroids to shard.");
815
+ const faiss::IndexIVF* index_ivf = get_shardable_ivf(index);
796
816
  FAISS_THROW_IF_MSG(
797
- filename_template.find("%d") == std::string::npos,
798
- "Invalid filename_template. Must contain format specifier for shard count.");
817
+ index_ivf == nullptr,
818
+ "index must be an IndexIVF, or an IndexPreTransform whose sub-index is an IndexIVF.");
819
+ check_sharding_args(index_ivf->quantizer, filename_template);
799
820
 
800
821
  DefaultShardingFunction default_sharding_function;
801
- if (sharding_function == nullptr) {
802
- sharding_function = &default_sharding_function;
803
- }
804
-
805
- if (typeid(IndexType) == typeid(faiss::IndexIVF)) {
806
- handle_ivf(
807
- dynamic_cast<faiss::IndexIVF*>(index),
808
- shard_count,
809
- filename_template,
810
- sharding_function,
811
- generate_ids);
812
- } else if (typeid(IndexType) == typeid(faiss::IndexBinaryIVF)) {
813
- handle_binary_ivf(
814
- dynamic_cast<faiss::IndexBinaryIVF*>(index),
815
- shard_count,
816
- filename_template,
817
- sharding_function,
818
- generate_ids);
819
- }
820
- }
821
-
822
- void shard_ivf_index_centroids(
823
- faiss::IndexIVF* index,
824
- int64_t shard_count,
825
- const std::string& filename_template,
826
- ShardingFunction* sharding_function,
827
- bool generate_ids) {
828
- sharding_helper(
822
+ handle_ivf(
829
823
  index,
830
824
  shard_count,
831
825
  filename_template,
832
- sharding_function,
826
+ sharding_function ? sharding_function : &default_sharding_function,
833
827
  generate_ids);
834
828
  }
835
829
 
@@ -839,11 +833,14 @@ void shard_binary_ivf_index_centroids(
839
833
  const std::string& filename_template,
840
834
  ShardingFunction* sharding_function,
841
835
  bool generate_ids) {
842
- sharding_helper(
836
+ check_sharding_args(index->quantizer, filename_template);
837
+
838
+ DefaultShardingFunction default_sharding_function;
839
+ handle_binary_ivf(
843
840
  index,
844
841
  shard_count,
845
842
  filename_template,
846
- sharding_function,
843
+ sharding_function ? sharding_function : &default_sharding_function,
847
844
  generate_ids);
848
845
  }
849
846
 
@@ -201,7 +201,11 @@ struct DefaultShardingFunction : ShardingFunction {
201
201
  * the index to the path given by filename_generator. The centroids must already
202
202
  * be added to the index quantizer.
203
203
  *
204
- * @param index The IVF index containing centroids to shard.
204
+ * @param index The IVF index containing centroids to shard: an
205
+ * IndexIVF, or an IndexPreTransform whose sub-index
206
+ * is an IndexIVF. Each shard of an IndexPreTransform
207
+ * is an IndexPreTransform with a copy of the
208
+ * transform chain.
205
209
  * @param shard_count Number of shards.
206
210
  * @param filename_template Template for shard filenames.
207
211
  * @param sharding_function The function to shard by. The default is ith vector
@@ -211,7 +215,7 @@ struct DefaultShardingFunction : ShardingFunction {
211
215
  * @return The number of shards written.
212
216
  */
213
217
  void shard_ivf_index_centroids(
214
- IndexIVF* index,
218
+ Index* index,
215
219
  int64_t shard_count = 20,
216
220
  const std::string& filename_template = "shard.%d.index",
217
221
  ShardingFunction* sharding_function = nullptr,
@@ -27,11 +27,11 @@ void Index::train(idx_t /*n*/, const float* /*x*/) {
27
27
  // does nothing by default
28
28
  }
29
29
 
30
- void Index::train(const gpu::Dataset&) {
30
+ void Index::train_dataset(const gpu::Dataset&) {
31
31
  FAISS_THROW_MSG("Dataset training not implemented for this type of index");
32
32
  }
33
33
 
34
- void Index::add(const gpu::Dataset&) {
34
+ void Index::add_dataset(const gpu::Dataset&) {
35
35
  FAISS_THROW_MSG("Dataset addition not implemented for this type of index");
36
36
  }
37
37
 
@@ -17,7 +17,7 @@
17
17
 
18
18
  #define FAISS_VERSION_MAJOR 1
19
19
  #define FAISS_VERSION_MINOR 16
20
- #define FAISS_VERSION_PATCH 0
20
+ #define FAISS_VERSION_PATCH 1
21
21
 
22
22
  // Macro to combine the version components into a single string
23
23
  #ifndef FAISS_STRINGIFY
@@ -136,7 +136,7 @@ struct Index {
136
136
  virtual void train(idx_t n, const float* x);
137
137
 
138
138
  /// Train from caller-controlled storage. Unsupported by default.
139
- virtual void train(const gpu::Dataset& dataset);
139
+ virtual void train_dataset(const gpu::Dataset& dataset);
140
140
 
141
141
  /** Perform training on a representative set of vectors and a representative
142
142
  * set of queries
@@ -171,7 +171,7 @@ struct Index {
171
171
  virtual void add(idx_t n, const float* x) = 0;
172
172
 
173
173
  /// Add caller-controlled storage. Unsupported by default.
174
- virtual void add(const gpu::Dataset& dataset);
174
+ virtual void add_dataset(const gpu::Dataset& dataset);
175
175
 
176
176
  virtual void add_ex(idx_t n, const void* x, NumericType numeric_type) {
177
177
  if (numeric_type == NumericType::Float32) {
@@ -29,11 +29,11 @@ void IndexBinary::train(idx_t, const uint8_t*) {
29
29
  // Does nothing by default.
30
30
  }
31
31
 
32
- void IndexBinary::train(const gpu::Dataset&) {
32
+ void IndexBinary::train_dataset(const gpu::Dataset&) {
33
33
  FAISS_THROW_MSG("Dataset training not implemented for this type of index");
34
34
  }
35
35
 
36
- void IndexBinary::add(const gpu::Dataset&) {
36
+ void IndexBinary::add_dataset(const gpu::Dataset&) {
37
37
  FAISS_THROW_MSG("Dataset addition not implemented for this type of index");
38
38
  }
39
39
 
@@ -54,7 +54,7 @@ struct IndexBinary {
54
54
  virtual void train(idx_t n, const uint8_t* x);
55
55
 
56
56
  /// Train from caller-controlled storage. Unsupported by default.
57
- virtual void train(const gpu::Dataset& dataset);
57
+ virtual void train_dataset(const gpu::Dataset& dataset);
58
58
 
59
59
  virtual void train_ex(idx_t n, const void* x, NumericType numeric_type) {
60
60
  if (numeric_type == NumericType::UInt8) {
@@ -72,7 +72,7 @@ struct IndexBinary {
72
72
  virtual void add(idx_t n, const uint8_t* x) = 0;
73
73
 
74
74
  /// Add caller-controlled storage. Unsupported by default.
75
- virtual void add(const gpu::Dataset& dataset);
75
+ virtual void add_dataset(const gpu::Dataset& dataset);
76
76
 
77
77
  virtual void add_ex(idx_t n, const void* x, NumericType numeric_type) {
78
78
  if (numeric_type == NumericType::UInt8) {
@@ -100,6 +100,7 @@ size_t IndexBinaryFlat::remove_ids(const IDSelector& sel) {
100
100
  }
101
101
 
102
102
  void IndexBinaryFlat::reconstruct(idx_t key, uint8_t* recons) const {
103
+ FAISS_THROW_IF_NOT(key >= 0 && key < ntotal);
103
104
  memcpy(recons, &(xb[code_size * key]), sizeof(*recons) * code_size);
104
105
  }
105
106
 
@@ -184,9 +184,12 @@ struct BinaryLinkSample : HNSWLinkSampleSearch {
184
184
  const IndexBinaryHNSW& index;
185
185
  const uint8_t* x;
186
186
 
187
- BinaryLinkSample(const IndexBinaryHNSW& index, idx_t n, const uint8_t* x)
188
- : index(index), x(x) {
189
- this->n = n;
187
+ BinaryLinkSample(
188
+ const IndexBinaryHNSW& index_in,
189
+ idx_t n_in,
190
+ const uint8_t* x_in)
191
+ : index(index_in), x(x_in) {
192
+ n = n_in;
190
193
  }
191
194
 
192
195
  std::vector<uint8_t> gather(size_t m, const idx_t* ids) const {
@@ -157,7 +157,7 @@ void IndexBinaryIVF::search(
157
157
  coarse_dis.get(),
158
158
  idx.get(),
159
159
  params ? params->quantizer_params : nullptr);
160
- indexIVF_stats.quantization_time += getmillisecs() - t0;
160
+ double quantization_time = getmillisecs() - t0;
161
161
 
162
162
  t0 = getmillisecs();
163
163
  invlists->prefetch_lists(idx.get(), static_cast<int>(n * nprobe_));
@@ -172,7 +172,7 @@ void IndexBinaryIVF::search(
172
172
  labels,
173
173
  false,
174
174
  params);
175
- indexIVF_stats.search_time += getmillisecs() - t0;
175
+ add_search_times(quantization_time, getmillisecs() - t0);
176
176
  }
177
177
 
178
178
  void IndexBinaryIVF::reconstruct(idx_t key, uint8_t* recons) const {
@@ -483,10 +483,7 @@ void search_knn_hamming_heap(
483
483
  } // parallel for
484
484
  } // parallel
485
485
 
486
- indexIVF_stats.nq += n;
487
- indexIVF_stats.nlist += nlistv;
488
- indexIVF_stats.ndis += ndis;
489
- indexIVF_stats.nheap_updates += nheap;
486
+ add_search_stats(n, nlistv, ndis, nheap);
490
487
  }
491
488
 
492
489
  } // anonymous namespace
@@ -556,14 +553,14 @@ void IndexBinaryIVF::range_search(
556
553
 
557
554
  double t0 = getmillisecs();
558
555
  quantizer->search(n, x, nprobe_2, coarse_dis.get(), idx.get());
559
- indexIVF_stats.quantization_time += getmillisecs() - t0;
556
+ double quantization_time = getmillisecs() - t0;
560
557
 
561
558
  t0 = getmillisecs();
562
559
  invlists->prefetch_lists(idx.get(), static_cast<int>(n * nprobe_2));
563
560
 
564
561
  range_search_preassigned(n, x, radius, idx.get(), coarse_dis.get(), res);
565
562
 
566
- indexIVF_stats.search_time += getmillisecs() - t0;
563
+ add_search_times(quantization_time, getmillisecs() - t0);
567
564
  }
568
565
 
569
566
  void IndexBinaryIVF::range_search_preassigned(
@@ -628,9 +625,7 @@ void IndexBinaryIVF::range_search_preassigned(
628
625
 
629
626
  pres.finalize();
630
627
  }
631
- indexIVF_stats.nq += n;
632
- indexIVF_stats.nlist += nlistv;
633
- indexIVF_stats.ndis += ndis;
628
+ add_search_stats(n, nlistv, ndis);
634
629
  }
635
630
 
636
631
  IndexBinaryIVF::~IndexBinaryIVF() {
@@ -194,7 +194,7 @@ void estimators_from_tables_generic(
194
194
  }
195
195
 
196
196
  if (nscale) {
197
- for (size_t m = 0; m < nscale; m++) {
197
+ for (int m = 0; m < nscale; m++) {
198
198
  uint64_t c = bsr.read(static_cast<int>(index.nbits));
199
199
  dis += dt[c] * context.pq2x4_scale;
200
200
  dt += index.ksub;
@@ -316,7 +316,7 @@ FlatCodesDistanceComputer* IndexFlat::get_FlatCodesDistanceComputer() const {
316
316
  }
317
317
 
318
318
  void IndexFlat::reconstruct(idx_t key, float* recons) const {
319
- FAISS_THROW_IF_NOT(key < ntotal);
319
+ FAISS_THROW_IF_NOT(key >= 0 && key < ntotal);
320
320
  memcpy(recons, &(codes[key * code_size]), code_size);
321
321
  }
322
322
 
@@ -1097,13 +1097,16 @@ struct IndexHNSWLinkSample : HNSWLinkSampleSearch {
1097
1097
  const Index* codec;
1098
1098
 
1099
1099
  IndexHNSWLinkSample(
1100
- const IndexHNSW& index,
1101
- idx_t n,
1102
- const uint8_t* x,
1103
- NumericType numeric_type,
1104
- const Index* codec)
1105
- : index(index), x(x), numeric_type(numeric_type), codec(codec) {
1106
- this->n = n;
1100
+ const IndexHNSW& index_in,
1101
+ idx_t n_in,
1102
+ const uint8_t* x_in,
1103
+ NumericType numeric_type_in,
1104
+ const Index* codec_in)
1105
+ : index(index_in),
1106
+ x(x_in),
1107
+ numeric_type(numeric_type_in),
1108
+ codec(codec_in) {
1109
+ n = n_in;
1107
1110
  }
1108
1111
 
1109
1112
  size_t line_size() const {
@@ -1451,11 +1454,11 @@ IndexRaBitQ* make_hnsw_rabitq_storage(
1451
1454
  } // namespace
1452
1455
 
1453
1456
  IndexHNSWRaBitQ::IndexHNSWRaBitQ(
1454
- int d,
1457
+ int d_in,
1455
1458
  int M,
1456
1459
  uint8_t nb_bits,
1457
1460
  MetricType metric)
1458
- : IndexHNSW(make_hnsw_rabitq_storage(d, nb_bits, metric), M) {
1461
+ : IndexHNSW(make_hnsw_rabitq_storage(d_in, nb_bits, metric), M) {
1459
1462
  own_fields = true;
1460
1463
  is_trained = storage->is_trained;
1461
1464
  // 1-bit codes store plain SignBitFactors with no f_error, so there is no
@@ -1828,7 +1831,6 @@ void IndexHNSWCagra::range_search(
1828
1831
  }
1829
1832
 
1830
1833
  auto run = [&]<class C>() {
1831
- const HNSW& hnsw = this->hnsw;
1832
1834
  size_t n1 = 0, n2 = 0, ndis = 0, nhops = 0;
1833
1835
  RangeSearchPartialResult pres(result);
1834
1836
 
@@ -269,12 +269,9 @@ struct IndexHNSWRaBitQ : IndexHNSW {
269
269
 
270
270
  IndexHNSWRaBitQ& operator=(const IndexHNSWRaBitQ&) = delete;
271
271
 
272
- private:
273
- // clone_index() replaces the shallow-copied storage with a deep copy
274
- // before returning it. Keep ordinary C++ copies from sharing ownership.
275
- IndexHNSWRaBitQ(const IndexHNSWRaBitQ&) = default;
276
272
  #ifndef SWIG
277
- friend IndexHNSW* clone_IndexHNSW(const IndexHNSW* index);
273
+ // The copy shares the storage. clone_index() replaces it with a deep copy.
274
+ IndexHNSWRaBitQ(const IndexHNSWRaBitQ&) = default;
278
275
  #endif
279
276
  };
280
277
 
@@ -97,13 +97,15 @@ void IndexIDMapTemplate<IndexT>::train(
97
97
  }
98
98
 
99
99
  template <typename IndexT>
100
- void IndexIDMapTemplate<IndexT>::train(const gpu::Dataset& dataset) {
101
- index->train(dataset);
100
+ void IndexIDMapTemplate<IndexT>::train_dataset(const gpu::Dataset& dataset) {
101
+ index->train_dataset(dataset);
102
102
  this->is_trained = index->is_trained;
103
103
  }
104
104
 
105
105
  template <typename IndexT>
106
106
  void IndexIDMapTemplate<IndexT>::reset() {
107
+ FAISS_THROW_IF_NOT_MSG(
108
+ id_map.is_owned, "reset is not supported on a viewed id_map");
107
109
  index->reset();
108
110
  id_map.clear();
109
111
  this->ntotal = 0;
@@ -116,6 +118,9 @@ void IndexIDMapTemplate<IndexT>::add_with_ids_ex(
116
118
  const void* x,
117
119
  NumericType numeric_type,
118
120
  const idx_t* xids) {
121
+ FAISS_THROW_IF_NOT_MSG(
122
+ id_map.is_owned,
123
+ "add_with_ids is not supported on a viewed id_map");
119
124
  index->add_ex(n, x, numeric_type);
120
125
  for (idx_t i = 0; i < n; i++) {
121
126
  id_map.push_back(xids[i]);
@@ -139,9 +144,12 @@ template <typename IndexT>
139
144
  void IndexIDMapTemplate<IndexT>::add_with_ids(
140
145
  const gpu::Dataset& dataset,
141
146
  const idx_t* xids) {
142
- index->add(dataset);
147
+ FAISS_THROW_IF_NOT_MSG(
148
+ id_map.is_owned,
149
+ "add_with_ids is not supported on a viewed id_map");
150
+ index->add_dataset(dataset);
143
151
  FAISS_THROW_IF_NOT(index->ntotal >= this->ntotal);
144
- // CAGRA may already have built the graph during train(dataset).
152
+ // CAGRA may already have built the graph during train_dataset(dataset).
145
153
  for (idx_t i = 0; i < index->ntotal - this->ntotal; i++) {
146
154
  id_map.push_back(xids[i]);
147
155
  }
@@ -159,6 +167,9 @@ void IndexIDMapTemplate<IndexT>::add_sa_codes(
159
167
  idx_t n,
160
168
  const uint8_t* codes,
161
169
  const idx_t* xids) {
170
+ FAISS_THROW_IF_NOT_MSG(
171
+ id_map.is_owned,
172
+ "add_sa_codes is not supported on a viewed id_map");
162
173
  // don't pass the ids to the sub-index, they are kept in id_map
163
174
  index->add_sa_codes(n, codes, nullptr);
164
175
  for (idx_t i = 0; i < n; i++) {
@@ -276,6 +287,8 @@ void IndexIDMapTemplate<IndexT>::range_search(
276
287
 
277
288
  template <typename IndexT>
278
289
  size_t IndexIDMapTemplate<IndexT>::remove_ids(const IDSelector& sel) {
290
+ FAISS_THROW_IF_NOT_MSG(
291
+ id_map.is_owned, "remove_ids is not supported on a viewed id_map");
279
292
  // remove in sub-index first
280
293
  IDSelectorTranslated sel2(id_map, &sel);
281
294
  size_t nremove = index->remove_ids(sel2);
@@ -10,6 +10,7 @@
10
10
  #include <faiss/Index.h>
11
11
  #include <faiss/IndexBinary.h>
12
12
  #include <faiss/impl/IDSelector.h>
13
+ #include <faiss/impl/maybe_owned_vector.h>
13
14
 
14
15
  #include <unordered_map>
15
16
  #include <vector>
@@ -24,7 +25,7 @@ struct IndexIDMapTemplate : IndexT {
24
25
 
25
26
  IndexT* index = nullptr; ///! the sub-index
26
27
  bool own_fields = false; ///! whether pointers are deleted in destructor
27
- std::vector<idx_t> id_map;
28
+ MaybeOwnedVector<idx_t> id_map;
28
29
 
29
30
  explicit IndexIDMapTemplate(IndexT* index);
30
31
 
@@ -38,7 +39,7 @@ struct IndexIDMapTemplate : IndexT {
38
39
  const idx_t* xids) override;
39
40
 
40
41
  /// Add a Dataset and assign one xids entry per unmapped vector, including
41
- /// vectors already built by train(dataset).
42
+ /// vectors already built by train_dataset(dataset).
42
43
  virtual void add_with_ids(const gpu::Dataset& dataset, const idx_t* xids);
43
44
 
44
45
  /// this will fail. Use add_with_ids
@@ -62,7 +63,7 @@ struct IndexIDMapTemplate : IndexT {
62
63
  const SearchParameters* params = nullptr) const override;
63
64
 
64
65
  void train(idx_t n, const component_t* x) override;
65
- void train(const gpu::Dataset& dataset) override;
66
+ void train_dataset(const gpu::Dataset& dataset) override;
66
67
  void train_ex(idx_t n, const void* x, NumericType numeric_type) override;
67
68
 
68
69
  void reset() override;
@@ -137,14 +138,18 @@ using IndexBinaryIDMap2 = IndexIDMap2Template<IndexBinary>;
137
138
 
138
139
  // IDSelector that translates the ids using an IDMap
139
140
  struct IDSelectorTranslated : IDSelector {
140
- const std::vector<int64_t>& id_map;
141
+ const MaybeOwnedVector<idx_t>& id_map;
141
142
  const IDSelector* sel;
142
143
 
143
144
  IDSelectorTranslated(
144
- const std::vector<int64_t>& id_map_in,
145
+ const MaybeOwnedVector<idx_t>& id_map_in,
145
146
  const IDSelector* sel_in)
146
147
  : id_map(id_map_in), sel(sel_in) {}
147
148
 
149
+ IDSelectorTranslated(
150
+ MaybeOwnedVector<idx_t>&& id_map_in,
151
+ const IDSelector* sel_in) = delete;
152
+
148
153
  IDSelectorTranslated(
149
154
  IndexBinaryIDMap& index_idmap,
150
155
  const IDSelector* sel_in)