Skip to main content

imported_lstm/common/
imported_lstm_i8_model.rs

1//! `example_lstm_i8`: 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, Requantizer, apply_lut, qrelu};
14
15/// Steps in the input sequence.
16pub const SEQUENCE_LENGTH: usize = 10;
17/// Features per step.
18pub const NUM_FEATURES: usize = 2;
19/// The input sequence, `[step][feature]`.
20pub type Input = [[i8; NUM_FEATURES]; SEQUENCE_LENGTH];
21/// Number of outputs.
22pub const NUM_OUTPUTS: usize = 1;
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.0061878837;
30/// Zero point of the `lstm1` tensor.
31pub const LSTM1_ZERO_POINT: i8 = 0;
32/// Scale of the `relu1` tensor.
33pub const RELU1_SCALE: f32 = 0.0031368663;
34/// Zero point of the `relu1` tensor.
35pub const RELU1_ZERO_POINT: i8 = -128;
36/// Scale of the `fc2` tensor.
37pub const FC2_SCALE: f32 = 0.000728905;
38/// Zero point of the `fc2` tensor.
39pub const FC2_ZERO_POINT: i8 = 93;
40/// Scale of the `out` tensor.
41pub const OUT_SCALE: f32 = 0.0078125;
42/// Zero point of the `out` tensor.
43pub const OUT_ZERO_POINT: i8 = 0;
44/// Scale of the output (`out`).
45pub const OUTPUT_SCALE: f32 = OUT_SCALE;
46/// Zero point of the output (`out`).
47pub const OUTPUT_ZERO_POINT: i8 = OUT_ZERO_POINT;
48
49/// `lstm1` hidden units.
50pub const LSTM1_HIDDEN: usize = 5;
51/// `lstm1` cell-state scale (i8, zero point 0).
52pub const LSTM1_CELL_SCALE: f32 = 0.02284724;
53/// Scale of the gates' pre-activation grid (the LUTs' input), fixed at 8/127.
54pub const LSTM1_LUT_INPUT_SCALE: f32 = 0.062992126;
55/// `lstm1` input weights, `[gate][unit][feature]`, gates i, f, g, o (weight scales 0.007874016, 0.007874016, 0.007874016, 0.0068897638).
56pub 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];
62/// `lstm1` recurrent weights, `[gate][unit][previous unit]` (weight scales 0.003937008, 0.003937008, 0.003937008, 0.003937008).
63pub 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];
93/// `lstm1` biases, `[gate][unit]`, in the pre-activation grid.
94pub 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];
100/// Sigmoid table for `lstm1`'s i, f and o gates (output scale 1/256, zero point -128).
101pub 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];
117/// Tanh table for `lstm1`'s g gate and cell state (output scale 1/128, zero point 0).
118pub 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];
135/// `lstm1`: an LSTM with 2 inputs and 5 units over `input`, one step per call; its hidden state is `lstm1`.
136pub 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
167/// `fc1` int8 weights, `[output][input]` (weight scale 0.007874016).
168pub 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];
176/// `fc1` int32 biases (scale = input scale * weight scale).
177pub static FC1_BIASES: [i32; 6] = [-5131, 2565, -2565, 5131, 0, -5131];
178/// `fc1`: 5 -> 6, from `lstm1`, then ReLU into `relu1`.
179pub 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
192/// `fc2` int8 weights, `[output][input]` (weight scale 0.0034448819).
193pub static FC2_WEIGHTS: [[i8; 6]; 1] = [[91, -91, 36, 54, -127, 0]];
194/// `fc2` int32 biases (scale = input scale * weight scale).
195pub static FC2_BIASES: [i32; 1] = [-11568];
196/// `fc2`: 6 -> 1, from `relu1`.
197pub 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
210/// `out`: tanh table from the `fc2` grid to the `out` grid.
211pub 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
226/// The network after `LSTM1` on one of its hidden states (int8, `LSTM1` grid).
227pub 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
233/// Run the network on an int8 sequence in the `INPUT` grid, from zero state.
234pub 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}