Elffuss BraidHub

kikocis/ncnn public

master · 39.6 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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#include "rnn_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#endif // __ARM_NEON
20
21#include "arm_activation.h"
22#include "arm_usability.h"
23
24#include "cpu.h"
25
26namespace ncnn {
27
28#if NCNN_INT8
29#include "rnn_int8.h"
30#endif
31
32RNN_arm::RNN_arm()
33{
34#if __ARM_NEON
35#if NCNN_ARM82
36 support_fp16_storage = cpu_support_arm_asimdhp();
37#endif
38#endif // __ARM_NEON
39
40#if NCNN_BF16
41 support_bf16_storage = true;
42#endif
43}
44
45int RNN_arm::create_pipeline(const Option& opt)
46{
47#if NCNN_INT8
48 if (int8_scale_term)
49 {
50 return create_pipeline_int8(opt);
51 }
52#endif
53
54#if NCNN_ARM82
55 if (support_fp16_storage && opt.use_fp16_storage)
56 {
57 return create_pipeline_fp16s(opt);
58 }
59#endif
60
61#if NCNN_BF16
62 if (opt.use_bf16_storage)
63 {
64 return create_pipeline_bf16s(opt);
65 }
66#endif
67
68 const int num_directions = direction == 2 ? 2 : 1;
69 const int size = weight_data_size / num_directions / num_output;
70
71#if __ARM_NEON
72 weight_xc_data_packed.create(size * 4, num_output / 4 + num_output % 4, num_directions);
73 weight_hc_data_packed.create(num_output * 4, num_output / 4 + num_output % 4, num_directions);
74#else
75 weight_xc_data_packed.create(size, num_output, num_directions);
76 weight_hc_data_packed.create(num_output, num_output, num_directions);
77#endif
78
79 #pragma omp parallel for num_threads(opt.num_threads)
80 for (int dr = 0; dr < num_directions; dr++)
81 {
82 const Mat weight_xc = weight_xc_data.channel(dr);
83 const Mat weight_hc = weight_hc_data.channel(dr);
84
85 Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr);
86 Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr);
87
88 int q = 0;
89#if __ARM_NEON
90 for (; q + 3 < num_output; q += 4)
91 {
92 const float* weight_xc_0 = weight_xc.row(q);
93 const float* weight_xc_1 = weight_xc.row(q + 1);
94 const float* weight_xc_2 = weight_xc.row(q + 2);
95 const float* weight_xc_3 = weight_xc.row(q + 3);
96
97 const float* weight_hc_0 = weight_hc.row(q);
98 const float* weight_hc_1 = weight_hc.row(q + 1);
99 const float* weight_hc_2 = weight_hc.row(q + 2);
100 const float* weight_hc_3 = weight_hc.row(q + 3);
101
102 float* weight_xc = weight_xc_data_packed_dr.row(q / 4);
103 float* weight_hc = weight_hc_data_packed_dr.row(q / 4);
104
105 for (int i = 0; i < size; i++)
106 {
107 weight_xc[0] = weight_xc_0[i];
108 weight_xc[1] = weight_xc_1[i];
109 weight_xc[2] = weight_xc_2[i];
110 weight_xc[3] = weight_xc_3[i];
111
112 weight_xc += 4;
113 }
114
115 for (int i = 0; i < num_output; i++)
116 {
117 weight_hc[0] = weight_hc_0[i];
118 weight_hc[1] = weight_hc_1[i];
119 weight_hc[2] = weight_hc_2[i];
120 weight_hc[3] = weight_hc_3[i];
121
122 weight_hc += 4;
123 }
124 }
125#endif // __ARM_NEON
126 for (; q < num_output; q++)
127 {
128 const float* weight_xc_0 = weight_xc.row(q);
129 const float* weight_hc_0 = weight_hc.row(q);
130
131#if __ARM_NEON
132 float* weight_xc = weight_xc_data_packed_dr.row(q / 4 + q % 4);
133 float* weight_hc = weight_hc_data_packed_dr.row(q / 4 + q % 4);
134#else
135 float* weight_xc = weight_xc_data_packed_dr.row(q);
136 float* weight_hc = weight_hc_data_packed_dr.row(q);
137#endif // __ARM_NEON
138
139 for (int i = 0; i < size; i++)
140 {
141 weight_xc[i] = weight_xc_0[i];
142 }
143
144 for (int i = 0; i < num_output; i++)
145 {
146 weight_hc[i] = weight_hc_0[i];
147 }
148 }
149 }
150
151 bias_c_data_packed = bias_c_data;
152
153 if (opt.lightmode)
154 {
155 weight_xc_data.release();
156 bias_c_data.release();
157 weight_hc_data.release();
158 }
159
160 return 0;
161}
162
163static int rnn(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc, const Mat& bias_c, const Mat& weight_hc, Mat& hidden_state, const Option& opt)
164{
165 int size = bottom_blob.w;
166 int T = bottom_blob.h;
167
168 int num_output = top_blob.w;
169
170 // num_output
171 Mat gates(num_output, 4u, opt.workspace_allocator);
172 if (gates.empty())
173 return -100;
174
175 // unroll
176 for (int t = 0; t < T; t++)
177 {
178 int ti = reverse ? T - 1 - t : t;
179
180 const float* x = bottom_blob.row(ti);
181
182 int remain_num_output_start = 0;
183#if __ARM_NEON
184 int nn_num_output = num_output >> 2;
185 remain_num_output_start = nn_num_output << 2;
186
187 #pragma omp parallel for num_threads(opt.num_threads)
188 for (int qq = 0; qq < nn_num_output; qq++)
189 {
190 int q = qq * 4;
191
192 const float* weight_xc_ptr = weight_xc.row(q / 4);
193 const float* weight_hc_ptr = weight_hc.row(q / 4);
194
195 float32x4_t _rnn_H = vld1q_f32((const float*)bias_c + q);
196 float32x4_t _sum1 = vdupq_n_f32(0.f);
197 float32x4_t _sum2 = vdupq_n_f32(0.f);
198 float32x4_t _sum3 = vdupq_n_f32(0.f);
199
200 int i = 0;
201 for (; i + 3 < size; i += 4)
202 {
203 float32x4_t _x = vld1q_f32(x + i);
204 float32x4_t _weight_xc = vld1q_f32(weight_xc_ptr);
205 float32x4_t _weight_xc_1 = vld1q_f32(weight_xc_ptr + 4);
206 float32x4_t _weight_xc_2 = vld1q_f32(weight_xc_ptr + 8);
207 float32x4_t _weight_xc_3 = vld1q_f32(weight_xc_ptr + 12);
208#if __aarch64__
209 _rnn_H = vfmaq_laneq_f32(_rnn_H, _weight_xc, _x, 0);
210 _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_1, _x, 1);
211 _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_2, _x, 2);
212 _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_3, _x, 3);
213#else
214 _rnn_H = vmlaq_lane_f32(_rnn_H, _weight_xc, vget_low_f32(_x), 0);
215 _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_1, vget_low_f32(_x), 1);
216 _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_2, vget_high_f32(_x), 0);
217 _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_3, vget_high_f32(_x), 1);
218#endif
219
220 weight_xc_ptr += 16;
221 }
222 for (; i < size; i++)
223 {
224 float32x4_t _x = vdupq_n_f32(x[i]);
225 float32x4_t _weight_xc = vld1q_f32(weight_xc_ptr);
226 _rnn_H = vmlaq_f32(_rnn_H, _weight_xc, _x);
227
228 weight_xc_ptr += 4;
229 }
230
231 i = 0;
232 for (; i + 3 < num_output; i += 4)
233 {
234 float32x4_t _hidden_state = vld1q_f32((const float*)hidden_state + i);
235 float32x4_t _weight_hc = vld1q_f32(weight_hc_ptr);
236 float32x4_t _weight_hc_1 = vld1q_f32(weight_hc_ptr + 4);
237 float32x4_t _weight_hc_2 = vld1q_f32(weight_hc_ptr + 8);
238 float32x4_t _weight_hc_3 = vld1q_f32(weight_hc_ptr + 12);
239#if __aarch64__
240 _rnn_H = vfmaq_laneq_f32(_rnn_H, _weight_hc, _hidden_state, 0);
241 _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_1, _hidden_state, 1);
242 _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_2, _hidden_state, 2);
243 _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_3, _hidden_state, 3);
244#else
245 _rnn_H = vmlaq_lane_f32(_rnn_H, _weight_hc, vget_low_f32(_hidden_state), 0);
246 _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_1, vget_low_f32(_hidden_state), 1);
247 _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_2, vget_high_f32(_hidden_state), 0);
248 _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_3, vget_high_f32(_hidden_state), 1);
249#endif
250
251 weight_hc_ptr += 16;
252 }
253 for (; i < num_output; i++)
254 {
255 float32x4_t _hidden_state = vdupq_n_f32(hidden_state[i]);
256 float32x4_t _weight_hc = vld1q_f32(weight_hc_ptr);
257 _rnn_H = vmlaq_f32(_rnn_H, _weight_hc, _hidden_state);
258
259 weight_hc_ptr += 4;
260 }
261
262 _rnn_H = vaddq_f32(_rnn_H, _sum1);
263 _sum2 = vaddq_f32(_sum2, _sum3);
264 _rnn_H = vaddq_f32(_rnn_H, _sum2);
265
266 _rnn_H = tanh_ps(_rnn_H);
267
268 vst1q_f32((float*)gates + q, _rnn_H);
269 }
270#endif // __ARM_NEON
271 #pragma omp parallel for num_threads(opt.num_threads)
272 for (int q = remain_num_output_start; q < num_output; q++)
273 {
274#if __ARM_NEON
275 const float* weight_xc_ptr = weight_xc.row(q / 4 + q % 4);
276 const float* weight_hc_ptr = weight_hc.row(q / 4 + q % 4);
277#else
278 const float* weight_xc_ptr = weight_xc.row(q);
279 const float* weight_hc_ptr = weight_hc.row(q);
280#endif // __ARM_NEON
281
282 float H = bias_c[q];
283
284 for (int i = 0; i < size; i++)
285 {
286 H += weight_xc_ptr[i] * x[i];
287 }
288
289 for (int i = 0; i < num_output; i++)
290 {
291 H += weight_hc_ptr[i] * hidden_state[i];
292 }
293
294 H = tanhf(H);
295
296 gates[q] = H;
297 }
298
299 float* output_data = top_blob.row(ti);
300
301 float* hidden_ptr = hidden_state;
302
303#if __ARM_NEON
304 nn_num_output = num_output >> 2;
305 remain_num_output_start = nn_num_output << 2;
306
307 #pragma omp parallel for num_threads(opt.num_threads)
308 for (int qq = 0; qq < nn_num_output; qq++)
309 {
310 int q = qq * 4;
311
312 float32x4_t _rnn_H = vld1q_f32((float*)gates + q);
313
314 vst1q_f32(hidden_ptr + q, _rnn_H);
315 vst1q_f32(output_data + q, _rnn_H);
316 }
317#endif // __ARM_NEON
318 #pragma omp parallel for num_threads(opt.num_threads)
319 for (int q = remain_num_output_start; q < num_output; q++)
320 {
321 float H = gates[q];
322
323 hidden_ptr[q] = H;
324 output_data[q] = H;
325 }
326 }
327
328 return 0;
329}
330
331int RNN_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
332{
333#if NCNN_INT8
334 if (int8_scale_term)
335 {
336 return forward_int8(bottom_blob, top_blob, opt);
337 }
338#endif
339
340 int elembits = bottom_blob.elembits();
341
342#if NCNN_ARM82
343 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
344 return forward_fp16s(bottom_blob, top_blob, opt);
345#endif
346
347#if NCNN_BF16
348 if (opt.use_bf16_storage && elembits == 16)
349 return forward_bf16s(bottom_blob, top_blob, opt);
350#endif
351
352 int T = bottom_blob.h;
353
354 int num_directions = direction == 2 ? 2 : 1;
355
356 // initial hidden state
357 Mat hidden(num_output, 4u, opt.workspace_allocator);
358 if (hidden.empty())
359 return -100;
360 hidden.fill(0.f);
361
362 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
363 if (top_blob.empty())
364 return -100;
365
366 // Uni directional
367 if (direction == 0 || direction == 1)
368 {
369 int ret = rnn(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt);
370 if (ret != 0)
371 return ret;
372 }
373
374 if (direction == 2)
375 {
376 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
377 if (top_blob_forward.empty())
378 return -100;
379
380 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
381 if (top_blob_reverse.empty())
382 return -100;
383
384 {
385 int ret = rnn(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt);
386 if (ret != 0)
387 return ret;
388 }
389
390 hidden.fill(0.0f);
391
392 {
393 int ret = rnn(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), hidden, opt);
394 if (ret != 0)
395 return ret;
396 }
397
398 // concat w
399 for (int i = 0; i < T; i++)
400 {
401 const float* pf = top_blob_forward.row(i);
402 const float* pr = top_blob_reverse.row(i);
403 float* ptr = top_blob.row(i);
404
405 memcpy(ptr, pf, num_output * sizeof(float));
406 memcpy(ptr + num_output, pr, num_output * sizeof(float));
407 }
408 }
409
410 return 0;
411}
412
413int RNN_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
414{
415#if NCNN_INT8
416 if (int8_scale_term)
417 {
418 return forward_int8(bottom_blobs, top_blobs, opt);
419 }
420#endif
421
422 const Mat& bottom_blob = bottom_blobs[0];
423 int elembits = bottom_blob.elembits();
424
425#if NCNN_ARM82
426 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
427 return forward_fp16s(bottom_blobs, top_blobs, opt);
428#endif
429
430#if NCNN_BF16
431 if (opt.use_bf16_storage && elembits == 16)
432 return forward_bf16s(bottom_blobs, top_blobs, opt);
433#endif
434
435 int T = bottom_blob.h;
436 int num_directions = direction == 2 ? 2 : 1;
437
438 Mat hidden;
439 Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
440 if (bottom_blobs.size() == 2)
441 {
442 hidden = bottom_blobs[1].clone(hidden_allocator);
443 }
444 else
445 {
446 hidden.create(num_output, num_directions, 4u, hidden_allocator);
447 if (hidden.empty())
448 return -100;
449 hidden.fill(0.f);
450 }
451
452 Mat& top_blob = top_blobs[0];
453 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
454 if (top_blob.empty())
455 return -100;
456
457 // Uni directional
458 if (direction == 0 || direction == 1)
459 {
460 int ret = rnn(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt);
461 if (ret != 0)
462 return ret;
463 }
464
465 if (direction == 2)
466 {
467 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
468 if (top_blob_forward.empty())
469 return -100;
470
471 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
472 if (top_blob_reverse.empty())
473 return -100;
474
475 Mat hidden0 = hidden.row_range(0, 1);
476 {
477 int ret = rnn(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden0, opt);
478 if (ret != 0)
479 return ret;
480 }
481
482 Mat hidden1 = hidden.row_range(1, 1);
483 {
484 int ret = rnn(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), hidden1, opt);
485 if (ret != 0)
486 return ret;
487 }
488
489 // concat w
490 for (int i = 0; i < T; i++)
491 {
492 const float* pf = top_blob_forward.row(i);
493 const float* pr = top_blob_reverse.row(i);
494 float* ptr = top_blob.row(i);
495
496 memcpy(ptr, pf, num_output * sizeof(float));
497 memcpy(ptr + num_output, pr, num_output * sizeof(float));
498 }
499 }
500
501 if (top_blobs.size() == 2)
502 {
503 top_blobs[1] = hidden;
504 }
505
506 return 0;
507}
508
509#if NCNN_BF16
510static int rnn_bf16s(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc, const Mat& bias_c, const Mat& weight_hc, Mat& hidden_state, const Option& opt)
511{
512 int size = bottom_blob.w;
513 int T = bottom_blob.h;
514
515 int num_output = top_blob.w;
516
517 // num_output
518 Mat gates(num_output, 4u, opt.workspace_allocator);
519 if (gates.empty())
520 return -100;
521
522 // unroll
523 for (int t = 0; t < T; t++)
524 {
525 int ti = reverse ? T - 1 - t : t;
526
527 const unsigned short* x = bottom_blob.row<const unsigned short>(ti);
528
529 int remain_num_output_start = 0;
530#if __ARM_NEON
531 int nn_num_output = num_output >> 2;
532 remain_num_output_start = nn_num_output << 2;
533
534 #pragma omp parallel for num_threads(opt.num_threads)
535 for (int qq = 0; qq < nn_num_output; qq++)
536 {
537 int q = qq * 4;
538
539 const unsigned short* weight_xc_ptr = weight_xc.row<const unsigned short>(q / 4);
540 const unsigned short* weight_hc_ptr = weight_hc.row<const unsigned short>(q / 4);
541
542 float32x4_t _rnn_H = bfloat2float(vld1_u16((const unsigned short*)bias_c + q));
543 float32x4_t _sum1 = vdupq_n_f32(0.f);
544 float32x4_t _sum2 = vdupq_n_f32(0.f);
545 float32x4_t _sum3 = vdupq_n_f32(0.f);
546
547 int i = 0;
548 for (; i + 3 < size; i += 4)
549 {
550 float32x4_t _x = bfloat2float(vld1_u16(x + i));
551 float32x4_t _weight_xc = bfloat2float(vld1_u16(weight_xc_ptr));
552 float32x4_t _weight_xc_1 = bfloat2float(vld1_u16(weight_xc_ptr + 4));
553 float32x4_t _weight_xc_2 = bfloat2float(vld1_u16(weight_xc_ptr + 8));
554 float32x4_t _weight_xc_3 = bfloat2float(vld1_u16(weight_xc_ptr + 12));
555#if __aarch64__
556 _rnn_H = vfmaq_laneq_f32(_rnn_H, _weight_xc, _x, 0);
557 _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_1, _x, 1);
558 _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_2, _x, 2);
559 _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_3, _x, 3);
560#else
561 _rnn_H = vmlaq_lane_f32(_rnn_H, _weight_xc, vget_low_f32(_x), 0);
562 _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_1, vget_low_f32(_x), 1);
563 _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_2, vget_high_f32(_x), 0);
564 _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_3, vget_high_f32(_x), 1);
565#endif
566
567 weight_xc_ptr += 16;
568 }
569 for (; i < size; i++)
570 {
571 float32x4_t _x = bfloat2float(vdup_n_u16(x[i]));
572 float32x4_t _weight_xc = bfloat2float(vld1_u16(weight_xc_ptr));
573 _rnn_H = vmlaq_f32(_rnn_H, _weight_xc, _x);
574
575 weight_xc_ptr += 4;
576 }
577
578 i = 0;
579 for (; i + 3 < num_output; i += 4)
580 {
581 float32x4_t _hidden_state = vld1q_f32((const float*)hidden_state + i);
582 float32x4_t _weight_hc = bfloat2float(vld1_u16(weight_hc_ptr));
583 float32x4_t _weight_hc_1 = bfloat2float(vld1_u16(weight_hc_ptr + 4));
584 float32x4_t _weight_hc_2 = bfloat2float(vld1_u16(weight_hc_ptr + 8));
585 float32x4_t _weight_hc_3 = bfloat2float(vld1_u16(weight_hc_ptr + 12));
586#if __aarch64__
587 _rnn_H = vfmaq_laneq_f32(_rnn_H, _weight_hc, _hidden_state, 0);
588 _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_1, _hidden_state, 1);
589 _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_2, _hidden_state, 2);
590 _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_3, _hidden_state, 3);
591#else
592 _rnn_H = vmlaq_lane_f32(_rnn_H, _weight_hc, vget_low_f32(_hidden_state), 0);
593 _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_1, vget_low_f32(_hidden_state), 1);
594 _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_2, vget_high_f32(_hidden_state), 0);
595 _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_3, vget_high_f32(_hidden_state), 1);
596#endif
597
598 weight_hc_ptr += 16;
599 }
600 for (; i < num_output; i++)
601 {
602 float32x4_t _hidden_state = vdupq_n_f32(hidden_state[i]);
603 float32x4_t _weight_hc = bfloat2float(vld1_u16(weight_hc_ptr));
604 _rnn_H = vmlaq_f32(_rnn_H, _weight_hc, _hidden_state);
605
606 weight_hc_ptr += 4;
607 }
608
609 _rnn_H = vaddq_f32(_rnn_H, _sum1);
610 _sum2 = vaddq_f32(_sum2, _sum3);
611 _rnn_H = vaddq_f32(_rnn_H, _sum2);
612
613 _rnn_H = tanh_ps(_rnn_H);
614
615 vst1q_f32((float*)gates + q, _rnn_H);
616 }
617#endif // __ARM_NEON
618 #pragma omp parallel for num_threads(opt.num_threads)
619 for (int q = remain_num_output_start; q < num_output; q++)
620 {
621#if __ARM_NEON
622 const unsigned short* weight_xc_ptr = weight_xc.row<const unsigned short>(q / 4 + q % 4);
623 const unsigned short* weight_hc_ptr = weight_hc.row<const unsigned short>(q / 4 + q % 4);
624#else
625 const unsigned short* weight_xc_ptr = weight_xc.row<const unsigned short>(q);
626 const unsigned short* weight_hc_ptr = weight_hc.row<const unsigned short>(q);
627#endif // __ARM_NEON
628
629 float H = bfloat16_to_float32(((const unsigned short*)bias_c)[q]);
630
631 for (int i = 0; i < size; i++)
632 {
633 H += bfloat16_to_float32(weight_xc_ptr[i]) * bfloat16_to_float32(x[i]);
634 }
635
636 for (int i = 0; i < num_output; i++)
637 {
638 H += bfloat16_to_float32(weight_hc_ptr[i]) * hidden_state[i];
639 }
640
641 H = tanhf(H);
642
643 gates[q] = H;
644 }
645
646 unsigned short* output_data = top_blob.row<unsigned short>(ti);
647
648 float* hidden_ptr = hidden_state;
649
650#if __ARM_NEON
651 nn_num_output = num_output >> 2;
652 remain_num_output_start = nn_num_output << 2;
653
654 #pragma omp parallel for num_threads(opt.num_threads)
655 for (int qq = 0; qq < nn_num_output; qq++)
656 {
657 int q = qq * 4;
658
659 float32x4_t _rnn_H = vld1q_f32((float*)gates + q);
660
661 vst1q_f32(hidden_ptr + q, _rnn_H);
662 vst1_u16(output_data + q, float2bfloat(_rnn_H));
663 }
664#endif // __ARM_NEON
665 #pragma omp parallel for num_threads(opt.num_threads)
666 for (int q = remain_num_output_start; q < num_output; q++)
667 {
668 float H = gates[q];
669
670 hidden_ptr[q] = H;
671 output_data[q] = float32_to_bfloat16(H);
672 }
673 }
674
675 return 0;
676}
677
678int RNN_arm::create_pipeline_bf16s(const Option& opt)
679{
680 int num_directions = direction == 2 ? 2 : 1;
681 int size = weight_data_size / num_directions / num_output;
682
683#if __ARM_NEON
684 weight_xc_data_packed.create(size * 4, num_output / 4 + num_output % 4, num_directions, 2u, 1);
685 weight_hc_data_packed.create(num_output * 4, num_output / 4 + num_output % 4, num_directions, 2u, 1);
686
687 #pragma omp parallel for num_threads(opt.num_threads)
688 for (int dr = 0; dr < num_directions; dr++)
689 {
690 const Mat weight_xc = weight_xc_data.channel(dr);
691 const Mat weight_hc = weight_hc_data.channel(dr);
692
693 Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr);
694 Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr);
695
696 int q = 0;
697#if __ARM_NEON
698 for (; q + 3 < num_output; q += 4)
699 {
700 const float* weight_xc_0 = weight_xc.row(q);
701 const float* weight_xc_1 = weight_xc.row(q + 1);
702 const float* weight_xc_2 = weight_xc.row(q + 2);
703 const float* weight_xc_3 = weight_xc.row(q + 3);
704
705 const float* weight_hc_0 = weight_hc.row(q);
706 const float* weight_hc_1 = weight_hc.row(q + 1);
707 const float* weight_hc_2 = weight_hc.row(q + 2);
708 const float* weight_hc_3 = weight_hc.row(q + 3);
709
710 unsigned short* weight_xc = weight_xc_data_packed_dr.row<unsigned short>(q / 4);
711 unsigned short* weight_hc = weight_hc_data_packed_dr.row<unsigned short>(q / 4);
712
713 for (int i = 0; i < size; i++)
714 {
715 weight_xc[0] = float32_to_bfloat16(weight_xc_0[i]);
716 weight_xc[1] = float32_to_bfloat16(weight_xc_1[i]);
717 weight_xc[2] = float32_to_bfloat16(weight_xc_2[i]);
718 weight_xc[3] = float32_to_bfloat16(weight_xc_3[i]);
719
720 weight_xc += 4;
721 }
722
723 for (int i = 0; i < num_output; i++)
724 {
725 weight_hc[0] = float32_to_bfloat16(weight_hc_0[i]);
726 weight_hc[1] = float32_to_bfloat16(weight_hc_1[i]);
727 weight_hc[2] = float32_to_bfloat16(weight_hc_2[i]);
728 weight_hc[3] = float32_to_bfloat16(weight_hc_3[i]);
729
730 weight_hc += 4;
731 }
732 }
733#endif // __ARM_NEON
734 for (; q < num_output; q++)
735 {
736 const float* weight_xc_0 = weight_xc.row(q);
737 const float* weight_hc_0 = weight_hc.row(q);
738
739#if __ARM_NEON
740 unsigned short* weight_xc = weight_xc_data_packed_dr.row<unsigned short>(q / 4 + q % 4);
741 unsigned short* weight_hc = weight_hc_data_packed_dr.row<unsigned short>(q / 4 + q % 4);
742#else
743 unsigned short* weight_xc = weight_xc_data_packed_dr.row<unsigned short>(q);
744 unsigned short* weight_hc = weight_hc_data_packed_dr.row<unsigned short>(q);
745#endif // __ARM_NEON
746
747 for (int i = 0; i < size; i++)
748 {
749 weight_xc[i] = float32_to_bfloat16(weight_xc_0[i]);
750 }
751
752 for (int i = 0; i < num_output; i++)
753 {
754 weight_hc[i] = float32_to_bfloat16(weight_hc_0[i]);
755 }
756 }
757 }
758#else
759 cast_float32_to_bfloat16(weight_xc_data, weight_xc_data_packed, opt);
760 cast_float32_to_bfloat16(weight_hc_data, weight_hc_data_packed, opt);
761#endif
762
763 cast_float32_to_bfloat16(bias_c_data, bias_c_data_packed, opt);
764
765 if (opt.lightmode)
766 {
767 weight_xc_data.release();
768 bias_c_data.release();
769 weight_hc_data.release();
770 }
771
772 return 0;
773}
774
775int RNN_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
776{
777 int T = bottom_blob.h;
778
779 int num_directions = direction == 2 ? 2 : 1;
780
781 // initial hidden state
782 Mat hidden(num_output, 4u, opt.workspace_allocator);
783 if (hidden.empty())
784 return -100;
785 hidden.fill(0.f);
786
787 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
788 if (top_blob.empty())
789 return -100;
790
791 // Uni directional
792 if (direction == 0 || direction == 1)
793 {
794 int ret = rnn_bf16s(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt);
795 if (ret != 0)
796 return ret;
797 }
798
799 if (direction == 2)
800 {
801 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
802 if (top_blob_forward.empty())
803 return -100;
804
805 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
806 if (top_blob_reverse.empty())
807 return -100;
808
809 {
810 int ret = rnn_bf16s(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt);
811 if (ret != 0)
812 return ret;
813 }
814
815 hidden.fill(0.f);
816
817 {
818 int ret = rnn_bf16s(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), hidden, opt);
819 if (ret != 0)
820 return ret;
821 }
822
823 // concat w
824 for (int i = 0; i < T; i++)
825 {
826 const unsigned short* pf = top_blob_forward.row<const unsigned short>(i);
827 const unsigned short* pr = top_blob_reverse.row<const unsigned short>(i);
828 unsigned short* ptr = top_blob.row<unsigned short>(i);
829
830 memcpy(ptr, pf, num_output * sizeof(unsigned short));
831 memcpy(ptr + num_output, pr, num_output * sizeof(unsigned short));
832 }
833 }
834
835 return 0;
836}
837
838int RNN_arm::forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
839{
840 const Mat& bottom_blob = bottom_blobs[0];
841 int T = bottom_blob.h;
842 int num_directions = direction == 2 ? 2 : 1;
843
844 Mat hidden;
845 Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
846 if (bottom_blobs.size() == 2)
847 {
848 Option opt_cast = opt;
849 opt_cast.blob_allocator = hidden_allocator;
850 cast_bfloat16_to_float32(bottom_blobs[1], hidden, opt_cast);
851 }
852 else
853 {
854 hidden.create(num_output, num_directions, 4u, hidden_allocator);
855 if (hidden.empty())
856 return -100;
857 hidden.fill(0.f);
858 }
859
860 Mat& top_blob = top_blobs[0];
861 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
862 if (top_blob.empty())
863 return -100;
864
865 // Uni directional
866 if (direction == 0 || direction == 1)
867 {
868 int ret = rnn_bf16s(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt);
869 if (ret != 0)
870 return ret;
871 }
872
873 if (direction == 2)
874 {
875 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
876 if (top_blob_forward.empty())
877 return -100;
878
879 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
880 if (top_blob_reverse.empty())
881 return -100;
882
883 Mat hidden0 = hidden.row_range(0, 1);
884 {
885 int ret = rnn_bf16s(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden0, opt);
886 if (ret != 0)
887 return ret;
888 }
889
890 Mat hidden1 = hidden.row_range(1, 1);
891 {
892 int ret = rnn_bf16s(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), hidden1, opt);
893 if (ret != 0)
894 return ret;
895 }
896
897 // concat w
898 for (int i = 0; i < T; i++)
899 {
900 const unsigned short* pf = top_blob_forward.row<const unsigned short>(i);
901 const unsigned short* pr = top_blob_reverse.row<const unsigned short>(i);
902 unsigned short* ptr = top_blob.row<unsigned short>(i);
903
904 memcpy(ptr, pf, num_output * sizeof(unsigned short));
905 memcpy(ptr + num_output, pr, num_output * sizeof(unsigned short));
906 }
907 }
908
909 if (top_blobs.size() == 2)
910 {
911 cast_float32_to_bfloat16(hidden, top_blobs[1], opt);
912 }
913
914 return 0;
915}
916#endif // NCNN_BF16
917
918#if NCNN_INT8
919int RNN_arm::create_pipeline_int8(const Option& opt)
920{
921 const int num_directions = direction == 2 ? 2 : 1;
922 const int size = weight_data_size / num_directions / num_output;
923
924 rnn_transform_weight_int8(weight_xc_data, weight_xc_data_int8_scales, weight_hc_data, weight_hc_data_int8_scales, bias_c_data, weight_data_tm, weight_data_tm_int8_descales, bias_c_data_packed, size, num_output, num_directions, opt);
925
926 if (opt.lightmode)
927 {
928 weight_xc_data.release();
929 weight_hc_data.release();
930 bias_c_data.release();
931 weight_xc_data_int8_scales.release();
932 weight_hc_data_int8_scales.release();
933 }
934
935 return 0;
936}
937
938void RNN_arm::dynamic_quantize(const Mat& bottom_blob, int elemtype, Mat& bottom_blob_int8, Mat& bottom_blob_int8_descales, const Option& opt) const
939{
940 int size = bottom_blob.w;
941 int T = bottom_blob.h;
942
943 // dynamic quantize bottom_blob
944 bottom_blob_int8_descales.create(T, (size_t)4u, 1, opt.blob_allocator);
945
946 Mat bottom_blob_int8_scales(T, (size_t)4u, 1, opt.blob_allocator);
947
948 if (elemtype == 1)
949 {
950 // fp32
951 for (int t = 0; t < T; t++)
952 {
953 const float* x = bottom_blob.row(t);
954
955 float absmax = 0.f;
956 for (int i = 0; i < size; i++)
957 {
958 absmax = std::max(absmax, (float)fabs(x[i]));
959 }
960
961 bottom_blob_int8_scales[t] = 127.f / absmax;
962 bottom_blob_int8_descales[t] = absmax / 127.f;
963 }
964 }
965 if (elemtype == 2)
966 {
967 // fp16
968 for (int t = 0; t < T; t++)
969 {
970 const unsigned short* x = bottom_blob.row<const unsigned short>(t);
971
972 float absmax = 0.f;
973 for (int i = 0; i < size; i++)
974 {
975 absmax = std::max(absmax, (float)fabs(float16_to_float32(x[i])));
976 }
977
978 bottom_blob_int8_scales[t] = 127.f / absmax;
979 bottom_blob_int8_descales[t] = absmax / 127.f;
980 }
981 }
982 if (elemtype == 4)
983 {
984 // bf16
985 for (int t = 0; t < T; t++)
986 {
987 const unsigned short* x = bottom_blob.row<const unsigned short>(t);
988
989 float absmax = 0.f;
990 for (int i = 0; i < size; i++)
991 {
992 absmax = std::max(absmax, (float)fabs(bfloat16_to_float32(x[i])));
993 }
994
995 bottom_blob_int8_scales[t] = 127.f / absmax;
996 bottom_blob_int8_descales[t] = absmax / 127.f;
997 }
998 }
999
1000 quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt);
1001}
1002
1003int RNN_arm::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
1004{
1005 int elemtype = 1; // fp32
1006 {
1007 int elembits = bottom_blob.elembits();
1008
1009 // clang-format off
1010 // *INDENT-OFF*
1011
1012#if NCNN_ARM82
1013 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
1014 {
1015 elemtype = 2; // fp16
1016 }
1017 else
1018#endif
1019#if NCNN_BF16
1020 if (opt.use_bf16_storage && elembits == 16)
1021 {
1022 elemtype = 4; // bf16
1023 }
1024 else
1025#endif
1026 {
1027 // fp32
1028 }
1029
1030 // *INDENT-ON*
1031 // clang-format on
1032 }
1033
1034 int T = bottom_blob.h;
1035 size_t elemsize = bottom_blob.elemsize;
1036
1037 int num_directions = direction == 2 ? 2 : 1;
1038
1039 // initial hidden state
1040 Mat hidden(num_output, 4u, opt.workspace_allocator);
1041 if (hidden.empty())
1042 return -100;
1043 hidden.fill(0.f);
1044
1045 top_blob.create(num_output * num_directions, T, elemsize, opt.blob_allocator);
1046 if (top_blob.empty())
1047 return -100;
1048
1049 // dynamic quantize bottom_blob
1050 Mat bottom_blob_int8;
1051 Mat bottom_blob_int8_descales;
1052 {
1053 Option opt_quant = opt;
1054 opt_quant.blob_allocator = opt.workspace_allocator;
1055 opt_quant.use_packing_layout = false;
1056 dynamic_quantize(bottom_blob, elemtype, bottom_blob_int8, bottom_blob_int8_descales, opt_quant);
1057 }
1058
1059 // Uni directional
1060 if (direction == 0 || direction == 1)
1061 {
1062 rnn_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob, elemtype, direction, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden, opt);
1063 }
1064
1065 if (direction == 2)
1066 {
1067 Mat top_blob_forward(num_output, T, elemsize, opt.workspace_allocator);
1068 if (top_blob_forward.empty())
1069 return -100;
1070
1071 Mat top_blob_reverse(num_output, T, elemsize, opt.workspace_allocator);
1072 if (top_blob_reverse.empty())
1073 return -100;
1074
1075 {
1076 rnn_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_forward, elemtype, 0, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden, opt);
1077 }
1078
1079 hidden.fill(0.0f);
1080
1081 {
1082 rnn_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_reverse, elemtype, 1, weight_data_tm.channel(1), weight_data_tm_int8_descales.channel(1), bias_c_data_packed.channel(1), hidden, opt);
1083 }
1084
1085 // concat w
1086 for (int i = 0; i < T; i++)
1087 {
1088 const unsigned char* pf = top_blob_forward.row<const unsigned char>(i);
1089 const unsigned char* pr = top_blob_reverse.row<const unsigned char>(i);
1090 unsigned char* ptr = top_blob.row<unsigned char>(i);
1091
1092 memcpy(ptr, pf, num_output * elemsize);
1093 memcpy(ptr + num_output * elemsize, pr, num_output * elemsize);
1094 }
1095 }
1096
1097 return 0;
1098}
1099
1100int RNN_arm::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
1101{
1102 const Mat& bottom_blob = bottom_blobs[0];
1103
1104 int elemtype = 1; // fp32
1105 {
1106 int elembits = bottom_blob.elembits();
1107
1108 // clang-format off
1109 // *INDENT-OFF*
1110
1111#if NCNN_ARM82
1112 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
1113 {
1114 elemtype = 2; // fp16
1115 }
1116 else
1117#endif
1118#if NCNN_BF16
1119 if (opt.use_bf16_storage && elembits == 16)
1120 {
1121 elemtype = 4; // bf16
1122 }
1123 else
1124#endif
1125 {
1126 // fp32
1127 }
1128
1129 // *INDENT-ON*
1130 // clang-format on
1131 }
1132
1133 int T = bottom_blob.h;
1134 size_t elemsize = bottom_blob.elemsize;
1135 int num_directions = direction == 2 ? 2 : 1;
1136
1137 Mat hidden;
1138 Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
1139 if (bottom_blobs.size() == 2)
1140 {
1141 if (elemtype == 1)
1142 {
1143 hidden = bottom_blobs[1].clone(hidden_allocator);
1144 }
1145 if (elemtype == 2)
1146 {
1147 Option opt_cast = opt;
1148 opt_cast.blob_allocator = hidden_allocator;
1149 cast_float16_to_float32(bottom_blobs[1], hidden, opt_cast);
1150 }
1151 if (elemtype == 4)
1152 {
1153 Option opt_cast = opt;
1154 opt_cast.blob_allocator = hidden_allocator;
1155 cast_bfloat16_to_float32(bottom_blobs[1], hidden, opt_cast);
1156 }
1157 }
1158 else
1159 {
1160 hidden.create(num_output, num_directions, 4u, hidden_allocator);
1161 if (hidden.empty())
1162 return -100;
1163 hidden.fill(0.f);
1164 }
1165
1166 Mat& top_blob = top_blobs[0];
1167 top_blob.create(num_output * num_directions, T, elemsize, opt.blob_allocator);
1168 if (top_blob.empty())
1169 return -100;
1170
1171 // dynamic quantize bottom_blob
1172 Mat bottom_blob_int8;
1173 Mat bottom_blob_int8_descales;
1174 {
1175 Option opt_quant = opt;
1176 opt_quant.blob_allocator = opt.workspace_allocator;
1177 opt_quant.use_packing_layout = false;
1178 dynamic_quantize(bottom_blob, elemtype, bottom_blob_int8, bottom_blob_int8_descales, opt_quant);
1179 }
1180
1181 // Uni directional
1182 if (direction == 0 || direction == 1)
1183 {
1184 rnn_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob, elemtype, direction, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden, opt);
1185 }
1186
1187 if (direction == 2)
1188 {
1189 Mat top_blob_forward(num_output, T, elemsize, opt.workspace_allocator);
1190 if (top_blob_forward.empty())
1191 return -100;
1192
1193 Mat top_blob_reverse(num_output, T, elemsize, opt.workspace_allocator);
1194 if (top_blob_reverse.empty())
1195 return -100;
1196
1197 Mat hidden0 = hidden.row_range(0, 1);
1198 {
1199 rnn_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_forward, elemtype, 0, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden0, opt);
1200 }
1201
1202 Mat hidden1 = hidden.row_range(1, 1);
1203 {
1204 rnn_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_reverse, elemtype, 1, weight_data_tm.channel(1), weight_data_tm_int8_descales.channel(1), bias_c_data_packed.channel(1), hidden1, opt);
1205 }
1206
1207 // concat w
1208 for (int i = 0; i < T; i++)
1209 {
1210 const unsigned char* pf = top_blob_forward.row<const unsigned char>(i);
1211 const unsigned char* pr = top_blob_reverse.row<const unsigned char>(i);
1212 unsigned char* ptr = top_blob.row<unsigned char>(i);
1213
1214 memcpy(ptr, pf, num_output * elemsize);
1215 memcpy(ptr + num_output * elemsize, pr, num_output * elemsize);
1216 }
1217 }
1218
1219 if (top_blobs.size() == 2)
1220 {
1221 if (elemtype == 1)
1222 {
1223 top_blobs[1] = hidden;
1224 }
1225 if (elemtype == 2)
1226 {
1227 cast_float32_to_float16(hidden, top_blobs[1], opt);
1228 }
1229 if (elemtype == 4)
1230 {
1231 cast_float32_to_bfloat16(hidden, top_blobs[1], opt);
1232 }
1233 }
1234
1235 return 0;
1236}
1237#endif // NCNN_INT8
1238
1239} // namespace ncnn