Skip to main content

imported_onnx/common/
imported_onnx_mlp_model.rs

1//! `onnx_mlp`: generated by tinymind-rs tools/import_onnx/tinymind_import_onnx.py
2//! from an int8 ONNX model quantized by ONNX Runtime. Do not edit; regenerate
3//! from the model instead.
4//! The importer's example MatMul + bias Add -> Tanh -> Linear -> Softmax on 7 inputs
5//! (tools/import_onnx/models/mlp.onnx: QDQ format, per-tensor weights).
6//!
7//! Integer-only and `no_std`: int8 input in the `INPUT` grid, int8 output in the
8//! `OUTPUT` grid (real value = `SCALE * (q - ZERO_POINT)`). Feature maps are NHWC
9//! without the batch axis, `[row][column][channel]`: the transpose of the NCHW
10//! tensor the ONNX model takes.
11
12// Not every program uses every constant.
13#![allow(dead_code)]
14
15use tinymind::int8::*;
16
17/// The input vector.
18pub type Input = [i8; 7];
19/// The output vector.
20pub type Output = [i8; 5];
21/// Scale of the input tensor.
22pub const INPUT_SCALE: f32 = 0.018672613;
23/// Zero point of the input tensor.
24pub const INPUT_ZERO_POINT: i8 = 7;
25/// Scale of the output tensor. The last layer is a softmax: its output is on TFLite's
26/// probability grid, 1/256, not on the 1/255 grid ONNX Runtime gives it.
27pub const OUTPUT_SCALE: f32 = 0.00390625;
28/// Zero point of the output tensor.
29pub const OUTPUT_ZERO_POINT: i8 = -128;
30
31/// `MatMul` (Dense, reads the input): fully connected, 7 -> 9.
32static FC0_WEIGHTS: [[i8; 7]; 9] = [
33    [-4, -16, -27, -62, 2, 3, -54],
34    [19, 13, -86, 52, -4, 34, -22],
35    [-41, -12, 55, 24, 22, 36, 27],
36    [4, 48, 27, 127, -45, -4, -1],
37    [35, 11, -9, -8, -14, -22, -58],
38    [-15, 30, 3, -25, 7, 22, -25],
39    [84, 22, 29, -15, 7, -13, -33],
40    [-83, 14, 118, -18, 36, -83, -41],
41    [-3, 49, 97, 18, -50, 73, -47],
42];
43static FC0_BIASES: [i32; 9] = [-642, -1645, 241, 1645, -1946, 361, 1444, 3169, -742];
44static FC0: QDense<'static, 7, 9> = QDense {
45    weights: &FC0_WEIGHTS,
46    biases: Some(&FC0_BIASES),
47    input_zero_point: 7,
48    requantizer: Requantizer {
49        multiplier: 1819092912,
50        shift: 7,
51        zero_point: -21,
52        qmin: -128,
53        qmax: 127,
54    },
55};
56
57/// `Tanh` (Tanh, reads `MatMul`): tanh as a table from the input grid (scale 0.02165182, zero point -21) to the output's (0.0077539543, -2).
58static TANH1_LUT: [i8; LUT_SIZE] = [
59    -128, -128, -128, -128, -128, -128, -128, -128, -127, -127, -127, -127, -127, -127, -126, -126,
60    -126, -126, -126, -125, -125, -125, -125, -124, -124, -124, -123, -123, -123, -122, -122, -122,
61    -121, -121, -120, -120, -120, -119, -119, -118, -118, -117, -116, -116, -115, -114, -114, -113,
62    -112, -112, -111, -110, -109, -108, -107, -106, -105, -104, -103, -102, -101, -100, -99, -98,
63    -96, -95, -94, -92, -91, -89, -88, -86, -85, -83, -81, -79, -78, -76, -74, -72, -70, -68, -66,
64    -64, -61, -59, -57, -55, -52, -50, -47, -45, -42, -40, -37, -35, -32, -29, -27, -24, -21, -19,
65    -16, -13, -10, -8, -5, -2, 1, 4, 6, 9, 12, 15, 17, 20, 23, 25, 28, 31, 33, 36, 38, 41, 43, 46,
66    48, 51, 53, 55, 57, 60, 62, 64, 66, 68, 70, 72, 74, 75, 77, 79, 81, 82, 84, 85, 87, 88, 90, 91,
67    92, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 108, 109, 110, 110,
68    111, 112, 112, 113, 114, 114, 115, 115, 116, 116, 116, 117, 117, 118, 118, 118, 119, 119, 119,
69    120, 120, 120, 121, 121, 121, 121, 122, 122, 122, 122, 122, 123, 123, 123, 123, 123, 123, 124,
70    124, 124, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125,
71    126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,
72    126, 126, 126, 126, 126, 126, 126, 127, 127, 127,
73];
74
75/// `f_Gemm` (Dense, reads `Tanh`): fully connected, 9 -> 5.
76static FC2_WEIGHTS: [[i8; 9]; 5] = [
77    [-63, 22, 53, 13, -45, 24, -49, -43, -47],
78    [76, -89, 55, 66, -85, -97, -85, -108, 107],
79    [112, -18, 119, 36, -125, -116, -61, -127, -58],
80    [34, -3, -24, 8, 116, 1, 64, -31, -96],
81    [25, 59, 74, -120, -17, -123, 2, 6, 41],
82];
83static FC2_BIASES: [i32; 5] = [-5635, -2399, 4029, -15905, 8076];
84static FC2: QDense<'static, 9, 5> = QDense {
85    weights: &FC2_WEIGHTS,
86    biases: Some(&FC2_BIASES),
87    input_zero_point: -2,
88    requantizer: Requantizer {
89        multiplier: 1905613711,
90        shift: 8,
91        zero_point: 21,
92        qmin: -128,
93        qmax: 127,
94    },
95};
96
97/// `Softmax` (Softmax, reads `f_Gemm`): softmax over 5 logits, output on the 1/256 grid.
98static SOFTMAX3: QSoftmax = QSoftmax {
99    input_multiplier: 1609826432,
100    input_left_shift: 19,
101    diff_min: -3968,
102};
103
104/// Run the network on an int8 input in the `INPUT` grid.
105pub fn forward(input: &Input) -> Output {
106    let v0 = FC0.forward(input);
107    let v1 = v0.map(|x| apply_lut(x, &TANH1_LUT));
108    let v2 = FC2.forward(&v1);
109    SOFTMAX3.forward(&v2)
110}