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_cnn_flat`: 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)`).
//! Feature maps are NHWC without the batch axis: `[row][column][channel]`.

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

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

/// The input feature map: 9 rows, 11 columns, 2 channels.
pub type Input = [[[i8; 2]; 11]; 9];
/// 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 `relu1` tensor.
pub const RELU1_SCALE: f32 = 0.0047058826;
/// Zero point of the `relu1` tensor.
pub const RELU1_ZERO_POINT: i8 = -128;
/// Scale of the `pool1` tensor.
pub const POOL1_SCALE: f32 = 0.0047058826;
/// Zero point of the `pool1` tensor.
pub const POOL1_ZERO_POINT: i8 = -128;
/// Scale of the `flat` tensor.
pub const FLAT_SCALE: f32 = 0.0047058826;
/// Zero point of the `flat` tensor.
pub const FLAT_ZERO_POINT: i8 = -128;
/// Scale of the `relu2` tensor.
pub const RELU2_SCALE: f32 = 0.0032607613;
/// Zero point of the `relu2` tensor.
pub const RELU2_ZERO_POINT: i8 = -1;
/// Scale of the `fc2` tensor.
pub const FC2_SCALE: f32 = 0.0034576417;
/// Zero point of the `fc2` tensor.
pub const FC2_ZERO_POINT: i8 = 86;
/// Scale of the `out` tensor.
pub const OUT_SCALE: f32 = 0.0078125;
/// Zero point of the `out` tensor.
pub const OUT_ZERO_POINT: i8 = 0;
/// 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;

/// `conv1` int8 weights, `[filter][row][column][channel]` (weight scale 0.003937008).
pub static CONV1_WEIGHTS: [[[[i8; 2]; 2]; 3]; 5] = [
    [
        [[0, 111], [-48, -32]],
        [[79, -79], [-64, 48]],
        [[-111, -95], [16, 127]],
    ],
    [
        [[-127, -16], [95, 111]],
        [[-48, 64], [79, -79]],
        [[32, 48], [-111, 0]],
    ],
    [
        [[16, 127], [-32, -16]],
        [[95, -64], [-48, 64]],
        [[-95, -79], [32, -127]],
    ],
    [
        [[-111, 0], [111, 127]],
        [[-32, 79], [95, -64]],
        [[48, 64], [-95, 16]],
    ],
    [
        [[32, -127], [-16, 0]],
        [[111, -48], [-32, 79]],
        [[-79, -64], [48, -111]],
    ],
];
/// `conv1` int32 biases (scale = input scale * weight scale).
pub static CONV1_BIASES: [i32; 5] = [-8096, 4048, -4048, 8096, 0];
/// `conv1`: 3x2 convolution, 5 filters, stride 2x1, padding 0/0/0/0 (top/bottom/left/right), from `input` (9x11x2) to 4x10x5, then ReLU into `relu1`.
pub static CONV1: QConv2D<'static, 2, 3, 2, 5, 2, 1, 0, 0, 0, 0> = QConv2D {
    weights: &CONV1_WEIGHTS,
    biases: Some(&CONV1_BIASES),
    input_zero_point: -1,
    requantizer: Requantizer {
        multiplier: 1803660800,
        shift: 7,
        zero_point: -128,
        qmin: -128,
        qmax: 127,
    },
};

/// `pool1`: 2x2 average pooling, stride 2x2, from `relu1` (4x10x5) to 2x5x5, in the same grid.
pub static POOL1: QAvgPool2D<2, 2, 2, 2> = QAvgPool2D {
    qmin: -128,
    qmax: 127,
};

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

/// `fc2` int8 weights, `[output][input]` (weight scale 0.007874016).
pub static FC2_WEIGHTS: [[i8; 6]; 2] = [[-111, 0, 111, 127, -32, 79], [95, -64, 48, 64, -95, 16]];
/// `fc2` int32 biases (scale = input scale * weight scale).
pub static FC2_BIASES: [i32; 2] = [0, -9737];
/// `fc2`: 6 -> 2, from `relu2`.
pub static FC2: QDense<'static, 6, 2> = QDense {
    weights: &FC2_WEIGHTS,
    biases: Some(&FC2_BIASES),
    input_zero_point: -1,
    requantizer: Requantizer {
        multiplier: 2041150979,
        shift: 7,
        zero_point: 86,
        qmin: -128,
        qmax: 127,
    },
};

/// `out`: tanh table from the `fc2` grid to the `out` grid.
pub static OUT_LUT: [i8; LUT_SIZE] = [
    -81, -80, -80, -80, -79, -79, -79, -79, -78, -78, -78, -78, -77, -77, -77, -76, -76, -76, -76,
    -75, -75, -75, -74, -74, -74, -73, -73, -73, -73, -72, -72, -72, -71, -71, -71, -70, -70, -70,
    -70, -69, -69, -69, -68, -68, -68, -67, -67, -67, -66, -66, -66, -65, -65, -65, -64, -64, -64,
    -63, -63, -63, -62, -62, -62, -61, -61, -61, -60, -60, -60, -59, -59, -59, -58, -58, -58, -57,
    -57, -56, -56, -56, -55, -55, -55, -54, -54, -54, -53, -53, -52, -52, -52, -51, -51, -51, -50,
    -50, -50, -49, -49, -48, -48, -48, -47, -47, -46, -46, -46, -45, -45, -45, -44, -44, -43, -43,
    -43, -42, -42, -41, -41, -41, -40, -40, -39, -39, -39, -38, -38, -37, -37, -37, -36, -36, -35,
    -35, -35, -34, -34, -33, -33, -32, -32, -32, -31, -31, -30, -30, -30, -29, -29, -28, -28, -27,
    -27, -27, -26, -26, -25, -25, -24, -24, -24, -23, -23, -22, -22, -21, -21, -21, -20, -20, -19,
    -19, -18, -18, -18, -17, -17, -16, -16, -15, -15, -15, -14, -14, -13, -13, -12, -12, -11, -11,
    -11, -10, -10, -9, -9, -8, -8, -8, -7, -7, -6, -6, -5, -5, -4, -4, -4, -3, -3, -2, -2, -1, -1,
    0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12,
    12, 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18,
];

/// Run the network on int8 inputs in the `INPUT` grid.
pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
    let mut x0 = [[[0i8; 5]; 10]; 4];
    CONV1.forward(input, &mut x0);
    let mut x2 = [[[0i8; 5]; 5]; 2];
    POOL1.forward(&x0, &mut x2);
    // `flat`: flatten `pool1` (2x5x5) in NHWC order.
    let mut x4 = [0i8; 50];
    for (out, &v) in x4.iter_mut().zip(x2.iter().flatten().flatten()) {
        *out = v;
    }
    let x9 = FC1.forward(&x4).map(|v| qrelu(v, RELU2_ZERO_POINT));
    let x10 = FC2.forward(&x9);
    x10.map(|v| apply_lut(v, &OUT_LUT))
}