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