diskann-benchmark-core 0.54.0

DiskANN3 is a composable library for bringing scalable, accurate and cost-effective vector indexing to multiple databases.
Documentation
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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use std::{collections::HashSet, hash::Hash};

use diskann_utils::{
    strided::StridedView,
    views::{Matrix, MatrixView},
};
use thiserror::Error;

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RecallMetrics {
    /// The `k` value for `k-recall-at-n`.
    pub recall_k: usize,
    /// The `n` value for `k-recall-at-n`.
    pub recall_n: usize,
    /// The number of queries.
    pub num_queries: usize,
    /// The average recall across queries with non-empty groundtruth.
    /// Queries with zero groundtruth results are excluded from the average.
    pub average: f64,
}

#[derive(Debug, Error)]
pub enum ComputeRecallError {
    #[error("results matrix has {0} rows but ground truth has {1}")]
    RowsMismatch(usize, usize),
    #[error("distances matrix has {0} rows but ground truth has {1}")]
    DistanceRowsMismatch(usize, usize),
    #[error("recall k value {0} must be less than or equal to recall n {1}")]
    RecallKAndNError(usize, usize),
    #[error(
        "number of groundtruth values per query {0} must be at least the specified recall n {1}"
    )]
    NotEnoughGroundTruth(usize, usize),
    #[error("number of groundtruth distances {0} does not match groundtruth entries {1}")]
    GroundTruthDistanceMismatch(usize, usize),
}

/// An abstraction over data-structures such as vector-of-vectors.
///
/// This is used in recall calculations such as [`knn`] and [`average_precision`] and
/// is purposely `dyn` compatible to reduce compilation overhead.
///
/// Implementations should ensure that if [`Self::nrows`] returns a value `N` that `row(i)`
/// returns a slice for all `i` in `0..N`. Accesses outside of that range are allowed to
/// panic.
///
/// The implementation [`Self::ncols`] is optional and can be implemented if the length of
/// all inner vectors is known and identical for all inner vectors, enabling faster error
/// paths during recall calculation.
///
/// If [`Self::ncols`] returns `Some(K)`, then the length of each slice returned from
/// [`Self::row`] should have a length equal to `K`. Note that unsafe code may **not** rely
/// on this behavior.
///
/// If [`Self::ncols`] returns `None` then no assumption can be made about the length of
/// the slices yielded from [`Self::row`].
pub trait Rows<T> {
    /// Return the number of subslices in `Self`.
    fn nrows(&self) -> usize;

    /// Return the `i`th subslice contained in self.
    fn row(&self, i: usize) -> &[T];

    /// Return `Some(K)` if all subslices are known to have length `K`. Otherwise, return
    /// `None`.
    ///
    /// # Provided Implementation
    ///
    /// The provided implementation returns `None`.
    fn ncols(&self) -> Option<usize> {
        None
    }
}

impl<T> Rows<T> for Matrix<T> {
    fn nrows(&self) -> usize {
        Matrix::<T>::nrows(self)
    }
    fn row(&self, i: usize) -> &[T] {
        Matrix::<T>::row(self, i)
    }
    fn ncols(&self) -> Option<usize> {
        Some(Matrix::<T>::ncols(self))
    }
}

impl<T> Rows<T> for MatrixView<'_, T> {
    fn nrows(&self) -> usize {
        MatrixView::<'_, T>::nrows(self)
    }
    fn row(&self, i: usize) -> &[T] {
        MatrixView::<'_, T>::row(self, i)
    }
    fn ncols(&self) -> Option<usize> {
        Some(MatrixView::<'_, T>::ncols(self))
    }
}

impl<T> Rows<T> for Vec<Vec<T>> {
    fn nrows(&self) -> usize {
        self.len()
    }
    fn row(&self, i: usize) -> &[T] {
        &self[i]
    }
}

/// Aggregate trait for required behavior when computing recall and average precision.
pub trait RecallCompatible: Eq + Hash + Clone + std::fmt::Debug {}

impl<T> RecallCompatible for T where T: Eq + Hash + Clone + std::fmt::Debug {}

// Enum representing whether the ground truth has fixed size
// (every row should have the same number, >= recall_k entries)
// or flexible size (row may have any number of entries, including none)
#[derive(Copy, Clone, Debug)]
pub enum GroundTruthMode {
    Fixed,
    Flexible,
}

/// Compute the K-nearest-neighbors recall value "K-recall-at-N".
///
/// For each entry in `groundtruth` and `results`, this computes the `recall_k` number of
/// elements of `groundtruth` that are present in the first `recall_n` entries of `results`.
///
/// If `groundtruth_distances` is provided, then it will be used to allow ties when matching
/// the last values of each entry of `results`. Values will be counted towards the recall if
/// they have the same distance as the last ordered candidate.
///
/// Note that an error will NOT be given if an entry in `results`
/// has fewer than `recall_n` candidates.
///
/// If `ground_truth_mode` is `GroundTruthMode::Fixed`, then an
/// error will be given if any entry in `groundtruth` has fewer
/// than `recall_k` candidates.
pub fn knn<T>(
    groundtruth: &dyn Rows<T>,
    groundtruth_distances: Option<StridedView<'_, f32>>,
    results: &dyn Rows<T>,
    recall_k: usize,
    recall_n: usize,
    ground_truth_mode: GroundTruthMode,
) -> Result<RecallMetrics, ComputeRecallError>
where
    T: RecallCompatible,
{
    if recall_k > recall_n {
        return Err(ComputeRecallError::RecallKAndNError(recall_k, recall_n));
    }

    let nrows = results.nrows();
    if nrows != groundtruth.nrows() {
        return Err(ComputeRecallError::RowsMismatch(nrows, groundtruth.nrows()));
    }

    if let GroundTruthMode::Fixed = ground_truth_mode {
        // Validate that all rows in `groundtruth` have at least `recall_k` entries.
        for i in 0..nrows {
            let gt_row = groundtruth.row(i);
            if gt_row.len() < recall_k {
                return Err(ComputeRecallError::NotEnoughGroundTruth(
                    gt_row.len(),
                    recall_k,
                ));
            }
        }
    }

    // If `groundtruth_distances` are present, validate that there are
    // enough rows and that each row has the same
    // number of entries as the corresponding row in `groundtruth`.
    if let Some(distances) = groundtruth_distances {
        if nrows != distances.nrows() {
            return Err(ComputeRecallError::DistanceRowsMismatch(
                distances.nrows(),
                nrows,
            ));
        }

        for i in 0..nrows {
            let gt_row = groundtruth.row(i);
            let distances_row = distances.row(i);
            if gt_row.len() != distances_row.len() {
                return Err(ComputeRecallError::GroundTruthDistanceMismatch(
                    distances_row.len(),
                    gt_row.len(),
                ));
            }
        }
    }

    // The actual recall computation for groundtruth
    let mut recall_values: Vec<f64> = Vec::new();
    let mut this_groundtruth = HashSet::new();
    let mut this_results = HashSet::new();

    for i in 0..results.nrows() {
        let result = results.row(i);

        let gt_row = groundtruth.row(i);
        // `groundtruth` does not have to be fixed-size,
        // so we compute `recall_k` for this row based on its gt length
        let this_recall_k = gt_row.len().min(recall_k);

        if this_recall_k == 0 {
            continue;
        }

        // Populate the groundtruth using the top-k
        this_groundtruth.clear();
        this_groundtruth.extend(gt_row.iter().take(this_recall_k).cloned());

        // If we have distances, then continue to append distances as long as the distance
        // value is constant
        if let Some(distances) = groundtruth_distances {
            let distances_row = distances.row(i);

            // we've already checked that `results` and `distances` have at lesat
            // `recall_k >= this_recall_k` entries, so it's safe to access `distances_row[this_recall_k - 1]`
            let last_distance = distances_row[this_recall_k - 1];
            for (d, g) in distances_row.iter().zip(gt_row.iter()).skip(this_recall_k) {
                if *d == last_distance {
                    this_groundtruth.insert(g.clone());
                } else {
                    break;
                }
            }
        }

        this_results.clear();
        this_results.extend(result.iter().take(recall_n).cloned());

        // Count the overlap
        let r = this_groundtruth
            .iter()
            .filter(|i| this_results.contains(i))
            .count()
            .min(this_recall_k);

        let recall = (r as f64) / (this_recall_k as f64);

        recall_values.push(recall);
    }

    // Compute the average recall
    let total: f64 = recall_values.iter().sum();
    let average = if recall_values.is_empty() {
        0.0
    } else {
        total / (recall_values.len() as f64)
    };

    Ok(RecallMetrics {
        recall_k,
        recall_n,
        num_queries: nrows,
        average,
    })
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct AveragePrecisionMetrics {
    /// The number of queries.
    pub num_queries: usize,
    /// The average precision
    pub average_precision: f64,
}

#[derive(Debug, Error)]
pub enum AveragePrecisionError {
    #[error("results has {0} elements but ground truth has {1}")]
    EntriesMismatch(usize, usize),
}

/// Compute average precision of a range search result
pub fn average_precision<T>(
    results: &dyn Rows<T>,
    groundtruth: &dyn Rows<T>,
) -> Result<AveragePrecisionMetrics, AveragePrecisionError>
where
    T: RecallCompatible,
{
    let nrows = results.nrows();
    let groundtruth_nrows = groundtruth.nrows();
    if nrows != groundtruth_nrows {
        return Err(AveragePrecisionError::EntriesMismatch(
            nrows,
            groundtruth_nrows,
        ));
    }

    // The actual recall computation.
    let mut num_gt_results = 0;
    let mut num_reported_results = 0;

    let mut scratch = HashSet::new();
    let nrows = results.nrows();

    for i in 0..nrows {
        let result = results.row(i);
        let gt = groundtruth.row(i);

        scratch.clear();
        scratch.extend(result.iter().cloned());
        num_reported_results += gt.iter().filter(|i| scratch.contains(i)).count();
        num_gt_results += gt.len();
    }

    // Perform post-processing.
    let average_precision = (num_reported_results as f64) / (num_gt_results as f64);

    Ok(AveragePrecisionMetrics {
        average_precision,
        num_queries: nrows,
    })
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use diskann_utils::views::{self, Matrix};

    use super::*;

    fn test_rows_inner(rows: &dyn Rows<usize>, ncols: Option<usize>) {
        assert_eq!(rows.ncols(), ncols);
        assert_eq!(rows.nrows(), 3);
        assert_eq!(rows.row(0), &[0, 1, 2, 3]);
        assert_eq!(rows.row(1), &[4, 5, 6, 7]);
        assert_eq!(rows.row(2), &[8, 9, 10, 11]);
    }

    #[test]
    fn test_rows() {
        let mut i = 0usize;
        let mat = Matrix::new(
            views::Init(|| {
                let v = i;
                i += 1;
                v
            }),
            3,
            4,
        );

        test_rows_inner(&mat, Some(4));
        test_rows_inner(&(mat.as_view()), Some(4));

        let vecs = vec![vec![0, 1, 2, 3], vec![4, 5, 6, 7], vec![8, 9, 10, 11]];
        test_rows_inner(&vecs, None);
    }

    struct ExpectedRecall {
        recall_k: usize,
        recall_n: usize,
        // Recall for each component.
        components: Vec<usize>,
    }

    impl ExpectedRecall {
        fn new(recall_k: usize, recall_n: usize, components: Vec<usize>) -> Self {
            assert!(recall_k <= recall_n);
            components.iter().for_each(|x| {
                assert!(*x <= recall_k);
            });
            Self {
                recall_k,
                recall_n,
                components,
            }
        }

        fn compute_recall(&self) -> f64 {
            (self.components.iter().sum::<usize>() as f64)
                / ((self.components.len() * self.recall_k) as f64)
        }
    }

    #[test]
    fn test_happy_path() {
        let groundtruth = Matrix::try_from(
            vec![
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // row 0
                5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // row 1
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // row 2
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // row 3
            ]
            .into(),
            4,
            10,
        )
        .unwrap();

        let distances = Matrix::try_from(
            vec![
                0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 5.0, 6.0, // row 0
                2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, // row 1
                0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, // row 2
                0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 5.0, 6.0, // row 3
            ]
            .into(),
            4,
            10,
        )
        .unwrap();

        // Shift row 0 by one and row 1 by two.
        let our_results = Matrix::try_from(
            vec![
                100, 0, 1, 2, 5, 6, // row 0
                100, 101, 7, 8, 9, 10, // row 1
                0, 1, 2, 3, 4, 5, // row 2
                0, 1, 2, 3, 4, 5, // row 3
            ]
            .into(),
            4,
            6,
        )
        .unwrap();

        //---------//
        // No Ties //
        //---------//
        let expected_no_ties = vec![
            // Equal `k` and `n`
            ExpectedRecall::new(1, 1, vec![0, 0, 1, 1]),
            ExpectedRecall::new(2, 2, vec![1, 0, 2, 2]),
            ExpectedRecall::new(3, 3, vec![2, 1, 3, 3]),
            ExpectedRecall::new(4, 4, vec![3, 2, 4, 4]),
            ExpectedRecall::new(5, 5, vec![3, 3, 5, 5]),
            ExpectedRecall::new(6, 6, vec![4, 4, 6, 6]),
            // Unequal `k` and `n`.
            ExpectedRecall::new(1, 2, vec![1, 0, 1, 1]),
            ExpectedRecall::new(1, 3, vec![1, 0, 1, 1]),
            ExpectedRecall::new(2, 3, vec![2, 0, 2, 2]),
            ExpectedRecall::new(3, 5, vec![3, 1, 3, 3]),
        ];
        let epsilon = 1e-6; // Define a small tolerance

        for (i, expected) in expected_no_ties.iter().enumerate() {
            assert_eq!(expected.components.len(), our_results.nrows());
            let recall = knn(
                &groundtruth,
                None,
                &our_results,
                expected.recall_k,
                expected.recall_n,
                GroundTruthMode::Fixed,
            )
            .unwrap();

            let left = recall.average;
            let right = expected.compute_recall();
            assert!(
                (left - right).abs() < epsilon,
                "left = {}, right = {} on input {}",
                left,
                right,
                i
            );

            assert_eq!(recall.num_queries, our_results.nrows());
            assert_eq!(recall.recall_k, expected.recall_k);
            assert_eq!(recall.recall_n, expected.recall_n);
        }

        //-----------//
        // With Ties //
        //-----------//
        let expected_with_ties = vec![
            // Equal `k` and `n`
            ExpectedRecall::new(1, 1, vec![0, 0, 1, 1]),
            ExpectedRecall::new(2, 2, vec![1, 0, 2, 2]),
            ExpectedRecall::new(3, 3, vec![2, 1, 3, 3]),
            ExpectedRecall::new(4, 4, vec![3, 2, 4, 4]),
            ExpectedRecall::new(5, 5, vec![4, 3, 5, 5]), // tie-breaker kicks in
            ExpectedRecall::new(6, 6, vec![5, 4, 6, 6]), // tie-breaker kicks in
            // Unequal `k` and `n`.
            ExpectedRecall::new(1, 2, vec![1, 0, 1, 1]),
            ExpectedRecall::new(1, 3, vec![1, 0, 1, 1]),
            ExpectedRecall::new(2, 3, vec![2, 1, 2, 2]),
            ExpectedRecall::new(4, 5, vec![4, 3, 4, 4]),
        ];

        for (i, expected) in expected_with_ties.iter().enumerate() {
            assert_eq!(expected.components.len(), our_results.nrows());
            let recall = knn(
                &groundtruth,
                Some(distances.as_view().into()),
                &our_results,
                expected.recall_k,
                expected.recall_n,
                GroundTruthMode::Fixed,
            )
            .unwrap();

            let left = recall.average;
            let right = expected.compute_recall();
            assert!(
                (left - right).abs() < epsilon,
                "left = {}, right = {} on input {}",
                left,
                right,
                i
            );

            assert_eq!(recall.num_queries, our_results.nrows());
            assert_eq!(recall.recall_k, expected.recall_k);
            assert_eq!(recall.recall_n, expected.recall_n);
        }
    }

    #[test]
    fn test_error_recall_k_and_n() {
        let groundtruth = Matrix::<u32>::new(0, 10, 10);
        let results = Matrix::<u32>::new(0, 10, 10);
        let err = knn(&groundtruth, None, &results, 11, 10, GroundTruthMode::Fixed).unwrap_err();
        assert!(matches!(err, ComputeRecallError::RecallKAndNError(..)));
    }

    #[test]
    fn test_error_rows_mismatch() {
        let groundtruth = Matrix::<u32>::new(0, 11, 10);
        let results = Matrix::<u32>::new(0, 10, 10);
        let err = knn(&groundtruth, None, &results, 10, 10, GroundTruthMode::Fixed).unwrap_err();
        assert!(matches!(err, ComputeRecallError::RowsMismatch(..)));
        let err_allow_insufficient_results =
            knn(&groundtruth, None, &results, 10, 10, GroundTruthMode::Fixed).unwrap_err();
        assert!(matches!(
            err_allow_insufficient_results,
            ComputeRecallError::RowsMismatch(..)
        ));
    }

    #[test]
    fn test_error_not_enough_groundtruth() {
        let groundtruth = Matrix::<u32>::new(0, 10, 5);
        let results = Matrix::<u32>::new(0, 10, 10);
        let err = knn(&groundtruth, None, &results, 10, 10, GroundTruthMode::Fixed).unwrap_err();
        assert!(matches!(err, ComputeRecallError::NotEnoughGroundTruth(..)));
        let err_allow_insufficient_results =
            knn(&groundtruth, None, &results, 10, 10, GroundTruthMode::Fixed).unwrap_err();
        assert!(matches!(
            err_allow_insufficient_results,
            ComputeRecallError::NotEnoughGroundTruth(..)
        ));
    }

    #[test]
    fn test_dynamic_groundtruth_valid() {
        let groundtruth: Vec<_> = (0..10).map(|_| vec![0u32; 5]).collect();
        let results = Matrix::<u32>::new(0, 10, 10);
        // Should succeed: each row uses this_recall_k = min(5, 10) = 5
        // Should succeed in Flexible mode, but fail in Fixed mode
        let recall_flexible = knn(
            &groundtruth,
            None,
            &results,
            10,
            10,
            GroundTruthMode::Flexible,
        )
        .unwrap();
        assert_eq!(recall_flexible.num_queries, 10);
        // Should fail in Fixed mode
        let err = knn(&groundtruth, None, &results, 10, 10, GroundTruthMode::Fixed).unwrap_err();
        assert!(matches!(err, ComputeRecallError::NotEnoughGroundTruth(..)));
        assert_eq!(recall_flexible.num_queries, 10);
    }

    #[test]
    fn test_dynamic_groundtruth_full_match() {
        let gt_row: Vec<u32> = (1..=5).collect();
        let groundtruth: Vec<_> = (0..10).map(|_| gt_row.clone()).collect();
        let mut results = Matrix::<u32>::new(0, 10, 10);
        for i in 0..10 {
            for (j, v) in (1u32..=10).enumerate() {
                results[(i, j)] = v;
            }
        }
        let recall = knn(
            &groundtruth,
            None,
            &results,
            10,
            10,
            GroundTruthMode::Flexible,
        )
        .unwrap();
        assert!((recall.average - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_dynamic_groundtruth_partial_match() {
        // groundtruth: [1, 2, 3, 4, 5]; results contain [1, 2, 3, 6, 7, 8, 9, 10, 11, 12]
        let gt_row: Vec<u32> = (1..=5).collect();
        let groundtruth: Vec<_> = (0..10).map(|_| gt_row.clone()).collect();
        let mut results = Matrix::<u32>::new(0, 10, 10);
        let res_row: Vec<u32> = vec![1, 2, 3, 6, 7, 8, 9, 10, 11, 12];
        for i in 0..10 {
            for (j, &v) in res_row.iter().enumerate() {
                results[(i, j)] = v;
            }
        }
        let recall = knn(
            &groundtruth,
            None,
            &results,
            10,
            10,
            GroundTruthMode::Flexible,
        )
        .unwrap();
        assert!((recall.average - 0.6).abs() < 1e-10);
    }

    #[test]
    fn test_dynamic_groundtruth_mixed_zero_nonzero() {
        let mut groundtruth: Vec<Vec<u32>> = Vec::new();
        // First 5 rows: non-empty groundtruth
        for _ in 0..5 {
            groundtruth.push((1..=5).collect());
        }
        // Last 5 rows: empty groundtruth
        for _ in 0..5 {
            groundtruth.push(vec![]);
        }

        let mut results = Matrix::<u32>::new(0, 10, 10);
        for i in 0..10 {
            for (j, v) in (1u32..=10).enumerate() {
                results[(i, j)] = v;
            }
        }

        let recall = knn(
            &groundtruth,
            None,
            &results,
            10,
            10,
            GroundTruthMode::Flexible,
        )
        .unwrap();
        assert_eq!(recall.num_queries, 10);
        assert!((recall.average - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_dynamic_groundtruth_all_zero() {
        let groundtruth: Vec<Vec<u32>> = (0..10).map(|_| vec![]).collect();
        let results = Matrix::<u32>::new(0, 10, 10);

        let recall = knn(
            &groundtruth,
            None,
            &results,
            10,
            10,
            GroundTruthMode::Flexible,
        )
        .unwrap();
        assert_eq!(recall.num_queries, 10);
        assert_eq!(recall.average, 0.0);
        assert!(!recall.average.is_nan());
        assert!(!recall.average.is_infinite());
    }

    #[test]
    fn test_error_distance_rows_mismatch() {
        let groundtruth = Matrix::<u32>::new(0, 10, 10);
        let distances = Matrix::<f32>::new(0.0, 9, 10);
        let results = Matrix::<u32>::new(0, 10, 10);
        let err = knn(
            &groundtruth,
            Some(distances.as_view().into()),
            &results,
            10,
            10,
            GroundTruthMode::Fixed,
        )
        .unwrap_err();
        assert!(matches!(err, ComputeRecallError::DistanceRowsMismatch(..)));
    }

    #[test]
    fn test_error_distance_cols_mismatch() {
        let groundtruth = Matrix::<u32>::new(0, 10, 10);
        let distances = Matrix::<f32>::new(0.0, 10, 9);
        let results = Matrix::<u32>::new(0, 10, 10);
        let err = knn(
            &groundtruth,
            Some(distances.as_view().into()),
            &results,
            10,
            10,
            GroundTruthMode::Fixed,
        )
        .unwrap_err();
        assert!(matches!(
            err,
            ComputeRecallError::GroundTruthDistanceMismatch(..)
        ));
    }

    #[test]
    fn test_error_distance_cols_mismatch_variable_size_groundtruth() {
        // groundtruth: 2 rows, first row has 3 elements, second row has 2
        let groundtruth: Vec<Vec<u32>> = vec![vec![1, 2, 3], vec![4, 5]];
        // distances: first row has 2 elements (should be 3), second row has 2 (matches)
        let distances: Vec<Vec<f32>> = vec![vec![0.1, 0.2], vec![0.3, 0.4]];
        let distances = Matrix::try_from(
            distances.into_iter().flatten().collect::<Vec<_>>().into(),
            2,
            2,
        )
        .unwrap();
        // results: 2 rows, each with 3 elements
        let results: Vec<Vec<u32>> = vec![vec![1, 2, 3], vec![4, 5, 6]];
        let err = knn(
            &groundtruth,
            Some(distances.as_view().into()),
            &results,
            3,
            3,
            GroundTruthMode::Flexible,
        )
        .unwrap_err();

        println!("{err}");

        assert!(matches!(
            err,
            ComputeRecallError::GroundTruthDistanceMismatch(2, 3)
        ));
    }
}