Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-4x8c4-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_4x8c4__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 <= 4);
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
67 const __m256i vsign_mask =_mm256_set1_epi8(0x80);
68 XNN_FORCE_REALIZATION(vsign_mask);
69 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
70 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
71 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
72 const __m256i vinput_zero_point3 = _mm256_set1_epi32((int) quantization_params[3].zero_point + 128);
73 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
74 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
75 // XNN_FORCE_REALIZATION(voutput_min);
76 // XNN_FORCE_REALIZATION(voutput_max);
77 do {
78 const __m256i vksum01234567 = _mm256_load_si256(w);
79 __m256i vacc0x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
80 __m256i vacc0x1x01234567 = _mm256_setzero_si256();
81 __m256i vacc0x2x01234567 = _mm256_setzero_si256();
82 __m256i vacc0x3x01234567 = _mm256_setzero_si256();
83 __m256i vacc1x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
84 __m256i vacc1x1x01234567 = _mm256_setzero_si256();
85 __m256i vacc1x2x01234567 = _mm256_setzero_si256();
86 __m256i vacc1x3x01234567 = _mm256_setzero_si256();
87 __m256i vacc2x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
88 __m256i vacc2x1x01234567 = _mm256_setzero_si256();
89 __m256i vacc2x2x01234567 = _mm256_setzero_si256();
90 __m256i vacc2x3x01234567 = _mm256_setzero_si256();
91 __m256i vacc3x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point3);
92 __m256i vacc3x1x01234567 = _mm256_setzero_si256();
93 __m256i vacc3x2x01234567 = _mm256_setzero_si256();
94 __m256i vacc3x3x01234567 = _mm256_setzero_si256();
95 w = (const int32_t*) w + 8;
96
97 size_t k = kc;
98 while (k >= 16 * sizeof(int8_t)) {
99 __m256i va0x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 0));
100 __m256i va0x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 4));
101 __m256i va0x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 8));
102 __m256i va0x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 12));
103 a0 += 16;
104 __m256i va1x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 0));
105 __m256i va1x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 4));
106 __m256i va1x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 8));
107 __m256i va1x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 12));
108 a1 += 16;
109 __m256i va2x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 0));
110 __m256i va2x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 4));
111 __m256i va2x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 8));
112 __m256i va2x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 12));
113 a2 += 16;
114 __m256i va3x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 0));
115 __m256i va3x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 4));
116 __m256i va3x2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 8));
117 __m256i va3x3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 12));
118 a3 += 16;
119
120 va0x0x0123 = _mm256_xor_si256(va0x0x0123, vsign_mask);
121 va0x1x0123 = _mm256_xor_si256(va0x1x0123, vsign_mask);
122 va0x2x0123 = _mm256_xor_si256(va0x2x0123, vsign_mask);
123 va0x3x0123 = _mm256_xor_si256(va0x3x0123, vsign_mask);
124 va1x0x0123 = _mm256_xor_si256(va1x0x0123, vsign_mask);
125 va1x1x0123 = _mm256_xor_si256(va1x1x0123, vsign_mask);
126 va1x2x0123 = _mm256_xor_si256(va1x2x0123, vsign_mask);
127 va1x3x0123 = _mm256_xor_si256(va1x3x0123, vsign_mask);
128 va2x0x0123 = _mm256_xor_si256(va2x0x0123, vsign_mask);
129 va2x1x0123 = _mm256_xor_si256(va2x1x0123, vsign_mask);
130 va2x2x0123 = _mm256_xor_si256(va2x2x0123, vsign_mask);
131 va2x3x0123 = _mm256_xor_si256(va2x3x0123, vsign_mask);
132 va3x0x0123 = _mm256_xor_si256(va3x0x0123, vsign_mask);
133 va3x1x0123 = _mm256_xor_si256(va3x1x0123, vsign_mask);
134 va3x2x0123 = _mm256_xor_si256(va3x2x0123, vsign_mask);
135 va3x3x0123 = _mm256_xor_si256(va3x3x0123, vsign_mask);
136
137 const __m256i vb0x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 0));
138 const __m256i vb1x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
139 const __m256i vb2x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
140 const __m256i vb3x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
141 w = (const int8_t*) w + 128;
142
143 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0x0123, vb0x01234567);
144 vacc0x1x01234567 = _mm256_dpbusd_avx_epi32(vacc0x1x01234567, va0x1x0123, vb1x01234567);
145 vacc0x2x01234567 = _mm256_dpbusd_avx_epi32(vacc0x2x01234567, va0x2x0123, vb2x01234567);
146 vacc0x3x01234567 = _mm256_dpbusd_avx_epi32(vacc0x3x01234567, va0x3x0123, vb3x01234567);
147 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0x0123, vb0x01234567);
148 vacc1x1x01234567 = _mm256_dpbusd_avx_epi32(vacc1x1x01234567, va1x1x0123, vb1x01234567);
149 vacc1x2x01234567 = _mm256_dpbusd_avx_epi32(vacc1x2x01234567, va1x2x0123, vb2x01234567);
150 vacc1x3x01234567 = _mm256_dpbusd_avx_epi32(vacc1x3x01234567, va1x3x0123, vb3x01234567);
151 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0x0123, vb0x01234567);
152 vacc2x1x01234567 = _mm256_dpbusd_avx_epi32(vacc2x1x01234567, va2x1x0123, vb1x01234567);
153 vacc2x2x01234567 = _mm256_dpbusd_avx_epi32(vacc2x2x01234567, va2x2x0123, vb2x01234567);
154 vacc2x3x01234567 = _mm256_dpbusd_avx_epi32(vacc2x3x01234567, va2x3x0123, vb3x01234567);
155 vacc3x0x01234567 = _mm256_dpbusd_avx_epi32(vacc3x0x01234567, va3x0x0123, vb0x01234567);
156 vacc3x1x01234567 = _mm256_dpbusd_avx_epi32(vacc3x1x01234567, va3x1x0123, vb1x01234567);
157 vacc3x2x01234567 = _mm256_dpbusd_avx_epi32(vacc3x2x01234567, va3x2x0123, vb2x01234567);
158 vacc3x3x01234567 = _mm256_dpbusd_avx_epi32(vacc3x3x01234567, va3x3x0123, vb3x01234567);
159
160 k -= 16 * sizeof(int8_t);
161 }
162 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x1x01234567);
163 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x1x01234567);
164 vacc2x0x01234567 = _mm256_add_epi32(vacc2x0x01234567, vacc2x1x01234567);
165 vacc3x0x01234567 = _mm256_add_epi32(vacc3x0x01234567, vacc3x1x01234567);
166 vacc0x2x01234567 = _mm256_add_epi32(vacc0x2x01234567, vacc0x3x01234567);
167 vacc1x2x01234567 = _mm256_add_epi32(vacc1x2x01234567, vacc1x3x01234567);
168 vacc2x2x01234567 = _mm256_add_epi32(vacc2x2x01234567, vacc2x3x01234567);
169 vacc3x2x01234567 = _mm256_add_epi32(vacc3x2x01234567, vacc3x3x01234567);
170 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x2x01234567);
171 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x2x01234567);
172 vacc2x0x01234567 = _mm256_add_epi32(vacc2x0x01234567, vacc2x2x01234567);
173 vacc3x0x01234567 = _mm256_add_epi32(vacc3x0x01234567, vacc3x2x01234567);
174 while (k != 0) {
175 __m256i va0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0));
176 a0 += 4;
177 __m256i va1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1));
178 a1 += 4;
179 __m256i va2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2));
180 a2 += 4;
181 __m256i va3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3));
182 a3 += 4;
183
184 va0x0123 = _mm256_xor_si256(va0x0123, vsign_mask);
185 va1x0123 = _mm256_xor_si256(va1x0123, vsign_mask);
186 va2x0123 = _mm256_xor_si256(va2x0123, vsign_mask);
187 va3x0123 = _mm256_xor_si256(va3x0123, vsign_mask);
188
189 const __m256i vb01234567 = _mm256_load_si256(w);
190
191 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0123, vb01234567);
192 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0123, vb01234567);
193 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0123, vb01234567);
194 vacc3x0x01234567 = _mm256_dpbusd_avx_epi32(vacc3x0x01234567, va3x0123, vb01234567);
195
196 w = (const int8_t*) w + 32;
197 k -= 4 * sizeof(int8_t);
198 }
199
200 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x0x01234567);
201 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x0x01234567);
202 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x0x01234567);
203 __m256 vout3x01234567 = _mm256_cvtepi32_ps(vacc3x0x01234567);
204
205 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
206 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
207 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
208 vout3x01234567 = _mm256_mul_ps(vout3x01234567, _mm256_set1_ps(quantization_params[3].inv_scale));
209
210 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
211 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
212 w = (const float*) w + 16;
213
214 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
215 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
216 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
217 vout3x01234567 = _mm256_fmadd_ps(vout3x01234567, vfilter_output_scale01234567, vbias01234567);
218
219 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
220 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
221 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
222 vout3x01234567 = _mm256_max_ps(vout3x01234567, voutput_min);
223
224 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
225 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
226 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
227 vout3x01234567 = _mm256_min_ps(vout3x01234567, voutput_max);
228
229 if XNN_LIKELY(nc >= 8) {
230 _mm256_storeu_ps(c0, vout0x01234567);
231 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
232 c0 = (float*) ((uintptr_t) c0 + cn_stride);
233 _mm256_storeu_ps(c1, vout1x01234567);
234 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
235 c1 = (float*) ((uintptr_t) c1 + cn_stride);
236 _mm256_storeu_ps(c2, vout2x01234567);
237 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
238 c2 = (float*) ((uintptr_t) c2 + cn_stride);
239 _mm256_storeu_ps(c3, vout3x01234567);
240 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
241 c3 = (float*) ((uintptr_t) c3 + cn_stride);
242 nc -= 8;
243 } else {
244 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
245 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
246 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
247 __m128 vout3x0123 = _mm256_castps256_ps128(vout3x01234567);
248 if (nc & 4) {
249 _mm_storeu_ps(c0, vout0x0123);
250 c0 += 4;
251 _mm_storeu_ps(c1, vout1x0123);
252 c1 += 4;
253 _mm_storeu_ps(c2, vout2x0123);
254 c2 += 4;
255 _mm_storeu_ps(c3, vout3x0123);
256 c3 += 4;
257 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
258 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
259 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
260 vout3x0123 = _mm256_extractf128_ps(vout3x01234567, 1);
261 }
262 if (nc & 2) {
263 _mm_storel_pi((__m64*) c0, vout0x0123);
264 c0 += 2;
265 _mm_storel_pi((__m64*) c1, vout1x0123);
266 c1 += 2;
267 _mm_storel_pi((__m64*) c2, vout2x0123);
268 c2 += 2;
269 _mm_storel_pi((__m64*) c3, vout3x0123);
270 c3 += 2;
271 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
272 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
273 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
274 vout3x0123 = _mm_movehl_ps(vout3x0123, vout3x0123);
275 }
276 if (nc & 1) {
277 _mm_store_ss(c0, vout0x0123);
278 _mm_store_ss(c1, vout1x0123);
279 _mm_store_ss(c2, vout2x0123);
280 _mm_store_ss(c3, vout3x0123);
281 }
282 nc = 0;
283 }
284 } while (nc != 0);
285 }