faiss 0.2.3 → 0.2.5
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/LICENSE.txt +1 -1
- data/README.md +23 -21
- data/ext/faiss/extconf.rb +11 -0
- data/ext/faiss/index.cpp +4 -4
- data/ext/faiss/index_binary.cpp +6 -6
- data/ext/faiss/product_quantizer.cpp +4 -4
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +13 -0
- data/vendor/faiss/faiss/Clustering.cpp +32 -0
- data/vendor/faiss/faiss/Clustering.h +14 -0
- data/vendor/faiss/faiss/IVFlib.cpp +101 -2
- data/vendor/faiss/faiss/IVFlib.h +26 -2
- data/vendor/faiss/faiss/Index.cpp +36 -3
- data/vendor/faiss/faiss/Index.h +43 -6
- data/vendor/faiss/faiss/Index2Layer.cpp +24 -93
- data/vendor/faiss/faiss/Index2Layer.h +8 -17
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +610 -0
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +253 -0
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +299 -0
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.h +199 -0
- data/vendor/faiss/faiss/IndexBinary.cpp +20 -4
- data/vendor/faiss/faiss/IndexBinary.h +18 -3
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +9 -2
- data/vendor/faiss/faiss/IndexBinaryFlat.h +4 -2
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +4 -1
- data/vendor/faiss/faiss/IndexBinaryFromFloat.h +2 -1
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +5 -1
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +2 -1
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +17 -4
- data/vendor/faiss/faiss/IndexBinaryHash.h +8 -4
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +28 -13
- data/vendor/faiss/faiss/IndexBinaryIVF.h +10 -7
- data/vendor/faiss/faiss/IndexFastScan.cpp +626 -0
- data/vendor/faiss/faiss/IndexFastScan.h +145 -0
- data/vendor/faiss/faiss/IndexFlat.cpp +52 -69
- data/vendor/faiss/faiss/IndexFlat.h +16 -19
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +101 -0
- data/vendor/faiss/faiss/IndexFlatCodes.h +59 -0
- data/vendor/faiss/faiss/IndexHNSW.cpp +66 -138
- data/vendor/faiss/faiss/IndexHNSW.h +4 -2
- data/vendor/faiss/faiss/IndexIDMap.cpp +247 -0
- data/vendor/faiss/faiss/IndexIDMap.h +107 -0
- data/vendor/faiss/faiss/IndexIVF.cpp +200 -40
- data/vendor/faiss/faiss/IndexIVF.h +59 -22
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +393 -0
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +183 -0
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +590 -0
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +171 -0
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +1290 -0
- data/vendor/faiss/faiss/IndexIVFFastScan.h +213 -0
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +43 -26
- data/vendor/faiss/faiss/IndexIVFFlat.h +4 -2
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +238 -53
- data/vendor/faiss/faiss/IndexIVFPQ.h +6 -2
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +23 -852
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +7 -112
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +3 -3
- data/vendor/faiss/faiss/IndexIVFPQR.h +1 -1
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +63 -40
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +23 -7
- data/vendor/faiss/faiss/IndexLSH.cpp +8 -32
- data/vendor/faiss/faiss/IndexLSH.h +4 -16
- data/vendor/faiss/faiss/IndexLattice.cpp +7 -1
- data/vendor/faiss/faiss/IndexLattice.h +3 -1
- data/vendor/faiss/faiss/IndexNNDescent.cpp +4 -5
- data/vendor/faiss/faiss/IndexNNDescent.h +2 -1
- data/vendor/faiss/faiss/IndexNSG.cpp +37 -5
- data/vendor/faiss/faiss/IndexNSG.h +25 -1
- data/vendor/faiss/faiss/IndexPQ.cpp +108 -120
- data/vendor/faiss/faiss/IndexPQ.h +21 -22
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +15 -450
- data/vendor/faiss/faiss/IndexPQFastScan.h +15 -78
- data/vendor/faiss/faiss/IndexPreTransform.cpp +47 -8
- data/vendor/faiss/faiss/IndexPreTransform.h +15 -3
- data/vendor/faiss/faiss/IndexRefine.cpp +36 -4
- data/vendor/faiss/faiss/IndexRefine.h +14 -2
- data/vendor/faiss/faiss/IndexReplicas.cpp +4 -2
- data/vendor/faiss/faiss/IndexReplicas.h +2 -1
- data/vendor/faiss/faiss/IndexRowwiseMinMax.cpp +438 -0
- data/vendor/faiss/faiss/IndexRowwiseMinMax.h +92 -0
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +28 -43
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +8 -23
- data/vendor/faiss/faiss/IndexShards.cpp +4 -1
- data/vendor/faiss/faiss/IndexShards.h +2 -1
- data/vendor/faiss/faiss/MetaIndexes.cpp +5 -178
- data/vendor/faiss/faiss/MetaIndexes.h +3 -81
- data/vendor/faiss/faiss/VectorTransform.cpp +45 -1
- data/vendor/faiss/faiss/VectorTransform.h +25 -4
- data/vendor/faiss/faiss/clone_index.cpp +26 -3
- data/vendor/faiss/faiss/clone_index.h +3 -0
- data/vendor/faiss/faiss/cppcontrib/SaDecodeKernels.h +300 -0
- data/vendor/faiss/faiss/cppcontrib/detail/CoarseBitType.h +24 -0
- data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +195 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-avx2-inl.h +2058 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +408 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-neon-inl.h +2147 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMax-inl.h +460 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMaxFP16-inl.h +465 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-avx2-inl.h +1618 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-inl.h +251 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-neon-inl.h +1452 -0
- data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +1 -0
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +2 -6
- data/vendor/faiss/faiss/gpu/GpuIcmEncoder.h +60 -0
- data/vendor/faiss/faiss/gpu/GpuIndex.h +28 -4
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +2 -1
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +10 -8
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +75 -14
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +19 -32
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +22 -31
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +22 -28
- data/vendor/faiss/faiss/gpu/GpuResources.cpp +14 -0
- data/vendor/faiss/faiss/gpu/GpuResources.h +16 -3
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +3 -3
- data/vendor/faiss/faiss/gpu/impl/IndexUtils.h +32 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +1 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +311 -75
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +10 -0
- data/vendor/faiss/faiss/gpu/test/TestUtils.h +3 -0
- data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +2 -2
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +5 -4
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +331 -29
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +110 -19
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +0 -54
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +0 -76
- data/vendor/faiss/faiss/impl/DistanceComputer.h +64 -0
- data/vendor/faiss/faiss/impl/HNSW.cpp +133 -32
- data/vendor/faiss/faiss/impl/HNSW.h +19 -16
- data/vendor/faiss/faiss/impl/IDSelector.cpp +125 -0
- data/vendor/faiss/faiss/impl/IDSelector.h +135 -0
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +378 -217
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +106 -29
- data/vendor/faiss/faiss/impl/LookupTableScaler.h +77 -0
- data/vendor/faiss/faiss/impl/NNDescent.cpp +1 -0
- data/vendor/faiss/faiss/impl/NSG.cpp +1 -4
- data/vendor/faiss/faiss/impl/NSG.h +1 -1
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +383 -0
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +154 -0
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +225 -145
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +29 -10
- data/vendor/faiss/faiss/impl/Quantizer.h +43 -0
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +521 -55
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +94 -16
- data/vendor/faiss/faiss/impl/ResultHandler.h +96 -0
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +108 -191
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +18 -18
- data/vendor/faiss/faiss/impl/index_read.cpp +338 -24
- data/vendor/faiss/faiss/impl/index_write.cpp +300 -18
- data/vendor/faiss/faiss/impl/io.cpp +1 -1
- data/vendor/faiss/faiss/impl/io_macros.h +20 -0
- data/vendor/faiss/faiss/impl/kmeans1d.cpp +303 -0
- data/vendor/faiss/faiss/impl/kmeans1d.h +48 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +56 -16
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +25 -8
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +66 -25
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +75 -27
- data/vendor/faiss/faiss/index_factory.cpp +772 -412
- data/vendor/faiss/faiss/index_factory.h +3 -0
- data/vendor/faiss/faiss/index_io.h +5 -0
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -0
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +4 -1
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +2 -1
- data/vendor/faiss/faiss/python/python_callbacks.cpp +27 -0
- data/vendor/faiss/faiss/python/python_callbacks.h +15 -0
- data/vendor/faiss/faiss/utils/Heap.h +31 -15
- data/vendor/faiss/faiss/utils/distances.cpp +384 -58
- data/vendor/faiss/faiss/utils/distances.h +149 -18
- data/vendor/faiss/faiss/utils/distances_simd.cpp +776 -6
- data/vendor/faiss/faiss/utils/extra_distances.cpp +12 -7
- data/vendor/faiss/faiss/utils/extra_distances.h +3 -1
- data/vendor/faiss/faiss/utils/fp16-fp16c.h +21 -0
- data/vendor/faiss/faiss/utils/fp16-inl.h +101 -0
- data/vendor/faiss/faiss/utils/fp16.h +11 -0
- data/vendor/faiss/faiss/utils/hamming-inl.h +54 -0
- data/vendor/faiss/faiss/utils/hamming.cpp +0 -48
- data/vendor/faiss/faiss/utils/ordered_key_value.h +10 -0
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +62 -0
- data/vendor/faiss/faiss/utils/quantize_lut.h +20 -0
- data/vendor/faiss/faiss/utils/random.cpp +53 -0
- data/vendor/faiss/faiss/utils/random.h +5 -0
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +4 -0
- data/vendor/faiss/faiss/utils/simdlib_emulated.h +6 -1
- data/vendor/faiss/faiss/utils/simdlib_neon.h +7 -2
- data/vendor/faiss/faiss/utils/utils.h +1 -1
- metadata +46 -5
- data/vendor/faiss/faiss/IndexResidual.cpp +0 -291
- data/vendor/faiss/faiss/IndexResidual.h +0 -152
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
#include <memory>
|
|
16
16
|
|
|
17
17
|
#include <faiss/impl/AuxIndexStructures.h>
|
|
18
|
+
#include <faiss/impl/DistanceComputer.h>
|
|
18
19
|
#include <faiss/impl/FaissAssert.h>
|
|
19
20
|
|
|
20
21
|
namespace faiss {
|
|
@@ -157,29 +158,42 @@ void IndexPreTransform::add_with_ids(
|
|
|
157
158
|
ntotal = index->ntotal;
|
|
158
159
|
}
|
|
159
160
|
|
|
161
|
+
namespace {
|
|
162
|
+
|
|
163
|
+
const SearchParameters* extract_index_search_params(
|
|
164
|
+
const SearchParameters* params_in) {
|
|
165
|
+
auto params = dynamic_cast<const SearchParametersPreTransform*>(params_in);
|
|
166
|
+
return params ? params->index_params : params_in;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
} // namespace
|
|
170
|
+
|
|
160
171
|
void IndexPreTransform::search(
|
|
161
172
|
idx_t n,
|
|
162
173
|
const float* x,
|
|
163
174
|
idx_t k,
|
|
164
175
|
float* distances,
|
|
165
|
-
idx_t* labels
|
|
176
|
+
idx_t* labels,
|
|
177
|
+
const SearchParameters* params) const {
|
|
166
178
|
FAISS_THROW_IF_NOT(k > 0);
|
|
167
|
-
|
|
168
179
|
FAISS_THROW_IF_NOT(is_trained);
|
|
169
180
|
const float* xt = apply_chain(n, x);
|
|
170
181
|
ScopeDeleter<float> del(xt == x ? nullptr : xt);
|
|
171
|
-
index->search(
|
|
182
|
+
index->search(
|
|
183
|
+
n, xt, k, distances, labels, extract_index_search_params(params));
|
|
172
184
|
}
|
|
173
185
|
|
|
174
186
|
void IndexPreTransform::range_search(
|
|
175
187
|
idx_t n,
|
|
176
188
|
const float* x,
|
|
177
189
|
float radius,
|
|
178
|
-
RangeSearchResult* result
|
|
190
|
+
RangeSearchResult* result,
|
|
191
|
+
const SearchParameters* params) const {
|
|
179
192
|
FAISS_THROW_IF_NOT(is_trained);
|
|
180
193
|
const float* xt = apply_chain(n, x);
|
|
181
194
|
ScopeDeleter<float> del(xt == x ? nullptr : xt);
|
|
182
|
-
index->range_search(
|
|
195
|
+
index->range_search(
|
|
196
|
+
n, xt, radius, result, extract_index_search_params(params));
|
|
183
197
|
}
|
|
184
198
|
|
|
185
199
|
void IndexPreTransform::reset() {
|
|
@@ -219,9 +233,9 @@ void IndexPreTransform::search_and_reconstruct(
|
|
|
219
233
|
idx_t k,
|
|
220
234
|
float* distances,
|
|
221
235
|
idx_t* labels,
|
|
222
|
-
float* recons
|
|
236
|
+
float* recons,
|
|
237
|
+
const SearchParameters* params) const {
|
|
223
238
|
FAISS_THROW_IF_NOT(k > 0);
|
|
224
|
-
|
|
225
239
|
FAISS_THROW_IF_NOT(is_trained);
|
|
226
240
|
|
|
227
241
|
const float* xt = apply_chain(n, x);
|
|
@@ -229,7 +243,14 @@ void IndexPreTransform::search_and_reconstruct(
|
|
|
229
243
|
|
|
230
244
|
float* recons_temp = chain.empty() ? recons : new float[n * k * index->d];
|
|
231
245
|
ScopeDeleter<float> del2((recons_temp == recons) ? nullptr : recons_temp);
|
|
232
|
-
index->search_and_reconstruct(
|
|
246
|
+
index->search_and_reconstruct(
|
|
247
|
+
n,
|
|
248
|
+
xt,
|
|
249
|
+
k,
|
|
250
|
+
distances,
|
|
251
|
+
labels,
|
|
252
|
+
recons_temp,
|
|
253
|
+
extract_index_search_params(params));
|
|
233
254
|
|
|
234
255
|
// Revert transformations from last to first
|
|
235
256
|
reverse_chain(n * k, recons_temp, recons);
|
|
@@ -262,6 +283,24 @@ void IndexPreTransform::sa_decode(idx_t n, const uint8_t* bytes, float* x)
|
|
|
262
283
|
}
|
|
263
284
|
}
|
|
264
285
|
|
|
286
|
+
void IndexPreTransform::merge_from(Index& otherIndex, idx_t add_id) {
|
|
287
|
+
check_compatible_for_merge(otherIndex);
|
|
288
|
+
auto other = static_cast<const IndexPreTransform*>(&otherIndex);
|
|
289
|
+
index->merge_from(*other->index, add_id);
|
|
290
|
+
ntotal = index->ntotal;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
void IndexPreTransform::check_compatible_for_merge(
|
|
294
|
+
const Index& otherIndex) const {
|
|
295
|
+
auto other = dynamic_cast<const IndexPreTransform*>(&otherIndex);
|
|
296
|
+
FAISS_THROW_IF_NOT(other);
|
|
297
|
+
FAISS_THROW_IF_NOT(chain.size() == other->chain.size());
|
|
298
|
+
for (int i = 0; i < chain.size(); i++) {
|
|
299
|
+
chain[i]->check_identical(*other->chain[i]);
|
|
300
|
+
}
|
|
301
|
+
index->check_compatible_for_merge(*other->index);
|
|
302
|
+
}
|
|
303
|
+
|
|
265
304
|
namespace {
|
|
266
305
|
|
|
267
306
|
struct PreTransformDistanceComputer : DistanceComputer {
|
|
@@ -14,6 +14,12 @@
|
|
|
14
14
|
|
|
15
15
|
namespace faiss {
|
|
16
16
|
|
|
17
|
+
struct SearchParametersPreTransform : SearchParameters {
|
|
18
|
+
// nothing to add here.
|
|
19
|
+
// as such, encapsulating the search params is considered optional
|
|
20
|
+
SearchParameters* index_params = nullptr;
|
|
21
|
+
};
|
|
22
|
+
|
|
17
23
|
/** Index that applies a LinearTransform transform on vectors before
|
|
18
24
|
* handing them over to a sub-index */
|
|
19
25
|
struct IndexPreTransform : Index {
|
|
@@ -48,14 +54,16 @@ struct IndexPreTransform : Index {
|
|
|
48
54
|
const float* x,
|
|
49
55
|
idx_t k,
|
|
50
56
|
float* distances,
|
|
51
|
-
idx_t* labels
|
|
57
|
+
idx_t* labels,
|
|
58
|
+
const SearchParameters* params = nullptr) const override;
|
|
52
59
|
|
|
53
60
|
/* range search, no attempt is done to change the radius */
|
|
54
61
|
void range_search(
|
|
55
62
|
idx_t n,
|
|
56
63
|
const float* x,
|
|
57
64
|
float radius,
|
|
58
|
-
RangeSearchResult* result
|
|
65
|
+
RangeSearchResult* result,
|
|
66
|
+
const SearchParameters* params = nullptr) const override;
|
|
59
67
|
|
|
60
68
|
void reconstruct(idx_t key, float* recons) const override;
|
|
61
69
|
|
|
@@ -67,7 +75,8 @@ struct IndexPreTransform : Index {
|
|
|
67
75
|
idx_t k,
|
|
68
76
|
float* distances,
|
|
69
77
|
idx_t* labels,
|
|
70
|
-
float* recons
|
|
78
|
+
float* recons,
|
|
79
|
+
const SearchParameters* params = nullptr) const override;
|
|
71
80
|
|
|
72
81
|
/// apply the transforms in the chain. The returned float * may be
|
|
73
82
|
/// equal to x, otherwise it should be deallocated.
|
|
@@ -84,6 +93,9 @@ struct IndexPreTransform : Index {
|
|
|
84
93
|
void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override;
|
|
85
94
|
void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override;
|
|
86
95
|
|
|
96
|
+
void merge_from(Index& otherIndex, idx_t add_id = 0) override;
|
|
97
|
+
void check_compatible_for_merge(const Index& otherIndex) const override;
|
|
98
|
+
|
|
87
99
|
~IndexPreTransform() override;
|
|
88
100
|
};
|
|
89
101
|
|
|
@@ -95,9 +95,11 @@ void IndexRefine::search(
|
|
|
95
95
|
const float* x,
|
|
96
96
|
idx_t k,
|
|
97
97
|
float* distances,
|
|
98
|
-
idx_t* labels
|
|
98
|
+
idx_t* labels,
|
|
99
|
+
const SearchParameters* params) const {
|
|
100
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
101
|
+
!params, "search params not supported for this index");
|
|
99
102
|
FAISS_THROW_IF_NOT(k > 0);
|
|
100
|
-
|
|
101
103
|
FAISS_THROW_IF_NOT(is_trained);
|
|
102
104
|
idx_t k_base = idx_t(k * k_factor);
|
|
103
105
|
idx_t* base_labels = labels;
|
|
@@ -155,6 +157,34 @@ void IndexRefine::reconstruct(idx_t key, float* recons) const {
|
|
|
155
157
|
refine_index->reconstruct(key, recons);
|
|
156
158
|
}
|
|
157
159
|
|
|
160
|
+
size_t IndexRefine::sa_code_size() const {
|
|
161
|
+
return base_index->sa_code_size() + refine_index->sa_code_size();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
void IndexRefine::sa_encode(idx_t n, const float* x, uint8_t* bytes) const {
|
|
165
|
+
size_t cs1 = base_index->sa_code_size(), cs2 = refine_index->sa_code_size();
|
|
166
|
+
std::unique_ptr<uint8_t[]> tmp1(new uint8_t[n * cs1]);
|
|
167
|
+
base_index->sa_encode(n, x, tmp1.get());
|
|
168
|
+
std::unique_ptr<uint8_t[]> tmp2(new uint8_t[n * cs2]);
|
|
169
|
+
refine_index->sa_encode(n, x, tmp2.get());
|
|
170
|
+
for (size_t i = 0; i < n; i++) {
|
|
171
|
+
uint8_t* b = bytes + i * (cs1 + cs2);
|
|
172
|
+
memcpy(b, tmp1.get() + cs1 * i, cs1);
|
|
173
|
+
memcpy(b + cs1, tmp2.get() + cs2 * i, cs2);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
void IndexRefine::sa_decode(idx_t n, const uint8_t* bytes, float* x) const {
|
|
178
|
+
size_t cs1 = base_index->sa_code_size(), cs2 = refine_index->sa_code_size();
|
|
179
|
+
std::unique_ptr<uint8_t[]> tmp2(
|
|
180
|
+
new uint8_t[n * refine_index->sa_code_size()]);
|
|
181
|
+
for (size_t i = 0; i < n; i++) {
|
|
182
|
+
memcpy(tmp2.get() + i * cs2, bytes + i * (cs1 + cs2), cs2);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
refine_index->sa_decode(n, tmp2.get(), x);
|
|
186
|
+
}
|
|
187
|
+
|
|
158
188
|
IndexRefine::~IndexRefine() {
|
|
159
189
|
if (own_fields)
|
|
160
190
|
delete base_index;
|
|
@@ -194,9 +224,11 @@ void IndexRefineFlat::search(
|
|
|
194
224
|
const float* x,
|
|
195
225
|
idx_t k,
|
|
196
226
|
float* distances,
|
|
197
|
-
idx_t* labels
|
|
227
|
+
idx_t* labels,
|
|
228
|
+
const SearchParameters* params) const {
|
|
229
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
230
|
+
!params, "search params not supported for this index");
|
|
198
231
|
FAISS_THROW_IF_NOT(k > 0);
|
|
199
|
-
|
|
200
232
|
FAISS_THROW_IF_NOT(is_trained);
|
|
201
233
|
idx_t k_base = idx_t(k * k_factor);
|
|
202
234
|
idx_t* base_labels = labels;
|
|
@@ -44,11 +44,22 @@ struct IndexRefine : Index {
|
|
|
44
44
|
const float* x,
|
|
45
45
|
idx_t k,
|
|
46
46
|
float* distances,
|
|
47
|
-
idx_t* labels
|
|
47
|
+
idx_t* labels,
|
|
48
|
+
const SearchParameters* params = nullptr) const override;
|
|
48
49
|
|
|
49
50
|
// reconstruct is routed to the refine_index
|
|
50
51
|
void reconstruct(idx_t key, float* recons) const override;
|
|
51
52
|
|
|
53
|
+
/* standalone codec interface: the base_index codes are interleaved with the
|
|
54
|
+
* refine_index ones */
|
|
55
|
+
size_t sa_code_size() const override;
|
|
56
|
+
|
|
57
|
+
void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override;
|
|
58
|
+
|
|
59
|
+
/// The sa_decode decodes from the index_refine, which is assumed to be more
|
|
60
|
+
/// accurate
|
|
61
|
+
void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override;
|
|
62
|
+
|
|
52
63
|
~IndexRefine() override;
|
|
53
64
|
};
|
|
54
65
|
|
|
@@ -66,7 +77,8 @@ struct IndexRefineFlat : IndexRefine {
|
|
|
66
77
|
const float* x,
|
|
67
78
|
idx_t k,
|
|
68
79
|
float* distances,
|
|
69
|
-
idx_t* labels
|
|
80
|
+
idx_t* labels,
|
|
81
|
+
const SearchParameters* params = nullptr) const override;
|
|
70
82
|
};
|
|
71
83
|
|
|
72
84
|
} // namespace faiss
|
|
@@ -108,9 +108,11 @@ void IndexReplicasTemplate<IndexT>::search(
|
|
|
108
108
|
const component_t* x,
|
|
109
109
|
idx_t k,
|
|
110
110
|
distance_t* distances,
|
|
111
|
-
idx_t* labels
|
|
111
|
+
idx_t* labels,
|
|
112
|
+
const SearchParameters* params) const {
|
|
113
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
114
|
+
!params, "search params not supported for this index");
|
|
112
115
|
FAISS_THROW_IF_NOT(k > 0);
|
|
113
|
-
|
|
114
116
|
FAISS_THROW_IF_NOT_MSG(this->count() > 0, "no replicas in index");
|
|
115
117
|
|
|
116
118
|
if (n == 0) {
|
|
@@ -65,7 +65,8 @@ class IndexReplicasTemplate : public ThreadedIndex<IndexT> {
|
|
|
65
65
|
const component_t* x,
|
|
66
66
|
idx_t k,
|
|
67
67
|
distance_t* distances,
|
|
68
|
-
idx_t* labels
|
|
68
|
+
idx_t* labels,
|
|
69
|
+
const SearchParameters* params = nullptr) const override;
|
|
69
70
|
|
|
70
71
|
/// reconstructs from the first index
|
|
71
72
|
void reconstruct(idx_t, component_t* v) const override;
|