1#![allow(dead_code)]
14
15use tinymind::int8::*;
16
17pub type Input = [[[i8; 2]; 24]; 8];
19pub type Output = [i8; 4];
21pub const INPUT_SCALE: f32 = 0.030285051;
23pub const INPUT_ZERO_POINT: i8 = -7;
25pub const OUTPUT_SCALE: f32 = 0.00390625;
28pub const OUTPUT_ZERO_POINT: i8 = -128;
30
31static CONV0_WEIGHTS: [[[[i8; 2]; 3]; 3]; 5] = [
33 [
34 [[85, 127], [-96, 27], [105, 17]],
35 [[82, -110], [108, 41], [-99, 55]],
36 [[-86, 20], [61, 124], [-120, 39]],
37 ],
38 [
39 [[-114, 46], [108, -61], [44, -28]],
40 [[-61, -86], [-118, -61], [73, -117]],
41 [[122, -3], [-105, 76], [7, 62]],
42 ],
43 [
44 [[121, 103], [-113, 108], [-16, 25]],
45 [[125, 92], [21, -32], [-36, 104]],
46 [[-116, -11], [60, -26], [98, -64]],
47 ],
48 [
49 [[68, -42], [52, -87], [82, 104]],
50 [[-82, 14], [-68, 59], [122, 95]],
51 [[-71, -83], [-73, 98], [-34, -64]],
52 ],
53 [
54 [[-20, -9], [97, -16], [118, 69]],
55 [[113, 88], [74, -93], [119, -71]],
56 [[-46, -40], [-8, 17], [-44, -50]],
57 ],
58];
59static CONV0_BIASES: [i32; 5] = [-2639, -775, -2071, 117, -3141];
60static CONV0: QConv2D<'static, 2, 3, 3, 5, 1, 1, 1, 1, 1, 1> = QConv2D {
61 weights: &CONV0_WEIGHTS,
62 biases: Some(&CONV0_BIASES),
63 input_zero_point: -7,
64 requantizer: Requantizer {
65 multiplier: 1590310321,
66 shift: 7,
67 zero_point: -128,
68 qmin: -128,
69 qmax: 127,
70 },
71};
72
73static MAXPOOL1: QMaxPool2D<2, 2, 2, 2> = QMaxPool2D;
75
76static DW2_WEIGHTS: [[[i8; 3]; 3]; 5] = [
78 [[-33, -48, -18], [103, 86, -42], [-120, -93, -100]],
79 [[-12, -58, -91], [29, 8, -20], [-47, 111, -126]],
80 [[-3, 61, 92], [-114, -20, 109], [22, 40, -58]],
81 [[91, -49, -70], [119, 35, -1], [-91, -29, -100]],
82 [[-88, -19, 25], [-107, 127, 96], [-122, 82, -78]],
83];
84static DW2_BIASES: [i32; 5] = [-5545, 6375, -2736, -2257, 848];
85static DW2_REQUANTIZERS: [Requantizer; 5] = [Requantizer {
86 multiplier: 2075791370,
87 shift: 7,
88 zero_point: -128,
89 qmin: -128,
90 qmax: 127,
91}; 5];
92static DW2: QDepthwiseConv2D<'static, 5, 3, 3, 1, 1, 1, 1, 1, 1> = QDepthwiseConv2D {
93 weights: &DW2_WEIGHTS,
94 biases: Some(&DW2_BIASES),
95 input_zero_point: -128,
96 requantizers: &DW2_REQUANTIZERS,
97};
98
99static TANH3_LUT: [i8; LUT_SIZE] = [
101 -128, -123, -118, -113, -108, -104, -99, -94, -89, -84, -80, -75, -70, -66, -61, -57, -52, -48,
102 -43, -39, -35, -30, -26, -22, -18, -14, -10, -6, -3, 1, 5, 8, 12, 15, 18, 22, 25, 28, 31, 34,
103 37, 40, 42, 45, 48, 50, 53, 55, 57, 60, 62, 64, 66, 68, 70, 72, 74, 76, 77, 79, 81, 82, 84, 85,
104 87, 88, 90, 91, 92, 93, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 104, 105, 106, 107, 108,
105 108, 109, 110, 110, 111, 111, 112, 113, 113, 114, 114, 115, 115, 115, 116, 116, 117, 117, 117,
106 118, 118, 118, 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, 122, 122,
107 123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 124, 124, 124, 125, 125, 125,
108 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, 126,
109 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 127,
110 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
111 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
112 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
113 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
114];
115
116static PW4_WEIGHTS: [[i8; 5]; 5] = [
118 [73, 10, -35, 11, -67],
119 [-93, 33, 14, 64, -48],
120 [-127, -32, -124, -10, 63],
121 [12, 74, 24, 55, 116],
122 [19, 21, -52, 80, 108],
123];
124static PW4_BIASES: [i32; 5] = [-6879, 9613, 2987, -1382, -9064];
125static PW4: QPointwiseConv2D<'static, 5, 5> = QPointwiseConv2D {
126 weights: &PW4_WEIGHTS,
127 biases: Some(&PW4_BIASES),
128 input_zero_point: -128,
129 requantizer: Requantizer {
130 multiplier: 1200983374,
131 shift: 7,
132 zero_point: -7,
133 qmin: -128,
134 qmax: 127,
135 },
136};
137
138static ADD5: QAdd = QAdd {
141 input_a_zero_point: -128,
142 input_b_zero_point: -7,
143 left_shift: 20,
144 input_a_multiplier: 1629484999,
145 input_a_shift: 3,
146 input_b_multiplier: 1073741824,
147 input_b_shift: 0,
148 output_requantizer: Requantizer {
149 multiplier: 1932328772,
150 shift: 19,
151 zero_point: -35,
152 qmin: -128,
153 qmax: 127,
154 },
155};
156
157static SIGMOID6_LUT: [i8; LUT_SIZE] = [
159 -100, -100, -99, -99, -98, -97, -97, -96, -96, -95, -94, -94, -93, -92, -91, -91, -90, -89,
160 -88, -88, -87, -86, -85, -84, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72,
161 -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -59, -58, -57, -56, -55, -54, -52, -51,
162 -50, -49, -47, -46, -45, -43, -42, -41, -39, -38, -37, -35, -34, -33, -31, -30, -28, -27, -25,
163 -24, -23, -21, -20, -18, -17, -15, -14, -12, -11, -9, -8, -6, -5, -3, -2, 0, 1, 3, 4, 6, 7, 9,
164 10, 12, 13, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 36, 38, 39, 40, 42, 43,
165 45, 46, 47, 49, 50, 51, 52, 54, 55, 56, 57, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 71, 73,
166 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 83, 84, 85, 86, 87, 88, 89, 90, 90, 91, 92, 93, 94, 94,
167 95, 96, 97, 97, 98, 99, 99, 100, 101, 101, 102, 103, 103, 104, 104, 105, 106, 106, 107, 107,
168 108, 108, 109, 109, 110, 110, 111, 111, 112, 112, 113, 113, 113, 114, 114, 115, 115, 115, 116,
169 116, 117, 117, 117, 118, 118, 118, 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122,
170 122, 122, 122, 123, 123, 123, 123, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 126, 126,
171 126, 126, 126, 126, 126, 127, 127, 127, 127,
172];
173
174static AVGPOOL7_WEIGHTS: [[[i8; 2]; 2]; 5] = [
178 [[64, 64], [64, 64]],
179 [[64, 64], [64, 64]],
180 [[64, 64], [64, 64]],
181 [[64, 64], [64, 64]],
182 [[64, 64], [64, 64]],
183];
184static AVGPOOL7_REQUANTIZERS: [Requantizer; 5] = [Requantizer {
185 multiplier: 1181803306,
186 shift: 7,
187 zero_point: -128,
188 qmin: -128,
189 qmax: 127,
190}; 5];
191static AVGPOOL7: QDepthwiseConv2D<'static, 5, 2, 2, 2, 2, 0, 0, 0, 0> = QDepthwiseConv2D {
192 weights: &AVGPOOL7_WEIGHTS,
193 biases: None,
194 input_zero_point: -128,
195 requantizers: &AVGPOOL7_REQUANTIZERS,
196};
197
198static AVGPOOL8_WEIGHTS: [[[i8; 6]; 2]; 5] = [
202 [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
203 [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
204 [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
205 [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
206 [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
207];
208static AVGPOOL8_REQUANTIZERS: [Requantizer; 5] = [Requantizer {
209 multiplier: 1507448591,
210 shift: 9,
211 zero_point: -128,
212 qmin: -128,
213 qmax: 127,
214}; 5];
215static AVGPOOL8: QDepthwiseConv2D<'static, 5, 2, 6, 1, 1, 0, 0, 0, 0> = QDepthwiseConv2D {
216 weights: &AVGPOOL8_WEIGHTS,
217 biases: None,
218 input_zero_point: -128,
219 requantizers: &AVGPOOL8_REQUANTIZERS,
220};
221
222static FC9_WEIGHTS: [[i8; 5]; 4] = [
224 [-127, -100, -106, -85, -37],
225 [-105, -106, -14, -107, -11],
226 [22, 24, 80, 69, -71],
227 [-43, 3, 107, 62, -71],
228];
229static FC9_BIASES: [i32; 4] = [-4514, 6954, 13362, -14296];
230static FC9: QDense<'static, 5, 4> = QDense {
231 weights: &FC9_WEIGHTS,
232 biases: Some(&FC9_BIASES),
233 input_zero_point: -128,
234 requantizer: Requantizer {
235 multiplier: 2044683897,
236 shift: 9,
237 zero_point: 46,
238 qmin: -128,
239 qmax: 127,
240 },
241};
242
243static SOFTMAX10: QSoftmax = QSoftmax {
245 input_multiplier: 1718222976,
246 input_left_shift: 20,
247 diff_min: -1984,
248};
249
250pub fn forward(input: &Input) -> Output {
252 let mut b0 = [[[0i8; 5]; 24]; 8];
253 let mut b1 = [[[0i8; 5]; 12]; 4];
254 let mut b2 = [[[0i8; 5]; 12]; 4];
255 let mut b3 = [[[0i8; 5]; 12]; 4];
256 let mut b4 = [[[0i8; 5]; 6]; 2];
257 let mut b5 = [[[0i8; 5]; 1]; 1];
258 CONV0.forward(input, &mut b0);
259 MAXPOOL1.forward(&b0, &mut b1);
260 DW2.forward(&b1, &mut b2);
261 for x in b1.as_flattened_mut().as_flattened_mut() {
262 *x = apply_lut(*x, &TANH3_LUT);
263 }
264 PW4.forward(&b2, &mut b3);
265 ADD5.forward(&b1, &b3, &mut b2);
266 for x in b2.as_flattened_mut().as_flattened_mut() {
267 *x = apply_lut(*x, &SIGMOID6_LUT);
268 }
269 AVGPOOL7.forward(&b2, &mut b4);
270 AVGPOOL8.forward(&b4, &mut b5);
271 let v9 = FC9.forward(
272 b5.as_flattened()
273 .as_flattened()
274 .first_chunk::<5>()
275 .expect("5 values"),
276 );
277 SOFTMAX10.forward(&v9)
278}