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

use std::num::NonZeroUsize;

use diskann_utils::views::MatrixView;

use crate::matrix_kernels::{
    bounds::{self, Bound},
    num::{DimK, Elements},
    ptr::Slice,
};

/// A view over an unpacked 2-dimensional matrix.
///
/// This struct avoids mentioning "rows" and "columns" as its use is contextual. Instead,
/// consider memory as divided contiguous "bands", each containing "k" elements of type `T`.
/// Bands are laid out sequentially and contiguously.
///
/// [`Self::extent`] describes the number of bands.
///
/// For row-major matrices, "bands" is interpreted as "rows". For column-major, "bands" is
/// interpreted as "columns".
///
/// # Class Invariants
///
/// The bound `ptr.len()` must be equal to `self.extent * self.k`.
#[derive(Debug, Clone, Copy)]
pub(crate) struct View<'a, T> {
    ptr: Slice<'a, T>,
    extent: NonZeroUsize,
    k: Bound,
}

impl<'a, T> View<'a, T> {
    /// Construct a [`View`] from a [`MatrixView`].
    ///
    /// Since [`MatrixView`]s are interpreted as "row-major", the value `k` will be derived
    /// from `v.ncols()` and the extent will be taken from `v.nrows()`.
    ///
    /// Returns `None` if either dimension is zero.
    pub(crate) fn from_matrix_view(v: MatrixView<'a, T>) -> Option<Self> {
        let extent = NonZeroUsize::new(v.nrows())?;
        let k = DimK::new(NonZeroUsize::new(v.ncols())?);

        // SAFETY: The `MatrixView` ensures that the inner slice has size `extent * k`.
        Some(unsafe { Self::new(Slice::new(v.into_inner()), extent, k) })
    }

    /// Construct a new [`View`] over `ptr`.
    ///
    /// # Safety
    ///
    /// The true length of `ptr` must be exactly `extent * k`.
    pub(crate) unsafe fn new(ptr: Slice<'a, T>, extent: NonZeroUsize, k: DimK) -> Self {
        let k: usize = k.value().get();
        bounds::check_eq!(ptr.len(), extent.get() * k);

        // SAFETY: Inherited from caller
        unsafe { Self::new_inner(ptr, extent, Bound::new(k)) }
    }

    /// # Safety
    ///
    /// The true length of `ptr` must be exactly `extent * k`.
    unsafe fn new_inner(ptr: Slice<'a, T>, extent: NonZeroUsize, k: Bound) -> Self {
        bounds::check_eq!(ptr.len(), Bound::new(extent.get()) * k);
        Self { ptr, extent, k }
    }

    /// View the underlying memory as a `&[T]`.
    ///
    /// # Safety
    ///
    /// `k` must equal the contraction dimension tracked by [`Self::k`].
    pub(in crate::matrix_kernels) unsafe fn as_std_slice(&self, k: DimK) -> &[T] {
        let len = self.stride(k) * self.extent().get();

        // SAFETY: By class invariant, the true ize f the underlying slice will be `len`.
        unsafe { self.ptr.as_std_slice(len.value()) }
    }

    /// Return the number of contiguous bands in `self`.
    ///
    /// Each band contains [`Self::k`] elements.
    pub(in crate::matrix_kernels) const fn extent(&self) -> NonZeroUsize {
        self.extent
    }

    /// Return the number of elements in each "band" of `self`.
    ///
    /// This is tracked as a [`Bound`]. Its provenance comes from the constructors of `self`.
    pub(in crate::matrix_kernels) const fn k(&self) -> Bound {
        self.k
    }

    /// Return the number of elements in each "band" of self.
    ///
    /// The value `k` must be equal to [`Self::k`].
    pub(in crate::matrix_kernels) fn stride(&self, k: DimK) -> Elements<T> {
        bounds::check_eq!(self.k, k.value());
        Elements::new(k.value().get())
    }

    /// Partition the matrix into sub-views each containing at most `sub_extent` bands, with
    /// the last one potentially containing fewer.
    ///
    /// Provide all sub-views to `f` in memory order. The second argument to `f` is the index
    /// of the first band in the sub-view within `self`.
    ///
    /// # Safety
    ///
    /// The bound [`Self::k`] must be equal to `k`.
    #[inline(always)]
    pub(in crate::matrix_kernels) unsafe fn visit_sub_views<F>(
        &self,
        sub_extent: NonZeroUsize,
        k: DimK,
        mut f: F,
    ) where
        F: FnMut(View<'_, T>, usize),
    {
        let stride = self.stride(k);

        let mut i = 0;

        // The loop bound is a bit funky because it is setup to give us a `NonZeroUsize` for
        // free. Once it returns `None`, we know `i == self.extent()` and we're done.
        while let Some(remaining) = NonZeroUsize::new(self.extent().get() - i) {
            let this_extent = remaining.min(sub_extent);

            // SAFETY: If `k` is correct:
            //
            // * The pointer offset and truncation are valid.
            // * The `Slice` provided to `new_inner` has a length of `this_extent * k`.
            let sub = unsafe {
                Self::new_inner(
                    self.ptr
                        .add(stride * i)
                        .truncate(stride * this_extent.get()),
                    this_extent,
                    self.k(),
                )
            };

            f(sub, i);

            i += this_extent.get();
        }
    }

    /// Partition the matrix into panels each containing exactly `EXTENT` bands.
    ///
    /// Provide all panels to `visitor` in memory order. The visitor receives the index of
    /// each panel's first band within `self`.
    ///
    /// If [`Self::extent`] is not a multiple of `EXTENT`, then a [`Remainder`] will be
    /// returned for the remaining bands. The remainder starts immediately after the visited
    /// panels.
    ///
    /// # Safety
    ///
    /// The bound [`Self::k`] must be equal to `k`.
    #[inline(always)]
    #[must_use = "the remainder needs to be handled separately"]
    pub(in crate::matrix_kernels) unsafe fn visit_panels<const EXTENT: usize>(
        &self,
        k: DimK,
        mut visitor: impl PanelVisitor<T, EXTENT>,
    ) -> Option<Remainder<'_, T, EXTENT>> {
        const { assert!(EXTENT > 0) };

        let full_groups = self.extent().get() - self.extent().get() % EXTENT;
        let stride = self.stride(k);

        for r in (0..full_groups).step_by(EXTENT) {
            // SAFETY: By class invariant, `self.ptr.len() == self.extent * self.k`.
            //
            // The caller asserts that `k == self.k`.
            //
            // Since `r < self.extent()`:
            //
            // * The pointer offset is valid.
            // * The truncation is valid.
            // * The size of the resulting slice is equal to `EXTENT * k`.
            let sub = unsafe {
                Panel::new_inner(
                    self.ptr
                        .add(stride * r)
                        .truncate(Elements::new(EXTENT) * k.value().get()),
                    self.k(),
                )
            };

            visitor.visit(sub, r);
        }

        NonZeroUsize::new(self.extent().get() - full_groups).map(|remaining| {
            // SAFETY: Following the logic above, the pointer offset, truncation, and
            // length are all correct.
            //
            // Further, `remaining` is guaranteed to be strictly less than `EXTENT`.
            unsafe {
                Remainder::new_inner(
                    self.ptr
                        .add(stride * full_groups)
                        .truncate(Elements::new(remaining.get()) * k.value().get()),
                    full_groups,
                    remaining,
                    self.k(),
                )
            }
        })
    }
}

/// A visitor for [`View::visit_panels`].
///
/// This is expressed as a custom trait rather than a closure because rustc/LLVM do not
/// seem to reliably inline closures, which is necessary when embedding visitation in a
/// context with `target_feature` enabled.
pub(in crate::matrix_kernels) trait PanelVisitor<T, const N: usize> {
    /// Visit a [`Panel`] from [`View::visit_panels`]. Argument `start` gives the index
    /// of the first band in `panel` in the parent [`View`].
    fn visit(&mut self, panel: Panel<'_, T, N>, start: usize);
}

impl<T, const N: usize, F> PanelVisitor<T, N> for F
where
    F: FnMut(Panel<'_, T, N>, usize),
{
    #[inline(always)]
    fn visit(&mut self, panel: Panel<'_, T, N>, start: usize) {
        (self)(panel, start)
    }
}

#[cfg(test)]
impl<'a, T> View<'a, T> {
    fn checked_as_std_slice(&self) -> &[T] {
        let k = DimK::from_bound(self.k());
        // SAFETY: Checked in test builds.
        unsafe { self.as_std_slice(k) }
    }

    fn checked_visit_sub_views<F>(&self, sub_extent: NonZeroUsize, f: F)
    where
        F: FnMut(View<'_, T>, usize),
    {
        let k = DimK::from_bound(self.k());
        // SAFETY: Checked in test builds.
        unsafe { self.visit_sub_views(sub_extent, k, f) }
    }

    fn checked_visit_panels<const EXTENT: usize>(
        &self,
        f: impl FnMut(Panel<'_, T, EXTENT>, usize),
    ) -> Option<Remainder<'_, T, EXTENT>> {
        let k = DimK::from_bound(self.k());
        // SAFETY: Checked in test builds.
        unsafe { self.visit_panels(k, f) }
    }
}

/// A block of `EXTENT` bands of a matrix of type `T`.
///
/// # Class Invariants
///
/// The bound `ptr.len()` must be equal to `EXTENT * self.k`.
#[derive(Debug, Clone, Copy)]
pub(in crate::matrix_kernels) struct Panel<'a, T, const EXTENT: usize> {
    ptr: Slice<'a, T>,
    k: Bound,
}

impl<'a, T, const EXTENT: usize> Panel<'a, T, EXTENT> {
    /// Construct a new [`Panel`].
    ///
    /// # Safety
    ///
    /// The bound `ptr.len()` must be equal to exactly `k * EXTENT`.
    #[cfg(test)]
    pub(in crate::matrix_kernels) unsafe fn new(ptr: Slice<'a, T>, k: DimK) -> Self {
        let k: usize = k.value().get();
        bounds::check_eq!(ptr.len(), k * EXTENT);
        // SAFETY: Inherited from caller.
        unsafe { Self::new_inner(ptr, Bound::new(k)) }
    }

    /// # Safety
    ///
    /// The bound `ptr.len()` must be equal to exactly `k * EXTENT`.
    unsafe fn new_inner(ptr: Slice<'a, T>, k: Bound) -> Self {
        bounds::check_eq!(ptr.len(), k * Bound::new(EXTENT));
        Self { ptr, k }
    }

    /// Return the base span of this panel as a [`Slice`].
    pub(in crate::matrix_kernels) const fn as_ptr(&self) -> Slice<'_, T> {
        self.ptr
    }

    /// Return the contraction dimension of `self`.
    ///
    /// This is inherited from all constructors.
    pub(in crate::matrix_kernels) const fn k(&self) -> Bound {
        self.k
    }

    /// Return the number of elements in each band of `self`.
    ///
    /// `k` must equal the contraction dimension tracked by [`Self::k`].
    pub(in crate::matrix_kernels) fn stride(&self, k: DimK) -> Elements<T> {
        bounds::check_eq!(self.k(), k);
        Elements::new(k.value().get())
    }

    /// Return the contents of `self` as a `&[T]`.
    ///
    /// # Safety
    ///
    /// `k` must equal the contraction dimension tracked by [`Self::k`].
    #[cfg(test)]
    unsafe fn as_std_slice(&self, k: DimK) -> &[T] {
        bounds::check_eq!(self.k(), k);

        // SAFETY: Since `k == self.k`, the underlying slice has a length of exactly
        // `EXTENT * k`.
        unsafe { self.ptr.as_std_slice(EXTENT * k.value().get()) }
    }
}

#[cfg(test)]
impl<T, const EXTENT: usize> Panel<'_, T, EXTENT> {
    fn checked_as_std_slice(&self) -> &[T] {
        let k = DimK::from_bound(self.k());
        // SAFETY: Checked in test builds.
        unsafe { self.as_std_slice(k) }
    }
}

/// A nonempty trailing view containing fewer than `CAPACITY` bands.
///
/// # Class Invariants
///
/// * The bound `ptr.len()` must be equal to `self.extent * self.k`.
/// * `self.extent` must be **strictly less** than `CAPACITY`.
#[derive(Debug, Clone, Copy)]
pub(in crate::matrix_kernels) struct Remainder<'a, T, const CAPACITY: usize> {
    ptr: Slice<'a, T>,
    _start: usize,
    extent: NonZeroUsize,
    k: Bound,
}

impl<'a, T, const CAPACITY: usize> Remainder<'a, T, CAPACITY> {
    /// # Safety
    ///
    /// * `ptr.len()` must be equal to `extent * k`.
    /// * `extent` must be strictly less than `CAPACITY`.
    unsafe fn new_inner(ptr: Slice<'a, T>, start: usize, extent: NonZeroUsize, k: Bound) -> Self {
        bounds::check_eq!(ptr.len(), Bound::new(extent.get()) * k);
        bounds::check_lt!(
            Bound::new(extent.get()),
            CAPACITY,
            "remainder must be strictly less than capacity"
        );

        Self {
            ptr,
            _start: start,
            extent,
            k,
        }
    }

    /// Return the number of bands in `self`.
    ///
    /// This is guaranteed to be less than `CAPACITY`.
    pub(in crate::matrix_kernels) fn extent(&self) -> NonZeroUsize {
        self.extent
    }

    /// Return the index of the first band in `self`'s immediate parent [`View`].
    #[cfg_attr(
        not(test),
        expect(unused, reason = "this completes an API but is not used yet")
    )]
    pub(in crate::matrix_kernels) fn start(&self) -> usize {
        self._start
    }

    /// Return the number of elements in each "band" of `self`.
    fn k(&self) -> Bound {
        self.k
    }

    /// Return a [`Panel`] if [`Self::extent`] is equal to `EXTENT`.
    ///
    /// Otherwise, returns `None`.
    pub(in crate::matrix_kernels) fn try_as_panel<const EXTENT: usize>(
        self,
    ) -> Option<Panel<'a, T, EXTENT>> {
        if self.extent().get() == EXTENT {
            // SAFETY: By class invariant, `self.ptr.len() == EXTENT * self.k()`.
            Some(unsafe { Panel::new_inner(self.ptr, self.k()) })
        } else {
            None
        }
    }
}

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

#[cfg(test)]
mod test {
    use super::*;
    use diskann_utils::views::{Init, Matrix};

    use crate::matrix_kernels::test_util::{assert_contains, panic_message_for};

    #[test]
    fn test_visit_panels() {
        for nrows in (1..100).step_by(7) {
            for ncols in (1..20).step_by(3) {
                test_visit_panels_inner(
                    NonZeroUsize::new(nrows).unwrap(),
                    NonZeroUsize::new(ncols).unwrap(),
                    format_args!("ncols = {ncols}, nrows = {nrows}"),
                )
            }
        }
    }

    fn test_visit_panels_inner(
        nrows: NonZeroUsize,
        ncols: NonZeroUsize,
        ctx: std::fmt::Arguments<'_>,
    ) {
        let mat = {
            let mut i = 0.0;
            let init = Init(|| {
                let v = i;
                i += 1.0;
                v
            });
            Matrix::new(init, nrows.get(), ncols.get())
        };

        let view = View::from_matrix_view(mat.as_view()).unwrap();
        assert_eq!(view.extent().get(), mat.nrows());
        assert_eq!(view.k().value(), mat.ncols());

        assert_eq!(
            view.checked_as_std_slice(),
            mat.as_slice(),
            "underlying slices must be unchanged -- {ctx}",
        );

        visit_panels::<1>(view, mat.as_view(), ctx);
        visit_panels::<2>(view, mat.as_view(), ctx);
        visit_panels::<3>(view, mat.as_view(), ctx);
        visit_panels::<4>(view, mat.as_view(), ctx);
        visit_panels::<6>(view, mat.as_view(), ctx);
    }

    fn visit_panels<const N: usize>(
        dut: View<'_, f32>,
        reference: MatrixView<'_, f32>,
        ctx: std::fmt::Arguments<'_>,
    ) {
        let mut count = 0;
        let ncols = reference.ncols();

        let visitor = |panel: Panel<'_, f32, N>, start| {
            assert_eq!(start, count, "{ctx}");
            let s = panel.checked_as_std_slice();
            assert_eq!(s, &reference.as_slice()[ncols * start..ncols * (start + N)],);

            count += N;
        };

        let remainder = dut.checked_visit_panels::<N>(visitor);

        match remainder {
            None => assert!(dut.extent().get().is_multiple_of(N), "{ctx}"),
            Some(remainder) => {
                assert_eq!(remainder.extent().get(), dut.extent().get() % N, "{ctx}");

                let start = remainder.start();
                assert_eq!(start, count, "{ctx}");
                assert_eq!(start + remainder.extent().get(), reference.nrows(), "{ctx}");

                let expected = &reference.as_slice()[ncols * start..];

                let mut passed = false;

                macro_rules! check {
                    ($N:literal) => {
                        if let Some(panel) = remainder.try_as_panel::<$N>() {
                            assert_eq!(
                                panel.checked_as_std_slice(),
                                expected,
                                "-- N = {}, {ctx}",
                                $N,
                            );
                            assert!(!passed, "-- N = {}, {ctx}", $N);
                            passed = true;
                        }
                    };
                }

                check!(1);
                check!(2);
                check!(3);
                check!(4);
                check!(5);
                check!(6);

                assert!(passed, "{ctx}");
            }
        }
    }

    #[test]
    fn test_visit_sub_views() {
        for nrows in (1..100).step_by(7) {
            for ncols in (1..20).step_by(3) {
                test_sub_views_inner(
                    NonZeroUsize::new(nrows).unwrap(),
                    NonZeroUsize::new(ncols).unwrap(),
                    format_args!("ncols = {ncols}, nrows = {nrows}"),
                )
            }
        }
    }

    fn test_sub_views_inner(
        nrows: NonZeroUsize,
        ncols: NonZeroUsize,
        ctx: std::fmt::Arguments<'_>,
    ) {
        let mat = {
            let mut i = 0.0;
            let init = Init(|| {
                let v = i;
                i += 1.0;
                v
            });
            Matrix::new(init, nrows.get(), ncols.get())
        };

        let view = View::from_matrix_view(mat.as_view()).unwrap();
        assert_eq!(view.extent().get(), mat.nrows());
        assert_eq!(view.k().value(), mat.ncols());

        assert_eq!(
            view.checked_as_std_slice(),
            mat.as_slice(),
            "underlying slices must be unchanged -- {ctx}",
        );

        let sub_extents: Vec<NonZeroUsize> = [
            1,
            nrows.get().div_ceil(10) + 1,
            nrows.get() / 2,
            nrows.get() - 1,
            nrows.get(),
            nrows.get() + 1,
        ]
        .map(NonZeroUsize::new)
        .into_iter()
        .flatten()
        .collect();

        for sub_extent in sub_extents.into_iter() {
            let mut count = 0;
            let nc = ncols.get();

            view.checked_visit_sub_views(sub_extent, |v, start| {
                assert_eq!(start, count, "{ctx}");
                assert!(v.extent() <= sub_extent, "{ctx}");
                assert_eq!(
                    v.checked_as_std_slice(),
                    &mat.as_slice()[start * nc..(start + v.extent().get()) * nc],
                );

                count += v.extent().get();
            });

            assert_eq!(count, nrows.get());
        }
    }

    #[test]
    fn test_rejects_inconsistent_lengths() {
        let data = [0u8; 7];
        let extent = NonZeroUsize::new(2).unwrap();
        let k = DimK::new(NonZeroUsize::new(3).unwrap());

        for len in [5, 7] {
            let message = panic_message_for(|| {
                // SAFETY: The deliberate length mismatch is caught while bounds are retained.
                let _ = unsafe { View::new(Slice::new(&data[..len]), extent, k) };
            });
            assert_contains!(message, "equal to 6");

            let message = panic_message_for(|| {
                // SAFETY: The deliberate length mismatch is caught while bounds are retained.
                let _ =
                    unsafe { Panel::<_, 2>::new_inner(Slice::new(&data[..len]), Bound::new(3)) };
            });
            assert_contains!(message, "equal to 6");

            let message = panic_message_for(|| {
                // SAFETY: The deliberate length mismatch is caught while bounds are retained.
                let _ = unsafe {
                    Remainder::<_, 3>::new_inner(Slice::new(&data[..len]), 0, extent, Bound::new(3))
                };
            });
            assert_contains!(message, "equal to 6");
        }

        let message = panic_message_for(|| {
            // SAFETY: The deliberate capacity mismatch is caught while bounds are retained.
            let _ = unsafe {
                Remainder::<_, 2>::new_inner(Slice::new(&data[..6]), 0, extent, Bound::new(3))
            };
        });
        assert_contains!(message, "remainder must be strictly less than capacity");
    }

    #[test]
    fn test_rejects_inconsistent_k() {
        let data = [0u8; 6];
        let actual_k = DimK::new(NonZeroUsize::new(3).unwrap());
        let wrong_k = DimK::new(NonZeroUsize::new(2).unwrap());

        // SAFETY: `data` contains two bands of three elements.
        let view = unsafe { View::new(Slice::new(&data), NonZeroUsize::new(2).unwrap(), actual_k) };

        assert_k_mismatch(|| {
            let _ = view.stride(wrong_k);
        });
        assert_k_mismatch(|| {
            // SAFETY: The deliberate K mismatch is caught before materialization.
            let _ = unsafe { view.as_std_slice(wrong_k) };
        });
        assert_k_mismatch(|| {
            // SAFETY: The deliberate K mismatch is caught before pointer arithmetic.
            unsafe {
                view.visit_sub_views(NonZeroUsize::new(1).unwrap(), wrong_k, |_, _| {});
            }
        });
        assert_k_mismatch(|| {
            // SAFETY: The deliberate K mismatch is caught before pointer arithmetic.
            let _ = unsafe { view.visit_panels::<2>(wrong_k, |_: Panel<'_, _, _>, _| {}) };
        });

        // SAFETY: `data` contains exactly two bands of three elements.
        let panel = unsafe { Panel::<_, 2>::new_inner(Slice::new(&data), Bound::new(3)) };

        assert_k_mismatch(|| {
            let _ = panel.stride(wrong_k);
        });
        assert_k_mismatch(|| {
            // SAFETY: The deliberate K mismatch is caught before materialization.
            let _ = unsafe { panel.as_std_slice(wrong_k) };
        });
    }

    fn assert_k_mismatch(f: impl FnOnce() + std::panic::UnwindSafe) {
        let message = panic_message_for(f);
        assert_contains!(message, "equal to 2");
    }
}