Elffuss BraidHub

kikocis/ncnn public

master · 33.9 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_ARM82DOT && __aarch64__ && !__ARM_FEATURE_DOTPROD
16void lstm_transform_weight_int8_asimddp(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_int8_asimddp(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int elemtype, 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);
18#endif
19
20#if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2)
21void lstm_int8_gate_output_vfpv4(const Mat& gates, const Mat& weight_hr, Mat& hidden_state, Mat& tmp_hidden_state, Mat& cell_state, Mat& top_blob, int ti, int elemtype, const Option& opt);
22#endif
23
24static 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)
25{
26#if NCNN_RUNTIME_CPU && NCNN_ARM82DOT && __aarch64__ && !__ARM_FEATURE_DOTPROD
27 if (ncnn::cpu_support_arm_asimddp())
28 {
29 lstm_transform_weight_int8_asimddp(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);
30 return;
31 }
32#endif
33
34 weight_data_tm.create(size + num_output, hidden_size, num_directions, 4u, 4);
35 weight_data_tm_int8_descales.create(4 + 4, hidden_size, num_directions);
36 bias_c_tm.create(hidden_size, 1, num_directions, 16u, 4);
37
38 #pragma omp parallel for num_threads(opt.num_threads)
39 for (int dr = 0; dr < num_directions; dr++)
40 {
41 const Mat weight_xc_dr = weight_xc.channel(dr);
42 const Mat weight_hc_dr = weight_hc.channel(dr);
43 const Mat bias_c_dr = bias_c.channel(dr);
44 const float* weight_xc_int8_scales_ptr = weight_xc_int8_scales.row(dr);
45 const float* weight_hc_int8_scales_ptr = weight_hc_int8_scales.row(dr);
46
47 Mat weight_data_tm_dr = weight_data_tm.channel(dr);
48 Mat bias_c_tm_dr = bias_c_tm.channel(dr);
49 Mat weight_data_tm_int8_descales_dr = weight_data_tm_int8_descales.channel(dr);
50
51 const float* bias_c_I = bias_c_dr.row(0);
52 const float* bias_c_F = bias_c_dr.row(1);
53 const float* bias_c_O = bias_c_dr.row(2);
54 const float* bias_c_G = bias_c_dr.row(3);
55
56 float* bias_c_IFOG = bias_c_tm_dr.row(0);
57
58 int q = 0;
59 for (; q < hidden_size; q++)
60 {
61 bias_c_IFOG[0] = bias_c_I[q];
62 bias_c_IFOG[1] = bias_c_F[q];
63 bias_c_IFOG[2] = bias_c_O[q];
64 bias_c_IFOG[3] = bias_c_G[q];
65
66 bias_c_IFOG += 4;
67
68 const signed char* weight_xc_I = weight_xc_dr.row<const signed char>(hidden_size * 0 + q);
69 const signed char* weight_xc_F = weight_xc_dr.row<const signed char>(hidden_size * 1 + q);
70 const signed char* weight_xc_O = weight_xc_dr.row<const signed char>(hidden_size * 2 + q);
71 const signed char* weight_xc_G = weight_xc_dr.row<const signed char>(hidden_size * 3 + q);
72
73 const signed char* weight_hc_I = weight_hc_dr.row<const signed char>(hidden_size * 0 + q);
74 const signed char* weight_hc_F = weight_hc_dr.row<const signed char>(hidden_size * 1 + q);
75 const signed char* weight_hc_O = weight_hc_dr.row<const signed char>(hidden_size * 2 + q);
76 const signed char* weight_hc_G = weight_hc_dr.row<const signed char>(hidden_size * 3 + q);
77
78 signed char* kptr = weight_data_tm_dr.row<signed char>(q);
79 float* descales_ptr = weight_data_tm_int8_descales_dr.row(q);
80
81 int i = 0;
82#if __ARM_NEON
83#if __ARM_FEATURE_DOTPROD
84 for (; i + 3 < size; i += 4)
85 {
86 kptr[0] = weight_xc_I[i];
87 kptr[1] = weight_xc_I[i + 1];
88 kptr[2] = weight_xc_I[i + 2];
89 kptr[3] = weight_xc_I[i + 3];
90 kptr[4] = weight_xc_F[i];
91 kptr[5] = weight_xc_F[i + 1];
92 kptr[6] = weight_xc_F[i + 2];
93 kptr[7] = weight_xc_F[i + 3];
94 kptr[8 + 0] = weight_xc_O[i];
95 kptr[8 + 1] = weight_xc_O[i + 1];
96 kptr[8 + 2] = weight_xc_O[i + 2];
97 kptr[8 + 3] = weight_xc_O[i + 3];
98 kptr[8 + 4] = weight_xc_G[i];
99 kptr[8 + 5] = weight_xc_G[i + 1];
100 kptr[8 + 6] = weight_xc_G[i + 2];
101 kptr[8 + 7] = weight_xc_G[i + 3];
102 kptr += 16;
103 }
104#else
105 for (; i + 7 < size; i += 8)
106 {
107 vst1_s8(kptr, vld1_s8(weight_xc_I + i));
108 vst1_s8(kptr + 8, vld1_s8(weight_xc_F + i));
109 vst1_s8(kptr + 16, vld1_s8(weight_xc_O + i));
110 vst1_s8(kptr + 24, vld1_s8(weight_xc_G + i));
111 kptr += 32;
112 }
113#endif // __ARM_FEATURE_DOTPROD
114 for (; i + 1 < size; i += 2)
115 {
116 kptr[0] = weight_xc_I[i];
117 kptr[1] = weight_xc_I[i + 1];
118 kptr[2] = weight_xc_F[i];
119 kptr[3] = weight_xc_F[i + 1];
120 kptr[4] = weight_xc_O[i];
121 kptr[5] = weight_xc_O[i + 1];
122 kptr[6] = weight_xc_G[i];
123 kptr[7] = weight_xc_G[i + 1];
124 kptr += 8;
125 }
126#endif // __ARM_NEON
127 for (; i < size; i++)
128 {
129 kptr[0] = weight_xc_I[i];
130 kptr[1] = weight_xc_F[i];
131 kptr[2] = weight_xc_O[i];
132 kptr[3] = weight_xc_G[i];
133 kptr += 4;
134 }
135
136 i = 0;
137#if __ARM_NEON
138#if __ARM_FEATURE_DOTPROD
139 for (; i + 3 < num_output; i += 4)
140 {
141 kptr[0] = weight_hc_I[i];
142 kptr[1] = weight_hc_I[i + 1];
143 kptr[2] = weight_hc_I[i + 2];
144 kptr[3] = weight_hc_I[i + 3];
145 kptr[4] = weight_hc_F[i];
146 kptr[5] = weight_hc_F[i + 1];
147 kptr[6] = weight_hc_F[i + 2];
148 kptr[7] = weight_hc_F[i + 3];
149 kptr[8 + 0] = weight_hc_O[i];
150 kptr[8 + 1] = weight_hc_O[i + 1];
151 kptr[8 + 2] = weight_hc_O[i + 2];
152 kptr[8 + 3] = weight_hc_O[i + 3];
153 kptr[8 + 4] = weight_hc_G[i];
154 kptr[8 + 5] = weight_hc_G[i + 1];
155 kptr[8 + 6] = weight_hc_G[i + 2];
156 kptr[8 + 7] = weight_hc_G[i + 3];
157 kptr += 16;
158 }
159#else
160 for (; i + 7 < num_output; i += 8)
161 {
162 vst1_s8(kptr, vld1_s8(weight_hc_I + i));
163 vst1_s8(kptr + 8, vld1_s8(weight_hc_F + i));
164 vst1_s8(kptr + 16, vld1_s8(weight_hc_O + i));
165 vst1_s8(kptr + 24, vld1_s8(weight_hc_G + i));
166 kptr += 32;
167 }
168#endif // __ARM_FEATURE_DOTPROD
169 for (; i + 1 < num_output; i += 2)
170 {
171 kptr[0] = weight_hc_I[i];
172 kptr[1] = weight_hc_I[i + 1];
173 kptr[2] = weight_hc_F[i];
174 kptr[3] = weight_hc_F[i + 1];
175 kptr[4] = weight_hc_O[i];
176 kptr[5] = weight_hc_O[i + 1];
177 kptr[6] = weight_hc_G[i];
178 kptr[7] = weight_hc_G[i + 1];
179 kptr += 8;
180 }
181#endif // __ARM_NEON
182 for (; i < num_output; i++)
183 {
184 kptr[0] = weight_hc_I[i];
185 kptr[1] = weight_hc_F[i];
186 kptr[2] = weight_hc_O[i];
187 kptr[3] = weight_hc_G[i];
188 kptr += 4;
189 }
190
191 descales_ptr[0] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 0 + q];
192 descales_ptr[1] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 1 + q];
193 descales_ptr[2] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 2 + q];
194 descales_ptr[3] = 1.f / weight_xc_int8_scales_ptr[hidden_size * 3 + q];
195 descales_ptr[4] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 0 + q];
196 descales_ptr[5] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 1 + q];
197 descales_ptr[6] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 2 + q];
198 descales_ptr[7] = 1.f / weight_hc_int8_scales_ptr[hidden_size * 3 + q];
199 }
200 }
201}
202
203static void lstm_int8_gate_output(const Mat& gates, const Mat& weight_hr, Mat& hidden_state, Mat& tmp_hidden_state, Mat& cell_state, Mat& top_blob, int ti, int elemtype, const Option& opt)
204{
205#if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2)
206 if (ncnn::cpu_support_arm_vfpv4())
207 {
208 lstm_int8_gate_output_vfpv4(gates, weight_hr, hidden_state, tmp_hidden_state, cell_state, top_blob, ti, elemtype, opt);
209 return;
210 }
211#endif
212
213 const int num_output = top_blob.w;
214 const int hidden_size = cell_state.w;
215
216 // lstm unit
217 // sigmoid(I)
218 // sigmoid(F)
219 // sigmoid(O)
220 // tanh(G)
221 // c_t := f_t .* c_{t-1} + i_t .* g_t
222 // h_t := o_t .* tanh[c_t]
223 float* output_data = top_blob.row(ti);
224
225 float* cell_ptr = cell_state;
226 float* hidden_ptr = hidden_state;
227 float* tmp_hidden_ptr = tmp_hidden_state;
228
229 int remain_hidden_size_start = 0;
230#if __ARM_NEON
231 int nn_hidden_size = hidden_size >> 2;
232 remain_hidden_size_start = nn_hidden_size << 2;
233
234 #pragma omp parallel for num_threads(opt.num_threads)
235 for (int qq = 0; qq < nn_hidden_size; qq++)
236 {
237 int q = qq * 4;
238
239 const float* gates_data = gates.row(q);
240
241 float32x4x4_t _IFOG_4x4 = vld4q_f32(gates_data);
242
243 float32x4_t _lstm_I = sigmoid_ps(_IFOG_4x4.val[0]);
244 float32x4_t _lstm_F = sigmoid_ps(_IFOG_4x4.val[1]);
245 float32x4_t _lstm_O = sigmoid_ps(_IFOG_4x4.val[2]);
246 float32x4_t _lstm_G = tanh_ps(_IFOG_4x4.val[3]);
247
248 float32x4_t _cell2 = vaddq_f32(vmulq_f32(_lstm_F, vld1q_f32(cell_ptr + q)), vmulq_f32(_lstm_I, _lstm_G));
249 float32x4_t _lstm_H = vmulq_f32(_lstm_O, tanh_ps(_cell2));
250
251 vst1q_f32(cell_ptr + q, _cell2);
252
253 if (num_output == hidden_size)
254 {
255 vst1q_f32(hidden_ptr + q, _lstm_H);
256
257 if (elemtype == 1)
258 {
259 // fp32
260 vst1q_f32(output_data + q, _lstm_H);
261 }
262 if (elemtype == 2)
263 {
264 // fp16
265 unsigned short* outptr = (unsigned short*)output_data + q;
266#if (__ARM_FP & 2)
267#if NCNN_GNU_INLINE_ASM
268#if __aarch64__
269 asm volatile(
270 "fcvtn v0.4h, %2.4s \n"
271 "st1 {v0.4h}, [%0] \n"
272 : "=r"(outptr) // %0
273 : "0"(outptr),
274 "w"(_lstm_H)
275 : "memory", "v0");
276#else // __aarch64__
277 asm volatile(
278 "vcvt.f16.f32 d0, %q2 \n"
279 "vst1.u16 {d0}, [%0] \n"
280 : "=r"(outptr) // %0
281 : "0"(outptr),
282 "w"(_lstm_H)
283 : "memory", "q0");
284#endif // __aarch64__
285#else // NCNN_GNU_INLINE_ASM
286 vst1_u16(outptr, (uint16x4_t)vcvt_f16_f32(_lstm_H));
287#endif // NCNN_GNU_INLINE_ASM
288#else
289 outptr[q] = float32_to_float16(hidden_ptr[q]);
290 outptr[q + 1] = float32_to_float16(hidden_ptr[q + 1]);
291 outptr[q + 2] = float32_to_float16(hidden_ptr[q + 2]);
292 outptr[q + 3] = float32_to_float16(hidden_ptr[q + 3]);
293#endif // (__ARM_FP & 2)
294 }
295 if (elemtype == 4)
296 {
297 // bf16
298 vst1_u16((unsigned short*)output_data + q, float2bfloat(_lstm_H));
299 }
300 }
301 else
302 {
303 vst1q_f32(tmp_hidden_ptr + q, _lstm_H);
304 }
305 }
306#endif // __ARM_NEON
307 #pragma omp parallel for num_threads(opt.num_threads)
308 for (int q = remain_hidden_size_start; q < hidden_size; q++)
309 {
310 const float* gates_data = gates.row(q);
311
312 float I = gates_data[0];
313 float F = gates_data[1];
314 float O = gates_data[2];
315 float G = gates_data[3];
316
317 I = 1.f / (1.f + expf(-I));
318 F = 1.f / (1.f + expf(-F));
319 O = 1.f / (1.f + expf(-O));
320 G = tanhf(G);
321
322 float cell2 = F * cell_ptr[q] + I * G;
323 float H = O * tanhf(cell2);
324
325 cell_ptr[q] = cell2;
326 if (num_output == hidden_size)
327 {
328 hidden_ptr[q] = H;
329
330 if (elemtype == 1)
331 {
332 output_data[q] = H;
333 }
334 if (elemtype == 2)
335 {
336 ((unsigned short*)output_data)[q] = float32_to_float16(H);
337 }
338 if (elemtype == 4)
339 {
340 ((unsigned short*)output_data)[q] = float32_to_bfloat16(H);
341 }
342 }
343 else
344 {
345 tmp_hidden_ptr[q] = H;
346 }
347 }
348
349 if (num_output != hidden_size)
350 {
351 // int nn_num_output = num_output >> 2;
352 // int remain_num_output_start = nn_num_output << 2;
353 // #pragma omp parallel for num_threads(opt.num_threads)
354 // for (int qq = 0; qq < nn_num_output; qq++)
355 // {
356 // int q = qq * 4;
357 //
358 // }
359 int remain_num_output_start = 0;
360 #pragma omp parallel for num_threads(opt.num_threads)
361 for (int q = remain_num_output_start; q < num_output; q++)
362 {
363 const float* hr = weight_hr.row(q);
364 const float* tmp_hidden_ptr = tmp_hidden_state;
365
366 float H = 0;
367 for (int i = 0; i < hidden_size; i++)
368 {
369 H += tmp_hidden_ptr[i] * hr[i];
370 }
371
372 hidden_ptr[q] = H;
373
374 if (elemtype == 1)
375 {
376 output_data[q] = H;
377 }
378 if (elemtype == 2)
379 {
380 ((unsigned short*)output_data)[q] = float32_to_float16(H);
381 }
382 if (elemtype == 4)
383 {
384 ((unsigned short*)output_data)[q] = float32_to_bfloat16(H);
385 }
386 }
387 }
388}
389
390static void lstm_int8(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int elemtype, 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)
391{
392#if NCNN_RUNTIME_CPU && NCNN_ARM82DOT && __aarch64__ && !__ARM_FEATURE_DOTPROD
393 if (ncnn::cpu_support_arm_asimddp())
394 {
395 lstm_int8_asimddp(bottom_blob_int8, bottom_blob_int8_descales, top_blob, elemtype, reverse, weight_data_tm, weight_data_tm_int8_descales, bias_c, weight_hr, hidden_state, cell_state, opt);
396 return;
397 }
398#endif
399
400 int size = bottom_blob_int8.w;
401 int T = bottom_blob_int8.h;
402
403 int num_output = top_blob.w;
404 int hidden_size = cell_state.w;
405
406 // 4 x hidden_size
407 Mat gates(4, hidden_size, 4u, opt.workspace_allocator);
408
409 Mat tmp_hidden_state;
410 if (num_output != hidden_size)
411 {
412 tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator);
413 }
414
415 Mat hidden_state_int8(num_output, (size_t)1u, 1, opt.workspace_allocator);
416 float hidden_state_int8_scale = 1.f;
417 float hidden_state_int8_descale = 1.f;
418
419 // unroll
420 for (int t = 0; t < T; t++)
421 {
422 int ti = reverse ? T - 1 - t : t;
423
424 // dynamic quantize hidden_state
425 {
426 float absmax = 0.f;
427 for (int i = 0; i < num_output; i++)
428 {
429 absmax = std::max(absmax, (float)fabs(hidden_state[i]));
430 }
431
432 if (absmax == 0.f)
433 {
434 hidden_state_int8.fill<signed char>(0);
435 }
436 else
437 {
438 hidden_state_int8_scale = 127.f / absmax;
439 hidden_state_int8_descale = absmax / 127.f;
440
441 signed char* hs = hidden_state_int8;
442 for (int i = 0; i < num_output; i++)
443 {
444 hs[i] = float2int8(hidden_state[i] * hidden_state_int8_scale);
445 }
446 }
447 }
448
449 #pragma omp parallel for num_threads(opt.num_threads)
450 for (int q = 0; q < hidden_size; q++)
451 {
452 const signed char* x = bottom_blob_int8.row<const signed char>(ti);
453 const signed char* hs = hidden_state_int8;
454 const float descale_x = bottom_blob_int8_descales[ti];
455 const float descale_h = hidden_state_int8_descale;
456
457 // gate reset update
458 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
459
460 const signed char* kptr = weight_data_tm.row<const signed char>(q);
461 const float* descales_ptr = weight_data_tm_int8_descales.row(q);
462
463 float* gates_data = gates.row(q);
464
465#if __ARM_NEON
466 int32x4_t _lstm_IFOGx0 = vdupq_n_s32(0);
467 int i = 0;
468#if __ARM_FEATURE_DOTPROD
469 int32x4_t _sum1 = vdupq_n_s32(0);
470 int32x4_t _sum2 = vdupq_n_s32(0);
471 int32x4_t _sum3 = vdupq_n_s32(0);
472 for (; i + 15 < size; i += 16)
473 {
474 int8x16_t _xi = vld1q_s8(x + i);
475 int8x16_t _w0 = vld1q_s8(kptr);
476 int8x16_t _w1 = vld1q_s8(kptr + 16);
477 int8x16_t _w2 = vld1q_s8(kptr + 32);
478 int8x16_t _w3 = vld1q_s8(kptr + 48);
479 _lstm_IFOGx0 = vdotq_laneq_s32(_lstm_IFOGx0, _w0, _xi, 0);
480 _sum1 = vdotq_laneq_s32(_sum1, _w1, _xi, 1);
481 _sum2 = vdotq_laneq_s32(_sum2, _w2, _xi, 2);
482 _sum3 = vdotq_laneq_s32(_sum3, _w3, _xi, 3);
483
484 kptr += 64;
485 }
486 for (; i + 7 < size; i += 8)
487 {
488 int8x8_t _xi = vld1_s8(x + i);
489 int8x16_t _w0 = vld1q_s8(kptr);
490 int8x16_t _w1 = vld1q_s8(kptr + 16);
491 _lstm_IFOGx0 = vdotq_lane_s32(_lstm_IFOGx0, _w0, _xi, 0);
492 _sum1 = vdotq_lane_s32(_sum1, _w1, _xi, 1);
493
494 kptr += 32;
495 }
496 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum1);
497 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum2);
498 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum3);
499#else
500 int32x4_t _sum0 = vdupq_n_s32(0);
501 int32x4_t _sum1 = vdupq_n_s32(0);
502 int32x4_t _sum2 = vdupq_n_s32(0);
503 int32x4_t _sum3 = vdupq_n_s32(0);
504 for (; i + 15 < size; i += 16)
505 {
506#if NCNN_GNU_INLINE_ASM && !__aarch64__
507 const signed char* xptr = x + i;
508
509 asm volatile(
510 "vldm %1!, {d0-d7} \n"
511 "vld1.s8 {d16-d17}, [%0] \n"
512 "vmull.s8 q4, d0, d16 \n"
513 "vmull.s8 q5, d1, d16 \n"
514 "vmull.s8 q6, d2, d16 \n"
515 "vmull.s8 q7, d3, d16 \n"
516 "vmlal.s8 q4, d4, d17 \n"
517 "vmlal.s8 q5, d5, d17 \n"
518 "vmlal.s8 q6, d6, d17 \n"
519 "vmlal.s8 q7, d7, d17 \n"
520 "vpadal.s16 %q2, q4 \n"
521 "vpadal.s16 %q3, q5 \n"
522 "vpadal.s16 %q4, q6 \n"
523 "vpadal.s16 %q5, q7 \n"
524 : "=r"(xptr), "=r"(kptr), "=w"(_sum0), "=w"(_sum1), "=w"(_sum2), "=w"(_sum3)
525 : "0"(xptr), "1"(kptr), "2"(_sum0), "3"(_sum1), "4"(_sum2), "5"(_sum3)
526 : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8");
527#else
528 int8x16_t _xi = vld1q_s8(x + i);
529 int8x16_t _w0 = vld1q_s8(kptr);
530 int8x16_t _w1 = vld1q_s8(kptr + 16);
531 int8x16_t _w2 = vld1q_s8(kptr + 32);
532 int8x16_t _w3 = vld1q_s8(kptr + 48);
533
534 int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), vget_low_s8(_xi));
535 int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), vget_low_s8(_xi));
536 int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), vget_low_s8(_xi));
537 int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), vget_low_s8(_xi));
538 _s0 = vmlal_s8(_s0, vget_low_s8(_w2), vget_high_s8(_xi));
539 _s1 = vmlal_s8(_s1, vget_high_s8(_w2), vget_high_s8(_xi));
540 _s2 = vmlal_s8(_s2, vget_low_s8(_w3), vget_high_s8(_xi));
541 _s3 = vmlal_s8(_s3, vget_high_s8(_w3), vget_high_s8(_xi));
542 _sum0 = vpadalq_s16(_sum0, _s0);
543 _sum1 = vpadalq_s16(_sum1, _s1);
544 _sum2 = vpadalq_s16(_sum2, _s2);
545 _sum3 = vpadalq_s16(_sum3, _s3);
546
547 kptr += 64;
548#endif
549 }
550 for (; i + 7 < size; i += 8)
551 {
552 int8x8_t _xi = vld1_s8(x + i);
553 int8x16_t _w0 = vld1q_s8(kptr);
554 int8x16_t _w1 = vld1q_s8(kptr + 16);
555
556 int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), _xi);
557 int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), _xi);
558 int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), _xi);
559 int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), _xi);
560 _sum0 = vpadalq_s16(_sum0, _s0);
561 _sum1 = vpadalq_s16(_sum1, _s1);
562 _sum2 = vpadalq_s16(_sum2, _s2);
563 _sum3 = vpadalq_s16(_sum3, _s3);
564
565 kptr += 32;
566 }
567 {
568 int32x4x2_t _tmp0 = vzipq_s32(_sum0, _sum1);
569 int32x4x2_t _tmp1 = vzipq_s32(_sum2, _sum3);
570 _sum0 = vcombine_s32(vget_low_s32(_tmp0.val[0]), vget_low_s32(_tmp1.val[0]));
571 _sum1 = vcombine_s32(vget_high_s32(_tmp0.val[0]), vget_high_s32(_tmp1.val[0]));
572 _sum2 = vcombine_s32(vget_low_s32(_tmp0.val[1]), vget_low_s32(_tmp1.val[1]));
573 _sum3 = vcombine_s32(vget_high_s32(_tmp0.val[1]), vget_high_s32(_tmp1.val[1]));
574 }
575 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum0);
576 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum1);
577 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum2);
578 _lstm_IFOGx0 = vaddq_s32(_lstm_IFOGx0, _sum3);
579#endif // __ARM_FEATURE_DOTPROD
580 for (; i + 3 < size; i += 4)
581 {
582#if __ARM_FEATURE_DOTPROD
583 int8x8_t _xi = vld1_s8(x + i);
584 int8x16_t _w = vld1q_s8(kptr);
585 _lstm_IFOGx0 = vdotq_lane_s32(_lstm_IFOGx0, _w, _xi, 0);
586#else
587 int16x4_t _xi01 = vreinterpret_s16_s8(vld1_s8(x + i));
588 int8x8_t _xi0 = vreinterpret_s8_s16(vdup_lane_s16(_xi01, 0));
589 int8x8_t _xi1 = vreinterpret_s8_s16(vdup_lane_s16(_xi01, 1));
590 int8x16_t _w01 = vld1q_s8(kptr);
591
592 int16x8_t _lstm_IFOGx = vmull_s8(vget_low_s8(_w01), _xi0);
593 _lstm_IFOGx = vmlal_s8(_lstm_IFOGx, vget_high_s8(_w01), _xi1);
594 _lstm_IFOGx0 = vpadalq_s16(_lstm_IFOGx0, _lstm_IFOGx);
595#endif // __ARM_FEATURE_DOTPROD
596
597 kptr += 16;
598 }
599 for (; i + 1 < size; i += 2)
600 {
601 int8x8_t _xi = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(vld1_s8(x + i)), 0));
602 int8x8_t _w = vld1_s8(kptr);
603
604 int16x8_t _lstm_IFOGx = vmull_s8(_w, _xi);
605 _lstm_IFOGx0 = vpadalq_s16(_lstm_IFOGx0, _lstm_IFOGx);
606
607 kptr += 8;
608 }
609 for (; i < size; i++)
610 {
611 int8x8_t _xi = vdup_n_s8(x[i]);
612 int8x8_t _w = vld1_s8(kptr);
613
614 int16x8_t _lstm_IFOGx = vmull_s8(_w, _xi);
615 _lstm_IFOGx0 = vaddw_s16(_lstm_IFOGx0, vget_low_s16(_lstm_IFOGx));
616
617 kptr += 4;
618 }
619
620 int32x4_t _lstm_IFOGh0 = vdupq_n_s32(0);
621 i = 0;
622#if __ARM_FEATURE_DOTPROD
623 _sum1 = vdupq_n_s32(0);
624 _sum2 = vdupq_n_s32(0);
625 _sum3 = vdupq_n_s32(0);
626 for (; i + 15 < num_output; i += 16)
627 {
628 int8x16_t _h_cont = vld1q_s8(hs + i);
629 int8x16_t _w0 = vld1q_s8(kptr);
630 int8x16_t _w1 = vld1q_s8(kptr + 16);
631 int8x16_t _w2 = vld1q_s8(kptr + 32);
632 int8x16_t _w3 = vld1q_s8(kptr + 48);
633 _lstm_IFOGh0 = vdotq_laneq_s32(_lstm_IFOGh0, _w0, _h_cont, 0);
634 _sum1 = vdotq_laneq_s32(_sum1, _w1, _h_cont, 1);
635 _sum2 = vdotq_laneq_s32(_sum2, _w2, _h_cont, 2);
636 _sum3 = vdotq_laneq_s32(_sum3, _w3, _h_cont, 3);
637
638 kptr += 64;
639 }
640 for (; i + 7 < num_output; i += 8)
641 {
642 int8x8_t _h_cont = vld1_s8(hs + i);
643 int8x16_t _w0 = vld1q_s8(kptr);
644 int8x16_t _w1 = vld1q_s8(kptr + 16);
645 _lstm_IFOGh0 = vdotq_lane_s32(_lstm_IFOGh0, _w0, _h_cont, 0);
646 _sum1 = vdotq_lane_s32(_sum1, _w1, _h_cont, 1);
647
648 kptr += 32;
649 }
650 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum1);
651 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum2);
652 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum3);
653#else
654 _sum0 = vdupq_n_s32(0);
655 _sum1 = vdupq_n_s32(0);
656 _sum2 = vdupq_n_s32(0);
657 _sum3 = vdupq_n_s32(0);
658 for (; i + 15 < num_output; i += 16)
659 {
660#if NCNN_GNU_INLINE_ASM && !__aarch64__
661 const signed char* hsptr = hs + i;
662
663 asm volatile(
664 "vldm %1!, {d0-d7} \n"
665 "vld1.s8 {d16-d17}, [%0] \n"
666 "vmull.s8 q4, d0, d16 \n"
667 "vmull.s8 q5, d1, d16 \n"
668 "vmull.s8 q6, d2, d16 \n"
669 "vmull.s8 q7, d3, d16 \n"
670 "vmlal.s8 q4, d4, d17 \n"
671 "vmlal.s8 q5, d5, d17 \n"
672 "vmlal.s8 q6, d6, d17 \n"
673 "vmlal.s8 q7, d7, d17 \n"
674 "vpadal.s16 %q2, q4 \n"
675 "vpadal.s16 %q3, q5 \n"
676 "vpadal.s16 %q4, q6 \n"
677 "vpadal.s16 %q5, q7 \n"
678 : "=r"(hsptr), "=r"(kptr), "=w"(_sum0), "=w"(_sum1), "=w"(_sum2), "=w"(_sum3)
679 : "0"(hsptr), "1"(kptr), "2"(_sum0), "3"(_sum1), "4"(_sum2), "5"(_sum3)
680 : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8");
681#else
682 int8x16_t _h_cont = vld1q_s8(hs + i);
683 int8x16_t _w0 = vld1q_s8(kptr);
684 int8x16_t _w1 = vld1q_s8(kptr + 16);
685 int8x16_t _w2 = vld1q_s8(kptr + 32);
686 int8x16_t _w3 = vld1q_s8(kptr + 48);
687
688 int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), vget_low_s8(_h_cont));
689 int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), vget_low_s8(_h_cont));
690 int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), vget_low_s8(_h_cont));
691 int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), vget_low_s8(_h_cont));
692 _s0 = vmlal_s8(_s0, vget_low_s8(_w2), vget_high_s8(_h_cont));
693 _s1 = vmlal_s8(_s1, vget_high_s8(_w2), vget_high_s8(_h_cont));
694 _s2 = vmlal_s8(_s2, vget_low_s8(_w3), vget_high_s8(_h_cont));
695 _s3 = vmlal_s8(_s3, vget_high_s8(_w3), vget_high_s8(_h_cont));
696 _sum0 = vpadalq_s16(_sum0, _s0);
697 _sum1 = vpadalq_s16(_sum1, _s1);
698 _sum2 = vpadalq_s16(_sum2, _s2);
699 _sum3 = vpadalq_s16(_sum3, _s3);
700
701 kptr += 64;
702#endif
703 }
704 for (; i + 7 < num_output; i += 8)
705 {
706 int8x8_t _h_cont = vld1_s8(hs + i);
707 int8x16_t _w0 = vld1q_s8(kptr);
708 int8x16_t _w1 = vld1q_s8(kptr + 16);
709
710 int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), _h_cont);
711 int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), _h_cont);
712 int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), _h_cont);
713 int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), _h_cont);
714 _sum0 = vpadalq_s16(_sum0, _s0);
715 _sum1 = vpadalq_s16(_sum1, _s1);
716 _sum2 = vpadalq_s16(_sum2, _s2);
717 _sum3 = vpadalq_s16(_sum3, _s3);
718
719 kptr += 32;
720 }
721 {
722 int32x4x2_t _tmp0 = vzipq_s32(_sum0, _sum1);
723 int32x4x2_t _tmp1 = vzipq_s32(_sum2, _sum3);
724 _sum0 = vcombine_s32(vget_low_s32(_tmp0.val[0]), vget_low_s32(_tmp1.val[0]));
725 _sum1 = vcombine_s32(vget_high_s32(_tmp0.val[0]), vget_high_s32(_tmp1.val[0]));
726 _sum2 = vcombine_s32(vget_low_s32(_tmp0.val[1]), vget_low_s32(_tmp1.val[1]));
727 _sum3 = vcombine_s32(vget_high_s32(_tmp0.val[1]), vget_high_s32(_tmp1.val[1]));
728 }
729 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum0);
730 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum1);
731 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum2);
732 _lstm_IFOGh0 = vaddq_s32(_lstm_IFOGh0, _sum3);
733#endif // __ARM_FEATURE_DOTPROD
734 for (; i + 3 < num_output; i += 4)
735 {
736#if __ARM_FEATURE_DOTPROD
737 int8x8_t _h_cont = vld1_s8(hs + i);
738 int8x16_t _w = vld1q_s8(kptr);
739 _lstm_IFOGh0 = vdotq_lane_s32(_lstm_IFOGh0, _w, _h_cont, 0);
740#else
741 int16x4_t _h_cont01 = vreinterpret_s16_s8(vld1_s8(hs + i));
742 int8x8_t _h_cont0 = vreinterpret_s8_s16(vdup_lane_s16(_h_cont01, 0));
743 int8x8_t _h_cont1 = vreinterpret_s8_s16(vdup_lane_s16(_h_cont01, 1));
744 int8x16_t _w01 = vld1q_s8(kptr);
745
746 int16x8_t _lstm_IFOGh = vmull_s8(vget_low_s8(_w01), _h_cont0);
747 _lstm_IFOGh = vmlal_s8(_lstm_IFOGh, vget_high_s8(_w01), _h_cont1);
748 _lstm_IFOGh0 = vpadalq_s16(_lstm_IFOGh0, _lstm_IFOGh);
749#endif // __ARM_FEATURE_DOTPROD
750
751 kptr += 16;
752 }
753 for (; i + 1 < num_output; i += 2)
754 {
755 int8x8_t _h_cont = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(vld1_s8(hs + i)), 0));
756 int8x8_t _w = vld1_s8(kptr);
757
758 int16x8_t _lstm_IFOGh = vmull_s8(_w, _h_cont);
759 _lstm_IFOGh0 = vpadalq_s16(_lstm_IFOGh0, _lstm_IFOGh);
760
761 kptr += 8;
762 }
763 for (; i < num_output; i++)
764 {
765 int8x8_t _h_cont = vdup_n_s8(hs[i]);
766 int8x8_t _w = vld1_s8(kptr);
767
768 int16x8_t _lstm_IFOGh = vmull_s8(_w, _h_cont);
769 _lstm_IFOGh0 = vaddw_s16(_lstm_IFOGh0, vget_low_s16(_lstm_IFOGh));
770
771 kptr += 4;
772 }
773
774 float32x4_t _descale_x = vdupq_n_f32(descale_x);
775 float32x4_t _descale_h = vdupq_n_f32(descale_h);
776
777 float32x4_t _lstm_IFOG0 = vld1q_f32(bias_c_IFOG);
778
779 float32x4_t _descale_xc_IFOG = vld1q_f32(descales_ptr);
780
781 _lstm_IFOG0 = vmlaq_f32(_lstm_IFOG0, vcvtq_f32_s32(_lstm_IFOGx0), vmulq_f32(_descale_x, _descale_xc_IFOG));
782
783 float32x4_t _descale_hc_IFOG = vld1q_f32(descales_ptr + 4);
784
785 _lstm_IFOG0 = vmlaq_f32(_lstm_IFOG0, vcvtq_f32_s32(_lstm_IFOGh0), vmulq_f32(_descale_h, _descale_hc_IFOG));
786
787 vst1q_f32(gates_data, _lstm_IFOG0);
788#else
789 int Ix = 0;
790 int Fx = 0;
791 int Ox = 0;
792 int Gx = 0;
793 for (int i = 0; i < size; i++)
794 {
795 signed char xi = x[i];
796
797 Ix += kptr[0] * xi;
798 Fx += kptr[1] * xi;
799 Ox += kptr[2] * xi;
800 Gx += kptr[3] * xi;
801
802 kptr += 4;
803 }
804
805 int Ih = 0;
806 int Fh = 0;
807 int Oh = 0;
808 int Gh = 0;
809 for (int i = 0; i < num_output; i++)
810 {
811 signed char h_cont = hs[i];
812
813 Ih += kptr[0] * h_cont;
814 Fh += kptr[1] * h_cont;
815 Oh += kptr[2] * h_cont;
816 Gh += kptr[3] * h_cont;
817
818 kptr += 4;
819 }
820
821 const float descale_xc_I = descales_ptr[0];
822 const float descale_xc_F = descales_ptr[1];
823 const float descale_xc_O = descales_ptr[2];
824 const float descale_xc_G = descales_ptr[3];
825 const float descale_hc_I = descales_ptr[4];
826 const float descale_hc_F = descales_ptr[5];
827 const float descale_hc_O = descales_ptr[6];
828 const float descale_hc_G = descales_ptr[7];
829
830 float I = bias_c_IFOG[0] + Ix * (descale_x * descale_xc_I) + Ih * (descale_h * descale_hc_I);
831 float F = bias_c_IFOG[1] + Fx * (descale_x * descale_xc_F) + Fh * (descale_h * descale_hc_F);
832 float O = bias_c_IFOG[2] + Ox * (descale_x * descale_xc_O) + Oh * (descale_h * descale_hc_O);
833 float G = bias_c_IFOG[3] + Gx * (descale_x * descale_xc_G) + Gh * (descale_h * descale_hc_G);
834
835 gates_data[0] = I;
836 gates_data[1] = F;
837 gates_data[2] = O;
838 gates_data[3] = G;
839#endif // __ARM_NEON
840 }
841
842 lstm_int8_gate_output(gates, weight_hr, hidden_state, tmp_hidden_state, cell_state, top_blob, ti, elemtype, opt);
843 }
844}