Debian Codegraph @

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