Skip to main content

imported_cnn/common/
imported_cnn_model.rs

1//! `example_cnn`: 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//! Feature maps are NHWC without the batch axis: `[row][column][channel]`.
7
8// Not every program uses every constant.
9#![allow(dead_code)]
10
11use tinymind::int8::{
12    QConv2D, QDense, QDepthwiseConv2D, QGlobalAvgPool2D, QMaxPool2D, QPointwiseConv2D, QSoftmax,
13    Requantizer,
14};
15
16/// The input feature map: 12 rows, 12 columns, 1 channel.
17pub type Input = [[[i8; 1]; 12]; 12];
18/// Number of outputs.
19pub const NUM_OUTPUTS: usize = 3;
20
21/// Scale of the `input` tensor.
22pub const INPUT_SCALE: f32 = 0.007843138;
23/// Zero point of the `input` tensor.
24pub const INPUT_ZERO_POINT: i8 = -1;
25/// Scale of the `relu1` tensor.
26pub const RELU1_SCALE: f32 = 0.011801419;
27/// Zero point of the `relu1` tensor.
28pub const RELU1_ZERO_POINT: i8 = -128;
29/// Scale of the `pool1` tensor.
30pub const POOL1_SCALE: f32 = 0.011801419;
31/// Zero point of the `pool1` tensor.
32pub const POOL1_ZERO_POINT: i8 = -128;
33/// Scale of the `relu2` tensor.
34pub const RELU2_SCALE: f32 = 0.010787795;
35/// Zero point of the `relu2` tensor.
36pub const RELU2_ZERO_POINT: i8 = -128;
37/// Scale of the `relu3` tensor.
38pub const RELU3_SCALE: f32 = 0.0009883966;
39/// Zero point of the `relu3` tensor.
40pub const RELU3_ZERO_POINT: i8 = -1;
41/// Scale of the `gap` tensor.
42pub const GAP_SCALE: f32 = 0.0009883966;
43/// Zero point of the `gap` tensor.
44pub const GAP_ZERO_POINT: i8 = -1;
45/// Scale of the `fc1` tensor.
46pub const FC1_SCALE: f32 = 0.0033405602;
47/// Zero point of the `fc1` tensor.
48pub const FC1_ZERO_POINT: i8 = 127;
49/// Scale of the `probs` tensor.
50pub const PROBS_SCALE: f32 = 0.00390625;
51/// Zero point of the `probs` tensor.
52pub const PROBS_ZERO_POINT: i8 = -128;
53/// Scale of the output (`probs`).
54pub const OUTPUT_SCALE: f32 = PROBS_SCALE;
55/// Zero point of the output (`probs`).
56pub const OUTPUT_ZERO_POINT: i8 = PROBS_ZERO_POINT;
57
58/// `bn1` int8 weights, `[filter][row][column][channel]` (weight scale 0.0059054825).
59pub static BN1_WEIGHTS: [[[[i8; 1]; 3]; 3]; 4] = [
60    [
61        [[-27], [37], [-55]],
62        [[-46], [18], [-73]],
63        [[-64], [0], [64]],
64    ],
65    [
66        [[127], [-32], [79]],
67        [[95], [-64], [48]],
68        [[64], [-95], [16]],
69    ],
70    [
71        [[19], [-76], [-9]],
72        [[0], [66], [-28]],
73        [[-19], [47], [-47]],
74    ],
75    [
76        [[-30], [22], [-52]],
77        [[-45], [7], [60]],
78        [[-60], [-7], [45]],
79    ],
80];
81/// `bn1` int32 biases (scale = input scale * weight scale).
82pub static BN1_BIASES: [i32; 4] = [-5398, 12144, 7526, -6515];
83/// `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`.
84pub static BN1: QConv2D<'static, 1, 3, 3, 4, 1, 1, 1, 1, 1, 1> = QConv2D {
85    weights: &BN1_WEIGHTS,
86    biases: Some(&BN1_BIASES),
87    input_zero_point: -1,
88    requantizer: Requantizer {
89        multiplier: 1078824586,
90        shift: 7,
91        zero_point: -128,
92        qmin: -128,
93        qmax: 127,
94    },
95};
96
97/// `pool1`: 2x2 max pooling, stride 2x2, from `relu1` (12x12x4) to 6x6x4, in the same grid.
98pub static POOL1: QMaxPool2D<2, 2, 2, 2> = QMaxPool2D;
99
100/// `bn2` int8 weights, `[channel][row][column]` (weight scale 0.0060896873).
101pub static BN2_WEIGHTS: [[[i8; 3]; 3]; 4] = [
102    [[21, -82, -10], [0, 72, -31], [-21, 51, -51]],
103    [[-18, 14, -32], [-28, 5, 37], [-37, -5, 28]],
104    [[127, -54, 73], [91, -91, 36], [54, -127, 0]],
105    [[9, 71, -18], [-9, 53, -36], [-27, 36, -53]],
106];
107/// `bn2` int32 biases (scale = input scale * weight scale).
108pub static BN2_BIASES: [i32; 4] = [-1739, -8329, 9628, -3013];
109/// `bn2` requantizers, one per channel (the weights share one scale).
110pub static BN2_REQUANTIZERS: [Requantizer; 4] = [Requantizer {
111    multiplier: 1831202441,
112    shift: 7,
113    zero_point: -128,
114    qmin: -128,
115    qmax: 127,
116}; 4];
117/// `bn2`: 3x3 depthwise convolution, stride 1x1, padding 1/1/1/1 (top/bottom/left/right), from `pool1` (6x6x4) to 6x6x4, then ReLU into `relu2`.
118pub static BN2: QDepthwiseConv2D<'static, 4, 3, 3, 1, 1, 1, 1, 1, 1> = QDepthwiseConv2D {
119    weights: &BN2_WEIGHTS,
120    biases: Some(&BN2_BIASES),
121    input_zero_point: -128,
122    requantizers: &BN2_REQUANTIZERS,
123};
124
125/// `conv3` int8 weights, `[filter][channel]` (weight scale 0.003937008).
126pub static CONV3_WEIGHTS: [[i8; 4]; 8] = [
127    [111, -48, 64, 79],
128    [-79, 32, 48, -111],
129    [0, 16, 127, -32],
130    [-16, 95, -64, -48],
131    [64, -95, -79, 32],
132    [-127, -111, 0, 111],
133    [127, -32, 79, 95],
134    [-64, 48, 64, -95],
135];
136/// `conv3` int32 biases (scale = input scale * weight scale).
137pub static CONV3_BIASES: [i32; 8] = [2943, -2943, 5886, 0, -5886, 2943, -2943, 5886];
138/// `conv3`: 1x1 (pointwise) convolution, 4 -> 8 channels, from `relu2` (6x6x4) to 6x6x8, then ReLU into `relu3`.
139pub static CONV3: QPointwiseConv2D<'static, 4, 8> = QPointwiseConv2D {
140    weights: &CONV3_WEIGHTS,
141    biases: Some(&CONV3_BIASES),
142    input_zero_point: -128,
143    requantizer: Requantizer {
144        multiplier: 1476446082,
145        shift: 4,
146        zero_point: -1,
147        qmin: -1,
148        qmax: 127,
149    },
150};
151
152/// `gap`: global average pooling of `relu3` (6x6x8), in the same grid.
153pub static GAP: QGlobalAvgPool2D = QGlobalAvgPool2D {
154    qmin: -128,
155    qmax: 127,
156};
157
158/// `fc1` int8 weights, `[output][input]` (weight scale 0.007874016).
159pub static FC1_WEIGHTS: [[i8; 8]; 3] = [
160    [-79, 32, -127, -111, 0, 111, 127, -32],
161    [79, 95, -64, 48, 64, -95, 16, 32],
162    [-127, -16, 0, 111, -48, -32, 79, -79],
163];
164/// `fc1` int32 biases (scale = input scale * weight scale).
165pub static FC1_BIASES: [i32; 3] = [32123, 0, -32123];
166/// `fc1`: 8 -> 3, from `gap`.
167pub static FC1: QDense<'static, 8, 3> = QDense {
168    weights: &FC1_WEIGHTS,
169    biases: Some(&FC1_BIASES),
170    input_zero_point: -1,
171    requantizer: Requantizer {
172        multiplier: 1280790316,
173        shift: 8,
174        zero_point: 127,
175        qmin: -128,
176        qmax: 127,
177    },
178};
179
180/// `probs`: softmax over the `fc1` logits, onto the `probs` grid (scale 1/256, zero point -128).
181pub static PROBS: QSoftmax = QSoftmax {
182    input_multiplier: 1836492416,
183    input_left_shift: 18,
184    diff_min: -7936,
185};
186
187/// Run the network on int8 inputs in the `INPUT` grid.
188pub fn forward(input: &Input) -> [i8; NUM_OUTPUTS] {
189    let mut x0 = [[[0i8; 4]; 12]; 12];
190    BN1.forward(input, &mut x0);
191    let mut x2 = [[[0i8; 4]; 6]; 6];
192    POOL1.forward(&x0, &mut x2);
193    let mut x4 = [[[0i8; 4]; 6]; 6];
194    BN2.forward(&x2, &mut x4);
195    let mut x6 = [[[0i8; 8]; 6]; 6];
196    CONV3.forward(&x4, &mut x6);
197    let x8 = GAP.forward(&x6);
198    let x9 = FC1.forward(&x8);
199    PROBS.forward(&x9)
200}