Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-3x8c8-minmax-avx2.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx8c8-avx2.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 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/common.h"
15 #include "xnnpack/gemm.h"
16 #include "xnnpack/intrinsics-polyfill.h"
17 #include "xnnpack/math.h"
18 #include "xnnpack/unaligned.h"
19
20
21 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_3x8c8__avx2(
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 __m256 vmin = _mm256_set1_ps(params->scalar.min);
60 const __m256 vmax = _mm256_set1_ps(params->scalar.max);
61 XNN_FORCE_REALIZATION(vmin);
62 XNN_FORCE_REALIZATION(vmax);
63
64 do {
65 const __m128i vinit0 = _mm_cvtsi32_si128(((const int*) w)[0]);
66 const __m128i vinit1 = _mm_cvtsi32_si128(((const int*) w)[1]);
67 const __m256i vinit01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit0), vinit1, 1);
68 const __m128i vinit2 = _mm_cvtsi32_si128(((const int*) w)[2]);
69 const __m128i vinit3 = _mm_cvtsi32_si128(((const int*) w)[3]);
70 const __m256i vinit23 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit2), vinit3, 1);
71 const __m128i vinit4 = _mm_cvtsi32_si128(((const int*) w)[4]);
72 const __m128i vinit5 = _mm_cvtsi32_si128(((const int*) w)[5]);
73 const __m256i vinit45 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit4), vinit5, 1);
74 const __m128i vinit6 = _mm_cvtsi32_si128(((const int*) w)[6]);
75 const __m128i vinit7 = _mm_cvtsi32_si128(((const int*) w)[7]);
76 const __m256i vinit67 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit6), vinit7, 1);
77 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point);
78 __m256i vacc0x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point0);
79 __m256i vacc0x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point0);
80 __m256i vacc0x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point0);
81 __m256i vacc0x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point0);
82 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point);
83 __m256i vacc1x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point1);
84 __m256i vacc1x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point1);
85 __m256i vacc1x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point1);
86 __m256i vacc1x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point1);
87 const __m256i vinput_zero_point2 = _mm256_set1_epi32((int) quantization_params[2].zero_point);
88 __m256i vacc2x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point2);
89 __m256i vacc2x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point2);
90 __m256i vacc2x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point2);
91 __m256i vacc2x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point2);
92 w = (const int32_t*) w + 8;
93
94 size_t k = kc;
95
96 while (k >= 8 * sizeof(int8_t)) {
97 const __m128i va0 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a0));
98 const __m256i vxa0 = _mm256_cvtepi8_epi16(va0);
99 a0 += 8;
100 const __m128i va1 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a1));
101 const __m256i vxa1 = _mm256_cvtepi8_epi16(va1);
102 a1 += 8;
103 const __m128i va2 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a2));
104 const __m256i vxa2 = _mm256_cvtepi8_epi16(va2);
105 a2 += 8;
106
107 const __m256i vxb01 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) w));
108
109 vacc0x01 = _mm256_add_epi32(vacc0x01, _mm256_madd_epi16(vxa0, vxb01));
110 vacc1x01 = _mm256_add_epi32(vacc1x01, _mm256_madd_epi16(vxa1, vxb01));
111 vacc2x01 = _mm256_add_epi32(vacc2x01, _mm256_madd_epi16(vxa2, vxb01));
112 const __m256i vxb23 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) ((const int8_t*) w + 16)));
113
114 vacc0x23 = _mm256_add_epi32(vacc0x23, _mm256_madd_epi16(vxa0, vxb23));
115 vacc1x23 = _mm256_add_epi32(vacc1x23, _mm256_madd_epi16(vxa1, vxb23));
116 vacc2x23 = _mm256_add_epi32(vacc2x23, _mm256_madd_epi16(vxa2, vxb23));
117 const __m256i vxb45 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) ((const int8_t*) w + 32)));
118
119 vacc0x45 = _mm256_add_epi32(vacc0x45, _mm256_madd_epi16(vxa0, vxb45));
120 vacc1x45 = _mm256_add_epi32(vacc1x45, _mm256_madd_epi16(vxa1, vxb45));
121 vacc2x45 = _mm256_add_epi32(vacc2x45, _mm256_madd_epi16(vxa2, vxb45));
122 const __m256i vxb67 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) ((const int8_t*) w + 48)));
123
124 vacc0x67 = _mm256_add_epi32(vacc0x67, _mm256_madd_epi16(vxa0, vxb67));
125 vacc1x67 = _mm256_add_epi32(vacc1x67, _mm256_madd_epi16(vxa1, vxb67));
126 vacc2x67 = _mm256_add_epi32(vacc2x67, _mm256_madd_epi16(vxa2, vxb67));
127
128 w = (const int8_t*) w + 64;
129 k -= 8 * sizeof(int8_t);
130 }
131
132 const __m256i vacc0x0213 = _mm256_hadd_epi32(vacc0x01, vacc0x23);
133 const __m256i vacc0x4657 = _mm256_hadd_epi32(vacc0x45, vacc0x67);
134 const __m256i vacc1x0213 = _mm256_hadd_epi32(vacc1x01, vacc1x23);
135 const __m256i vacc1x4657 = _mm256_hadd_epi32(vacc1x45, vacc1x67);
136 const __m256i vacc2x0213 = _mm256_hadd_epi32(vacc2x01, vacc2x23);
137 const __m256i vacc2x4657 = _mm256_hadd_epi32(vacc2x45, vacc2x67);
138
139 const __m256i vacc0x02461357 = _mm256_hadd_epi32(vacc0x0213, vacc0x4657);
140 const __m256i vacc1x02461357 = _mm256_hadd_epi32(vacc1x0213, vacc1x4657);
141 const __m256i vacc2x02461357 = _mm256_hadd_epi32(vacc2x0213, vacc2x4657);
142
143 const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
144 __m256i vacc0x01234567 = _mm256_permutevar8x32_epi32(vacc0x02461357, vpermute_mask);
145 __m256i vacc1x01234567 = _mm256_permutevar8x32_epi32(vacc1x02461357, vpermute_mask);
146 __m256i vacc2x01234567 = _mm256_permutevar8x32_epi32(vacc2x02461357, vpermute_mask);
147
148 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
149 const __m256 vinput_scale0 = _mm256_broadcast_ss(&quantization_params[0].inv_scale);
150 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
151 const __m256 vinput_scale1 = _mm256_broadcast_ss(&quantization_params[1].inv_scale);
152 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
153 const __m256 vinput_scale2 = _mm256_broadcast_ss(&quantization_params[2].inv_scale);
154
155 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vinput_scale0);
156 vout1x01234567 = _mm256_mul_ps(vout1x01234567, vinput_scale1);
157 vout2x01234567 = _mm256_mul_ps(vout2x01234567, vinput_scale2);
158
159 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
160 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
161 w = (const float*) w + 16;
162 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
163 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
164 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
165
166 vout0x01234567 = _mm256_max_ps(vout0x01234567, vmin);
167 vout1x01234567 = _mm256_max_ps(vout1x01234567, vmin);
168 vout2x01234567 = _mm256_max_ps(vout2x01234567, vmin);
169
170 vout0x01234567 = _mm256_min_ps(vout0x01234567, vmax);
171 vout1x01234567 = _mm256_min_ps(vout1x01234567, vmax);
172 vout2x01234567 = _mm256_min_ps(vout2x01234567, vmax);
173
174 if XNN_LIKELY(nc >= 8) {
175 _mm256_storeu_ps(c0, vout0x01234567);
176 c0 = (float*) ((uintptr_t) c0 + cn_stride);
177 _mm256_storeu_ps(c1, vout1x01234567);
178 c1 = (float*) ((uintptr_t) c1 + cn_stride);
179 _mm256_storeu_ps(c2, vout2x01234567);
180 c2 = (float*) ((uintptr_t) c2 + cn_stride);
181
182 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
183 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
184 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
185
186 nc -= 8;
187 } else {
188 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
189 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
190 __m128 vout2x0123 = _mm256_castps256_ps128(vout2x01234567);
191 if (nc & 4) {
192 _mm_storeu_ps(c0, vout0x0123);
193 _mm_storeu_ps(c1, vout1x0123);
194 _mm_storeu_ps(c2, vout2x0123);
195
196 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
197 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
198 vout2x0123 = _mm256_extractf128_ps(vout2x01234567, 1);
199
200 c0 += 4;
201 c1 += 4;
202 c2 += 4;
203 }
204 if (nc & 2) {
205 _mm_storel_pi((__m64*) c0, vout0x0123);
206 _mm_storel_pi((__m64*) c1, vout1x0123);
207 _mm_storel_pi((__m64*) c2, vout2x0123);
208
209 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
210 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
211 vout2x0123 = _mm_movehl_ps(vout2x0123, vout2x0123);
212
213 c0 += 2;
214 c1 += 2;
215 c2 += 2;
216 }
217 if (nc & 1) {
218 _mm_store_ss(c0, vout0x0123);
219 _mm_store_ss(c1, vout1x0123);
220 _mm_store_ss(c2, vout2x0123);
221 }
222 nc = 0;
223 }
224 } while (nc != 0);
225 }