diskann-quantization 0.51.0

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
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
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

//! # Spherical Quantization Distance Functions
//!
//! ## Inner Product - 1-bit Symmetric
//!
//! Key:
//! * `X'` (Upper case with "prime"): The original, full-precision vectors.
//! * `C`: The dataset centroid.
//! * `X` (Upper case, no "prime"): The result of `X' - C`. That is, the centered vectors.
//! * `|X|`: The L2 norm of a vector.
//! * `x` (Lower case): The normalized version of a vector `X` respectively.
//! * `x'`: The quantized reconstruction of `x`, computed as `x = T(x!)` where
//!
//!   - `x!` is the binary encoded vector in `{-1/sqrt(dim), +1/sqrt(dim)}^dim`.
//!   - `x -> T(x)` is the distance-preserving transformation.
//!
//! ```math
//! <X', Y'> = <X + C, Y + C>
//!          = <X, Y> + <X, C> + <Y, C> + <C, C>
//!          = |X| |Y| <x, y> + <X, C> + <Y, C> + |C|^2
//!                    ------
//!                      |
//!                 Normalized
//!                 Components
//! ```
//!
//! Now, working with the normalized components:
//! ```math
//! <x, y> \approx <x', y'> / (<x', x> * <y', y>)         [From the RabitQ Paper]
//!                            -------   -------
//!                               |         |
//!                            Self Dot  Self Dot
//!                            Product    Product
//! ```
//! Where `x'` and `y'` are transformed vectors in the domain `{-1/sqrt(D), 1/sqrt(D)}^D`.
//!
//! This is the result from the RabitQ paper (though modified to work on two symmetrically
//! compressed vectors).
//!
//! NOTE: The symmetric correction factor gives incorrect estimates for estimating the
//! distance between a vector and itself because the term `<x', x>` is strictly less
//! than one, bringing the estimate for the inner product `<x, x>` to a value greater than 1.
//! In practice, this still yields better recall (both exhaustive and via graph build) than
//! no correction, so we keep it.
//!
//! Finally, to compute the inner product `<x', y'>` we use the following general approach:
//! ```math
//! <x', y'> = <a * (bx + b), c * (by + d)>
//!          = (a * b) ( <bx, by> + b*sum(by) + d*sum(bx) + b*d )
//!            -------   --------   - -------   - -------
//!               |         |       |    |      |    |
//!            Scaling      |       | Bit Sum   | Bit Sum
//!             Terms       |       |           |
//!                         |       |        y offset
//!                    Bit Inner    |
//!                     Product     |
//!                              x offset
//! ```
//!
//! When the vectors `x` and `y` use the same scaling or offset terms, some of this
//! computation cam be simplified. However, spherical quantization allows queries to use
//! a different compression (i.e., scalar quantization) and so this term reflects the
//! general strategy.
//!
//! Thus, for each vector `X`, we need the following compensation values:
//!
//! 1. `|X| * a / <x', x>`: The norm of `X'` after it has been shifted by the centroid
//!    multiplied by the quantization scaling parameter, divided by the correction term.
//!    This whole expression is multiplied to the the result of the inner product term to
//!    obtain the full-norm estimate of the shifted inner product.
//!
//! 2. `<X, C>`: The inner product between the shifted vector and the centroid.
//!
//! 3. `sum(bx)`: The sum of the bits in the binary vector representation of `x'`.
//!
//! 4. `|X|`: The norm of the shifted vector - used to computed L2 distances.
//!
//! ## Squared L2 - 1-bit Symmetric
//!
//! ```math
//! |X' - Y'| = | (X' - C) - (Y' - C) |
//!           = | X - Y |
//!           = |X|^2 + |Y|^2 - 2 <X, Y>
//!           = |X|^2 + |Y|^2 - 2 |X| |Y| <x, y>
//!                                       ------
//!                                         |
//!                              Reuse from Inner Product
//! ```
//!
//! The compensation terms used here are the same as the same.
//!
//! # Full Precision Queries
//!
//! When the vector `Y` is full-precision, the expression for the inner product becomes
//! ```math
//! <a(X + b), Y> = a (<X, Y> + b * sum(Y))
//! ```
//!
//! # Dev Notes
//!
//! The functions implemented here use the [`diskann_wide::arch::Target2`] interface to
//! propagate micro-architecture defails from the caller.
//!
//! When calling implementations in [`crate::bits::distances]`, be sure to use
//! [`diskann_wide::Architecture::run2`] instead to invoke the distance functions. This will
//! architecture specific
//! [target features](https://rust-lang.github.io/rfcs/2045-target-feature.html) are
//! inhereted properly, even if these functions are not inlined.

use diskann_utils::{Reborrow, ReborrowMut};
use diskann_vector::{Norm, norm::FastL2NormSquared};
use diskann_wide::{Architecture, arch::Target2};
use half::f16;
use thiserror::Error;

#[cfg(feature = "flatbuffers")]
use crate::flatbuffers as fb;
use crate::{
    alloc::{AllocatorCore, AllocatorError, Poly},
    bits::{BitSlice, Dense, PermutationStrategy, Representation, Unsigned},
    distances::{self, InnerProduct, MV},
    meta,
};

//////////////////////
// Supported Metric //
//////////////////////

/// The metrics that are supported by [`crate::spherical::SphericalQuantizer`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SupportedMetric {
    SquaredL2,
    InnerProduct,
    Cosine,
}

#[cfg(test)]
impl SupportedMetric {
    fn pick(self, shifted_norm: f32, inner_product_with_centroid: f32) -> f32 {
        match self {
            Self::SquaredL2 => shifted_norm * shifted_norm,
            Self::InnerProduct | Self::Cosine => inner_product_with_centroid,
        }
    }

    #[cfg(feature = "flatbuffers")]
    pub(super) fn all() -> [Self; 3] {
        [Self::SquaredL2, Self::InnerProduct, Self::Cosine]
    }
}

impl TryFrom<diskann_vector::distance::Metric> for SupportedMetric {
    type Error = UnsupportedMetric;
    fn try_from(metric: diskann_vector::distance::Metric) -> Result<Self, Self::Error> {
        use diskann_vector::distance::Metric;
        match metric {
            Metric::L2 => Ok(Self::SquaredL2),
            Metric::InnerProduct => Ok(Self::InnerProduct),
            Metric::Cosine => Ok(Self::Cosine),
            unsupported => Err(UnsupportedMetric(unsupported)),
        }
    }
}

impl PartialEq<diskann_vector::distance::Metric> for SupportedMetric {
    fn eq(&self, metric: &diskann_vector::distance::Metric) -> bool {
        match Self::try_from(*metric) {
            Ok(m) => *self == m,
            Err(_) => false,
        }
    }
}

#[derive(Debug, Clone, Copy, Error)]
#[error("metric {0:?} is not supported for spherical quantization")]
pub struct UnsupportedMetric(pub(crate) diskann_vector::distance::Metric);

#[cfg(feature = "flatbuffers")]
#[cfg_attr(docsrs, doc(cfg(feature = "flatbuffers")))]
#[derive(Debug, Clone, Copy, PartialEq, Error)]
#[error("the value {0} is not recognized as a supported metric")]
pub struct InvalidMetric(i8);

#[cfg(feature = "flatbuffers")]
#[cfg_attr(docsrs, doc(cfg(feature = "flatbuffers")))]
impl TryFrom<fb::spherical::SupportedMetric> for SupportedMetric {
    type Error = InvalidMetric;
    fn try_from(value: fb::spherical::SupportedMetric) -> Result<Self, Self::Error> {
        match value {
            fb::spherical::SupportedMetric::SquaredL2 => Ok(Self::SquaredL2),
            fb::spherical::SupportedMetric::InnerProduct => Ok(Self::InnerProduct),
            fb::spherical::SupportedMetric::Cosine => Ok(Self::Cosine),
            unsupported => Err(InvalidMetric(unsupported.0)),
        }
    }
}

#[cfg(feature = "flatbuffers")]
#[cfg_attr(docsrs, doc(cfg(feature = "flatbuffers")))]
impl From<SupportedMetric> for fb::spherical::SupportedMetric {
    fn from(value: SupportedMetric) -> Self {
        match value {
            SupportedMetric::SquaredL2 => fb::spherical::SupportedMetric::SquaredL2,
            SupportedMetric::InnerProduct => fb::spherical::SupportedMetric::InnerProduct,
            SupportedMetric::Cosine => fb::spherical::SupportedMetric::Cosine,
        }
    }
}

//////////
// Data //
//////////

/// Metadata for correcting quantization for computing distances among quant vectors.
#[derive(Debug, Default, Clone, Copy, PartialEq, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub struct DataMeta {
    /// This is the whole term
    /// ```math
    /// |X| * a / <x', x>
    /// ```
    /// and represents the entires correction factor for computing inner products on the
    /// representation
    /// ```math
    /// bx + b
    /// ```
    /// where `bx` is unsigned binary encoding of the vector and `b` (obtained from
    /// `Self::offset_term`) is the compression offset.
    pub inner_product_correction: f16,

    /// A metric-specific correction term. Refer to the module level documentation to
    /// understand the implication of the terms outlined here.
    ///
    /// | Squared L2    |  `|X|^2`  |
    /// | Inner Product | `<X', C>` |
    pub metric_specific: f16,

    /// Two times the sum of the ones in the binary representation of the transformed
    /// unit vector.
    ///
    /// This is the term `sum(bx)` in the module level documentation.
    pub bit_sum: u16,
}

#[derive(Debug, Error, Clone, Copy, PartialEq)]
pub enum DataMetaError {
    #[error("inner product correction {value} cannot fit in a 16-bit floating point number")]
    InnerProductCorrection { value: f32 },

    #[error("metric specific correction {value} cannot fit in a 16-bit floating point number")]
    MetricSpecific { value: f32 },

    #[error("bit sum {value} cannot fit in a 16-bit unsigned integer")]
    BitSum { value: u32 },
}

impl DataMeta {
    /// Construct a new metadata from components.
    ///
    /// This will internally convert the `f32` values to `f16`.
    pub fn new(
        inner_product_correction: f32,
        metric_specific: f32,
        bit_sum: u32,
    ) -> Result<Self, DataMetaError> {
        let inner_product_correction_f16 = diskann_wide::cast_f32_to_f16(inner_product_correction);
        if !inner_product_correction_f16.is_finite() {
            return Err(DataMetaError::InnerProductCorrection {
                value: inner_product_correction,
            });
        }

        let metric_specific_f16 = diskann_wide::cast_f32_to_f16(metric_specific);
        if !metric_specific_f16.is_finite() {
            return Err(DataMetaError::MetricSpecific {
                value: metric_specific,
            });
        }

        let bit_sum_u16: u16 = bit_sum
            .try_into()
            .map_err(|_| DataMetaError::BitSum { value: bit_sum })?;

        Ok(Self {
            inner_product_correction: inner_product_correction_f16,
            metric_specific: metric_specific_f16,
            bit_sum: bit_sum_u16,
        })
    }

    /// Compute the term `b` for a binary compression of a vector so the reconstruction can
    /// be expressed as
    /// ```math
    /// a (bx + b)
    /// ```
    /// where
    ///
    /// * `a` is the scaling term to achieve the correct dynamic range.
    /// * `bx` is the unsigned binary encoded vector.
    ///
    /// This value is computed as
    /// ```math
    /// 2 ^ NBITS - 1
    /// -------------
    ///      2
    /// ```
    /// and ensures equal coverage above and below 0.
    const fn offset_term<const NBITS: usize>() -> f32 {
        ((2usize).pow(NBITS as u32) as f32 - 1.0) / 2.0
    }

    /// Convert the values in `self` to their full precision representation for computation.
    #[inline(always)]
    pub fn to_full<A>(self, arch: A) -> DataMetaF32
    where
        A: Architecture,
    {
        use diskann_wide::SIMDVector;

        // Relying on `diskann_wide::cast_f16_to_f32` to correctly propagation `target_features`
        // correction does not seem to completely work.
        //
        // We take matters into our own hand and use the architecture's conversion routines
        // directly.
        let pre = [
            self.metric_specific,
            self.inner_product_correction,
            half::f16::default(),
            half::f16::default(),
            half::f16::default(),
            half::f16::default(),
            half::f16::default(),
            half::f16::default(),
        ];

        let post: <A as Architecture>::f32x8 =
            <A as Architecture>::f16x8::from_array(arch, pre).into();
        let post = post.to_array();

        DataMetaF32 {
            metric_specific: post[0],
            inner_product_correction: post[1],
            bit_sum: self.bit_sum.into(),
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub struct DataMetaF32 {
    pub inner_product_correction: f32,
    pub metric_specific: f32,
    pub bit_sum: f32,
}

/// A borrowed `ComptensatedVector`.
pub type DataRef<'a, const NBITS: usize> = meta::VectorRef<'a, NBITS, Unsigned, DataMeta>;

/// A mutably borrowed `ComptensatedVector`.
pub type DataMut<'a, const NBITS: usize> = meta::VectorMut<'a, NBITS, Unsigned, DataMeta>;

/// An owning data vector.
pub type Data<const NBITS: usize, A> = meta::PolyVector<NBITS, Unsigned, DataMeta, Dense, A>;

///////////
// Query //
///////////

/// Scalar quantization correction factors for computing distances between scalar quantized
/// queries and spherically quantized data elements.
///
/// Computing the distance between a query and a data vector uses the same forumla derived
/// in the module level documentation.
///
/// The one difference is that the query must explicitly carry the "offset" term as it
/// cannot be derived from the number of bits used for the compression.
#[derive(Copy, Clone, Default, Debug, PartialEq, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub struct QueryMeta {
    /// The value with which to scale the bit-level inner product with the 1-bit data data
    /// vectors.
    pub inner_product_correction: f32,

    /// Scaling factor for the `DataMeta::twice_contraction`. Applied separately to
    /// still allow 1-bit vectors using `DataMeta` to compute distances with eachother
    /// efficiently.
    pub bit_sum: f32,

    /// The query-specific offset, taking into account the scaling factor for the query as
    /// well as its minimum value. See the struct-level documentation for an explanation.
    pub offset: f32,

    /// The corresponding metric specific term as [`DataMeta`].
    pub metric_specific: f32,
}

/// A specialized type for computing higher-precision inner products with data vectors.
pub type Query<const NBITS: usize, Perm, A> = meta::PolyVector<NBITS, Unsigned, QueryMeta, Perm, A>;

/// A reference-like version of `Query`.
pub type QueryRef<'a, const NBITS: usize, Perm> =
    meta::VectorRef<'a, NBITS, Unsigned, QueryMeta, Perm>;

/// A mutable reference-like version of `Query`.
pub type QueryMut<'a, const NBITS: usize, Perm> =
    meta::VectorMut<'a, NBITS, Unsigned, QueryMeta, Perm>;

////////////////////
// Full Precision //
////////////////////

#[derive(Debug, Clone, Copy, Default, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub struct FullQueryMeta {
    /// The sum of `data`.
    pub sum: f32,
    /// The norm of the shifted vector.
    pub shifted_norm: f32,
    /// Metric specific correction term. See [`DataMeta`].
    pub metric_specific: f32,
}

/// A full-precision query.
#[derive(Debug)]
pub struct FullQuery<A>
where
    A: AllocatorCore,
{
    /// The data after centering, normalization, and transformation.
    pub data: Poly<[f32], A>,
    pub meta: FullQueryMeta,
}

impl<A> FullQuery<A>
where
    A: AllocatorCore,
{
    /// Construct an empty `FullQuery` for `dim` dimensional data.
    pub fn empty(dim: usize, allocator: A) -> Result<Self, AllocatorError> {
        Ok(Self {
            data: Poly::broadcast(0.0f32, dim, allocator)?,
            meta: Default::default(),
        })
    }
}

pub type FullQueryRef<'a> = meta::slice::SliceRef<'a, f32, FullQueryMeta>;

pub type FullQueryMut<'a> = meta::slice::SliceMut<'a, f32, FullQueryMeta>;

impl<'short, A> Reborrow<'short> for FullQuery<A>
where
    A: AllocatorCore,
{
    type Target = FullQueryRef<'short>;
    fn reborrow(&'short self) -> Self::Target {
        FullQueryRef::new(&self.data, &self.meta)
    }
}

impl<'short, A> ReborrowMut<'short> for FullQuery<A>
where
    A: AllocatorCore,
{
    type Target = FullQueryMut<'short>;
    fn reborrow_mut(&'short mut self) -> Self::Target {
        FullQueryMut::new(&mut self.data, &mut self.meta)
    }
}

/////////////
// Helpers //
/////////////

/// This is a workaround to the error `Can't use generic parameters from outer function.` by
/// forcing constant evaluation of expressions involving offset terms.
struct ConstOffset<const NBITS: usize>;

impl<const NBITS: usize> ConstOffset<NBITS> {
    const OFFSET: f32 = DataMeta::offset_term::<NBITS>();
    const OFFSET_SQUARED: f32 = DataMeta::offset_term::<NBITS>() * DataMeta::offset_term::<NBITS>();
}

/// This represents the computation
/// ```math
/// |X'| |Y'| <x, y>
/// ```
/// from the module-level docstring.
#[inline(always)]
fn kernel<A, const NBITS: usize>(
    arch: A,
    x: DataRef<'_, NBITS>,
    y: DataRef<'_, NBITS>,
    dim: f32,
) -> distances::Result<f32>
where
    A: Architecture,
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a> Target2<
            A,
            distances::MathematicalResult<u32>,
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'a, NBITS, Unsigned>,
        >,
{
    // NOTE: `Target2<_, _, _, _>` is used instead of `Architecture::run2` to ensure that
    // the kernel is inlined into this callsize.
    //
    // Even using `Architecture::run2_inline` is not sufficient to guarantee inlining.
    let ip: distances::MathematicalResult<u32> =
        <_ as Target2<_, _, _, _>>::run(InnerProduct, arch, x.vector(), y.vector());

    let ip = ip?.into_inner() as f32;

    let offset = ConstOffset::<NBITS>::OFFSET;
    let offset_squared = ConstOffset::<NBITS>::OFFSET_SQUARED;

    let xc = x.meta().to_full(arch);
    let yc = y.meta().to_full(arch);

    Ok(xc.inner_product_correction
        * yc.inner_product_correction
        * (ip - offset * (xc.bit_sum + yc.bit_sum) + offset_squared * dim))
}

////////////////////////////
// Compensated Squared L2 //
////////////////////////////

/// A `DistanceFunction` containing scaling parameters to enable distance the SquaredL2
/// distance function over `CompensatedVectors` belonging to the same quantization space.
#[derive(Debug, Clone, Copy)]
pub struct CompensatedSquaredL2 {
    pub(super) dim: f32,
}

impl CompensatedSquaredL2 {
    /// Construct a new `CompensatedSquaredL2` with the given scaling factor.
    pub fn new(dim: usize) -> Self {
        Self { dim: dim as f32 }
    }
}

/// A blanket implementation for applying the identity transformation from
/// `MathematicalValue` to `f32` for Euclidean distance computations.
impl<A, T, U> Target2<A, distances::Result<f32>, T, U> for CompensatedSquaredL2
where
    A: Architecture,
    Self: Target2<A, distances::MathematicalResult<f32>, T, U>,
{
    #[inline(always)]
    fn run(self, arch: A, x: T, y: U) -> distances::Result<f32> {
        self.run(arch, x, y).map(|r| r.into_inner())
    }
}

/// Compute the squared euclidean distance between the two compensated vectors.
///
/// The value returned by this function is scaled properly, meaning that distances returned
/// by this method are compatible with full-precision distances.
///
/// # Validity
///
/// The results of this function are only meaningful if both `x`, `y`, and `Self` belong to
/// the same quantizer.
impl<A, const NBITS: usize>
    Target2<A, distances::MathematicalResult<f32>, DataRef<'_, NBITS>, DataRef<'_, NBITS>>
    for CompensatedSquaredL2
where
    A: Architecture,
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a> Target2<
            A,
            distances::MathematicalResult<u32>,
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'a, NBITS, Unsigned>,
        >,
{
    #[inline(always)]
    fn run(
        self,
        arch: A,
        x: DataRef<'_, NBITS>,
        y: DataRef<'_, NBITS>,
    ) -> distances::MathematicalResult<f32> {
        let xc = x.meta().to_full(arch);
        let yc = y.meta().to_full(arch);
        let result = xc.metric_specific + yc.metric_specific - 2.0 * kernel(arch, x, y, self.dim)?;
        Ok(MV::new(result))
    }
}

impl<A, const Q: usize, const D: usize, Perm>
    Target2<A, distances::MathematicalResult<f32>, QueryRef<'_, Q, Perm>, DataRef<'_, D>>
    for CompensatedSquaredL2
where
    A: Architecture,
    Unsigned: Representation<Q>,
    Unsigned: Representation<D>,
    Perm: PermutationStrategy<Q>,
    for<'a> InnerProduct: Target2<
            A,
            distances::MathematicalResult<u32>,
            BitSlice<'a, Q, Unsigned, Perm>,
            BitSlice<'a, D, Unsigned>,
        >,
{
    #[inline(always)]
    fn run(
        self,
        arch: A,
        x: QueryRef<'_, Q, Perm>,
        y: DataRef<'_, D>,
    ) -> distances::MathematicalResult<f32> {
        let ip: distances::MathematicalResult<u32> =
            arch.run2_inline(InnerProduct, x.vector(), y.vector());
        let ip = ip?.into_inner() as f32;

        let yc = y.meta().to_full(arch);
        let xc = x.meta();

        let y_offset: f32 = DataMeta::offset_term::<D>();

        let corrected_ip = yc.inner_product_correction
            * xc.inner_product_correction
            * (ip - y_offset * xc.bit_sum + xc.offset * yc.bit_sum
                - y_offset * xc.offset * self.dim);

        Ok(MV::new(
            yc.metric_specific + xc.metric_specific - 2.0 * corrected_ip,
        ))
    }
}

/// Compute the inner product between a full-precision query and a spherically quantized
/// data vector.
///
/// Returns an error if the arguments have different lengths.
impl<A, const NBITS: usize>
    Target2<A, distances::MathematicalResult<f32>, FullQueryRef<'_>, DataRef<'_, NBITS>>
    for CompensatedSquaredL2
where
    A: Architecture,
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a> Target2<
            A,
            distances::MathematicalResult<f32>,
            &'a [f32],
            BitSlice<'a, NBITS, Unsigned>,
        >,
{
    #[inline(always)]
    fn run(
        self,
        arch: A,
        x: FullQueryRef<'_>,
        y: DataRef<'_, NBITS>,
    ) -> distances::MathematicalResult<f32> {
        let s = arch
            .run2(InnerProduct, x.vector(), y.vector())?
            .into_inner();

        let xc = x.meta();
        let yc = y.meta().to_full(arch);

        let offset = ConstOffset::<NBITS>::OFFSET;
        let ip = s - xc.sum * offset;

        // NOTE: `xc.metric_specific` already carries the square norm, so we can save
        // a multiple by using it directly.
        let r = xc.metric_specific + yc.metric_specific
            - 2.0 * xc.shifted_norm * yc.inner_product_correction * ip;
        Ok(MV::new(r))
    }
}

////////////////////
// Compensated IP //
////////////////////

/// A `DistanceFunction` containing scaling parameters to enable distance the SquaredL2
/// distance function over `CompensatedVectors` belonging to the same quantization space.
#[derive(Debug, Clone, Copy)]
pub struct CompensatedIP {
    pub(super) squared_shift_norm: f32,
    pub(super) dim: f32,
}

impl CompensatedIP {
    /// Construct a new `CompensatedIP` with the given scaling factor and shift norm.
    pub fn new(shift: &[f32], dim: usize) -> Self {
        Self {
            squared_shift_norm: FastL2NormSquared.evaluate(shift),
            dim: dim as f32,
        }
    }
}

/// A blanket implementation for applying the negating transformation
/// ```text
/// x -> -x
/// ```
/// from `MathematicalValue` to `f32` for inner product distance computations.
impl<A, T, U> Target2<A, distances::Result<f32>, T, U> for CompensatedIP
where
    A: Architecture,
    Self: Target2<A, distances::MathematicalResult<f32>, T, U>,
{
    #[inline(always)]
    fn run(self, arch: A, x: T, y: U) -> distances::Result<f32> {
        arch.run2(self, x, y).map(|r| -r.into_inner())
    }
}

/// Compute the inner product between the two compensated vectors.
///
/// Returns an error if the arguments have different lengths.
///
/// The value returned by this function is scaled properly, meaning that distances returned
/// by this method are compatible with full-precision computations.
///
/// # Validity
///
/// The results of this function are only meaningful if both `x`, `y`, and `Self` belong to
/// the same quantizer.
impl<A, const NBITS: usize>
    Target2<A, distances::MathematicalResult<f32>, DataRef<'_, NBITS>, DataRef<'_, NBITS>>
    for CompensatedIP
where
    A: Architecture,
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a> Target2<
            A,
            distances::MathematicalResult<u32>,
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'a, NBITS, Unsigned>,
        >,
{
    #[inline(always)]
    fn run(
        self,
        arch: A,
        x: DataRef<'_, NBITS>,
        y: DataRef<'_, NBITS>,
    ) -> distances::MathematicalResult<f32> {
        let xc = x.meta().to_full(arch);
        let yc = y.meta().to_full(arch);

        let result = xc.metric_specific
            + yc.metric_specific
            + kernel(arch, x, y, self.dim)?
            + self.squared_shift_norm;
        Ok(MV::new(result))
    }
}

impl<A, const Q: usize, const D: usize, Perm>
    Target2<A, distances::MathematicalResult<f32>, QueryRef<'_, Q, Perm>, DataRef<'_, D>>
    for CompensatedIP
where
    A: Architecture,
    Unsigned: Representation<Q>,
    Unsigned: Representation<D>,
    Perm: PermutationStrategy<Q>,
    for<'a> InnerProduct: Target2<
            A,
            distances::MathematicalResult<u32>,
            BitSlice<'a, Q, Unsigned, Perm>,
            BitSlice<'a, D, Unsigned>,
        >,
{
    #[inline(always)]
    fn run(
        self,
        arch: A,
        x: QueryRef<'_, Q, Perm>,
        y: DataRef<'_, D>,
    ) -> distances::MathematicalResult<f32> {
        // The inner product of the bit-level data.
        let ip: MV<u32> = arch.run2_inline(InnerProduct, x.vector(), y.vector())?;

        let yc = y.meta().to_full(arch);
        let xc = x.meta();

        // Rely on constant propagation to pre-compute these terms.
        let y_offset: f32 = DataMeta::offset_term::<D>();

        let corrected_ip = xc.inner_product_correction
            * yc.inner_product_correction
            * (ip.into_inner() as f32 - y_offset * xc.bit_sum + xc.offset * yc.bit_sum
                - y_offset * xc.offset * self.dim);

        // Finally, reassemble the remaining compensation terms.
        Ok(MV::new(
            corrected_ip + yc.metric_specific + xc.metric_specific + self.squared_shift_norm,
        ))
    }
}

/// Compute the inner product between a full-precision query and a spherically quantized
/// data vector.
///
/// Returns an error if the arguments have different lengths.
impl<A, const NBITS: usize>
    Target2<A, distances::MathematicalResult<f32>, FullQueryRef<'_>, DataRef<'_, NBITS>>
    for CompensatedIP
where
    A: Architecture,
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a> Target2<
            A,
            distances::MathematicalResult<f32>,
            &'a [f32],
            BitSlice<'a, NBITS, Unsigned>,
        >,
{
    #[inline(always)]
    fn run(
        self,
        arch: A,
        x: FullQueryRef<'_>,
        y: DataRef<'_, NBITS>,
    ) -> distances::MathematicalResult<f32> {
        let s = arch
            .run2(InnerProduct, x.vector(), y.vector())?
            .into_inner();

        let yc = y.meta().to_full(arch);
        let xc = x.meta();

        let offset = ConstOffset::<NBITS>::OFFSET;
        let ip = xc.shifted_norm * yc.inner_product_correction * (s - xc.sum * offset);

        Ok(MV::new(
            ip + xc.metric_specific + yc.metric_specific + self.squared_shift_norm,
        ))
    }
}

////////////////////////
// Compensated Cosine //
////////////////////////

/// A `DistanceFunction` containing scaling parameters to enable distance the Cosine
/// distance function over vectors belonging to the same quantization space.
///
/// This distance function works by assuming input vectors were normalized **prior** to
/// compression and therefore cosine may be computed by delegating to inner product
/// computations. The [`crate::spherical::SphericalQuantizer`] will ensure this
/// pre-normalization when constructed with [`SupportedMetric::Cosine`].
#[derive(Debug, Clone, Copy)]
pub struct CompensatedCosine {
    pub(super) inner: CompensatedIP,
}

impl CompensatedCosine {
    /// Construct a new `CompensatedCosine` around the [`CompensatedIP`].
    pub fn new(inner: CompensatedIP) -> Self {
        Self { inner }
    }
}

impl<A, T, U> Target2<A, distances::MathematicalResult<f32>, T, U> for CompensatedCosine
where
    A: Architecture,
    CompensatedIP: Target2<A, distances::MathematicalResult<f32>, T, U>,
{
    #[inline(always)]
    fn run(self, arch: A, x: T, y: U) -> distances::MathematicalResult<f32> {
        self.inner.run(arch, x, y)
    }
}

/// A blanket implementation for applying the transformation
/// ```text
/// x -> 1-x
/// ```
/// from `MathematicalValue` to `f32` for cosine distance computations.
impl<A, T, U> Target2<A, distances::Result<f32>, T, U> for CompensatedCosine
where
    A: Architecture,
    Self: Target2<A, distances::MathematicalResult<f32>, T, U>,
{
    #[inline(always)]
    fn run(self, arch: A, x: T, y: U) -> distances::Result<f32> {
        let r: MV<f32> = self.run(arch, x, y)?;
        Ok(1.0 - r.into_inner())
    }
}

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

#[cfg(test)]
mod tests {
    use diskann_utils::{Reborrow, lazy_format};
    use diskann_vector::{PureDistanceFunction, distance::Metric, norm::FastL2Norm};
    use diskann_wide::ARCH;
    use rand::{
        SeedableRng,
        distr::{Distribution, Uniform},
        rngs::StdRng,
    };
    use rand_distr::StandardNormal;

    use super::*;
    use crate::{
        alloc::GlobalAllocator,
        bits::{BitTranspose, Dense},
    };

    #[derive(Debug, Clone, Copy, PartialEq)]
    struct Approx {
        absolute: f32,
        relative: f32,
    }

    impl Approx {
        const fn new(absolute: f32, relative: f32) -> Self {
            assert!(absolute >= 0.0);
            assert!(relative >= 0.0);
            Self { absolute, relative }
        }

        fn check(&self, got: f32, expected: f32, ctx: Option<&dyn std::fmt::Display>) -> bool {
            struct Ctx<'a>(Option<&'a dyn std::fmt::Display>);

            impl std::fmt::Display for Ctx<'_> {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    match self.0 {
                        None => write!(f, "none"),
                        Some(d) => write!(f, "{}", d),
                    }
                }
            }

            let absolute = (got - expected).abs();
            if absolute <= self.absolute {
                true
            } else {
                let relative = absolute / expected.abs();
                if relative <= self.relative {
                    true
                } else {
                    panic!(
                        "got {}, expected {}. Abs/Rel = {}/{} with bounds {}/{}: Ctx: {}",
                        got,
                        expected,
                        absolute,
                        relative,
                        self.absolute,
                        self.relative,
                        Ctx(ctx)
                    );
                }
            }
        }
    }

    //////////////
    // DataMeta //
    //////////////

    #[test]
    fn test_data_meta() {
        // Test constructor happy path.
        let meta = DataMeta::new(1.0, 2.0, 10).unwrap();
        let expected = DataMetaF32 {
            inner_product_correction: 1.0,
            metric_specific: 2.0,
            bit_sum: 10.0,
        };
        assert_eq!(meta.to_full(ARCH), expected);

        // Test constructor errors.
        let err = DataMeta::new(65600.0, 2.0, 10).unwrap_err();
        assert_eq!(
            err.to_string(),
            "inner product correction 65600 cannot fit in a 16-bit floating point number"
        );

        let err = DataMeta::new(2.0, 65600.0, 10).unwrap_err();
        assert_eq!(
            err.to_string(),
            "metric specific correction 65600 cannot fit in a 16-bit floating point number"
        );

        let err = DataMeta::new(2.0, 2.0, 65536).unwrap_err();
        assert_eq!(
            err.to_string(),
            "bit sum 65536 cannot fit in a 16-bit unsigned integer",
        );
    }

    //////////////////////
    // Supported Metric //
    //////////////////////

    #[test]
    fn supported_metric() {
        assert_eq!(
            SupportedMetric::try_from(Metric::L2).unwrap(),
            SupportedMetric::SquaredL2
        );
        assert_eq!(
            SupportedMetric::try_from(Metric::InnerProduct).unwrap(),
            SupportedMetric::InnerProduct
        );
        assert_eq!(
            SupportedMetric::try_from(Metric::Cosine).unwrap(),
            SupportedMetric::Cosine
        );
        assert!(matches!(
            SupportedMetric::try_from(Metric::CosineNormalized),
            Err(UnsupportedMetric(Metric::CosineNormalized))
        ));

        assert_eq!(SupportedMetric::SquaredL2, Metric::L2);
        assert_ne!(SupportedMetric::SquaredL2, Metric::InnerProduct);
        assert_ne!(SupportedMetric::SquaredL2, Metric::Cosine);
        assert_ne!(SupportedMetric::SquaredL2, Metric::CosineNormalized);

        assert_ne!(SupportedMetric::InnerProduct, Metric::L2);
        assert_eq!(SupportedMetric::InnerProduct, Metric::InnerProduct);
        assert_ne!(SupportedMetric::SquaredL2, Metric::Cosine);
        assert_ne!(SupportedMetric::SquaredL2, Metric::CosineNormalized);
    }

    ///////////////
    // Distances //
    ///////////////

    struct Reference<T> {
        compressed: T,
        reconstructed: Vec<f32>,
        norm: f32,
        center_ip: f32,
        self_ip: Option<f32>,
    }

    trait GenerateReference: Sized {
        fn generate_reference(
            center: &[f32],
            metric: SupportedMetric,
            rng: &mut StdRng,
        ) -> Reference<Self>;
    }

    impl<const NBITS: usize> GenerateReference for Data<NBITS, GlobalAllocator>
    where
        Unsigned: Representation<NBITS>,
    {
        fn generate_reference(
            center: &[f32],
            metric: SupportedMetric,
            rng: &mut StdRng,
        ) -> Reference<Self> {
            let dim = center.len();

            let mut reconstructed = vec![0.0f32; dim];
            let mut compressed = Data::<NBITS, _>::new_boxed(dim);

            let mut bit_sum = 0;
            let dist = Uniform::try_from(Unsigned::domain_const::<NBITS>()).unwrap();
            let offset = (2usize.pow(NBITS as u32) as f32 - 1.0) / 2.0;
            for (i, r) in reconstructed.iter_mut().enumerate() {
                let b: i64 = dist.sample(rng);
                bit_sum += b;
                compressed.vector_mut().set(i, b).unwrap();
                *r = (b as f32) - offset;
            }

            let r_norm = FastL2Norm.evaluate(reconstructed.as_slice());
            reconstructed.iter_mut().for_each(|i| *i /= r_norm);

            let norm: f32 = Uniform::new(0.0, 2.0).unwrap().sample(rng);
            let center_ip: f32 = Uniform::new(0.5, 2.5).unwrap().sample(rng);
            let self_ip: f32 = Uniform::new(0.5, 1.5).unwrap().sample(rng);

            compressed.set_meta(
                DataMeta::new(
                    norm / (self_ip * r_norm),
                    metric.pick(norm, center_ip),
                    bit_sum.try_into().unwrap(),
                )
                .unwrap(),
            );

            Reference {
                compressed,
                reconstructed,
                norm,
                center_ip,
                self_ip: Some(self_ip),
            }
        }
    }

    impl<const NBITS: usize, Perm> GenerateReference for Query<NBITS, Perm, GlobalAllocator>
    where
        Unsigned: Representation<NBITS>,
        Perm: PermutationStrategy<NBITS>,
    {
        fn generate_reference(
            center: &[f32],
            metric: SupportedMetric,
            rng: &mut StdRng,
        ) -> Reference<Self> {
            let dim = center.len();

            let mut reconstructed = vec![0.0f32; dim];
            let mut compressed = Query::<NBITS, Perm, _>::new_boxed(dim);

            let distribution = Uniform::try_from(Unsigned::domain_const::<NBITS>()).unwrap();

            let base: f32 = StandardNormal {}.sample(rng);
            let scale: f32 = {
                let scale: f32 = StandardNormal {}.sample(rng);
                scale.abs()
            };

            let mut bit_sum = 0;
            for (i, r) in reconstructed.iter_mut().enumerate() {
                let b = distribution.sample(rng);
                compressed.vector_mut().set(i, b).unwrap();
                *r = base + scale * (b as f32);
                bit_sum += b;
            }

            let norm: f32 = Uniform::new(0.0, 2.0).unwrap().sample(rng);
            let center_ip: f32 = Uniform::new(-2.0, 2.0).unwrap().sample(rng);

            compressed.set_meta(QueryMeta {
                inner_product_correction: norm * scale,
                bit_sum: bit_sum as f32,
                offset: base / scale,
                metric_specific: metric.pick(norm, center_ip),
            });

            Reference {
                compressed,
                reconstructed,
                norm,
                center_ip,
                self_ip: None,
            }
        }
    }

    impl GenerateReference for FullQuery<GlobalAllocator> {
        fn generate_reference(
            center: &[f32],
            metric: SupportedMetric,
            rng: &mut StdRng,
        ) -> Reference<Self> {
            let dim = center.len();

            let mut query = FullQuery::empty(dim, GlobalAllocator).unwrap();

            let mut sum = 0.0;
            let dist = StandardNormal {};
            for r in query.data.iter_mut() {
                let b: f32 = dist.sample(rng);
                sum += b;
                *r = b;
            }

            let r_norm = FastL2Norm.evaluate(&*query.data);
            query.data.iter_mut().for_each(|i| *i /= r_norm);

            let norm: f32 = Uniform::new(0.0, 2.0).unwrap().sample(rng);
            let center_ip: f32 = Uniform::new(-2.0, 2.0).unwrap().sample(rng);

            query.meta = FullQueryMeta {
                sum: sum / r_norm,
                shifted_norm: norm,
                metric_specific: metric.pick(norm, center_ip),
            };

            let reconstructed = query.data.to_vec();
            Reference {
                compressed: query,
                reconstructed,
                norm,
                center_ip,
                self_ip: None,
            }
        }
    }

    /// Refer to the module level documentation for some insight into what these components
    /// mean.
    ///
    /// The gist of these tests are that we generate the binary vectors `bx` and `by`
    /// (along with their code-book representation), the center, and the shifted versions
    /// of the target vectors.
    ///
    /// From those components, we computed the compensation terms and compute the expected
    /// result manually, verifying that the compensated computation works as expected.
    fn test_compensated_distance<const NBITS: usize>(
        dim: usize,
        ntrials: usize,
        err_l2: Approx,
        err_ip: Approx,
        rng: &mut StdRng,
    ) where
        Unsigned: Representation<NBITS>,
        for<'a> CompensatedIP: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                DataRef<'a, NBITS>,
                DataRef<'a, NBITS>,
            > + Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                DataRef<'a, NBITS>,
                DataRef<'a, NBITS>,
            >,
        for<'a> CompensatedSquaredL2: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                DataRef<'a, NBITS>,
                DataRef<'a, NBITS>,
            > + Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                DataRef<'a, NBITS>,
                DataRef<'a, NBITS>,
            >,
    {
        let mut center = vec![0.0f32; dim];
        for trial in 0..ntrials {
            // Sample the center.
            center
                .iter_mut()
                .for_each(|c| *c = StandardNormal {}.sample(rng));

            let c_square_norm = FastL2NormSquared.evaluate(&*center);

            // Inner Product
            {
                let x = Data::<NBITS, _>::generate_reference(
                    &center,
                    SupportedMetric::InnerProduct,
                    rng,
                );
                let y = Data::<NBITS, _>::generate_reference(
                    &center,
                    SupportedMetric::InnerProduct,
                    rng,
                );

                let kernel_result = {
                    let xy: MV<f32> = diskann_vector::distance::InnerProduct::evaluate(
                        &*x.reconstructed,
                        &*y.reconstructed,
                    );
                    x.norm * y.norm * xy.into_inner() / (x.self_ip.unwrap() * y.self_ip.unwrap())
                };

                let reference_ip = kernel_result + x.center_ip + y.center_ip + c_square_norm;
                let ip = CompensatedIP::new(&center, center.len());
                let got_ip: distances::MathematicalResult<f32> =
                    ARCH.run2(ip, x.compressed.reborrow(), y.compressed.reborrow());
                let got_ip = got_ip.unwrap();

                let ctx = &lazy_format!(
                    "Inner Product, trial {} of {}, dim = {}",
                    trial,
                    ntrials,
                    dim
                );
                assert!(err_ip.check(got_ip.into_inner(), reference_ip, Some(ctx)));

                let got_ip_f32: distances::Result<f32> =
                    ARCH.run2(ip, x.compressed.reborrow(), y.compressed.reborrow());

                let got_ip_f32 = got_ip_f32.unwrap();

                assert_eq!(got_ip_f32, -got_ip.into_inner());

                // Cosine (very similary to inner-product).
                let cosine = CompensatedCosine::new(ip);
                let got_cosine: distances::MathematicalResult<f32> =
                    ARCH.run2(cosine, x.compressed.reborrow(), y.compressed.reborrow());
                let got_cosine = got_cosine.unwrap();
                assert_eq!(
                    got_cosine.into_inner(),
                    got_ip.into_inner(),
                    "cosine and IP should be the same"
                );

                let got_cosine_f32: distances::Result<f32> =
                    ARCH.run2(cosine, x.compressed.reborrow(), y.compressed.reborrow());

                let got_cosine_f32 = got_cosine_f32.unwrap();

                assert_eq!(
                    got_cosine_f32,
                    1.0 - got_cosine.into_inner(),
                    "incorrect transform performed"
                );
            }

            // Squared L2
            {
                let x =
                    Data::<NBITS, _>::generate_reference(&center, SupportedMetric::SquaredL2, rng);
                let y =
                    Data::<NBITS, _>::generate_reference(&center, SupportedMetric::SquaredL2, rng);

                // Compute the expected value for the quantity `|X'| |Y'| <x, y>`.
                let kernel_result = {
                    let xy: MV<f32> = diskann_vector::distance::InnerProduct::evaluate(
                        &*x.reconstructed,
                        &*y.reconstructed,
                    );
                    x.norm * y.norm * xy.into_inner() / (x.self_ip.unwrap() * y.self_ip.unwrap())
                };

                let reference_l2 = x.norm * x.norm + y.norm * y.norm - 2.0 * kernel_result;
                let l2 = CompensatedSquaredL2::new(dim);
                let got_l2: distances::MathematicalResult<f32> =
                    ARCH.run2(l2, x.compressed.reborrow(), y.compressed.reborrow());
                let got_l2 = got_l2.unwrap();

                let ctx =
                    &lazy_format!("Squared L2, trial {} of {}, dim = {}", trial, ntrials, dim);
                assert!(err_l2.check(got_l2.into_inner(), reference_l2, Some(ctx)));

                let got_l2_f32: distances::Result<f32> =
                    ARCH.run2(l2, x.compressed.reborrow(), y.compressed.reborrow());
                let got_l2_f32 = got_l2_f32.unwrap();

                assert_eq!(got_l2_f32, got_l2.into_inner());
            }
        }
    }

    /// This works similarly to the 1-bit compensated distances, but checks the 4-bit query
    /// path.
    fn test_mixed_compensated_distance<const Q: usize, const D: usize, Perm>(
        dim: usize,
        ntrials: usize,
        err_l2: Approx,
        err_ip: Approx,
        rng: &mut StdRng,
    ) where
        Unsigned: Representation<Q>,
        Unsigned: Representation<D>,
        Perm: PermutationStrategy<Q>,
        for<'a> CompensatedIP: Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                QueryRef<'a, Q, Perm>,
                DataRef<'a, D>,
            >,
        for<'a> CompensatedSquaredL2: Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                QueryRef<'a, Q, Perm>,
                DataRef<'a, D>,
            >,
        for<'a> CompensatedCosine: Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                QueryRef<'a, Q, Perm>,
                DataRef<'a, D>,
            >,
        for<'a> CompensatedIP: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                QueryRef<'a, Q, Perm>,
                DataRef<'a, D>,
            >,
        for<'a> CompensatedSquaredL2: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                QueryRef<'a, Q, Perm>,
                DataRef<'a, D>,
            >,
        for<'a> CompensatedCosine: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                QueryRef<'a, Q, Perm>,
                DataRef<'a, D>,
            >,
    {
        // The center
        let mut center = vec![0.0f32; dim];
        for trial in 0..ntrials {
            // Sample the center.
            center
                .iter_mut()
                .for_each(|c| *c = StandardNormal {}.sample(rng));

            let c_square_norm = FastL2NormSquared.evaluate(&*center);

            // Inner Product
            {
                let x = Query::<Q, Perm, _>::generate_reference(
                    &center,
                    SupportedMetric::InnerProduct,
                    rng,
                );
                let y =
                    Data::<D, _>::generate_reference(&center, SupportedMetric::InnerProduct, rng);

                // The expected scaled dot-product between the normalized vectors.
                let xy = {
                    let xy: MV<f32> = diskann_vector::distance::InnerProduct::evaluate(
                        &*x.reconstructed,
                        &*y.reconstructed,
                    );
                    x.norm * y.norm * xy.into_inner() / y.self_ip.unwrap()
                };

                let reference_ip = -(xy + x.center_ip + y.center_ip + c_square_norm);
                let ip = CompensatedIP::new(&center, center.len());
                let got_ip: distances::Result<f32> =
                    ARCH.run2(ip, x.compressed.reborrow(), y.compressed.reborrow());
                let got_ip = got_ip.unwrap();

                let ctx = &lazy_format!(
                    "Inner Product, trial = {} of {}, dim = {}",
                    trial,
                    ntrials,
                    dim
                );

                assert!(err_ip.check(got_ip, reference_ip, Some(ctx)));

                // Cosine (very similary to inner-product).
                let cosine = CompensatedCosine::new(ip);
                let got_cosine: distances::MathematicalResult<f32> =
                    ARCH.run2(cosine, x.compressed.reborrow(), y.compressed.reborrow());

                let got_cosine = got_cosine.unwrap();
                assert_eq!(
                    got_cosine.into_inner(),
                    -got_ip,
                    "cosine and IP should be the same"
                );

                let got_cosine_f32: distances::Result<f32> =
                    ARCH.run2(cosine, x.compressed.reborrow(), y.compressed.reborrow());

                let got_cosine_f32 = got_cosine_f32.unwrap();
                assert_eq!(
                    got_cosine_f32,
                    1.0 - got_cosine.into_inner(),
                    "incorrect transform performed"
                );
            }

            // Squared L2
            {
                let x = Query::<Q, Perm, _>::generate_reference(
                    &center,
                    SupportedMetric::SquaredL2,
                    rng,
                );
                let y = Data::<D, _>::generate_reference(&center, SupportedMetric::SquaredL2, rng);

                // The expected scaled dot-product between the normalized vectors.
                let xy = {
                    let xy: MV<f32> = diskann_vector::distance::InnerProduct::evaluate(
                        &*x.reconstructed,
                        &*y.reconstructed,
                    );
                    x.norm * y.norm * xy.into_inner() / y.self_ip.unwrap()
                };
                let reference_l2 = x.norm * x.norm + y.norm * y.norm - 2.0 * xy;
                let l2 = CompensatedSquaredL2::new(dim);
                let got_l2: distances::Result<f32> =
                    ARCH.run2(l2, x.compressed.reborrow(), y.compressed.reborrow());
                let got_l2 = got_l2.unwrap();

                let ctx = &lazy_format!(
                    "Squared L2, trial = {} of {}, dim = {}",
                    trial,
                    ntrials,
                    dim
                );

                assert!(err_l2.check(got_l2, reference_l2, Some(ctx)));
            }
        }
    }

    fn test_full_distances<const NBITS: usize>(
        dim: usize,
        ntrials: usize,
        err_l2: Approx,
        err_ip: Approx,
        rng: &mut StdRng,
    ) where
        Unsigned: Representation<NBITS>,
        for<'a> CompensatedIP: Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                FullQueryRef<'a>,
                DataRef<'a, NBITS>,
            >,
        for<'a> CompensatedSquaredL2: Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                FullQueryRef<'a>,
                DataRef<'a, NBITS>,
            >,
        for<'a> CompensatedCosine: Target2<
                diskann_wide::arch::Current,
                distances::MathematicalResult<f32>,
                FullQueryRef<'a>,
                DataRef<'a, NBITS>,
            >,
        for<'a> CompensatedIP: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                FullQueryRef<'a>,
                DataRef<'a, NBITS>,
            >,
        for<'a> CompensatedSquaredL2: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                FullQueryRef<'a>,
                DataRef<'a, NBITS>,
            >,
        for<'a> CompensatedCosine: Target2<
                diskann_wide::arch::Current,
                distances::Result<f32>,
                FullQueryRef<'a>,
                DataRef<'a, NBITS>,
            >,
    {
        // The center
        let mut center = vec![0.0f32; dim];
        for trial in 0..ntrials {
            // Sample the center.
            center
                .iter_mut()
                .for_each(|c| *c = StandardNormal {}.sample(rng));

            let c_square_norm = FastL2NormSquared.evaluate(&*center);

            // Inner Product
            {
                let x = FullQuery::generate_reference(&center, SupportedMetric::InnerProduct, rng);
                let y = Data::<NBITS, _>::generate_reference(
                    &center,
                    SupportedMetric::InnerProduct,
                    rng,
                );

                // The expected scaled dot-product between the normalized vectors.
                let xy = {
                    let xy: MV<f32> = diskann_vector::distance::InnerProduct::evaluate(
                        &*x.reconstructed,
                        &*y.reconstructed,
                    );
                    x.norm * y.norm * xy.into_inner() / y.self_ip.unwrap()
                };

                let reference_ip = -(xy + x.center_ip + y.center_ip + c_square_norm);
                let ip = CompensatedIP::new(&center, center.len());
                let got_ip: distances::Result<f32> =
                    ARCH.run2(ip, x.compressed.reborrow(), y.compressed.reborrow());
                let got_ip = got_ip.unwrap();

                let ctx = &lazy_format!(
                    "Inner Product, trial = {} of {}, dim = {}",
                    trial,
                    ntrials,
                    dim
                );

                assert!(err_ip.check(got_ip, reference_ip, Some(ctx)));

                // Cosine (very similary to inner-product).
                let cosine = CompensatedCosine::new(ip);
                let got_cosine: distances::MathematicalResult<f32> =
                    ARCH.run2(cosine, x.compressed.reborrow(), y.compressed.reborrow());
                let got_cosine = got_cosine.unwrap();
                assert_eq!(
                    got_cosine.into_inner(),
                    -got_ip,
                    "cosine and IP should be the same"
                );

                let got_cosine_f32: distances::Result<f32> =
                    ARCH.run2(cosine, x.compressed.reborrow(), y.compressed.reborrow());

                let got_cosine_f32 = got_cosine_f32.unwrap();
                assert_eq!(
                    got_cosine_f32,
                    1.0 - got_cosine.into_inner(),
                    "incorrect transform performed"
                );
            }

            // Squared L2
            {
                let x = FullQuery::generate_reference(&center, SupportedMetric::SquaredL2, rng);
                let y =
                    Data::<NBITS, _>::generate_reference(&center, SupportedMetric::SquaredL2, rng);

                // The expected scaled dot-product between the normalized vectors.
                let xy = {
                    let xy: MV<f32> = diskann_vector::distance::InnerProduct::evaluate(
                        &*x.reconstructed,
                        &*y.reconstructed,
                    );
                    x.norm * y.norm * xy.into_inner() / y.self_ip.unwrap()
                };

                let reference_l2 = x.norm * x.norm + y.norm * y.norm - 2.0 * xy;
                let l2 = CompensatedSquaredL2::new(dim);
                let got_l2: distances::Result<f32> =
                    ARCH.run2(l2, x.compressed.reborrow(), y.compressed.reborrow());
                let got_l2 = got_l2.unwrap();

                let ctx = &lazy_format!(
                    "Squared L2, trial = {} of {}, dim = {}",
                    trial,
                    ntrials,
                    dim
                );
                assert!(err_l2.check(got_l2, reference_l2, Some(ctx)));
            }
        }
    }

    cfg_if::cfg_if! {
        if #[cfg(miri)] {
            // The max dim does not need to be as high for these vectors because they
            // defer their distance function implementation to `BitSlice`, which is more
            // heavily tested.
            const MAX_DIM: usize = 37;
            const TRIALS_PER_DIM: usize = 1;
        } else {
            const MAX_DIM: usize = 256;
            const TRIALS_PER_DIM: usize = 20;
        }
    }

    #[test]
    fn test_symmetric_distances_1bit() {
        let mut rng = StdRng::seed_from_u64(0x2a5f79a2469218f6);
        for dim in 1..MAX_DIM {
            test_compensated_distance::<1>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(4.0e-3, 3.0e-3),
                Approx::new(1.0e-3, 5.0e-4),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_symmetric_distances_2bit() {
        let mut rng = StdRng::seed_from_u64(0x68f8f52057f94399);
        for dim in 1..MAX_DIM {
            test_compensated_distance::<2>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(3.5e-3, 2.0e-3),
                Approx::new(2.0e-3, 5.0e-4),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_symmetric_distances_4bit() {
        let mut rng = StdRng::seed_from_u64(0xb88d76ac4c58e923);
        for dim in 1..MAX_DIM {
            test_compensated_distance::<4>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(2.0e-3, 2.0e-3),
                Approx::new(2.0e-3, 5.0e-4),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_symmetric_distances_8bit() {
        let mut rng = StdRng::seed_from_u64(0x1c2b79873ee32626);
        for dim in 1..MAX_DIM {
            test_compensated_distance::<8>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(2.0e-3, 2.0e-3),
                Approx::new(2.0e-3, 4.0e-4),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_mixed_distances_4x1() {
        let mut rng = StdRng::seed_from_u64(0x1efb4d87ed0a8ada);
        for dim in 1..MAX_DIM {
            test_mixed_compensated_distance::<4, 1, BitTranspose>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(4.0e-3, 3.0e-3),
                Approx::new(1.3e-2, 8.3e-3),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_mixed_distances_4x4() {
        let mut rng = StdRng::seed_from_u64(0x508554264eb7a51b);
        for dim in 1..MAX_DIM {
            test_mixed_compensated_distance::<4, 4, Dense>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(4.0e-3, 3.0e-3),
                Approx::new(3.0e-4, 8.3e-2),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_mixed_distances_8x8() {
        let mut rng = StdRng::seed_from_u64(0x8acd8e4224c76c43);
        for dim in 1..MAX_DIM {
            test_mixed_compensated_distance::<8, 8, Dense>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(2.0e-3, 6.0e-3),
                Approx::new(1.0e-2, 3.0e-2),
                &mut rng,
            );
        }
    }

    // Full
    #[test]
    fn test_full_distances_1bit() {
        let mut rng = StdRng::seed_from_u64(0x7f93530559f42d66);
        for dim in 1..MAX_DIM {
            test_full_distances::<1>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(1.0e-3, 2.0e-3),
                Approx::new(0.0, 5.0e-3),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_full_distances_2bit() {
        let mut rng = StdRng::seed_from_u64(0xa3ad61d3d03a0c5a);
        for dim in 1..MAX_DIM {
            test_full_distances::<2>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(2.0e-3, 1.1e-3),
                Approx::new(7.0e-4, 1.0e-3),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_full_distances_4bit() {
        let mut rng = StdRng::seed_from_u64(0x3e2f50ed7c64f0c2);
        for dim in 1..MAX_DIM {
            test_full_distances::<4>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(2.0e-3, 1.0e-2),
                Approx::new(1.0e-3, 5.0e-4),
                &mut rng,
            );
        }
    }

    #[test]
    fn test_full_distances_8bit() {
        let mut rng = StdRng::seed_from_u64(0x95705070e415c6d3);
        for dim in 1..MAX_DIM {
            test_full_distances::<8>(
                dim,
                TRIALS_PER_DIM,
                Approx::new(1.0e-3, 1.0e-3),
                Approx::new(2.0e-3, 1.0e-4),
                &mut rng,
            );
        }
    }
}