-
Notifications
You must be signed in to change notification settings - Fork 147
/
lib.cpp
1349 lines (1139 loc) · 62.3 KB
/
lib.cpp
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief Python bindings for Unum USearch.
* @file lib.cpp
* @author Ash Vardanian
* @date April 26, 2023
* @copyright Copyright (c) 2023
*
* https://pythoncapi.readthedocs.io/type_object.html
* https://numpy.org/doc/stable/reference/c-api/types-and-structures.html
* https://pythonextensionpatterns.readthedocs.io/en/latest/refcount.html
* https://docs.python.org/3/extending/newtypes_tutorial.html#adding-data-and-methods-to-the-basic-example
*/
#if !defined(__cpp_exceptions)
#define __cpp_exceptions 1
#endif
#include <limits> // `std::numeric_limits`
#include <thread> // `std::thread`
#define _CRT_SECURE_NO_WARNINGS
#define PY_SSIZE_T_CLEAN
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#if defined(_WIN32) //! On Windows, `ssize_t` is not defined by default
typedef intptr_t ssize_t; //! Use `intptr_t` for a signed integer with the same width as `size_t`
#endif
#include <usearch/index_dense.hpp>
#include <usearch/index_plugins.hpp>
using namespace unum::usearch;
using namespace unum;
namespace py = pybind11;
using py_shape_t = py::array::ShapeContainer;
using metric_t = metric_punned_t;
using distance_t = distance_punned_t;
using dense_key_t = typename index_dense_t::vector_key_t;
using dense_add_result_t = typename index_dense_t::add_result_t;
using dense_search_result_t = typename index_dense_t::search_result_t;
using dense_labeling_result_t = typename index_dense_t::labeling_result_t;
using dense_cluster_result_t = typename index_dense_t::cluster_result_t;
using dense_clustering_result_t = typename index_dense_t::clustering_result_t;
using progress_func_t = std::function<bool(std::size_t /*processed*/, std::size_t /*total*/)>;
struct progress_t {
inline progress_t(std::nullptr_t = nullptr) : func_(&dummy_progress) {}
inline progress_t(progress_func_t const& func) : func_(func ? func : &dummy_progress) {}
inline bool operator()(std::size_t processed, std::size_t total) const noexcept { return func_(processed, total); }
private:
static inline bool dummy_progress(std::size_t /*processed*/, std::size_t /*total*/) { return true; }
progress_func_t func_;
};
struct dense_index_py_t : public index_dense_t {
using native_t = index_dense_t;
using native_t::add;
using native_t::capacity;
using native_t::reserve;
using native_t::search;
using native_t::size;
dense_index_py_t(native_t&& base) : index_dense_t(std::move(base)) {}
};
struct dense_indexes_py_t {
std::vector<std::shared_ptr<dense_index_py_t>> shards_;
void merge(std::shared_ptr<dense_index_py_t> shard) { shards_.push_back(shard); }
std::size_t bytes_per_vector() const noexcept { return shards_.empty() ? 0 : shards_[0]->bytes_per_vector(); }
std::size_t scalar_words() const noexcept { return shards_.empty() ? 0 : shards_[0]->scalar_words(); }
index_limits_t limits() const noexcept { return {size(), std::numeric_limits<std::size_t>::max()}; }
void merge_paths(std::vector<std::string> const& paths, bool view = true, std::size_t threads = 0) {
if (!threads)
threads = std::thread::hardware_concurrency();
shards_.reserve(shards_.size() + paths.size());
std::mutex shards_mutex;
executor_default_t{threads}.dynamic(paths.size(), [&](std::size_t, std::size_t task_idx) {
index_dense_t index = index_dense_t::make(paths[task_idx].c_str(), view);
if (!index)
return false;
auto shared_index = std::make_shared<dense_index_py_t>(std::move(index));
std::unique_lock<std::mutex> lock(shards_mutex);
shards_.push_back(shared_index);
if (PyErr_CheckSignals() != 0)
throw py::error_already_set();
return true;
});
}
std::size_t size() const noexcept {
std::size_t result = 0;
for (auto const& shard : shards_)
result += shard->size();
return result;
}
};
static dense_index_py_t make_index( //
std::size_t dimensions, //
scalar_kind_t scalar_kind, //
std::size_t connectivity, //
std::size_t expansion_add, //
std::size_t expansion_search, //
metric_kind_t metric_kind, //
metric_punned_signature_t metric_signature, //
std::uintptr_t metric_uintptr, //
bool multi, //
bool enable_key_lookups) {
index_dense_config_t config(connectivity, expansion_add, expansion_search);
config.multi = multi;
config.enable_key_lookups = enable_key_lookups;
metric_t metric = //
metric_uintptr //
? metric_t::stateless(dimensions, metric_uintptr, metric_signature, metric_kind, scalar_kind)
: metric_t::builtin(dimensions, metric_kind, scalar_kind);
if (metric.missing())
throw std::invalid_argument("Unsupported metric!");
using index_state_t = typename index_dense_t::state_result_t;
index_state_t state = index_dense_t::make(metric, config);
if (!state)
throw std::invalid_argument(state.error.release());
return std::move(state.index);
}
scalar_kind_t numpy_string_to_kind(std::string const& name) {
// https://docs.python.org/3/library/struct.html#format-characters
if (name == "B" || name == "<B" || name == "u1" || name == "|u1")
return scalar_kind_t::b1x8_k;
else if (name == "b" || name == "<b" || name == "i1" || name == "|i1")
return scalar_kind_t::i8_k;
else if (name == "e" || name == "<e" || name == "f2" || name == "<f2")
return scalar_kind_t::f16_k;
else if (name == "f" || name == "<f" || name == "f4" || name == "<f4")
return scalar_kind_t::f32_k;
else if (name == "d" || name == "<d" || name == "i8" || name == "<i8")
return scalar_kind_t::f64_k;
else
return scalar_kind_t::unknown_k;
}
template <typename result_at> void forward_error(result_at&& result) {
if (!result)
throw std::invalid_argument(result.error.release());
int signals = PyErr_CheckSignals();
if (signals != 0)
throw py::error_already_set();
}
using atomic_error_t = std::atomic<char const*>;
template <typename scalar_at>
static void add_typed_to_index( //
dense_index_py_t& index, //
py::buffer_info const& keys_info, py::buffer_info const& vectors_info, //
bool force_copy, std::size_t threads, //
progress_func_t const& progress) {
Py_ssize_t vectors_count = vectors_info.shape[0];
byte_t const* vectors_data = reinterpret_cast<byte_t const*>(vectors_info.ptr);
byte_t const* keys_data = reinterpret_cast<byte_t const*>(keys_info.ptr);
atomic_error_t atomic_error{nullptr};
// Progress status
progress_t progress_{progress};
std::atomic<std::size_t> processed{0};
executor_default_t{threads}.dynamic(vectors_count, [&](std::size_t thread_idx, std::size_t task_idx) {
dense_key_t key = *reinterpret_cast<dense_key_t const*>(keys_data + task_idx * keys_info.strides[0]);
scalar_at const* vector = reinterpret_cast<scalar_at const*>(vectors_data + task_idx * vectors_info.strides[0]);
dense_add_result_t result = index.add(key, vector, thread_idx, force_copy);
if (!result) {
atomic_error = result.error.release();
return false;
}
// We don't want to check for signals from multiple threads
++processed;
if (thread_idx == 0)
if (PyErr_CheckSignals() != 0 || !progress_(processed.load(), vectors_count)) {
atomic_error.store("Operation has been terminated");
return false;
}
return true;
});
// At the end report the latest numbers, because the reporter thread may be finished earlier
progress_(processed.load(), vectors_count);
// Raise the error from a single thread
auto error = atomic_error.load();
if (error) {
PyErr_SetString(PyExc_RuntimeError, error);
throw py::error_already_set();
}
}
template <typename index_at>
static void add_many_to_index( //
index_at& index, py::buffer keys, py::buffer vectors, //
bool force_copy, std::size_t threads, //
progress_func_t const& progress) {
py::buffer_info keys_info = keys.request();
py::buffer_info vectors_info = vectors.request();
if (keys_info.itemsize != sizeof(dense_key_t))
throw std::invalid_argument("Incompatible key type!");
if (keys_info.ndim != 1)
throw std::invalid_argument("Keys must be placed in a single-dimensional array!");
if (vectors_info.ndim != 2)
throw std::invalid_argument("Expects a matrix of vectors to add!");
Py_ssize_t keys_count = keys_info.shape[0];
Py_ssize_t vectors_count = vectors_info.shape[0];
Py_ssize_t vectors_dimensions = vectors_info.shape[1];
if (vectors_dimensions != static_cast<Py_ssize_t>(index.scalar_words()))
throw std::invalid_argument("The number of vector dimensions doesn't match!");
if (keys_count != vectors_count)
throw std::invalid_argument("Number of keys and vectors must match!");
if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.try_reserve(index_limits_t(ceil2(index.size() + vectors_count), threads)))
throw std::invalid_argument("Out of memory!");
// clang-format off
switch (numpy_string_to_kind(vectors_info.format)) {
case scalar_kind_t::b1x8_k: add_typed_to_index<b1x8_t>(index, keys_info, vectors_info, force_copy, threads, progress); break;
case scalar_kind_t::i8_k: add_typed_to_index<i8_t>(index, keys_info, vectors_info, force_copy, threads, progress); break;
case scalar_kind_t::f16_k: add_typed_to_index<f16_t>(index, keys_info, vectors_info, force_copy, threads, progress); break;
case scalar_kind_t::f32_k: add_typed_to_index<f32_t>(index, keys_info, vectors_info, force_copy, threads, progress); break;
case scalar_kind_t::f64_k: add_typed_to_index<f64_t>(index, keys_info, vectors_info, force_copy, threads, progress); break;
default: throw std::invalid_argument("Incompatible scalars in the vectors matrix: " + vectors_info.format);
}
// clang-format on
}
template <typename scalar_at>
static void search_typed( //
dense_index_py_t& index, py::buffer_info& vectors_info, //
std::size_t wanted, bool exact, std::size_t threads, //
py::array_t<dense_key_t>& keys_py, py::array_t<distance_t>& distances_py, py::array_t<Py_ssize_t>& counts_py,
std::atomic<std::size_t>& stats_visited_members, std::atomic<std::size_t>& stats_computed_distances,
progress_func_t const& progress) {
auto keys_py2d = keys_py.template mutable_unchecked<2>();
auto distances_py2d = distances_py.template mutable_unchecked<2>();
auto counts_py1d = counts_py.template mutable_unchecked<1>();
Py_ssize_t vectors_count = vectors_info.shape[0];
byte_t const* vectors_data = reinterpret_cast<byte_t const*>(vectors_info.ptr);
if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.try_reserve(index_limits_t(index.size(), threads)))
throw std::invalid_argument("Out of memory!");
// Progress status
progress_t progress_{progress};
std::atomic<std::size_t> processed{0};
atomic_error_t atomic_error{nullptr};
executor_default_t{threads}.dynamic(vectors_count, [&](std::size_t thread_idx, std::size_t task_idx) {
scalar_at const* vector = (scalar_at const*)(vectors_data + task_idx * vectors_info.strides[0]);
dense_search_result_t result = index.search(vector, wanted, thread_idx, exact);
if (!result) {
atomic_error = result.error.release();
return false;
}
counts_py1d(task_idx) =
static_cast<Py_ssize_t>(result.dump_to(&keys_py2d(task_idx, 0), &distances_py2d(task_idx, 0)));
stats_visited_members += result.visited_members;
stats_computed_distances += result.computed_distances;
// We don't want to check for signals from multiple threads
++processed;
if (thread_idx == 0)
if (PyErr_CheckSignals() != 0 || !progress_(processed.load(), vectors_count)) {
atomic_error.store("Operation has been terminated");
return false;
}
return true;
});
// At the end report the latest numbers, because the reporter thread may be finished earlier
progress_(processed.load(), vectors_count);
// Raise the error from a single thread
auto error = atomic_error.load();
if (error) {
PyErr_SetString(PyExc_RuntimeError, error);
throw py::error_already_set();
}
}
template <typename scalar_at>
static void search_typed( //
dense_indexes_py_t& indexes, py::buffer_info& vectors_info, //
std::size_t wanted, bool exact, std::size_t threads, //
py::array_t<dense_key_t>& keys_py, py::array_t<distance_t>& distances_py, py::array_t<Py_ssize_t>& counts_py,
std::atomic<std::size_t>& stats_visited_members, std::atomic<std::size_t>& stats_computed_distances,
progress_func_t const& progress) {
auto keys_py2d = keys_py.template mutable_unchecked<2>();
auto distances_py2d = distances_py.template mutable_unchecked<2>();
auto counts_py1d = counts_py.template mutable_unchecked<1>();
Py_ssize_t vectors_count = vectors_info.shape[0];
byte_t const* vectors_data = reinterpret_cast<byte_t const*>(vectors_info.ptr);
for (std::size_t vector_idx = 0; vector_idx != static_cast<std::size_t>(vectors_count); ++vector_idx)
counts_py1d(vector_idx) = 0;
if (!threads)
threads = std::thread::hardware_concurrency();
bitset_t query_mutexes(static_cast<std::size_t>(vectors_count));
if (!query_mutexes)
throw std::bad_alloc();
// Progress status
progress_t progress_{progress};
std::atomic<std::size_t> processed{0};
atomic_error_t atomic_error{nullptr};
executor_default_t{threads}.dynamic(indexes.shards_.size(), [&](std::size_t thread_idx, std::size_t task_idx) {
dense_index_py_t& index = *indexes.shards_[task_idx].get();
index_limits_t limits;
limits.members = index.size();
limits.threads_add = 0;
limits.threads_search = 1;
if (!index.try_reserve(limits)) {
atomic_error = "Out of memory!";
return false;
}
for (std::size_t vector_idx = 0; vector_idx != static_cast<std::size_t>(vectors_count); ++vector_idx) {
scalar_at const* vector = (scalar_at const*)(vectors_data + vector_idx * vectors_info.strides[0]);
dense_search_result_t result = index.search(vector, wanted, 0, exact);
if (!result) {
atomic_error = result.error.release();
return false;
}
{
auto lock = query_mutexes.lock(vector_idx);
counts_py1d(vector_idx) = static_cast<Py_ssize_t>(result.merge_into( //
&keys_py2d(vector_idx, 0), //
&distances_py2d(vector_idx, 0), //
static_cast<std::size_t>(counts_py1d(vector_idx)), //
wanted));
}
stats_visited_members += result.visited_members;
stats_computed_distances += result.computed_distances;
// We don't want to check for signals from multiple threads
++processed;
if (thread_idx == 0)
if (PyErr_CheckSignals() != 0 || !progress_(processed.load(), indexes.shards_.size())) {
atomic_error.store("Operation has been terminated");
return false;
}
}
return true;
});
// At the end report the latest numbers, because the reporter thread may be finished earlier
progress_(processed.load(), indexes.shards_.size());
// Raise the error from a single thread
auto error = atomic_error.load();
if (error) {
PyErr_SetString(PyExc_RuntimeError, error);
throw py::error_already_set();
}
}
/**
* @param vectors Matrix of vectors to search for.
* @param wanted Number of matches per request.
*
* @return Tuple with:
* 1. matrix of neighbors,
* 2. matrix of distances,
* 3. array with match counts,
* 4. number of visited nodes,
* 4. number of computed pairwise distances.
*/
template <typename index_at>
static py::tuple search_many_in_index( //
index_at& index, py::buffer vectors, std::size_t wanted, bool exact, std::size_t threads,
progress_func_t const& progress) {
if (wanted == 0)
return py::tuple(5);
if (index.limits().threads_search < threads)
throw std::invalid_argument("Can't use that many threads!");
py::buffer_info vectors_info = vectors.request();
if (vectors_info.ndim != 2)
throw std::invalid_argument("Expects a matrix of vectors to add!");
Py_ssize_t vectors_count = vectors_info.shape[0];
Py_ssize_t vectors_dimensions = vectors_info.shape[1];
if (vectors_dimensions != static_cast<Py_ssize_t>(index.scalar_words()))
throw std::invalid_argument("The number of vector dimensions doesn't match!");
py::array_t<dense_key_t> keys_py({vectors_count, static_cast<Py_ssize_t>(wanted)});
py::array_t<distance_t> distances_py({vectors_count, static_cast<Py_ssize_t>(wanted)});
py::array_t<Py_ssize_t> counts_py(vectors_count);
std::atomic<std::size_t> stats_visited_members(0);
std::atomic<std::size_t> stats_computed_distances(0);
// clang-format off
switch (numpy_string_to_kind(vectors_info.format)) {
case scalar_kind_t::b1x8_k: search_typed<b1x8_t>(index, vectors_info, wanted, exact, threads, keys_py, distances_py, counts_py, stats_visited_members, stats_computed_distances, progress); break;
case scalar_kind_t::i8_k: search_typed<i8_t>(index, vectors_info, wanted, exact, threads, keys_py, distances_py, counts_py, stats_visited_members, stats_computed_distances, progress); break;
case scalar_kind_t::f16_k: search_typed<f16_t>(index, vectors_info, wanted, exact, threads, keys_py, distances_py, counts_py, stats_visited_members, stats_computed_distances, progress); break;
case scalar_kind_t::f32_k: search_typed<f32_t>(index, vectors_info, wanted, exact, threads, keys_py, distances_py, counts_py, stats_visited_members, stats_computed_distances, progress); break;
case scalar_kind_t::f64_k: search_typed<f64_t>(index, vectors_info, wanted, exact, threads, keys_py, distances_py, counts_py, stats_visited_members, stats_computed_distances, progress); break;
default: throw std::invalid_argument("Incompatible scalars in the query matrix: " + vectors_info.format);
}
// clang-format on
py::tuple results(5);
results[0] = keys_py;
results[1] = distances_py;
results[2] = counts_py;
results[3] = stats_visited_members.load();
results[4] = stats_computed_distances.load();
return results;
}
/**
* @brief Brute-force @b exact search implementation, compatible with
* NumPy-like Tensors and other objects supporting Buffer Protocol.
*/
static py::tuple search_many_brute_force( //
py::buffer dataset, py::buffer queries, //
std::size_t wanted, std::size_t threads, //
metric_kind_t metric_kind, //
metric_punned_signature_t metric_signature, //
std::uintptr_t metric_uintptr, //
progress_func_t const& progress_func) {
if (wanted == 0)
return py::tuple(5);
py::buffer_info dataset_info = dataset.request();
py::buffer_info queries_info = queries.request();
if (dataset_info.ndim != 2 || queries_info.ndim != 2)
throw std::invalid_argument("Expects a matrix of dataset to add!");
std::size_t dataset_count = static_cast<std::size_t>(dataset_info.shape[0]);
std::size_t dataset_dimensions = static_cast<std::size_t>(dataset_info.shape[1]);
std::size_t dataset_stride = static_cast<std::size_t>(dataset_info.strides[0]);
std::size_t queries_stride = static_cast<std::size_t>(queries_info.strides[0]);
std::size_t queries_count = static_cast<std::size_t>(queries_info.shape[0]);
std::size_t queries_dimensions = static_cast<std::size_t>(queries_info.shape[1]);
if (dataset_dimensions != queries_dimensions)
throw std::invalid_argument("The number of vector dimensions doesn't match!");
if (wanted > dataset_count)
throw std::invalid_argument("You can't request more matches than in the dataset!");
scalar_kind_t dataset_kind = numpy_string_to_kind(dataset_info.format);
scalar_kind_t queries_kind = numpy_string_to_kind(queries_info.format);
if (dataset_kind != queries_kind)
throw std::invalid_argument("The types of vectors don't match!");
std::size_t dimensions = static_cast<std::size_t>(queries_dimensions);
metric_t metric = //
metric_uintptr //
? metric_t::stateless(dimensions, metric_uintptr, metric_signature, metric_kind, queries_kind)
: metric_t::builtin(dimensions, metric_kind, queries_kind);
if (!metric)
throw std::invalid_argument("Unsupported metric!");
py::array_t<dense_key_t> keys_py({static_cast<Py_ssize_t>(queries_count), static_cast<Py_ssize_t>(wanted)});
py::array_t<distance_t> distances_py({static_cast<Py_ssize_t>(queries_count), static_cast<Py_ssize_t>(wanted)});
py::array_t<Py_ssize_t> counts_py(static_cast<Py_ssize_t>(queries_count));
auto keys_py2d = keys_py.template mutable_unchecked<2>();
auto distances_py2d = distances_py.template mutable_unchecked<2>();
auto counts_py1d = counts_py.template mutable_unchecked<1>();
byte_t const* dataset_data = reinterpret_cast<byte_t const*>(dataset_info.ptr);
byte_t const* queries_data = reinterpret_cast<byte_t const*>(queries_info.ptr);
for (std::size_t query_idx = 0; query_idx != queries_count; ++query_idx)
counts_py1d(query_idx) = wanted;
if (!threads)
threads = std::thread::hardware_concurrency();
// Dispatch brute-force search
progress_t progress{progress_func};
executor_default_t executor{threads};
exact_search_t search;
exact_search_results_t offsets_and_distances = search( //
dataset_data, dataset_count, dataset_stride, //
queries_data, queries_count, queries_stride, //
wanted, metric, executor,
[&](std::size_t passed, std::size_t total) { return PyErr_CheckSignals() == 0 && progress(passed, total); });
if (!offsets_and_distances)
throw std::bad_alloc();
// Export the results
for (std::size_t query_idx = 0; query_idx != queries_count; ++query_idx) {
dense_key_t* query_keys = &keys_py2d(query_idx, 0);
distance_t* query_distances = &distances_py2d(query_idx, 0);
auto query_result = offsets_and_distances.at(query_idx);
for (std::size_t i = 0; i != wanted; ++i)
query_keys[i] = static_cast<dense_key_t>(query_result[i].offset),
query_distances[i] = query_result[i].distance;
}
py::tuple results(5);
results[0] = keys_py;
results[1] = distances_py;
results[2] = counts_py;
results[3] = 0;
results[4] = static_cast<std::size_t>(dataset_count * queries_count);
return results;
}
/**
* @brief Brute-force @b K-Means clustering, compatible with
* NumPy-like Tensors and other objects supporting Buffer Protocol.
*/
static py::tuple cluster_many_brute_force( //
py::buffer dataset, //
std::size_t wanted, //
std::size_t max_iterations, //
double inertia_threshold, //
double max_seconds, //
double min_shifts, //
std::uint64_t seed, //
std::size_t threads, //
scalar_kind_t scalar_kind, //
metric_kind_t metric_kind, //
progress_func_t const& progress_func) {
using distance_t = typename kmeans_clustering_t::distance_t;
py::buffer_info dataset_info = dataset.request();
if (dataset_info.ndim != 2)
throw std::invalid_argument("Expects a matrix (rank-2 tensor) of dataset to cluster!");
std::size_t dataset_count = static_cast<std::size_t>(dataset_info.shape[0]);
std::size_t dataset_dimensions = static_cast<std::size_t>(dataset_info.shape[1]);
std::size_t dataset_stride = static_cast<std::size_t>(dataset_info.strides[0]);
scalar_kind_t dataset_kind = numpy_string_to_kind(dataset_info.format);
std::size_t bytes_per_scalar = bits_per_scalar_word(dataset_kind) / CHAR_BIT;
std::vector<std::size_t> point_to_centroid_index(dataset_count, 0);
std::vector<distance_t> point_to_centroid_distance(dataset_count, 0);
std::vector<byte_t> centroids(wanted * dataset_dimensions * bytes_per_scalar, 0);
if (!threads)
threads = std::thread::hardware_concurrency();
// Dispatch brute-force search
progress_t progress{progress_func};
executor_default_t executor{threads};
kmeans_clustering_t engine;
engine.metric_kind = metric_kind;
engine.quantization_kind = scalar_kind;
engine.max_iterations = max_iterations;
engine.min_shifts = min_shifts;
engine.max_seconds = max_seconds;
engine.inertia_threshold = inertia_threshold;
kmeans_clustering_result_t result = engine( //
reinterpret_cast<byte_t const*>(dataset_info.ptr), dataset_count, dataset_stride, //
centroids.data(), wanted, dataset_dimensions * bytes_per_scalar, //
point_to_centroid_index.data(), point_to_centroid_distance.data(), dataset_kind, dataset_dimensions, executor,
[&](std::size_t passed, std::size_t total) { return PyErr_CheckSignals() == 0 && progress(passed, total); });
if (!result)
throw std::runtime_error(result.error.release());
// Following constructor doesn't seem to be documented, but it's used in the source code of `pybind11`
// https://github.com/pybind/pybind11/blob/aeda49ed0b4e6e8abba7abc265ace86a6c26ba66/include/pybind11/numpy.h#L918-L919
// https://github.com/pybind/pybind11/blob/aeda49ed0b4e6e8abba7abc265ace86a6c26ba66/include/pybind11/buffer_info.h#L60-L75
py::buffer_info centroids_info;
centroids_info.ptr = reinterpret_cast<void*>(centroids.data());
centroids_info.itemsize = dataset_info.itemsize;
centroids_info.size = wanted * dataset_dimensions;
centroids_info.format = dataset_info.format;
centroids_info.ndim = 2;
centroids_info.shape = {static_cast<ssize_t>(wanted), static_cast<ssize_t>(dataset_dimensions)};
centroids_info.strides = {static_cast<ssize_t>(dataset_dimensions * bytes_per_scalar),
static_cast<ssize_t>(bytes_per_scalar)};
py::tuple results(3);
results[0] = py::array_t<std::size_t>({static_cast<ssize_t>(dataset_count)}, point_to_centroid_index.data());
results[1] = py::array_t<distance_t>({static_cast<ssize_t>(dataset_count)}, point_to_centroid_distance.data());
results[2] = py::array(centroids_info);
return results;
}
template <typename scalar_at> struct rows_lookup_gt {
byte_t* data_;
std::size_t stride_;
rows_lookup_gt(void* data, std::size_t stride) noexcept : data_((byte_t*)data), stride_(stride) {}
scalar_at* operator[](std::size_t i) const noexcept { return reinterpret_cast<scalar_at*>(data_ + i * stride_); }
std::ptrdiff_t operator-(rows_lookup_gt const& other) const noexcept { return (data_ - other.data_) / stride_; }
rows_lookup_gt operator+(std::size_t n) const noexcept { return {data_ + stride_ * n, stride_}; }
template <typename other_scalar_at> rows_lookup_gt<other_scalar_at> as() const noexcept { return {data_, stride_}; }
};
/**
* @param queries Matrix of vectors to search for.
* @param count Number of clusters to produce.
*
* @return Tuple with:
* 1. vector of cluster IDs,
* 2. vector of distances to those clusters,
* 3. array with match counts, set to all ones,
* 4. number of visited nodes,
* 5. number of computed pairwise distances.
*/
template <typename index_at>
static py::tuple cluster_vectors( //
index_at& index, py::buffer queries, //
std::size_t min_count, std::size_t max_count, std::size_t threads, progress_func_t const& progress) {
if (index.limits().threads_search < threads)
throw std::invalid_argument("Can't use that many threads!");
py::buffer_info queries_info = queries.request();
if (queries_info.ndim != 2)
throw std::invalid_argument("Expects a matrix of queries to add!");
std::size_t queries_count = static_cast<std::size_t>(queries_info.shape[0]);
std::size_t queries_stride = static_cast<std::size_t>(queries_info.strides[0]);
std::size_t queries_dimensions = static_cast<std::size_t>(queries_info.shape[1]);
if (queries_dimensions != index.scalar_words())
throw std::invalid_argument("The number of vector dimensions doesn't match!");
py::array_t<dense_key_t> keys_py({Py_ssize_t(queries_count), Py_ssize_t(1)});
py::array_t<distance_t> distances_py({Py_ssize_t(queries_count), Py_ssize_t(1)});
dense_clustering_result_t cluster_result;
executor_default_t executor{threads};
auto keys_py2d = keys_py.template mutable_unchecked<2>();
auto distances_py2d = distances_py.template mutable_unchecked<2>();
dense_key_t* keys_ptr = reinterpret_cast<dense_key_t*>(&keys_py2d(0, 0));
distance_t* distances_ptr = reinterpret_cast<distance_t*>(&distances_py2d(0, 0));
index_dense_clustering_config_t config;
config.min_clusters = min_count;
config.max_clusters = max_count;
rows_lookup_gt<byte_t const> queries_begin(queries_info.ptr, queries_stride);
rows_lookup_gt<byte_t const> queries_end = queries_begin + queries_count;
// clang-format off
switch (numpy_string_to_kind(queries_info.format)) {
case scalar_kind_t::b1x8_k: cluster_result = index.cluster(queries_begin.as<b1x8_t const>(), queries_end.as<b1x8_t const>(), config, keys_ptr, distances_ptr, executor, progress_t{progress}); break;
case scalar_kind_t::i8_k: cluster_result = index.cluster(queries_begin.as<i8_t const>(), queries_end.as<i8_t const>(), config, keys_ptr, distances_ptr, executor, progress_t{progress}); break;
case scalar_kind_t::f16_k: cluster_result = index.cluster(queries_begin.as<f16_t const>(), queries_end.as<f16_t const>(), config, keys_ptr, distances_ptr, executor, progress_t{progress}); break;
case scalar_kind_t::f32_k: cluster_result = index.cluster(queries_begin.as<f32_t const>(), queries_end.as<f32_t const>(), config, keys_ptr, distances_ptr, executor, progress_t{progress}); break;
case scalar_kind_t::f64_k: cluster_result = index.cluster(queries_begin.as<f64_t const>(), queries_end.as<f64_t const>(), config, keys_ptr, distances_ptr, executor, progress_t{progress}); break;
default: throw std::invalid_argument("Incompatible scalars in the query matrix: " + queries_info.format);
}
// clang-format on
cluster_result.error.raise();
// Those would be set to 1 for all entries, in case of success
py::array_t<Py_ssize_t> counts_py(queries_count);
auto counts_py1d = counts_py.template mutable_unchecked<1>();
for (std::size_t query_idx = 0; query_idx != queries_count; ++query_idx)
counts_py1d(static_cast<Py_ssize_t>(query_idx)) = 1;
py::tuple results(5);
results[0] = keys_py;
results[1] = distances_py;
results[2] = counts_py;
results[3] = cluster_result.visited_members;
results[4] = cluster_result.computed_distances;
return results;
}
/**
* @param queries Array of keys to cluster.
* @param count Number of clusters to produce.
*
* @return Tuple with:
* 1. vector of cluster IDs,
* 2. vector of distances to those clusters,
* 3. array with match counts, set to all ones,
* 4. number of visited nodes,
* 5. number of computed pairwise distances.
*/
template <typename index_at>
static py::tuple cluster_keys( //
index_at& index, py::array_t<dense_key_t> queries_py, //
std::size_t min_count, std::size_t max_count, std::size_t threads, progress_func_t const& progress) {
if (index.limits().threads_search < threads)
throw std::invalid_argument("Can't use that many threads!");
std::size_t queries_count = static_cast<std::size_t>(queries_py.size());
auto queries_py1d = queries_py.template unchecked<1>();
dense_key_t const* queries_begin = &queries_py1d(0);
dense_key_t const* queries_end = queries_begin + queries_count;
py::array_t<dense_key_t> keys_py({Py_ssize_t(queries_count), Py_ssize_t(1)});
py::array_t<distance_t> distances_py({Py_ssize_t(queries_count), Py_ssize_t(1)});
executor_default_t executor{threads};
auto keys_py2d = keys_py.template mutable_unchecked<2>();
auto distances_py2d = distances_py.template mutable_unchecked<2>();
dense_key_t* keys_ptr = reinterpret_cast<dense_key_t*>(&keys_py2d(0, 0));
distance_t* distances_ptr = reinterpret_cast<distance_t*>(&distances_py2d(0, 0));
index_dense_clustering_config_t config;
config.min_clusters = min_count;
config.max_clusters = max_count;
dense_clustering_result_t cluster_result =
index.cluster(queries_begin, queries_end, config, keys_ptr, distances_ptr, executor, progress_t{progress});
cluster_result.error.raise();
// Those would be set to 1 for all entries, in case of success
py::array_t<Py_ssize_t> counts_py(queries_count);
auto counts_py1d = counts_py.template mutable_unchecked<1>();
for (std::size_t query_idx = 0; query_idx != queries_count; ++query_idx)
counts_py1d(static_cast<Py_ssize_t>(query_idx)) = 1;
py::tuple results(5);
results[0] = keys_py;
results[1] = distances_py;
results[2] = counts_py;
results[3] = cluster_result.visited_members;
results[4] = cluster_result.computed_distances;
return results;
}
static std::unordered_map<dense_key_t, dense_key_t> join_index( //
dense_index_py_t const& a, dense_index_py_t const& b, //
std::size_t max_proposals, bool exact, //
progress_func_t const& progress) {
std::unordered_map<dense_key_t, dense_key_t> a_to_b;
dummy_key_to_key_mapping_t b_to_a;
a_to_b.reserve((std::min)(a.size(), b.size()));
index_join_config_t config;
config.max_proposals = max_proposals;
config.exact = exact;
config.expansion = (std::max)(a.expansion_search(), b.expansion_search());
std::size_t threads = (std::min)(a.limits().threads(), b.limits().threads());
executor_default_t executor{threads};
join_result_t result = a.join(b, config, a_to_b, b_to_a, executor, progress_t{progress});
forward_error(result);
return a_to_b;
}
static dense_index_py_t copy_index(dense_index_py_t const& index, bool force_copy) {
using copy_result_t = typename dense_index_py_t::copy_result_t;
index_dense_copy_config_t config;
config.force_vector_copy = force_copy;
copy_result_t result = index.copy(config);
forward_error(result);
return std::move(result.index);
}
static void compact_index(dense_index_py_t& index, std::size_t threads, progress_func_t const& progress) {
if (!threads)
threads = std::thread::hardware_concurrency();
if (!index.try_reserve(index_limits_t(index.size(), threads)))
throw std::invalid_argument("Out of memory!");
index.compact(executor_default_t{threads}, progress_t{progress});
}
static py::dict index_metadata(index_dense_metadata_result_t const& meta) {
py::dict result;
result["matrix_included"] = !meta.config.exclude_vectors;
result["matrix_uses_64_bit_dimensions"] = meta.config.use_64_bit_dimensions;
index_dense_head_t const& head = meta.head;
result["version"] = std::to_string(head.version_major) + "." + //
std::to_string(head.version_minor) + "." + //
std::to_string(head.version_patch);
result["kind_metric"] = metric_kind_t(head.kind_metric);
result["kind_scalar"] = scalar_kind_t(head.kind_scalar);
result["kind_key"] = scalar_kind_t(head.kind_key);
result["kind_compressed_slot"] = scalar_kind_t(head.kind_compressed_slot);
result["count_present"] = std::uint64_t(head.count_present);
result["count_deleted"] = std::uint64_t(head.count_deleted);
result["dimensions"] = std::uint64_t(head.dimensions);
return result;
}
// clang-format off
template <typename index_at> void save_index_to_path(index_at const& index, std::string const& path, progress_func_t const& progress) { index.save(path.c_str(), {}, progress_t{progress}).error.raise(); }
template <typename index_at> void load_index_from_path(index_at& index, std::string const& path, progress_func_t const& progress) { index.load(path.c_str(), {}, progress_t{progress}).error.raise(); }
template <typename index_at> void view_index_from_path(index_at& index, std::string const& path, progress_func_t const& progress) { index.view(path.c_str(), 0, {}, progress_t{progress}).error.raise(); }
template <typename index_at> void reset_index(index_at& index) { index.reset(); }
template <typename index_at> void clear_index(index_at& index) { index.clear(); }
template <typename index_at> std::size_t max_level(index_at const &index) { return index.max_level(); }
template <typename index_at> std::size_t serialized_length(index_at const &index) { return index.serialized_length(); }
template <typename index_at> typename index_at::stats_t compute_stats(index_at const &index) { return index.stats(); }
template <typename index_at> typename index_at::stats_t compute_level_stats(index_at const &index, std::size_t level) { return index.stats(level); }
// clang-format on
template <typename py_bytes_at> memory_mapped_file_t memory_map_from_bytes(py_bytes_at&& bytes) {
py::buffer_info info(py::buffer(bytes).request());
return {(byte_t*)(info.ptr), static_cast<std::size_t>(info.size)};
}
template <typename index_at> py::object save_index_to_buffer(index_at const& index, progress_func_t const& progress) {
std::size_t serialized_length = index.serialized_length();
// Create an empty bytearray object using CPython API
PyObject* byte_array = PyByteArray_FromStringAndSize(nullptr, 0);
if (!byte_array)
throw std::runtime_error("Could not allocate bytearray object");
// Resize the bytearray object to the desired length
if (PyByteArray_Resize(byte_array, static_cast<Py_ssize_t>(serialized_length)) != 0) {
Py_XDECREF(byte_array);
throw std::runtime_error("Could not resize bytearray object");
}
char* buffer = PyByteArray_AS_STRING(byte_array);
memory_mapped_file_t memory_map((byte_t*)buffer, serialized_length);
serialization_result_t result = index.save(std::move(memory_map), {}, {}, progress_t{progress});
if (!result) {
Py_XDECREF(byte_array);
result.error.raise();
}
return py::reinterpret_steal<py::object>(byte_array);
}
template <typename index_at>
void load_index_from_buffer(index_at& index, py::bytes const& buffer, progress_func_t const& progress) {
index.load(memory_map_from_bytes(buffer), {}, {}, progress_t{progress}).error.raise();
}
template <typename index_at>
void view_index_from_buffer(index_at& index, py::bytes const& buffer, progress_func_t const& progress) {
index.view(memory_map_from_bytes(buffer), {}, {}, progress_t{progress}).error.raise();
}
template <typename index_at> std::vector<typename index_at::stats_t> compute_levels_stats(index_at const& index) {
using stats_t = typename index_at::stats_t;
std::size_t max_level = index.max_level();
std::vector<stats_t> result(max_level + 1);
index.stats(result.data(), max_level);
return result;
}
template <typename internal_at, typename external_at = internal_at, typename index_at = void>
static py::object get_typed_vectors_for_keys(index_at const& index, py::buffer keys) {
py::buffer_info keys_info = keys.request();
if (keys_info.ndim != 1)
throw std::invalid_argument("Keys must be placed in a single-dimensional array!");
Py_ssize_t keys_count = keys_info.shape[0];
byte_t const* keys_data = reinterpret_cast<byte_t const*>(keys_info.ptr);
if (index.multi()) {
py::tuple results(keys_count);
for (Py_ssize_t task_idx = 0; task_idx != keys_count; ++task_idx) {
dense_key_t key = *reinterpret_cast<dense_key_t const*>(keys_data + task_idx * keys_info.strides[0]);
std::size_t vectors_count = index.count(key);
if (!vectors_count) {
results[task_idx] = py::none();
continue;
}
py::array_t<external_at> result_py({static_cast<Py_ssize_t>(vectors_count), //
static_cast<Py_ssize_t>(index.scalar_words())});
auto result_py2d = result_py.template mutable_unchecked<2>();
index.get(key, (internal_at*)&result_py2d(0, 0), vectors_count);
results[task_idx] = result_py;
}
return results;
} else {
py::array_t<external_at> result_py({keys_count, static_cast<Py_ssize_t>(index.scalar_words())});
auto result_py2d = result_py.template mutable_unchecked<2>();
for (Py_ssize_t task_idx = 0; task_idx != keys_count; ++task_idx) {
dense_key_t key = *reinterpret_cast<dense_key_t const*>(keys_data + task_idx * keys_info.strides[0]);
index.get(key, (internal_at*)&result_py2d(task_idx, 0), 1);
}
return result_py;
}
}
template <typename index_at> py::object get_many(index_at const& index, py::buffer keys, scalar_kind_t scalar_kind) {
if (scalar_kind == scalar_kind_t::f32_k)
return get_typed_vectors_for_keys<f32_t>(index, keys);
else if (scalar_kind == scalar_kind_t::f64_k)
return get_typed_vectors_for_keys<f64_t>(index, keys);
else if (scalar_kind == scalar_kind_t::f16_k)
return get_typed_vectors_for_keys<f16_t, std::uint16_t>(index, keys);
else if (scalar_kind == scalar_kind_t::i8_k)
return get_typed_vectors_for_keys<i8_t, std::int8_t>(index, keys);
else if (scalar_kind == scalar_kind_t::b1x8_k)
return get_typed_vectors_for_keys<b1x8_t, std::uint8_t>(index, keys);
else
throw std::invalid_argument("Incompatible scalars in the query matrix!");
}
PYBIND11_MODULE(compiled, m) {
m.doc() = "Smaller & Faster Single-File Vector Search Engine from Unum";
m.attr("DEFAULT_CONNECTIVITY") = py::int_(default_connectivity());
m.attr("DEFAULT_EXPANSION_ADD") = py::int_(default_expansion_add());
m.attr("DEFAULT_EXPANSION_SEARCH") = py::int_(default_expansion_search());
m.attr("USES_OPENMP") = py::int_(USEARCH_USE_OPENMP);
m.attr("USES_FP16LIB") = py::int_(USEARCH_USE_FP16LIB);
m.attr("USES_SIMSIMD") = py::int_(USEARCH_USE_SIMSIMD);
#if USEARCH_USE_SIMSIMD
m.attr("USES_SIMSIMD_DYNAMIC_DISPATCH") = py::int_(simsimd_uses_dynamic_dispatch());
#else
m.attr("USES_SIMSIMD_DYNAMIC_DISPATCH") = py::int_(0);
#endif
m.attr("VERSION_MAJOR") = py::int_(USEARCH_VERSION_MAJOR);
m.attr("VERSION_MINOR") = py::int_(USEARCH_VERSION_MINOR);
m.attr("VERSION_PATCH") = py::int_(USEARCH_VERSION_PATCH);
py::enum_<metric_punned_signature_t>(m, "MetricSignature")
.value("ArrayArray", metric_punned_signature_t::array_array_k)
.value("ArrayArraySize", metric_punned_signature_t::array_array_size_k);
py::enum_<metric_kind_t>(m, "MetricKind")
.value("Unknown", metric_kind_t::unknown_k)
.value("IP", metric_kind_t::ip_k)
.value("Cos", metric_kind_t::cos_k)
.value("L2sq", metric_kind_t::l2sq_k)
.value("Haversine", metric_kind_t::haversine_k)
.value("Divergence", metric_kind_t::divergence_k)
.value("Pearson", metric_kind_t::pearson_k)
.value("Jaccard", metric_kind_t::jaccard_k)
.value("Hamming", metric_kind_t::hamming_k)
.value("Tanimoto", metric_kind_t::tanimoto_k)
.value("Sorensen", metric_kind_t::sorensen_k)
.value("Cosine", metric_kind_t::cos_k)
.value("InnerProduct", metric_kind_t::ip_k);
py::enum_<scalar_kind_t>(m, "ScalarKind")
.value("Unknown", scalar_kind_t::unknown_k)
.value("B1", scalar_kind_t::b1x8_k)
.value("U40", scalar_kind_t::u40_k)
.value("UUID", scalar_kind_t::uuid_k)
.value("BF16", scalar_kind_t::bf16_k)
.value("F64", scalar_kind_t::f64_k)
.value("F32", scalar_kind_t::f32_k)
.value("F16", scalar_kind_t::f16_k)
.value("F8", scalar_kind_t::f8_k)
.value("U64", scalar_kind_t::u64_k)
.value("U32", scalar_kind_t::u32_k)
.value("U16", scalar_kind_t::u16_k)