Elffuss BraidHub

kikocis/ncnn public

master · 34.3 KiBRawPermalink
1// Xavier Hsinyuan is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 Xavier Hsinyuan <me@lstlx.com>. 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 "gru_riscv.h"
16
17#if __riscv_vector
18#include <riscv_vector.h>
19#endif // __riscv_vector
20
21namespace ncnn {
22
23//core rvv-optimized gru impl.
24#if __riscv_vector
25static int gru(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)
26{
27 int size = bottom_blob.w;
28 int T = bottom_blob.h;
29
30 int num_output = top_blob.w;
31
32 // 2 x num_output
33 Mat gates(2, num_output, 4u, opt.workspace_allocator);
34 if (gates.empty())
35 return -100;
36
37 // unroll
38 for (int t = 0; t < T; t++)
39 {
40 int ti = reverse ? T - 1 - t : t;
41
42 const float* x = bottom_blob.row(ti);
43 #pragma omp parallel for num_threads(opt.num_threads)
44 for (int q = 0; q < num_output; q++)
45 {
46 float* gates_data = gates.row(q);
47
48 // gate reset update
49 const float* bias_c_R = bias_c.row(0);
50 const float* bias_c_U = bias_c.row(1);
51
52 const float* weight_xc_R = weight_xc.row(num_output * 0 + q);
53 const float* weight_xc_U = weight_xc.row(num_output * 1 + q);
54 const float* weight_hc_R = weight_hc.row(num_output * 0 + q);
55 const float* weight_hc_U = weight_hc.row(num_output * 1 + q);
56
57 float R = bias_c_R[q];
58 float U = bias_c_U[q];
59
60 int n = size;
61 const float* ptr_x = x;
62 const float* ptr_xcr = weight_xc_R;
63 const float* ptr_xcu = weight_xc_U;
64 while (n > 0)
65 {
66 size_t vl = vsetvl_e32m8(n);
67 vfloat32m8_t _x = vle32_v_f32m8(ptr_x, vl);
68 vfloat32m8_t _xcr = vle32_v_f32m8(ptr_xcr, vl);
69 vfloat32m8_t _xcu = vle32_v_f32m8(ptr_xcu, vl);
70 vfloat32m1_t _scalar_r = vfmv_s_f_f32m1(vundefined_f32m1(), R, vl);
71 vfloat32m1_t _scalar_u = vfmv_s_f_f32m1(vundefined_f32m1(), U, vl);
72
73 _xcr = vfmul_vv_f32m8(_xcr, _x, vl);
74 _xcu = vfmul_vv_f32m8(_xcu, _x, vl);
75 _scalar_r = vfredusum_vs_f32m8_f32m1(_scalar_r, _xcr, _scalar_r, vl);
76 _scalar_u = vfredusum_vs_f32m8_f32m1(_scalar_u, _xcu, _scalar_u, vl);
77
78 R = vfmv_f_s_f32m1_f32(_scalar_r);
79 U = vfmv_f_s_f32m1_f32(_scalar_u);
80
81 ptr_x += vl;
82 ptr_xcr += vl;
83 ptr_xcu += vl;
84 n -= vl;
85 }
86 ptr_x = NULL;
87 ptr_xcr = NULL;
88 ptr_xcu = NULL;
89
90 int n_out = num_output;
91 const float* ptr_hc = hidden_state;
92 const float* ptr_hcr = weight_hc_R;
93 const float* ptr_hcu = weight_hc_U;
94 while (n_out > 0)
95 {
96 size_t vl = vsetvl_e32m8(n_out);
97 vfloat32m8_t _h_cont = vle32_v_f32m8(ptr_hc, vl);
98 vfloat32m8_t _hcr = vle32_v_f32m8(ptr_hcr, vl);
99 vfloat32m8_t _hcu = vle32_v_f32m8(ptr_hcu, vl);
100 vfloat32m1_t _scalar_r = vfmv_s_f_f32m1(vundefined_f32m1(), R, vl);
101 vfloat32m1_t _scalar_u = vfmv_s_f_f32m1(vundefined_f32m1(), U, vl);
102
103 _hcr = vfmul_vv_f32m8(_hcr, _h_cont, vl);
104 _hcu = vfmul_vv_f32m8(_hcu, _h_cont, vl);
105 _scalar_r = vfredusum_vs_f32m8_f32m1(_scalar_r, _hcr, _scalar_r, vl);
106 _scalar_u = vfredusum_vs_f32m8_f32m1(_scalar_u, _hcu, _scalar_u, vl);
107
108 R = vfmv_f_s_f32m1_f32(_scalar_r);
109 U = vfmv_f_s_f32m1_f32(_scalar_u);
110
111 ptr_hc += vl;
112 ptr_hcr += vl;
113 ptr_hcu += vl;
114 n_out -= vl;
115 }
116 ptr_hc = NULL;
117 ptr_hcr = NULL;
118 ptr_hcu = NULL;
119
120 // sigmoid(R)
121 // sigmoid(U)
122 R = 1.f / (1.f + exp(-R));
123 U = 1.f / (1.f + exp(-U));
124
125 // gate new
126 const float* bias_c_WN = bias_c.row(2);
127 const float* bias_c_BN = bias_c.row(3);
128
129 const float* weight_xc_N = weight_xc.row(num_output * 2 + q);
130 const float* weight_hc_N = weight_hc.row(num_output * 2 + q);
131
132 float N = bias_c_BN[q];
133
134 int n_out2 = num_output;
135 const float* ptr_hc2 = hidden_state;
136 const float* ptr_whc_n = weight_hc_N;
137 while (n_out2 > 0)
138 {
139 size_t vl = vsetvl_e32m8(n_out2);
140
141 vfloat32m8_t _h_cont = vle32_v_f32m8(ptr_hc2, vl);
142 vfloat32m8_t _whc_n = vle32_v_f32m8(ptr_whc_n, vl);
143 vfloat32m1_t _scalar_n = vfmv_s_f_f32m1(vundefined_f32m1(), N, vl);
144
145 _h_cont = vfmul_vv_f32m8(_whc_n, _h_cont, vl);
146 _scalar_n = vfredusum_vs_f32m8_f32m1(_scalar_n, _h_cont, _scalar_n, vl);
147
148 N = vfmv_f_s_f32m1_f32(_scalar_n);
149 n_out2 -= vl;
150 ptr_hc2 += vl;
151 ptr_whc_n += vl;
152 }
153 ptr_hc2 = NULL;
154 ptr_whc_n = NULL;
155
156 N = bias_c_WN[q] + R * N;
157
158 int n2 = size;
159 const float* ptr_x2 = x;
160 const float* ptr_xcn = weight_xc_N;
161 while (n2 > 0)
162 {
163 size_t vl = vsetvl_e32m8(n2);
164
165 vfloat32m8_t _x = vle32_v_f32m8(ptr_x2, vl);
166 vfloat32m8_t _xcn = vle32_v_f32m8(ptr_xcn, vl);
167 vfloat32m1_t _scalar_n = vfmv_s_f_f32m1(vundefined_f32m1(), N, vl);
168
169 _xcn = vfmul_vv_f32m8(_x, _xcn, vl);
170 _scalar_n = vfredusum_vs_f32m8_f32m1(_scalar_n, _xcn, _scalar_n, vl);
171 N = vfmv_f_s_f32m1_f32(_scalar_n);
172
173 n2 -= vl;
174 ptr_x2 += vl;
175 ptr_xcn += vl;
176 }
177 ptr_x2 = NULL;
178 ptr_xcn = NULL;
179
180 // tanh(N)
181 N = tanh(N);
182
183 gates_data[0] = U;
184 gates_data[1] = N;
185 }
186
187 // h_t := (1 - update) .* new + update .* h_{t-1}
188 float* output_data = top_blob.row(ti);
189 #pragma omp parallel for num_threads(opt.num_threads)
190 for (int q = 0; q < num_output; q++)
191 {
192 const float* gates_data = gates.row(q);
193
194 float U = gates_data[0];
195 float N = gates_data[1];
196
197 float H = (1 - U) * N + U * hidden_state[q];
198
199 hidden_state[q] = H;
200 output_data[q] = H;
201 }
202 }
203
204 return 0;
205}
206
207#endif
208
209GRU_riscv::GRU_riscv()
210{
211#if __riscv_vector && __riscv_zfh
212 support_fp16_storage = true;
213#endif
214}
215
216int GRU_riscv::create_pipeline(const Option& opt)
217{
218#if NCNN_INT8
219 if (int8_scale_term)
220 {
221 support_fp16_storage = false;
222 return 0;
223 }
224#endif
225
226#if __riscv_vector && __riscv_zfh
227 if (opt.use_fp16_storage && opt.use_fp16_arithmetic)
228 return create_pipeline_fp16sa(opt);
229#endif
230
231 return GRU::create_pipeline(opt);
232}
233
234int GRU_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
235{
236#if NCNN_INT8
237 if (int8_scale_term)
238 {
239 return GRU::forward(bottom_blob, top_blob, opt);
240 }
241#endif
242
243 int elembits = bottom_blob.elembits();
244#if __riscv_vector
245
246#if __riscv_zfh
247 if (opt.use_fp16_storage && elembits == 16)
248 {
249 if (opt.use_fp16_arithmetic)
250 return forward_fp16sa(bottom_blob, top_blob, opt);
251 else
252 return forward_fp16s(bottom_blob, top_blob, opt);
253 }
254#endif
255
256 int T = bottom_blob.h;
257
258 int num_directions = direction == 2 ? 2 : 1;
259
260 // initial hidden state
261 Mat hidden(num_output, 4u, opt.workspace_allocator);
262 if (hidden.empty())
263 return -100;
264 hidden.fill(0.f);
265
266 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
267 if (top_blob.empty())
268 return -100;
269
270 // Uni directional
271 if (direction == 0 || direction == 1)
272 {
273 int ret = gru(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
274 if (ret != 0)
275 return ret;
276 }
277
278 if (direction == 2)
279 {
280 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
281 if (top_blob_forward.empty())
282 return -100;
283
284 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
285 if (top_blob_reverse.empty())
286 return -100;
287
288 int ret0 = gru(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
289 if (ret0 != 0)
290 return ret0;
291
292 hidden.fill(0.0f);
293
294 int ret1 = gru(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), hidden, opt);
295 if (ret1 != 0)
296 return ret1;
297
298 // concat w
299 for (int i = 0; i < T; i++)
300 {
301 const float* pf = top_blob_forward.row(i);
302 const float* pr = top_blob_reverse.row(i);
303 float* ptr = top_blob.row(i);
304
305 memcpy(ptr, pf, num_output * sizeof(float));
306 memcpy(ptr + num_output, pr, num_output * sizeof(float));
307 }
308 }
309
310 return 0;
311#endif
312 return GRU::forward(bottom_blob, top_blob, opt);
313}
314
315int GRU_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
316{
317#if NCNN_INT8
318 if (int8_scale_term)
319 {
320 return GRU::forward(bottom_blobs, top_blobs, opt);
321 }
322#endif
323
324 const Mat& bottom_blob = bottom_blobs[0];
325 int elembits = bottom_blob.elembits();
326
327#if __riscv_vector
328#if __riscv_zfh
329 if (opt.use_fp16_storage && elembits == 16)
330 {
331 if (opt.use_fp16_arithmetic)
332 return forward_fp16sa(bottom_blobs, top_blobs, opt);
333 else
334 return forward_fp16s(bottom_blobs, top_blobs, opt);
335 }
336#endif
337
338 int T = bottom_blob.h;
339 int num_directions = direction == 2 ? 2 : 1;
340
341 Mat hidden;
342 Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
343 if (bottom_blobs.size() == 2)
344 {
345 hidden = bottom_blobs[1].clone(hidden_allocator);
346 }
347 else
348 {
349 hidden.create(num_output, num_directions, 4u, hidden_allocator);
350 if (hidden.empty())
351 return -100;
352 hidden.fill(0.f);
353 }
354
355 Mat& top_blob = top_blobs[0];
356 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
357 if (top_blob.empty())
358 return -100;
359
360 // Uni directional
361 if (direction == 0 || direction == 1)
362 {
363 Mat hidden0 = hidden.row_range(0, 1);
364 int ret = gru(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden0, opt);
365 if (ret != 0)
366 return ret;
367 }
368
369 if (direction == 2)
370 {
371 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
372 if (top_blob_forward.empty())
373 return -100;
374
375 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
376 if (top_blob_reverse.empty())
377 return -100;
378
379 Mat hidden0 = hidden.row_range(0, 1);
380 int ret0 = gru(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden0, opt);
381 if (ret0 != 0)
382 return ret0;
383
384 Mat hidden1 = hidden.row_range(1, 1);
385 int ret1 = gru(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), hidden1, opt);
386 if (ret1 != 0)
387 return ret1;
388
389 // concat w
390 for (int i = 0; i < T; i++)
391 {
392 const float* pf = top_blob_forward.row(i);
393 const float* pr = top_blob_reverse.row(i);
394 float* ptr = top_blob.row(i);
395
396 memcpy(ptr, pf, num_output * sizeof(float));
397 memcpy(ptr + num_output, pr, num_output * sizeof(float));
398 }
399 }
400
401 if (top_blobs.size() == 2)
402 {
403 top_blobs[1] = hidden;
404 }
405
406 return 0;
407#endif
408 return GRU::forward(bottom_blobs, top_blobs, opt);
409}
410
411#if __riscv_vector && __riscv_zfh
412static int gru_fp16s(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)
413{
414 int size = bottom_blob.w;
415 int T = bottom_blob.h;
416
417 int num_output = top_blob.w;
418
419 // 2 x num_output
420 Mat gates(2, num_output, 4u, opt.workspace_allocator);
421 if (gates.empty())
422 return -100;
423
424 // unroll
425 for (int t = 0; t < T; t++)
426 {
427 int ti = reverse ? T - 1 - t : t;
428
429 const __fp16* x = bottom_blob.row<const __fp16>(ti);
430 #pragma omp parallel for num_threads(opt.num_threads)
431 for (int q = 0; q < num_output; q++)
432 {
433 float* gates_data = gates.row(q);
434
435 // gate reset update
436 const float* bias_c_R = bias_c.row(0);
437 const float* bias_c_U = bias_c.row(1);
438
439 const float* weight_xc_R = weight_xc.row(num_output * 0 + q);
440 const float* weight_xc_U = weight_xc.row(num_output * 1 + q);
441 const float* weight_hc_R = weight_hc.row(num_output * 0 + q);
442 const float* weight_hc_U = weight_hc.row(num_output * 1 + q);
443
444 float R = bias_c_R[q];
445 float U = bias_c_U[q];
446
447 int n = size;
448 const __fp16* ptr_x = x;
449 const float* ptr_xcr = weight_xc_R;
450 const float* ptr_xcu = weight_xc_U;
451 while (n > 0)
452 {
453 size_t vl = vsetvl_e16m4(n);
454 vfloat32m8_t _x = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr_x, vl), vl);
455 vfloat32m8_t _xcr = vle32_v_f32m8(ptr_xcr, vl);
456 vfloat32m8_t _xcu = vle32_v_f32m8(ptr_xcu, vl);
457 vfloat32m1_t _scalar_r = vfmv_s_f_f32m1(vundefined_f32m1(), R, vl);
458 vfloat32m1_t _scalar_u = vfmv_s_f_f32m1(vundefined_f32m1(), U, vl);
459
460 _xcr = vfmul_vv_f32m8(_xcr, _x, vl);
461 _xcu = vfmul_vv_f32m8(_xcu, _x, vl);
462 _scalar_r = vfredusum_vs_f32m8_f32m1(_scalar_r, _xcr, _scalar_r, vl);
463 _scalar_u = vfredusum_vs_f32m8_f32m1(_scalar_u, _xcu, _scalar_u, vl);
464
465 R = vfmv_f_s_f32m1_f32(_scalar_r);
466 U = vfmv_f_s_f32m1_f32(_scalar_u);
467
468 ptr_x += vl;
469 ptr_xcr += vl;
470 ptr_xcu += vl;
471 n -= vl;
472 }
473 ptr_x = NULL;
474 ptr_xcr = NULL;
475 ptr_xcu = NULL;
476
477 int n_out = num_output;
478 const float* ptr_hc = hidden_state;
479 const float* ptr_hcr = weight_hc_R;
480 const float* ptr_hcu = weight_hc_U;
481 while (n_out > 0)
482 {
483 size_t vl = vsetvl_e16m4(n_out);
484 vfloat32m8_t _h_cont = vle32_v_f32m8(ptr_hc, vl);
485 vfloat32m8_t _hcr = vle32_v_f32m8(ptr_hcr, vl);
486 vfloat32m8_t _hcu = vle32_v_f32m8(ptr_hcu, vl);
487 vfloat32m1_t _scalar_r = vfmv_s_f_f32m1(vundefined_f32m1(), R, vl);
488 vfloat32m1_t _scalar_u = vfmv_s_f_f32m1(vundefined_f32m1(), U, vl);
489
490 _hcr = vfmul_vv_f32m8(_hcr, _h_cont, vl);
491 _hcu = vfmul_vv_f32m8(_hcu, _h_cont, vl);
492 _scalar_r = vfredusum_vs_f32m8_f32m1(_scalar_r, _hcr, _scalar_r, vl);
493 _scalar_u = vfredusum_vs_f32m8_f32m1(_scalar_u, _hcu, _scalar_u, vl);
494
495 R = vfmv_f_s_f32m1_f32(_scalar_r);
496 U = vfmv_f_s_f32m1_f32(_scalar_u);
497
498 ptr_hc += vl;
499 ptr_hcr += vl;
500 ptr_hcu += vl;
501 n_out -= vl;
502 }
503 ptr_hc = NULL;
504 ptr_hcr = NULL;
505 ptr_hcu = NULL;
506
507 // sigmoid(R)
508 // sigmoid(U)
509 R = 1.f / (1.f + exp(-R));
510 U = 1.f / (1.f + exp(-U));
511
512 // gate new
513 const float* bias_c_WN = bias_c.row(2);
514 const float* bias_c_BN = bias_c.row(3);
515
516 const float* weight_xc_N = weight_xc.row(num_output * 2 + q);
517 const float* weight_hc_N = weight_hc.row(num_output * 2 + q);
518
519 float N = bias_c_BN[q];
520
521 int n_out2 = num_output;
522 const float* ptr_hc2 = hidden_state;
523 const float* ptr_whc_n = weight_hc_N;
524 while (n_out2 > 0)
525 {
526 size_t vl = vsetvl_e16m4(n_out2);
527
528 vfloat32m8_t _h_cont = vle32_v_f32m8(ptr_hc2, vl);
529 vfloat32m8_t _whc_n = vle32_v_f32m8(ptr_whc_n, vl);
530 vfloat32m1_t _scalar_n = vfmv_s_f_f32m1(vundefined_f32m1(), N, vl);
531
532 _h_cont = vfmul_vv_f32m8(_whc_n, _h_cont, vl);
533 _scalar_n = vfredusum_vs_f32m8_f32m1(_scalar_n, _h_cont, _scalar_n, vl);
534
535 N = vfmv_f_s_f32m1_f32(_scalar_n);
536 n_out2 -= vl;
537 ptr_hc2 += vl;
538 ptr_whc_n += vl;
539 }
540 ptr_hc2 = NULL;
541 ptr_whc_n = NULL;
542
543 N = bias_c_WN[q] + R * N;
544
545 int n2 = size;
546 const __fp16* ptr_x2 = x;
547 const float* ptr_xcn = weight_xc_N;
548 while (n2 > 0)
549 {
550 size_t vl = vsetvl_e16m4(n2);
551
552 vfloat32m8_t _x = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr_x2, vl), vl);
553 vfloat32m8_t _xcn = vle32_v_f32m8(ptr_xcn, vl);
554 vfloat32m1_t _scalar_n = vfmv_s_f_f32m1(vundefined_f32m1(), N, vl);
555
556 _xcn = vfmul_vv_f32m8(_x, _xcn, vl);
557 _scalar_n = vfredusum_vs_f32m8_f32m1(_scalar_n, _xcn, _scalar_n, vl);
558 N = vfmv_f_s_f32m1_f32(_scalar_n);
559
560 n2 -= vl;
561 ptr_x2 += vl;
562 ptr_xcn += vl;
563 }
564 ptr_x2 = NULL;
565 ptr_xcn = NULL;
566
567 // tanh(N)
568 N = tanh(N);
569
570 gates_data[0] = U;
571 gates_data[1] = N;
572 }
573
574 // h_t := (1 - update) .* new + update .* h_{t-1}
575 __fp16* output_data = top_blob.row<__fp16>(ti);
576 #pragma omp parallel for num_threads(opt.num_threads)
577 for (int q = 0; q < num_output; q++)
578 {
579 const float* gates_data = gates.row(q);
580
581 float U = gates_data[0];
582 float N = gates_data[1];
583
584 float H = (1 - U) * N + U * hidden_state[q];
585
586 hidden_state[q] = H;
587 output_data[q] = (__fp16)H;
588 }
589 }
590
591 return 0;
592}
593
594int GRU_riscv::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
595{
596 int T = bottom_blob.h;
597
598 int num_directions = direction == 2 ? 2 : 1;
599 // initial hidden state
600 Mat hidden(num_output, 4u, opt.workspace_allocator);
601 if (hidden.empty())
602 return -100;
603 hidden.fill(0.f);
604
605 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
606 if (top_blob.empty())
607 return -100;
608
609 // Uni directional
610 if (direction == 0 || direction == 1)
611 {
612 int ret = gru_fp16s(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
613 if (ret != 0)
614 return ret;
615 }
616
617 if (direction == 2)
618 {
619 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
620 if (top_blob_forward.empty())
621 return -100;
622
623 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
624 if (top_blob_reverse.empty())
625 return -100;
626
627 int ret0 = gru_fp16s(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden, opt);
628 if (ret0 != 0)
629 return ret0;
630
631 hidden.fill(0.0f);
632
633 int ret1 = gru_fp16s(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), hidden, opt);
634 if (ret1 != 0)
635 return ret1;
636
637 // concat w
638 for (int i = 0; i < T; i++)
639 {
640 const __fp16* pf = top_blob_forward.row<const __fp16>(i);
641 const __fp16* pr = top_blob_reverse.row<const __fp16>(i);
642 __fp16* ptr = top_blob.row<__fp16>(i);
643
644 memcpy(ptr, pf, num_output * sizeof(__fp16));
645 memcpy(ptr + num_output, pr, num_output * sizeof(__fp16));
646 }
647 }
648
649 return 0;
650}
651
652int GRU_riscv::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
653{
654 const Mat& bottom_blob = bottom_blobs[0];
655 int T = bottom_blob.h;
656 int num_directions = direction == 2 ? 2 : 1;
657
658 Mat hidden;
659 Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
660 if (bottom_blobs.size() == 2)
661 {
662 Option opt_cast = opt;
663 opt_cast.blob_allocator = hidden_allocator;
664 cast_float16_to_float32(bottom_blobs[1], hidden, opt_cast);
665 }
666 else
667 {
668 hidden.create(num_output, num_directions, 4u, hidden_allocator);
669 if (hidden.empty())
670 return -100;
671 hidden.fill(0.f);
672 }
673
674 Mat& top_blob = top_blobs[0];
675 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
676 if (top_blob.empty())
677 return -100;
678
679 // Uni directional
680 if (direction == 0 || direction == 1)
681 {
682 Mat hidden0 = hidden.row_range(0, 1);
683 int ret = gru_fp16s(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden0, opt);
684 if (ret != 0)
685 return ret;
686 }
687
688 if (direction == 2)
689 {
690 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
691 if (top_blob_forward.empty())
692 return -100;
693
694 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
695 if (top_blob_reverse.empty())
696 return -100;
697
698 Mat hidden0 = hidden.row_range(0, 1);
699 int ret0 = gru_fp16s(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), hidden0, opt);
700 if (ret0 != 0)
701 return ret0;
702
703 Mat hidden1 = hidden.row_range(1, 1);
704 int ret1 = gru_fp16s(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), hidden1, opt);
705 if (ret1 != 0)
706 return ret1;
707
708 // concat w
709 for (int i = 0; i < T; i++)
710 {
711 const __fp16* pf = top_blob_forward.row<const __fp16>(i);
712 const __fp16* pr = top_blob_reverse.row<const __fp16>(i);
713 __fp16* ptr = top_blob.row<__fp16>(i);
714
715 memcpy(ptr, pf, num_output * sizeof(__fp16));
716 memcpy(ptr + num_output, pr, num_output * sizeof(__fp16));
717 }
718 }
719
720 if (top_blobs.size() == 2)
721 {
722 cast_float32_to_float16(hidden, top_blobs[1], opt);
723 }
724
725 return 0;
726}
727
728#endif
729
730//fp16sa start at here
731#if __riscv_vector && __riscv_zfh
732
733int GRU_riscv::create_pipeline_fp16sa(const Option& opt)
734{
735 cast_float32_to_float16(weight_xc_data, weight_xc_data_fp16sa, opt);
736 cast_float32_to_float16(weight_hc_data, weight_hc_data_fp16sa, opt);
737 cast_float32_to_float16(bias_c_data, bias_c_data_fp16sa, opt);
738
739 if (opt.lightmode)
740 {
741 weight_xc_data.release();
742 bias_c_data.release();
743 weight_hc_data.release();
744 }
745
746 return 0;
747}
748
749static int gru_fp16sa(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)
750{
751 int size = bottom_blob.w;
752 int T = bottom_blob.h;
753
754 int num_output = top_blob.w;
755
756 // 2 x num_output
757 Mat gates(2, num_output, 4u, opt.workspace_allocator);
758 if (gates.empty())
759 return -100;
760
761 // unroll
762 for (int t = 0; t < T; t++)
763 {
764 int ti = reverse ? T - 1 - t : t;
765
766 const __fp16* x = bottom_blob.row<const __fp16>(ti);
767 #pragma omp parallel for num_threads(opt.num_threads)
768 for (int q = 0; q < num_output; q++)
769 {
770 float* gates_data = gates.row(q);
771
772 // gate reset update
773 const __fp16* bias_c_R = bias_c.row<const __fp16>(0);
774 const __fp16* bias_c_U = bias_c.row<const __fp16>(1);
775
776 const __fp16* weight_xc_R = weight_xc.row<const __fp16>(num_output * 0 + q);
777 const __fp16* weight_xc_U = weight_xc.row<const __fp16>(num_output * 1 + q);
778 const __fp16* weight_hc_R = weight_hc.row<const __fp16>(num_output * 0 + q);
779 const __fp16* weight_hc_U = weight_hc.row<const __fp16>(num_output * 1 + q);
780
781 __fp16 R = bias_c_R[q];
782 __fp16 U = bias_c_U[q];
783
784 int n = size;
785 const __fp16* ptr_x = x;
786 const __fp16* ptr_xcr = weight_xc_R;
787 const __fp16* ptr_xcu = weight_xc_U;
788 while (n > 0)
789 {
790 size_t vl = vsetvl_e16m8(n);
791 vfloat16m8_t _x = vle16_v_f16m8(ptr_x, vl);
792 vfloat16m8_t _xcr = vle16_v_f16m8(ptr_xcr, vl);
793 vfloat16m8_t _xcu = vle16_v_f16m8(ptr_xcu, vl);
794 vfloat16m1_t _scalar_r = vfmv_s_f_f16m1(vundefined_f16m1(), R, vl);
795 vfloat16m1_t _scalar_u = vfmv_s_f_f16m1(vundefined_f16m1(), U, vl);
796
797 _xcr = vfmul_vv_f16m8(_xcr, _x, vl);
798 _xcu = vfmul_vv_f16m8(_xcu, _x, vl);
799 _scalar_r = vfredusum_vs_f16m8_f16m1(_scalar_r, _xcr, _scalar_r, vl);
800 _scalar_u = vfredusum_vs_f16m8_f16m1(_scalar_u, _xcu, _scalar_u, vl);
801
802 R = vfmv_f_s_f16m1_f16(_scalar_r);
803 U = vfmv_f_s_f16m1_f16(_scalar_u);
804
805 ptr_x += vl;
806 ptr_xcr += vl;
807 ptr_xcu += vl;
808 n -= vl;
809 }
810
811 int n_out = num_output;
812 const float* ptr_hc = hidden_state;
813 const __fp16* ptr_hcr = weight_hc_R;
814 const __fp16* ptr_hcu = weight_hc_U;
815 while (n_out > 0)
816 {
817 size_t vl = vsetvl_e16m4(n_out);
818 vfloat16m4_t _h_cont = vfncvt_f_f_w_f16m4(vle32_v_f32m8(ptr_hc, vl), vl);
819 vfloat16m4_t _hcr = vle16_v_f16m4(ptr_hcr, vl);
820 vfloat16m4_t _hcu = vle16_v_f16m4(ptr_hcu, vl);
821 vfloat16m1_t _scalar_r = vfmv_s_f_f16m1(vundefined_f16m1(), R, vl);
822 vfloat16m1_t _scalar_u = vfmv_s_f_f16m1(vundefined_f16m1(), U, vl);
823
824 _hcr = vfmul_vv_f16m4(_hcr, _h_cont, vl);
825 _hcu = vfmul_vv_f16m4(_hcu, _h_cont, vl);
826 _scalar_r = vfredusum_vs_f16m4_f16m1(_scalar_r, _hcr, _scalar_r, vl);
827 _scalar_u = vfredusum_vs_f16m4_f16m1(_scalar_u, _hcu, _scalar_u, vl);
828
829 R = vfmv_f_s_f16m1_f16(_scalar_r);
830 U = vfmv_f_s_f16m1_f16(_scalar_u);
831
832 ptr_hc += vl;
833 ptr_hcr += vl;
834 ptr_hcu += vl;
835 n_out -= vl;
836 }
837
838 // sigmoid(R)
839 // sigmoid(U)
840 R = 1.f / (1.f + (__fp16)exp((float)(-R)));
841 U = 1.f / (1.f + (__fp16)exp((float)(-U)));
842
843 // gate new
844 const __fp16* bias_c_WN = bias_c.row<const __fp16>(2);
845 const __fp16* bias_c_BN = bias_c.row<const __fp16>(3);
846
847 const __fp16* weight_xc_N = weight_xc.row<const __fp16>(num_output * 2 + q);
848 const __fp16* weight_hc_N = weight_hc.row<const __fp16>(num_output * 2 + q);
849
850 __fp16 N = bias_c_BN[q];
851
852 int n_out2 = num_output;
853 const float* ptr_hc2 = hidden_state;
854 const __fp16* ptr_whc_n = weight_hc_N;
855 while (n_out2 > 0)
856 {
857 size_t vl = vsetvl_e16m4(n_out2);
858
859 vfloat16m4_t _h_cont = vfncvt_f_f_w_f16m4(vle32_v_f32m8(ptr_hc2, vl), vl);
860 vfloat16m4_t _whc_n = vle16_v_f16m4(ptr_whc_n, vl);
861 vfloat16m1_t _scalar_n = vfmv_s_f_f16m1(vundefined_f16m1(), N, vl);
862
863 _h_cont = vfmul_vv_f16m4(_whc_n, _h_cont, vl);
864 _scalar_n = vfredusum_vs_f16m4_f16m1(_scalar_n, _h_cont, _scalar_n, vl);
865
866 N = vfmv_f_s_f16m1_f16(_scalar_n);
867 n_out2 -= vl;
868 ptr_hc2 += vl;
869 ptr_whc_n += vl;
870 }
871 N = bias_c_WN[q] + R * N;
872
873 int n2 = size;
874 const __fp16* ptr_x2 = x;
875 const __fp16* ptr_xcn = weight_xc_N;
876 while (n2 > 0)
877 {
878 size_t vl = vsetvl_e16m8(n2);
879
880 vfloat16m8_t _x = vle16_v_f16m8(ptr_x2, vl);
881 vfloat16m8_t _xcn = vle16_v_f16m8(ptr_xcn, vl);
882 vfloat16m1_t _scalar_n = vfmv_s_f_f16m1(vundefined_f16m1(), N, vl);
883
884 _xcn = vfmul_vv_f16m8(_x, _xcn, vl);
885 _scalar_n = vfredusum_vs_f16m8_f16m1(_scalar_n, _xcn, _scalar_n, vl);
886 N = vfmv_f_s_f16m1_f16(_scalar_n);
887
888 n2 -= vl;
889 ptr_x2 += vl;
890 ptr_xcn += vl;
891 }
892
893 // tanh(N)
894 N = (__fp16)tanh((float)N);
895
896 gates_data[0] = U;
897 gates_data[1] = N;
898 }
899
900 // h_t := (1 - update) .* new + update .* h_{t-1}
901 __fp16* output_data = top_blob.row<__fp16>(ti);
902 #pragma omp parallel for num_threads(opt.num_threads)
903 for (int q = 0; q < num_output; q++)
904 {
905 const float* gates_data = gates.row(q);
906
907 float U = gates_data[0];
908 float N = gates_data[1];
909
910 float H = (1 - U) * N + U * hidden_state[q];
911
912 hidden_state[q] = H;
913 output_data[q] = H;
914 }
915 }
916
917 return 0;
918}
919
920int GRU_riscv::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
921{
922 int T = bottom_blob.h;
923
924 int num_directions = direction == 2 ? 2 : 1;
925 // initial hidden state
926 Mat hidden(num_output, 4u, opt.workspace_allocator);
927 if (hidden.empty())
928 return -100;
929 hidden.fill(0.f);
930
931 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
932 if (top_blob.empty())
933 return -100;
934
935 // Uni directional
936 if (direction == 0 || direction == 1)
937 {
938 int ret = gru_fp16sa(bottom_blob, top_blob, direction, weight_xc_data_fp16sa.channel(0), bias_c_data_fp16sa.channel(0), weight_hc_data_fp16sa.channel(0), hidden, opt);
939 if (ret != 0)
940 return ret;
941 }
942
943 if (direction == 2)
944 {
945 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
946 if (top_blob_forward.empty())
947 return -100;
948
949 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
950 if (top_blob_reverse.empty())
951 return -100;
952
953 int ret0 = gru_fp16sa(bottom_blob, top_blob_forward, 0, weight_xc_data_fp16sa.channel(0), bias_c_data_fp16sa.channel(0), weight_hc_data_fp16sa.channel(0), hidden, opt);
954 if (ret0 != 0)
955 return ret0;
956
957 hidden.fill(0.0f);
958
959 int ret1 = gru_fp16sa(bottom_blob, top_blob_reverse, 1, weight_xc_data_fp16sa.channel(1), bias_c_data_fp16sa.channel(1), weight_hc_data_fp16sa.channel(1), hidden, opt);
960 if (ret1 != 0)
961 return ret1;
962
963 // concat w
964 for (int i = 0; i < T; i++)
965 {
966 const __fp16* pf = top_blob_forward.row<const __fp16>(i);
967 const __fp16* pr = top_blob_reverse.row<const __fp16>(i);
968 __fp16* ptr = top_blob.row<__fp16>(i);
969
970 memcpy(ptr, pf, num_output * sizeof(__fp16));
971 memcpy(ptr + num_output, pr, num_output * sizeof(__fp16));
972 }
973 }
974
975 return 0;
976}
977
978int GRU_riscv::forward_fp16sa(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
979{
980 const Mat& bottom_blob = bottom_blobs[0];
981 int T = bottom_blob.h;
982 int num_directions = direction == 2 ? 2 : 1;
983
984 Mat hidden;
985 Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator;
986 if (bottom_blobs.size() == 2)
987 {
988 Option opt_cast = opt;
989 opt_cast.blob_allocator = hidden_allocator;
990 cast_float16_to_float32(bottom_blobs[1], hidden, opt_cast);
991 }
992 else
993 {
994 hidden.create(num_output, num_directions, 4u, hidden_allocator);
995 if (hidden.empty())
996 return -100;
997 hidden.fill(0.f);
998 }
999
1000 Mat& top_blob = top_blobs[0];
1001 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
1002 if (top_blob.empty())
1003 return -100;
1004
1005 // Uni directional
1006 if (direction == 0 || direction == 1)
1007 {
1008 int ret = gru_fp16sa(bottom_blob, top_blob, direction, weight_xc_data_fp16sa.channel(0), bias_c_data_fp16sa.channel(0), weight_hc_data_fp16sa.channel(0), hidden, opt);
1009 if (ret != 0)
1010 return ret;
1011 }
1012
1013 if (direction == 2)
1014 {
1015 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
1016 if (top_blob_forward.empty())
1017 return -100;
1018
1019 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
1020 if (top_blob_reverse.empty())
1021 return -100;
1022
1023 Mat hidden0 = hidden.row_range(0, 1);
1024 int ret0 = gru_fp16sa(bottom_blob, top_blob_forward, 0, weight_xc_data_fp16sa.channel(0), bias_c_data_fp16sa.channel(0), weight_hc_data_fp16sa.channel(0), hidden0, opt);
1025 if (ret0 != 0)
1026 return ret0;
1027
1028 Mat hidden1 = hidden.row_range(1, 1);
1029 int ret1 = gru_fp16sa(bottom_blob, top_blob_reverse, 1, weight_xc_data_fp16sa.channel(1), bias_c_data_fp16sa.channel(1), weight_hc_data_fp16sa.channel(1), hidden1, opt);
1030 if (ret1 != 0)
1031 return ret1;
1032
1033 // concat w
1034 for (int i = 0; i < T; i++)
1035 {
1036 const __fp16* pf = top_blob_forward.row<const __fp16>(i);
1037 const __fp16* pr = top_blob_reverse.row<const __fp16>(i);
1038 __fp16* ptr = top_blob.row<__fp16>(i);
1039
1040 memcpy(ptr, pf, num_output * sizeof(__fp16));
1041 memcpy(ptr + num_output, pr, num_output * sizeof(__fp16));
1042 }
1043 }
1044
1045 if (top_blobs.size() == 2)
1046 {
1047 cast_float32_to_float16(hidden, top_blobs[1], opt);
1048 }
1049
1050 return 0;
1051}
1052
1053#endif
1054
1055} // namespace ncnn