tinymind 0.3.5

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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
//! 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));
            }

            /// `mul_round_toward_zero` (C++ `multiplyRoundTowardZero`): the
            /// exact product rounded toward zero to the format's resolution,
            /// then wrapped; the `Scalar` method is the same function.
            #[kani::proof]
            fn mul_round_toward_zero_then_wraps() {
                let (a, b) = (any(), any());
                let p = i128::from(wide(a)) * i128::from(wide(b));
                // z is p / 2^FRAC rounded toward zero: for p >= 0 the integer
                // with z * 2^FRAC <= p < (z + 1) * 2^FRAC, for p < 0 the one
                // with (z - 1) * 2^FRAC < p <= z * 2^FRAC.
                let z: i64 = kani::any();
                let scaled = i128::from(z) * (1i128 << FRAC);
                if p >= 0 {
                    kani::assume(scaled <= p && p - scaled < (1i128 << FRAC));
                } else {
                    kani::assume(p <= scaled && scaled - p < (1i128 << FRAC));
                }
                let r = a.mul_round_toward_zero(b);
                assert!(congruent(wide(r), z, BITS));
                assert!(<Q as Scalar>::mul_round_toward_zero(a, b) == r);
            }

            /// `mul_round_nearest` (C++ `multiplyRoundNearest`): the exact
            /// product rounded to nearest, ties away from zero, to the
            /// format's resolution, then wrapped; the `Scalar` method is the
            /// same function.
            #[kani::proof]
            fn mul_round_nearest_then_wraps() {
                let (a, b) = (any(), any());
                let p = i128::from(wide(a)) * i128::from(wide(b));
                // n is p / 2^FRAC rounded to nearest, ties away from zero:
                // |p - n * 2^FRAC| <= 2^(FRAC - 1), and a tie (equality)
                // takes the n further from zero, the one with p's sign.
                let n: i64 = kani::any();
                let scaled = i128::from(n) * (1i128 << FRAC);
                let half = 1i128 << (FRAC - 1);
                if p >= 0 {
                    kani::assume(p - half < scaled && scaled <= p + half);
                } else {
                    kani::assume(p - half <= scaled && scaled < p + half);
                }
                let r = a.mul_round_nearest(b);
                assert!(congruent(wide(r), n, BITS));
                assert!(<Q as Scalar>::mul_round_nearest(a, b) == r);
            }

            /// 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);
    }

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

    /// One step of a 1-input, 2-unit int8 LSTM cell with cell state `C`, for
    /// any weights, biases, LUTs, multipliers, shifts, zero points, bounds,
    /// input and state: it never panics, and with `cell_qmin <= cell_qmax` the
    /// new cell state lies within them.
    fn lstm_step<C: crate::int8::CellStorage + kani::Arbitrary + PartialOrd>() {
        let (w_in, w_rec, biases): ([[[i8; 1]; 2]; 4], [[[i8; 2]; 2]; 4], [[i32; 2]; 4]) =
            (kani::any(), kani::any(), kani::any());
        let (sigmoid, tanh, tanh_cell): ([i8; 256], [i8; 256], [i8; 256]) =
            (kani::any(), kani::any(), kani::any());
        let cell = crate::int8::QLstmCell::<'_, 1, 2, C> {
            input_weights: &w_in,
            recurrent_weights: &w_rec,
            biases: &biases,
            input_zero_point: kani::any(),
            hidden_zero_point: kani::any(),
            cell_zero_point: kani::any(),
            input_to_lut_multiplier: kani::any(),
            input_to_lut_shift: kani::any(),
            recurrent_to_lut_multiplier: kani::any(),
            recurrent_to_lut_shift: kani::any(),
            sigmoid_lut: &sigmoid,
            tanh_lut: &tanh,
            f_times_c_multiplier: kani::any(),
            f_times_c_shift: kani::any(),
            i_times_g_multiplier: kani::any(),
            i_times_g_shift: kani::any(),
            cell_qmin: kani::any(),
            cell_qmax: kani::any(),
            cell_to_tanh_multiplier: kani::any(),
            cell_to_tanh_shift: kani::any(),
            tanh_cell_lut: &tanh_cell,
            output_requantizer: any_requantizer(),
        };
        let x: [i8; 1] = kani::any();
        let mut h: [i8; 2] = kani::any();
        let mut c: [C; 2] = kani::any();
        cell.forward(&x, &mut h, &mut c);
        if cell.cell_qmin <= cell.cell_qmax {
            assert!(
                c.iter()
                    .all(|v| cell.cell_qmin <= *v && *v <= cell.cell_qmax)
            );
        }
    }

    #[kani::proof]
    #[kani::unwind(5)]
    fn lstm_int8_cell_never_panics() {
        lstm_step::<i8>();
    }

    #[kani::proof]
    #[kani::unwind(5)]
    fn lstm_int16_cell_never_panics() {
        lstm_step::<i16>();
    }

    /// One step of a 1-input, 2-unit int8 GRU cell, for any weights, biases,
    /// LUTs, multipliers, shifts, zero points, bounds, input and state: it
    /// never panics, and with `output_qmin <= output_qmax` the new hidden state
    /// lies within them.
    #[kani::proof]
    #[kani::unwind(5)]
    fn gru_cell_never_panics() {
        let (w_in, w_rec, biases): ([[[i8; 1]; 2]; 3], [[[i8; 2]; 2]; 3], [[i32; 2]; 3]) =
            (kani::any(), kani::any(), kani::any());
        let (sigmoid, tanh): ([i8; 256], [i8; 256]) = (kani::any(), kani::any());
        let cell = crate::int8::QGruCell::<'_, 1, 2> {
            input_weights: &w_in,
            recurrent_weights: &w_rec,
            biases: &biases,
            input_zero_point: kani::any(),
            hidden_zero_point: kani::any(),
            input_to_lut_multiplier: kani::any(),
            input_to_lut_shift: kani::any(),
            recurrent_to_lut_multiplier: kani::any(),
            recurrent_to_lut_shift: kani::any(),
            sigmoid_lut: &sigmoid,
            tanh_lut: &tanh,
            r_times_h_multiplier: kani::any(),
            r_times_h_shift: kani::any(),
            one_minus_z_times_n_multiplier: kani::any(),
            one_minus_z_times_n_shift: kani::any(),
            z_times_h_multiplier: kani::any(),
            z_times_h_shift: kani::any(),
            output_qmin: kani::any(),
            output_qmax: kani::any(),
        };
        let x: [i8; 1] = kani::any();
        let mut h: [i8; 2] = kani::any();
        cell.forward(&x, &mut h);
        if cell.output_qmin <= cell.output_qmax {
            assert!(
                h.iter()
                    .all(|v| cell.output_qmin <= *v && *v <= cell.output_qmax)
            );
        }
    }

    /// 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());
    }
}

/// Q-learning: no panic for any Q values, rates, exploration percentage,
/// states, actions and rewards, whatever the environment returns (more valid
/// actions than `ACTIONS`, random integers out of range).
mod qlearn {
    use crate::activation::{Linear, Relu};
    use crate::neuralnet::MultilayerPerceptron;
    use crate::qformat::Q8_8;
    use crate::qlearn::{Environment, Experience, QLearner, QNetwork, QTable, QValues};

    struct AnyEnvironment;

    impl Environment<3> for AnyEnvironment {
        fn valid_actions(&self, _state: usize, actions: &mut [usize; 3]) -> usize {
            *actions = kani::any();
            kani::any()
        }

        fn random_int(&mut self, _min: usize, _max: usize) -> usize {
            kani::any()
        }
    }

    fn any_q8_8() -> Q8_8 {
        Q8_8::from_raw(kani::any())
    }

    #[kani::proof]
    #[kani::unwind(5)]
    fn qlearner_never_panics() {
        let table = QTable::<Q8_8, 2, 3>::from_values([
            [any_q8_8(), any_q8_8(), any_q8_8()],
            [any_q8_8(), any_q8_8(), any_q8_8()],
        ]);
        let mut q = QLearner::<Q8_8, AnyEnvironment, 2, 3, _>::with_q_values(
            AnyEnvironment,
            table,
            any_q8_8(),
            any_q8_8(),
            kani::any(),
        );
        if kani::any() {
            q.start_new_episode();
        }
        let _ = q.take_action(kani::any());
        q.update_from_experience(&Experience {
            state: kani::any(),
            action: kani::any(),
            reward: any_q8_8(),
            new_state: kani::any(),
        });
        let _ = q.take_action(kani::any());
    }

    fn encode(state: usize) -> [Q8_8; 1] {
        [Q8_8::from_raw(state as i16)]
    }

    /// The deep Q-network's reads and its training step never panic, for any
    /// weights, rates, states, actions (in or out of range), targets and
    /// refresh interval (1 input, 1 hidden neuron, 2 actions).
    #[kani::proof]
    #[kani::unwind(4)]
    fn qnetwork_never_panics() {
        let mut net = MultilayerPerceptron::<Q8_8, Relu, Linear, 1, 1, 2>::new(any_q8_8);
        net.set_learning_rate(any_q8_8());
        net.set_momentum_rate(any_q8_8());
        net.set_acceleration_rate(any_q8_8());
        let mut q = QNetwork::new(net, encode, kani::any());
        let _ = q.q_value(kani::any(), kani::any());
        let _ = q.future_q_value(kani::any(), kani::any());
        q.set_q_value(kani::any(), kani::any(), any_q8_8());
        q.set_q_value(kani::any(), kani::any(), any_q8_8());
        let _ = q.future_q_value(kani::any(), kani::any());
    }
}

/// 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);
    }

    /// The filter-pair path needs rows of `PAIR_MIN_CHANNELS` (32); three
    /// pixels also reach its odd last pixel. (The odd last filter runs the
    /// one-filter path the harnesses above cover.)
    #[kani::proof]
    #[kani::unwind(34)]
    fn pointwise_pairs_never_panic() {
        let weights: [[i8; 32]; 2] = kani::any();
        let biases: [i32; 2] = kani::any();
        let input: [[[i8; 32]; 1]; 3] = kani::any();
        let mut output = [[[0i8; 2]; 1]; 3];
        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));
        }
    }

    /// The one-rounding form the dense layers run when every requantizer
    /// allows it gives exactly `Requantizer::apply`'s result, for every
    /// accumulator; for any other requantizer it never panics.
    #[kani::proof]
    fn apply_one_rounding_matches_apply() {
        let rq = any_requantizer();
        let acc: i32 = kani::any();
        let r = rq.apply_one_rounding(acc);
        if rq.one_rounding() {
            assert!(r == 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());
        }
    }
}

/// LSTM network: no panic for any weights, rates, clipping range, inputs and
/// targets, over two steps with training and a reset (one input, one cell, one
/// output in Q8.8; every loop is over the network's const sizes).
mod recurrent {
    use crate::activation::{Sigmoid, Tanh};
    use crate::qformat::Q8_8;
    use crate::recurrent::LstmNetwork;

    fn any_q8_8() -> Q8_8 {
        Q8_8::from_raw(kani::any())
    }

    #[kani::proof]
    #[kani::unwind(6)]
    fn lstm_never_panics() {
        let mut nn = LstmNetwork::<Q8_8, Tanh, Sigmoid, 1, 1, 1>::new(any_q8_8);
        nn.set_learning_rate(any_q8_8());
        nn.set_momentum_rate(any_q8_8());
        nn.set_acceleration_rate(any_q8_8());
        if kani::any() {
            nn.set_gradient_clip(Some((any_q8_8(), any_q8_8())));
        }
        nn.feed_forward(&[any_q8_8()]);
        nn.train(&[any_q8_8()]);
        if kani::any() {
            nn.reset_state();
        }
        nn.feed_forward(&[any_q8_8()]);
        let _ = (nn.calculate_error(&[any_q8_8()]), nn.learned_values());
    }
}

/// The quantized FFT: the Q1.15 multiply floors, the saturation clamps, and the
/// transforms never panic or overflow for any samples and any twiddle tables
/// (`-32768` included), which the built-in overflow checks cover; magnitudes
/// saturate only at the both-rails bin.
mod fft {
    use crate::int8::QFft1D;
    use crate::int8::fft::{q15_mul, saturate_i16};

    /// `q15_mul(a, b)` is `floor(a * b / 2^15)`: the only `q` with
    /// `q * 2^15 <= a * b < (q + 1) * 2^15`.
    #[kani::proof]
    fn q15_mul_floors() {
        let (a, b): (i16, i16) = (kani::any(), kani::any());
        let p = i64::from(a) * i64::from(b);
        let q = i64::from(q15_mul(i32::from(a), i32::from(b)));
        assert!(q * 32768 <= p && p < (q + 1) * 32768);
    }

    #[kani::proof]
    fn saturate_i16_clamps() {
        let v: i32 = kani::any();
        let s = i32::from(saturate_i16(v));
        assert!(s == v.max(-32768).min(32767));
    }

    fn any_fft_4<'a>(c: &'a [i16; 2], s: &'a [i16; 2]) -> QFft1D<'a, 4, 2> {
        QFft1D {
            twiddle_cos: c,
            twiddle_sin: s,
        }
    }

    #[kani::proof]
    #[kani::unwind(5)]
    fn fft_forward_never_panics() {
        let (c, s): ([i16; 2], [i16; 2]) = (kani::any(), kani::any());
        let (mut re, mut im): ([i16; 4], [i16; 4]) = (kani::any(), kani::any());
        any_fft_4(&c, &s).forward(&mut re, &mut im);
    }

    #[kani::proof]
    #[kani::unwind(5)]
    fn fft_inverse_never_panics() {
        let (c, s): ([i16; 2], [i16; 2]) = (kani::any(), kani::any());
        let (mut re, mut im): ([i16; 4], [i16; 4]) = (kani::any(), kani::any());
        any_fft_4(&c, &s).inverse(&mut re, &mut im);
    }

    /// Skipping the saturation when both products fit in `i16` (the
    /// Cortex-M0+ form) gives exactly the saturating form's results, for any
    /// samples and any twiddle: one butterfly (N = 2), which every butterfly
    /// of every length runs.
    #[kani::proof]
    #[kani::unwind(3)]
    fn fft_saturation_skip_is_exact() {
        let (c, s): ([i16; 1], [i16; 1]) = (kani::any(), kani::any());
        let fft: QFft1D<'_, 2, 1> = QFft1D {
            twiddle_cos: &c,
            twiddle_sin: &s,
        };
        let (re, im): ([i16; 2], [i16; 2]) = (kani::any(), kani::any());
        let (mut r0, mut i0) = (re, im);
        fft.transform::<true, false>(&mut r0, &mut i0);
        let (mut r1, mut i1) = (re, im);
        fft.transform::<true, true>(&mut r1, &mut i1);
        // Element by element: `==` on arrays is a memcmp loop.
        assert!(r0[0] == r1[0] && r0[1] == r1[1] && i0[0] == i1[0] && i0[1] == i1[1]);
    }

    /// `magnitude_squared` is `min(r^2 + i^2, i32::MAX)`, spec in i128.
    #[kani::proof]
    #[kani::unwind(3)]
    fn fft_magnitude_saturates() {
        let t = [32767i16];
        let fft: QFft1D<'_, 2, 1> = QFft1D {
            twiddle_cos: &t,
            twiddle_sin: &t,
        };
        let (re, im): ([i16; 2], [i16; 2]) = (kani::any(), kani::any());
        let mut m = [0i32; 2];
        fft.magnitude_squared(&re, &im, &mut m);
        for i in 0..2 {
            let exact =
                i128::from(re[i]) * i128::from(re[i]) + i128::from(im[i]) * i128::from(im[i]);
            assert!(i128::from(m[i]) == exact.min(i128::from(i32::MAX)));
        }
    }
}

/// The fine-tuning heads: the softmax's exp stays in [0, 1] for every
/// non-positive argument and is 0 at or below -6; the dense head (both losses)
/// and the prototype head never panic for any parameters, inputs, classes
/// (out of range too), rates and caps, and no class count exceeds the cap.
mod head {
    use crate::head::{DenseHead, ManhattanDistance, PrototypeHead, SoftmaxExp, SquaredErrorLoss};
    use crate::qformat::{Q8_8, Q16_16, Scalar};

    fn any_q8_8() -> Q8_8 {
        Q8_8::from_raw(kani::any())
    }

    #[kani::proof]
    fn softmax_exp_in_unit_range() {
        let x = Q16_16::from_raw(kani::any());
        kani::assume(x <= Q16_16::ZERO);
        let y = x.softmax_exp();
        assert!(y >= Q16_16::ZERO && y <= Q16_16::ONE);
        if x <= Q16_16::from_int(-6) {
            assert!(y == Q16_16::ZERO);
        }
        let x = any_q8_8();
        kani::assume(x <= Q8_8::ZERO);
        let y = x.softmax_exp();
        assert!(y >= Q8_8::ZERO && y <= Q8_8::ONE);
        if x <= Q8_8::from_int(-6) {
            assert!(y == Q8_8::ZERO);
        }
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn dense_head_never_panics() {
        let mut head = DenseHead::<Q8_8, 2, 2>::from_weights(
            [[any_q8_8(), any_q8_8()], [any_q8_8(), any_q8_8()]],
            [any_q8_8(), any_q8_8()],
        );
        let input = [any_q8_8(), any_q8_8()];
        let mut deltas = [Q8_8::ZERO; 2];
        let _ = head.train_class(&input, kani::any(), any_q8_8(), Some(&mut deltas));
        let _ = head.predict(&input);
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn dense_head_squared_error_never_panics() {
        let mut head = DenseHead::<Q8_8, 2, 2, SquaredErrorLoss>::from_weights(
            [[any_q8_8(), any_q8_8()], [any_q8_8(), any_q8_8()]],
            [any_q8_8(), any_q8_8()],
        );
        let input = [any_q8_8(), any_q8_8()];
        let targets = [any_q8_8(), any_q8_8()];
        let _ = head.train(&input, &targets, any_q8_8(), None);
    }

    #[kani::proof]
    #[kani::unwind(4)]
    fn prototype_head_never_panics_and_caps_counts() {
        let mut head = PrototypeHead::<Q8_8, 2, 2, ManhattanDistance>::new();
        head.set_prototype(kani::any(), &[any_q8_8(), any_q8_8()], kani::any());
        head.set_maximum_count(kani::any());
        head.set_prototype(kani::any(), &[any_q8_8(), any_q8_8()], kani::any());
        head.learn(&[any_q8_8(), any_q8_8()], kani::any());
        head.learn(&[any_q8_8(), any_q8_8()], kani::any());
        if kani::any() {
            head.forget(kani::any());
        }
        let query = [any_q8_8(), any_q8_8()];
        let predicted = head.predict(&query);
        assert!(predicted <= 2);
        let _ = head.distance(&query, kani::any());
        assert!(head.maximum_count() >= 1);
        assert!(head.count(0) <= head.maximum_count() && head.count(1) <= head.maximum_count());
    }
}