Skip to main content

imported_onnx/common/
imported_onnx_cnn_qop_model.rs

1//! `onnx_cnn_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 Conv 3x3 + ReLU -> pointwise Conv + ReLU -> Linear + ReLU -> Linear without bias
5//! -> Sigmoid on a 1 x 6 x 6 input
6//! (tools/import_onnx/models/cnn_qop.onnx: QOperator format, per-tensor weights).
7//!
8//! Integer-only and `no_std`: int8 input in the `INPUT` grid, int8 output in the
9//! `OUTPUT` grid (real value = `SCALE * (q - ZERO_POINT)`). Feature maps are NHWC
10//! without the batch axis, `[row][column][channel]`: the transpose of the NCHW
11//! tensor the ONNX model takes.
12
13// Not every program uses every constant.
14#![allow(dead_code)]
15
16use tinymind::int8::*;
17
18/// The input: 6 x 6 x 1 (height x width x channels, NHWC).
19pub type Input = [[[i8; 1]; 6]; 6];
20/// The output vector.
21pub type Output = [i8; 3];
22/// Scale of the input tensor.
23pub const INPUT_SCALE: f32 = 0.022835715;
24/// Zero point of the input tensor.
25pub const INPUT_ZERO_POINT: i8 = 14;
26/// Scale of the output tensor.
27pub const OUTPUT_SCALE: f32 = 0.0028052556;
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), 6x6x1 -> 6x6x4.
32static CONV0_WEIGHTS: [[[[i8; 1]; 3]; 3]; 4] = [
33    [
34        [[30], [-31], [36]],
35        [[-7], [55], [31]],
36        [[-15], [-105], [30]],
37    ],
38    [
39        [[-115], [17], [9]],
40        [[-29], [106], [9]],
41        [[54], [55], [-76]],
42    ],
43    [
44        [[-50], [125], [-127]],
45        [[-9], [-10], [92]],
46        [[-12], [34], [-6]],
47    ],
48    [
49        [[-73], [-73], [-63]],
50        [[-118], [-84], [31]],
51        [[85], [6], [-59]],
52    ],
53];
54static CONV0_BIASES: [i32; 4] = [2495, -2180, -1258, -3113];
55static CONV0: QConv2D<'static, 1, 3, 3, 4, 1, 1, 1, 1, 1, 1> = QConv2D {
56    weights: &CONV0_WEIGHTS,
57    biases: Some(&CONV0_BIASES),
58    input_zero_point: 14,
59    requantizer: Requantizer {
60        multiplier: 1480698594,
61        shift: 6,
62        zero_point: -128,
63        qmin: -128,
64        qmax: 127,
65    },
66};
67
68/// `c2_Conv_quant` (Conv, reads `c1_Conv_quant`): 1x1 pointwise convolution, stride 1x1, padding 0/0/0/0 (top/bottom/left/right), 6x6x4 -> 6x6x3.
69static PW1_WEIGHTS: [[i8; 4]; 3] = [[-43, -127, 58, 53], [27, 67, 58, 6], [15, 10, 73, 92]];
70static PW1_BIASES: [i32; 3] = [9032, 7289, -3066];
71static PW1: QPointwiseConv2D<'static, 4, 3> = QPointwiseConv2D {
72    weights: &PW1_WEIGHTS,
73    biases: Some(&PW1_BIASES),
74    input_zero_point: -128,
75    requantizer: Requantizer {
76        multiplier: 1901513862,
77        shift: 7,
78        zero_point: -128,
79        qmin: -128,
80        qmax: 127,
81    },
82};
83
84/// `f1_Gemm_quant` (Dense, reads `c2_Conv_quant`): fully connected, 108 -> 10.
85static FC2_WEIGHTS: [[i8; 108]; 10] = [
86    [
87        -6, -89, 87, -26, 78, -124, 74, 10, -111, 14, 76, -86, 118, 68, 40, 64, -98, -52, -109,
88        -79, -113, 37, -87, 49, 122, -41, 64, 113, -46, 48, -2, -20, -109, 42, -123, 124, -119, 54,
89        -10, -41, 72, -121, 62, 40, -19, -116, 81, 83, 111, 96, 59, -84, -126, -1, 40, 19, 90, -5,
90        118, -116, 22, -92, 88, 12, -57, 126, -119, -7, -77, -27, -79, 27, -80, -24, -21, 108, -19,
91        -107, -16, 113, -1, -127, -8, -43, 31, 69, -109, 55, 93, -93, -57, 57, 3, -12, -111, 118,
92        55, 95, 46, -79, 84, -85, -67, -91, 88, -12, -45, 10,
93    ],
94    [
95        -124, 97, -4, 6, -84, -35, 82, -79, -43, -75, -53, -63, -6, -109, -62, -63, -15, -60, -100,
96        79, 50, -72, 48, -108, 13, -41, -121, 82, -16, 109, -24, -55, 46, -114, -103, 70, -1, -35,
97        55, -111, 9, -49, 7, -64, -80, 94, -95, -87, 54, 37, -8, -78, 19, 32, -28, -40, -119, 51,
98        35, -98, 34, 118, 72, -119, 7, 16, -74, -87, 84, -45, 118, 113, 47, 92, 77, 114, -12, -27,
99        57, -57, -57, 48, -54, 36, -108, 111, 103, 82, 66, -122, 46, -65, 34, -41, 5, -98, -61, 26,
100        -101, 36, 34, 46, -69, -7, 65, 52, -67, 44,
101    ],
102    [
103        37, -95, 92, -38, -33, 92, 3, 72, -64, -112, 79, -104, -83, -93, 3, 3, -92, -62, -29, 53,
104        80, -20, -87, -66, 72, 92, 13, 48, -97, -111, -27, -75, 0, -42, 50, 111, -92, 70, 67, 65,
105        -45, 54, -89, -74, -75, 39, 49, 109, -23, 41, -63, 99, 33, 117, -81, -117, -105, -83, -38,
106        -63, 15, -47, -39, 4, 1, 21, -55, 114, 69, 127, -92, 27, -109, -2, -48, 69, 49, -101, 62,
107        72, -37, -29, -126, -60, 45, 5, -77, -21, 100, 79, -96, -83, 18, 84, 79, -68, -33, -18, 61,
108        -19, 114, -86, 125, -109, -75, 123, 22, -12,
109    ],
110    [
111        83, -37, 62, 65, 35, -30, -86, 85, -40, -90, -102, 12, 43, -48, 10, -9, 108, -15, 13, -121,
112        10, 4, 102, -66, -91, -26, 107, -21, 35, 8, -41, -66, 64, -22, 60, -106, 80, 16, -39, -119,
113        -31, 44, 12, 120, -83, 125, -115, 6, -114, 110, -69, 46, 86, -40, 126, -20, 6, 71, -40,
114        -100, 68, 102, 40, -48, 51, 80, 52, -113, -112, 15, -98, -8, 123, -124, -28, -123, -54,
115        -115, 65, -21, -27, -78, 47, 104, -115, 75, 17, 119, -113, -4, 26, 65, 118, -70, 44, 37,
116        -119, 42, 98, -25, -117, -101, -28, -85, 68, -10, -15, 98,
117    ],
118    [
119        58, -120, -10, -19, -69, 52, 115, 73, -53, -63, -115, 81, 46, -126, -98, -126, 115, -110,
120        -95, -104, 15, -117, 28, 22, -98, 122, 40, -50, 32, -82, -111, -16, 35, -87, 47, -107, -65,
121        123, 62, 59, -90, 80, 118, 62, -61, 66, -51, 22, -55, -81, 80, -75, -7, -20, 66, 80, 49,
122        -76, -104, -81, -104, -29, 32, -50, -32, 38, 118, 72, 22, 104, 48, -83, -107, 64, 4, 123,
123        98, 81, -69, 69, -97, -55, -50, 36, -117, 16, -56, 106, 97, -61, 100, 79, -83, 121, -89,
124        62, 38, -1, 92, -50, 53, -108, 74, 42, 99, -33, -1, 73,
125    ],
126    [
127        27, 35, 52, 90, -34, 85, 27, 47, -94, 21, 55, -26, 85, -80, 41, -10, -2, 66, 88, -125, 57,
128        -10, 59, 86, 110, -19, 73, 11, -93, -88, -47, 75, 51, -20, -32, 21, -112, -87, -105, -61,
129        -12, 82, 19, -76, 77, -116, 14, -84, -104, 46, -5, 32, 6, 63, 38, 4, 33, -52, -21, 122,
130        120, 91, -99, -44, 104, -13, -75, 2, -125, 24, -106, 127, 59, 60, -69, -65, 50, 64, 32, 59,
131        77, 34, -47, -36, 53, 54, -78, 21, 96, 105, -122, 84, 126, -119, -101, -16, 106, 15, -15,
132        69, -107, 110, 120, -107, 11, 68, -86, -73,
133    ],
134    [
135        72, -48, 45, -120, 28, -62, 23, 33, 100, -92, -74, 29, -5, -32, -89, -44, -29, 116, -55,
136        -122, 54, -51, 3, 86, 33, -13, 21, 34, -102, 36, -104, -65, 24, 123, 93, 115, 81, 48, 111,
137        -100, -107, 45, 121, 47, 0, -22, -126, -87, -81, -74, 68, 56, 20, -66, 27, -13, 9, -52,
138        -83, 77, 43, 21, -48, 45, 27, -10, 52, 95, -86, -80, -27, -38, -111, 117, -103, -18, 80,
139        -26, -119, -51, 114, 27, 96, -104, -124, -66, -67, -23, 25, -101, 99, 46, -54, -85, 5, 80,
140        -30, 42, 78, 52, 57, 125, -96, -115, 48, -120, 71, 110,
141    ],
142    [
143        -59, -57, 50, -57, 35, -48, -116, -10, 83, -127, -101, 104, 29, -126, -69, -24, -89, -22,
144        -94, 92, 45, -20, 5, -66, -114, 14, -127, 70, -69, 123, 83, -59, -16, -15, 36, 90, -5, 99,
145        57, 9, -67, -11, 13, -84, -75, 7, 30, 34, -57, -1, -66, -30, -40, 40, 49, -126, -106, -17,
146        113, -18, 60, -35, -40, -41, 49, -94, 117, -103, -105, 86, -104, -48, -82, 86, -42, -111,
147        -98, 61, 59, 4, 62, -107, 21, -76, 124, 21, -46, -114, -100, 107, 74, 72, -105, -6, -10,
148        -30, 31, 44, 46, 106, 96, -118, 107, -93, -120, -31, 14, 61,
149    ],
150    [
151        -63, 105, 23, 107, -22, -109, 85, -94, -120, 103, 30, 100, -93, -46, -125, -45, -52, 28,
152        70, 126, 38, -102, -81, 3, 7, -115, -79, 90, 117, -64, 35, -10, -98, 63, -20, -78, -58,
153        -60, -31, 8, -57, 84, -85, -82, -50, 42, -4, 5, -1, -51, -38, 82, 127, 12, -33, -15, 121,
154        -70, 30, 4, 105, -76, -85, 102, 5, -110, 64, -29, -59, -82, 0, 111, -74, -69, -58, -28,
155        -33, -122, -106, -2, -22, 37, 75, 8, 86, -23, 121, 105, 90, 25, -43, 114, 12, -92, -101,
156        -105, -109, -1, -40, -1, -42, -16, 115, 101, -3, -24, -67, 36,
157    ],
158    [
159        58, 52, -6, 4, -124, -111, 16, -116, -58, 26, 27, -23, 115, -75, 20, -12, -50, 122, 102,
160        -67, -32, -120, -20, -41, 47, 111, 31, -47, -36, -111, -103, 12, 65, 25, -75, -63, 57,
161        -111, -88, -64, -77, -113, -55, -102, 105, 55, -90, 121, 56, -17, 58, -96, 60, -32, 2, 28,
162        106, -12, 105, -21, 96, 111, -50, 84, 73, -34, -70, 40, 116, -34, -63, -100, -11, 91, -123,
163        -126, 53, 55, 5, -120, 101, 98, 93, -107, 118, -104, -82, 21, 64, -17, 45, 106, 42, 15, 54,
164        86, 41, -112, -64, 26, 32, -62, 75, 23, 26, 86, 32, 94,
165    ],
166];
167static FC2_BIASES: [i32; 10] = [
168    -474, -1913, -2778, 10426, -7677, -8855, 9847, 1184, -5721, -10634,
169];
170static FC2: QDense<'static, 108, 10> = QDense {
171    weights: &FC2_WEIGHTS,
172    biases: Some(&FC2_BIASES),
173    input_zero_point: -128,
174    requantizer: Requantizer {
175        multiplier: 2028465121,
176        shift: 8,
177        zero_point: -128,
178        qmin: -128,
179        qmax: 127,
180    },
181};
182
183/// `f2_MatMul_quant` (Dense, reads `f1_Gemm_quant`): fully connected, 10 -> 3.
184static FC3_WEIGHTS: [[i8; 10]; 3] = [
185    [-19, 8, -11, 15, 85, 127, 23, -53, -33, -103],
186    [-13, 89, 70, -97, 98, -19, -31, 92, 46, -62],
187    [-120, -93, 106, -66, 63, -45, 97, 92, 54, -47],
188];
189static FC3: QDense<'static, 10, 3> = QDense {
190    weights: &FC3_WEIGHTS,
191    biases: None,
192    input_zero_point: -128,
193    requantizer: Requantizer {
194        multiplier: 1197765923,
195        shift: 7,
196        zero_point: -45,
197        qmin: -128,
198        qmax: 127,
199    },
200};
201
202/// `Sigmoid_quant` (Sigmoid, reads `f2_MatMul_quant`): sigmoid as a table from the input grid (scale 0.0053680753, zero point -45) to the output's (0.0028052556, -128).
203static SIGMOID4_LUT: [i8; LUT_SIZE] = [
204    11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 22,
205    22, 23, 23, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
206    34, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 44,
207    45, 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 55, 56,
208    56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63, 64, 64, 65, 65, 66, 66, 66, 67, 67,
209    68, 68, 69, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79,
210    79, 80, 80, 81, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, 87, 88, 88, 89, 89, 90,
211    90, 91, 91, 92, 92, 92, 93, 93, 94, 94, 95, 95, 96, 96, 97, 97, 97, 98, 98, 99, 99, 100, 100,
212    101, 101, 101, 102, 102, 103, 103, 104, 104, 104, 105, 105, 106, 106, 107, 107, 107, 108, 108,
213    109, 109, 110, 110, 110, 111, 111, 112, 112, 113, 113, 113, 114, 114, 115, 115, 115, 116, 116,
214    117, 117, 118, 118, 118, 119, 119, 120, 120, 120, 121, 121, 122, 122, 122, 123, 123, 124, 124,
215    124, 125, 125, 126, 126, 126, 127, 127,
216];
217
218/// Run the network on an int8 input in the `INPUT` grid.
219pub fn forward(input: &Input) -> Output {
220    let mut b0 = [[[0i8; 4]; 6]; 6];
221    let mut b1 = [[[0i8; 3]; 6]; 6];
222    CONV0.forward(input, &mut b0);
223    PW1.forward(&b0, &mut b1);
224    let v2 = FC2.forward(
225        b1.as_flattened()
226            .as_flattened()
227            .first_chunk::<108>()
228            .expect("108 values"),
229    );
230    let v3 = FC3.forward(&v2);
231    v3.map(|x| apply_lut(x, &SIGMOID4_LUT))
232}