Skip to main content

imported_mlp/common/
imported_classifier_model.rs

1//! `example_classifier`: 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::{QDense, QSoftmax, Requantizer, qrelu};
11
12/// Number of inputs.
13pub const NUM_INPUTS: usize = 4;
14/// Number of outputs.
15pub const NUM_OUTPUTS: usize = 3;
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.00622549;
23/// Zero point of the `h1` tensor.
24pub const H1_ZERO_POINT: i8 = -128;
25/// Scale of the `fc2` tensor.
26pub const FC2_SCALE: f32 = 0.023566175;
27/// Zero point of the `fc2` tensor.
28pub const FC2_ZERO_POINT: i8 = 15;
29/// Scale of the `probs` tensor.
30pub const PROBS_SCALE: f32 = 0.00390625;
31/// Zero point of the `probs` tensor.
32pub const PROBS_ZERO_POINT: i8 = -128;
33/// Scale of the output (`probs`).
34pub const OUTPUT_SCALE: f32 = PROBS_SCALE;
35/// Zero point of the output (`probs`).
36pub const OUTPUT_ZERO_POINT: i8 = PROBS_ZERO_POINT;
37
38/// `fc1` int8 weights, `[output][input]` (weight scale 0.007874016).
39pub static FC1_WEIGHTS: [[i8; 4]; 10] = [
40    [-32, -95, 111, 48],
41    [79, 16, -48, -111],
42    [-79, 127, 64, 0],
43    [32, -32, -95, 111],
44    [-127, 79, 16, -48],
45    [-16, -79, 127, 64],
46    [95, 32, -32, -95],
47    [-64, -127, 79, 16],
48    [48, -16, -79, 127],
49    [-111, 95, 32, -32],
50];
51/// `fc1` int32 biases (scale = input scale * weight scale).
52pub static FC1_BIASES: [i32; 10] = [4048, -4048, 8096, 0, -8096, 4048, -4048, 8096, 0, -8096];
53/// `fc1`: 4 -> 10, from `input`, then ReLU into `h1`.
54pub static FC1: QDense<'static, 4, 10> = QDense {
55    weights: &FC1_WEIGHTS,
56    biases: Some(&FC1_BIASES),
57    input_zero_point: -1,
58    requantizer: Requantizer {
59        multiplier: 1363397261,
60        shift: 6,
61        zero_point: -128,
62        qmin: -128,
63        qmax: 127,
64    },
65};
66
67/// `fc2` int8 weights, `[output][input]` (weight scale 0.015748031).
68pub static FC2_WEIGHTS: [[i8; 10]; 3] = [
69    [-64, -127, 79, 16, -48, -111, 95, 32, -32, -95],
70    [48, -16, -79, 127, 64, 0, -64, -127, 79, 16],
71    [-111, 95, 32, -32, -95, 111, 48, -16, -79, 127],
72];
73/// `fc2` int32 biases (scale = input scale * weight scale).
74pub static FC2_BIASES: [i32; 3] = [-2550, 5100, 0];
75/// `fc2`: 10 -> 3, from `h1`.
76pub static FC2: QDense<'static, 10, 3> = QDense {
77    weights: &FC2_WEIGHTS,
78    biases: Some(&FC2_BIASES),
79    input_zero_point: -128,
80    requantizer: Requantizer {
81        multiplier: 1143537855,
82        shift: 7,
83        zero_point: 15,
84        qmin: -128,
85        qmax: 127,
86    },
87};
88
89/// `probs`: softmax over the `fc2` logits, onto the `probs` grid (scale 1/256, zero point -128).
90pub static PROBS: QSoftmax = QSoftmax {
91    input_multiplier: 1619455232,
92    input_left_shift: 21,
93    diff_min: -992,
94};
95
96/// Run the network on int8 inputs in the `INPUT` grid.
97pub fn forward(input: &[i8; NUM_INPUTS]) -> [i8; NUM_OUTPUTS] {
98    let x0 = FC1.forward(input).map(|v| qrelu(v, H1_ZERO_POINT));
99    let x1 = FC2.forward(&x0);
100    PROBS.forward(&x1)
101}