Elffuss BraidHub

kikocis/ncnn public

master · 155.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#if NCNN_RUNTIME_CPU && NCNN_AVX512VNNI && __AVX512F__ && !__AVX512VNNI__
16void lstm_transform_weight_int8_avx512vnni(const Mat& weight_xc, const Mat& weight_xc_int8_scales, const Mat& weight_hc, const Mat& weight_hc_int8_scales, const Mat& bias_c, Mat& weight_data_tm, Mat& weight_data_tm_int8_descales, Mat& bias_c_tm, int size, int num_output, int num_directions, int hidden_size, const Option& opt);
17void lstm_dynamic_quantize_scale2int8_avx512vnni(const float* ptr, int size, float scale, signed char* outptr);
18void lstm_int8_avx512vnni(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt);
19#endif
20
21#if NCNN_RUNTIME_CPU && NCNN_AVXVNNI && __AVX2__ && !__AVX512F__ && !__AVXVNNI__ && !__AVX512VNNI__
22void lstm_transform_weight_int8_avxvnni(const Mat& weight_xc, const Mat& weight_xc_int8_scales, const Mat& weight_hc, const Mat& weight_hc_int8_scales, const Mat& bias_c, Mat& weight_data_tm, Mat& weight_data_tm_int8_descales, Mat& bias_c_tm, int size, int num_output, int num_directions, int hidden_size, const Option& opt);
23void lstm_dynamic_quantize_scale2int8_avxvnni(const float* ptr, int size, float scale, signed char* outptr);
24void lstm_int8_avxvnni(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt);
25#endif
26
27#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__ && !__AVXVNNI__ && !__AVX512VNNI__
28void lstm_transform_weight_int8_avx2(const Mat& weight_xc, const Mat& weight_xc_int8_scales, const Mat& weight_hc, const Mat& weight_hc_int8_scales, const Mat& bias_c, Mat& weight_data_tm, Mat& weight_data_tm_int8_descales, Mat& bias_c_tm, int size, int num_output, int num_directions, int hidden_size, const Option& opt);
29void lstm_int8_avx2(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt);
30#endif
31
32#if NCNN_RUNTIME_CPU && NCNN_XOP && __SSE2__ && !__XOP__ && !__AVX2__ && !__AVXVNNI__ && !__AVX512VNNI__
33void lstm_int8_xop(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt);
34#endif
35
36static void lstm_transform_weight_int8(const Mat& weight_xc, const Mat& weight_xc_int8_scales, const Mat& weight_hc, const Mat& weight_hc_int8_scales, const Mat& bias_c, Mat& weight_data_tm, Mat& weight_data_tm_int8_descales, Mat& bias_c_tm, int size, int num_output, int num_directions, int hidden_size, const Option& opt)
37{
38#if NCNN_RUNTIME_CPU && NCNN_AVX512VNNI && __AVX512F__ && !__AVX512VNNI__
39 if (ncnn::cpu_support_x86_avx512_vnni())
40 {
41 lstm_transform_weight_int8_avx512vnni(weight_xc, weight_xc_int8_scales, weight_hc, weight_hc_int8_scales, bias_c, weight_data_tm, weight_data_tm_int8_descales, bias_c_tm, size, num_output, num_directions, hidden_size, opt);
42 return;
43 }
44#endif
45
46#if NCNN_RUNTIME_CPU && NCNN_AVXVNNI && __AVX2__ && !__AVX512F__ && !__AVXVNNI__ && !__AVX512VNNI__
47 if (ncnn::cpu_support_x86_avx_vnni())
48 {
49 lstm_transform_weight_int8_avxvnni(weight_xc, weight_xc_int8_scales, weight_hc, weight_hc_int8_scales, bias_c, weight_data_tm, weight_data_tm_int8_descales, bias_c_tm, size, num_output, num_directions, hidden_size, opt);
50 return;
51 }
52#endif
53
54#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__ && !__AVXVNNI__ && !__AVX512VNNI__
55 if (ncnn::cpu_support_x86_avx2())
56 {
57 lstm_transform_weight_int8_avx2(weight_xc, weight_xc_int8_scales, weight_hc, weight_hc_int8_scales, bias_c, weight_data_tm, weight_data_tm_int8_descales, bias_c_tm, size, num_output, num_directions, hidden_size, opt);
58 return;
59 }
60#endif
61
62#if __AVX512F__
63#if __AVX512VNNI__
64 weight_data_tm.create(size + 4 + num_output + 4, hidden_size / 4 + hidden_size % 4, num_directions, 16u, 16);
65#else
66 weight_data_tm.create(size + num_output, hidden_size / 4 + hidden_size % 4, num_directions, 16u, 16);
67#endif
68 weight_data_tm_int8_descales.create(16 + 16, hidden_size / 4 + hidden_size % 4, num_directions);
69#elif __AVX2__
70#if __AVXVNNI__
71 weight_data_tm.create(size + 4 + num_output + 4, hidden_size / 2 + hidden_size % 2, num_directions, 8u, 8);
72#else
73 weight_data_tm.create(size + num_output, hidden_size / 2 + hidden_size % 2, num_directions, 8u, 8);
74#endif
75 weight_data_tm_int8_descales.create(8 + 8, hidden_size / 2 + hidden_size % 2, num_directions);
76#else
77 weight_data_tm.create(size + num_output, hidden_size, num_directions, 4u, 4);
78 weight_data_tm_int8_descales.create(4 + 4, hidden_size, num_directions);
79#endif
80 bias_c_tm.create(hidden_size, 1, num_directions, 16u, 4);
81
82 #pragma omp parallel for num_threads(opt.num_threads)
83 for (int dr = 0; dr < num_directions; dr++)
84 {
85 const Mat weight_xc_dr = weight_xc.channel(dr);
86 const Mat weight_hc_dr = weight_hc.channel(dr);
87 const Mat bias_c_dr = bias_c.channel(dr);
88 const float* weight_xc_int8_scales_ptr = weight_xc_int8_scales.row(dr);
89 const float* weight_hc_int8_scales_ptr = weight_hc_int8_scales.row(dr);
90
91 Mat weight_data_tm_dr = weight_data_tm.channel(dr);
92 Mat bias_c_tm_dr = bias_c_tm.channel(dr);
93 Mat weight_data_tm_int8_descales_dr = weight_data_tm_int8_descales.channel(dr);
94
95 const float* bias_c_I = bias_c_dr.row(0);
96 const float* bias_c_F = bias_c_dr.row(1);
97 const float* bias_c_O = bias_c_dr.row(2);
98 const float* bias_c_G = bias_c_dr.row(3);
99
100 float* bias_c_IFOG = bias_c_tm_dr.row(0);
101
102 int q = 0;
103#if __AVX2__
104#if __AVX512F__
105 for (; q + 3 < hidden_size; q += 4)
106 {
107 _mm_storeu_ps(bias_c_IFOG, _mm_loadu_ps(bias_c_I + q));
108 _mm_storeu_ps(bias_c_IFOG + 4, _mm_loadu_ps(bias_c_F + q));
109 _mm_storeu_ps(bias_c_IFOG + 8, _mm_loadu_ps(bias_c_O + q));
110 _mm_storeu_ps(bias_c_IFOG + 12, _mm_loadu_ps(bias_c_G + q));
111 bias_c_IFOG += 16;
112
113 const signed char* weight_xc_I_0 = weight_xc_dr.row<const signed char>(hidden_size * 0 + q);
114 const signed char* weight_xc_F_0 = weight_xc_dr.row<const signed char>(hidden_size * 1 + q);
115 const signed char* weight_xc_O_0 = weight_xc_dr.row<const signed char>(hidden_size * 2 + q);
116 const signed char* weight_xc_G_0 = weight_xc_dr.row<const signed char>(hidden_size * 3 + q);
117 const signed char* weight_xc_I_1 = weight_xc_dr.row<const signed char>(hidden_size * 0 + q + 1);
118 const signed char* weight_xc_F_1 = weight_xc_dr.row<const signed char>(hidden_size * 1 + q + 1);
119 const signed char* weight_xc_O_1 = weight_xc_dr.row<const signed char>(hidden_size * 2 + q + 1);
120 const signed char* weight_xc_G_1 = weight_xc_dr.row<const signed char>(hidden_size * 3 + q + 1);
121 const signed char* weight_xc_I_2 = weight_xc_dr.row<const signed char>(hidden_size * 0 + q + 2);
122 const signed char* weight_xc_F_2 = weight_xc_dr.row<const signed char>(hidden_size * 1 + q + 2);
123 const signed char* weight_xc_O_2 = weight_xc_dr.row<const signed char>(hidden_size * 2 + q + 2);
124 const signed char* weight_xc_G_2 = weight_xc_dr.row<const signed char>(hidden_size * 3 + q + 2);
125 const signed char* weight_xc_I_3 = weight_xc_dr.row<const signed char>(hidden_size * 0 + q + 3);
126 const signed char* weight_xc_F_3 = weight_xc_dr.row<const signed char>(hidden_size * 1 + q + 3);
127 const signed char* weight_xc_O_3 = weight_xc_dr.row<const signed char>(hidden_size * 2 + q + 3);
128 const signed char* weight_xc_G_3 = weight_xc_dr.row<const signed char>(hidden_size * 3 + q + 3);
129
130 const signed char* weight_hc_I_0 = weight_hc_dr.row<const signed char>(hidden_size * 0 + q);
131 const signed char* weight_hc_F_0 = weight_hc_dr.row<const signed char>(hidden_size * 1 + q);
132 const signed char* weight_hc_O_0 = weight_hc_dr.row<const signed char>(hidden_size * 2 + q);
133 const signed char* weight_hc_G_0 = weight_hc_dr.row<const signed char>(hidden_size * 3 + q);
134 const signed char* weight_hc_I_1 = weight_hc_dr.row<const signed char>(hidden_size * 0 + q + 1);
135 const signed char* weight_hc_F_1 = weight_hc_dr.row<const signed char>(hidden_size * 1 + q + 1);
136 const signed char* weight_hc_O_1 = weight_hc_dr.row<const signed char>(hidden_size * 2 + q + 1);
137 const signed char* weight_hc_G_1 = weight_hc_dr.row<const signed char>(hidden_size * 3 + q + 1);
138 const signed char* weight_hc_I_2 = weight_hc_dr.row<const signed char>(hidden_size * 0 + q + 2);
139 const signed char* weight_hc_F_2 = weight_hc_dr.row<const signed char>(hidden_size * 1 + q + 2);
140 const signed char* weight_hc_O_2 = weight_hc_dr.row<const signed char>(hidden_size * 2 + q + 2);
141 const signed char* weight_hc_G_2 = weight_hc_dr.row<const signed char>(hidden_size * 3 + q + 2);
142 const signed char* weight_hc_I_3 = weight_hc_dr.row<const signed char>(hidden_size * 0 + q + 3);
143 const signed char* weight_hc_F_3 = weight_hc_dr.row<const signed char>(hidden_size * 1 + q + 3);
144 const signed char* weight_hc_O_3 = weight_hc_dr.row<const signed char>(hidden_size * 2 + q + 3);
145 const signed char* weight_hc_G_3 = weight_hc_dr.row<const signed char>(hidden_size * 3 + q + 3);
146
147 signed char* kptr = weight_data_tm_dr.row<signed char>(q / 4);
148 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 4);
149
150 int i = 0;
151#if __AVX512VNNI__
152 __m512i _w_shift = _mm512_setzero_si512();
153 __m512i _v127 = _mm512_set1_epi8(127);
154
155 __m512i _w0_shift = _mm512_setzero_si512();
156 __m512i _w1_shift = _mm512_setzero_si512();
157#if defined(__x86_64__) || defined(_M_X64)
158 __m512i _w2_shift = _mm512_setzero_si512();
159 __m512i _w3_shift = _mm512_setzero_si512();
160 for (; i + 15 < size; i += 16)
161 {
162 _mm_storeu_si128((__m128i*)kptr, _mm_loadu_si128((const __m128i*)(weight_xc_I_0 + i)));
163 _mm_storeu_si128((__m128i*)(kptr + 16), _mm_loadu_si128((const __m128i*)(weight_xc_F_0 + i)));
164 _mm_storeu_si128((__m128i*)(kptr + 16 * 2), _mm_loadu_si128((const __m128i*)(weight_xc_O_0 + i)));
165 _mm_storeu_si128((__m128i*)(kptr + 16 * 3), _mm_loadu_si128((const __m128i*)(weight_xc_G_0 + i)));
166 _mm_storeu_si128((__m128i*)(kptr + 16 * 4), _mm_loadu_si128((const __m128i*)(weight_xc_I_1 + i)));
167 _mm_storeu_si128((__m128i*)(kptr + 16 * 5), _mm_loadu_si128((const __m128i*)(weight_xc_F_1 + i)));
168 _mm_storeu_si128((__m128i*)(kptr + 16 * 6), _mm_loadu_si128((const __m128i*)(weight_xc_O_1 + i)));
169 _mm_storeu_si128((__m128i*)(kptr + 16 * 7), _mm_loadu_si128((const __m128i*)(weight_xc_G_1 + i)));
170 _mm_storeu_si128((__m128i*)(kptr + 16 * 8), _mm_loadu_si128((const __m128i*)(weight_xc_I_2 + i)));
171 _mm_storeu_si128((__m128i*)(kptr + 16 * 9), _mm_loadu_si128((const __m128i*)(weight_xc_F_2 + i)));
172 _mm_storeu_si128((__m128i*)(kptr + 16 * 10), _mm_loadu_si128((const __m128i*)(weight_xc_O_2 + i)));
173 _mm_storeu_si128((__m128i*)(kptr + 16 * 11), _mm_loadu_si128((const __m128i*)(weight_xc_G_2 + i)));
174 _mm_storeu_si128((__m128i*)(kptr + 16 * 12), _mm_loadu_si128((const __m128i*)(weight_xc_I_3 + i)));
175 _mm_storeu_si128((__m128i*)(kptr + 16 * 13), _mm_loadu_si128((const __m128i*)(weight_xc_F_3 + i)));
176 _mm_storeu_si128((__m128i*)(kptr + 16 * 14), _mm_loadu_si128((const __m128i*)(weight_xc_O_3 + i)));
177 _mm_storeu_si128((__m128i*)(kptr + 16 * 15), _mm_loadu_si128((const __m128i*)(weight_xc_G_3 + i)));
178
179 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
180 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
181 __m512i _w2 = _mm512_loadu_si512((const __m512i*)(kptr + 128));
182 __m512i _w3 = _mm512_loadu_si512((const __m512i*)(kptr + 192));
183 _w0_shift = _mm512_dpbusd_epi32(_w0_shift, _v127, _w0);
184 _w1_shift = _mm512_dpbusd_epi32(_w1_shift, _v127, _w1);
185 _w2_shift = _mm512_dpbusd_epi32(_w2_shift, _v127, _w2);
186 _w3_shift = _mm512_dpbusd_epi32(_w3_shift, _v127, _w3);
187
188 kptr += 256;
189 }
190 {
191 __m512i _tmp0 = _mm512_unpacklo_epi32(_w0_shift, _w1_shift);
192 __m512i _tmp1 = _mm512_unpackhi_epi32(_w0_shift, _w1_shift);
193 __m512i _tmp2 = _mm512_unpacklo_epi32(_w2_shift, _w3_shift);
194 __m512i _tmp3 = _mm512_unpackhi_epi32(_w2_shift, _w3_shift);
195 _w0_shift = _mm512_unpacklo_epi64(_tmp0, _tmp2);
196 _w1_shift = _mm512_unpackhi_epi64(_tmp0, _tmp2);
197 _w2_shift = _mm512_unpacklo_epi64(_tmp1, _tmp3);
198 _w3_shift = _mm512_unpackhi_epi64(_tmp1, _tmp3);
199
200 _w_shift = _mm512_add_epi32(_w_shift, _w0_shift);
201 _w_shift = _mm512_add_epi32(_w_shift, _w1_shift);
202 _w_shift = _mm512_add_epi32(_w_shift, _w2_shift);
203 _w_shift = _mm512_add_epi32(_w_shift, _w3_shift);
204 }
205
206 _w0_shift = _mm512_setzero_si512();
207 _w1_shift = _mm512_setzero_si512();
208#endif // defined(__x86_64__) || defined(_M_X64)
209 for (; i + 7 < size; i += 8)
210 {
211 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_xc_I_0 + i)));
212 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_xc_I_1 + i)));
213 _mm_storel_epi64((__m128i*)(kptr + 8 * 2), _mm_loadl_epi64((const __m128i*)(weight_xc_F_0 + i)));
214 _mm_storel_epi64((__m128i*)(kptr + 8 * 3), _mm_loadl_epi64((const __m128i*)(weight_xc_F_1 + i)));
215 _mm_storel_epi64((__m128i*)(kptr + 8 * 4), _mm_loadl_epi64((const __m128i*)(weight_xc_O_0 + i)));
216 _mm_storel_epi64((__m128i*)(kptr + 8 * 5), _mm_loadl_epi64((const __m128i*)(weight_xc_O_1 + i)));
217 _mm_storel_epi64((__m128i*)(kptr + 8 * 6), _mm_loadl_epi64((const __m128i*)(weight_xc_G_0 + i)));
218 _mm_storel_epi64((__m128i*)(kptr + 8 * 7), _mm_loadl_epi64((const __m128i*)(weight_xc_G_1 + i)));
219 _mm_storel_epi64((__m128i*)(kptr + 8 * 8), _mm_loadl_epi64((const __m128i*)(weight_xc_I_2 + i)));
220 _mm_storel_epi64((__m128i*)(kptr + 8 * 9), _mm_loadl_epi64((const __m128i*)(weight_xc_I_3 + i)));
221 _mm_storel_epi64((__m128i*)(kptr + 8 * 10), _mm_loadl_epi64((const __m128i*)(weight_xc_F_2 + i)));
222 _mm_storel_epi64((__m128i*)(kptr + 8 * 11), _mm_loadl_epi64((const __m128i*)(weight_xc_F_3 + i)));
223 _mm_storel_epi64((__m128i*)(kptr + 8 * 12), _mm_loadl_epi64((const __m128i*)(weight_xc_O_2 + i)));
224 _mm_storel_epi64((__m128i*)(kptr + 8 * 13), _mm_loadl_epi64((const __m128i*)(weight_xc_O_3 + i)));
225 _mm_storel_epi64((__m128i*)(kptr + 8 * 14), _mm_loadl_epi64((const __m128i*)(weight_xc_G_2 + i)));
226 _mm_storel_epi64((__m128i*)(kptr + 8 * 15), _mm_loadl_epi64((const __m128i*)(weight_xc_G_3 + i)));
227
228 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
229 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
230 _w0_shift = _mm512_dpbusd_epi32(_w0_shift, _v127, _w0);
231 _w1_shift = _mm512_dpbusd_epi32(_w1_shift, _v127, _w1);
232
233 kptr += 128;
234 }
235 {
236 __m512i _tmp0 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_w0_shift), _mm512_castsi512_ps(_w1_shift), _MM_SHUFFLE(2, 0, 2, 0)));
237 __m512i _tmp1 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_w0_shift), _mm512_castsi512_ps(_w1_shift), _MM_SHUFFLE(3, 1, 3, 1)));
238
239 _w_shift = _mm512_add_epi32(_w_shift, _tmp0);
240 _w_shift = _mm512_add_epi32(_w_shift, _tmp1);
241 }
242
243 for (; i + 3 < size; i += 4)
244 {
245 kptr[0] = weight_xc_I_0[i];
246 kptr[1] = weight_xc_I_0[i + 1];
247 kptr[2] = weight_xc_I_0[i + 2];
248 kptr[3] = weight_xc_I_0[i + 3];
249 kptr[4] = weight_xc_I_1[i];
250 kptr[5] = weight_xc_I_1[i + 1];
251 kptr[6] = weight_xc_I_1[i + 2];
252 kptr[7] = weight_xc_I_1[i + 3];
253 kptr[8 + 0] = weight_xc_I_2[i];
254 kptr[8 + 1] = weight_xc_I_2[i + 1];
255 kptr[8 + 2] = weight_xc_I_2[i + 2];
256 kptr[8 + 3] = weight_xc_I_2[i + 3];
257 kptr[8 + 4] = weight_xc_I_3[i];
258 kptr[8 + 5] = weight_xc_I_3[i + 1];
259 kptr[8 + 6] = weight_xc_I_3[i + 2];
260 kptr[8 + 7] = weight_xc_I_3[i + 3];
261 kptr[16 + 0] = weight_xc_F_0[i];
262 kptr[16 + 1] = weight_xc_F_0[i + 1];
263 kptr[16 + 2] = weight_xc_F_0[i + 2];
264 kptr[16 + 3] = weight_xc_F_0[i + 3];
265 kptr[16 + 4] = weight_xc_F_1[i];
266 kptr[16 + 5] = weight_xc_F_1[i + 1];
267 kptr[16 + 6] = weight_xc_F_1[i + 2];
268 kptr[16 + 7] = weight_xc_F_1[i + 3];
269 kptr[24 + 0] = weight_xc_F_2[i];
270 kptr[24 + 1] = weight_xc_F_2[i + 1];
271 kptr[24 + 2] = weight_xc_F_2[i + 2];
272 kptr[24 + 3] = weight_xc_F_2[i + 3];
273 kptr[24 + 4] = weight_xc_F_3[i];
274 kptr[24 + 5] = weight_xc_F_3[i + 1];
275 kptr[24 + 6] = weight_xc_F_3[i + 2];
276 kptr[24 + 7] = weight_xc_F_3[i + 3];
277 kptr[32 + 0] = weight_xc_O_0[i];
278 kptr[32 + 1] = weight_xc_O_0[i + 1];
279 kptr[32 + 2] = weight_xc_O_0[i + 2];
280 kptr[32 + 3] = weight_xc_O_0[i + 3];
281 kptr[32 + 4] = weight_xc_O_1[i];
282 kptr[32 + 5] = weight_xc_O_1[i + 1];
283 kptr[32 + 6] = weight_xc_O_1[i + 2];
284 kptr[32 + 7] = weight_xc_O_1[i + 3];
285 kptr[40 + 0] = weight_xc_O_2[i];
286 kptr[40 + 1] = weight_xc_O_2[i + 1];
287 kptr[40 + 2] = weight_xc_O_2[i + 2];
288 kptr[40 + 3] = weight_xc_O_2[i + 3];
289 kptr[40 + 4] = weight_xc_O_3[i];
290 kptr[40 + 5] = weight_xc_O_3[i + 1];
291 kptr[40 + 6] = weight_xc_O_3[i + 2];
292 kptr[40 + 7] = weight_xc_O_3[i + 3];
293 kptr[48 + 0] = weight_xc_G_0[i];
294 kptr[48 + 1] = weight_xc_G_0[i + 1];
295 kptr[48 + 2] = weight_xc_G_0[i + 2];
296 kptr[48 + 3] = weight_xc_G_0[i + 3];
297 kptr[48 + 4] = weight_xc_G_1[i];
298 kptr[48 + 5] = weight_xc_G_1[i + 1];
299 kptr[48 + 6] = weight_xc_G_1[i + 2];
300 kptr[48 + 7] = weight_xc_G_1[i + 3];
301 kptr[56 + 0] = weight_xc_G_2[i];
302 kptr[56 + 1] = weight_xc_G_2[i + 1];
303 kptr[56 + 2] = weight_xc_G_2[i + 2];
304 kptr[56 + 3] = weight_xc_G_2[i + 3];
305 kptr[56 + 4] = weight_xc_G_3[i];
306 kptr[56 + 5] = weight_xc_G_3[i + 1];
307 kptr[56 + 6] = weight_xc_G_3[i + 2];
308 kptr[56 + 7] = weight_xc_G_3[i + 3];
309
310 __m512i _w = _mm512_loadu_si512((const __m512i*)kptr);
311 _w_shift = _mm512_dpbusd_epi32(_w_shift, _v127, _w);
312
313 kptr += 64;
314 }
315
316 _mm512_storeu_si512((__m512i*)kptr, _w_shift);
317 kptr += 64;
318#else
319#if defined(__x86_64__) || defined(_M_X64)
320 for (; i + 7 < size; i += 8)
321 {
322 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_xc_I_0 + i)));
323 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_xc_F_0 + i)));
324 _mm_storel_epi64((__m128i*)(kptr + 8 * 2), _mm_loadl_epi64((const __m128i*)(weight_xc_O_0 + i)));
325 _mm_storel_epi64((__m128i*)(kptr + 8 * 3), _mm_loadl_epi64((const __m128i*)(weight_xc_G_0 + i)));
326 _mm_storel_epi64((__m128i*)(kptr + 8 * 4), _mm_loadl_epi64((const __m128i*)(weight_xc_I_1 + i)));
327 _mm_storel_epi64((__m128i*)(kptr + 8 * 5), _mm_loadl_epi64((const __m128i*)(weight_xc_F_1 + i)));
328 _mm_storel_epi64((__m128i*)(kptr + 8 * 6), _mm_loadl_epi64((const __m128i*)(weight_xc_O_1 + i)));
329 _mm_storel_epi64((__m128i*)(kptr + 8 * 7), _mm_loadl_epi64((const __m128i*)(weight_xc_G_1 + i)));
330 _mm_storel_epi64((__m128i*)(kptr + 8 * 8), _mm_loadl_epi64((const __m128i*)(weight_xc_I_2 + i)));
331 _mm_storel_epi64((__m128i*)(kptr + 8 * 9), _mm_loadl_epi64((const __m128i*)(weight_xc_F_2 + i)));
332 _mm_storel_epi64((__m128i*)(kptr + 8 * 10), _mm_loadl_epi64((const __m128i*)(weight_xc_O_2 + i)));
333 _mm_storel_epi64((__m128i*)(kptr + 8 * 11), _mm_loadl_epi64((const __m128i*)(weight_xc_G_2 + i)));
334 _mm_storel_epi64((__m128i*)(kptr + 8 * 12), _mm_loadl_epi64((const __m128i*)(weight_xc_I_3 + i)));
335 _mm_storel_epi64((__m128i*)(kptr + 8 * 13), _mm_loadl_epi64((const __m128i*)(weight_xc_F_3 + i)));
336 _mm_storel_epi64((__m128i*)(kptr + 8 * 14), _mm_loadl_epi64((const __m128i*)(weight_xc_O_3 + i)));
337 _mm_storel_epi64((__m128i*)(kptr + 8 * 15), _mm_loadl_epi64((const __m128i*)(weight_xc_G_3 + i)));
338 kptr += 128;
339 }
340#endif // defined(__x86_64__) || defined(_M_X64)
341 for (; i + 3 < size; i += 4)
342 {
343 kptr[0] = weight_xc_I_0[i];
344 kptr[1] = weight_xc_I_0[i + 1];
345 kptr[2] = weight_xc_I_0[i + 2];
346 kptr[3] = weight_xc_I_0[i + 3];
347 kptr[4] = weight_xc_I_1[i];
348 kptr[5] = weight_xc_I_1[i + 1];
349 kptr[6] = weight_xc_I_1[i + 2];
350 kptr[7] = weight_xc_I_1[i + 3];
351 kptr[8 + 0] = weight_xc_F_0[i];
352 kptr[8 + 1] = weight_xc_F_0[i + 1];
353 kptr[8 + 2] = weight_xc_F_0[i + 2];
354 kptr[8 + 3] = weight_xc_F_0[i + 3];
355 kptr[8 + 4] = weight_xc_F_1[i];
356 kptr[8 + 5] = weight_xc_F_1[i + 1];
357 kptr[8 + 6] = weight_xc_F_1[i + 2];
358 kptr[8 + 7] = weight_xc_F_1[i + 3];
359 kptr[16 + 0] = weight_xc_O_0[i];
360 kptr[16 + 1] = weight_xc_O_0[i + 1];
361 kptr[16 + 2] = weight_xc_O_0[i + 2];
362 kptr[16 + 3] = weight_xc_O_0[i + 3];
363 kptr[16 + 4] = weight_xc_O_1[i];
364 kptr[16 + 5] = weight_xc_O_1[i + 1];
365 kptr[16 + 6] = weight_xc_O_1[i + 2];
366 kptr[16 + 7] = weight_xc_O_1[i + 3];
367 kptr[24 + 0] = weight_xc_G_0[i];
368 kptr[24 + 1] = weight_xc_G_0[i + 1];
369 kptr[24 + 2] = weight_xc_G_0[i + 2];
370 kptr[24 + 3] = weight_xc_G_0[i + 3];
371 kptr[24 + 4] = weight_xc_G_1[i];
372 kptr[24 + 5] = weight_xc_G_1[i + 1];
373 kptr[24 + 6] = weight_xc_G_1[i + 2];
374 kptr[24 + 7] = weight_xc_G_1[i + 3];
375 kptr[32 + 0] = weight_xc_I_2[i];
376 kptr[32 + 1] = weight_xc_I_2[i + 1];
377 kptr[32 + 2] = weight_xc_I_2[i + 2];
378 kptr[32 + 3] = weight_xc_I_2[i + 3];
379 kptr[32 + 4] = weight_xc_I_3[i];
380 kptr[32 + 5] = weight_xc_I_3[i + 1];
381 kptr[32 + 6] = weight_xc_I_3[i + 2];
382 kptr[32 + 7] = weight_xc_I_3[i + 3];
383 kptr[40 + 0] = weight_xc_F_2[i];
384 kptr[40 + 1] = weight_xc_F_2[i + 1];
385 kptr[40 + 2] = weight_xc_F_2[i + 2];
386 kptr[40 + 3] = weight_xc_F_2[i + 3];
387 kptr[40 + 4] = weight_xc_F_3[i];
388 kptr[40 + 5] = weight_xc_F_3[i + 1];
389 kptr[40 + 6] = weight_xc_F_3[i + 2];
390 kptr[40 + 7] = weight_xc_F_3[i + 3];
391 kptr[48 + 0] = weight_xc_O_2[i];
392 kptr[48 + 1] = weight_xc_O_2[i + 1];
393 kptr[48 + 2] = weight_xc_O_2[i + 2];
394 kptr[48 + 3] = weight_xc_O_2[i + 3];
395 kptr[48 + 4] = weight_xc_O_3[i];
396 kptr[48 + 5] = weight_xc_O_3[i + 1];
397 kptr[48 + 6] = weight_xc_O_3[i + 2];
398 kptr[48 + 7] = weight_xc_O_3[i + 3];
399 kptr[56 + 0] = weight_xc_G_2[i];
400 kptr[56 + 1] = weight_xc_G_2[i + 1];
401 kptr[56 + 2] = weight_xc_G_2[i + 2];
402 kptr[56 + 3] = weight_xc_G_2[i + 3];
403 kptr[56 + 4] = weight_xc_G_3[i];
404 kptr[56 + 5] = weight_xc_G_3[i + 1];
405 kptr[56 + 6] = weight_xc_G_3[i + 2];
406 kptr[56 + 7] = weight_xc_G_3[i + 3];
407 kptr += 64;
408 }
409#endif // __AVX512VNNI__
410 for (; i + 1 < size; i += 2)
411 {
412 kptr[0] = weight_xc_I_0[i];
413 kptr[1] = weight_xc_I_0[i + 1];
414 kptr[2] = weight_xc_I_1[i];
415 kptr[3] = weight_xc_I_1[i + 1];
416 kptr[4] = weight_xc_I_2[i];
417 kptr[5] = weight_xc_I_2[i + 1];
418 kptr[6] = weight_xc_I_3[i];
419 kptr[7] = weight_xc_I_3[i + 1];
420 kptr[8 + 0] = weight_xc_F_0[i];
421 kptr[8 + 1] = weight_xc_F_0[i + 1];
422 kptr[8 + 2] = weight_xc_F_1[i];
423 kptr[8 + 3] = weight_xc_F_1[i + 1];
424 kptr[8 + 4] = weight_xc_F_2[i];
425 kptr[8 + 5] = weight_xc_F_2[i + 1];
426 kptr[8 + 6] = weight_xc_F_3[i];
427 kptr[8 + 7] = weight_xc_F_3[i + 1];
428 kptr[16 + 0] = weight_xc_O_0[i];
429 kptr[16 + 1] = weight_xc_O_0[i + 1];
430 kptr[16 + 2] = weight_xc_O_1[i];
431 kptr[16 + 3] = weight_xc_O_1[i + 1];
432 kptr[16 + 4] = weight_xc_O_2[i];
433 kptr[16 + 5] = weight_xc_O_2[i + 1];
434 kptr[16 + 6] = weight_xc_O_3[i];
435 kptr[16 + 7] = weight_xc_O_3[i + 1];
436 kptr[24 + 0] = weight_xc_G_0[i];
437 kptr[24 + 1] = weight_xc_G_0[i + 1];
438 kptr[24 + 2] = weight_xc_G_1[i];
439 kptr[24 + 3] = weight_xc_G_1[i + 1];
440 kptr[24 + 4] = weight_xc_G_2[i];
441 kptr[24 + 5] = weight_xc_G_2[i + 1];
442 kptr[24 + 6] = weight_xc_G_3[i];
443 kptr[24 + 7] = weight_xc_G_3[i + 1];
444 kptr += 32;
445 }
446 for (; i < size; i++)
447 {
448 kptr[0] = weight_xc_I_0[i];
449 kptr[1] = weight_xc_I_1[i];
450 kptr[2] = weight_xc_I_2[i];
451 kptr[3] = weight_xc_I_3[i];
452 kptr[4] = weight_xc_F_0[i];
453 kptr[5] = weight_xc_F_1[i];
454 kptr[6] = weight_xc_F_2[i];
455 kptr[7] = weight_xc_F_3[i];
456 kptr[8 + 0] = weight_xc_O_0[i];
457 kptr[8 + 1] = weight_xc_O_1[i];
458 kptr[8 + 2] = weight_xc_O_2[i];
459 kptr[8 + 3] = weight_xc_O_3[i];
460 kptr[8 + 4] = weight_xc_G_0[i];
461 kptr[8 + 5] = weight_xc_G_1[i];
462 kptr[8 + 6] = weight_xc_G_2[i];
463 kptr[8 + 7] = weight_xc_G_3[i];
464 kptr += 16;
465 }
466
467 i = 0;
468#if __AVX512VNNI__
469 _w_shift = _mm512_setzero_si512();
470 _w0_shift = _mm512_setzero_si512();
471 _w1_shift = _mm512_setzero_si512();
472#if defined(__x86_64__) || defined(_M_X64)
473 _w2_shift = _mm512_setzero_si512();
474 _w3_shift = _mm512_setzero_si512();
475 for (; i + 15 < num_output; i += 16)
476 {
477 _mm_storeu_si128((__m128i*)kptr, _mm_loadu_si128((const __m128i*)(weight_hc_I_0 + i)));
478 _mm_storeu_si128((__m128i*)(kptr + 16), _mm_loadu_si128((const __m128i*)(weight_hc_F_0 + i)));
479 _mm_storeu_si128((__m128i*)(kptr + 16 * 2), _mm_loadu_si128((const __m128i*)(weight_hc_O_0 + i)));
480 _mm_storeu_si128((__m128i*)(kptr + 16 * 3), _mm_loadu_si128((const __m128i*)(weight_hc_G_0 + i)));
481 _mm_storeu_si128((__m128i*)(kptr + 16 * 4), _mm_loadu_si128((const __m128i*)(weight_hc_I_1 + i)));
482 _mm_storeu_si128((__m128i*)(kptr + 16 * 5), _mm_loadu_si128((const __m128i*)(weight_hc_F_1 + i)));
483 _mm_storeu_si128((__m128i*)(kptr + 16 * 6), _mm_loadu_si128((const __m128i*)(weight_hc_O_1 + i)));
484 _mm_storeu_si128((__m128i*)(kptr + 16 * 7), _mm_loadu_si128((const __m128i*)(weight_hc_G_1 + i)));
485 _mm_storeu_si128((__m128i*)(kptr + 16 * 8), _mm_loadu_si128((const __m128i*)(weight_hc_I_2 + i)));
486 _mm_storeu_si128((__m128i*)(kptr + 16 * 9), _mm_loadu_si128((const __m128i*)(weight_hc_F_2 + i)));
487 _mm_storeu_si128((__m128i*)(kptr + 16 * 10), _mm_loadu_si128((const __m128i*)(weight_hc_O_2 + i)));
488 _mm_storeu_si128((__m128i*)(kptr + 16 * 11), _mm_loadu_si128((const __m128i*)(weight_hc_G_2 + i)));
489 _mm_storeu_si128((__m128i*)(kptr + 16 * 12), _mm_loadu_si128((const __m128i*)(weight_hc_I_3 + i)));
490 _mm_storeu_si128((__m128i*)(kptr + 16 * 13), _mm_loadu_si128((const __m128i*)(weight_hc_F_3 + i)));
491 _mm_storeu_si128((__m128i*)(kptr + 16 * 14), _mm_loadu_si128((const __m128i*)(weight_hc_O_3 + i)));
492 _mm_storeu_si128((__m128i*)(kptr + 16 * 15), _mm_loadu_si128((const __m128i*)(weight_hc_G_3 + i)));
493
494 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
495 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
496 __m512i _w2 = _mm512_loadu_si512((const __m512i*)(kptr + 128));
497 __m512i _w3 = _mm512_loadu_si512((const __m512i*)(kptr + 192));
498 _w0_shift = _mm512_dpbusd_epi32(_w0_shift, _v127, _w0);
499 _w1_shift = _mm512_dpbusd_epi32(_w1_shift, _v127, _w1);
500 _w2_shift = _mm512_dpbusd_epi32(_w2_shift, _v127, _w2);
501 _w3_shift = _mm512_dpbusd_epi32(_w3_shift, _v127, _w3);
502
503 kptr += 256;
504 }
505 {
506 __m512i _tmp0 = _mm512_unpacklo_epi32(_w0_shift, _w1_shift);
507 __m512i _tmp1 = _mm512_unpackhi_epi32(_w0_shift, _w1_shift);
508 __m512i _tmp2 = _mm512_unpacklo_epi32(_w2_shift, _w3_shift);
509 __m512i _tmp3 = _mm512_unpackhi_epi32(_w2_shift, _w3_shift);
510 _w0_shift = _mm512_unpacklo_epi64(_tmp0, _tmp2);
511 _w1_shift = _mm512_unpackhi_epi64(_tmp0, _tmp2);
512 _w2_shift = _mm512_unpacklo_epi64(_tmp1, _tmp3);
513 _w3_shift = _mm512_unpackhi_epi64(_tmp1, _tmp3);
514
515 _w_shift = _mm512_add_epi32(_w_shift, _w0_shift);
516 _w_shift = _mm512_add_epi32(_w_shift, _w1_shift);
517 _w_shift = _mm512_add_epi32(_w_shift, _w2_shift);
518 _w_shift = _mm512_add_epi32(_w_shift, _w3_shift);
519 }
520
521 _w0_shift = _mm512_setzero_si512();
522 _w1_shift = _mm512_setzero_si512();
523#endif // defined(__x86_64__) || defined(_M_X64)
524 for (; i + 7 < num_output; i += 8)
525 {
526 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_hc_I_0 + i)));
527 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_hc_I_1 + i)));
528 _mm_storel_epi64((__m128i*)(kptr + 8 * 2), _mm_loadl_epi64((const __m128i*)(weight_hc_F_0 + i)));
529 _mm_storel_epi64((__m128i*)(kptr + 8 * 3), _mm_loadl_epi64((const __m128i*)(weight_hc_F_1 + i)));
530 _mm_storel_epi64((__m128i*)(kptr + 8 * 4), _mm_loadl_epi64((const __m128i*)(weight_hc_O_0 + i)));
531 _mm_storel_epi64((__m128i*)(kptr + 8 * 5), _mm_loadl_epi64((const __m128i*)(weight_hc_O_1 + i)));
532 _mm_storel_epi64((__m128i*)(kptr + 8 * 6), _mm_loadl_epi64((const __m128i*)(weight_hc_G_0 + i)));
533 _mm_storel_epi64((__m128i*)(kptr + 8 * 7), _mm_loadl_epi64((const __m128i*)(weight_hc_G_1 + i)));
534 _mm_storel_epi64((__m128i*)(kptr + 8 * 8), _mm_loadl_epi64((const __m128i*)(weight_hc_I_2 + i)));
535 _mm_storel_epi64((__m128i*)(kptr + 8 * 9), _mm_loadl_epi64((const __m128i*)(weight_hc_I_3 + i)));
536 _mm_storel_epi64((__m128i*)(kptr + 8 * 10), _mm_loadl_epi64((const __m128i*)(weight_hc_F_2 + i)));
537 _mm_storel_epi64((__m128i*)(kptr + 8 * 11), _mm_loadl_epi64((const __m128i*)(weight_hc_F_3 + i)));
538 _mm_storel_epi64((__m128i*)(kptr + 8 * 12), _mm_loadl_epi64((const __m128i*)(weight_hc_O_2 + i)));
539 _mm_storel_epi64((__m128i*)(kptr + 8 * 13), _mm_loadl_epi64((const __m128i*)(weight_hc_O_3 + i)));
540 _mm_storel_epi64((__m128i*)(kptr + 8 * 14), _mm_loadl_epi64((const __m128i*)(weight_hc_G_2 + i)));
541 _mm_storel_epi64((__m128i*)(kptr + 8 * 15), _mm_loadl_epi64((const __m128i*)(weight_hc_G_3 + i)));
542
543 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
544 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
545 _w0_shift = _mm512_dpbusd_epi32(_w0_shift, _v127, _w0);
546 _w1_shift = _mm512_dpbusd_epi32(_w1_shift, _v127, _w1);
547
548 kptr += 128;
549 }
550 {
551 __m512i _tmp0 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_w0_shift), _mm512_castsi512_ps(_w1_shift), _MM_SHUFFLE(2, 0, 2, 0)));
552 __m512i _tmp1 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_w0_shift), _mm512_castsi512_ps(_w1_shift), _MM_SHUFFLE(3, 1, 3, 1)));
553
554 _w_shift = _mm512_add_epi32(_w_shift, _tmp0);
555 _w_shift = _mm512_add_epi32(_w_shift, _tmp1);
556 }
557
558 for (; i + 3 < num_output; i += 4)
559 {
560 kptr[0] = weight_hc_I_0[i];
561 kptr[1] = weight_hc_I_0[i + 1];
562 kptr[2] = weight_hc_I_0[i + 2];
563 kptr[3] = weight_hc_I_0[i + 3];
564 kptr[4] = weight_hc_I_1[i];
565 kptr[5] = weight_hc_I_1[i + 1];
566 kptr[6] = weight_hc_I_1[i + 2];
567 kptr[7] = weight_hc_I_1[i + 3];
568 kptr[8 + 0] = weight_hc_I_2[i];
569 kptr[8 + 1] = weight_hc_I_2[i + 1];
570 kptr[8 + 2] = weight_hc_I_2[i + 2];
571 kptr[8 + 3] = weight_hc_I_2[i + 3];
572 kptr[8 + 4] = weight_hc_I_3[i];
573 kptr[8 + 5] = weight_hc_I_3[i + 1];
574 kptr[8 + 6] = weight_hc_I_3[i + 2];
575 kptr[8 + 7] = weight_hc_I_3[i + 3];
576 kptr[16 + 0] = weight_hc_F_0[i];
577 kptr[16 + 1] = weight_hc_F_0[i + 1];
578 kptr[16 + 2] = weight_hc_F_0[i + 2];
579 kptr[16 + 3] = weight_hc_F_0[i + 3];
580 kptr[16 + 4] = weight_hc_F_1[i];
581 kptr[16 + 5] = weight_hc_F_1[i + 1];
582 kptr[16 + 6] = weight_hc_F_1[i + 2];
583 kptr[16 + 7] = weight_hc_F_1[i + 3];
584 kptr[24 + 0] = weight_hc_F_2[i];
585 kptr[24 + 1] = weight_hc_F_2[i + 1];
586 kptr[24 + 2] = weight_hc_F_2[i + 2];
587 kptr[24 + 3] = weight_hc_F_2[i + 3];
588 kptr[24 + 4] = weight_hc_F_3[i];
589 kptr[24 + 5] = weight_hc_F_3[i + 1];
590 kptr[24 + 6] = weight_hc_F_3[i + 2];
591 kptr[24 + 7] = weight_hc_F_3[i + 3];
592 kptr[32 + 0] = weight_hc_O_0[i];
593 kptr[32 + 1] = weight_hc_O_0[i + 1];
594 kptr[32 + 2] = weight_hc_O_0[i + 2];
595 kptr[32 + 3] = weight_hc_O_0[i + 3];
596 kptr[32 + 4] = weight_hc_O_1[i];
597 kptr[32 + 5] = weight_hc_O_1[i + 1];
598 kptr[32 + 6] = weight_hc_O_1[i + 2];
599 kptr[32 + 7] = weight_hc_O_1[i + 3];
600 kptr[40 + 0] = weight_hc_O_2[i];
601 kptr[40 + 1] = weight_hc_O_2[i + 1];
602 kptr[40 + 2] = weight_hc_O_2[i + 2];
603 kptr[40 + 3] = weight_hc_O_2[i + 3];
604 kptr[40 + 4] = weight_hc_O_3[i];
605 kptr[40 + 5] = weight_hc_O_3[i + 1];
606 kptr[40 + 6] = weight_hc_O_3[i + 2];
607 kptr[40 + 7] = weight_hc_O_3[i + 3];
608 kptr[48 + 0] = weight_hc_G_0[i];
609 kptr[48 + 1] = weight_hc_G_0[i + 1];
610 kptr[48 + 2] = weight_hc_G_0[i + 2];
611 kptr[48 + 3] = weight_hc_G_0[i + 3];
612 kptr[48 + 4] = weight_hc_G_1[i];
613 kptr[48 + 5] = weight_hc_G_1[i + 1];
614 kptr[48 + 6] = weight_hc_G_1[i + 2];
615 kptr[48 + 7] = weight_hc_G_1[i + 3];
616 kptr[56 + 0] = weight_hc_G_2[i];
617 kptr[56 + 1] = weight_hc_G_2[i + 1];
618 kptr[56 + 2] = weight_hc_G_2[i + 2];
619 kptr[56 + 3] = weight_hc_G_2[i + 3];
620 kptr[56 + 4] = weight_hc_G_3[i];
621 kptr[56 + 5] = weight_hc_G_3[i + 1];
622 kptr[56 + 6] = weight_hc_G_3[i + 2];
623 kptr[56 + 7] = weight_hc_G_3[i + 3];
624
625 __m512i _w = _mm512_loadu_si512((const __m512i*)kptr);
626 _w_shift = _mm512_dpbusd_epi32(_w_shift, _v127, _w);
627
628 kptr += 64;
629 }
630
631 _mm512_storeu_si512((__m512i*)kptr, _w_shift);
632 kptr += 64;
633#else
634#if defined(__x86_64__) || defined(_M_X64)
635 for (; i + 7 < num_output; i += 8)
636 {
637 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_hc_I_0 + i)));
638 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_hc_F_0 + i)));
639 _mm_storel_epi64((__m128i*)(kptr + 8 * 2), _mm_loadl_epi64((const __m128i*)(weight_hc_O_0 + i)));
640 _mm_storel_epi64((__m128i*)(kptr + 8 * 3), _mm_loadl_epi64((const __m128i*)(weight_hc_G_0 + i)));
641 _mm_storel_epi64((__m128i*)(kptr + 8 * 4), _mm_loadl_epi64((const __m128i*)(weight_hc_I_1 + i)));
642 _mm_storel_epi64((__m128i*)(kptr + 8 * 5), _mm_loadl_epi64((const __m128i*)(weight_hc_F_1 + i)));
643 _mm_storel_epi64((__m128i*)(kptr + 8 * 6), _mm_loadl_epi64((const __m128i*)(weight_hc_O_1 + i)));
644 _mm_storel_epi64((__m128i*)(kptr + 8 * 7), _mm_loadl_epi64((const __m128i*)(weight_hc_G_1 + i)));
645 _mm_storel_epi64((__m128i*)(kptr + 8 * 8), _mm_loadl_epi64((const __m128i*)(weight_hc_I_2 + i)));
646 _mm_storel_epi64((__m128i*)(kptr + 8 * 9), _mm_loadl_epi64((const __m128i*)(weight_hc_F_2 + i)));
647 _mm_storel_epi64((__m128i*)(kptr + 8 * 10), _mm_loadl_epi64((const __m128i*)(weight_hc_O_2 + i)));
648 _mm_storel_epi64((__m128i*)(kptr + 8 * 11), _mm_loadl_epi64((const __m128i*)(weight_hc_G_2 + i)));
649 _mm_storel_epi64((__m128i*)(kptr + 8 * 12), _mm_loadl_epi64((const __m128i*)(weight_hc_I_3 + i)));
650 _mm_storel_epi64((__m128i*)(kptr + 8 * 13), _mm_loadl_epi64((const __m128i*)(weight_hc_F_3 + i)));
651 _mm_storel_epi64((__m128i*)(kptr + 8 * 14), _mm_loadl_epi64((const __m128i*)(weight_hc_O_3 + i)));
652 _mm_storel_epi64((__m128i*)(kptr + 8 * 15), _mm_loadl_epi64((const __m128i*)(weight_hc_G_3 + i)));
653 kptr += 128;
654 }
655#endif // defined(__x86_64__) || defined(_M_X64)
656 for (; i + 3 < num_output; i += 4)
657 {
658 kptr[0] = weight_hc_I_0[i];
659 kptr[1] = weight_hc_I_0[i + 1];
660 kptr[2] = weight_hc_I_0[i + 2];
661 kptr[3] = weight_hc_I_0[i + 3];
662 kptr[4] = weight_hc_I_1[i];
663 kptr[5] = weight_hc_I_1[i + 1];
664 kptr[6] = weight_hc_I_1[i + 2];
665 kptr[7] = weight_hc_I_1[i + 3];
666 kptr[8 + 0] = weight_hc_F_0[i];
667 kptr[8 + 1] = weight_hc_F_0[i + 1];
668 kptr[8 + 2] = weight_hc_F_0[i + 2];
669 kptr[8 + 3] = weight_hc_F_0[i + 3];
670 kptr[8 + 4] = weight_hc_F_1[i];
671 kptr[8 + 5] = weight_hc_F_1[i + 1];
672 kptr[8 + 6] = weight_hc_F_1[i + 2];
673 kptr[8 + 7] = weight_hc_F_1[i + 3];
674 kptr[16 + 0] = weight_hc_O_0[i];
675 kptr[16 + 1] = weight_hc_O_0[i + 1];
676 kptr[16 + 2] = weight_hc_O_0[i + 2];
677 kptr[16 + 3] = weight_hc_O_0[i + 3];
678 kptr[16 + 4] = weight_hc_O_1[i];
679 kptr[16 + 5] = weight_hc_O_1[i + 1];
680 kptr[16 + 6] = weight_hc_O_1[i + 2];
681 kptr[16 + 7] = weight_hc_O_1[i + 3];
682 kptr[24 + 0] = weight_hc_G_0[i];
683 kptr[24 + 1] = weight_hc_G_0[i + 1];
684 kptr[24 + 2] = weight_hc_G_0[i + 2];
685 kptr[24 + 3] = weight_hc_G_0[i + 3];
686 kptr[24 + 4] = weight_hc_G_1[i];
687 kptr[24 + 5] = weight_hc_G_1[i + 1];
688 kptr[24 + 6] = weight_hc_G_1[i + 2];
689 kptr[24 + 7] = weight_hc_G_1[i + 3];
690 kptr[32 + 0] = weight_hc_I_2[i];
691 kptr[32 + 1] = weight_hc_I_2[i + 1];
692 kptr[32 + 2] = weight_hc_I_2[i + 2];
693 kptr[32 + 3] = weight_hc_I_2[i + 3];
694 kptr[32 + 4] = weight_hc_I_3[i];
695 kptr[32 + 5] = weight_hc_I_3[i + 1];
696 kptr[32 + 6] = weight_hc_I_3[i + 2];
697 kptr[32 + 7] = weight_hc_I_3[i + 3];
698 kptr[40 + 0] = weight_hc_F_2[i];
699 kptr[40 + 1] = weight_hc_F_2[i + 1];
700 kptr[40 + 2] = weight_hc_F_2[i + 2];
701 kptr[40 + 3] = weight_hc_F_2[i + 3];
702 kptr[40 + 4] = weight_hc_F_3[i];
703 kptr[40 + 5] = weight_hc_F_3[i + 1];
704 kptr[40 + 6] = weight_hc_F_3[i + 2];
705 kptr[40 + 7] = weight_hc_F_3[i + 3];
706 kptr[48 + 0] = weight_hc_O_2[i];
707 kptr[48 + 1] = weight_hc_O_2[i + 1];
708 kptr[48 + 2] = weight_hc_O_2[i + 2];
709 kptr[48 + 3] = weight_hc_O_2[i + 3];
710 kptr[48 + 4] = weight_hc_O_3[i];
711 kptr[48 + 5] = weight_hc_O_3[i + 1];
712 kptr[48 + 6] = weight_hc_O_3[i + 2];
713 kptr[48 + 7] = weight_hc_O_3[i + 3];
714 kptr[56 + 0] = weight_hc_G_2[i];
715 kptr[56 + 1] = weight_hc_G_2[i + 1];
716 kptr[56 + 2] = weight_hc_G_2[i + 2];
717 kptr[56 + 3] = weight_hc_G_2[i + 3];
718 kptr[56 + 4] = weight_hc_G_3[i];
719 kptr[56 + 5] = weight_hc_G_3[i + 1];
720 kptr[56 + 6] = weight_hc_G_3[i + 2];
721 kptr[56 + 7] = weight_hc_G_3[i + 3];
722 kptr += 64;
723 }
724#endif // __AVX512VNNI__
725 for (; i + 1 < num_output; i += 2)
726 {
727 kptr[0] = weight_hc_I_0[i];
728 kptr[1] = weight_hc_I_0[i + 1];
729 kptr[2] = weight_hc_I_1[i];
730 kptr[3] = weight_hc_I_1[i + 1];
731 kptr[4] = weight_hc_I_2[i];
732 kptr[5] = weight_hc_I_2[i + 1];
733 kptr[6] = weight_hc_I_3[i];
734 kptr[7] = weight_hc_I_3[i + 1];
735 kptr[8 + 0] = weight_hc_F_0[i];
736 kptr[8 + 1] = weight_hc_F_0[i + 1];
737 kptr[8 + 2] = weight_hc_F_1[i];
738 kptr[8 + 3] = weight_hc_F_1[i + 1];
739 kptr[8 + 4] = weight_hc_F_2[i];
740 kptr[8 + 5] = weight_hc_F_2[i + 1];
741 kptr[8 + 6] = weight_hc_F_3[i];
742 kptr[8 + 7] = weight_hc_F_3[i + 1];
743 kptr[16 + 0] = weight_hc_O_0[i];
744 kptr[16 + 1] = weight_hc_O_0[i + 1];
745 kptr[16 + 2] = weight_hc_O_1[i];
746 kptr[16 + 3] = weight_hc_O_1[i + 1];
747 kptr[16 + 4] = weight_hc_O_2[i];
748 kptr[16 + 5] = weight_hc_O_2[i + 1];
749 kptr[16 + 6] = weight_hc_O_3[i];
750 kptr[16 + 7] = weight_hc_O_3[i + 1];
751 kptr[24 + 0] = weight_hc_G_0[i];
752 kptr[24 + 1] = weight_hc_G_0[i + 1];
753 kptr[24 + 2] = weight_hc_G_1[i];
754 kptr[24 + 3] = weight_hc_G_1[i + 1];
755 kptr[24 + 4] = weight_hc_G_2[i];
756 kptr[24 + 5] = weight_hc_G_2[i + 1];
757 kptr[24 + 6] = weight_hc_G_3[i];
758 kptr[24 + 7] = weight_hc_G_3[i + 1];
759 kptr += 32;
760 }
761 for (; i < num_output; i++)
762 {
763 kptr[0] = weight_hc_I_0[i];
764 kptr[1] = weight_hc_I_1[i];
765 kptr[2] = weight_hc_I_2[i];
766 kptr[3] = weight_hc_I_3[i];
767 kptr[4] = weight_hc_F_0[i];
768 kptr[5] = weight_hc_F_1[i];
769 kptr[6] = weight_hc_F_2[i];
770 kptr[7] = weight_hc_F_3[i];
771 kptr[8 + 0] = weight_hc_O_0[i];
772 kptr[8 + 1] = weight_hc_O_1[i];
773 kptr[8 + 2] = weight_hc_O_2[i];
774 kptr[8 + 3] = weight_hc_O_3[i];
775 kptr[8 + 4] = weight_hc_G_0[i];
776 kptr[8 + 5] = weight_hc_G_1[i];
777 kptr[8 + 6] = weight_hc_G_2[i];
778 kptr[8 + 7] = weight_hc_G_3[i];
779 kptr += 16;
780 }
781
782 _mm_storeu_ps(bias_c_IFOG, _mm_loadu_ps(bias_c_I + q));
783
784 __m128 _descale_xc_I = _mm_loadu_ps(weight_xc_int8_scales_ptr + hidden_size * 0 + q);
785 __m128 _descale_xc_F = _mm_loadu_ps(weight_xc_int8_scales_ptr + hidden_size * 1 + q);
786 __m128 _descale_xc_O = _mm_loadu_ps(weight_xc_int8_scales_ptr + hidden_size * 2 + q);
787 __m128 _descale_xc_G = _mm_loadu_ps(weight_xc_int8_scales_ptr + hidden_size * 3 + q);
788 __m128 _descale_hc_I = _mm_loadu_ps(weight_hc_int8_scales_ptr + hidden_size * 0 + q);
789 __m128 _descale_hc_F = _mm_loadu_ps(weight_hc_int8_scales_ptr + hidden_size * 1 + q);
790 __m128 _descale_hc_O = _mm_loadu_ps(weight_hc_int8_scales_ptr + hidden_size * 2 + q);
791 __m128 _descale_hc_G = _mm_loadu_ps(weight_hc_int8_scales_ptr + hidden_size * 3 + q);
792
793 __m512 _descale_xc_IFOG = _mm512_castps128_ps512(_descale_xc_I);
794 _descale_xc_IFOG = _mm512_insertf32x4(_descale_xc_IFOG, _descale_xc_F, 1);
795 _descale_xc_IFOG = _mm512_insertf32x4(_descale_xc_IFOG, _descale_xc_O, 2);
796 _descale_xc_IFOG = _mm512_insertf32x4(_descale_xc_IFOG, _descale_xc_G, 3);
797 __m512 _descale_hc_IFOG = _mm512_castps128_ps512(_descale_hc_I);
798 _descale_hc_IFOG = _mm512_insertf32x4(_descale_hc_IFOG, _descale_hc_F, 1);
799 _descale_hc_IFOG = _mm512_insertf32x4(_descale_hc_IFOG, _descale_hc_O, 2);
800 _descale_hc_IFOG = _mm512_insertf32x4(_descale_hc_IFOG, _descale_hc_G, 3);
801
802 _descale_xc_IFOG = _mm512_div_ps(_mm512_set1_ps(1.f), _descale_xc_IFOG);
803 _descale_hc_IFOG = _mm512_div_ps(_mm512_set1_ps(1.f), _descale_hc_IFOG);
804
805 _mm512_storeu_ps(descales_ptr, _descale_xc_IFOG);
806 _mm512_storeu_ps(descales_ptr + 16, _descale_hc_IFOG);
807 }
808#endif // __AVX512F__
809 for (; q + 1 < hidden_size; q += 2)
810 {
811 bias_c_IFOG[0] = bias_c_I[q];
812 bias_c_IFOG[1] = bias_c_F[q];
813 bias_c_IFOG[2] = bias_c_O[q];
814 bias_c_IFOG[3] = bias_c_G[q];
815 bias_c_IFOG[4] = bias_c_I[q + 1];
816 bias_c_IFOG[5] = bias_c_F[q + 1];
817 bias_c_IFOG[6] = bias_c_O[q + 1];
818 bias_c_IFOG[7] = bias_c_G[q + 1];
819
820 bias_c_IFOG += 8;
821
822 const signed char* weight_xc_I_0 = weight_xc_dr.row<const signed char>(hidden_size * 0 + q);
823 const signed char* weight_xc_F_0 = weight_xc_dr.row<const signed char>(hidden_size * 1 + q);
824 const signed char* weight_xc_O_0 = weight_xc_dr.row<const signed char>(hidden_size * 2 + q);
825 const signed char* weight_xc_G_0 = weight_xc_dr.row<const signed char>(hidden_size * 3 + q);
826 const signed char* weight_xc_I_1 = weight_xc_dr.row<const signed char>(hidden_size * 0 + q + 1);
827 const signed char* weight_xc_F_1 = weight_xc_dr.row<const signed char>(hidden_size * 1 + q + 1);
828 const signed char* weight_xc_O_1 = weight_xc_dr.row<const signed char>(hidden_size * 2 + q + 1);
829 const signed char* weight_xc_G_1 = weight_xc_dr.row<const signed char>(hidden_size * 3 + q + 1);
830
831 const signed char* weight_hc_I_0 = weight_hc_dr.row<const signed char>(hidden_size * 0 + q);
832 const signed char* weight_hc_F_0 = weight_hc_dr.row<const signed char>(hidden_size * 1 + q);
833 const signed char* weight_hc_O_0 = weight_hc_dr.row<const signed char>(hidden_size * 2 + q);
834 const signed char* weight_hc_G_0 = weight_hc_dr.row<const signed char>(hidden_size * 3 + q);
835 const signed char* weight_hc_I_1 = weight_hc_dr.row<const signed char>(hidden_size * 0 + q + 1);
836 const signed char* weight_hc_F_1 = weight_hc_dr.row<const signed char>(hidden_size * 1 + q + 1);
837 const signed char* weight_hc_O_1 = weight_hc_dr.row<const signed char>(hidden_size * 2 + q + 1);
838 const signed char* weight_hc_G_1 = weight_hc_dr.row<const signed char>(hidden_size * 3 + q + 1);
839
840#if __AVX512F__
841 signed char* kptr = weight_data_tm_dr.row<signed char>(q / 4 + (q % 4) / 2);
842 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 4 + (q % 4) / 2);
843#else
844 signed char* kptr = weight_data_tm_dr.row<signed char>(q / 2);
845 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 2);
846#endif
847
848 int i = 0;
849#if __AVXVNNI__ || __AVX512VNNI__
850 __m256i _w_shift = _mm256_setzero_si256();
851 __m256i _v127 = _mm256_set1_epi8(127);
852
853 __m256i _w0_shift = _mm256_setzero_si256();
854 __m256i _w1_shift = _mm256_setzero_si256();
855#if defined(__x86_64__) || defined(_M_X64)
856 __m256i _w2_shift = _mm256_setzero_si256();
857 __m256i _w3_shift = _mm256_setzero_si256();
858 for (; i + 15 < size; i += 16)
859 {
860 _mm_storeu_si128((__m128i*)kptr, _mm_loadu_si128((const __m128i*)(weight_xc_I_0 + i)));
861 _mm_storeu_si128((__m128i*)(kptr + 16), _mm_loadu_si128((const __m128i*)(weight_xc_I_1 + i)));
862 _mm_storeu_si128((__m128i*)(kptr + 16 * 2), _mm_loadu_si128((const __m128i*)(weight_xc_F_0 + i)));
863 _mm_storeu_si128((__m128i*)(kptr + 16 * 3), _mm_loadu_si128((const __m128i*)(weight_xc_F_1 + i)));
864 _mm_storeu_si128((__m128i*)(kptr + 16 * 4), _mm_loadu_si128((const __m128i*)(weight_xc_O_0 + i)));
865 _mm_storeu_si128((__m128i*)(kptr + 16 * 5), _mm_loadu_si128((const __m128i*)(weight_xc_O_1 + i)));
866 _mm_storeu_si128((__m128i*)(kptr + 16 * 6), _mm_loadu_si128((const __m128i*)(weight_xc_G_0 + i)));
867 _mm_storeu_si128((__m128i*)(kptr + 16 * 7), _mm_loadu_si128((const __m128i*)(weight_xc_G_1 + i)));
868
869 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
870 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
871 __m256i _w2 = _mm256_loadu_si256((const __m256i*)(kptr + 64));
872 __m256i _w3 = _mm256_loadu_si256((const __m256i*)(kptr + 96));
873 _w0_shift = _mm256_dpbusd_epi32(_w0_shift, _v127, _w0);
874 _w1_shift = _mm256_dpbusd_epi32(_w1_shift, _v127, _w1);
875 _w2_shift = _mm256_dpbusd_epi32(_w2_shift, _v127, _w2);
876 _w3_shift = _mm256_dpbusd_epi32(_w3_shift, _v127, _w3);
877
878 kptr += 128;
879 }
880 {
881 __m256i _tmp0 = _mm256_hadd_epi32(_w0_shift, _w1_shift);
882 __m256i _tmp1 = _mm256_hadd_epi32(_w2_shift, _w3_shift);
883 _tmp0 = _mm256_hadd_epi32(_tmp0, _tmp1);
884 _w_shift = _mm256_add_epi32(_w_shift, _tmp0);
885 }
886
887 _w0_shift = _mm256_setzero_si256();
888 _w1_shift = _mm256_setzero_si256();
889#endif // defined(__x86_64__) || defined(_M_X64)
890 for (; i + 7 < size; i += 8)
891 {
892 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_xc_I_0 + i)));
893 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_xc_F_0 + i)));
894 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_xc_I_1 + i)));
895 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_xc_F_1 + i)));
896 _mm_storel_epi64((__m128i*)(kptr + 32), _mm_loadl_epi64((const __m128i*)(weight_xc_O_0 + i)));
897 _mm_storel_epi64((__m128i*)(kptr + 40), _mm_loadl_epi64((const __m128i*)(weight_xc_G_0 + i)));
898 _mm_storel_epi64((__m128i*)(kptr + 48), _mm_loadl_epi64((const __m128i*)(weight_xc_O_1 + i)));
899 _mm_storel_epi64((__m128i*)(kptr + 56), _mm_loadl_epi64((const __m128i*)(weight_xc_G_1 + i)));
900
901 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
902 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
903 _w0_shift = _mm256_dpbusd_epi32(_w0_shift, _v127, _w0);
904 _w1_shift = _mm256_dpbusd_epi32(_w1_shift, _v127, _w1);
905
906 kptr += 64;
907 }
908 {
909 __m256i _tmp0 = _mm256_hadd_epi32(_w0_shift, _w1_shift);
910 _w_shift = _mm256_add_epi32(_w_shift, _tmp0);
911 }
912
913 for (; i + 3 < size; i += 4)
914 {
915 kptr[0] = weight_xc_I_0[i];
916 kptr[1] = weight_xc_I_0[i + 1];
917 kptr[2] = weight_xc_I_0[i + 2];
918 kptr[3] = weight_xc_I_0[i + 3];
919 kptr[4] = weight_xc_F_0[i];
920 kptr[5] = weight_xc_F_0[i + 1];
921 kptr[6] = weight_xc_F_0[i + 2];
922 kptr[7] = weight_xc_F_0[i + 3];
923 kptr[8 + 0] = weight_xc_O_0[i];
924 kptr[8 + 1] = weight_xc_O_0[i + 1];
925 kptr[8 + 2] = weight_xc_O_0[i + 2];
926 kptr[8 + 3] = weight_xc_O_0[i + 3];
927 kptr[8 + 4] = weight_xc_G_0[i];
928 kptr[8 + 5] = weight_xc_G_0[i + 1];
929 kptr[8 + 6] = weight_xc_G_0[i + 2];
930 kptr[8 + 7] = weight_xc_G_0[i + 3];
931 kptr[16 + 0] = weight_xc_I_1[i];
932 kptr[16 + 1] = weight_xc_I_1[i + 1];
933 kptr[16 + 2] = weight_xc_I_1[i + 2];
934 kptr[16 + 3] = weight_xc_I_1[i + 3];
935 kptr[16 + 4] = weight_xc_F_1[i];
936 kptr[16 + 5] = weight_xc_F_1[i + 1];
937 kptr[16 + 6] = weight_xc_F_1[i + 2];
938 kptr[16 + 7] = weight_xc_F_1[i + 3];
939 kptr[24 + 0] = weight_xc_O_1[i];
940 kptr[24 + 1] = weight_xc_O_1[i + 1];
941 kptr[24 + 2] = weight_xc_O_1[i + 2];
942 kptr[24 + 3] = weight_xc_O_1[i + 3];
943 kptr[24 + 4] = weight_xc_G_1[i];
944 kptr[24 + 5] = weight_xc_G_1[i + 1];
945 kptr[24 + 6] = weight_xc_G_1[i + 2];
946 kptr[24 + 7] = weight_xc_G_1[i + 3];
947
948 __m256i _w = _mm256_loadu_si256((const __m256i*)kptr);
949 _w_shift = _mm256_dpbusd_epi32(_w_shift, _v127, _w);
950
951 kptr += 32;
952 }
953
954 _mm256_storeu_si256((__m256i*)kptr, _w_shift);
955 kptr += 32;
956#else
957#if defined(__x86_64__) || defined(_M_X64)
958 for (; i + 7 < size; i += 8)
959 {
960 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_xc_I_0 + i)));
961 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_xc_I_1 + i)));
962 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_xc_F_0 + i)));
963 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_xc_F_1 + i)));
964 _mm_storel_epi64((__m128i*)(kptr + 32), _mm_loadl_epi64((const __m128i*)(weight_xc_O_0 + i)));
965 _mm_storel_epi64((__m128i*)(kptr + 40), _mm_loadl_epi64((const __m128i*)(weight_xc_O_1 + i)));
966 _mm_storel_epi64((__m128i*)(kptr + 48), _mm_loadl_epi64((const __m128i*)(weight_xc_G_0 + i)));
967 _mm_storel_epi64((__m128i*)(kptr + 56), _mm_loadl_epi64((const __m128i*)(weight_xc_G_1 + i)));
968 kptr += 64;
969 }
970#endif // defined(__x86_64__) || defined(_M_X64)
971 for (; i + 3 < size; i += 4)
972 {
973 kptr[0] = weight_xc_I_0[i];
974 kptr[1] = weight_xc_I_0[i + 1];
975 kptr[2] = weight_xc_I_0[i + 2];
976 kptr[3] = weight_xc_I_0[i + 3];
977 kptr[4] = weight_xc_F_0[i];
978 kptr[5] = weight_xc_F_0[i + 1];
979 kptr[6] = weight_xc_F_0[i + 2];
980 kptr[7] = weight_xc_F_0[i + 3];
981 kptr[8 + 0] = weight_xc_I_1[i];
982 kptr[8 + 1] = weight_xc_I_1[i + 1];
983 kptr[8 + 2] = weight_xc_I_1[i + 2];
984 kptr[8 + 3] = weight_xc_I_1[i + 3];
985 kptr[8 + 4] = weight_xc_F_1[i];
986 kptr[8 + 5] = weight_xc_F_1[i + 1];
987 kptr[8 + 6] = weight_xc_F_1[i + 2];
988 kptr[8 + 7] = weight_xc_F_1[i + 3];
989 kptr[16 + 0] = weight_xc_O_0[i];
990 kptr[16 + 1] = weight_xc_O_0[i + 1];
991 kptr[16 + 2] = weight_xc_O_0[i + 2];
992 kptr[16 + 3] = weight_xc_O_0[i + 3];
993 kptr[16 + 4] = weight_xc_G_0[i];
994 kptr[16 + 5] = weight_xc_G_0[i + 1];
995 kptr[16 + 6] = weight_xc_G_0[i + 2];
996 kptr[16 + 7] = weight_xc_G_0[i + 3];
997 kptr[24 + 0] = weight_xc_O_1[i];
998 kptr[24 + 1] = weight_xc_O_1[i + 1];
999 kptr[24 + 2] = weight_xc_O_1[i + 2];
1000 kptr[24 + 3] = weight_xc_O_1[i + 3];
1001 kptr[24 + 4] = weight_xc_G_1[i];
1002 kptr[24 + 5] = weight_xc_G_1[i + 1];
1003 kptr[24 + 6] = weight_xc_G_1[i + 2];
1004 kptr[24 + 7] = weight_xc_G_1[i + 3];
1005 kptr += 32;
1006 }
1007#endif // __AVXVNNI__ || __AVX512VNNI__
1008 for (; i + 1 < size; i += 2)
1009 {
1010 kptr[0] = weight_xc_I_0[i];
1011 kptr[1] = weight_xc_I_0[i + 1];
1012 kptr[2] = weight_xc_F_0[i];
1013 kptr[3] = weight_xc_F_0[i + 1];
1014 kptr[4] = weight_xc_O_0[i];
1015 kptr[5] = weight_xc_O_0[i + 1];
1016 kptr[6] = weight_xc_G_0[i];
1017 kptr[7] = weight_xc_G_0[i + 1];
1018 kptr[8 + 0] = weight_xc_I_1[i];
1019 kptr[8 + 1] = weight_xc_I_1[i + 1];
1020 kptr[8 + 2] = weight_xc_F_1[i];
1021 kptr[8 + 3] = weight_xc_F_1[i + 1];
1022 kptr[8 + 4] = weight_xc_O_1[i];
1023 kptr[8 + 5] = weight_xc_O_1[i + 1];
1024 kptr[8 + 6] = weight_xc_G_1[i];
1025 kptr[8 + 7] = weight_xc_G_1[i + 1];
1026 kptr += 16;
1027 }
1028 for (; i < size; i++)
1029 {
1030 kptr[0] = weight_xc_I_0[i];
1031 kptr[1] = weight_xc_F_0[i];
1032 kptr[2] = weight_xc_O_0[i];
1033 kptr[3] = weight_xc_G_0[i];
1034 kptr[4] = weight_xc_I_1[i];
1035 kptr[5] = weight_xc_F_1[i];
1036 kptr[6] = weight_xc_O_1[i];
1037 kptr[7] = weight_xc_G_1[i];
1038 kptr += 8;
1039 }
1040
1041 i = 0;
1042#if __AVXVNNI__ || __AVX512VNNI__
1043 _w_shift = _mm256_setzero_si256();
1044 _v127 = _mm256_set1_epi8(127);
1045 _w0_shift = _mm256_setzero_si256();
1046 _w1_shift = _mm256_setzero_si256();
1047#if defined(__x86_64__) || defined(_M_X64)
1048 _w2_shift = _mm256_setzero_si256();
1049 _w3_shift = _mm256_setzero_si256();
1050 for (; i + 15 < num_output; i += 16)
1051 {
1052 _mm_storeu_si128((__m128i*)kptr, _mm_loadu_si128((const __m128i*)(weight_hc_I_0 + i)));
1053 _mm_storeu_si128((__m128i*)(kptr + 16), _mm_loadu_si128((const __m128i*)(weight_hc_I_1 + i)));
1054 _mm_storeu_si128((__m128i*)(kptr + 16 * 2), _mm_loadu_si128((const __m128i*)(weight_hc_F_0 + i)));
1055 _mm_storeu_si128((__m128i*)(kptr + 16 * 3), _mm_loadu_si128((const __m128i*)(weight_hc_F_1 + i)));
1056 _mm_storeu_si128((__m128i*)(kptr + 16 * 4), _mm_loadu_si128((const __m128i*)(weight_hc_O_0 + i)));
1057 _mm_storeu_si128((__m128i*)(kptr + 16 * 5), _mm_loadu_si128((const __m128i*)(weight_hc_O_1 + i)));
1058 _mm_storeu_si128((__m128i*)(kptr + 16 * 6), _mm_loadu_si128((const __m128i*)(weight_hc_G_0 + i)));
1059 _mm_storeu_si128((__m128i*)(kptr + 16 * 7), _mm_loadu_si128((const __m128i*)(weight_hc_G_1 + i)));
1060
1061 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
1062 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
1063 __m256i _w2 = _mm256_loadu_si256((const __m256i*)(kptr + 64));
1064 __m256i _w3 = _mm256_loadu_si256((const __m256i*)(kptr + 96));
1065 _w0_shift = _mm256_dpbusd_epi32(_w0_shift, _v127, _w0);
1066 _w1_shift = _mm256_dpbusd_epi32(_w1_shift, _v127, _w1);
1067 _w2_shift = _mm256_dpbusd_epi32(_w2_shift, _v127, _w2);
1068 _w3_shift = _mm256_dpbusd_epi32(_w3_shift, _v127, _w3);
1069
1070 kptr += 128;
1071 }
1072 {
1073 __m256i _tmp0 = _mm256_hadd_epi32(_w0_shift, _w1_shift);
1074 __m256i _tmp1 = _mm256_hadd_epi32(_w2_shift, _w3_shift);
1075 _tmp0 = _mm256_hadd_epi32(_tmp0, _tmp1);
1076 _w_shift = _mm256_add_epi32(_w_shift, _tmp0);
1077 }
1078
1079 _w0_shift = _mm256_setzero_si256();
1080 _w1_shift = _mm256_setzero_si256();
1081#endif // defined(__x86_64__) || defined(_M_X64)
1082 for (; i + 7 < num_output; i += 8)
1083 {
1084 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_hc_I_0 + i)));
1085 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_hc_F_0 + i)));
1086 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_hc_I_1 + i)));
1087 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_hc_F_1 + i)));
1088 _mm_storel_epi64((__m128i*)(kptr + 32), _mm_loadl_epi64((const __m128i*)(weight_hc_O_0 + i)));
1089 _mm_storel_epi64((__m128i*)(kptr + 40), _mm_loadl_epi64((const __m128i*)(weight_hc_G_0 + i)));
1090 _mm_storel_epi64((__m128i*)(kptr + 48), _mm_loadl_epi64((const __m128i*)(weight_hc_O_1 + i)));
1091 _mm_storel_epi64((__m128i*)(kptr + 56), _mm_loadl_epi64((const __m128i*)(weight_hc_G_1 + i)));
1092
1093 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
1094 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
1095 _w0_shift = _mm256_dpbusd_epi32(_w0_shift, _v127, _w0);
1096 _w1_shift = _mm256_dpbusd_epi32(_w1_shift, _v127, _w1);
1097
1098 kptr += 64;
1099 }
1100 {
1101 __m256i _tmp0 = _mm256_hadd_epi32(_w0_shift, _w1_shift);
1102 _w_shift = _mm256_add_epi32(_w_shift, _tmp0);
1103 }
1104
1105 for (; i + 3 < num_output; i += 4)
1106 {
1107 kptr[0] = weight_hc_I_0[i];
1108 kptr[1] = weight_hc_I_0[i + 1];
1109 kptr[2] = weight_hc_I_0[i + 2];
1110 kptr[3] = weight_hc_I_0[i + 3];
1111 kptr[4] = weight_hc_F_0[i];
1112 kptr[5] = weight_hc_F_0[i + 1];
1113 kptr[6] = weight_hc_F_0[i + 2];
1114 kptr[7] = weight_hc_F_0[i + 3];
1115 kptr[8 + 0] = weight_hc_O_0[i];
1116 kptr[8 + 1] = weight_hc_O_0[i + 1];
1117 kptr[8 + 2] = weight_hc_O_0[i + 2];
1118 kptr[8 + 3] = weight_hc_O_0[i + 3];
1119 kptr[8 + 4] = weight_hc_G_0[i];
1120 kptr[8 + 5] = weight_hc_G_0[i + 1];
1121 kptr[8 + 6] = weight_hc_G_0[i + 2];
1122 kptr[8 + 7] = weight_hc_G_0[i + 3];
1123 kptr[16 + 0] = weight_hc_I_1[i];
1124 kptr[16 + 1] = weight_hc_I_1[i + 1];
1125 kptr[16 + 2] = weight_hc_I_1[i + 2];
1126 kptr[16 + 3] = weight_hc_I_1[i + 3];
1127 kptr[16 + 4] = weight_hc_F_1[i];
1128 kptr[16 + 5] = weight_hc_F_1[i + 1];
1129 kptr[16 + 6] = weight_hc_F_1[i + 2];
1130 kptr[16 + 7] = weight_hc_F_1[i + 3];
1131 kptr[24 + 0] = weight_hc_O_1[i];
1132 kptr[24 + 1] = weight_hc_O_1[i + 1];
1133 kptr[24 + 2] = weight_hc_O_1[i + 2];
1134 kptr[24 + 3] = weight_hc_O_1[i + 3];
1135 kptr[24 + 4] = weight_hc_G_1[i];
1136 kptr[24 + 5] = weight_hc_G_1[i + 1];
1137 kptr[24 + 6] = weight_hc_G_1[i + 2];
1138 kptr[24 + 7] = weight_hc_G_1[i + 3];
1139
1140 __m256i _w = _mm256_loadu_si256((const __m256i*)kptr);
1141 _w_shift = _mm256_dpbusd_epi32(_w_shift, _v127, _w);
1142
1143 kptr += 32;
1144 }
1145
1146 _mm256_storeu_si256((__m256i*)kptr, _w_shift);
1147 kptr += 32;
1148#else
1149#if defined(__x86_64__) || defined(_M_X64)
1150 for (; i + 7 < num_output; i += 8)
1151 {
1152 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_hc_I_0 + i)));
1153 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_hc_I_1 + i)));
1154 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_hc_F_0 + i)));
1155 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_hc_F_1 + i)));
1156 _mm_storel_epi64((__m128i*)(kptr + 32), _mm_loadl_epi64((const __m128i*)(weight_hc_O_0 + i)));
1157 _mm_storel_epi64((__m128i*)(kptr + 40), _mm_loadl_epi64((const __m128i*)(weight_hc_O_1 + i)));
1158 _mm_storel_epi64((__m128i*)(kptr + 48), _mm_loadl_epi64((const __m128i*)(weight_hc_G_0 + i)));
1159 _mm_storel_epi64((__m128i*)(kptr + 56), _mm_loadl_epi64((const __m128i*)(weight_hc_G_1 + i)));
1160 kptr += 64;
1161 }
1162#endif // defined(__x86_64__) || defined(_M_X64)
1163 for (; i + 3 < num_output; i += 4)
1164 {
1165 kptr[0] = weight_hc_I_0[i];
1166 kptr[1] = weight_hc_I_0[i + 1];
1167 kptr[2] = weight_hc_I_0[i + 2];
1168 kptr[3] = weight_hc_I_0[i + 3];
1169 kptr[4] = weight_hc_F_0[i];
1170 kptr[5] = weight_hc_F_0[i + 1];
1171 kptr[6] = weight_hc_F_0[i + 2];
1172 kptr[7] = weight_hc_F_0[i + 3];
1173 kptr[8 + 0] = weight_hc_I_1[i];
1174 kptr[8 + 1] = weight_hc_I_1[i + 1];
1175 kptr[8 + 2] = weight_hc_I_1[i + 2];
1176 kptr[8 + 3] = weight_hc_I_1[i + 3];
1177 kptr[8 + 4] = weight_hc_F_1[i];
1178 kptr[8 + 5] = weight_hc_F_1[i + 1];
1179 kptr[8 + 6] = weight_hc_F_1[i + 2];
1180 kptr[8 + 7] = weight_hc_F_1[i + 3];
1181 kptr[16 + 0] = weight_hc_O_0[i];
1182 kptr[16 + 1] = weight_hc_O_0[i + 1];
1183 kptr[16 + 2] = weight_hc_O_0[i + 2];
1184 kptr[16 + 3] = weight_hc_O_0[i + 3];
1185 kptr[16 + 4] = weight_hc_G_0[i];
1186 kptr[16 + 5] = weight_hc_G_0[i + 1];
1187 kptr[16 + 6] = weight_hc_G_0[i + 2];
1188 kptr[16 + 7] = weight_hc_G_0[i + 3];
1189 kptr[24 + 0] = weight_hc_O_1[i];
1190 kptr[24 + 1] = weight_hc_O_1[i + 1];
1191 kptr[24 + 2] = weight_hc_O_1[i + 2];
1192 kptr[24 + 3] = weight_hc_O_1[i + 3];
1193 kptr[24 + 4] = weight_hc_G_1[i];
1194 kptr[24 + 5] = weight_hc_G_1[i + 1];
1195 kptr[24 + 6] = weight_hc_G_1[i + 2];
1196 kptr[24 + 7] = weight_hc_G_1[i + 3];
1197 kptr += 32;
1198 }
1199#endif // __AVXVNNI__ || __AVX512VNNI__
1200 for (; i + 1 < num_output; i += 2)
1201 {
1202 kptr[0] = weight_hc_I_0[i];
1203 kptr[1] = weight_hc_I_0[i + 1];
1204 kptr[2] = weight_hc_F_0[i];
1205 kptr[3] = weight_hc_F_0[i + 1];
1206 kptr[4] = weight_hc_O_0[i];
1207 kptr[5] = weight_hc_O_0[i + 1];
1208 kptr[6] = weight_hc_G_0[i];
1209 kptr[7] = weight_hc_G_0[i + 1];
1210 kptr[8 + 0] = weight_hc_I_1[i];
1211 kptr[8 + 1] = weight_hc_I_1[i + 1];
1212 kptr[8 + 2] = weight_hc_F_1[i];
1213 kptr[8 + 3] = weight_hc_F_1[i + 1];
1214 kptr[8 + 4] = weight_hc_O_1[i];
1215 kptr[8 + 5] = weight_hc_O_1[i + 1];
1216 kptr[8 + 6] = weight_hc_G_1[i];
1217 kptr[8 + 7] = weight_hc_G_1[i + 1];
1218 kptr += 16;
1219 }
1220 for (; i < num_output; i++)
1221 {
1222 kptr[0] = weight_hc_I_0[i];
1223 kptr[1] = weight_hc_F_0[i];
1224 kptr[2] = weight_hc_O_0[i];
1225 kptr[3] = weight_hc_G_0[i];
1226 kptr[4] = weight_hc_I_1[i];
1227 kptr[5] = weight_hc_F_1[i];
1228 kptr[6] = weight_hc_O_1[i];
1229 kptr[7] = weight_hc_G_1[i];
1230 kptr += 8;
1231 }
1232
1233 descales_ptr[0] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 0 + q];
1234 descales_ptr[1] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 1 + q];
1235 descales_ptr[2] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 2 + q];
1236 descales_ptr[3] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 3 + q];
1237 descales_ptr[4] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 0 + q + 1];
1238 descales_ptr[5] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 1 + q + 1];
1239 descales_ptr[6] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 2 + q + 1];
1240 descales_ptr[7] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 3 + q + 1];
1241 descales_ptr[8 + 0] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 0 + q];
1242 descales_ptr[8 + 1] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 1 + q];
1243 descales_ptr[8 + 2] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 2 + q];
1244 descales_ptr[8 + 3] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 3 + q];
1245 descales_ptr[8 + 4] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 0 + q + 1];
1246 descales_ptr[8 + 5] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 1 + q + 1];
1247 descales_ptr[8 + 6] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 2 + q + 1];
1248 descales_ptr[8 + 7] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 3 + q + 1];
1249 }
1250#endif // __AVX2__
1251 for (; q < hidden_size; q++)
1252 {
1253 bias_c_IFOG[0] = bias_c_I[q];
1254 bias_c_IFOG[1] = bias_c_F[q];
1255 bias_c_IFOG[2] = bias_c_O[q];
1256 bias_c_IFOG[3] = bias_c_G[q];
1257
1258 bias_c_IFOG += 4;
1259
1260 const signed char* weight_xc_I = weight_xc_dr.row<const signed char>(hidden_size * 0 + q);
1261 const signed char* weight_xc_F = weight_xc_dr.row<const signed char>(hidden_size * 1 + q);
1262 const signed char* weight_xc_O = weight_xc_dr.row<const signed char>(hidden_size * 2 + q);
1263 const signed char* weight_xc_G = weight_xc_dr.row<const signed char>(hidden_size * 3 + q);
1264
1265 const signed char* weight_hc_I = weight_hc_dr.row<const signed char>(hidden_size * 0 + q);
1266 const signed char* weight_hc_F = weight_hc_dr.row<const signed char>(hidden_size * 1 + q);
1267 const signed char* weight_hc_O = weight_hc_dr.row<const signed char>(hidden_size * 2 + q);
1268 const signed char* weight_hc_G = weight_hc_dr.row<const signed char>(hidden_size * 3 + q);
1269
1270#if __AVX512F__
1271 signed char* kptr = weight_data_tm_dr.row<signed char>(q / 4 + (q % 4) / 2 + q % 2);
1272 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 4 + (q % 4) / 2 + q % 2);
1273#elif __AVX2__
1274 signed char* kptr = weight_data_tm_dr.row<signed char>(q / 2 + q % 2);
1275 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 2 + q % 2);
1276#else
1277 signed char* kptr = weight_data_tm_dr.row<signed char>(q);
1278 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q);
1279#endif
1280
1281 int i = 0;
1282#if __SSE2__
1283#if __AVXVNNI__ || __AVX512VNNI__
1284 __m128i _w_shift = _mm_setzero_si128();
1285 __m128i _v127 = _mm_set1_epi8(127);
1286 __m128i _w0_shift = _mm_setzero_si128();
1287 __m128i _w1_shift = _mm_setzero_si128();
1288#if defined(__x86_64__) || defined(_M_X64)
1289 __m128i _w2_shift = _mm_setzero_si128();
1290 __m128i _w3_shift = _mm_setzero_si128();
1291 for (; i + 15 < size; i += 16)
1292 {
1293 _mm_storeu_si128((__m128i*)kptr, _mm_loadu_si128((const __m128i*)(weight_xc_I + i)));
1294 _mm_storeu_si128((__m128i*)(kptr + 16), _mm_loadu_si128((const __m128i*)(weight_xc_F + i)));
1295 _mm_storeu_si128((__m128i*)(kptr + 32), _mm_loadu_si128((const __m128i*)(weight_xc_O + i)));
1296 _mm_storeu_si128((__m128i*)(kptr + 48), _mm_loadu_si128((const __m128i*)(weight_xc_G + i)));
1297
1298 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
1299 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
1300 __m128i _w2 = _mm_loadu_si128((const __m128i*)(kptr + 32));
1301 __m128i _w3 = _mm_loadu_si128((const __m128i*)(kptr + 48));
1302 _w0_shift = _mm_dpbusd_epi32(_w0_shift, _v127, _w0);
1303 _w1_shift = _mm_dpbusd_epi32(_w1_shift, _v127, _w1);
1304 _w2_shift = _mm_dpbusd_epi32(_w2_shift, _v127, _w2);
1305 _w3_shift = _mm_dpbusd_epi32(_w3_shift, _v127, _w3);
1306
1307 kptr += 64;
1308 }
1309 {
1310 transpose4x4_epi32(_w0_shift, _w1_shift, _w2_shift, _w3_shift);
1311 _w_shift = _mm_add_epi32(_w_shift, _w0_shift);
1312 _w_shift = _mm_add_epi32(_w_shift, _w1_shift);
1313 _w_shift = _mm_add_epi32(_w_shift, _w2_shift);
1314 _w_shift = _mm_add_epi32(_w_shift, _w3_shift);
1315 }
1316
1317 _w0_shift = _mm_setzero_si128();
1318 _w1_shift = _mm_setzero_si128();
1319#endif // defined(__x86_64__) || defined(_M_X64)
1320 for (; i + 7 < size; i += 8)
1321 {
1322 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_xc_I + i)));
1323 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_xc_F + i)));
1324 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_xc_O + i)));
1325 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_xc_G + i)));
1326
1327 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
1328 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
1329 _w0_shift = _mm_dpbusd_epi32(_w0_shift, _v127, _w0);
1330 _w1_shift = _mm_dpbusd_epi32(_w1_shift, _v127, _w1);
1331
1332 kptr += 32;
1333 }
1334 {
1335 __m128i _tmp0 = _mm_hadd_epi32(_w0_shift, _w1_shift);
1336 _w_shift = _mm_add_epi32(_w_shift, _tmp0);
1337 }
1338
1339 for (; i + 3 < size; i += 4)
1340 {
1341 kptr[0] = weight_xc_I[i];
1342 kptr[1] = weight_xc_I[i + 1];
1343 kptr[2] = weight_xc_I[i + 2];
1344 kptr[3] = weight_xc_I[i + 3];
1345 kptr[4] = weight_xc_F[i];
1346 kptr[5] = weight_xc_F[i + 1];
1347 kptr[6] = weight_xc_F[i + 2];
1348 kptr[7] = weight_xc_F[i + 3];
1349 kptr[8 + 0] = weight_xc_O[i];
1350 kptr[8 + 1] = weight_xc_O[i + 1];
1351 kptr[8 + 2] = weight_xc_O[i + 2];
1352 kptr[8 + 3] = weight_xc_O[i + 3];
1353 kptr[8 + 4] = weight_xc_G[i];
1354 kptr[8 + 5] = weight_xc_G[i + 1];
1355 kptr[8 + 6] = weight_xc_G[i + 2];
1356 kptr[8 + 7] = weight_xc_G[i + 3];
1357
1358 __m128i _w = _mm_loadu_si128((const __m128i*)kptr);
1359 _w_shift = _mm_dpbusd_epi32(_w_shift, _v127, _w);
1360
1361 kptr += 16;
1362 }
1363
1364 _mm_storeu_si128((__m128i*)kptr, _w_shift);
1365 kptr += 16;
1366#else
1367#if defined(__x86_64__) || defined(_M_X64)
1368 for (; i + 7 < size; i += 8)
1369 {
1370 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_xc_I + i)));
1371 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_xc_F + i)));
1372 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_xc_O + i)));
1373 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_xc_G + i)));
1374 kptr += 32;
1375 }
1376#endif // defined(__x86_64__) || defined(_M_X64)
1377 for (; i + 3 < size; i += 4)
1378 {
1379 kptr[0] = weight_xc_I[i];
1380 kptr[1] = weight_xc_I[i + 1];
1381 kptr[2] = weight_xc_I[i + 2];
1382 kptr[3] = weight_xc_I[i + 3];
1383 kptr[4] = weight_xc_F[i];
1384 kptr[5] = weight_xc_F[i + 1];
1385 kptr[6] = weight_xc_F[i + 2];
1386 kptr[7] = weight_xc_F[i + 3];
1387 kptr[8 + 0] = weight_xc_O[i];
1388 kptr[8 + 1] = weight_xc_O[i + 1];
1389 kptr[8 + 2] = weight_xc_O[i + 2];
1390 kptr[8 + 3] = weight_xc_O[i + 3];
1391 kptr[8 + 4] = weight_xc_G[i];
1392 kptr[8 + 5] = weight_xc_G[i + 1];
1393 kptr[8 + 6] = weight_xc_G[i + 2];
1394 kptr[8 + 7] = weight_xc_G[i + 3];
1395 kptr += 16;
1396 }
1397#endif // __AVXVNNI__ || __AVX512VNNI__
1398 for (; i + 1 < size; i += 2)
1399 {
1400 kptr[0] = weight_xc_I[i];
1401 kptr[1] = weight_xc_I[i + 1];
1402 kptr[2] = weight_xc_F[i];
1403 kptr[3] = weight_xc_F[i + 1];
1404 kptr[4] = weight_xc_O[i];
1405 kptr[5] = weight_xc_O[i + 1];
1406 kptr[6] = weight_xc_G[i];
1407 kptr[7] = weight_xc_G[i + 1];
1408 kptr += 8;
1409 }
1410#endif // __SSE2__
1411 for (; i < size; i++)
1412 {
1413 kptr[0] = weight_xc_I[i];
1414 kptr[1] = weight_xc_F[i];
1415 kptr[2] = weight_xc_O[i];
1416 kptr[3] = weight_xc_G[i];
1417 kptr += 4;
1418 }
1419
1420 i = 0;
1421#if __SSE2__
1422#if __AVXVNNI__ || __AVX512VNNI__
1423 _w_shift = _mm_setzero_si128();
1424 _w0_shift = _mm_setzero_si128();
1425 _w1_shift = _mm_setzero_si128();
1426#if defined(__x86_64__) || defined(_M_X64)
1427 _w2_shift = _mm_setzero_si128();
1428 _w3_shift = _mm_setzero_si128();
1429 for (; i + 15 < num_output; i += 16)
1430 {
1431 _mm_storeu_si128((__m128i*)kptr, _mm_loadu_si128((const __m128i*)(weight_hc_I + i)));
1432 _mm_storeu_si128((__m128i*)(kptr + 16), _mm_loadu_si128((const __m128i*)(weight_hc_F + i)));
1433 _mm_storeu_si128((__m128i*)(kptr + 32), _mm_loadu_si128((const __m128i*)(weight_hc_O + i)));
1434 _mm_storeu_si128((__m128i*)(kptr + 48), _mm_loadu_si128((const __m128i*)(weight_hc_G + i)));
1435
1436 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
1437 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
1438 __m128i _w2 = _mm_loadu_si128((const __m128i*)(kptr + 32));
1439 __m128i _w3 = _mm_loadu_si128((const __m128i*)(kptr + 48));
1440 _w0_shift = _mm_dpbusd_epi32(_w0_shift, _v127, _w0);
1441 _w1_shift = _mm_dpbusd_epi32(_w1_shift, _v127, _w1);
1442 _w2_shift = _mm_dpbusd_epi32(_w2_shift, _v127, _w2);
1443 _w3_shift = _mm_dpbusd_epi32(_w3_shift, _v127, _w3);
1444
1445 kptr += 64;
1446 }
1447 {
1448 transpose4x4_epi32(_w0_shift, _w1_shift, _w2_shift, _w3_shift);
1449 _w_shift = _mm_add_epi32(_w_shift, _w0_shift);
1450 _w_shift = _mm_add_epi32(_w_shift, _w1_shift);
1451 _w_shift = _mm_add_epi32(_w_shift, _w2_shift);
1452 _w_shift = _mm_add_epi32(_w_shift, _w3_shift);
1453 }
1454
1455 _w0_shift = _mm_setzero_si128();
1456 _w1_shift = _mm_setzero_si128();
1457#endif // defined(__x86_64__) || defined(_M_X64)
1458 for (; i + 7 < num_output; i += 8)
1459 {
1460 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_hc_I + i)));
1461 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_hc_F + i)));
1462 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_hc_O + i)));
1463 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_hc_G + i)));
1464
1465 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
1466 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
1467 _w0_shift = _mm_dpbusd_epi32(_w0_shift, _v127, _w0);
1468 _w1_shift = _mm_dpbusd_epi32(_w1_shift, _v127, _w1);
1469
1470 kptr += 32;
1471 }
1472 {
1473 __m128i _tmp0 = _mm_hadd_epi32(_w0_shift, _w1_shift);
1474 _w_shift = _mm_add_epi32(_w_shift, _tmp0);
1475 }
1476
1477 for (; i + 3 < num_output; i += 4)
1478 {
1479 kptr[0] = weight_hc_I[i];
1480 kptr[1] = weight_hc_I[i + 1];
1481 kptr[2] = weight_hc_I[i + 2];
1482 kptr[3] = weight_hc_I[i + 3];
1483 kptr[4] = weight_hc_F[i];
1484 kptr[5] = weight_hc_F[i + 1];
1485 kptr[6] = weight_hc_F[i + 2];
1486 kptr[7] = weight_hc_F[i + 3];
1487 kptr[8 + 0] = weight_hc_O[i];
1488 kptr[8 + 1] = weight_hc_O[i + 1];
1489 kptr[8 + 2] = weight_hc_O[i + 2];
1490 kptr[8 + 3] = weight_hc_O[i + 3];
1491 kptr[8 + 4] = weight_hc_G[i];
1492 kptr[8 + 5] = weight_hc_G[i + 1];
1493 kptr[8 + 6] = weight_hc_G[i + 2];
1494 kptr[8 + 7] = weight_hc_G[i + 3];
1495
1496 __m128i _w = _mm_loadu_si128((const __m128i*)kptr);
1497 _w_shift = _mm_dpbusd_epi32(_w_shift, _v127, _w);
1498
1499 kptr += 16;
1500 }
1501
1502 _mm_storeu_si128((__m128i*)kptr, _w_shift);
1503 kptr += 16;
1504#else
1505#if defined(__x86_64__) || defined(_M_X64)
1506 for (; i + 7 < num_output; i += 8)
1507 {
1508 _mm_storel_epi64((__m128i*)kptr, _mm_loadl_epi64((const __m128i*)(weight_hc_I + i)));
1509 _mm_storel_epi64((__m128i*)(kptr + 8), _mm_loadl_epi64((const __m128i*)(weight_hc_F + i)));
1510 _mm_storel_epi64((__m128i*)(kptr + 16), _mm_loadl_epi64((const __m128i*)(weight_hc_O + i)));
1511 _mm_storel_epi64((__m128i*)(kptr + 24), _mm_loadl_epi64((const __m128i*)(weight_hc_G + i)));
1512 kptr += 32;
1513 }
1514#endif // defined(__x86_64__) || defined(_M_X64)
1515 for (; i + 3 < num_output; i += 4)
1516 {
1517 kptr[0] = weight_hc_I[i];
1518 kptr[1] = weight_hc_I[i + 1];
1519 kptr[2] = weight_hc_I[i + 2];
1520 kptr[3] = weight_hc_I[i + 3];
1521 kptr[4] = weight_hc_F[i];
1522 kptr[5] = weight_hc_F[i + 1];
1523 kptr[6] = weight_hc_F[i + 2];
1524 kptr[7] = weight_hc_F[i + 3];
1525 kptr[8 + 0] = weight_hc_O[i];
1526 kptr[8 + 1] = weight_hc_O[i + 1];
1527 kptr[8 + 2] = weight_hc_O[i + 2];
1528 kptr[8 + 3] = weight_hc_O[i + 3];
1529 kptr[8 + 4] = weight_hc_G[i];
1530 kptr[8 + 5] = weight_hc_G[i + 1];
1531 kptr[8 + 6] = weight_hc_G[i + 2];
1532 kptr[8 + 7] = weight_hc_G[i + 3];
1533 kptr += 16;
1534 }
1535#endif // __AVXVNNI__ || __AVX512VNNI__
1536 for (; i + 1 < num_output; i += 2)
1537 {
1538 kptr[0] = weight_hc_I[i];
1539 kptr[1] = weight_hc_I[i + 1];
1540 kptr[2] = weight_hc_F[i];
1541 kptr[3] = weight_hc_F[i + 1];
1542 kptr[4] = weight_hc_O[i];
1543 kptr[5] = weight_hc_O[i + 1];
1544 kptr[6] = weight_hc_G[i];
1545 kptr[7] = weight_hc_G[i + 1];
1546 kptr += 8;
1547 }
1548#endif // __SSE2__
1549 for (; i < num_output; i++)
1550 {
1551 kptr[0] = weight_hc_I[i];
1552 kptr[1] = weight_hc_F[i];
1553 kptr[2] = weight_hc_O[i];
1554 kptr[3] = weight_hc_G[i];
1555 kptr += 4;
1556 }
1557
1558 descales_ptr[0] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 0 + q];
1559 descales_ptr[1] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 1 + q];
1560 descales_ptr[2] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 2 + q];
1561 descales_ptr[3] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 3 + q];
1562 descales_ptr[4] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 0 + q];
1563 descales_ptr[5] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 1 + q];
1564 descales_ptr[6] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 2 + q];
1565 descales_ptr[7] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 3 + q];
1566 }
1567 }
1568}
1569
1570static float lstm_dynamic_quantize_get_absmax(const float* ptr, int size)
1571{
1572 float absmax = 0.f;
1573
1574 int i = 0;
1575#if __SSE2__
1576#if __AVX__
1577#if __AVX512F__
1578 __m512 _absmax_avx512 = _mm512_set1_ps(0.f);
1579 for (; i + 15 < size; i += 16)
1580 {
1581 __m512 _p = _mm512_loadu_ps(ptr);
1582 _absmax_avx512 = _mm512_max_ps(_absmax_avx512, abs512_ps(_p));
1583 ptr += 16;
1584 }
1585 absmax = std::max(absmax, _mm512_comp_reduce_max_ps(_absmax_avx512));
1586#endif // __AVX512F__
1587 __m256 _absmax_avx = _mm256_set1_ps(0.f);
1588 for (; i + 7 < size; i += 8)
1589 {
1590 __m256 _p = _mm256_loadu_ps(ptr);
1591 _absmax_avx = _mm256_max_ps(_absmax_avx, abs256_ps(_p));
1592 ptr += 8;
1593 }
1594 absmax = std::max(absmax, _mm256_reduce_max_ps(_absmax_avx));
1595#endif // __AVX__
1596 __m128 _absmax = _mm_set1_ps(0.f);
1597 for (; i + 3 < size; i += 4)
1598 {
1599 __m128 _p = _mm_loadu_ps(ptr);
1600 _absmax = _mm_max_ps(_absmax, abs_ps(_p));
1601 ptr += 4;
1602 }
1603 absmax = std::max(absmax, _mm_reduce_max_ps(_absmax));
1604#endif // __SSE2__
1605 for (; i < size; i++)
1606 {
1607 absmax = std::max(absmax, (float)fabs(*ptr));
1608 ptr++;
1609 }
1610
1611 return absmax;
1612}
1613
1614static void lstm_dynamic_quantize_scale2int8(const float* ptr, int size, float scale, signed char* outptr)
1615{
1616#if NCNN_RUNTIME_CPU && NCNN_AVX512VNNI && __AVX512F__ && !__AVX512VNNI__
1617 if (ncnn::cpu_support_x86_avx512_vnni())
1618 {
1619 lstm_dynamic_quantize_scale2int8_avx512vnni(ptr, size, scale, outptr);
1620 return;
1621 }
1622#endif
1623
1624#if NCNN_RUNTIME_CPU && NCNN_AVXVNNI && __AVX2__ && !__AVX512F__ && !__AVXVNNI__ && !__AVX512VNNI__
1625 if (ncnn::cpu_support_x86_avx_vnni())
1626 {
1627 lstm_dynamic_quantize_scale2int8_avxvnni(ptr, size, scale, outptr);
1628 return;
1629 }
1630#endif
1631
1632 int i = 0;
1633#if __SSE2__
1634#if __AVX__
1635#if __AVX512F__
1636 __m512 _scale_avx512 = _mm512_set1_ps(scale);
1637#if __AVX512VNNI__
1638 __m128i _v127 = _mm_set1_epi8(127);
1639#endif
1640 for (; i + 15 < size; i += 16)
1641 {
1642 __m512 _p = _mm512_loadu_ps(ptr);
1643 _p = _mm512_mul_ps(_p, _scale_avx512);
1644 __m128i _outp = float2int8_avx512(_p);
1645#if __AVX512VNNI__
1646 _outp = _mm_add_epi8(_outp, _v127);
1647#endif
1648 _mm_storeu_si128((__m128i*)outptr, _outp);
1649 ptr += 16;
1650 outptr += 16;
1651 }
1652#endif // __AVX512F__
1653 __m256 _scale_avx = _mm256_set1_ps(scale);
1654 for (; i + 7 < size; i += 8)
1655 {
1656 __m256 _p = _mm256_loadu_ps(ptr);
1657 _p = _mm256_mul_ps(_p, _scale_avx);
1658 *(int64_t*)outptr = float2int8_avx(_p);
1659#if __AVXVNNI__ || __AVX512VNNI__
1660 outptr[0] += 127;
1661 outptr[1] += 127;
1662 outptr[2] += 127;
1663 outptr[3] += 127;
1664 outptr[4] += 127;
1665 outptr[5] += 127;
1666 outptr[6] += 127;
1667 outptr[7] += 127;
1668#endif
1669 ptr += 8;
1670 outptr += 8;
1671 }
1672#endif // __AVX__
1673 __m128 _scale = _mm_set1_ps(scale);
1674 for (; i + 3 < size; i += 4)
1675 {
1676 __m128 _p = _mm_loadu_ps(ptr);
1677 _p = _mm_mul_ps(_p, _scale);
1678 *(int32_t*)outptr = float2int8_sse(_p);
1679#ifndef _MSC_VER
1680 // vs2019 crash on 128bit vnni :L --- nihui
1681#if __AVXVNNI__ || __AVX512VNNI__
1682 outptr[0] += 127;
1683 outptr[1] += 127;
1684 outptr[2] += 127;
1685 outptr[3] += 127;
1686#endif
1687#endif
1688 ptr += 4;
1689 outptr += 4;
1690 }
1691#endif // __SSE2__
1692 for (; i < size; i++)
1693 {
1694 *outptr++ = float2int8(*ptr++ * scale);
1695 }
1696}
1697
1698static void lstm_int8(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt)
1699{
1700#if NCNN_RUNTIME_CPU && NCNN_AVX512VNNI && __AVX512F__ && !__AVX512VNNI__
1701 if (ncnn::cpu_support_x86_avx512_vnni())
1702 {
1703 lstm_int8_avx512vnni(bottom_blob_int8, bottom_blob_int8_descales, top_blob, reverse, weight_data_tm, weight_data_tm_int8_descales, bias_c, weight_hr, hidden_state, cell_state, opt);
1704 return;
1705 }
1706#endif
1707
1708#if NCNN_RUNTIME_CPU && NCNN_AVXVNNI && __AVX2__ && !__AVX512F__ && !__AVXVNNI__ && !__AVX512VNNI__
1709 if (ncnn::cpu_support_x86_avx_vnni())
1710 {
1711 lstm_int8_avxvnni(bottom_blob_int8, bottom_blob_int8_descales, top_blob, reverse, weight_data_tm, weight_data_tm_int8_descales, bias_c, weight_hr, hidden_state, cell_state, opt);
1712 return;
1713 }
1714#endif
1715
1716#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__ && !__AVXVNNI__ && !__AVX512VNNI__
1717 if (ncnn::cpu_support_x86_avx2())
1718 {
1719 lstm_int8_avx2(bottom_blob_int8, bottom_blob_int8_descales, top_blob, reverse, weight_data_tm, weight_data_tm_int8_descales, bias_c, weight_hr, hidden_state, cell_state, opt);
1720 return;
1721 }
1722#endif
1723
1724#if NCNN_RUNTIME_CPU && NCNN_XOP && __SSE2__ && !__XOP__ && !__AVX2__ && !__AVXVNNI__ && !__AVX512VNNI__
1725 if (ncnn::cpu_support_x86_xop())
1726 {
1727 lstm_int8_xop(bottom_blob_int8, bottom_blob_int8_descales, top_blob, reverse, weight_data_tm, weight_data_tm_int8_descales, bias_c, weight_hr, hidden_state, cell_state, opt);
1728 return;
1729 }
1730#endif
1731
1732 int size = bottom_blob_int8.w;
1733 int T = bottom_blob_int8.h;
1734
1735 int num_output = top_blob.w;
1736 int hidden_size = cell_state.w;
1737
1738 // 4 x hidden_size
1739 Mat gates(4, hidden_size, 4u, opt.workspace_allocator);
1740
1741 Mat tmp_hidden_state;
1742 if (num_output != hidden_size)
1743 {
1744 tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator);
1745 }
1746
1747 Mat hidden_state_int8(num_output, (size_t)1u, 1, opt.workspace_allocator);
1748 float hidden_state_int8_descale = 1.f;
1749
1750 // unroll
1751 for (int t = 0; t < T; t++)
1752 {
1753 int ti = reverse ? T - 1 - t : t;
1754
1755 // dynamic quantize hidden_state
1756 {
1757 const float* ptr = hidden_state;
1758
1759 const float absmax = lstm_dynamic_quantize_get_absmax(ptr, num_output);
1760
1761 if (absmax == 0.f)
1762 {
1763#if __AVXVNNI__ || __AVX512VNNI__
1764 signed char* hs = hidden_state_int8;
1765
1766 int i = 0;
1767 __m128i _v127 = _mm_set1_epi8(127);
1768 for (; i + 15 < num_output; i += 16)
1769 {
1770 _mm_storeu_si128((__m128i*)hs, _v127);
1771 hs += 16;
1772 }
1773 for (; i + 7 < num_output; i += 8)
1774 {
1775 hs[0] = 127;
1776 hs[1] = 127;
1777 hs[2] = 127;
1778 hs[3] = 127;
1779 hs[4] = 127;
1780 hs[5] = 127;
1781 hs[6] = 127;
1782 hs[7] = 127;
1783 hs += 8;
1784 }
1785 for (; i + 3 < num_output; i += 4)
1786 {
1787#ifdef _MSC_VER
1788 hs[0] = 0;
1789 hs[1] = 0;
1790 hs[2] = 0;
1791 hs[3] = 0;
1792#else
1793 hs[0] = 127;
1794 hs[1] = 127;
1795 hs[2] = 127;
1796 hs[3] = 127;
1797#endif
1798 hs += 4;
1799 }
1800 for (; i < num_output; i++)
1801 {
1802 hs[0] = 0;
1803 hs += 1;
1804 }
1805#else
1806 hidden_state_int8.fill<signed char>(0);
1807#endif
1808 }
1809 else
1810 {
1811 hidden_state_int8_descale = absmax / 127.f;
1812
1813 signed char* hs = hidden_state_int8;
1814
1815 const float scale = 127.f / absmax;
1816 lstm_dynamic_quantize_scale2int8(ptr, num_output, scale, hs);
1817 }
1818 }
1819
1820 int remain_hidden_size_start = 0;
1821 int nn_hidden_size = 0;
1822#if __AVX2__
1823#if __AVX512F__
1824 nn_hidden_size = hidden_size >> 2;
1825 #pragma omp parallel for num_threads(opt.num_threads)
1826 for (int qq = 0; qq < nn_hidden_size; qq++)
1827 {
1828 int q = qq * 4;
1829
1830 const signed char* x = bottom_blob_int8.row<const signed char>(ti);
1831 const signed char* hs = hidden_state_int8;
1832 const float descale_x = bottom_blob_int8_descales[ti];
1833 const float descale_h = hidden_state_int8_descale;
1834
1835 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
1836
1837 const signed char* kptr = weight_data_tm.row<const signed char>(q / 4);
1838 const float* descales_ptr = weight_data_tm_int8_descales.row(q / 4);
1839
1840 float* gates_data = gates.row(q);
1841
1842 __m512i _lstm_IFOGx0 = _mm512_setzero_si512();
1843 __m512i _sum0 = _mm512_setzero_si512();
1844 __m512i _sum1 = _mm512_setzero_si512();
1845 int i = 0;
1846#if __AVX512VNNI__
1847#if defined(__x86_64__) || defined(_M_X64)
1848 __m512i _sum2 = _mm512_setzero_si512();
1849 __m512i _sum3 = _mm512_setzero_si512();
1850 for (; i + 15 < size; i += 16)
1851 {
1852 __m128i _xi = _mm_loadu_si128((const __m128i*)(x + i));
1853 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
1854 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
1855 __m512i _w2 = _mm512_loadu_si512((const __m512i*)(kptr + 128));
1856 __m512i _w3 = _mm512_loadu_si512((const __m512i*)(kptr + 192));
1857
1858 __m512i _xii = _mm512_broadcast_i32x4(_xi);
1859
1860 _sum0 = _mm512_dpbusd_epi32(_sum0, _xii, _w0);
1861 _sum1 = _mm512_dpbusd_epi32(_sum1, _xii, _w1);
1862 _sum2 = _mm512_dpbusd_epi32(_sum2, _xii, _w2);
1863 _sum3 = _mm512_dpbusd_epi32(_sum3, _xii, _w3);
1864
1865 kptr += 256;
1866 }
1867 {
1868 __m512i _tmp0 = _mm512_unpacklo_epi32(_sum0, _sum1);
1869 __m512i _tmp1 = _mm512_unpackhi_epi32(_sum0, _sum1);
1870 __m512i _tmp2 = _mm512_unpacklo_epi32(_sum2, _sum3);
1871 __m512i _tmp3 = _mm512_unpackhi_epi32(_sum2, _sum3);
1872 _sum0 = _mm512_unpacklo_epi64(_tmp0, _tmp2);
1873 _sum1 = _mm512_unpackhi_epi64(_tmp0, _tmp2);
1874 _sum2 = _mm512_unpacklo_epi64(_tmp1, _tmp3);
1875 _sum3 = _mm512_unpackhi_epi64(_tmp1, _tmp3);
1876
1877 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum0);
1878 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum1);
1879 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum2);
1880 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum3);
1881 }
1882
1883 _sum0 = _mm512_setzero_si512();
1884 _sum1 = _mm512_setzero_si512();
1885#endif // defined(__x86_64__) || defined(_M_X64)
1886 for (; i + 7 < size; i += 8)
1887 {
1888 __m128i _xi = _mm_loadl_epi64((const __m128i*)(x + i));
1889 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
1890 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
1891
1892 __m512i _xii = _mm512_broadcastq_epi64(_xi);
1893
1894 _sum0 = _mm512_dpbusd_epi32(_sum0, _xii, _w0);
1895 _sum1 = _mm512_dpbusd_epi32(_sum1, _xii, _w1);
1896
1897 kptr += 128;
1898 }
1899 {
1900 __m512i _tmp0 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(2, 0, 2, 0)));
1901 __m512i _tmp1 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(3, 1, 3, 1)));
1902
1903 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _tmp0);
1904 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _tmp1);
1905 }
1906
1907 for (; i + 3 < size; i += 4)
1908 {
1909 __m512i _xi = _mm512_set1_epi32(((const int*)(x + i))[0]);
1910 __m512i _w = _mm512_loadu_si512((const __m512i*)kptr);
1911
1912#ifdef _MSC_VER
1913 _xi = _mm512_add_epi32(_xi, _mm512_set1_epi8(127));
1914#endif
1915 _lstm_IFOGx0 = _mm512_dpbusd_epi32(_lstm_IFOGx0, _xi, _w);
1916
1917 kptr += 64;
1918 }
1919 {
1920 __m512i _w_shift = _mm512_loadu_si512((const __m512i*)kptr);
1921 _lstm_IFOGx0 = _mm512_sub_epi32(_lstm_IFOGx0, _w_shift);
1922 kptr += 64;
1923 }
1924#else
1925#if defined(__x86_64__) || defined(_M_X64)
1926 __m512i _sum2 = _mm512_setzero_si512();
1927 __m512i _sum3 = _mm512_setzero_si512();
1928 for (; i + 7 < size; i += 8)
1929 {
1930 __m256i _xi = _mm256_castpd_si256(_mm256_broadcast_sd((const double*)(x + i)));
1931 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
1932 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
1933 __m256i _w2 = _mm256_loadu_si256((const __m256i*)(kptr + 64));
1934 __m256i _w3 = _mm256_loadu_si256((const __m256i*)(kptr + 96));
1935
1936 __m512i _xii = _mm512_cvtepi8_epi16(_xi);
1937 __m512i _ww0 = _mm512_cvtepi8_epi16(_w0);
1938 __m512i _ww1 = _mm512_cvtepi8_epi16(_w1);
1939 __m512i _ww2 = _mm512_cvtepi8_epi16(_w2);
1940 __m512i _ww3 = _mm512_cvtepi8_epi16(_w3);
1941
1942 __m512i _s0 = _mm512_madd_epi16(_ww0, _xii);
1943 __m512i _s1 = _mm512_madd_epi16(_ww1, _xii);
1944 __m512i _s2 = _mm512_madd_epi16(_ww2, _xii);
1945 __m512i _s3 = _mm512_madd_epi16(_ww3, _xii);
1946 _sum0 = _mm512_add_epi32(_sum0, _s0);
1947 _sum1 = _mm512_add_epi32(_sum1, _s1);
1948 _sum2 = _mm512_add_epi32(_sum2, _s2);
1949 _sum3 = _mm512_add_epi32(_sum3, _s3);
1950
1951 kptr += 128;
1952 }
1953 {
1954 __m512i _tmp0 = _mm512_unpacklo_epi32(_sum0, _sum1);
1955 __m512i _tmp1 = _mm512_unpackhi_epi32(_sum0, _sum1);
1956 __m512i _tmp2 = _mm512_unpacklo_epi32(_sum2, _sum3);
1957 __m512i _tmp3 = _mm512_unpackhi_epi32(_sum2, _sum3);
1958 _sum0 = _mm512_unpacklo_epi64(_tmp0, _tmp2);
1959 _sum1 = _mm512_unpackhi_epi64(_tmp0, _tmp2);
1960 _sum2 = _mm512_unpacklo_epi64(_tmp1, _tmp3);
1961 _sum3 = _mm512_unpackhi_epi64(_tmp1, _tmp3);
1962
1963 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum0);
1964 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum1);
1965 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum2);
1966 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _sum3);
1967 }
1968
1969 _sum0 = _mm512_setzero_si512();
1970 _sum1 = _mm512_setzero_si512();
1971#endif // defined(__x86_64__) || defined(_M_X64)
1972 for (; i + 3 < size; i += 4)
1973 {
1974 __m256i _xi = _mm256_castps_si256(_mm256_broadcast_ss((const float*)(x + i)));
1975 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
1976 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
1977
1978 __m512i _xii = _mm512_cvtepi8_epi16(_xi);
1979 __m512i _ww0 = _mm512_cvtepi8_epi16(_w0);
1980 __m512i _ww1 = _mm512_cvtepi8_epi16(_w1);
1981
1982 __m512i _s0 = _mm512_madd_epi16(_ww0, _xii);
1983 __m512i _s1 = _mm512_madd_epi16(_ww1, _xii);
1984 _sum0 = _mm512_add_epi32(_sum0, _s0);
1985 _sum1 = _mm512_add_epi32(_sum1, _s1);
1986
1987 kptr += 64;
1988 }
1989 {
1990 __m512i _tmp0 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(2, 0, 2, 0)));
1991 __m512i _tmp1 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(3, 1, 3, 1)));
1992
1993 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _tmp0);
1994 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _tmp1);
1995 }
1996#endif // __AVX512VNNI__
1997 for (; i + 1 < size; i += 2)
1998 {
1999 __m256i _w = _mm256_loadu_si256((const __m256i*)kptr);
2000 __m256i _xi = _mm256_castps_si256(_mm256_broadcast_ss((const float*)(x + i)));
2001
2002 __m512i _ww = _mm512_cvtepi8_epi16(_w);
2003 __m512i _xixi = _mm512_cvtepi8_epi16(_xi);
2004
2005 __m512i _xixi0 = _mm512_shuffle_epi32(_xixi, _MM_PERM_AAAA);
2006
2007#if __AVX512VNNI__
2008 _lstm_IFOGx0 = _mm512_dpwssd_epi32(_lstm_IFOGx0, _ww, _xixi0);
2009#else
2010 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _mm512_madd_epi16(_ww, _xixi0));
2011#endif // __AVX512VNNI__
2012
2013 kptr += 32;
2014 }
2015 for (; i < size; i++)
2016 {
2017 __m128i _w = _mm_load_si128((const __m128i*)kptr);
2018 __m256i _xi = _mm256_set1_epi16(x[i]);
2019
2020 __m256i _ww = _mm256_cvtepi8_epi16(_w);
2021
2022 __m512i _s0 = _mm512_cvtepi16_epi32(_mm256_mullo_epi16(_ww, _xi));
2023
2024 _lstm_IFOGx0 = _mm512_add_epi32(_lstm_IFOGx0, _s0);
2025
2026 kptr += 16;
2027 }
2028
2029 __m512i _lstm_IFOGh0 = _mm512_setzero_si512();
2030 _sum0 = _mm512_setzero_si512();
2031 _sum1 = _mm512_setzero_si512();
2032 i = 0;
2033#if __AVX512VNNI__
2034#if defined(__x86_64__) || defined(_M_X64)
2035 _sum2 = _mm512_setzero_si512();
2036 _sum3 = _mm512_setzero_si512();
2037 for (; i + 15 < num_output; i += 16)
2038 {
2039 __m128i _h_cont = _mm_loadu_si128((const __m128i*)(hs + i));
2040 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
2041 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
2042 __m512i _w2 = _mm512_loadu_si512((const __m512i*)(kptr + 128));
2043 __m512i _w3 = _mm512_loadu_si512((const __m512i*)(kptr + 192));
2044
2045 __m512i _hh_cont = _mm512_broadcast_i32x4(_h_cont);
2046
2047 _sum0 = _mm512_dpbusd_epi32(_sum0, _hh_cont, _w0);
2048 _sum1 = _mm512_dpbusd_epi32(_sum1, _hh_cont, _w1);
2049 _sum2 = _mm512_dpbusd_epi32(_sum2, _hh_cont, _w2);
2050 _sum3 = _mm512_dpbusd_epi32(_sum3, _hh_cont, _w3);
2051
2052 kptr += 256;
2053 }
2054 {
2055 __m512i _tmp0 = _mm512_unpacklo_epi32(_sum0, _sum1);
2056 __m512i _tmp1 = _mm512_unpackhi_epi32(_sum0, _sum1);
2057 __m512i _tmp2 = _mm512_unpacklo_epi32(_sum2, _sum3);
2058 __m512i _tmp3 = _mm512_unpackhi_epi32(_sum2, _sum3);
2059 _sum0 = _mm512_unpacklo_epi64(_tmp0, _tmp2);
2060 _sum1 = _mm512_unpackhi_epi64(_tmp0, _tmp2);
2061 _sum2 = _mm512_unpacklo_epi64(_tmp1, _tmp3);
2062 _sum3 = _mm512_unpackhi_epi64(_tmp1, _tmp3);
2063
2064 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum0);
2065 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum1);
2066 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum2);
2067 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum3);
2068 }
2069
2070 _sum0 = _mm512_setzero_si512();
2071 _sum1 = _mm512_setzero_si512();
2072#endif // defined(__x86_64__) || defined(_M_X64)
2073 for (; i + 7 < num_output; i += 8)
2074 {
2075 __m128i _h_cont = _mm_loadl_epi64((const __m128i*)(hs + i));
2076 __m512i _w0 = _mm512_loadu_si512((const __m512i*)kptr);
2077 __m512i _w1 = _mm512_loadu_si512((const __m512i*)(kptr + 64));
2078
2079 __m512i _hh_cont = _mm512_broadcastq_epi64(_h_cont);
2080
2081 _sum0 = _mm512_dpbusd_epi32(_sum0, _hh_cont, _w0);
2082 _sum1 = _mm512_dpbusd_epi32(_sum1, _hh_cont, _w1);
2083
2084 kptr += 128;
2085 }
2086 {
2087 __m512i _tmp0 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(2, 0, 2, 0)));
2088 __m512i _tmp1 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(3, 1, 3, 1)));
2089
2090 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _tmp0);
2091 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _tmp1);
2092 }
2093
2094 for (; i + 3 < num_output; i += 4)
2095 {
2096 __m512i _h_cont = _mm512_set1_epi32(((const int*)(hs + i))[0]);
2097 __m512i _w = _mm512_loadu_si512((const __m512i*)kptr);
2098
2099#ifdef _MSC_VER
2100 _h_cont = _mm512_add_epi32(_h_cont, _mm512_set1_epi8(127));
2101#endif
2102 _lstm_IFOGh0 = _mm512_dpbusd_epi32(_lstm_IFOGh0, _h_cont, _w);
2103
2104 kptr += 64;
2105 }
2106 {
2107 __m512i _w_shift = _mm512_loadu_si512((const __m512i*)kptr);
2108 _lstm_IFOGh0 = _mm512_sub_epi32(_lstm_IFOGh0, _w_shift);
2109 kptr += 64;
2110 }
2111#else
2112#if defined(__x86_64__) || defined(_M_X64)
2113 _sum2 = _mm512_setzero_si512();
2114 _sum3 = _mm512_setzero_si512();
2115 for (; i + 7 < num_output; i += 8)
2116 {
2117 __m256i _h_cont = _mm256_castpd_si256(_mm256_broadcast_sd((const double*)(hs + i)));
2118 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
2119 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
2120 __m256i _w2 = _mm256_loadu_si256((const __m256i*)(kptr + 64));
2121 __m256i _w3 = _mm256_loadu_si256((const __m256i*)(kptr + 96));
2122
2123 __m512i _hh_cont = _mm512_cvtepi8_epi16(_h_cont);
2124 __m512i _ww0 = _mm512_cvtepi8_epi16(_w0);
2125 __m512i _ww1 = _mm512_cvtepi8_epi16(_w1);
2126 __m512i _ww2 = _mm512_cvtepi8_epi16(_w2);
2127 __m512i _ww3 = _mm512_cvtepi8_epi16(_w3);
2128
2129 __m512i _s0 = _mm512_madd_epi16(_ww0, _hh_cont);
2130 __m512i _s1 = _mm512_madd_epi16(_ww1, _hh_cont);
2131 __m512i _s2 = _mm512_madd_epi16(_ww2, _hh_cont);
2132 __m512i _s3 = _mm512_madd_epi16(_ww3, _hh_cont);
2133 _sum0 = _mm512_add_epi32(_sum0, _s0);
2134 _sum1 = _mm512_add_epi32(_sum1, _s1);
2135 _sum2 = _mm512_add_epi32(_sum2, _s2);
2136 _sum3 = _mm512_add_epi32(_sum3, _s3);
2137
2138 kptr += 128;
2139 }
2140 {
2141 __m512i _tmp0 = _mm512_unpacklo_epi32(_sum0, _sum1);
2142 __m512i _tmp1 = _mm512_unpackhi_epi32(_sum0, _sum1);
2143 __m512i _tmp2 = _mm512_unpacklo_epi32(_sum2, _sum3);
2144 __m512i _tmp3 = _mm512_unpackhi_epi32(_sum2, _sum3);
2145 _sum0 = _mm512_unpacklo_epi64(_tmp0, _tmp2);
2146 _sum1 = _mm512_unpackhi_epi64(_tmp0, _tmp2);
2147 _sum2 = _mm512_unpacklo_epi64(_tmp1, _tmp3);
2148 _sum3 = _mm512_unpackhi_epi64(_tmp1, _tmp3);
2149
2150 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum0);
2151 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum1);
2152 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum2);
2153 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _sum3);
2154 }
2155
2156 _sum0 = _mm512_setzero_si512();
2157 _sum1 = _mm512_setzero_si512();
2158#endif // defined(__x86_64__) || defined(_M_X64)
2159 for (; i + 3 < num_output; i += 4)
2160 {
2161 __m256i _h_cont = _mm256_castps_si256(_mm256_broadcast_ss((const float*)(hs + i)));
2162 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
2163 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
2164
2165 __m512i _hh_cont = _mm512_cvtepi8_epi16(_h_cont);
2166 __m512i _ww0 = _mm512_cvtepi8_epi16(_w0);
2167 __m512i _ww1 = _mm512_cvtepi8_epi16(_w1);
2168
2169 __m512i _s0 = _mm512_madd_epi16(_ww0, _hh_cont);
2170 __m512i _s1 = _mm512_madd_epi16(_ww1, _hh_cont);
2171 _sum0 = _mm512_add_epi32(_sum0, _s0);
2172 _sum1 = _mm512_add_epi32(_sum1, _s1);
2173
2174 kptr += 64;
2175 }
2176 {
2177 __m512i _tmp0 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(2, 0, 2, 0)));
2178 __m512i _tmp1 = _mm512_castps_si512(_mm512_shuffle_ps(_mm512_castsi512_ps(_sum0), _mm512_castsi512_ps(_sum1), _MM_SHUFFLE(3, 1, 3, 1)));
2179
2180 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _tmp0);
2181 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _tmp1);
2182 }
2183#endif // __AVX512VNNI__
2184 for (; i + 1 < num_output; i += 2)
2185 {
2186 __m256i _w = _mm256_loadu_si256((const __m256i*)kptr);
2187 __m256i _h_cont = _mm256_castps_si256(_mm256_broadcast_ss((const float*)(hs + i)));
2188
2189 __m512i _ww = _mm512_cvtepi8_epi16(_w);
2190 __m512i _hh_cont = _mm512_cvtepi8_epi16(_h_cont);
2191
2192 __m512i _hh_cont0 = _mm512_shuffle_epi32(_hh_cont, _MM_PERM_AAAA);
2193
2194#if __AVX512VNNI__
2195 _lstm_IFOGh0 = _mm512_dpwssd_epi32(_lstm_IFOGh0, _ww, _hh_cont0);
2196#else
2197 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _mm512_madd_epi16(_ww, _hh_cont0));
2198#endif // __AVX512VNNI__
2199
2200 kptr += 32;
2201 }
2202 for (; i < num_output; i++)
2203 {
2204 __m128i _w = _mm_load_si128((const __m128i*)kptr);
2205 __m256i _h_cont = _mm256_set1_epi16(hs[i]);
2206
2207 __m256i _ww = _mm256_cvtepi8_epi16(_w);
2208
2209 __m512i _s0 = _mm512_cvtepi16_epi32(_mm256_mullo_epi16(_ww, _h_cont));
2210
2211 _lstm_IFOGh0 = _mm512_add_epi32(_lstm_IFOGh0, _s0);
2212
2213 kptr += 16;
2214 }
2215
2216 __m512 _descale_x = _mm512_set1_ps(descale_x);
2217 __m512 _descale_h = _mm512_set1_ps(descale_h);
2218
2219 __m512 _lstm_IFOG0 = _mm512_loadu_ps(bias_c_IFOG);
2220
2221 __m512 _descale_xc_IFOG = _mm512_loadu_ps(descales_ptr);
2222
2223 _lstm_IFOG0 = _mm512_fmadd_ps(_mm512_cvtepi32_ps(_lstm_IFOGx0), _mm512_mul_ps(_descale_x, _descale_xc_IFOG), _lstm_IFOG0);
2224
2225 __m512 _descale_hc_IFOG = _mm512_loadu_ps(descales_ptr + 16);
2226
2227 _lstm_IFOG0 = _mm512_fmadd_ps(_mm512_cvtepi32_ps(_lstm_IFOGh0), _mm512_mul_ps(_descale_h, _descale_hc_IFOG), _lstm_IFOG0);
2228
2229 _mm512_storeu_ps(gates_data, _lstm_IFOG0);
2230 }
2231 remain_hidden_size_start += nn_hidden_size << 2;
2232 nn_hidden_size = (hidden_size - remain_hidden_size_start) >> 1;
2233#else
2234 nn_hidden_size = hidden_size >> 1;
2235#endif // __AVX512F__
2236 #pragma omp parallel for num_threads(opt.num_threads)
2237 for (int qq = 0; qq < nn_hidden_size; qq++)
2238 {
2239 int q = remain_hidden_size_start + qq * 2;
2240
2241 const signed char* x = bottom_blob_int8.row<const signed char>(ti);
2242 const signed char* hs = hidden_state_int8;
2243 const float descale_x = bottom_blob_int8_descales[ti];
2244 const float descale_h = hidden_state_int8_descale;
2245
2246 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
2247
2248#if __AVX512F__
2249 const signed char* kptr = weight_data_tm.row<const signed char>(q / 4 + (q % 4) / 2);
2250 const float* descales_ptr = weight_data_tm_int8_descales.row(q / 4 + (q % 4) / 2);
2251#else
2252 const signed char* kptr = weight_data_tm.row<const signed char>(q / 2);
2253 const float* descales_ptr = weight_data_tm_int8_descales.row(q / 2);
2254#endif
2255
2256 float* gates_data = gates.row(q);
2257
2258 __m256i _lstm_IFOGx0 = _mm256_setzero_si256();
2259 __m256i _sum0 = _mm256_setzero_si256();
2260 __m256i _sum1 = _mm256_setzero_si256();
2261 int i = 0;
2262#if __AVXVNNI__ || __AVX512VNNI__
2263#if defined(__x86_64__) || defined(_M_X64)
2264 __m256i _sum2 = _mm256_setzero_si256();
2265 __m256i _sum3 = _mm256_setzero_si256();
2266 for (; i + 15 < size; i += 16)
2267 {
2268 __m128i _xi = _mm_loadu_si128((const __m128i*)(x + i));
2269 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
2270 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
2271 __m256i _w2 = _mm256_loadu_si256((const __m256i*)(kptr + 64));
2272 __m256i _w3 = _mm256_loadu_si256((const __m256i*)(kptr + 96));
2273
2274 __m256i _xii = _mm256_inserti128_si256(_mm256_castsi128_si256(_xi), _xi, 1);
2275
2276 _sum0 = _mm256_dpbusd_epi32(_sum0, _xii, _w0);
2277 _sum1 = _mm256_dpbusd_epi32(_sum1, _xii, _w1);
2278 _sum2 = _mm256_dpbusd_epi32(_sum2, _xii, _w2);
2279 _sum3 = _mm256_dpbusd_epi32(_sum3, _xii, _w3);
2280
2281 kptr += 128;
2282 }
2283 {
2284 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2285 __m256i _tmp1 = _mm256_hadd_epi32(_sum2, _sum3);
2286 _tmp0 = _mm256_hadd_epi32(_tmp0, _tmp1);
2287 _lstm_IFOGx0 = _mm256_add_epi32(_lstm_IFOGx0, _tmp0);
2288 }
2289
2290 _sum0 = _mm256_setzero_si256();
2291 _sum1 = _mm256_setzero_si256();
2292#endif // defined(__x86_64__) || defined(_M_X64)
2293 for (; i + 7 < size; i += 8)
2294 {
2295 __m256i _xi = _mm256_castpd_si256(_mm256_broadcast_sd((const double*)(x + i)));
2296 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
2297 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
2298
2299 _sum0 = _mm256_dpbusd_epi32(_sum0, _xi, _w0);
2300 _sum1 = _mm256_dpbusd_epi32(_sum1, _xi, _w1);
2301
2302 kptr += 64;
2303 }
2304 {
2305 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2306 _lstm_IFOGx0 = _mm256_add_epi32(_lstm_IFOGx0, _tmp0);
2307 }
2308
2309 for (; i + 3 < size; i += 4)
2310 {
2311 __m256i _xi = _mm256_castps_si256(_mm256_broadcast_ss((const float*)(x + i)));
2312 __m256i _w = _mm256_loadu_si256((const __m256i*)kptr);
2313
2314#ifdef _MSC_VER
2315 _xi = _mm256_add_epi32(_xi, _mm256_set1_epi8(127));
2316#endif
2317 _lstm_IFOGx0 = _mm256_dpbusd_epi32(_lstm_IFOGx0, _xi, _w);
2318
2319 kptr += 32;
2320 }
2321 {
2322 __m256i _w_shift = _mm256_loadu_si256((const __m256i*)kptr);
2323 _lstm_IFOGx0 = _mm256_sub_epi32(_lstm_IFOGx0, _w_shift);
2324 kptr += 32;
2325 }
2326#else
2327#if defined(__x86_64__) || defined(_M_X64)
2328 __m256i _sum2 = _mm256_setzero_si256();
2329 __m256i _sum3 = _mm256_setzero_si256();
2330 for (; i + 7 < size; i += 8)
2331 {
2332 __m128i _xi = _mm_castpd_si128(_mm_load1_pd((const double*)(x + i)));
2333 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2334 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2335 __m128i _w2 = _mm_loadu_si128((const __m128i*)(kptr + 32));
2336 __m128i _w3 = _mm_loadu_si128((const __m128i*)(kptr + 48));
2337
2338 __m256i _xii = _mm256_cvtepi8_epi16(_xi);
2339 __m256i _ww0 = _mm256_cvtepi8_epi16(_w0);
2340 __m256i _ww1 = _mm256_cvtepi8_epi16(_w1);
2341 __m256i _ww2 = _mm256_cvtepi8_epi16(_w2);
2342 __m256i _ww3 = _mm256_cvtepi8_epi16(_w3);
2343
2344 __m256i _s0 = _mm256_madd_epi16(_ww0, _xii);
2345 __m256i _s1 = _mm256_madd_epi16(_ww1, _xii);
2346 __m256i _s2 = _mm256_madd_epi16(_ww2, _xii);
2347 __m256i _s3 = _mm256_madd_epi16(_ww3, _xii);
2348 _sum0 = _mm256_add_epi32(_sum0, _s0);
2349 _sum1 = _mm256_add_epi32(_sum1, _s1);
2350 _sum2 = _mm256_add_epi32(_sum2, _s2);
2351 _sum3 = _mm256_add_epi32(_sum3, _s3);
2352
2353 kptr += 64;
2354 }
2355 {
2356 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2357 __m256i _tmp1 = _mm256_hadd_epi32(_sum2, _sum3);
2358 _tmp0 = _mm256_hadd_epi32(_tmp0, _tmp1);
2359 _lstm_IFOGx0 = _mm256_add_epi32(_lstm_IFOGx0, _tmp0);
2360 }
2361
2362 _sum0 = _mm256_setzero_si256();
2363 _sum1 = _mm256_setzero_si256();
2364#endif // defined(__x86_64__) || defined(_M_X64)
2365 for (; i + 3 < size; i += 4)
2366 {
2367 __m128i _xi = _mm_castps_si128(_mm_load1_ps((const float*)(x + i)));
2368 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2369 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2370
2371 __m256i _xii = _mm256_cvtepi8_epi16(_xi);
2372 __m256i _ww0 = _mm256_cvtepi8_epi16(_w0);
2373 __m256i _ww1 = _mm256_cvtepi8_epi16(_w1);
2374
2375 __m256i _s0 = _mm256_madd_epi16(_ww0, _xii);
2376 __m256i _s1 = _mm256_madd_epi16(_ww1, _xii);
2377 _sum0 = _mm256_add_epi32(_sum0, _s0);
2378 _sum1 = _mm256_add_epi32(_sum1, _s1);
2379
2380 kptr += 32;
2381 }
2382 {
2383 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2384 _lstm_IFOGx0 = _mm256_add_epi32(_lstm_IFOGx0, _tmp0);
2385 }
2386#endif // __AVXVNNI__ || __AVX512VNNI__
2387 for (; i + 1 < size; i += 2)
2388 {
2389 __m128i _w = _mm_loadu_si128((const __m128i*)kptr);
2390 __m128i _xi = _mm_castps_si128(_mm_load1_ps((const float*)(x + i)));
2391
2392 __m256i _ww = _mm256_cvtepi8_epi16(_w);
2393 __m256i _xixi = _mm256_cvtepi8_epi16(_xi);
2394
2395 __m256i _xixi0 = _mm256_shuffle_epi32(_xixi, _MM_SHUFFLE(0, 0, 0, 0));
2396
2397#if __AVXVNNI__ || __AVX512VNNI__
2398 _lstm_IFOGx0 = _mm256_dpwssd_epi32(_lstm_IFOGx0, _ww, _xixi0);
2399#else
2400 _lstm_IFOGx0 = _mm256_add_epi32(_lstm_IFOGx0, _mm256_madd_epi16(_ww, _xixi0));
2401#endif // __AVXVNNI__ || __AVX512VNNI__
2402
2403 kptr += 16;
2404 }
2405 for (; i < size; i++)
2406 {
2407 __m128i _w = _mm_loadl_epi64((const __m128i*)kptr);
2408 __m128i _xi = _mm_set1_epi16(x[i]);
2409
2410 _w = _mm_cvtepi8_epi16(_w);
2411
2412 __m256i _s0 = _mm256_cvtepi16_epi32(_mm_mullo_epi16(_w, _xi));
2413
2414 _lstm_IFOGx0 = _mm256_add_epi32(_lstm_IFOGx0, _s0);
2415
2416 kptr += 8;
2417 }
2418
2419 __m256i _lstm_IFOGh0 = _mm256_setzero_si256();
2420 _sum0 = _mm256_setzero_si256();
2421 _sum1 = _mm256_setzero_si256();
2422 i = 0;
2423#if __AVXVNNI__ || __AVX512VNNI__
2424#if defined(__x86_64__) || defined(_M_X64)
2425 _sum2 = _mm256_setzero_si256();
2426 _sum3 = _mm256_setzero_si256();
2427 for (; i + 15 < num_output; i += 16)
2428 {
2429 __m128i _h_cont = _mm_loadu_si128((const __m128i*)(hs + i));
2430 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
2431 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
2432 __m256i _w2 = _mm256_loadu_si256((const __m256i*)(kptr + 64));
2433 __m256i _w3 = _mm256_loadu_si256((const __m256i*)(kptr + 96));
2434
2435 __m256i _hh_cont = _mm256_broadcastsi128_si256(_h_cont);
2436
2437 _sum0 = _mm256_dpbusd_epi32(_sum0, _hh_cont, _w0);
2438 _sum1 = _mm256_dpbusd_epi32(_sum1, _hh_cont, _w1);
2439 _sum2 = _mm256_dpbusd_epi32(_sum2, _hh_cont, _w2);
2440 _sum3 = _mm256_dpbusd_epi32(_sum3, _hh_cont, _w3);
2441
2442 kptr += 128;
2443 }
2444 {
2445 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2446 __m256i _tmp1 = _mm256_hadd_epi32(_sum2, _sum3);
2447 _tmp0 = _mm256_hadd_epi32(_tmp0, _tmp1);
2448 _lstm_IFOGh0 = _mm256_add_epi32(_lstm_IFOGh0, _tmp0);
2449 }
2450
2451 _sum0 = _mm256_setzero_si256();
2452 _sum1 = _mm256_setzero_si256();
2453#endif // defined(__x86_64__) || defined(_M_X64)
2454 for (; i + 7 < num_output; i += 8)
2455 {
2456 __m256i _h_cont = _mm256_castpd_si256(_mm256_broadcast_sd((const double*)(hs + i)));
2457 __m256i _w0 = _mm256_loadu_si256((const __m256i*)kptr);
2458 __m256i _w1 = _mm256_loadu_si256((const __m256i*)(kptr + 32));
2459
2460 _sum0 = _mm256_dpbusd_epi32(_sum0, _h_cont, _w0);
2461 _sum1 = _mm256_dpbusd_epi32(_sum1, _h_cont, _w1);
2462
2463 kptr += 64;
2464 }
2465 {
2466 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2467 _lstm_IFOGh0 = _mm256_add_epi32(_lstm_IFOGh0, _tmp0);
2468 }
2469
2470 for (; i + 3 < num_output; i += 4)
2471 {
2472 __m256i _h_cont = _mm256_castps_si256(_mm256_broadcast_ss((const float*)(hs + i)));
2473 __m256i _w = _mm256_loadu_si256((const __m256i*)kptr);
2474
2475#ifdef _MSC_VER
2476 _h_cont = _mm256_add_epi32(_h_cont, _mm256_set1_epi8(127));
2477#endif
2478 _lstm_IFOGh0 = _mm256_dpbusd_epi32(_lstm_IFOGh0, _h_cont, _w);
2479
2480 kptr += 32;
2481 }
2482 {
2483 __m256i _w_shift = _mm256_loadu_si256((const __m256i*)kptr);
2484 _lstm_IFOGh0 = _mm256_sub_epi32(_lstm_IFOGh0, _w_shift);
2485 kptr += 32;
2486 }
2487#else
2488#if defined(__x86_64__) || defined(_M_X64)
2489 _sum2 = _mm256_setzero_si256();
2490 _sum3 = _mm256_setzero_si256();
2491 for (; i + 7 < num_output; i += 8)
2492 {
2493 __m128i _h_cont = _mm_castpd_si128(_mm_load1_pd((const double*)(hs + i)));
2494 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2495 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2496 __m128i _w2 = _mm_loadu_si128((const __m128i*)(kptr + 32));
2497 __m128i _w3 = _mm_loadu_si128((const __m128i*)(kptr + 48));
2498
2499 __m256i _hh_cont = _mm256_cvtepi8_epi16(_h_cont);
2500 __m256i _ww0 = _mm256_cvtepi8_epi16(_w0);
2501 __m256i _ww1 = _mm256_cvtepi8_epi16(_w1);
2502 __m256i _ww2 = _mm256_cvtepi8_epi16(_w2);
2503 __m256i _ww3 = _mm256_cvtepi8_epi16(_w3);
2504
2505 __m256i _s0 = _mm256_madd_epi16(_ww0, _hh_cont);
2506 __m256i _s1 = _mm256_madd_epi16(_ww1, _hh_cont);
2507 __m256i _s2 = _mm256_madd_epi16(_ww2, _hh_cont);
2508 __m256i _s3 = _mm256_madd_epi16(_ww3, _hh_cont);
2509 _sum0 = _mm256_add_epi32(_sum0, _s0);
2510 _sum1 = _mm256_add_epi32(_sum1, _s1);
2511 _sum2 = _mm256_add_epi32(_sum2, _s2);
2512 _sum3 = _mm256_add_epi32(_sum3, _s3);
2513
2514 kptr += 64;
2515 }
2516 {
2517 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2518 __m256i _tmp1 = _mm256_hadd_epi32(_sum2, _sum3);
2519 _tmp0 = _mm256_hadd_epi32(_tmp0, _tmp1);
2520 _lstm_IFOGh0 = _mm256_add_epi32(_lstm_IFOGh0, _tmp0);
2521 }
2522
2523 _sum0 = _mm256_setzero_si256();
2524 _sum1 = _mm256_setzero_si256();
2525#endif // defined(__x86_64__) || defined(_M_X64)
2526 for (; i + 3 < num_output; i += 4)
2527 {
2528 __m128i _h_cont = _mm_castps_si128(_mm_load1_ps((const float*)(hs + i)));
2529 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2530 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2531
2532 __m256i _hh_cont = _mm256_cvtepi8_epi16(_h_cont);
2533 __m256i _ww0 = _mm256_cvtepi8_epi16(_w0);
2534 __m256i _ww1 = _mm256_cvtepi8_epi16(_w1);
2535
2536 __m256i _s0 = _mm256_madd_epi16(_ww0, _hh_cont);
2537 __m256i _s1 = _mm256_madd_epi16(_ww1, _hh_cont);
2538 _sum0 = _mm256_add_epi32(_sum0, _s0);
2539 _sum1 = _mm256_add_epi32(_sum1, _s1);
2540
2541 kptr += 32;
2542 }
2543 {
2544 __m256i _tmp0 = _mm256_hadd_epi32(_sum0, _sum1);
2545 _lstm_IFOGh0 = _mm256_add_epi32(_lstm_IFOGh0, _tmp0);
2546 }
2547#endif // __AVXVNNI__ || __AVX512VNNI__
2548 for (; i + 1 < num_output; i += 2)
2549 {
2550 __m128i _w = _mm_loadu_si128((const __m128i*)kptr);
2551 __m128i _h_cont = _mm_castps_si128(_mm_load1_ps((const float*)(hs + i)));
2552
2553 __m256i _ww = _mm256_cvtepi8_epi16(_w);
2554 __m256i _hh_cont = _mm256_cvtepi8_epi16(_h_cont);
2555
2556 __m256i _hh_cont0 = _mm256_shuffle_epi32(_hh_cont, _MM_SHUFFLE(0, 0, 0, 0));
2557
2558#if __AVXVNNI__ || __AVX512VNNI__
2559 _lstm_IFOGh0 = _mm256_dpwssd_epi32(_lstm_IFOGh0, _ww, _hh_cont0);
2560#else
2561 _lstm_IFOGh0 = _mm256_add_epi32(_lstm_IFOGh0, _mm256_madd_epi16(_ww, _hh_cont0));
2562#endif // __AVXVNNI__ || __AVX512VNNI__
2563
2564 kptr += 16;
2565 }
2566 for (; i < num_output; i++)
2567 {
2568 __m128i _w = _mm_loadl_epi64((const __m128i*)kptr);
2569 __m128i _h_cont = _mm_set1_epi16(hs[i]);
2570
2571 _w = _mm_cvtepi8_epi16(_w);
2572
2573 __m256i _s0 = _mm256_cvtepi16_epi32(_mm_mullo_epi16(_w, _h_cont));
2574
2575 _lstm_IFOGh0 = _mm256_add_epi32(_lstm_IFOGh0, _s0);
2576
2577 kptr += 8;
2578 }
2579
2580 __m256 _descale_x = _mm256_set1_ps(descale_x);
2581 __m256 _descale_h = _mm256_set1_ps(descale_h);
2582
2583 __m256 _lstm_IFOG0 = _mm256_loadu_ps(bias_c_IFOG);
2584
2585 __m256 _descale_xc_IFOG = _mm256_loadu_ps(descales_ptr);
2586
2587 _lstm_IFOG0 = _mm256_comp_fmadd_ps(_mm256_cvtepi32_ps(_lstm_IFOGx0), _mm256_mul_ps(_descale_x, _descale_xc_IFOG), _lstm_IFOG0);
2588
2589 __m256 _descale_hc_IFOG = _mm256_loadu_ps(descales_ptr + 8);
2590
2591 _lstm_IFOG0 = _mm256_comp_fmadd_ps(_mm256_cvtepi32_ps(_lstm_IFOGh0), _mm256_mul_ps(_descale_h, _descale_hc_IFOG), _lstm_IFOG0);
2592
2593 _mm256_storeu_ps(gates_data, _lstm_IFOG0);
2594 }
2595 remain_hidden_size_start += nn_hidden_size << 1;
2596#endif // __AVX2__
2597 #pragma omp parallel for num_threads(opt.num_threads)
2598 for (int q = remain_hidden_size_start; q < hidden_size; q++)
2599 {
2600 const signed char* x = bottom_blob_int8.row<const signed char>(ti);
2601 const signed char* hs = hidden_state_int8;
2602 const float descale_x = bottom_blob_int8_descales[ti];
2603 const float descale_h = hidden_state_int8_descale;
2604
2605 // gate reset update
2606 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
2607
2608#if __AVX512F__
2609 const signed char* kptr = weight_data_tm.row<const signed char>(q / 4 + (q % 4) / 2 + q % 2);
2610 const float* descales_ptr = weight_data_tm_int8_descales.row(q / 4 + (q % 4) / 2 + q % 2);
2611#elif __AVX2__
2612 const signed char* kptr = weight_data_tm.row<const signed char>(q / 2 + q % 2);
2613 const float* descales_ptr = weight_data_tm_int8_descales.row(q / 2 + q % 2);
2614#else
2615 const signed char* kptr = weight_data_tm.row<const signed char>(q);
2616 const float* descales_ptr = weight_data_tm_int8_descales.row(q);
2617#endif
2618
2619 float* gates_data = gates.row(q);
2620
2621#if __SSE2__
2622 __m128i _lstm_IFOGx0 = _mm_setzero_si128();
2623 __m128i _sum0 = _mm_setzero_si128();
2624 __m128i _sum1 = _mm_setzero_si128();
2625 int i = 0;
2626#if __AVXVNNI__ || __AVX512VNNI__
2627#if defined(__x86_64__) || defined(_M_X64)
2628 __m128i _sum2 = _mm_setzero_si128();
2629 __m128i _sum3 = _mm_setzero_si128();
2630 for (; i + 15 < size; i += 16)
2631 {
2632 __m128i _xi = _mm_loadu_si128((const __m128i*)(x + i));
2633 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2634 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2635 __m128i _w2 = _mm_loadu_si128((const __m128i*)(kptr + 32));
2636 __m128i _w3 = _mm_loadu_si128((const __m128i*)(kptr + 48));
2637
2638 _sum0 = _mm_dpbusd_epi32(_sum0, _xi, _w0);
2639 _sum1 = _mm_dpbusd_epi32(_sum1, _xi, _w1);
2640 _sum2 = _mm_dpbusd_epi32(_sum2, _xi, _w2);
2641 _sum3 = _mm_dpbusd_epi32(_sum3, _xi, _w3);
2642
2643 kptr += 64;
2644 }
2645 {
2646 transpose4x4_epi32(_sum0, _sum1, _sum2, _sum3);
2647 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum0);
2648 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum1);
2649 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum2);
2650 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum3);
2651 }
2652
2653 _sum0 = _mm_setzero_si128();
2654 _sum1 = _mm_setzero_si128();
2655#endif // defined(__x86_64__) || defined(_M_X64)
2656 for (; i + 7 < size; i += 8)
2657 {
2658 __m128i _xi = _mm_castpd_si128(_mm_load1_pd((const double*)(x + i)));
2659 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2660 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2661
2662 _sum0 = _mm_dpbusd_epi32(_sum0, _xi, _w0);
2663 _sum1 = _mm_dpbusd_epi32(_sum1, _xi, _w1);
2664
2665 kptr += 32;
2666 }
2667 {
2668 __m128i _tmp0 = _mm_hadd_epi32(_sum0, _sum1);
2669 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _tmp0);
2670 }
2671
2672 for (; i + 3 < size; i += 4)
2673 {
2674 __m128i _xi = _mm_castps_si128(_mm_load1_ps((const float*)(x + i)));
2675 __m128i _w = _mm_loadu_si128((const __m128i*)kptr);
2676
2677#ifdef _MSC_VER
2678 _xi = _mm_add_epi32(_xi, _mm_set1_epi8(127));
2679#endif
2680 _lstm_IFOGx0 = _mm_dpbusd_epi32(_lstm_IFOGx0, _xi, _w);
2681
2682 kptr += 16;
2683 }
2684 {
2685 __m128i _w_shift = _mm_loadu_si128((const __m128i*)kptr);
2686 _lstm_IFOGx0 = _mm_sub_epi32(_lstm_IFOGx0, _w_shift);
2687 kptr += 16;
2688 }
2689#else
2690#if defined(__x86_64__) || defined(_M_X64)
2691 __m128i _sum2 = _mm_setzero_si128();
2692 __m128i _sum3 = _mm_setzero_si128();
2693 for (; i + 7 < size; i += 8)
2694 {
2695 __m128i _xi = _mm_castpd_si128(_mm_load1_pd((const double*)(x + i)));
2696 __m128i _w0 = _mm_loadl_epi64((const __m128i*)kptr);
2697 __m128i _w1 = _mm_loadl_epi64((const __m128i*)(kptr + 8));
2698 __m128i _w2 = _mm_loadl_epi64((const __m128i*)(kptr + 16));
2699 __m128i _w3 = _mm_loadl_epi64((const __m128i*)(kptr + 24));
2700
2701#if __SSE4_1__
2702 _xi = _mm_cvtepi8_epi16(_xi);
2703 _w0 = _mm_cvtepi8_epi16(_w0);
2704 _w1 = _mm_cvtepi8_epi16(_w1);
2705 _w2 = _mm_cvtepi8_epi16(_w2);
2706 _w3 = _mm_cvtepi8_epi16(_w3);
2707#else
2708 _xi = _mm_unpacklo_epi8(_xi, _mm_cmpgt_epi8(_mm_setzero_si128(), _xi));
2709 _w0 = _mm_unpacklo_epi8(_w0, _mm_cmpgt_epi8(_mm_setzero_si128(), _w0));
2710 _w1 = _mm_unpacklo_epi8(_w1, _mm_cmpgt_epi8(_mm_setzero_si128(), _w1));
2711 _w2 = _mm_unpacklo_epi8(_w2, _mm_cmpgt_epi8(_mm_setzero_si128(), _w2));
2712 _w3 = _mm_unpacklo_epi8(_w3, _mm_cmpgt_epi8(_mm_setzero_si128(), _w3));
2713#endif
2714
2715#if __XOP__
2716 _sum0 = _mm_maddd_epi16(_w0, _xi, _sum0);
2717 _sum1 = _mm_maddd_epi16(_w1, _xi, _sum1);
2718 _sum2 = _mm_maddd_epi16(_w2, _xi, _sum2);
2719 _sum3 = _mm_maddd_epi16(_w3, _xi, _sum3);
2720#else
2721 __m128i _s0 = _mm_madd_epi16(_w0, _xi);
2722 __m128i _s1 = _mm_madd_epi16(_w1, _xi);
2723 __m128i _s2 = _mm_madd_epi16(_w2, _xi);
2724 __m128i _s3 = _mm_madd_epi16(_w3, _xi);
2725 _sum0 = _mm_add_epi32(_sum0, _s0);
2726 _sum1 = _mm_add_epi32(_sum1, _s1);
2727 _sum2 = _mm_add_epi32(_sum2, _s2);
2728 _sum3 = _mm_add_epi32(_sum3, _s3);
2729#endif
2730
2731 kptr += 32;
2732 }
2733 {
2734 transpose4x4_epi32(_sum0, _sum1, _sum2, _sum3);
2735 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum0);
2736 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum1);
2737 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum2);
2738 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _sum3);
2739 }
2740
2741 _sum0 = _mm_setzero_si128();
2742 _sum1 = _mm_setzero_si128();
2743#endif // defined(__x86_64__) || defined(_M_X64)
2744 for (; i + 3 < size; i += 4)
2745 {
2746 __m128i _xi = _mm_castps_si128(_mm_load1_ps((const float*)(x + i)));
2747 __m128i _w0 = _mm_loadl_epi64((const __m128i*)kptr);
2748 __m128i _w1 = _mm_loadl_epi64((const __m128i*)(kptr + 8));
2749
2750#if __SSE4_1__
2751 _xi = _mm_cvtepi8_epi16(_xi);
2752 _w0 = _mm_cvtepi8_epi16(_w0);
2753 _w1 = _mm_cvtepi8_epi16(_w1);
2754#else
2755 _xi = _mm_unpacklo_epi8(_xi, _mm_cmpgt_epi8(_mm_setzero_si128(), _xi));
2756 _w0 = _mm_unpacklo_epi8(_w0, _mm_cmpgt_epi8(_mm_setzero_si128(), _w0));
2757 _w1 = _mm_unpacklo_epi8(_w1, _mm_cmpgt_epi8(_mm_setzero_si128(), _w1));
2758#endif
2759
2760#if __XOP__
2761 _sum0 = _mm_maddd_epi16(_w0, _xi, _sum0);
2762 _sum1 = _mm_maddd_epi16(_w1, _xi, _sum1);
2763#else
2764 __m128i _s0 = _mm_madd_epi16(_w0, _xi);
2765 __m128i _s1 = _mm_madd_epi16(_w1, _xi);
2766 _sum0 = _mm_add_epi32(_sum0, _s0);
2767 _sum1 = _mm_add_epi32(_sum1, _s1);
2768#endif
2769
2770 kptr += 16;
2771 }
2772 {
2773#if __SSSE3__
2774 __m128i _tmp0 = _mm_hadd_epi32(_sum0, _sum1);
2775 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _tmp0);
2776#else
2777 __m128i _tmp0 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(_sum0), _mm_castsi128_ps(_sum1), _MM_SHUFFLE(2, 0, 2, 0)));
2778 __m128i _tmp1 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(_sum0), _mm_castsi128_ps(_sum1), _MM_SHUFFLE(3, 1, 3, 1)));
2779 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _tmp0);
2780 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _tmp1);
2781#endif // __SSSE3__
2782 }
2783#endif // __AVXVNNI__ || __AVX512VNNI__
2784 for (; i + 1 < size; i += 2)
2785 {
2786 __m128i _w = _mm_loadl_epi64((const __m128i*)kptr);
2787 __m128i _xi = _mm_set1_epi16(((const short*)(x + i))[0]);
2788
2789#if __SSE4_1__
2790 _w = _mm_cvtepi8_epi16(_w);
2791 _xi = _mm_cvtepi8_epi16(_xi);
2792#else
2793 _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w));
2794 _xi = _mm_unpacklo_epi8(_xi, _mm_cmpgt_epi8(_mm_setzero_si128(), _xi));
2795#endif
2796
2797#if __XOP__
2798 _lstm_IFOGx0 = _mm_maddd_epi16(_w, _xi, _lstm_IFOGx0);
2799#else
2800 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _mm_madd_epi16(_w, _xi));
2801#endif
2802
2803 kptr += 8;
2804 }
2805 for (; i < size; i++)
2806 {
2807 __m128i _w = _mm_loadl_epi64((const __m128i*)kptr);
2808 __m128i _xi = _mm_set1_epi16(x[i]);
2809
2810#if __SSE4_1__
2811 _w = _mm_cvtepi8_epi16(_w);
2812#else
2813 _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w));
2814#endif
2815
2816#if __XOP__
2817 _w = _mm_unpacklo_epi16(_w, _w);
2818
2819 _lstm_IFOGx0 = _mm_maccd_epi16(_w, _xi, _lstm_IFOGx0);
2820#else
2821 __m128i _sl = _mm_mullo_epi16(_w, _xi);
2822 __m128i _sh = _mm_mulhi_epi16(_w, _xi);
2823 __m128i _s0 = _mm_unpacklo_epi16(_sl, _sh);
2824
2825 _lstm_IFOGx0 = _mm_add_epi32(_lstm_IFOGx0, _s0);
2826#endif
2827
2828 kptr += 4;
2829 }
2830
2831 __m128i _lstm_IFOGh0 = _mm_setzero_si128();
2832 _sum0 = _mm_setzero_si128();
2833 _sum1 = _mm_setzero_si128();
2834 i = 0;
2835#if __AVXVNNI__ || __AVX512VNNI__
2836#if defined(__x86_64__) || defined(_M_X64)
2837 _sum2 = _mm_setzero_si128();
2838 _sum3 = _mm_setzero_si128();
2839 for (; i + 15 < num_output; i += 16)
2840 {
2841 __m128i _h_cont = _mm_loadu_si128((const __m128i*)(hs + i));
2842 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2843 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2844 __m128i _w2 = _mm_loadu_si128((const __m128i*)(kptr + 32));
2845 __m128i _w3 = _mm_loadu_si128((const __m128i*)(kptr + 48));
2846
2847 _sum0 = _mm_dpbusd_epi32(_sum0, _h_cont, _w0);
2848 _sum1 = _mm_dpbusd_epi32(_sum1, _h_cont, _w1);
2849 _sum2 = _mm_dpbusd_epi32(_sum2, _h_cont, _w2);
2850 _sum3 = _mm_dpbusd_epi32(_sum3, _h_cont, _w3);
2851
2852 kptr += 64;
2853 }
2854 {
2855 transpose4x4_epi32(_sum0, _sum1, _sum2, _sum3);
2856 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum0);
2857 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum1);
2858 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum2);
2859 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum3);
2860 }
2861
2862 _sum0 = _mm_setzero_si128();
2863 _sum1 = _mm_setzero_si128();
2864#endif // defined(__x86_64__) || defined(_M_X64)
2865 for (; i + 7 < num_output; i += 8)
2866 {
2867 __m128i _h_cont = _mm_castpd_si128(_mm_load1_pd((const double*)(hs + i)));
2868 __m128i _w0 = _mm_loadu_si128((const __m128i*)kptr);
2869 __m128i _w1 = _mm_loadu_si128((const __m128i*)(kptr + 16));
2870
2871 _sum0 = _mm_dpbusd_epi32(_sum0, _h_cont, _w0);
2872 _sum1 = _mm_dpbusd_epi32(_sum1, _h_cont, _w1);
2873
2874 kptr += 32;
2875 }
2876 {
2877 __m128i _tmp0 = _mm_hadd_epi32(_sum0, _sum1);
2878 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _tmp0);
2879 }
2880
2881 for (; i + 3 < num_output; i += 4)
2882 {
2883 __m128i _h_cont = _mm_castps_si128(_mm_load1_ps((const float*)(hs + i)));
2884 __m128i _w = _mm_loadu_si128((const __m128i*)kptr);
2885
2886#ifdef _MSC_VER
2887 _h_cont = _mm_add_epi32(_h_cont, _mm_set1_epi8(127));
2888#endif
2889 _lstm_IFOGh0 = _mm_dpbusd_epi32(_lstm_IFOGh0, _h_cont, _w);
2890
2891 kptr += 16;
2892 }
2893 {
2894 __m128i _w_shift = _mm_loadu_si128((const __m128i*)kptr);
2895 _lstm_IFOGh0 = _mm_sub_epi32(_lstm_IFOGh0, _w_shift);
2896 kptr += 16;
2897 }
2898#else
2899#if defined(__x86_64__) || defined(_M_X64)
2900 _sum2 = _mm_setzero_si128();
2901 _sum3 = _mm_setzero_si128();
2902 for (; i + 7 < num_output; i += 8)
2903 {
2904 __m128i _h_cont = _mm_castpd_si128(_mm_load1_pd((const double*)(hs + i)));
2905 __m128i _w0 = _mm_loadl_epi64((const __m128i*)kptr);
2906 __m128i _w1 = _mm_loadl_epi64((const __m128i*)(kptr + 8));
2907 __m128i _w2 = _mm_loadl_epi64((const __m128i*)(kptr + 16));
2908 __m128i _w3 = _mm_loadl_epi64((const __m128i*)(kptr + 24));
2909
2910#if __SSE4_1__
2911 _h_cont = _mm_cvtepi8_epi16(_h_cont);
2912 _w0 = _mm_cvtepi8_epi16(_w0);
2913 _w1 = _mm_cvtepi8_epi16(_w1);
2914 _w2 = _mm_cvtepi8_epi16(_w2);
2915 _w3 = _mm_cvtepi8_epi16(_w3);
2916#else
2917 _h_cont = _mm_unpacklo_epi8(_h_cont, _mm_cmpgt_epi8(_mm_setzero_si128(), _h_cont));
2918 _w0 = _mm_unpacklo_epi8(_w0, _mm_cmpgt_epi8(_mm_setzero_si128(), _w0));
2919 _w1 = _mm_unpacklo_epi8(_w1, _mm_cmpgt_epi8(_mm_setzero_si128(), _w1));
2920 _w2 = _mm_unpacklo_epi8(_w2, _mm_cmpgt_epi8(_mm_setzero_si128(), _w2));
2921 _w3 = _mm_unpacklo_epi8(_w3, _mm_cmpgt_epi8(_mm_setzero_si128(), _w3));
2922#endif
2923
2924#if __XOP__
2925 _sum0 = _mm_maddd_epi16(_w0, _h_cont, _sum0);
2926 _sum1 = _mm_maddd_epi16(_w1, _h_cont, _sum1);
2927 _sum2 = _mm_maddd_epi16(_w2, _h_cont, _sum2);
2928 _sum3 = _mm_maddd_epi16(_w3, _h_cont, _sum3);
2929#else
2930 __m128i _s0 = _mm_madd_epi16(_w0, _h_cont);
2931 __m128i _s1 = _mm_madd_epi16(_w1, _h_cont);
2932 __m128i _s2 = _mm_madd_epi16(_w2, _h_cont);
2933 __m128i _s3 = _mm_madd_epi16(_w3, _h_cont);
2934 _sum0 = _mm_add_epi32(_sum0, _s0);
2935 _sum1 = _mm_add_epi32(_sum1, _s1);
2936 _sum2 = _mm_add_epi32(_sum2, _s2);
2937 _sum3 = _mm_add_epi32(_sum3, _s3);
2938#endif
2939
2940 kptr += 32;
2941 }
2942 {
2943 transpose4x4_epi32(_sum0, _sum1, _sum2, _sum3);
2944 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum0);
2945 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum1);
2946 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum2);
2947 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _sum3);
2948 }
2949
2950 _sum0 = _mm_setzero_si128();
2951 _sum1 = _mm_setzero_si128();
2952#endif // defined(__x86_64__) || defined(_M_X64)
2953 for (; i + 3 < num_output; i += 4)
2954 {
2955 __m128i _h_cont = _mm_castps_si128(_mm_load1_ps((const float*)(hs + i)));
2956 __m128i _w0 = _mm_loadl_epi64((const __m128i*)kptr);
2957 __m128i _w1 = _mm_loadl_epi64((const __m128i*)(kptr + 8));
2958
2959#if __SSE4_1__
2960 _h_cont = _mm_cvtepi8_epi16(_h_cont);
2961 _w0 = _mm_cvtepi8_epi16(_w0);
2962 _w1 = _mm_cvtepi8_epi16(_w1);
2963#else
2964 _h_cont = _mm_unpacklo_epi8(_h_cont, _mm_cmpgt_epi8(_mm_setzero_si128(), _h_cont));
2965 _w0 = _mm_unpacklo_epi8(_w0, _mm_cmpgt_epi8(_mm_setzero_si128(), _w0));
2966 _w1 = _mm_unpacklo_epi8(_w1, _mm_cmpgt_epi8(_mm_setzero_si128(), _w1));
2967#endif
2968
2969#if __XOP__
2970 _sum0 = _mm_maddd_epi16(_w0, _h_cont, _sum0);
2971 _sum1 = _mm_maddd_epi16(_w1, _h_cont, _sum1);
2972#else
2973 __m128i _s0 = _mm_madd_epi16(_w0, _h_cont);
2974 __m128i _s1 = _mm_madd_epi16(_w1, _h_cont);
2975 _sum0 = _mm_add_epi32(_sum0, _s0);
2976 _sum1 = _mm_add_epi32(_sum1, _s1);
2977#endif
2978
2979 kptr += 16;
2980 }
2981 {
2982#if __SSSE3__
2983 __m128i _tmp0 = _mm_hadd_epi32(_sum0, _sum1);
2984 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _tmp0);
2985#else
2986 __m128i _tmp0 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(_sum0), _mm_castsi128_ps(_sum1), _MM_SHUFFLE(2, 0, 2, 0)));
2987 __m128i _tmp1 = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(_sum0), _mm_castsi128_ps(_sum1), _MM_SHUFFLE(3, 1, 3, 1)));
2988 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _tmp0);
2989 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _tmp1);
2990#endif // __SSSE3__
2991 }
2992#endif // __AVXVNNI__ || __AVX512VNNI__
2993 for (; i + 1 < num_output; i += 2)
2994 {
2995 __m128i _w = _mm_loadl_epi64((const __m128i*)kptr);
2996 __m128i _h_cont = _mm_set1_epi16(((const short*)(hs + i))[0]);
2997
2998#if __SSE4_1__
2999 _w = _mm_cvtepi8_epi16(_w);
3000 _h_cont = _mm_cvtepi8_epi16(_h_cont);
3001#else
3002 _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w));
3003 _h_cont = _mm_unpacklo_epi8(_h_cont, _mm_cmpgt_epi8(_mm_setzero_si128(), _h_cont));
3004#endif
3005
3006#if __XOP__
3007 _lstm_IFOGh0 = _mm_maddd_epi16(_w, _h_cont, _lstm_IFOGh0);
3008#else
3009 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _mm_madd_epi16(_w, _h_cont));
3010#endif
3011
3012 kptr += 8;
3013 }
3014 for (; i < num_output; i++)
3015 {
3016 __m128i _w = _mm_loadl_epi64((const __m128i*)kptr);
3017 __m128i _h_cont = _mm_set1_epi16(hs[i]);
3018
3019#if __SSE4_1__
3020 _w = _mm_cvtepi8_epi16(_w);
3021#else
3022 _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w));
3023#endif
3024
3025#if __XOP__
3026 _w = _mm_unpacklo_epi16(_w, _w);
3027
3028 _lstm_IFOGh0 = _mm_maccd_epi16(_w, _h_cont, _lstm_IFOGh0);
3029#else
3030 __m128i _sl = _mm_mullo_epi16(_w, _h_cont);
3031 __m128i _sh = _mm_mulhi_epi16(_w, _h_cont);
3032 __m128i _s0 = _mm_unpacklo_epi16(_sl, _sh);
3033
3034 _lstm_IFOGh0 = _mm_add_epi32(_lstm_IFOGh0, _s0);
3035#endif
3036
3037 kptr += 4;
3038 }
3039
3040 __m128 _descale_x = _mm_set1_ps(descale_x);
3041 __m128 _descale_h = _mm_set1_ps(descale_h);
3042
3043 __m128 _lstm_IFOG0 = _mm_loadu_ps(bias_c_IFOG);
3044
3045 __m128 _descale_xc_IFOG = _mm_loadu_ps(descales_ptr);
3046
3047 _lstm_IFOG0 = _mm_comp_fmadd_ps(_mm_cvtepi32_ps(_lstm_IFOGx0), _mm_mul_ps(_descale_x, _descale_xc_IFOG), _lstm_IFOG0);
3048
3049 __m128 _descale_hc_IFOG = _mm_loadu_ps(descales_ptr + 4);
3050
3051 _lstm_IFOG0 = _mm_comp_fmadd_ps(_mm_cvtepi32_ps(_lstm_IFOGh0), _mm_mul_ps(_descale_h, _descale_hc_IFOG), _lstm_IFOG0);
3052
3053 _mm_storeu_ps(gates_data, _lstm_IFOG0);
3054#else
3055 int Ix = 0;
3056 int Fx = 0;
3057 int Ox = 0;
3058 int Gx = 0;
3059 for (int i = 0; i < size; i++)
3060 {
3061 signed char xi = x[i];
3062
3063 Ix += kptr[0] * xi;
3064 Fx += kptr[1] * xi;
3065 Ox += kptr[2] * xi;
3066 Gx += kptr[3] * xi;
3067
3068 kptr += 4;
3069 }
3070
3071 int Ih = 0;
3072 int Fh = 0;
3073 int Oh = 0;
3074 int Gh = 0;
3075 for (int i = 0; i < num_output; i++)
3076 {
3077 signed char h_cont = hs[i];
3078
3079 Ih += kptr[0] * h_cont;
3080 Fh += kptr[1] * h_cont;
3081 Oh += kptr[2] * h_cont;
3082 Gh += kptr[3] * h_cont;
3083
3084 kptr += 4;
3085 }
3086
3087 const float descale_xc_I = descales_ptr[0];
3088 const float descale_xc_F = descales_ptr[1];
3089 const float descale_xc_O = descales_ptr[2];
3090 const float descale_xc_G = descales_ptr[3];
3091 const float descale_hc_I = descales_ptr[4];
3092 const float descale_hc_F = descales_ptr[5];
3093 const float descale_hc_O = descales_ptr[6];
3094 const float descale_hc_G = descales_ptr[7];
3095
3096 float I = bias_c_IFOG[0] + Ix * (descale_x * descale_xc_I) + Ih * (descale_h * descale_hc_I);
3097 float F = bias_c_IFOG[1] + Fx * (descale_x * descale_xc_F) + Fh * (descale_h * descale_hc_F);
3098 float O = bias_c_IFOG[2] + Ox * (descale_x * descale_xc_O) + Oh * (descale_h * descale_hc_O);
3099 float G = bias_c_IFOG[3] + Gx * (descale_x * descale_xc_G) + Gh * (descale_h * descale_hc_G);
3100
3101 gates_data[0] = I;
3102 gates_data[1] = F;
3103 gates_data[2] = O;
3104 gates_data[3] = G;
3105#endif // __SSE2__
3106 }
3107
3108 // lstm unit
3109 // sigmoid(I)
3110 // sigmoid(F)
3111 // sigmoid(O)
3112 // tanh(G)
3113 // c_t := f_t .* c_{t-1} + i_t .* g_t
3114 // h_t := o_t .* tanh[c_t]
3115 float* output_data = top_blob.row(ti);
3116
3117 float* cell_ptr = cell_state;
3118 float* hidden_ptr = hidden_state;
3119 float* tmp_hidden_ptr = tmp_hidden_state;
3120
3121 remain_hidden_size_start = 0;
3122#if __SSE2__
3123#if __AVX__
3124#if __AVX512F__
3125 nn_hidden_size = hidden_size >> 4;
3126 #pragma omp parallel for num_threads(opt.num_threads)
3127 for (int qq = 0; qq < nn_hidden_size; qq++)
3128 {
3129 int q = qq * 16;
3130
3131 const float* gates_data = gates.row(q);
3132
3133 __m512 _IFOG_0 = _mm512_loadu_ps(gates_data);
3134 __m512 _IFOG_1 = _mm512_loadu_ps(gates_data + 16);
3135 __m512 _IFOG_2 = _mm512_loadu_ps(gates_data + 32);
3136 __m512 _IFOG_3 = _mm512_loadu_ps(gates_data + 48);
3137
3138 __m512 _tmp0 = _mm512_shuffle_f32x4(_IFOG_0, _IFOG_1, _MM_SHUFFLE(1, 0, 1, 0));
3139 __m512 _tmp1 = _mm512_shuffle_f32x4(_IFOG_2, _IFOG_3, _MM_SHUFFLE(1, 0, 1, 0));
3140 __m512 _tmp2 = _mm512_shuffle_f32x4(_IFOG_0, _IFOG_1, _MM_SHUFFLE(3, 2, 3, 2));
3141 __m512 _tmp3 = _mm512_shuffle_f32x4(_IFOG_2, _IFOG_3, _MM_SHUFFLE(3, 2, 3, 2));
3142 __m512 _lstm_I = _mm512_shuffle_f32x4(_tmp0, _tmp1, _MM_SHUFFLE(2, 0, 2, 0));
3143 __m512 _lstm_F = _mm512_shuffle_f32x4(_tmp0, _tmp1, _MM_SHUFFLE(3, 1, 3, 1));
3144 __m512 _lstm_O = _mm512_shuffle_f32x4(_tmp2, _tmp3, _MM_SHUFFLE(2, 0, 2, 0));
3145 __m512 _lstm_G = _mm512_shuffle_f32x4(_tmp2, _tmp3, _MM_SHUFFLE(3, 1, 3, 1));
3146
3147 _lstm_I = sigmoid_avx512(_lstm_I);
3148 _lstm_F = sigmoid_avx512(_lstm_F);
3149 _lstm_O = sigmoid_avx512(_lstm_O);
3150 _lstm_G = tanh_avx512(_lstm_G);
3151
3152 __m512 _cell2 = _mm512_add_ps(_mm512_mul_ps(_lstm_F, _mm512_loadu_ps(cell_ptr + q)), _mm512_mul_ps(_lstm_I, _lstm_G));
3153 __m512 _lstm_H = _mm512_mul_ps(_lstm_O, tanh_avx512(_cell2));
3154
3155 _mm512_storeu_ps(cell_ptr + q, _cell2);
3156
3157 if (num_output == hidden_size)
3158 {
3159 _mm512_storeu_ps(hidden_ptr + q, _lstm_H);
3160 _mm512_storeu_ps(output_data + q, _lstm_H);
3161 }
3162 else
3163 {
3164 _mm512_storeu_ps(tmp_hidden_ptr + q, _lstm_H);
3165 }
3166 }
3167 remain_hidden_size_start += nn_hidden_size << 4;
3168 nn_hidden_size = (hidden_size - remain_hidden_size_start) >> 3;
3169#else
3170 nn_hidden_size = hidden_size >> 3;
3171#endif // __AVX512F__
3172 #pragma omp parallel for num_threads(opt.num_threads)
3173 for (int qq = 0; qq < nn_hidden_size; qq++)
3174 {
3175 int q = remain_hidden_size_start + qq * 8;
3176
3177 const float* gates_data = gates.row(q);
3178
3179 __m256 _IFOG_0 = _mm256_loadu_ps(gates_data);
3180 __m256 _IFOG_1 = _mm256_loadu_ps(gates_data + 8);
3181 __m256 _IFOG_2 = _mm256_loadu_ps(gates_data + 16);
3182 __m256 _IFOG_3 = _mm256_loadu_ps(gates_data + 24);
3183
3184#if __AVX512F__
3185 __m256 _lstm_I = _mm256_permute2f128_ps(_IFOG_0, _IFOG_2, _MM_SHUFFLE(0, 2, 0, 0));
3186 __m256 _lstm_F = _mm256_permute2f128_ps(_IFOG_0, _IFOG_2, _MM_SHUFFLE(0, 3, 0, 1));
3187 __m256 _lstm_O = _mm256_permute2f128_ps(_IFOG_1, _IFOG_3, _MM_SHUFFLE(0, 2, 0, 0));
3188 __m256 _lstm_G = _mm256_permute2f128_ps(_IFOG_1, _IFOG_3, _MM_SHUFFLE(0, 3, 0, 1));
3189#else
3190 // unzip4
3191 __m256 _tmp0 = _mm256_permute2f128_ps(_IFOG_0, _IFOG_2, _MM_SHUFFLE(0, 2, 0, 0));
3192 __m256 _tmp1 = _mm256_permute2f128_ps(_IFOG_1, _IFOG_3, _MM_SHUFFLE(0, 2, 0, 0));
3193 __m256 _tmp2 = _mm256_permute2f128_ps(_IFOG_0, _IFOG_2, _MM_SHUFFLE(0, 3, 0, 1));
3194 __m256 _tmp3 = _mm256_permute2f128_ps(_IFOG_1, _IFOG_3, _MM_SHUFFLE(0, 3, 0, 1));
3195 __m256 _tmp4 = _mm256_unpacklo_ps(_tmp0, _tmp1);
3196 __m256 _tmp5 = _mm256_unpacklo_ps(_tmp2, _tmp3);
3197 __m256 _tmp6 = _mm256_unpackhi_ps(_tmp0, _tmp1);
3198 __m256 _tmp7 = _mm256_unpackhi_ps(_tmp2, _tmp3);
3199 __m256 _lstm_I = _mm256_unpacklo_ps(_tmp4, _tmp5);
3200 __m256 _lstm_F = _mm256_unpackhi_ps(_tmp4, _tmp5);
3201 __m256 _lstm_O = _mm256_unpacklo_ps(_tmp6, _tmp7);
3202 __m256 _lstm_G = _mm256_unpackhi_ps(_tmp6, _tmp7);
3203#endif
3204
3205 _lstm_I = sigmoid_avx(_lstm_I);
3206 _lstm_F = sigmoid_avx(_lstm_F);
3207 _lstm_O = sigmoid_avx(_lstm_O);
3208 _lstm_G = tanh_avx(_lstm_G);
3209
3210 __m256 _cell2 = _mm256_add_ps(_mm256_mul_ps(_lstm_F, _mm256_loadu_ps(cell_ptr + q)), _mm256_mul_ps(_lstm_I, _lstm_G));
3211 __m256 _lstm_H = _mm256_mul_ps(_lstm_O, tanh_avx(_cell2));
3212
3213 _mm256_storeu_ps(cell_ptr + q, _cell2);
3214
3215 if (num_output == hidden_size)
3216 {
3217 _mm256_storeu_ps(hidden_ptr + q, _lstm_H);
3218 _mm256_storeu_ps(output_data + q, _lstm_H);
3219 }
3220 else
3221 {
3222 _mm256_storeu_ps(tmp_hidden_ptr + q, _lstm_H);
3223 }
3224 }
3225 remain_hidden_size_start += nn_hidden_size << 3;
3226 nn_hidden_size = (hidden_size - remain_hidden_size_start) >> 2;
3227#else
3228 nn_hidden_size = hidden_size >> 2;
3229#endif // __AVX__
3230 #pragma omp parallel for num_threads(opt.num_threads)
3231 for (int qq = 0; qq < nn_hidden_size; qq++)
3232 {
3233 int q = remain_hidden_size_start + qq * 4;
3234
3235 const float* gates_data = gates.row(q);
3236
3237 __m128 _lstm_I = _mm_loadu_ps(gates_data);
3238 __m128 _lstm_F = _mm_loadu_ps(gates_data + 4);
3239 __m128 _lstm_O = _mm_loadu_ps(gates_data + 8);
3240 __m128 _lstm_G = _mm_loadu_ps(gates_data + 12);
3241
3242#if !__AVX512F__
3243 _MM_TRANSPOSE4_PS(_lstm_I, _lstm_F, _lstm_O, _lstm_G);
3244#endif
3245
3246 _lstm_I = sigmoid_sse(_lstm_I);
3247 _lstm_F = sigmoid_sse(_lstm_F);
3248 _lstm_O = sigmoid_sse(_lstm_O);
3249 _lstm_G = tanh_sse(_lstm_G);
3250
3251 __m128 _cell2 = _mm_add_ps(_mm_mul_ps(_lstm_F, _mm_loadu_ps(cell_ptr + q)), _mm_mul_ps(_lstm_I, _lstm_G));
3252 __m128 _lstm_H = _mm_mul_ps(_lstm_O, tanh_sse(_cell2));
3253
3254 _mm_storeu_ps(cell_ptr + q, _cell2);
3255
3256 if (num_output == hidden_size)
3257 {
3258 _mm_storeu_ps(hidden_ptr + q, _lstm_H);
3259 _mm_storeu_ps(output_data + q, _lstm_H);
3260 }
3261 else
3262 {
3263 _mm_storeu_ps(tmp_hidden_ptr + q, _lstm_H);
3264 }
3265 }
3266 remain_hidden_size_start += nn_hidden_size << 2;
3267#endif // __SSE2__
3268 #pragma omp parallel for num_threads(opt.num_threads)
3269 for (int q = remain_hidden_size_start; q < hidden_size; q++)
3270 {
3271 const float* gates_data = gates.row(q);
3272
3273 float I = gates_data[0];
3274 float F = gates_data[1];
3275 float O = gates_data[2];
3276 float G = gates_data[3];
3277
3278 I = 1.f / (1.f + expf(-I));
3279 F = 1.f / (1.f + expf(-F));
3280 O = 1.f / (1.f + expf(-O));
3281 G = tanhf(G);
3282
3283 float cell2 = F * cell_ptr[q] + I * G;
3284 float H = O * tanhf(cell2);
3285
3286 cell_ptr[q] = cell2;
3287 if (num_output == hidden_size)
3288 {
3289 hidden_ptr[q] = H;
3290 output_data[q] = H;
3291 }
3292 else
3293 {
3294 tmp_hidden_ptr[q] = H;
3295 }
3296 }
3297
3298 if (num_output != hidden_size)
3299 {
3300 // int nn_num_output = num_output >> 2;
3301 // int remain_num_output_start = nn_num_output << 2;
3302 // #pragma omp parallel for num_threads(opt.num_threads)
3303 // for (int qq = 0; qq < nn_num_output; qq++)
3304 // {
3305 // int q = qq * 4;
3306 //
3307 // }
3308 int remain_num_output_start = 0;
3309 #pragma omp parallel for num_threads(opt.num_threads)
3310 for (int q = remain_num_output_start; q < num_output; q++)
3311 {
3312 const float* hr = weight_hr.row(q);
3313 const float* tmp_hidden_ptr = tmp_hidden_state;
3314
3315 float H = 0;
3316 for (int i = 0; i < hidden_size; i++)
3317 {
3318 H += tmp_hidden_ptr[i] * hr[i];
3319 }
3320
3321 hidden_ptr[q] = H;
3322 output_data[q] = H;
3323 }
3324 }
3325 }
3326}