-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_svs.cpp
More file actions
2780 lines (2335 loc) · 98.8 KB
/
test_svs.cpp
File metadata and controls
2780 lines (2335 loc) · 98.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#include "gtest/gtest.h"
#include "VecSim/vec_sim.h"
#include "unit_test_utils.h"
#include <array>
#include <cmath>
#include <random>
#include <vector>
#if HAVE_SVS
#include <sstream>
#include "spdlog/sinks/ostream_sink.h"
#include "VecSim/algorithms/svs/svs.h"
// There are possible cases when SVS Index cannot be created with the requested quantization mode
// due to platform and/or hardware limitations or combination of requested 'compression' modes.
// This assert handle those cases and skip a test if the mode is not supported.
// Elsewhere, test will fail if the index creation failed with no reason explained above.
#define ASSERT_INDEX(index) \
if (index == nullptr) { \
if (std::get<1>(svs_details::isSVSQuantBitsSupported(TypeParam::get_quant_bits()))) { \
GTEST_FAIL() << "Failed to create SVS index"; \
} else { \
GTEST_SKIP() << "SVS LVQ is not supported."; \
} \
}
// Log callback function to print non-debug log messages
static void svsTestLogCallBackNoDebug(void *ctx, const char *level, const char *message) {
if (level == nullptr || message == nullptr) {
return; // Skip null messages
}
if (std::string_view{level} == VecSimCommonStrings::LOG_DEBUG_STRING) {
return; // Skip debug messages
}
// Print other log levels
std::cout << level << ": " << message << std::endl;
}
template <typename index_type_t>
class SVSTest : public ::testing::Test {
public:
using data_t = typename index_type_t::data_t;
protected:
void SetTypeParams(SVSParams ¶ms) {
params.quantBits = index_type_t::get_quant_bits();
params.type = index_type_t::get_index_type();
params.multi = false;
}
VecSimIndex *CreateNewIndex(const VecSimParams &index_params) {
return VecSimIndex_New(&index_params);
}
VecSimIndex *CreateNewIndex(SVSParams ¶ms) {
SetTypeParams(params);
VecSimParams index_params = CreateParams(params);
return CreateNewIndex(index_params);
}
SVSIndexBase *CastToSVS(VecSimIndex *index) {
auto indexBase = dynamic_cast<SVSIndexBase *>(index);
assert(indexBase != nullptr);
return indexBase;
}
void SetUp() override {
// Limit VecSim log level to avoid printing too much information
VecSimIndexInterface::setLogCallbackFunction(svsTestLogCallBackNoDebug);
}
// Check if the test is running in fallback mode to scalar quantization.
bool isFallbackToSQ() const {
// Get the fallback quantization mode and compare it to the scalar quantization mode.
return VecSimSvsQuant_Scalar ==
std::get<0>(svs_details::isSVSQuantBitsSupported(index_type_t::get_quant_bits()));
}
// Use svsInfoStruct for parameter validation (ignoring additional runtime fields)
using ExpectedSVSValues = svsInfoStruct;
// Helper method to validate SVS parameters using debugInfo
static void validateSVSParameters(VecSimIndex *index, const ExpectedSVSValues &expected) {
// Get debug info to validate all parameters
VecSimIndexDebugInfo info = VecSimIndex_DebugInfo(index);
// Validate basic index properties
EXPECT_EQ(info.commonInfo.basicInfo.algo, VecSimAlgo_SVS);
compareSVSInfo(info.svsInfo, expected);
}
};
// TEST_DATA_T and TEST_DIST_T are defined in test_utils.h
template <VecSimType type, typename DataType, VecSimSvsQuantBits quantBits>
struct SVSIndexType {
static constexpr VecSimType get_index_type() { return type; }
static constexpr VecSimSvsQuantBits get_quant_bits() { return quantBits; }
typedef DataType data_t;
};
// clang-format off
using SVSDataTypeSet = ::testing::Types<SVSIndexType<VecSimType_FLOAT32, float, VecSimSvsQuant_NONE>
,SVSIndexType<VecSimType_FLOAT32, float, VecSimSvsQuant_8>
,SVSIndexType<VecSimType_FLOAT32, float, VecSimSvsQuant_8x8_LeanVec>
>;
// clang-format on
TYPED_TEST_SUITE(SVSTest, SVSDataTypeSet);
TYPED_TEST(SVSTest, svs_vector_add_test) {
size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_IP,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
EXPECT_EQ(VecSimIndex_IndexSize(index), 0);
GenerateAndAddVector<TEST_DATA_T>(index, dim, 1);
EXPECT_EQ(VecSimIndex_IndexSize(index), 1);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_miltiple_indexes) {
size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_IP,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
VecSimIndex *index2 = this->CreateNewIndex(params);
ASSERT_INDEX(index2);
VecSimIndex_Free(index);
VecSimIndex_Free(index2);
}
TYPED_TEST(SVSTest, svs_vector_update_test) {
size_t dim = 4;
size_t n = 1;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_IP,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
auto *svs_index = this->CastToSVS(index);
EXPECT_EQ(VecSimIndex_IndexSize(index), 0);
GenerateAndAddVector<TEST_DATA_T>(index, dim, 1);
EXPECT_EQ(VecSimIndex_IndexSize(index), 1);
// Prepare new vector data and call addVector with the same id, different data.
GenerateAndAddVector<TEST_DATA_T>(index, dim, 1, 2.0);
// Index size shouldn't change.
EXPECT_EQ(VecSimIndex_IndexSize(index), 1);
// Delete the last vector.
VecSimIndex_DeleteVector(index, 1);
EXPECT_EQ(VecSimIndex_IndexSize(index), 0);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_vector_search_by_id_test) {
// Scalar quantization accuracy is insufficient for this test.
if (this->isFallbackToSQ()) {
GTEST_SKIP() << "SVS Scalar quantization accuracy is insufficient for this test.";
}
size_t n = 100;
size_t k = 11;
size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
TEST_DATA_T query[] = {50, 50, 50, 50};
auto verify_res = [&](size_t id, double score, size_t index) { EXPECT_EQ(id, (index + 45)); };
runTopKSearchTest(index, query, k, verify_res, nullptr, BY_ID);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_bulk_vectors_add_delete_test) {
size_t n = 256;
size_t k = 11;
const size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
auto svs_index = this->CastToSVS(index); // CAST_TO_SVS(index, svs::distance::DistanceL2);
std::vector<std::array<TEST_DATA_T, dim>> v(n);
for (size_t i = 0; i < n; i++) {
GenerateVector<TEST_DATA_T>(v[i].data(), dim, i);
}
std::vector<size_t> ids(n);
std::iota(ids.begin(), ids.end(), 0);
svs_index->addVectors(v.data(), ids.data(), n);
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
TEST_DATA_T query[] = {50, 50, 50, 50};
auto verify_res = [&](size_t id, double score, size_t index) { EXPECT_EQ(id, (index + 45)); };
runTopKSearchTest(index, query, k, verify_res, nullptr, BY_ID);
// Delete almost all vectors
const size_t keep_num = 1;
ASSERT_EQ(svs_index->deleteVectors(ids.data(), n - keep_num), n - keep_num);
ASSERT_EQ(VecSimIndex_IndexSize(index), keep_num);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_get_distance) {
// Scalar quantization accuracy is insufficient for this test.
if (this->isFallbackToSQ()) {
GTEST_SKIP() << "SVS Scalar quantization accuracy is insufficient for this test.";
}
size_t n = 4;
size_t dim = 2;
size_t numIndex = 3;
VecSimIndex *index[numIndex];
std::vector<double> distances;
TEST_DATA_T v1[] = {M_PI, M_PI};
TEST_DATA_T v2[] = {M_E, M_E};
TEST_DATA_T v3[] = {M_PI, M_E};
TEST_DATA_T v4[] = {M_SQRT2, -M_SQRT2};
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
for (size_t i = 0; i < numIndex; i++) {
params.metric = (VecSimMetric)i;
index[i] = this->CreateNewIndex(params);
ASSERT_INDEX(index[i]);
VecSimIndex_AddVector(index[i], v1, 1);
VecSimIndex_AddVector(index[i], v2, 2);
VecSimIndex_AddVector(index[i], v3, 3);
VecSimIndex_AddVector(index[i], v4, 4);
ASSERT_EQ(VecSimIndex_IndexSize(index[i]), 4);
}
TEST_DATA_T *query = v1;
TEST_DATA_T *norm = v2; // {e, e}
VecSim_Normalize(norm, dim, params.type); // now {1/sqrt(2), 1/sqrt(2)}
ASSERT_TYPE_EQ(norm[0], TEST_DATA_T(1.0 / sqrt(2.0)));
ASSERT_TYPE_EQ(norm[1], TEST_DATA_T(1.0 / sqrt(2.0)));
double dist;
auto qbits = TypeParam::get_quant_bits();
double relative_err = qbits ? 1e-2 : 1.e-5;
// VecSimMetric_L2
distances = {0, 0.3583844006061554, 0.1791922003030777, 23.739208221435547};
for (size_t i = 0; i < n; i++) {
dist = VecSimIndex_GetDistanceFrom_Unsafe(index[VecSimMetric_L2], i + 1, query);
EXPECT_NEAR(dist, distances[i], std::abs(distances[i] * relative_err));
}
// VecSimMetric_IP
distances = {-18.73921012878418, -16.0794677734375, -17.409339904785156, 1};
for (size_t i = 0; i < n; i++) {
dist = VecSimIndex_GetDistanceFrom_Unsafe(index[VecSimMetric_IP], i + 1, query);
EXPECT_NEAR(dist, distances[i], std::abs(distances[i] * relative_err));
}
// VecSimMetric_Cosine
distances = {5.9604644775390625e-08, 5.9604644775390625e-08, 0.0025991201400756836, 1};
for (size_t i = 0; i < n; i++) {
dist = VecSimIndex_GetDistanceFrom_Unsafe(index[VecSimMetric_Cosine], i + 1, norm);
EXPECT_NEAR(dist, distances[i], std::abs(distances[i] * relative_err));
}
// Bad values
dist = VecSimIndex_GetDistanceFrom_Unsafe(index[VecSimMetric_Cosine], 0, norm);
EXPECT_TRUE(std::isnan(dist));
dist = VecSimIndex_GetDistanceFrom_Unsafe(index[VecSimMetric_L2], 46, query);
EXPECT_TRUE(std::isnan(dist));
// Clean-up.
for (size_t i = 0; i < numIndex; i++) {
VecSimIndex_Free(index[i]);
}
}
TYPED_TEST(SVSTest, svs_indexing_same_vector) {
const size_t n = 100;
const size_t k = 10;
const size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
auto svs_index = this->CastToSVS(index);
ASSERT_NE(svs_index, nullptr);
std::vector<std::array<TEST_DATA_T, dim>> v(n);
for (size_t i = 0; i < n; i++) {
GenerateVector<TEST_DATA_T>(v[i].data(), dim,
i / 10); // i / 10 is in integer (take the "floor" value).
}
std::vector<size_t> ids(n);
std::iota(ids.begin(), ids.end(), 0);
svs_index->addVectors(v.data(), ids.data(), n);
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Run a query where all the results are supposed to be {5,5,5,5} (different ids).
TEST_DATA_T query[] = {4.9, 4.95, 5.05, 5.1};
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(id >= 50 && id < 60 && score <= 1);
};
runTopKSearchTest(index, query, k, verify_res);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_reindexing_same_vector) {
const size_t n = 100;
const size_t k = 10;
const size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
auto svs_index = this->CastToSVS(index);
ASSERT_NE(svs_index, nullptr);
std::vector<std::array<TEST_DATA_T, dim>> v(n);
for (size_t i = 0; i < n; i++) {
// i / 10 is in integer (take the "floor" value).
GenerateVector<TEST_DATA_T>(v[i].data(), dim, i / 10);
}
std::vector<size_t> ids(n);
std::iota(ids.begin(), ids.end(), 0);
svs_index->addVectors(v.data(), ids.data(), n);
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Run a query where all the results are supposed to be {5,5,5,5} (different ids).
TEST_DATA_T query[] = {4.9, 4.95, 5.05, 5.1};
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(id >= 50 && id < 60 && score <= 1);
};
runTopKSearchTest(index, query, k, verify_res);
// Delete almost all vectors - keeping SVS index implementation alive.
for (size_t i = 0; i < n - 1; i++) {
VecSimIndex_DeleteVector(index, i);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), 1);
// Reinsert the same vectors under the same ids.
for (size_t i = 0; i < n; i++) {
// i / 10 is in integer (take the "floor value).
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i / 10);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Run the same query again.
runTopKSearchTest(index, query, k, verify_res);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_reindexing_same_vector_different_id) {
const size_t n = 100;
const size_t k = 10;
const size_t dim = 4;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
auto svs_index = this->CastToSVS(index);
ASSERT_NE(svs_index, nullptr);
std::vector<std::array<TEST_DATA_T, dim>> v(n);
for (size_t i = 0; i < n; i++) {
GenerateVector<TEST_DATA_T>(v[i].data(), dim,
i / 10); // i / 10 is in integer (take the "floor" value).
}
std::vector<size_t> ids(n);
std::iota(ids.begin(), ids.end(), 0);
svs_index->addVectors(v.data(), ids.data(), n);
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Run a query where all the results are supposed to be {5,5,5,5} (different ids).
TEST_DATA_T query[] = {4.9, 4.95, 5.05, 5.1};
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(id >= 50 && id < 60 && score <= 1);
};
runTopKSearchTest(index, query, k, verify_res);
for (size_t i = 0; i < n - 1; i++) {
VecSimIndex_DeleteVector(index, i);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), 1);
// Reinsert the same vectors under different ids than before.
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i + 10,
i / 10); // i / 10 is in integer (take the "floor" value).
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Run the same query again.
auto verify_res_different_id = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(id >= 60 && id < 70 && score <= 1);
};
runTopKSearchTest(index, query, k, verify_res_different_id);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_batch_iterator) {
// Scalar quantization accuracy is insufficient for this test.
if (this->isFallbackToSQ()) {
GTEST_SKIP() << "SVS Scalar quantization accuracy is insufficient for this test.";
}
size_t dim = 4;
// run the test twice - for index of size 100, every iteration will run select-based search,
// as the number of results is 5, which is more than 0.1% of the index size. for index of size
// 10000, we will run the heap-based search until we return 5000 results, and then switch to
// select-based search.
for (size_t n : {100, 1000}) {
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Query for (n,n,...,n) vector (recall that n is the largest id in te index).
TEST_DATA_T query[dim];
GenerateVector<TEST_DATA_T>(query, dim, n);
VecSimBatchIterator *batchIterator = VecSimBatchIterator_New(index, query, nullptr);
size_t iteration_num = 0;
// Get the 10 vectors whose ids are the maximal among those that hasn't been returned yet,
// in every iteration. The order should be from the largest to the lowest id.
size_t n_res = 20;
while (VecSimBatchIterator_HasNext(batchIterator)) {
std::vector<size_t> expected_ids(n_res);
for (size_t i = 0; i < n_res; i++) {
expected_ids[i] = (n - iteration_num * n_res - i - 1);
}
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_EQ(id, expected_ids[index]);
};
runBatchIteratorSearchTest(batchIterator, n_res, verify_res);
iteration_num++;
}
ASSERT_EQ(iteration_num, n / n_res);
VecSimBatchIterator_Free(batchIterator);
VecSimIndex_Free(index);
}
}
TYPED_TEST(SVSTest, svs_batch_iterator_non_unique_scores) {
// Scalar quantization accuracy is insufficient for this test.
if (this->isFallbackToSQ()) {
GTEST_SKIP() << "SVS Scalar quantization accuracy is insufficient for this test.";
}
size_t dim = 4;
// Run the test twice - for index of size 100, every iteration will run select-based search,
// as the number of results is 5, which is more than 0.1% of the index size. for index of size
// 10000, we will run the heap-based search until we return 5000 results, and then switch to
// select-based search.
for (size_t n : {100, 1000}) {
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i / 10);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Query for (n,n,...,n) vector (recall that n is the largest id in te index).
TEST_DATA_T query[dim];
GenerateVector<TEST_DATA_T>(query, dim, n);
VecSimBatchIterator *batchIterator = VecSimBatchIterator_New(index, query, nullptr);
size_t iteration_num = 0;
// Get the 5 vectors whose ids are the maximal among those that hasn't been returned yet, in
// every iteration. there are n/10 groups of 10 different vectors with the same score.
size_t n_res = 5;
bool even_iteration = false;
std::set<size_t> expected_ids;
while (VecSimBatchIterator_HasNext(batchIterator)) {
// Insert the maximal 10 ids in every odd iteration.
if (!even_iteration) {
for (size_t i = 1; i <= 2 * n_res; i++) {
expected_ids.insert(n - iteration_num * n_res - i);
}
}
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(expected_ids.find(id) != expected_ids.end());
expected_ids.erase(id);
};
runBatchIteratorSearchTest(batchIterator, n_res, verify_res);
// Make sure that the expected ids set is empty after two iterations.
if (even_iteration) {
ASSERT_TRUE(expected_ids.empty());
}
iteration_num++;
even_iteration = !even_iteration;
}
ASSERT_EQ(iteration_num, n / n_res);
VecSimBatchIterator_Free(batchIterator);
VecSimIndex_Free(index);
}
}
TYPED_TEST(SVSTest, svs_batch_iterator_reset) {
// Scalar quantization accuracy is insufficient for this test.
if (this->isFallbackToSQ()) {
GTEST_SKIP() << "SVS Scalar quantization accuracy is insufficient for this test.";
}
size_t dim = 4;
size_t n = 10000;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i / 10);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Query for (n,n,...,n) vector (recall that n is the largest id in te index).
TEST_DATA_T query[dim];
GenerateVector<TEST_DATA_T>(query, dim, n);
VecSimBatchIterator *batchIterator = VecSimBatchIterator_New(index, query, nullptr);
// Get the 100 vectors whose ids are the maximal among those that hasn't been returned yet, in
// every iteration. run this flow for 5 times, each time for 10 iteration, and reset the
// iterator.
size_t n_res = 100;
size_t total_iteration = 5;
size_t re_runs = 3;
for (size_t take = 0; take < re_runs; take++) {
size_t iteration_num = 0;
while (VecSimBatchIterator_HasNext(batchIterator)) {
std::set<size_t> expected_ids;
for (size_t i = 1; i <= n_res * 2; i++) {
expected_ids.insert(n - iteration_num * n_res - i);
}
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(expected_ids.find(id) != expected_ids.end());
expected_ids.erase(id);
};
runBatchIteratorSearchTest(batchIterator, n_res, verify_res);
iteration_num++;
if (iteration_num == total_iteration) {
break;
}
}
VecSimBatchIterator_Reset(batchIterator);
}
VecSimBatchIterator_Free(batchIterator);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, svs_batch_iterator_corner_cases) {
// Scalar quantization accuracy is insufficient for this test.
if (this->isFallbackToSQ()) {
GTEST_SKIP() << "SVS Scalar quantization accuracy is insufficient for this test.";
}
size_t dim = 4;
size_t n = 1000;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
// Query for (n,n,...,n) vector (recall that n is the largest id in te index).
TEST_DATA_T query[dim];
GenerateVector<TEST_DATA_T>(query, dim, n);
// Create batch iterator for empty index.
VecSimBatchIterator *batchIterator = VecSimBatchIterator_New(index, query, nullptr);
// Try to get more results even though there are no.
VecSimQueryReply *res = VecSimBatchIterator_Next(batchIterator, 1, BY_SCORE);
ASSERT_EQ(VecSimQueryReply_Len(res), 0);
VecSimQueryReply_Free(res);
// Retry to get results.
VecSimBatchIterator_Reset(batchIterator);
res = VecSimBatchIterator_Next(batchIterator, 1, BY_SCORE);
ASSERT_EQ(VecSimQueryReply_Len(res), 0);
VecSimQueryReply_Free(res);
// Check if depleted
ASSERT_FALSE(VecSimBatchIterator_HasNext(batchIterator));
VecSimBatchIterator_Free(batchIterator);
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
batchIterator = VecSimBatchIterator_New(index, query, nullptr);
// Ask for zero results.
res = VecSimBatchIterator_Next(batchIterator, 0, BY_SCORE);
ASSERT_EQ(VecSimQueryReply_Len(res), 0);
VecSimQueryReply_Free(res);
// Get all in first iteration, expect to use select search.
size_t n_res = n;
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(id == n - 1 - index);
};
runBatchIteratorSearchTest(batchIterator, n_res, verify_res);
ASSERT_FALSE(VecSimBatchIterator_HasNext(batchIterator));
// Try to get more results even though there are no.
res = VecSimBatchIterator_Next(batchIterator, n_res, BY_SCORE);
ASSERT_EQ(VecSimQueryReply_Len(res), 0);
VecSimQueryReply_Free(res);
// Reset, and run in batches, but the final batch is partial.
VecSimBatchIterator_Reset(batchIterator);
res = VecSimBatchIterator_Next(batchIterator, n_res / 2, BY_SCORE);
ASSERT_EQ(VecSimQueryReply_Len(res), n / 2);
VecSimQueryReply_Free(res);
res = VecSimBatchIterator_Next(batchIterator, n_res / 2 + 1, BY_SCORE);
ASSERT_EQ(VecSimQueryReply_Len(res), n / 2);
VecSimQueryReply_Free(res);
ASSERT_FALSE(VecSimBatchIterator_HasNext(batchIterator));
VecSimBatchIterator_Free(batchIterator);
VecSimIndex_Free(index);
}
// Add up to capacity.
TYPED_TEST(SVSTest, resizeIndex) {
size_t dim = 4;
size_t n = 10;
size_t bs = 4;
SVSParams params = {.dim = dim, .metric = VecSimMetric_L2, .blockSize = bs};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
// Add up to n.
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i);
}
// Initial capacity is rounded up to the block size.
size_t extra_cap = n % bs == 0 ? 0 : bs - n % bs;
auto quantBits = TypeParam::get_quant_bits();
// Get the fallback quantization mode
quantBits = std::get<0>(svs_details::isSVSQuantBitsSupported(quantBits));
if (quantBits != VecSimSvsQuant_NONE) {
// LVQDataset does not provide a capacity method
extra_cap = 0;
}
// The size (+extra) and the capacity should be equal.
ASSERT_EQ(index->indexCapacity(), VecSimIndex_IndexSize(index) + extra_cap);
// The capacity shouldn't be changed.
ASSERT_EQ(index->indexCapacity(), n + extra_cap);
VecSimIndex_Free(index);
}
// Test empty index edge cases.
TYPED_TEST(SVSTest, svs_empty_index) {
size_t dim = 4;
size_t n = 20;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
// Try to remove from an empty index - should fail because label doesn't exist.
VecSimIndex_DeleteVector(index, 0);
// Add one vector.
GenerateAndAddVector<TEST_DATA_T>(index, dim, 1, 1.7);
// Size equals 1.
ASSERT_EQ(VecSimIndex_IndexSize(index), 1);
// Try to remove it.
VecSimIndex_DeleteVector(index, 1);
// Size equals 0.
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
// The expected capacity should be 0 for empty index.
ASSERT_EQ(index->indexCapacity(), 0);
// Try to remove it again.
VecSimIndex_DeleteVector(index, 1);
// Nor the size.
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, test_delete_vector) {
size_t k = 5;
size_t dim = 2;
size_t block_size = 3;
SVSParams params = {
.dim = dim,
.metric = VecSimMetric_L2,
.blockSize = block_size,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
// Delete from empty index
ASSERT_EQ(VecSimIndex_DeleteVector(index, 111), 0);
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
size_t n = 6;
for (size_t i = 0; i < n; i++) {
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i);
}
ASSERT_EQ(VecSimIndex_IndexSize(index), n);
// Here the shift should happen.
VecSimIndex_DeleteVector(index, 1);
ASSERT_EQ(VecSimIndex_IndexSize(index), n - 1);
TEST_DATA_T query[] = {0.0, 0.0};
auto verify_res = [&](size_t id, double score, size_t index) {
if (index == 0) {
ASSERT_EQ(id, index);
} else {
ASSERT_EQ(id, index + 1);
}
};
runTopKSearchTest(index, query, k, verify_res);
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, sanity_reinsert_1280) {
size_t n = 5;
size_t d = 1280;
size_t k = 5;
SVSParams params = {
.dim = d,
.metric = VecSimMetric_L2,
/* SVS-Vamana specifics */
.alpha = 1.2,
.graph_max_degree = 64,
.construction_window_size = 20,
.max_candidate_pool_size = 1024,
.prune_to = 60,
.use_search_history = VecSimOption_ENABLE,
};
VecSimIndex *index = this->CreateNewIndex(params);
ASSERT_INDEX(index);
auto *vectors = new TEST_DATA_T[n * d];
// Generate random vectors in every iteration and inert them under different ids.
for (size_t iter = 1; iter <= 3; iter++) {
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < d; j++) {
(vectors + i * d)[j] = (TEST_DATA_T)rand() / (TEST_DATA_T)(RAND_MAX) / 100;
}
}
auto expected_ids = std::set<size_t>();
for (size_t i = 0; i < n; i++) {
VecSimIndex_AddVector(index, (vectors + i * d), i * iter);
expected_ids.insert(i * iter);
}
auto verify_res = [&](size_t id, double score, size_t index) {
ASSERT_TRUE(expected_ids.find(id) != expected_ids.end());
expected_ids.erase(id);
};
// Send arbitrary vector (the first) and search for top k. This should return all the
// vectors that were inserted in this iteration - verify their ids.
runTopKSearchTest(index, vectors, k, verify_res);
// Remove vectors form current iteration.
for (size_t i = 0; i < n; i++) {
VecSimIndex_DeleteVector(index, i * iter);
}
}
delete[] vectors;
VecSimIndex_Free(index);
}
TYPED_TEST(SVSTest, test_svs_info) {