diskann-quantization 0.62.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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

//! A PQ table optimized for computing distances between quantized vectors.
//!
//! During similarity search index construction, it is not uncommon to compute distances
//! among quantized elements within a table. The [`PaddedTable`] is designed to facilitate
//! such distances.
//!
//! ```
//! use diskann_quantization::{views::ChunkOffsets, product::tables};
//! use diskann_utils::views::rowmajor::{Owned, Matrix, MatrixMut};
//!
//! // We're creating the following pivot table.
//! //
//! // | chunk 0 | chunk 1 |
//! // |   0  0  |   1  1  | pivot 0
//! // |   1  1  |   2  2  | pivot 1
//! // |   2  2  |   3  3  | pivot 2
//!
//! let mut pivots = Owned::from_element(3, 4, 0.0f32);
//! pivots.row_mut(0).copy_from_slice(&[0.0, 0.0, 1.0, 1.0]);
//! pivots.row_mut(1).copy_from_slice(&[1.0, 1.0, 2.0, 2.0]);
//! pivots.row_mut(2).copy_from_slice(&[2.0, 2.0, 3.0, 3.0]);
//!
//! let offsets = ChunkOffsets::new(Box::new([0, 2, 4])).unwrap();
//!
//! let basic = tables::BasicTable::new(pivots, offsets).unwrap();
//! let padded = tables::PaddedTable::from_basic(basic.as_view());
//!
//! // Distances are provided through a v-table.
//! let vtable = padded.vtable(tables::padded::Metric::SquaredL2);
//!
//! // Compute the distance between [1, 1, 1, 1] and the chunk defined by [2, 1], which
//! // should translate to the compressed vector [2, 2, 2, 2].
//! let distance = vtable.distance(&padded, &[1.0, 1.0, 1.0, 1.0], &[2, 1]).unwrap();
//! assert_eq!(distance, 4.0);
//!
//! // Compute the distance between the two compressed vectors encoded by [0, 2] and [2, 0].
//! let distance = vtable.self_distance(&padded, &[0, 2], &[2, 0]).unwrap();
//! assert_eq!(distance, 16.0);
//! ```

use std::{marker::PhantomData, num::NonZeroUsize};

use diskann_utils::views::rowmajor::{self, Matrix, MatrixMut};
use diskann_vector::distance::Metric as VectorMetric;
use diskann_wide::{
    SIMDFloat, SIMDSumTree, SIMDVector,
    arch::{Architecture, Dispatched3, FTarget3, Scalar, Target, dispatch_no_features},
    lifetime::Ref,
};
use thiserror::Error;

#[cfg(target_arch = "x86_64")]
use diskann_wide::arch::x86_64::{V3, V4};

#[cfg(target_arch = "aarch64")]
use diskann_wide::arch::aarch64::Neon;

use crate::{product::tables::BasicTableView, views::ChunkOffsets};

/// A PQ table that stores pivots grouped by chunk in the following dense, row-major form:
/// ```text
///            | -- pivot 0 --    | -- pivot 1 --    | .... | -- pivot K-1 --    |
///            +------------------+------------------+------+--------------------+
///  chunk 0   | c000 c001 ... 0X | c010 c011 ... 0X | .... | c0K0 c0K1 ...  0X |
///  chunk 1   | c100 c101 ... 0X | c110 c111 ... 0X | .... | c1K0 c1K1 ...  0X |
///    ...     |       ...        |       ...        | .... |       ...         |
///  chunk N-1 | cN00 cN01 ... 0X | cN10 cN11 ... 0X | .... | cNK0 cNK1 ...  0X |
/// ```
/// where `cCPD` is dimension `D` of pivot `P` in chunk `C`, and trailing `0X`s denote
/// potential zero-padding to the SIMD-aligned pivot width.
///
/// The member `offsets` describes the number of *unpadded* dimensions of each chunk.
///
/// Importantly, though, the storage for each pivot is rounded up to a multiple of the
/// runtime system's preferred SIMD width and all pivots are padded to the same length.
/// This makes distance computations between pivots very fast when computing distances
/// between two product-quantized vectors.
#[derive(Debug, Clone)]
pub struct PaddedTable {
    /// Invariants:
    /// * `pivots.ncols()` is at least as large as the largest chunk in [`Self::offsets`]
    ///   and is always a multiple of `simd_width`.
    /// * `pivots.nrows() == offsets.len() * pivots_per_chunk`.
    pivots: rowmajor::Owned<f32>,
    offsets: ChunkOffsets,
    pivots_per_chunk: usize,
    simd_width: SIMDWidth,
    arch: RuntimeArch,
}

impl PaddedTable {
    /// Construct a [`PaddedTable`] with the same contents as `basic`.
    pub fn from_basic(basic: BasicTableView<'_>) -> Self {
        let arch = RuntimeArch::new();
        Self::from_basic_with(basic, arch)
    }

    fn from_basic_with(basic: BasicTableView<'_>, arch: RuntimeArch) -> Self {
        let pivots = basic.view_pivots();
        let offsets = basic.view_offsets();

        let pivots_per_chunk = pivots.nrows();

        // Compute the padded dimension of the pivots.
        //
        // There exists a corner case where `pivots` barely fits within the `isize` limit
        // of an allocation and padding will put us beyond that threshold, but that is
        // exceedingly unlikely for typical data.
        let max_chunk_dim = offsets.max_chunk_dim();
        let simd_width = arch.select_simd_width(max_chunk_dim);
        let padded_dim = max_chunk_dim
            .get()
            .next_multiple_of(simd_width.as_nonzero().get());

        let rows = pivots_per_chunk * offsets.len();
        let mut padded = rowmajor::Owned::from_element(rows, padded_dim, 0.0);
        let mut row = 0;

        // Since we padded, we use a custom "copy_from_slice" that allows `dst` to shrink.
        fn copy_from_slice_subset(dst: &mut [f32], src: &[f32]) {
            dst[..src.len()].copy_from_slice(src)
        }

        // Copy the pivots.
        (0..offsets.len()).for_each(|i| {
            #[expect(clippy::expect_used, reason = "`i` should be in-bounds")]
            let view = basic.pivots_for(i).expect("`i` should be in-bounds");
            view.rows().for_each(|src| {
                copy_from_slice_subset(padded.row_mut(row), src);
                row += 1;
            });
        });

        Self {
            pivots: padded,
            offsets: offsets.to_owned(),
            pivots_per_chunk,
            simd_width,
            arch,
        }
    }

    /// Return the distance [`VTable`] for the requested [`Metric`].
    ///
    /// Note that [`VTable`]s are generally specific to the [`PaddedTable`] that generated
    /// them and cannot be reliably shared with different tables.
    ///
    /// While this is not a safety issue, incorrect [`VTable`]s may yield errors due to
    /// mismatched SIMD widths.
    pub fn vtable(&self, metric: Metric) -> VTable {
        self.arch.dispatch(self.simd_width, metric)
    }

    /// Return the number of PQ centers per chunk.
    pub fn ncenters(&self) -> usize {
        self.pivots_per_chunk
    }

    /// Return the number of PQ chunks.
    pub fn nchunks(&self) -> usize {
        self.offsets.len()
    }

    /// Return the full-precision dimension expected by this table.
    pub fn dim(&self) -> usize {
        self.offsets.dim()
    }
}

/// Distance metrics used by [`PaddedTable`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Metric {
    SquaredL2,
    InnerProduct,
    Cosine,
}

impl From<VectorMetric> for Metric {
    fn from(metric: VectorMetric) -> Self {
        match metric {
            VectorMetric::L2 => Self::SquaredL2,
            VectorMetric::InnerProduct => Self::InnerProduct,
            VectorMetric::Cosine => Self::Cosine,
            VectorMetric::CosineNormalized => Self::Cosine,
        }
    }
}

type Distance = Dispatched3<Result<f32, DistanceError>, Ref<PaddedTable>, Ref<[f32]>, Ref<[u8]>>;
type SelfDistance =
    Dispatched3<Result<f32, SelfDistanceError>, Ref<PaddedTable>, Ref<[u8]>, Ref<[u8]>>;

/// A distance [`VTable`] for a [`PaddedTable`].
///
/// See: [`PaddedTable::vtable`].
#[derive(Debug, Clone, Copy)]
pub struct VTable {
    distance: Distance,
    self_distance: SelfDistance,
}

impl VTable {
    fn new<O>(arch: O::Arch) -> Self
    where
        O: Op,
    {
        Self {
            distance: arch.dispatch3::<
                OpTarget<O>,
                Result<f32, DistanceError>,
                Ref<PaddedTable>,
                Ref<[f32]>,
                Ref<[u8]>,
            >(),
            self_distance: arch.dispatch3::<
                OpTarget<O>,
                Result<f32, SelfDistanceError>,
                Ref<PaddedTable>,
                Ref<[u8]>,
                Ref<[u8]>,
            >(),
        }
    }

    /// Compute the distance between `vector` and the PQ vector encoded by `codes`.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    ///
    /// * `vector.len() != padded.dim()`
    /// * `codes.len() != padded.nchunks()`
    /// * Any element in `codes` is equal to or greater than `padded.ncenters()`.
    ///
    /// In addition, an error may be returned if `self` was created for a different
    /// [`PaddedTable`]. Mixing [`VTable`]s in this way is not a safety issue, but is also
    /// not guaranteed to work.
    #[inline]
    pub fn distance(
        &self,
        padded: &PaddedTable,
        vector: &[f32],
        codes: &[u8],
    ) -> Result<f32, DistanceError> {
        (self.distance).call(padded, vector, codes)
    }

    /// Compute the distance between two compressed vectors.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    ///
    /// * `a.len() != padded.nchunks()`
    /// * `b.len() != padded.nchunks()`
    /// * Any element in `a` or `b` is equal to or greater than `padded.ncenters()`.
    ///
    /// In addition, an error may be returned if `self` was created for a different
    /// [`PaddedTable`]. Mixing [`VTable`]s in this way is not a safety issue, but is also
    /// not guaranteed to work.
    #[inline]
    pub fn self_distance(
        &self,
        padded: &PaddedTable,
        a: &[u8],
        b: &[u8],
    ) -> Result<f32, SelfDistanceError> {
        (self.self_distance).call(padded, a, b)
    }
}

//-----------------//
// Width Selection //
//-----------------//

const FOUR: NonZeroUsize = NonZeroUsize::new(4).unwrap();

#[cfg(target_arch = "x86_64")]
const EIGHT: NonZeroUsize = NonZeroUsize::new(8).unwrap();

#[derive(Debug, Clone, Copy)]
enum SIMDWidth {
    Four,
    #[cfg(target_arch = "x86_64")]
    Eight,
}

impl SIMDWidth {
    fn as_nonzero(self) -> NonZeroUsize {
        match self {
            Self::Four => FOUR,
            #[cfg(target_arch = "x86_64")]
            Self::Eight => EIGHT,
        }
    }
}

/// The runtime architecture.
#[derive(Debug, Clone, Copy)]
enum RuntimeArch {
    Scalar(Scalar),
    #[cfg(target_arch = "x86_64")]
    V3(V3),
    #[cfg(target_arch = "aarch64")]
    Neon(Neon),
}

impl RuntimeArch {
    fn new() -> Self {
        dispatch_no_features(GetArch)
    }

    #[cfg_attr(
        not(target_arch = "x86_64"),
        expect(
            unused_variables,
            reason = "the same result is returned regardless of the actual chunk size"
        )
    )]
    fn select_simd_width(&self, max_chunk_size: NonZeroUsize) -> SIMDWidth {
        match self {
            Self::Scalar(_) => SIMDWidth::Four,
            // Use a smaller width if available.
            #[cfg(target_arch = "x86_64")]
            Self::V3(_) => match max_chunk_size.get() {
                0..=4 => SIMDWidth::Four,
                _ => SIMDWidth::Eight,
            },
            #[cfg(target_arch = "aarch64")]
            Self::Neon(_) => SIMDWidth::Four,
        }
    }

    fn dispatch(&self, simd_width: SIMDWidth, metric: Metric) -> VTable {
        diskann_wide::alias!(f32x4<A> = f32x4);
        diskann_wide::alias!(f32x8<A> = f32x8);

        match (*self, simd_width, metric) {
            (Self::Scalar(a), _, Metric::SquaredL2) => VTable::new::<SquaredL2<f32x4<Scalar>>>(a),
            (Self::Scalar(a), _, Metric::InnerProduct) => {
                VTable::new::<InnerProduct<f32x4<Scalar>>>(a)
            }
            (Self::Scalar(a), _, Metric::Cosine) => VTable::new::<Cosine<f32x4<Scalar>>>(a),

            // V3 //
            #[cfg(target_arch = "x86_64")]
            (Self::V3(a), SIMDWidth::Four, Metric::SquaredL2) => {
                VTable::new::<SquaredL2<f32x4<V3>>>(a)
            }
            #[cfg(target_arch = "x86_64")]
            (Self::V3(a), SIMDWidth::Four, Metric::InnerProduct) => {
                VTable::new::<InnerProduct<f32x4<V3>>>(a)
            }
            #[cfg(target_arch = "x86_64")]
            (Self::V3(a), SIMDWidth::Four, Metric::Cosine) => VTable::new::<Cosine<f32x4<V3>>>(a),

            #[cfg(target_arch = "x86_64")]
            (Self::V3(a), SIMDWidth::Eight, Metric::SquaredL2) => {
                VTable::new::<SquaredL2<f32x8<V3>>>(a)
            }
            #[cfg(target_arch = "x86_64")]
            (Self::V3(a), SIMDWidth::Eight, Metric::InnerProduct) => {
                VTable::new::<InnerProduct<f32x8<V3>>>(a)
            }
            #[cfg(target_arch = "x86_64")]
            (Self::V3(a), SIMDWidth::Eight, Metric::Cosine) => VTable::new::<Cosine<f32x8<V3>>>(a),

            // Neon //
            #[cfg(target_arch = "aarch64")]
            (Self::Neon(a), _, Metric::SquaredL2) => VTable::new::<SquaredL2<f32x4<Neon>>>(a),
            #[cfg(target_arch = "aarch64")]
            (Self::Neon(a), _, Metric::InnerProduct) => VTable::new::<InnerProduct<f32x4<Neon>>>(a),
            #[cfg(target_arch = "aarch64")]
            (Self::Neon(a), _, Metric::Cosine) => VTable::new::<Cosine<f32x4<Neon>>>(a),
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct GetArch;

impl Target<Scalar, RuntimeArch> for GetArch {
    #[inline(always)]
    fn run(self, arch: Scalar) -> RuntimeArch {
        RuntimeArch::Scalar(arch)
    }
}

#[cfg(target_arch = "x86_64")]
impl Target<V3, RuntimeArch> for GetArch {
    #[inline(always)]
    fn run(self, arch: V3) -> RuntimeArch {
        RuntimeArch::V3(arch)
    }
}

#[cfg(target_arch = "x86_64")]
impl Target<V4, RuntimeArch> for GetArch {
    #[inline(always)]
    fn run(self, arch: V4) -> RuntimeArch {
        RuntimeArch::V3(arch.retarget())
    }
}

#[cfg(target_arch = "aarch64")]
impl Target<Neon, RuntimeArch> for GetArch {
    #[inline(always)]
    fn run(self, arch: Neon) -> RuntimeArch {
        RuntimeArch::Neon(arch)
    }
}

//----------//
// SIMD Ops //
//----------//

trait Op {
    type Arch: Architecture;
    type Accum;
    type Vector: SIMDVector<Scalar = f32, Arch = Self::Arch>;

    fn init(arch: Self::Arch) -> Self::Accum;
    fn accum(acc: Self::Accum, x: Self::Vector, y: Self::Vector) -> Self::Accum;
    fn reduce_pair(a: Self::Accum, b: Self::Accum) -> f32;
}

#[derive(Debug)]
struct SquaredL2<V>(PhantomData<V>);

impl<V> Op for SquaredL2<V>
where
    V: SIMDFloat<Scalar = f32> + SIMDSumTree,
    V::Arch: Architecture,
{
    type Arch = V::Arch;
    type Accum = V;
    type Vector = V;

    fn init(arch: Self::Arch) -> V {
        V::default(arch)
    }

    fn accum(acc: V, x: V, y: V) -> V {
        let d = x - y;
        d.mul_add_simd(d, acc)
    }

    fn reduce_pair(a: V, b: V) -> f32 {
        (a + b).sum_tree()
    }
}

#[derive(Debug)]
struct InnerProduct<V>(PhantomData<V>);

impl<V> Op for InnerProduct<V>
where
    V: SIMDFloat<Scalar = f32> + SIMDSumTree,
    V::Arch: Architecture,
{
    type Arch = V::Arch;
    type Accum = V;
    type Vector = V;

    fn init(arch: Self::Arch) -> V {
        V::default(arch)
    }

    fn accum(acc: V, x: V, y: V) -> V {
        x.mul_add_simd(y, acc)
    }

    fn reduce_pair(a: V, b: V) -> f32 {
        -(a + b).sum_tree()
    }
}

#[derive(Debug)]
struct Cosine<V>(PhantomData<V>);

#[derive(Debug)]
struct CosineAccumulator<V> {
    xy: V,
    xnorm: V,
    ynorm: V,
}

fn finish_cosine(xy: f32, xnorm: f32, ynorm: f32) -> f32 {
    if xnorm < f32::MIN_POSITIVE || ynorm < f32::MIN_POSITIVE {
        1.0
    } else {
        let v = xy / (xnorm.sqrt() * ynorm.sqrt());
        1.0 - (-1.0f32).max(1.0f32.min(v))
    }
}

impl<V> Op for Cosine<V>
where
    V: SIMDFloat<Scalar = f32> + SIMDSumTree,
    V::Arch: Architecture,
{
    type Arch = V::Arch;
    type Accum = CosineAccumulator<V>;
    type Vector = V;

    fn init(arch: Self::Arch) -> Self::Accum {
        CosineAccumulator {
            xy: V::default(arch),
            xnorm: V::default(arch),
            ynorm: V::default(arch),
        }
    }

    fn accum(acc: Self::Accum, x: V, y: V) -> Self::Accum {
        CosineAccumulator {
            xy: x.mul_add_simd(y, acc.xy),
            xnorm: x.mul_add_simd(x, acc.xnorm),
            ynorm: y.mul_add_simd(y, acc.ynorm),
        }
    }

    fn reduce_pair(a: Self::Accum, b: Self::Accum) -> f32 {
        let xy = (a.xy + b.xy).sum_tree();
        let xnorm = (a.xnorm + b.xnorm).sum_tree();
        let ynorm = (a.ynorm + b.ynorm).sum_tree();
        finish_cosine(xy, xnorm, ynorm)
    }
}

#[derive(Debug, Clone, Copy)]
struct OpTarget<O>(PhantomData<O>);

impl<O> FTarget3<O::Arch, Result<f32, DistanceError>, &PaddedTable, &[f32], &[u8]> for OpTarget<O>
where
    O: Op,
{
    #[inline(always)]
    fn run(arch: O::Arch, table: &PaddedTable, a: &[f32], b: &[u8]) -> Result<f32, DistanceError> {
        distance::<O>(arch, table, a, b)
    }
}

impl<O> FTarget3<O::Arch, Result<f32, SelfDistanceError>, &PaddedTable, &[u8], &[u8]>
    for OpTarget<O>
where
    O: Op,
{
    #[inline(always)]
    fn run(
        arch: O::Arch,
        table: &PaddedTable,
        a: &[u8],
        b: &[u8],
    ) -> Result<f32, SelfDistanceError> {
        self_distance::<O>(arch, table, a, b)
    }
}

//--------------------------------//
// Full Precision-Quant Distances //
//--------------------------------//

/// Errors for [`VTable::distance`].
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum DistanceError {
    #[error("table has dimension {} but full vector has length {}", dim, alen)]
    ALen { dim: usize, alen: usize },
    #[error("table has {} chunks but codes slice has length {}", chunks, blen)]
    BLen { chunks: usize, blen: usize },
    #[error("codes have a value that exceeds the number of pivots {}", ncenters)]
    OutOfBounds { ncenters: usize },
    #[error("An invalid SIMD width was chosen - ensure the correct vtable is used")]
    InvalidSimd,
}

#[inline(always)]
fn distance<O>(
    arch: O::Arch,
    table: &PaddedTable,
    a: &[f32],
    b: &[u8],
) -> Result<f32, DistanceError>
where
    O: Op,
{
    // Check 1
    if !table
        .simd_width
        .as_nonzero()
        .get()
        .is_multiple_of(O::Vector::LANES)
    {
        return Err(DistanceError::InvalidSimd);
    }

    // Check 2
    if a.len() != table.dim() {
        return Err(DistanceError::ALen {
            dim: table.dim(),
            alen: a.len(),
        });
    }

    // Check 3
    if b.len() != table.nchunks() {
        return Err(DistanceError::BLen {
            chunks: table.nchunks(),
            blen: b.len(),
        });
    }

    // Check 4
    if let Ok(ncenters) = u8::try_from(table.ncenters())
        && let Some(max) = b.iter().max()
        && *max >= ncenters
    {
        return Err(DistanceError::OutOfBounds {
            ncenters: table.ncenters(),
        });
    }

    // All checks passed - we're good to go!

    let pivots = &table.pivots;

    let pivot_stride = pivots.ncols();
    let chunk_stride = table.pivots_per_chunk * pivot_stride;

    let nchunks = b.len();
    let mut d0 = O::init(arch);
    let mut d1 = O::init(arch);

    let mut i = 0;
    let mut p = pivots.as_ptr();

    let offsets = table.offsets.as_slice();
    let aptr = a.as_ptr();

    let lanes = O::Vector::LANES;

    // Here is our conundrum. Unrolling is nice, but there is an issue if two adjacent chunks
    // have wildly different lengths.
    //
    // So here, we process two chunks at a time. We first tackle the common full-width prefix.
    // Then we process the tails independently.
    //
    // SAFETY INVARIANTS (SI)
    //
    // 1. `offsets.len() == nchunks + 1`. Offsets are strictly increasing with
    //    `offsets[0] == 0` and `offsets[nchunks] == a.len()` by the table invariant and
    //    Check 2.
    //
    // 2. At the start of each outer iteration, `p` points to the beginning of pivot storage
    //    for chunk `i`.
    //
    // 3. Pivot chunk `i` begins at `pivots.as_ptr().add(i * chunk_stride)`. Within a chunk,
    //    pivot `j` begins at `chunk_base.add(j * pivot_stride)`. Each chunk and pivot row
    //    contains `chunk_stride` and `pivot_stride` initialized elements, respectively.
    //
    // 4. Each code selects an existing pivot. Check 4 establishes this when the number of
    //    pivots fits in `u8`; otherwise every possible `u8` is less than
    //    `pivots_per_chunk`.
    //
    // 5. `pivot_stride` is a multiple of the table's SIMD width, and Check 1 establishes
    //    that width is a multiple of `lanes`. Each pivot row is at least as long as every
    //    unpadded chunk rounded up to `lanes`.
    while i + 2 <= nchunks {
        // SAFETY: `i + 2 <= nchunks`, so offsets `i` through `i + 2` exist by SI-1.
        let (o0, o1, o2) = unsafe {
            (
                *offsets.get_unchecked(i),
                *offsets.get_unchecked(i + 1),
                *offsets.get_unchecked(i + 2),
            )
        };

        // SAFETY: `o0` and `o1` are in-bounds chunk starts in `a` by SI-1.
        let (a0, a1) = unsafe { (aptr.add(o0), aptr.add(o1)) };

        // SAFETY: By SI-(2,3,4), `p` begins chunk `i` and the code selects an in-bounds
        // pivot row in that chunk.
        let b0 = unsafe { p.add(pivot_stride * (*b.get_unchecked(i) as usize)) };

        // SAFETY: `i + 1 < nchunks`; by SI-(2,3,4), this selects an in-bounds pivot row in
        // the following chunk.
        let b1 = unsafe { p.add(chunk_stride + pivot_stride * (*b.get_unchecked(i + 1) as usize)) };

        let full0 = (o1 - o0) / lanes;
        let full1 = (o2 - o1) / lanes;

        let common = full0.min(full1);
        for j in 0..common {
            // SAFETY: `j < full0` and `j < full1`, so both query loads end at or before
            // their respective chunk boundaries. By SI-(3,4,5), the corresponding pivot
            // loads remain within their initialized padded rows.
            unsafe {
                let va = O::Vector::load_simd(arch, a0.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b0.add(lanes * j));
                d0 = O::accum(d0, va, vb);

                let va = O::Vector::load_simd(arch, a1.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b1.add(lanes * j));
                d1 = O::accum(d1, va, vb);
            }
        }

        // Handle whatever happens to be left of `a0`.
        for j in common..full0 {
            // SAFETY: `j < full0`, so the query load remains within chunk 0. By SI-(3,4,5),
            // the corresponding pivot load remains within its padded row.
            unsafe {
                let va = O::Vector::load_simd(arch, a0.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b0.add(lanes * j));
                d0 = O::accum(d0, va, vb);
            }
        }

        let a0_remaining = (o1 - o0) - full0 * lanes;
        if a0_remaining != 0 {
            // SAFETY: `full0 * lanes + a0_remaining == o1 - o0` and `a0_remaining < lanes`,
            // so this reads exactly the initialized remainder.
            let va =
                unsafe { O::Vector::load_simd_first(arch, a0.add(lanes * full0), a0_remaining) };
            // SAFETY: `(full0 + 1) * lanes` is the chunk length rounded up to `lanes`,
            // which is at most `pivot_stride` by SI-5.
            let vb = unsafe { O::Vector::load_simd(arch, b0.add(lanes * full0)) };
            d0 = O::accum(d0, va, vb);
        }

        // Handle whatever happens to be left of `a1`.
        for j in common..full1 {
            // SAFETY: Same proof as the remaining full-vector loop for chunk 0.
            unsafe {
                let va = O::Vector::load_simd(arch, a1.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b1.add(lanes * j));
                d1 = O::accum(d1, va, vb);
            }
        }

        let a1_remaining = (o2 - o1) - full1 * lanes;
        if a1_remaining != 0 {
            // SAFETY: Same remainder proof as chunk 0.
            let va =
                unsafe { O::Vector::load_simd_first(arch, a1.add(lanes * full1), a1_remaining) };
            // SAFETY: Same padded-row proof as chunk 0.
            let vb = unsafe { O::Vector::load_simd(arch, b1.add(lanes * full1)) };
            d1 = O::accum(d1, va, vb);
        }

        i += 2;

        // SAFETY: Before incrementing, `p` began chunk `i - 2` by SI-2. The loop bound
        // established `i <= nchunks`, so advancing two chunk strides reaches chunk `i` or
        // one-past the final chunk, preserving SI-2.
        p = unsafe { p.add(2 * chunk_stride) };
    }

    if i < nchunks {
        debug_assert!(i + 1 == nchunks);

        // SAFETY: `i < nchunks`, so offsets `i` and `i + 1` exist by SI-1.
        let (o0, o1) = unsafe { (*offsets.get_unchecked(i), *offsets.get_unchecked(i + 1)) };

        // SAFETY: `o0` is an in-bounds chunk start in `a` by SI-1.
        let a0 = unsafe { aptr.add(o0) };

        // SAFETY: By SI-(2,3,4), this selects an in-bounds pivot row in chunk `i`.
        let b0 = unsafe { p.add(pivot_stride * (*b.get_unchecked(i) as usize)) };

        // The number of unprocessed elements.
        let full = (o1 - o0) / lanes;
        for j in 0..full {
            // SAFETY: `j < full`, so the query load remains within the chunk. By SI-(3,4,5),
            // the corresponding pivot load remains within its padded row.
            unsafe {
                let va = O::Vector::load_simd(arch, a0.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b0.add(lanes * j));
                d0 = O::accum(d0, va, vb);
            }
        }

        let remaining = (o1 - o0) - full * lanes;
        if remaining != 0 {
            // SAFETY: `full * lanes + remaining == o1 - o0` and `remaining < lanes`, so
            // this reads exactly the initialized remainder.
            let va = unsafe { O::Vector::load_simd_first(arch, a0.add(lanes * full), remaining) };
            // SAFETY: `(full + 1) * lanes` is at most `pivot_stride` by SI-5.
            let vb = unsafe { O::Vector::load_simd(arch, b0.add(lanes * full)) };
            d0 = O::accum(d0, va, vb);
        }
    }

    Ok(O::reduce_pair(d0, d1))
}

//-----------------------//
// Quant-Quant Distances //
//-----------------------//

/// Errors for [`VTable::self_distance`].
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum SelfDistanceError {
    #[error("table has {} chunks but codes slice has length {}", chunks, len)]
    Len { chunks: usize, len: usize },
    #[error("codes have a value that exceeds the number of pivots {}", ncenters)]
    OutOfBounds { ncenters: usize },
    #[error("An invalid SIMD width was chosen - ensure the correct vtable is used")]
    InvalidSimd,
}

#[inline(always)]
fn self_distance<O>(
    arch: O::Arch,
    table: &PaddedTable,
    a: &[u8],
    b: &[u8],
) -> Result<f32, SelfDistanceError>
where
    O: Op,
{
    // Check 1
    if !table
        .simd_width
        .as_nonzero()
        .get()
        .is_multiple_of(O::Vector::LANES)
    {
        return Err(SelfDistanceError::InvalidSimd);
    }

    let chunks = table.nchunks();
    // Check 2
    if a.len() != chunks {
        return Err(SelfDistanceError::Len {
            chunks,
            len: a.len(),
        });
    }

    // Check 3
    if b.len() != chunks {
        return Err(SelfDistanceError::Len {
            chunks,
            len: b.len(),
        });
    }

    // Check 4
    if let Ok(ncenters) = u8::try_from(table.ncenters()) {
        if let Some(max) = a.iter().max()
            && *max >= ncenters
        {
            return Err(SelfDistanceError::OutOfBounds {
                ncenters: ncenters.into(),
            });
        }

        if let Some(max) = b.iter().max()
            && *max >= ncenters
        {
            return Err(SelfDistanceError::OutOfBounds {
                ncenters: ncenters.into(),
            });
        }
    }

    // All checks passed - we're good to go!

    let pivots = &table.pivots;

    // The number of SIMD steps to process for each pivot.
    let steps = pivots.ncols() / O::Vector::LANES;

    let pivot_stride = pivots.ncols();
    let chunk_stride = table.pivots_per_chunk * pivot_stride;

    let len = a.len();
    let mut d0 = O::init(arch);
    let mut d1 = O::init(arch);

    let mut p = pivots.as_ptr();

    let lanes = O::Vector::LANES;
    let mut i = 0;

    // SAFETY INVARIANTS (SI)
    //
    // 1. Both code slices contain exactly `len == table.nchunks()` entries by Checks 2
    //    and 3.
    //
    // 2. At the start of each outer iteration, `p` points to the beginning of pivot storage
    //    for chunk `i`.
    //
    // 3. Pivot chunk `i` begins at `pivots.as_ptr().add(i * chunk_stride)`. Within a chunk,
    //    pivot `j` begins at `chunk_base.add(j * pivot_stride)`. Each chunk and pivot row
    //    contains `chunk_stride` and `pivot_stride` initialized elements, respectively.
    //
    // 4. Every code selects an existing pivot. Check 4 establishes this when the number of
    //    pivots fits in `u8`; otherwise every possible `u8` is in bounds.
    //
    // 5. `pivot_stride` is a multiple of the table's SIMD width, and Check 1 establishes
    //    that width is a multiple of `lanes`. Therefore
    //    `steps * lanes == pivot_stride`.
    while i + 2 <= len {
        // SAFETY: `i + 2 <= len`; by SI-(1,2,3,4), `p` begins chunk `i` and this code
        // selects an in-bounds pivot row in that chunk.
        let a0 = unsafe { p.add(pivot_stride * (*a.get_unchecked(i) as usize)) };

        // SAFETY: By the same invariants, this selects an in-bounds pivot row in chunk
        // `i + 1`.
        let a1 = unsafe { p.add(chunk_stride + pivot_stride * (*a.get_unchecked(i + 1) as usize)) };

        // SAFETY: Same proof as `a0`; SI-1 establishes that `b[i]` exists.
        let b0 = unsafe { p.add(pivot_stride * (*b.get_unchecked(i) as usize)) };
        // SAFETY: Same proof as `a1`.
        let b1 = unsafe { p.add(chunk_stride + pivot_stride * (*b.get_unchecked(i + 1) as usize)) };

        for j in 0..steps {
            // SAFETY: `j < steps` and `steps * lanes == pivot_stride` by SI-5, so every
            // full-vector load remains within its selected initialized pivot row.
            unsafe {
                // Unroll 0
                let va = O::Vector::load_simd(arch, a0.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b0.add(lanes * j));
                d0 = O::accum(d0, va, vb);

                // Unroll 1
                let va = O::Vector::load_simd(arch, a1.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b1.add(lanes * j));
                d1 = O::accum(d1, va, vb);
            }
        }

        i += 2;

        // SAFETY: Before incrementing, `p` began chunk `i - 2` by SI-2. The loop bound
        // established `i <= len`, so advancing two chunk strides reaches chunk `i` or
        // one-past the final chunk, preserving SI-2.
        p = unsafe { p.add(2 * chunk_stride) };
    }

    if i < len {
        debug_assert!(i + 1 == len);

        // SAFETY: By SI-(1,2,3,4), `p` begins chunk `i` and this selects an in-bounds
        // pivot row.
        let a0 = unsafe { p.add(pivot_stride * (*a.get_unchecked(i) as usize)) };
        // SAFETY: Same proof for `b[i]`.
        let b0 = unsafe { p.add(pivot_stride * (*b.get_unchecked(i) as usize)) };

        for j in 0..steps {
            // SAFETY: `j < steps`, so both loads remain within their initialized pivot rows
            // by SI-5.
            unsafe {
                let va = O::Vector::load_simd(arch, a0.add(lanes * j));
                let vb = O::Vector::load_simd(arch, b0.add(lanes * j));
                d0 = O::accum(d0, va, vb);
            }
        }
    }

    Ok(O::reduce_pair(d0, d1))
}

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

#[cfg(test)]
mod tests {
    use super::*;

    use diskann_utils::assert_contains;

    use crate::{
        product::tables::test::{self as table_test, DistanceTestTable, QueryLike, SelfLike},
        test_util::Check,
    };

    fn cases() -> &'static [Case] {
        const CASES: &[Case] = &[
            // chunk dimensions, pivots, start
            Case::new(&[1], 1, 0.0, Check::exact()),
            Case::new(&[1, 1, 1], 2, -1.0, Check::exact()),
            Case::new(&[3, 2, 2], 15, -7.0, Check::exact()),
            Case::new(&[2, 2, 2, 2], 16, -8.0, Check::exact()),
            Case::new(&[3, 3, 3, 2, 2], 17, -11.0, Check::exact()),
            Case::new(&[8, 8, 8, 8], 32, -16.0, Check::exact()),
            Case::new(&[6, 6, 5, 5, 5, 5, 5], 33, -20.0, Check::exact()),
            Case::new(&[13, 12, 12], 33, -20.0, Check::exact()),
            Case::new(&[4, 4, 3, 3, 3], 256, -128.0, Check::exact()),
            // Exercise sharply imbalanced chunks in either position of an unrolled pair,
            // followed by an odd chunk that must use the remainder loop.
            Case::new(&[1, 23, 23], 17, -9.0, Check::exact()),
            Case::new(&[23, 1, 23], 17, -9.0, Check::exact()),
            Case::new(&[1, 17, 2, 9, 3], 17, -9.0, Check::exact()),
        ];
        CASES
    }

    #[derive(Debug, Clone, Copy)]
    struct Case {
        chunk_dims: &'static [usize],
        pivots: usize,
        start: f32,
        check: Check,
    }

    impl Case {
        const fn new(
            chunk_dims: &'static [usize],
            pivots: usize,
            start: f32,
            check: Check,
        ) -> Self {
            Self {
                chunk_dims,
                pivots,
                start,
                check,
            }
        }
    }

    fn run_test(
        cases: &[Case],
        metric: Metric,
        arch: Option<RuntimeArch>,
        reference: &dyn Fn(&[f32], &[f32]) -> f32,
        ctx: &dyn std::fmt::Display,
    ) {
        let (num_queries, num_trials) = if cfg!(miri) {
            // The driver will run some directed tests even if there are no regular random
            // trials.
            (1, 0)
        } else {
            (10, 10)
        };

        #[derive(Debug)]
        struct Dut<'a> {
            table: &'a PaddedTable,
            query: Vec<f32>,
            vtable: VTable,
        }

        impl QueryLike for Dut<'_> {
            fn preprocess(&mut self, query: &[f32]) {
                self.query.clear();
                self.query.extend_from_slice(query);
            }

            fn evaluate(&mut self, code: &[u8]) -> f32 {
                self.vtable.distance(self.table, &self.query, code).unwrap()
            }
        }

        impl SelfLike for Dut<'_> {
            fn evaluate(&mut self, a: &[u8], b: &[u8]) -> f32 {
                self.vtable.self_distance(self.table, a, b).unwrap()
            }
        }

        for Case {
            chunk_dims,
            pivots,
            start,
            check,
        } in cases.iter().copied()
        {
            let driver = DistanceTestTable::from_chunk_dims(chunk_dims, pivots, start);
            let dim = driver.dim();
            let chunks = driver.chunks();
            let basic = driver.basic_table();

            let table = match arch {
                Some(arch) => PaddedTable::from_basic_with(basic.as_view(), arch),
                None => PaddedTable::from_basic(basic.as_view()),
            };

            let vtable = table.vtable(metric);

            let mut dut = Dut {
                table: &table,
                query: Vec::new(),
                vtable,
            };

            driver.drive_query_like(
                num_queries,
                num_trials,
                &mut driver.rng(0xc0ffee),
                check,
                reference,
                &mut dut,
                format_args!(
                    "[{}] padded table - dim = {}, chunks = {}, pivots = {}",
                    ctx, dim, chunks, pivots
                ),
            );

            driver.drive_self_like(
                num_trials,
                &mut driver.rng(0xc0ffee),
                check,
                reference,
                &mut dut,
                format_args!(
                    "[{}] padded table - dim = {}, chunks = {}, pivots = {}",
                    ctx, dim, chunks, pivots
                ),
            );
        }
    }

    // L2 - query-like
    #[test]
    fn test_l2_query_like() {
        run_test(
            cases(),
            Metric::SquaredL2,
            None,
            &table_test::squared_l2,
            &"squared l2 full x quant - auto-detect",
        );

        run_test(
            cases(),
            Metric::SquaredL2,
            Some(RuntimeArch::Scalar(Scalar::new())),
            &table_test::squared_l2,
            &"squared l2 full x quant - scalar",
        );

        #[cfg(target_arch = "x86_64")]
        if let Some(arch) = V3::new_checked() {
            run_test(
                cases(),
                Metric::SquaredL2,
                Some(RuntimeArch::V3(arch)),
                &table_test::squared_l2,
                &"squared l2 full x quant - V3",
            );
        }

        #[cfg(target_arch = "aarch64")]
        if let Some(arch) = Neon::new_checked() {
            run_test(
                cases(),
                Metric::SquaredL2,
                Some(RuntimeArch::Neon(arch)),
                &table_test::squared_l2,
                &"squared l2 full x quant - V3",
            );
        }
    }

    // Inner Product - query-like
    #[test]
    fn test_inner_product_query_like() {
        run_test(
            cases(),
            Metric::InnerProduct,
            None,
            &table_test::inner_product,
            &"inner-product full x quant - auto-detect",
        );

        run_test(
            cases(),
            Metric::InnerProduct,
            Some(RuntimeArch::Scalar(Scalar::new())),
            &table_test::inner_product,
            &"inner-product full x quant - scalar",
        );

        #[cfg(target_arch = "x86_64")]
        if let Some(arch) = V3::new_checked() {
            run_test(
                cases(),
                Metric::InnerProduct,
                Some(RuntimeArch::V3(arch)),
                &table_test::inner_product,
                &"inner-product full x quant - V3",
            );
        }

        #[cfg(target_arch = "aarch64")]
        if let Some(arch) = Neon::new_checked() {
            run_test(
                cases(),
                Metric::InnerProduct,
                Some(RuntimeArch::Neon(arch)),
                &table_test::inner_product,
                &"inner-product full x quant - V3",
            );
        }
    }

    // Cosine - query-like
    #[test]
    fn test_cosine_query_like() {
        run_test(
            cases(),
            Metric::Cosine,
            None,
            &table_test::cosine,
            &"cosine full x quant - auto-detect",
        );

        run_test(
            cases(),
            Metric::Cosine,
            Some(RuntimeArch::Scalar(Scalar::new())),
            &table_test::cosine,
            &"cosine full x quant - scalar",
        );

        #[cfg(target_arch = "x86_64")]
        if let Some(arch) = V3::new_checked() {
            run_test(
                cases(),
                Metric::Cosine,
                Some(RuntimeArch::V3(arch)),
                &table_test::cosine,
                &"cosine full x quant - V3",
            );
        }

        #[cfg(target_arch = "aarch64")]
        if let Some(arch) = Neon::new_checked() {
            run_test(
                cases(),
                Metric::Cosine,
                Some(RuntimeArch::Neon(arch)),
                &table_test::cosine,
                &"cosine full x quant - V3",
            );
        }
    }

    ////////////
    // Errors //
    ////////////

    /// A table with dimension 7, 3 chunks, and 3 pivots per chunk.
    fn error_table() -> (PaddedTable, VTable) {
        let driver = DistanceTestTable::new(7, 3, 3, 0.0);
        let basic = driver.basic_table();
        let table =
            PaddedTable::from_basic_with(basic.as_view(), RuntimeArch::Scalar(Scalar::new()));
        let vtable = table.vtable(Metric::SquaredL2);
        (table, vtable)
    }

    #[test]
    fn test_distance_errors() {
        let (table, vtable) = error_table();
        let vector = vec![0.0; table.dim()];
        let codes = vec![0; table.nchunks()];

        for len in [table.dim() - 1, table.dim() + 1] {
            let invalid = vec![0.0; len];
            let err = vtable.distance(&table, &invalid, &codes).unwrap_err();
            assert_contains!(
                err.to_string(),
                format!(
                    "table has dimension {} but full vector has length {len}",
                    table.dim()
                )
            );
        }

        for len in [table.nchunks() - 1, table.nchunks() + 1] {
            let invalid = vec![0; len];
            let err = vtable.distance(&table, &vector, &invalid).unwrap_err();
            assert_contains!(
                err.to_string(),
                format!(
                    "table has {} chunks but codes slice has length {len}",
                    table.nchunks()
                )
            );
        }

        let mut invalid = codes;
        invalid[1] = u8::try_from(table.ncenters()).unwrap();
        let err = vtable.distance(&table, &vector, &invalid).unwrap_err();
        assert_contains!(
            err.to_string(),
            format!(
                "codes have a value that exceeds the number of pivots {}",
                table.ncenters()
            )
        );
    }

    #[test]
    fn test_self_distance_errors() {
        let (table, vtable) = error_table();
        let codes = vec![0; table.nchunks()];

        for len in [table.nchunks() - 1, table.nchunks() + 1] {
            let invalid = vec![0; len];

            let err = vtable.self_distance(&table, &invalid, &codes).unwrap_err();
            assert_contains!(
                err.to_string(),
                format!(
                    "table has {} chunks but codes slice has length {len}",
                    table.nchunks()
                )
            );

            let err = vtable.self_distance(&table, &codes, &invalid).unwrap_err();
            assert_contains!(
                err.to_string(),
                format!(
                    "table has {} chunks but codes slice has length {len}",
                    table.nchunks()
                )
            );
        }

        for invalid_operand in 0..2 {
            let mut a = codes.clone();
            let mut b = codes.clone();
            let invalid = if invalid_operand == 0 { &mut a } else { &mut b };
            invalid[1] = u8::try_from(table.ncenters()).unwrap();

            let err = vtable.self_distance(&table, &a, &b).unwrap_err();
            assert_contains!(
                err.to_string(),
                format!(
                    "codes have a value that exceeds the number of pivots {}",
                    table.ncenters()
                )
            );
        }
    }

    #[test]
    fn test_mismatched_simd_width_errors() {
        diskann_wide::alias!(f32x8<A> = f32x8);

        let (table, _) = error_table();
        let invalid_vtable = VTable::new::<SquaredL2<f32x8<Scalar>>>(Scalar::new());
        let vector = vec![0.0; table.dim()];
        let codes = vec![0; table.nchunks()];

        let err = invalid_vtable
            .distance(&table, &vector, &codes)
            .unwrap_err();
        assert_contains!(err.to_string(), "An invalid SIMD width was chosen");

        let err = invalid_vtable
            .self_distance(&table, &codes, &codes)
            .unwrap_err();
        assert_contains!(err.to_string(), "An invalid SIMD width was chosen");
    }
}