tinymind 0.2.0

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind (C++) library, bit-exact with it
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
//! Kani proofs: properties checked for *every* input, not a sample. Run with
//! `cargo kani` (see CLAUDE.md for setup). Compiled only under `cfg(kani)`.
//!
//! Every harness also gets Kani's built-in checks for free: no panic, no
//! arithmetic overflow, no out-of-range shift, no out-of-bounds index.
//!
//! The specifications avoid the operations they check. "Wraps" is stated as a
//! congruence modulo 2^bits; "floors" and "truncates towards zero" are stated as
//! inequalities on a remainder, with the exact quotient as a free (`kani::any`)
//! variable constrained to be the only value that satisfies them. That keeps a
//! proof from restating the implementation, and keeps division (expensive for
//! the solver) out of the specification.

use crate::activation::{Activation, Relu, Sigmoid, Tanh};
use crate::lut::{SIGMOID_Q8_8, SIGMOID_Q16_16, TANH_Q8_8, TANH_Q16_16};
use crate::qformat::{Q8_8, Q16_16};

/// `x ≡ y (mod 2^bits)`.
fn congruent(x: i64, y: i64, bits: u32) -> bool {
    (x.wrapping_sub(y)).rem_euclid(1i64 << bits) == 0
}

/// Proofs for one format, generated per storage type and fractional-bit count.
macro_rules! format_proofs {
    ($module:ident, $s:ty, $frac:expr) => {
        mod $module {
            use super::congruent;
            use crate::qformat::{QValue, Scalar};

            type Q = QValue<$s, $frac>;
            const FRAC: u32 = $frac;
            const BITS: u32 = <$s>::BITS;

            fn any() -> Q {
                Q::from_raw(kani::any())
            }

            fn wide(q: Q) -> i64 {
                i64::from(q.raw())
            }

            fn bound_for_sign(a: Q) -> i64 {
                if a.raw() > 0 {
                    i64::from(<$s>::MAX)
                } else if a.raw() < 0 {
                    i64::from(<$s>::MIN)
                } else {
                    0
                }
            }

            #[kani::proof]
            fn add_and_sub_wrap() {
                let (a, b) = (any(), any());
                let mut c = a;
                c += b;
                assert!(congruent(wide(a + b), wide(a) + wide(b), BITS));
                assert!(congruent(wide(c), wide(a) + wide(b), BITS));
                assert!(congruent(wide(a - b), wide(a) - wide(b), BITS));
            }

            /// TruncatePolicy: the exact product floored to the format's
            /// resolution, then wrapped.
            #[kani::proof]
            fn mul_floors_then_wraps() {
                let (a, b) = (any(), any());
                // In i128 so the specification itself cannot overflow.
                let p = i128::from(wide(a)) * i128::from(wide(b));
                // f is the floor of p / 2^FRAC: the only integer with
                // f * 2^FRAC <= p < (f + 1) * 2^FRAC.
                let f: i64 = kani::any();
                let scaled = i128::from(f) * (1i128 << FRAC);
                kani::assume(scaled <= p && p - scaled < (1i128 << FRAC));
                assert!(congruent(wide(a * b), f, BITS));
            }

            /// Division never panics, for any operands (Kani's built-in overflow
            /// and divide-by-zero checks; no functional specification).
            #[kani::proof]
            fn div_never_panics() {
                let _ = any() / any();
                let _ = any().div_int(kani::any());
            }

            /// `from_int(n)` is n * 2^FRAC wrapped (the fixed part truncated).
            #[kani::proof]
            fn from_int_scales_then_wraps() {
                let n: i32 = kani::any();
                assert!(congruent(
                    wide(Q::from_int(n)),
                    i64::from(n) * (1i64 << FRAC),
                    BITS
                ));
            }

            /// `pow2_neg(n)` is 2^-n in the format: 2^(FRAC - n) raw, wrapped, or
            /// zero once 2^-n is below the resolution.
            #[kani::proof]
            fn pow2_neg_is_exact_or_zero() {
                let n: u32 = kani::any();
                let r = wide(Q::pow2_neg(n));
                if n > FRAC {
                    assert!(r == 0);
                } else {
                    assert!(congruent(r << n, 1i64 << FRAC, BITS + n));
                }
            }

            #[kani::proof]
            fn zero_tolerance_matches_definition() {
                let a = any();
                let tolerance = 1i64 << if FRAC > 7 { FRAC - 7 } else { 1 };
                assert!(a.is_within_zero_tolerance() == (wide(a).abs() < tolerance));
            }

            #[kani::proof]
            fn comparisons_are_on_the_raw_value() {
                let (a, b) = (any(), any());
                assert!((a < b) == (a.raw() < b.raw()));
                assert!((a == b) == (a.raw() == b.raw()));
            }
        }
    };
}

format_proofs!(q16_16, i32, 16);
format_proofs!(q8_8, i16, 8);
// Formats at the edges of the storage width: no fractional room, no integer room.
format_proofs!(q31_1, i32, 1);
format_proofs!(q1_31, i32, 31);
format_proofs!(q15_1, i16, 1);
format_proofs!(q1_15, i16, 15);

/// For every input, a table activation returns a value between its first and
/// last table entries (interpolation never overshoots), and the derivative
/// formula does not panic.
macro_rules! activation_proofs {
    ($name:ident, $q:ty, $s:ty, $act:ty, $table:expr) => {
        #[kani::proof]
        fn $name() {
            let x = <$q>::from_raw(kani::any());
            let y = <$act>::activate(x).raw();
            let first = $table[0] as $s;
            let last = $table[$table.len() - 1] as $s;
            assert!(first <= y && y <= last);
            let _ = <$act>::derivative(<$q>::from_raw(kani::any()));
        }
    };
}

activation_proofs!(tanh_q16_16_in_range, Q16_16, i32, Tanh, TANH_Q16_16);
activation_proofs!(
    sigmoid_q16_16_in_range,
    Q16_16,
    i32,
    Sigmoid,
    SIGMOID_Q16_16
);
activation_proofs!(tanh_q8_8_in_range, Q8_8, i16, Tanh, TANH_Q8_8);
activation_proofs!(sigmoid_q8_8_in_range, Q8_8, i16, Sigmoid, SIGMOID_Q8_8);

/// ReLU: max(0, x), derivative 0 or 1, for every input in both formats.
#[kani::proof]
fn relu_definition() {
    let x = Q16_16::from_raw(kani::any());
    let y = Relu::activate(x);
    assert!(y.raw() == x.raw().max(0));
    let d = Relu::derivative(y);
    assert!(d == Q16_16::ZERO || d == Q16_16::ONE);
    let x = Q8_8::from_raw(kani::any());
    assert!(Relu::activate(x).raw() == x.raw().max(0));
}

/// Int8 affine inference (`crate::int8`).
mod int8 {
    use crate::int8::{
        LUT_SIZE, QDense, QDensePerChannel, QSoftmax, Requantizer, apply_lut,
        exp_on_negative_values, multiply_by_quantized_multiplier, one_over_one_plus_x,
        rounding_divide_by_pot, rounding_high_mul_divide_by_pot, saturating_shl,
    };

    /// gemmlowp's two roundings, written as tinymind (C++)'s unit test writes them:
    /// the high multiply with a sign-dependent nudge and a division that
    /// truncates towards zero, then the remainder/threshold divide.
    fn two_step(x: i32, m: i32, e: i32) -> i32 {
        let h = if x == i32::MIN && m == i32::MIN {
            i32::MAX
        } else {
            let ab = i64::from(x) * i64::from(m);
            let nudge: i64 = if ab >= 0 { 1 << 30 } else { 1 - (1 << 30) };
            ((ab + nudge) / (1i64 << 31)) as i32
        };
        let mask = ((1u32 << e) - 1) as i32;
        let threshold = (mask >> 1) + i32::from(h < 0);
        (h >> e) + i32::from((h & mask) > threshold)
    }

    /// The requantizer's fused single-rounding path equals the two-step form
    /// wherever it is used (multiplier >= 0, 2 <= shift <= 31).
    #[kani::proof]
    fn fused_requantize_matches_two_step() {
        let (x, m, e): (i32, i32, i32) = (kani::any(), kani::any(), kani::any());
        kani::assume(m >= 0 && (2..=31).contains(&e));
        assert!(rounding_high_mul_divide_by_pot(x, m, e) == two_step(x, m, e));
    }

    /// The building blocks and the requantizer never panic, for any field
    /// values, including shifts far outside the documented range.
    #[kani::proof]
    fn requantizer_never_panics() {
        let (x, m, s): (i32, i32, i32) = (kani::any(), kani::any(), kani::any());
        let _ = rounding_divide_by_pot(x, s);
        let _ = multiply_by_quantized_multiplier(x, m, s);
        let rq = Requantizer {
            multiplier: m,
            shift: s,
            zero_point: kani::any(),
            qmin: kani::any(),
            qmax: kani::any(),
        };
        let y = rq.apply(x);
        // With qmin <= qmax the result is inside the bounds.
        if rq.qmin <= rq.qmax {
            assert!(rq.qmin <= y && y <= rq.qmax);
        }
    }

    /// The left shift in `multiply_by_quantized_multiplier` and `QAdd`:
    /// `x * 2^n` saturated to the `i32` range, for every `x` and every `n` up
    /// to 40 (each `n` above 31 takes the same branch as 31). The spec is in
    /// i128, where the product cannot overflow.
    #[kani::proof]
    fn saturating_shl_saturates() {
        let (x, n): (i32, u32) = (kani::any(), kani::any());
        kani::assume(n <= 40);
        let exact = i128::from(x) << n;
        let want = exact.clamp(i128::from(i32::MIN), i128::from(i32::MAX)) as i32;
        assert!(saturating_shl(x, n) == want);
    }

    /// Multiplication, padding and concatenation never panic, for any
    /// parameters and inputs.
    #[kani::proof]
    #[kani::unwind(4)]
    fn compose_never_panics() {
        let mul = crate::int8::QMul {
            input_a_zero_point: kani::any(),
            input_b_zero_point: kani::any(),
            requantizer: Requantizer {
                multiplier: kani::any(),
                shift: kani::any(),
                zero_point: kani::any(),
                qmin: kani::any(),
                qmax: kani::any(),
            },
        };
        let (a, b): ([i8; 2], [i8; 2]) = (kani::any(), kani::any());
        let mut y = [0i8; 2];
        mul.forward(&a, &b, &mut y);
        let x: [[[i8; 1]; 1]; 1] = kani::any();
        let mut padded = [[[0i8; 1]; 2]; 2];
        crate::int8::QPad2D::<1, 0, 0, 1> {
            pad_value: kani::any(),
        }
        .forward(&x, &mut padded);
        let concat = crate::int8::QConcat {
            input_a_zero_point: kani::any(),
            input_b_zero_point: kani::any(),
            requantizer_a: mul.requantizer,
            requantizer_b: Requantizer {
                multiplier: kani::any(),
                shift: kani::any(),
                zero_point: kani::any(),
                qmin: kani::any(),
                qmax: kani::any(),
            },
        };
        let xb: [[[i8; 2]; 1]; 1] = kani::any();
        let mut joined = [[[0i8; 3]; 1]; 1];
        concat.forward(&x, &xb, &mut joined);
    }

    /// The elementwise addition never panics, for any parameters and inputs,
    /// shifts far outside the documented range included.
    #[kani::proof]
    fn add_never_panics() {
        let add = crate::int8::QAdd {
            input_a_zero_point: kani::any(),
            input_b_zero_point: kani::any(),
            left_shift: kani::any(),
            input_a_multiplier: kani::any(),
            input_a_shift: kani::any(),
            input_b_multiplier: kani::any(),
            input_b_shift: kani::any(),
            output_requantizer: Requantizer {
                multiplier: kani::any(),
                shift: kani::any(),
                zero_point: kani::any(),
                qmin: kani::any(),
                qmax: kani::any(),
            },
        };
        let (a, b): ([i8; 1], [i8; 1]) = (kani::any(), kani::any());
        let mut y = [0i8; 1];
        add.forward(&a, &b, &mut y);
    }

    /// A dense layer never panics, for any weights, biases, inputs and
    /// requantizer.
    #[kani::proof]
    fn dense_never_panics() {
        let weights: [[i8; 3]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let with_bias: bool = kani::any();
        let layer = QDense {
            weights: &weights,
            biases: if with_bias { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizer: Requantizer {
                multiplier: kani::any(),
                shift: kani::any(),
                zero_point: kani::any(),
                qmin: kani::any(),
                qmax: kani::any(),
            },
        };
        let input: [i8; 3] = kani::any();
        let _ = layer.forward(&input);
    }

    /// The per-channel dense layer never panics either.
    #[kani::proof]
    fn dense_per_channel_never_panics() {
        let weights: [[i8; 3]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let requantizers: [Requantizer; 2] = [
            Requantizer {
                multiplier: kani::any(),
                shift: kani::any(),
                zero_point: kani::any(),
                qmin: kani::any(),
                qmax: kani::any(),
            },
            Requantizer {
                multiplier: kani::any(),
                shift: kani::any(),
                zero_point: kani::any(),
                qmin: kani::any(),
                qmax: kani::any(),
            },
        ];
        let layer = QDensePerChannel {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizers: &requantizers,
        };
        let input: [i8; 3] = kani::any();
        let _ = layer.forward(&input);
    }

    /// The softmax's exponential and reciprocal never panic, for any input,
    /// in or out of their domains.
    #[kani::proof]
    fn softmax_parts_never_panic() {
        let _ = exp_on_negative_values(kani::any());
        let _ = one_over_one_plus_x(kani::any());
    }

    /// The softmax layer never panics, for any parameters and logits.
    #[kani::proof]
    fn softmax_never_panics() {
        let softmax = QSoftmax {
            input_multiplier: kani::any(),
            input_left_shift: kani::any(),
            diff_min: kani::any(),
        };
        let input: [i8; 2] = kani::any();
        let _ = softmax.forward(&input);
    }

    #[kani::proof]
    fn lut_never_panics() {
        let lut: [i8; LUT_SIZE] = [0; LUT_SIZE];
        let _ = apply_lut(kani::any(), &lut);
    }
}

/// Trees: the walk, the ensemble and the validity checks never panic, for any
/// pool (indices in or out of range, cycles), root, class, base score and input.
mod tree {
    use crate::tree::{QDecisionTree, QGbdt, QTreeNode, walk_tree};

    fn any_node() -> QTreeNode<i8> {
        QTreeNode {
            feature: kani::any(),
            threshold: kani::any(),
            left: kani::any(),
            right: kani::any(),
            leaf_value: kani::any(),
        }
    }

    #[kani::proof]
    #[kani::unwind(7)]
    fn walk_never_panics() {
        let nodes = [any_node(), any_node(), any_node(), any_node()];
        let x: [i8; 2] = kani::any();
        let max_nodes: usize = kani::any();
        kani::assume(max_nodes <= 6);
        let _ = walk_tree(&nodes, kani::any(), &x, max_nodes);
        let tree = QDecisionTree::<'_, i8, 4, 2>::new(&nodes);
        let _ = (tree.predict(&x), tree.is_valid());
    }

    #[kani::proof]
    #[kani::unwind(5)]
    fn ensemble_never_panics() {
        let nodes = [any_node(), any_node(), any_node()];
        let roots: [i16; 2] = kani::any();
        let classes: [i16; 2] = kani::any();
        let base: [i32; 2] = kani::any();
        let gbdt = QGbdt::<'_, i8, 2, 2, 3, 2> {
            nodes: &nodes,
            tree_root: &roots,
            tree_class: &classes,
            base_score: if kani::any() { Some(&base) } else { None },
        };
        let x: [i8; 2] = kani::any();
        let _ = (gbdt.forward(&x), gbdt.predict(&x), gbdt.is_valid());
    }
}

/// Int8 convolutions and pooling: no panic for any weights, biases, inputs,
/// zero points and requantizers, with windows reaching into padding on every
/// side; the pooling divide rounds half away from zero.
mod conv {
    use crate::int8::pool::rounded_divide;
    use crate::int8::{
        FastRequantizer, QAvgPool2D, QConv2D, QConv2DPerChannel, QDepthwiseConv2D,
        QGlobalAvgPool2D, QMaxPool2D, QPointwiseConv2D, QPointwiseConv2DPerChannel, Requantizer,
    };

    fn any_requantizer() -> Requantizer {
        Requantizer {
            multiplier: kani::any(),
            shift: kani::any(),
            zero_point: kani::any(),
            qmin: kani::any(),
            qmax: kani::any(),
        }
    }

    /// A 2x2 kernel over a 2x2 input, stride 2 down with one row of padding
    /// on top, stride 1 across with one column on the right: two outputs,
    /// each with taps in the padding.
    #[kani::proof]
    #[kani::unwind(6)]
    fn conv_never_panics() {
        let weights: [[[[i8; 1]; 2]; 2]; 1] = kani::any();
        let biases: [i32; 1] = kani::any();
        let layer: QConv2D<'_, 1, 2, 2, 1, 2, 1, 1, 0, 0, 1> = QConv2D {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizer: any_requantizer(),
        };
        let input: [[[i8; 1]; 2]; 2] = kani::any();
        let mut output = [[[0i8; 1]; 2]; 1];
        layer.forward(&input, &mut output);
    }

    /// Two input channels, so the layer takes its 16-bit band path: a 2x2
    /// kernel over a 2x2 input padded on the bottom and the left, so one
    /// window lies inside the input and the others reach into the padding.
    #[kani::proof]
    #[kani::unwind(6)]
    fn conv_band_never_panics() {
        let weights: [[[[i8; 2]; 2]; 2]; 1] = kani::any();
        let biases: [i32; 1] = kani::any();
        let requantizers = [any_requantizer()];
        let layer: QConv2DPerChannel<'_, 2, 2, 2, 1, 1, 1, 0, 1, 1, 0> = QConv2DPerChannel {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizers: &requantizers,
        };
        let input: [[[i8; 2]; 2]; 2] = kani::any();
        let mut output = [[[0i8; 1]; 2]; 2];
        layer.forward(&input, &mut output);
    }

    #[kani::proof]
    #[kani::unwind(6)]
    fn conv_per_channel_never_panics() {
        let weights: [[[[i8; 1]; 2]; 2]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let requantizers = [any_requantizer(), any_requantizer()];
        let layer: QConv2DPerChannel<'_, 1, 2, 2, 2, 1, 1, 0, 1, 1, 0> = QConv2DPerChannel {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizers: &requantizers,
        };
        let input: [[[i8; 1]; 1]; 1] = kani::any();
        let mut output = [[[0i8; 2]; 1]; 1];
        layer.forward(&input, &mut output);
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn depthwise_never_panics() {
        let weights: [[[i8; 2]; 2]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let requantizers = [any_requantizer(), any_requantizer()];
        let layer: QDepthwiseConv2D<'_, 2, 2, 2, 1, 1, 1, 0, 0, 1> = QDepthwiseConv2D {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizers: &requantizers,
        };
        let input: [[[i8; 2]; 1]; 1] = kani::any();
        let mut output = [[[0i8; 2]; 1]; 1];
        layer.forward(&input, &mut output);
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn pointwise_never_panics() {
        let weights: [[i8; 2]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let input: [[[i8; 2]; 1]; 1] = kani::any();
        let mut output = [[[0i8; 2]; 1]; 1];
        let layer = QPointwiseConv2D {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizer: any_requantizer(),
        };
        layer.forward(&input, &mut output);
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn pointwise_per_channel_never_panics() {
        let weights: [[i8; 2]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let input: [[[i8; 2]; 1]; 1] = kani::any();
        let mut output = [[[0i8; 2]; 1]; 1];
        let requantizers = [any_requantizer(), any_requantizer()];
        let layer = QPointwiseConv2DPerChannel {
            weights: &weights,
            biases: if kani::any() { Some(&biases) } else { None },
            input_zero_point: kani::any(),
            requantizers: &requantizers,
        };
        layer.forward(&input, &mut output);
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn pools_never_panic() {
        let input: [[[i8; 1]; 3]; 2] = kani::any();
        let mut output = [[[0i8; 1]; 2]; 1];
        QMaxPool2D::<2, 2, 1, 1>.forward(&input, &mut output);
        let avg = QAvgPool2D::<2, 2, 1, 1> {
            qmin: kani::any(),
            qmax: kani::any(),
        };
        avg.forward(&input, &mut output);
        let global = QGlobalAvgPool2D {
            qmin: kani::any(),
            qmax: kani::any(),
        };
        let _ = global.forward(&input);
    }

    /// The prepared requantizer the conv layers use (tinymind (C++)'s
    /// `FastRequantizer`) gives exactly `Requantizer::apply`'s result, for every
    /// accumulator and every requantizer it accepts.
    #[kani::proof]
    fn fast_requantizer_matches_apply() {
        let rq = any_requantizer();
        if let Some(fast) = FastRequantizer::new(&rq) {
            let acc: i32 = kani::any();
            assert!(fast.apply(acc) == rq.apply(acc));
        }
    }

    /// Total for every numerator and denominator.
    #[kani::proof]
    fn rounded_divide_never_panics() {
        let _ = rounded_divide(kani::any(), kani::any());
    }

    /// For a positive denominator and a numerator the bias cannot push past
    /// the `i32` rails, the result is the nearest integer to `n / d`, ties
    /// away from zero.
    #[kani::proof]
    fn rounded_divide_rounds_half_away() {
        let (n, d): (i32, i32) = (kani::any(), kani::any());
        kani::assume(d > 0 && i64::from(n).abs() <= i64::from(i32::MAX - d / 2));
        let r = i64::from(rounded_divide(n, d));
        let (n, d) = (i64::from(n), i64::from(d));
        let error = r * d - n;
        assert!(2 * error.abs() <= d);
        if 2 * error.abs() == d {
            assert!((r * d).abs() > n.abs());
        }
    }
}