Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-2x8c8-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_2x8c8__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 <= 2);
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
53 const __m256 vmin = _mm256_set1_ps(params->scalar.min);
54 const __m256 vmax = _mm256_set1_ps(params->scalar.max);
55 XNN_FORCE_REALIZATION(vmin);
56 XNN_FORCE_REALIZATION(vmax);
57
58 do {
59 const __m128i vinit0 = _mm_cvtsi32_si128(((const int*) w)[0]);
60 const __m128i vinit1 = _mm_cvtsi32_si128(((const int*) w)[1]);
61 const __m256i vinit01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit0), vinit1, 1);
62 const __m128i vinit2 = _mm_cvtsi32_si128(((const int*) w)[2]);
63 const __m128i vinit3 = _mm_cvtsi32_si128(((const int*) w)[3]);
64 const __m256i vinit23 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit2), vinit3, 1);
65 const __m128i vinit4 = _mm_cvtsi32_si128(((const int*) w)[4]);
66 const __m128i vinit5 = _mm_cvtsi32_si128(((const int*) w)[5]);
67 const __m256i vinit45 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit4), vinit5, 1);
68 const __m128i vinit6 = _mm_cvtsi32_si128(((const int*) w)[6]);
69 const __m128i vinit7 = _mm_cvtsi32_si128(((const int*) w)[7]);
70 const __m256i vinit67 = _mm256_inserti128_si256(_mm256_castsi128_si256(vinit6), vinit7, 1);
71 const __m256i vinput_zero_point0 = _mm256_set1_epi32((int) quantization_params[0].zero_point);
72 __m256i vacc0x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point0);
73 __m256i vacc0x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point0);
74 __m256i vacc0x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point0);
75 __m256i vacc0x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point0);
76 const __m256i vinput_zero_point1 = _mm256_set1_epi32((int) quantization_params[1].zero_point);
77 __m256i vacc1x01 = _mm256_mullo_epi32(vinit01, vinput_zero_point1);
78 __m256i vacc1x23 = _mm256_mullo_epi32(vinit23, vinput_zero_point1);
79 __m256i vacc1x45 = _mm256_mullo_epi32(vinit45, vinput_zero_point1);
80 __m256i vacc1x67 = _mm256_mullo_epi32(vinit67, vinput_zero_point1);
81 w = (const int32_t*) w + 8;
82
83 size_t k = kc;
84
85 while (k >= 8 * sizeof(int8_t)) {
86 const __m128i va0 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a0));
87 const __m256i vxa0 = _mm256_cvtepi8_epi16(va0);
88 a0 += 8;
89 const __m128i va1 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a1));
90 const __m256i vxa1 = _mm256_cvtepi8_epi16(va1);
91 a1 += 8;
92
93 const __m256i vxb01 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) w));
94
95 vacc0x01 = _mm256_add_epi32(vacc0x01, _mm256_madd_epi16(vxa0, vxb01));
96 vacc1x01 = _mm256_add_epi32(vacc1x01, _mm256_madd_epi16(vxa1, vxb01));
97 const __m256i vxb23 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) ((const int8_t*) w + 16)));
98
99 vacc0x23 = _mm256_add_epi32(vacc0x23, _mm256_madd_epi16(vxa0, vxb23));
100 vacc1x23 = _mm256_add_epi32(vacc1x23, _mm256_madd_epi16(vxa1, vxb23));
101 const __m256i vxb45 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) ((const int8_t*) w + 32)));
102
103 vacc0x45 = _mm256_add_epi32(vacc0x45, _mm256_madd_epi16(vxa0, vxb45));
104 vacc1x45 = _mm256_add_epi32(vacc1x45, _mm256_madd_epi16(vxa1, vxb45));
105 const __m256i vxb67 = _mm256_cvtepi8_epi16(_mm_load_si128((const __m128i*) ((const int8_t*) w + 48)));
106
107 vacc0x67 = _mm256_add_epi32(vacc0x67, _mm256_madd_epi16(vxa0, vxb67));
108 vacc1x67 = _mm256_add_epi32(vacc1x67, _mm256_madd_epi16(vxa1, vxb67));
109
110 w = (const int8_t*) w + 64;
111 k -= 8 * sizeof(int8_t);
112 }
113
114 const __m256i vacc0x0213 = _mm256_hadd_epi32(vacc0x01, vacc0x23);
115 const __m256i vacc0x4657 = _mm256_hadd_epi32(vacc0x45, vacc0x67);
116 const __m256i vacc1x0213 = _mm256_hadd_epi32(vacc1x01, vacc1x23);
117 const __m256i vacc1x4657 = _mm256_hadd_epi32(vacc1x45, vacc1x67);
118
119 const __m256i vacc0x02461357 = _mm256_hadd_epi32(vacc0x0213, vacc0x4657);
120 const __m256i vacc1x02461357 = _mm256_hadd_epi32(vacc1x0213, vacc1x4657);
121
122 const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
123 __m256i vacc0x01234567 = _mm256_permutevar8x32_epi32(vacc0x02461357, vpermute_mask);
124 __m256i vacc1x01234567 = _mm256_permutevar8x32_epi32(vacc1x02461357, vpermute_mask);
125
126 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
127 const __m256 vinput_scale0 = _mm256_broadcast_ss(&quantization_params[0].inv_scale);
128 __m256 vout1x01234567 = _mm256_cvtepi32_ps(vacc1x01234567);
129 const __m256 vinput_scale1 = _mm256_broadcast_ss(&quantization_params[1].inv_scale);
130
131 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vinput_scale0);
132 vout1x01234567 = _mm256_mul_ps(vout1x01234567, vinput_scale1);
133
134 const __m256 vfilter_output_scale01234567 = _mm256_load_ps((const float*) w);
135 const __m256 vbias01234567 = _mm256_load_ps((const float*) w + 8);
136 w = (const float*) w + 16;
137 vout0x01234567 = _mm256_fmadd_ps(vout0x01234567, vfilter_output_scale01234567, vbias01234567);
138 vout1x01234567 = _mm256_fmadd_ps(vout1x01234567, vfilter_output_scale01234567, vbias01234567);
139
140 vout0x01234567 = _mm256_max_ps(vout0x01234567, vmin);
141 vout1x01234567 = _mm256_max_ps(vout1x01234567, vmin);
142
143 vout0x01234567 = _mm256_min_ps(vout0x01234567, vmax);
144 vout1x01234567 = _mm256_min_ps(vout1x01234567, vmax);
145
146 if XNN_LIKELY(nc >= 8) {
147 _mm256_storeu_ps(c0, vout0x01234567);
148 c0 = (float*) ((uintptr_t) c0 + cn_stride);
149 _mm256_storeu_ps(c1, vout1x01234567);
150 c1 = (float*) ((uintptr_t) c1 + cn_stride);
151
152 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
153 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
154
155 nc -= 8;
156 } else {
157 __m128 vout0x0123 = _mm256_castps256_ps128(vout0x01234567);
158 __m128 vout1x0123 = _mm256_castps256_ps128(vout1x01234567);
159 if (nc & 4) {
160 _mm_storeu_ps(c0, vout0x0123);
161 _mm_storeu_ps(c1, vout1x0123);
162
163 vout0x0123 = _mm256_extractf128_ps(vout0x01234567, 1);
164 vout1x0123 = _mm256_extractf128_ps(vout1x01234567, 1);
165
166 c0 += 4;
167 c1 += 4;
168 }
169 if (nc & 2) {
170 _mm_storel_pi((__m64*) c0, vout0x0123);
171 _mm_storel_pi((__m64*) c1, vout1x0123);
172
173 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
174 vout1x0123 = _mm_movehl_ps(vout1x0123, vout1x0123);
175
176 c0 += 2;
177 c1 += 2;
178 }
179 if (nc & 1) {
180 _mm_store_ss(c0, vout0x0123);
181 _mm_store_ss(c1, vout1x0123);
182 }
183 nc = 0;
184 }
185 } while (nc != 0);
186 }