1#![allow(dead_code)]
12
13use tinymind::int8::{LUT_SIZE, QDense, QLstmCell, QSoftmax, Requantizer};
14
15pub const SEQUENCE_LENGTH: usize = 12;
17pub const NUM_FEATURES: usize = 3;
19pub type Input = [[i8; NUM_FEATURES]; SEQUENCE_LENGTH];
21pub const NUM_OUTPUTS: usize = 4;
23
24pub const INPUT_SCALE: f32 = 0.007843138;
26pub const INPUT_ZERO_POINT: i8 = -1;
28pub const LSTM1_SCALE: f32 = 0.0060337572;
30pub const LSTM1_ZERO_POINT: i8 = 0;
32pub const FC1_SCALE: f32 = 0.00684222;
34pub const FC1_ZERO_POINT: i8 = -61;
36pub const PROBS_SCALE: f32 = 0.00390625;
38pub const PROBS_ZERO_POINT: i8 = -128;
40pub const OUTPUT_SCALE: f32 = PROBS_SCALE;
42pub const OUTPUT_ZERO_POINT: i8 = PROBS_ZERO_POINT;
44
45pub const LSTM1_HIDDEN: usize = 8;
47pub const LSTM1_CELL_SCALE: f32 = 0.00004979594;
49pub const LSTM1_LUT_INPUT_SCALE: f32 = 0.062992126;
51pub static LSTM1_INPUT_WEIGHTS: [[[i8; 3]; 8]; 4] = [
53 [
54 [-48, 64, -95],
55 [-79, 32, -127],
56 [-111, 0, 111],
57 [127, -32, 79],
58 [95, -64, 48],
59 [64, -95, 16],
60 [32, -127, -16],
61 [0, 111, -48],
62 ],
63 [
64 [-32, 79, -79],
65 [-64, 48, -111],
66 [-95, 16, 127],
67 [-127, -16, 95],
68 [111, -48, 64],
69 [79, -79, 32],
70 [48, -111, 0],
71 [16, 127, -32],
72 ],
73 [
74 [-16, 95, -64],
75 [-48, 64, -95],
76 [-79, 32, -127],
77 [-111, 0, 111],
78 [127, -32, 79],
79 [95, -64, 48],
80 [64, -95, 16],
81 [32, -127, -16],
82 ],
83 [
84 [0, 111, -48],
85 [-32, 79, -79],
86 [-64, 48, -111],
87 [-95, 16, 127],
88 [-127, -16, 95],
89 [111, -48, 64],
90 [79, -79, 32],
91 [48, -111, 0],
92 ],
93];
94pub static LSTM1_RECURRENT_WEIGHTS: [[[i8; 8]; 8]; 4] = [
96 [
97 [32, -127, -16, 0, 111, -48, -32, 79],
98 [-79, -64, 48, -111, -95, 16, 127, -127],
99 [-16, 95, 111, -48, 64, 79, -79, 32],
100 [48, -111, 0, 16, 127, -32, -16, 95],
101 [-64, -48, 64, -95, -79, 32, -127, -111],
102 [0, 111, 127, -32, 79, 95, -64, 48],
103 [64, -95, 16, 32, -127, -16, 0, 111],
104 [-48, -32, 79, -79, -64, 48, -111, -95],
105 ],
106 [
107 [16, 127, -127, -16, 95, 111, -48, 64],
108 [79, -79, 32, 48, -111, 0, 16, 127],
109 [-32, -16, 95, -64, -48, 64, -95, -79],
110 [32, -127, -111, 0, 111, 127, -32, 79],
111 [95, -64, 48, 64, -95, 16, 32, -127],
112 [-16, 0, 111, -48, -32, 79, -79, -64],
113 [48, -111, -95, 16, 127, -127, -16, 95],
114 [111, -48, 64, 79, -79, 32, 48, -111],
115 ],
116 [
117 [0, 16, 127, -32, -16, 95, -64, -48],
118 [64, -95, -79, 32, -127, -111, 0, 111],
119 [127, -32, 79, 95, -64, 48, 64, -95],
120 [16, 32, -127, -16, 0, 111, -48, -32],
121 [79, -79, -64, 48, -111, -95, 16, 127],
122 [-127, -16, 95, 111, -48, 64, 79, -79],
123 [32, 48, -111, 0, 16, 127, -32, -16],
124 [95, -64, -48, 64, -95, -79, 32, -127],
125 ],
126 [
127 [-111, 0, 111, 127, -32, 79, 95, -64],
128 [48, 64, -95, 16, 32, -127, -16, 0],
129 [111, -48, -32, 79, -79, -64, 48, -111],
130 [-95, 16, 127, -127, -16, 95, 111, -48],
131 [64, 79, -79, 32, 48, -111, 0, 16],
132 [127, -32, -16, 95, -64, -48, 64, -95],
133 [-79, 32, -127, -111, 0, 111, 127, -32],
134 [79, 95, -64, 48, 64, -95, 16, 32],
135 ],
136];
137pub static LSTM1_BIASES: [[i32; 8]; 4] = [
139 [2, -2, 4, 0, -4, 2, -2, 4],
140 [16, 12, 18, 14, 20, 16, 12, 18],
141 [-2, 4, 0, -4, 2, -2, 4, 0],
142 [-4, 2, -2, 4, 0, -4, 2, -2],
143];
144pub static LSTM1_SIGMOID_LUT: [i8; LUT_SIZE] = [
146 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
147 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -127, -127, -127,
148 -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -126,
149 -126, -126, -126, -126, -126, -126, -126, -125, -125, -125, -125, -125, -125, -124, -124, -124,
150 -124, -123, -123, -123, -122, -122, -122, -121, -121, -120, -120, -119, -119, -118, -117, -117,
151 -116, -115, -115, -114, -113, -112, -111, -110, -109, -108, -107, -105, -104, -103, -101, -100,
152 -98, -96, -94, -93, -91, -88, -86, -84, -82, -79, -77, -74, -71, -69, -66, -63, -60, -56, -53,
153 -50, -46, -43, -39, -35, -32, -28, -24, -20, -16, -12, -8, -4, 0, 4, 8, 12, 16, 20, 24, 28, 32,
154 35, 39, 43, 46, 50, 53, 56, 60, 63, 66, 69, 71, 74, 77, 79, 82, 84, 86, 88, 91, 93, 94, 96, 98,
155 100, 101, 103, 104, 105, 107, 108, 109, 110, 111, 112, 113, 114, 115, 115, 116, 117, 117, 118,
156 119, 119, 120, 120, 121, 121, 122, 122, 122, 123, 123, 123, 124, 124, 124, 124, 125, 125, 125,
157 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127,
158 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
159 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
160];
161pub static LSTM1_TANH_LUT: [i8; LUT_SIZE] = [
163 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
164 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
165 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
166 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
167 -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128, -127,
168 -127, -127, -127, -127, -127, -127, -127, -127, -126, -126, -126, -126, -125, -125, -125, -124,
169 -124, -123, -122, -122, -121, -120, -119, -117, -116, -115, -113, -111, -109, -107, -104, -101,
170 -98, -94, -91, -86, -82, -77, -71, -66, -60, -53, -46, -39, -32, -24, -16, -8, 0, 8, 16, 24,
171 32, 39, 46, 53, 60, 66, 71, 77, 82, 86, 91, 94, 98, 101, 104, 107, 109, 111, 113, 115, 116,
172 117, 119, 120, 121, 122, 122, 123, 124, 124, 125, 125, 125, 126, 126, 126, 126, 127, 127, 127,
173 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
174 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
175 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
176 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
177 127, 127, 127, 127, 127, 127, 127, 127,
178];
179pub static LSTM1: QLstmCell<'static, 3, 8, i16> = QLstmCell {
181 input_weights: &LSTM1_INPUT_WEIGHTS,
182 recurrent_weights: &LSTM1_RECURRENT_WEIGHTS,
183 biases: &LSTM1_BIASES,
184 input_zero_point: -1,
185 hidden_zero_point: 0,
186 cell_zero_point: 0,
187 input_to_lut_multiplier: [1616928960, 1616928960, 1616928960, 1616928960],
188 input_to_lut_shift: [10, 10, 10, 10],
189 recurrent_to_lut_multiplier: [1658546560, 1658546560, 1658546560, 1658546560],
190 recurrent_to_lut_shift: [11, 11, 11, 11],
191 sigmoid_lut: &LSTM1_SIGMOID_LUT,
192 tanh_lut: &LSTM1_TANH_LUT,
193 f_times_c_multiplier: 1073741824,
194 f_times_c_shift: 7,
195 i_times_g_multiplier: 1316091209,
196 i_times_g_shift: 0,
197 cell_qmin: -32768,
198 cell_qmax: 32767,
199 cell_to_tanh_multiplier: 1738351114,
200 cell_to_tanh_shift: 10,
201 tanh_cell_lut: &LSTM1_TANH_LUT,
202 output_requantizer: Requantizer {
203 multiplier: 1390279335,
204 shift: 7,
205 zero_point: 0,
206 qmin: -128,
207 qmax: 127,
208 },
209};
210
211pub static FC1_WEIGHTS: [[i8; 8]; 4] = [
213 [-79, 32, -127, -111, 0, 111, 127, -32],
214 [79, 95, -64, 48, 64, -95, 16, 32],
215 [-127, -16, 0, 111, -48, -32, 79, -79],
216 [-64, 48, -111, -95, 16, 127, -127, -16],
217];
218pub static FC1_BIASES: [i32; 4] = [5262, 0, -5262, 2631];
220pub static FC1: QDense<'static, 8, 4> = QDense {
222 weights: &FC1_WEIGHTS,
223 biases: Some(&FC1_BIASES),
224 input_zero_point: 0,
225 requantizer: Requantizer {
226 multiplier: 1908652645,
227 shift: 7,
228 zero_point: -61,
229 qmin: -128,
230 qmax: 127,
231 },
232};
233
234pub static PROBS: QSoftmax = QSoftmax {
236 input_multiplier: 1880775168,
237 input_left_shift: 19,
238 diff_min: -3968,
239};
240
241pub fn head(input: &[i8; LSTM1_HIDDEN]) -> [i8; NUM_OUTPUTS] {
243 let x0 = FC1.forward(input);
244 PROBS.forward(&x0)
245}
246
247pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
249 let mut hidden = [LSTM1_ZERO_POINT; LSTM1_HIDDEN];
250 let mut cell = [0i16; LSTM1_HIDDEN];
251 for step in input {
252 LSTM1.forward(step, &mut hidden, &mut cell);
253 }
254 head(&hidden)
255}