-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdynamic_vamana_index.cpp
More file actions
359 lines (319 loc) · 11.6 KB
/
dynamic_vamana_index.cpp
File metadata and controls
359 lines (319 loc) · 11.6 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
/*
* Copyright 2025 Intel Corporation
*
* Licensed 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 "svs/runtime/dynamic_vamana_index.h"
#include "dynamic_vamana_index_impl.h"
#include "svs_runtime_utils.h"
#ifdef SVS_RUNTIME_HAVE_LVQ_LEANVEC
#include "dynamic_vamana_index_leanvec_impl.h"
#endif
#include <svs/core/data.h>
#include <svs/core/distance.h>
#include <svs/core/query_result.h>
#include <svs/extensions/vamana/scalar.h>
#include <svs/lib/float16.h>
#include <svs/orchestrators/dynamic_vamana.h>
#include <svs/quantization/scalar/scalar.h>
#include <algorithm>
#include <memory>
#include <variant>
#include <vector>
namespace svs {
namespace runtime {
namespace {
template <typename Impl = DynamicVamanaIndexImpl>
struct DynamicVamanaIndexManagerBase : public DynamicVamanaIndex {
std::unique_ptr<Impl> impl_;
DynamicVamanaIndexManagerBase(std::unique_ptr<Impl> impl)
: impl_{std::move(impl)} {
assert(impl_ != nullptr);
}
DynamicVamanaIndexManagerBase(const DynamicVamanaIndexManagerBase&) = delete;
DynamicVamanaIndexManagerBase& operator=(const DynamicVamanaIndexManagerBase&) = delete;
DynamicVamanaIndexManagerBase(DynamicVamanaIndexManagerBase&&) = default;
DynamicVamanaIndexManagerBase& operator=(DynamicVamanaIndexManagerBase&&) = default;
~DynamicVamanaIndexManagerBase() override = default;
Status add(size_t n, const size_t* labels, const float* x) noexcept override {
return runtime_error_wrapper([&] {
svs::data::ConstSimpleDataView<float> data{x, n, impl_->dimensions()};
std::span<const size_t> lbls(labels, n);
impl_->add(data, lbls);
});
}
size_t blocksize_bytes() const noexcept { return impl_->blocksize_bytes(); }
Status
remove_selected(size_t* num_removed, const IDFilter& selector) noexcept override {
return runtime_error_wrapper([&] {
*num_removed = impl_->remove_selected(selector);
});
}
Status remove(size_t n, const size_t* labels) noexcept override {
return runtime_error_wrapper([&] {
std::span<const size_t> lbls(labels, n);
impl_->remove(lbls);
});
}
Status search(
size_t n,
const float* x,
size_t k,
float* distances,
size_t* labels,
const SearchParams* params = nullptr,
IDFilter* filter = nullptr
) const noexcept override {
return runtime_error_wrapper([&] {
auto result = svs::QueryResultView<size_t>{
svs::MatrixView<size_t>{svs::make_dims(n, k), labels},
svs::MatrixView<float>{svs::make_dims(n, k), distances}};
auto queries = svs::data::ConstSimpleDataView<float>(x, n, impl_->dimensions());
impl_->search(result, queries, params, filter);
});
}
Status range_search(
size_t n,
const float* x,
float radius,
const ResultsAllocator& results,
const SearchParams* params = nullptr,
IDFilter* filter = nullptr
) const noexcept override {
return runtime_error_wrapper([&] {
auto queries = svs::data::ConstSimpleDataView<float>(x, n, impl_->dimensions());
impl_->range_search(queries, radius, results, params, filter);
});
}
Status reset() noexcept override {
return runtime_error_wrapper([&] { impl_->reset(); });
}
Status save(std::ostream& out) const noexcept override {
return runtime_error_wrapper([&] { impl_->save(out); });
}
Status
get_distance(double* distance, size_t id, const float* query) const noexcept override {
return runtime_error_wrapper([&] { *distance = impl_->get_distance(id, query); });
}
Status reconstruct_at(size_t n, const size_t* ids, float* output) noexcept override {
return runtime_error_wrapper([&] { impl_->reconstruct_at(n, ids, output); });
}
};
} // namespace
// DynamicVamanaIndex interface implementation
Status DynamicVamanaIndex::check_storage_kind(StorageKind storage_kind) noexcept {
bool supported = false;
auto status = runtime_error_wrapper([&] {
supported = storage::is_supported_storage_kind(storage_kind);
});
if (!status.ok()) {
return status;
}
return supported ? Status_Ok
: Status(
ErrorCode::INVALID_ARGUMENT,
"The specified storage kind is not compatible with the "
"DynamicVamanaIndex"
);
}
Status DynamicVamanaIndex::check_params(
const DynamicVamanaIndex::DynamicIndexParams& dynamic_index_params
) noexcept {
constexpr static size_t kMaxBlockSizeExp = 30; // 1GB
constexpr static size_t kMinBlockSizeExp = 12; // 4KB
if (dynamic_index_params.blocksize_exp > kMaxBlockSizeExp)
return Status(ErrorCode::INVALID_ARGUMENT, "Blocksize is too large");
if (dynamic_index_params.blocksize_exp < kMinBlockSizeExp)
return Status(ErrorCode::INVALID_ARGUMENT, "Blocksize is too small");
return Status_Ok;
}
// ABI backward compatibility
Status DynamicVamanaIndex::build(
DynamicVamanaIndex** index,
size_t dim,
MetricType metric,
StorageKind storage_kind,
const DynamicVamanaIndex::BuildParams& params,
const DynamicVamanaIndex::SearchParams& default_search_params
) noexcept {
return build(
index,
dim,
metric,
storage_kind,
params,
default_search_params,
DynamicVamanaIndex::DynamicIndexParams{}
);
}
Status DynamicVamanaIndex::build(
DynamicVamanaIndex** index,
size_t dim,
MetricType metric,
StorageKind storage_kind,
const DynamicVamanaIndex::BuildParams& params,
const DynamicVamanaIndex::SearchParams& default_search_params,
const DynamicVamanaIndex::DynamicIndexParams& dynamic_index_params
) noexcept {
using Impl = DynamicVamanaIndexImpl;
*index = nullptr;
auto status = DynamicVamanaIndex::check_params(dynamic_index_params);
if (!status.ok())
return status;
return runtime_error_wrapper([&] {
auto impl = std::make_unique<Impl>(
dim, metric, storage_kind, params, default_search_params, dynamic_index_params
);
*index = new DynamicVamanaIndexManagerBase<Impl>{std::move(impl)};
});
}
Status DynamicVamanaIndex::destroy(DynamicVamanaIndex* index) noexcept {
return runtime_error_wrapper([&] { delete index; });
}
Status DynamicVamanaIndex::load(
DynamicVamanaIndex** index,
std::istream& in,
MetricType metric,
StorageKind storage_kind
) noexcept {
using Impl = DynamicVamanaIndexImpl;
*index = nullptr;
return runtime_error_wrapper([&] {
std::unique_ptr<Impl> impl{Impl::load(in, metric, storage_kind)};
*index = new DynamicVamanaIndexManagerBase<Impl>{std::move(impl)};
});
}
// Specialization to build LeanVec-based Vamana index with specified leanvec dims
// ABI backward compatibility
Status DynamicVamanaIndexLeanVec::build(
DynamicVamanaIndex** index,
size_t dim,
MetricType metric,
StorageKind storage_kind,
size_t leanvec_dims,
const DynamicVamanaIndex::BuildParams& params,
const DynamicVamanaIndex::SearchParams& default_search_params
) noexcept {
return build(
index,
dim,
metric,
storage_kind,
leanvec_dims,
params,
default_search_params,
DynamicVamanaIndex::DynamicIndexParams{}
);
}
// Specialization to build LeanVec-based Vamana index with provided training data
// ABI backward compatibility
Status DynamicVamanaIndexLeanVec::build(
DynamicVamanaIndex** index,
size_t dim,
MetricType metric,
StorageKind storage_kind,
const LeanVecTrainingData* training_data,
const DynamicVamanaIndex::BuildParams& params,
const DynamicVamanaIndex::SearchParams& default_search_params
) noexcept {
return build(
index,
dim,
metric,
storage_kind,
training_data,
params,
default_search_params,
DynamicVamanaIndex::DynamicIndexParams{}
);
}
#ifdef SVS_RUNTIME_HAVE_LVQ_LEANVEC
// Specialization to build LeanVec-based Vamana index with specified leanvec dims
Status DynamicVamanaIndexLeanVec::build(
DynamicVamanaIndex** index,
size_t dim,
MetricType metric,
StorageKind storage_kind,
size_t leanvec_dims,
const DynamicVamanaIndex::BuildParams& params,
const DynamicVamanaIndex::SearchParams& default_search_params,
const DynamicVamanaIndex::DynamicIndexParams& dynamic_index_params
) noexcept {
using Impl = DynamicVamanaIndexLeanVecImpl;
*index = nullptr;
auto status = DynamicVamanaIndex::check_params(dynamic_index_params);
if (!status.ok())
return status;
return runtime_error_wrapper([&] {
auto impl = std::make_unique<Impl>(
dim,
metric,
storage_kind,
leanvec_dims,
params,
default_search_params,
dynamic_index_params
);
*index = new DynamicVamanaIndexManagerBase<Impl>{std::move(impl)};
});
}
// Specialization to build LeanVec-based Vamana index with provided training data
Status DynamicVamanaIndexLeanVec::build(
DynamicVamanaIndex** index,
size_t dim,
MetricType metric,
StorageKind storage_kind,
const LeanVecTrainingData* training_data,
const DynamicVamanaIndex::BuildParams& params,
const DynamicVamanaIndex::SearchParams& default_search_params,
const DynamicVamanaIndex::DynamicIndexParams& dynamic_index_params
) noexcept {
using Impl = DynamicVamanaIndexLeanVecImpl;
*index = nullptr;
auto status = DynamicVamanaIndex::check_params(dynamic_index_params);
if (!status.ok())
return status;
return runtime_error_wrapper([&] {
auto training_data_impl =
static_cast<const LeanVecTrainingDataManager*>(training_data)->impl_;
auto impl = std::make_unique<Impl>(
dim,
metric,
storage_kind,
training_data_impl,
params,
default_search_params,
dynamic_index_params
);
*index = new DynamicVamanaIndexManagerBase<Impl>{std::move(impl)};
});
}
#else // SVS_RUNTIME_HAVE_LVQ_LEANVEC
// LeanVec storage kind is not supported in this build configuration
Status DynamicVamanaIndexLeanVec::
build(DynamicVamanaIndex**, size_t, MetricType, StorageKind, size_t, const DynamicVamanaIndex::BuildParams&, const DynamicVamanaIndex::SearchParams&, const DynamicVamanaIndex::DynamicIndexParams&) noexcept {
return Status(
ErrorCode::NOT_IMPLEMENTED,
"DynamicVamanaIndexLeanVec is not supported in this build configuration."
);
}
Status DynamicVamanaIndexLeanVec::
build(DynamicVamanaIndex**, size_t, MetricType, StorageKind, const LeanVecTrainingData*, const DynamicVamanaIndex::BuildParams&, const DynamicVamanaIndex::SearchParams&, const DynamicVamanaIndex::DynamicIndexParams&) noexcept {
return Status(
ErrorCode::NOT_IMPLEMENTED,
"DynamicVamanaIndexLeanVec is not supported in this build configuration."
);
}
#endif // SVS_RUNTIME_HAVE_LVQ_LEANVEC
} // namespace runtime
} // namespace svs