Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-3x8c8-minmax-avxvnni-prfm.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 #include "xnnpack/prefetch.h"
19
20
21 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_3x8c8__avxvnni_prfm(
22 size_t mr,
23 size_t nc,
24 size_t kc,
25 const int8_t* restrict a,
26 size_t a_stride,
27 const void* restrict w,
28 float* restrict c,
29 size_t cm_stride,
30 size_t cn_stride,
31 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
32 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
33 {
34 assert(mr != 0);
35 assert(mr <= 3);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(kc % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 kc = round_up_po2(kc, 8 * sizeof(int8_t));
44 const int8_t* a0 = a;
45 float* c0 = c;
46 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
47 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
48 if XNN_UNPREDICTABLE(mr < 2) {
49 a1 = a0;
50 c1 = c0;
51 }
52 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
53 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
54 if XNN_UNPREDICTABLE(mr <= 2) {
55 a2 = a1;
56 c2 = c1;
57 }
58
59 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
60 XNN_FORCE_REALIZATION(vsign_mask);
61 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point + 128);
62 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point + 128);
63 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point + 128);
64 const __m256 voutput_min = _mm256_set1_ps(params->scalar.min);
65 const __m256 voutput_max = _mm256_set1_ps(params->scalar.max);
66 // XNN_FORCE_REALIZATION(voutput_min);
67 // XNN_FORCE_REALIZATION(voutput_max);
68 do {
69 const __m256i vksum01234567 = _mm256_load_si256(w);
70 __m256i vsum0x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point0);
71 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 0));
72 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum0x01234567, 1));
73 __m256i vsum1x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point1);
74 __m256i vacc1x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 0));
75 __m256i vacc1x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum1x01234567, 1));
76 __m256i vsum2x01234567 = _mm256_mullo_epi32(vksum01234567, vinput_zero_point2);
77 __m256i vacc2x0123 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 0));
78 __m256i vacc2x4567 = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(vsum2x01234567, 1));
79 w = (const int32_t*) w + 8;
80
81 size_t k = kc;
82 while (k >= 16 * sizeof(int8_t)) {
83 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
84 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
85 a0 += 16;
86 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
87 const __m256i va1x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1 + 8)), vsign_mask);
88 a1 += 16;
89 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
90 const __m256i va2x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2 + 8)), vsign_mask);
91 a2 += 16;
92
93 const __m256i vb01234567x0123 = _mm256_load_si256(w);
94 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
95 const __m256i vb01234567x4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
96 const __m256i vb89ABCDEFx4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
97 xnn_prefetch_to_l1((const int8_t*) w + 896);
98
99 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
100 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
101 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
102 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
103 vacc2x0123 = _mm256_dpbusd_avx_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
104 vacc2x4567 = _mm256_dpbusd_avx_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
105 xnn_prefetch_to_l1((const int8_t*) w + 960);
106 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x89ABCDEF, vb01234567x4567);
107 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
108 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x89ABCDEF, vb01234567x4567);
109 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x89ABCDEF, vb89ABCDEFx4567);
110 vacc2x0123 = _mm256_dpbusd_avx_epi32(vacc2x0123, va2x89ABCDEF, vb01234567x4567);
111 vacc2x4567 = _mm256_dpbusd_avx_epi32(vacc2x4567, va2x89ABCDEF, vb89ABCDEFx4567);
112
113 w = (const int8_t*) w + 128;
114 k -= 16 * sizeof(int8_t);
115 }
116
117 if (k != 0) {
118 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
119 a0 += 8;
120 const __m256i va1x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a1)), vsign_mask);
121 a1 += 8;
122 const __m256i va2x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a2)), vsign_mask);
123 a2 += 8;
124
125 const __m256i vb01234567x0123 = _mm256_load_si256(w);
126 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
127
128 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
129 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
130 vacc1x0123 = _mm256_dpbusd_avx_epi32(vacc1x0123, va1x01234567, vb01234567x0123);
131 vacc1x4567 = _mm256_dpbusd_avx_epi32(vacc1x4567, va1x01234567, vb89ABCDEFx0123);
132 vacc2x0123 = _mm256_dpbusd_avx_epi32(vacc2x0123, va2x01234567, vb01234567x0123);
133 vacc2x4567 = _mm256_dpbusd_avx_epi32(vacc2x4567, va2x01234567, vb89ABCDEFx0123);
134 xnn_prefetch_to_l1((const int8_t*) w + 960);
135
136 w = (const int8_t*) w + 64;
137 k -= 8 * sizeof(int8_t);
138 }
139
140 // Add adjacent pairs
141 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
142 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
143 const __m256i vsum1x02134657 = _mm256_hadd_epi32(vacc1x0123, vacc1x4567);
144 __m256i vacc1x01234567 = _mm256_permute4x64_epi64(vsum1x02134657, _MM_SHUFFLE(3, 1, 2, 0));
145 const __m256i vsum2x02134657 = _mm256_hadd_epi32(vacc2x0123, vacc2x4567);
146 __m256i vacc2x01234567 = _mm256_permute4x64_epi64(vsum2x02134657, _MM_SHUFFLE(3, 1, 2, 0));
147
148 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
149 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
150 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
151
152 vout0x01234567 = _mm256_mul_ps(vout0x01234567, _mm256_set1_ps(quantization_params[0].inv_scale));
153 vout1x01234567 = _mm256_mul_ps(vout1x01234567, _mm256_set1_ps(quantization_params[1].inv_scale));
154 vout2x01234567 = _mm256_mul_ps(vout2x01234567, _mm256_set1_ps(quantization_params[2].inv_scale));
155
156 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
157 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
158 w = (const float*) w + 16;
159
160 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
161 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
162 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
163
164 vout0x01234567 = _mm256_max_ps(vout0x01234567, voutput_min);
165 vout1x01234567 = _mm256_max_ps(vout1x01234567, voutput_min);
166 vout2x01234567 = _mm256_max_ps(vout2x01234567, voutput_min);
167
168 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max);
169 vout1x01234567 = _mm256_min_ps(vout1x01234567, voutput_max);
170 vout2x01234567 = _mm256_min_ps(vout2x01234567, voutput_max);
171
172 if XNN_LIKELY(nc >= 8) {
173 _mm256_storeu_ps(c0, vout0x01234567);
174 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
175 c0 = (float*) ((uintptr_t) c0 + cn_stride);
176 _mm256_storeu_ps(c1, vout1x01234567);
177 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
178 c1 = (float*) ((uintptr_t) c1 + cn_stride);
179 _mm256_storeu_ps(c2, vout2x01234567);
180 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
181 c2 = (float*) ((uintptr_t) c2 + cn_stride);
182 nc -= 8;
183 } else {
184 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
185 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
186 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
187 if (nc & 4) {
188 _mm_storeu_ps(c0, vout0x0123);
189 c0 += 4;
190 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
191 _mm_storeu_ps(c1, vout1x0123);
192 c1 += 4;
193 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
194 _mm_storeu_ps(c2, vout2x0123);
195 c2 += 4;
196 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
197 }
198 if (nc & 2) {
199 _mm_storel_pi((__m64*) c0, vout0x0123);
200 c0 += 2;
201 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
202 _mm_storel_pi((__m64*) c1, vout1x0123);
203 c1 += 2;
204 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
205 _mm_storel_pi((__m64*) c2, vout2x0123);
206 c2 += 2;
207 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
208 }
209 if (nc & 1) {
210 _mm_store_ss(c0, vout0x0123);
211 _mm_store_ss(c1, vout1x0123);
212 _mm_store_ss(c2, vout2x0123);
213 }
214 nc = 0;
215 }
216 } while (nc != 0);
217 }