Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-5x8c8-minmax-avx256vnni.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx8c8-avxvnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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 <immintrin.h>
13
14 #include "xnnpack/gemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18
19
20 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_5x8c8__avx256vnni(
21 size_t mr,
22 size_t nc,
23 size_t kc,
24 const int8_t* restrict a,
25 size_t a_stride,
26 const void* restrict w,
27 float* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
31 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33 assert(mr != 0);
34 assert(mr <= 5);
35 assert(nc != 0);
36 assert(kc != 0);
37 assert(kc % sizeof(int8_t) == 0);
38 assert(a != NULL);
39 assert(w != NULL);
40 assert(c != NULL);
41
42 kc = round_up_po2(kc, 8 * sizeof(int8_t));
43 const int8_t* a0 = a;
44 float* c0 = c;
45 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
46 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
47 if XNN_UNPREDICTABLE(mr < 2) {
48 a1 = a0;
49 c1 = c0;
50 }
51 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
52 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
53 if XNN_UNPREDICTABLE(mr <= 2) {
54 a2 = a1;
55 c2 = c1;
56 }
57 const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
58 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
59 if XNN_UNPREDICTABLE(mr < 4) {
60 a3 = a2;
61 c3 = c2;
62 }
63 const int8_t* a4 = (const int8_t*) ((uintptr_t) a3 + a_stride);
64 float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
65 if XNN_UNPREDICTABLE(mr <= 4) {
66 a4 = a3;
67 c4 = c3;
68 }
69
70 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
71 XNN_FORCE_REALIZATION(vsign_mask);
72 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
73 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
74 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
75 const __m256i vinput_zero_point3 = _mm256_set1_epi32((int) quantization_params[3].zero_point + 128);
76 const __m256i vinput_zero_point4 = _mm256_set1_epi32((int) quantization_params[4].zero_point + 128);
77 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
78 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
79 // XNN_FORCE_REALIZATION(voutput_min);
80 // XNN_FORCE_REALIZATION(voutput_max);
81 do {
82 const __m256i vksum01234567 = _mm256_load_si256(w);
83 __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
84 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
85 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
86 __m256i vsum1x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
87 __m256i vacc1x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 0));
88 __m256i vacc1x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 1));
89 __m256i vsum2x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
90 __m256i vacc2x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 0));
91 __m256i vacc2x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 1));
92 __m256i vsum3x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point3);
93 __m256i vacc3x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum3x01234567, 0));
94 __m256i vacc3x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum3x01234567, 1));
95 __m256i vsum4x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point4);
96 __m256i vacc4x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum4x01234567, 0));
97 __m256i vacc4x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum4x01234567, 1));
98 w = (const int32_t*) w + 8;
99
100 size_t k = kc;
101 while (k >= 16 * sizeof(int8_t)) {
102 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
103 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
104 a0 += 16;
105 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
106 const __m256i va1x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
107 a1 += 16;
108 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
109 const __m256i va2x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
110 a2 += 16;
111 const __m256i va3x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
112 const __m256i va3x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3 + 8)), vsign_mask);
113 a3 += 16;
114 const __m256i va4x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a4)), vsign_mask);
115 const __m256i va4x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a4 + 8)), vsign_mask);
116 a4 += 16;
117
118 const __m256i vb01234567x0123 = _mm256_load_si256(w);
119 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
120 const __m256i vb01234567x4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
121 const __m256i vb89ABCDEFx4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
122
123 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
124 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
125 vacc1x0123 = _mm256_dpbusd_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
126 vacc1x4567 = _mm256_dpbusd_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
127 vacc2x0123 = _mm256_dpbusd_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
128 vacc2x4567 = _mm256_dpbusd_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
129 vacc3x0123 = _mm256_dpbusd_epi32(vacc3x0123, va3x01234567, vb01234567x0123);
130 vacc3x4567 = _mm256_dpbusd_epi32(vacc3x4567, va3x01234567, vb89ABCDEFx0123);
131 vacc4x0123 = _mm256_dpbusd_epi32(vacc4x0123, va4x01234567, vb01234567x0123);
132 vacc4x4567 = _mm256_dpbusd_epi32(vacc4x4567, va4x01234567, vb89ABCDEFx0123);
133 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x89ABCDEF, vb01234567x4567);
134 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
135 vacc1x0123 = _mm256_dpbusd_epi32(vacc1x0123, va1x89ABCDEF, vb01234567x4567);
136 vacc1x4567 = _mm256_dpbusd_epi32(vacc1x4567, va1x89ABCDEF, vb89ABCDEFx4567);
137 vacc2x0123 = _mm256_dpbusd_epi32(vacc2x0123, va2x89ABCDEF, vb01234567x4567);
138 vacc2x4567 = _mm256_dpbusd_epi32(vacc2x4567, va2x89ABCDEF, vb89ABCDEFx4567);
139 vacc3x0123 = _mm256_dpbusd_epi32(vacc3x0123, va3x89ABCDEF, vb01234567x4567);
140 vacc3x4567 = _mm256_dpbusd_epi32(vacc3x4567, va3x89ABCDEF, vb89ABCDEFx4567);
141 vacc4x0123 = _mm256_dpbusd_epi32(vacc4x0123, va4x89ABCDEF, vb01234567x4567);
142 vacc4x4567 = _mm256_dpbusd_epi32(vacc4x4567, va4x89ABCDEF, vb89ABCDEFx4567);
143
144 w = (const int8_t*) w + 128;
145 k -= 16 * sizeof(int8_t);
146 }
147
148 if (k != 0) {
149 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
150 a0 += 8;
151 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
152 a1 += 8;
153 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
154 a2 += 8;
155 const __m256i va3x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
156 a3 += 8;
157 const __m256i va4x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a4)), vsign_mask);
158 a4 += 8;
159
160 const __m256i vb01234567x0123 = _mm256_load_si256(w);
161 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
162
163 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
164 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
165 vacc1x0123 = _mm256_dpbusd_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
166 vacc1x4567 = _mm256_dpbusd_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
167 vacc2x0123 = _mm256_dpbusd_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
168 vacc2x4567 = _mm256_dpbusd_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
169 vacc3x0123 = _mm256_dpbusd_epi32(vacc3x0123, va3x01234567, vb01234567x0123);
170 vacc3x4567 = _mm256_dpbusd_epi32(vacc3x4567, va3x01234567, vb89ABCDEFx0123);
171 vacc4x0123 = _mm256_dpbusd_epi32(vacc4x0123, va4x01234567, vb01234567x0123);
172 vacc4x4567 = _mm256_dpbusd_epi32(vacc4x4567, va4x01234567, vb89ABCDEFx0123);
173
174 w = (const int8_t*) w + 64;
175 k -= 8 * sizeof(int8_t);
176 }
177
178 // Add adjacent pairs
179 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
180 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
181 const __m256i vsum1x02134657 = _mm256_hadd_epi32(vacc1x0123, vacc1x4567);
182 __m256i vacc1x01234567 = _mm256_permute4x64_epi64(vsum1x02134657, _MM_SHUFFLE(3, 1, 2, 0));
183 const __m256i vsum2x02134657 = _mm256_hadd_epi32(vacc2x0123, vacc2x4567);
184 __m256i vacc2x01234567 = _mm256_permute4x64_epi64(vsum2x02134657, _MM_SHUFFLE(3, 1, 2, 0));
185 const __m256i vsum3x02134657 = _mm256_hadd_epi32(vacc3x0123, vacc3x4567);
186 __m256i vacc3x01234567 = _mm256_permute4x64_epi64(vsum3x02134657, _MM_SHUFFLE(3, 1, 2, 0));
187 const __m256i vsum4x02134657 = _mm256_hadd_epi32(vacc4x0123, vacc4x4567);
188 __m256i vacc4x01234567 = _mm256_permute4x64_epi64(vsum4x02134657, _MM_SHUFFLE(3, 1, 2, 0));
189
190 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
191 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
192 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
193 __m256 vout3x01234567 = _mm256_cvtepi32_ps(vacc3x01234567);
194 __m256 vout4x01234567 = _mm256_cvtepi32_ps(vacc4x01234567);
195
196 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
197 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
198 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
199 vout3x01234567 = _mm256_mul_ps(vout3x01234567, _mm256_set1_ps(quantization_params[3].inv_scale));
200 vout4x01234567 = _mm256_mul_ps(vout4x01234567, _mm256_set1_ps(quantization_params[4].inv_scale));
201
202 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
203 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
204 w = (const float*) w + 16;
205
206 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
207 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
208 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
209 vout3x01234567 = _mm256_fmadd_ps(vout3x01234567, vfilter_output_scale01234567, vbias01234567);
210 vout4x01234567 = _mm256_fmadd_ps(vout4x01234567, vfilter_output_scale01234567, vbias01234567);
211
212 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
213 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
214 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
215 vout3x01234567 = _mm256_max_ps(vout3x01234567, voutput_min);
216 vout4x01234567 = _mm256_max_ps(vout4x01234567, voutput_min);
217
218 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
219 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
220 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
221 vout3x01234567 = _mm256_min_ps(vout3x01234567, voutput_max);
222 vout4x01234567 = _mm256_min_ps(vout4x01234567, voutput_max);
223
224 if XNN_LIKELY(nc >= 8) {
225 _mm256_storeu_ps(c0, vout0x01234567);
226 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
227 c0 = (float*) ((uintptr_t) c0 + cn_stride);
228 _mm256_storeu_ps(c1, vout1x01234567);
229 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
230 c1 = (float*) ((uintptr_t) c1 + cn_stride);
231 _mm256_storeu_ps(c2, vout2x01234567);
232 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
233 c2 = (float*) ((uintptr_t) c2 + cn_stride);
234 _mm256_storeu_ps(c3, vout3x01234567);
235 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
236 c3 = (float*) ((uintptr_t) c3 + cn_stride);
237 _mm256_storeu_ps(c4, vout4x01234567);
238 a4 = (const int8_t*) ((uintptr_t) a4 - kc);
239 c4 = (float*) ((uintptr_t) c4 + cn_stride);
240 nc -= 8;
241 } else {
242 // Prepare mask for valid 32-bit elements (depends on nc).
243 const __mmask16 vmask = _cvtu32_mask16((UINT32_C(1) << nc) - 1);
244 _mm256_mask_storeu_ps(c0, vmask, vout0x01234567);
245 _mm256_mask_storeu_ps(c1, vmask, vout1x01234567);
246 _mm256_mask_storeu_ps(c2, vmask, vout2x01234567);
247 _mm256_mask_storeu_ps(c3, vmask, vout3x01234567);
248 _mm256_mask_storeu_ps(c4, vmask, vout4x01234567);
249 nc = 0;
250 }
251 } while (nc != 0);
252 }