| 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 "rnn_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 rnn_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 | // num_output |
| 34 | Mat gates(num_output, 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 | const __fp16* x = bottom_blob.row<const __fp16>(ti); |
| 44 | |
| 45 | int nn_num_output = num_output >> 3; |
| 46 | int remain_num_output_start = nn_num_output << 3; |
| 47 | #pragma omp parallel for num_threads(opt.num_threads) |
| 48 | for (int qq = 0; qq < nn_num_output; qq++) |
| 49 | { |
| 50 | int q = qq * 8; |
| 51 | |
| 52 | const __fp16* weight_xc_ptr = weight_xc.row<const __fp16>(q / 8); |
| 53 | const __fp16* weight_hc_ptr = weight_hc.row<const __fp16>(q / 8); |
| 54 | |
| 55 | float16x8_t _rnn_H = vld1q_f16((const __fp16*)bias_c + q); |
| 56 | float16x8_t _sum1 = vdupq_n_f16(0.f); |
| 57 | float16x8_t _sum2 = vdupq_n_f16(0.f); |
| 58 | float16x8_t _sum3 = vdupq_n_f16(0.f); |
| 59 | |
| 60 | int i = 0; |
| 61 | for (; i + 3 < size; i += 4) |
| 62 | { |
| 63 | float16x4_t _x = vld1_f16(x + i); |
| 64 | float16x8_t _weight_xc = vld1q_f16(weight_xc_ptr); |
| 65 | float16x8_t _weight_xc_1 = vld1q_f16(weight_xc_ptr + 8); |
| 66 | float16x8_t _weight_xc_2 = vld1q_f16(weight_xc_ptr + 16); |
| 67 | float16x8_t _weight_xc_3 = vld1q_f16(weight_xc_ptr + 24); |
| 68 | _rnn_H = vfmaq_lane_f16(_rnn_H, _weight_xc, _x, 0); |
| 69 | _sum1 = vfmaq_lane_f16(_sum1, _weight_xc_1, _x, 1); |
| 70 | _sum2 = vfmaq_lane_f16(_sum2, _weight_xc_2, _x, 2); |
| 71 | _sum3 = vfmaq_lane_f16(_sum3, _weight_xc_3, _x, 3); |
| 72 | |
| 73 | weight_xc_ptr += 32; |
| 74 | } |
| 75 | for (; i < size; i++) |
| 76 | { |
| 77 | float16x8_t _x = vdupq_n_f16(x[i]); |
| 78 | float16x8_t _weight_xc = vld1q_f16(weight_xc_ptr); |
| 79 | _rnn_H = vfmaq_f16(_rnn_H, _weight_xc, _x); |
| 80 | |
| 81 | weight_xc_ptr += 8; |
| 82 | } |
| 83 | |
| 84 | i = 0; |
| 85 | for (; i + 3 < num_output; i += 4) |
| 86 | { |
| 87 | float16x4_t _hidden_state = vcvt_f16_f32(vld1q_f32((const float*)hidden_state + i)); |
| 88 | float16x8_t _weight_hc = vld1q_f16(weight_hc_ptr); |
| 89 | float16x8_t _weight_hc_1 = vld1q_f16(weight_hc_ptr + 8); |
| 90 | float16x8_t _weight_hc_2 = vld1q_f16(weight_hc_ptr + 16); |
| 91 | float16x8_t _weight_hc_3 = vld1q_f16(weight_hc_ptr + 24); |
| 92 | _rnn_H = vfmaq_lane_f16(_rnn_H, _weight_hc, _hidden_state, 0); |
| 93 | _sum1 = vfmaq_lane_f16(_sum1, _weight_hc_1, _hidden_state, 1); |
| 94 | _sum2 = vfmaq_lane_f16(_sum2, _weight_hc_2, _hidden_state, 2); |
| 95 | _sum3 = vfmaq_lane_f16(_sum3, _weight_hc_3, _hidden_state, 3); |
| 96 | |
| 97 | weight_hc_ptr += 32; |
| 98 | } |
| 99 | for (; i < num_output; i++) |
| 100 | { |
| 101 | float16x8_t _hidden_state = vdupq_n_f16((__fp16)hidden_state[i]); |
| 102 | float16x8_t _weight_hc = vld1q_f16(weight_hc_ptr); |
| 103 | _rnn_H = vfmaq_f16(_rnn_H, _weight_hc, _hidden_state); |
| 104 | |
| 105 | weight_hc_ptr += 8; |
| 106 | } |
| 107 | |
| 108 | _rnn_H = vaddq_f16(_rnn_H, _sum1); |
| 109 | _sum2 = vaddq_f16(_sum2, _sum3); |
| 110 | _rnn_H = vaddq_f16(_rnn_H, _sum2); |
| 111 | |
| 112 | float32x4_t _H32low = tanh_ps(vcvt_f32_f16(vget_low_f16(_rnn_H))); |
| 113 | float32x4_t _H32high = tanh_ps(vcvt_f32_f16(vget_high_f16(_rnn_H))); |
| 114 | |
| 115 | vst1q_f32((float*)gates + q, _H32low); |
| 116 | vst1q_f32((float*)gates + q + 4, _H32high); |
| 117 | } |
| 118 | nn_num_output = (num_output - remain_num_output_start) >> 2; |
| 119 | #pragma omp parallel for num_threads(opt.num_threads) |
| 120 | for (int qq = 0; qq < nn_num_output; qq++) |
| 121 | { |
| 122 | int q = remain_num_output_start + qq * 4; |
| 123 | |
| 124 | const __fp16* weight_xc_ptr = weight_xc.row<const __fp16>(q / 8 + (q % 8) / 4); |
| 125 | const __fp16* weight_hc_ptr = weight_hc.row<const __fp16>(q / 8 + (q % 8) / 4); |
| 126 | |
| 127 | float16x4_t _rnn_H = vld1_f16((const __fp16*)bias_c + q); |
| 128 | float16x4_t _sum1 = vdup_n_f16(0.f); |
| 129 | float16x4_t _sum2 = vdup_n_f16(0.f); |
| 130 | float16x4_t _sum3 = vdup_n_f16(0.f); |
| 131 | |
| 132 | int i = 0; |
| 133 | for (; i + 3 < size; i += 4) |
| 134 | { |
| 135 | float16x4_t _x = vld1_f16(x + i); |
| 136 | float16x4_t _weight_xc = vld1_f16(weight_xc_ptr); |
| 137 | float16x4_t _weight_xc_1 = vld1_f16(weight_xc_ptr + 4); |
| 138 | float16x4_t _weight_xc_2 = vld1_f16(weight_xc_ptr + 8); |
| 139 | float16x4_t _weight_xc_3 = vld1_f16(weight_xc_ptr + 12); |
| 140 | _rnn_H = vfma_lane_f16(_rnn_H, _weight_xc, _x, 0); |
| 141 | _sum1 = vfma_lane_f16(_sum1, _weight_xc_1, _x, 1); |
| 142 | _sum2 = vfma_lane_f16(_sum2, _weight_xc_2, _x, 2); |
| 143 | _sum3 = vfma_lane_f16(_sum3, _weight_xc_3, _x, 3); |
| 144 | |
| 145 | weight_xc_ptr += 16; |
| 146 | } |
| 147 | for (; i < size; i++) |
| 148 | { |
| 149 | float16x4_t _x = vdup_n_f16(x[i]); |
| 150 | float16x4_t _weight_xc = vld1_f16(weight_xc_ptr); |
| 151 | _rnn_H = vfma_f16(_rnn_H, _weight_xc, _x); |
| 152 | |
| 153 | weight_xc_ptr += 4; |
| 154 | } |
| 155 | |
| 156 | i = 0; |
| 157 | for (; i + 3 < num_output; i += 4) |
| 158 | { |
| 159 | float16x4_t _hidden_state = vcvt_f16_f32(vld1q_f32((const float*)hidden_state + i)); |
| 160 | float16x4_t _weight_hc = vld1_f16(weight_hc_ptr); |
| 161 | float16x4_t _weight_hc_1 = vld1_f16(weight_hc_ptr + 4); |
| 162 | float16x4_t _weight_hc_2 = vld1_f16(weight_hc_ptr + 8); |
| 163 | float16x4_t _weight_hc_3 = vld1_f16(weight_hc_ptr + 12); |
| 164 | _rnn_H = vfma_lane_f16(_rnn_H, _weight_hc, _hidden_state, 0); |
| 165 | _sum1 = vfma_lane_f16(_sum1, _weight_hc_1, _hidden_state, 1); |
| 166 | _sum2 = vfma_lane_f16(_sum2, _weight_hc_2, _hidden_state, 2); |
| 167 | _sum3 = vfma_lane_f16(_sum3, _weight_hc_3, _hidden_state, 3); |
| 168 | |
| 169 | weight_hc_ptr += 16; |
| 170 | } |
| 171 | for (; i < num_output; i++) |
| 172 | { |
| 173 | float16x4_t _hidden_state = vdup_n_f16((__fp16)hidden_state[i]); |
| 174 | float16x4_t _weight_hc = vld1_f16(weight_hc_ptr); |
| 175 | _rnn_H = vfma_f16(_rnn_H, _weight_hc, _hidden_state); |
| 176 | |
| 177 | weight_hc_ptr += 4; |
| 178 | } |
| 179 | |
| 180 | _rnn_H = vadd_f16(_rnn_H, _sum1); |
| 181 | _sum2 = vadd_f16(_sum2, _sum3); |
| 182 | _rnn_H = vadd_f16(_rnn_H, _sum2); |
| 183 | |
| 184 | float32x4_t _H32 = tanh_ps(vcvt_f32_f16(_rnn_H)); |
| 185 | |
| 186 | vst1q_f32((float*)gates + q, _H32); |
| 187 | } |
| 188 | remain_num_output_start += nn_num_output << 2; |
| 189 | #pragma omp parallel for num_threads(opt.num_threads) |
| 190 | for (int q = remain_num_output_start; q < num_output; q++) |
| 191 | { |
| 192 | const __fp16* weight_xc_ptr = weight_xc.row<const __fp16>(q / 8 + (q % 8) / 4 + q % 4); |
| 193 | const __fp16* weight_hc_ptr = weight_hc.row<const __fp16>(q / 8 + (q % 8) / 4 + q % 4); |
| 194 | |
| 195 | __fp16 H = ((const __fp16*)bias_c)[q]; |
| 196 | |
| 197 | for (int i = 0; i < size; i++) |
| 198 | { |
| 199 | H += weight_xc_ptr[i] * x[i]; |
| 200 | } |
| 201 | |
| 202 | for (int i = 0; i < num_output; i++) |
| 203 | { |
| 204 | H += weight_hc_ptr[i] * (__fp16)hidden_state[i]; |
| 205 | } |
| 206 | |
| 207 | float H32 = tanhf((float)H); |
| 208 | |
| 209 | gates[q] = H32; |
| 210 | } |
| 211 | |
| 212 | __fp16* output_data = top_blob.row<__fp16>(ti); |
| 213 | |
| 214 | float* hidden_ptr = hidden_state; |
| 215 | |
| 216 | nn_num_output = num_output >> 2; |
| 217 | remain_num_output_start = nn_num_output << 2; |
| 218 | #pragma omp parallel for num_threads(opt.num_threads) |
| 219 | for (int qq = 0; qq < nn_num_output; qq++) |
| 220 | { |
| 221 | int q = qq * 4; |
| 222 | |
| 223 | float32x4_t _rnn_H = vld1q_f32((float*)gates + q); |
| 224 | |
| 225 | vst1q_f32(hidden_ptr + q, _rnn_H); |
| 226 | vst1_f16(output_data + q, vcvt_f16_f32(_rnn_H)); |
| 227 | } |
| 228 | #pragma omp parallel for num_threads(opt.num_threads) |
| 229 | for (int q = remain_num_output_start; q < num_output; q++) |
| 230 | { |
| 231 | float H = gates[q]; |
| 232 | |
| 233 | hidden_ptr[q] = H; |
| 234 | output_data[q] = (__fp16)H; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int rnn_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) |
| 242 | { |
| 243 | if (opt.use_fp16_arithmetic) |
| 244 | return rnn_fp16sa(bottom_blob, top_blob, reverse, weight_xc, bias_c, weight_hc, hidden_state, opt); |
| 245 | |
| 246 | int size = bottom_blob.w; |
| 247 | int T = bottom_blob.h; |
| 248 | |
| 249 | int num_output = top_blob.w; |
| 250 | |
| 251 | // num_output |
| 252 | Mat gates(num_output, 4u, opt.workspace_allocator); |
| 253 | if (gates.empty()) |
| 254 | return -100; |
| 255 | |
| 256 | // unroll |
| 257 | for (int t = 0; t < T; t++) |
| 258 | { |
| 259 | int ti = reverse ? T - 1 - t : t; |
| 260 | |
| 261 | const __fp16* x = bottom_blob.row<const __fp16>(ti); |
| 262 | |
| 263 | int nn_num_output = num_output >> 2; |
| 264 | int remain_num_output_start = nn_num_output << 2; |
| 265 | #pragma omp parallel for num_threads(opt.num_threads) |
| 266 | for (int qq = 0; qq < nn_num_output; qq++) |
| 267 | { |
| 268 | int q = qq * 4; |
| 269 | |
| 270 | const __fp16* weight_xc_ptr = weight_xc.row<const __fp16>(q / 4); |
| 271 | const __fp16* weight_hc_ptr = weight_hc.row<const __fp16>(q / 4); |
| 272 | |
| 273 | float32x4_t _rnn_H = vcvt_f32_f16(vld1_f16((const __fp16*)bias_c + q)); |
| 274 | float32x4_t _sum1 = vdupq_n_f32(0.f); |
| 275 | float32x4_t _sum2 = vdupq_n_f32(0.f); |
| 276 | float32x4_t _sum3 = vdupq_n_f32(0.f); |
| 277 | |
| 278 | int i = 0; |
| 279 | for (; i + 3 < size; i += 4) |
| 280 | { |
| 281 | float32x4_t _x = vcvt_f32_f16(vld1_f16(x + i)); |
| 282 | float32x4_t _weight_xc = vcvt_f32_f16(vld1_f16(weight_xc_ptr)); |
| 283 | float32x4_t _weight_xc_1 = vcvt_f32_f16(vld1_f16(weight_xc_ptr + 4)); |
| 284 | float32x4_t _weight_xc_2 = vcvt_f32_f16(vld1_f16(weight_xc_ptr + 8)); |
| 285 | float32x4_t _weight_xc_3 = vcvt_f32_f16(vld1_f16(weight_xc_ptr + 12)); |
| 286 | _rnn_H = vfmaq_laneq_f32(_rnn_H, _weight_xc, _x, 0); |
| 287 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_1, _x, 1); |
| 288 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_2, _x, 2); |
| 289 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_3, _x, 3); |
| 290 | |
| 291 | weight_xc_ptr += 16; |
| 292 | } |
| 293 | for (; i < size; i++) |
| 294 | { |
| 295 | float32x4_t _x = vcvt_f32_f16(vdup_n_f16(x[i])); |
| 296 | float32x4_t _weight_xc = vcvt_f32_f16(vld1_f16(weight_xc_ptr)); |
| 297 | _rnn_H = vfmaq_f32(_rnn_H, _weight_xc, _x); |
| 298 | |
| 299 | weight_xc_ptr += 4; |
| 300 | } |
| 301 | |
| 302 | i = 0; |
| 303 | for (; i + 3 < num_output; i += 4) |
| 304 | { |
| 305 | float32x4_t _hidden_state = vld1q_f32((const float*)hidden_state + i); |
| 306 | float32x4_t _weight_hc = vcvt_f32_f16(vld1_f16(weight_hc_ptr)); |
| 307 | float32x4_t _weight_hc_1 = vcvt_f32_f16(vld1_f16(weight_hc_ptr + 4)); |
| 308 | float32x4_t _weight_hc_2 = vcvt_f32_f16(vld1_f16(weight_hc_ptr + 8)); |
| 309 | float32x4_t _weight_hc_3 = vcvt_f32_f16(vld1_f16(weight_hc_ptr + 12)); |
| 310 | _rnn_H = vfmaq_laneq_f32(_rnn_H, _weight_hc, _hidden_state, 0); |
| 311 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_1, _hidden_state, 1); |
| 312 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_2, _hidden_state, 2); |
| 313 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_3, _hidden_state, 3); |
| 314 | |
| 315 | weight_hc_ptr += 16; |
| 316 | } |
| 317 | for (; i < num_output; i++) |
| 318 | { |
| 319 | float32x4_t _hidden_state = vdupq_n_f32(hidden_state[i]); |
| 320 | float32x4_t _weight_hc = vcvt_f32_f16(vld1_f16(weight_hc_ptr)); |
| 321 | _rnn_H = vfmaq_f32(_rnn_H, _weight_hc, _hidden_state); |
| 322 | |
| 323 | weight_hc_ptr += 4; |
| 324 | } |
| 325 | |
| 326 | _rnn_H = vaddq_f32(_rnn_H, _sum1); |
| 327 | _sum2 = vaddq_f32(_sum2, _sum3); |
| 328 | _rnn_H = vaddq_f32(_rnn_H, _sum2); |
| 329 | |
| 330 | _rnn_H = tanh_ps(_rnn_H); |
| 331 | |
| 332 | vst1q_f32((float*)gates + q, _rnn_H); |
| 333 | } |
| 334 | #pragma omp parallel for num_threads(opt.num_threads) |
| 335 | for (int q = remain_num_output_start; q < num_output; q++) |
| 336 | { |
| 337 | const __fp16* weight_xc_ptr = weight_xc.row<const __fp16>(q / 4 + q % 4); |
| 338 | const __fp16* weight_hc_ptr = weight_hc.row<const __fp16>(q / 4 + q % 4); |
| 339 | |
| 340 | float H = (float)(((const __fp16*)bias_c)[q]); |
| 341 | |
| 342 | for (int i = 0; i < size; i++) |
| 343 | { |
| 344 | H += (float)weight_xc_ptr[i] * (float)x[i]; |
| 345 | } |
| 346 | |
| 347 | for (int i = 0; i < num_output; i++) |
| 348 | { |
| 349 | H += (float)weight_hc_ptr[i] * hidden_state[i]; |
| 350 | } |
| 351 | |
| 352 | H = tanhf(H); |
| 353 | |
| 354 | gates[q] = H; |
| 355 | } |
| 356 | |
| 357 | __fp16* output_data = top_blob.row<__fp16>(ti); |
| 358 | |
| 359 | float* hidden_ptr = hidden_state; |
| 360 | |
| 361 | nn_num_output = num_output >> 2; |
| 362 | remain_num_output_start = nn_num_output << 2; |
| 363 | #pragma omp parallel for num_threads(opt.num_threads) |
| 364 | for (int qq = 0; qq < nn_num_output; qq++) |
| 365 | { |
| 366 | int q = qq * 4; |
| 367 | |
| 368 | float32x4_t _rnn_H = vld1q_f32((float*)gates + q); |
| 369 | |
| 370 | vst1q_f32(hidden_ptr + q, _rnn_H); |
| 371 | vst1_f16(output_data + q, vcvt_f16_f32(_rnn_H)); |
| 372 | } |
| 373 | #pragma omp parallel for num_threads(opt.num_threads) |
| 374 | for (int q = remain_num_output_start; q < num_output; q++) |
| 375 | { |
| 376 | float H = gates[q]; |
| 377 | |
| 378 | hidden_ptr[q] = H; |
| 379 | output_data[q] = (__fp16)H; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int RNN_arm::create_pipeline_fp16s(const Option& opt) |
| 387 | { |
| 388 | int num_directions = direction == 2 ? 2 : 1; |
| 389 | int size = weight_data_size / num_directions / num_output; |
| 390 | |
| 391 | if (opt.use_fp16_arithmetic) |
| 392 | { |
| 393 | weight_xc_data_packed.create(size * 8, num_output / 8 + (num_output % 8) / 4 + num_output % 4, num_directions, 2u, 1); |
| 394 | weight_hc_data_packed.create(num_output * 8, num_output / 8 + (num_output % 8) / 4 + num_output % 4, num_directions, 2u, 1); |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | weight_xc_data_packed.create(size * 4, num_output / 4 + num_output % 4, num_directions, 2u, 1); |
| 399 | weight_hc_data_packed.create(num_output * 4, num_output / 4 + num_output % 4, num_directions, 2u, 1); |
| 400 | } |
| 401 | |
| 402 | #pragma omp parallel for num_threads(opt.num_threads) |
| 403 | for (int dr = 0; dr < num_directions; dr++) |
| 404 | { |
| 405 | const Mat weight_xc = weight_xc_data.channel(dr); |
| 406 | const Mat weight_hc = weight_hc_data.channel(dr); |
| 407 | |
| 408 | Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr); |
| 409 | Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr); |
| 410 | |
| 411 | int q = 0; |
| 412 | if (opt.use_fp16_arithmetic) |
| 413 | { |
| 414 | for (; q + 7 < num_output; q += 8) |
| 415 | { |
| 416 | const float* weight_xc_0 = weight_xc.row(q); |
| 417 | const float* weight_xc_1 = weight_xc.row(q + 1); |
| 418 | const float* weight_xc_2 = weight_xc.row(q + 2); |
| 419 | const float* weight_xc_3 = weight_xc.row(q + 3); |
| 420 | const float* weight_xc_4 = weight_xc.row(q + 4); |
| 421 | const float* weight_xc_5 = weight_xc.row(q + 5); |
| 422 | const float* weight_xc_6 = weight_xc.row(q + 6); |
| 423 | const float* weight_xc_7 = weight_xc.row(q + 7); |
| 424 | |
| 425 | const float* weight_hc_0 = weight_hc.row(q); |
| 426 | const float* weight_hc_1 = weight_hc.row(q + 1); |
| 427 | const float* weight_hc_2 = weight_hc.row(q + 2); |
| 428 | const float* weight_hc_3 = weight_hc.row(q + 3); |
| 429 | const float* weight_hc_4 = weight_hc.row(q + 4); |
| 430 | const float* weight_hc_5 = weight_hc.row(q + 5); |
| 431 | const float* weight_hc_6 = weight_hc.row(q + 6); |
| 432 | const float* weight_hc_7 = weight_hc.row(q + 7); |
| 433 | |
| 434 | __fp16* weight_xc = weight_xc_data_packed_dr.row<__fp16>(q / 8); |
| 435 | __fp16* weight_hc = weight_hc_data_packed_dr.row<__fp16>(q / 8); |
| 436 | |
| 437 | for (int i = 0; i < size; i++) |
| 438 | { |
| 439 | weight_xc[0] = (__fp16)weight_xc_0[i]; |
| 440 | weight_xc[1] = (__fp16)weight_xc_1[i]; |
| 441 | weight_xc[2] = (__fp16)weight_xc_2[i]; |
| 442 | weight_xc[3] = (__fp16)weight_xc_3[i]; |
| 443 | weight_xc[4] = (__fp16)weight_xc_4[i]; |
| 444 | weight_xc[5] = (__fp16)weight_xc_5[i]; |
| 445 | weight_xc[6] = (__fp16)weight_xc_6[i]; |
| 446 | weight_xc[7] = (__fp16)weight_xc_7[i]; |
| 447 | |
| 448 | weight_xc += 8; |
| 449 | } |
| 450 | |
| 451 | for (int i = 0; i < num_output; i++) |
| 452 | { |
| 453 | weight_hc[0] = (__fp16)weight_hc_0[i]; |
| 454 | weight_hc[1] = (__fp16)weight_hc_1[i]; |
| 455 | weight_hc[2] = (__fp16)weight_hc_2[i]; |
| 456 | weight_hc[3] = (__fp16)weight_hc_3[i]; |
| 457 | weight_hc[4] = (__fp16)weight_hc_4[i]; |
| 458 | weight_hc[5] = (__fp16)weight_hc_5[i]; |
| 459 | weight_hc[6] = (__fp16)weight_hc_6[i]; |
| 460 | weight_hc[7] = (__fp16)weight_hc_7[i]; |
| 461 | |
| 462 | weight_hc += 8; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | for (; q + 3 < num_output; q += 4) |
| 467 | { |
| 468 | const float* weight_xc_0 = weight_xc.row(q); |
| 469 | const float* weight_xc_1 = weight_xc.row(q + 1); |
| 470 | const float* weight_xc_2 = weight_xc.row(q + 2); |
| 471 | const float* weight_xc_3 = weight_xc.row(q + 3); |
| 472 | |
| 473 | const float* weight_hc_0 = weight_hc.row(q); |
| 474 | const float* weight_hc_1 = weight_hc.row(q + 1); |
| 475 | const float* weight_hc_2 = weight_hc.row(q + 2); |
| 476 | const float* weight_hc_3 = weight_hc.row(q + 3); |
| 477 | |
| 478 | __fp16* weight_xc = opt.use_fp16_arithmetic ? weight_xc_data_packed_dr.row<__fp16>(q / 8 + (q % 8) / 4) : weight_xc_data_packed_dr.row<__fp16>(q / 4); |
| 479 | __fp16* weight_hc = opt.use_fp16_arithmetic ? weight_hc_data_packed_dr.row<__fp16>(q / 8 + (q % 8) / 4) : weight_hc_data_packed_dr.row<__fp16>(q / 4); |
| 480 | |
| 481 | for (int i = 0; i < size; i++) |
| 482 | { |
| 483 | weight_xc[0] = (__fp16)weight_xc_0[i]; |
| 484 | weight_xc[1] = (__fp16)weight_xc_1[i]; |
| 485 | weight_xc[2] = (__fp16)weight_xc_2[i]; |
| 486 | weight_xc[3] = (__fp16)weight_xc_3[i]; |
| 487 | |
| 488 | weight_xc += 4; |
| 489 | } |
| 490 | |
| 491 | for (int i = 0; i < num_output; i++) |
| 492 | { |
| 493 | weight_hc[0] = (__fp16)weight_hc_0[i]; |
| 494 | weight_hc[1] = (__fp16)weight_hc_1[i]; |
| 495 | weight_hc[2] = (__fp16)weight_hc_2[i]; |
| 496 | weight_hc[3] = (__fp16)weight_hc_3[i]; |
| 497 | |
| 498 | weight_hc += 4; |
| 499 | } |
| 500 | } |
| 501 | for (; q < num_output; q++) |
| 502 | { |
| 503 | const float* weight_xc_0 = weight_xc.row(q); |
| 504 | const float* weight_hc_0 = weight_hc.row(q); |
| 505 | |
| 506 | __fp16* weight_xc = opt.use_fp16_arithmetic ? weight_xc_data_packed_dr.row<__fp16>(q / 8 + (q % 8) / 4 + q % 4) : weight_xc_data_packed_dr.row<__fp16>(q / 4 + q % 4); |
| 507 | __fp16* weight_hc = opt.use_fp16_arithmetic ? weight_hc_data_packed_dr.row<__fp16>(q / 8 + (q % 8) / 4 + q % 4) : weight_hc_data_packed_dr.row<__fp16>(q / 4 + q % 4); |
| 508 | |
| 509 | for (int i = 0; i < size; i++) |
| 510 | { |
| 511 | weight_xc[i] = (__fp16)weight_xc_0[i]; |
| 512 | } |
| 513 | |
| 514 | for (int i = 0; i < num_output; i++) |
| 515 | { |
| 516 | weight_hc[i] = (__fp16)weight_hc_0[i]; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | cast_float32_to_float16(bias_c_data, bias_c_data_packed, opt); |
| 522 | |
| 523 | if (opt.lightmode) |
| 524 | { |
| 525 | weight_xc_data.release(); |
| 526 | bias_c_data.release(); |
| 527 | weight_hc_data.release(); |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | int RNN_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 534 | { |
| 535 | int T = bottom_blob.h; |
| 536 | |
| 537 | int num_directions = direction == 2 ? 2 : 1; |
| 538 | |
| 539 | // initial hidden state |
| 540 | Mat hidden(num_output, 4u, opt.workspace_allocator); |
| 541 | if (hidden.empty()) |
| 542 | return -100; |
| 543 | hidden.fill(0.f); |
| 544 | |
| 545 | top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator); |
| 546 | if (top_blob.empty()) |
| 547 | return -100; |
| 548 | |
| 549 | // Uni directional |
| 550 | if (direction == 0 || direction == 1) |
| 551 | { |
| 552 | int ret = rnn_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); |
| 553 | if (ret != 0) |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | if (direction == 2) |
| 558 | { |
| 559 | Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator); |
| 560 | if (top_blob_forward.empty()) |
| 561 | return -100; |
| 562 | |
| 563 | Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator); |
| 564 | if (top_blob_reverse.empty()) |
| 565 | return -100; |
| 566 | |
| 567 | { |
| 568 | int ret = rnn_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); |
| 569 | if (ret != 0) |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | hidden.fill(0.f); |
| 574 | |
| 575 | { |
| 576 | int ret = rnn_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); |
| 577 | if (ret != 0) |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | // concat w |
| 582 | for (int i = 0; i < T; i++) |
| 583 | { |
| 584 | const __fp16* pf = top_blob_forward.row<const __fp16>(i); |
| 585 | const __fp16* pr = top_blob_reverse.row<const __fp16>(i); |
| 586 | __fp16* ptr = top_blob.row<__fp16>(i); |
| 587 | |
| 588 | memcpy(ptr, pf, num_output * sizeof(__fp16)); |
| 589 | memcpy(ptr + num_output, pr, num_output * sizeof(__fp16)); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | int RNN_arm::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 597 | { |
| 598 | const Mat& bottom_blob = bottom_blobs[0]; |
| 599 | int T = bottom_blob.h; |
| 600 | int num_directions = direction == 2 ? 2 : 1; |
| 601 | |
| 602 | Mat hidden; |
| 603 | Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator; |
| 604 | if (bottom_blobs.size() == 2) |
| 605 | { |
| 606 | Option opt_cast = opt; |
| 607 | opt_cast.blob_allocator = hidden_allocator; |
| 608 | cast_float16_to_float32(bottom_blobs[1], hidden, opt_cast); |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | hidden.create(num_output, num_directions, 4u, hidden_allocator); |
| 613 | if (hidden.empty()) |
| 614 | return -100; |
| 615 | hidden.fill(0.f); |
| 616 | } |
| 617 | |
| 618 | Mat& top_blob = top_blobs[0]; |
| 619 | top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator); |
| 620 | if (top_blob.empty()) |
| 621 | return -100; |
| 622 | |
| 623 | // Uni directional |
| 624 | if (direction == 0 || direction == 1) |
| 625 | { |
| 626 | int ret = rnn_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); |
| 627 | if (ret != 0) |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | if (direction == 2) |
| 632 | { |
| 633 | Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator); |
| 634 | if (top_blob_forward.empty()) |
| 635 | return -100; |
| 636 | |
| 637 | Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator); |
| 638 | if (top_blob_reverse.empty()) |
| 639 | return -100; |
| 640 | |
| 641 | Mat hidden0 = hidden.row_range(0, 1); |
| 642 | { |
| 643 | int ret = rnn_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); |
| 644 | if (ret != 0) |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | Mat hidden1 = hidden.row_range(1, 1); |
| 649 | { |
| 650 | int ret = rnn_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); |
| 651 | if (ret != 0) |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | // concat w |
| 656 | for (int i = 0; i < T; i++) |
| 657 | { |
| 658 | const __fp16* pf = top_blob_forward.row<const __fp16>(i); |
| 659 | const __fp16* pr = top_blob_reverse.row<const __fp16>(i); |
| 660 | __fp16* ptr = top_blob.row<__fp16>(i); |
| 661 | |
| 662 | memcpy(ptr, pf, num_output * sizeof(__fp16)); |
| 663 | memcpy(ptr + num_output, pr, num_output * sizeof(__fp16)); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (top_blobs.size() == 2) |
| 668 | { |
| 669 | cast_float32_to_float16(hidden, top_blobs[1], opt); |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | #endif |
| 675 | |
| 676 | } // namespace ncnn |