-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdynamic_ivf.cpp
More file actions
790 lines (696 loc) · 25.6 KB
/
dynamic_ivf.cpp
File metadata and controls
790 lines (696 loc) · 25.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*
* 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.
*/
// svs python bindings
#include "svs/python/dynamic_ivf.h"
#include "svs/python/common.h"
#include "svs/python/core.h"
#include "svs/python/ivf.h"
#include "svs/python/ivf_loader.h"
#include "svs/python/manager.h"
// svs
#include "svs/core/data/simple.h"
#include "svs/index/ivf/data_traits.h"
#include "svs/lib/dispatcher.h"
#include "svs/lib/saveload.h"
#include "svs/orchestrators/dynamic_ivf.h"
// toml
#include <toml++/toml.h>
// pybind
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
// fmt
#include <fmt/format.h>
// stl
#include <filesystem>
#include <span>
/////
///// DynamicIVF
/////
namespace py = pybind11;
namespace svs::python::dynamic_ivf {
// Reuse the Clustering type from static IVF since clustering is the same
using Clustering = svs::python::ivf::Clustering;
using IVFAssembleTypes =
std::variant<UnspecializedVectorDataLoader, svs::lib::SerializedObject>;
/////
///// Dispatch Invocation
/////
/////
///// Assembly from Clustering
/////
template <typename Q, typename T, size_t N>
svs::DynamicIVF assemble_uncompressed(
Clustering clustering,
svs::VectorDataLoader<T, N, RebindAllocator<T>> data,
std::span<const size_t> ids,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
return svs::DynamicIVF::assemble_from_clustering<Q>(
std::move(clustering),
std::move(data),
ids,
distance_type,
num_threads,
intra_query_threads
);
}
template <typename Dispatcher>
void register_uncompressed_ivf_assemble(Dispatcher& dispatcher) {
for_standard_specializations([&dispatcher]<typename Q, typename T, size_t N>() {
auto method = &assemble_uncompressed<Q, T, N>;
dispatcher.register_target(svs::lib::dispatcher_build_docs, method);
});
}
template <typename Dispatcher> void register_ivf_assembly(Dispatcher& dispatcher) {
register_uncompressed_ivf_assemble(dispatcher);
}
/////
///// Assembly from File
/////
template <typename Q, typename T, size_t N>
svs::DynamicIVF assemble_from_file_uncompressed(
const std::filesystem::path& cluster_path,
svs::VectorDataLoader<T, N, RebindAllocator<T>> data,
std::span<const size_t> ids,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
return svs::DynamicIVF::assemble_from_file<Q, svs::BFloat16>(
cluster_path, std::move(data), ids, distance_type, num_threads, intra_query_threads
);
}
template <typename Dispatcher>
void register_uncompressed_ivf_assemble_from_file(Dispatcher& dispatcher) {
for_standard_specializations([&dispatcher]<typename Q, typename T, size_t N>() {
auto method = &assemble_from_file_uncompressed<Q, T, N>;
dispatcher.register_target(svs::lib::dispatcher_build_docs, method);
});
}
template <typename Dispatcher>
void register_ivf_assembly_from_file(Dispatcher& dispatcher) {
register_uncompressed_ivf_assemble_from_file(dispatcher);
}
/////
///// Assemble from Clustering from Array
/////
template <typename Q, typename T, size_t N>
svs::DynamicIVF uncompressed_assemble_from_clustering_from_array(
Clustering clustering,
svs::data::ConstSimpleDataView<T, N> view,
std::span<const size_t> ids,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
auto mutable_view = svs::data::SimpleDataView<T, N>(
const_cast<T*>(view.data()), view.size(), view.dimensions()
);
return svs::DynamicIVF::assemble_from_clustering<Q>(
std::move(clustering),
mutable_view,
ids,
distance_type,
num_threads,
intra_query_threads
);
}
template <typename Dispatcher>
void register_ivf_assemble_from_clustering_from_array(Dispatcher& dispatcher) {
for_standard_specializations([&dispatcher]<typename Q, typename T, size_t N>() {
auto method = &uncompressed_assemble_from_clustering_from_array<Q, T, N>;
dispatcher.register_target(svs::lib::dispatcher_build_docs, method);
});
}
/////
///// Assemble from File from Array
/////
template <typename Q, typename T, size_t N>
svs::DynamicIVF uncompressed_assemble_from_file_from_array(
const std::filesystem::path& cluster_path,
svs::data::ConstSimpleDataView<T, N> view,
std::span<const size_t> ids,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
auto mutable_view = svs::data::SimpleDataView<T, N>(
const_cast<T*>(view.data()), view.size(), view.dimensions()
);
return svs::DynamicIVF::assemble_from_file<Q, svs::BFloat16>(
cluster_path, mutable_view, ids, distance_type, num_threads, intra_query_threads
);
}
template <typename Dispatcher>
void register_ivf_assemble_from_file_from_array(Dispatcher& dispatcher) {
for_standard_specializations([&dispatcher]<typename Q, typename T, size_t N>() {
auto method = &uncompressed_assemble_from_file_from_array<Q, T, N>;
dispatcher.register_target(svs::lib::dispatcher_build_docs, method);
});
}
using IVFAssembleTypes =
std::variant<UnspecializedVectorDataLoader, svs::lib::SerializedObject>;
/////
///// Dispatch Invocation
/////
using AssemblyDispatcher = svs::lib::Dispatcher<
svs::DynamicIVF,
Clustering,
IVFAssembleTypes,
std::span<const size_t>,
svs::DistanceType,
size_t,
size_t>;
AssemblyDispatcher assembly_dispatcher() {
auto dispatcher = AssemblyDispatcher{};
// Register available backend methods.
register_ivf_assembly(dispatcher);
return dispatcher;
}
// Assemble
svs::DynamicIVF assemble_from_clustering(
Clustering clustering,
IVFAssembleTypes data_kind,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance_type,
svs::DataType SVS_UNUSED(query_type),
bool SVS_UNUSED(enforce_dims),
size_t num_threads,
size_t intra_query_threads = 1
) {
auto ids = std::span<const size_t>(py_ids.data(), py_ids.size());
return assembly_dispatcher().invoke(
std::move(clustering),
std::move(data_kind),
ids,
distance_type,
num_threads,
intra_query_threads
);
}
using AssemblyFromFileDispatcher = svs::lib::Dispatcher<
svs::DynamicIVF,
const std::filesystem::path&,
IVFAssembleTypes,
std::span<const size_t>,
svs::DistanceType,
size_t,
size_t>;
AssemblyFromFileDispatcher assembly_from_file_dispatcher() {
auto dispatcher = AssemblyFromFileDispatcher{};
// Register available backend methods.
register_ivf_assembly_from_file(dispatcher);
return dispatcher;
}
// Assemble from file
svs::DynamicIVF assemble_from_file(
const std::string& cluster_path,
IVFAssembleTypes data_kind,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance_type,
svs::DataType SVS_UNUSED(query_type),
bool SVS_UNUSED(enforce_dims),
size_t num_threads,
size_t intra_query_threads = 1
) {
auto ids = std::span<const size_t>(py_ids.data(), py_ids.size());
return assembly_from_file_dispatcher().invoke(
cluster_path,
std::move(data_kind),
ids,
distance_type,
num_threads,
intra_query_threads
);
}
// Assemble from clustering from array.
using AssembleFromClusteringArrayDispatcher = svs::lib::Dispatcher<
svs::DynamicIVF,
Clustering,
AnonymousVectorData,
std::span<const size_t>,
svs::DistanceType,
size_t,
size_t>;
AssembleFromClusteringArrayDispatcher assemble_from_clustering_array_dispatcher() {
auto dispatcher = AssembleFromClusteringArrayDispatcher{};
register_ivf_assemble_from_clustering_from_array(dispatcher);
return dispatcher;
}
svs::DynamicIVF assemble_from_clustering_from_array(
Clustering clustering,
AnonymousVectorData py_data,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
auto ids = std::span<const size_t>(py_ids.data(), py_ids.size());
return assemble_from_clustering_array_dispatcher().invoke(
std::move(clustering), py_data, ids, distance_type, num_threads, intra_query_threads
);
}
// Assemble from file from array.
using AssembleFromFileArrayDispatcher = svs::lib::Dispatcher<
svs::DynamicIVF,
const std::filesystem::path&,
AnonymousVectorData,
std::span<const size_t>,
svs::DistanceType,
size_t,
size_t>;
AssembleFromFileArrayDispatcher assemble_from_file_array_dispatcher() {
auto dispatcher = AssembleFromFileArrayDispatcher{};
register_ivf_assemble_from_file_from_array(dispatcher);
return dispatcher;
}
svs::DynamicIVF assemble_from_file_from_array(
const std::string& cluster_path,
AnonymousVectorData py_data,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
auto ids = std::span<const size_t>(py_ids.data(), py_ids.size());
return assemble_from_file_array_dispatcher().invoke(
cluster_path, py_data, ids, distance_type, num_threads, intra_query_threads
);
}
// Templatize at the top level for numpy array assemble specializations.
template <typename ElementType>
void add_assemble_from_clustering_array_specialization(
py::class_<svs::DynamicIVF>& dynamic_ivf
) {
dynamic_ivf.def_static(
"assemble_from_clustering",
[](Clustering clustering,
py_contiguous_array_t<ElementType> py_data,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance,
size_t num_threads,
size_t intra_query_threads) {
return assemble_from_clustering_from_array(
std::move(clustering),
AnonymousVectorData(py_data),
py_ids,
distance,
num_threads,
intra_query_threads
);
},
py::arg("clustering"),
py::arg("py_data"),
py::arg("ids"),
py::arg("distance") = svs::L2,
py::arg("num_threads") = 1,
py::arg("intra_query_threads") = 1,
R"(
Assemble a searchable DynamicIVF index from provided clustering and numpy data array.
Args:
clustering: The clustering object (from Clustering.build or Clustering.load_clustering).
py_data: The dataset as a numpy array. SVS will maintain an internal copy.
ids: External IDs for the vectors. Must match dataset length and contain unique values.
distance: The distance function to use. Default: L2.
num_threads: The number of threads to use for queries. Default: 1.
intra_query_threads: Number of threads for intra-query parallelism. Default: 1.
)"
);
}
template <typename ElementType>
void add_assemble_from_file_array_specialization(py::class_<svs::DynamicIVF>& dynamic_ivf) {
dynamic_ivf.def_static(
"assemble_from_file",
[](const std::string& clustering_path,
py_contiguous_array_t<ElementType> py_data,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance,
size_t num_threads,
size_t intra_query_threads) {
return assemble_from_file_from_array(
clustering_path,
AnonymousVectorData(py_data),
py_ids,
distance,
num_threads,
intra_query_threads
);
},
py::arg("clustering_path"),
py::arg("py_data"),
py::arg("ids"),
py::arg("distance") = svs::L2,
py::arg("num_threads") = 1,
py::arg("intra_query_threads") = 1,
R"(
Assemble a searchable DynamicIVF index from clustering on disk and numpy data array.
Args:
clustering_path: Path to the directory where the clustering was generated.
py_data: The dataset as a numpy array. SVS will maintain an internal copy.
ids: External IDs for the vectors. Must match dataset length and contain unique values.
distance: The distance function to use. Default: L2.
num_threads: The number of threads to use for queries. Default: 1.
intra_query_threads: Number of threads for intra-query parallelism. Default: 1.
)"
);
}
constexpr std::string_view ASSEMBLE_DOCSTRING_PROTO = R"(
Assemble a searchable IVF index from provided clustering and data
Args:
clustering_path/clustering: Path to the directory where the clustering was generated.
OR directly provide the loaded Clustering.
data_loader: The loader for the dataset. See comment below for accepted types.
ids: External IDs for the vectors. Must match dataset length and contain unique values.
distance: The distance function to use.
query_type: The data type of the queries.
enforce_dims: Require that the compiled dimensionality of the returned index matches
the dimensionality provided in the ``data_loader`` argument. If a match is not
found, an exception is thrown.
This is meant to ensure that specialized dimensionality is provided without falling
back to generic implementations. Leaving the ``dims`` out when constructing the
``data_loader`` will with `enable_dims = True` will always attempt to use a generic
implementation.
num_threads: The number of threads to use for queries (can't be changed after loading).
intra_query_threads: (default: 1) these many threads work on a single query.
Total number of threads required = ``query_batch_size`` * ``intra_query_threads``.
Where ``query_batch_size`` is the number of queries processed in parallel.
Use this parameter only when the ``query_batch_size`` is smaller and ensure your
system has sufficient threads available. Set ``num_threads`` = ``query_batch_size``
The top level type is an abstract type backed by various specialized backends that will
be instantiated based on their applicability to the particular problem instance.
The arguments upon which specialization is conducted are:
* `data_loader`: Both kind (type of loader) and inner aspects of the loader like data type,
quantization type, and number of dimensions.
* `distance`: The distance measure being used.
Specializations compiled into the binary are listed below.
{}
)";
/////
///// Add points
/////
template <typename ElementType>
void add_points(
svs::DynamicIVF& index,
const py_contiguous_array_t<ElementType>& py_data,
const py_contiguous_array_t<size_t>& ids,
bool reuse_empty = false
) {
if (py_data.ndim() != 2) {
throw ANNEXCEPTION("Expected points to have 2 dimensions!");
}
if (ids.ndim() != 1) {
throw ANNEXCEPTION("Expected ids to have 1 dimension!");
}
if (py_data.shape(0) != ids.shape(0)) {
throw ANNEXCEPTION(
"Expected IDs to be the same length as the number of rows in points!"
);
}
index.add_points(data_view(py_data), std::span(ids.data(), ids.size()), reuse_empty);
}
const char* ADD_POINTS_DOCSTRING = R"(
Add every point in ``points`` to the index, assigning the element-wise corresponding ID to
each point.
Args:
points: A matrix of data whose rows, corresponding to points in R^n, will be added to
the index.
ids: Vector of ids to assign to each row in ``points``. Must have the same number of
elements as ``points`` has rows.
reuse_empty: A flag that determines whether to reuse empty entries that may exist after deletion and consolidation. When enabled,
scan from the beginning to find and fill these empty entries when adding new points.
Furthermore, all entries in ``ids`` must be unique and not already exist in the index.
If either of these does not hold, an exception will be thrown without mutating the
underlying index.
When ``delete`` is called, a soft deletion is performed, marking the entries as ``deleted``.
When ``consolidate`` is called, the state of these deleted entries becomes ``empty``.
When ``add_points`` is called with the ``reuse_empty`` flag enabled, the memory is scanned from the beginning to locate and fill these empty entries with new points.
)";
template <typename ElementType>
void add_points_specialization(py::class_<svs::DynamicIVF>& index) {
index.def(
"add",
&add_points<ElementType>,
py::arg("points"),
py::arg("ids"),
py::arg("reuse_empty") = false,
ADD_POINTS_DOCSTRING
);
}
const char* CONSOLIDATE_DOCSTRING = R"(
No-op method for compatibility with dynamic index interface.
For the IVF index, deletion marks entries as Empty and they are excluded from searches.
Empty slots can be reused when adding new points.
)";
const char* COMPACT_DOCSTRING = R"(
Remove any holes created in the data by renumbering internal IDs.
Shrink the underlying data structures.
This can potentially reduce the memory footprint of the index
if a sufficient number of points were deleted.
)";
const char* DELETE_DOCSTRING = R"(
Soft delete the IDs from the index. Soft deletion does not remove the IDs from the index,
but prevents them from being returned from future searches.
Args:
ids: The IDs to delete.
Each element in IDs must be unique and must correspond to a valid ID stored in the index.
Otherwise, an exception will be thrown. If an exception is thrown for this reason, the
index will be left unchanged from before the function call.
)";
const char* ALL_IDS_DOCSTRING = R"(
Return a Numpy vector of all IDs currently in the index.
)";
// Index saving.
void save_index(
svs::DynamicIVF& index, const std::string& config_path, const std::string& data_dir
) {
index.save(config_path, data_dir);
}
// Load with auto-detection from saved config using common template dispatcher
svs::DynamicIVF load_index_auto(
const std::string& config_path,
const std::string& data_path,
svs::DistanceType distance_type,
size_t num_threads,
size_t intra_query_threads = 1
) {
return svs::python::ivf_loader::
load_index_auto<svs::DynamicIVF, svs::data::BlockedData, Allocator>(
config_path, data_path, distance_type, num_threads, intra_query_threads
);
}
void wrap(py::module& m) {
std::string name = "DynamicIVF";
py::class_<svs::DynamicIVF> dynamic_ivf(
m, name.c_str(), "Top level class for the dynamic IVF index."
);
add_search_specialization<float>(dynamic_ivf);
add_threading_interface(dynamic_ivf);
add_data_interface(dynamic_ivf);
// IVF specific extensions.
ivf::add_interface(dynamic_ivf);
// Dynamic interface.
dynamic_ivf.def("consolidate", &svs::DynamicIVF::consolidate, CONSOLIDATE_DOCSTRING);
dynamic_ivf.def(
"compact",
&svs::DynamicIVF::compact,
py::arg("batchsize") = 1'000'000,
COMPACT_DOCSTRING
);
// Assemble interface
{
auto dispatcher = assembly_dispatcher();
// Procedurally generate the dispatch string.
auto dynamic = std::string{};
for (size_t i = 0; i < dispatcher.size(); ++i) {
fmt::format_to(
std::back_inserter(dynamic),
R"(
Method {}:
- data_loader: {}
- distance: {}
)",
i,
dispatcher.description(i, 2),
dispatcher.description(i, 3)
);
}
dynamic_ivf.def_static(
"assemble_from_clustering",
[](Clustering clustering,
IVFAssembleTypes data_loader,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance,
svs::DataType query_type,
bool enforce_dims,
size_t num_threads,
size_t intra_query_threads) {
return assemble_from_clustering(
std::move(clustering),
std::move(data_loader),
py_ids,
distance,
query_type,
enforce_dims,
num_threads,
intra_query_threads
);
},
py::arg("clustering"),
py::arg("data_loader"),
py::arg("ids"),
py::arg("distance") = svs::L2,
py::arg("query_type") = svs::DataType::float32,
py::arg("enforce_dims") = false,
py::arg("num_threads") = 1,
py::arg("intra_query_threads") = 1,
fmt::format(ASSEMBLE_DOCSTRING_PROTO, dynamic).c_str()
);
dynamic_ivf.def_static(
"assemble_from_file",
[](const std::string& clustering_path,
IVFAssembleTypes data_loader,
const py_contiguous_array_t<size_t>& py_ids,
svs::DistanceType distance,
svs::DataType query_type,
bool enforce_dims,
size_t num_threads,
size_t intra_query_threads) {
return assemble_from_file(
clustering_path,
std::move(data_loader),
py_ids,
distance,
query_type,
enforce_dims,
num_threads,
intra_query_threads
);
},
py::arg("clustering_path"),
py::arg("data_loader"),
py::arg("ids"),
py::arg("distance") = svs::L2,
py::arg("query_type") = svs::DataType::float32,
py::arg("enforce_dims") = false,
py::arg("num_threads") = 1,
py::arg("intra_query_threads") = 1,
fmt::format(ASSEMBLE_DOCSTRING_PROTO, dynamic).c_str()
);
}
// Assemble from numpy array.
add_assemble_from_clustering_array_specialization<float>(dynamic_ivf);
add_assemble_from_file_array_specialization<float>(dynamic_ivf);
// Index modification.
add_points_specialization<float>(dynamic_ivf);
// Note: DynamicIVFIndex doesn't support reconstruct_at, so we don't add reconstruct
// interface
// Index Deletion.
dynamic_ivf.def(
"delete",
[](svs::DynamicIVF& index, const py_contiguous_array_t<size_t>& ids) {
return index.delete_points(as_span(ids));
},
py::arg("ids"),
DELETE_DOCSTRING
);
// ID inspection
dynamic_ivf.def(
"has_id",
&svs::DynamicIVF::has_id,
py::arg("id"),
"Return whether the ID exists in the index."
);
dynamic_ivf.def(
"all_ids",
[](const svs::DynamicIVF& index) {
const auto& v = index.all_ids();
// Populate a numpy-set
auto npv = numpy_vector<size_t>(v.size());
std::copy(v.begin(), v.end(), npv.mutable_unchecked().mutable_data());
return npv;
},
ALL_IDS_DOCSTRING
);
// Distance calculation
dynamic_ivf.def(
"get_distance",
[](const svs::DynamicIVF& index,
size_t id,
const py_contiguous_array_t<float>& query) {
return index.get_distance(id, as_span(query));
},
py::arg("id"),
py::arg("query"),
R"(
Compute the distance between a query vector and a vector in the index.
Args:
id: The external ID of the vector in the index.
query: The query vector as a numpy array.
Returns:
The distance between the query and the indexed vector.
Raises:
RuntimeError: If the ID doesn't exist or dimensions don't match.
)"
);
// Saving
dynamic_ivf.def(
"save",
&save_index,
py::arg("config_directory"),
py::arg("data_directory"),
R"(
Save a constructed index to disk (useful following index construction).
Args:
config_directory: Directory where index configuration information will be saved.
data_directory: Directory where the dataset will be saved.
Note: All directories should be separate to avoid accidental name collision with any
auxiliary files that are needed when saving the various components of the index.
If the directory does not exist, it will be created if its parent exists.
It is the caller's responsibility to ensure that no existing data will be
overwritten when saving the index to this directory.
)"
);
// Loading
dynamic_ivf.def_static(
"load",
&load_index_auto,
py::arg("config_directory"),
py::arg("data_directory"),
py::arg("distance") = svs::L2,
py::arg("num_threads") = 1,
py::arg("intra_query_threads") = 1,
R"(
Load a saved DynamicIVF index from disk.
The data type (uncompressed with float32 or float16) and centroid type (bfloat16)
are automatically detected from the saved configuration file.
Args:
config_directory: Directory where index configuration was saved.
data_directory: Directory where the dataset was saved.
distance: The distance function to use.
num_threads: The number of threads to use for queries.
intra_query_threads: Number of threads for intra-query parallelism (default: 1).
Returns:
A loaded DynamicIVF index ready for searching and modifications.
Note:
This method auto-detects the data type from the saved configuration.
The index must have been saved with a version that includes data type information.
)"
);
}
} // namespace svs::python::dynamic_ivf