Skip to main content

imported_onnx/common/
imported_onnx_resnet_qop_model.rs

1//! `onnx_resnet_qop`: generated by tinymind-rs tools/import_onnx/tinymind_import_onnx.py
2//! from an int8 ONNX model quantized by ONNX Runtime. Do not edit; regenerate
3//! from the model instead.
4//! The importer's example The same network ending in a Softmax
5//! (tools/import_onnx/models/resnet_qop.onnx: QOperator format, per-tensor weights).
6//!
7//! Integer-only and `no_std`: int8 input in the `INPUT` grid, int8 output in the
8//! `OUTPUT` grid (real value = `SCALE * (q - ZERO_POINT)`). Feature maps are NHWC
9//! without the batch axis, `[row][column][channel]`: the transpose of the NCHW
10//! tensor the ONNX model takes.
11
12// Not every program uses every constant.
13#![allow(dead_code)]
14
15use tinymind::int8::*;
16
17/// The input: 8 x 24 x 2 (height x width x channels, NHWC).
18pub type Input = [[[i8; 2]; 24]; 8];
19/// The output vector.
20pub type Output = [i8; 4];
21/// Scale of the input tensor.
22pub const INPUT_SCALE: f32 = 0.030285051;
23/// Zero point of the input tensor.
24pub const INPUT_ZERO_POINT: i8 = -7;
25/// Scale of the output tensor. The last layer is a softmax: its output is on TFLite's
26/// probability grid, 1/256, not on the 1/255 grid ONNX Runtime gives it.
27pub const OUTPUT_SCALE: f32 = 0.00390625;
28/// Zero point of the output tensor.
29pub const OUTPUT_ZERO_POINT: i8 = -128;
30
31/// `c1_Conv_quant` (Conv, reads the input): 3x3 convolution, stride 1x1, padding 1/1/1/1 (top/bottom/left/right), 8x24x2 -> 8x24x5.
32static CONV0_WEIGHTS: [[[[i8; 2]; 3]; 3]; 5] = [
33    [
34        [[85, 127], [-96, 27], [105, 17]],
35        [[82, -110], [108, 41], [-99, 55]],
36        [[-86, 20], [61, 124], [-120, 39]],
37    ],
38    [
39        [[-114, 46], [108, -61], [44, -28]],
40        [[-61, -86], [-118, -61], [73, -117]],
41        [[122, -3], [-105, 76], [7, 62]],
42    ],
43    [
44        [[121, 103], [-113, 108], [-16, 25]],
45        [[125, 92], [21, -32], [-36, 104]],
46        [[-116, -11], [60, -26], [98, -64]],
47    ],
48    [
49        [[68, -42], [52, -87], [82, 104]],
50        [[-82, 14], [-68, 59], [122, 95]],
51        [[-71, -83], [-73, 98], [-34, -64]],
52    ],
53    [
54        [[-20, -9], [97, -16], [118, 69]],
55        [[113, 88], [74, -93], [119, -71]],
56        [[-46, -40], [-8, 17], [-44, -50]],
57    ],
58];
59static CONV0_BIASES: [i32; 5] = [-2639, -775, -2071, 117, -3141];
60static CONV0: QConv2D<'static, 2, 3, 3, 5, 1, 1, 1, 1, 1, 1> = QConv2D {
61    weights: &CONV0_WEIGHTS,
62    biases: Some(&CONV0_BIASES),
63    input_zero_point: -7,
64    requantizer: Requantizer {
65        multiplier: 1590310321,
66        shift: 7,
67        zero_point: -128,
68        qmin: -128,
69        qmax: 127,
70    },
71};
72
73/// `MaxPool` (MaxPool, reads `c1_Conv_quant`): 2x2 max pool, stride 2x2, 8x24x5 -> 4x12x5.
74static MAXPOOL1: QMaxPool2D<2, 2, 2, 2> = QMaxPool2D;
75
76/// `dw_Conv_quant` (DepthwiseConv, reads `MaxPool`): 3x3 depthwise convolution, stride 1x1, padding 1/1/1/1 (top/bottom/left/right), 4x12x5 -> 4x12x5.
77static DW2_WEIGHTS: [[[i8; 3]; 3]; 5] = [
78    [[-33, -48, -18], [103, 86, -42], [-120, -93, -100]],
79    [[-12, -58, -91], [29, 8, -20], [-47, 111, -126]],
80    [[-3, 61, 92], [-114, -20, 109], [22, 40, -58]],
81    [[91, -49, -70], [119, 35, -1], [-91, -29, -100]],
82    [[-88, -19, 25], [-107, 127, 96], [-122, 82, -78]],
83];
84static DW2_BIASES: [i32; 5] = [-5545, 6375, -2736, -2257, 848];
85static DW2_REQUANTIZERS: [Requantizer; 5] = [Requantizer {
86    multiplier: 2075791370,
87    shift: 7,
88    zero_point: -128,
89    qmin: -128,
90    qmax: 127,
91}; 5];
92static DW2: QDepthwiseConv2D<'static, 5, 3, 3, 1, 1, 1, 1, 1, 1> = QDepthwiseConv2D {
93    weights: &DW2_WEIGHTS,
94    biases: Some(&DW2_BIASES),
95    input_zero_point: -128,
96    requantizers: &DW2_REQUANTIZERS,
97};
98
99/// `Tanh` (Tanh, reads `MaxPool`): tanh as a table from the input grid (scale 0.019204173, zero point -128) to the output's (0.003921131, -128).
100static TANH3_LUT: [i8; LUT_SIZE] = [
101    -128, -123, -118, -113, -108, -104, -99, -94, -89, -84, -80, -75, -70, -66, -61, -57, -52, -48,
102    -43, -39, -35, -30, -26, -22, -18, -14, -10, -6, -3, 1, 5, 8, 12, 15, 18, 22, 25, 28, 31, 34,
103    37, 40, 42, 45, 48, 50, 53, 55, 57, 60, 62, 64, 66, 68, 70, 72, 74, 76, 77, 79, 81, 82, 84, 85,
104    87, 88, 90, 91, 92, 93, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 104, 105, 106, 107, 108,
105    108, 109, 110, 110, 111, 111, 112, 113, 113, 114, 114, 115, 115, 115, 116, 116, 117, 117, 117,
106    118, 118, 118, 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, 122, 122,
107    123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 124, 124, 124, 125, 125, 125,
108    125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, 126,
109    126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 127,
110    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
111    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
112    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
113    127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
114];
115
116/// `pw_Conv_quant` (Conv, reads `dw_Conv_quant`): 1x1 pointwise convolution, stride 1x1, padding 0/0/0/0 (top/bottom/left/right), 4x12x5 -> 4x12x5.
117static PW4_WEIGHTS: [[i8; 5]; 5] = [
118    [73, 10, -35, 11, -67],
119    [-93, 33, 14, 64, -48],
120    [-127, -32, -124, -10, 63],
121    [12, 74, 24, 55, 116],
122    [19, 21, -52, 80, 108],
123];
124static PW4_BIASES: [i32; 5] = [-6879, 9613, 2987, -1382, -9064];
125static PW4: QPointwiseConv2D<'static, 5, 5> = QPointwiseConv2D {
126    weights: &PW4_WEIGHTS,
127    biases: Some(&PW4_BIASES),
128    input_zero_point: -128,
129    requantizer: Requantizer {
130        multiplier: 1200983374,
131        shift: 7,
132        zero_point: -7,
133        qmin: -128,
134        qmax: 127,
135    },
136};
137
138/// `Add_quant` (Add, reads `Tanh` and `pw_Conv_quant`): elementwise sum, 4 x 12 x 5; parameters as tinymind (C++)'s
139/// buildQAddParams builds them.
140static ADD5: QAdd = QAdd {
141    input_a_zero_point: -128,
142    input_b_zero_point: -7,
143    left_shift: 20,
144    input_a_multiplier: 1629484999,
145    input_a_shift: 3,
146    input_b_multiplier: 1073741824,
147    input_b_shift: 0,
148    output_requantizer: Requantizer {
149        multiplier: 1932328772,
150        shift: 19,
151        zero_point: -35,
152        qmin: -128,
153        qmax: 127,
154    },
155};
156
157/// `Sigmoid_quant` (Sigmoid, reads `Add_quant`): sigmoid as a table from the input grid (scale 0.022972047, zero point -35) to the output's (0.0038280992, -128).
158static SIGMOID6_LUT: [i8; LUT_SIZE] = [
159    -100, -100, -99, -99, -98, -97, -97, -96, -96, -95, -94, -94, -93, -92, -91, -91, -90, -89,
160    -88, -88, -87, -86, -85, -84, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72,
161    -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -59, -58, -57, -56, -55, -54, -52, -51,
162    -50, -49, -47, -46, -45, -43, -42, -41, -39, -38, -37, -35, -34, -33, -31, -30, -28, -27, -25,
163    -24, -23, -21, -20, -18, -17, -15, -14, -12, -11, -9, -8, -6, -5, -3, -2, 0, 1, 3, 4, 6, 7, 9,
164    10, 12, 13, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 36, 38, 39, 40, 42, 43,
165    45, 46, 47, 49, 50, 51, 52, 54, 55, 56, 57, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 71, 73,
166    74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 83, 84, 85, 86, 87, 88, 89, 90, 90, 91, 92, 93, 94, 94,
167    95, 96, 97, 97, 98, 99, 99, 100, 101, 101, 102, 103, 103, 104, 104, 105, 106, 106, 107, 107,
168    108, 108, 109, 109, 110, 110, 111, 111, 112, 112, 113, 113, 113, 114, 114, 115, 115, 115, 116,
169    116, 117, 117, 117, 118, 118, 118, 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122,
170    122, 122, 122, 123, 123, 123, 123, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 126, 126,
171    126, 126, 126, 126, 126, 127, 127, 127, 127,
172];
173
174/// `AveragePool_quant` (AveragePool, reads `Sigmoid_quant`): 2x2 average pool, stride 2x2, padding 0/0/0/0 (top/bottom/left/right), 4x12x5 -> 2x6x5. A depthwise convolution
175/// whose taps all weigh 64: its requantizer divides by the window and moves to
176/// the output's grid in one rounding (tinymind (C++)'s importer does the same).
177static AVGPOOL7_WEIGHTS: [[[i8; 2]; 2]; 5] = [
178    [[64, 64], [64, 64]],
179    [[64, 64], [64, 64]],
180    [[64, 64], [64, 64]],
181    [[64, 64], [64, 64]],
182    [[64, 64], [64, 64]],
183];
184static AVGPOOL7_REQUANTIZERS: [Requantizer; 5] = [Requantizer {
185    multiplier: 1181803306,
186    shift: 7,
187    zero_point: -128,
188    qmin: -128,
189    qmax: 127,
190}; 5];
191static AVGPOOL7: QDepthwiseConv2D<'static, 5, 2, 2, 2, 2, 0, 0, 0, 0> = QDepthwiseConv2D {
192    weights: &AVGPOOL7_WEIGHTS,
193    biases: None,
194    input_zero_point: -128,
195    requantizers: &AVGPOOL7_REQUANTIZERS,
196};
197
198/// `GlobalAveragePool_quant` (AveragePool, reads `AveragePool_quant`): 2x6 average pool, stride 1x1, padding 0/0/0/0 (top/bottom/left/right), 2x6x5 -> 1x1x5. A depthwise convolution
199/// whose taps all weigh 64: its requantizer divides by the window and moves to
200/// the output's grid in one rounding (tinymind (C++)'s importer does the same).
201static AVGPOOL8_WEIGHTS: [[[i8; 6]; 2]; 5] = [
202    [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
203    [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
204    [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
205    [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
206    [[64, 64, 64, 64, 64, 64], [64, 64, 64, 64, 64, 64]],
207];
208static AVGPOOL8_REQUANTIZERS: [Requantizer; 5] = [Requantizer {
209    multiplier: 1507448591,
210    shift: 9,
211    zero_point: -128,
212    qmin: -128,
213    qmax: 127,
214}; 5];
215static AVGPOOL8: QDepthwiseConv2D<'static, 5, 2, 6, 1, 1, 0, 0, 0, 0> = QDepthwiseConv2D {
216    weights: &AVGPOOL8_WEIGHTS,
217    biases: None,
218    input_zero_point: -128,
219    requantizers: &AVGPOOL8_REQUANTIZERS,
220};
221
222/// `f_Gemm_quant` (Dense, reads `GlobalAveragePool_quant`): fully connected, 5 -> 4.
223static FC9_WEIGHTS: [[i8; 5]; 4] = [
224    [-127, -100, -106, -85, -37],
225    [-105, -106, -14, -107, -11],
226    [22, 24, 80, 69, -71],
227    [-43, 3, 107, 62, -71],
228];
229static FC9_BIASES: [i32; 4] = [-4514, 6954, 13362, -14296];
230static FC9: QDense<'static, 5, 4> = QDense {
231    weights: &FC9_WEIGHTS,
232    biases: Some(&FC9_BIASES),
233    input_zero_point: -128,
234    requantizer: Requantizer {
235        multiplier: 2044683897,
236        shift: 9,
237        zero_point: 46,
238        qmin: -128,
239        qmax: 127,
240    },
241};
242
243/// `Softmax_quant` (Softmax, reads `f_Gemm_quant`): softmax over 4 logits, output on the 1/256 grid.
244static SOFTMAX10: QSoftmax = QSoftmax {
245    input_multiplier: 1718222976,
246    input_left_shift: 20,
247    diff_min: -1984,
248};
249
250/// Run the network on an int8 input in the `INPUT` grid.
251pub fn forward(input: &Input) -> Output {
252    let mut b0 = [[[0i8; 5]; 24]; 8];
253    let mut b1 = [[[0i8; 5]; 12]; 4];
254    let mut b2 = [[[0i8; 5]; 12]; 4];
255    let mut b3 = [[[0i8; 5]; 12]; 4];
256    let mut b4 = [[[0i8; 5]; 6]; 2];
257    let mut b5 = [[[0i8; 5]; 1]; 1];
258    CONV0.forward(input, &mut b0);
259    MAXPOOL1.forward(&b0, &mut b1);
260    DW2.forward(&b1, &mut b2);
261    for x in b1.as_flattened_mut().as_flattened_mut() {
262        *x = apply_lut(*x, &TANH3_LUT);
263    }
264    PW4.forward(&b2, &mut b3);
265    ADD5.forward(&b1, &b3, &mut b2);
266    for x in b2.as_flattened_mut().as_flattened_mut() {
267        *x = apply_lut(*x, &SIGMOID6_LUT);
268    }
269    AVGPOOL7.forward(&b2, &mut b4);
270    AVGPOOL8.forward(&b4, &mut b5);
271    let v9 = FC9.forward(
272        b5.as_flattened()
273            .as_flattened()
274            .first_chunk::<5>()
275            .expect("5 values"),
276    );
277    SOFTMAX10.forward(&v9)
278}