tinymind 0.1.2

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind C++ library, bit-exact with it
Documentation
//! `example_classifier`: 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::{QDense, QSoftmax, Requantizer, qrelu};

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

/// 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.00622549;
/// Zero point of the `h1` tensor.
pub const H1_ZERO_POINT: i8 = -128;
/// Scale of the `fc2` tensor.
pub const FC2_SCALE: f32 = 0.023566175;
/// Zero point of the `fc2` tensor.
pub const FC2_ZERO_POINT: i8 = 15;
/// Scale of the `probs` tensor.
pub const PROBS_SCALE: f32 = 0.00390625;
/// Zero point of the `probs` tensor.
pub const PROBS_ZERO_POINT: i8 = -128;
/// Scale of the output (`probs`).
pub const OUTPUT_SCALE: f32 = PROBS_SCALE;
/// Zero point of the output (`probs`).
pub const OUTPUT_ZERO_POINT: i8 = PROBS_ZERO_POINT;

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

/// `fc2` int8 weights, `[output][input]` (weight scale 0.015748031).
pub static FC2_WEIGHTS: [[i8; 10]; 3] = [
    [-64, -127, 79, 16, -48, -111, 95, 32, -32, -95],
    [48, -16, -79, 127, 64, 0, -64, -127, 79, 16],
    [-111, 95, 32, -32, -95, 111, 48, -16, -79, 127],
];
/// `fc2` int32 biases (scale = input scale * weight scale).
pub static FC2_BIASES: [i32; 3] = [-2550, 5100, 0];
/// `fc2`: 10 -> 3, from `h1`.
pub static FC2: QDense<'static, 10, 3> = QDense {
    weights: &FC2_WEIGHTS,
    biases: Some(&FC2_BIASES),
    input_zero_point: -128,
    requantizer: Requantizer {
        multiplier: 1143537855,
        shift: 7,
        zero_point: 15,
        qmin: -128,
        qmax: 127,
    },
};

/// `probs`: softmax over the `fc2` logits, onto the `probs` grid (scale 1/256, zero point -128).
pub static PROBS: QSoftmax = QSoftmax {
    input_multiplier: 1619455232,
    input_left_shift: 21,
    diff_min: -992,
};

/// 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);
    PROBS.forward(&x1)
}