1#![allow(dead_code)]
10
11use tinymind::int8::{
12 QConv2D, QDense, QDepthwiseConv2D, QGlobalAvgPool2D, QMaxPool2D, QPointwiseConv2D, QSoftmax,
13 Requantizer,
14};
15
16pub type Input = [[[i8; 1]; 12]; 12];
18pub const NUM_OUTPUTS: usize = 3;
20
21pub const INPUT_SCALE: f32 = 0.007843138;
23pub const INPUT_ZERO_POINT: i8 = -1;
25pub const RELU1_SCALE: f32 = 0.011801419;
27pub const RELU1_ZERO_POINT: i8 = -128;
29pub const POOL1_SCALE: f32 = 0.011801419;
31pub const POOL1_ZERO_POINT: i8 = -128;
33pub const RELU2_SCALE: f32 = 0.010787795;
35pub const RELU2_ZERO_POINT: i8 = -128;
37pub const RELU3_SCALE: f32 = 0.0009883966;
39pub const RELU3_ZERO_POINT: i8 = -1;
41pub const GAP_SCALE: f32 = 0.0009883966;
43pub const GAP_ZERO_POINT: i8 = -1;
45pub const FC1_SCALE: f32 = 0.0033405602;
47pub const FC1_ZERO_POINT: i8 = 127;
49pub const PROBS_SCALE: f32 = 0.00390625;
51pub const PROBS_ZERO_POINT: i8 = -128;
53pub const OUTPUT_SCALE: f32 = PROBS_SCALE;
55pub const OUTPUT_ZERO_POINT: i8 = PROBS_ZERO_POINT;
57
58pub static BN1_WEIGHTS: [[[[i8; 1]; 3]; 3]; 4] = [
60 [
61 [[-27], [37], [-55]],
62 [[-46], [18], [-73]],
63 [[-64], [0], [64]],
64 ],
65 [
66 [[127], [-32], [79]],
67 [[95], [-64], [48]],
68 [[64], [-95], [16]],
69 ],
70 [
71 [[19], [-76], [-9]],
72 [[0], [66], [-28]],
73 [[-19], [47], [-47]],
74 ],
75 [
76 [[-30], [22], [-52]],
77 [[-45], [7], [60]],
78 [[-60], [-7], [45]],
79 ],
80];
81pub static BN1_BIASES: [i32; 4] = [-5398, 12144, 7526, -6515];
83pub static BN1: QConv2D<'static, 1, 3, 3, 4, 1, 1, 1, 1, 1, 1> = QConv2D {
85 weights: &BN1_WEIGHTS,
86 biases: Some(&BN1_BIASES),
87 input_zero_point: -1,
88 requantizer: Requantizer {
89 multiplier: 1078824586,
90 shift: 7,
91 zero_point: -128,
92 qmin: -128,
93 qmax: 127,
94 },
95};
96
97pub static POOL1: QMaxPool2D<2, 2, 2, 2> = QMaxPool2D;
99
100pub static BN2_WEIGHTS: [[[i8; 3]; 3]; 4] = [
102 [[21, -82, -10], [0, 72, -31], [-21, 51, -51]],
103 [[-18, 14, -32], [-28, 5, 37], [-37, -5, 28]],
104 [[127, -54, 73], [91, -91, 36], [54, -127, 0]],
105 [[9, 71, -18], [-9, 53, -36], [-27, 36, -53]],
106];
107pub static BN2_BIASES: [i32; 4] = [-1739, -8329, 9628, -3013];
109pub static BN2_REQUANTIZERS: [Requantizer; 4] = [Requantizer {
111 multiplier: 1831202441,
112 shift: 7,
113 zero_point: -128,
114 qmin: -128,
115 qmax: 127,
116}; 4];
117pub static BN2: QDepthwiseConv2D<'static, 4, 3, 3, 1, 1, 1, 1, 1, 1> = QDepthwiseConv2D {
119 weights: &BN2_WEIGHTS,
120 biases: Some(&BN2_BIASES),
121 input_zero_point: -128,
122 requantizers: &BN2_REQUANTIZERS,
123};
124
125pub static CONV3_WEIGHTS: [[i8; 4]; 8] = [
127 [111, -48, 64, 79],
128 [-79, 32, 48, -111],
129 [0, 16, 127, -32],
130 [-16, 95, -64, -48],
131 [64, -95, -79, 32],
132 [-127, -111, 0, 111],
133 [127, -32, 79, 95],
134 [-64, 48, 64, -95],
135];
136pub static CONV3_BIASES: [i32; 8] = [2943, -2943, 5886, 0, -5886, 2943, -2943, 5886];
138pub static CONV3: QPointwiseConv2D<'static, 4, 8> = QPointwiseConv2D {
140 weights: &CONV3_WEIGHTS,
141 biases: Some(&CONV3_BIASES),
142 input_zero_point: -128,
143 requantizer: Requantizer {
144 multiplier: 1476446082,
145 shift: 4,
146 zero_point: -1,
147 qmin: -1,
148 qmax: 127,
149 },
150};
151
152pub static GAP: QGlobalAvgPool2D = QGlobalAvgPool2D {
154 qmin: -128,
155 qmax: 127,
156};
157
158pub static FC1_WEIGHTS: [[i8; 8]; 3] = [
160 [-79, 32, -127, -111, 0, 111, 127, -32],
161 [79, 95, -64, 48, 64, -95, 16, 32],
162 [-127, -16, 0, 111, -48, -32, 79, -79],
163];
164pub static FC1_BIASES: [i32; 3] = [32123, 0, -32123];
166pub static FC1: QDense<'static, 8, 3> = QDense {
168 weights: &FC1_WEIGHTS,
169 biases: Some(&FC1_BIASES),
170 input_zero_point: -1,
171 requantizer: Requantizer {
172 multiplier: 1280790316,
173 shift: 8,
174 zero_point: 127,
175 qmin: -128,
176 qmax: 127,
177 },
178};
179
180pub static PROBS: QSoftmax = QSoftmax {
182 input_multiplier: 1836492416,
183 input_left_shift: 18,
184 diff_min: -7936,
185};
186
187pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
189 let mut x0 = [[[0i8; 4]; 12]; 12];
190 BN1.forward(input, &mut x0);
191 let mut x2 = [[[0i8; 4]; 6]; 6];
192 POOL1.forward(&x0, &mut x2);
193 let mut x4 = [[[0i8; 4]; 6]; 6];
194 BN2.forward(&x2, &mut x4);
195 let mut x6 = [[[0i8; 8]; 6]; 6];
196 CONV3.forward(&x4, &mut x6);
197 let x8 = GAP.forward(&x6);
198 let x9 = FC1.forward(&x8);
199 PROBS.forward(&x9)
200}