Elffuss BraidHub

kikocis/ncnn public

master · 33.2 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_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#include "sse_mathfun.h"
20#if __AVX__
21#include <immintrin.h>
22#include "avx_mathfun.h"
23#if __AVX512F__
24#include "avx512_mathfun.h"
25#endif // __AVX512F__
26#endif // __AVX__
27#endif // __SSE2__
28
29#include "x86_activation.h"
30#include "x86_usability.h"
31
32#include "cpu.h"
33
34namespace ncnn {
35
36#include "lstm_int8.h"
37
38LSTM_x86::LSTM_x86()
39{
40 one_blob_only = false;
41 support_inplace = false;
42}
43
44int LSTM_x86::create_pipeline(const Option& opt)
45{
46#if NCNN_INT8
47 if (int8_scale_term)
48 {
49 return create_pipeline_int8(opt);
50 }
51#endif
52
53 // pack IFOG
54 int num_directions = direction == 2 ? 2 : 1;
55 int size = weight_data_size / num_directions / hidden_size / 4;
56
57#if __AVX__
58 weight_xc_data_packed.create(size, hidden_size / 2 + hidden_size % 2, num_directions, 32u, 8);
59 bias_c_data_packed.create(hidden_size, 1, num_directions, 16u, 4);
60 weight_hc_data_packed.create(num_output, hidden_size / 2 + hidden_size % 2, num_directions, 32u, 8);
61#else
62 weight_xc_data_packed.create(size, hidden_size, num_directions, 16u, 4);
63 bias_c_data_packed.create(hidden_size, 1, num_directions, 16u, 4);
64 weight_hc_data_packed.create(num_output, hidden_size, num_directions, 16u, 4);
65#endif
66
67 #pragma omp parallel for num_threads(opt.num_threads)
68 for (int dr = 0; dr < num_directions; dr++)
69 {
70 const Mat weight_xc = weight_xc_data.channel(dr);
71 const Mat bias_c = bias_c_data.channel(dr);
72 const Mat weight_hc = weight_hc_data.channel(dr);
73
74 Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr);
75 Mat bias_c_data_packed_dr = bias_c_data_packed.channel(dr);
76 Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr);
77
78 const float* bias_c_I = bias_c.row(0);
79 const float* bias_c_F = bias_c.row(1);
80 const float* bias_c_O = bias_c.row(2);
81 const float* bias_c_G = bias_c.row(3);
82
83 float* bias_c_IFOG = bias_c_data_packed_dr.row(0);
84
85 int q = 0;
86#if __AVX__
87 for (; q + 1 < hidden_size; q += 2)
88 {
89 bias_c_IFOG[0] = bias_c_I[q];
90 bias_c_IFOG[1] = bias_c_F[q];
91 bias_c_IFOG[2] = bias_c_O[q];
92 bias_c_IFOG[3] = bias_c_G[q];
93 bias_c_IFOG[4] = bias_c_I[q + 1];
94 bias_c_IFOG[5] = bias_c_F[q + 1];
95 bias_c_IFOG[6] = bias_c_O[q + 1];
96 bias_c_IFOG[7] = bias_c_G[q + 1];
97
98 bias_c_IFOG += 8;
99
100 const float* weight_xc_I = weight_xc.row(hidden_size * 0 + q);
101 const float* weight_xc_F = weight_xc.row(hidden_size * 1 + q);
102 const float* weight_xc_O = weight_xc.row(hidden_size * 2 + q);
103 const float* weight_xc_G = weight_xc.row(hidden_size * 3 + q);
104 const float* weight_xc_I_1 = weight_xc.row(hidden_size * 0 + q + 1);
105 const float* weight_xc_F_1 = weight_xc.row(hidden_size * 1 + q + 1);
106 const float* weight_xc_O_1 = weight_xc.row(hidden_size * 2 + q + 1);
107 const float* weight_xc_G_1 = weight_xc.row(hidden_size * 3 + q + 1);
108
109 const float* weight_hc_I = weight_hc.row(hidden_size * 0 + q);
110 const float* weight_hc_F = weight_hc.row(hidden_size * 1 + q);
111 const float* weight_hc_O = weight_hc.row(hidden_size * 2 + q);
112 const float* weight_hc_G = weight_hc.row(hidden_size * 3 + q);
113 const float* weight_hc_I_1 = weight_hc.row(hidden_size * 0 + q + 1);
114 const float* weight_hc_F_1 = weight_hc.row(hidden_size * 1 + q + 1);
115 const float* weight_hc_O_1 = weight_hc.row(hidden_size * 2 + q + 1);
116 const float* weight_hc_G_1 = weight_hc.row(hidden_size * 3 + q + 1);
117
118 float* weight_xc_IFOG = weight_xc_data_packed_dr.row(q / 2);
119 float* weight_hc_IFOG = weight_hc_data_packed_dr.row(q / 2);
120
121 for (int i = 0; i < size; i++)
122 {
123 weight_xc_IFOG[0] = weight_xc_I[i];
124 weight_xc_IFOG[1] = weight_xc_F[i];
125 weight_xc_IFOG[2] = weight_xc_O[i];
126 weight_xc_IFOG[3] = weight_xc_G[i];
127 weight_xc_IFOG[4] = weight_xc_I_1[i];
128 weight_xc_IFOG[5] = weight_xc_F_1[i];
129 weight_xc_IFOG[6] = weight_xc_O_1[i];
130 weight_xc_IFOG[7] = weight_xc_G_1[i];
131
132 weight_xc_IFOG += 8;
133 }
134
135 for (int i = 0; i < num_output; i++)
136 {
137 weight_hc_IFOG[0] = weight_hc_I[i];
138 weight_hc_IFOG[1] = weight_hc_F[i];
139 weight_hc_IFOG[2] = weight_hc_O[i];
140 weight_hc_IFOG[3] = weight_hc_G[i];
141 weight_hc_IFOG[4] = weight_hc_I_1[i];
142 weight_hc_IFOG[5] = weight_hc_F_1[i];
143 weight_hc_IFOG[6] = weight_hc_O_1[i];
144 weight_hc_IFOG[7] = weight_hc_G_1[i];
145
146 weight_hc_IFOG += 8;
147 }
148 }
149#endif // __AVX__
150 for (; q < hidden_size; q++)
151 {
152 bias_c_IFOG[0] = bias_c_I[q];
153 bias_c_IFOG[1] = bias_c_F[q];
154 bias_c_IFOG[2] = bias_c_O[q];
155 bias_c_IFOG[3] = bias_c_G[q];
156
157 bias_c_IFOG += 4;
158
159 const float* weight_xc_I = weight_xc.row(hidden_size * 0 + q);
160 const float* weight_xc_F = weight_xc.row(hidden_size * 1 + q);
161 const float* weight_xc_O = weight_xc.row(hidden_size * 2 + q);
162 const float* weight_xc_G = weight_xc.row(hidden_size * 3 + q);
163
164 const float* weight_hc_I = weight_hc.row(hidden_size * 0 + q);
165 const float* weight_hc_F = weight_hc.row(hidden_size * 1 + q);
166 const float* weight_hc_O = weight_hc.row(hidden_size * 2 + q);
167 const float* weight_hc_G = weight_hc.row(hidden_size * 3 + q);
168
169#if __AVX__
170 float* weight_xc_IFOG = weight_xc_data_packed_dr.row(q / 2 + q % 2);
171 float* weight_hc_IFOG = weight_hc_data_packed_dr.row(q / 2 + q % 2);
172#else
173 float* weight_xc_IFOG = weight_xc_data_packed_dr.row(q);
174 float* weight_hc_IFOG = weight_hc_data_packed_dr.row(q);
175#endif
176
177 for (int i = 0; i < size; i++)
178 {
179 weight_xc_IFOG[0] = weight_xc_I[i];
180 weight_xc_IFOG[1] = weight_xc_F[i];
181 weight_xc_IFOG[2] = weight_xc_O[i];
182 weight_xc_IFOG[3] = weight_xc_G[i];
183
184 weight_xc_IFOG += 4;
185 }
186
187 for (int i = 0; i < num_output; i++)
188 {
189 weight_hc_IFOG[0] = weight_hc_I[i];
190 weight_hc_IFOG[1] = weight_hc_F[i];
191 weight_hc_IFOG[2] = weight_hc_O[i];
192 weight_hc_IFOG[3] = weight_hc_G[i];
193
194 weight_hc_IFOG += 4;
195 }
196 }
197 }
198
199 if (opt.lightmode)
200 {
201 weight_xc_data.release();
202 bias_c_data.release();
203 weight_hc_data.release();
204 }
205
206 return 0;
207}
208
209static 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)
210{
211 int size = bottom_blob.w;
212 int T = bottom_blob.h;
213
214 int num_output = top_blob.w;
215 int hidden_size = cell_state.w;
216
217 // 4 x hidden_size
218 Mat gates(4, hidden_size, 4u, opt.workspace_allocator);
219 if (gates.empty())
220 return -100;
221
222 Mat tmp_hidden_state;
223 if (num_output != hidden_size)
224 {
225 tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator);
226 if (tmp_hidden_state.empty())
227 return -100;
228 }
229
230 // unroll
231 for (int t = 0; t < T; t++)
232 {
233 // clip hidden by continuation indicator
234 // h_cont_{t-1} = cont_t * h_{t-1}
235 // h_cont_{t-1} = h_{t-1} if cont_t == 1
236 // 0 otherwise
237 // calculate hidden
238 // gate_input_t := W_hc * h_conted_{t-1} + W_xc * x_t + b_c
239
240 int ti = reverse ? T - 1 - t : t;
241
242#if __AVX__
243 int nn_hidden_size = hidden_size >> 1;
244 int remain_hidden_size_start = nn_hidden_size << 1;
245 #pragma omp parallel for num_threads(opt.num_threads)
246 for (int qq = 0; qq < nn_hidden_size; qq++)
247 {
248 int q = qq * 2;
249
250 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
251
252 // gate I F O G
253 const float* weight_xc_IFOG = weight_xc.row(q / 2);
254 const float* weight_hc_IFOG = weight_hc.row(q / 2);
255
256 __m256 _IFOG = _mm256_loadu_ps(bias_c_IFOG);
257 __m256 _sum1 = _mm256_setzero_ps();
258 __m256 _sum2 = _mm256_setzero_ps();
259 __m256 _sum3 = _mm256_setzero_ps();
260
261 const float* x = bottom_blob.row(ti);
262
263 int i = 0;
264 for (; i + 3 < size; i += 4)
265 {
266 __m256 _xi0 = _mm256_broadcast_ss(x);
267 __m256 _xi1 = _mm256_broadcast_ss(x + 1);
268 __m256 _xi2 = _mm256_broadcast_ss(x + 2);
269 __m256 _xi3 = _mm256_broadcast_ss(x + 3);
270 __m256 _weight_xc_IFOG0 = _mm256_loadu_ps(weight_xc_IFOG);
271 __m256 _weight_xc_IFOG1 = _mm256_loadu_ps(weight_xc_IFOG + 8);
272 __m256 _weight_xc_IFOG2 = _mm256_loadu_ps(weight_xc_IFOG + 16);
273 __m256 _weight_xc_IFOG3 = _mm256_loadu_ps(weight_xc_IFOG + 24);
274 _IFOG = _mm256_comp_fmadd_ps(_weight_xc_IFOG0, _xi0, _IFOG);
275 _sum1 = _mm256_comp_fmadd_ps(_weight_xc_IFOG1, _xi1, _sum1);
276 _sum2 = _mm256_comp_fmadd_ps(_weight_xc_IFOG2, _xi2, _sum2);
277 _sum3 = _mm256_comp_fmadd_ps(_weight_xc_IFOG3, _xi3, _sum3);
278
279 x += 4;
280 weight_xc_IFOG += 32;
281 }
282 for (; i < size; i++)
283 {
284 __m256 _xi = _mm256_broadcast_ss(x);
285 __m256 _weight_xc_IFOG = _mm256_loadu_ps(weight_xc_IFOG);
286 _IFOG = _mm256_comp_fmadd_ps(_weight_xc_IFOG, _xi, _IFOG);
287
288 x += 1;
289 weight_xc_IFOG += 8;
290 }
291
292 const float* hidden_ptr = hidden_state;
293
294 i = 0;
295 for (; i + 3 < num_output; i += 4)
296 {
297 __m256 _h_cont0 = _mm256_broadcast_ss(hidden_ptr);
298 __m256 _h_cont1 = _mm256_broadcast_ss(hidden_ptr + 1);
299 __m256 _h_cont2 = _mm256_broadcast_ss(hidden_ptr + 2);
300 __m256 _h_cont3 = _mm256_broadcast_ss(hidden_ptr + 3);
301 __m256 _weight_hc_IFOG0 = _mm256_loadu_ps(weight_hc_IFOG);
302 __m256 _weight_hc_IFOG1 = _mm256_loadu_ps(weight_hc_IFOG + 8);
303 __m256 _weight_hc_IFOG2 = _mm256_loadu_ps(weight_hc_IFOG + 16);
304 __m256 _weight_hc_IFOG3 = _mm256_loadu_ps(weight_hc_IFOG + 24);
305 _IFOG = _mm256_comp_fmadd_ps(_weight_hc_IFOG0, _h_cont0, _IFOG);
306 _sum1 = _mm256_comp_fmadd_ps(_weight_hc_IFOG1, _h_cont1, _sum1);
307 _sum2 = _mm256_comp_fmadd_ps(_weight_hc_IFOG2, _h_cont2, _sum2);
308 _sum3 = _mm256_comp_fmadd_ps(_weight_hc_IFOG3, _h_cont3, _sum3);
309
310 hidden_ptr += 4;
311 weight_hc_IFOG += 32;
312 }
313 for (; i < num_output; i++)
314 {
315 __m256 _h_cont = _mm256_broadcast_ss(hidden_ptr);
316 __m256 _weight_hc_IFOG = _mm256_loadu_ps(weight_hc_IFOG);
317 _IFOG = _mm256_comp_fmadd_ps(_weight_hc_IFOG, _h_cont, _IFOG);
318
319 hidden_ptr += 1;
320 weight_hc_IFOG += 8;
321 }
322
323 float* gates_data = gates.row(q);
324
325 _IFOG = _mm256_add_ps(_IFOG, _sum1);
326 _sum2 = _mm256_add_ps(_sum2, _sum3);
327 _IFOG = _mm256_add_ps(_IFOG, _sum2);
328
329 _mm256_storeu_ps(gates_data, _IFOG);
330 }
331#else
332 int nn_hidden_size = 0;
333 int remain_hidden_size_start = 0;
334#endif // __AVX__
335
336 #pragma omp parallel for num_threads(opt.num_threads)
337 for (int q = remain_hidden_size_start; q < hidden_size; q++)
338 {
339 const float* bias_c_IFOG = (const float*)bias_c + q * 4;
340
341 // gate I F O G
342#if __AVX__
343 const float* weight_xc_IFOG = weight_xc.row(q / 2 + q % 2);
344 const float* weight_hc_IFOG = weight_hc.row(q / 2 + q % 2);
345#else
346 const float* weight_xc_IFOG = weight_xc.row(q);
347 const float* weight_hc_IFOG = weight_hc.row(q);
348#endif
349
350#if __SSE2__
351 __m128 _IFOG = _mm_loadu_ps(bias_c_IFOG);
352 __m128 _sum1 = _mm_setzero_ps();
353 __m128 _sum2 = _mm_setzero_ps();
354 __m128 _sum3 = _mm_setzero_ps();
355#else // __SSE2__
356 float I = bias_c_IFOG[0];
357 float F = bias_c_IFOG[1];
358 float O = bias_c_IFOG[2];
359 float G = bias_c_IFOG[3];
360#endif // __SSE2__
361
362 const float* x = bottom_blob.row(ti);
363
364 int i = 0;
365#if __SSE2__
366 for (; i + 3 < size; i += 4)
367 {
368 __m128 _xi0 = _mm_load1_ps(x);
369 __m128 _xi1 = _mm_load1_ps(x + 1);
370 __m128 _xi2 = _mm_load1_ps(x + 2);
371 __m128 _xi3 = _mm_load1_ps(x + 3);
372 __m128 _weight_xc_IFOG0 = _mm_loadu_ps(weight_xc_IFOG);
373 __m128 _weight_xc_IFOG1 = _mm_loadu_ps(weight_xc_IFOG + 4);
374 __m128 _weight_xc_IFOG2 = _mm_loadu_ps(weight_xc_IFOG + 8);
375 __m128 _weight_xc_IFOG3 = _mm_loadu_ps(weight_xc_IFOG + 12);
376 _IFOG = _mm_comp_fmadd_ps(_weight_xc_IFOG0, _xi0, _IFOG);
377 _sum1 = _mm_comp_fmadd_ps(_weight_xc_IFOG1, _xi1, _sum1);
378 _sum2 = _mm_comp_fmadd_ps(_weight_xc_IFOG2, _xi2, _sum2);
379 _sum3 = _mm_comp_fmadd_ps(_weight_xc_IFOG3, _xi3, _sum3);
380
381 x += 4;
382 weight_xc_IFOG += 16;
383 }
384#endif // __SSE2__
385 for (; i < size; i++)
386 {
387#if __SSE2__
388 __m128 _xi = _mm_load1_ps(x);
389 __m128 _weight_xc_IFOG = _mm_loadu_ps(weight_xc_IFOG);
390 _IFOG = _mm_comp_fmadd_ps(_weight_xc_IFOG, _xi, _IFOG);
391#else // __SSE2__
392 float xi = x[0];
393 I += xi * weight_xc_IFOG[0];
394 F += xi * weight_xc_IFOG[1];
395 O += xi * weight_xc_IFOG[2];
396 G += xi * weight_xc_IFOG[3];
397#endif // __SSE2__
398
399 x += 1;
400 weight_xc_IFOG += 4;
401 }
402
403 const float* hidden_ptr = hidden_state;
404
405 i = 0;
406#if __SSE2__
407 for (; i + 3 < num_output; i += 4)
408 {
409 __m128 _h_cont0 = _mm_load1_ps(hidden_ptr);
410 __m128 _h_cont1 = _mm_load1_ps(hidden_ptr + 1);
411 __m128 _h_cont2 = _mm_load1_ps(hidden_ptr + 2);
412 __m128 _h_cont3 = _mm_load1_ps(hidden_ptr + 3);
413 __m128 _weight_hc_IFOG0 = _mm_loadu_ps(weight_hc_IFOG);
414 __m128 _weight_hc_IFOG1 = _mm_loadu_ps(weight_hc_IFOG + 4);
415 __m128 _weight_hc_IFOG2 = _mm_loadu_ps(weight_hc_IFOG + 8);
416 __m128 _weight_hc_IFOG3 = _mm_loadu_ps(weight_hc_IFOG + 12);
417 _IFOG = _mm_comp_fmadd_ps(_weight_hc_IFOG0, _h_cont0, _IFOG);
418 _sum1 = _mm_comp_fmadd_ps(_weight_hc_IFOG1, _h_cont1, _sum1);
419 _sum2 = _mm_comp_fmadd_ps(_weight_hc_IFOG2, _h_cont2, _sum2);
420 _sum3 = _mm_comp_fmadd_ps(_weight_hc_IFOG3, _h_cont3, _sum3);
421
422 hidden_ptr += 4;
423 weight_hc_IFOG += 16;
424 }
425#endif // __SSE2__
426 for (; i < num_output; i++)
427 {
428#if __SSE2__
429 __m128 _h_cont = _mm_load1_ps(hidden_ptr);
430 __m128 _weight_hc_IFOG = _mm_loadu_ps(weight_hc_IFOG);
431 _IFOG = _mm_comp_fmadd_ps(_weight_hc_IFOG, _h_cont, _IFOG);
432#else // __SSE2__
433 float h_cont = hidden_ptr[0];
434 I += h_cont * weight_hc_IFOG[0];
435 F += h_cont * weight_hc_IFOG[1];
436 O += h_cont * weight_hc_IFOG[2];
437 G += h_cont * weight_hc_IFOG[3];
438#endif // __SSE2__
439
440 hidden_ptr += 1;
441 weight_hc_IFOG += 4;
442 }
443
444 float* gates_data = gates.row(q);
445
446#if __SSE2__
447 _IFOG = _mm_add_ps(_IFOG, _sum1);
448 _sum2 = _mm_add_ps(_sum2, _sum3);
449 _IFOG = _mm_add_ps(_IFOG, _sum2);
450
451 _mm_storeu_ps(gates_data, _IFOG);
452#else // __SSE2__
453 gates_data[0] = I;
454 gates_data[1] = F;
455 gates_data[2] = O;
456 gates_data[3] = G;
457#endif // __SSE2__
458 }
459
460 // lstm unit
461 // sigmoid(I)
462 // sigmoid(F)
463 // sigmoid(O)
464 // tanh(G)
465 // c_t := f_t .* c_{t-1} + i_t .* g_t
466 // h_t := o_t .* tanh[c_t]
467 float* output_data = top_blob.row(ti);
468
469 float* cell_ptr = cell_state;
470 float* hidden_ptr = hidden_state;
471 float* tmp_hidden_ptr = tmp_hidden_state;
472
473#if __SSE2__
474 nn_hidden_size = hidden_size >> 2;
475 remain_hidden_size_start = nn_hidden_size << 2;
476 #pragma omp parallel for num_threads(opt.num_threads)
477 for (int qq = 0; qq < nn_hidden_size; qq++)
478 {
479 int q = qq * 4;
480
481 const float* gates_data = gates.row(q);
482
483 __m128 _IFOG_4x4_0 = _mm_loadu_ps(gates_data);
484 __m128 _IFOG_4x4_1 = _mm_loadu_ps(gates_data + 4);
485 __m128 _IFOG_4x4_2 = _mm_loadu_ps(gates_data + 8);
486 __m128 _IFOG_4x4_3 = _mm_loadu_ps(gates_data + 12);
487
488 _MM_TRANSPOSE4_PS(_IFOG_4x4_0, _IFOG_4x4_1, _IFOG_4x4_2, _IFOG_4x4_3);
489
490 __m128 _lstm_I = sigmoid_sse(_IFOG_4x4_0);
491 __m128 _lstm_F = sigmoid_sse(_IFOG_4x4_1);
492 __m128 _lstm_O = sigmoid_sse(_IFOG_4x4_2);
493 __m128 _lstm_G = tanh_sse(_IFOG_4x4_3);
494
495 __m128 _cell2 = _mm_add_ps(_mm_mul_ps(_lstm_F, _mm_loadu_ps(cell_ptr + q)), _mm_mul_ps(_lstm_I, _lstm_G));
496 __m128 _lstm_H = _mm_mul_ps(_lstm_O, tanh_sse(_cell2));
497
498 _mm_storeu_ps(cell_ptr + q, _cell2);
499
500 if (num_output == hidden_size)
501 {
502 _mm_storeu_ps(hidden_ptr + q, _lstm_H);
503 _mm_storeu_ps(output_data + q, _lstm_H);
504 }
505 else
506 {
507 _mm_storeu_ps(tmp_hidden_ptr + q, _lstm_H);
508 }
509 }
510#else // __SSE2__
511 remain_hidden_size_start = 0;
512#endif // __SSE2__
513 #pragma omp parallel for num_threads(opt.num_threads)
514 for (int q = remain_hidden_size_start; q < hidden_size; q++)
515 {
516 const float* gates_data = gates.row(q);
517
518 float I = gates_data[0];
519 float F = gates_data[1];
520 float O = gates_data[2];
521 float G = gates_data[3];
522
523 I = 1.f / (1.f + expf(-I));
524 F = 1.f / (1.f + expf(-F));
525 O = 1.f / (1.f + expf(-O));
526 G = tanhf(G);
527
528 float cell2 = F * cell_ptr[q] + I * G;
529 float H = O * tanhf(cell2);
530
531 cell_ptr[q] = cell2;
532 if (num_output == hidden_size)
533 {
534 hidden_ptr[q] = H;
535 output_data[q] = H;
536 }
537 else
538 {
539 tmp_hidden_ptr[q] = H;
540 }
541 }
542
543 if (num_output != hidden_size)
544 {
545 // int nn_num_output = num_output >> 2;
546 // int remain_num_output_start = nn_num_output << 2;
547 // #pragma omp parallel for num_threads(opt.num_threads)
548 // for (int qq = 0; qq < nn_num_output; qq++)
549 // {
550 // int q = qq * 4;
551 //
552 // }
553 int remain_num_output_start = 0;
554 #pragma omp parallel for num_threads(opt.num_threads)
555 for (int q = remain_num_output_start; q < num_output; q++)
556 {
557 const float* hr = weight_hr.row(q);
558 const float* tmp_hidden_ptr = tmp_hidden_state;
559
560 float H = 0;
561 for (int i = 0; i < hidden_size; i++)
562 {
563 H += tmp_hidden_ptr[i] * hr[i];
564 }
565
566 output_data[q] = H;
567 hidden_ptr[q] = H;
568 }
569 }
570 }
571
572 return 0;
573}
574
575int LSTM_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
576{
577#if NCNN_INT8
578 if (int8_scale_term)
579 {
580 return forward_int8(bottom_blob, top_blob, opt);
581 }
582#endif
583
584 int T = bottom_blob.h;
585
586 int num_directions = direction == 2 ? 2 : 1;
587
588 // initial hidden state
589 Mat hidden(num_output, 4u, opt.workspace_allocator);
590 if (hidden.empty())
591 return -100;
592 hidden.fill(0.f);
593
594 Mat cell(hidden_size, 4u, opt.workspace_allocator);
595 if (cell.empty())
596 return -100;
597 cell.fill(0.f);
598
599 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
600 if (top_blob.empty())
601 return -100;
602
603 // Uni directional
604 if (direction == 0 || direction == 1)
605 {
606 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);
607 if (ret != 0)
608 return ret;
609 }
610
611 if (direction == 2)
612 {
613 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
614 if (top_blob_forward.empty())
615 return -100;
616
617 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
618 if (top_blob_reverse.empty())
619 return -100;
620
621 {
622 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);
623 if (ret != 0)
624 return ret;
625 }
626
627 hidden.fill(0.0f);
628 cell.fill(0.0f);
629
630 {
631 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);
632 if (ret != 0)
633 return ret;
634 }
635
636 // concat w
637 for (int i = 0; i < T; i++)
638 {
639 const float* pf = top_blob_forward.row(i);
640 const float* pr = top_blob_reverse.row(i);
641 float* ptr = top_blob.row(i);
642
643 memcpy(ptr, pf, num_output * sizeof(float));
644 memcpy(ptr + num_output, pr, num_output * sizeof(float));
645 }
646 }
647
648 return 0;
649}
650
651int LSTM_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
652{
653#if NCNN_INT8
654 if (int8_scale_term)
655 {
656 return forward_int8(bottom_blobs, top_blobs, opt);
657 }
658#endif
659
660 const Mat& bottom_blob = bottom_blobs[0];
661 int T = bottom_blob.h;
662 int num_directions = direction == 2 ? 2 : 1;
663
664 Mat hidden;
665 Mat cell;
666 Allocator* hidden_cell_allocator = top_blobs.size() == 3 ? opt.blob_allocator : opt.workspace_allocator;
667 if (bottom_blobs.size() == 3)
668 {
669 hidden = bottom_blobs[1].clone(hidden_cell_allocator);
670 cell = bottom_blobs[2].clone(hidden_cell_allocator);
671 }
672 else
673 {
674 hidden.create(num_output, num_directions, 4u, hidden_cell_allocator);
675 if (hidden.empty())
676 return -100;
677 hidden.fill(0.f);
678
679 cell.create(hidden_size, num_directions, 4u, hidden_cell_allocator);
680 if (cell.empty())
681 return -100;
682 cell.fill(0.f);
683 }
684
685 Mat& top_blob = top_blobs[0];
686 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
687 if (top_blob.empty())
688 return -100;
689
690 // Uni directional
691 if (direction == 0 || direction == 1)
692 {
693 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);
694 if (ret != 0)
695 return ret;
696 }
697
698 if (direction == 2)
699 {
700 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
701 if (top_blob_forward.empty())
702 return -100;
703
704 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
705 if (top_blob_reverse.empty())
706 return -100;
707
708 Mat hidden0 = hidden.row_range(0, 1);
709 Mat cell0 = cell.row_range(0, 1);
710 {
711 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);
712 if (ret != 0)
713 return ret;
714 }
715
716 Mat hidden1 = hidden.row_range(1, 1);
717 Mat cell1 = cell.row_range(1, 1);
718 {
719 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);
720 if (ret != 0)
721 return ret;
722 }
723
724 // concat w
725 for (int i = 0; i < T; i++)
726 {
727 const float* pf = top_blob_forward.row(i);
728 const float* pr = top_blob_reverse.row(i);
729 float* ptr = top_blob.row(i);
730
731 memcpy(ptr, pf, num_output * sizeof(float));
732 memcpy(ptr + num_output, pr, num_output * sizeof(float));
733 }
734 }
735
736 if (top_blobs.size() == 3)
737 {
738 top_blobs[1] = hidden;
739 top_blobs[2] = cell;
740 }
741
742 return 0;
743}
744
745#if NCNN_INT8
746int LSTM_x86::create_pipeline_int8(const Option& opt)
747{
748 // pack IFOG
749 const int num_directions = direction == 2 ? 2 : 1;
750 const int size = weight_data_size / num_directions / hidden_size / 4;
751
752 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);
753
754 if (opt.lightmode)
755 {
756 weight_xc_data.release();
757 bias_c_data.release();
758 weight_hc_data.release();
759 weight_xc_data_int8_scales.release();
760 weight_hc_data_int8_scales.release();
761 }
762
763 return 0;
764}
765
766static void lstm_dynamic_quantize(const Mat& bottom_blob, Mat& bottom_blob_int8, Mat& bottom_blob_int8_descales, const Option& opt)
767{
768 int size = bottom_blob.w;
769 int T = bottom_blob.h;
770
771 // dynamic quantize bottom_blob
772 bottom_blob_int8_descales.create(T, (size_t)4u, 1, opt.blob_allocator);
773
774 bottom_blob_int8.create(size, T, (size_t)1u, opt.blob_allocator);
775
776 // fp32
777 for (int t = 0; t < T; t++)
778 {
779 const float* ptr = bottom_blob.row(t);
780 signed char* outptr = bottom_blob_int8.row<signed char>(t);
781
782 const float absmax = lstm_dynamic_quantize_get_absmax(ptr, size);
783
784 bottom_blob_int8_descales[t] = absmax / 127.f;
785
786 const float scale = 127.f / absmax;
787 lstm_dynamic_quantize_scale2int8(ptr, size, scale, outptr);
788 }
789}
790
791int LSTM_x86::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
792{
793 int T = bottom_blob.h;
794
795 int num_directions = direction == 2 ? 2 : 1;
796
797 // initial hidden state
798 Mat hidden(num_output, 4u, opt.workspace_allocator);
799 if (hidden.empty())
800 return -100;
801 hidden.fill(0.f);
802
803 Mat cell(hidden_size, 4u, opt.workspace_allocator);
804 if (cell.empty())
805 return -100;
806 cell.fill(0.f);
807
808 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
809 if (top_blob.empty())
810 return -100;
811
812 // dynamic quantize bottom_blob
813 Mat bottom_blob_int8;
814 Mat bottom_blob_int8_descales;
815 {
816 Option opt_quant = opt;
817 opt_quant.blob_allocator = opt.workspace_allocator;
818 opt_quant.use_packing_layout = false;
819 lstm_dynamic_quantize(bottom_blob, bottom_blob_int8, bottom_blob_int8_descales, opt_quant);
820 }
821
822 // Uni directional
823 if (direction == 0 || direction == 1)
824 {
825 lstm_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob, 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);
826 }
827
828 if (direction == 2)
829 {
830 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
831 if (top_blob_forward.empty())
832 return -100;
833
834 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
835 if (top_blob_reverse.empty())
836 return -100;
837
838 {
839 lstm_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_forward, 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);
840 }
841
842 hidden.fill(0.f);
843 cell.fill(0.0f);
844
845 {
846 lstm_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_reverse, 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);
847 }
848
849 // concat w
850 for (int i = 0; i < T; i++)
851 {
852 const float* pf = top_blob_forward.row(i);
853 const float* pr = top_blob_reverse.row(i);
854 float* ptr = top_blob.row(i);
855
856 memcpy(ptr, pf, num_output * sizeof(float));
857 memcpy(ptr + num_output, pr, num_output * sizeof(float));
858 }
859 }
860
861 return 0;
862}
863
864int LSTM_x86::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
865{
866 const Mat& bottom_blob = bottom_blobs[0];
867
868 int T = bottom_blob.h;
869 int num_directions = direction == 2 ? 2 : 1;
870
871 Mat hidden;
872 Mat cell;
873 Allocator* hidden_cell_allocator = top_blobs.size() == 3 ? opt.blob_allocator : opt.workspace_allocator;
874 if (bottom_blobs.size() == 3)
875 {
876 hidden = bottom_blobs[1].clone(hidden_cell_allocator);
877 cell = bottom_blobs[2].clone(hidden_cell_allocator);
878 }
879 else
880 {
881 hidden.create(num_output, num_directions, 4u, hidden_cell_allocator);
882 if (hidden.empty())
883 return -100;
884 hidden.fill(0.f);
885
886 cell.create(hidden_size, num_directions, 4u, hidden_cell_allocator);
887 if (cell.empty())
888 return -100;
889 cell.fill(0.f);
890 }
891
892 Mat& top_blob = top_blobs[0];
893 top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator);
894 if (top_blob.empty())
895 return -100;
896
897 // dynamic quantize bottom_blob
898 Mat bottom_blob_int8;
899 Mat bottom_blob_int8_descales;
900 {
901 Option opt_quant = opt;
902 opt_quant.blob_allocator = opt.workspace_allocator;
903 opt_quant.use_packing_layout = false;
904 lstm_dynamic_quantize(bottom_blob, bottom_blob_int8, bottom_blob_int8_descales, opt_quant);
905 }
906
907 // Uni directional
908 if (direction == 0 || direction == 1)
909 {
910 lstm_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob, 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);
911 }
912
913 if (direction == 2)
914 {
915 Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator);
916 if (top_blob_forward.empty())
917 return -100;
918
919 Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator);
920 if (top_blob_reverse.empty())
921 return -100;
922
923 Mat hidden0 = hidden.row_range(0, 1);
924 Mat cell0 = cell.row_range(0, 1);
925 {
926 lstm_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_forward, 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);
927 }
928
929 Mat hidden1 = hidden.row_range(1, 1);
930 Mat cell1 = cell.row_range(1, 1);
931 {
932 lstm_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_reverse, 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);
933 }
934
935 // concat w
936 for (int i = 0; i < T; i++)
937 {
938 const float* pf = top_blob_forward.row(i);
939 const float* pr = top_blob_reverse.row(i);
940 float* ptr = top_blob.row(i);
941
942 memcpy(ptr, pf, num_output * sizeof(float));
943 memcpy(ptr + num_output, pr, num_output * sizeof(float));
944 }
945 }
946
947 if (top_blobs.size() == 3)
948 {
949 top_blobs[1] = hidden;
950 top_blobs[2] = cell;
951 }
952
953 return 0;
954}
955#endif // NCNN_INT8
956
957} // namespace ncnn