1#![allow(dead_code)]
12
13use tinymind::int8::{LUT_SIZE, QDense, QLstmCell, Requantizer, apply_lut, qrelu};
14
15pub const SEQUENCE_LENGTH: usize = 10;
17pub const NUM_FEATURES: usize = 2;
19pub type Input = [[i8; NUM_FEATURES]; SEQUENCE_LENGTH];
21pub const NUM_OUTPUTS: usize = 1;
23
24pub const INPUT_SCALE: f32 = 0.007843138;
26pub const INPUT_ZERO_POINT: i8 = -1;
28pub const LSTM1_SCALE: f32 = 0.0061878837;
30pub const LSTM1_ZERO_POINT: i8 = 0;
32pub const RELU1_SCALE: f32 = 0.0031368663;
34pub const RELU1_ZERO_POINT: i8 = -128;
36pub const FC2_SCALE: f32 = 0.000728905;
38pub const FC2_ZERO_POINT: i8 = 93;
40pub const OUT_SCALE: f32 = 0.0078125;
42pub const OUT_ZERO_POINT: i8 = 0;
44pub const OUTPUT_SCALE: f32 = OUT_SCALE;
46pub const OUTPUT_ZERO_POINT: i8 = OUT_ZERO_POINT;
48
49pub const LSTM1_HIDDEN: usize = 5;
51pub const LSTM1_CELL_SCALE: f32 = 0.02284724;
53pub const LSTM1_LUT_INPUT_SCALE: f32 = 0.062992126;
55pub static LSTM1_INPUT_WEIGHTS: [[[i8; 2]; 5]; 4] = [
57 [[-111, 0], [111, 127], [-32, 79], [95, -64], [48, 64]],
58 [[-95, 16], [32, -127], [-16, 0], [111, -48], [-32, 79]],
59 [[-79, -64], [48, -111], [-95, 16], [127, -127], [-16, 95]],
60 [[127, -54], [73, 91], [-91, 36], [54, -127], [0, 18]],
61];
62pub static LSTM1_RECURRENT_WEIGHTS: [[[i8; 5]; 5]; 4] = [
64 [
65 [-32, 79, -79, -64, 48],
66 [-111, -95, 16, 127, -127],
67 [-16, 95, 111, -48, 64],
68 [79, -79, 32, 48, -111],
69 [0, 16, 127, -32, -16],
70 ],
71 [
72 [95, -64, -48, 64, -95],
73 [-79, 32, -127, -111, 0],
74 [111, 127, -32, 79, 95],
75 [-64, 48, 64, -95, 16],
76 [32, -127, -16, 0, 111],
77 ],
78 [
79 [-48, -32, 79, -79, -64],
80 [48, -111, -95, 16, 127],
81 [-127, -16, 95, 111, -48],
82 [64, 79, -79, 32, 48],
83 [-111, 0, 16, 127, -32],
84 ],
85 [
86 [-16, 95, -64, -48, 64],
87 [-95, -79, 32, -127, -111],
88 [0, 111, 127, -32, 79],
89 [95, -64, 48, 64, -95],
90 [16, 32, -127, -16, 0],
91 ],
92];
93pub static LSTM1_BIASES: [[i32; 5]; 4] = [
95 [4, 0, -4, 2, -2],
96 [20, 16, 12, 18, 14],
97 [4, 0, -4, 2, -2],
98 [4, 0, -4, 2, -2],
99];
100pub static LSTM1_SIGMOID_LUT: [i8; LUT_SIZE] = [
102 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
103 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -127, -127, -127,
104 -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -126,
105 -126, -126, -126, -126, -126, -126, -126, -125, -125, -125, -125, -125, -125, -124, -124, -124,
106 -124, -123, -123, -123, -122, -122, -122, -121, -121, -120, -120, -119, -119, -118, -117, -117,
107 -116, -115, -115, -114, -113, -112, -111, -110, -109, -108, -107, -105, -104, -103, -101, -100,
108 -98, -96, -94, -93, -91, -88, -86, -84, -82, -79, -77, -74, -71, -69, -66, -63, -60, -56, -53,
109 -50, -46, -43, -39, -35, -32, -28, -24, -20, -16, -12, -8, -4, 0, 4, 8, 12, 16, 20, 24, 28, 32,
110 35, 39, 43, 46, 50, 53, 56, 60, 63, 66, 69, 71, 74, 77, 79, 82, 84, 86, 88, 91, 93, 94, 96, 98,
111 100, 101, 103, 104, 105, 107, 108, 109, 110, 111, 112, 113, 114, 115, 115, 116, 117, 117, 118,
112 119, 119, 120, 120, 121, 121, 122, 122, 122, 123, 123, 123, 124, 124, 124, 124, 125, 125, 125,
113 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127,
114 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
115 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
116];
117pub static LSTM1_TANH_LUT: [i8; LUT_SIZE] = [
119 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
120 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
121 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
122 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
123 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -127,
124 -127, -127, -127, -127, -127, -127, -127, -127, -126, -126, -126, -126, -125, -125, -125, -124,
125 -124, -123, -122, -122, -121, -120, -119, -117, -116, -115, -113, -111, -109, -107, -104, -101,
126 -98, -94, -91, -86, -82, -77, -71, -66, -60, -53, -46, -39, -32, -24, -16, -8, 0, 8, 16, 24,
127 32, 39, 46, 53, 60, 66, 71, 77, 82, 86, 91, 94, 98, 101, 104, 107, 109, 111, 113, 115, 116,
128 117, 119, 120, 121, 122, 122, 123, 124, 124, 125, 125, 125, 126, 126, 126, 126, 127, 127, 127,
129 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
130 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
131 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
132 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
133 127, 127, 127, 127, 127, 127, 127, 127,
134];
135pub static LSTM1: QLstmCell<'static, 2, 5, i8> = QLstmCell {
137 input_weights: &LSTM1_INPUT_WEIGHTS,
138 recurrent_weights: &LSTM1_RECURRENT_WEIGHTS,
139 biases: &LSTM1_BIASES,
140 input_zero_point: -1,
141 hidden_zero_point: 0,
142 cell_zero_point: 0,
143 input_to_lut_multiplier: [1077952640, 1077952640, 1077952640, 1886417120],
144 input_to_lut_shift: [9, 9, 9, 10],
145 recurrent_to_lut_multiplier: [1700912512, 1700912512, 1700912512, 1700912512],
146 recurrent_to_lut_shift: [11, 11, 11, 11],
147 sigmoid_lut: &LSTM1_SIGMOID_LUT,
148 tanh_lut: &LSTM1_TANH_LUT,
149 f_times_c_multiplier: 1073741824,
150 f_times_c_shift: 7,
151 i_times_g_multiplier: 1468642639,
152 i_times_g_shift: 9,
153 cell_qmin: -128,
154 cell_qmax: 127,
155 cell_to_tanh_multiplier: 1557784419,
156 cell_to_tanh_shift: 1,
157 tanh_cell_lut: &LSTM1_TANH_LUT,
158 output_requantizer: Requantizer {
159 multiplier: 1355650566,
160 shift: 7,
161 zero_point: 0,
162 qmin: -128,
163 qmax: 127,
164 },
165};
166
167pub static FC1_WEIGHTS: [[i8; 5]; 6] = [
169 [0, 111, -48, -32, 79],
170 [-79, -64, 48, -111, -95],
171 [16, 127, -127, -16, 95],
172 [111, -48, 64, 79, -79],
173 [32, 48, -111, 0, 16],
174 [127, -32, -16, 95, -64],
175];
176pub static FC1_BIASES: [i32; 6] = [-5131, 2565, -2565, 5131, 0, -5131];
178pub static FC1: QDense<'static, 5, 6> = QDense {
180 weights: &FC1_WEIGHTS,
181 biases: Some(&FC1_BIASES),
182 input_zero_point: 0,
183 requantizer: Requantizer {
184 multiplier: 2134775719,
185 shift: 6,
186 zero_point: -128,
187 qmin: -128,
188 qmax: 127,
189 },
190};
191
192pub static FC2_WEIGHTS: [[i8; 6]; 1] = [[91, -91, 36, 54, -127, 0]];
194pub static FC2_BIASES: [i32; 1] = [-11568];
196pub static FC2: QDense<'static, 6, 1> = QDense {
198 weights: &FC2_WEIGHTS,
199 biases: Some(&FC2_BIASES),
200 input_zero_point: -128,
201 requantizer: Requantizer {
202 multiplier: 2037554492,
203 shift: 6,
204 zero_point: 93,
205 qmin: -128,
206 qmax: 127,
207 },
208};
209
210pub static OUT_LUT: [i8; LUT_SIZE] = [
212 -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -19, -19, -19, -19, -19, -19, -19, -19,
213 -19, -19, -19, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -17, -17, -17, -17, -17,
214 -17, -17, -17, -17, -17, -17, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -15, -15,
215 -15, -15, -15, -15, -15, -15, -15, -15, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14,
216 -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -12, -12, -12, -12, -12, -12, -12, -12,
217 -12, -12, -12, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -10, -10, -10, -10, -10,
218 -10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -8, -8, -8, -8, -8, -8,
219 -8, -8, -8, -8, -8, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -6, -6, -6, -6, -6, -6, -6, -6,
220 -6, -6, -6, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
221 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
222 -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
223 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
224];
225
226pub fn head(input: &[i8; LSTM1_HIDDEN]) -> [i8; NUM_OUTPUTS] {
228 let x0 = FC1.forward(input).map(|v| qrelu(v, RELU1_ZERO_POINT));
229 let x1 = FC2.forward(&x0);
230 x1.map(|v| apply_lut(v, &OUT_LUT))
231}
232
233pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
235 let mut hidden = [LSTM1_ZERO_POINT; LSTM1_HIDDEN];
236 let mut cell = [0i8; LSTM1_HIDDEN];
237 for step in input {
238 LSTM1.forward(step, &mut hidden, &mut cell);
239 }
240 head(&hidden)
241}