/root/doris/contrib/faiss/faiss/IndexAdditiveQuantizer.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #ifndef FAISS_INDEX_ADDITIVE_QUANTIZER_H |
9 | | #define FAISS_INDEX_ADDITIVE_QUANTIZER_H |
10 | | |
11 | | #include <faiss/impl/AdditiveQuantizer.h> |
12 | | |
13 | | #include <cstdint> |
14 | | #include <vector> |
15 | | |
16 | | #include <faiss/IndexFlatCodes.h> |
17 | | #include <faiss/impl/LocalSearchQuantizer.h> |
18 | | #include <faiss/impl/ProductAdditiveQuantizer.h> |
19 | | #include <faiss/impl/ResidualQuantizer.h> |
20 | | #include <faiss/impl/platform_macros.h> |
21 | | |
22 | | namespace faiss { |
23 | | |
24 | | /// Abstract class for additive quantizers. The search functions are in common. |
25 | | struct IndexAdditiveQuantizer : IndexFlatCodes { |
26 | | // the quantizer, this points to the relevant field in the inheriting |
27 | | // classes |
28 | | AdditiveQuantizer* aq; |
29 | | using Search_type_t = AdditiveQuantizer::Search_type_t; |
30 | | |
31 | | explicit IndexAdditiveQuantizer( |
32 | | idx_t d, |
33 | | AdditiveQuantizer* aq, |
34 | | MetricType metric = METRIC_L2); |
35 | | |
36 | | void search( |
37 | | idx_t n, |
38 | | const float* x, |
39 | | idx_t k, |
40 | | float* distances, |
41 | | idx_t* labels, |
42 | | const SearchParameters* params = nullptr) const override; |
43 | | |
44 | | /* The standalone codec interface */ |
45 | | void sa_encode(idx_t n, const float* x, uint8_t* bytes) const override; |
46 | | |
47 | | void sa_decode(idx_t n, const uint8_t* bytes, float* x) const override; |
48 | | |
49 | | FlatCodesDistanceComputer* get_FlatCodesDistanceComputer() const override; |
50 | | }; |
51 | | |
52 | | /** Index based on a residual quantizer. Stored vectors are |
53 | | * approximated by residual quantization codes. |
54 | | * Can also be used as a codec |
55 | | */ |
56 | | struct IndexResidualQuantizer : IndexAdditiveQuantizer { |
57 | | /// The residual quantizer used to encode the vectors |
58 | | ResidualQuantizer rq; |
59 | | |
60 | | /** Constructor. |
61 | | * |
62 | | * @param d dimensionality of the input vectors |
63 | | * @param M number of subquantizers |
64 | | * @param nbits number of bit per subvector index |
65 | | */ |
66 | | IndexResidualQuantizer( |
67 | | int d, ///< dimensionality of the input vectors |
68 | | size_t M, ///< number of subquantizers |
69 | | size_t nbits, ///< number of bit per subvector index |
70 | | MetricType metric = METRIC_L2, |
71 | | Search_type_t search_type = AdditiveQuantizer::ST_decompress); |
72 | | |
73 | | IndexResidualQuantizer( |
74 | | int d, |
75 | | const std::vector<size_t>& nbits, |
76 | | MetricType metric = METRIC_L2, |
77 | | Search_type_t search_type = AdditiveQuantizer::ST_decompress); |
78 | | |
79 | | IndexResidualQuantizer(); |
80 | | |
81 | | void train(idx_t n, const float* x) override; |
82 | | }; |
83 | | |
84 | | struct IndexLocalSearchQuantizer : IndexAdditiveQuantizer { |
85 | | LocalSearchQuantizer lsq; |
86 | | |
87 | | /** Constructor. |
88 | | * |
89 | | * @param d dimensionality of the input vectors |
90 | | * @param M number of subquantizers |
91 | | * @param nbits number of bit per subvector index |
92 | | */ |
93 | | IndexLocalSearchQuantizer( |
94 | | int d, ///< dimensionality of the input vectors |
95 | | size_t M, ///< number of subquantizers |
96 | | size_t nbits, ///< number of bit per subvector index |
97 | | MetricType metric = METRIC_L2, |
98 | | Search_type_t search_type = AdditiveQuantizer::ST_decompress); |
99 | | |
100 | | IndexLocalSearchQuantizer(); |
101 | | |
102 | | void train(idx_t n, const float* x) override; |
103 | | }; |
104 | | |
105 | | /** Index based on a product residual quantizer. |
106 | | */ |
107 | | struct IndexProductResidualQuantizer : IndexAdditiveQuantizer { |
108 | | /// The product residual quantizer used to encode the vectors |
109 | | ProductResidualQuantizer prq; |
110 | | |
111 | | /** Constructor. |
112 | | * |
113 | | * @param d dimensionality of the input vectors |
114 | | * @param nsplits number of residual quantizers |
115 | | * @param Msub number of subquantizers per RQ |
116 | | * @param nbits number of bit per subvector index |
117 | | */ |
118 | | IndexProductResidualQuantizer( |
119 | | int d, ///< dimensionality of the input vectors |
120 | | size_t nsplits, ///< number of residual quantizers |
121 | | size_t Msub, ///< number of subquantizers per RQ |
122 | | size_t nbits, ///< number of bit per subvector index |
123 | | MetricType metric = METRIC_L2, |
124 | | Search_type_t search_type = AdditiveQuantizer::ST_decompress); |
125 | | |
126 | | IndexProductResidualQuantizer(); |
127 | | |
128 | | void train(idx_t n, const float* x) override; |
129 | | }; |
130 | | |
131 | | /** Index based on a product local search quantizer. |
132 | | */ |
133 | | struct IndexProductLocalSearchQuantizer : IndexAdditiveQuantizer { |
134 | | /// The product local search quantizer used to encode the vectors |
135 | | ProductLocalSearchQuantizer plsq; |
136 | | |
137 | | /** Constructor. |
138 | | * |
139 | | * @param d dimensionality of the input vectors |
140 | | * @param nsplits number of local search quantizers |
141 | | * @param Msub number of subquantizers per LSQ |
142 | | * @param nbits number of bit per subvector index |
143 | | */ |
144 | | IndexProductLocalSearchQuantizer( |
145 | | int d, ///< dimensionality of the input vectors |
146 | | size_t nsplits, ///< number of local search quantizers |
147 | | size_t Msub, ///< number of subquantizers per LSQ |
148 | | size_t nbits, ///< number of bit per subvector index |
149 | | MetricType metric = METRIC_L2, |
150 | | Search_type_t search_type = AdditiveQuantizer::ST_decompress); |
151 | | |
152 | | IndexProductLocalSearchQuantizer(); |
153 | | |
154 | | void train(idx_t n, const float* x) override; |
155 | | }; |
156 | | |
157 | | /** A "virtual" index where the elements are the residual quantizer centroids. |
158 | | * |
159 | | * Intended for use as a coarse quantizer in an IndexIVF. |
160 | | */ |
161 | | struct AdditiveCoarseQuantizer : Index { |
162 | | AdditiveQuantizer* aq; |
163 | | |
164 | | explicit AdditiveCoarseQuantizer( |
165 | | idx_t d = 0, |
166 | | AdditiveQuantizer* aq = nullptr, |
167 | | MetricType metric = METRIC_L2); |
168 | | |
169 | | /// norms of centroids, useful for knn-search |
170 | | std::vector<float> centroid_norms; |
171 | | |
172 | | /// N/A |
173 | | void add(idx_t n, const float* x) override; |
174 | | |
175 | | void search( |
176 | | idx_t n, |
177 | | const float* x, |
178 | | idx_t k, |
179 | | float* distances, |
180 | | idx_t* labels, |
181 | | const SearchParameters* params = nullptr) const override; |
182 | | |
183 | | void reconstruct(idx_t key, float* recons) const override; |
184 | | void train(idx_t n, const float* x) override; |
185 | | |
186 | | /// N/A |
187 | | void reset() override; |
188 | | }; |
189 | | |
190 | | struct SearchParametersResidualCoarseQuantizer : SearchParameters { |
191 | | float beam_factor = 4.0f; |
192 | 0 | ~SearchParametersResidualCoarseQuantizer() {} |
193 | | }; |
194 | | |
195 | | /** The ResidualCoarseQuantizer is a bit specialized compared to the |
196 | | * default AdditiveCoarseQuantizer because it can use a beam search |
197 | | * at search time (slow but may be useful for very large vocabularies) */ |
198 | | struct ResidualCoarseQuantizer : AdditiveCoarseQuantizer { |
199 | | /// The residual quantizer used to encode the vectors |
200 | | ResidualQuantizer rq; |
201 | | |
202 | | /// factor between the beam size and the search k |
203 | | /// if negative, use exact search-to-centroid |
204 | | float beam_factor = 4.0f; |
205 | | |
206 | | /// computes centroid norms if required |
207 | | void set_beam_factor(float new_beam_factor); |
208 | | |
209 | | /** Constructor. |
210 | | * |
211 | | * @param d dimensionality of the input vectors |
212 | | * @param M number of subquantizers |
213 | | * @param nbits number of bit per subvector index |
214 | | */ |
215 | | ResidualCoarseQuantizer( |
216 | | int d, ///< dimensionality of the input vectors |
217 | | size_t M, ///< number of subquantizers |
218 | | size_t nbits, ///< number of bit per subvector index |
219 | | MetricType metric = METRIC_L2); |
220 | | |
221 | | ResidualCoarseQuantizer( |
222 | | int d, |
223 | | const std::vector<size_t>& nbits, |
224 | | MetricType metric = METRIC_L2); |
225 | | |
226 | | void search( |
227 | | idx_t n, |
228 | | const float* x, |
229 | | idx_t k, |
230 | | float* distances, |
231 | | idx_t* labels, |
232 | | const SearchParameters* params = nullptr) const override; |
233 | | |
234 | | /** Copy the M first codebook levels from other. Useful to crop a |
235 | | * ResidualQuantizer to its first M quantizers. */ |
236 | | void initialize_from(const ResidualCoarseQuantizer& other); |
237 | | |
238 | | ResidualCoarseQuantizer(); |
239 | | }; |
240 | | |
241 | | struct LocalSearchCoarseQuantizer : AdditiveCoarseQuantizer { |
242 | | /// The residual quantizer used to encode the vectors |
243 | | LocalSearchQuantizer lsq; |
244 | | |
245 | | /** Constructor. |
246 | | * |
247 | | * @param d dimensionality of the input vectors |
248 | | * @param M number of subquantizers |
249 | | * @param nbits number of bit per subvector index |
250 | | */ |
251 | | LocalSearchCoarseQuantizer( |
252 | | int d, ///< dimensionality of the input vectors |
253 | | size_t M, ///< number of subquantizers |
254 | | size_t nbits, ///< number of bit per subvector index |
255 | | MetricType metric = METRIC_L2); |
256 | | |
257 | | LocalSearchCoarseQuantizer(); |
258 | | }; |
259 | | |
260 | | } // namespace faiss |
261 | | |
262 | | #endif |