Elffuss BraidHub

kikocis/ncnn public

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