-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathann_cagra.cuh
1033 lines (930 loc) · 40.1 KB
/
ann_cagra.cuh
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 "../test_utils.cuh"
#include "ann_utils.cuh"
#include <raft/core/resource/cuda_stream.hpp>
#include "naive_knn.cuh"
#include <cuvs/distance/distance.hpp>
#include <cuvs/neighbors/cagra.hpp>
#include <raft/core/device_mdspan.hpp>
#include <raft/core/device_resources.hpp>
#include <raft/core/host_mdarray.hpp>
#include <raft/core/host_mdspan.hpp>
#include <raft/core/logger.hpp>
#include <raft/linalg/add.cuh>
#include <raft/linalg/matrix_vector_op.cuh>
#include <raft/linalg/normalize.cuh>
#include <raft/linalg/reduce.cuh>
#include <raft/random/rng.cuh>
#include <raft/util/itertools.hpp>
#include <rmm/device_buffer.hpp>
#include <gtest/gtest.h>
#include <thrust/sequence.h>
#include <cstddef>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
namespace cuvs::neighbors::cagra {
namespace {
struct test_cagra_sample_filter {
static constexpr unsigned offset = 300;
inline _RAFT_HOST_DEVICE auto operator()(
// query index
const uint32_t query_ix,
// the index of the current sample inside the current inverted list
const uint32_t sample_ix) const
{
return sample_ix >= offset;
}
};
/** Xorshift rondem number generator.
*
* See https://en.wikipedia.org/wiki/Xorshift#xorshift for reference.
*/
_RAFT_HOST_DEVICE inline uint64_t xorshift64(uint64_t u)
{
u ^= u >> 12;
u ^= u << 25;
u ^= u >> 27;
return u * 0x2545F4914F6CDD1DULL;
}
// For sort_knn_graph test
template <typename IdxT>
void RandomSuffle(raft::host_matrix_view<IdxT, int64_t> index)
{
for (IdxT i = 0; i < index.extent(0); i++) {
uint64_t rand = i;
IdxT* const row_ptr = index.data_handle() + i * index.extent(1);
for (unsigned j = 0; j < index.extent(1); j++) {
// Swap two indices at random
rand = xorshift64(rand);
const auto i0 = rand % index.extent(1);
rand = xorshift64(rand);
const auto i1 = rand % index.extent(1);
const auto tmp = row_ptr[i0];
row_ptr[i0] = row_ptr[i1];
row_ptr[i1] = tmp;
}
}
}
template <typename DistanceT, typename DatatT, typename IdxT>
testing::AssertionResult CheckOrder(raft::host_matrix_view<IdxT, int64_t> index_test,
raft::host_matrix_view<DatatT, int64_t> dataset)
{
for (IdxT i = 0; i < index_test.extent(0); i++) {
const DatatT* const base_vec = dataset.data_handle() + i * dataset.extent(1);
const IdxT* const index_row = index_test.data_handle() + i * index_test.extent(1);
DistanceT prev_distance = 0;
for (unsigned j = 0; j < index_test.extent(1) - 1; j++) {
const DatatT* const target_vec = dataset.data_handle() + index_row[j] * dataset.extent(1);
DistanceT distance = 0;
for (unsigned l = 0; l < dataset.extent(1); l++) {
const auto diff =
static_cast<DistanceT>(target_vec[l]) - static_cast<DistanceT>(base_vec[l]);
distance += diff * diff;
}
if (prev_distance > distance) {
return testing::AssertionFailure()
<< "Wrong index order (row = " << i << ", neighbor_id = " << j
<< "). (distance[neighbor_id-1] = " << prev_distance
<< "should be larger than distance[neighbor_id] = " << distance << ")";
}
prev_distance = distance;
}
}
return testing::AssertionSuccess();
}
template <typename T>
struct fpi_mapper {};
template <>
struct fpi_mapper<double> {
using type = int64_t;
static constexpr int kBitshiftBase = 53;
};
template <>
struct fpi_mapper<float> {
using type = int32_t;
static constexpr int kBitshiftBase = 24;
};
template <>
struct fpi_mapper<half> {
using type = int16_t;
static constexpr int kBitshiftBase = 11;
};
// Generate dataset to ensure no rounding error occurs in the norm computation of any two vectors.
// When testing the CAGRA index sorting function, rounding errors can affect the norm and alter the
// order of the index. To ensure the accuracy of the test, we utilize the dataset. The generation
// method is based on the error-free transformation (EFT) method.
template <typename T>
RAFT_KERNEL GenerateRoundingErrorFreeDataset_kernel(T* const ptr,
const uint32_t size,
const typename fpi_mapper<T>::type resolution)
{
const auto tid = threadIdx.x + blockIdx.x * blockDim.x;
if (tid >= size) { return; }
const float u32 = *reinterpret_cast<const typename fpi_mapper<T>::type*>(ptr + tid);
ptr[tid] = u32 / resolution;
}
template <typename T>
void GenerateRoundingErrorFreeDataset(
const raft::resources& handle,
T* const ptr,
const uint32_t n_row,
const uint32_t dim,
raft::random::RngState& rng,
const bool diff_flag // true if compute the norm between two vectors
)
{
using mapper_type = fpi_mapper<T>;
using int_type = typename mapper_type::type;
auto cuda_stream = raft::resource::get_cuda_stream(handle);
const uint32_t size = n_row * dim;
const uint32_t block_size = 256;
const uint32_t grid_size = (size + block_size - 1) / block_size;
const auto bitshift = (mapper_type::kBitshiftBase - std::log2(dim) - (diff_flag ? 1 : 0)) / 2;
// Skip the test when `dim` is too big for type `T` to allow rounding error-free test.
if (bitshift <= 1) { GTEST_SKIP(); }
const int_type resolution = int_type{1} << static_cast<unsigned>(std::floor(bitshift));
raft::random::uniformInt<int_type>(
handle, rng, reinterpret_cast<int_type*>(ptr), size, -resolution, resolution - 1);
GenerateRoundingErrorFreeDataset_kernel<T>
<<<grid_size, block_size, 0, cuda_stream>>>(ptr, size, resolution);
}
template <class DataT>
void InitDataset(const raft::resources& handle,
DataT* const datatset_ptr,
std::uint32_t size,
std::uint32_t dim,
distance::DistanceType metric,
raft::random::RngState& r)
{
if constexpr (std::is_same_v<DataT, float> || std::is_same_v<DataT, half>) {
GenerateRoundingErrorFreeDataset(handle, datatset_ptr, size, dim, r, true);
if (metric == InnerProduct) {
auto dataset_view = raft::make_device_matrix_view(datatset_ptr, size, dim);
raft::linalg::row_normalize(
handle, raft::make_const_mdspan(dataset_view), dataset_view, raft::linalg::L2Norm);
}
} else if constexpr (std::is_same_v<DataT, std::uint8_t> || std::is_same_v<DataT, std::int8_t>) {
if constexpr (std::is_same_v<DataT, std::int8_t>) {
raft::random::uniformInt(handle, r, datatset_ptr, size * dim, DataT(-10), DataT(10));
} else {
raft::random::uniformInt(handle, r, datatset_ptr, size * dim, DataT(1), DataT(20));
}
if (metric == InnerProduct) {
// TODO (enp1s0): Change this once row_normalize supports (u)int8 matrices.
// https://github.com/rapidsai/raft/issues/2291
using ComputeT = float;
auto dataset_view = raft::make_device_matrix_view(datatset_ptr, size, dim);
auto dev_row_norm = raft::make_device_vector<ComputeT>(handle, size);
const auto normalized_norm =
(std::is_same_v<DataT, std::uint8_t> ? 40 : 20) * std::sqrt(static_cast<ComputeT>(dim));
raft::linalg::reduce(dev_row_norm.data_handle(),
datatset_ptr,
dim,
size,
0.f,
true,
true,
raft::resource::get_cuda_stream(handle),
false,
raft::sq_op(),
raft::add_op(),
raft::sqrt_op());
raft::linalg::matrix_vector_op(
handle,
raft::make_const_mdspan(dataset_view),
raft::make_const_mdspan(dev_row_norm.view()),
dataset_view,
raft::linalg::Apply::ALONG_COLUMNS,
[normalized_norm] __device__(DataT elm, ComputeT norm) {
const ComputeT v = elm / norm * normalized_norm;
const ComputeT max_v_range = std::numeric_limits<DataT>::max();
const ComputeT min_v_range = std::numeric_limits<DataT>::min();
return static_cast<DataT>(std::min(max_v_range, std::max(min_v_range, v)));
});
}
}
}
enum class graph_build_algo {
/* Use IVF-PQ to build all-neighbors knn graph */
IVF_PQ,
/* Experimental, use NN-Descent to build all-neighbors knn graph */
NN_DESCENT,
/* Choose default automatically */
AUTO
};
} // namespace
struct AnnCagraInputs {
int n_queries;
int n_rows;
int dim;
int k;
graph_build_algo build_algo;
search_algo algo;
int max_queries;
int team_size;
int itopk_size;
int search_width;
cuvs::distance::DistanceType metric;
bool host_dataset;
bool include_serialized_dataset;
// std::optional<double>
double min_recall; // = std::nullopt;
std::optional<float> ivf_pq_search_refine_ratio = std::nullopt;
std::optional<vpq_params> compression = std::nullopt;
std::optional<bool> non_owning_memory_buffer_flag = std::nullopt;
};
inline ::std::ostream& operator<<(::std::ostream& os, const AnnCagraInputs& p)
{
const auto metric_str = [](const cuvs::distance::DistanceType dist) -> std::string {
switch (dist) {
case InnerProduct: return "InnerProduct";
case L2Expanded: return "L2";
default: break;
}
return "Unknown";
};
std::vector<std::string> algo = {"single-cta", "multi_cta", "multi_kernel", "auto"};
std::vector<std::string> build_algo = {"IVF_PQ", "NN_DESCENT", "AUTO"};
os << "{n_queries=" << p.n_queries << ", dataset shape=" << p.n_rows << "x" << p.dim
<< ", k=" << p.k << ", " << algo.at((int)p.algo) << ", max_queries=" << p.max_queries
<< ", itopk_size=" << p.itopk_size << ", search_width=" << p.search_width
<< ", metric=" << metric_str(p.metric) << ", " << (p.host_dataset ? "host" : "device")
<< ", build_algo=" << build_algo.at((int)p.build_algo);
if ((int)p.build_algo == 0 && p.ivf_pq_search_refine_ratio) {
os << "(refine_rate=" << *p.ivf_pq_search_refine_ratio << ')';
}
if (p.compression.has_value()) {
auto vpq = p.compression.value();
os << ", pq_bits=" << vpq.pq_bits << ", pq_dim=" << vpq.pq_dim
<< ", vq_n_centers=" << vpq.vq_n_centers;
}
os << '}' << std::endl;
return os;
}
template <typename DistanceT, typename DataT, typename IdxT>
class AnnCagraTest : public ::testing::TestWithParam<AnnCagraInputs> {
public:
AnnCagraTest()
: stream_(raft::resource::get_cuda_stream(handle_)),
ps(::testing::TestWithParam<AnnCagraInputs>::GetParam()),
database(0, stream_),
search_queries(0, stream_)
{
}
protected:
void testCagra()
{
size_t queries_size = ps.n_queries * ps.k;
std::vector<IdxT> indices_Cagra(queries_size);
std::vector<IdxT> indices_naive(queries_size);
std::vector<DistanceT> distances_Cagra(queries_size);
std::vector<DistanceT> distances_naive(queries_size);
{
rmm::device_uvector<DistanceT> distances_naive_dev(queries_size, stream_);
rmm::device_uvector<IdxT> indices_naive_dev(queries_size, stream_);
cuvs::neighbors::naive_knn<DistanceT, DataT, IdxT>(handle_,
distances_naive_dev.data(),
indices_naive_dev.data(),
search_queries.data(),
database.data(),
ps.n_queries,
ps.n_rows,
ps.dim,
ps.k,
ps.metric);
raft::update_host(distances_naive.data(), distances_naive_dev.data(), queries_size, stream_);
raft::update_host(indices_naive.data(), indices_naive_dev.data(), queries_size, stream_);
raft::resource::sync_stream(handle_);
}
{
rmm::device_uvector<DistanceT> distances_dev(queries_size, stream_);
rmm::device_uvector<IdxT> indices_dev(queries_size, stream_);
{
cagra::index_params index_params;
index_params.metric = ps.metric; // Note: currently ony the cagra::index_params metric is
// not used for knn_graph building.
switch (ps.build_algo) {
case graph_build_algo::IVF_PQ:
index_params.graph_build_params = graph_build_params::ivf_pq_params(
raft::matrix_extent<int64_t>(ps.n_rows, ps.dim), index_params.metric);
if (ps.ivf_pq_search_refine_ratio) {
std::get<cuvs::neighbors::cagra::graph_build_params::ivf_pq_params>(
index_params.graph_build_params)
.refinement_rate = *ps.ivf_pq_search_refine_ratio;
}
break;
case graph_build_algo::NN_DESCENT: {
index_params.graph_build_params = graph_build_params::nn_descent_params(
index_params.intermediate_graph_degree, index_params.metric);
break;
}
case graph_build_algo::AUTO:
// do nothing
break;
};
index_params.compression = ps.compression;
cagra::search_params search_params;
search_params.algo = ps.algo;
search_params.max_queries = ps.max_queries;
search_params.team_size = ps.team_size;
auto database_view = raft::make_device_matrix_view<const DataT, int64_t>(
(const DataT*)database.data(), ps.n_rows, ps.dim);
{
cagra::index<DataT, IdxT> index(handle_, index_params.metric);
if (ps.host_dataset) {
auto database_host = raft::make_host_matrix<DataT, int64_t>(ps.n_rows, ps.dim);
raft::copy(database_host.data_handle(), database.data(), database.size(), stream_);
auto database_host_view = raft::make_host_matrix_view<const DataT, int64_t>(
(const DataT*)database_host.data_handle(), ps.n_rows, ps.dim);
index = cagra::build(handle_, index_params, database_host_view);
} else {
index = cagra::build(handle_, index_params, database_view);
};
cagra::serialize(handle_, "cagra_index", index, ps.include_serialized_dataset);
}
cagra::index<DataT, IdxT> index(handle_);
cagra::deserialize(handle_, "cagra_index", &index);
if (!ps.include_serialized_dataset) { index.update_dataset(handle_, database_view); }
auto search_queries_view = raft::make_device_matrix_view<const DataT, int64_t>(
search_queries.data(), ps.n_queries, ps.dim);
auto indices_out_view =
raft::make_device_matrix_view<IdxT, int64_t>(indices_dev.data(), ps.n_queries, ps.k);
auto dists_out_view = raft::make_device_matrix_view<DistanceT, int64_t>(
distances_dev.data(), ps.n_queries, ps.k);
cagra::search(
handle_, search_params, index, search_queries_view, indices_out_view, dists_out_view);
raft::update_host(distances_Cagra.data(), distances_dev.data(), queries_size, stream_);
raft::update_host(indices_Cagra.data(), indices_dev.data(), queries_size, stream_);
raft::resource::sync_stream(handle_);
}
// for (int i = 0; i < min(ps.n_queries, 10); i++) {
// // std::cout << "query " << i << std::end;
// print_vector("T", indices_naive.data() + i * ps.k, ps.k, std::cout);
// print_vector("C", indices_Cagra.data() + i * ps.k, ps.k, std::cout);
// print_vector("T", distances_naive.data() + i * ps.k, ps.k, std::cout);
// print_vector("C", distances_Cagra.data() + i * ps.k, ps.k, std::cout);
// }
double min_recall = ps.min_recall;
EXPECT_TRUE(eval_neighbours(indices_naive,
indices_Cagra,
distances_naive,
distances_Cagra,
ps.n_queries,
ps.k,
0.003,
min_recall));
if (!ps.compression.has_value()) {
// Don't evaluate distances for CAGRA-Q for now as the error can be somewhat large
EXPECT_TRUE(eval_distances(handle_,
database.data(),
search_queries.data(),
indices_dev.data(),
distances_dev.data(),
ps.n_rows,
ps.dim,
ps.n_queries,
ps.k,
ps.metric,
1.0e-4));
}
}
}
void SetUp() override
{
database.resize(((size_t)ps.n_rows) * ps.dim, stream_);
search_queries.resize(ps.n_queries * ps.dim, stream_);
raft::random::RngState r(1234ULL);
InitDataset(handle_, database.data(), ps.n_rows, ps.dim, ps.metric, r);
InitDataset(handle_, search_queries.data(), ps.n_queries, ps.dim, ps.metric, r);
raft::resource::sync_stream(handle_);
}
void TearDown() override
{
raft::resource::sync_stream(handle_);
database.resize(0, stream_);
search_queries.resize(0, stream_);
}
private:
raft::resources handle_;
rmm::cuda_stream_view stream_;
AnnCagraInputs ps;
rmm::device_uvector<DataT> database;
rmm::device_uvector<DataT> search_queries;
};
template <typename DistanceT, typename DataT, typename IdxT>
class AnnCagraAddNodesTest : public ::testing::TestWithParam<AnnCagraInputs> {
public:
AnnCagraAddNodesTest()
: stream_(raft::resource::get_cuda_stream(handle_)),
ps(::testing::TestWithParam<AnnCagraInputs>::GetParam()),
database(0, stream_),
search_queries(0, stream_)
{
}
protected:
void testCagra()
{
// TODO (tarang-jain): remove when NN Descent index building support InnerProduct. Reference
// issue: https://github.com/rapidsai/raft/issues/2276
if (ps.metric == InnerProduct && ps.build_algo == graph_build_algo::NN_DESCENT) GTEST_SKIP();
if (ps.compression != std::nullopt) GTEST_SKIP();
size_t queries_size = ps.n_queries * ps.k;
std::vector<IdxT> indices_Cagra(queries_size);
std::vector<IdxT> indices_naive(queries_size);
std::vector<DistanceT> distances_Cagra(queries_size);
std::vector<DistanceT> distances_naive(queries_size);
{
rmm::device_uvector<DistanceT> distances_naive_dev(queries_size, stream_);
rmm::device_uvector<IdxT> indices_naive_dev(queries_size, stream_);
cuvs::neighbors::naive_knn<DistanceT, DataT, IdxT>(handle_,
distances_naive_dev.data(),
indices_naive_dev.data(),
search_queries.data(),
database.data(),
ps.n_queries,
ps.n_rows,
ps.dim,
ps.k,
ps.metric);
raft::update_host(distances_naive.data(), distances_naive_dev.data(), queries_size, stream_);
raft::update_host(indices_naive.data(), indices_naive_dev.data(), queries_size, stream_);
raft::resource::sync_stream(handle_);
}
{
rmm::device_uvector<DistanceT> distances_dev(queries_size, stream_);
rmm::device_uvector<IdxT> indices_dev(queries_size, stream_);
{
cagra::index_params index_params;
index_params.metric = ps.metric; // Note: currently ony the cagra::index_params metric is
// not used for knn_graph building.
switch (ps.build_algo) {
case graph_build_algo::IVF_PQ:
index_params.graph_build_params =
graph_build_params::ivf_pq_params(raft::matrix_extent<int64_t>(ps.n_rows, ps.dim));
if (ps.ivf_pq_search_refine_ratio) {
std::get<cuvs::neighbors::cagra::graph_build_params::ivf_pq_params>(
index_params.graph_build_params)
.refinement_rate = *ps.ivf_pq_search_refine_ratio;
}
break;
case graph_build_algo::NN_DESCENT: {
index_params.graph_build_params =
graph_build_params::nn_descent_params(index_params.intermediate_graph_degree);
break;
}
case graph_build_algo::AUTO:
// do nothing
break;
};
cagra::search_params search_params;
search_params.algo = ps.algo;
search_params.max_queries = ps.max_queries;
search_params.team_size = ps.team_size;
search_params.itopk_size = ps.itopk_size;
const double initial_dataset_ratio = 0.90;
const std::size_t initial_database_size = ps.n_rows * initial_dataset_ratio;
auto initial_database_view = raft::make_device_matrix_view<const DataT, int64_t>(
(const DataT*)database.data(), initial_database_size, ps.dim);
cagra::index<DataT, IdxT> index(handle_);
if (ps.host_dataset) {
auto database_host = raft::make_host_matrix<DataT, int64_t>(ps.n_rows, ps.dim);
raft::copy(
database_host.data_handle(), database.data(), initial_database_view.size(), stream_);
auto database_host_view = raft::make_host_matrix_view<const DataT, int64_t>(
(const DataT*)database_host.data_handle(), initial_database_size, ps.dim);
index = cagra::build(handle_, index_params, database_host_view);
} else {
index = cagra::build(handle_, index_params, initial_database_view);
};
auto additional_dataset =
raft::make_host_matrix<DataT, int64_t>(ps.n_rows - initial_database_size, index.dim());
raft::copy(additional_dataset.data_handle(),
static_cast<const DataT*>(database.data()) + initial_database_view.size(),
additional_dataset.size(),
stream_);
auto new_dataset_buffer = raft::make_device_matrix<DataT, int64_t>(handle_, 0, 0);
auto new_graph_buffer = raft::make_device_matrix<IdxT, int64_t>(handle_, 0, 0);
std::optional<raft::device_matrix_view<DataT, int64_t, raft::layout_stride>>
new_dataset_buffer_view = std::nullopt;
std::optional<raft::device_matrix_view<IdxT, int64_t>> new_graph_buffer_view = std::nullopt;
if (ps.non_owning_memory_buffer_flag.has_value() &&
ps.non_owning_memory_buffer_flag.value()) {
const auto stride =
dynamic_cast<const cuvs::neighbors::strided_dataset<DataT, int64_t>*>(&index.data())
->stride();
new_dataset_buffer = raft::make_device_matrix<DataT, int64_t>(handle_, ps.n_rows, stride);
new_graph_buffer =
raft::make_device_matrix<IdxT, int64_t>(handle_, ps.n_rows, index.graph_degree());
new_dataset_buffer_view = raft::make_device_strided_matrix_view<DataT, int64_t>(
new_dataset_buffer.data_handle(), ps.n_rows, ps.dim, stride);
new_graph_buffer_view = new_graph_buffer.view();
}
cagra::extend_params extend_params;
cagra::extend(handle_,
extend_params,
raft::make_const_mdspan(additional_dataset.view()),
index,
new_dataset_buffer_view,
new_graph_buffer_view);
auto search_queries_view = raft::make_device_matrix_view<const DataT, int64_t>(
search_queries.data(), ps.n_queries, ps.dim);
auto indices_out_view =
raft::make_device_matrix_view<IdxT, int64_t>(indices_dev.data(), ps.n_queries, ps.k);
auto dists_out_view = raft::make_device_matrix_view<DistanceT, int64_t>(
distances_dev.data(), ps.n_queries, ps.k);
cagra::search(
handle_, search_params, index, search_queries_view, indices_out_view, dists_out_view);
raft::update_host(distances_Cagra.data(), distances_dev.data(), queries_size, stream_);
raft::update_host(indices_Cagra.data(), indices_dev.data(), queries_size, stream_);
raft::resource::sync_stream(handle_);
}
double min_recall = ps.min_recall;
EXPECT_TRUE(eval_neighbours(indices_naive,
indices_Cagra,
distances_naive,
distances_Cagra,
ps.n_queries,
ps.k,
0.006,
min_recall));
EXPECT_TRUE(eval_distances(handle_,
database.data(),
search_queries.data(),
indices_dev.data(),
distances_dev.data(),
ps.n_rows,
ps.dim,
ps.n_queries,
ps.k,
ps.metric,
1.0e-4));
}
}
void SetUp() override
{
database.resize(((size_t)ps.n_rows) * ps.dim, stream_);
search_queries.resize(ps.n_queries * ps.dim, stream_);
raft::random::RngState r(1234ULL);
InitDataset(handle_, database.data(), ps.n_rows, ps.dim, ps.metric, r);
InitDataset(handle_, search_queries.data(), ps.n_queries, ps.dim, ps.metric, r);
raft::resource::sync_stream(handle_);
}
void TearDown() override
{
raft::resource::sync_stream(handle_);
database.resize(0, stream_);
search_queries.resize(0, stream_);
}
private:
raft::resources handle_;
rmm::cuda_stream_view stream_;
AnnCagraInputs ps;
rmm::device_uvector<DataT> database;
rmm::device_uvector<DataT> search_queries;
};
template <typename DistanceT, typename DataT, typename IdxT>
class AnnCagraFilterTest : public ::testing::TestWithParam<AnnCagraInputs> {
public:
AnnCagraFilterTest()
: stream_(raft::resource::get_cuda_stream(handle_)),
ps(::testing::TestWithParam<AnnCagraInputs>::GetParam()),
database(0, stream_),
search_queries(0, stream_)
{
}
protected:
void testCagra()
{
if (ps.metric == cuvs::distance::DistanceType::InnerProduct &&
ps.build_algo == graph_build_algo::NN_DESCENT)
GTEST_SKIP();
size_t queries_size = ps.n_queries * ps.k;
std::vector<IdxT> indices_Cagra(queries_size);
std::vector<IdxT> indices_naive(queries_size);
std::vector<DistanceT> distances_Cagra(queries_size);
std::vector<DistanceT> distances_naive(queries_size);
{
rmm::device_uvector<DistanceT> distances_naive_dev(queries_size, stream_);
rmm::device_uvector<IdxT> indices_naive_dev(queries_size, stream_);
auto* database_filtered_ptr = database.data() + test_cagra_sample_filter::offset * ps.dim;
cuvs::neighbors::naive_knn<DistanceT, DataT, IdxT>(
handle_,
distances_naive_dev.data(),
indices_naive_dev.data(),
search_queries.data(),
database_filtered_ptr,
ps.n_queries,
ps.n_rows - test_cagra_sample_filter::offset,
ps.dim,
ps.k,
ps.metric);
raft::linalg::addScalar(indices_naive_dev.data(),
indices_naive_dev.data(),
IdxT(test_cagra_sample_filter::offset),
queries_size,
stream_);
raft::update_host(distances_naive.data(), distances_naive_dev.data(), queries_size, stream_);
raft::update_host(indices_naive.data(), indices_naive_dev.data(), queries_size, stream_);
raft::resource::sync_stream(handle_);
}
{
rmm::device_uvector<DistanceT> distances_dev(queries_size, stream_);
rmm::device_uvector<IdxT> indices_dev(queries_size, stream_);
{
cagra::index_params index_params;
index_params.metric = ps.metric; // Note: currently ony the cagra::index_params metric is
// not used for knn_graph building.
switch (ps.build_algo) {
case graph_build_algo::IVF_PQ:
index_params.graph_build_params =
graph_build_params::ivf_pq_params(raft::matrix_extent<int64_t>(ps.n_rows, ps.dim));
if (ps.ivf_pq_search_refine_ratio) {
std::get<cuvs::neighbors::cagra::graph_build_params::ivf_pq_params>(
index_params.graph_build_params)
.refinement_rate = *ps.ivf_pq_search_refine_ratio;
}
break;
case graph_build_algo::NN_DESCENT: {
index_params.graph_build_params =
graph_build_params::nn_descent_params(index_params.intermediate_graph_degree);
break;
}
case graph_build_algo::AUTO:
// do nothing
break;
};
index_params.compression = ps.compression;
cagra::search_params search_params;
search_params.algo = ps.algo;
search_params.max_queries = ps.max_queries;
search_params.team_size = ps.team_size;
// TODO: setting search_params.itopk_size here breaks the filter tests, but is required for
// k>1024 skip these tests until fixed
if (ps.k >= 1024) { GTEST_SKIP(); }
// search_params.itopk_size = ps.itopk_size;
auto database_view = raft::make_device_matrix_view<const DataT, int64_t>(
(const DataT*)database.data(), ps.n_rows, ps.dim);
cagra::index<DataT, IdxT> index(handle_);
if (ps.host_dataset) {
auto database_host = raft::make_host_matrix<DataT, int64_t>(ps.n_rows, ps.dim);
raft::copy(database_host.data_handle(), database.data(), database.size(), stream_);
auto database_host_view = raft::make_host_matrix_view<const DataT, int64_t>(
(const DataT*)database_host.data_handle(), ps.n_rows, ps.dim);
index = cagra::build(handle_, index_params, database_host_view);
} else {
index = cagra::build(handle_, index_params, database_view);
}
if (!ps.include_serialized_dataset) { index.update_dataset(handle_, database_view); }
auto search_queries_view = raft::make_device_matrix_view<const DataT, int64_t>(
search_queries.data(), ps.n_queries, ps.dim);
auto indices_out_view =
raft::make_device_matrix_view<IdxT, int64_t>(indices_dev.data(), ps.n_queries, ps.k);
auto dists_out_view = raft::make_device_matrix_view<DistanceT, int64_t>(
distances_dev.data(), ps.n_queries, ps.k);
auto removed_indices =
raft::make_device_vector<int64_t, int64_t>(handle_, test_cagra_sample_filter::offset);
thrust::sequence(
raft::resource::get_thrust_policy(handle_),
thrust::device_pointer_cast(removed_indices.data_handle()),
thrust::device_pointer_cast(removed_indices.data_handle() + removed_indices.extent(0)));
raft::resource::sync_stream(handle_);
cuvs::core::bitset<std::uint32_t, int64_t> removed_indices_bitset(
handle_, removed_indices.view(), ps.n_rows);
auto bitset_filter_obj =
cuvs::neighbors::filtering::bitset_filter(removed_indices_bitset.view());
cagra::search(handle_,
search_params,
index,
search_queries_view,
indices_out_view,
dists_out_view,
bitset_filter_obj);
raft::update_host(distances_Cagra.data(), distances_dev.data(), queries_size, stream_);
raft::update_host(indices_Cagra.data(), indices_dev.data(), queries_size, stream_);
raft::resource::sync_stream(handle_);
}
// Test search results for nodes marked as filtered
bool unacceptable_node = false;
for (int q = 0; q < ps.n_queries; q++) {
for (int i = 0; i < ps.k; i++) {
const auto n = indices_Cagra[q * ps.k + i];
unacceptable_node = unacceptable_node | !test_cagra_sample_filter()(q, n);
}
}
EXPECT_FALSE(unacceptable_node);
double min_recall = ps.min_recall;
// TODO(mfoerster): re-enable uniquenes test
EXPECT_TRUE(eval_neighbours(indices_naive,
indices_Cagra,
distances_naive,
distances_Cagra,
ps.n_queries,
ps.k,
0.003,
min_recall,
false));
if (!ps.compression.has_value()) {
// Don't evaluate distances for CAGRA-Q for now as the error can be somewhat large
EXPECT_TRUE(eval_distances(handle_,
database.data(),
search_queries.data(),
indices_dev.data(),
distances_dev.data(),
ps.n_rows,
ps.dim,
ps.n_queries,
ps.k,
ps.metric,
1.0e-4));
}
}
}
void SetUp() override
{
database.resize(((size_t)ps.n_rows) * ps.dim, stream_);
search_queries.resize(ps.n_queries * ps.dim, stream_);
raft::random::RngState r(1234ULL);
InitDataset(handle_, database.data(), ps.n_rows, ps.dim, ps.metric, r);
InitDataset(handle_, search_queries.data(), ps.n_queries, ps.dim, ps.metric, r);
raft::resource::sync_stream(handle_);
}
void TearDown() override
{
raft::resource::sync_stream(handle_);
database.resize(0, stream_);
search_queries.resize(0, stream_);
}
private:
raft::resources handle_;
rmm::cuda_stream_view stream_;
AnnCagraInputs ps;
rmm::device_uvector<DataT> database;
rmm::device_uvector<DataT> search_queries;
};
inline std::vector<AnnCagraInputs> generate_inputs()
{
// TODO(tfeher): test MULTI_CTA kernel with search_width > 1 to allow multiple CTA per queries
std::vector<AnnCagraInputs> inputs = raft::util::itertools::product<AnnCagraInputs>(
{100},
{1000},
{1, 8, 17},
{1, 16}, // k
{graph_build_algo::IVF_PQ, graph_build_algo::NN_DESCENT},
{search_algo::SINGLE_CTA, search_algo::MULTI_CTA, search_algo::MULTI_KERNEL},
{0, 1, 10, 100}, // query size
{0},
{256},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false},
{true},
{0.995});
auto inputs2 = raft::util::itertools::product<AnnCagraInputs>(
{100},
{1000},
{1, 3, 5, 7, 8, 17, 64, 128, 137, 192, 256, 512, 619, 1024}, // dim
{16}, // k
{graph_build_algo::IVF_PQ, graph_build_algo::NN_DESCENT},
{search_algo::AUTO},
{10},
{0},
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false},
{true},
{0.995});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());
inputs2 = raft::util::itertools::product<AnnCagraInputs>(
{100},
{1000},
{64},
{16},
{graph_build_algo::IVF_PQ, graph_build_algo::NN_DESCENT},
{search_algo::AUTO},
{10},
{0, 8, 16, 32}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false},
{false},
{0.995});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());
inputs2 = raft::util::itertools::product<AnnCagraInputs>(
{100},
{1000},
{64},
{16},
{graph_build_algo::IVF_PQ, graph_build_algo::NN_DESCENT},
{search_algo::AUTO},
{10},
{0}, // team_size
{32, 64, 128, 256, 512, 768},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false},
{true},
{0.995});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());
inputs2 =
raft::util::itertools::product<AnnCagraInputs>({100},
{10000, 20000},
{32},
{10},
{graph_build_algo::AUTO},
{search_algo::AUTO},
{10},
{0}, // team_size
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false, true},
{false},
{0.985});
inputs.insert(inputs.end(), inputs2.begin(), inputs2.end());
// a few PQ configurations
inputs2 = raft::util::itertools::product<AnnCagraInputs>(
{100},
{10000},
{64, 128, 192, 256, 512, 1024}, // dim
{16}, // k
{graph_build_algo::IVF_PQ},
{search_algo::AUTO},
{10},
{0},
{64},
{1},
{cuvs::distance::DistanceType::L2Expanded},
{false},
{true},
{0.6}); // don't demand high recall without refinement
for (uint32_t pq_len : {2}) { // for now, only pq_len = 2 is supported, more options coming soon
for (uint32_t vq_n_centers : {100, 1000}) {
for (auto input : inputs2) {
vpq_params ps{};
ps.pq_dim = input.dim / pq_len;
ps.vq_n_centers = vq_n_centers;
input.compression.emplace(ps);
inputs.push_back(input);
}
}
}
// refinement options
inputs2 =
raft::util::itertools::product<AnnCagraInputs>({100},
{5000},
{32, 64},
{16},
{graph_build_algo::IVF_PQ},
{search_algo::AUTO},
{10},
{0}, // team_size
{64},
{1},