Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-1x8-minmax-scalar.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/scalar.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11
12 #include "xnnpack/gemm.h"
13 #include "xnnpack/math.h"
14
15
16 void xnn_qd8_f32_qb4w_gemm_minmax_ukernel_1x8__scalar(
17 size_t mr,
18 size_t nc,
19 size_t kc,
20 const int8_t* restrict a,
21 size_t a_stride,
22 const void* restrict w,
23 float* restrict c,
24 size_t cm_stride,
25 size_t cn_stride,
26 const struct xnn_f32_qb4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
27 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29 assert(mr != 0);
30 assert(mr <= 1);
31 assert(nc != 0);
32 assert(kc != 0);
33 size_t bl = params->scalar.blocksize;
34 assert(bl <= round_up_po2(kc, 2));
35 assert(bl != 0);
36 assert(bl % 32 == 0);
37
38 const int8_t* a0 = a;
39 float* c0 = c;
40
41
42 kc = round_up_po2(kc, 2);
43 do {
44 const float vksum0 = ((const float*) w)[0];
45 const float vksum1 = ((const float*) w)[1];
46 const float vksum2 = ((const float*) w)[2];
47 const float vksum3 = ((const float*) w)[3];
48 const float vksum4 = ((const float*) w)[4];
49 const float vksum5 = ((const float*) w)[5];
50 const float vksum6 = ((const float*) w)[6];
51 const float vksum7 = ((const float*) w)[7];
52 const float vinput_zero_point0 = (const float) quantization_params[0].zero_point;
53 float vout0x0 = vksum0 * vinput_zero_point0;
54 float vout0x1 = vksum1 * vinput_zero_point0;
55 float vout0x2 = vksum2 * vinput_zero_point0;
56 float vout0x3 = vksum3 * vinput_zero_point0;
57 float vout0x4 = vksum4 * vinput_zero_point0;
58 float vout0x5 = vksum5 * vinput_zero_point0;
59 float vout0x6 = vksum6 * vinput_zero_point0;
60 float vout0x7 = vksum7 * vinput_zero_point0;
61 w = (const float*) w + 8;
62
63 for (size_t kb=0; kb < kc; kb += bl) {
64 int32_t vacc0x0 = 0;
65 int32_t vacc0x1 = 0;
66 int32_t vacc0x2 = 0;
67 int32_t vacc0x3 = 0;
68 int32_t vacc0x4 = 0;
69 int32_t vacc0x5 = 0;
70 int32_t vacc0x6 = 0;
71 int32_t vacc0x7 = 0;
72 size_t k = bl;
73 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
74 const int32_t va0c0 = (int32_t) a0[0];
75 const int32_t va0c1 = (int32_t) a0[1];
76 a0 += 2;
77
78 const uint8_t vbi0 = ((const uint8_t*) w)[0];
79 const uint8_t vbi1 = ((const uint8_t*) w)[1];
80 const uint8_t vbi2 = ((const uint8_t*) w)[2];
81 const uint8_t vbi3 = ((const uint8_t*) w)[3];
82 const uint8_t vbi4 = ((const uint8_t*) w)[4];
83 const uint8_t vbi5 = ((const uint8_t*) w)[5];
84 const uint8_t vbi6 = ((const uint8_t*) w)[6];
85 const uint8_t vbi7 = ((const uint8_t*) w)[7];
86 w = (const uint8_t*) w + 8;
87 const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4);
88 const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0);
89 const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4);
90 const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0);
91 const int32_t vb2c0 = (int32_t) (int8_t) (vbi2 << 4);
92 const int32_t vb2c1 = (int32_t) (int8_t) (vbi2 & 0xF0);
93 const int32_t vb3c0 = (int32_t) (int8_t) (vbi3 << 4);
94 const int32_t vb3c1 = (int32_t) (int8_t) (vbi3 & 0xF0);
95 const int32_t vb4c0 = (int32_t) (int8_t) (vbi4 << 4);
96 const int32_t vb4c1 = (int32_t) (int8_t) (vbi4 & 0xF0);
97 const int32_t vb5c0 = (int32_t) (int8_t) (vbi5 << 4);
98 const int32_t vb5c1 = (int32_t) (int8_t) (vbi5 & 0xF0);
99 const int32_t vb6c0 = (int32_t) (int8_t) (vbi6 << 4);
100 const int32_t vb6c1 = (int32_t) (int8_t) (vbi6 & 0xF0);
101 const int32_t vb7c0 = (int32_t) (int8_t) (vbi7 << 4);
102 const int32_t vb7c1 = (int32_t) (int8_t) (vbi7 & 0xF0);
103
104 vacc0x0 += va0c0 * vb0c0;
105 vacc0x1 += va0c0 * vb1c0;
106 vacc0x2 += va0c0 * vb2c0;
107 vacc0x3 += va0c0 * vb3c0;
108 vacc0x4 += va0c0 * vb4c0;
109 vacc0x5 += va0c0 * vb5c0;
110 vacc0x6 += va0c0 * vb6c0;
111 vacc0x7 += va0c0 * vb7c0;
112 vacc0x0 += va0c1 * vb0c1;
113 vacc0x1 += va0c1 * vb1c1;
114 vacc0x2 += va0c1 * vb2c1;
115 vacc0x3 += va0c1 * vb3c1;
116 vacc0x4 += va0c1 * vb4c1;
117 vacc0x5 += va0c1 * vb5c1;
118 vacc0x6 += va0c1 * vb6c1;
119 vacc0x7 += va0c1 * vb7c1;
120 }
121 // accumulate in float
122 float vf0x0 = vacc0x0;
123 const float vfilter_output_scale0 = math_cvt_fp32_bf16(((const uint16_t*) w)[0]);
124 float vf0x1 = vacc0x1;
125 const float vfilter_output_scale1 = math_cvt_fp32_bf16(((const uint16_t*) w)[1]);
126 float vf0x2 = vacc0x2;
127 const float vfilter_output_scale2 = math_cvt_fp32_bf16(((const uint16_t*) w)[2]);
128 float vf0x3 = vacc0x3;
129 const float vfilter_output_scale3 = math_cvt_fp32_bf16(((const uint16_t*) w)[3]);
130 float vf0x4 = vacc0x4;
131 const float vfilter_output_scale4 = math_cvt_fp32_bf16(((const uint16_t*) w)[4]);
132 float vf0x5 = vacc0x5;
133 const float vfilter_output_scale5 = math_cvt_fp32_bf16(((const uint16_t*) w)[5]);
134 float vf0x6 = vacc0x6;
135 const float vfilter_output_scale6 = math_cvt_fp32_bf16(((const uint16_t*) w)[6]);
136 float vf0x7 = vacc0x7;
137 const float vfilter_output_scale7 = math_cvt_fp32_bf16(((const uint16_t*) w)[7]);
138
139 vf0x0 *= vfilter_output_scale0;
140 vout0x0 += vf0x0;
141 vf0x1 *= vfilter_output_scale1;
142 vout0x1 += vf0x1;
143 vf0x2 *= vfilter_output_scale2;
144 vout0x2 += vf0x2;
145 vf0x3 *= vfilter_output_scale3;
146 vout0x3 += vf0x3;
147 vf0x4 *= vfilter_output_scale4;
148 vout0x4 += vf0x4;
149 vf0x5 *= vfilter_output_scale5;
150 vout0x5 += vf0x5;
151 vf0x6 *= vfilter_output_scale6;
152 vout0x6 += vf0x6;
153 vf0x7 *= vfilter_output_scale7;
154 vout0x7 += vf0x7;
155 w = (const uint16_t*) w + 8;
156 }
157
158
159 const float vinput_scale0 = quantization_params[0].inv_scale;
160 vout0x0 *= vinput_scale0;
161 vout0x1 *= vinput_scale0;
162 vout0x2 *= vinput_scale0;
163 vout0x3 *= vinput_scale0;
164 vout0x4 *= vinput_scale0;
165 vout0x5 *= vinput_scale0;
166 vout0x6 *= vinput_scale0;
167 vout0x7 *= vinput_scale0;
168
169
170 const float vbias0 = ((const float*) w)[0];
171 vout0x0 += vbias0;
172 const float vbias1 = ((const float*) w)[1];
173 vout0x1 += vbias1;
174 const float vbias2 = ((const float*) w)[2];
175 vout0x2 += vbias2;
176 const float vbias3 = ((const float*) w)[3];
177 vout0x3 += vbias3;
178 const float vbias4 = ((const float*) w)[4];
179 vout0x4 += vbias4;
180 const float vbias5 = ((const float*) w)[5];
181 vout0x5 += vbias5;
182 const float vbias6 = ((const float*) w)[6];
183 vout0x6 += vbias6;
184 const float vbias7 = ((const float*) w)[7];
185 vout0x7 += vbias7;
186
187 w = (const float*) w + 8;
188
189 const float voutput_min = params->scalar.min;
190 vout0x0 = math_max_f32(vout0x0, voutput_min);
191 vout0x1 = math_max_f32(vout0x1, voutput_min);
192 vout0x2 = math_max_f32(vout0x2, voutput_min);
193 vout0x3 = math_max_f32(vout0x3, voutput_min);
194 vout0x4 = math_max_f32(vout0x4, voutput_min);
195 vout0x5 = math_max_f32(vout0x5, voutput_min);
196 vout0x6 = math_max_f32(vout0x6, voutput_min);
197 vout0x7 = math_max_f32(vout0x7, voutput_min);
198
199 const float voutput_max = params->scalar.max;
200 vout0x0 = math_min_f32(vout0x0, voutput_max);
201 vout0x1 = math_min_f32(vout0x1, voutput_max);
202 vout0x2 = math_min_f32(vout0x2, voutput_max);
203 vout0x3 = math_min_f32(vout0x3, voutput_max);
204 vout0x4 = math_min_f32(vout0x4, voutput_max);
205 vout0x5 = math_min_f32(vout0x5, voutput_max);
206 vout0x6 = math_min_f32(vout0x6, voutput_max);
207 vout0x7 = math_min_f32(vout0x7, voutput_max);
208
209 if XNN_LIKELY(nc >= 8) {
210 c0[0] = vout0x0;
211 c0[1] = vout0x1;
212 c0[2] = vout0x2;
213 c0[3] = vout0x3;
214 c0[4] = vout0x4;
215 c0[5] = vout0x5;
216 c0[6] = vout0x6;
217 c0[7] = vout0x7;
218
219 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
220
221 c0 = (float*) ((uintptr_t) c0 + cn_stride);
222
223 nc -= 8;
224 } else {
225 if (nc & 4) {
226 c0[0] = vout0x0;
227 c0[1] = vout0x1;
228 c0[2] = vout0x2;
229 c0[3] = vout0x3;
230 vout0x0 = vout0x4;
231 vout0x1 = vout0x5;
232 vout0x2 = vout0x6;
233 c0 += 4;
234 }
235 if (nc & 2) {
236 c0[0] = vout0x0;
237 c0[1] = vout0x1;
238 vout0x0 = vout0x2;
239 vout0x1 = vout0x3;
240 vout0x2 = vout0x4;
241 vout0x3 = vout0x5;
242 vout0x4 = vout0x6;
243 c0 += 2;
244 }
245 if (nc & 1) {
246 c0[0] = vout0x0;
247 }
248
249 nc = 0;
250 }
251 } while (nc != 0);
252 }