Skip to main content

imported_mlp/common/
imported_mlp_model.rs

1//! `example_mlp`: 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
7// Not every program uses every constant.
8#![allow(dead_code)]
9
10use tinymind::int8::{LUT_SIZE, QDense, Requantizer, apply_lut, qrelu};
11
12/// Number of inputs.
13pub const NUM_INPUTS: usize = 3;
14/// Number of outputs.
15pub const NUM_OUTPUTS: usize = 2;
16
17/// Scale of the `input` tensor.
18pub const INPUT_SCALE: f32 = 0.007843138;
19/// Zero point of the `input` tensor.
20pub const INPUT_ZERO_POINT: i8 = -1;
21/// Scale of the `h1` tensor.
22pub const H1_SCALE: f32 = 0.004509804;
23/// Zero point of the `h1` tensor.
24pub const H1_ZERO_POINT: i8 = -128;
25/// Scale of the `h2` tensor.
26pub const H2_SCALE: f32 = 0.0060324753;
27/// Zero point of the `h2` tensor.
28pub const H2_ZERO_POINT: i8 = -128;
29/// Scale of the `fc3` tensor.
30pub const FC3_SCALE: f32 = 0.005711566;
31/// Zero point of the `fc3` tensor.
32pub const FC3_ZERO_POINT: i8 = -1;
33/// Scale of the `out` tensor.
34pub const OUT_SCALE: f32 = 0.00390625;
35/// Zero point of the `out` tensor.
36pub const OUT_ZERO_POINT: i8 = -128;
37/// Scale of the output (`out`).
38pub const OUTPUT_SCALE: f32 = OUT_SCALE;
39/// Zero point of the output (`out`).
40pub const OUTPUT_ZERO_POINT: i8 = OUT_ZERO_POINT;
41
42/// `fc1` int8 weights, `[output][input]` (weight scale 0.007874016).
43pub static FC1_WEIGHTS: [[i8; 3]; 8] = [
44    [-48, -111, 95],
45    [64, 0, -64],
46    [-95, 111, 48],
47    [16, -48, -111],
48    [127, 64, 0],
49    [-32, -95, 111],
50    [79, 16, -48],
51    [-79, 127, 64],
52];
53/// `fc1` int32 biases (scale = input scale * weight scale).
54pub static FC1_BIASES: [i32; 8] = [-4048, 8096, 0, -8096, 4048, -4048, 8096, 0];
55/// `fc1`: 3 -> 8, from `input`, then ReLU into `h1`.
56pub static FC1: QDense<'static, 3, 8> = QDense {
57    weights: &FC1_WEIGHTS,
58    biases: Some(&FC1_BIASES),
59    input_zero_point: -1,
60    requantizer: Requantizer {
61        multiplier: 1882080981,
62        shift: 6,
63        zero_point: -128,
64        qmin: -128,
65        qmax: 127,
66    },
67};
68
69/// `fc2` int8 weights, `[output][input]` (weight scale 0.007874016).
70pub static FC2_WEIGHTS: [[i8; 8]; 4] = [
71    [32, -32, -95, 111, 48, -16, -79, 127],
72    [-127, 79, 16, -48, -111, 95, 32, -32],
73    [-16, -79, 127, 64, 0, -64, -127, 79],
74    [95, 32, -32, -95, 111, 48, -16, -79],
75];
76/// `fc2` int32 biases (scale = input scale * weight scale).
77pub static FC2_BIASES: [i32; 4] = [0, -14080, 7040, -7040];
78/// `fc2`: 8 -> 4, from `h1`, then ReLU into `h2`.
79pub static FC2: QDense<'static, 8, 4> = QDense {
80    weights: &FC2_WEIGHTS,
81    biases: Some(&FC2_BIASES),
82    input_zero_point: -128,
83    requantizer: Requantizer {
84        multiplier: 1618073385,
85        shift: 7,
86        zero_point: -128,
87        qmin: -128,
88        qmax: 127,
89    },
90};
91
92/// `fc3` int8 weights, `[output][input]` (weight scale 0.0068897638).
93pub static FC3_WEIGHTS: [[i8; 4]; 2] = [[127, 54, -18, -91], [-54, -127, 109, 36]];
94/// `fc3` int32 biases (scale = input scale * weight scale).
95pub static FC3_BIASES: [i32; 2] = [6015, -6015];
96/// `fc3`: 4 -> 2, from `h2`.
97pub static FC3: QDense<'static, 4, 2> = QDense {
98    weights: &FC3_WEIGHTS,
99    biases: Some(&FC3_BIASES),
100    input_zero_point: -128,
101    requantizer: Requantizer {
102        multiplier: 2000251025,
103        shift: 7,
104        zero_point: -1,
105        qmin: -128,
106        qmax: 127,
107    },
108};
109
110/// `out`: sigmoid table from the `fc3` grid to the `out` grid.
111pub static OUT_LUT: [i8; LUT_SIZE] = [
112    -44, -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39,
113    -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -34, -33, -33, -33, -32,
114    -32, -32, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26,
115    -25, -25, -25, -24, -24, -23, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19,
116    -19, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -13, -13, -13, -12, -12,
117    -12, -11, -11, -11, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -5, -5, -5, -4, -4,
118    -4, -3, -3, -3, -2, -2, -1, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7,
119    7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 15, 15, 15, 16, 16,
120    16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 25,
121    25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 32, 32, 32, 33, 33,
122    33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41,
123    41, 42, 42, 42, 43, 43, 43, 44, 44, 44, 44, 45,
124];
125
126/// Run the network on int8 inputs in the `INPUT` grid.
127pub fn forward(input: &[i8; NUM_INPUTS]) -> [i8; NUM_OUTPUTS] {
128    let x0 = FC1.forward(input).map(|v| qrelu(v, H1_ZERO_POINT));
129    let x1 = FC2.forward(&x0).map(|v| qrelu(v, H2_ZERO_POINT));
130    let x2 = FC3.forward(&x1);
131    x2.map(|v| apply_lut(v, &OUT_LUT))
132}