faiss 0.3.0 → 0.3.2
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 +1 -1
- data/ext/faiss/extconf.rb +9 -2
- data/ext/faiss/index.cpp +1 -1
- data/ext/faiss/index_binary.cpp +2 -2
- data/ext/faiss/product_quantizer.cpp +1 -1
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/AutoTune.cpp +7 -7
- data/vendor/faiss/faiss/AutoTune.h +1 -2
- data/vendor/faiss/faiss/Clustering.cpp +39 -22
- data/vendor/faiss/faiss/Clustering.h +40 -21
- data/vendor/faiss/faiss/IVFlib.cpp +26 -12
- data/vendor/faiss/faiss/Index.cpp +1 -1
- data/vendor/faiss/faiss/Index.h +40 -10
- data/vendor/faiss/faiss/Index2Layer.cpp +7 -7
- data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +176 -166
- data/vendor/faiss/faiss/IndexAdditiveQuantizerFastScan.cpp +15 -15
- data/vendor/faiss/faiss/IndexBinary.cpp +9 -4
- data/vendor/faiss/faiss/IndexBinary.h +8 -19
- data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +2 -1
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +24 -31
- data/vendor/faiss/faiss/IndexBinaryHNSW.h +1 -1
- data/vendor/faiss/faiss/IndexBinaryHash.cpp +25 -50
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +107 -188
- data/vendor/faiss/faiss/IndexFastScan.cpp +95 -146
- data/vendor/faiss/faiss/IndexFastScan.h +9 -8
- data/vendor/faiss/faiss/IndexFlat.cpp +206 -10
- data/vendor/faiss/faiss/IndexFlat.h +20 -1
- data/vendor/faiss/faiss/IndexFlatCodes.cpp +170 -5
- data/vendor/faiss/faiss/IndexFlatCodes.h +23 -4
- data/vendor/faiss/faiss/IndexHNSW.cpp +231 -382
- data/vendor/faiss/faiss/IndexHNSW.h +62 -49
- data/vendor/faiss/faiss/IndexIDMap.cpp +69 -28
- data/vendor/faiss/faiss/IndexIDMap.h +24 -2
- data/vendor/faiss/faiss/IndexIVF.cpp +162 -56
- data/vendor/faiss/faiss/IndexIVF.h +46 -6
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +33 -26
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.h +6 -2
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.cpp +19 -46
- data/vendor/faiss/faiss/IndexIVFAdditiveQuantizerFastScan.h +4 -3
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +502 -401
- data/vendor/faiss/faiss/IndexIVFFastScan.h +63 -26
- data/vendor/faiss/faiss/IndexIVFFlat.cpp +15 -5
- data/vendor/faiss/faiss/IndexIVFFlat.h +3 -2
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.cpp +172 -0
- data/vendor/faiss/faiss/IndexIVFIndependentQuantizer.h +56 -0
- data/vendor/faiss/faiss/IndexIVFPQ.cpp +79 -125
- data/vendor/faiss/faiss/IndexIVFPQ.h +6 -7
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +39 -52
- data/vendor/faiss/faiss/IndexIVFPQFastScan.h +4 -3
- data/vendor/faiss/faiss/IndexIVFPQR.cpp +45 -29
- data/vendor/faiss/faiss/IndexIVFPQR.h +5 -2
- data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +25 -27
- data/vendor/faiss/faiss/IndexIVFSpectralHash.h +6 -6
- data/vendor/faiss/faiss/IndexLSH.cpp +14 -16
- data/vendor/faiss/faiss/IndexLattice.cpp +1 -19
- data/vendor/faiss/faiss/IndexLattice.h +3 -22
- data/vendor/faiss/faiss/IndexNNDescent.cpp +3 -33
- data/vendor/faiss/faiss/IndexNNDescent.h +1 -1
- data/vendor/faiss/faiss/IndexNSG.cpp +11 -27
- data/vendor/faiss/faiss/IndexNSG.h +11 -11
- data/vendor/faiss/faiss/IndexNeuralNetCodec.cpp +56 -0
- data/vendor/faiss/faiss/IndexNeuralNetCodec.h +49 -0
- data/vendor/faiss/faiss/IndexPQ.cpp +72 -88
- data/vendor/faiss/faiss/IndexPQ.h +1 -4
- data/vendor/faiss/faiss/IndexPQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexPreTransform.cpp +25 -31
- data/vendor/faiss/faiss/IndexPreTransform.h +1 -1
- data/vendor/faiss/faiss/IndexRefine.cpp +54 -24
- data/vendor/faiss/faiss/IndexRefine.h +7 -0
- data/vendor/faiss/faiss/IndexReplicas.cpp +23 -26
- data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +25 -17
- data/vendor/faiss/faiss/IndexScalarQuantizer.h +6 -4
- data/vendor/faiss/faiss/IndexShards.cpp +21 -29
- data/vendor/faiss/faiss/IndexShardsIVF.cpp +1 -2
- data/vendor/faiss/faiss/MatrixStats.cpp +17 -32
- data/vendor/faiss/faiss/MatrixStats.h +21 -9
- data/vendor/faiss/faiss/MetaIndexes.cpp +35 -35
- data/vendor/faiss/faiss/MetricType.h +7 -2
- data/vendor/faiss/faiss/VectorTransform.cpp +13 -26
- data/vendor/faiss/faiss/VectorTransform.h +7 -7
- data/vendor/faiss/faiss/clone_index.cpp +15 -10
- data/vendor/faiss/faiss/clone_index.h +3 -0
- data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +95 -17
- data/vendor/faiss/faiss/cppcontrib/factory_tools.cpp +152 -0
- data/vendor/faiss/faiss/cppcontrib/factory_tools.h +24 -0
- data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +83 -30
- data/vendor/faiss/faiss/gpu/GpuCloner.cpp +123 -8
- data/vendor/faiss/faiss/gpu/GpuCloner.h +22 -0
- data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +13 -0
- data/vendor/faiss/faiss/gpu/GpuDistance.h +46 -38
- data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -1
- data/vendor/faiss/faiss/gpu/GpuIndex.h +30 -12
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +282 -0
- data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +4 -4
- data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +14 -9
- data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +20 -3
- data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +22 -11
- data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +1 -3
- data/vendor/faiss/faiss/gpu/GpuResources.cpp +24 -3
- data/vendor/faiss/faiss/gpu/GpuResources.h +39 -11
- data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +142 -17
- data/vendor/faiss/faiss/gpu/StandardGpuResources.h +57 -3
- data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +26 -21
- data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +7 -1
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +8 -5
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +25 -0
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +129 -9
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +332 -40
- data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +299 -208
- data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +1 -0
- data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +1 -1
- data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +6 -0
- data/vendor/faiss/faiss/gpu/utils/RaftUtils.h +75 -0
- data/vendor/faiss/faiss/gpu/utils/Timer.cpp +4 -1
- data/vendor/faiss/faiss/gpu/utils/Timer.h +1 -1
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +3 -1
- data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +5 -5
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +26 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.h +10 -3
- data/vendor/faiss/faiss/impl/DistanceComputer.h +70 -1
- data/vendor/faiss/faiss/impl/FaissAssert.h +4 -2
- data/vendor/faiss/faiss/impl/FaissException.h +13 -34
- data/vendor/faiss/faiss/impl/HNSW.cpp +605 -186
- data/vendor/faiss/faiss/impl/HNSW.h +52 -30
- data/vendor/faiss/faiss/impl/IDSelector.h +4 -4
- data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +11 -9
- data/vendor/faiss/faiss/impl/LookupTableScaler.h +34 -0
- data/vendor/faiss/faiss/impl/NNDescent.cpp +42 -27
- data/vendor/faiss/faiss/impl/NSG.cpp +0 -29
- data/vendor/faiss/faiss/impl/NSG.h +1 -1
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +14 -12
- data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.h +1 -1
- data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +25 -22
- data/vendor/faiss/faiss/impl/ProductQuantizer.h +6 -2
- data/vendor/faiss/faiss/impl/Quantizer.h +1 -1
- data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +27 -1015
- data/vendor/faiss/faiss/impl/ResidualQuantizer.h +5 -63
- data/vendor/faiss/faiss/impl/ResultHandler.h +347 -172
- data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +1104 -147
- data/vendor/faiss/faiss/impl/ScalarQuantizer.h +3 -8
- data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +285 -42
- data/vendor/faiss/faiss/impl/code_distance/code_distance-avx512.h +248 -0
- data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +21 -14
- data/vendor/faiss/faiss/impl/code_distance/code_distance.h +22 -12
- data/vendor/faiss/faiss/impl/index_read.cpp +74 -34
- data/vendor/faiss/faiss/impl/index_read_utils.h +37 -0
- data/vendor/faiss/faiss/impl/index_write.cpp +88 -51
- data/vendor/faiss/faiss/impl/io.cpp +23 -15
- data/vendor/faiss/faiss/impl/io.h +4 -4
- data/vendor/faiss/faiss/impl/io_macros.h +6 -0
- data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -1
- data/vendor/faiss/faiss/impl/platform_macros.h +40 -1
- data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +14 -0
- data/vendor/faiss/faiss/impl/pq4_fast_scan.h +7 -6
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +52 -38
- data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +487 -49
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.cpp +960 -0
- data/vendor/faiss/faiss/impl/residual_quantizer_encode_steps.h +176 -0
- data/vendor/faiss/faiss/impl/simd_result_handlers.h +481 -225
- data/vendor/faiss/faiss/index_factory.cpp +41 -20
- data/vendor/faiss/faiss/index_io.h +12 -5
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +28 -8
- data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +3 -0
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +10 -2
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +73 -17
- data/vendor/faiss/faiss/invlists/InvertedLists.h +26 -8
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +24 -9
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +2 -1
- data/vendor/faiss/faiss/python/python_callbacks.cpp +4 -4
- data/vendor/faiss/faiss/utils/Heap.cpp +3 -1
- data/vendor/faiss/faiss/utils/Heap.h +105 -0
- data/vendor/faiss/faiss/utils/NeuralNet.cpp +342 -0
- data/vendor/faiss/faiss/utils/NeuralNet.h +147 -0
- data/vendor/faiss/faiss/utils/WorkerThread.h +1 -0
- data/vendor/faiss/faiss/utils/bf16.h +36 -0
- data/vendor/faiss/faiss/utils/distances.cpp +147 -123
- data/vendor/faiss/faiss/utils/distances.h +86 -9
- data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +5 -5
- data/vendor/faiss/faiss/utils/distances_fused/avx512.h +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +2 -2
- data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +1 -1
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +5 -5
- data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +1 -1
- data/vendor/faiss/faiss/utils/distances_simd.cpp +1589 -243
- data/vendor/faiss/faiss/utils/extra_distances-inl.h +70 -0
- data/vendor/faiss/faiss/utils/extra_distances.cpp +85 -137
- data/vendor/faiss/faiss/utils/extra_distances.h +3 -2
- data/vendor/faiss/faiss/utils/fp16-arm.h +29 -0
- data/vendor/faiss/faiss/utils/fp16.h +2 -0
- data/vendor/faiss/faiss/utils/hamming.cpp +163 -111
- data/vendor/faiss/faiss/utils/hamming.h +58 -0
- data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +16 -89
- data/vendor/faiss/faiss/utils/hamming_distance/common.h +1 -0
- data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +19 -88
- data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +58 -0
- data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +14 -104
- data/vendor/faiss/faiss/utils/partitioning.cpp +3 -4
- data/vendor/faiss/faiss/utils/prefetch.h +77 -0
- data/vendor/faiss/faiss/utils/quantize_lut.cpp +0 -14
- data/vendor/faiss/faiss/utils/random.cpp +43 -0
- data/vendor/faiss/faiss/utils/random.h +25 -0
- data/vendor/faiss/faiss/utils/simdlib.h +10 -1
- data/vendor/faiss/faiss/utils/simdlib_avx2.h +0 -6
- data/vendor/faiss/faiss/utils/simdlib_avx512.h +296 -0
- data/vendor/faiss/faiss/utils/simdlib_neon.h +77 -79
- data/vendor/faiss/faiss/utils/simdlib_ppc64.h +1084 -0
- data/vendor/faiss/faiss/utils/sorting.cpp +140 -5
- data/vendor/faiss/faiss/utils/sorting.h +27 -0
- data/vendor/faiss/faiss/utils/transpose/transpose-avx512-inl.h +176 -0
- data/vendor/faiss/faiss/utils/utils.cpp +120 -7
- data/vendor/faiss/faiss/utils/utils.h +60 -20
- metadata +23 -4
- data/vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h +0 -102
|
@@ -18,7 +18,9 @@ namespace faiss {
|
|
|
18
18
|
|
|
19
19
|
/** Index that stores the full vectors and performs exhaustive search */
|
|
20
20
|
struct IndexFlat : IndexFlatCodes {
|
|
21
|
-
explicit IndexFlat(
|
|
21
|
+
explicit IndexFlat(
|
|
22
|
+
idx_t d, ///< dimensionality of the input vectors
|
|
23
|
+
MetricType metric = METRIC_L2);
|
|
22
24
|
|
|
23
25
|
void search(
|
|
24
26
|
idx_t n,
|
|
@@ -76,8 +78,25 @@ struct IndexFlatIP : IndexFlat {
|
|
|
76
78
|
};
|
|
77
79
|
|
|
78
80
|
struct IndexFlatL2 : IndexFlat {
|
|
81
|
+
// Special cache for L2 norms.
|
|
82
|
+
// If this cache is set, then get_distance_computer() returns
|
|
83
|
+
// a special version that computes the distance using dot products
|
|
84
|
+
// and l2 norms.
|
|
85
|
+
std::vector<float> cached_l2norms;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param d dimensionality of the input vectors
|
|
89
|
+
*/
|
|
79
90
|
explicit IndexFlatL2(idx_t d) : IndexFlat(d, METRIC_L2) {}
|
|
80
91
|
IndexFlatL2() {}
|
|
92
|
+
|
|
93
|
+
// override for l2 norms cache.
|
|
94
|
+
FlatCodesDistanceComputer* get_FlatCodesDistanceComputer() const override;
|
|
95
|
+
|
|
96
|
+
// compute L2 norms
|
|
97
|
+
void sync_l2norms();
|
|
98
|
+
// clear L2 norms
|
|
99
|
+
void clear_l2norms();
|
|
81
100
|
};
|
|
82
101
|
|
|
83
102
|
/// optimized version for 1D "vectors".
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
#include <faiss/impl/DistanceComputer.h>
|
|
13
13
|
#include <faiss/impl/FaissAssert.h>
|
|
14
14
|
#include <faiss/impl/IDSelector.h>
|
|
15
|
+
#include <faiss/impl/ResultHandler.h>
|
|
16
|
+
#include <faiss/utils/extra_distances.h>
|
|
15
17
|
|
|
16
18
|
namespace faiss {
|
|
17
19
|
|
|
@@ -70,11 +72,6 @@ void IndexFlatCodes::reconstruct(idx_t key, float* recons) const {
|
|
|
70
72
|
reconstruct_n(key, 1, recons);
|
|
71
73
|
}
|
|
72
74
|
|
|
73
|
-
FlatCodesDistanceComputer* IndexFlatCodes::get_FlatCodesDistanceComputer()
|
|
74
|
-
const {
|
|
75
|
-
FAISS_THROW_MSG("not implemented");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
75
|
void IndexFlatCodes::check_compatible_for_merge(const Index& otherIndex) const {
|
|
79
76
|
// minimal sanity checks
|
|
80
77
|
const IndexFlatCodes* other =
|
|
@@ -103,4 +100,172 @@ CodePacker* IndexFlatCodes::get_CodePacker() const {
|
|
|
103
100
|
return new CodePackerFlat(code_size);
|
|
104
101
|
}
|
|
105
102
|
|
|
103
|
+
void IndexFlatCodes::permute_entries(const idx_t* perm) {
|
|
104
|
+
std::vector<uint8_t> new_codes(codes.size());
|
|
105
|
+
|
|
106
|
+
for (idx_t i = 0; i < ntotal; i++) {
|
|
107
|
+
memcpy(new_codes.data() + i * code_size,
|
|
108
|
+
codes.data() + perm[i] * code_size,
|
|
109
|
+
code_size);
|
|
110
|
+
}
|
|
111
|
+
std::swap(codes, new_codes);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
namespace {
|
|
115
|
+
|
|
116
|
+
template <class VD>
|
|
117
|
+
struct GenericFlatCodesDistanceComputer : FlatCodesDistanceComputer {
|
|
118
|
+
const IndexFlatCodes& codec;
|
|
119
|
+
const VD vd;
|
|
120
|
+
// temp buffers
|
|
121
|
+
std::vector<uint8_t> code_buffer;
|
|
122
|
+
std::vector<float> vec_buffer;
|
|
123
|
+
const float* query = nullptr;
|
|
124
|
+
|
|
125
|
+
GenericFlatCodesDistanceComputer(const IndexFlatCodes* codec, const VD& vd)
|
|
126
|
+
: FlatCodesDistanceComputer(codec->codes.data(), codec->code_size),
|
|
127
|
+
codec(*codec),
|
|
128
|
+
vd(vd),
|
|
129
|
+
code_buffer(codec->code_size * 4),
|
|
130
|
+
vec_buffer(codec->d * 4) {}
|
|
131
|
+
|
|
132
|
+
void set_query(const float* x) override {
|
|
133
|
+
query = x;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
float operator()(idx_t i) override {
|
|
137
|
+
codec.sa_decode(1, codes + i * code_size, vec_buffer.data());
|
|
138
|
+
return vd(query, vec_buffer.data());
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
float distance_to_code(const uint8_t* code) override {
|
|
142
|
+
codec.sa_decode(1, code, vec_buffer.data());
|
|
143
|
+
return vd(query, vec_buffer.data());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
float symmetric_dis(idx_t i, idx_t j) override {
|
|
147
|
+
codec.sa_decode(1, codes + i * code_size, vec_buffer.data());
|
|
148
|
+
codec.sa_decode(1, codes + j * code_size, vec_buffer.data() + vd.d);
|
|
149
|
+
return vd(vec_buffer.data(), vec_buffer.data() + vd.d);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
void distances_batch_4(
|
|
153
|
+
const idx_t idx0,
|
|
154
|
+
const idx_t idx1,
|
|
155
|
+
const idx_t idx2,
|
|
156
|
+
const idx_t idx3,
|
|
157
|
+
float& dis0,
|
|
158
|
+
float& dis1,
|
|
159
|
+
float& dis2,
|
|
160
|
+
float& dis3) override {
|
|
161
|
+
uint8_t* cp = code_buffer.data();
|
|
162
|
+
for (idx_t i : {idx0, idx1, idx2, idx3}) {
|
|
163
|
+
memcpy(cp, codes + i * code_size, code_size);
|
|
164
|
+
cp += code_size;
|
|
165
|
+
}
|
|
166
|
+
// potential benefit is if batch decoding is more efficient than 1 by 1
|
|
167
|
+
// decoding
|
|
168
|
+
codec.sa_decode(4, code_buffer.data(), vec_buffer.data());
|
|
169
|
+
dis0 = vd(query, vec_buffer.data());
|
|
170
|
+
dis1 = vd(query, vec_buffer.data() + vd.d);
|
|
171
|
+
dis2 = vd(query, vec_buffer.data() + 2 * vd.d);
|
|
172
|
+
dis3 = vd(query, vec_buffer.data() + 3 * vd.d);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
struct Run_get_distance_computer {
|
|
177
|
+
using T = FlatCodesDistanceComputer*;
|
|
178
|
+
|
|
179
|
+
template <class VD>
|
|
180
|
+
FlatCodesDistanceComputer* f(const VD& vd, const IndexFlatCodes* codec) {
|
|
181
|
+
return new GenericFlatCodesDistanceComputer<VD>(codec, vd);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
template <class BlockResultHandler>
|
|
186
|
+
struct Run_search_with_decompress {
|
|
187
|
+
using T = void;
|
|
188
|
+
|
|
189
|
+
template <class VectorDistance>
|
|
190
|
+
void f(VectorDistance& vd,
|
|
191
|
+
const IndexFlatCodes* index_ptr,
|
|
192
|
+
const float* xq,
|
|
193
|
+
BlockResultHandler& res) {
|
|
194
|
+
// Note that there seems to be a clang (?) bug that "sometimes" passes
|
|
195
|
+
// the const Index & parameters by value, so to be on the safe side,
|
|
196
|
+
// it's better to use pointers.
|
|
197
|
+
const IndexFlatCodes& index = *index_ptr;
|
|
198
|
+
size_t ntotal = index.ntotal;
|
|
199
|
+
using SingleResultHandler =
|
|
200
|
+
typename BlockResultHandler::SingleResultHandler;
|
|
201
|
+
using DC = GenericFlatCodesDistanceComputer<VectorDistance>;
|
|
202
|
+
#pragma omp parallel // if (res.nq > 100)
|
|
203
|
+
{
|
|
204
|
+
std::unique_ptr<DC> dc(new DC(&index, vd));
|
|
205
|
+
SingleResultHandler resi(res);
|
|
206
|
+
#pragma omp for
|
|
207
|
+
for (int64_t q = 0; q < res.nq; q++) {
|
|
208
|
+
resi.begin(q);
|
|
209
|
+
dc->set_query(xq + vd.d * q);
|
|
210
|
+
for (size_t i = 0; i < ntotal; i++) {
|
|
211
|
+
if (res.is_in_selection(i)) {
|
|
212
|
+
float dis = (*dc)(i);
|
|
213
|
+
resi.add_result(dis, i);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
resi.end();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
struct Run_search_with_decompress_res {
|
|
223
|
+
using T = void;
|
|
224
|
+
|
|
225
|
+
template <class ResultHandler>
|
|
226
|
+
void f(ResultHandler& res, const IndexFlatCodes* index, const float* xq) {
|
|
227
|
+
Run_search_with_decompress<ResultHandler> r;
|
|
228
|
+
dispatch_VectorDistance(
|
|
229
|
+
index->d,
|
|
230
|
+
index->metric_type,
|
|
231
|
+
index->metric_arg,
|
|
232
|
+
r,
|
|
233
|
+
index,
|
|
234
|
+
xq,
|
|
235
|
+
res);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
} // anonymous namespace
|
|
240
|
+
|
|
241
|
+
FlatCodesDistanceComputer* IndexFlatCodes::get_FlatCodesDistanceComputer()
|
|
242
|
+
const {
|
|
243
|
+
Run_get_distance_computer r;
|
|
244
|
+
return dispatch_VectorDistance(d, metric_type, metric_arg, r, this);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
void IndexFlatCodes::search(
|
|
248
|
+
idx_t n,
|
|
249
|
+
const float* x,
|
|
250
|
+
idx_t k,
|
|
251
|
+
float* distances,
|
|
252
|
+
idx_t* labels,
|
|
253
|
+
const SearchParameters* params) const {
|
|
254
|
+
Run_search_with_decompress_res r;
|
|
255
|
+
const IDSelector* sel = params ? params->sel : nullptr;
|
|
256
|
+
dispatch_knn_ResultHandler(
|
|
257
|
+
n, distances, labels, k, metric_type, sel, r, this, x);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
void IndexFlatCodes::range_search(
|
|
261
|
+
idx_t n,
|
|
262
|
+
const float* x,
|
|
263
|
+
float radius,
|
|
264
|
+
RangeSearchResult* result,
|
|
265
|
+
const SearchParameters* params) const {
|
|
266
|
+
const IDSelector* sel = params ? params->sel : nullptr;
|
|
267
|
+
Run_search_with_decompress_res r;
|
|
268
|
+
dispatch_range_ResultHandler(result, radius, metric_type, sel, r, this, x);
|
|
269
|
+
}
|
|
270
|
+
|
|
106
271
|
} // namespace faiss
|
|
@@ -5,8 +5,6 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
// -*- c++ -*-
|
|
9
|
-
|
|
10
8
|
#pragma once
|
|
11
9
|
|
|
12
10
|
#include <faiss/Index.h>
|
|
@@ -34,7 +32,6 @@ struct IndexFlatCodes : Index {
|
|
|
34
32
|
|
|
35
33
|
void reset() override;
|
|
36
34
|
|
|
37
|
-
/// reconstruction using the codec interface
|
|
38
35
|
void reconstruct_n(idx_t i0, idx_t ni, float* recons) const override;
|
|
39
36
|
|
|
40
37
|
void reconstruct(idx_t key, float* recons) const override;
|
|
@@ -46,19 +43,41 @@ struct IndexFlatCodes : Index {
|
|
|
46
43
|
* different from the usual ones: the new ids are shifted */
|
|
47
44
|
size_t remove_ids(const IDSelector& sel) override;
|
|
48
45
|
|
|
49
|
-
/** a FlatCodesDistanceComputer offers a distance_to_code method
|
|
46
|
+
/** a FlatCodesDistanceComputer offers a distance_to_code method
|
|
47
|
+
*
|
|
48
|
+
* The default implementation explicitly decodes the vector with sa_decode.
|
|
49
|
+
*/
|
|
50
50
|
virtual FlatCodesDistanceComputer* get_FlatCodesDistanceComputer() const;
|
|
51
51
|
|
|
52
52
|
DistanceComputer* get_distance_computer() const override {
|
|
53
53
|
return get_FlatCodesDistanceComputer();
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/** Search implemented by decoding */
|
|
57
|
+
void search(
|
|
58
|
+
idx_t n,
|
|
59
|
+
const float* x,
|
|
60
|
+
idx_t k,
|
|
61
|
+
float* distances,
|
|
62
|
+
idx_t* labels,
|
|
63
|
+
const SearchParameters* params = nullptr) const override;
|
|
64
|
+
|
|
65
|
+
void range_search(
|
|
66
|
+
idx_t n,
|
|
67
|
+
const float* x,
|
|
68
|
+
float radius,
|
|
69
|
+
RangeSearchResult* result,
|
|
70
|
+
const SearchParameters* params = nullptr) const override;
|
|
71
|
+
|
|
56
72
|
// returns a new instance of a CodePacker
|
|
57
73
|
CodePacker* get_CodePacker() const;
|
|
58
74
|
|
|
59
75
|
void check_compatible_for_merge(const Index& otherIndex) const override;
|
|
60
76
|
|
|
61
77
|
virtual void merge_from(Index& otherIndex, idx_t add_id = 0) override;
|
|
78
|
+
|
|
79
|
+
// permute_entries. perm of size ntotal maps new to old positions
|
|
80
|
+
void permute_entries(const idx_t* perm);
|
|
62
81
|
};
|
|
63
82
|
|
|
64
83
|
} // namespace faiss
|