1#![allow(dead_code)]
10
11use tinymind::int8::{LUT_SIZE, QAvgPool2D, QConv2D, QDense, Requantizer, apply_lut, qrelu};
12
13pub type Input = [[[i8; 2]; 11]; 9];
15pub const NUM_OUTPUTS: usize = 2;
17
18pub const INPUT_SCALE: f32 = 0.007843138;
20pub const INPUT_ZERO_POINT: i8 = -1;
22pub const RELU1_SCALE: f32 = 0.0047058826;
24pub const RELU1_ZERO_POINT: i8 = -128;
26pub const POOL1_SCALE: f32 = 0.0047058826;
28pub const POOL1_ZERO_POINT: i8 = -128;
30pub const FLAT_SCALE: f32 = 0.0047058826;
32pub const FLAT_ZERO_POINT: i8 = -128;
34pub const RELU2_SCALE: f32 = 0.0032607613;
36pub const RELU2_ZERO_POINT: i8 = -1;
38pub const FC2_SCALE: f32 = 0.0034576417;
40pub const FC2_ZERO_POINT: i8 = 86;
42pub const OUT_SCALE: f32 = 0.0078125;
44pub const OUT_ZERO_POINT: i8 = 0;
46pub const OUTPUT_SCALE: f32 = OUT_SCALE;
48pub const OUTPUT_ZERO_POINT: i8 = OUT_ZERO_POINT;
50
51pub 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];
79pub static CONV1_BIASES: [i32; 5] = [-8096, 4048, -4048, 8096, 0];
81pub 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
95pub static POOL1: QAvgPool2D<2, 2, 2, 2> = QAvgPool2D {
97 qmin: -128,
98 qmax: 127,
99};
100
101pub 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];
134pub static FC1_BIASES: [i32; 6] = [-13494, 26987, 0, -26987, 13494, -13494];
136pub 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
150pub static FC2_WEIGHTS: [[i8; 6]; 2] = [[-111, 0, 111, 127, -32, 79], [95, -64, 48, 64, -95, 16]];
152pub static FC2_BIASES: [i32; 2] = [0, -9737];
154pub 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
168pub 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
185pub 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 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}