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