diskann-quantization 0.50.1

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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use diskann_vector::{MathematicalValue, PureDistanceFunction};
use thiserror::Error;

use crate::{
    alloc::GlobalAllocator,
    bits::{BitSlice, Dense, Representation, Unsigned},
    distances,
    distances::{InnerProduct, MV},
    meta::{self, slice},
};

/// A per-vector precomputed coefficients to help compute inner products
/// and squared L2 distances for the MinMax quantized vectors.
///
/// The inner product between `X = ax * X' + bx` and `Y = ay * Y' + by` for d-dimensional
/// vectors X and Y is:
/// ```math
/// <X, Y> = <ax * X' + bx, ay * Y' + by>
///        = ax * ay * <X', Y'> + ax * <X', by> + ay * <Y', bx> + d * bx * by.
/// ```
/// Let us define a grouping of these terms to make it easier to understand:
/// ```math
///  Nx = ax * sum_i X'[i],     Ny = ay * sum_i Y'[i],
/// ```
/// We can then simplify the dot product calculation as follows:
/// ```math
/// <X, Y> = ax * ay * <X', Y'> + Nx * by + Ny * bx +  d * bx * by
///                    --------
///                       |
///               Integer Dot Product
/// ```
///
/// To compute the squared L2 distance,
/// ```math
/// |X - Y|^2 = |ax * X' + bx|^2 + |ay * Y' + by|^2 - 2 * <X, Y>
/// ```
/// we can re-use the computation for inner-product from above.
#[derive(Default, Debug, Clone, Copy, PartialEq, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub struct MinMaxCompensation {
    pub dim: u32,          // - dimension
    pub b: f32,            // - bx
    pub n: f32,            // - Nx
    pub a: f32,            // - ax
    pub norm_squared: f32, // - |ax * X' + bx|^2
}

const META_BYTES: usize = std::mem::size_of::<MinMaxCompensation>(); // This will be 5 * 4 = 20 bytes.

/// Error type for parsing a slice of bytes as a `DataRef`
/// and returning corresponding dimension.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum MetaParseError {
    #[error("Invalid size: {0}, must contain at least {META_BYTES} bytes")]
    NotCanonical(usize),
}

impl MinMaxCompensation {
    /// Reads the dimension from the first 4 bytes of a MinMax quantized vector's metadata.
    ///
    /// This function is used to extract the vector dimension from serialized MinMax quantized
    /// vector data without fully deserializing the entire vector structure.
    ///
    /// # Arguments
    /// * `bytes` - A byte slice containing the serialized MinMax vector data
    ///
    /// # Returns
    /// * `Ok(dimension)` - The dimension of the vector as a `usize`
    /// * `Err(MetaParseError::NotCanonical(size))` - If the byte slice is shorter than 20 bytes (META_BYTES)
    ///
    /// # Usage
    /// Use this when you need to determine the vector dimension from serialized data before
    /// creating a `DataRef` or allocating appropriately sized buffers for decompression.
    #[inline(always)]
    pub fn read_dimension(bytes: &[u8]) -> Result<usize, MetaParseError> {
        if bytes.len() < META_BYTES {
            return Err(MetaParseError::NotCanonical(bytes.len()));
        }

        // SAFETY: There are at least `META_BYTES` = 20 bytes in the array so this access is within bounds.
        let dim_bytes: [u8; 4] = bytes.get(..4).map_or_else(
            || Err(MetaParseError::NotCanonical(bytes.len())),
            |slice| {
                slice
                    .try_into()
                    .map_err(|_| MetaParseError::NotCanonical(bytes.len()))
            },
        )?;

        let dim = u32::from_le_bytes(dim_bytes) as usize;

        Ok(dim)
    }
}

/// An owning compressed data vector
///
/// See: [`meta::Vector`].
pub type Data<const NBITS: usize> = meta::Vector<NBITS, Unsigned, MinMaxCompensation, Dense>;

/// A borrowed `Data` vector
///
/// See: [`meta::Vector`].
pub type DataRef<'a, const NBITS: usize> =
    meta::VectorRef<'a, NBITS, Unsigned, MinMaxCompensation, Dense>;

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
pub enum DecompressError {
    #[error("expected src and dst length to be identical, instead src is {0}, and dst is {1}")]
    LengthMismatch(usize, usize),
}
impl<const NBITS: usize> DataRef<'_, NBITS>
where
    Unsigned: Representation<NBITS>,
{
    /// Decompresses a MinMax quantized vector back into its original floating-point representation.
    ///
    /// This method reconstructs the original vector values using the stored quantization parameters
    /// and the MinMax dequantization formula: `x = x' * a + b` and stores the result in `dst`
    ///
    /// # Arguments
    ///
    /// * `dst` - A mutable slice of `f32` values where the decompressed data will be written.
    ///   Must have the same length as the compressed vector.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - On successful decompression
    /// * `Err(DecompressError::LengthMismatch(src_len, dst_len))` - If the destination slice
    ///   length doesn't match the compressed vector length
    pub fn decompress_into(&self, dst: &mut [f32]) -> Result<(), DecompressError> {
        if dst.len() != self.len() {
            return Err(DecompressError::LengthMismatch(self.len(), dst.len()));
        }
        let meta = self.meta();

        // SAFETY: We checked that the length of the underlying vector is the same as
        // as `dst` so we are guaranteed to be within bounds when accessing the vector.
        dst.iter_mut().enumerate().for_each(|(i, d)| unsafe {
            *d = self.vector().get_unchecked(i) as f32 * meta.a + meta.b
        });
        Ok(())
    }
}

/// A mutable borrowed `Data` vector
///
/// See: [`meta::Vector`].
pub type DataMutRef<'a, const NBITS: usize> =
    meta::VectorMut<'a, NBITS, Unsigned, MinMaxCompensation, Dense>;

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

/// A meta struct storing the `sum` and `norm_squared` of a
/// full query after transformation is applied to it.
///
/// The inner product between `X = ax * X' + bx` and `Y` for d-dimensional
/// vectors X and Y is:
/// ```math
/// <X, Y> = <ax * X' + bx, Y>
///        = ax * <X', Y> + bx * sum(Y).
///               --------
///                  |
///          Integer-Float Dot Product
/// ```
///
/// To compute the squared L2 distance,
/// ```math
/// |X - Y|^2 = |ax * X' + bx|^2 + |Y|^2 - 2 * <X', Y>
/// ```
#[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 'data'.
    pub norm_squared: f32,
}

/// A full precision query.
///
/// See: [`slice::Slice`].
pub type FullQuery<A = GlobalAllocator> = slice::PolySlice<f32, FullQueryMeta, A>;

/// A borrowed full precision query.
///
/// See: [`slice::SliceRef`].
pub type FullQueryRef<'a> = slice::SliceRef<'a, f32, FullQueryMeta>;

/// A mutable borrowed full precision query.
///
/// See: [`slice::SliceMut`].
pub type FullQueryMut<'a> = slice::SliceMut<'a, f32, FullQueryMeta>;

///////////////////////////
// Compensated Distances //
///////////////////////////
#[inline(always)]
fn kernel<const NBITS: usize, const MBITS: usize, F>(
    x: DataRef<'_, NBITS>,
    y: DataRef<'_, MBITS>,
    f: F,
) -> distances::MathematicalResult<f32>
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'b, MBITS, Unsigned>,
            distances::MathematicalResult<u32>,
        >,
    F: Fn(f32, &MinMaxCompensation, &MinMaxCompensation) -> f32,
{
    let raw_product = InnerProduct::evaluate(x.vector(), y.vector())?;
    let (xm, ym) = (x.meta(), y.meta());
    let term0 = xm.a * ym.a * raw_product.into_inner() as f32;
    let term1_x = xm.n * ym.b;
    let term1_y = ym.n * xm.b;
    let term2 = xm.b * ym.b * (x.len() as f32);

    let v = term0 + term1_x + term1_y + term2;
    Ok(MV::new(f(v, &xm, &ym)))
}

pub struct MinMaxIP;

impl<const NBITS: usize, const MBITS: usize>
    PureDistanceFunction<DataRef<'_, NBITS>, DataRef<'_, MBITS>, distances::MathematicalResult<f32>>
    for MinMaxIP
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'b, MBITS, Unsigned>,
            distances::MathematicalResult<u32>,
        >,
{
    #[inline(always)]
    fn evaluate(
        x: DataRef<'_, NBITS>,
        y: DataRef<'_, MBITS>,
    ) -> distances::MathematicalResult<f32> {
        kernel(x, y, |v, _, _| v)
    }
}

impl<const NBITS: usize, const MBITS: usize>
    PureDistanceFunction<DataRef<'_, NBITS>, DataRef<'_, MBITS>, distances::Result<f32>>
    for MinMaxIP
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'b, MBITS, Unsigned>,
            distances::MathematicalResult<u32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: DataRef<'_, NBITS>, y: DataRef<'_, MBITS>) -> distances::Result<f32> {
        let v: distances::MathematicalResult<f32> = Self::evaluate(x, y);
        Ok(-v?.into_inner())
    }
}

impl<const NBITS: usize>
    PureDistanceFunction<FullQueryRef<'_>, DataRef<'_, NBITS>, distances::MathematicalResult<f32>>
    for MinMaxIP
where
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            &'a [f32],
            BitSlice<'b, NBITS, Unsigned>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: FullQueryRef<'_>, y: DataRef<'_, NBITS>) -> distances::MathematicalResult<f32> {
        let raw_product: f32 = InnerProduct::evaluate(x.vector(), y.vector())?.into_inner();
        Ok(MathematicalValue::new(
            raw_product * y.meta().a + x.meta().sum * y.meta().b,
        ))
    }
}

impl<const NBITS: usize>
    PureDistanceFunction<FullQueryRef<'_>, DataRef<'_, NBITS>, distances::Result<f32>> for MinMaxIP
where
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            &'a [f32],
            BitSlice<'b, NBITS, Unsigned>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: FullQueryRef<'_>, y: DataRef<'_, NBITS>) -> distances::Result<f32> {
        let v: distances::MathematicalResult<f32> = Self::evaluate(x, y);
        Ok(-v?.into_inner())
    }
}

pub struct MinMaxL2Squared;

impl<const NBITS: usize, const MBITS: usize>
    PureDistanceFunction<DataRef<'_, NBITS>, DataRef<'_, MBITS>, distances::MathematicalResult<f32>>
    for MinMaxL2Squared
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'b, MBITS, Unsigned>,
            distances::MathematicalResult<u32>,
        >,
{
    #[inline(always)]
    fn evaluate(
        x: DataRef<'_, NBITS>,
        y: DataRef<'_, MBITS>,
    ) -> distances::MathematicalResult<f32> {
        kernel(x, y, |v, xm, ym| {
            -2.0 * v + xm.norm_squared + ym.norm_squared
        })
    }
}

impl<const NBITS: usize, const MBITS: usize>
    PureDistanceFunction<DataRef<'_, NBITS>, DataRef<'_, MBITS>, distances::Result<f32>>
    for MinMaxL2Squared
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            BitSlice<'a, NBITS, Unsigned>,
            BitSlice<'b, MBITS, Unsigned>,
            distances::MathematicalResult<u32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: DataRef<'_, NBITS>, y: DataRef<'_, MBITS>) -> distances::Result<f32> {
        let v: distances::MathematicalResult<f32> = Self::evaluate(x, y);
        Ok(v?.into_inner())
    }
}

impl<const NBITS: usize>
    PureDistanceFunction<FullQueryRef<'_>, DataRef<'_, NBITS>, distances::MathematicalResult<f32>>
    for MinMaxL2Squared
where
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            &'a [f32],
            BitSlice<'b, NBITS, Unsigned>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: FullQueryRef<'_>, y: DataRef<'_, NBITS>) -> distances::MathematicalResult<f32> {
        let raw_product = InnerProduct::evaluate(x.vector(), y.vector())?.into_inner();

        let ym = y.meta();
        let compensated_ip = raw_product * ym.a + x.meta().sum * ym.b;
        Ok(MV::new(
            x.meta().norm_squared + ym.norm_squared - 2.0 * compensated_ip,
        ))
    }
}

impl<const NBITS: usize>
    PureDistanceFunction<FullQueryRef<'_>, DataRef<'_, NBITS>, distances::Result<f32>>
    for MinMaxL2Squared
where
    Unsigned: Representation<NBITS>,
    InnerProduct: for<'a, 'b> PureDistanceFunction<
            &'a [f32],
            BitSlice<'b, NBITS, Unsigned>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: FullQueryRef<'_>, y: DataRef<'_, NBITS>) -> distances::Result<f32> {
        let v: distances::MathematicalResult<f32> = Self::evaluate(x, y);
        Ok(v?.into_inner())
    }
}

///////////////////////
// Cosine Distances //
///////////////////////

pub struct MinMaxCosine;

impl<const NBITS: usize, const MBITS: usize>
    PureDistanceFunction<DataRef<'_, NBITS>, DataRef<'_, MBITS>, distances::Result<f32>>
    for MinMaxCosine
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    MinMaxIP: for<'a, 'b> PureDistanceFunction<
            DataRef<'a, NBITS>,
            DataRef<'b, MBITS>,
            distances::MathematicalResult<f32>,
        >,
{
    // 1 - <X, Y> / (|X| * |Y|)
    #[inline(always)]
    fn evaluate(x: DataRef<'_, NBITS>, y: DataRef<'_, MBITS>) -> distances::Result<f32> {
        let ip: MV<f32> = MinMaxIP::evaluate(x, y)?;
        let (xm, ym) = (x.meta(), y.meta());
        Ok(1.0 - ip.into_inner() / (xm.norm_squared.sqrt() * ym.norm_squared.sqrt()))
    }
}

impl<const NBITS: usize>
    PureDistanceFunction<FullQueryRef<'_>, DataRef<'_, NBITS>, distances::Result<f32>>
    for MinMaxCosine
where
    Unsigned: Representation<NBITS>,
    MinMaxIP: for<'a, 'b> PureDistanceFunction<
            FullQueryRef<'a>,
            DataRef<'b, NBITS>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: FullQueryRef<'_>, y: DataRef<'_, NBITS>) -> distances::Result<f32> {
        let ip: MathematicalValue<f32> = MinMaxIP::evaluate(x, y)?;
        let (xm, ym) = (x.meta().norm_squared, y.meta());
        Ok(1.0 - ip.into_inner() / (xm.sqrt() * ym.norm_squared.sqrt()))
        // 1 - <X, Y> / (|X| * |Y|)
    }
}

pub struct MinMaxCosineNormalized;

impl<const NBITS: usize, const MBITS: usize>
    PureDistanceFunction<DataRef<'_, NBITS>, DataRef<'_, MBITS>, distances::Result<f32>>
    for MinMaxCosineNormalized
where
    Unsigned: Representation<NBITS> + Representation<MBITS>,
    MinMaxIP: for<'a, 'b> PureDistanceFunction<
            DataRef<'a, NBITS>,
            DataRef<'b, MBITS>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: DataRef<'_, NBITS>, y: DataRef<'_, MBITS>) -> distances::Result<f32> {
        let ip: MathematicalValue<f32> = MinMaxIP::evaluate(x, y)?;
        Ok(1.0 - ip.into_inner()) // 1 - <X, Y>
    }
}

impl<const NBITS: usize>
    PureDistanceFunction<FullQueryRef<'_>, DataRef<'_, NBITS>, distances::Result<f32>>
    for MinMaxCosineNormalized
where
    Unsigned: Representation<NBITS>,
    MinMaxIP: for<'a, 'b> PureDistanceFunction<
            FullQueryRef<'a>,
            DataRef<'b, NBITS>,
            distances::MathematicalResult<f32>,
        >,
{
    #[inline(always)]
    fn evaluate(x: FullQueryRef<'_>, y: DataRef<'_, NBITS>) -> distances::Result<f32> {
        let ip: MathematicalValue<f32> = MinMaxIP::evaluate(x, y)?;
        Ok(1.0 - ip.into_inner()) // 1 - <X, Y>
    }
}

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

#[cfg(test)]
#[cfg(not(miri))]
mod minmax_vector_tests {
    use diskann_utils::Reborrow;
    use rand::{
        Rng, SeedableRng,
        distr::{Distribution, Uniform},
        rngs::StdRng,
    };

    use super::*;
    use crate::{alloc::GlobalAllocator, scalar::bit_scale};

    /// Builds a random MinMax quantized vector and its full-precision reconstruction.
    ///
    /// Returns `(compressed, original)` where `compressed` has its `MinMaxCompensation`
    /// metadata fully populated and `original` is the dequantized f32 vector.
    fn random_minmax_vector<const NBITS: usize>(
        dim: usize,
        rng: &mut impl Rng,
    ) -> (Data<NBITS>, Vec<f32>)
    where
        Unsigned: Representation<NBITS>,
    {
        let mut v = Data::<NBITS>::new_boxed(dim);

        let domain = Unsigned::domain_const::<NBITS>();
        let code_dist = Uniform::new_inclusive(*domain.start(), *domain.end()).unwrap();

        {
            let mut bs = v.vector_mut();
            for i in 0..dim {
                bs.set(i, code_dist.sample(rng)).unwrap();
            }
        }

        let a: f32 = Uniform::new_inclusive(0.0, 2.0).unwrap().sample(rng);
        let b: f32 = Uniform::new_inclusive(0.0, 2.0).unwrap().sample(rng);

        let original: Vec<f32> = (0..dim)
            .map(|i| a * v.vector().get(i).unwrap() as f32 + b)
            .collect();

        let code_sum: f32 = (0..dim).map(|i| v.vector().get(i).unwrap() as f32).sum();
        let norm_squared: f32 = original.iter().map(|x| x * x).sum();

        v.set_meta(MinMaxCompensation {
            a,
            b,
            n: a * code_sum,
            norm_squared,
            dim: dim as u32,
        });

        (v, original)
    }

    fn test_minmax_compensated_vectors<const NBITS: usize, R>(dim: usize, rng: &mut R)
    where
        Unsigned: Representation<NBITS>,
        InnerProduct: for<'a, 'b> PureDistanceFunction<
                BitSlice<'a, NBITS, Unsigned>,
                BitSlice<'b, NBITS, Unsigned>,
                distances::MathematicalResult<u32>,
            >,
        InnerProduct: for<'a, 'b> PureDistanceFunction<
                &'a [f32],
                BitSlice<'b, NBITS, Unsigned>,
                distances::MathematicalResult<f32>,
            >,
        R: Rng,
    {
        assert!(dim <= bit_scale::<NBITS>() as usize);

        let (v1, original1) = random_minmax_vector::<NBITS>(dim, rng);
        let (v2, original2) = random_minmax_vector::<NBITS>(dim, rng);

        let norm1_squared = v1.meta().norm_squared;
        let norm2_squared = v2.meta().norm_squared;

        // Calculate raw integer dot product
        let expected_ip = (0..dim).map(|i| original1[i] * original2[i]).sum::<f32>();

        // Test inner product with f32
        let computed_ip_f32: distances::Result<f32> =
            MinMaxIP::evaluate(v1.reborrow(), v2.reborrow());
        let computed_ip_f32 = computed_ip_f32.unwrap();
        assert!(
            (expected_ip - (-computed_ip_f32)).abs() / expected_ip.abs() < 1e-3,
            "Inner product (f32) failed: expected {}, got {} on dim : {}",
            -expected_ip,
            computed_ip_f32,
            dim
        );

        // Expected L2 distance = |X|² + |Y|² - 2<X,Y>
        let expected_l2 = (0..dim)
            .map(|i| original1[i] - original2[i])
            .map(|x| x.powf(2.0))
            .sum::<f32>();

        // Test L2 distance with f32
        let computed_l2_f32: distances::Result<f32> =
            MinMaxL2Squared::evaluate(v1.reborrow(), v2.reborrow());
        let computed_l2_f32 = computed_l2_f32.unwrap();
        assert!(
            ((computed_l2_f32 - expected_l2).abs() / expected_l2) < 1e-3,
            "L2 distance (f32) failed: expected {}, got {} on dim : {}",
            expected_l2,
            computed_l2_f32,
            dim
        );

        let expected_cosine = 1.0 - expected_ip / (norm1_squared.sqrt() * norm2_squared.sqrt());

        let computed_cosine: distances::Result<f32> =
            MinMaxCosine::evaluate(v1.reborrow(), v2.reborrow());
        let computed_cosine = computed_cosine.unwrap();

        {
            let passed = (computed_cosine - expected_cosine).abs() < 1e-6
                || ((computed_cosine - expected_cosine).abs() / expected_cosine) < 1e-3;

            assert!(
                passed,
                "Cosine distance (f32) failed: expected {}, got {} on dim : {}",
                expected_cosine, computed_cosine, dim
            );
        }

        let cosine_normalized: distances::Result<f32> =
            MinMaxCosineNormalized::evaluate(v1.reborrow(), v2.reborrow());
        let cosine_normalized = cosine_normalized.unwrap();
        let expected_cos_normalized = 1.0 - expected_ip;
        assert!(
            ((expected_cos_normalized - cosine_normalized).abs() / expected_cos_normalized.abs())
                < 1e-6,
            "CosineNormalized distance (f32) failed: expected {}, got {} on dim : {}",
            expected_cos_normalized,
            cosine_normalized,
            dim
        );

        //Calculate inner product with full precision vector
        let mut fp_query = FullQuery::new_in(dim, GlobalAllocator).unwrap();
        fp_query.vector_mut().copy_from_slice(&original1);
        *fp_query.meta_mut() = FullQueryMeta {
            norm_squared: norm1_squared,
            sum: original1.iter().sum::<f32>(),
        };

        let fp_ip: distances::Result<f32> = MinMaxIP::evaluate(fp_query.reborrow(), v2.reborrow());
        let fp_ip = fp_ip.unwrap();
        assert!(
            (expected_ip - (-fp_ip)).abs() / expected_ip.abs() < 1e-3,
            "Inner product (f32) failed: expected {}, got {} on dim : {}",
            -expected_ip,
            fp_ip,
            dim
        );

        let fp_l2: distances::Result<f32> =
            MinMaxL2Squared::evaluate(fp_query.reborrow(), v2.reborrow());
        let fp_l2 = fp_l2.unwrap();
        assert!(
            ((fp_l2 - expected_l2).abs() / expected_l2) < 1e-3,
            "L2 distance (f32) failed: expected {}, got {} on dim : {}",
            expected_l2,
            computed_l2_f32,
            dim
        );

        let fp_cosine: distances::Result<f32> =
            MinMaxCosine::evaluate(fp_query.reborrow(), v2.reborrow());
        let fp_cosine = fp_cosine.unwrap();
        let diff = (fp_cosine - expected_cosine).abs();
        assert!(
            (diff / expected_cosine) < 1e-3 || diff <= 1e-6,
            "Cosine distance (f32) failed: expected {}, got {} on dim : {}",
            expected_cosine,
            fp_cosine,
            dim
        );

        let fp_cos_norm: distances::Result<f32> =
            MinMaxCosineNormalized::evaluate(fp_query.reborrow(), v2.reborrow());
        let fp_cos_norm = fp_cos_norm.unwrap();
        assert!(
            (((1.0 - expected_ip) - fp_cos_norm).abs() / (1.0 - expected_ip)) < 1e-3,
            "Cosine distance (f32) failed: expected {}, got {} on dim : {}",
            (1.0 - expected_ip),
            fp_cos_norm,
            dim
        );

        //Test `decompress_into` to make sure it outputs tje full-precision vector correctly.
        let meta = v1.meta();
        let v1_ref = DataRef::new(v1.vector(), &meta);
        let dim = v1_ref.len();
        let mut boxed = vec![0f32; dim + 1];

        let pre = v1_ref.decompress_into(&mut boxed);
        assert_eq!(
            pre.unwrap_err(),
            DecompressError::LengthMismatch(dim, dim + 1)
        );
        let pre = v1_ref.decompress_into(&mut boxed[..dim - 1]);
        assert_eq!(
            pre.unwrap_err(),
            DecompressError::LengthMismatch(dim, dim - 1)
        );
        let pre = v1_ref.decompress_into(&mut boxed[..dim]);
        assert!(pre.is_ok());

        boxed
            .iter()
            .zip(original1.iter())
            .for_each(|(x, y)| assert!((*x - *y).abs() <= 1e-6));

        // Verify `read_dimension` is correct.
        let mut bytes = vec![0u8; Data::canonical_bytes(dim)];
        let mut data = DataMutRef::from_canonical_front_mut(bytes.as_mut_slice(), dim).unwrap();
        data.set_meta(meta);

        let pre = MinMaxCompensation::read_dimension(&bytes);
        assert!(pre.is_ok());
        let read_dim = pre.unwrap();
        assert_eq!(read_dim, dim);

        let pre = MinMaxCompensation::read_dimension(&[0_u8; 2]);
        assert_eq!(pre.unwrap_err(), MetaParseError::NotCanonical(2));
    }

    cfg_if::cfg_if! {
        if #[cfg(miri)] {
            // The max dim does not need to be as high for `CompensatedVectors` because they
            // defer their distance function implementation to `BitSlice`, which is more
            // heavily tested.
            const TRIALS: usize = 2;
        } else {
            const TRIALS: usize = 10;
        }
    }

    macro_rules! test_minmax_compensated {
        ($name:ident, $nbits:literal, $seed:literal) => {
            #[test]
            fn $name() {
                let mut rng = StdRng::seed_from_u64($seed);
                const MAX_DIM: usize = (bit_scale::<$nbits>() as usize);
                for dim in 1..=MAX_DIM {
                    for _ in 0..TRIALS {
                        test_minmax_compensated_vectors::<$nbits, _>(dim, &mut rng);
                    }
                }
            }
        };
    }
    test_minmax_compensated!(unsigned_minmax_compensated_test_u1, 1, 0xa33d5658097a1c35);
    test_minmax_compensated!(unsigned_minmax_compensated_test_u2, 2, 0xaedf3d2a223b7b77);
    test_minmax_compensated!(unsigned_minmax_compensated_test_u4, 4, 0xf60c0c8d1aadc126);
    test_minmax_compensated!(unsigned_minmax_compensated_test_u8, 8, 0x09fa14c42a9d7d98);

    /// Test the heterogeneous MinMax kernel for N-bit queries × M-bit database vectors.
    ///
    /// Verifies that `kernel::<N, M, _>` produces inner-product and squared-L2
    /// results matching the full-precision reference, for random codes and
    /// random compensation coefficients.
    fn test_minmax_heterogeneous_kernel<const NBITS: usize, const MBITS: usize, R>(
        dim: usize,
        rng: &mut R,
    ) where
        Unsigned: Representation<NBITS> + Representation<MBITS>,
        InnerProduct: for<'a, 'b> PureDistanceFunction<
                BitSlice<'a, NBITS, Unsigned>,
                BitSlice<'b, MBITS, Unsigned>,
                distances::MathematicalResult<u32>,
            >,
        R: Rng,
    {
        let (v_query, original1) = random_minmax_vector::<NBITS>(dim, rng);
        let (v_data, original2) = random_minmax_vector::<MBITS>(dim, rng);

        // ── Inner Product ──
        let expected_ip: f32 = original1.iter().zip(&original2).map(|(x, y)| x * y).sum();
        let computed_ip = kernel(v_query.reborrow(), v_data.reborrow(), |v, _, _| v)
            .unwrap()
            .into_inner();
        assert!(
            (expected_ip - computed_ip).abs() / expected_ip.abs().max(1e-10) < 1e-6,
            "Heterogeneous IP ({},{}) failed: expected {}, got {} on dim: {}",
            NBITS,
            MBITS,
            expected_ip,
            computed_ip,
            dim,
        );
    }

    macro_rules! test_minmax_heterogeneous {
        ($name:ident, $N:literal, $M:literal, $seed:literal) => {
            #[test]
            fn $name() {
                let mut rng = StdRng::seed_from_u64($seed);
                // Use the smaller bit width's scale as max dimension.
                const MAX_DIM: usize = bit_scale::<$M>() as usize;
                for dim in 1..=MAX_DIM {
                    for _ in 0..TRIALS {
                        test_minmax_heterogeneous_kernel::<$N, $M, _>(dim, &mut rng);
                    }
                }
            }
        };
    }

    test_minmax_heterogeneous!(minmax_heterogeneous_8x4, 8, 4, 0xb7c3d9e5f1a20864);
    test_minmax_heterogeneous!(minmax_heterogeneous_8x2, 8, 2, 0x4e8f2c6a1d3b5079);
    test_minmax_heterogeneous!(minmax_heterogeneous_8x1, 8, 1, 0x1b0f2c614d2a7141);
}