forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaiss_vector_index.cpp
More file actions
403 lines (366 loc) · 18.2 KB
/
Copy pathfaiss_vector_index.cpp
File metadata and controls
403 lines (366 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "faiss_vector_index.h"
#include <faiss/index_io.h>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include "CLucene/store/IndexInput.h"
#include "CLucene/store/IndexOutput.h"
#include "common/exception.h"
#include "common/logging.h"
#include "common/status.h"
#include "faiss/IndexHNSW.h"
#include "faiss/impl/io.h"
#include "olap/rowset/segment_v2/ann_index/ann_search_params.h"
#include "util/metrics.h"
#include "vector/vector_index.h"
namespace doris::segment_v2 {
std::unique_ptr<faiss::IDSelector> FaissVectorIndex::roaring_to_faiss_selector(
const roaring::Roaring& roaring) {
std::vector<faiss::idx_t> ids;
ids.reserve(roaring.cardinality());
for (roaring::Roaring::const_iterator it = roaring.begin(); it != roaring.end(); ++it) {
ids.push_back(static_cast<faiss::idx_t>(*it));
}
return std::make_unique<faiss::IDSelectorBatch>(ids.size(), ids.data());
}
void FaissVectorIndex::update_roaring(const faiss::idx_t* labels, const size_t n,
roaring::Roaring& roaring) {
// make sure roaring is empty before adding new elements
DCHECK(roaring.cardinality() == 0);
for (size_t i = 0; i < n; ++i) {
if (labels[i] >= 0) {
roaring.add(labels[i]);
}
}
}
struct FaissIndexWriter : faiss::IOWriter {
public:
FaissIndexWriter() = default;
FaissIndexWriter(lucene::store::IndexOutput* output) : _output(output) {}
~FaissIndexWriter() override {
if (_output != nullptr) {
_output->close();
delete _output;
}
}
size_t operator()(const void* ptr, size_t size, size_t nitems) override {
size_t bytes = size * nitems;
if (bytes > 0) {
try {
_output->writeBytes(reinterpret_cast<const uint8_t*>(ptr), bytes);
} catch (const std::exception& e) {
throw doris::Exception(doris::ErrorCode::IO_ERROR,
"Failed to write vector index {}", e.what());
}
}
return nitems;
};
lucene::store::IndexOutput* _output = nullptr;
};
struct FaissIndexReader : faiss::IOReader {
public:
FaissIndexReader() = default;
FaissIndexReader(lucene::store::IndexInput* input) : _input(input) {}
~FaissIndexReader() override {
if (_input != nullptr) {
_input->close();
delete _input;
}
}
size_t operator()(void* ptr, size_t size, size_t nitems) override {
size_t bytes = size * nitems;
if (bytes > 0) {
try {
_input->readBytes(reinterpret_cast<uint8_t*>(ptr), bytes);
} catch (const std::exception& e) {
throw doris::Exception(doris::ErrorCode::IO_ERROR, "Failed to read vector index {}",
e.what());
}
}
return nitems;
};
lucene::store::IndexInput* _input = nullptr;
};
/** Add n vectors of dimension d to the index.
*
* Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
* This function slices the input vectors in chunks smaller than
* blocksize_add and calls add_core.
* @param n number of vectors
* @param x input matrix, size n * d
*/
doris::Status FaissVectorIndex::add(int n, const float* vec) {
DCHECK(vec != nullptr);
DCHECK(_index != nullptr);
_index->add(n, vec);
return doris::Status::OK();
}
void FaissVectorIndex::set_build_params(const FaissBuildParameter& params) {
_dimension = params.d;
switch (params.metric_type) {
case FaissBuildParameter::MetricType::L2:
_metric = Metric::L2;
break;
case FaissBuildParameter::MetricType::IP:
_metric = Metric::IP;
break;
default:
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT, "Unsupported metric type: {}",
static_cast<int>(params.metric_type));
break;
}
if (params.index_type == FaissBuildParameter::IndexType::BruteForce) {
if (params.metric_type == FaissBuildParameter::MetricType::L2) {
_index = std::make_unique<faiss::IndexFlatL2>(params.d);
} else if (params.metric_type == FaissBuildParameter::MetricType::IP) {
_index = std::make_unique<faiss::IndexFlatIP>(params.d);
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"Unsupported metric type: {}",
static_cast<int>(params.metric_type));
}
} else if (params.index_type == FaissBuildParameter::IndexType::HNSW) {
if (params.quantilizer == FaissBuildParameter::Quantilizer::FLAT) {
if (params.metric_type == FaissBuildParameter::MetricType::L2) {
_index = std::make_unique<faiss::IndexHNSWFlat>(params.d, params.m);
} else if (params.metric_type == FaissBuildParameter::MetricType::IP) {
_index = std::make_unique<faiss::IndexHNSWFlat>(params.d, params.m,
faiss::METRIC_INNER_PRODUCT);
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"Unsupported metric type: {}",
static_cast<int>(params.metric_type));
}
} else if (params.quantilizer == FaissBuildParameter::Quantilizer::PQ) {
if (params.pq_m <= 0) {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"pq_m should be greater than 0 for PQ quantilizer");
}
if (params.metric_type == FaissBuildParameter::MetricType::L2) {
_index = std::make_unique<faiss::IndexHNSWPQ>(params.d, params.m, params.pq_m);
} else if (params.metric_type == FaissBuildParameter::MetricType::IP) {
_index = std::make_unique<faiss::IndexHNSWPQ>(params.d, params.m, params.pq_m,
faiss::METRIC_INNER_PRODUCT);
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"Unsupported metric type: {}",
static_cast<int>(params.metric_type));
}
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"Unsupported quantilizer type: {}",
static_cast<int>(params.quantilizer));
}
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT, "Unsupported index type: {}",
static_cast<int>(params.index_type));
}
}
// TODO: Support batch search
doris::Status FaissVectorIndex::ann_topn_search(const float* query_vec, int k,
const vectorized::IndexSearchParameters& params,
vectorized::IndexSearchResult& result) {
std::unique_ptr<float[]> distances_ptr = std::make_unique<float[]>(k);
float* distances = distances_ptr.get();
// Initialize labels with -1
// Even if there are N vectors in the index, limit N search in faiss could return less than N(eg, HNSW)
// so we need to initialize labels with -1 to tell the end of the result ids.
std::unique_ptr<std::vector<faiss::idx_t>> labels_ptr =
std::make_unique<std::vector<faiss::idx_t>>(k, -1);
faiss::idx_t* labels = (*labels_ptr).data();
if (params.roaring == nullptr) {
_index->search(1, query_vec, k, distances, labels);
} else {
std::unique_ptr<faiss::IDSelector> id_sel = nullptr;
id_sel = roaring_to_faiss_selector(*params.roaring);
faiss::SearchParametersHNSW param;
const vectorized::HNSWSearchParameters* hnsw_params =
dynamic_cast<const vectorized::HNSWSearchParameters*>(¶ms);
if (hnsw_params == nullptr) {
return doris::Status::InvalidArgument(
"HNSW search parameters should not be null for HNSW index");
}
param.sel = id_sel.get();
param.efSearch = hnsw_params->ef_search;
param.check_relative_distance = hnsw_params->check_relative_distance;
param.bounded_queue = hnsw_params->bounded_queue;
_index->search(1, query_vec, k, distances, labels, ¶m);
}
result.roaring = std::make_shared<roaring::Roaring>();
update_roaring(labels, k, *result.roaring);
size_t roaring_cardinality = result.roaring->cardinality();
result.distances = std::make_unique<float[]>(roaring_cardinality);
result.row_ids = std::make_unique<std::vector<uint64_t>>();
if (_metric == Metric::L2) {
// For l2_distance, we need to convert the distance to the actual distance.
// The distance returned by Faiss is actually the squared distance.
// So we need to take the square root of the squared distance.
for (size_t i = 0; i < roaring_cardinality; ++i) {
result.row_ids->push_back(labels[i]);
result.distances[i] = std::sqrt(distances[i]);
}
} else if (_metric == Metric::IP) {
// For inner product, we can use the distance directly.
for (size_t i = 0; i < roaring_cardinality; ++i) {
result.row_ids->push_back(labels[i]);
result.distances[i] = distances[i]; // Convert squared distance to actual distance
}
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT, "Unsupported metric type: {}",
static_cast<int>(_metric));
}
DCHECK(result.row_ids->size() == result.roaring->cardinality())
<< "Row ids size: " << result.row_ids->size()
<< ", roaring size: " << result.roaring->cardinality();
return doris::Status::OK();
}
// For l2 distance, range search radius is the squared distance.
// For inner product, range search radius is the actual distance.
// range search on inner product returns all vectors with inner product greater than or equal to the radius.
// For l2 distance, range search returns all vectors with squared distance less than or equal to the radius.
doris::Status FaissVectorIndex::range_search(const float* query_vec, const float& radius,
const vectorized::IndexSearchParameters& params,
vectorized::IndexSearchResult& result) {
DCHECK(_index != nullptr);
DCHECK(query_vec != nullptr);
DCHECK(params.roaring != nullptr)
<< "Roaring should not be null for range search, please set roaring in params";
std::unique_ptr<faiss::IDSelector> sel = roaring_to_faiss_selector(*params.roaring);
faiss::RangeSearchResult native_search_result(1, true);
const vectorized::HNSWSearchParameters* hnsw_params =
dynamic_cast<const vectorized::HNSWSearchParameters*>(¶ms);
// Currently only support HNSW index for range search.
DCHECK(hnsw_params != nullptr) << "HNSW search parameters should not be null for HNSW index";
faiss::SearchParametersHNSW param;
param.efSearch = hnsw_params->ef_search;
param.check_relative_distance = hnsw_params->check_relative_distance;
param.bounded_queue = hnsw_params->bounded_queue;
param.sel = sel.get();
if (_metric == Metric::L2) {
_index->range_search(1, query_vec, radius * radius, &native_search_result, ¶m);
} else if (_metric == Metric::IP) {
_index->range_search(1, query_vec, radius, &native_search_result, ¶m);
}
size_t begin = native_search_result.lims[0];
size_t end = native_search_result.lims[1];
auto row_ids = std::make_unique<std::vector<uint64_t>>();
row_ids->resize(end - begin);
LOG_INFO("Range search result: begin {}, end {}", begin, end);
if (params.is_le_or_lt) {
if (_metric == Metric::L2) {
std::unique_ptr<float[]> distances_ptr = std::make_unique<float[]>(end - begin);
float* distances = distances_ptr.get();
auto roaring = std::make_shared<roaring::Roaring>();
// The distance returned by Faiss is actually the squared distance.
// So we need to take the square root of the squared distance.
for (size_t i = begin; i < end; ++i) {
(*row_ids)[i] = native_search_result.labels[i];
roaring->add(native_search_result.labels[i]);
distances[i - begin] = sqrt(native_search_result.distances[i]);
}
result.distances = std::move(distances_ptr);
result.row_ids = std::move(row_ids);
result.roaring = roaring;
DCHECK(result.row_ids->size() == result.roaring->cardinality())
<< "row_ids size: " << result.row_ids->size()
<< ", roaring size: " << result.roaring->cardinality();
} else if (_metric == Metric::IP) {
// For IP, we can use the distance directly.
// range search on ip gets all vectors with inner product greater than or equal to the radius.
// so we need to do a convertion.
const roaring::Roaring& origin_row_ids = *params.roaring;
std::shared_ptr<roaring::Roaring> roaring = std::make_shared<roaring::Roaring>();
for (size_t i = begin; i < end; ++i) {
roaring->add(native_search_result.labels[i]);
}
result.roaring = std::make_shared<roaring::Roaring>();
// remove all rows that should not be included.
*(result.roaring) = origin_row_ids - *roaring;
// Just update the roaring. distance can not be used.
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"Unsupported metric type: {}", static_cast<int>(_metric));
}
} else {
if (_metric == Metric::L2) {
// Faiss can only return labels in the range of radius.
// If the precidate is not less than, we need to to a convertion.
const roaring::Roaring& origin_row_ids = *params.roaring;
std::shared_ptr<roaring::Roaring> roaring = std::make_shared<roaring::Roaring>();
for (size_t i = begin; i < end; ++i) {
roaring->add(native_search_result.labels[i]);
}
result.roaring = std::make_shared<roaring::Roaring>();
*(result.roaring) = origin_row_ids - *roaring;
result.distances = nullptr;
result.row_ids = nullptr;
} else if (_metric == Metric::IP) {
// For inner product, we can use the distance directly.
// range search on ip gets all vectors with inner product greater than or equal to the radius.
// when query condition is not le_or_lt, we can use the roaring and distance directly.
std::unique_ptr<float[]> distances_ptr = std::make_unique<float[]>(end - begin);
float* distances = distances_ptr.get();
auto roaring = std::make_shared<roaring::Roaring>();
// The distance returned by Faiss is actually the squared distance.
// So we need to take the square root of the squared distance.
for (size_t i = begin; i < end; ++i) {
(*row_ids)[i] = native_search_result.labels[i];
roaring->add(native_search_result.labels[i]);
distances[i - begin] = native_search_result.distances[i];
}
result.distances = std::move(distances_ptr);
result.row_ids = std::move(row_ids);
result.roaring = roaring;
DCHECK(result.row_ids->size() == result.roaring->cardinality())
<< "row_ids size: " << result.row_ids->size()
<< ", roaring size: " << result.roaring->cardinality();
} else {
throw doris::Exception(doris::ErrorCode::INVALID_ARGUMENT,
"Unsupported metric type: {}", static_cast<int>(_metric));
}
}
return Status::OK();
}
doris::Status FaissVectorIndex::save(lucene::store::Directory* dir) {
auto start_time = std::chrono::high_resolution_clock::now();
lucene::store::IndexOutput* idx_output = dir->createOutput("faiss.idx");
auto writer = std::make_unique<FaissIndexWriter>(idx_output);
faiss::write_index(_index.get(), writer.get());
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
LOG_INFO(fmt::format("Faiss index saved to {}, {}, rows {}, cost {} ms", dir->toString(),
"faiss.idx", _index->ntotal, duration.count()));
return doris::Status::OK();
}
doris::Status FaissVectorIndex::load(lucene::store::Directory* dir) {
LOG_INFO("Loading Faiss index from: {}", dir->getObjectName());
auto start_time = std::chrono::high_resolution_clock::now();
lucene::store::IndexInput* idx_input = dir->openInput("faiss.idx");
auto reader = std::make_unique<FaissIndexReader>(idx_input);
faiss::Index* idx = faiss::read_index(reader.get());
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
LOG_INFO("Load index from {} costs {} ms, rows {}", dir->getObjectName(), duration.count(),
idx->ntotal);
_index.reset(idx);
return doris::Status::OK();
}
} // namespace doris::segment_v2