| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 | #include "arm_usability.h" |
| 23 | |
| 24 | #include "cpu.h" |
| 25 | |
| 26 | namespace ncnn { |
| 27 | |
| 28 | #if NCNN_INT8 |
| 29 | #include "gru_int8.h" |
| 30 | #endif |
| 31 | |
| 32 | GRU_arm::GRU_arm() |
| 33 | { |
| 34 | #if __ARM_NEON |
| 35 | #if NCNN_ARM82 |
| 36 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 37 | #endif |
| 38 | #endif // __ARM_NEON |
| 39 | |
| 40 | #if NCNN_BF16 |
| 41 | support_bf16_storage = true; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | int GRU_arm::create_pipeline(const Option& opt) |
| 46 | { |
| 47 | #if NCNN_INT8 |
| 48 | if (int8_scale_term) |
| 49 | { |
| 50 | return create_pipeline_int8(opt); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | #if NCNN_ARM82 |
| 55 | if (support_fp16_storage && opt.use_fp16_storage) |
| 56 | { |
| 57 | return create_pipeline_fp16s(opt); |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | #if NCNN_BF16 |
| 62 | if (opt.use_bf16_storage) |
| 63 | { |
| 64 | return create_pipeline_bf16s(opt); |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | // pack RUN |
| 69 | const int num_directions = direction == 2 ? 2 : 1; |
| 70 | const int size = weight_data_size / num_directions / num_output / 3; |
| 71 | |
| 72 | #if __ARM_NEON |
| 73 | weight_xc_data_packed.create(size * 12, num_output / 4 + num_output % 4, num_directions); |
| 74 | bias_c_data_packed.create(num_output, 1, num_directions, 16u, 4); |
| 75 | weight_hc_data_packed.create(num_output * 12, num_output / 4 + num_output % 4, num_directions); |
| 76 | #else |
| 77 | weight_xc_data_packed.create(size * 3, num_output, num_directions); |
| 78 | bias_c_data_packed.create(num_output, 1, num_directions, 16u, 4); |
| 79 | weight_hc_data_packed.create(num_output * 3, num_output, num_directions); |
| 80 | #endif |
| 81 | |
| 82 | #pragma omp parallel for num_threads(opt.num_threads) |
| 83 | for (int dr = 0; dr < num_directions; dr++) |
| 84 | { |
| 85 | const Mat weight_xc = weight_xc_data.channel(dr); |
| 86 | const Mat bias_c = bias_c_data.channel(dr); |
| 87 | const Mat weight_hc = weight_hc_data.channel(dr); |
| 88 | |
| 89 | Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr); |
| 90 | Mat bias_c_data_packed_dr = bias_c_data_packed.channel(dr); |
| 91 | Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr); |
| 92 | |
| 93 | const float* bias_c_R = bias_c.row(0); |
| 94 | const float* bias_c_U = bias_c.row(1); |
| 95 | const float* bias_c_WN = bias_c.row(2); |
| 96 | const float* bias_c_BN = bias_c.row(3); |
| 97 | |
| 98 | float* bias_c_RUBNWN = bias_c_data_packed_dr.row(0); |
| 99 | |
| 100 | int q = 0; |
| 101 | #if __ARM_NEON |
| 102 | for (; q + 3 < num_output; q += 4) |
| 103 | { |
| 104 | vst1q_f32(bias_c_RUBNWN, vld1q_f32(bias_c_R + q)); |
| 105 | vst1q_f32(bias_c_RUBNWN + 4, vld1q_f32(bias_c_U + q)); |
| 106 | vst1q_f32(bias_c_RUBNWN + 8, vld1q_f32(bias_c_BN + q)); |
| 107 | vst1q_f32(bias_c_RUBNWN + 12, vld1q_f32(bias_c_WN + q)); |
| 108 | |
| 109 | bias_c_RUBNWN += 16; |
| 110 | |
| 111 | const float* weight_xc_R = weight_xc.row(num_output * 0 + q); |
| 112 | const float* weight_xc_U = weight_xc.row(num_output * 1 + q); |
| 113 | const float* weight_xc_N = weight_xc.row(num_output * 2 + q); |
| 114 | |
| 115 | const float* weight_xc_R_1 = weight_xc.row(num_output * 0 + q + 1); |
| 116 | const float* weight_xc_U_1 = weight_xc.row(num_output * 1 + q + 1); |
| 117 | const float* weight_xc_N_1 = weight_xc.row(num_output * 2 + q + 1); |
| 118 | |
| 119 | const float* weight_xc_R_2 = weight_xc.row(num_output * 0 + q + 2); |
| 120 | const float* weight_xc_U_2 = weight_xc.row(num_output * 1 + q + 2); |
| 121 | const float* weight_xc_N_2 = weight_xc.row(num_output * 2 + q + 2); |
| 122 | |
| 123 | const float* weight_xc_R_3 = weight_xc.row(num_output * 0 + q + 3); |
| 124 | const float* weight_xc_U_3 = weight_xc.row(num_output * 1 + q + 3); |
| 125 | const float* weight_xc_N_3 = weight_xc.row(num_output * 2 + q + 3); |
| 126 | |
| 127 | const float* weight_hc_R = weight_hc.row(num_output * 0 + q); |
| 128 | const float* weight_hc_U = weight_hc.row(num_output * 1 + q); |
| 129 | const float* weight_hc_N = weight_hc.row(num_output * 2 + q); |
| 130 | |
| 131 | const float* weight_hc_R_1 = weight_hc.row(num_output * 0 + q + 1); |
| 132 | const float* weight_hc_U_1 = weight_hc.row(num_output * 1 + q + 1); |
| 133 | const float* weight_hc_N_1 = weight_hc.row(num_output * 2 + q + 1); |
| 134 | |
| 135 | const float* weight_hc_R_2 = weight_hc.row(num_output * 0 + q + 2); |
| 136 | const float* weight_hc_U_2 = weight_hc.row(num_output * 1 + q + 2); |
| 137 | const float* weight_hc_N_2 = weight_hc.row(num_output * 2 + q + 2); |
| 138 | |
| 139 | const float* weight_hc_R_3 = weight_hc.row(num_output * 0 + q + 3); |
| 140 | const float* weight_hc_U_3 = weight_hc.row(num_output * 1 + q + 3); |
| 141 | const float* weight_hc_N_3 = weight_hc.row(num_output * 2 + q + 3); |
| 142 | |
| 143 | float* weight_xc_RUN = weight_xc_data_packed_dr.row(q / 4); |
| 144 | float* weight_hc_RUN = weight_hc_data_packed_dr.row(q / 4); |
| 145 | |
| 146 | for (int i = 0; i < size; i++) |
| 147 | { |
| 148 | weight_xc_RUN[0] = weight_xc_R[i]; |
| 149 | weight_xc_RUN[1] = weight_xc_R_1[i]; |
| 150 | weight_xc_RUN[2] = weight_xc_R_2[i]; |
| 151 | weight_xc_RUN[3] = weight_xc_R_3[i]; |
| 152 | weight_xc_RUN[4] = weight_xc_U[i]; |
| 153 | weight_xc_RUN[5] = weight_xc_U_1[i]; |
| 154 | weight_xc_RUN[6] = weight_xc_U_2[i]; |
| 155 | weight_xc_RUN[7] = weight_xc_U_3[i]; |
| 156 | |
| 157 | weight_xc_RUN += 8; |
| 158 | } |
| 159 | |
| 160 | for (int i = 0; i < num_output; i++) |
| 161 | { |
| 162 | weight_hc_RUN[0] = weight_hc_R[i]; |
| 163 | weight_hc_RUN[1] = weight_hc_R_1[i]; |
| 164 | weight_hc_RUN[2] = weight_hc_R_2[i]; |
| 165 | weight_hc_RUN[3] = weight_hc_R_3[i]; |
| 166 | weight_hc_RUN[4] = weight_hc_U[i]; |
| 167 | weight_hc_RUN[5] = weight_hc_U_1[i]; |
| 168 | weight_hc_RUN[6] = weight_hc_U_2[i]; |
| 169 | weight_hc_RUN[7] = weight_hc_U_3[i]; |
| 170 | |
| 171 | weight_hc_RUN += 8; |
| 172 | } |
| 173 | |
| 174 | for (int i = 0; i < size; i++) |
| 175 | { |
| 176 | weight_xc_RUN[0] = weight_xc_N[i]; |
| 177 | weight_xc_RUN[1] = weight_xc_N_1[i]; |
| 178 | weight_xc_RUN[2] = weight_xc_N_2[i]; |
| 179 | weight_xc_RUN[3] = weight_xc_N_3[i]; |
| 180 | |
| 181 | weight_xc_RUN += 4; |
| 182 | } |
| 183 | |
| 184 | for (int i = 0; i < num_output; i++) |
| 185 | { |
| 186 | weight_hc_RUN[0] = weight_hc_N[i]; |
| 187 | weight_hc_RUN[1] = weight_hc_N_1[i]; |
| 188 | weight_hc_RUN[2] = weight_hc_N_2[i]; |
| 189 | weight_hc_RUN[3] = weight_hc_N_3[i]; |
| 190 | |
| 191 | weight_hc_RUN += 4; |
| 192 | } |
| 193 | } |
| 194 | #endif // __ARM_NEON |
| 195 | for (; q < num_output; q++) |
| 196 | { |
| 197 | bias_c_RUBNWN[0] = bias_c_R[q]; |
| 198 | bias_c_RUBNWN[1] = bias_c_U[q]; |
| 199 | bias_c_RUBNWN[2] = bias_c_BN[q]; |
| 200 | bias_c_RUBNWN[3] = bias_c_WN[q]; |
| 201 | |
| 202 | bias_c_RUBNWN += 4; |
| 203 | |
| 204 | const float* weight_xc_R = weight_xc.row(num_output * 0 + q); |
| 205 | const float* weight_xc_U = weight_xc.row(num_output * 1 + q); |
| 206 | const float* weight_xc_N = weight_xc.row(num_output * 2 + q); |
| 207 | |
| 208 | const float* weight_hc_R = weight_hc.row(num_output * 0 + q); |
| 209 | const float* weight_hc_U = weight_hc.row(num_output * 1 + q); |
| 210 | const float* weight_hc_N = weight_hc.row(num_output * 2 + q); |
| 211 | |
| 212 | #if __ARM_NEON |
| 213 | float* weight_xc_RUN = weight_xc_data_packed_dr.row(q / 4 + q % 4); |
| 214 | float* weight_hc_RUN = weight_hc_data_packed_dr.row(q / 4 + q % 4); |
| 215 | #else |
| 216 | float* weight_xc_RUN = weight_xc_data_packed_dr.row(q); |
| 217 | float* weight_hc_RUN = weight_hc_data_packed_dr.row(q); |
| 218 | #endif // __ARM_NEON |
| 219 | |
| 220 | for (int i = 0; i < size; i++) |
| 221 | { |
| 222 | weight_xc_RUN[0] = weight_xc_R[i]; |
| 223 | weight_xc_RUN[1] = weight_xc_U[i]; |
| 224 | |
| 225 | weight_xc_RUN += 2; |
| 226 | } |
| 227 | |
| 228 | for (int i = 0; i < num_output; i++) |
| 229 | { |
| 230 | weight_hc_RUN[0] = weight_hc_R[i]; |
| 231 | weight_hc_RUN[1] = weight_hc_U[i]; |
| 232 | |
| 233 | weight_hc_RUN += 2; |
| 234 | } |
| 235 | |
| 236 | for (int i = 0; i < size; i++) |
| 237 | { |
| 238 | weight_xc_RUN[0] = weight_xc_N[i]; |
| 239 | |
| 240 | weight_xc_RUN += 1; |
| 241 | } |
| 242 | |
| 243 | for (int i = 0; i < num_output; i++) |
| 244 | { |
| 245 | weight_hc_RUN[0] = weight_hc_N[i]; |
| 246 | |
| 247 | weight_hc_RUN += 1; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (opt.lightmode) |
| 253 | { |
| 254 | weight_xc_data.release(); |
| 255 | bias_c_data.release(); |
| 256 | weight_hc_data.release(); |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int gru(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) |
| 263 | { |
| 264 | int size = bottom_blob.w; |
| 265 | int T = bottom_blob.h; |
| 266 | |
| 267 | int num_output = top_blob.w; |
| 268 | |
| 269 | // 2 x num_output |
| 270 | #if __ARM_NEON |
| 271 | Mat gates(4 * 2, num_output / 4 + num_output % 4, 4u, opt.workspace_allocator); |
| 272 | #else |
| 273 | Mat gates(2, num_output, 4u, opt.workspace_allocator); |
| 274 | #endif |
| 275 | if (gates.empty()) |
| 276 | return -100; |
| 277 | |
| 278 | // unroll |
| 279 | for (int t = 0; t < T; t++) |
| 280 | { |
| 281 | int ti = reverse ? T - 1 - t : t; |
| 282 | |
| 283 | int remain_num_output_start = 0; |
| 284 | #if __ARM_NEON |
| 285 | int nn_num_output = num_output >> 2; |
| 286 | remain_num_output_start = nn_num_output << 2; |
| 287 | |
| 288 | #pragma omp parallel for num_threads(opt.num_threads) |
| 289 | for (int qq = 0; qq < nn_num_output; qq++) |
| 290 | { |
| 291 | int q = qq * 4; |
| 292 | |
| 293 | const float* x = bottom_blob.row(ti); |
| 294 | |
| 295 | // gate reset update |
| 296 | const float* bias_c_RUBNWN = (const float*)bias_c + q * 4; |
| 297 | |
| 298 | const float* weight_xc_RUN = weight_xc.row(q / 4); |
| 299 | const float* weight_hc_RUN = weight_hc.row(q / 4); |
| 300 | |
| 301 | float32x4_t _gru_R = vld1q_f32(bias_c_RUBNWN); |
| 302 | float32x4_t _gru_U = vld1q_f32(bias_c_RUBNWN + 4); |
| 303 | float32x4_t _sum1 = vdupq_n_f32(0.f); |
| 304 | float32x4_t _sum2 = vdupq_n_f32(0.f); |
| 305 | float32x4_t _sum3 = vdupq_n_f32(0.f); |
| 306 | float32x4_t _sum4 = vdupq_n_f32(0.f); |
| 307 | float32x4_t _sum5 = vdupq_n_f32(0.f); |
| 308 | float32x4_t _sum6 = vdupq_n_f32(0.f); |
| 309 | |
| 310 | int i = 0; |
| 311 | for (; i + 3 < size; i += 4) |
| 312 | { |
| 313 | float32x4_t _xi = vld1q_f32(x + i); |
| 314 | float32x4_t _weight_xc_R = vld1q_f32(weight_xc_RUN); |
| 315 | float32x4_t _weight_xc_U = vld1q_f32(weight_xc_RUN + 4); |
| 316 | float32x4_t _weight_xc_R_1 = vld1q_f32(weight_xc_RUN + 8); |
| 317 | float32x4_t _weight_xc_U_1 = vld1q_f32(weight_xc_RUN + 12); |
| 318 | float32x4_t _weight_xc_R_2 = vld1q_f32(weight_xc_RUN + 16); |
| 319 | float32x4_t _weight_xc_U_2 = vld1q_f32(weight_xc_RUN + 20); |
| 320 | float32x4_t _weight_xc_R_3 = vld1q_f32(weight_xc_RUN + 24); |
| 321 | float32x4_t _weight_xc_U_3 = vld1q_f32(weight_xc_RUN + 28); |
| 322 | #if __aarch64__ |
| 323 | _gru_R = vfmaq_laneq_f32(_gru_R, _weight_xc_R, _xi, 0); |
| 324 | _gru_U = vfmaq_laneq_f32(_gru_U, _weight_xc_U, _xi, 0); |
| 325 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_R_1, _xi, 1); |
| 326 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_U_1, _xi, 1); |
| 327 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_R_2, _xi, 2); |
| 328 | _sum4 = vfmaq_laneq_f32(_sum4, _weight_xc_U_2, _xi, 2); |
| 329 | _sum5 = vfmaq_laneq_f32(_sum5, _weight_xc_R_3, _xi, 3); |
| 330 | _sum6 = vfmaq_laneq_f32(_sum6, _weight_xc_U_3, _xi, 3); |
| 331 | #else |
| 332 | _gru_R = vmlaq_lane_f32(_gru_R, _weight_xc_R, vget_low_f32(_xi), 0); |
| 333 | _gru_U = vmlaq_lane_f32(_gru_U, _weight_xc_U, vget_low_f32(_xi), 0); |
| 334 | _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_R_1, vget_low_f32(_xi), 1); |
| 335 | _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_U_1, vget_low_f32(_xi), 1); |
| 336 | _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_R_2, vget_high_f32(_xi), 0); |
| 337 | _sum4 = vmlaq_lane_f32(_sum4, _weight_xc_U_2, vget_high_f32(_xi), 0); |
| 338 | _sum5 = vmlaq_lane_f32(_sum5, _weight_xc_R_3, vget_high_f32(_xi), 1); |
| 339 | _sum6 = vmlaq_lane_f32(_sum6, _weight_xc_U_3, vget_high_f32(_xi), 1); |
| 340 | #endif |
| 341 | |
| 342 | weight_xc_RUN += 32; |
| 343 | } |
| 344 | for (; i < size; i++) |
| 345 | { |
| 346 | float xi = x[i]; |
| 347 | |
| 348 | float32x4_t _xi = vdupq_n_f32(xi); |
| 349 | float32x4_t _weight_xc_R = vld1q_f32(weight_xc_RUN); |
| 350 | float32x4_t _weight_xc_U = vld1q_f32(weight_xc_RUN + 4); |
| 351 | _gru_R = vmlaq_f32(_gru_R, _weight_xc_R, _xi); |
| 352 | _gru_U = vmlaq_f32(_gru_U, _weight_xc_U, _xi); |
| 353 | |
| 354 | weight_xc_RUN += 8; |
| 355 | } |
| 356 | |
| 357 | i = 0; |
| 358 | for (; i + 3 < num_output; i += 4) |
| 359 | { |
| 360 | float32x4_t _h_cont = vld1q_f32((const float*)hidden_state + i); |
| 361 | float32x4_t _weight_hc_R = vld1q_f32(weight_hc_RUN); |
| 362 | float32x4_t _weight_hc_U = vld1q_f32(weight_hc_RUN + 4); |
| 363 | float32x4_t _weight_hc_R_1 = vld1q_f32(weight_hc_RUN + 8); |
| 364 | float32x4_t _weight_hc_U_1 = vld1q_f32(weight_hc_RUN + 12); |
| 365 | float32x4_t _weight_hc_R_2 = vld1q_f32(weight_hc_RUN + 16); |
| 366 | float32x4_t _weight_hc_U_2 = vld1q_f32(weight_hc_RUN + 20); |
| 367 | float32x4_t _weight_hc_R_3 = vld1q_f32(weight_hc_RUN + 24); |
| 368 | float32x4_t _weight_hc_U_3 = vld1q_f32(weight_hc_RUN + 28); |
| 369 | #if __aarch64__ |
| 370 | _gru_R = vfmaq_laneq_f32(_gru_R, _weight_hc_R, _h_cont, 0); |
| 371 | _gru_U = vfmaq_laneq_f32(_gru_U, _weight_hc_U, _h_cont, 0); |
| 372 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_R_1, _h_cont, 1); |
| 373 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_U_1, _h_cont, 1); |
| 374 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_R_2, _h_cont, 2); |
| 375 | _sum4 = vfmaq_laneq_f32(_sum4, _weight_hc_U_2, _h_cont, 2); |
| 376 | _sum5 = vfmaq_laneq_f32(_sum5, _weight_hc_R_3, _h_cont, 3); |
| 377 | _sum6 = vfmaq_laneq_f32(_sum6, _weight_hc_U_3, _h_cont, 3); |
| 378 | #else |
| 379 | _gru_R = vmlaq_lane_f32(_gru_R, _weight_hc_R, vget_low_f32(_h_cont), 0); |
| 380 | _gru_U = vmlaq_lane_f32(_gru_U, _weight_hc_U, vget_low_f32(_h_cont), 0); |
| 381 | _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_R_1, vget_low_f32(_h_cont), 1); |
| 382 | _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_U_1, vget_low_f32(_h_cont), 1); |
| 383 | _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_R_2, vget_high_f32(_h_cont), 0); |
| 384 | _sum4 = vmlaq_lane_f32(_sum4, _weight_hc_U_2, vget_high_f32(_h_cont), 0); |
| 385 | _sum5 = vmlaq_lane_f32(_sum5, _weight_hc_R_3, vget_high_f32(_h_cont), 1); |
| 386 | _sum6 = vmlaq_lane_f32(_sum6, _weight_hc_U_3, vget_high_f32(_h_cont), 1); |
| 387 | #endif |
| 388 | |
| 389 | weight_hc_RUN += 32; |
| 390 | } |
| 391 | for (; i < num_output; i++) |
| 392 | { |
| 393 | float h_cont = hidden_state[i]; |
| 394 | |
| 395 | float32x4_t _h_cont = vdupq_n_f32(h_cont); |
| 396 | float32x4_t _weight_hc_R = vld1q_f32(weight_hc_RUN); |
| 397 | float32x4_t _weight_hc_U = vld1q_f32(weight_hc_RUN + 4); |
| 398 | _gru_R = vmlaq_f32(_gru_R, _weight_hc_R, _h_cont); |
| 399 | _gru_U = vmlaq_f32(_gru_U, _weight_hc_U, _h_cont); |
| 400 | |
| 401 | weight_hc_RUN += 8; |
| 402 | } |
| 403 | |
| 404 | _gru_R = vaddq_f32(_gru_R, _sum1); |
| 405 | _gru_U = vaddq_f32(_gru_U, _sum2); |
| 406 | _sum3 = vaddq_f32(_sum3, _sum5); |
| 407 | _sum4 = vaddq_f32(_sum4, _sum6); |
| 408 | _gru_R = vaddq_f32(_gru_R, _sum3); |
| 409 | _gru_U = vaddq_f32(_gru_U, _sum4); |
| 410 | |
| 411 | // sigmoid(R) |
| 412 | // sigmoid(U) |
| 413 | _gru_R = sigmoid_ps(_gru_R); |
| 414 | _gru_U = sigmoid_ps(_gru_U); |
| 415 | |
| 416 | // gate new |
| 417 | float32x4_t _gru_N = vld1q_f32(bias_c_RUBNWN + 8); |
| 418 | _sum1 = vdupq_n_f32(0.f); |
| 419 | _sum2 = vdupq_n_f32(0.f); |
| 420 | _sum3 = vdupq_n_f32(0.f); |
| 421 | |
| 422 | i = 0; |
| 423 | for (; i + 3 < num_output; i += 4) |
| 424 | { |
| 425 | float32x4_t _h_cont = vld1q_f32((const float*)hidden_state + i); |
| 426 | float32x4_t _weight_hc_N = vld1q_f32(weight_hc_RUN); |
| 427 | float32x4_t _weight_hc_N_1 = vld1q_f32(weight_hc_RUN + 4); |
| 428 | float32x4_t _weight_hc_N_2 = vld1q_f32(weight_hc_RUN + 8); |
| 429 | float32x4_t _weight_hc_N_3 = vld1q_f32(weight_hc_RUN + 12); |
| 430 | #if __aarch64__ |
| 431 | _gru_N = vfmaq_laneq_f32(_gru_N, _weight_hc_N, _h_cont, 0); |
| 432 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_N_1, _h_cont, 1); |
| 433 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_N_2, _h_cont, 2); |
| 434 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_N_3, _h_cont, 3); |
| 435 | #else |
| 436 | _gru_N = vmlaq_lane_f32(_gru_N, _weight_hc_N, vget_low_f32(_h_cont), 0); |
| 437 | _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_N_1, vget_low_f32(_h_cont), 1); |
| 438 | _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_N_2, vget_high_f32(_h_cont), 0); |
| 439 | _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_N_3, vget_high_f32(_h_cont), 1); |
| 440 | #endif |
| 441 | |
| 442 | weight_hc_RUN += 16; |
| 443 | } |
| 444 | for (; i < num_output; i++) |
| 445 | { |
| 446 | float h_cont = hidden_state[i]; |
| 447 | |
| 448 | float32x4_t _h_cont = vdupq_n_f32(h_cont); |
| 449 | float32x4_t _weight_hc_N = vld1q_f32(weight_hc_RUN); |
| 450 | _gru_N = vmlaq_f32(_gru_N, _weight_hc_N, _h_cont); |
| 451 | |
| 452 | weight_hc_RUN += 4; |
| 453 | } |
| 454 | |
| 455 | _gru_N = vaddq_f32(_gru_N, _sum1); |
| 456 | _sum2 = vaddq_f32(_sum2, _sum3); |
| 457 | _gru_N = vaddq_f32(_gru_N, _sum2); |
| 458 | |
| 459 | _gru_N = vmlaq_f32(vld1q_f32(bias_c_RUBNWN + 12), _gru_R, _gru_N); |
| 460 | _sum1 = vdupq_n_f32(0.f); |
| 461 | _sum2 = vdupq_n_f32(0.f); |
| 462 | _sum3 = vdupq_n_f32(0.f); |
| 463 | |
| 464 | i = 0; |
| 465 | for (; i + 3 < size; i += 4) |
| 466 | { |
| 467 | float32x4_t _xi = vld1q_f32(x + i); |
| 468 | float32x4_t _weight_xc_N = vld1q_f32(weight_xc_RUN); |
| 469 | float32x4_t _weight_xc_N_1 = vld1q_f32(weight_xc_RUN + 4); |
| 470 | float32x4_t _weight_xc_N_2 = vld1q_f32(weight_xc_RUN + 8); |
| 471 | float32x4_t _weight_xc_N_3 = vld1q_f32(weight_xc_RUN + 12); |
| 472 | #if __aarch64__ |
| 473 | _gru_N = vfmaq_laneq_f32(_gru_N, _weight_xc_N, _xi, 0); |
| 474 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_N_1, _xi, 1); |
| 475 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_N_2, _xi, 2); |
| 476 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_N_3, _xi, 3); |
| 477 | #else |
| 478 | _gru_N = vmlaq_lane_f32(_gru_N, _weight_xc_N, vget_low_f32(_xi), 0); |
| 479 | _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_N_1, vget_low_f32(_xi), 1); |
| 480 | _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_N_2, vget_high_f32(_xi), 0); |
| 481 | _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_N_3, vget_high_f32(_xi), 1); |
| 482 | #endif |
| 483 | |
| 484 | weight_xc_RUN += 16; |
| 485 | } |
| 486 | for (; i < size; i++) |
| 487 | { |
| 488 | float xi = x[i]; |
| 489 | |
| 490 | float32x4_t _xi = vdupq_n_f32(xi); |
| 491 | float32x4_t _weight_xc_N = vld1q_f32(weight_xc_RUN); |
| 492 | _gru_N = vmlaq_f32(_gru_N, _weight_xc_N, _xi); |
| 493 | |
| 494 | weight_xc_RUN += 4; |
| 495 | } |
| 496 | |
| 497 | _gru_N = vaddq_f32(_gru_N, _sum1); |
| 498 | _sum2 = vaddq_f32(_sum2, _sum3); |
| 499 | _gru_N = vaddq_f32(_gru_N, _sum2); |
| 500 | |
| 501 | // tanh(N) |
| 502 | _gru_N = tanh_ps(_gru_N); |
| 503 | |
| 504 | float* gates_data = gates.row(q / 4); |
| 505 | |
| 506 | vst1q_f32(gates_data, _gru_U); |
| 507 | vst1q_f32(gates_data + 4, _gru_N); |
| 508 | } |
| 509 | #endif // __ARM_NEON |
| 510 | #pragma omp parallel for num_threads(opt.num_threads) |
| 511 | for (int q = remain_num_output_start; q < num_output; q++) |
| 512 | { |
| 513 | const float* x = bottom_blob.row(ti); |
| 514 | |
| 515 | // gate reset update |
| 516 | const float* bias_c_RUBNWN = (const float*)bias_c + q * 4; |
| 517 | |
| 518 | #if __ARM_NEON |
| 519 | const float* weight_xc_RUN = weight_xc.row(q / 4 + q % 4); |
| 520 | const float* weight_hc_RUN = weight_hc.row(q / 4 + q % 4); |
| 521 | #else |
| 522 | const float* weight_xc_RUN = weight_xc.row(q); |
| 523 | const float* weight_hc_RUN = weight_hc.row(q); |
| 524 | #endif |
| 525 | |
| 526 | float R = bias_c_RUBNWN[0]; |
| 527 | float U = bias_c_RUBNWN[1]; |
| 528 | |
| 529 | for (int i = 0; i < size; i++) |
| 530 | { |
| 531 | float xi = x[i]; |
| 532 | |
| 533 | R += weight_xc_RUN[0] * xi; |
| 534 | U += weight_xc_RUN[1] * xi; |
| 535 | |
| 536 | weight_xc_RUN += 2; |
| 537 | } |
| 538 | |
| 539 | for (int i = 0; i < num_output; i++) |
| 540 | { |
| 541 | float h_cont = hidden_state[i]; |
| 542 | |
| 543 | R += weight_hc_RUN[0] * h_cont; |
| 544 | U += weight_hc_RUN[1] * h_cont; |
| 545 | |
| 546 | weight_hc_RUN += 2; |
| 547 | } |
| 548 | |
| 549 | // sigmoid(R) |
| 550 | // sigmoid(U) |
| 551 | R = 1.f / (1.f + expf(-R)); |
| 552 | U = 1.f / (1.f + expf(-U)); |
| 553 | |
| 554 | // gate new |
| 555 | float N = bias_c_RUBNWN[2]; |
| 556 | |
| 557 | for (int i = 0; i < num_output; i++) |
| 558 | { |
| 559 | float h_cont = hidden_state[i]; |
| 560 | |
| 561 | N += weight_hc_RUN[0] * h_cont; |
| 562 | |
| 563 | weight_hc_RUN += 1; |
| 564 | } |
| 565 | |
| 566 | N = bias_c_RUBNWN[3] + R * N; |
| 567 | |
| 568 | for (int i = 0; i < size; i++) |
| 569 | { |
| 570 | float xi = x[i]; |
| 571 | |
| 572 | N += weight_xc_RUN[0] * xi; |
| 573 | |
| 574 | weight_xc_RUN += 1; |
| 575 | } |
| 576 | |
| 577 | // tanh(N) |
| 578 | N = tanhf(N); |
| 579 | |
| 580 | #if __ARM_NEON |
| 581 | float* gates_data = gates.row(q / 4 + q % 4); |
| 582 | #else |
| 583 | float* gates_data = gates.row(q); |
| 584 | #endif |
| 585 | |
| 586 | gates_data[0] = U; |
| 587 | gates_data[1] = N; |
| 588 | } |
| 589 | |
| 590 | // h_t := (1 - update) .* new + update .* h_{t-1} |
| 591 | float* output_data = top_blob.row(ti); |
| 592 | |
| 593 | float* hidden_ptr = hidden_state; |
| 594 | |
| 595 | #if __ARM_NEON |
| 596 | nn_num_output = num_output >> 2; |
| 597 | remain_num_output_start = nn_num_output << 2; |
| 598 | |
| 599 | #pragma omp parallel for num_threads(opt.num_threads) |
| 600 | for (int qq = 0; qq < nn_num_output; qq++) |
| 601 | { |
| 602 | int q = qq * 4; |
| 603 | |
| 604 | const float* gates_data = gates.row(q / 4); |
| 605 | |
| 606 | float32x4_t _gru_U = vld1q_f32(gates_data); |
| 607 | float32x4_t _gru_N = vld1q_f32(gates_data + 4); |
| 608 | |
| 609 | 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))); |
| 610 | |
| 611 | vst1q_f32(hidden_ptr + q, _gru_H); |
| 612 | vst1q_f32(output_data + q, _gru_H); |
| 613 | } |
| 614 | #endif // __ARM_NEON |
| 615 | #pragma omp parallel for num_threads(opt.num_threads) |
| 616 | for (int q = remain_num_output_start; q < num_output; q++) |
| 617 | { |
| 618 | #if __ARM_NEON |
| 619 | const float* gates_data = gates.row(q / 4 + q % 4); |
| 620 | #else |
| 621 | const float* gates_data = gates.row(q); |
| 622 | #endif |
| 623 | |
| 624 | float U = gates_data[0]; |
| 625 | float N = gates_data[1]; |
| 626 | |
| 627 | float H = (1 - U) * N + U * hidden_ptr[q]; |
| 628 | |
| 629 | hidden_ptr[q] = H; |
| 630 | output_data[q] = H; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | int GRU_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 638 | { |
| 639 | #if NCNN_INT8 |
| 640 | if (int8_scale_term) |
| 641 | { |
| 642 | return forward_int8(bottom_blob, top_blob, opt); |
| 643 | } |
| 644 | #endif |
| 645 | |
| 646 | int elembits = bottom_blob.elembits(); |
| 647 | |
| 648 | #if NCNN_ARM82 |
| 649 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 650 | return forward_fp16s(bottom_blob, top_blob, opt); |
| 651 | #endif |
| 652 | |
| 653 | #if NCNN_BF16 |
| 654 | if (opt.use_bf16_storage && elembits == 16) |
| 655 | return forward_bf16s(bottom_blob, top_blob, opt); |
| 656 | #endif |
| 657 | |
| 658 | int T = bottom_blob.h; |
| 659 | |
| 660 | int num_directions = direction == 2 ? 2 : 1; |
| 661 | |
| 662 | // initial hidden state |
| 663 | Mat hidden(num_output, 4u, opt.workspace_allocator); |
| 664 | if (hidden.empty()) |
| 665 | return -100; |
| 666 | hidden.fill(0.f); |
| 667 | |
| 668 | top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator); |
| 669 | if (top_blob.empty()) |
| 670 | return -100; |
| 671 | |
| 672 | // Uni directional |
| 673 | if (direction == 0 || direction == 1) |
| 674 | { |
| 675 | int ret = gru(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); |
| 676 | if (ret != 0) |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | if (direction == 2) |
| 681 | { |
| 682 | Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator); |
| 683 | if (top_blob_forward.empty()) |
| 684 | return -100; |
| 685 | |
| 686 | Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator); |
| 687 | if (top_blob_reverse.empty()) |
| 688 | return -100; |
| 689 | |
| 690 | { |
| 691 | int ret = gru(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); |
| 692 | if (ret != 0) |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | hidden.fill(0.0f); |
| 697 | |
| 698 | { |
| 699 | int ret = gru(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); |
| 700 | if (ret != 0) |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | // concat w |
| 705 | for (int i = 0; i < T; i++) |
| 706 | { |
| 707 | const float* pf = top_blob_forward.row(i); |
| 708 | const float* pr = top_blob_reverse.row(i); |
| 709 | float* ptr = top_blob.row(i); |
| 710 | |
| 711 | memcpy(ptr, pf, num_output * sizeof(float)); |
| 712 | memcpy(ptr + num_output, pr, num_output * sizeof(float)); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | int GRU_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 720 | { |
| 721 | #if NCNN_INT8 |
| 722 | if (int8_scale_term) |
| 723 | { |
| 724 | return forward_int8(bottom_blobs, top_blobs, opt); |
| 725 | } |
| 726 | #endif |
| 727 | |
| 728 | const Mat& bottom_blob = bottom_blobs[0]; |
| 729 | int elembits = bottom_blob.elembits(); |
| 730 | |
| 731 | #if NCNN_ARM82 |
| 732 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 733 | return forward_fp16s(bottom_blobs, top_blobs, opt); |
| 734 | #endif |
| 735 | |
| 736 | #if NCNN_BF16 |
| 737 | if (opt.use_bf16_storage && elembits == 16) |
| 738 | return forward_bf16s(bottom_blobs, top_blobs, opt); |
| 739 | #endif |
| 740 | |
| 741 | int T = bottom_blob.h; |
| 742 | int num_directions = direction == 2 ? 2 : 1; |
| 743 | |
| 744 | Mat hidden; |
| 745 | Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator; |
| 746 | if (bottom_blobs.size() == 2) |
| 747 | { |
| 748 | hidden = bottom_blobs[1].clone(hidden_allocator); |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | hidden.create(num_output, num_directions, 4u, hidden_allocator); |
| 753 | if (hidden.empty()) |
| 754 | return -100; |
| 755 | hidden.fill(0.f); |
| 756 | } |
| 757 | |
| 758 | Mat& top_blob = top_blobs[0]; |
| 759 | top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator); |
| 760 | if (top_blob.empty()) |
| 761 | return -100; |
| 762 | |
| 763 | // Uni directional |
| 764 | if (direction == 0 || direction == 1) |
| 765 | { |
| 766 | int ret = gru(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); |
| 767 | if (ret != 0) |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | if (direction == 2) |
| 772 | { |
| 773 | Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator); |
| 774 | if (top_blob_forward.empty()) |
| 775 | return -100; |
| 776 | |
| 777 | Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator); |
| 778 | if (top_blob_reverse.empty()) |
| 779 | return -100; |
| 780 | |
| 781 | Mat hidden0 = hidden.row_range(0, 1); |
| 782 | { |
| 783 | int ret = gru(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); |
| 784 | if (ret != 0) |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | Mat hidden1 = hidden.row_range(1, 1); |
| 789 | { |
| 790 | int ret = gru(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); |
| 791 | if (ret != 0) |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | // concat w |
| 796 | for (int i = 0; i < T; i++) |
| 797 | { |
| 798 | const float* pf = top_blob_forward.row(i); |
| 799 | const float* pr = top_blob_reverse.row(i); |
| 800 | float* ptr = top_blob.row(i); |
| 801 | |
| 802 | memcpy(ptr, pf, num_output * sizeof(float)); |
| 803 | memcpy(ptr + num_output, pr, num_output * sizeof(float)); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if (top_blobs.size() == 2) |
| 808 | { |
| 809 | top_blobs[1] = hidden; |
| 810 | } |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | #if NCNN_BF16 |
| 816 | static int gru_bf16s(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) |
| 817 | { |
| 818 | int size = bottom_blob.w; |
| 819 | int T = bottom_blob.h; |
| 820 | |
| 821 | int num_output = top_blob.w; |
| 822 | |
| 823 | // 2 x num_output |
| 824 | #if __ARM_NEON |
| 825 | Mat gates(4 * 2, num_output / 4 + num_output % 4, 4u, opt.workspace_allocator); |
| 826 | #else |
| 827 | Mat gates(2, num_output, 4u, opt.workspace_allocator); |
| 828 | #endif |
| 829 | if (gates.empty()) |
| 830 | return -100; |
| 831 | |
| 832 | // unroll |
| 833 | for (int t = 0; t < T; t++) |
| 834 | { |
| 835 | int ti = reverse ? T - 1 - t : t; |
| 836 | |
| 837 | int remain_num_output_start = 0; |
| 838 | #if __ARM_NEON |
| 839 | int nn_num_output = num_output >> 2; |
| 840 | remain_num_output_start = nn_num_output << 2; |
| 841 | |
| 842 | #pragma omp parallel for num_threads(opt.num_threads) |
| 843 | for (int qq = 0; qq < nn_num_output; qq++) |
| 844 | { |
| 845 | int q = qq * 4; |
| 846 | |
| 847 | const unsigned short* x = bottom_blob.row<const unsigned short>(ti); |
| 848 | |
| 849 | // gate reset update |
| 850 | const unsigned short* bias_c_RUBNWN = (const unsigned short*)bias_c + q * 4; |
| 851 | |
| 852 | const unsigned short* weight_xc_RUN = weight_xc.row<const unsigned short>(q / 4); |
| 853 | const unsigned short* weight_hc_RUN = weight_hc.row<const unsigned short>(q / 4); |
| 854 | |
| 855 | float32x4_t _gru_R = bfloat2float(vld1_u16(bias_c_RUBNWN)); |
| 856 | float32x4_t _gru_U = bfloat2float(vld1_u16(bias_c_RUBNWN + 4)); |
| 857 | float32x4_t _sum1 = vdupq_n_f32(0.f); |
| 858 | float32x4_t _sum2 = vdupq_n_f32(0.f); |
| 859 | float32x4_t _sum3 = vdupq_n_f32(0.f); |
| 860 | float32x4_t _sum4 = vdupq_n_f32(0.f); |
| 861 | float32x4_t _sum5 = vdupq_n_f32(0.f); |
| 862 | float32x4_t _sum6 = vdupq_n_f32(0.f); |
| 863 | |
| 864 | int i = 0; |
| 865 | for (; i + 3 < size; i += 4) |
| 866 | { |
| 867 | float32x4_t _xi = bfloat2float(vld1_u16(x + i)); |
| 868 | float32x4_t _weight_xc_R = bfloat2float(vld1_u16(weight_xc_RUN)); |
| 869 | float32x4_t _weight_xc_U = bfloat2float(vld1_u16(weight_xc_RUN + 4)); |
| 870 | float32x4_t _weight_xc_R_1 = bfloat2float(vld1_u16(weight_xc_RUN + 8)); |
| 871 | float32x4_t _weight_xc_U_1 = bfloat2float(vld1_u16(weight_xc_RUN + 12)); |
| 872 | float32x4_t _weight_xc_R_2 = bfloat2float(vld1_u16(weight_xc_RUN + 16)); |
| 873 | float32x4_t _weight_xc_U_2 = bfloat2float(vld1_u16(weight_xc_RUN + 20)); |
| 874 | float32x4_t _weight_xc_R_3 = bfloat2float(vld1_u16(weight_xc_RUN + 24)); |
| 875 | float32x4_t _weight_xc_U_3 = bfloat2float(vld1_u16(weight_xc_RUN + 28)); |
| 876 | #if __aarch64__ |
| 877 | _gru_R = vfmaq_laneq_f32(_gru_R, _weight_xc_R, _xi, 0); |
| 878 | _gru_U = vfmaq_laneq_f32(_gru_U, _weight_xc_U, _xi, 0); |
| 879 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_R_1, _xi, 1); |
| 880 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_U_1, _xi, 1); |
| 881 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_R_2, _xi, 2); |
| 882 | _sum4 = vfmaq_laneq_f32(_sum4, _weight_xc_U_2, _xi, 2); |
| 883 | _sum5 = vfmaq_laneq_f32(_sum5, _weight_xc_R_3, _xi, 3); |
| 884 | _sum6 = vfmaq_laneq_f32(_sum6, _weight_xc_U_3, _xi, 3); |
| 885 | #else |
| 886 | _gru_R = vmlaq_lane_f32(_gru_R, _weight_xc_R, vget_low_f32(_xi), 0); |
| 887 | _gru_U = vmlaq_lane_f32(_gru_U, _weight_xc_U, vget_low_f32(_xi), 0); |
| 888 | _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_R_1, vget_low_f32(_xi), 1); |
| 889 | _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_U_1, vget_low_f32(_xi), 1); |
| 890 | _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_R_2, vget_high_f32(_xi), 0); |
| 891 | _sum4 = vmlaq_lane_f32(_sum4, _weight_xc_U_2, vget_high_f32(_xi), 0); |
| 892 | _sum5 = vmlaq_lane_f32(_sum5, _weight_xc_R_3, vget_high_f32(_xi), 1); |
| 893 | _sum6 = vmlaq_lane_f32(_sum6, _weight_xc_U_3, vget_high_f32(_xi), 1); |
| 894 | #endif |
| 895 | |
| 896 | weight_xc_RUN += 32; |
| 897 | } |
| 898 | for (; i < size; i++) |
| 899 | { |
| 900 | unsigned short xi = x[i]; |
| 901 | |
| 902 | float32x4_t _xi = bfloat2float(vdup_n_u16(xi)); |
| 903 | float32x4_t _weight_xc_R = bfloat2float(vld1_u16(weight_xc_RUN)); |
| 904 | float32x4_t _weight_xc_U = bfloat2float(vld1_u16(weight_xc_RUN + 4)); |
| 905 | _gru_R = vmlaq_f32(_gru_R, _weight_xc_R, _xi); |
| 906 | _gru_U = vmlaq_f32(_gru_U, _weight_xc_U, _xi); |
| 907 | |
| 908 | weight_xc_RUN += 8; |
| 909 | } |
| 910 | |
| 911 | i = 0; |
| 912 | for (; i + 3 < num_output; i += 4) |
| 913 | { |
| 914 | float32x4_t _h_cont = vld1q_f32((const float*)hidden_state + i); |
| 915 | float32x4_t _weight_hc_R = bfloat2float(vld1_u16(weight_hc_RUN)); |
| 916 | float32x4_t _weight_hc_U = bfloat2float(vld1_u16(weight_hc_RUN + 4)); |
| 917 | float32x4_t _weight_hc_R_1 = bfloat2float(vld1_u16(weight_hc_RUN + 8)); |
| 918 | float32x4_t _weight_hc_U_1 = bfloat2float(vld1_u16(weight_hc_RUN + 12)); |
| 919 | float32x4_t _weight_hc_R_2 = bfloat2float(vld1_u16(weight_hc_RUN + 16)); |
| 920 | float32x4_t _weight_hc_U_2 = bfloat2float(vld1_u16(weight_hc_RUN + 20)); |
| 921 | float32x4_t _weight_hc_R_3 = bfloat2float(vld1_u16(weight_hc_RUN + 24)); |
| 922 | float32x4_t _weight_hc_U_3 = bfloat2float(vld1_u16(weight_hc_RUN + 28)); |
| 923 | #if __aarch64__ |
| 924 | _gru_R = vfmaq_laneq_f32(_gru_R, _weight_hc_R, _h_cont, 0); |
| 925 | _gru_U = vfmaq_laneq_f32(_gru_U, _weight_hc_U, _h_cont, 0); |
| 926 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_R_1, _h_cont, 1); |
| 927 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_U_1, _h_cont, 1); |
| 928 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_R_2, _h_cont, 2); |
| 929 | _sum4 = vfmaq_laneq_f32(_sum4, _weight_hc_U_2, _h_cont, 2); |
| 930 | _sum5 = vfmaq_laneq_f32(_sum5, _weight_hc_R_3, _h_cont, 3); |
| 931 | _sum6 = vfmaq_laneq_f32(_sum6, _weight_hc_U_3, _h_cont, 3); |
| 932 | #else |
| 933 | _gru_R = vmlaq_lane_f32(_gru_R, _weight_hc_R, vget_low_f32(_h_cont), 0); |
| 934 | _gru_U = vmlaq_lane_f32(_gru_U, _weight_hc_U, vget_low_f32(_h_cont), 0); |
| 935 | _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_R_1, vget_low_f32(_h_cont), 1); |
| 936 | _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_U_1, vget_low_f32(_h_cont), 1); |
| 937 | _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_R_2, vget_high_f32(_h_cont), 0); |
| 938 | _sum4 = vmlaq_lane_f32(_sum4, _weight_hc_U_2, vget_high_f32(_h_cont), 0); |
| 939 | _sum5 = vmlaq_lane_f32(_sum5, _weight_hc_R_3, vget_high_f32(_h_cont), 1); |
| 940 | _sum6 = vmlaq_lane_f32(_sum6, _weight_hc_U_3, vget_high_f32(_h_cont), 1); |
| 941 | #endif |
| 942 | |
| 943 | weight_hc_RUN += 32; |
| 944 | } |
| 945 | for (; i < num_output; i++) |
| 946 | { |
| 947 | float h_cont = hidden_state[i]; |
| 948 | |
| 949 | float32x4_t _h_cont = vdupq_n_f32(h_cont); |
| 950 | float32x4_t _weight_hc_R = bfloat2float(vld1_u16(weight_hc_RUN)); |
| 951 | float32x4_t _weight_hc_U = bfloat2float(vld1_u16(weight_hc_RUN + 4)); |
| 952 | _gru_R = vmlaq_f32(_gru_R, _weight_hc_R, _h_cont); |
| 953 | _gru_U = vmlaq_f32(_gru_U, _weight_hc_U, _h_cont); |
| 954 | |
| 955 | weight_hc_RUN += 8; |
| 956 | } |
| 957 | |
| 958 | _gru_R = vaddq_f32(_gru_R, _sum1); |
| 959 | _gru_U = vaddq_f32(_gru_U, _sum2); |
| 960 | _sum3 = vaddq_f32(_sum3, _sum5); |
| 961 | _sum4 = vaddq_f32(_sum4, _sum6); |
| 962 | _gru_R = vaddq_f32(_gru_R, _sum3); |
| 963 | _gru_U = vaddq_f32(_gru_U, _sum4); |
| 964 | |
| 965 | // sigmoid(R) |
| 966 | // sigmoid(U) |
| 967 | _gru_R = sigmoid_ps(_gru_R); |
| 968 | _gru_U = sigmoid_ps(_gru_U); |
| 969 | |
| 970 | // gate new |
| 971 | float32x4_t _gru_N = bfloat2float(vld1_u16(bias_c_RUBNWN + 8)); |
| 972 | _sum1 = vdupq_n_f32(0.f); |
| 973 | _sum2 = vdupq_n_f32(0.f); |
| 974 | _sum3 = vdupq_n_f32(0.f); |
| 975 | |
| 976 | i = 0; |
| 977 | for (; i + 3 < num_output; i += 4) |
| 978 | { |
| 979 | float32x4_t _h_cont = vld1q_f32((const float*)hidden_state + i); |
| 980 | float32x4_t _weight_hc_N = bfloat2float(vld1_u16(weight_hc_RUN)); |
| 981 | float32x4_t _weight_hc_N_1 = bfloat2float(vld1_u16(weight_hc_RUN + 4)); |
| 982 | float32x4_t _weight_hc_N_2 = bfloat2float(vld1_u16(weight_hc_RUN + 8)); |
| 983 | float32x4_t _weight_hc_N_3 = bfloat2float(vld1_u16(weight_hc_RUN + 12)); |
| 984 | #if __aarch64__ |
| 985 | _gru_N = vfmaq_laneq_f32(_gru_N, _weight_hc_N, _h_cont, 0); |
| 986 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_hc_N_1, _h_cont, 1); |
| 987 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_hc_N_2, _h_cont, 2); |
| 988 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_hc_N_3, _h_cont, 3); |
| 989 | #else |
| 990 | _gru_N = vmlaq_lane_f32(_gru_N, _weight_hc_N, vget_low_f32(_h_cont), 0); |
| 991 | _sum1 = vmlaq_lane_f32(_sum1, _weight_hc_N_1, vget_low_f32(_h_cont), 1); |
| 992 | _sum2 = vmlaq_lane_f32(_sum2, _weight_hc_N_2, vget_high_f32(_h_cont), 0); |
| 993 | _sum3 = vmlaq_lane_f32(_sum3, _weight_hc_N_3, vget_high_f32(_h_cont), 1); |
| 994 | #endif |
| 995 | |
| 996 | weight_hc_RUN += 16; |
| 997 | } |
| 998 | for (; i < num_output; i++) |
| 999 | { |
| 1000 | float h_cont = hidden_state[i]; |
| 1001 | |
| 1002 | float32x4_t _h_cont = vdupq_n_f32(h_cont); |
| 1003 | float32x4_t _weight_hc_N = bfloat2float(vld1_u16(weight_hc_RUN)); |
| 1004 | _gru_N = vmlaq_f32(_gru_N, _weight_hc_N, _h_cont); |
| 1005 | |
| 1006 | weight_hc_RUN += 4; |
| 1007 | } |
| 1008 | |
| 1009 | _gru_N = vaddq_f32(_gru_N, _sum1); |
| 1010 | _sum2 = vaddq_f32(_sum2, _sum3); |
| 1011 | _gru_N = vaddq_f32(_gru_N, _sum2); |
| 1012 | |
| 1013 | _gru_N = vmlaq_f32(bfloat2float(vld1_u16(bias_c_RUBNWN + 12)), _gru_R, _gru_N); |
| 1014 | _sum1 = vdupq_n_f32(0.f); |
| 1015 | _sum2 = vdupq_n_f32(0.f); |
| 1016 | _sum3 = vdupq_n_f32(0.f); |
| 1017 | |
| 1018 | i = 0; |
| 1019 | for (; i + 3 < size; i += 4) |
| 1020 | { |
| 1021 | float32x4_t _xi = bfloat2float(vld1_u16(x + i)); |
| 1022 | float32x4_t _weight_xc_N = bfloat2float(vld1_u16(weight_xc_RUN)); |
| 1023 | float32x4_t _weight_xc_N_1 = bfloat2float(vld1_u16(weight_xc_RUN + 4)); |
| 1024 | float32x4_t _weight_xc_N_2 = bfloat2float(vld1_u16(weight_xc_RUN + 8)); |
| 1025 | float32x4_t _weight_xc_N_3 = bfloat2float(vld1_u16(weight_xc_RUN + 12)); |
| 1026 | #if __aarch64__ |
| 1027 | _gru_N = vfmaq_laneq_f32(_gru_N, _weight_xc_N, _xi, 0); |
| 1028 | _sum1 = vfmaq_laneq_f32(_sum1, _weight_xc_N_1, _xi, 1); |
| 1029 | _sum2 = vfmaq_laneq_f32(_sum2, _weight_xc_N_2, _xi, 2); |
| 1030 | _sum3 = vfmaq_laneq_f32(_sum3, _weight_xc_N_3, _xi, 3); |
| 1031 | #else |
| 1032 | _gru_N = vmlaq_lane_f32(_gru_N, _weight_xc_N, vget_low_f32(_xi), 0); |
| 1033 | _sum1 = vmlaq_lane_f32(_sum1, _weight_xc_N_1, vget_low_f32(_xi), 1); |
| 1034 | _sum2 = vmlaq_lane_f32(_sum2, _weight_xc_N_2, vget_high_f32(_xi), 0); |
| 1035 | _sum3 = vmlaq_lane_f32(_sum3, _weight_xc_N_3, vget_high_f32(_xi), 1); |
| 1036 | #endif |
| 1037 | |
| 1038 | weight_xc_RUN += 16; |
| 1039 | } |
| 1040 | for (; i < size; i++) |
| 1041 | { |
| 1042 | unsigned short xi = x[i]; |
| 1043 | |
| 1044 | float32x4_t _xi = bfloat2float(vdup_n_u16(xi)); |
| 1045 | float32x4_t _weight_xc_N = bfloat2float(vld1_u16(weight_xc_RUN)); |
| 1046 | _gru_N = vmlaq_f32(_gru_N, _weight_xc_N, _xi); |
| 1047 | |
| 1048 | weight_xc_RUN += 4; |
| 1049 | } |
| 1050 | |
| 1051 | _gru_N = vaddq_f32(_gru_N, _sum1); |
| 1052 | _sum2 = vaddq_f32(_sum2, _sum3); |
| 1053 | _gru_N = vaddq_f32(_gru_N, _sum2); |
| 1054 | |
| 1055 | // tanh(N) |
| 1056 | _gru_N = tanh_ps(_gru_N); |
| 1057 | |
| 1058 | float* gates_data = gates.row(q / 4); |
| 1059 | |
| 1060 | vst1q_f32(gates_data, _gru_U); |
| 1061 | vst1q_f32(gates_data + 4, _gru_N); |
| 1062 | } |
| 1063 | #endif // __ARM_NEON |
| 1064 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1065 | for (int q = remain_num_output_start; q < num_output; q++) |
| 1066 | { |
| 1067 | const unsigned short* x = bottom_blob.row<const unsigned short>(ti); |
| 1068 | |
| 1069 | // gate reset update |
| 1070 | const unsigned short* bias_c_RUBNWN = (const unsigned short*)bias_c + q * 4; |
| 1071 | |
| 1072 | #if __ARM_NEON |
| 1073 | const unsigned short* weight_xc_RUN = weight_xc.row<const unsigned short>(q / 4 + q % 4); |
| 1074 | const unsigned short* weight_hc_RUN = weight_hc.row<const unsigned short>(q / 4 + q % 4); |
| 1075 | #else |
| 1076 | const unsigned short* weight_xc_RUN = weight_xc.row<const unsigned short>(q); |
| 1077 | const unsigned short* weight_hc_RUN = weight_hc.row<const unsigned short>(q); |
| 1078 | #endif |
| 1079 | |
| 1080 | float R = bfloat16_to_float32(bias_c_RUBNWN[0]); |
| 1081 | float U = bfloat16_to_float32(bias_c_RUBNWN[1]); |
| 1082 | |
| 1083 | for (int i = 0; i < size; i++) |
| 1084 | { |
| 1085 | float xi = bfloat16_to_float32(x[i]); |
| 1086 | |
| 1087 | R += bfloat16_to_float32(weight_xc_RUN[0]) * xi; |
| 1088 | U += bfloat16_to_float32(weight_xc_RUN[1]) * xi; |
| 1089 | |
| 1090 | weight_xc_RUN += 2; |
| 1091 | } |
| 1092 | |
| 1093 | for (int i = 0; i < num_output; i++) |
| 1094 | { |
| 1095 | float h_cont = hidden_state[i]; |
| 1096 | |
| 1097 | R += bfloat16_to_float32(weight_hc_RUN[0]) * h_cont; |
| 1098 | U += bfloat16_to_float32(weight_hc_RUN[1]) * h_cont; |
| 1099 | |
| 1100 | weight_hc_RUN += 2; |
| 1101 | } |
| 1102 | |
| 1103 | // sigmoid(R) |
| 1104 | // sigmoid(U) |
| 1105 | R = 1.f / (1.f + expf(-R)); |
| 1106 | U = 1.f / (1.f + expf(-U)); |
| 1107 | |
| 1108 | // gate new |
| 1109 | float N = bfloat16_to_float32(bias_c_RUBNWN[2]); |
| 1110 | |
| 1111 | for (int i = 0; i < num_output; i++) |
| 1112 | { |
| 1113 | float h_cont = hidden_state[i]; |
| 1114 | |
| 1115 | N += bfloat16_to_float32(weight_hc_RUN[0]) * h_cont; |
| 1116 | |
| 1117 | weight_hc_RUN += 1; |
| 1118 | } |
| 1119 | |
| 1120 | N = bfloat16_to_float32(bias_c_RUBNWN[3]) + R * N; |
| 1121 | |
| 1122 | for (int i = 0; i < size; i++) |
| 1123 | { |
| 1124 | float xi = bfloat16_to_float32(x[i]); |
| 1125 | |
| 1126 | N += bfloat16_to_float32(weight_xc_RUN[0]) * xi; |
| 1127 | |
| 1128 | weight_xc_RUN += 1; |
| 1129 | } |
| 1130 | |
| 1131 | // tanh(N) |
| 1132 | N = tanhf(N); |
| 1133 | |
| 1134 | #if __ARM_NEON |
| 1135 | float* gates_data = gates.row(q / 4 + q % 4); |
| 1136 | #else |
| 1137 | float* gates_data = gates.row(q); |
| 1138 | #endif |
| 1139 | |
| 1140 | gates_data[0] = U; |
| 1141 | gates_data[1] = N; |
| 1142 | } |
| 1143 | |
| 1144 | // h_t := (1 - update) .* new + update .* h_{t-1} |
| 1145 | unsigned short* output_data = top_blob.row<unsigned short>(ti); |
| 1146 | |
| 1147 | float* hidden_ptr = hidden_state; |
| 1148 | |
| 1149 | #if __ARM_NEON |
| 1150 | nn_num_output = num_output >> 2; |
| 1151 | remain_num_output_start = nn_num_output << 2; |
| 1152 | |
| 1153 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1154 | for (int qq = 0; qq < nn_num_output; qq++) |
| 1155 | { |
| 1156 | int q = qq * 4; |
| 1157 | |
| 1158 | const float* gates_data = gates.row(q / 4); |
| 1159 | |
| 1160 | float32x4_t _gru_U = vld1q_f32(gates_data); |
| 1161 | float32x4_t _gru_N = vld1q_f32(gates_data + 4); |
| 1162 | |
| 1163 | 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))); |
| 1164 | |
| 1165 | vst1q_f32(hidden_ptr + q, _gru_H); |
| 1166 | vst1_u16(output_data + q, float2bfloat(_gru_H)); |
| 1167 | } |
| 1168 | #endif // __ARM_NEON |
| 1169 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1170 | for (int q = remain_num_output_start; q < num_output; q++) |
| 1171 | { |
| 1172 | #if __ARM_NEON |
| 1173 | const float* gates_data = gates.row(q / 4 + q % 4); |
| 1174 | #else |
| 1175 | const float* gates_data = gates.row(q); |
| 1176 | #endif |
| 1177 | |
| 1178 | float U = gates_data[0]; |
| 1179 | float N = gates_data[1]; |
| 1180 | |
| 1181 | float H = (1 - U) * N + U * hidden_ptr[q]; |
| 1182 | |
| 1183 | hidden_ptr[q] = H; |
| 1184 | output_data[q] = float32_to_bfloat16(H); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | int GRU_arm::create_pipeline_bf16s(const Option& opt) |
| 1192 | { |
| 1193 | // pack RUN |
| 1194 | int num_directions = direction == 2 ? 2 : 1; |
| 1195 | int size = weight_data_size / num_directions / num_output / 3; |
| 1196 | |
| 1197 | #if __ARM_NEON |
| 1198 | weight_xc_data_packed.create(size * 12, num_output / 4 + num_output % 4, num_directions, 2u, 1); |
| 1199 | bias_c_data_packed.create(num_output, 1, num_directions, 8u, 4); |
| 1200 | weight_hc_data_packed.create(num_output * 12, num_output / 4 + num_output % 4, num_directions, 2u, 1); |
| 1201 | #else |
| 1202 | weight_xc_data_packed.create(size * 3, num_output, num_directions, 2u, 1); |
| 1203 | bias_c_data_packed.create(num_output, 1, num_directions, 8u, 4); |
| 1204 | weight_hc_data_packed.create(num_output * 3, num_output, num_directions, 2u, 1); |
| 1205 | #endif |
| 1206 | |
| 1207 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1208 | for (int dr = 0; dr < num_directions; dr++) |
| 1209 | { |
| 1210 | const Mat weight_xc = weight_xc_data.channel(dr); |
| 1211 | const Mat bias_c = bias_c_data.channel(dr); |
| 1212 | const Mat weight_hc = weight_hc_data.channel(dr); |
| 1213 | |
| 1214 | Mat weight_xc_data_packed_dr = weight_xc_data_packed.channel(dr); |
| 1215 | Mat bias_c_data_packed_dr = bias_c_data_packed.channel(dr); |
| 1216 | Mat weight_hc_data_packed_dr = weight_hc_data_packed.channel(dr); |
| 1217 | |
| 1218 | const float* bias_c_R = bias_c.row(0); |
| 1219 | const float* bias_c_U = bias_c.row(1); |
| 1220 | const float* bias_c_WN = bias_c.row(2); |
| 1221 | const float* bias_c_BN = bias_c.row(3); |
| 1222 | |
| 1223 | unsigned short* bias_c_RUBNWN = bias_c_data_packed_dr.row<unsigned short>(0); |
| 1224 | |
| 1225 | int q = 0; |
| 1226 | #if __ARM_NEON |
| 1227 | for (; q + 3 < num_output; q += 4) |
| 1228 | { |
| 1229 | vst1_u16(bias_c_RUBNWN, float2bfloat(vld1q_f32(bias_c_R + q))); |
| 1230 | vst1_u16(bias_c_RUBNWN + 4, float2bfloat(vld1q_f32(bias_c_U + q))); |
| 1231 | vst1_u16(bias_c_RUBNWN + 8, float2bfloat(vld1q_f32(bias_c_BN + q))); |
| 1232 | vst1_u16(bias_c_RUBNWN + 12, float2bfloat(vld1q_f32(bias_c_WN + q))); |
| 1233 | |
| 1234 | bias_c_RUBNWN += 16; |
| 1235 | |
| 1236 | const float* weight_xc_R = weight_xc.row(num_output * 0 + q); |
| 1237 | const float* weight_xc_U = weight_xc.row(num_output * 1 + q); |
| 1238 | const float* weight_xc_N = weight_xc.row(num_output * 2 + q); |
| 1239 | |
| 1240 | const float* weight_xc_R_1 = weight_xc.row(num_output * 0 + q + 1); |
| 1241 | const float* weight_xc_U_1 = weight_xc.row(num_output * 1 + q + 1); |
| 1242 | const float* weight_xc_N_1 = weight_xc.row(num_output * 2 + q + 1); |
| 1243 | |
| 1244 | const float* weight_xc_R_2 = weight_xc.row(num_output * 0 + q + 2); |
| 1245 | const float* weight_xc_U_2 = weight_xc.row(num_output * 1 + q + 2); |
| 1246 | const float* weight_xc_N_2 = weight_xc.row(num_output * 2 + q + 2); |
| 1247 | |
| 1248 | const float* weight_xc_R_3 = weight_xc.row(num_output * 0 + q + 3); |
| 1249 | const float* weight_xc_U_3 = weight_xc.row(num_output * 1 + q + 3); |
| 1250 | const float* weight_xc_N_3 = weight_xc.row(num_output * 2 + q + 3); |
| 1251 | |
| 1252 | const float* weight_hc_R = weight_hc.row(num_output * 0 + q); |
| 1253 | const float* weight_hc_U = weight_hc.row(num_output * 1 + q); |
| 1254 | const float* weight_hc_N = weight_hc.row(num_output * 2 + q); |
| 1255 | |
| 1256 | const float* weight_hc_R_1 = weight_hc.row(num_output * 0 + q + 1); |
| 1257 | const float* weight_hc_U_1 = weight_hc.row(num_output * 1 + q + 1); |
| 1258 | const float* weight_hc_N_1 = weight_hc.row(num_output * 2 + q + 1); |
| 1259 | |
| 1260 | const float* weight_hc_R_2 = weight_hc.row(num_output * 0 + q + 2); |
| 1261 | const float* weight_hc_U_2 = weight_hc.row(num_output * 1 + q + 2); |
| 1262 | const float* weight_hc_N_2 = weight_hc.row(num_output * 2 + q + 2); |
| 1263 | |
| 1264 | const float* weight_hc_R_3 = weight_hc.row(num_output * 0 + q + 3); |
| 1265 | const float* weight_hc_U_3 = weight_hc.row(num_output * 1 + q + 3); |
| 1266 | const float* weight_hc_N_3 = weight_hc.row(num_output * 2 + q + 3); |
| 1267 | |
| 1268 | unsigned short* weight_xc_RUN = weight_xc_data_packed_dr.row<unsigned short>(q / 4); |
| 1269 | unsigned short* weight_hc_RUN = weight_hc_data_packed_dr.row<unsigned short>(q / 4); |
| 1270 | |
| 1271 | for (int i = 0; i < size; i++) |
| 1272 | { |
| 1273 | weight_xc_RUN[0] = float32_to_bfloat16(weight_xc_R[i]); |
| 1274 | weight_xc_RUN[1] = float32_to_bfloat16(weight_xc_R_1[i]); |
| 1275 | weight_xc_RUN[2] = float32_to_bfloat16(weight_xc_R_2[i]); |
| 1276 | weight_xc_RUN[3] = float32_to_bfloat16(weight_xc_R_3[i]); |
| 1277 | weight_xc_RUN[4] = float32_to_bfloat16(weight_xc_U[i]); |
| 1278 | weight_xc_RUN[5] = float32_to_bfloat16(weight_xc_U_1[i]); |
| 1279 | weight_xc_RUN[6] = float32_to_bfloat16(weight_xc_U_2[i]); |
| 1280 | weight_xc_RUN[7] = float32_to_bfloat16(weight_xc_U_3[i]); |
| 1281 | |
| 1282 | weight_xc_RUN += 8; |
| 1283 | } |
| 1284 | |
| 1285 | for (int i = 0; i < num_output; i++) |
| 1286 | { |
| 1287 | weight_hc_RUN[0] = float32_to_bfloat16(weight_hc_R[i]); |
| 1288 | weight_hc_RUN[1] = float32_to_bfloat16(weight_hc_R_1[i]); |
| 1289 | weight_hc_RUN[2] = float32_to_bfloat16(weight_hc_R_2[i]); |
| 1290 | weight_hc_RUN[3] = float32_to_bfloat16(weight_hc_R_3[i]); |
| 1291 | weight_hc_RUN[4] = float32_to_bfloat16(weight_hc_U[i]); |
| 1292 | weight_hc_RUN[5] = float32_to_bfloat16(weight_hc_U_1[i]); |
| 1293 | weight_hc_RUN[6] = float32_to_bfloat16(weight_hc_U_2[i]); |
| 1294 | weight_hc_RUN[7] = float32_to_bfloat16(weight_hc_U_3[i]); |
| 1295 | |
| 1296 | weight_hc_RUN += 8; |
| 1297 | } |
| 1298 | |
| 1299 | for (int i = 0; i < size; i++) |
| 1300 | { |
| 1301 | weight_xc_RUN[0] = float32_to_bfloat16(weight_xc_N[i]); |
| 1302 | weight_xc_RUN[1] = float32_to_bfloat16(weight_xc_N_1[i]); |
| 1303 | weight_xc_RUN[2] = float32_to_bfloat16(weight_xc_N_2[i]); |
| 1304 | weight_xc_RUN[3] = float32_to_bfloat16(weight_xc_N_3[i]); |
| 1305 | |
| 1306 | weight_xc_RUN += 4; |
| 1307 | } |
| 1308 | |
| 1309 | for (int i = 0; i < num_output; i++) |
| 1310 | { |
| 1311 | weight_hc_RUN[0] = float32_to_bfloat16(weight_hc_N[i]); |
| 1312 | weight_hc_RUN[1] = float32_to_bfloat16(weight_hc_N_1[i]); |
| 1313 | weight_hc_RUN[2] = float32_to_bfloat16(weight_hc_N_2[i]); |
| 1314 | weight_hc_RUN[3] = float32_to_bfloat16(weight_hc_N_3[i]); |
| 1315 | |
| 1316 | weight_hc_RUN += 4; |
| 1317 | } |
| 1318 | } |
| 1319 | #endif // __ARM_NEON |
| 1320 | for (; q < num_output; q++) |
| 1321 | { |
| 1322 | bias_c_RUBNWN[0] = float32_to_bfloat16(bias_c_R[q]); |
| 1323 | bias_c_RUBNWN[1] = float32_to_bfloat16(bias_c_U[q]); |
| 1324 | bias_c_RUBNWN[2] = float32_to_bfloat16(bias_c_BN[q]); |
| 1325 | bias_c_RUBNWN[3] = float32_to_bfloat16(bias_c_WN[q]); |
| 1326 | |
| 1327 | bias_c_RUBNWN += 4; |
| 1328 | |
| 1329 | const float* weight_xc_R = weight_xc.row(num_output * 0 + q); |
| 1330 | const float* weight_xc_U = weight_xc.row(num_output * 1 + q); |
| 1331 | const float* weight_xc_N = weight_xc.row(num_output * 2 + q); |
| 1332 | |
| 1333 | const float* weight_hc_R = weight_hc.row(num_output * 0 + q); |
| 1334 | const float* weight_hc_U = weight_hc.row(num_output * 1 + q); |
| 1335 | const float* weight_hc_N = weight_hc.row(num_output * 2 + q); |
| 1336 | |
| 1337 | #if __ARM_NEON |
| 1338 | unsigned short* weight_xc_RUN = weight_xc_data_packed_dr.row<unsigned short>(q / 4 + q % 4); |
| 1339 | unsigned short* weight_hc_RUN = weight_hc_data_packed_dr.row<unsigned short>(q / 4 + q % 4); |
| 1340 | #else |
| 1341 | unsigned short* weight_xc_RUN = weight_xc_data_packed_dr.row<unsigned short>(q); |
| 1342 | unsigned short* weight_hc_RUN = weight_hc_data_packed_dr.row<unsigned short>(q); |
| 1343 | #endif // __ARM_NEON |
| 1344 | |
| 1345 | for (int i = 0; i < size; i++) |
| 1346 | { |
| 1347 | weight_xc_RUN[0] = float32_to_bfloat16(weight_xc_R[i]); |
| 1348 | weight_xc_RUN[1] = float32_to_bfloat16(weight_xc_U[i]); |
| 1349 | |
| 1350 | weight_xc_RUN += 2; |
| 1351 | } |
| 1352 | |
| 1353 | for (int i = 0; i < num_output; i++) |
| 1354 | { |
| 1355 | weight_hc_RUN[0] = float32_to_bfloat16(weight_hc_R[i]); |
| 1356 | weight_hc_RUN[1] = float32_to_bfloat16(weight_hc_U[i]); |
| 1357 | |
| 1358 | weight_hc_RUN += 2; |
| 1359 | } |
| 1360 | |
| 1361 | for (int i = 0; i < size; i++) |
| 1362 | { |
| 1363 | weight_xc_RUN[0] = float32_to_bfloat16(weight_xc_N[i]); |
| 1364 | |
| 1365 | weight_xc_RUN += 1; |
| 1366 | } |
| 1367 | |
| 1368 | for (int i = 0; i < num_output; i++) |
| 1369 | { |
| 1370 | weight_hc_RUN[0] = float32_to_bfloat16(weight_hc_N[i]); |
| 1371 | |
| 1372 | weight_hc_RUN += 1; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (opt.lightmode) |
| 1378 | { |
| 1379 | weight_xc_data.release(); |
| 1380 | bias_c_data.release(); |
| 1381 | weight_hc_data.release(); |
| 1382 | } |
| 1383 | |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | int GRU_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 1388 | { |
| 1389 | int T = bottom_blob.h; |
| 1390 | |
| 1391 | int num_directions = direction == 2 ? 2 : 1; |
| 1392 | |
| 1393 | // initial hidden state |
| 1394 | Mat hidden(num_output, 4u, opt.workspace_allocator); |
| 1395 | if (hidden.empty()) |
| 1396 | return -100; |
| 1397 | hidden.fill(0.f); |
| 1398 | |
| 1399 | top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator); |
| 1400 | if (top_blob.empty()) |
| 1401 | return -100; |
| 1402 | |
| 1403 | // Uni directional |
| 1404 | if (direction == 0 || direction == 1) |
| 1405 | { |
| 1406 | int ret = gru_bf16s(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); |
| 1407 | if (ret != 0) |
| 1408 | return ret; |
| 1409 | } |
| 1410 | |
| 1411 | if (direction == 2) |
| 1412 | { |
| 1413 | Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator); |
| 1414 | if (top_blob_forward.empty()) |
| 1415 | return -100; |
| 1416 | |
| 1417 | Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator); |
| 1418 | if (top_blob_reverse.empty()) |
| 1419 | return -100; |
| 1420 | |
| 1421 | { |
| 1422 | int ret = gru_bf16s(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden, opt); |
| 1423 | if (ret != 0) |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | hidden.fill(0.f); |
| 1428 | |
| 1429 | { |
| 1430 | int ret = gru_bf16s(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), hidden, opt); |
| 1431 | if (ret != 0) |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | // concat w |
| 1436 | for (int i = 0; i < T; i++) |
| 1437 | { |
| 1438 | const unsigned short* pf = top_blob_forward.row<const unsigned short>(i); |
| 1439 | const unsigned short* pr = top_blob_reverse.row<const unsigned short>(i); |
| 1440 | unsigned short* ptr = top_blob.row<unsigned short>(i); |
| 1441 | |
| 1442 | memcpy(ptr, pf, num_output * sizeof(unsigned short)); |
| 1443 | memcpy(ptr + num_output, pr, num_output * sizeof(unsigned short)); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | int GRU_arm::forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 1451 | { |
| 1452 | const Mat& bottom_blob = bottom_blobs[0]; |
| 1453 | int T = bottom_blob.h; |
| 1454 | int num_directions = direction == 2 ? 2 : 1; |
| 1455 | |
| 1456 | Mat hidden; |
| 1457 | Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator; |
| 1458 | if (bottom_blobs.size() == 2) |
| 1459 | { |
| 1460 | Option opt_cast = opt; |
| 1461 | opt_cast.blob_allocator = hidden_allocator; |
| 1462 | cast_bfloat16_to_float32(bottom_blobs[1], hidden, opt_cast); |
| 1463 | } |
| 1464 | else |
| 1465 | { |
| 1466 | hidden.create(num_output, num_directions, 4u, hidden_allocator); |
| 1467 | if (hidden.empty()) |
| 1468 | return -100; |
| 1469 | hidden.fill(0.f); |
| 1470 | } |
| 1471 | |
| 1472 | Mat& top_blob = top_blobs[0]; |
| 1473 | top_blob.create(num_output * num_directions, T, 2u, opt.blob_allocator); |
| 1474 | if (top_blob.empty()) |
| 1475 | return -100; |
| 1476 | |
| 1477 | // Uni directional |
| 1478 | if (direction == 0 || direction == 1) |
| 1479 | { |
| 1480 | int ret = gru_bf16s(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); |
| 1481 | if (ret != 0) |
| 1482 | return ret; |
| 1483 | } |
| 1484 | |
| 1485 | if (direction == 2) |
| 1486 | { |
| 1487 | Mat top_blob_forward(num_output, T, 2u, opt.workspace_allocator); |
| 1488 | if (top_blob_forward.empty()) |
| 1489 | return -100; |
| 1490 | |
| 1491 | Mat top_blob_reverse(num_output, T, 2u, opt.workspace_allocator); |
| 1492 | if (top_blob_reverse.empty()) |
| 1493 | return -100; |
| 1494 | |
| 1495 | Mat hidden0 = hidden.row_range(0, 1); |
| 1496 | { |
| 1497 | int ret = gru_bf16s(bottom_blob, top_blob_forward, 0, weight_xc_data_packed.channel(0), bias_c_data_packed.channel(0), weight_hc_data_packed.channel(0), hidden0, opt); |
| 1498 | if (ret != 0) |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
| 1502 | Mat hidden1 = hidden.row_range(1, 1); |
| 1503 | { |
| 1504 | int ret = gru_bf16s(bottom_blob, top_blob_reverse, 1, weight_xc_data_packed.channel(1), bias_c_data_packed.channel(1), weight_hc_data_packed.channel(1), hidden1, opt); |
| 1505 | if (ret != 0) |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | // concat w |
| 1510 | for (int i = 0; i < T; i++) |
| 1511 | { |
| 1512 | const unsigned short* pf = top_blob_forward.row<const unsigned short>(i); |
| 1513 | const unsigned short* pr = top_blob_reverse.row<const unsigned short>(i); |
| 1514 | unsigned short* ptr = top_blob.row<unsigned short>(i); |
| 1515 | |
| 1516 | memcpy(ptr, pf, num_output * sizeof(unsigned short)); |
| 1517 | memcpy(ptr + num_output, pr, num_output * sizeof(unsigned short)); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | if (top_blobs.size() == 2) |
| 1522 | { |
| 1523 | cast_float32_to_bfloat16(hidden, top_blobs[1], opt); |
| 1524 | } |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | #endif // NCNN_BF16 |
| 1529 | |
| 1530 | #if NCNN_INT8 |
| 1531 | int GRU_arm::create_pipeline_int8(const Option& opt) |
| 1532 | { |
| 1533 | const int num_directions = direction == 2 ? 2 : 1; |
| 1534 | const int size = weight_data_size / num_directions / num_output / 3; |
| 1535 | |
| 1536 | gru_transform_weight_int8(weight_xc_data, weight_xc_data_int8_scales, weight_hc_data, weight_hc_data_int8_scales, bias_c_data, weight_data_tm, weight_data_tm_int8_descales, bias_c_data_packed, size, num_output, num_directions, opt); |
| 1537 | |
| 1538 | if (opt.lightmode) |
| 1539 | { |
| 1540 | weight_xc_data.release(); |
| 1541 | weight_hc_data.release(); |
| 1542 | bias_c_data.release(); |
| 1543 | weight_xc_data_int8_scales.release(); |
| 1544 | weight_hc_data_int8_scales.release(); |
| 1545 | } |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | void GRU_arm::dynamic_quantize(const Mat& bottom_blob, int elemtype, Mat& bottom_blob_int8, Mat& bottom_blob_int8_descales, const Option& opt) const |
| 1551 | { |
| 1552 | int size = bottom_blob.w; |
| 1553 | int T = bottom_blob.h; |
| 1554 | |
| 1555 | // dynamic quantize bottom_blob |
| 1556 | bottom_blob_int8_descales.create(T, (size_t)4u, 1, opt.blob_allocator); |
| 1557 | |
| 1558 | Mat bottom_blob_int8_scales(T, (size_t)4u, 1, opt.blob_allocator); |
| 1559 | |
| 1560 | if (elemtype == 1) |
| 1561 | { |
| 1562 | // fp32 |
| 1563 | for (int t = 0; t < T; t++) |
| 1564 | { |
| 1565 | const float* x = bottom_blob.row(t); |
| 1566 | |
| 1567 | float absmax = 0.f; |
| 1568 | for (int i = 0; i < size; i++) |
| 1569 | { |
| 1570 | absmax = std::max(absmax, (float)fabs(x[i])); |
| 1571 | } |
| 1572 | |
| 1573 | bottom_blob_int8_scales[t] = 127.f / absmax; |
| 1574 | bottom_blob_int8_descales[t] = absmax / 127.f; |
| 1575 | } |
| 1576 | } |
| 1577 | if (elemtype == 2) |
| 1578 | { |
| 1579 | // fp16 |
| 1580 | for (int t = 0; t < T; t++) |
| 1581 | { |
| 1582 | const unsigned short* x = bottom_blob.row<const unsigned short>(t); |
| 1583 | |
| 1584 | float absmax = 0.f; |
| 1585 | for (int i = 0; i < size; i++) |
| 1586 | { |
| 1587 | absmax = std::max(absmax, (float)fabs(float16_to_float32(x[i]))); |
| 1588 | } |
| 1589 | |
| 1590 | bottom_blob_int8_scales[t] = 127.f / absmax; |
| 1591 | bottom_blob_int8_descales[t] = absmax / 127.f; |
| 1592 | } |
| 1593 | } |
| 1594 | if (elemtype == 4) |
| 1595 | { |
| 1596 | // bf16 |
| 1597 | for (int t = 0; t < T; t++) |
| 1598 | { |
| 1599 | const unsigned short* x = bottom_blob.row<const unsigned short>(t); |
| 1600 | |
| 1601 | float absmax = 0.f; |
| 1602 | for (int i = 0; i < size; i++) |
| 1603 | { |
| 1604 | absmax = std::max(absmax, (float)fabs(bfloat16_to_float32(x[i]))); |
| 1605 | } |
| 1606 | |
| 1607 | bottom_blob_int8_scales[t] = 127.f / absmax; |
| 1608 | bottom_blob_int8_descales[t] = absmax / 127.f; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt); |
| 1613 | } |
| 1614 | |
| 1615 | int GRU_arm::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 1616 | { |
| 1617 | int elemtype = 1; // fp32 |
| 1618 | { |
| 1619 | int elembits = bottom_blob.elembits(); |
| 1620 | |
| 1621 | // clang-format off |
| 1622 | // *INDENT-OFF* |
| 1623 | |
| 1624 | #if NCNN_ARM82 |
| 1625 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 1626 | { |
| 1627 | elemtype = 2; // fp16 |
| 1628 | } |
| 1629 | else |
| 1630 | #endif |
| 1631 | #if NCNN_BF16 |
| 1632 | if (opt.use_bf16_storage && elembits == 16) |
| 1633 | { |
| 1634 | elemtype = 4; // bf16 |
| 1635 | } |
| 1636 | else |
| 1637 | #endif |
| 1638 | { |
| 1639 | // fp32 |
| 1640 | } |
| 1641 | |
| 1642 | // *INDENT-ON* |
| 1643 | // clang-format on |
| 1644 | } |
| 1645 | |
| 1646 | int T = bottom_blob.h; |
| 1647 | size_t elemsize = bottom_blob.elemsize; |
| 1648 | |
| 1649 | int num_directions = direction == 2 ? 2 : 1; |
| 1650 | |
| 1651 | // initial hidden state |
| 1652 | Mat hidden(num_output, 4u, opt.workspace_allocator); |
| 1653 | if (hidden.empty()) |
| 1654 | return -100; |
| 1655 | hidden.fill(0.f); |
| 1656 | |
| 1657 | top_blob.create(num_output * num_directions, T, elemsize, opt.blob_allocator); |
| 1658 | if (top_blob.empty()) |
| 1659 | return -100; |
| 1660 | |
| 1661 | // dynamic quantize bottom_blob |
| 1662 | Mat bottom_blob_int8; |
| 1663 | Mat bottom_blob_int8_descales; |
| 1664 | { |
| 1665 | Option opt_quant = opt; |
| 1666 | opt_quant.blob_allocator = opt.workspace_allocator; |
| 1667 | opt_quant.use_packing_layout = false; |
| 1668 | dynamic_quantize(bottom_blob, elemtype, bottom_blob_int8, bottom_blob_int8_descales, opt_quant); |
| 1669 | } |
| 1670 | |
| 1671 | // Uni directional |
| 1672 | if (direction == 0 || direction == 1) |
| 1673 | { |
| 1674 | gru_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob, elemtype, direction, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden, opt); |
| 1675 | } |
| 1676 | |
| 1677 | if (direction == 2) |
| 1678 | { |
| 1679 | Mat top_blob_forward(num_output, T, elemsize, opt.workspace_allocator); |
| 1680 | if (top_blob_forward.empty()) |
| 1681 | return -100; |
| 1682 | |
| 1683 | Mat top_blob_reverse(num_output, T, elemsize, opt.workspace_allocator); |
| 1684 | if (top_blob_reverse.empty()) |
| 1685 | return -100; |
| 1686 | |
| 1687 | { |
| 1688 | gru_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_forward, elemtype, 0, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden, opt); |
| 1689 | } |
| 1690 | |
| 1691 | hidden.fill(0.f); |
| 1692 | |
| 1693 | { |
| 1694 | gru_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_reverse, elemtype, 1, weight_data_tm.channel(1), weight_data_tm_int8_descales.channel(1), bias_c_data_packed.channel(1), hidden, opt); |
| 1695 | } |
| 1696 | |
| 1697 | // concat w |
| 1698 | for (int i = 0; i < T; i++) |
| 1699 | { |
| 1700 | const unsigned char* pf = top_blob_forward.row<const unsigned char>(i); |
| 1701 | const unsigned char* pr = top_blob_reverse.row<const unsigned char>(i); |
| 1702 | unsigned char* ptr = top_blob.row<unsigned char>(i); |
| 1703 | |
| 1704 | memcpy(ptr, pf, num_output * elemsize); |
| 1705 | memcpy(ptr + num_output * elemsize, pr, num_output * elemsize); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | int GRU_arm::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 1713 | { |
| 1714 | const Mat& bottom_blob = bottom_blobs[0]; |
| 1715 | |
| 1716 | int elemtype = 1; // fp32 |
| 1717 | { |
| 1718 | int elembits = bottom_blob.elembits(); |
| 1719 | |
| 1720 | // clang-format off |
| 1721 | // *INDENT-OFF* |
| 1722 | |
| 1723 | #if NCNN_ARM82 |
| 1724 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 1725 | { |
| 1726 | elemtype = 2; // fp16 |
| 1727 | } |
| 1728 | else |
| 1729 | #endif |
| 1730 | #if NCNN_BF16 |
| 1731 | if (opt.use_bf16_storage && elembits == 16) |
| 1732 | { |
| 1733 | elemtype = 4; // bf16 |
| 1734 | } |
| 1735 | else |
| 1736 | #endif |
| 1737 | { |
| 1738 | // fp32 |
| 1739 | } |
| 1740 | |
| 1741 | // *INDENT-ON* |
| 1742 | // clang-format on |
| 1743 | } |
| 1744 | |
| 1745 | int T = bottom_blob.h; |
| 1746 | size_t elemsize = bottom_blob.elemsize; |
| 1747 | int num_directions = direction == 2 ? 2 : 1; |
| 1748 | |
| 1749 | Mat hidden; |
| 1750 | Allocator* hidden_allocator = top_blobs.size() == 2 ? opt.blob_allocator : opt.workspace_allocator; |
| 1751 | if (bottom_blobs.size() == 2) |
| 1752 | { |
| 1753 | if (elemtype == 1) |
| 1754 | { |
| 1755 | hidden = bottom_blobs[1].clone(hidden_allocator); |
| 1756 | } |
| 1757 | if (elemtype == 2) |
| 1758 | { |
| 1759 | Option opt_cast = opt; |
| 1760 | opt_cast.blob_allocator = hidden_allocator; |
| 1761 | cast_float16_to_float32(bottom_blobs[1], hidden, opt_cast); |
| 1762 | } |
| 1763 | if (elemtype == 4) |
| 1764 | { |
| 1765 | Option opt_cast = opt; |
| 1766 | opt_cast.blob_allocator = hidden_allocator; |
| 1767 | cast_bfloat16_to_float32(bottom_blobs[1], hidden, opt_cast); |
| 1768 | } |
| 1769 | } |
| 1770 | else |
| 1771 | { |
| 1772 | hidden.create(num_output, num_directions, 4u, hidden_allocator); |
| 1773 | if (hidden.empty()) |
| 1774 | return -100; |
| 1775 | hidden.fill(0.f); |
| 1776 | } |
| 1777 | |
| 1778 | Mat& top_blob = top_blobs[0]; |
| 1779 | top_blob.create(num_output * num_directions, T, elemsize, opt.blob_allocator); |
| 1780 | if (top_blob.empty()) |
| 1781 | return -100; |
| 1782 | |
| 1783 | // dynamic quantize bottom_blob |
| 1784 | Mat bottom_blob_int8; |
| 1785 | Mat bottom_blob_int8_descales; |
| 1786 | { |
| 1787 | Option opt_quant = opt; |
| 1788 | opt_quant.blob_allocator = opt.workspace_allocator; |
| 1789 | opt_quant.use_packing_layout = false; |
| 1790 | dynamic_quantize(bottom_blob, elemtype, bottom_blob_int8, bottom_blob_int8_descales, opt_quant); |
| 1791 | } |
| 1792 | |
| 1793 | // Uni directional |
| 1794 | if (direction == 0 || direction == 1) |
| 1795 | { |
| 1796 | gru_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob, elemtype, direction, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden, opt); |
| 1797 | } |
| 1798 | |
| 1799 | if (direction == 2) |
| 1800 | { |
| 1801 | Mat top_blob_forward(num_output, T, elemsize, opt.workspace_allocator); |
| 1802 | if (top_blob_forward.empty()) |
| 1803 | return -100; |
| 1804 | |
| 1805 | Mat top_blob_reverse(num_output, T, elemsize, opt.workspace_allocator); |
| 1806 | if (top_blob_reverse.empty()) |
| 1807 | return -100; |
| 1808 | |
| 1809 | Mat hidden0 = hidden.row_range(0, 1); |
| 1810 | { |
| 1811 | gru_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_forward, elemtype, 0, weight_data_tm.channel(0), weight_data_tm_int8_descales.channel(0), bias_c_data_packed.channel(0), hidden0, opt); |
| 1812 | } |
| 1813 | |
| 1814 | Mat hidden1 = hidden.row_range(1, 1); |
| 1815 | { |
| 1816 | gru_int8(bottom_blob_int8, bottom_blob_int8_descales, top_blob_reverse, elemtype, 1, weight_data_tm.channel(1), weight_data_tm_int8_descales.channel(1), bias_c_data_packed.channel(1), hidden1, opt); |
| 1817 | } |
| 1818 | |
| 1819 | // concat w |
| 1820 | for (int i = 0; i < T; i++) |
| 1821 | { |
| 1822 | const unsigned char* pf = top_blob_forward.row<const unsigned char>(i); |
| 1823 | const unsigned char* pr = top_blob_reverse.row<const unsigned char>(i); |
| 1824 | unsigned char* ptr = top_blob.row<unsigned char>(i); |
| 1825 | |
| 1826 | memcpy(ptr, pf, num_output * elemsize); |
| 1827 | memcpy(ptr + num_output * elemsize, pr, num_output * elemsize); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | if (top_blobs.size() == 2) |
| 1832 | { |
| 1833 | if (elemtype == 1) |
| 1834 | { |
| 1835 | top_blobs[1] = hidden; |
| 1836 | } |
| 1837 | if (elemtype == 2) |
| 1838 | { |
| 1839 | cast_float32_to_float16(hidden, top_blobs[1], opt); |
| 1840 | } |
| 1841 | if (elemtype == 4) |
| 1842 | { |
| 1843 | cast_float32_to_bfloat16(hidden, top_blobs[1], opt); |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | return 0; |
| 1848 | } |
| 1849 | #endif // NCNN_INT8 |
| 1850 | |
| 1851 | } // namespace ncnn |