faiss 0.2.0 → 0.2.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 (215) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +7 -7
  5. data/ext/faiss/extconf.rb +6 -3
  6. data/ext/faiss/numo.hpp +4 -4
  7. data/ext/faiss/utils.cpp +1 -1
  8. data/ext/faiss/utils.h +1 -1
  9. data/lib/faiss/version.rb +1 -1
  10. data/vendor/faiss/faiss/AutoTune.cpp +292 -291
  11. data/vendor/faiss/faiss/AutoTune.h +55 -56
  12. data/vendor/faiss/faiss/Clustering.cpp +365 -194
  13. data/vendor/faiss/faiss/Clustering.h +102 -35
  14. data/vendor/faiss/faiss/IVFlib.cpp +171 -195
  15. data/vendor/faiss/faiss/IVFlib.h +48 -51
  16. data/vendor/faiss/faiss/Index.cpp +85 -103
  17. data/vendor/faiss/faiss/Index.h +54 -48
  18. data/vendor/faiss/faiss/Index2Layer.cpp +126 -224
  19. data/vendor/faiss/faiss/Index2Layer.h +22 -36
  20. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +407 -0
  21. data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +195 -0
  22. data/vendor/faiss/faiss/IndexBinary.cpp +45 -37
  23. data/vendor/faiss/faiss/IndexBinary.h +140 -132
  24. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +73 -53
  25. data/vendor/faiss/faiss/IndexBinaryFlat.h +29 -24
  26. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +46 -43
  27. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +16 -15
  28. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +215 -232
  29. data/vendor/faiss/faiss/IndexBinaryHNSW.h +25 -24
  30. data/vendor/faiss/faiss/IndexBinaryHash.cpp +182 -177
  31. data/vendor/faiss/faiss/IndexBinaryHash.h +41 -34
  32. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +489 -461
  33. data/vendor/faiss/faiss/IndexBinaryIVF.h +97 -68
  34. data/vendor/faiss/faiss/IndexFlat.cpp +115 -176
  35. data/vendor/faiss/faiss/IndexFlat.h +42 -59
  36. data/vendor/faiss/faiss/IndexFlatCodes.cpp +67 -0
  37. data/vendor/faiss/faiss/IndexFlatCodes.h +47 -0
  38. data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
  39. data/vendor/faiss/faiss/IndexHNSW.h +57 -41
  40. data/vendor/faiss/faiss/IndexIVF.cpp +545 -453
  41. data/vendor/faiss/faiss/IndexIVF.h +169 -118
  42. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +316 -0
  43. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +121 -0
  44. data/vendor/faiss/faiss/IndexIVFFlat.cpp +247 -252
  45. data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
  46. data/vendor/faiss/faiss/IndexIVFPQ.cpp +459 -517
  47. data/vendor/faiss/faiss/IndexIVFPQ.h +75 -67
  48. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +406 -372
  49. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +82 -57
  50. data/vendor/faiss/faiss/IndexIVFPQR.cpp +104 -102
  51. data/vendor/faiss/faiss/IndexIVFPQR.h +33 -28
  52. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +163 -150
  53. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +38 -25
  54. data/vendor/faiss/faiss/IndexLSH.cpp +66 -113
  55. data/vendor/faiss/faiss/IndexLSH.h +20 -38
  56. data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
  57. data/vendor/faiss/faiss/IndexLattice.h +11 -16
  58. data/vendor/faiss/faiss/IndexNNDescent.cpp +229 -0
  59. data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
  60. data/vendor/faiss/faiss/IndexNSG.cpp +301 -0
  61. data/vendor/faiss/faiss/IndexNSG.h +85 -0
  62. data/vendor/faiss/faiss/IndexPQ.cpp +387 -495
  63. data/vendor/faiss/faiss/IndexPQ.h +64 -82
  64. data/vendor/faiss/faiss/IndexPQFastScan.cpp +143 -170
  65. data/vendor/faiss/faiss/IndexPQFastScan.h +46 -32
  66. data/vendor/faiss/faiss/IndexPreTransform.cpp +120 -150
  67. data/vendor/faiss/faiss/IndexPreTransform.h +33 -36
  68. data/vendor/faiss/faiss/IndexRefine.cpp +139 -127
  69. data/vendor/faiss/faiss/IndexRefine.h +32 -23
  70. data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
  71. data/vendor/faiss/faiss/IndexReplicas.h +62 -56
  72. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +111 -172
  73. data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -59
  74. data/vendor/faiss/faiss/IndexShards.cpp +256 -240
  75. data/vendor/faiss/faiss/IndexShards.h +85 -73
  76. data/vendor/faiss/faiss/MatrixStats.cpp +112 -97
  77. data/vendor/faiss/faiss/MatrixStats.h +7 -10
  78. data/vendor/faiss/faiss/MetaIndexes.cpp +135 -157
  79. data/vendor/faiss/faiss/MetaIndexes.h +40 -34
  80. data/vendor/faiss/faiss/MetricType.h +7 -7
  81. data/vendor/faiss/faiss/VectorTransform.cpp +654 -475
  82. data/vendor/faiss/faiss/VectorTransform.h +64 -89
  83. data/vendor/faiss/faiss/clone_index.cpp +78 -73
  84. data/vendor/faiss/faiss/clone_index.h +4 -9
  85. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +33 -38
  86. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +11 -9
  87. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +198 -171
  88. data/vendor/faiss/faiss/gpu/GpuCloner.h +53 -35
  89. data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +12 -14
  90. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +27 -25
  91. data/vendor/faiss/faiss/gpu/GpuDistance.h +116 -112
  92. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -2
  93. data/vendor/faiss/faiss/gpu/GpuIcmEncoder.h +60 -0
  94. data/vendor/faiss/faiss/gpu/GpuIndex.h +134 -137
  95. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +76 -73
  96. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +173 -162
  97. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +67 -64
  98. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +89 -86
  99. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +150 -141
  100. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +101 -103
  101. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +17 -16
  102. data/vendor/faiss/faiss/gpu/GpuResources.cpp +116 -128
  103. data/vendor/faiss/faiss/gpu/GpuResources.h +182 -186
  104. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +433 -422
  105. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +131 -130
  106. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +468 -456
  107. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +25 -19
  108. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +22 -20
  109. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +9 -8
  110. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +39 -44
  111. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +16 -14
  112. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +77 -71
  113. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +109 -88
  114. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +75 -64
  115. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +230 -215
  116. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +80 -86
  117. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +284 -277
  118. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +416 -416
  119. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +611 -517
  120. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +166 -164
  121. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +61 -53
  122. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +274 -238
  123. data/vendor/faiss/faiss/gpu/test/TestUtils.h +73 -57
  124. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +47 -50
  125. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +79 -72
  126. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +140 -146
  127. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +69 -71
  128. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +21 -16
  129. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +25 -29
  130. data/vendor/faiss/faiss/gpu/utils/Timer.h +30 -29
  131. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +503 -0
  132. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +175 -0
  133. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +90 -120
  134. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +81 -65
  135. data/vendor/faiss/faiss/impl/FaissAssert.h +73 -58
  136. data/vendor/faiss/faiss/impl/FaissException.cpp +56 -48
  137. data/vendor/faiss/faiss/impl/FaissException.h +41 -29
  138. data/vendor/faiss/faiss/impl/HNSW.cpp +606 -617
  139. data/vendor/faiss/faiss/impl/HNSW.h +179 -200
  140. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +855 -0
  141. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +244 -0
  142. data/vendor/faiss/faiss/impl/NNDescent.cpp +487 -0
  143. data/vendor/faiss/faiss/impl/NNDescent.h +154 -0
  144. data/vendor/faiss/faiss/impl/NSG.cpp +679 -0
  145. data/vendor/faiss/faiss/impl/NSG.h +199 -0
  146. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +484 -454
  147. data/vendor/faiss/faiss/impl/PolysemousTraining.h +52 -55
  148. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +26 -47
  149. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +469 -459
  150. data/vendor/faiss/faiss/impl/ProductQuantizer.h +76 -87
  151. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +758 -0
  152. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +188 -0
  153. data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
  154. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +647 -707
  155. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +48 -46
  156. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +129 -131
  157. data/vendor/faiss/faiss/impl/ThreadedIndex.h +61 -55
  158. data/vendor/faiss/faiss/impl/index_read.cpp +631 -480
  159. data/vendor/faiss/faiss/impl/index_write.cpp +547 -407
  160. data/vendor/faiss/faiss/impl/io.cpp +76 -95
  161. data/vendor/faiss/faiss/impl/io.h +31 -41
  162. data/vendor/faiss/faiss/impl/io_macros.h +60 -29
  163. data/vendor/faiss/faiss/impl/kmeans1d.cpp +301 -0
  164. data/vendor/faiss/faiss/impl/kmeans1d.h +48 -0
  165. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +137 -186
  166. data/vendor/faiss/faiss/impl/lattice_Zn.h +40 -51
  167. data/vendor/faiss/faiss/impl/platform_macros.h +29 -8
  168. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +77 -124
  169. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +39 -48
  170. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +41 -52
  171. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +80 -117
  172. data/vendor/faiss/faiss/impl/simd_result_handlers.h +109 -137
  173. data/vendor/faiss/faiss/index_factory.cpp +619 -397
  174. data/vendor/faiss/faiss/index_factory.h +8 -6
  175. data/vendor/faiss/faiss/index_io.h +23 -26
  176. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +67 -75
  177. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +22 -24
  178. data/vendor/faiss/faiss/invlists/DirectMap.cpp +96 -112
  179. data/vendor/faiss/faiss/invlists/DirectMap.h +29 -33
  180. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +307 -364
  181. data/vendor/faiss/faiss/invlists/InvertedLists.h +151 -151
  182. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +29 -34
  183. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +17 -18
  184. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +257 -293
  185. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +50 -45
  186. data/vendor/faiss/faiss/python/python_callbacks.cpp +23 -26
  187. data/vendor/faiss/faiss/python/python_callbacks.h +9 -16
  188. data/vendor/faiss/faiss/utils/AlignedTable.h +79 -44
  189. data/vendor/faiss/faiss/utils/Heap.cpp +40 -48
  190. data/vendor/faiss/faiss/utils/Heap.h +186 -209
  191. data/vendor/faiss/faiss/utils/WorkerThread.cpp +67 -76
  192. data/vendor/faiss/faiss/utils/WorkerThread.h +32 -33
  193. data/vendor/faiss/faiss/utils/distances.cpp +305 -312
  194. data/vendor/faiss/faiss/utils/distances.h +170 -122
  195. data/vendor/faiss/faiss/utils/distances_simd.cpp +498 -508
  196. data/vendor/faiss/faiss/utils/extra_distances-inl.h +117 -0
  197. data/vendor/faiss/faiss/utils/extra_distances.cpp +113 -232
  198. data/vendor/faiss/faiss/utils/extra_distances.h +30 -29
  199. data/vendor/faiss/faiss/utils/hamming-inl.h +260 -209
  200. data/vendor/faiss/faiss/utils/hamming.cpp +375 -469
  201. data/vendor/faiss/faiss/utils/hamming.h +62 -85
  202. data/vendor/faiss/faiss/utils/ordered_key_value.h +16 -18
  203. data/vendor/faiss/faiss/utils/partitioning.cpp +393 -318
  204. data/vendor/faiss/faiss/utils/partitioning.h +26 -21
  205. data/vendor/faiss/faiss/utils/quantize_lut.cpp +78 -66
  206. data/vendor/faiss/faiss/utils/quantize_lut.h +22 -20
  207. data/vendor/faiss/faiss/utils/random.cpp +39 -63
  208. data/vendor/faiss/faiss/utils/random.h +13 -16
  209. data/vendor/faiss/faiss/utils/simdlib.h +4 -2
  210. data/vendor/faiss/faiss/utils/simdlib_avx2.h +88 -85
  211. data/vendor/faiss/faiss/utils/simdlib_emulated.h +226 -165
  212. data/vendor/faiss/faiss/utils/simdlib_neon.h +832 -0
  213. data/vendor/faiss/faiss/utils/utils.cpp +304 -287
  214. data/vendor/faiss/faiss/utils/utils.h +54 -49
  215. metadata +29 -4
@@ -12,108 +12,95 @@
12
12
  #include <memory>
13
13
 
14
14
  #include <faiss/IndexPreTransform.h>
15
- #include <faiss/impl/FaissAssert.h>
16
15
  #include <faiss/MetaIndexes.h>
16
+ #include <faiss/impl/FaissAssert.h>
17
17
  #include <faiss/utils/utils.h>
18
18
 
19
+ namespace faiss {
20
+ namespace ivflib {
19
21
 
20
-
21
- namespace faiss { namespace ivflib {
22
-
23
-
24
- void check_compatible_for_merge (const Index * index0,
25
- const Index * index1)
26
- {
27
-
28
- const faiss::IndexPreTransform *pt0 =
29
- dynamic_cast<const faiss::IndexPreTransform *>(index0);
22
+ void check_compatible_for_merge(const Index* index0, const Index* index1) {
23
+ const faiss::IndexPreTransform* pt0 =
24
+ dynamic_cast<const faiss::IndexPreTransform*>(index0);
30
25
 
31
26
  if (pt0) {
32
- const faiss::IndexPreTransform *pt1 =
33
- dynamic_cast<const faiss::IndexPreTransform *>(index1);
34
- FAISS_THROW_IF_NOT_MSG (pt1, "both indexes should be pretransforms");
27
+ const faiss::IndexPreTransform* pt1 =
28
+ dynamic_cast<const faiss::IndexPreTransform*>(index1);
29
+ FAISS_THROW_IF_NOT_MSG(pt1, "both indexes should be pretransforms");
35
30
 
36
- FAISS_THROW_IF_NOT (pt0->chain.size() == pt1->chain.size());
31
+ FAISS_THROW_IF_NOT(pt0->chain.size() == pt1->chain.size());
37
32
  for (int i = 0; i < pt0->chain.size(); i++) {
38
- FAISS_THROW_IF_NOT (typeid(pt0->chain[i]) == typeid(pt1->chain[i]));
33
+ FAISS_THROW_IF_NOT(typeid(pt0->chain[i]) == typeid(pt1->chain[i]));
39
34
  }
40
35
 
41
36
  index0 = pt0->index;
42
37
  index1 = pt1->index;
43
38
  }
44
- FAISS_THROW_IF_NOT (typeid(index0) == typeid(index1));
45
- FAISS_THROW_IF_NOT (index0->d == index1->d &&
46
- index0->metric_type == index1->metric_type);
39
+ FAISS_THROW_IF_NOT(typeid(index0) == typeid(index1));
40
+ FAISS_THROW_IF_NOT(
41
+ index0->d == index1->d &&
42
+ index0->metric_type == index1->metric_type);
47
43
 
48
- const faiss::IndexIVF *ivf0 = dynamic_cast<const faiss::IndexIVF *>(index0);
44
+ const faiss::IndexIVF* ivf0 = dynamic_cast<const faiss::IndexIVF*>(index0);
49
45
  if (ivf0) {
50
- const faiss::IndexIVF *ivf1 =
51
- dynamic_cast<const faiss::IndexIVF *>(index1);
52
- FAISS_THROW_IF_NOT (ivf1);
46
+ const faiss::IndexIVF* ivf1 =
47
+ dynamic_cast<const faiss::IndexIVF*>(index1);
48
+ FAISS_THROW_IF_NOT(ivf1);
53
49
 
54
- ivf0->check_compatible_for_merge (*ivf1);
50
+ ivf0->check_compatible_for_merge(*ivf1);
55
51
  }
56
52
 
57
53
  // TODO: check as thoroughfully for other index types
58
-
59
54
  }
60
55
 
61
- const IndexIVF * try_extract_index_ivf (const Index * index)
62
- {
63
- if (auto *pt =
64
- dynamic_cast<const IndexPreTransform *>(index)) {
56
+ const IndexIVF* try_extract_index_ivf(const Index* index) {
57
+ if (auto* pt = dynamic_cast<const IndexPreTransform*>(index)) {
65
58
  index = pt->index;
66
59
  }
67
60
 
68
- if (auto *idmap =
69
- dynamic_cast<const IndexIDMap *>(index)) {
61
+ if (auto* idmap = dynamic_cast<const IndexIDMap*>(index)) {
70
62
  index = idmap->index;
71
63
  }
72
- if (auto *idmap =
73
- dynamic_cast<const IndexIDMap2 *>(index)) {
64
+ if (auto* idmap = dynamic_cast<const IndexIDMap2*>(index)) {
74
65
  index = idmap->index;
75
66
  }
76
67
 
77
- auto *ivf = dynamic_cast<const IndexIVF *>(index);
68
+ auto* ivf = dynamic_cast<const IndexIVF*>(index);
78
69
 
79
70
  return ivf;
80
71
  }
81
72
 
82
- IndexIVF * try_extract_index_ivf (Index * index) {
83
- return const_cast<IndexIVF*> (try_extract_index_ivf ((const Index*)(index)));
73
+ IndexIVF* try_extract_index_ivf(Index* index) {
74
+ return const_cast<IndexIVF*>(try_extract_index_ivf((const Index*)(index)));
84
75
  }
85
76
 
86
- const IndexIVF * extract_index_ivf (const Index * index)
87
- {
88
- const IndexIVF *ivf = try_extract_index_ivf (index);
89
- FAISS_THROW_IF_NOT (ivf);
77
+ const IndexIVF* extract_index_ivf(const Index* index) {
78
+ const IndexIVF* ivf = try_extract_index_ivf(index);
79
+ FAISS_THROW_IF_NOT(ivf);
90
80
  return ivf;
91
81
  }
92
82
 
93
- IndexIVF * extract_index_ivf (Index * index) {
94
- return const_cast<IndexIVF*> (extract_index_ivf ((const Index*)(index)));
83
+ IndexIVF* extract_index_ivf(Index* index) {
84
+ return const_cast<IndexIVF*>(extract_index_ivf((const Index*)(index)));
95
85
  }
96
86
 
87
+ void merge_into(faiss::Index* index0, faiss::Index* index1, bool shift_ids) {
88
+ check_compatible_for_merge(index0, index1);
89
+ IndexIVF* ivf0 = extract_index_ivf(index0);
90
+ IndexIVF* ivf1 = extract_index_ivf(index1);
97
91
 
98
- void merge_into(faiss::Index *index0, faiss::Index *index1, bool shift_ids) {
99
-
100
- check_compatible_for_merge (index0, index1);
101
- IndexIVF * ivf0 = extract_index_ivf (index0);
102
- IndexIVF * ivf1 = extract_index_ivf (index1);
103
-
104
- ivf0->merge_from (*ivf1, shift_ids ? ivf0->ntotal : 0);
92
+ ivf0->merge_from(*ivf1, shift_ids ? ivf0->ntotal : 0);
105
93
 
106
94
  // useful for IndexPreTransform
107
95
  index0->ntotal = ivf0->ntotal;
108
96
  index1->ntotal = ivf1->ntotal;
109
97
  }
110
98
 
111
-
112
-
113
- void search_centroid(faiss::Index *index,
114
- const float* x, int n,
115
- idx_t* centroid_ids)
116
- {
99
+ void search_centroid(
100
+ faiss::Index* index,
101
+ const float* x,
102
+ int n,
103
+ idx_t* centroid_ids) {
117
104
  std::unique_ptr<float[]> del;
118
105
  if (auto index_pre = dynamic_cast<faiss::IndexPreTransform*>(index)) {
119
106
  x = index_pre->apply_chain(n, x);
@@ -125,19 +112,17 @@ void search_centroid(faiss::Index *index,
125
112
  index_ivf->quantizer->assign(n, x, centroid_ids);
126
113
  }
127
114
 
128
-
129
-
130
- void search_and_return_centroids(faiss::Index *index,
131
- size_t n,
132
- const float* xin,
133
- long k,
134
- float *distances,
135
- idx_t* labels,
136
- idx_t* query_centroid_ids,
137
- idx_t* result_centroid_ids)
138
- {
139
- const float *x = xin;
140
- std::unique_ptr<float []> del;
115
+ void search_and_return_centroids(
116
+ faiss::Index* index,
117
+ size_t n,
118
+ const float* xin,
119
+ long k,
120
+ float* distances,
121
+ idx_t* labels,
122
+ idx_t* query_centroid_ids,
123
+ idx_t* result_centroid_ids) {
124
+ const float* x = xin;
125
+ std::unique_ptr<float[]> del;
141
126
  if (auto index_pre = dynamic_cast<faiss::IndexPreTransform*>(index)) {
142
127
  x = index_pre->apply_chain(n, x);
143
128
  del.reset((float*)x);
@@ -147,19 +132,18 @@ void search_and_return_centroids(faiss::Index *index,
147
132
  assert(index_ivf);
148
133
 
149
134
  size_t nprobe = index_ivf->nprobe;
150
- std::vector<idx_t> cent_nos (n * nprobe);
151
- std::vector<float> cent_dis (n * nprobe);
135
+ std::vector<idx_t> cent_nos(n * nprobe);
136
+ std::vector<float> cent_dis(n * nprobe);
152
137
  index_ivf->quantizer->search(
153
- n, x, nprobe, cent_dis.data(), cent_nos.data());
138
+ n, x, nprobe, cent_dis.data(), cent_nos.data());
154
139
 
155
140
  if (query_centroid_ids) {
156
141
  for (size_t i = 0; i < n; i++)
157
142
  query_centroid_ids[i] = cent_nos[i * nprobe];
158
143
  }
159
144
 
160
- index_ivf->search_preassigned (n, x, k,
161
- cent_nos.data(), cent_dis.data(),
162
- distances, labels, true);
145
+ index_ivf->search_preassigned(
146
+ n, x, k, cent_nos.data(), cent_dis.data(), distances, labels, true);
163
147
 
164
148
  for (size_t i = 0; i < n * k; i++) {
165
149
  idx_t label = labels[i];
@@ -167,8 +151,8 @@ void search_and_return_centroids(faiss::Index *index,
167
151
  if (result_centroid_ids)
168
152
  result_centroid_ids[i] = -1;
169
153
  } else {
170
- long list_no = lo_listno (label);
171
- long list_index = lo_offset (label);
154
+ long list_no = lo_listno(label);
155
+ long list_index = lo_offset(label);
172
156
  if (result_centroid_ids)
173
157
  result_centroid_ids[i] = list_no;
174
158
  labels[i] = index_ivf->invlists->get_single_id(list_no, list_index);
@@ -176,61 +160,61 @@ void search_and_return_centroids(faiss::Index *index,
176
160
  }
177
161
  }
178
162
 
179
-
180
- SlidingIndexWindow::SlidingIndexWindow (Index *index): index (index) {
163
+ SlidingIndexWindow::SlidingIndexWindow(Index* index) : index(index) {
181
164
  n_slice = 0;
182
- IndexIVF* index_ivf = const_cast<IndexIVF*>(extract_index_ivf (index));
183
- ils = dynamic_cast<ArrayInvertedLists *> (index_ivf->invlists);
184
- FAISS_THROW_IF_NOT_MSG (ils,
185
- "only supports indexes with ArrayInvertedLists");
165
+ IndexIVF* index_ivf = const_cast<IndexIVF*>(extract_index_ivf(index));
166
+ ils = dynamic_cast<ArrayInvertedLists*>(index_ivf->invlists);
167
+ FAISS_THROW_IF_NOT_MSG(
168
+ ils, "only supports indexes with ArrayInvertedLists");
186
169
  nlist = ils->nlist;
187
170
  sizes.resize(nlist);
188
171
  }
189
172
 
190
- template<class T>
191
- static void shift_and_add (std::vector<T> & dst,
192
- size_t remove,
193
- const std::vector<T> & src)
194
- {
173
+ template <class T>
174
+ static void shift_and_add(
175
+ std::vector<T>& dst,
176
+ size_t remove,
177
+ const std::vector<T>& src) {
195
178
  if (remove > 0)
196
- memmove (dst.data(), dst.data() + remove,
197
- (dst.size() - remove) * sizeof (T));
179
+ memmove(dst.data(),
180
+ dst.data() + remove,
181
+ (dst.size() - remove) * sizeof(T));
198
182
  size_t insert_point = dst.size() - remove;
199
- dst.resize (insert_point + src.size());
200
- memcpy (dst.data() + insert_point, src.data (), src.size() * sizeof(T));
183
+ dst.resize(insert_point + src.size());
184
+ memcpy(dst.data() + insert_point, src.data(), src.size() * sizeof(T));
201
185
  }
202
186
 
203
- template<class T>
204
- static void remove_from_begin (std::vector<T> & v,
205
- size_t remove)
206
- {
187
+ template <class T>
188
+ static void remove_from_begin(std::vector<T>& v, size_t remove) {
207
189
  if (remove > 0)
208
- v.erase (v.begin(), v.begin() + remove);
190
+ v.erase(v.begin(), v.begin() + remove);
209
191
  }
210
192
 
211
- void SlidingIndexWindow::step(const Index *sub_index, bool remove_oldest) {
212
-
213
- FAISS_THROW_IF_NOT_MSG (!remove_oldest || n_slice > 0,
214
- "cannot remove slice: there is none");
193
+ void SlidingIndexWindow::step(const Index* sub_index, bool remove_oldest) {
194
+ FAISS_THROW_IF_NOT_MSG(
195
+ !remove_oldest || n_slice > 0,
196
+ "cannot remove slice: there is none");
215
197
 
216
- const ArrayInvertedLists *ils2 = nullptr;
217
- if(sub_index) {
218
- check_compatible_for_merge (index, sub_index);
198
+ const ArrayInvertedLists* ils2 = nullptr;
199
+ if (sub_index) {
200
+ check_compatible_for_merge(index, sub_index);
219
201
  ils2 = dynamic_cast<const ArrayInvertedLists*>(
220
- extract_index_ivf (sub_index)->invlists);
221
- FAISS_THROW_IF_NOT_MSG (ils2, "supports only ArrayInvertedLists");
202
+ extract_index_ivf(sub_index)->invlists);
203
+ FAISS_THROW_IF_NOT_MSG(ils2, "supports only ArrayInvertedLists");
222
204
  }
223
- IndexIVF *index_ivf = extract_index_ivf (index);
205
+ IndexIVF* index_ivf = extract_index_ivf(index);
224
206
 
225
207
  if (remove_oldest && ils2) {
226
208
  for (int i = 0; i < nlist; i++) {
227
- std::vector<size_t> & sizesi = sizes[i];
209
+ std::vector<size_t>& sizesi = sizes[i];
228
210
  size_t amount_to_remove = sizesi[0];
229
211
  index_ivf->ntotal += ils2->ids[i].size() - amount_to_remove;
230
212
 
231
- shift_and_add (ils->ids[i], amount_to_remove, ils2->ids[i]);
232
- shift_and_add (ils->codes[i], amount_to_remove * ils->code_size,
233
- ils2->codes[i]);
213
+ shift_and_add(ils->ids[i], amount_to_remove, ils2->ids[i]);
214
+ shift_and_add(
215
+ ils->codes[i],
216
+ amount_to_remove * ils->code_size,
217
+ ils2->codes[i]);
234
218
  for (int j = 0; j + 1 < n_slice; j++) {
235
219
  sizesi[j] = sizesi[j + 1] - amount_to_remove;
236
220
  }
@@ -239,8 +223,8 @@ void SlidingIndexWindow::step(const Index *sub_index, bool remove_oldest) {
239
223
  } else if (ils2) {
240
224
  for (int i = 0; i < nlist; i++) {
241
225
  index_ivf->ntotal += ils2->ids[i].size();
242
- shift_and_add (ils->ids[i], 0, ils2->ids[i]);
243
- shift_and_add (ils->codes[i], 0, ils2->codes[i]);
226
+ shift_and_add(ils->ids[i], 0, ils2->ids[i]);
227
+ shift_and_add(ils->codes[i], 0, ils2->codes[i]);
244
228
  sizes[i].push_back(ils->ids[i].size());
245
229
  }
246
230
  n_slice++;
@@ -248,74 +232,72 @@ void SlidingIndexWindow::step(const Index *sub_index, bool remove_oldest) {
248
232
  for (int i = 0; i < nlist; i++) {
249
233
  size_t amount_to_remove = sizes[i][0];
250
234
  index_ivf->ntotal -= amount_to_remove;
251
- remove_from_begin (ils->ids[i], amount_to_remove);
252
- remove_from_begin (ils->codes[i],
253
- amount_to_remove * ils->code_size);
235
+ remove_from_begin(ils->ids[i], amount_to_remove);
236
+ remove_from_begin(ils->codes[i], amount_to_remove * ils->code_size);
254
237
  for (int j = 0; j + 1 < n_slice; j++) {
255
238
  sizes[i][j] = sizes[i][j + 1] - amount_to_remove;
256
239
  }
257
- sizes[i].pop_back ();
240
+ sizes[i].pop_back();
258
241
  }
259
242
  n_slice--;
260
243
  } else {
261
- FAISS_THROW_MSG ("nothing to do???");
244
+ FAISS_THROW_MSG("nothing to do???");
262
245
  }
263
246
  index->ntotal = index_ivf->ntotal;
264
247
  }
265
248
 
266
-
267
-
268
249
  // Get a subset of inverted lists [i0, i1). Works on IndexIVF's and
269
250
  // IndexIVF's embedded in a IndexPreTransform
270
251
 
271
- ArrayInvertedLists *
272
- get_invlist_range (const Index *index, long i0, long i1)
273
- {
274
- const IndexIVF *ivf = extract_index_ivf (index);
252
+ ArrayInvertedLists* get_invlist_range(const Index* index, long i0, long i1) {
253
+ const IndexIVF* ivf = extract_index_ivf(index);
275
254
 
276
- FAISS_THROW_IF_NOT (0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
255
+ FAISS_THROW_IF_NOT(0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
277
256
 
278
- const InvertedLists *src = ivf->invlists;
257
+ const InvertedLists* src = ivf->invlists;
279
258
 
280
- ArrayInvertedLists * il = new ArrayInvertedLists(i1 - i0, src->code_size);
259
+ ArrayInvertedLists* il = new ArrayInvertedLists(i1 - i0, src->code_size);
281
260
 
282
261
  for (long i = i0; i < i1; i++) {
283
- il->add_entries(i - i0, src->list_size(i),
284
- InvertedLists::ScopedIds (src, i).get(),
285
- InvertedLists::ScopedCodes (src, i).get());
262
+ il->add_entries(
263
+ i - i0,
264
+ src->list_size(i),
265
+ InvertedLists::ScopedIds(src, i).get(),
266
+ InvertedLists::ScopedCodes(src, i).get());
286
267
  }
287
268
  return il;
288
269
  }
289
270
 
271
+ void set_invlist_range(
272
+ Index* index,
273
+ long i0,
274
+ long i1,
275
+ ArrayInvertedLists* src) {
276
+ IndexIVF* ivf = extract_index_ivf(index);
290
277
 
278
+ FAISS_THROW_IF_NOT(0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
291
279
 
292
- void set_invlist_range (Index *index, long i0, long i1,
293
- ArrayInvertedLists * src)
294
- {
295
- IndexIVF *ivf = extract_index_ivf (index);
296
-
297
- FAISS_THROW_IF_NOT (0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
298
-
299
- ArrayInvertedLists *dst = dynamic_cast<ArrayInvertedLists *>(ivf->invlists);
300
- FAISS_THROW_IF_NOT_MSG (dst, "only ArrayInvertedLists supported");
301
- FAISS_THROW_IF_NOT (src->nlist == i1 - i0 &&
302
- dst->code_size == src->code_size);
280
+ ArrayInvertedLists* dst = dynamic_cast<ArrayInvertedLists*>(ivf->invlists);
281
+ FAISS_THROW_IF_NOT_MSG(dst, "only ArrayInvertedLists supported");
282
+ FAISS_THROW_IF_NOT(
283
+ src->nlist == i1 - i0 && dst->code_size == src->code_size);
303
284
 
304
285
  size_t ntotal = index->ntotal;
305
- for (long i = i0 ; i < i1; i++) {
306
- ntotal -= dst->list_size (i);
307
- ntotal += src->list_size (i - i0);
308
- std::swap (src->codes[i - i0], dst->codes[i]);
309
- std::swap (src->ids[i - i0], dst->ids[i]);
286
+ for (long i = i0; i < i1; i++) {
287
+ ntotal -= dst->list_size(i);
288
+ ntotal += src->list_size(i - i0);
289
+ std::swap(src->codes[i - i0], dst->codes[i]);
290
+ std::swap(src->ids[i - i0], dst->ids[i]);
310
291
  }
311
292
  ivf->ntotal = index->ntotal = ntotal;
312
293
  }
313
294
 
314
- static size_t count_ndis(const IndexIVF * index_ivf, size_t n_list_scan,
315
- const idx_t *Iq)
316
- {
295
+ static size_t count_ndis(
296
+ const IndexIVF* index_ivf,
297
+ size_t n_list_scan,
298
+ const idx_t* Iq) {
317
299
  size_t nb_dis = 0;
318
- const InvertedLists *il = index_ivf->invlists;
300
+ const InvertedLists* il = index_ivf->invlists;
319
301
  for (idx_t i = 0; i < n_list_scan; i++) {
320
302
  if (Iq[i] >= 0) {
321
303
  nb_dis += il->list_size(Iq[i]);
@@ -324,23 +306,24 @@ static size_t count_ndis(const IndexIVF * index_ivf, size_t n_list_scan,
324
306
  return nb_dis;
325
307
  }
326
308
 
327
-
328
-
329
- void search_with_parameters (const Index *index,
330
- idx_t n, const float *x, idx_t k,
331
- float *distances, idx_t *labels,
332
- const IVFSearchParameters *params,
333
- size_t *nb_dis_ptr,
334
- double *ms_per_stage)
335
- {
336
- FAISS_THROW_IF_NOT (params);
337
- const float *prev_x = x;
309
+ void search_with_parameters(
310
+ const Index* index,
311
+ idx_t n,
312
+ const float* x,
313
+ idx_t k,
314
+ float* distances,
315
+ idx_t* labels,
316
+ const IVFSearchParameters* params,
317
+ size_t* nb_dis_ptr,
318
+ double* ms_per_stage) {
319
+ FAISS_THROW_IF_NOT(params);
320
+ const float* prev_x = x;
338
321
  ScopeDeleter<float> del;
339
322
 
340
323
  double t0 = getmillisecs();
341
324
 
342
- if (auto ip = dynamic_cast<const IndexPreTransform *> (index)) {
343
- x = ip->apply_chain (n, x);
325
+ if (auto ip = dynamic_cast<const IndexPreTransform*>(index)) {
326
+ x = ip->apply_chain(n, x);
344
327
  if (x != prev_x) {
345
328
  del.set(x);
346
329
  }
@@ -352,21 +335,19 @@ void search_with_parameters (const Index *index,
352
335
  std::vector<idx_t> Iq(params->nprobe * n);
353
336
  std::vector<float> Dq(params->nprobe * n);
354
337
 
355
- const IndexIVF *index_ivf = dynamic_cast<const IndexIVF *>(index);
356
- FAISS_THROW_IF_NOT (index_ivf);
338
+ const IndexIVF* index_ivf = dynamic_cast<const IndexIVF*>(index);
339
+ FAISS_THROW_IF_NOT(index_ivf);
357
340
 
358
- index_ivf->quantizer->search(n, x, params->nprobe,
359
- Dq.data(), Iq.data());
341
+ index_ivf->quantizer->search(n, x, params->nprobe, Dq.data(), Iq.data());
360
342
 
361
343
  if (nb_dis_ptr) {
362
- *nb_dis_ptr = count_ndis (index_ivf, n * params->nprobe, Iq.data());
344
+ *nb_dis_ptr = count_ndis(index_ivf, n * params->nprobe, Iq.data());
363
345
  }
364
346
 
365
347
  double t2 = getmillisecs();
366
348
 
367
- index_ivf->search_preassigned(n, x, k, Iq.data(), Dq.data(),
368
- distances, labels,
369
- false, params);
349
+ index_ivf->search_preassigned(
350
+ n, x, k, Iq.data(), Dq.data(), distances, labels, false, params);
370
351
  double t3 = getmillisecs();
371
352
  if (ms_per_stage) {
372
353
  ms_per_stage[0] = t1 - t0;
@@ -375,21 +356,23 @@ void search_with_parameters (const Index *index,
375
356
  }
376
357
  }
377
358
 
378
- void range_search_with_parameters (const Index *index,
379
- idx_t n, const float *x, float radius,
380
- RangeSearchResult *result,
381
- const IVFSearchParameters *params,
382
- size_t *nb_dis_ptr,
383
- double *ms_per_stage)
384
- {
385
- FAISS_THROW_IF_NOT (params);
386
- const float *prev_x = x;
359
+ void range_search_with_parameters(
360
+ const Index* index,
361
+ idx_t n,
362
+ const float* x,
363
+ float radius,
364
+ RangeSearchResult* result,
365
+ const IVFSearchParameters* params,
366
+ size_t* nb_dis_ptr,
367
+ double* ms_per_stage) {
368
+ FAISS_THROW_IF_NOT(params);
369
+ const float* prev_x = x;
387
370
  ScopeDeleter<float> del;
388
371
 
389
372
  double t0 = getmillisecs();
390
373
 
391
- if (auto ip = dynamic_cast<const IndexPreTransform *> (index)) {
392
- x = ip->apply_chain (n, x);
374
+ if (auto ip = dynamic_cast<const IndexPreTransform*>(index)) {
375
+ x = ip->apply_chain(n, x);
393
376
  if (x != prev_x) {
394
377
  del.set(x);
395
378
  }
@@ -401,22 +384,19 @@ void range_search_with_parameters (const Index *index,
401
384
  std::vector<idx_t> Iq(params->nprobe * n);
402
385
  std::vector<float> Dq(params->nprobe * n);
403
386
 
404
- const IndexIVF *index_ivf = dynamic_cast<const IndexIVF *>(index);
405
- FAISS_THROW_IF_NOT (index_ivf);
387
+ const IndexIVF* index_ivf = dynamic_cast<const IndexIVF*>(index);
388
+ FAISS_THROW_IF_NOT(index_ivf);
406
389
 
407
- index_ivf->quantizer->search(n, x, params->nprobe,
408
- Dq.data(), Iq.data());
390
+ index_ivf->quantizer->search(n, x, params->nprobe, Dq.data(), Iq.data());
409
391
 
410
392
  if (nb_dis_ptr) {
411
- *nb_dis_ptr = count_ndis (index_ivf, n * params->nprobe, Iq.data());
393
+ *nb_dis_ptr = count_ndis(index_ivf, n * params->nprobe, Iq.data());
412
394
  }
413
395
 
414
396
  double t2 = getmillisecs();
415
397
 
416
398
  index_ivf->range_search_preassigned(
417
- n, x, radius, Iq.data(), Dq.data(),
418
- result, false, params
419
- );
399
+ n, x, radius, Iq.data(), Dq.data(), result, false, params);
420
400
 
421
401
  double t3 = getmillisecs();
422
402
  if (ms_per_stage) {
@@ -426,9 +406,5 @@ void range_search_with_parameters (const Index *index,
426
406
  }
427
407
  }
428
408
 
429
-
430
-
431
-
432
-
433
-
434
- } } // namespace faiss::ivflib
409
+ } // namespace ivflib
410
+ } // namespace faiss