-
Notifications
You must be signed in to change notification settings - Fork 67
/
cagra.hpp
1755 lines (1654 loc) · 67 KB
/
cagra.hpp
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
/*
* Copyright (c) 2023-2024, NVIDIA 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.
*/
#pragma once
#include "common.hpp"
#include <cuvs/distance/distance.hpp>
#include <cuvs/neighbors/common.hpp>
#include <cuvs/neighbors/ivf_pq.hpp>
#include <cuvs/neighbors/nn_descent.hpp>
#include <raft/core/device_mdspan.hpp>
#include <raft/core/host_device_accessor.hpp>
#include <raft/core/host_mdspan.hpp>
#include <raft/core/mdspan.hpp>
#include <raft/core/mdspan_types.hpp>
#include <raft/core/resource/stream_view.hpp>
#include <raft/core/resources.hpp>
#include <raft/util/integer_utils.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <optional>
#include <variant>
namespace cuvs::neighbors::cagra {
/**
* @defgroup cagra_cpp_index_params CAGRA index build parameters
* @{
*/
/**
* @brief ANN parameters used by CAGRA to build knn graph
*
*/
namespace graph_build_params {
/** Specialized parameters utilizing IVF-PQ to build knn graph */
struct ivf_pq_params {
cuvs::neighbors::ivf_pq::index_params build_params;
cuvs::neighbors::ivf_pq::search_params search_params;
float refinement_rate;
ivf_pq_params() = default;
/**
* Set default parameters based on shape of the input dataset.
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* raft::resources res;
* // create index_params for a [N. D] dataset
* auto dataset = raft::make_device_matrix<float, int64_t>(res, N, D);
* auto pq_params =
* cagra::graph_build_params::ivf_pq_params(dataset.extents());
* // modify/update index_params as needed
* pq_params.kmeans_trainset_fraction = 0.1;
* @endcode
*/
ivf_pq_params(raft::matrix_extent<int64_t> dataset_extents,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Expanded);
};
using nn_descent_params = cuvs::neighbors::nn_descent::index_params;
} // namespace graph_build_params
struct index_params : cuvs::neighbors::index_params {
/** Degree of input graph for pruning. */
size_t intermediate_graph_degree = 128;
/** Degree of output graph. */
size_t graph_degree = 64;
/**
* Specify compression parameters if compression is desired. If set, overrides the
* attach_dataset_on_build (and the compressed dataset is always added to the index).
*/
std::optional<cuvs::neighbors::vpq_params> compression = std::nullopt;
/** Parameters for graph building.
*
* Set ivf_pq_params or nn_descent_params to select the graph build algorithm and control their
* parameters. The default (std::monostate) is to use a heuristic to decide the algorithm and its
* parameters.
*
* @code{.cpp}
* cagra::index_params params;
* // 1. Choose IVF-PQ algorithm
* params.graph_build_params = cagra::graph_build_params::ivf_pq_params(dataset.extent,
* params.metric);
*
* // 2. Choose NN Descent algorithm for kNN graph construction
* params.graph_build_params =
* cagra::graph_build_params::nn_descent_params(params.intermediate_graph_degree);
* @endcode
*/
std::variant<std::monostate,
graph_build_params::ivf_pq_params,
graph_build_params::nn_descent_params>
graph_build_params;
/**
* Whether to use MST optimization to guarantee graph connectivity.
*/
bool guarantee_connectivity = false;
/**
* Whether to add the dataset content to the index, i.e.:
*
* - `true` means the index is filled with the dataset vectors and ready to search after calling
* `build` provided there is enough memory available.
* - `false` means `build` only builds the graph and the user is expected to
* update the dataset using cuvs::neighbors::cagra::update_dataset.
*
* Regardless of the value of `attach_dataset_on_build`, the search graph is created using all
* the vectors in the dataset. Setting `attach_dataset_on_build = false` can be useful if
* the user needs to build only the search graph but does not intend to search it using CAGRA
* (e.g. search using another graph search algorithm), or if specific memory placement options
* need to be applied on the dataset before it is attached to the index using `update_dataset`.
* API.
* @code{.cpp}
* auto dataset = raft::make_device_matrix<float, int64_t>(res, n_rows, n_cols);
* // use default index_parameters
* cagra::index_params index_params;
* // update index_params to only build the CAGRA graph
* index_params.attach_dataset_on_build = false;
* auto index = cagra::build(res, index_params, dataset.view());
* // assert that the dataset is not attached to the index
* ASSERT(index.dataset().extent(0) == 0);
* // update dataset
* index.update_dataset(res, dataset.view());
* // The index is now ready for search
* cagra::search(res, search_params, index, queries, neighbors, distances);
* @endcode
*/
bool attach_dataset_on_build = true;
};
/**
* @}
*/
/**
* @defgroup cagra_cpp_search_params CAGRA index search parameters
* @{
*/
enum class search_algo {
/** For large batch sizes. */
SINGLE_CTA,
/** For small batch sizes. */
MULTI_CTA,
MULTI_KERNEL,
AUTO
};
enum class hash_mode { HASH, SMALL, AUTO };
struct search_params : cuvs::neighbors::search_params {
/** Maximum number of queries to search at the same time (batch size). Auto select when 0.*/
size_t max_queries = 0;
/** Number of intermediate search results retained during the search.
*
* This is the main knob to adjust trade off between accuracy and search speed.
* Higher values improve the search accuracy.
*/
size_t itopk_size = 64;
/** Upper limit of search iterations. Auto select when 0.*/
size_t max_iterations = 0;
// In the following we list additional search parameters for fine tuning.
// Reasonable default values are automatically chosen.
/** Which search implementation to use. */
search_algo algo = search_algo::AUTO;
/** Number of threads used to calculate a single distance. 4, 8, 16, or 32. */
size_t team_size = 0;
/** Number of graph nodes to select as the starting point for the search in each iteration. aka
* search width?*/
size_t search_width = 1;
/** Lower limit of search iterations. */
size_t min_iterations = 0;
/** Thread block size. 0, 64, 128, 256, 512, 1024. Auto selection when 0. */
size_t thread_block_size = 0;
/** Hashmap type. Auto selection when AUTO. */
hash_mode hashmap_mode = hash_mode::AUTO;
/** Lower limit of hashmap bit length. More than 8. */
size_t hashmap_min_bitlen = 0;
/** Upper limit of hashmap fill rate. More than 0.1, less than 0.9.*/
float hashmap_max_fill_rate = 0.5;
/** Number of iterations of initial random seed node selection. 1 or more. */
uint32_t num_random_samplings = 1;
/** Bit mask used for initial random seed node selection. */
uint64_t rand_xor_mask = 0x128394;
/** Whether to use the persistent version of the kernel (only SINGLE_CTA is supported a.t.m.) */
bool persistent = false;
/** Persistent kernel: time in seconds before the kernel stops if no requests received. */
float persistent_lifetime = 2;
/**
* Set the fraction of maximum grid size used by persistent kernel.
* Value 1.0 means the kernel grid size is maximum possible for the selected device.
* The value must be greater than 0.0 and not greater than 1.0.
*
* One may need to run other kernels alongside this persistent kernel. This parameter can
* be used to reduce the grid size of the persistent kernel to leave a few SMs idle.
* Note: running any other work on GPU alongside with the persistent kernel makes the setup
* fragile.
* - Running another kernel in another thread usually works, but no progress guaranteed
* - Any CUDA allocations block the context (this issue may be obscured by using pools)
* - Memory copies to not-pinned host memory may block the context
*
* Even when we know there are no other kernels working at the same time, setting
* kDeviceUsage to 1.0 surprisingly sometimes hurts performance. Proceed with care.
* If you suspect this is an issue, you can reduce this number to ~0.9 without a significant
* impact on the throughput.
*/
float persistent_device_usage = 1.0;
};
/**
* @}
*/
/**
* @defgroup cagra_cpp_extend_params CAGRA index extend parameters
* @{
*/
struct extend_params {
/** The additional dataset is divided into chunks and added to the graph. This is the knob to
* adjust the tradeoff between the recall and operation throughput. Large chunk sizes can result
* in high throughput, but use more working memory (O(max_chunk_size*degree^2)). This can also
* degrade recall because no edges are added between the nodes in the same chunk. Auto select when
* 0. */
uint32_t max_chunk_size = 0;
};
/**
* @}
*/
static_assert(std::is_aggregate_v<index_params>);
static_assert(std::is_aggregate_v<search_params>);
/**
* @defgroup cagra_cpp_index CAGRA index type
* @{
*/
/**
* @brief CAGRA index.
*
* The index stores the dataset and a kNN graph in device memory.
*
* @tparam T data element type
* @tparam IdxT type of the vector indices (represent dataset.extent(0))
*
*/
template <typename T, typename IdxT>
struct index : cuvs::neighbors::index {
using index_params_type = cagra::index_params;
using search_params_type = cagra::search_params;
using index_type = IdxT;
using value_type = T;
static_assert(!raft::is_narrowing_v<uint32_t, IdxT>,
"IdxT must be able to represent all values of uint32_t");
public:
/** Distance metric used for clustering. */
[[nodiscard]] constexpr inline auto metric() const noexcept -> cuvs::distance::DistanceType
{
return metric_;
}
/** Total length of the index (number of vectors). */
[[nodiscard]] constexpr inline auto size() const noexcept -> IdxT
{
auto data_rows = dataset_->n_rows();
return data_rows > 0 ? data_rows : graph_view_.extent(0);
}
/** Dimensionality of the data. */
[[nodiscard]] constexpr inline auto dim() const noexcept -> uint32_t { return dataset_->dim(); }
/** Graph degree */
[[nodiscard]] constexpr inline auto graph_degree() const noexcept -> uint32_t
{
return graph_view_.extent(1);
}
[[nodiscard]] inline auto dataset() const noexcept
-> raft::device_matrix_view<const T, int64_t, raft::layout_stride>
{
auto p = dynamic_cast<strided_dataset<T, int64_t>*>(dataset_.get());
if (p != nullptr) { return p->view(); }
auto d = dataset_->dim();
return raft::make_device_strided_matrix_view<const T, int64_t>(nullptr, 0, d, d);
}
/** Dataset [size, dim] */
[[nodiscard]] inline auto data() const noexcept -> const cuvs::neighbors::dataset<int64_t>&
{
return *dataset_;
}
/** neighborhood graph [size, graph-degree] */
[[nodiscard]] inline auto graph() const noexcept
-> raft::device_matrix_view<const IdxT, int64_t, raft::row_major>
{
return graph_view_;
}
// Don't allow copying the index for performance reasons (try avoiding copying data)
index(const index&) = delete;
index(index&&) = default;
auto operator=(const index&) -> index& = delete;
auto operator=(index&&) -> index& = default;
~index() = default;
/** Construct an empty index. */
index(raft::resources const& res,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Expanded)
: cuvs::neighbors::index(),
metric_(metric),
graph_(raft::make_device_matrix<IdxT, int64_t>(res, 0, 0)),
dataset_(new cuvs::neighbors::empty_dataset<int64_t>(0))
{
}
/** Construct an index from dataset and knn_graph arrays
*
* If the dataset and graph is already in GPU memory, then the index is just a thin wrapper around
* these that stores a non-owning a reference to the arrays.
*
* The constructor also accepts host arrays. In that case they are copied to the device, and the
* device arrays will be owned by the index.
*
* In case the dasates rows are not 16 bytes aligned, then we create a padded copy in device
* memory to ensure alignment for vectorized load.
*
* Usage examples:
*
* - Cagra index is normally created by the cagra::build
* @code{.cpp}
* using namespace raft::neighbors::experimental;
* auto dataset = raft::make_host_matrix<float, int64_t>(n_rows, n_cols);
* load_dataset(dataset.view());
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t, int64_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float, int64_t>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
* In the above example, we have passed a host dataset to build. The returned index will own a
* device copy of the dataset and the knn_graph. In contrast, if we pass the dataset as a
* device_mdspan to build, then it will only store a reference to it.
*
* - Constructing index using existing knn-graph
* @code{.cpp}
* using namespace raft::neighbors::experimental;
*
* auto dataset = raft::make_device_matrix<float, int64_t>(res, n_rows, n_cols);
* auto knn_graph = raft::make_device_matrix<uint32_n, int64_t>(res, n_rows, graph_degree);
*
* // custom loading and graph creation
* // load_dataset(dataset.view());
* // create_knn_graph(knn_graph.view());
*
* // Wrap the existing device arrays into an index structure
* cagra::index<T, IdxT> index(res, metric, raft::make_const_mdspan(dataset.view()),
* raft::make_const_mdspan(knn_graph.view()));
*
* // Both knn_graph and dataset objects have to be in scope while the index is used because
* // the index only stores a reference to these.
* cagra::search(res, search_params, index, queries, neighbors, distances);
* @endcode
*/
template <typename data_accessor, typename graph_accessor>
index(raft::resources const& res,
cuvs::distance::DistanceType metric,
raft::mdspan<const T, raft::matrix_extent<int64_t>, raft::row_major, data_accessor> dataset,
raft::mdspan<const IdxT, raft::matrix_extent<int64_t>, raft::row_major, graph_accessor>
knn_graph)
: cuvs::neighbors::index(),
metric_(metric),
graph_(raft::make_device_matrix<IdxT, int64_t>(res, 0, 0)),
dataset_(make_aligned_dataset(res, dataset, 16))
{
RAFT_EXPECTS(dataset.extent(0) == knn_graph.extent(0),
"Dataset and knn_graph must have equal number of rows");
update_graph(res, knn_graph);
raft::resource::sync_stream(res);
}
/**
* Replace the dataset with a new dataset.
*
* If the new dataset rows are aligned on 16 bytes, then only a reference is stored to the
* dataset. It is the caller's responsibility to ensure that dataset stays alive as long as the
* index. It is expected that the same set of vectors are used for update_dataset and index build.
*/
void update_dataset(raft::resources const& res,
raft::device_matrix_view<const T, int64_t, raft::row_major> dataset)
{
dataset_ = make_aligned_dataset(res, dataset, 16);
}
/** Set the dataset reference explicitly to a device matrix view with padding. */
void update_dataset(raft::resources const& res,
raft::device_matrix_view<const T, int64_t, raft::layout_stride> dataset)
{
dataset_ = make_aligned_dataset(res, dataset, 16);
}
/**
* Replace the dataset with a new dataset.
*
* We create a copy of the dataset on the device. The index manages the lifetime of this copy. It
* is expected that the same set of vectors are used for update_dataset and index build.
*/
void update_dataset(raft::resources const& res,
raft::host_matrix_view<const T, int64_t, raft::row_major> dataset)
{
dataset_ = make_aligned_dataset(res, dataset, 16);
}
/**
* Replace the dataset with a new dataset. It is expected that the same set of vectors are used
* for update_dataset and index build.
*/
template <typename DatasetT>
auto update_dataset(raft::resources const& res, DatasetT&& dataset)
-> std::enable_if_t<std::is_base_of_v<cuvs::neighbors::dataset<int64_t>, DatasetT>>
{
dataset_ = std::make_unique<DatasetT>(std::move(dataset));
}
template <typename DatasetT>
auto update_dataset(raft::resources const& res, std::unique_ptr<DatasetT>&& dataset)
-> std::enable_if_t<std::is_base_of_v<neighbors::dataset<int64_t>, DatasetT>>
{
dataset_ = std::move(dataset);
}
/**
* Replace the graph with a new graph.
*
* Since the new graph is a device array, we store a reference to that, and it is
* the caller's responsibility to ensure that knn_graph stays alive as long as the index.
*/
void update_graph(raft::resources const& res,
raft::device_matrix_view<const IdxT, int64_t, raft::row_major> knn_graph)
{
graph_view_ = knn_graph;
}
/**
* Replace the graph with a new graph.
*
* We create a copy of the graph on the device. The index manages the lifetime of this copy.
*/
void update_graph(raft::resources const& res,
raft::host_matrix_view<const IdxT, int64_t, raft::row_major> knn_graph)
{
RAFT_LOG_DEBUG("Copying CAGRA knn graph from host to device");
if ((graph_.extent(0) != knn_graph.extent(0)) || (graph_.extent(1) != knn_graph.extent(1))) {
// clear existing memory before allocating to prevent OOM errors on large graphs
if (graph_.size()) { graph_ = raft::make_device_matrix<IdxT, int64_t>(res, 0, 0); }
graph_ =
raft::make_device_matrix<IdxT, int64_t>(res, knn_graph.extent(0), knn_graph.extent(1));
}
raft::copy(graph_.data_handle(),
knn_graph.data_handle(),
knn_graph.size(),
raft::resource::get_cuda_stream(res));
graph_view_ = graph_.view();
}
private:
cuvs::distance::DistanceType metric_;
raft::device_matrix<IdxT, int64_t, raft::row_major> graph_;
raft::device_matrix_view<const IdxT, int64_t, raft::row_major> graph_view_;
std::unique_ptr<neighbors::dataset<int64_t>> dataset_;
};
/**
* @}
*/
/**
* @defgroup cagra_cpp_index_build CAGRA index build functions
* @{
*/
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
* - InnerProduct (currently only supported with IVF-PQ as the build algorithm)
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (device) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::device_matrix_view<const float, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<float, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
* - InnerProduct (currently only supported with IVF-PQ as the build algorithm)
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (host) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::host_matrix_view<const float, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<float, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
* - InnerProduct (currently only supported with IVF-PQ as the build algorithm)
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (device) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::device_matrix_view<const half, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<half, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (host) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::host_matrix_view<const half, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<half, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (device) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::device_matrix_view<const int8_t, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<int8_t, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
* - InnerProduct (currently only supported with IVF-PQ as the build algorithm)
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (host) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::host_matrix_view<const int8_t, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<int8_t, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
* - InnerProduct (currently only supported with IVF-PQ as the build algorithm)
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (device) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<uint8_t, uint32_t>;
/**
* @brief Build the index from the dataset for efficient search.
*
* The build consist of two steps: build an intermediate knn-graph, and optimize it to
* create the final graph. The index_params struct controls the node degree of these
* graphs.
*
* The following distance metrics are supported:
* - L2
* - InnerProduct (currently only supported with IVF-PQ as the build algorithm)
*
* Usage example:
* @code{.cpp}
* using namespace cuvs::neighbors;
* // use default index parameters
* cagra::index_params index_params;
* // create and fill the index from a [N, D] dataset
* auto index = cagra::build(res, index_params, dataset);
* // use default search parameters
* cagra::search_params search_params;
* // search K nearest neighbours
* auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k);
* auto distances = raft::make_device_matrix<float>(res, n_queries, k);
* cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
* @endcode
*
* @param[in] res
* @param[in] params parameters for building the index
* @param[in] dataset a matrix view (host) to a row-major matrix [n_rows, dim]
*
* @return the constructed cagra index
*/
auto build(raft::resources const& res,
const cuvs::neighbors::cagra::index_params& params,
raft::host_matrix_view<const uint8_t, int64_t, raft::row_major> dataset)
-> cuvs::neighbors::cagra::index<uint8_t, uint32_t>;
/**
* @}
*/
/**
* @defgroup cagra_cpp_index_extend CAGRA extend functions
* @{
*/
/** @brief Add new vectors to a CAGRA index
*
* Usage example:
* @code{.cpp}
* using namespace raft::neighbors;
* auto additional_dataset = raft::make_device_matrix<float, int64_t>(handle,add_size,dim);
* // set_additional_dataset(additional_dataset.view());
*
* cagra::extend_params params;
* cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
* @endcode
*
* @param[in] handle raft resources
* @param[in] params extend params
* @param[in] additional_dataset additional dataset on device memory
* @param[in,out] idx CAGRA index
* @param[out] new_dataset_buffer_view memory buffer view for the dataset including the additional
* part. The data will be copied from the current index in this function. The num rows must be the
* sum of the original and additional datasets, cols must be the dimension of the dataset, and the
* stride must be the same as the original index dataset. This view will be stored in the output
* index. It is the caller's responsibility to ensure that dataset stays alive as long as the index.
* This option is useful when users want to manage the memory space for the dataset themselves.
* @param[out] new_graph_buffer_view memory buffer view for the graph including the additional part.
* The data will be copied from the current index in this function. The num rows must be the sum of
* the original and additional datasets and cols must be the graph degree. This view will be stored
* in the output index. It is the caller's responsibility to ensure that dataset stays alive as long
* as the index. This option is useful when users want to manage the memory space for the graph
* themselves.
*/
void extend(
raft::resources const& handle,
const cagra::extend_params& params,
raft::device_matrix_view<const float, int64_t, raft::row_major> additional_dataset,
cuvs::neighbors::cagra::index<float, uint32_t>& idx,
std::optional<raft::device_matrix_view<float, int64_t, raft::layout_stride>>
new_dataset_buffer_view = std::nullopt,
std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt);
/** @brief Add new vectors to a CAGRA index
*
* Usage example:
* @code{.cpp}
* using namespace raft::neighbors;
* auto additional_dataset = raft::make_host_matrix<float, int64_t>(handle,add_size,dim);
* // set_additional_dataset(additional_dataset.view());
*
* cagra::extend_params params;
* cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
* @endcode
*
* @param[in] handle raft resources
* @param[in] params extend params
* @param[in] additional_dataset additional dataset on host memory
* @param[in,out] idx CAGRA index
* @param[out] new_dataset_buffer_view memory buffer view for the dataset including the additional
* part. The data will be copied from the current index in this function. The num rows must be the
* sum of the original and additional datasets, cols must be the dimension of the dataset, and the
* stride must be the same as the original index dataset. This view will be stored in the output
* index. It is the caller's responsibility to ensure that dataset stays alive as long as the index.
* This option is useful when users want to manage the memory space for the dataset themselves.
* @param[out] new_graph_buffer_view memory buffer view for the graph including the additional part.
* The data will be copied from the current index in this function. The num rows must be the sum of
* the original and additional datasets and cols must be the graph degree. This view will be stored
* in the output index. It is the caller's responsibility to ensure that dataset stays alive as long
* as the index. This option is useful when users want to manage the memory space for the graph
* themselves.
*/
void extend(
raft::resources const& handle,
const cagra::extend_params& params,
raft::host_matrix_view<const float, int64_t, raft::row_major> additional_dataset,
cuvs::neighbors::cagra::index<float, uint32_t>& idx,
std::optional<raft::device_matrix_view<float, int64_t, raft::layout_stride>>
new_dataset_buffer_view = std::nullopt,
std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt);
/** @brief Add new vectors to a CAGRA index
*
* Usage example:
* @code{.cpp}
* using namespace raft::neighbors;
* auto additional_dataset = raft::make_device_matrix<int8_t, int64_t>(handle,add_size,dim);
* // set_additional_dataset(additional_dataset.view());
*
* cagra::extend_params params;
* cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
* @endcode
*
* @param[in] handle raft resources
* @param[in] params extend params
* @param[in] additional_dataset additional dataset on device memory
* @param[in,out] idx CAGRA index
* @param[out] new_dataset_buffer_view memory buffer view for the dataset including the additional
* part. The data will be copied from the current index in this function. The num rows must be the
* sum of the original and additional datasets, cols must be the dimension of the dataset, and the
* stride must be the same as the original index dataset. This view will be stored in the output
* index. It is the caller's responsibility to ensure that dataset stays alive as long as the index.
* This option is useful when users want to manage the memory space for the dataset themselves.
* @param[out] new_graph_buffer_view memory buffer view for the graph including the additional part.
* The data will be copied from the current index in this function. The num rows must be the sum of
* the original and additional datasets and cols must be the graph degree. This view will be stored
* in the output index. It is the caller's responsibility to ensure that dataset stays alive as long
* as the index. This option is useful when users want to manage the memory space for the graph
* themselves.
*/
void extend(
raft::resources const& handle,
const cagra::extend_params& params,
raft::device_matrix_view<const int8_t, int64_t, raft::row_major> additional_dataset,
cuvs::neighbors::cagra::index<int8_t, uint32_t>& idx,
std::optional<raft::device_matrix_view<int8_t, int64_t, raft::layout_stride>>
new_dataset_buffer_view = std::nullopt,
std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt);
/** @brief Add new vectors to a CAGRA index
*
* Usage example:
* @code{.cpp}
* using namespace raft::neighbors;
* auto additional_dataset = raft::make_host_matrix<int8_t, int64_t>(handle,add_size,dim);
* // set_additional_dataset(additional_dataset.view());
*
* cagra::extend_params params;
* cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
* @endcode
*
* @param[in] handle raft resources
* @param[in] params extend params
* @param[in] additional_dataset additional dataset on host memory
* @param[in,out] idx CAGRA index
* @param[out] new_dataset_buffer_view memory buffer view for the dataset including the additional
* part. The data will be copied from the current index in this function. The num rows must be the
* sum of the original and additional datasets, cols must be the dimension of the dataset, and the
* stride must be the same as the original index dataset. This view will be stored in the output
* index. It is the caller's responsibility to ensure that dataset stays alive as long as the index.
* This option is useful when users want to manage the memory space for the dataset themselves.
* @param[out] new_graph_buffer_view memory buffer view for the graph including the additional part.
* The data will be copied from the current index in this function. The num rows must be the sum of
* the original and additional datasets and cols must be the graph degree. This view will be stored
* in the output index. It is the caller's responsibility to ensure that dataset stays alive as long
* as the index. This option is useful when users want to manage the memory space for the graph
* themselves.
*/
void extend(
raft::resources const& handle,
const cagra::extend_params& params,
raft::host_matrix_view<const int8_t, int64_t, raft::row_major> additional_dataset,
cuvs::neighbors::cagra::index<int8_t, uint32_t>& idx,
std::optional<raft::device_matrix_view<int8_t, int64_t, raft::layout_stride>>
new_dataset_buffer_view = std::nullopt,
std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt);
/** @brief Add new vectors to a CAGRA index
*
* Usage example:
* @code{.cpp}
* using namespace raft::neighbors;
* auto additional_dataset = raft::make_host_matrix<uint8_t, int64_t>(handle,add_size,dim);
* // set_additional_dataset(additional_dataset.view());
*
* cagra::extend_params params;
* cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
* @endcode
*
* @param[in] handle raft resources
* @param[in] params extend params
* @param[in] additional_dataset additional dataset on host memory
* @param[in,out] idx CAGRA index
* @param[out] new_dataset_buffer_view memory buffer view for the dataset including the additional
* part. The data will be copied from the current index in this function. The num rows must be the
* sum of the original and additional datasets, cols must be the dimension of the dataset, and the
* stride must be the same as the original index dataset. This view will be stored in the output
* index. It is the caller's responsibility to ensure that dataset stays alive as long as the index.
* This option is useful when users want to manage the memory space for the dataset themselves.
* @param[out] new_graph_buffer_view memory buffer view for the graph including the additional part.
* The data will be copied from the current index in this function. The num rows must be the sum of
* the original and additional datasets and cols must be the graph degree. This view will be stored
* in the output index. It is the caller's responsibility to ensure that dataset stays alive as long
* as the index. This option is useful when users want to manage the memory space for the graph
* themselves.
*/
void extend(
raft::resources const& handle,
const cagra::extend_params& params,
raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> additional_dataset,