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`: 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::{
    QConv2D, QDense, QDepthwiseConv2D, QGlobalAvgPool2D, QMaxPool2D, QPointwiseConv2D, QSoftmax,
    Requantizer,
};

/// The input feature map: 12 rows, 12 columns, 1 channel.
pub type Input = [[[i8; 1]; 12]; 12];
/// 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 `relu1` tensor.
pub const RELU1_SCALE: f32 = 0.011801419;
/// Zero point of the `relu1` tensor.
pub const RELU1_ZERO_POINT: i8 = -128;
/// Scale of the `pool1` tensor.
pub const POOL1_SCALE: f32 = 0.011801419;
/// Zero point of the `pool1` tensor.
pub const POOL1_ZERO_POINT: i8 = -128;
/// Scale of the `relu2` tensor.
pub const RELU2_SCALE: f32 = 0.010787795;
/// Zero point of the `relu2` tensor.
pub const RELU2_ZERO_POINT: i8 = -128;
/// Scale of the `relu3` tensor.
pub const RELU3_SCALE: f32 = 0.0009883966;
/// Zero point of the `relu3` tensor.
pub const RELU3_ZERO_POINT: i8 = -1;
/// Scale of the `gap` tensor.
pub const GAP_SCALE: f32 = 0.0009883966;
/// Zero point of the `gap` tensor.
pub const GAP_ZERO_POINT: i8 = -1;
/// Scale of the `fc1` tensor.
pub const FC1_SCALE: f32 = 0.0033405602;
/// Zero point of the `fc1` tensor.
pub const FC1_ZERO_POINT: i8 = 127;
/// 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;

/// `bn1` int8 weights, `[filter][row][column][channel]` (weight scale 0.0059054825).
pub static BN1_WEIGHTS: [[[[i8; 1]; 3]; 3]; 4] = [
    [
        [[-27], [37], [-55]],
        [[-46], [18], [-73]],
        [[-64], [0], [64]],
    ],
    [
        [[127], [-32], [79]],
        [[95], [-64], [48]],
        [[64], [-95], [16]],
    ],
    [
        [[19], [-76], [-9]],
        [[0], [66], [-28]],
        [[-19], [47], [-47]],
    ],
    [
        [[-30], [22], [-52]],
        [[-45], [7], [60]],
        [[-60], [-7], [45]],
    ],
];
/// `bn1` int32 biases (scale = input scale * weight scale).
pub static BN1_BIASES: [i32; 4] = [-5398, 12144, 7526, -6515];
/// `bn1`: 3x3 convolution, 4 filters, stride 1x1, padding 1/1/1/1 (top/bottom/left/right), from `input` (12x12x1) to 12x12x4, then ReLU into `relu1`.
pub static BN1: QConv2D<'static, 1, 3, 3, 4, 1, 1, 1, 1, 1, 1> = QConv2D {
    weights: &BN1_WEIGHTS,
    biases: Some(&BN1_BIASES),
    input_zero_point: -1,
    requantizer: Requantizer {
        multiplier: 1078824586,
        shift: 7,
        zero_point: -128,
        qmin: -128,
        qmax: 127,
    },
};

/// `pool1`: 2x2 max pooling, stride 2x2, from `relu1` (12x12x4) to 6x6x4, in the same grid.
pub static POOL1: QMaxPool2D<2, 2, 2, 2> = QMaxPool2D;

/// `bn2` int8 weights, `[channel][row][column]` (weight scale 0.0060896873).
pub static BN2_WEIGHTS: [[[i8; 3]; 3]; 4] = [
    [[21, -82, -10], [0, 72, -31], [-21, 51, -51]],
    [[-18, 14, -32], [-28, 5, 37], [-37, -5, 28]],
    [[127, -54, 73], [91, -91, 36], [54, -127, 0]],
    [[9, 71, -18], [-9, 53, -36], [-27, 36, -53]],
];
/// `bn2` int32 biases (scale = input scale * weight scale).
pub static BN2_BIASES: [i32; 4] = [-1739, -8329, 9628, -3013];
/// `bn2` requantizers, one per channel (the weights share one scale).
pub static BN2_REQUANTIZERS: [Requantizer; 4] = [Requantizer {
    multiplier: 1831202441,
    shift: 7,
    zero_point: -128,
    qmin: -128,
    qmax: 127,
}; 4];
/// `bn2`: 3x3 depthwise convolution, stride 1x1, padding 1/1/1/1 (top/bottom/left/right), from `pool1` (6x6x4) to 6x6x4, then ReLU into `relu2`.
pub static BN2: QDepthwiseConv2D<'static, 4, 3, 3, 1, 1, 1, 1, 1, 1> = QDepthwiseConv2D {
    weights: &BN2_WEIGHTS,
    biases: Some(&BN2_BIASES),
    input_zero_point: -128,
    requantizers: &BN2_REQUANTIZERS,
};

/// `conv3` int8 weights, `[filter][channel]` (weight scale 0.003937008).
pub static CONV3_WEIGHTS: [[i8; 4]; 8] = [
    [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],
];
/// `conv3` int32 biases (scale = input scale * weight scale).
pub static CONV3_BIASES: [i32; 8] = [2943, -2943, 5886, 0, -5886, 2943, -2943, 5886];
/// `conv3`: 1x1 (pointwise) convolution, 4 -> 8 channels, from `relu2` (6x6x4) to 6x6x8, then ReLU into `relu3`.
pub static CONV3: QPointwiseConv2D<'static, 4, 8> = QPointwiseConv2D {
    weights: &CONV3_WEIGHTS,
    biases: Some(&CONV3_BIASES),
    input_zero_point: -128,
    requantizer: Requantizer {
        multiplier: 1476446082,
        shift: 4,
        zero_point: -1,
        qmin: -1,
        qmax: 127,
    },
};

/// `gap`: global average pooling of `relu3` (6x6x8), in the same grid.
pub static GAP: QGlobalAvgPool2D = QGlobalAvgPool2D {
    qmin: -128,
    qmax: 127,
};

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

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

/// Run the network on int8 inputs in the `INPUT` grid.
pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
    let mut x0 = [[[0i8; 4]; 12]; 12];
    BN1.forward(input, &mut x0);
    let mut x2 = [[[0i8; 4]; 6]; 6];
    POOL1.forward(&x0, &mut x2);
    let mut x4 = [[[0i8; 4]; 6]; 6];
    BN2.forward(&x2, &mut x4);
    let mut x6 = [[[0i8; 8]; 6]; 6];
    CONV3.forward(&x4, &mut x6);
    let x8 = GAP.forward(&x6);
    let x9 = FC1.forward(&x8);
    PROBS.forward(&x9)
}