diskann-quantization 0.62.0

DiskANN3 is a composable library for bringing scalable, accurate and cost-effective vector indexing to multiple databases.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

// A collection of test helpers to ensure uniformity across tables.

use std::num::NonZeroUsize;

use diskann_utils::views::rowmajor::{self, Matrix, MatrixMut};
use diskann_vector::{PureDistanceFunction, distance};
#[cfg(not(miri))]
use rand::seq::IndexedRandom;
use rand::{
    Rng, SeedableRng,
    distr::{Distribution, Uniform},
    rngs::StdRng,
};

use crate::{
    product::tables::BasicTable,
    test_util::Check,
    traits::CompressInto,
    views::{self, ChunkOffsets, ChunkOffsetsView},
};

//////////////////////
// Distance Helpers //
//////////////////////

/// To test the implementation of distances, we need a way to seed the source pivot table
/// with known contents.
///
/// The layout of the pivot table will look like this:
///
///      chunk 0          chunk 1       ...        chunk K
///
/// | S    S    ... | S+1   S+1   ... | ... | S+K    S+K    ... |   pivot 0
/// | S+1  S+1  ... | S+2   S+2   ... | ... | S+K+1  S+K+1  ... |   pivot 1
/// | S+2  S+2  ... | S+3   S+3   ... | ... | S+K+2  S+K+2  ... |   pivot 2
/// |     ...       |       ...       | ... |        ...        |     ...
/// | S+N  S+N  ... | S+N+1 S+N+1 ... | ... | S+K+N  S+K+N  ... |   pivot N
///
/// where
///
/// * S: The configured start value for chunk 0, pivot 0 (i.e., [`Self::start`])
/// * K + 1: The number of PQ chunks ([`Self::chunks`]).
/// * N + 1: The number of PQ pivots ([`Self::pivots`]).
#[derive(Debug, Clone)]
pub(super) struct DistanceTestTable {
    /// The chunking schema.
    pub(super) offsets: ChunkOffsets,
    /// The number of pivots per chunk.
    pub(super) pivots: usize,
    /// The starting value for chunk 0, pivot 0.
    pub(super) start: f32,
}

/// The position within the chunking scheme.
#[derive(Debug, Clone, Copy)]
struct Location {
    /// The chunk number.
    chunk: usize,
    /// The pivot.
    pivot: usize,
}

#[derive(Debug, Clone)]
pub(super) struct UniformFloat(Uniform<usize>);

impl UniformFloat {
    pub(super) fn new(low: usize, high: usize) -> Result<Self, rand::distr::uniform::Error> {
        Uniform::new(low, high).map(Self)
    }
}

impl Distribution<f32> for UniformFloat {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
        self.0.sample(rng) as f32
    }
}

type DriveFn<'a> = &'a mut (dyn FnMut(&[u8], &[f32], std::fmt::Arguments<'_>) + 'a);

impl DistanceTestTable {
    pub(super) fn new(dim: usize, chunks: usize, pivots: usize, start: f32) -> Self {
        Self {
            offsets: ChunkOffsets::partition(
                NonZeroUsize::new(dim).unwrap(),
                NonZeroUsize::new(chunks).unwrap(),
            )
            .unwrap(),
            pivots,
            start,
        }
    }

    pub(super) fn from_chunk_dims(chunk_dims: &[usize], pivots: usize, start: f32) -> Self {
        let mut offsets = Vec::with_capacity(chunk_dims.len() + 1);
        let mut offset = 0usize;
        offsets.push(offset);
        for &dim in chunk_dims {
            offset = offset.checked_add(dim).unwrap();
            offsets.push(offset);
        }

        Self {
            offsets: ChunkOffsets::new(offsets.into_boxed_slice()).unwrap(),
            pivots,
            start,
        }
    }

    /// This is mainly a convenience so we don't always have to import `StdRng` and
    /// `SeedableRng` and all that jazz.
    pub(super) fn rng(&self, seed: u64) -> StdRng {
        StdRng::seed_from_u64(seed)
    }

    pub(super) fn offsets(&self) -> ChunkOffsetsView<'_> {
        self.offsets.as_view()
    }

    pub(super) fn chunks(&self) -> usize {
        self.offsets.len()
    }

    pub(super) fn dim(&self) -> usize {
        self.offsets.dim()
    }

    pub(super) fn pivots(&self) -> usize {
        self.pivots
    }

    fn value(&self, loc: Location) -> f32 {
        (loc.chunk + loc.pivot) as f32 + self.start
    }

    pub(super) fn basic_table(&self) -> BasicTable {
        // This creates a base vector like
        // |  chunk 0  |  chunk 1  |  ... |  chunk K  |
        // | 0 0 ... 0 | 1 1 ... 1 |  ... | K K ... K |
        let mut base = Vec::<f32>::new();
        for i in 0..self.offsets.len() {
            let v = (i as f32) + self.start;
            for _ in self.offsets.at(i) {
                base.push(v);
            }
        }

        // Use our base vector to build the rest of the pivot matrix.
        let pivots = rowmajor::Owned::from_fn(self.pivots(), self.dim(), |rc| {
            (rc.row as f32) + base[rc.col]
        });

        BasicTable::new(pivots, self.offsets().to_owned()).unwrap()
    }

    pub(super) fn expected_vector_into(&self, v: &mut [f32], codes: &[u8]) {
        assert_eq!(v.len(), self.dim());
        assert_eq!(codes.len(), self.chunks());

        let mut i = 0;
        for (chunk, pivot) in codes.iter().copied().enumerate() {
            let pivot = usize::from(pivot);
            assert!(pivot < self.pivots());
            let loc = Location { chunk, pivot };

            for _ in self.offsets.at(chunk) {
                v[i] = self.value(loc);
                i += 1;
            }
        }
    }

    pub(super) fn drive(
        &self,
        num_trials: usize,
        rng: &mut StdRng,
        f: DriveFn<'_>,
        ctx: std::fmt::Arguments<'_>,
    ) {
        // Run two fixed trials - one with all zeros and one with the max setting.
        //
        // Then we perform random trials.
        let mut codes = vec![0u8; self.chunks()];
        let mut vector = vec![0.0; self.dim()];
        self.expected_vector_into(&mut vector, &codes);
        f(&codes, &mut vector, format_args!("{ctx}, all zeros"));

        let max = u8::try_from(self.pivots() - 1).unwrap();
        codes.fill(max);
        self.expected_vector_into(&mut vector, &codes);
        f(&codes, &mut vector, format_args!("{ctx}, all {max}"));

        // Begin random trials.
        let dist = Uniform::new(0, self.pivots()).unwrap();
        for trial in 0..num_trials {
            codes
                .iter_mut()
                .for_each(|c| *c = u8::try_from(dist.sample(rng)).unwrap());
            self.expected_vector_into(&mut vector, &codes);
            f(
                &codes,
                &mut vector,
                format_args!("{ctx}, trial {} of {}", trial + 1, num_trials),
            );
        }
    }

    pub(super) fn drive_unary(
        &self,
        num_trials: usize,
        rng: &mut StdRng,
        check: Check,
        reference: &dyn Fn(&[f32]) -> f32,
        dut: &mut dyn FnMut(&[u8]) -> f32,
        ctx: std::fmt::Arguments<'_>,
    ) {
        let mut f = |codes: &[u8], vector: &[f32], ctx: std::fmt::Arguments<'_>| {
            let expected = reference(vector);
            let got = dut(codes);

            if let Err(reason) = check.check(got, expected) {
                panic!("Check failed: {} -- {}", reason, ctx);
            }
        };

        self.drive(num_trials, rng, &mut f, ctx)
    }

    #[expect(clippy::too_many_arguments, reason = "this is a test function")]
    pub(super) fn drive_query_like(
        &self,
        num_queries: usize,
        num_trials: usize,
        rng: &mut StdRng,
        check: Check,
        f: &dyn Fn(&[f32], &[f32]) -> f32,
        dut: &mut dyn QueryLike,
        ctx: std::fmt::Arguments<'_>,
    ) {
        let dist = UniformFloat::new(0, self.chunks() + self.pivots()).unwrap();
        let mut query = vec![0.0f32; self.dim()];
        for trial in 0..num_queries {
            query.iter_mut().for_each(|q| *q = dist.sample(rng));

            dut.preprocess(&query);
            self.drive_unary(
                num_trials,
                rng,
                check,
                &|vector: &[f32]| f(&query, vector),
                &mut |code| dut.evaluate(code),
                format_args!("{ctx}, query {} of {}", trial + 1, num_queries),
            )
        }
    }

    pub(super) fn drive_self_like(
        &self,
        num_trials: usize,
        rng: &mut StdRng,
        check: Check,
        f: &dyn Fn(&[f32], &[f32]) -> f32,
        dut: &mut dyn SelfLike,
        ctx: std::fmt::Arguments<'_>,
    ) {
        let mut run_check = |lhs_code: &[u8],
                             lhs_vector: &[f32],
                             rng: &mut StdRng,
                             ctx: std::fmt::Arguments<'_>| {
            self.drive(
                num_trials,
                rng,
                &mut |rhs_code: &[u8], rhs_vector: &[f32], ctx: std::fmt::Arguments<'_>| {
                    let expected = f(lhs_vector, rhs_vector);
                    let got = dut.evaluate(lhs_code, rhs_code);

                    if let Err(reason) = check.check(got, expected) {
                        panic!("Check failed: {} -- {}", reason, ctx);
                    }
                },
                ctx,
            );
        };

        let mut a_code = vec![0u8; self.chunks()];
        let mut a_vector = vec![0.0; self.dim()];
        self.expected_vector_into(&mut a_vector, &a_code);

        // Test with all zeros.
        run_check(
            &a_code,
            &a_vector,
            rng,
            format_args!("{ctx}, all-zeros lhs"),
        );

        // Test with all max.
        let max = u8::try_from(self.pivots() - 1).unwrap();
        a_code.fill(max);
        self.expected_vector_into(&mut a_vector, &a_code);
        run_check(
            &a_code,
            &a_vector,
            rng,
            format_args!("{ctx}, all-{max} lhs"),
        );

        // Begin random trials.
        let dist = Uniform::new(0, self.pivots()).unwrap();
        for _ in 0..num_trials {
            a_code
                .iter_mut()
                .for_each(|c| *c = u8::try_from(dist.sample(rng)).unwrap());
            self.expected_vector_into(&mut a_vector, &a_code);

            run_check(&a_code, &a_vector, rng, ctx);
        }
    }
}

pub(super) fn squared_l2(x: &[f32], y: &[f32]) -> f32 {
    distance::SquaredL2::evaluate(x, y)
}

pub(super) fn inner_product(x: &[f32], y: &[f32]) -> f32 {
    distance::InnerProduct::evaluate(x, y)
}

pub(super) fn cosine(x: &[f32], y: &[f32]) -> f32 {
    distance::Cosine::evaluate(x, y)
}

/// A trait modeling query-like distances with split pre-processing and evaluation.
pub(super) trait QueryLike {
    fn preprocess(&mut self, query: &[f32]);
    fn evaluate(&mut self, code: &[u8]) -> f32;
}

/// A trait modeling self-like distances with split pre-processing and evaluation.
pub(super) trait SelfLike {
    fn evaluate(&mut self, a: &[u8], b: &[u8]) -> f32;
}

/////////////////////////
// Compression Helpers //
/////////////////////////

// TESTING STRATEGY:
//
// We need the following to test the block compression primitive:
//
// 1. A collection of pivots with known entries.
// 2. A data corpus where we know the mapping of chunks and rows to a center in the
//    previously mentioned known collection of pivots.
//
// This test fulfills these goals in the following way:
//
// ## Pivot Seeding
//
// Use a barrel-shifting approach to seeding the contents of each chunk pivots.
//
// Chunk 0 will contain the following values:
// ```
// 0.25     -0.25      0.25     -0.25     ...  +/- 0.25
// 1.25      0.25      1.25      0.25     ...  1.0 +/- 0.25
// ...
// L + 0.25  L - 0.25  L + 0.25  L - 0.25 ...  L +/- 0.25
// ```
// where Chunk 0 has (L+1) centers (of any dimension).
//
// The integer values are offset by `0.25` to yield a non-zero distance and (in this
// example) are configured so a query with all entries equal to `I <= L` will be mapped to
// row `I`.
//
// Chunk 1 will contain the following:
// ```
// 1.25      0.25      1.25      0.25     ...  1.0 +/- 0.25
// 2.25      1.25      2.25      1.25     ...  2.0 +/- 0.25
// ...
// L + 0.25  L - 0.25  L + 0.25  L - 0.25 ...  L +/- 0.25
// 0.25     -0.25      0.25     -0.25     ...    +/- 0.25
// ```
// That is, Chunk 1 will have the same contents Chunk 0, but the first row of Chunk 0 will
// be moved to the end.
//
// Chunk 2 will continue the pattern by moving the first row of Chunk 1 to the end.
//
// This pattern allows us to compute which center a properly seeded dataset chunk should
// match while providing enough entropy to make sure intermediate values are being
// computed properly.
//
// ## Data Seeding
//
// We will keep data seeding simple, using a seeded random number generate to pick a
// value between O and L and initialize all dimensions with that value.
//
// During seeding, we will record this value so that results yielded by the compression
// algorithm can be checked.
//
// The formula for going from a chunk with index "C" with the assigned value "K" is:
// ```math
// K - C mod (L + 1)
// ```

/// Seed pivot tables for the provided schema using the strategy outlined in the
/// introduction documentation to the test module.
pub(super) fn create_pivot_tables(
    schema: ChunkOffsets,
    num_centers: usize,
) -> (rowmajor::Owned<f32>, ChunkOffsets) {
    let mut pivots = rowmajor::Owned::<f32>::from_element(num_centers, schema.dim(), 0.0);

    (0..schema.len()).for_each(|chunk| {
        let range = schema.at(chunk);

        (0..num_centers).for_each(|center| {
            let buffer = &mut pivots.row_mut(center)[range.clone()];

            // It's okay if this conversion is lossy (though the magnitude of the
            // numbers involved means that this is almost certainly a lossless
            // conversion).
            //
            // The "remainder" operation is what performs the "barrel shifting"
            // for the centers.
            let base = ((center + chunk) % num_centers) as f32;
            buffer.iter_mut().enumerate().for_each(|(dim, b)| {
                // Flip-flop adding and subtracting 0.25.
                *b = if dim % 2 == 0 {
                    base + 0.25
                } else {
                    base - 0.25
                };
            });
        });
    });

    (pivots, schema)
}

/// Initialize a dataset for the provided schema using the strategy outlined in
/// the test module introduction documentation.
///
/// Returns:
///
/// * The initialized dataset as a rowmajor::Owned.
/// * The expected center as a rowmajor::Owned.
pub(super) fn create_dataset<R: Rng>(
    schema: ChunkOffsetsView<'_>,
    num_centers: usize,
    num_data: usize,
    rng: &mut R,
) -> (rowmajor::Owned<f32>, rowmajor::Owned<usize>) {
    let mut data = rowmajor::Owned::<f32>::from_element(num_data, schema.dim(), 0.0);
    let mut expected = rowmajor::Owned::<usize>::from_element(num_data, schema.len(), 0);

    let dist = Uniform::new(0, num_centers).unwrap();
    for row_index in 0..data.nrows() {
        let mut row_view = views::MutChunkView::new(data.row_mut(row_index), schema).unwrap();
        for chunk in 0..schema.len() {
            let value = rng.sample(dist);
            row_view[chunk].fill(value as f32);

            // Compute the expected value based on the rotation scheme used in pivot
            // seeding.
            let value: i64 = value.try_into().unwrap();
            let num_centers: i64 = num_centers.try_into().unwrap();
            let chunk_i64: i64 = chunk.try_into().unwrap();

            let expected_index: u64 = (value - chunk_i64)
                .rem_euclid(num_centers)
                .try_into()
                .unwrap();

            *expected.element_mut(row_index, chunk) = expected_index as usize;
        }
    }

    (data, expected)
}

/////////////////////////////////////////
// Testing `CompressInto<[f32], [u8]>` //
/////////////////////////////////////////

// A cantralized test for error handling in `CompressInto<[f32], [u8]>`
pub(super) fn check_pqtable_single_compression_errors<T>(
    build: &dyn Fn(rowmajor::Owned<f32>, ChunkOffsets) -> T,
    context: &dyn std::fmt::Display,
) where
    T: for<'a, 'b> CompressInto<&'a [f32], &'b mut [u8]>,
{
    let dim = 10;
    let num_chunks = 3;
    let offsets = ChunkOffsets::new(Box::new([0, 4, 9, 10])).unwrap();

    // Set up `ncenters > 256`.
    {
        let pivots = rowmajor::Owned::from_element(257, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = vec![f32::default(); dim];
        let mut output = vec![u8::MAX; num_chunks];
        let result = table.compress_into(input.as_slice(), output.as_mut_slice());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "num centers (257) must be at most 256 to compress into a byte vector",
            "{}",
            context
        );
        assert!(
            output.iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup input dim not equal to expected.
    {
        let pivots = rowmajor::Owned::from_element(10, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = vec![f32::default(); dim - 1];
        let mut output = vec![u8::MAX; num_chunks];
        let result = table.compress_into(input.as_slice(), output.as_mut_slice());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("invalid input len - expected {}, got {}", dim, dim - 1),
            "{}",
            context,
        );
        assert!(
            output.iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup output dim not equal to expected.
    {
        let pivots = rowmajor::Owned::from_element(10, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = vec![f32::default(); dim];
        let mut output = vec![u8::MAX; num_chunks - 1];
        let result = table.compress_into(input.as_slice(), output.as_mut_slice());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!(
                "invalid PQ buffer len - expected {}, got {}",
                num_chunks,
                num_chunks - 1,
            ),
            "{}",
            context,
        );
        assert!(
            output.iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context,
        );
    }

    // Infinity or NaN detection.
    {
        let offsets = ChunkOffsets::new(Box::new([
            0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
        ]))
        .unwrap();

        let (pivots, o) = create_pivot_tables(offsets.clone(), 7);
        let table = build(pivots, o);

        let mut buf: Box<[f32]> = (0..offsets.dim()).map(|_| 0.0).collect();
        let mut output: Box<[u8]> = (0..offsets.len()).map(|_| 0).collect();

        fn clear(x: &mut [f32]) {
            x.iter_mut().for_each(|i| *i = 0.0);
        }

        let mut rng = rand::rngs::StdRng::seed_from_u64(0x90a10423fdd8f1cf);
        let values = [f32::NEG_INFINITY, f32::INFINITY, f32::NAN];

        // Feed in positive infinity, negative infinity, and NaN into each chunk.
        for chunk in 0..offsets.len() {
            let range = offsets.at(chunk);
            let distribution = Uniform::new(range.start, range.end).unwrap();
            let expected = format!(
                "a value of infinity or NaN was observed while compressing chunk {}",
                chunk
            );

            for &value in values.iter() {
                clear(&mut buf);
                buf[distribution.sample(&mut rng)] = value;
                let err = table
                    .compress_into(&buf, &mut output)
                    .unwrap_err()
                    .to_string();

                assert!(
                    err.contains(&expected),
                    "wrong error message for {} - expected \"{}\", got \"{}\"",
                    value,
                    expected,
                    err
                );
            }
        }
    }
}

////////////////////////////////////////////////////////////////////
// Testing `CompressInto<rowmajor::Ref<'_, f32>, MarixView<'_, u8>>` //
////////////////////////////////////////////////////////////////////

// A cantralized test for error handling in `CompressInto<[f32], [u8]>`
#[cfg(not(miri))]
pub(super) fn check_pqtable_batch_compression_errors<T>(
    build: &dyn Fn(rowmajor::Owned<f32>, ChunkOffsets) -> T,
    context: &dyn std::fmt::Display,
) where
    T: for<'a> CompressInto<rowmajor::Ref<'a, f32>, rowmajor::Mut<'a, u8>>,
{
    let dim = 10;
    let num_chunks = 3;
    let offsets = ChunkOffsets::new(Box::new([0, 4, 9, 10])).unwrap();

    let batchsize = 10;

    // Set up `ncenters > 256`.
    {
        let pivots = rowmajor::Owned::from_element(257, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = rowmajor::Owned::from_element(batchsize, dim, f32::default());
        let mut output = rowmajor::Owned::from_element(batchsize, num_chunks, u8::MAX);
        let result = table.compress_into(input.as_view(), output.as_view_mut());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "num centers (257) must be at most 256 to compress into a byte vector",
            "{}",
            context
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup input dim not equal to expected.
    {
        let pivots = rowmajor::Owned::from_element(10, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = rowmajor::Owned::from_element(batchsize, dim - 1, f32::default());
        let mut output = rowmajor::Owned::from_element(batchsize, num_chunks, u8::MAX);
        let result = table.compress_into(input.as_view(), output.as_view_mut());
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("invalid input len - expected {}, got {}", dim, dim - 1),
            "{}",
            context,
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context
        );
    }

    // Setup output dim not equal to expected.
    {
        let pivots = rowmajor::Owned::from_element(10, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = rowmajor::Owned::from_element(batchsize, dim, f32::default());
        let mut output = rowmajor::Owned::from_element(batchsize, num_chunks - 1, u8::MAX);
        let result = table.compress_into(input.as_view(), output.as_view_mut());

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!(
                "invalid PQ buffer len - expected {}, got {}",
                num_chunks,
                num_chunks - 1,
            ),
            "{}",
            context,
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context,
        );
    }

    // Num rows are different.
    {
        let pivots = rowmajor::Owned::from_element(10, dim, 0.0);
        let table = build(pivots, offsets.clone());

        let input = rowmajor::Owned::from_element(batchsize, dim, f32::default());
        let mut output = rowmajor::Owned::from_element(batchsize - 1, num_chunks, u8::MAX);
        let result = table.compress_into(input.as_view(), output.as_view_mut());

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            format!(
                "input and output must have the same number of rows - instead, got {0} and {1} \
                 (respectively)",
                batchsize,
                batchsize - 1,
            ),
            "{}",
            context,
        );
        assert!(
            output.as_slice().iter().all(|i| *i == u8::MAX),
            "output vector should be unmodified -- {}",
            context,
        );
    }

    // Infinity and NaN detection.
    {
        let offsets = ChunkOffsets::new(Box::new([
            0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
        ]))
        .unwrap();

        let (pivots, o) = create_pivot_tables(offsets.clone(), 7);
        let table = build(pivots, o);

        let num_points = 15;
        let mut buf = rowmajor::Owned::<f32>::from_element(num_points, offsets.dim(), 0.0);
        let mut output = rowmajor::Owned::<u8>::from_element(num_points, offsets.len(), 0);

        fn clear<T: Default>(mut x: rowmajor::Mut<T>) {
            x.as_mut_slice().iter_mut().for_each(|i| *i = T::default());
        }

        let mut rng = rand::rngs::StdRng::seed_from_u64(0x8aa9f8cc50260d5c);

        let sample = [f32::NEG_INFINITY, f32::INFINITY, f32::NAN];

        // Feed in positive infinity, negative infinity, and NaN into each chunk.
        for chunk in 0..offsets.len() {
            let range = offsets.at(chunk);
            let distribution = Uniform::new(range.start, range.end).unwrap();

            for row in 0..num_points {
                clear(buf.as_view_mut());
                let value = *sample.choose(&mut rng).unwrap();
                *buf.element_mut(row, distribution.sample(&mut rng)) = value;
                let err = table
                    .compress_into(buf.as_view(), output.as_view_mut())
                    .expect_err(&format!("expected a value of {}", value));

                let message = err.to_string();
                let expected = format!(
                    "a value of infinity or NaN was observed while compressing chunk {} \
                     of batch input {}",
                    chunk, row
                );

                assert!(
                    message.contains(&expected),
                    "wrong error message - expected \"{}\", got \"{}\"",
                    expected,
                    err
                );
            }
        }
    }
}