Elffuss BraidHub

kikocis/ncnn public

master · 48.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 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 "lstm_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#include "lstm_int8.h"
29
30LSTM_arm::LSTM_arm()
31{
32#if __ARM_NEON
33#if NCNN_ARM82
34 support_fp16_storage = cpu_support_arm_asimdhp();
35#endif
36#endif // __ARM_NEON
37
38#if NCNN_BF16
39 support_bf16_storage = true;
40#endif
41}
42
43int LSTM_arm::create_pipeline(const Option& opt)
44{
45#if NCNN_INT8
46 if (int8_scale_term)
47 {
48 return create_pipeline_int8(opt);
49 }
50#endif
51
52#if NCNN_ARM82
53 if (support_fp16_storage && opt.use_fp16_storage)
54 {
55 return create_pipeline_fp16s(opt);
56 }
57#endif
58
59#if NCNN_BF16
60 if (opt.use_bf16_storage)
61 {
62 return create_pipeline_bf16s(opt);
63 }
64#endif
65
66 // pack IFOG
67 int num_directions = direction == 2 ? 2 : 1;
68 int size = weight_data_size / num_directions / hidden_size / 4;
69
70 weight_xc_data_packed.create(size, hidden_size, num_directions, 16u, 4);
71 bias_c_data_packed.create(hidden_size, 1, num_directions, 16u, 4);
72 weight_hc_data_packed.create(num_output, hidden_size, num_directions, 16u, 4);
73
74 #pragma omp parallel for num_threads(opt.num_threads)
75 for (int dr = 0; dr < num_directions; dr++)
76 {
77 const Mat weight_xc = weight_xc_data.channel(dr);
78 const Mat bias_c = bias_c_data.channel(dr);
79 const Mat weight_hc = weight_hc_data.channel(dr);
80
81 Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr);
82 Mat bias_c_data_packed_dr = bias_c_data_packed.channel(dr);
83 Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr);
84
85 const float* bias_c_I = bias_c.row(0);
86 const float* bias_c_F = bias_c.row(1);
87 const float* bias_c_O = bias_c.row(2);
88 const float* bias_c_G = bias_c.row(3);
89
90 float* bias_c_IFOG = bias_c_data_packed_dr.row(0);
91
92 for (int q = 0; q < hidden_size; q++)
93 {
94 bias_c_IFOG[0] = bias_c_I[q];
95 bias_c_IFOG[1] = bias_c_F[q];
96 bias_c_IFOG[2] = bias_c_O[q];
97 bias_c_IFOG[3] = bias_c_G[q];
98
99 bias_c_IFOG += 4;
100
101 const float* weight_xc_I = weight_xc.row(hidden_size * 0 + q);
102 const float* weight_xc_F = weight_xc.row(hidden_size * 1 + q);
103 const float* weight_xc_O = weight_xc.row(hidden_size * 2 + q);
104 const float* weight_xc_G = weight_xc.row(hidden_size * 3 + q);
105
106 const float* weight_hc_I = weight_hc.row(hidden_size * 0 + q);
107 const float* weight_hc_F = weight_hc.row(hidden_size * 1 + q);
108 const float* weight_hc_O = weight_hc.row(hidden_size * 2 + q);
109 const float* weight_hc_G = weight_hc.row(hidden_size * 3 + q);
110
111 float* weight_xc_IFOG = weight_xc_data_packed_dr.row(q);
112 float* weight_hc_IFOG = weight_hc_data_packed_dr.row(q);
113
114 for (int i = 0; i < size; i++)
115 {
116 weight_xc_IFOG[0] = weight_xc_I[i];
117 weight_xc_IFOG[1] = weight_xc_F[i];
118 weight_xc_IFOG[2] = weight_xc_O[i];
119 weight_xc_IFOG[3] = weight_xc_G[i];
120
121 weight_xc_IFOG += 4;
122 }
123
124 for (int i = 0; i < num_output; i++)
125 {
126 weight_hc_IFOG[0] = weight_hc_I[i];
127 weight_hc_IFOG[1] = weight_hc_F[i];
128 weight_hc_IFOG[2] = weight_hc_O[i];
129 weight_hc_IFOG[3] = weight_hc_G[i];
130
131 weight_hc_IFOG += 4;
132 }
133 }
134 }
135
136 if (opt.lightmode)
137 {
138 weight_xc_data.release();
139 bias_c_data.release();
140 weight_hc_data.release();
141 }
142
143 return 0;
144}
145
146static int lstm(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc, const Mat& bias_c, const Mat& weight_hc, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt)
147{
148 int size = bottom_blob.w;
149 int T = bottom_blob.h;
150
151 int num_output = top_blob.w;
152 int hidden_size = cell_state.w;
153
154 // 4 x hidden_size
155 Mat gates(4, hidden_size, 4u, opt.workspace_allocator);
156 if (gates.empty())
157 return -100;
158
159 Mat tmp_hidden_state;
160 if (num_output != hidden_size)
161 {
162 tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator);
163 if (tmp_hidden_state.empty())
164 return -100;
165 }
166
167 // unroll
168 for (int t = 0; t < T; t++)
169 {
170 // clip hidden by continuation indicator
171 // h_cont_{t-1} = cont_t * h_{t-1}
172 // h_cont_{t-1} = h_{t-1} if cont_t == 1
173 // 0 otherwise
174 // calculate hidden
175 // gate_input_t := W_hc * h_conted_{t-1} + W_xc * x_t + b_c
176
177 int ti = reverse ? T - 1 - t : t;
178
179 const float* x = bottom_blob.row(ti);
180 #pragma omp parallel for num_threads(opt.num_threads)
181 for (int q = 0; q < hidden_size; q++)
182 {
183 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
184
185 // gate I F O G
186 const float* weight_xc_IFOG = weight_xc.row(q);
187
188 const float* weight_hc_IFOG = weight_hc.row(q);
189
190#if __ARM_NEON
191 float32x4_t _IFOG = vld1q_f32(bias_c_IFOG);
192 float32x4_t _sum1 = vdupq_n_f32(0.f);
193 float32x4_t _sum2 = vdupq_n_f32(0.f);
194 float32x4_t _sum3 = vdupq_n_f32(0.f);
195#else
196 float I = bias_c_IFOG[0];
197 float F = bias_c_IFOG[1];
198 float O = bias_c_IFOG[2];
199 float G = bias_c_IFOG[3];
200#endif // __ARM_NEON
201
202 int i = 0;
203#if __ARM_NEON
204 for (; i + 3 < size; i += 4)
205 {
206 float32x4_t _xi = vld1q_f32(x + i);
207
208 float32x4_t _weight_xc_IFOG_0 = vld1q_f32(weight_xc_IFOG);
209 float32x4_t _weight_xc_IFOG_1 = vld1q_f32(weight_xc_IFOG + 4);
210 float32x4_t _weight_xc_IFOG_2 = vld1q_f32(weight_xc_IFOG + 8);
211 float32x4_t _weight_xc_IFOG_3 = vld1q_f32(weight_xc_IFOG + 12);
212
213#if __aarch64__
214 _IFOG = vfmaq_laneq_f32(_IFOG, _weight_xc_IFOG_0, _xi, 0);
215 _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_IFOG_1, _xi, 1);
216 _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_IFOG_2, _xi, 2);
217 _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_IFOG_3, _xi, 3);
218#else
219 _IFOG = vmlaq_lane_f32(_IFOG, _weight_xc_IFOG_0, vget_low_f32(_xi), 0);
220 _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_IFOG_1, vget_low_f32(_xi), 1);
221 _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_IFOG_2, vget_high_f32(_xi), 0);
222 _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_IFOG_3, vget_high_f32(_xi), 1);
223#endif
224
225 weight_xc_IFOG += 16;
226 }
227#endif // __ARM_NEON
228 for (; i < size; i++)
229 {
230 float xi = x[i];
231
232#if __ARM_NEON
233 float32x4_t _xi = vdupq_n_f32(xi);
234 float32x4_t _weight_xc_IFOG = vld1q_f32(weight_xc_IFOG);
235 _IFOG = vmlaq_f32(_IFOG, _weight_xc_IFOG, _xi);
236#else
237 I += weight_xc_IFOG[0] * xi;
238 F += weight_xc_IFOG[1] * xi;
239 O += weight_xc_IFOG[2] * xi;
240 G += weight_xc_IFOG[3] * xi;
241#endif // __ARM_NEON
242
243 weight_xc_IFOG += 4;
244 }
245
246 i = 0;
247#if __ARM_NEON
248 for (; i + 3 < num_output; i += 4)
249 {
250 float32x4_t _h_cont = vld1q_f32((const float*)hidden_state + i);
251
252 float32x4_t _weight_hc_IFOG_0 = vld1q_f32(weight_hc_IFOG);
253 float32x4_t _weight_hc_IFOG_1 = vld1q_f32(weight_hc_IFOG + 4);
254 float32x4_t _weight_hc_IFOG_2 = vld1q_f32(weight_hc_IFOG + 8);
255 float32x4_t _weight_hc_IFOG_3 = vld1q_f32(weight_hc_IFOG + 12);
256
257#if __aarch64__
258 _IFOG = vfmaq_laneq_f32(_IFOG, _weight_hc_IFOG_0, _h_cont, 0);
259 _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_IFOG_1, _h_cont, 1);
260 _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_IFOG_2, _h_cont, 2);
261 _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_IFOG_3, _h_cont, 3);
262#else
263 _IFOG = vmlaq_lane_f32(_IFOG, _weight_hc_IFOG_0, vget_low_f32(_h_cont), 0);
264 _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_IFOG_1, vget_low_f32(_h_cont), 1);
265 _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_IFOG_2, vget_high_f32(_h_cont), 0);
266 _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_IFOG_3, vget_high_f32(_h_cont), 1);
267#endif
268
269 weight_hc_IFOG += 16;
270 }
271#endif // __ARM_NEON
272 for (; i < num_output; i++)
273 {
274 float h_cont = hidden_state[i];
275
276#if __ARM_NEON
277 float32x4_t _h_cont = vdupq_n_f32(h_cont);
278 float32x4_t _weight_hc_IFOG = vld1q_f32(weight_hc_IFOG);
279 _IFOG = vmlaq_f32(_IFOG, _weight_hc_IFOG, _h_cont);
280#else
281 I += weight_hc_IFOG[0] * h_cont;
282 F += weight_hc_IFOG[1] * h_cont;
283 O += weight_hc_IFOG[2] * h_cont;
284 G += weight_hc_IFOG[3] * h_cont;
285#endif // __ARM_NEON
286
287 weight_hc_IFOG += 4;
288 }
289
290 float* gates_data = gates.row(q);
291
292#if __ARM_NEON
293 _IFOG = vaddq_f32(_IFOG, _sum1);
294 _sum2 = vaddq_f32(_sum2, _sum3);
295 _IFOG = vaddq_f32(_IFOG, _sum2);
296
297 vst1q_f32(gates_data, _IFOG);
298#else
299 gates_data[0] = I;
300 gates_data[1] = F;
301 gates_data[2] = O;
302 gates_data[3] = G;
303#endif // __ARM_NEON
304 }
305
306 // lstm unit
307 // sigmoid(I)
308 // sigmoid(F)
309 // sigmoid(O)
310 // tanh(G)
311 // c_t := f_t .* c_{t-1} + i_t .* g_t
312 // h_t := o_t .* tanh[c_t]
313 float* output_data = top_blob.row(ti);
314
315 float* cell_ptr = cell_state;
316 float* hidden_ptr = hidden_state;
317 float* tmp_hidden_ptr = tmp_hidden_state;
318
319 int remain_hidden_size_start = 0;
320#if __ARM_NEON
321 int nn_hidden_size = hidden_size >> 2;
322 remain_hidden_size_start = nn_hidden_size << 2;
323
324 #pragma omp parallel for num_threads(opt.num_threads)
325 for (int qq = 0; qq < nn_hidden_size; qq++)
326 {
327 int q = qq * 4;
328
329 const float* gates_data = gates.row(q);
330
331 float32x4x4_t _IFOG_4x4 = vld4q_f32(gates_data);
332
333 float32x4_t _lstm_I = sigmoid_ps(_IFOG_4x4.val[0]);
334 float32x4_t _lstm_F = sigmoid_ps(_IFOG_4x4.val[1]);
335 float32x4_t _lstm_O = sigmoid_ps(_IFOG_4x4.val[2]);
336 float32x4_t _lstm_G = tanh_ps(_IFOG_4x4.val[3]);
337
338 float32x4_t _cell2 = vaddq_f32(vmulq_f32(_lstm_F, vld1q_f32(cell_ptr + q)), vmulq_f32(_lstm_I, _lstm_G));
339 float32x4_t _lstm_H = vmulq_f32(_lstm_O, tanh_ps(_cell2));
340
341 vst1q_f32(cell_ptr + q, _cell2);
342
343 if (num_output == hidden_size)
344 {
345 vst1q_f32(hidden_ptr + q, _lstm_H);
346 vst1q_f32(output_data + q, _lstm_H);
347 }
348 else
349 {
350 vst1q_f32(tmp_hidden_ptr + q, _lstm_H);
351 }
352 }
353#endif // __ARM_NEON
354 #pragma omp parallel for num_threads(opt.num_threads)
355 for (int q = remain_hidden_size_start; q < hidden_size; q++)
356 {
357 const float* gates_data = gates.row(q);
358
359 float I = gates_data[0];
360 float F = gates_data[1];
361 float O = gates_data[2];
362 float G = gates_data[3];
363
364 I = 1.f / (1.f + expf(-I));
365 F = 1.f / (1.f + expf(-F));
366 O = 1.f / (1.f + expf(-O));
367 G = tanhf(G);
368
369 float cell2 = F * cell_ptr[q] + I * G;
370 float H = O * tanhf(cell2);
371
372 cell_ptr[q] = cell2;
373 if (num_output == hidden_size)
374 {
375 hidden_ptr[q] = H;
376 output_data[q] = H;
377 }
378 else
379 {
380 tmp_hidden_ptr[q] = H;
381 }
382 }
383
384 if (num_output != hidden_size)
385 {
386 // int nn_num_output = num_output >> 2;
387 // int remain_num_output_start = nn_num_output << 2;
388 // #pragma omp parallel for num_threads(opt.num_threads)
389 // for (int qq = 0; qq < nn_num_output; qq++)
390 // {
391 // int q = qq * 4;
392 //
393 // }
394 int remain_num_output_start = 0;
395 #pragma omp parallel for num_threads(opt.num_threads)
396 for (int q = remain_num_output_start; q < num_output; q++)
397 {
398 const float* hr = weight_hr.row(q);
399 const float* tmp_hidden_ptr = tmp_hidden_state;
400
401 float H = 0;
402 for (int i = 0; i < hidden_size; i++)
403 {
404 H += tmp_hidden_ptr[i] * hr[i];
405 }
406
407 hidden_ptr[q] = H;
408 output_data[q] = H;
409 }
410 }
411 }
412
413 return 0;
414}
415
416int LSTM_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
417{
418#if NCNN_INT8
419 if (int8_scale_term)
420 {
421 return forward_int8(bottom_blob, top_blob, opt);
422 }
423#endif
424
425 int elembits = bottom_blob.elembits();
426
427#if NCNN_ARM82
428 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
429 return forward_fp16s(bottom_blob, top_blob, opt);
430#endif
431
432#if NCNN_BF16
433 if (opt.use_bf16_storage && elembits == 16)
434 return forward_bf16s(bottom_blob, top_blob, opt);
435#endif
436
437 int T = bottom_blob.h;
438
439 int num_directions = direction == 2 ? 2 : 1;
440
441 // initial hidden state
442 Mat hidden(num_output, 4u, opt.workspace_allocator);
443 if (hidden.empty())
444 return -100;
445 hidden.fill(0.f);
446
447 Mat cell(hidden_size, 4u, opt.workspace_allocator);
448 if (cell.empty())
449 return -100;
450 cell.fill(0.f);
451
452 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
453 if (top_blob.empty())
454 return -100;
455
456 // Uni directional
457 if (direction == 0 || direction == 1)
458 {
459 int ret = lstm(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
460 if (ret != 0)
461 return ret;
462 }
463
464 if (direction == 2)
465 {
466 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
467 if (top_blob_forward.empty())
468 return -100;
469
470 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
471 if (top_blob_reverse.empty())
472 return -100;
473
474 {
475 int ret = lstm(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
476 if (ret != 0)
477 return ret;
478 }
479
480 hidden.fill(0.0f);
481 cell.fill(0.0f);
482
483 {
484 int ret = lstm(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden, cell, 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 return 0;
502}
503
504int LSTM_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
505{
506#if NCNN_INT8
507 if (int8_scale_term)
508 {
509 return forward_int8(bottom_blobs, top_blobs, opt);
510 }
511#endif
512
513 const Mat& bottom_blob = bottom_blobs[0];
514 int elembits = bottom_blob.elembits();
515
516#if NCNN_ARM82
517 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
518 return forward_fp16s(bottom_blobs, top_blobs, opt);
519#endif
520
521#if NCNN_BF16
522 if (opt.use_bf16_storage && elembits == 16)
523 return forward_bf16s(bottom_blobs, top_blobs, opt);
524#endif
525
526 int T = bottom_blob.h;
527 int num_directions = direction == 2 ? 2 : 1;
528
529 Mat hidden;
530 Mat cell;
531 Allocator* hidden_cell_allocator = top_blobs.size() == 3 ? opt.blob_allocator : opt.workspace_allocator;
532 if (bottom_blobs.size() == 3)
533 {
534 hidden = bottom_blobs[1].clone(hidden_cell_allocator);
535 cell = bottom_blobs[2].clone(hidden_cell_allocator);
536 }
537 else
538 {
539 hidden.create(num_output, num_directions, 4u, hidden_cell_allocator);
540 if (hidden.empty())
541 return -100;
542 hidden.fill(0.f);
543
544 cell.create(hidden_size, num_directions, 4u, hidden_cell_allocator);
545 if (cell.empty())
546 return -100;
547 cell.fill(0.f);
548 }
549
550 Mat& top_blob = top_blobs[0];
551 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
552 if (top_blob.empty())
553 return -100;
554
555 // Uni directional
556 if (direction == 0 || direction == 1)
557 {
558 int ret = lstm(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
559 if (ret != 0)
560 return ret;
561 }
562
563 if (direction == 2)
564 {
565 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
566 if (top_blob_forward.empty())
567 return -100;
568
569 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
570 if (top_blob_reverse.empty())
571 return -100;
572
573 Mat hidden0 = hidden.row_range(0, 1);
574 Mat cell0 = cell.row_range(0, 1);
575 {
576 int ret = lstm(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden0, cell0, opt);
577 if (ret != 0)
578 return ret;
579 }
580
581 Mat hidden1 = hidden.row_range(1, 1);
582 Mat cell1 = cell.row_range(1, 1);
583 {
584 int ret = lstm(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden1, cell1, opt);
585 if (ret != 0)
586 return ret;
587 }
588
589 // concat w
590 for (int i = 0; i < T; i++)
591 {
592 const float* pf = top_blob_forward.row(i);
593 const float* pr = top_blob_reverse.row(i);
594 float* ptr = top_blob.row(i);
595
596 memcpy(ptr, pf, num_output * sizeof(float));
597 memcpy(ptr + num_output, pr, num_output * sizeof(float));
598 }
599 }
600
601 if (top_blobs.size() == 3)
602 {
603 top_blobs[1] = hidden;
604 top_blobs[2] = cell;
605 }
606
607 return 0;
608}
609
610#if NCNN_BF16
611static int lstm_bf16s(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc, const Mat& bias_c, const Mat& weight_hc, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt)
612{
613 int size = bottom_blob.w;
614 int T = bottom_blob.h;
615
616 int num_output = top_blob.w;
617 int hidden_size = cell_state.w;
618
619 // 4 x hidden_size
620 Mat gates(4, hidden_size, 4u, opt.workspace_allocator);
621 if (gates.empty())
622 return -100;
623
624 Mat tmp_hidden_state;
625 if (num_output != hidden_size)
626 {
627 tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator);
628 if (tmp_hidden_state.empty())
629 return -100;
630 }
631
632 // unroll
633 for (int t = 0; t < T; t++)
634 {
635 // clip hidden by continuation indicator
636 // h_cont_{t-1} = cont_t * h_{t-1}
637 // h_cont_{t-1} = h_{t-1} if cont_t == 1
638 // 0 otherwise
639 // calculate hidden
640 // gate_input_t := W_hc * h_conted_{t-1} + W_xc * x_t + b_c
641
642 int ti = reverse ? T - 1 - t : t;
643
644 const unsigned short* x = bottom_blob.row<const unsigned short>(ti);
645 #pragma omp parallel for num_threads(opt.num_threads)
646 for (int q = 0; q < hidden_size; q++)
647 {
648 const unsigned short* bias_c_IFOG = (const unsigned short*)bias_c + q * 4;
649
650 // gate I F O G
651 const unsigned short* weight_xc_IFOG = weight_xc.row<const unsigned short>(q);
652
653 const unsigned short* weight_hc_IFOG = weight_hc.row<const unsigned short>(q);
654
655#if __ARM_NEON
656 float32x4_t _IFOG = bfloat2float(vld1_u16(bias_c_IFOG));
657 float32x4_t _sum1 = vdupq_n_f32(0.f);
658 float32x4_t _sum2 = vdupq_n_f32(0.f);
659 float32x4_t _sum3 = vdupq_n_f32(0.f);
660#else
661 float I = bfloat16_to_float32(bias_c_IFOG[0]);
662 float F = bfloat16_to_float32(bias_c_IFOG[1]);
663 float O = bfloat16_to_float32(bias_c_IFOG[2]);
664 float G = bfloat16_to_float32(bias_c_IFOG[3]);
665#endif // __ARM_NEON
666
667 int i = 0;
668#if __ARM_NEON
669 for (; i + 3 < size; i += 4)
670 {
671 float32x4_t _xi = bfloat2float(vld1_u16(x + i));
672
673 float32x4_t _weight_xc_IFOG_0 = bfloat2float(vld1_u16(weight_xc_IFOG));
674 float32x4_t _weight_xc_IFOG_1 = bfloat2float(vld1_u16(weight_xc_IFOG + 4));
675 float32x4_t _weight_xc_IFOG_2 = bfloat2float(vld1_u16(weight_xc_IFOG + 8));
676 float32x4_t _weight_xc_IFOG_3 = bfloat2float(vld1_u16(weight_xc_IFOG + 12));
677
678#if __aarch64__
679 _IFOG = vfmaq_laneq_f32(_IFOG, _weight_xc_IFOG_0, _xi, 0);
680 _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_IFOG_1, _xi, 1);
681 _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_IFOG_2, _xi, 2);
682 _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_IFOG_3, _xi, 3);
683#else
684 _IFOG = vmlaq_lane_f32(_IFOG, _weight_xc_IFOG_0, vget_low_f32(_xi), 0);
685 _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_IFOG_1, vget_low_f32(_xi), 1);
686 _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_IFOG_2, vget_high_f32(_xi), 0);
687 _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_IFOG_3, vget_high_f32(_xi), 1);
688#endif
689
690 weight_xc_IFOG += 16;
691 }
692#endif // __ARM_NEON
693 for (; i < size; i++)
694 {
695#if __ARM_NEON
696 unsigned short xi = x[i];
697
698 float32x4_t _xi = bfloat2float(vdup_n_u16(xi));
699 float32x4_t _weight_xc_IFOG = bfloat2float(vld1_u16(weight_xc_IFOG));
700 _IFOG = vmlaq_f32(_IFOG, _weight_xc_IFOG, _xi);
701#else
702 float xi = bfloat16_to_float32(x[i]);
703
704 I += bfloat16_to_float32(weight_xc_IFOG[0]) * xi;
705 F += bfloat16_to_float32(weight_xc_IFOG[1]) * xi;
706 O += bfloat16_to_float32(weight_xc_IFOG[2]) * xi;
707 G += bfloat16_to_float32(weight_xc_IFOG[3]) * xi;
708#endif // __ARM_NEON
709
710 weight_xc_IFOG += 4;
711 }
712
713 i = 0;
714#if __ARM_NEON
715 for (; i + 3 < num_output; i += 4)
716 {
717 float32x4_t _h_cont = vld1q_f32((const float*)hidden_state + i);
718
719 float32x4_t _weight_hc_IFOG_0 = bfloat2float(vld1_u16(weight_hc_IFOG));
720 float32x4_t _weight_hc_IFOG_1 = bfloat2float(vld1_u16(weight_hc_IFOG + 4));
721 float32x4_t _weight_hc_IFOG_2 = bfloat2float(vld1_u16(weight_hc_IFOG + 8));
722 float32x4_t _weight_hc_IFOG_3 = bfloat2float(vld1_u16(weight_hc_IFOG + 12));
723
724#if __aarch64__
725 _IFOG = vfmaq_laneq_f32(_IFOG, _weight_hc_IFOG_0, _h_cont, 0);
726 _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_IFOG_1, _h_cont, 1);
727 _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_IFOG_2, _h_cont, 2);
728 _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_IFOG_3, _h_cont, 3);
729#else
730 _IFOG = vmlaq_lane_f32(_IFOG, _weight_hc_IFOG_0, vget_low_f32(_h_cont), 0);
731 _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_IFOG_1, vget_low_f32(_h_cont), 1);
732 _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_IFOG_2, vget_high_f32(_h_cont), 0);
733 _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_IFOG_3, vget_high_f32(_h_cont), 1);
734#endif
735
736 weight_hc_IFOG += 16;
737 }
738#endif // __ARM_NEON
739 for (; i < num_output; i++)
740 {
741 float h_cont = hidden_state[i];
742
743#if __ARM_NEON
744 float32x4_t _h_cont = vdupq_n_f32(h_cont);
745 float32x4_t _weight_hc_IFOG = bfloat2float(vld1_u16(weight_hc_IFOG));
746 _IFOG = vmlaq_f32(_IFOG, _weight_hc_IFOG, _h_cont);
747#else
748 I += bfloat16_to_float32(weight_hc_IFOG[0]) * h_cont;
749 F += bfloat16_to_float32(weight_hc_IFOG[1]) * h_cont;
750 O += bfloat16_to_float32(weight_hc_IFOG[2]) * h_cont;
751 G += bfloat16_to_float32(weight_hc_IFOG[3]) * h_cont;
752#endif // __ARM_NEON
753
754 weight_hc_IFOG += 4;
755 }
756
757 float* gates_data = gates.row(q);
758
759#if __ARM_NEON
760 _IFOG = vaddq_f32(_IFOG, _sum1);
761 _sum2 = vaddq_f32(_sum2, _sum3);
762 _IFOG = vaddq_f32(_IFOG, _sum2);
763
764 vst1q_f32(gates_data, _IFOG);
765#else
766 gates_data[0] = I;
767 gates_data[1] = F;
768 gates_data[2] = O;
769 gates_data[3] = G;
770#endif // __ARM_NEON
771 }
772
773 // lstm unit
774 // sigmoid(I)
775 // sigmoid(F)
776 // sigmoid(O)
777 // tanh(G)
778 // c_t := f_t .* c_{t-1} + i_t .* g_t
779 // h_t := o_t .* tanh[c_t]
780 unsigned short* output_data = top_blob.row<unsigned short>(ti);
781
782 float* cell_ptr = cell_state;
783 float* hidden_ptr = hidden_state;
784 float* tmp_hidden_ptr = tmp_hidden_state;
785
786 int remain_hidden_size_start = 0;
787#if __ARM_NEON
788 int nn_hidden_size = hidden_size >> 2;
789 remain_hidden_size_start = nn_hidden_size << 2;
790
791 #pragma omp parallel for num_threads(opt.num_threads)
792 for (int qq = 0; qq < nn_hidden_size; qq++)
793 {
794 int q = qq * 4;
795
796 const float* gates_data = gates.row(q);
797
798 float32x4x4_t _IFOG_4x4 = vld4q_f32(gates_data);
799
800 float32x4_t _lstm_I = sigmoid_ps(_IFOG_4x4.val[0]);
801 float32x4_t _lstm_F = sigmoid_ps(_IFOG_4x4.val[1]);
802 float32x4_t _lstm_O = sigmoid_ps(_IFOG_4x4.val[2]);
803 float32x4_t _lstm_G = tanh_ps(_IFOG_4x4.val[3]);
804
805 float32x4_t _cell2 = vaddq_f32(vmulq_f32(_lstm_F, vld1q_f32(cell_ptr + q)), vmulq_f32(_lstm_I, _lstm_G));
806 float32x4_t _lstm_H = vmulq_f32(_lstm_O, tanh_ps(_cell2));
807
808 vst1q_f32(cell_ptr + q, _cell2);
809
810 if (num_output == hidden_size)
811 {
812 vst1q_f32(hidden_ptr + q, _lstm_H);
813 vst1_u16(output_data + q, float2bfloat(_lstm_H));
814 }
815 else
816 {
817 vst1q_f32(tmp_hidden_ptr + q, _lstm_H);
818 }
819 }
820#endif // __ARM_NEON
821 #pragma omp parallel for num_threads(opt.num_threads)
822 for (int q = remain_hidden_size_start; q < hidden_size; q++)
823 {
824 const float* gates_data = gates.row(q);
825
826 float I = gates_data[0];
827 float F = gates_data[1];
828 float O = gates_data[2];
829 float G = gates_data[3];
830
831 I = 1.f / (1.f + expf(-I));
832 F = 1.f / (1.f + expf(-F));
833 O = 1.f / (1.f + expf(-O));
834 G = tanhf(G);
835
836 float cell2 = F * cell_ptr[q] + I * G;
837 float H = O * tanhf(cell2);
838
839 cell_ptr[q] = cell2;
840 if (num_output == hidden_size)
841 {
842 hidden_ptr[q] = H;
843 output_data[q] = float32_to_bfloat16(H);
844 }
845 else
846 {
847 tmp_hidden_ptr[q] = H;
848 }
849 }
850
851 if (num_output != hidden_size)
852 {
853 // int nn_num_output = num_output >> 2;
854 // int remain_num_output_start = nn_num_output << 2;
855 // #pragma omp parallel for num_threads(opt.num_threads)
856 // for (int qq = 0; qq < nn_num_output; qq++)
857 // {
858 // int q = qq * 4;
859 //
860 // }
861 int remain_num_output_start = 0;
862 #pragma omp parallel for num_threads(opt.num_threads)
863 for (int q = remain_num_output_start; q < num_output; q++)
864 {
865 const float* hr = weight_hr.row(q);
866 const float* tmp_hidden_ptr = tmp_hidden_state;
867
868 float H = 0;
869 for (int i = 0; i < hidden_size; i++)
870 {
871 H += tmp_hidden_ptr[i] * hr[i];
872 }
873
874 hidden_ptr[q] = H;
875 output_data[q] = float32_to_bfloat16(H);
876 }
877 }
878 }
879
880 return 0;
881}
882
883int LSTM_arm::create_pipeline_bf16s(const Option& opt)
884{
885 // pack IFOG
886 int num_directions = direction == 2 ? 2 : 1;
887 int size = weight_data_size / num_directions / hidden_size / 4;
888
889 weight_xc_data_packed.create(size, hidden_size, num_directions, 8u, 4);
890 bias_c_data_packed.create(hidden_size, 1, num_directions, 8u, 4);
891 weight_hc_data_packed.create(num_output, hidden_size, num_directions, 8u, 4);
892
893 #pragma omp parallel for num_threads(opt.num_threads)
894 for (int dr = 0; dr < num_directions; dr++)
895 {
896 const Mat weight_xc = weight_xc_data.channel(dr);
897 const Mat bias_c = bias_c_data.channel(dr);
898 const Mat weight_hc = weight_hc_data.channel(dr);
899
900 Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr);
901 Mat bias_c_data_packed_dr = bias_c_data_packed.channel(dr);
902 Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr);
903
904 const float* bias_c_I = bias_c.row(0);
905 const float* bias_c_F = bias_c.row(1);
906 const float* bias_c_O = bias_c.row(2);
907 const float* bias_c_G = bias_c.row(3);
908
909 unsigned short* bias_c_IFOG = bias_c_data_packed_dr.row<unsigned short>(0);
910
911 for (int q = 0; q < hidden_size; q++)
912 {
913 bias_c_IFOG[0] = float32_to_bfloat16(bias_c_I[q]);
914 bias_c_IFOG[1] = float32_to_bfloat16(bias_c_F[q]);
915 bias_c_IFOG[2] = float32_to_bfloat16(bias_c_O[q]);
916 bias_c_IFOG[3] = float32_to_bfloat16(bias_c_G[q]);
917
918 bias_c_IFOG += 4;
919
920 const float* weight_xc_I = weight_xc.row(hidden_size * 0 + q);
921 const float* weight_xc_F = weight_xc.row(hidden_size * 1 + q);
922 const float* weight_xc_O = weight_xc.row(hidden_size * 2 + q);
923 const float* weight_xc_G = weight_xc.row(hidden_size * 3 + q);
924
925 const float* weight_hc_I = weight_hc.row(hidden_size * 0 + q);
926 const float* weight_hc_F = weight_hc.row(hidden_size * 1 + q);
927 const float* weight_hc_O = weight_hc.row(hidden_size * 2 + q);
928 const float* weight_hc_G = weight_hc.row(hidden_size * 3 + q);
929
930 unsigned short* weight_xc_IFOG = weight_xc_data_packed_dr.row<unsigned short>(q);
931 unsigned short* weight_hc_IFOG = weight_hc_data_packed_dr.row<unsigned short>(q);
932
933 for (int i = 0; i < size; i++)
934 {
935 weight_xc_IFOG[0] = float32_to_bfloat16(weight_xc_I[i]);
936 weight_xc_IFOG[1] = float32_to_bfloat16(weight_xc_F[i]);
937 weight_xc_IFOG[2] = float32_to_bfloat16(weight_xc_O[i]);
938 weight_xc_IFOG[3] = float32_to_bfloat16(weight_xc_G[i]);
939
940 weight_xc_IFOG += 4;
941 }
942
943 for (int i = 0; i < num_output; i++)
944 {
945 weight_hc_IFOG[0] = float32_to_bfloat16(weight_hc_I[i]);
946 weight_hc_IFOG[1] = float32_to_bfloat16(weight_hc_F[i]);
947 weight_hc_IFOG[2] = float32_to_bfloat16(weight_hc_O[i]);
948 weight_hc_IFOG[3] = float32_to_bfloat16(weight_hc_G[i]);
949
950 weight_hc_IFOG += 4;
951 }
952 }
953 }
954
955 if (opt.lightmode)
956 {
957 weight_xc_data.release();
958 bias_c_data.release();
959 weight_hc_data.release();
960 }
961
962 return 0;
963}
964
965int LSTM_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
966{
967 int T = bottom_blob.h;
968
969 int num_directions = direction == 2 ? 2 : 1;
970
971 // initial hidden state
972 Mat hidden(num_output, 4u, opt.workspace_allocator);
973 if (hidden.empty())
974 return -100;
975 hidden.fill(0.f);
976
977 Mat cell(hidden_size, 4u, opt.workspace_allocator);
978 if (cell.empty())
979 return -100;
980 cell.fill(0.f);
981
982 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
983 if (top_blob.empty())
984 return -100;
985
986 // Uni directional
987 if (direction == 0 || direction == 1)
988 {
989 int ret = lstm_bf16s(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
990 if (ret != 0)
991 return ret;
992 }
993
994 if (direction == 2)
995 {
996 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
997 if (top_blob_forward.empty())
998 return -100;
999
1000 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
1001 if (top_blob_reverse.empty())
1002 return -100;
1003
1004 {
1005 int ret = lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
1006 if (ret != 0)
1007 return ret;
1008 }
1009
1010 hidden.fill(0.f);
1011 cell.fill(0.f);
1012
1013 {
1014 int ret = lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden, cell, opt);
1015 if (ret != 0)
1016 return ret;
1017 }
1018
1019 // concat w
1020 for (int i = 0; i < T; i++)
1021 {
1022 const unsigned short* pf = top_blob_forward.row<const unsigned short>(i);
1023 const unsigned short* pr = top_blob_reverse.row<const unsigned short>(i);
1024 unsigned short* ptr = top_blob.row<unsigned short>(i);
1025
1026 memcpy(ptr, pf, num_output * sizeof(unsigned short));
1027 memcpy(ptr + num_output, pr, num_output * sizeof(unsigned short));
1028 }
1029 }
1030
1031 return 0;
1032}
1033
1034int LSTM_arm::forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
1035{
1036 const Mat& bottom_blob = bottom_blobs[0];
1037 int T = bottom_blob.h;
1038 int num_directions = direction == 2 ? 2 : 1;
1039
1040 Mat hidden;
1041 Mat cell;
1042 Allocator* hidden_cell_allocator = top_blobs.size() == 3 ? opt.blob_allocator : opt.workspace_allocator;
1043 if (bottom_blobs.size() == 3)
1044 {
1045 Option opt_cast = opt;
1046 opt_cast.blob_allocator = hidden_cell_allocator;
1047 cast_bfloat16_to_float32(bottom_blobs[1], hidden, opt_cast);
1048 cast_bfloat16_to_float32(bottom_blobs[2], cell, opt_cast);
1049 }
1050 else
1051 {
1052 hidden.create(num_output, num_directions, 4u, hidden_cell_allocator);
1053 if (hidden.empty())
1054 return -100;
1055 hidden.fill(0.f);
1056
1057 cell.create(hidden_size, num_directions, 4u, hidden_cell_allocator);
1058 if (cell.empty())
1059 return -100;
1060 cell.fill(0.f);
1061 }
1062
1063 Mat& top_blob = top_blobs[0];
1064 top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator);
1065 if (top_blob.empty())
1066 return -100;
1067
1068 // Uni directional
1069 if (direction == 0 || direction == 1)
1070 {
1071 int ret = lstm_bf16s(bottom_blob, top_blob, direction, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
1072 if (ret != 0)
1073 return ret;
1074 }
1075
1076 if (direction == 2)
1077 {
1078 Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator);
1079 if (top_blob_forward.empty())
1080 return -100;
1081
1082 Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator);
1083 if (top_blob_reverse.empty())
1084 return -100;
1085
1086 Mat hidden0 = hidden.row_range(0, 1);
1087 Mat cell0 = cell.row_range(0, 1);
1088 {
1089 int ret = lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden0, cell0, opt);
1090 if (ret != 0)
1091 return ret;
1092 }
1093
1094 Mat hidden1 = hidden.row_range(1, 1);
1095 Mat cell1 = cell.row_range(1, 1);
1096 {
1097 int ret = lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden1, cell1, opt);
1098 if (ret != 0)
1099 return ret;
1100 }
1101
1102 // concat w
1103 for (int i = 0; i < T; i++)
1104 {
1105 const unsigned short* pf = top_blob_forward.row<const unsigned short>(i);
1106 const unsigned short* pr = top_blob_reverse.row<const unsigned short>(i);
1107 unsigned short* ptr = top_blob.row<unsigned short>(i);
1108
1109 memcpy(ptr, pf, num_output * sizeof(unsigned short));
1110 memcpy(ptr + num_output, pr, num_output * sizeof(unsigned short));
1111 }
1112 }
1113
1114 if (top_blobs.size() == 3)
1115 {
1116 cast_float32_to_bfloat16(hidden, top_blobs[1], opt);
1117 cast_float32_to_bfloat16(cell, top_blobs[2], opt);
1118 }
1119
1120 return 0;
1121}
1122#endif // NCNN_BF16
1123
1124#if NCNN_INT8
1125int LSTM_arm::create_pipeline_int8(const Option& opt)
1126{
1127 // pack IFOG
1128 const int num_directions = direction == 2 ? 2 : 1;
1129 const int size = weight_data_size / num_directions / hidden_size / 4;
1130
1131 lstm_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, hidden_size, opt);
1132
1133 if (opt.lightmode)
1134 {
1135 weight_xc_data.release();
1136 bias_c_data.release();
1137 weight_hc_data.release();
1138 weight_xc_data_int8_scales.release();
1139 weight_hc_data_int8_scales.release();
1140 }
1141
1142 return 0;
1143}
1144
1145void LSTM_arm::dynamic_quantize(const Mat& bottom_blob, int elemtype, Mat& bottom_blob_int8, Mat& bottom_blob_int8_descales, const Option& opt) const
1146{
1147 int size = bottom_blob.w;
1148 int T = bottom_blob.h;
1149
1150 // dynamic quantize bottom_blob
1151 bottom_blob_int8_descales.create(T, (size_t)4u, 1, opt.blob_allocator);
1152
1153 Mat bottom_blob_int8_scales(T, (size_t)4u, 1, opt.blob_allocator);
1154
1155 if (elemtype == 1)
1156 {
1157 // fp32
1158 for (int t = 0; t < T; t++)
1159 {
1160 const float* x = bottom_blob.row(t);
1161
1162 float absmax = 0.f;
1163 for (int i = 0; i < size; i++)
1164 {
1165 absmax = std::max(absmax, (float)fabs(x[i]));
1166 }
1167
1168 bottom_blob_int8_scales[t] = 127.f / absmax;
1169 bottom_blob_int8_descales[t] = absmax / 127.f;
1170 }
1171 }
1172 if (elemtype == 2)
1173 {
1174 // fp16
1175 for (int t = 0; t < T; t++)
1176 {
1177 const unsigned short* x = bottom_blob.row<const unsigned short>(t);
1178
1179 float absmax = 0.f;
1180 for (int i = 0; i < size; i++)
1181 {
1182 absmax = std::max(absmax, (float)fabs(float16_to_float32(x[i])));
1183 }
1184
1185 bottom_blob_int8_scales[t] = 127.f / absmax;
1186 bottom_blob_int8_descales[t] = absmax / 127.f;
1187 }
1188 }
1189 if (elemtype == 4)
1190 {
1191 // bf16
1192 for (int t = 0; t < T; t++)
1193 {
1194 const unsigned short* x = bottom_blob.row<const unsigned short>(t);
1195
1196 float absmax = 0.f;
1197 for (int i = 0; i < size; i++)
1198 {
1199 absmax = std::max(absmax, (float)fabs(bfloat16_to_float32(x[i])));
1200 }
1201
1202 bottom_blob_int8_scales[t] = 127.f / absmax;
1203 bottom_blob_int8_descales[t] = absmax / 127.f;
1204 }
1205 }
1206
1207 quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt);
1208}
1209
1210int LSTM_arm::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
1211{
1212 int elemtype = 1; // fp32
1213 {
1214 int elembits = bottom_blob.elembits();
1215
1216 // clang-format off
1217 // *INDENT-OFF*
1218
1219#if NCNN_ARM82
1220 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
1221 {
1222 elemtype = 2; // fp16
1223 }
1224 else
1225#endif
1226#if NCNN_BF16
1227 if (opt.use_bf16_storage && elembits == 16)
1228 {
1229 elemtype = 4; // bf16
1230 }
1231 else
1232#endif
1233 {
1234 // fp32
1235 }
1236
1237 // *INDENT-ON*
1238 // clang-format on
1239 }
1240
1241 int T = bottom_blob.h;
1242 size_t elemsize = bottom_blob.elemsize;
1243
1244 int num_directions = direction == 2 ? 2 : 1;
1245
1246 // initial hidden state
1247 Mat hidden(num_output, 4u, opt.workspace_allocator);
1248 if (hidden.empty())
1249 return -100;
1250 hidden.fill(0.f);
1251
1252 Mat cell(hidden_size, 4u, opt.workspace_allocator);
1253 if (cell.empty())
1254 return -100;
1255 cell.fill(0.f);
1256
1257 top_blob.create(num_output * num_directions, T, elemsize, opt.blob_allocator);
1258 if (top_blob.empty())
1259 return -100;
1260
1261 // dynamic quantize bottom_blob
1262 Mat bottom_blob_int8;
1263 Mat bottom_blob_int8_descales;
1264 {
1265 Option opt_quant = opt;
1266 opt_quant.blob_allocator = opt.workspace_allocator;
1267 opt_quant.use_packing_layout = false;
1268 dynamic_quantize(bottom_blob, elemtype, bottom_blob_int8, bottom_blob_int8_descales, opt_quant);
1269 }
1270
1271 // Uni directional
1272 if (direction == 0 || direction == 1)
1273 {
1274 lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
1275 }
1276
1277 if (direction == 2)
1278 {
1279 Mat top_blob_forward(num_output, T, elemsize, opt.workspace_allocator);
1280 if (top_blob_forward.empty())
1281 return -100;
1282
1283 Mat top_blob_reverse(num_output, T, elemsize, opt.workspace_allocator);
1284 if (top_blob_reverse.empty())
1285 return -100;
1286
1287 {
1288 lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
1289 }
1290
1291 hidden.fill(0.f);
1292 cell.fill(0.0f);
1293
1294 {
1295 lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden, cell, opt);
1296 }
1297
1298 // concat w
1299 for (int i = 0; i < T; i++)
1300 {
1301 const unsigned char* pf = top_blob_forward.row<const unsigned char>(i);
1302 const unsigned char* pr = top_blob_reverse.row<const unsigned char>(i);
1303 unsigned char* ptr = top_blob.row<unsigned char>(i);
1304
1305 memcpy(ptr, pf, num_output * elemsize);
1306 memcpy(ptr + num_output * elemsize, pr, num_output * elemsize);
1307 }
1308 }
1309
1310 return 0;
1311}
1312
1313int LSTM_arm::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
1314{
1315 const Mat& bottom_blob = bottom_blobs[0];
1316
1317 int elemtype = 1; // fp32
1318 {
1319 int elembits = bottom_blob.elembits();
1320
1321 // clang-format off
1322 // *INDENT-OFF*
1323
1324#if NCNN_ARM82
1325 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
1326 {
1327 elemtype = 2; // fp16
1328 }
1329 else
1330#endif
1331#if NCNN_BF16
1332 if (opt.use_bf16_storage && elembits == 16)
1333 {
1334 elemtype = 4; // bf16
1335 }
1336 else
1337#endif
1338 {
1339 // fp32
1340 }
1341
1342 // *INDENT-ON*
1343 // clang-format on
1344 }
1345
1346 int T = bottom_blob.h;
1347 size_t elemsize = bottom_blob.elemsize;
1348 int num_directions = direction == 2 ? 2 : 1;
1349
1350 Mat hidden;
1351 Mat cell;
1352 Allocator* hidden_cell_allocator = top_blobs.size() == 3 ? opt.blob_allocator : opt.workspace_allocator;
1353 if (bottom_blobs.size() == 3)
1354 {
1355 if (elemtype == 1)
1356 {
1357 hidden = bottom_blobs[1].clone(hidden_cell_allocator);
1358 cell = bottom_blobs[2].clone(hidden_cell_allocator);
1359 }
1360 if (elemtype == 2)
1361 {
1362 Option opt_cast = opt;
1363 opt_cast.blob_allocator = hidden_cell_allocator;
1364 cast_float16_to_float32(bottom_blobs[1], hidden, opt_cast);
1365 cast_float16_to_float32(bottom_blobs[2], cell, opt_cast);
1366 }
1367 if (elemtype == 4)
1368 {
1369 Option opt_cast = opt;
1370 opt_cast.blob_allocator = hidden_cell_allocator;
1371 cast_bfloat16_to_float32(bottom_blobs[1], hidden, opt_cast);
1372 cast_bfloat16_to_float32(bottom_blobs[2], cell, opt_cast);
1373 }
1374 }
1375 else
1376 {
1377 hidden.create(num_output, num_directions, 4u, hidden_cell_allocator);
1378 if (hidden.empty())
1379 return -100;
1380 hidden.fill(0.f);
1381
1382 cell.create(hidden_size, num_directions, 4u, hidden_cell_allocator);
1383 if (cell.empty())
1384 return -100;
1385 cell.fill(0.f);
1386 }
1387
1388 Mat& top_blob = top_blobs[0];
1389 top_blob.create(num_output * num_directions, T, elemsize, opt.blob_allocator);
1390 if (top_blob.empty())
1391 return -100;
1392
1393 // dynamic quantize bottom_blob
1394 Mat bottom_blob_int8;
1395 Mat bottom_blob_int8_descales;
1396 {
1397 Option opt_quant = opt;
1398 opt_quant.blob_allocator = opt.workspace_allocator;
1399 opt_quant.use_packing_layout = false;
1400 dynamic_quantize(bottom_blob, elemtype, bottom_blob_int8, bottom_blob_int8_descales, opt_quant);
1401 }
1402
1403 // Uni directional
1404 if (direction == 0 || direction == 1)
1405 {
1406 lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt);
1407 }
1408
1409 if (direction == 2)
1410 {
1411 Mat top_blob_forward(num_output, T, elemsize, opt.workspace_allocator);
1412 if (top_blob_forward.empty())
1413 return -100;
1414
1415 Mat top_blob_reverse(num_output, T, elemsize, opt.workspace_allocator);
1416 if (top_blob_reverse.empty())
1417 return -100;
1418
1419 Mat hidden0 = hidden.row_range(0, 1);
1420 Mat cell0 = cell.row_range(0, 1);
1421 {
1422 lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden0, cell0, opt);
1423 }
1424
1425 Mat hidden1 = hidden.row_range(1, 1);
1426 Mat cell1 = cell.row_range(1, 1);
1427 {
1428 lstm_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), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden1, cell1, opt);
1429 }
1430
1431 // concat w
1432 for (int i = 0; i < T; i++)
1433 {
1434 const unsigned char* pf = top_blob_forward.row<const unsigned char>(i);
1435 const unsigned char* pr = top_blob_reverse.row<const unsigned char>(i);
1436 unsigned char* ptr = top_blob.row<unsigned char>(i);
1437
1438 memcpy(ptr, pf, num_output * elemsize);
1439 memcpy(ptr + num_output * elemsize, pr, num_output * elemsize);
1440 }
1441 }
1442
1443 if (top_blobs.size() == 3)
1444 {
1445 if (elemtype == 1)
1446 {
1447 top_blobs[1] = hidden;
1448 top_blobs[2] = cell;
1449 }
1450 if (elemtype == 2)
1451 {
1452 cast_float32_to_float16(hidden, top_blobs[1], opt);
1453 cast_float32_to_float16(cell, top_blobs[2], opt);
1454 }
1455 if (elemtype == 4)
1456 {
1457 cast_float32_to_bfloat16(hidden, top_blobs[1], opt);
1458 cast_float32_to_bfloat16(cell, top_blobs[2], opt);
1459 }
1460 }
1461
1462 return 0;
1463}
1464#endif // NCNN_INT8
1465
1466} // namespace ncnn