Skip to main content

imported_lstm/common/
imported_lstm_model.rs

1//! `example_lstm`: generated by tinymind-rs tools/import_pytorch/tinymind_import.py.
2//! Do not edit; regenerate from the model instead.
3//!
4//! Integer-only and `no_std`: int8 inputs in the `INPUT` grid, int8 outputs in the
5//! `OUTPUT` grid (real value = `SCALE * (q - ZERO_POINT)`).
6//! The input is a sequence, `[step][feature]`; `forward` runs the LSTM over it from
7//! zero state, and `head` runs the rest of the network on a hidden state, for
8//! callers that step the LSTM themselves (streaming).
9
10// Not every program uses every constant.
11#![allow(dead_code)]
12
13use tinymind::int8::{LUT_SIZE, QDense, QLstmCell, QSoftmax, Requantizer};
14
15/// Steps in the input sequence.
16pub const SEQUENCE_LENGTH: usize = 12;
17/// Features per step.
18pub const NUM_FEATURES: usize = 3;
19/// The input sequence, `[step][feature]`.
20pub type Input = [[i8; NUM_FEATURES]; SEQUENCE_LENGTH];
21/// Number of outputs.
22pub const NUM_OUTPUTS: usize = 4;
23
24/// Scale of the `input` tensor.
25pub const INPUT_SCALE: f32 = 0.007843138;
26/// Zero point of the `input` tensor.
27pub const INPUT_ZERO_POINT: i8 = -1;
28/// Scale of the `lstm1` tensor.
29pub const LSTM1_SCALE: f32 = 0.0060337572;
30/// Zero point of the `lstm1` tensor.
31pub const LSTM1_ZERO_POINT: i8 = 0;
32/// Scale of the `fc1` tensor.
33pub const FC1_SCALE: f32 = 0.00684222;
34/// Zero point of the `fc1` tensor.
35pub const FC1_ZERO_POINT: i8 = -61;
36/// Scale of the `probs` tensor.
37pub const PROBS_SCALE: f32 = 0.00390625;
38/// Zero point of the `probs` tensor.
39pub const PROBS_ZERO_POINT: i8 = -128;
40/// Scale of the output (`probs`).
41pub const OUTPUT_SCALE: f32 = PROBS_SCALE;
42/// Zero point of the output (`probs`).
43pub const OUTPUT_ZERO_POINT: i8 = PROBS_ZERO_POINT;
44
45/// `lstm1` hidden units.
46pub const LSTM1_HIDDEN: usize = 8;
47/// `lstm1` cell-state scale (i16, zero point 0).
48pub const LSTM1_CELL_SCALE: f32 = 0.00004979594;
49/// Scale of the gates' pre-activation grid (the LUTs' input), fixed at 8/127.
50pub const LSTM1_LUT_INPUT_SCALE: f32 = 0.062992126;
51/// `lstm1` input weights, `[gate][unit][feature]`, gates i, f, g, o (weight scales 0.005905512, 0.005905512, 0.005905512, 0.005905512).
52pub 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];
94/// `lstm1` recurrent weights, `[gate][unit][previous unit]` (weight scales 0.003937008, 0.003937008, 0.003937008, 0.003937008).
95pub 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];
137/// `lstm1` biases, `[gate][unit]`, in the pre-activation grid.
138pub 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];
144/// Sigmoid table for `lstm1`'s i, f and o gates (output scale 1/256, zero point -128).
145pub 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];
161/// Tanh table for `lstm1`'s g gate and cell state (output scale 1/128, zero point 0).
162pub 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];
179/// `lstm1`: an LSTM with 3 inputs and 8 units over `input`, one step per call; its hidden state is `lstm1`.
180pub 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
211/// `fc1` int8 weights, `[output][input]` (weight scale 0.007874016).
212pub 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];
218/// `fc1` int32 biases (scale = input scale * weight scale).
219pub static FC1_BIASES: [i32; 4] = [5262, 0, -5262, 2631];
220/// `fc1`: 8 -> 4, from `lstm1`.
221pub 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
234/// `probs`: softmax over the `fc1` logits, onto the `probs` grid (scale 1/256, zero point -128).
235pub static PROBS: QSoftmax = QSoftmax {
236    input_multiplier: 1880775168,
237    input_left_shift: 19,
238    diff_min: -3968,
239};
240
241/// The network after `LSTM1` on one of its hidden states (int8, `LSTM1` grid).
242pub fn head(input: &[i8; LSTM1_HIDDEN]) -> [i8; NUM_OUTPUTS] {
243    let x0 = FC1.forward(input);
244    PROBS.forward(&x0)
245}
246
247/// Run the network on an int8 sequence in the `INPUT` grid, from zero state.
248pub 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}