Debian Codegraph @

src/qd8-f16-qc8w-igemm/gen/qd8-f16-qc8w-igemm-3x8c8-minmax-avx2.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/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/igemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18
19
20 void xnn_qd8_f16_qc8w_igemm_minmax_ukernel_3x8c8__avx2(
21 size_t mr,
22 size_t nc,
23 size_t kc,
24 size_t ks,
25 const int8_t** restrict a,
26 const void* restrict w,
27 xnn_float16* restrict c,
28 size_t cm_stride,
29 size_t cn_stride,
30 size_t a_offset,
31 const int8_t* zero,
32 const int8_t* zero_data,
33 const union xnn_f16_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(ks != 0);
41 assert(ks % (3 * sizeof(void*)) == 0);
42 assert(a_offset % sizeof(int8_t) == 0);
43 assert(a != NULL);
44 assert(w != NULL);
45 assert(c != NULL);
46
47 kc = round_up_po2(kc, 8 * sizeof(int8_t));
48 uint16_t* c0 = (uint16_t*) c;
49 uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
50 if XNN_UNPREDICTABLE(mr < 2) {
51 c1 = c0;
52 }
53 uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
54 if XNN_UNPREDICTABLE(mr <= 2) {
55 c2 = c1;
56 }
57
58 const __m256i vinput_zero_point = _mm256_set1_epi32((int) quantization_params->zero_point);
59 const __m256 vinput_scale = _mm256_broadcast_ss(&quantization_params->inv_scale);
60 const __m256 vmin = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
61 const __m256 vmax = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
62 // XNN_FORCE_REALIZATION(vinput_zero_point);
63 // XNN_FORCE_REALIZATION(vinput_scale);
64 // XNN_FORCE_REALIZATION(vmin);
65 // XNN_FORCE_REALIZATION(vmax);
66
67 do {
68 const __m128i vinit0 = _mm_cvtsi32_si128(((const int*) w)[0]);
69 const __m128i vinit1 = _mm_cvtsi32_si128(((const int*) w)[1]);
70 const __m256i vinit01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit0), vinit1, 1);
71 const __m128i vinit2 = _mm_cvtsi32_si128(((const int*) w)[2]);
72 const __m128i vinit3 = _mm_cvtsi32_si128(((const int*) w)[3]);
73 const __m256i vinit23 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit2), vinit3, 1);
74 const __m128i vinit4 = _mm_cvtsi32_si128(((const int*) w)[4]);
75 const __m128i vinit5 = _mm_cvtsi32_si128(((const int*) w)[5]);
76 const __m256i vinit45 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit4), vinit5, 1);
77 const __m128i vinit6 = _mm_cvtsi32_si128(((const int*) w)[6]);
78 const __m128i vinit7 = _mm_cvtsi32_si128(((const int*) w)[7]);
79 const __m256i vinit67 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit6), vinit7, 1);
80 __m256i vacc0x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point);
81 __m256i vacc0x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point);
82 __m256i vacc0x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point);
83 __m256i vacc0x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point);
84 __m256i vacc1x01 = vacc0x01;
85 __m256i vacc1x23 = vacc0x23;
86 __m256i vacc1x45 = vacc0x45;
87 __m256i vacc1x67 = vacc0x67;
88 __m256i vacc2x01 = vacc0x01;
89 __m256i vacc2x23 = vacc0x23;
90 __m256i vacc2x45 = vacc0x45;
91 __m256i vacc2x67 = vacc0x67;
92 w = (const int32_t*) w + 8;
93
94 size_t p = ks;
95 do {
96 const int8_t* restrict a0 = a[0];
97 if XNN_UNPREDICTABLE(a0 != zero) {
98 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
99 } else {
100 a0 = zero_data;
101 }
102 const int8_t* restrict a1 = a[1];
103 if XNN_UNPREDICTABLE(a1 != zero) {
104 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
105 } else {
106 a1 = zero_data;
107 }
108 const int8_t* restrict a2 = a[2];
109 if XNN_UNPREDICTABLE(a2 != zero) {
110 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
111 } else {
112 a2 = zero_data;
113 }
114 a += 3;
115
116 size_t k = 0;
117 while (k < kc) {
118 const __m128i va0 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a0));
119 const __m256i vxa0 = _mm256_cvtepi8_epi16(va0);
120 a0 += 8;
121 const __m128i va1 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a1));
122 const __m256i vxa1 = _mm256_cvtepi8_epi16(va1);
123 a1 += 8;
124 const __m128i va2 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a2));
125 const __m256i vxa2 = _mm256_cvtepi8_epi16(va2);
126 a2 += 8;
127
128 const __m128i vb01 = _mm_load_si128((const __m128i*) w);
129 const __m256i vxb01 = _mm256_cvtepi8_epi16(vb01);
130
131 vacc0x01 = _mm256_add_epi32(vacc0x01, _mm256_madd_epi16(vxa0, vxb01));
132 vacc1x01 = _mm256_add_epi32(vacc1x01, _mm256_madd_epi16(vxa1, vxb01));
133 vacc2x01 = _mm256_add_epi32(vacc2x01, _mm256_madd_epi16(vxa2, vxb01));
134 const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
135 const __m256i vxb23 = _mm256_cvtepi8_epi16(vb23);
136
137 vacc0x23 = _mm256_add_epi32(vacc0x23, _mm256_madd_epi16(vxa0, vxb23));
138 vacc1x23 = _mm256_add_epi32(vacc1x23, _mm256_madd_epi16(vxa1, vxb23));
139 vacc2x23 = _mm256_add_epi32(vacc2x23, _mm256_madd_epi16(vxa2, vxb23));
140 const __m128i vb45 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 32));
141 const __m256i vxb45 = _mm256_cvtepi8_epi16(vb45);
142
143 vacc0x45 = _mm256_add_epi32(vacc0x45, _mm256_madd_epi16(vxa0, vxb45));
144 vacc1x45 = _mm256_add_epi32(vacc1x45, _mm256_madd_epi16(vxa1, vxb45));
145 vacc2x45 = _mm256_add_epi32(vacc2x45, _mm256_madd_epi16(vxa2, vxb45));
146 const __m128i vb67 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 48));
147 const __m256i vxb67 = _mm256_cvtepi8_epi16(vb67);
148
149 vacc0x67 = _mm256_add_epi32(vacc0x67, _mm256_madd_epi16(vxa0, vxb67));
150 vacc1x67 = _mm256_add_epi32(vacc1x67, _mm256_madd_epi16(vxa1, vxb67));
151 vacc2x67 = _mm256_add_epi32(vacc2x67, _mm256_madd_epi16(vxa2, vxb67));
152
153 w = (const void*) ((const int8_t*) w + 64);
154 k += 8 * sizeof(int8_t);
155 }
156 p -= 3 * sizeof(void*);
157 } while (p != 0);
158
159 const __m256i vacc0x0213 = _mm256_hadd_epi32(vacc0x01, vacc0x23);
160 const __m256i vacc0x4657 = _mm256_hadd_epi32(vacc0x45, vacc0x67);
161 const __m256i vacc1x0213 = _mm256_hadd_epi32(vacc1x01, vacc1x23);
162 const __m256i vacc1x4657 = _mm256_hadd_epi32(vacc1x45, vacc1x67);
163 const __m256i vacc2x0213 = _mm256_hadd_epi32(vacc2x01, vacc2x23);
164 const __m256i vacc2x4657 = _mm256_hadd_epi32(vacc2x45, vacc2x67);
165
166 const __m256i vacc0x02461357 = _mm256_hadd_epi32(vacc0x0213, vacc0x4657);
167 const __m256i vacc1x02461357 = _mm256_hadd_epi32(vacc1x0213, vacc1x4657);
168 const __m256i vacc2x02461357 = _mm256_hadd_epi32(vacc2x0213, vacc2x4657);
169
170 const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
171 __m256i vacc0x01234567 = _mm256_permutevar8x32_epi32(vacc0x02461357, vpermute_mask);
172 __m256i vacc1x01234567 = _mm256_permutevar8x32_epi32(vacc1x02461357, vpermute_mask);
173 __m256i vacc2x01234567 = _mm256_permutevar8x32_epi32(vacc2x02461357, vpermute_mask);
174
175 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
176 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
177 __m256 vout2x01234567 = _mm256_cvtepi32_ps(vacc2x01234567);
178
179 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vinput_scale);
180 vout1x01234567 = _mm256_mul_ps(vout1x01234567, vinput_scale);
181 vout2x01234567 = _mm256_mul_ps(vout2x01234567, vinput_scale);
182
183 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
184 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
185 w = (const float*) w + 16;
186 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
187 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
188 vout2x01234567 = _mm256_fmadd_ps(vout2x01234567, vfilter_output_scale01234567, vbias01234567);
189
190 vout2x01234567 = _mm256_max_ps(vout2x01234567, vmin);
191 vout1x01234567 = _mm256_max_ps(vout1x01234567, vmin);
192 vout0x01234567 = _mm256_max_ps(vout0x01234567, vmin);
193
194 vout2x01234567 = _mm256_min_ps(vout2x01234567, vmax);
195 vout1x01234567 = _mm256_min_ps(vout1x01234567, vmax);
196 vout0x01234567 = _mm256_min_ps(vout0x01234567, vmax);
197 __m128i vfp16out2x01234567 = _mm256_cvtps_ph(vout2x01234567, _MM_FROUND_TO_NEAREST_INT);
198 __m128i vfp16out1x01234567 = _mm256_cvtps_ph(vout1x01234567, _MM_FROUND_TO_NEAREST_INT);
199 __m128i vfp16out0x01234567 = _mm256_cvtps_ph(vout0x01234567, _MM_FROUND_TO_NEAREST_INT);
200 if XNN_LIKELY(nc >= 8) {
201 _mm_storeu_si128((__m128i*) c2, vfp16out2x01234567);
202 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
203 _mm_storeu_si128((__m128i*) c1, vfp16out1x01234567);
204 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
205 _mm_storeu_si128((__m128i*) c0, vfp16out0x01234567);
206 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
207
208 a = (const int8_t**restrict) ((uintptr_t) a - ks);
209 nc -= 8;
210 } else {
211 if (nc & 4) {
212 _mm_storel_epi64((__m128i*) c2, vfp16out2x01234567);
213 _mm_storel_epi64((__m128i*) c1, vfp16out1x01234567);
214 _mm_storel_epi64((__m128i*) c0, vfp16out0x01234567);
215
216 vfp16out2x01234567 = _mm_unpackhi_epi64(vfp16out2x01234567, vfp16out2x01234567);
217 vfp16out1x01234567 = _mm_unpackhi_epi64(vfp16out1x01234567, vfp16out1x01234567);
218 vfp16out0x01234567 = _mm_unpackhi_epi64(vfp16out0x01234567, vfp16out0x01234567);
219
220 c2 += 4;
221 c1 += 4;
222 c0 += 4;
223 }
224 if (nc & 2) {
225 _mm_storeu_si32(c2, vfp16out2x01234567);
226 _mm_storeu_si32(c1, vfp16out1x01234567);
227 _mm_storeu_si32(c0, vfp16out0x01234567);
228
229 vfp16out2x01234567 = _mm_srli_epi64(vfp16out2x01234567, 32);
230 vfp16out1x01234567 = _mm_srli_epi64(vfp16out1x01234567, 32);
231 vfp16out0x01234567 = _mm_srli_epi64(vfp16out0x01234567, 32);
232
233 c2 += 2;
234 c1 += 2;
235 c0 += 2;
236 }
237 if (nc & 1) {
238 *c2 = (uint16_t) _mm_extract_epi16(vfp16out2x01234567, 0);
239 *c1 = (uint16_t) _mm_extract_epi16(vfp16out1x01234567, 0);
240 *c0 = (uint16_t) _mm_extract_epi16(vfp16out0x01234567, 0);
241 }
242 nc = 0;
243 }
244 } while (nc != 0);
245 }