Skip to main content

imported_cnn/common/
imported_cnn_flat_model.rs

1//! `example_cnn_flat`: generated by tinymind-rs tools/import_pytorch/tinymind_import.py.
2//! Do not edit; regenerate from the model instead.
3//!
4//! Integer-only and `no_std`: int8 inputs in the `INPUT` grid, int8 outputs in the
5//! `OUTPUT` grid (real value = `SCALE * (q - ZERO_POINT)`).
6//! Feature maps are NHWC without the batch axis: `[row][column][channel]`.
7
8// Not every program uses every constant.
9#![allow(dead_code)]
10
11use tinymind::int8::{LUT_SIZE, QAvgPool2D, QConv2D, QDense, Requantizer, apply_lut, qrelu};
12
13/// The input feature map: 9 rows, 11 columns, 2 channels.
14pub type Input = [[[i8; 2]; 11]; 9];
15/// Number of outputs.
16pub const NUM_OUTPUTS: usize = 2;
17
18/// Scale of the `input` tensor.
19pub const INPUT_SCALE: f32 = 0.007843138;
20/// Zero point of the `input` tensor.
21pub const INPUT_ZERO_POINT: i8 = -1;
22/// Scale of the `relu1` tensor.
23pub const RELU1_SCALE: f32 = 0.0047058826;
24/// Zero point of the `relu1` tensor.
25pub const RELU1_ZERO_POINT: i8 = -128;
26/// Scale of the `pool1` tensor.
27pub const POOL1_SCALE: f32 = 0.0047058826;
28/// Zero point of the `pool1` tensor.
29pub const POOL1_ZERO_POINT: i8 = -128;
30/// Scale of the `flat` tensor.
31pub const FLAT_SCALE: f32 = 0.0047058826;
32/// Zero point of the `flat` tensor.
33pub const FLAT_ZERO_POINT: i8 = -128;
34/// Scale of the `relu2` tensor.
35pub const RELU2_SCALE: f32 = 0.0032607613;
36/// Zero point of the `relu2` tensor.
37pub const RELU2_ZERO_POINT: i8 = -1;
38/// Scale of the `fc2` tensor.
39pub const FC2_SCALE: f32 = 0.0034576417;
40/// Zero point of the `fc2` tensor.
41pub const FC2_ZERO_POINT: i8 = 86;
42/// Scale of the `out` tensor.
43pub const OUT_SCALE: f32 = 0.0078125;
44/// Zero point of the `out` tensor.
45pub const OUT_ZERO_POINT: i8 = 0;
46/// Scale of the output (`out`).
47pub const OUTPUT_SCALE: f32 = OUT_SCALE;
48/// Zero point of the output (`out`).
49pub const OUTPUT_ZERO_POINT: i8 = OUT_ZERO_POINT;
50
51/// `conv1` int8 weights, `[filter][row][column][channel]` (weight scale 0.003937008).
52pub static CONV1_WEIGHTS: [[[[i8; 2]; 2]; 3]; 5] = [
53    [
54        [[0, 111], [-48, -32]],
55        [[79, -79], [-64, 48]],
56        [[-111, -95], [16, 127]],
57    ],
58    [
59        [[-127, -16], [95, 111]],
60        [[-48, 64], [79, -79]],
61        [[32, 48], [-111, 0]],
62    ],
63    [
64        [[16, 127], [-32, -16]],
65        [[95, -64], [-48, 64]],
66        [[-95, -79], [32, -127]],
67    ],
68    [
69        [[-111, 0], [111, 127]],
70        [[-32, 79], [95, -64]],
71        [[48, 64], [-95, 16]],
72    ],
73    [
74        [[32, -127], [-16, 0]],
75        [[111, -48], [-32, 79]],
76        [[-79, -64], [48, -111]],
77    ],
78];
79/// `conv1` int32 biases (scale = input scale * weight scale).
80pub static CONV1_BIASES: [i32; 5] = [-8096, 4048, -4048, 8096, 0];
81/// `conv1`: 3x2 convolution, 5 filters, stride 2x1, padding 0/0/0/0 (top/bottom/left/right), from `input` (9x11x2) to 4x10x5, then ReLU into `relu1`.
82pub static CONV1: QConv2D<'static, 2, 3, 2, 5, 2, 1, 0, 0, 0, 0> = QConv2D {
83    weights: &CONV1_WEIGHTS,
84    biases: Some(&CONV1_BIASES),
85    input_zero_point: -1,
86    requantizer: Requantizer {
87        multiplier: 1803660800,
88        shift: 7,
89        zero_point: -128,
90        qmin: -128,
91        qmax: 127,
92    },
93};
94
95/// `pool1`: 2x2 average pooling, stride 2x2, from `relu1` (4x10x5) to 2x5x5, in the same grid.
96pub static POOL1: QAvgPool2D<2, 2, 2, 2> = QAvgPool2D {
97    qmin: -128,
98    qmax: 127,
99};
100
101/// `fc1` int8 weights, `[output][input]` (weight scale 0.001968504).
102pub static FC1_WEIGHTS: [[i8; 50]; 6] = [
103    [
104        79, -79, 32, 48, -111, 0, 16, 127, -32, -16, 95, -64, -48, 64, -95, -79, 32, -127, -111, 0,
105        111, 127, -32, 79, 95, -64, 48, 64, -95, 16, 32, -127, -16, 0, 111, -48, -32, 79, -79, -64,
106        48, -111, -95, 16, 127, -127, -16, 95, 111, -48,
107    ],
108    [
109        64, 79, -79, 32, 48, -111, 0, 16, 127, -32, -16, 95, -64, -48, 64, -95, -79, 32, -127,
110        -111, 0, 111, 127, -32, 79, 95, -64, 48, 64, -95, 16, 32, -127, -16, 0, 111, -48, -32, 79,
111        -79, -64, 48, -111, -95, 16, 127, -127, -16, 95, 111,
112    ],
113    [
114        -48, 64, 79, -79, 32, 48, -111, 0, 16, 127, -32, -16, 95, -64, -48, 64, -95, -79, 32, -127,
115        -111, 0, 111, 127, -32, 79, 95, -64, 48, 64, -95, 16, 32, -127, -16, 0, 111, -48, -32, 79,
116        -79, -64, 48, -111, -95, 16, 127, -127, -16, 95,
117    ],
118    [
119        111, -48, 64, 79, -79, 32, 48, -111, 0, 16, 127, -32, -16, 95, -64, -48, 64, -95, -79, 32,
120        -127, -111, 0, 111, 127, -32, 79, 95, -64, 48, 64, -95, 16, 32, -127, -16, 0, 111, -48,
121        -32, 79, -79, -64, 48, -111, -95, 16, 127, -127, -16,
122    ],
123    [
124        95, 111, -48, 64, 79, -79, 32, 48, -111, 0, 16, 127, -32, -16, 95, -64, -48, 64, -95, -79,
125        32, -127, -111, 0, 111, 127, -32, 79, 95, -64, 48, 64, -95, 16, 32, -127, -16, 0, 111, -48,
126        -32, 79, -79, -64, 48, -111, -95, 16, 127, -127,
127    ],
128    [
129        -16, 95, 111, -48, 64, 79, -79, 32, 48, -111, 0, 16, 127, -32, -16, 95, -64, -48, 64, -95,
130        -79, 32, -127, -111, 0, 111, 127, -32, 79, 95, -64, 48, 64, -95, 16, 32, -127, -16, 0, 111,
131        -48, -32, 79, -79, -64, 48, -111, -95, 16, 127,
132    ],
133];
134/// `fc1` int32 biases (scale = input scale * weight scale).
135pub static FC1_BIASES: [i32; 6] = [-13494, 26987, 0, -26987, 13494, -13494];
136/// `fc1`: 50 -> 6, from `flat`, then ReLU into `relu2`.
137pub static FC1: QDense<'static, 50, 6> = QDense {
138    weights: &FC1_WEIGHTS,
139    biases: Some(&FC1_BIASES),
140    input_zero_point: -128,
141    requantizer: Requantizer {
142        multiplier: 1561810020,
143        shift: 8,
144        zero_point: -1,
145        qmin: -128,
146        qmax: 127,
147    },
148};
149
150/// `fc2` int8 weights, `[output][input]` (weight scale 0.007874016).
151pub static FC2_WEIGHTS: [[i8; 6]; 2] = [[-111, 0, 111, 127, -32, 79], [95, -64, 48, 64, -95, 16]];
152/// `fc2` int32 biases (scale = input scale * weight scale).
153pub static FC2_BIASES: [i32; 2] = [0, -9737];
154/// `fc2`: 6 -> 2, from `relu2`.
155pub static FC2: QDense<'static, 6, 2> = QDense {
156    weights: &FC2_WEIGHTS,
157    biases: Some(&FC2_BIASES),
158    input_zero_point: -1,
159    requantizer: Requantizer {
160        multiplier: 2041150979,
161        shift: 7,
162        zero_point: 86,
163        qmin: -128,
164        qmax: 127,
165    },
166};
167
168/// `out`: tanh table from the `fc2` grid to the `out` grid.
169pub static OUT_LUT: [i8; LUT_SIZE] = [
170    -81, -80, -80, -80, -79, -79, -79, -79, -78, -78, -78, -78, -77, -77, -77, -76, -76, -76, -76,
171    -75, -75, -75, -74, -74, -74, -73, -73, -73, -73, -72, -72, -72, -71, -71, -71, -70, -70, -70,
172    -70, -69, -69, -69, -68, -68, -68, -67, -67, -67, -66, -66, -66, -65, -65, -65, -64, -64, -64,
173    -63, -63, -63, -62, -62, -62, -61, -61, -61, -60, -60, -60, -59, -59, -59, -58, -58, -58, -57,
174    -57, -56, -56, -56, -55, -55, -55, -54, -54, -54, -53, -53, -52, -52, -52, -51, -51, -51, -50,
175    -50, -50, -49, -49, -48, -48, -48, -47, -47, -46, -46, -46, -45, -45, -45, -44, -44, -43, -43,
176    -43, -42, -42, -41, -41, -41, -40, -40, -39, -39, -39, -38, -38, -37, -37, -37, -36, -36, -35,
177    -35, -35, -34, -34, -33, -33, -32, -32, -32, -31, -31, -30, -30, -30, -29, -29, -28, -28, -27,
178    -27, -27, -26, -26, -25, -25, -24, -24, -24, -23, -23, -22, -22, -21, -21, -21, -20, -20, -19,
179    -19, -18, -18, -18, -17, -17, -16, -16, -15, -15, -15, -14, -14, -13, -13, -12, -12, -11, -11,
180    -11, -10, -10, -9, -9, -8, -8, -8, -7, -7, -6, -6, -5, -5, -4, -4, -4, -3, -3, -2, -2, -1, -1,
181    0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12,
182    12, 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18,
183];
184
185/// Run the network on int8 inputs in the `INPUT` grid.
186pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
187    let mut x0 = [[[0i8; 5]; 10]; 4];
188    CONV1.forward(input, &mut x0);
189    let mut x2 = [[[0i8; 5]; 5]; 2];
190    POOL1.forward(&x0, &mut x2);
191    // `flat`: flatten `pool1` (2x5x5) in NHWC order.
192    let mut x4 = [0i8; 50];
193    for (out, &v) in x4.iter_mut().zip(x2.iter().flatten().flatten()) {
194        *out = v;
195    }
196    let x9 = FC1.forward(&x4).map(|v| qrelu(v, RELU2_ZERO_POINT));
197    let x10 = FC2.forward(&x9);
198    x10.map(|v| apply_lut(v, &OUT_LUT))
199}