Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-5x8c4-minmax-avxvnni-u4-acc4.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx8c4-avxvnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2023 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 #include <stdint.h>
12
13 #include <immintrin.h>
14
15 #include "xnnpack/common.h"
16 #include "xnnpack/gemm.h"
17 #include "xnnpack/intrinsics-polyfill.h"
18 #include "xnnpack/math.h"
19 #include "xnnpack/unaligned.h"
20 #include "xnnpack/prefetch.h"
21
22
23 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_5x8c4__avxvnni_u4_acc4(
24 size_t mr,
25 size_t nc,
26 size_t kc,
27 const int8_t* restrict a,
28 size_t a_stride,
29 const void* restrict w,
30 float* restrict c,
31 size_t cm_stride,
32 size_t cn_stride,
33 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
34 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
35 {
36 assert(mr != 0);
37 assert(mr <= 5);
38 assert(nc != 0);
39 assert(kc != 0);
40 assert(kc % sizeof(int8_t) == 0);
41 assert(a != NULL);
42 assert(w != NULL);
43 assert(c != NULL);
44
45 kc = round_up_po2(kc, 4 * sizeof(int8_t));
46 const int8_t* a0 = a;
47 float* c0 = c;
48 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
49 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
50 if XNN_UNPREDICTABLE(mr < 2) {
51 a1 = a0;
52 c1 = c0;
53 }
54 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
55 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
56 if XNN_UNPREDICTABLE(mr <= 2) {
57 a2 = a1;
58 c2 = c1;
59 }
60 const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
61 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
62 if XNN_UNPREDICTABLE(mr < 4) {
63 a3 = a2;
64 c3 = c2;
65 }
66 const int8_t* a4 = (const int8_t*) ((uintptr_t) a3 + a_stride);
67 float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
68 if XNN_UNPREDICTABLE(mr <= 4) {
69 a4 = a3;
70 c4 = c3;
71 }
72
73 const __m256i vsign_mask =_mm256_set1_epi8(0x80);
74 XNN_FORCE_REALIZATION(vsign_mask);
75 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
76 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
77 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
78 const __m256i vinput_zero_point3 = _mm256_set1_epi32((int) quantization_params[3].zero_point + 128);
79 const __m256i vinput_zero_point4 = _mm256_set1_epi32((int) quantization_params[4].zero_point + 128);
80 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
81 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
82 // XNN_FORCE_REALIZATION(voutput_min);
83 // XNN_FORCE_REALIZATION(voutput_max);
84 do {
85 const __m256i vksum01234567 = _mm256_load_si256(w);
86 __m256i vacc0x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
87 __m256i vacc0x1x01234567 = _mm256_setzero_si256();
88 __m256i vacc0x2x01234567 = _mm256_setzero_si256();
89 __m256i vacc0x3x01234567 = _mm256_setzero_si256();
90 __m256i vacc1x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
91 __m256i vacc1x1x01234567 = _mm256_setzero_si256();
92 __m256i vacc1x2x01234567 = _mm256_setzero_si256();
93 __m256i vacc1x3x01234567 = _mm256_setzero_si256();
94 __m256i vacc2x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
95 __m256i vacc2x1x01234567 = _mm256_setzero_si256();
96 __m256i vacc2x2x01234567 = _mm256_setzero_si256();
97 __m256i vacc2x3x01234567 = _mm256_setzero_si256();
98 __m256i vacc3x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point3);
99 __m256i vacc3x1x01234567 = _mm256_setzero_si256();
100 __m256i vacc3x2x01234567 = _mm256_setzero_si256();
101 __m256i vacc3x3x01234567 = _mm256_setzero_si256();
102 __m256i vacc4x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point4);
103 __m256i vacc4x1x01234567 = _mm256_setzero_si256();
104 __m256i vacc4x2x01234567 = _mm256_setzero_si256();
105 __m256i vacc4x3x01234567 = _mm256_setzero_si256();
106 w = (const int32_t*) w + 8;
107
108 size_t k = kc;
109 while (k >= 16 * sizeof(int8_t)) {
110 __m256i va0x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 0));
111 __m256i va0x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 4));
112 __m256i va0x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 8));
113 __m256i va0x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 12));
114 a0 += 16;
115 __m256i va1x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 0));
116 __m256i va1x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 4));
117 __m256i va1x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 8));
118 __m256i va1x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 12));
119 a1 += 16;
120 __m256i va2x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 0));
121 __m256i va2x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 4));
122 __m256i va2x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 8));
123 __m256i va2x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 12));
124 a2 += 16;
125 __m256i va3x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 0));
126 __m256i va3x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 4));
127 __m256i va3x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 8));
128 __m256i va3x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 12));
129 a3 += 16;
130 __m256i va4x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a4 + 0));
131 __m256i va4x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a4 + 4));
132 __m256i va4x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a4 + 8));
133 __m256i va4x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a4 + 12));
134 a4 += 16;
135
136 va0x0x0123 = _mm256_xor_si256(va0x0x0123, vsign_mask);
137 va0x1x0123 = _mm256_xor_si256(va0x1x0123, vsign_mask);
138 va0x2x0123 = _mm256_xor_si256(va0x2x0123, vsign_mask);
139 va0x3x0123 = _mm256_xor_si256(va0x3x0123, vsign_mask);
140 va1x0x0123 = _mm256_xor_si256(va1x0x0123, vsign_mask);
141 va1x1x0123 = _mm256_xor_si256(va1x1x0123, vsign_mask);
142 va1x2x0123 = _mm256_xor_si256(va1x2x0123, vsign_mask);
143 va1x3x0123 = _mm256_xor_si256(va1x3x0123, vsign_mask);
144 va2x0x0123 = _mm256_xor_si256(va2x0x0123, vsign_mask);
145 va2x1x0123 = _mm256_xor_si256(va2x1x0123, vsign_mask);
146 va2x2x0123 = _mm256_xor_si256(va2x2x0123, vsign_mask);
147 va2x3x0123 = _mm256_xor_si256(va2x3x0123, vsign_mask);
148 va3x0x0123 = _mm256_xor_si256(va3x0x0123, vsign_mask);
149 va3x1x0123 = _mm256_xor_si256(va3x1x0123, vsign_mask);
150 va3x2x0123 = _mm256_xor_si256(va3x2x0123, vsign_mask);
151 va3x3x0123 = _mm256_xor_si256(va3x3x0123, vsign_mask);
152 va4x0x0123 = _mm256_xor_si256(va4x0x0123, vsign_mask);
153 va4x1x0123 = _mm256_xor_si256(va4x1x0123, vsign_mask);
154 va4x2x0123 = _mm256_xor_si256(va4x2x0123, vsign_mask);
155 va4x3x0123 = _mm256_xor_si256(va4x3x0123, vsign_mask);
156
157 const __m256i vb0x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 0));
158 const __m256i vb1x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
159 const __m256i vb2x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
160 const __m256i vb3x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
161 w = (const int8_t*) w + 128;
162
163 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0x0123, vb0x01234567);
164 vacc0x1x01234567 = _mm256_dpbusd_avx_epi32(vacc0x1x01234567, va0x1x0123, vb1x01234567);
165 vacc0x2x01234567 = _mm256_dpbusd_avx_epi32(vacc0x2x01234567, va0x2x0123, vb2x01234567);
166 vacc0x3x01234567 = _mm256_dpbusd_avx_epi32(vacc0x3x01234567, va0x3x0123, vb3x01234567);
167 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0x0123, vb0x01234567);
168 vacc1x1x01234567 = _mm256_dpbusd_avx_epi32(vacc1x1x01234567, va1x1x0123, vb1x01234567);
169 vacc1x2x01234567 = _mm256_dpbusd_avx_epi32(vacc1x2x01234567, va1x2x0123, vb2x01234567);
170 vacc1x3x01234567 = _mm256_dpbusd_avx_epi32(vacc1x3x01234567, va1x3x0123, vb3x01234567);
171 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0x0123, vb0x01234567);
172 vacc2x1x01234567 = _mm256_dpbusd_avx_epi32(vacc2x1x01234567, va2x1x0123, vb1x01234567);
173 vacc2x2x01234567 = _mm256_dpbusd_avx_epi32(vacc2x2x01234567, va2x2x0123, vb2x01234567);
174 vacc2x3x01234567 = _mm256_dpbusd_avx_epi32(vacc2x3x01234567, va2x3x0123, vb3x01234567);
175 vacc3x0x01234567 = _mm256_dpbusd_avx_epi32(vacc3x0x01234567, va3x0x0123, vb0x01234567);
176 vacc3x1x01234567 = _mm256_dpbusd_avx_epi32(vacc3x1x01234567, va3x1x0123, vb1x01234567);
177 vacc3x2x01234567 = _mm256_dpbusd_avx_epi32(vacc3x2x01234567, va3x2x0123, vb2x01234567);
178 vacc3x3x01234567 = _mm256_dpbusd_avx_epi32(vacc3x3x01234567, va3x3x0123, vb3x01234567);
179 vacc4x0x01234567 = _mm256_dpbusd_avx_epi32(vacc4x0x01234567, va4x0x0123, vb0x01234567);
180 vacc4x1x01234567 = _mm256_dpbusd_avx_epi32(vacc4x1x01234567, va4x1x0123, vb1x01234567);
181 vacc4x2x01234567 = _mm256_dpbusd_avx_epi32(vacc4x2x01234567, va4x2x0123, vb2x01234567);
182 vacc4x3x01234567 = _mm256_dpbusd_avx_epi32(vacc4x3x01234567, va4x3x0123, vb3x01234567);
183
184 k -= 16 * sizeof(int8_t);
185 }
186 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x1x01234567);
187 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x1x01234567);
188 vacc2x0x01234567 = _mm256_add_epi32(vacc2x0x01234567, vacc2x1x01234567);
189 vacc3x0x01234567 = _mm256_add_epi32(vacc3x0x01234567, vacc3x1x01234567);
190 vacc4x0x01234567 = _mm256_add_epi32(vacc4x0x01234567, vacc4x1x01234567);
191 vacc0x2x01234567 = _mm256_add_epi32(vacc0x2x01234567, vacc0x3x01234567);
192 vacc1x2x01234567 = _mm256_add_epi32(vacc1x2x01234567, vacc1x3x01234567);
193 vacc2x2x01234567 = _mm256_add_epi32(vacc2x2x01234567, vacc2x3x01234567);
194 vacc3x2x01234567 = _mm256_add_epi32(vacc3x2x01234567, vacc3x3x01234567);
195 vacc4x2x01234567 = _mm256_add_epi32(vacc4x2x01234567, vacc4x3x01234567);
196 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x2x01234567);
197 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x2x01234567);
198 vacc2x0x01234567 = _mm256_add_epi32(vacc2x0x01234567, vacc2x2x01234567);
199 vacc3x0x01234567 = _mm256_add_epi32(vacc3x0x01234567, vacc3x2x01234567);
200 vacc4x0x01234567 = _mm256_add_epi32(vacc4x0x01234567, vacc4x2x01234567);
201 while (k != 0) {
202 __m256i va0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0));
203 a0 += 4;
204 __m256i va1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1));
205 a1 += 4;
206 __m256i va2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2));
207 a2 += 4;
208 __m256i va3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3));
209 a3 += 4;
210 __m256i va4x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a4));
211 a4 += 4;
212
213 va0x0123 = _mm256_xor_si256(va0x0123, vsign_mask);
214 va1x0123 = _mm256_xor_si256(va1x0123, vsign_mask);
215 va2x0123 = _mm256_xor_si256(va2x0123, vsign_mask);
216 va3x0123 = _mm256_xor_si256(va3x0123, vsign_mask);
217 va4x0123 = _mm256_xor_si256(va4x0123, vsign_mask);
218
219 const __m256i vb01234567 = _mm256_load_si256(w);
220
221 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0123, vb01234567);
222 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0123, vb01234567);
223 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0123, vb01234567);
224 vacc3x0x01234567 = _mm256_dpbusd_avx_epi32(vacc3x0x01234567, va3x0123, vb01234567);
225 vacc4x0x01234567 = _mm256_dpbusd_avx_epi32(vacc4x0x01234567, va4x0123, vb01234567);
226
227 w = (const int8_t*) w + 32;
228 k -= 4 * sizeof(int8_t);
229 }
230
231 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x0x01234567);
232 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x0x01234567);
233 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x0x01234567);
234 __m256 vout3x01234567 = _mm256_cvtepi32_ps(vacc3x0x01234567);
235 __m256 vout4x01234567 = _mm256_cvtepi32_ps(vacc4x0x01234567);
236
237 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
238 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
239 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
240 vout3x01234567 = _mm256_mul_ps(vout3x01234567, _mm256_set1_ps(quantization_params[3].inv_scale));
241 vout4x01234567 = _mm256_mul_ps(vout4x01234567, _mm256_set1_ps(quantization_params[4].inv_scale));
242
243 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
244 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
245 w = (const float*) w + 16;
246
247 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
248 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
249 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
250 vout3x01234567 = _mm256_fmadd_ps(vout3x01234567, vfilter_output_scale01234567, vbias01234567);
251 vout4x01234567 = _mm256_fmadd_ps(vout4x01234567, vfilter_output_scale01234567, vbias01234567);
252
253 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
254 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
255 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
256 vout3x01234567 = _mm256_max_ps(vout3x01234567, voutput_min);
257 vout4x01234567 = _mm256_max_ps(vout4x01234567, voutput_min);
258
259 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
260 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
261 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
262 vout3x01234567 = _mm256_min_ps(vout3x01234567, voutput_max);
263 vout4x01234567 = _mm256_min_ps(vout4x01234567, voutput_max);
264
265 if XNN_LIKELY(nc >= 8) {
266 _mm256_storeu_ps(c0, vout0x01234567);
267 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
268 c0 = (float*) ((uintptr_t) c0 + cn_stride);
269 _mm256_storeu_ps(c1, vout1x01234567);
270 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
271 c1 = (float*) ((uintptr_t) c1 + cn_stride);
272 _mm256_storeu_ps(c2, vout2x01234567);
273 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
274 c2 = (float*) ((uintptr_t) c2 + cn_stride);
275 _mm256_storeu_ps(c3, vout3x01234567);
276 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
277 c3 = (float*) ((uintptr_t) c3 + cn_stride);
278 _mm256_storeu_ps(c4, vout4x01234567);
279 a4 = (const int8_t*) ((uintptr_t) a4 - kc);
280 c4 = (float*) ((uintptr_t) c4 + cn_stride);
281 nc -= 8;
282 } else {
283 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
284 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
285 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
286 __m128 vout3x0123 = _mm256_castps256_ps128(vout3x01234567);
287 __m128 vout4x0123 = _mm256_castps256_ps128(vout4x01234567);
288 if (nc & 4) {
289 _mm_storeu_ps(c0, vout0x0123);
290 c0 += 4;
291 _mm_storeu_ps(c1, vout1x0123);
292 c1 += 4;
293 _mm_storeu_ps(c2, vout2x0123);
294 c2 += 4;
295 _mm_storeu_ps(c3, vout3x0123);
296 c3 += 4;
297 _mm_storeu_ps(c4, vout4x0123);
298 c4 += 4;
299 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
300 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
301 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
302 vout3x0123 = _mm256_extractf128_ps(vout3x01234567, 1);
303 vout4x0123 = _mm256_extractf128_ps(vout4x01234567, 1);
304 }
305 if (nc & 2) {
306 _mm_storel_pi((__m64*) c0, vout0x0123);
307 c0 += 2;
308 _mm_storel_pi((__m64*) c1, vout1x0123);
309 c1 += 2;
310 _mm_storel_pi((__m64*) c2, vout2x0123);
311 c2 += 2;
312 _mm_storel_pi((__m64*) c3, vout3x0123);
313 c3 += 2;
314 _mm_storel_pi((__m64*) c4, vout4x0123);
315 c4 += 2;
316 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
317 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
318 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
319 vout3x0123 = _mm_movehl_ps(vout3x0123, vout3x0123);
320 vout4x0123 = _mm_movehl_ps(vout4x0123, vout4x0123);
321 }
322 if (nc & 1) {
323 _mm_store_ss(c0, vout0x0123);
324 _mm_store_ss(c1, vout1x0123);
325 _mm_store_ss(c2, vout2x0123);
326 _mm_store_ss(c3, vout3x0123);
327 _mm_store_ss(c4, vout4x0123);
328 }
329 nc = 0;
330 }
331 } while (nc != 0);
332 }