tinymind 0.3.8

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind (C++) library, bit-exact with it
Documentation
//! `example_mlp`: generated by tinymind-rs tools/import_pytorch/tinymind_import.py.
//! Do not edit; regenerate from the model instead.
//!
//! Integer-only and `no_std`: int8 inputs in the `INPUT` grid, int8 outputs in the
//! `OUTPUT` grid (real value = `SCALE * (q - ZERO_POINT)`).

// Not every program uses every constant.
#![allow(dead_code)]

use tinymind::int8::{LUT_SIZE, QDense, Requantizer, apply_lut, qrelu};

/// Number of inputs.
pub const NUM_INPUTS: usize = 3;
/// Number of outputs.
pub const NUM_OUTPUTS: usize = 2;

/// Scale of the `input` tensor.
pub const INPUT_SCALE: f32 = 0.007843138;
/// Zero point of the `input` tensor.
pub const INPUT_ZERO_POINT: i8 = -1;
/// Scale of the `h1` tensor.
pub const H1_SCALE: f32 = 0.004509804;
/// Zero point of the `h1` tensor.
pub const H1_ZERO_POINT: i8 = -128;
/// Scale of the `h2` tensor.
pub const H2_SCALE: f32 = 0.0060324753;
/// Zero point of the `h2` tensor.
pub const H2_ZERO_POINT: i8 = -128;
/// Scale of the `fc3` tensor.
pub const FC3_SCALE: f32 = 0.005711566;
/// Zero point of the `fc3` tensor.
pub const FC3_ZERO_POINT: i8 = -1;
/// Scale of the `out` tensor.
pub const OUT_SCALE: f32 = 0.00390625;
/// Zero point of the `out` tensor.
pub const OUT_ZERO_POINT: i8 = -128;
/// Scale of the output (`out`).
pub const OUTPUT_SCALE: f32 = OUT_SCALE;
/// Zero point of the output (`out`).
pub const OUTPUT_ZERO_POINT: i8 = OUT_ZERO_POINT;

/// `fc1` int8 weights, `[output][input]` (weight scale 0.007874016).
pub static FC1_WEIGHTS: [[i8; 3]; 8] = [
    [-48, -111, 95],
    [64, 0, -64],
    [-95, 111, 48],
    [16, -48, -111],
    [127, 64, 0],
    [-32, -95, 111],
    [79, 16, -48],
    [-79, 127, 64],
];
/// `fc1` int32 biases (scale = input scale * weight scale).
pub static FC1_BIASES: [i32; 8] = [-4048, 8096, 0, -8096, 4048, -4048, 8096, 0];
/// `fc1`: 3 -> 8, from `input`, then ReLU into `h1`.
pub static FC1: QDense<'static, 3, 8> = QDense {
    weights: &FC1_WEIGHTS,
    biases: Some(&FC1_BIASES),
    input_zero_point: -1,
    requantizer: Requantizer {
        multiplier: 1882080981,
        shift: 6,
        zero_point: -128,
        qmin: -128,
        qmax: 127,
    },
};

/// `fc2` int8 weights, `[output][input]` (weight scale 0.007874016).
pub static FC2_WEIGHTS: [[i8; 8]; 4] = [
    [32, -32, -95, 111, 48, -16, -79, 127],
    [-127, 79, 16, -48, -111, 95, 32, -32],
    [-16, -79, 127, 64, 0, -64, -127, 79],
    [95, 32, -32, -95, 111, 48, -16, -79],
];
/// `fc2` int32 biases (scale = input scale * weight scale).
pub static FC2_BIASES: [i32; 4] = [0, -14080, 7040, -7040];
/// `fc2`: 8 -> 4, from `h1`, then ReLU into `h2`.
pub static FC2: QDense<'static, 8, 4> = QDense {
    weights: &FC2_WEIGHTS,
    biases: Some(&FC2_BIASES),
    input_zero_point: -128,
    requantizer: Requantizer {
        multiplier: 1618073385,
        shift: 7,
        zero_point: -128,
        qmin: -128,
        qmax: 127,
    },
};

/// `fc3` int8 weights, `[output][input]` (weight scale 0.0068897638).
pub static FC3_WEIGHTS: [[i8; 4]; 2] = [[127, 54, -18, -91], [-54, -127, 109, 36]];
/// `fc3` int32 biases (scale = input scale * weight scale).
pub static FC3_BIASES: [i32; 2] = [6015, -6015];
/// `fc3`: 4 -> 2, from `h2`.
pub static FC3: QDense<'static, 4, 2> = QDense {
    weights: &FC3_WEIGHTS,
    biases: Some(&FC3_BIASES),
    input_zero_point: -128,
    requantizer: Requantizer {
        multiplier: 2000251025,
        shift: 7,
        zero_point: -1,
        qmin: -128,
        qmax: 127,
    },
};

/// `out`: sigmoid table from the `fc3` grid to the `out` grid.
pub static OUT_LUT: [i8; LUT_SIZE] = [
    -44, -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39,
    -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -34, -33, -33, -33, -32,
    -32, -32, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26,
    -25, -25, -25, -24, -24, -23, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19,
    -19, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -13, -13, -13, -12, -12,
    -12, -11, -11, -11, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -5, -5, -5, -4, -4,
    -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,
    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,
    16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 25,
    25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 32, 32, 32, 33, 33,
    33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41,
    41, 42, 42, 42, 43, 43, 43, 44, 44, 44, 44, 45,
];

/// Run the network on int8 inputs in the `INPUT` grid.
pub fn forward(input: &[i8; NUM_INPUTS]) -> [i8; NUM_OUTPUTS] {
    let x0 = FC1.forward(input).map(|v| qrelu(v, H1_ZERO_POINT));
    let x1 = FC2.forward(&x0).map(|v| qrelu(v, H2_ZERO_POINT));
    let x2 = FC3.forward(&x1);
    x2.map(|v| apply_lut(v, &OUT_LUT))
}