Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-4x8c8-minmax-avxvnni.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_qc4w_gemm_minmax_ukernel_4x8c8__avxvnni(
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 struct xnn_f32_qc4w_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 <= 4);
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
64 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
65 XNN_FORCE_REALIZATION(vsign_mask);
66 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
67 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
68 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
69 const __m256i vinput_zero_point3 = _mm256_set1_epi32((int) quantization_params[3].zero_point + 128);
70 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
71 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
72 // XNN_FORCE_REALIZATION(voutput_min);
73 // XNN_FORCE_REALIZATION(voutput_max);
74 const __m256i vmask = _mm256_set1_epi8(0xF0);
75 XNN_FORCE_REALIZATION(vmask);
76 do {
77 const __m256i vksum01234567 = _mm256_load_si256(w);
78 __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
79 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
80 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
81 __m256i vsum1x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
82 __m256i vacc1x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 0));
83 __m256i vacc1x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 1));
84 __m256i vsum2x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
85 __m256i vacc2x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 0));
86 __m256i vacc2x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 1));
87 __m256i vsum3x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point3);
88 __m256i vacc3x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum3x01234567, 0));
89 __m256i vacc3x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum3x01234567, 1));
90 w = (const int32_t*) w + 8;
91
92 size_t k = kc;
93 while (k >= 16 * sizeof(int8_t)) {
94 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
95 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
96 a0 += 16;
97 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
98 const __m256i va1x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
99 a1 += 16;
100 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
101 const __m256i va2x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
102 a2 += 16;
103 const __m256i va3x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
104 const __m256i va3x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3 + 8)), vsign_mask);
105 a3 += 16;
106
107 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
108 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
109 const __m256i vbs01234567x0123 = _mm256_slli_epi32(vbb01234567x01234567, 4);
110 const __m256i vbs89ABCDEFx0123 = _mm256_slli_epi32(vbb89ABCDEFx01234567, 4);
111 const __m256i vb01234567x4567 = _mm256_and_si256(vbb01234567x01234567, vmask);
112 const __m256i vb89ABCDEFx4567 = _mm256_and_si256(vbb89ABCDEFx01234567, vmask);
113 const __m256i vb01234567x0123 = _mm256_and_si256(vbs01234567x0123, vmask);
114 const __m256i vb89ABCDEFx0123 = _mm256_and_si256(vbs89ABCDEFx0123, vmask);
115
116 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
117 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
118 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
119 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
120 vacc2x0123 = _mm256_dpbusd_avx_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
121 vacc2x4567 = _mm256_dpbusd_avx_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
122 vacc3x0123 = _mm256_dpbusd_avx_epi32(vacc3x0123, va3x01234567, vb01234567x0123);
123 vacc3x4567 = _mm256_dpbusd_avx_epi32(vacc3x4567, va3x01234567, vb89ABCDEFx0123);
124 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x89ABCDEF, vb01234567x4567);
125 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
126 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x89ABCDEF, vb01234567x4567);
127 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x89ABCDEF, vb89ABCDEFx4567);
128 vacc2x0123 = _mm256_dpbusd_avx_epi32(vacc2x0123, va2x89ABCDEF, vb01234567x4567);
129 vacc2x4567 = _mm256_dpbusd_avx_epi32(vacc2x4567, va2x89ABCDEF, vb89ABCDEFx4567);
130 vacc3x0123 = _mm256_dpbusd_avx_epi32(vacc3x0123, va3x89ABCDEF, vb01234567x4567);
131 vacc3x4567 = _mm256_dpbusd_avx_epi32(vacc3x4567, va3x89ABCDEF, vb89ABCDEFx4567);
132
133 w = (const int8_t*) w + 64;
134 k -= 16 * sizeof(int8_t);
135 }
136
137 if (k != 0) {
138 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
139 a0 += 8;
140 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
141 a1 += 8;
142 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
143 a2 += 8;
144 const __m256i va3x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
145 a3 += 8;
146
147 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
148 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
149 const __m256i vb01234567x0123 = _mm256_slli_epi32(vbb01234567x01234567, 4);
150 const __m256i vb89ABCDEFx0123 = _mm256_slli_epi32(vbb89ABCDEFx01234567, 4);
151
152 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
153 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
154 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
155 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
156 vacc2x0123 = _mm256_dpbusd_avx_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
157 vacc2x4567 = _mm256_dpbusd_avx_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
158 vacc3x0123 = _mm256_dpbusd_avx_epi32(vacc3x0123, va3x01234567, vb01234567x0123);
159 vacc3x4567 = _mm256_dpbusd_avx_epi32(vacc3x4567, va3x01234567, vb89ABCDEFx0123);
160
161 w = (const int8_t*) w + 64;
162 k -= 8 * sizeof(int8_t);
163 }
164
165 // Add adjacent pairs
166 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
167 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
168 const __m256i vsum1x02134657 = _mm256_hadd_epi32(vacc1x0123, vacc1x4567);
169 __m256i vacc1x01234567 = _mm256_permute4x64_epi64(vsum1x02134657, _MM_SHUFFLE(3, 1, 2, 0));
170 const __m256i vsum2x02134657 = _mm256_hadd_epi32(vacc2x0123, vacc2x4567);
171 __m256i vacc2x01234567 = _mm256_permute4x64_epi64(vsum2x02134657, _MM_SHUFFLE(3, 1, 2, 0));
172 const __m256i vsum3x02134657 = _mm256_hadd_epi32(vacc3x0123, vacc3x4567);
173 __m256i vacc3x01234567 = _mm256_permute4x64_epi64(vsum3x02134657, _MM_SHUFFLE(3, 1, 2, 0));
174
175 vacc0x01234567 = _mm256_srai_epi32(vacc0x01234567, 4);
176 vacc1x01234567 = _mm256_srai_epi32(vacc1x01234567, 4);
177 vacc2x01234567 = _mm256_srai_epi32(vacc2x01234567, 4);
178 vacc3x01234567 = _mm256_srai_epi32(vacc3x01234567, 4);
179 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
180 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
181 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
182 __m256 vout3x01234567 = _mm256_cvtepi32_ps(vacc3x01234567);
183
184 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
185 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
186 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
187 vout3x01234567 = _mm256_mul_ps(vout3x01234567, _mm256_set1_ps(quantization_params[3].inv_scale));
188
189 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
190 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
191 w = (const float*) w + 16;
192
193 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
194 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
195 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
196 vout3x01234567 = _mm256_fmadd_ps(vout3x01234567, vfilter_output_scale01234567, vbias01234567);
197
198 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
199 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
200 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
201 vout3x01234567 = _mm256_max_ps(vout3x01234567, voutput_min);
202
203 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
204 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
205 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
206 vout3x01234567 = _mm256_min_ps(vout3x01234567, voutput_max);
207
208 if XNN_LIKELY(nc >= 8) {
209 _mm256_storeu_ps(c0, vout0x01234567);
210 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
211 c0 = (float*) ((uintptr_t) c0 + cn_stride);
212 _mm256_storeu_ps(c1, vout1x01234567);
213 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
214 c1 = (float*) ((uintptr_t) c1 + cn_stride);
215 _mm256_storeu_ps(c2, vout2x01234567);
216 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
217 c2 = (float*) ((uintptr_t) c2 + cn_stride);
218 _mm256_storeu_ps(c3, vout3x01234567);
219 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
220 c3 = (float*) ((uintptr_t) c3 + cn_stride);
221 nc -= 8;
222 } else {
223 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
224 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
225 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
226 __m128 vout3x0123 = _mm256_castps256_ps128(vout3x01234567);
227 if (nc & 4) {
228 _mm_storeu_ps(c0, vout0x0123);
229 c0 += 4;
230 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
231 _mm_storeu_ps(c1, vout1x0123);
232 c1 += 4;
233 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
234 _mm_storeu_ps(c2, vout2x0123);
235 c2 += 4;
236 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
237 _mm_storeu_ps(c3, vout3x0123);
238 c3 += 4;
239 vout3x0123 = _mm256_extractf128_ps(vout3x01234567, 1);
240 }
241 if (nc & 2) {
242 _mm_storel_pi((__m64*) c0, vout0x0123);
243 c0 += 2;
244 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
245 _mm_storel_pi((__m64*) c1, vout1x0123);
246 c1 += 2;
247 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
248 _mm_storel_pi((__m64*) c2, vout2x0123);
249 c2 += 2;
250 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
251 _mm_storel_pi((__m64*) c3, vout3x0123);
252 c3 += 2;
253 vout3x0123 = _mm_movehl_ps(vout3x0123, vout3x0123);
254 }
255 if (nc & 1) {
256 _mm_store_ss(c0, vout0x0123);
257 _mm_store_ss(c1, vout1x0123);
258 _mm_store_ss(c2, vout2x0123);
259 _mm_store_ss(c3, vout3x0123);
260 }
261 nc = 0;
262 }
263 } while (nc != 0);
264 }