Debian Codegraph @

src/qd8-f32-qb4w-gemm/gen/qd8-f32-qb4w-gemm-2x4-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_2x4__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 <= 2);
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 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
41 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
42 if XNN_UNPREDICTABLE(mr != 2) {
43 a1 = a0;
44 c1 = c0;
45 }
46
47
48 kc = round_up_po2(kc, 2);
49 do {
50 const float vksum0 = ((const float*) w)[0];
51 const float vksum1 = ((const float*) w)[1];
52 const float vksum2 = ((const float*) w)[2];
53 const float vksum3 = ((const float*) w)[3];
54 const float vinput_zero_point0 = (const float) quantization_params[0].zero_point;
55 float vout0x0 = vksum0 * vinput_zero_point0;
56 float vout0x1 = vksum1 * vinput_zero_point0;
57 float vout0x2 = vksum2 * vinput_zero_point0;
58 float vout0x3 = vksum3 * vinput_zero_point0;
59 const float vinput_zero_point1 = (const float) quantization_params[1].zero_point;
60 float vout1x0 = vksum0 * vinput_zero_point1;
61 float vout1x1 = vksum1 * vinput_zero_point1;
62 float vout1x2 = vksum2 * vinput_zero_point1;
63 float vout1x3 = vksum3 * vinput_zero_point1;
64 w = (const float*) w + 4;
65
66 for (size_t kb=0; kb < kc; kb += bl) {
67 int32_t vacc0x0 = 0;
68 int32_t vacc0x1 = 0;
69 int32_t vacc0x2 = 0;
70 int32_t vacc0x3 = 0;
71 int32_t vacc1x0 = 0;
72 int32_t vacc1x1 = 0;
73 int32_t vacc1x2 = 0;
74 int32_t vacc1x3 = 0;
75 size_t k = bl;
76 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
77 const int32_t va0c0 = (int32_t) a0[0];
78 const int32_t va0c1 = (int32_t) a0[1];
79 a0 += 2;
80 const int32_t va1c0 = (int32_t) a1[0];
81 const int32_t va1c1 = (int32_t) a1[1];
82 a1 += 2;
83
84 const uint8_t vbi0 = ((const uint8_t*) w)[0];
85 const uint8_t vbi1 = ((const uint8_t*) w)[1];
86 const uint8_t vbi2 = ((const uint8_t*) w)[2];
87 const uint8_t vbi3 = ((const uint8_t*) w)[3];
88 w = (const uint8_t*) w + 4;
89 const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4);
90 const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0);
91 const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4);
92 const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0);
93 const int32_t vb2c0 = (int32_t) (int8_t) (vbi2 << 4);
94 const int32_t vb2c1 = (int32_t) (int8_t) (vbi2 & 0xF0);
95 const int32_t vb3c0 = (int32_t) (int8_t) (vbi3 << 4);
96 const int32_t vb3c1 = (int32_t) (int8_t) (vbi3 & 0xF0);
97
98 vacc0x0 += va0c0 * vb0c0;
99 vacc0x1 += va0c0 * vb1c0;
100 vacc0x2 += va0c0 * vb2c0;
101 vacc0x3 += va0c0 * vb3c0;
102 vacc1x0 += va1c0 * vb0c0;
103 vacc1x1 += va1c0 * vb1c0;
104 vacc1x2 += va1c0 * vb2c0;
105 vacc1x3 += va1c0 * vb3c0;
106 vacc0x0 += va0c1 * vb0c1;
107 vacc0x1 += va0c1 * vb1c1;
108 vacc0x2 += va0c1 * vb2c1;
109 vacc0x3 += va0c1 * vb3c1;
110 vacc1x0 += va1c1 * vb0c1;
111 vacc1x1 += va1c1 * vb1c1;
112 vacc1x2 += va1c1 * vb2c1;
113 vacc1x3 += va1c1 * vb3c1;
114 }
115 // accumulate in float
116 float vf0x0 = vacc0x0;
117 const float vfilter_output_scale0 = math_cvt_fp32_bf16(((const uint16_t*) w)[0]);
118 float vf0x1 = vacc0x1;
119 const float vfilter_output_scale1 = math_cvt_fp32_bf16(((const uint16_t*) w)[1]);
120 float vf0x2 = vacc0x2;
121 const float vfilter_output_scale2 = math_cvt_fp32_bf16(((const uint16_t*) w)[2]);
122 float vf0x3 = vacc0x3;
123 const float vfilter_output_scale3 = math_cvt_fp32_bf16(((const uint16_t*) w)[3]);
124 float vf1x0 = vacc1x0;
125 float vf1x1 = vacc1x1;
126 float vf1x2 = vacc1x2;
127 float vf1x3 = vacc1x3;
128
129 vf0x0 *= vfilter_output_scale0;
130 vout0x0 += vf0x0;
131 vf0x1 *= vfilter_output_scale1;
132 vout0x1 += vf0x1;
133 vf0x2 *= vfilter_output_scale2;
134 vout0x2 += vf0x2;
135 vf0x3 *= vfilter_output_scale3;
136 vout0x3 += vf0x3;
137 vf1x0 *= vfilter_output_scale0;
138 vout1x0 += vf1x0;
139 vf1x1 *= vfilter_output_scale1;
140 vout1x1 += vf1x1;
141 vf1x2 *= vfilter_output_scale2;
142 vout1x2 += vf1x2;
143 vf1x3 *= vfilter_output_scale3;
144 vout1x3 += vf1x3;
145 w = (const uint16_t*) w + 4;
146 }
147
148
149 const float vinput_scale0 = quantization_params[0].inv_scale;
150 vout0x0 *= vinput_scale0;
151 vout0x1 *= vinput_scale0;
152 vout0x2 *= vinput_scale0;
153 vout0x3 *= vinput_scale0;
154 const float vinput_scale1 = quantization_params[1].inv_scale;
155 vout1x0 *= vinput_scale1;
156 vout1x1 *= vinput_scale1;
157 vout1x2 *= vinput_scale1;
158 vout1x3 *= vinput_scale1;
159
160
161 const float vbias0 = ((const float*) w)[0];
162 vout0x0 += vbias0;
163 vout1x0 += vbias0;
164 const float vbias1 = ((const float*) w)[1];
165 vout0x1 += vbias1;
166 vout1x1 += vbias1;
167 const float vbias2 = ((const float*) w)[2];
168 vout0x2 += vbias2;
169 vout1x2 += vbias2;
170 const float vbias3 = ((const float*) w)[3];
171 vout0x3 += vbias3;
172 vout1x3 += vbias3;
173
174 w = (const float*) w + 4;
175
176 const float voutput_min = params->scalar.min;
177 vout0x0 = math_max_f32(vout0x0, voutput_min);
178 vout1x0 = math_max_f32(vout1x0, voutput_min);
179 vout0x1 = math_max_f32(vout0x1, voutput_min);
180 vout1x1 = math_max_f32(vout1x1, voutput_min);
181 vout0x2 = math_max_f32(vout0x2, voutput_min);
182 vout1x2 = math_max_f32(vout1x2, voutput_min);
183 vout0x3 = math_max_f32(vout0x3, voutput_min);
184 vout1x3 = math_max_f32(vout1x3, voutput_min);
185
186 const float voutput_max = params->scalar.max;
187 vout0x0 = math_min_f32(vout0x0, voutput_max);
188 vout1x0 = math_min_f32(vout1x0, voutput_max);
189 vout0x1 = math_min_f32(vout0x1, voutput_max);
190 vout1x1 = math_min_f32(vout1x1, voutput_max);
191 vout0x2 = math_min_f32(vout0x2, voutput_max);
192 vout1x2 = math_min_f32(vout1x2, voutput_max);
193 vout0x3 = math_min_f32(vout0x3, voutput_max);
194 vout1x3 = math_min_f32(vout1x3, voutput_max);
195
196 if XNN_LIKELY(nc >= 4) {
197 c0[0] = vout0x0;
198 c0[1] = vout0x1;
199 c0[2] = vout0x2;
200 c0[3] = vout0x3;
201 c1[0] = vout1x0;
202 c1[1] = vout1x1;
203 c1[2] = vout1x2;
204 c1[3] = vout1x3;
205
206 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
207 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
208
209 c0 = (float*) ((uintptr_t) c0 + cn_stride);
210 c1 = (float*) ((uintptr_t) c1 + cn_stride);
211
212 nc -= 4;
213 } else {
214 if (nc & 2) {
215 c0[0] = vout0x0;
216 c0[1] = vout0x1;
217 vout0x0 = vout0x2;
218 c0 += 2;
219 c1[0] = vout1x0;
220 c1[1] = vout1x1;
221 vout1x0 = vout1x2;
222 c1 += 2;
223 }
224 if (nc & 1) {
225 c0[0] = vout0x0;
226 c1[0] = vout1x0;
227 }
228
229 nc = 0;
230 }
231 } while (nc != 0);
232 }