Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-4x8c4-minmax-avxvnni-u2-acc2.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_u2_acc2(
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 vacc1x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
82 __m256i vacc1x1x01234567 = _mm256_setzero_si256();
83 __m256i vacc2x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
84 __m256i vacc2x1x01234567 = _mm256_setzero_si256();
85 __m256i vacc3x0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point3);
86 __m256i vacc3x1x01234567 = _mm256_setzero_si256();
87 w = (const int32_t*) w + 8;
88
89 size_t k = kc;
90 while (k >= 8 * sizeof(int8_t)) {
91 __m256i va0x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 0));
92 __m256i va0x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0 + 4));
93 a0 += 8;
94 __m256i va1x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 0));
95 __m256i va1x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1 + 4));
96 a1 += 8;
97 __m256i va2x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 0));
98 __m256i va2x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2 + 4));
99 a2 += 8;
100 __m256i va3x0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 0));
101 __m256i va3x1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3 + 4));
102 a3 += 8;
103
104 va0x0x0123 = _mm256_xor_si256(va0x0x0123, vsign_mask);
105 va0x1x0123 = _mm256_xor_si256(va0x1x0123, vsign_mask);
106 va1x0x0123 = _mm256_xor_si256(va1x0x0123, vsign_mask);
107 va1x1x0123 = _mm256_xor_si256(va1x1x0123, vsign_mask);
108 va2x0x0123 = _mm256_xor_si256(va2x0x0123, vsign_mask);
109 va2x1x0123 = _mm256_xor_si256(va2x1x0123, vsign_mask);
110 va3x0x0123 = _mm256_xor_si256(va3x0x0123, vsign_mask);
111 va3x1x0123 = _mm256_xor_si256(va3x1x0123, vsign_mask);
112
113 const __m256i vb0x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 0));
114 const __m256i vb1x01234567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
115 w = (const int8_t*) w + 64;
116
117 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0x0123, vb0x01234567);
118 vacc0x1x01234567 = _mm256_dpbusd_avx_epi32(vacc0x1x01234567, va0x1x0123, vb1x01234567);
119 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0x0123, vb0x01234567);
120 vacc1x1x01234567 = _mm256_dpbusd_avx_epi32(vacc1x1x01234567, va1x1x0123, vb1x01234567);
121 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0x0123, vb0x01234567);
122 vacc2x1x01234567 = _mm256_dpbusd_avx_epi32(vacc2x1x01234567, va2x1x0123, vb1x01234567);
123 vacc3x0x01234567 = _mm256_dpbusd_avx_epi32(vacc3x0x01234567, va3x0x0123, vb0x01234567);
124 vacc3x1x01234567 = _mm256_dpbusd_avx_epi32(vacc3x1x01234567, va3x1x0123, vb1x01234567);
125
126 k -= 8 * sizeof(int8_t);
127 }
128 vacc0x0x01234567 = _mm256_add_epi32(vacc0x0x01234567, vacc0x1x01234567);
129 vacc1x0x01234567 = _mm256_add_epi32(vacc1x0x01234567, vacc1x1x01234567);
130 vacc2x0x01234567 = _mm256_add_epi32(vacc2x0x01234567, vacc2x1x01234567);
131 vacc3x0x01234567 = _mm256_add_epi32(vacc3x0x01234567, vacc3x1x01234567);
132 while (k != 0) {
133 __m256i va0x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a0));
134 a0 += 4;
135 __m256i va1x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a1));
136 a1 += 4;
137 __m256i va2x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a2));
138 a2 += 4;
139 __m256i va3x0123 = _mm256_set1_epi32((int) unaligned_load_u32(a3));
140 a3 += 4;
141
142 va0x0123 = _mm256_xor_si256(va0x0123, vsign_mask);
143 va1x0123 = _mm256_xor_si256(va1x0123, vsign_mask);
144 va2x0123 = _mm256_xor_si256(va2x0123, vsign_mask);
145 va3x0123 = _mm256_xor_si256(va3x0123, vsign_mask);
146
147 const __m256i vb01234567 = _mm256_load_si256(w);
148
149 vacc0x0x01234567 = _mm256_dpbusd_avx_epi32(vacc0x0x01234567, va0x0123, vb01234567);
150 vacc1x0x01234567 = _mm256_dpbusd_avx_epi32(vacc1x0x01234567, va1x0123, vb01234567);
151 vacc2x0x01234567 = _mm256_dpbusd_avx_epi32(vacc2x0x01234567, va2x0123, vb01234567);
152 vacc3x0x01234567 = _mm256_dpbusd_avx_epi32(vacc3x0x01234567, va3x0123, vb01234567);
153
154 w = (const int8_t*) w + 32;
155 k -= 4 * sizeof(int8_t);
156 }
157
158 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x0x01234567);
159 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x0x01234567);
160 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x0x01234567);
161 __m256 vout3x01234567 = _mm256_cvtepi32_ps(vacc3x0x01234567);
162
163 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
164 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
165 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
166 vout3x01234567 = _mm256_mul_ps(vout3x01234567, _mm256_set1_ps(quantization_params[3].inv_scale));
167
168 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
169 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
170 w = (const float*) w + 16;
171
172 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
173 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
174 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
175 vout3x01234567 = _mm256_fmadd_ps(vout3x01234567, vfilter_output_scale01234567, vbias01234567);
176
177 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
178 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
179 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
180 vout3x01234567 = _mm256_max_ps(vout3x01234567, voutput_min);
181
182 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
183 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
184 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
185 vout3x01234567 = _mm256_min_ps(vout3x01234567, voutput_max);
186
187 if XNN_LIKELY(nc >= 8) {
188 _mm256_storeu_ps(c0, vout0x01234567);
189 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
190 c0 = (float*) ((uintptr_t) c0 + cn_stride);
191 _mm256_storeu_ps(c1, vout1x01234567);
192 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
193 c1 = (float*) ((uintptr_t) c1 + cn_stride);
194 _mm256_storeu_ps(c2, vout2x01234567);
195 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
196 c2 = (float*) ((uintptr_t) c2 + cn_stride);
197 _mm256_storeu_ps(c3, vout3x01234567);
198 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
199 c3 = (float*) ((uintptr_t) c3 + cn_stride);
200 nc -= 8;
201 } else {
202 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
203 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
204 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
205 __m128 vout3x0123 = _mm256_castps256_ps128(vout3x01234567);
206 if (nc & 4) {
207 _mm_storeu_ps(c0, vout0x0123);
208 c0 += 4;
209 _mm_storeu_ps(c1, vout1x0123);
210 c1 += 4;
211 _mm_storeu_ps(c2, vout2x0123);
212 c2 += 4;
213 _mm_storeu_ps(c3, vout3x0123);
214 c3 += 4;
215 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
216 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
217 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
218 vout3x0123 = _mm256_extractf128_ps(vout3x01234567, 1);
219 }
220 if (nc & 2) {
221 _mm_storel_pi((__m64*) c0, vout0x0123);
222 c0 += 2;
223 _mm_storel_pi((__m64*) c1, vout1x0123);
224 c1 += 2;
225 _mm_storel_pi((__m64*) c2, vout2x0123);
226 c2 += 2;
227 _mm_storel_pi((__m64*) c3, vout3x0123);
228 c3 += 2;
229 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
230 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
231 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
232 vout3x0123 = _mm_movehl_ps(vout3x0123, vout3x0123);
233 }
234 if (nc & 1) {
235 _mm_store_ss(c0, vout0x0123);
236 _mm_store_ss(c1, vout1x0123);
237 _mm_store_ss(c2, vout2x0123);
238 _mm_store_ss(c3, vout3x0123);
239 }
240 nc = 0;
241 }
242 } while (nc != 0);
243 }