faiss 0.6.2 → 0.6.4

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 (178) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/lib/faiss/version.rb +1 -1
  4. data/vendor/faiss/faiss/AutoTune.cpp +3 -1
  5. data/vendor/faiss/faiss/Clustering.cpp +9 -1
  6. data/vendor/faiss/faiss/Clustering.h +8 -0
  7. data/vendor/faiss/faiss/IVFlib.cpp +14 -3
  8. data/vendor/faiss/faiss/Index.h +2 -2
  9. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +9 -10
  10. data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +2 -3
  11. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +24 -4
  12. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +16 -145
  13. data/vendor/faiss/faiss/IndexBinaryHNSW.h +0 -6
  14. data/vendor/faiss/faiss/IndexBinaryHash.cpp +5 -9
  15. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +8 -18
  16. data/vendor/faiss/faiss/IndexBinaryIVF.h +8 -1
  17. data/vendor/faiss/faiss/IndexEDEN.cpp +273 -0
  18. data/vendor/faiss/faiss/IndexEDEN.h +57 -0
  19. data/vendor/faiss/faiss/IndexFastScan.cpp +15 -4
  20. data/vendor/faiss/faiss/IndexFlat.cpp +21 -54
  21. data/vendor/faiss/faiss/IndexFlat.h +2 -2
  22. data/vendor/faiss/faiss/IndexHNSW.cpp +311 -102
  23. data/vendor/faiss/faiss/IndexHNSW.h +31 -7
  24. data/vendor/faiss/faiss/IndexIDMap.cpp +26 -8
  25. data/vendor/faiss/faiss/IndexIDMap.h +2 -0
  26. data/vendor/faiss/faiss/IndexIVF.cpp +36 -10
  27. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +1 -1
  28. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +3 -4
  29. data/vendor/faiss/faiss/IndexIVFEDEN.cpp +302 -0
  30. data/vendor/faiss/faiss/IndexIVFEDEN.h +70 -0
  31. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -6
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +3 -4
  33. data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +1 -1
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +49 -23
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +11 -0
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +0 -1
  37. data/vendor/faiss/faiss/IndexIVFRaBitQ.cpp +19 -49
  38. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +180 -76
  39. data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +5 -4
  40. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +8 -6
  41. data/vendor/faiss/faiss/IndexLSH.cpp +2 -3
  42. data/vendor/faiss/faiss/IndexLattice.cpp +5 -0
  43. data/vendor/faiss/faiss/IndexNNDescent.cpp +9 -2
  44. data/vendor/faiss/faiss/IndexNSG.cpp +7 -2
  45. data/vendor/faiss/faiss/IndexPQ.cpp +6 -8
  46. data/vendor/faiss/faiss/IndexPreTransform.cpp +15 -0
  47. data/vendor/faiss/faiss/IndexRaBitQ.cpp +2 -2
  48. data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -2
  49. data/vendor/faiss/faiss/IndexRaBitQFastScan.h +5 -1
  50. data/vendor/faiss/faiss/IndexRefine.cpp +30 -1
  51. data/vendor/faiss/faiss/IndexReplicas.cpp +1 -2
  52. data/vendor/faiss/faiss/IndexShards.cpp +5 -5
  53. data/vendor/faiss/faiss/IndexShardsIVF.cpp +6 -5
  54. data/vendor/faiss/faiss/MetaIndexes.cpp +2 -4
  55. data/vendor/faiss/faiss/SuperKMeans.cpp +286 -247
  56. data/vendor/faiss/faiss/SuperKMeans.h +33 -2
  57. data/vendor/faiss/faiss/VectorTransform.cpp +71 -2
  58. data/vendor/faiss/faiss/VectorTransform.h +3 -0
  59. data/vendor/faiss/faiss/clone_index.cpp +8 -0
  60. data/vendor/faiss/faiss/factory_tools.cpp +47 -4
  61. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +11 -11
  62. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +1 -5
  63. data/vendor/faiss/faiss/gpu/GpuDistance.h +2 -5
  64. data/vendor/faiss/faiss/gpu/GpuIndex.h +38 -16
  65. data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +71 -1
  66. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +17 -0
  67. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +16 -0
  68. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +1 -1
  69. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +2 -2
  70. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +180 -0
  71. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -5
  72. data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +88 -0
  73. data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +134 -0
  74. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +1 -1
  75. data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +7 -4
  76. data/vendor/faiss/faiss/impl/DistanceComputer.h +34 -0
  77. data/vendor/faiss/faiss/impl/EDENQuantizer.h +119 -0
  78. data/vendor/faiss/faiss/impl/HNSW.cpp +528 -267
  79. data/vendor/faiss/faiss/impl/HNSW.h +46 -7
  80. data/vendor/faiss/faiss/impl/IDSelector.h +44 -0
  81. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +2 -2
  82. data/vendor/faiss/faiss/impl/NNDescent.cpp +10 -3
  83. data/vendor/faiss/faiss/impl/NSG.cpp +3 -1
  84. data/vendor/faiss/faiss/impl/Panorama.h +20 -9
  85. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +152 -84
  86. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +38 -26
  87. data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +45 -37
  88. data/vendor/faiss/faiss/impl/RaBitQUtils.h +35 -0
  89. data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +239 -72
  90. data/vendor/faiss/faiss/impl/RaBitQuantizer.h +66 -4
  91. data/vendor/faiss/faiss/impl/RaBitQuantizerMultiBit.cpp +4 -13
  92. data/vendor/faiss/faiss/impl/ResultHandler.h +34 -34
  93. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +287 -84
  94. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +26 -10
  95. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +2 -2
  96. data/vendor/faiss/faiss/impl/VisitedTable.cpp +22 -2
  97. data/vendor/faiss/faiss/impl/VisitedTable.h +20 -0
  98. data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +90 -14
  99. data/vendor/faiss/faiss/impl/binary_hamming/avx2.cpp +4 -4
  100. data/vendor/faiss/faiss/impl/expanded_scanners.h +5 -1
  101. data/vendor/faiss/faiss/impl/fast_scan/decompose_qbs.h +1 -0
  102. data/vendor/faiss/faiss/impl/fast_scan/dispatching.h +35 -2
  103. data/vendor/faiss/faiss/impl/hnsw/LockVector.cpp +1 -1
  104. data/vendor/faiss/faiss/impl/index_read.cpp +491 -50
  105. data/vendor/faiss/faiss/impl/index_write.cpp +86 -30
  106. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +8 -9
  107. data/vendor/faiss/faiss/impl/platform_macros.h +3 -1
  108. data/vendor/faiss/faiss/impl/polysemous_training/avx512.cpp +284 -0
  109. data/vendor/faiss/faiss/impl/polysemous_training/dispatch.h +115 -0
  110. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +73 -39
  111. data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +0 -1
  112. data/vendor/faiss/faiss/impl/pq_code_distance/PQDistanceComputer_impl.h +26 -15
  113. data/vendor/faiss/faiss/impl/pq_code_distance/avx2.cpp +4 -4
  114. data/vendor/faiss/faiss/impl/pq_code_distance/pq_code_distance-generic.cpp +4 -4
  115. data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +195 -0
  116. data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +133 -0
  117. data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +281 -0
  118. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx2.cpp +72 -0
  119. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer-avx512.cpp +228 -0
  120. data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +887 -0
  121. data/vendor/faiss/faiss/impl/scalar_quantizer/distance_computers.h +2 -2
  122. data/vendor/faiss/faiss/impl/scalar_quantizer/quantizers.h +9 -8
  123. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +90 -24
  124. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +30 -30
  125. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +4 -5
  126. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +101 -34
  127. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-dispatch.h +169 -13
  128. data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +125 -26
  129. data/vendor/faiss/faiss/impl/simd_dispatch.h +70 -31
  130. data/vendor/faiss/faiss/index_factory.cpp +40 -7
  131. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  132. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +9 -6
  133. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +29 -8
  134. data/vendor/faiss/faiss/python/python_callbacks.cpp +3 -1
  135. data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +60 -0
  136. data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +26 -1
  137. data/vendor/faiss/faiss/svs/IndexSVSFlat.h +13 -0
  138. data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +1 -1
  139. data/vendor/faiss/faiss/svs/IndexSVSIVFLeanVec.cpp +1 -1
  140. data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +47 -5
  141. data/vendor/faiss/faiss/svs/IndexSVSVamana.h +23 -3
  142. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.cpp +4 -2
  143. data/vendor/faiss/faiss/svs/IndexSVSVamanaLVQ.h +2 -1
  144. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.cpp +10 -4
  145. data/vendor/faiss/faiss/svs/IndexSVSVamanaLeanVec.h +2 -1
  146. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +1 -1
  147. data/vendor/faiss/faiss/utils/distances.cpp +30 -11
  148. data/vendor/faiss/faiss/utils/distances_dispatch.h +30 -24
  149. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +1 -1
  150. data/vendor/faiss/faiss/utils/distances_simd.cpp +4 -3
  151. data/vendor/faiss/faiss/utils/extra_distances.cpp +4 -14
  152. data/vendor/faiss/faiss/utils/extra_distances.h +1 -2
  153. data/vendor/faiss/faiss/utils/hamming.cpp +16 -10
  154. data/vendor/faiss/faiss/utils/hamming.h +10 -1
  155. data/vendor/faiss/faiss/utils/hamming_distance/common.h +14 -3
  156. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_vpopcnt.cpp +24 -0
  157. data/vendor/faiss/faiss/utils/hamming_distance/hamming_computer-avx512.h +1 -1
  158. data/vendor/faiss/faiss/utils/hamming_distance/{hamming_computer-avx512_spr.h → hamming_computer-avx512_vpopcnt.h} +85 -24
  159. data/vendor/faiss/faiss/utils/hamming_distance/hamming_impl.h +141 -0
  160. data/vendor/faiss/faiss/utils/quantize_lut.cpp +29 -8
  161. data/vendor/faiss/faiss/utils/rabitq_simd.h +202 -0
  162. data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +194 -30
  163. data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +0 -1
  164. data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +263 -15
  165. data/vendor/faiss/faiss/utils/simd_impl/distances_rvv.cpp +198 -18
  166. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +245 -0
  167. data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +330 -40
  168. data/vendor/faiss/faiss/utils/simd_impl/{rabitq_avx512_spr.cpp → rabitq_avx512_vpopcnt.cpp} +112 -23
  169. data/vendor/faiss/faiss/utils/simd_impl/rabitq_neon.cpp +11 -0
  170. data/vendor/faiss/faiss/utils/simd_impl/rabitq_rvv.cpp +143 -6
  171. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_dispatch.h +2 -7
  172. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels.h +6 -1
  173. data/vendor/faiss/faiss/utils/simd_impl/super_kmeans_kernels_sve.cpp +34 -0
  174. data/vendor/faiss/faiss/utils/simd_levels.cpp +196 -47
  175. data/vendor/faiss/faiss/utils/simd_levels.h +33 -8
  176. data/vendor/faiss/faiss/utils/utils.cpp +9 -27
  177. metadata +21 -5
  178. data/vendor/faiss/faiss/utils/hamming_distance/hamming_avx512_spr.cpp +0 -15
@@ -28,7 +28,6 @@
28
28
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
29
29
  // NOLINTNEXTLINE(facebook-hte-InlineHeader)
30
30
  #include <faiss/impl/binary_hamming/IndexIVFSpectralHash_impl.h>
31
- #include <faiss/utils/hamming_distance/hamming_computer-generic.h>
32
31
  #undef THE_SIMD_LEVEL
33
32
 
34
33
  namespace faiss {
@@ -88,7 +87,7 @@ void IndexIVFSpectralHash::train_encoder(
88
87
  if (!vt->is_trained) {
89
88
  vt->train(n, x);
90
89
  }
91
- FAISS_THROW_IF_NOT(!by_residual);
90
+ FAISS_THROW_IF_MSG(by_residual, "by_residual not supported for this index");
92
91
 
93
92
  if (threshold_type == Thresh_global) {
94
93
  // nothing to do
@@ -185,7 +184,7 @@ void IndexIVFSpectralHash::encode_vectors(
185
184
  uint8_t* codes,
186
185
  bool include_listnos) const {
187
186
  FAISS_THROW_IF_NOT(is_trained);
188
- FAISS_THROW_IF_NOT(!by_residual);
187
+ FAISS_THROW_IF_MSG(by_residual, "by_residual not supported for this index");
189
188
  float freq = 2.0 / period;
190
189
  size_t coarse_size = include_listnos ? coarse_code_size() : 0;
191
190
 
@@ -222,7 +221,7 @@ InvertedListScanner* IndexIVFSpectralHash::get_InvertedListScanner(
222
221
  bool store_pairs,
223
222
  const IDSelector* sel,
224
223
  const IVFSearchParameters*) const {
225
- FAISS_THROW_IF_NOT(!sel);
224
+ FAISS_THROW_IF_MSG(sel, "id selector not supported for this index");
226
225
  return with_simd_level([&]<SIMDLevel SL>() {
227
226
  return make_spectral_hash_scanner_fixSL<SL>(
228
227
  code_size, this, store_pairs);
@@ -254,8 +253,11 @@ void IndexIVFSpectralHash::replace_vt(IndexPreTransform* encoder, bool own) {
254
253
  auto sub_index = dynamic_cast<IndexLSH*>(encoder->index);
255
254
  FAISS_THROW_IF_NOT_MSG(sub_index, "final index should be LSH");
256
255
  FAISS_THROW_IF_NOT(sub_index->nbits == nbit);
257
- FAISS_THROW_IF_NOT(!sub_index->rotate_data);
258
- FAISS_THROW_IF_NOT(!sub_index->train_thresholds);
256
+ FAISS_THROW_IF_MSG(
257
+ sub_index->rotate_data, "LSH sub-index must not rotate data");
258
+ FAISS_THROW_IF_MSG(
259
+ sub_index->train_thresholds,
260
+ "LSH sub-index thresholds must already be trained");
259
261
  replace_vt(encoder->chain[0], own);
260
262
  }
261
263
 
@@ -119,8 +119,7 @@ void IndexLSH::search(
119
119
  float* distances,
120
120
  idx_t* labels,
121
121
  const SearchParameters* params) const {
122
- FAISS_THROW_IF_NOT_MSG(
123
- !params, "search params not supported for this index");
122
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
124
123
  FAISS_THROW_IF_NOT(k > 0);
125
124
  FAISS_THROW_IF_NOT(is_trained);
126
125
  const float* xt = apply_preprocess(n, x);
@@ -151,7 +150,7 @@ void IndexLSH::transfer_thresholds(LinearTransform* vt) {
151
150
  vt->b.resize(nbits, 0);
152
151
  vt->have_bias = true;
153
152
  }
154
- FAISS_THROW_IF_NOT(!vt->b.empty());
153
+ FAISS_THROW_IF_MSG(vt->b.empty(), "bias vector must not be empty");
155
154
  for (int i = 0; i < nbits; i++) {
156
155
  vt->b[i] -= thresholds[i];
157
156
  }
@@ -26,6 +26,11 @@ IndexLattice::IndexLattice(idx_t d_in, int nsq_in, int scale_nbit_in, int r2)
26
26
  lattice_nbit = 0;
27
27
  while (!(((uint64_t)1 << lattice_nbit) >= zn_sphere_codec.nv)) {
28
28
  lattice_nbit++;
29
+ FAISS_THROW_IF_NOT_FMT(
30
+ lattice_nbit < 64,
31
+ "IndexLattice: nv=%zu too large, lattice code would exceed "
32
+ "63 bits (likely corrupt r2/dsq)",
33
+ (size_t)zn_sphere_codec.nv);
29
34
  }
30
35
 
31
36
  int total_nbit = (lattice_nbit + scale_nbit_in) * nsq_in;
@@ -101,8 +101,7 @@ void IndexNNDescent::search(
101
101
  float* distances,
102
102
  idx_t* labels,
103
103
  const SearchParameters* params) const {
104
- FAISS_THROW_IF_NOT_MSG(
105
- !params, "search params not supported for this index");
104
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
106
105
  FAISS_THROW_IF_NOT_MSG(
107
106
  storage,
108
107
  "Please use IndexNNDescentFlat (or variants) "
@@ -183,11 +182,19 @@ void IndexNNDescent::add(idx_t n, const float* x) {
183
182
 
184
183
  void IndexNNDescent::reset() {
185
184
  nndescent.reset();
185
+ FAISS_THROW_IF_NOT_MSG(
186
+ storage,
187
+ "Please use IndexNNDescentFlat (or variants) "
188
+ "instead of IndexNNDescent directly");
186
189
  storage->reset();
187
190
  ntotal = 0;
188
191
  }
189
192
 
190
193
  void IndexNNDescent::reconstruct(idx_t key, float* recons) const {
194
+ FAISS_THROW_IF_NOT_MSG(
195
+ storage,
196
+ "Please use IndexNNDescentFlat (or variants) "
197
+ "instead of IndexNNDescent directly");
191
198
  storage->reconstruct(key, recons);
192
199
  }
193
200
 
@@ -62,8 +62,7 @@ void IndexNSG::search(
62
62
  float* distances,
63
63
  idx_t* labels,
64
64
  const SearchParameters* params) const {
65
- FAISS_THROW_IF_NOT_MSG(
66
- !params, "search params not supported for this index");
65
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
67
66
  FAISS_THROW_IF_NOT_MSG(
68
67
  storage,
69
68
  "Please use IndexNSGFlat (or variants) instead of IndexNSG directly");
@@ -245,12 +244,18 @@ void IndexNSG::add(idx_t n, const float* x) {
245
244
 
246
245
  void IndexNSG::reset() {
247
246
  nsg.reset();
247
+ FAISS_THROW_IF_NOT_MSG(
248
+ storage,
249
+ "Please use IndexNSGFlat (or variants) instead of IndexNSG directly");
248
250
  storage->reset();
249
251
  ntotal = 0;
250
252
  is_built = false;
251
253
  }
252
254
 
253
255
  void IndexNSG::reconstruct(idx_t key, float* recons) const {
256
+ FAISS_THROW_IF_NOT_MSG(
257
+ storage,
258
+ "Please use IndexNSGFlat (or variants) instead of IndexNSG directly");
254
259
  storage->reconstruct(key, recons);
255
260
  }
256
261
 
@@ -107,7 +107,7 @@ void IndexPQ::search(
107
107
  if (iparams) {
108
108
  params = dynamic_cast<const SearchParametersPQ*>(iparams);
109
109
  FAISS_THROW_IF_NOT_MSG(params, "invalid search params");
110
- FAISS_THROW_IF_NOT_MSG(!params->sel, "selector not supported");
110
+ FAISS_THROW_IF_MSG(params->sel, "selector not supported");
111
111
  param_search_type = params->search_type;
112
112
  }
113
113
 
@@ -459,7 +459,7 @@ struct SortedArray {
459
459
 
460
460
  void init(const T* x_2) {
461
461
  this->x = x_2;
462
- FAISS_THROW_IF_NOT(!perm.empty());
462
+ FAISS_THROW_IF_MSG(perm.empty(), "permutation array must not be empty");
463
463
  for (int n = 0; n < N; n++) {
464
464
  perm[n] = n;
465
465
  }
@@ -543,7 +543,7 @@ struct SemiSortedArray {
543
543
 
544
544
  void init(const T* x_2) {
545
545
  this->x = x_2;
546
- FAISS_THROW_IF_NOT(!perm.empty());
546
+ FAISS_THROW_IF_MSG(perm.empty(), "permutation array must not be empty");
547
547
  for (int n = 0; n < N; n++) {
548
548
  perm[n] = n;
549
549
  }
@@ -665,7 +665,7 @@ struct MinSumK {
665
665
 
666
666
  void mark_seen(int64_t i) {
667
667
  if (use_seen) {
668
- FAISS_THROW_IF_NOT(!seen.empty());
668
+ FAISS_THROW_IF_MSG(seen.empty(), "seen bitmap must not be empty");
669
669
  seen[i >> 3] |= 1 << (i & 7);
670
670
  }
671
671
  }
@@ -795,8 +795,7 @@ void MultiIndexQuantizer::search(
795
795
  float* distances,
796
796
  idx_t* labels,
797
797
  const SearchParameters* params) const {
798
- FAISS_THROW_IF_NOT_MSG(
799
- !params, "search params not supported for this index");
798
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
800
799
  if (n == 0) {
801
800
  return;
802
801
  }
@@ -943,8 +942,7 @@ void MultiIndexQuantizer2::search(
943
942
  float* distances,
944
943
  idx_t* labels,
945
944
  const SearchParameters* params) const {
946
- FAISS_THROW_IF_NOT_MSG(
947
- !params, "search params not supported for this index");
945
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
948
946
 
949
947
  if (n == 0) {
950
948
  return;
@@ -229,6 +229,7 @@ size_t IndexPreTransform::remove_ids(const IDSelector& sel) {
229
229
  }
230
230
 
231
231
  void IndexPreTransform::reconstruct(idx_t key, float* recons) const {
232
+ FAISS_THROW_IF_NOT_MSG(index, "IndexPreTransform: null sub-index");
232
233
  float* x = chain.empty() ? recons : new float[index->d];
233
234
  std::unique_ptr<float[]> del(recons == x ? nullptr : x);
234
235
  // Initial reconstruction
@@ -239,6 +240,7 @@ void IndexPreTransform::reconstruct(idx_t key, float* recons) const {
239
240
  }
240
241
 
241
242
  void IndexPreTransform::reconstruct_n(idx_t i0, idx_t ni, float* recons) const {
243
+ FAISS_THROW_IF_NOT_MSG(index, "IndexPreTransform: null sub-index");
242
244
  float* x = chain.empty() ? recons : new float[ni * index->d];
243
245
  std::unique_ptr<float[]> del(recons == x ? nullptr : x);
244
246
  // Initial reconstruction
@@ -345,6 +347,19 @@ struct PreTransformDistanceComputer : DistanceComputer {
345
347
  float operator()(idx_t i) override {
346
348
  return (*sub_dc)(i);
347
349
  }
350
+
351
+ void distances_batch_4(
352
+ const idx_t idx0,
353
+ const idx_t idx1,
354
+ const idx_t idx2,
355
+ const idx_t idx3,
356
+ float& dis0,
357
+ float& dis1,
358
+ float& dis2,
359
+ float& dis3) override {
360
+ sub_dc->distances_batch_4(
361
+ idx0, idx1, idx2, idx3, dis0, dis1, dis2, dis3);
362
+ }
348
363
  };
349
364
 
350
365
  } // anonymous namespace
@@ -122,8 +122,8 @@ struct Run_search_with_dc_res {
122
122
  // RaBitQuantizer.cpp for details.
123
123
  auto* dc = dynamic_cast<RaBitQDistanceComputer*>(
124
124
  dc_base.get());
125
- FAISS_THROW_IF_NOT_MSG(
126
- dc != nullptr,
125
+ FAISS_THROW_IF_MSG(
126
+ dc == nullptr,
127
127
  "Failed to cast to RaBitQDistanceComputer for two-stage search");
128
128
 
129
129
  bool is_similarity =
@@ -512,8 +512,7 @@ void IndexRaBitQFastScan::search(
512
512
  float* distances,
513
513
  idx_t* labels,
514
514
  const SearchParameters* params) const {
515
- FAISS_THROW_IF_NOT_MSG(
516
- !params, "search params not supported for this index");
515
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
517
516
 
518
517
  // Create query factors array on stack - memory managed by caller
519
518
  std::vector<rabitq_utils::QueryFactorsData> query_factors_storage(n);
@@ -209,8 +209,12 @@ struct RaBitQHeapHandler
209
209
  : 0;
210
210
 
211
211
  const size_t block_idx = base_db_idx / rabitq_index->bbs;
212
+ // aux_base points at this 32-lane sub-block's factors: the bbs block
213
+ // base plus the loop-invariant intra-block offset
214
+ // ((base_db_idx % bbs)). Only the per-element i term varies below.
212
215
  const uint8_t* aux_base = rabitq_index->codes.get() +
213
- block_idx * full_block_size + packed_block_size;
216
+ block_idx * full_block_size + packed_block_size +
217
+ (base_db_idx % rabitq_index->bbs) * storage_size;
214
218
 
215
219
  for (size_t i = 0; i < max_vectors; i++) {
216
220
  const size_t db_idx = base_db_idx + i;
@@ -156,7 +156,17 @@ void IndexRefine::range_search(
156
156
  SearchParameters* base_index_params =
157
157
  (params != nullptr) ? params->base_index_params : nullptr;
158
158
 
159
- base_index->range_search(n, x, radius, result, base_index_params);
159
+ const float kf = (params != nullptr) ? params->k_factor : this->k_factor;
160
+
161
+ const bool is_similarity = is_similarity_metric(metric_type);
162
+
163
+ // Scale the base_index search radius by k_factor. Results are filtered to
164
+ // the exact radius below, so this only affects recall, not correctness; it
165
+ // is up to the user to pick a k_factor that widens the base search for
166
+ // their metric.
167
+ const float base_radius = radius * kf;
168
+
169
+ base_index->range_search(n, x, base_radius, result, base_index_params);
160
170
 
161
171
  #pragma omp parallel if (n > 1)
162
172
  {
@@ -177,6 +187,25 @@ void IndexRefine::range_search(
177
187
  }
178
188
  }
179
189
  }
190
+
191
+ // Exact filtering done here, as base index's approximations
192
+ // can fall outside the specified exact radius.
193
+ const std::vector<size_t> prev_lims(result->lims, result->lims + n + 1);
194
+ size_t wp = 0;
195
+ for (idx_t i = 0; i < n; i++) {
196
+ for (size_t j = prev_lims[i]; j < prev_lims[i + 1]; j++) {
197
+ const float dis = result->distances[j];
198
+ const bool within = is_similarity
199
+ ? CMin<float, idx_t>::cmp(radius, dis)
200
+ : CMax<float, idx_t>::cmp(radius, dis);
201
+ if (within) {
202
+ result->labels[wp] = result->labels[j];
203
+ result->distances[wp] = result->distances[j];
204
+ wp++;
205
+ }
206
+ }
207
+ result->lims[i + 1] = wp;
208
+ }
180
209
  }
181
210
 
182
211
  void IndexRefine::reconstruct(idx_t key, float* recons) const {
@@ -127,8 +127,7 @@ void IndexReplicasTemplate<IndexT>::search(
127
127
  distance_t* distances,
128
128
  idx_t* labels,
129
129
  const SearchParameters* params) const {
130
- FAISS_THROW_IF_NOT_MSG(
131
- !params, "search params not supported for this index");
130
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
132
131
  FAISS_THROW_IF_NOT(k > 0);
133
132
  FAISS_THROW_IF_NOT_MSG(this->count() > 0, "no replicas in index");
134
133
 
@@ -143,8 +143,8 @@ void IndexShardsTemplate<IndexT>::add_with_ids(
143
143
  "request them to be shifted");
144
144
 
145
145
  if (successive_ids) {
146
- FAISS_THROW_IF_NOT_MSG(
147
- !xids,
146
+ FAISS_THROW_IF_MSG(
147
+ xids,
148
148
  "It makes no sense to pass in ids and "
149
149
  "request them to be shifted");
150
150
  FAISS_THROW_IF_NOT_MSG(
@@ -243,8 +243,8 @@ void IndexShardsTemplate<IndexT>::search(
243
243
 
244
244
  this->runOnIndex(fn);
245
245
 
246
- if (this->metric_type == METRIC_L2) {
247
- merge_knn_results<idx_t, CMin<distance_t, int>>(
246
+ if (is_similarity_metric(this->metric_type)) {
247
+ merge_knn_results<idx_t, CMax<distance_t, int>>(
248
248
  n,
249
249
  k,
250
250
  nshard,
@@ -253,7 +253,7 @@ void IndexShardsTemplate<IndexT>::search(
253
253
  distances,
254
254
  labels);
255
255
  } else {
256
- merge_knn_results<idx_t, CMax<distance_t, int>>(
256
+ merge_knn_results<idx_t, CMin<distance_t, int>>(
257
257
  n,
258
258
  k,
259
259
  nshard,
@@ -13,6 +13,7 @@
13
13
  #include <cstdio>
14
14
  #include <functional>
15
15
 
16
+ #include <faiss/MetricType.h>
16
17
  #include <faiss/impl/FaissAssert.h>
17
18
  #include <faiss/utils/Heap.h>
18
19
  #include <faiss/utils/WorkerThread.h>
@@ -106,8 +107,8 @@ void IndexShardsIVF::add_with_ids(
106
107
  "request them to be shifted");
107
108
 
108
109
  if (successive_ids) {
109
- FAISS_THROW_IF_NOT_MSG(
110
- !xids,
110
+ FAISS_THROW_IF_MSG(
111
+ xids,
111
112
  "It makes no sense to pass in ids and "
112
113
  "request them to be shifted");
113
114
  FAISS_THROW_IF_NOT_MSG(
@@ -227,8 +228,8 @@ void IndexShardsIVF::search(
227
228
 
228
229
  this->runOnIndex(fn);
229
230
 
230
- if (this->metric_type == METRIC_L2) {
231
- merge_knn_results<idx_t, CMin<distance_t, int>>(
231
+ if (is_similarity_metric(metric_type)) {
232
+ merge_knn_results<idx_t, CMax<distance_t, int>>(
232
233
  n,
233
234
  k,
234
235
  nshard,
@@ -237,7 +238,7 @@ void IndexShardsIVF::search(
237
238
  distances,
238
239
  labels);
239
240
  } else {
240
- merge_knn_results<idx_t, CMax<distance_t, int>>(
241
+ merge_knn_results<idx_t, CMin<distance_t, int>>(
241
242
  n,
242
243
  k,
243
244
  nshard,
@@ -63,8 +63,7 @@ void IndexSplitVectors::search(
63
63
  float* distances,
64
64
  idx_t* labels,
65
65
  const SearchParameters* params) const {
66
- FAISS_THROW_IF_NOT_MSG(
67
- !params, "search params not supported for this index");
66
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
68
67
  FAISS_THROW_IF_NOT_MSG(k == 1, "search implemented only for k=1");
69
68
  FAISS_THROW_IF_NOT_MSG(
70
69
  sum_d == d, "not enough indexes compared to # dimensions");
@@ -187,8 +186,7 @@ void IndexRandom::search(
187
186
  float* distances,
188
187
  idx_t* labels,
189
188
  const SearchParameters* params) const {
190
- FAISS_THROW_IF_NOT_MSG(
191
- !params, "search params not supported for this index");
189
+ FAISS_THROW_IF_MSG(params, "search params not supported for this index");
192
190
  FAISS_THROW_IF_NOT(k <= ntotal);
193
191
  #pragma omp parallel for if (n > 1000)
194
192
  for (idx_t i = 0; i < n; i++) {