Debian Codegraph @

src/qd8-f16-qc8w-igemm/gen/qd8-f16-qc8w-igemm-2x8c8-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_2x8c8__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 <= 2);
38 assert(nc != 0);
39 assert(kc != 0);
40 assert(ks != 0);
41 assert(ks % (2 * 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
54 const __m256i vinput_zero_point = _mm256_set1_epi32((int) quantization_params->zero_point);
55 const __m256 vinput_scale = _mm256_broadcast_ss(&quantization_params->inv_scale);
56 const __m256 vmin = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
57 const __m256 vmax = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
58 // XNN_FORCE_REALIZATION(vinput_zero_point);
59 // XNN_FORCE_REALIZATION(vinput_scale);
60 // XNN_FORCE_REALIZATION(vmin);
61 // XNN_FORCE_REALIZATION(vmax);
62
63 do {
64 const __m128i vinit0 = _mm_cvtsi32_si128(((const int*) w)[0]);
65 const __m128i vinit1 = _mm_cvtsi32_si128(((const int*) w)[1]);
66 const __m256i vinit01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit0), vinit1, 1);
67 const __m128i vinit2 = _mm_cvtsi32_si128(((const int*) w)[2]);
68 const __m128i vinit3 = _mm_cvtsi32_si128(((const int*) w)[3]);
69 const __m256i vinit23 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit2), vinit3, 1);
70 const __m128i vinit4 = _mm_cvtsi32_si128(((const int*) w)[4]);
71 const __m128i vinit5 = _mm_cvtsi32_si128(((const int*) w)[5]);
72 const __m256i vinit45 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit4), vinit5, 1);
73 const __m128i vinit6 = _mm_cvtsi32_si128(((const int*) w)[6]);
74 const __m128i vinit7 = _mm_cvtsi32_si128(((const int*) w)[7]);
75 const __m256i vinit67 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit6), vinit7, 1);
76 __m256i vacc0x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point);
77 __m256i vacc0x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point);
78 __m256i vacc0x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point);
79 __m256i vacc0x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point);
80 __m256i vacc1x01 = vacc0x01;
81 __m256i vacc1x23 = vacc0x23;
82 __m256i vacc1x45 = vacc0x45;
83 __m256i vacc1x67 = vacc0x67;
84 w = (const int32_t*) w + 8;
85
86 size_t p = ks;
87 do {
88 const int8_t* restrict a0 = a[0];
89 if XNN_UNPREDICTABLE(a0 != zero) {
90 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
91 } else {
92 a0 = zero_data;
93 }
94 const int8_t* restrict a1 = a[1];
95 if XNN_UNPREDICTABLE(a1 != zero) {
96 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
97 } else {
98 a1 = zero_data;
99 }
100 a += 2;
101
102 size_t k = 0;
103 while (k < kc) {
104 const __m128i va0 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a0));
105 const __m256i vxa0 = _mm256_cvtepi8_epi16(va0);
106 a0 += 8;
107 const __m128i va1 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a1));
108 const __m256i vxa1 = _mm256_cvtepi8_epi16(va1);
109 a1 += 8;
110
111 const __m128i vb01 = _mm_load_si128((const __m128i*) w);
112 const __m256i vxb01 = _mm256_cvtepi8_epi16(vb01);
113
114 vacc0x01 = _mm256_add_epi32(vacc0x01, _mm256_madd_epi16(vxa0, vxb01));
115 vacc1x01 = _mm256_add_epi32(vacc1x01, _mm256_madd_epi16(vxa1, vxb01));
116 const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
117 const __m256i vxb23 = _mm256_cvtepi8_epi16(vb23);
118
119 vacc0x23 = _mm256_add_epi32(vacc0x23, _mm256_madd_epi16(vxa0, vxb23));
120 vacc1x23 = _mm256_add_epi32(vacc1x23, _mm256_madd_epi16(vxa1, vxb23));
121 const __m128i vb45 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 32));
122 const __m256i vxb45 = _mm256_cvtepi8_epi16(vb45);
123
124 vacc0x45 = _mm256_add_epi32(vacc0x45, _mm256_madd_epi16(vxa0, vxb45));
125 vacc1x45 = _mm256_add_epi32(vacc1x45, _mm256_madd_epi16(vxa1, vxb45));
126 const __m128i vb67 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 48));
127 const __m256i vxb67 = _mm256_cvtepi8_epi16(vb67);
128
129 vacc0x67 = _mm256_add_epi32(vacc0x67, _mm256_madd_epi16(vxa0, vxb67));
130 vacc1x67 = _mm256_add_epi32(vacc1x67, _mm256_madd_epi16(vxa1, vxb67));
131
132 w = (const void*) ((const int8_t*) w + 64);
133 k += 8 * sizeof(int8_t);
134 }
135 p -= 2 * sizeof(void*);
136 } while (p != 0);
137
138 const __m256i vacc0x0213 = _mm256_hadd_epi32(vacc0x01, vacc0x23);
139 const __m256i vacc0x4657 = _mm256_hadd_epi32(vacc0x45, vacc0x67);
140 const __m256i vacc1x0213 = _mm256_hadd_epi32(vacc1x01, vacc1x23);
141 const __m256i vacc1x4657 = _mm256_hadd_epi32(vacc1x45, vacc1x67);
142
143 const __m256i vacc0x02461357 = _mm256_hadd_epi32(vacc0x0213, vacc0x4657);
144 const __m256i vacc1x02461357 = _mm256_hadd_epi32(vacc1x0213, vacc1x4657);
145
146 const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
147 __m256i vacc0x01234567 = _mm256_permutevar8x32_epi32(vacc0x02461357, vpermute_mask);
148 __m256i vacc1x01234567 = _mm256_permutevar8x32_epi32(vacc1x02461357, vpermute_mask);
149
150 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
151 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
152
153 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vinput_scale);
154 vout1x01234567 = _mm256_mul_ps(vout1x01234567, vinput_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 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
160 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
161
162 vout1x01234567 = _mm256_max_ps(vout1x01234567, vmin);
163 vout0x01234567 = _mm256_max_ps(vout0x01234567, vmin);
164
165 vout1x01234567 = _mm256_min_ps(vout1x01234567, vmax);
166 vout0x01234567 = _mm256_min_ps(vout0x01234567, vmax);
167 __m128i vfp16out1x01234567 = _mm256_cvtps_ph(vout1x01234567, _MM_FROUND_TO_NEAREST_INT);
168 __m128i vfp16out0x01234567 = _mm256_cvtps_ph(vout0x01234567, _MM_FROUND_TO_NEAREST_INT);
169 if XNN_LIKELY(nc >= 8) {
170 _mm_storeu_si128((__m128i*) c1, vfp16out1x01234567);
171 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
172 _mm_storeu_si128((__m128i*) c0, vfp16out0x01234567);
173 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
174
175 a = (const int8_t**restrict) ((uintptr_t) a - ks);
176 nc -= 8;
177 } else {
178 if (nc & 4) {
179 _mm_storel_epi64((__m128i*) c1, vfp16out1x01234567);
180 _mm_storel_epi64((__m128i*) c0, vfp16out0x01234567);
181
182 vfp16out1x01234567 = _mm_unpackhi_epi64(vfp16out1x01234567, vfp16out1x01234567);
183 vfp16out0x01234567 = _mm_unpackhi_epi64(vfp16out0x01234567, vfp16out0x01234567);
184
185 c1 += 4;
186 c0 += 4;
187 }
188 if (nc & 2) {
189 _mm_storeu_si32(c1, vfp16out1x01234567);
190 _mm_storeu_si32(c0, vfp16out0x01234567);
191
192 vfp16out1x01234567 = _mm_srli_epi64(vfp16out1x01234567, 32);
193 vfp16out0x01234567 = _mm_srli_epi64(vfp16out0x01234567, 32);
194
195 c1 += 2;
196 c0 += 2;
197 }
198 if (nc & 1) {
199 *c1 = (uint16_t) _mm_extract_epi16(vfp16out1x01234567, 0);
200 *c0 = (uint16_t) _mm_extract_epi16(vfp16out0x01234567, 0);
201 }
202 nc = 0;
203 }
204 } while (nc != 0);
205 }