Debian Codegraph @

src/qd8-f16-qc4w-gemm/gen/qd8-f16-qc4w-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_f16_qc4w_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 xnn_float16* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 const struct xnn_f16_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 <= 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 uint16_t* c0 = (uint16_t*) c;
45 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
46 uint16_t* c1 = (uint16_t*) ((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 uint16_t* c2 = (uint16_t*) ((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 uint16_t* c3 = (uint16_t*) ((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 uint16_t* c4 = (uint16_t*) ((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_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
78 const __m256 voutput_max = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
79 // XNN_FORCE_REALIZATION(voutput_min);
80 // XNN_FORCE_REALIZATION(voutput_max);
81 const __m256i vmask = _mm256_set1_epi8(0xF0);
82 XNN_FORCE_REALIZATION(vmask);
83 do {
84 const __m256i vksum01234567 = _mm256_load_si256(w);
85 __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
86 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
87 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
88 __m256i vsum1x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
89 __m256i vacc1x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 0));
90 __m256i vacc1x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 1));
91 __m256i vsum2x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
92 __m256i vacc2x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 0));
93 __m256i vacc2x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 1));
94 __m256i vsum3x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point3);
95 __m256i vacc3x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum3x01234567, 0));
96 __m256i vacc3x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum3x01234567, 1));
97 __m256i vsum4x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point4);
98 __m256i vacc4x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum4x01234567, 0));
99 __m256i vacc4x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum4x01234567, 1));
100 w = (const int32_t*) w + 8;
101
102 size_t k = kc;
103 while (k >= 16 * sizeof(int8_t)) {
104 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
105 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
106 a0 += 16;
107 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
108 const __m256i va1x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
109 a1 += 16;
110 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
111 const __m256i va2x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
112 a2 += 16;
113 const __m256i va3x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
114 const __m256i va3x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3 + 8)), vsign_mask);
115 a3 += 16;
116 const __m256i va4x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a4)), vsign_mask);
117 const __m256i va4x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a4 + 8)), vsign_mask);
118 a4 += 16;
119
120 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
121 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
122 const __m256i vbs01234567x0123 = _mm256_slli_epi32(vbb01234567x01234567, 4);
123 const __m256i vbs89ABCDEFx0123 = _mm256_slli_epi32(vbb89ABCDEFx01234567, 4);
124 const __m256i vb01234567x4567 = _mm256_and_si256(vbb01234567x01234567, vmask);
125 const __m256i vb89ABCDEFx4567 = _mm256_and_si256(vbb89ABCDEFx01234567, vmask);
126 const __m256i vb01234567x0123 = _mm256_and_si256(vbs01234567x0123, vmask);
127 const __m256i vb89ABCDEFx0123 = _mm256_and_si256(vbs89ABCDEFx0123, vmask);
128
129 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
130 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
131 vacc1x0123 = _mm256_dpbusd_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
132 vacc1x4567 = _mm256_dpbusd_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
133 vacc2x0123 = _mm256_dpbusd_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
134 vacc2x4567 = _mm256_dpbusd_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
135 vacc3x0123 = _mm256_dpbusd_epi32(vacc3x0123, va3x01234567, vb01234567x0123);
136 vacc3x4567 = _mm256_dpbusd_epi32(vacc3x4567, va3x01234567, vb89ABCDEFx0123);
137 vacc4x0123 = _mm256_dpbusd_epi32(vacc4x0123, va4x01234567, vb01234567x0123);
138 vacc4x4567 = _mm256_dpbusd_epi32(vacc4x4567, va4x01234567, vb89ABCDEFx0123);
139 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x89ABCDEF, vb01234567x4567);
140 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
141 vacc1x0123 = _mm256_dpbusd_epi32(vacc1x0123, va1x89ABCDEF, vb01234567x4567);
142 vacc1x4567 = _mm256_dpbusd_epi32(vacc1x4567, va1x89ABCDEF, vb89ABCDEFx4567);
143 vacc2x0123 = _mm256_dpbusd_epi32(vacc2x0123, va2x89ABCDEF, vb01234567x4567);
144 vacc2x4567 = _mm256_dpbusd_epi32(vacc2x4567, va2x89ABCDEF, vb89ABCDEFx4567);
145 vacc3x0123 = _mm256_dpbusd_epi32(vacc3x0123, va3x89ABCDEF, vb01234567x4567);
146 vacc3x4567 = _mm256_dpbusd_epi32(vacc3x4567, va3x89ABCDEF, vb89ABCDEFx4567);
147 vacc4x0123 = _mm256_dpbusd_epi32(vacc4x0123, va4x89ABCDEF, vb01234567x4567);
148 vacc4x4567 = _mm256_dpbusd_epi32(vacc4x4567, va4x89ABCDEF, vb89ABCDEFx4567);
149
150 w = (const int8_t*) w + 64;
151 k -= 16 * sizeof(int8_t);
152 }
153
154 if (k != 0) {
155 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
156 a0 += 8;
157 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
158 a1 += 8;
159 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
160 a2 += 8;
161 const __m256i va3x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a3)), vsign_mask);
162 a3 += 8;
163 const __m256i va4x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a4)), vsign_mask);
164 a4 += 8;
165
166 const __m256i vbb01234567x01234567 = _mm256_load_si256(w);
167 const __m256i vbb89ABCDEFx01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
168 const __m256i vb01234567x0123 = _mm256_slli_epi32(vbb01234567x01234567, 4);
169 const __m256i vb89ABCDEFx0123 = _mm256_slli_epi32(vbb89ABCDEFx01234567, 4);
170
171 vacc0x0123 = _mm256_dpbusd_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
172 vacc0x4567 = _mm256_dpbusd_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
173 vacc1x0123 = _mm256_dpbusd_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
174 vacc1x4567 = _mm256_dpbusd_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
175 vacc2x0123 = _mm256_dpbusd_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
176 vacc2x4567 = _mm256_dpbusd_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
177 vacc3x0123 = _mm256_dpbusd_epi32(vacc3x0123, va3x01234567, vb01234567x0123);
178 vacc3x4567 = _mm256_dpbusd_epi32(vacc3x4567, va3x01234567, vb89ABCDEFx0123);
179 vacc4x0123 = _mm256_dpbusd_epi32(vacc4x0123, va4x01234567, vb01234567x0123);
180 vacc4x4567 = _mm256_dpbusd_epi32(vacc4x4567, va4x01234567, vb89ABCDEFx0123);
181
182 w = (const int8_t*) w + 64;
183 k -= 8 * sizeof(int8_t);
184 }
185
186 // Add adjacent pairs
187 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
188 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
189 const __m256i vsum1x02134657 = _mm256_hadd_epi32(vacc1x0123, vacc1x4567);
190 __m256i vacc1x01234567 = _mm256_permute4x64_epi64(vsum1x02134657, _MM_SHUFFLE(3, 1, 2, 0));
191 const __m256i vsum2x02134657 = _mm256_hadd_epi32(vacc2x0123, vacc2x4567);
192 __m256i vacc2x01234567 = _mm256_permute4x64_epi64(vsum2x02134657, _MM_SHUFFLE(3, 1, 2, 0));
193 const __m256i vsum3x02134657 = _mm256_hadd_epi32(vacc3x0123, vacc3x4567);
194 __m256i vacc3x01234567 = _mm256_permute4x64_epi64(vsum3x02134657, _MM_SHUFFLE(3, 1, 2, 0));
195 const __m256i vsum4x02134657 = _mm256_hadd_epi32(vacc4x0123, vacc4x4567);
196 __m256i vacc4x01234567 = _mm256_permute4x64_epi64(vsum4x02134657, _MM_SHUFFLE(3, 1, 2, 0));
197
198 vacc0x01234567 = _mm256_srai_epi32(vacc0x01234567, 4);
199 vacc1x01234567 = _mm256_srai_epi32(vacc1x01234567, 4);
200 vacc2x01234567 = _mm256_srai_epi32(vacc2x01234567, 4);
201 vacc3x01234567 = _mm256_srai_epi32(vacc3x01234567, 4);
202 vacc4x01234567 = _mm256_srai_epi32(vacc4x01234567, 4);
203 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
204 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
205 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
206 __m256 vout3x01234567 = _mm256_cvtepi32_ps(vacc3x01234567);
207 __m256 vout4x01234567 = _mm256_cvtepi32_ps(vacc4x01234567);
208
209 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
210 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
211 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
212 vout3x01234567 = _mm256_mul_ps(vout3x01234567, _mm256_set1_ps(quantization_params[3].inv_scale));
213 vout4x01234567 = _mm256_mul_ps(vout4x01234567, _mm256_set1_ps(quantization_params[4].inv_scale));
214
215 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
216 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
217 w = (const float*) w + 16;
218
219 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
220 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
221 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
222 vout3x01234567 = _mm256_fmadd_ps(vout3x01234567, vfilter_output_scale01234567, vbias01234567);
223 vout4x01234567 = _mm256_fmadd_ps(vout4x01234567, vfilter_output_scale01234567, vbias01234567);
224
225 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
226 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
227 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
228 vout3x01234567 = _mm256_max_ps(vout3x01234567, voutput_min);
229 vout4x01234567 = _mm256_max_ps(vout4x01234567, voutput_min);
230
231 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
232 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
233 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
234 vout3x01234567 = _mm256_min_ps(vout3x01234567, voutput_max);
235 vout4x01234567 = _mm256_min_ps(vout4x01234567, voutput_max);
236
237 __m128i vfp16out0x01234567 = _mm256_cvtps_ph(vout0x01234567, _MM_FROUND_TO_NEAREST_INT);
238 __m128i vfp16out1x01234567 = _mm256_cvtps_ph(vout1x01234567, _MM_FROUND_TO_NEAREST_INT);
239 __m128i vfp16out2x01234567 = _mm256_cvtps_ph(vout2x01234567, _MM_FROUND_TO_NEAREST_INT);
240 __m128i vfp16out3x01234567 = _mm256_cvtps_ph(vout3x01234567, _MM_FROUND_TO_NEAREST_INT);
241 __m128i vfp16out4x01234567 = _mm256_cvtps_ph(vout4x01234567, _MM_FROUND_TO_NEAREST_INT);
242 if XNN_LIKELY(nc >= 8) {
243 _mm_storeu_si128((__m128i*) c0, vfp16out0x01234567);
244 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
245 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
246 _mm_storeu_si128((__m128i*) c1, vfp16out1x01234567);
247 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
248 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
249 _mm_storeu_si128((__m128i*) c2, vfp16out2x01234567);
250 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
251 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
252 _mm_storeu_si128((__m128i*) c3, vfp16out3x01234567);
253 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
254 c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
255 _mm_storeu_si128((__m128i*) c4, vfp16out4x01234567);
256 a4 = (const int8_t*) ((uintptr_t) a4 - kc);
257 c4 = (uint16_t*) ((uintptr_t) c4 + cn_stride);
258 nc -= 8;
259 } else {
260 // Prepare mask for valid 16-bit elements (depends on nc).
261 const __mmask8 vmask = _cvtu32_mask8((UINT32_C(1) << nc) - 1);
262 _mm_mask_storeu_epi16(c0, vmask, vfp16out0x01234567);
263 _mm_mask_storeu_epi16(c1, vmask, vfp16out1x01234567);
264 _mm_mask_storeu_epi16(c2, vmask, vfp16out2x01234567);
265 _mm_mask_storeu_epi16(c3, vmask, vfp16out3x01234567);
266 _mm_mask_storeu_epi16(c4, vmask, vfp16out4x01234567);
267 nc = 0;
268 }
269 } while (nc != 0);
270 }