| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 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 | #if NCNN_RUNTIME_CPU && NCNN_ARM82DOT && __aarch64__ && !__ARM_FEATURE_DOTPROD |
| 16 | void gru_transform_weight_int8_asimddp(const Mat& weight_xc, const Mat& weight_xc_int8_scales, const Mat& weight_hc, const Mat& weight_hc_int8_scales, const Mat& bias_c, Mat& weight_data_tm, Mat& weight_data_tm_int8_descales, Mat& bias_c_tm, int size, int num_output, int num_directions, const Option& opt); |
| 17 | void gru_int8_asimddp(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int elemtype, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, Mat& hidden_state, const Option& opt); |
| 18 | #endif |
| 19 | |
| 20 | #if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2) |
| 21 | void gru_int8_gate_output_vfpv4(const Mat& gates, Mat& hidden_state, Mat& top_blob, int ti, int elemtype, const Option& opt); |
| 22 | #endif |
| 23 | |
| 24 | static void gru_transform_weight_int8(const Mat& weight_xc, const Mat& weight_xc_int8_scales, const Mat& weight_hc, const Mat& weight_hc_int8_scales, const Mat& bias_c, Mat& weight_data_tm, Mat& weight_data_tm_int8_descales, Mat& bias_c_tm, int size, int num_output, int num_directions, const Option& opt) |
| 25 | { |
| 26 | #if NCNN_RUNTIME_CPU && NCNN_ARM82DOT && __aarch64__ && !__ARM_FEATURE_DOTPROD |
| 27 | if (ncnn::cpu_support_arm_asimddp()) |
| 28 | { |
| 29 | gru_transform_weight_int8_asimddp(weight_xc, weight_xc_int8_scales, weight_hc, weight_hc_int8_scales, bias_c, weight_data_tm, weight_data_tm_int8_descales, bias_c_tm, size, num_output, num_directions, opt); |
| 30 | return; |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | #if __ARM_NEON |
| 35 | weight_data_tm.create(size * 12 + num_output * 12, num_output / 4 + num_output % 4, num_directions, 1u, 1); |
| 36 | weight_data_tm_int8_descales.create(12 + 12, num_output / 4 + num_output % 4, num_directions); |
| 37 | #else |
| 38 | weight_data_tm.create(size * 3 + num_output * 3, num_output, num_directions, 1u, 1); |
| 39 | weight_data_tm_int8_descales.create(3 + 3, num_output, num_directions); |
| 40 | #endif |
| 41 | bias_c_tm.create(num_output, 1, num_directions, 16u, 4); |
| 42 | |
| 43 | #pragma omp parallel for num_threads(opt.num_threads) |
| 44 | for (int dr = 0; dr < num_directions; dr++) |
| 45 | { |
| 46 | const Mat weight_xc_dr = weight_xc.channel(dr); |
| 47 | const Mat weight_hc_dr = weight_hc.channel(dr); |
| 48 | const Mat bias_c_dr = bias_c.channel(dr); |
| 49 | const float* weight_xc_int8_scales_ptr = weight_xc_int8_scales.row(dr); |
| 50 | const float* weight_hc_int8_scales_ptr = weight_hc_int8_scales.row(dr); |
| 51 | |
| 52 | Mat weight_data_tm_dr = weight_data_tm.channel(dr); |
| 53 | Mat bias_c_tm_dr = bias_c_tm.channel(dr); |
| 54 | Mat weight_data_tm_int8_descales_dr = weight_data_tm_int8_descales.channel(dr); |
| 55 | |
| 56 | const float* bias_c_R = bias_c_dr.row(0); |
| 57 | const float* bias_c_U = bias_c_dr.row(1); |
| 58 | const float* bias_c_WN = bias_c_dr.row(2); |
| 59 | const float* bias_c_BN = bias_c_dr.row(3); |
| 60 | |
| 61 | float* bias_c_RUBNWN = bias_c_tm_dr.row(0); |
| 62 | |
| 63 | int q = 0; |
| 64 | #if __ARM_NEON |
| 65 | for (; q + 3 < num_output; q += 4) |
| 66 | { |
| 67 | vst1q_f32(bias_c_RUBNWN, vld1q_f32(bias_c_R + q)); |
| 68 | vst1q_f32(bias_c_RUBNWN + 4, vld1q_f32(bias_c_U + q)); |
| 69 | vst1q_f32(bias_c_RUBNWN + 8, vld1q_f32(bias_c_BN + q)); |
| 70 | vst1q_f32(bias_c_RUBNWN + 12, vld1q_f32(bias_c_WN + q)); |
| 71 | |
| 72 | bias_c_RUBNWN += 16; |
| 73 | |
| 74 | const signed char* weight_xc_R_0 = weight_xc_dr.row<const signed char>(num_output * 0 + q); |
| 75 | const signed char* weight_xc_U_0 = weight_xc_dr.row<const signed char>(num_output * 1 + q); |
| 76 | const signed char* weight_xc_N_0 = weight_xc_dr.row<const signed char>(num_output * 2 + q); |
| 77 | |
| 78 | const signed char* weight_xc_R_1 = weight_xc_dr.row<const signed char>(num_output * 0 + q + 1); |
| 79 | const signed char* weight_xc_U_1 = weight_xc_dr.row<const signed char>(num_output * 1 + q + 1); |
| 80 | const signed char* weight_xc_N_1 = weight_xc_dr.row<const signed char>(num_output * 2 + q + 1); |
| 81 | |
| 82 | const signed char* weight_xc_R_2 = weight_xc_dr.row<const signed char>(num_output * 0 + q + 2); |
| 83 | const signed char* weight_xc_U_2 = weight_xc_dr.row<const signed char>(num_output * 1 + q + 2); |
| 84 | const signed char* weight_xc_N_2 = weight_xc_dr.row<const signed char>(num_output * 2 + q + 2); |
| 85 | |
| 86 | const signed char* weight_xc_R_3 = weight_xc_dr.row<const signed char>(num_output * 0 + q + 3); |
| 87 | const signed char* weight_xc_U_3 = weight_xc_dr.row<const signed char>(num_output * 1 + q + 3); |
| 88 | const signed char* weight_xc_N_3 = weight_xc_dr.row<const signed char>(num_output * 2 + q + 3); |
| 89 | |
| 90 | const signed char* weight_hc_R_0 = weight_hc_dr.row<const signed char>(num_output * 0 + q); |
| 91 | const signed char* weight_hc_U_0 = weight_hc_dr.row<const signed char>(num_output * 1 + q); |
| 92 | const signed char* weight_hc_N_0 = weight_hc_dr.row<const signed char>(num_output * 2 + q); |
| 93 | |
| 94 | const signed char* weight_hc_R_1 = weight_hc_dr.row<const signed char>(num_output * 0 + q + 1); |
| 95 | const signed char* weight_hc_U_1 = weight_hc_dr.row<const signed char>(num_output * 1 + q + 1); |
| 96 | const signed char* weight_hc_N_1 = weight_hc_dr.row<const signed char>(num_output * 2 + q + 1); |
| 97 | |
| 98 | const signed char* weight_hc_R_2 = weight_hc_dr.row<const signed char>(num_output * 0 + q + 2); |
| 99 | const signed char* weight_hc_U_2 = weight_hc_dr.row<const signed char>(num_output * 1 + q + 2); |
| 100 | const signed char* weight_hc_N_2 = weight_hc_dr.row<const signed char>(num_output * 2 + q + 2); |
| 101 | |
| 102 | const signed char* weight_hc_R_3 = weight_hc_dr.row<const signed char>(num_output * 0 + q + 3); |
| 103 | const signed char* weight_hc_U_3 = weight_hc_dr.row<const signed char>(num_output * 1 + q + 3); |
| 104 | const signed char* weight_hc_N_3 = weight_hc_dr.row<const signed char>(num_output * 2 + q + 3); |
| 105 | |
| 106 | signed char* kptr = weight_data_tm_dr.row<signed char>(q / 4); |
| 107 | float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 4); |
| 108 | |
| 109 | int i = 0; |
| 110 | #if __ARM_FEATURE_DOTPROD |
| 111 | for (; i + 3 < size; i += 4) |
| 112 | { |
| 113 | kptr[0] = weight_xc_R_0[i]; |
| 114 | kptr[1] = weight_xc_R_0[i + 1]; |
| 115 | kptr[2] = weight_xc_R_0[i + 2]; |
| 116 | kptr[3] = weight_xc_R_0[i + 3]; |
| 117 | kptr[4] = weight_xc_R_1[i]; |
| 118 | kptr[5] = weight_xc_R_1[i + 1]; |
| 119 | kptr[6] = weight_xc_R_1[i + 2]; |
| 120 | kptr[7] = weight_xc_R_1[i + 3]; |
| 121 | kptr[8 + 0] = weight_xc_R_2[i]; |
| 122 | kptr[8 + 1] = weight_xc_R_2[i + 1]; |
| 123 | kptr[8 + 2] = weight_xc_R_2[i + 2]; |
| 124 | kptr[8 + 3] = weight_xc_R_2[i + 3]; |
| 125 | kptr[8 + 4] = weight_xc_R_3[i]; |
| 126 | kptr[8 + 5] = weight_xc_R_3[i + 1]; |
| 127 | kptr[8 + 6] = weight_xc_R_3[i + 2]; |
| 128 | kptr[8 + 7] = weight_xc_R_3[i + 3]; |
| 129 | kptr[16 + 0] = weight_xc_U_0[i]; |
| 130 | kptr[16 + 1] = weight_xc_U_0[i + 1]; |
| 131 | kptr[16 + 2] = weight_xc_U_0[i + 2]; |
| 132 | kptr[16 + 3] = weight_xc_U_0[i + 3]; |
| 133 | kptr[16 + 4] = weight_xc_U_1[i]; |
| 134 | kptr[16 + 5] = weight_xc_U_1[i + 1]; |
| 135 | kptr[16 + 6] = weight_xc_U_1[i + 2]; |
| 136 | kptr[16 + 7] = weight_xc_U_1[i + 3]; |
| 137 | kptr[24 + 0] = weight_xc_U_2[i]; |
| 138 | kptr[24 + 1] = weight_xc_U_2[i + 1]; |
| 139 | kptr[24 + 2] = weight_xc_U_2[i + 2]; |
| 140 | kptr[24 + 3] = weight_xc_U_2[i + 3]; |
| 141 | kptr[24 + 4] = weight_xc_U_3[i]; |
| 142 | kptr[24 + 5] = weight_xc_U_3[i + 1]; |
| 143 | kptr[24 + 6] = weight_xc_U_3[i + 2]; |
| 144 | kptr[24 + 7] = weight_xc_U_3[i + 3]; |
| 145 | |
| 146 | kptr += 32; |
| 147 | } |
| 148 | #else |
| 149 | for (; i + 7 < size; i += 8) |
| 150 | { |
| 151 | int8x8_t _w0 = vld1_s8(weight_xc_R_0 + i); |
| 152 | int8x8_t _w1 = vld1_s8(weight_xc_R_1 + i); |
| 153 | int8x8_t _w2 = vld1_s8(weight_xc_R_2 + i); |
| 154 | int8x8_t _w3 = vld1_s8(weight_xc_R_3 + i); |
| 155 | int8x8_t _w4 = vld1_s8(weight_xc_U_0 + i); |
| 156 | int8x8_t _w5 = vld1_s8(weight_xc_U_1 + i); |
| 157 | int8x8_t _w6 = vld1_s8(weight_xc_U_2 + i); |
| 158 | int8x8_t _w7 = vld1_s8(weight_xc_U_3 + i); |
| 159 | |
| 160 | int32x2x2_t _t0 = vtrn_s32(vreinterpret_s32_s8(_w0), vreinterpret_s32_s8(_w4)); |
| 161 | int32x2x2_t _t1 = vtrn_s32(vreinterpret_s32_s8(_w1), vreinterpret_s32_s8(_w5)); |
| 162 | int32x2x2_t _t2 = vtrn_s32(vreinterpret_s32_s8(_w2), vreinterpret_s32_s8(_w6)); |
| 163 | int32x2x2_t _t3 = vtrn_s32(vreinterpret_s32_s8(_w3), vreinterpret_s32_s8(_w7)); |
| 164 | |
| 165 | int32x4x4_t _w; |
| 166 | _w.val[0] = vcombine_s32(_t0.val[0], _t0.val[1]); |
| 167 | _w.val[1] = vcombine_s32(_t1.val[0], _t1.val[1]); |
| 168 | _w.val[2] = vcombine_s32(_t2.val[0], _t2.val[1]); |
| 169 | _w.val[3] = vcombine_s32(_t3.val[0], _t3.val[1]); |
| 170 | |
| 171 | vst4q_s32((int*)kptr, _w); |
| 172 | |
| 173 | kptr += 64; |
| 174 | } |
| 175 | #endif // __ARM_FEATURE_DOTPROD |
| 176 | for (; i + 1 < size; i += 2) |
| 177 | { |
| 178 | kptr[0] = weight_xc_R_0[i]; |
| 179 | kptr[1] = weight_xc_R_0[i + 1]; |
| 180 | kptr[2] = weight_xc_R_1[i]; |
| 181 | kptr[3] = weight_xc_R_1[i + 1]; |
| 182 | kptr[4] = weight_xc_R_2[i]; |
| 183 | kptr[5] = weight_xc_R_2[i + 1]; |
| 184 | kptr[6] = weight_xc_R_3[i]; |
| 185 | kptr[7] = weight_xc_R_3[i + 1]; |
| 186 | kptr[8 + 0] = weight_xc_U_0[i]; |
| 187 | kptr[8 + 1] = weight_xc_U_0[i + 1]; |
| 188 | kptr[8 + 2] = weight_xc_U_1[i]; |
| 189 | kptr[8 + 3] = weight_xc_U_1[i + 1]; |
| 190 | kptr[8 + 4] = weight_xc_U_2[i]; |
| 191 | kptr[8 + 5] = weight_xc_U_2[i + 1]; |
| 192 | kptr[8 + 6] = weight_xc_U_3[i]; |
| 193 | kptr[8 + 7] = weight_xc_U_3[i + 1]; |
| 194 | |
| 195 | kptr += 16; |
| 196 | } |
| 197 | for (; i < size; i++) |
| 198 | { |
| 199 | kptr[0] = weight_xc_R_0[i]; |
| 200 | kptr[1] = weight_xc_R_1[i]; |
| 201 | kptr[2] = weight_xc_R_2[i]; |
| 202 | kptr[3] = weight_xc_R_3[i]; |
| 203 | kptr[4] = weight_xc_U_0[i]; |
| 204 | kptr[5] = weight_xc_U_1[i]; |
| 205 | kptr[6] = weight_xc_U_2[i]; |
| 206 | kptr[7] = weight_xc_U_3[i]; |
| 207 | |
| 208 | kptr += 8; |
| 209 | } |
| 210 | |
| 211 | i = 0; |
| 212 | #if __ARM_FEATURE_DOTPROD |
| 213 | for (; i + 3 < num_output; i += 4) |
| 214 | { |
| 215 | kptr[0] = weight_hc_R_0[i]; |
| 216 | kptr[1] = weight_hc_R_0[i + 1]; |
| 217 | kptr[2] = weight_hc_R_0[i + 2]; |
| 218 | kptr[3] = weight_hc_R_0[i + 3]; |
| 219 | kptr[4] = weight_hc_R_1[i]; |
| 220 | kptr[5] = weight_hc_R_1[i + 1]; |
| 221 | kptr[6] = weight_hc_R_1[i + 2]; |
| 222 | kptr[7] = weight_hc_R_1[i + 3]; |
| 223 | kptr[8 + 0] = weight_hc_R_2[i]; |
| 224 | kptr[8 + 1] = weight_hc_R_2[i + 1]; |
| 225 | kptr[8 + 2] = weight_hc_R_2[i + 2]; |
| 226 | kptr[8 + 3] = weight_hc_R_2[i + 3]; |
| 227 | kptr[8 + 4] = weight_hc_R_3[i]; |
| 228 | kptr[8 + 5] = weight_hc_R_3[i + 1]; |
| 229 | kptr[8 + 6] = weight_hc_R_3[i + 2]; |
| 230 | kptr[8 + 7] = weight_hc_R_3[i + 3]; |
| 231 | kptr[16 + 0] = weight_hc_U_0[i]; |
| 232 | kptr[16 + 1] = weight_hc_U_0[i + 1]; |
| 233 | kptr[16 + 2] = weight_hc_U_0[i + 2]; |
| 234 | kptr[16 + 3] = weight_hc_U_0[i + 3]; |
| 235 | kptr[16 + 4] = weight_hc_U_1[i]; |
| 236 | kptr[16 + 5] = weight_hc_U_1[i + 1]; |
| 237 | kptr[16 + 6] = weight_hc_U_1[i + 2]; |
| 238 | kptr[16 + 7] = weight_hc_U_1[i + 3]; |
| 239 | kptr[24 + 0] = weight_hc_U_2[i]; |
| 240 | kptr[24 + 1] = weight_hc_U_2[i + 1]; |
| 241 | kptr[24 + 2] = weight_hc_U_2[i + 2]; |
| 242 | kptr[24 + 3] = weight_hc_U_2[i + 3]; |
| 243 | kptr[24 + 4] = weight_hc_U_3[i]; |
| 244 | kptr[24 + 5] = weight_hc_U_3[i + 1]; |
| 245 | kptr[24 + 6] = weight_hc_U_3[i + 2]; |
| 246 | kptr[24 + 7] = weight_hc_U_3[i + 3]; |
| 247 | |
| 248 | kptr += 32; |
| 249 | } |
| 250 | #else |
| 251 | for (; i + 7 < num_output; i += 8) |
| 252 | { |
| 253 | int8x8_t _w0 = vld1_s8(weight_hc_R_0 + i); |
| 254 | int8x8_t _w1 = vld1_s8(weight_hc_R_1 + i); |
| 255 | int8x8_t _w2 = vld1_s8(weight_hc_R_2 + i); |
| 256 | int8x8_t _w3 = vld1_s8(weight_hc_R_3 + i); |
| 257 | int8x8_t _w4 = vld1_s8(weight_hc_U_0 + i); |
| 258 | int8x8_t _w5 = vld1_s8(weight_hc_U_1 + i); |
| 259 | int8x8_t _w6 = vld1_s8(weight_hc_U_2 + i); |
| 260 | int8x8_t _w7 = vld1_s8(weight_hc_U_3 + i); |
| 261 | |
| 262 | int32x2x2_t _t0 = vtrn_s32(vreinterpret_s32_s8(_w0), vreinterpret_s32_s8(_w4)); |
| 263 | int32x2x2_t _t1 = vtrn_s32(vreinterpret_s32_s8(_w1), vreinterpret_s32_s8(_w5)); |
| 264 | int32x2x2_t _t2 = vtrn_s32(vreinterpret_s32_s8(_w2), vreinterpret_s32_s8(_w6)); |
| 265 | int32x2x2_t _t3 = vtrn_s32(vreinterpret_s32_s8(_w3), vreinterpret_s32_s8(_w7)); |
| 266 | |
| 267 | int32x4x4_t _w; |
| 268 | _w.val[0] = vcombine_s32(_t0.val[0], _t0.val[1]); |
| 269 | _w.val[1] = vcombine_s32(_t1.val[0], _t1.val[1]); |
| 270 | _w.val[2] = vcombine_s32(_t2.val[0], _t2.val[1]); |
| 271 | _w.val[3] = vcombine_s32(_t3.val[0], _t3.val[1]); |
| 272 | |
| 273 | vst4q_s32((int*)kptr, _w); |
| 274 | |
| 275 | kptr += 64; |
| 276 | } |
| 277 | #endif // __ARM_FEATURE_DOTPROD |
| 278 | for (; i + 1 < num_output; i += 2) |
| 279 | { |
| 280 | kptr[0] = weight_hc_R_0[i]; |
| 281 | kptr[1] = weight_hc_R_0[i + 1]; |
| 282 | kptr[2] = weight_hc_R_1[i]; |
| 283 | kptr[3] = weight_hc_R_1[i + 1]; |
| 284 | kptr[4] = weight_hc_R_2[i]; |
| 285 | kptr[5] = weight_hc_R_2[i + 1]; |
| 286 | kptr[6] = weight_hc_R_3[i]; |
| 287 | kptr[7] = weight_hc_R_3[i + 1]; |
| 288 | kptr[8 + 0] = weight_hc_U_0[i]; |
| 289 | kptr[8 + 1] = weight_hc_U_0[i + 1]; |
| 290 | kptr[8 + 2] = weight_hc_U_1[i]; |
| 291 | kptr[8 + 3] = weight_hc_U_1[i + 1]; |
| 292 | kptr[8 + 4] = weight_hc_U_2[i]; |
| 293 | kptr[8 + 5] = weight_hc_U_2[i + 1]; |
| 294 | kptr[8 + 6] = weight_hc_U_3[i]; |
| 295 | kptr[8 + 7] = weight_hc_U_3[i + 1]; |
| 296 | |
| 297 | kptr += 16; |
| 298 | } |
| 299 | for (; i < num_output; i++) |
| 300 | { |
| 301 | kptr[0] = weight_hc_R_0[i]; |
| 302 | kptr[1] = weight_hc_R_1[i]; |
| 303 | kptr[2] = weight_hc_R_2[i]; |
| 304 | kptr[3] = weight_hc_R_3[i]; |
| 305 | kptr[4] = weight_hc_U_0[i]; |
| 306 | kptr[5] = weight_hc_U_1[i]; |
| 307 | kptr[6] = weight_hc_U_2[i]; |
| 308 | kptr[7] = weight_hc_U_3[i]; |
| 309 | |
| 310 | kptr += 8; |
| 311 | } |
| 312 | |
| 313 | i = 0; |
| 314 | #if __ARM_FEATURE_DOTPROD |
| 315 | for (; i + 3 < num_output; i += 4) |
| 316 | { |
| 317 | kptr[0] = weight_hc_N_0[i]; |
| 318 | kptr[1] = weight_hc_N_0[i + 1]; |
| 319 | kptr[2] = weight_hc_N_0[i + 2]; |
| 320 | kptr[3] = weight_hc_N_0[i + 3]; |
| 321 | kptr[4] = weight_hc_N_1[i]; |
| 322 | kptr[5] = weight_hc_N_1[i + 1]; |
| 323 | kptr[6] = weight_hc_N_1[i + 2]; |
| 324 | kptr[7] = weight_hc_N_1[i + 3]; |
| 325 | kptr[8 + 0] = weight_hc_N_2[i]; |
| 326 | kptr[8 + 1] = weight_hc_N_2[i + 1]; |
| 327 | kptr[8 + 2] = weight_hc_N_2[i + 2]; |
| 328 | kptr[8 + 3] = weight_hc_N_2[i + 3]; |
| 329 | kptr[8 + 4] = weight_hc_N_3[i]; |
| 330 | kptr[8 + 5] = weight_hc_N_3[i + 1]; |
| 331 | kptr[8 + 6] = weight_hc_N_3[i + 2]; |
| 332 | kptr[8 + 7] = weight_hc_N_3[i + 3]; |
| 333 | |
| 334 | kptr += 16; |
| 335 | } |
| 336 | #else |
| 337 | for (; i + 7 < num_output; i += 8) |
| 338 | { |
| 339 | vst1_s8(kptr, vld1_s8(weight_hc_N_0 + i)); |
| 340 | vst1_s8(kptr + 8, vld1_s8(weight_hc_N_1 + i)); |
| 341 | vst1_s8(kptr + 16, vld1_s8(weight_hc_N_2 + i)); |
| 342 | vst1_s8(kptr + 24, vld1_s8(weight_hc_N_3 + i)); |
| 343 | kptr += 32; |
| 344 | } |
| 345 | #endif // __ARM_FEATURE_DOTPROD |
| 346 | for (; i + 1 < num_output; i += 2) |
| 347 | { |
| 348 | kptr[0] = weight_hc_N_0[i]; |
| 349 | kptr[1] = weight_hc_N_0[i + 1]; |
| 350 | kptr[2] = weight_hc_N_1[i]; |
| 351 | kptr[3] = weight_hc_N_1[i + 1]; |
| 352 | kptr[4] = weight_hc_N_2[i]; |
| 353 | kptr[5] = weight_hc_N_2[i + 1]; |
| 354 | kptr[6] = weight_hc_N_3[i]; |
| 355 | kptr[7] = weight_hc_N_3[i + 1]; |
| 356 | |
| 357 | kptr += 8; |
| 358 | } |
| 359 | for (; i < num_output; i++) |
| 360 | { |
| 361 | kptr[0] = weight_hc_N_0[i]; |
| 362 | kptr[1] = weight_hc_N_1[i]; |
| 363 | kptr[2] = weight_hc_N_2[i]; |
| 364 | kptr[3] = weight_hc_N_3[i]; |
| 365 | |
| 366 | kptr += 4; |
| 367 | } |
| 368 | |
| 369 | i = 0; |
| 370 | #if __ARM_FEATURE_DOTPROD |
| 371 | for (; i + 3 < size; i += 4) |
| 372 | { |
| 373 | kptr[0] = weight_xc_N_0[i]; |
| 374 | kptr[1] = weight_xc_N_0[i + 1]; |
| 375 | kptr[2] = weight_xc_N_0[i + 2]; |
| 376 | kptr[3] = weight_xc_N_0[i + 3]; |
| 377 | kptr[4] = weight_xc_N_1[i]; |
| 378 | kptr[5] = weight_xc_N_1[i + 1]; |
| 379 | kptr[6] = weight_xc_N_1[i + 2]; |
| 380 | kptr[7] = weight_xc_N_1[i + 3]; |
| 381 | kptr[8 + 0] = weight_xc_N_2[i]; |
| 382 | kptr[8 + 1] = weight_xc_N_2[i + 1]; |
| 383 | kptr[8 + 2] = weight_xc_N_2[i + 2]; |
| 384 | kptr[8 + 3] = weight_xc_N_2[i + 3]; |
| 385 | kptr[8 + 4] = weight_xc_N_3[i]; |
| 386 | kptr[8 + 5] = weight_xc_N_3[i + 1]; |
| 387 | kptr[8 + 6] = weight_xc_N_3[i + 2]; |
| 388 | kptr[8 + 7] = weight_xc_N_3[i + 3]; |
| 389 | |
| 390 | kptr += 16; |
| 391 | } |
| 392 | #else |
| 393 | for (; i + 7 < size; i += 8) |
| 394 | { |
| 395 | vst1_s8(kptr, vld1_s8(weight_xc_N_0 + i)); |
| 396 | vst1_s8(kptr + 8, vld1_s8(weight_xc_N_1 + i)); |
| 397 | vst1_s8(kptr + 16, vld1_s8(weight_xc_N_2 + i)); |
| 398 | vst1_s8(kptr + 24, vld1_s8(weight_xc_N_3 + i)); |
| 399 | kptr += 32; |
| 400 | } |
| 401 | #endif // __ARM_FEATURE_DOTPROD |
| 402 | for (; i + 1 < size; i += 2) |
| 403 | { |
| 404 | kptr[0] = weight_xc_N_0[i]; |
| 405 | kptr[1] = weight_xc_N_0[i + 1]; |
| 406 | kptr[2] = weight_xc_N_1[i]; |
| 407 | kptr[3] = weight_xc_N_1[i + 1]; |
| 408 | kptr[4] = weight_xc_N_2[i]; |
| 409 | kptr[5] = weight_xc_N_2[i + 1]; |
| 410 | kptr[6] = weight_xc_N_3[i]; |
| 411 | kptr[7] = weight_xc_N_3[i + 1]; |
| 412 | |
| 413 | kptr += 8; |
| 414 | } |
| 415 | for (; i < size; i++) |
| 416 | { |
| 417 | kptr[0] = weight_xc_N_0[i]; |
| 418 | kptr[1] = weight_xc_N_1[i]; |
| 419 | kptr[2] = weight_xc_N_2[i]; |
| 420 | kptr[3] = weight_xc_N_3[i]; |
| 421 | |
| 422 | kptr += 4; |
| 423 | } |
| 424 | |
| 425 | float32x4_t _xc_R0 = vld1q_f32(weight_xc_int8_scales_ptr + q); |
| 426 | float32x4_t _xc_U0 = vld1q_f32(weight_xc_int8_scales_ptr + num_output + q); |
| 427 | float32x4_t _xc_N0 = vld1q_f32(weight_xc_int8_scales_ptr + num_output * 2 + q); |
| 428 | float32x4_t _hc_R0 = vld1q_f32(weight_hc_int8_scales_ptr + q); |
| 429 | float32x4_t _hc_U0 = vld1q_f32(weight_hc_int8_scales_ptr + num_output + q); |
| 430 | float32x4_t _hc_N0 = vld1q_f32(weight_hc_int8_scales_ptr + num_output * 2 + q); |
| 431 | |
| 432 | #if __aarch64__ |
| 433 | float32x4_t _one = vdupq_n_f32(1.f); |
| 434 | float32x4_t _reciprocal_xc_R0 = vdivq_f32(_one, _xc_R0); |
| 435 | float32x4_t _reciprocal_xc_U0 = vdivq_f32(_one, _xc_U0); |
| 436 | float32x4_t _reciprocal_xc_N0 = vdivq_f32(_one, _xc_N0); |
| 437 | float32x4_t _reciprocal_hc_R0 = vdivq_f32(_one, _hc_R0); |
| 438 | float32x4_t _reciprocal_hc_U0 = vdivq_f32(_one, _hc_U0); |
| 439 | float32x4_t _reciprocal_hc_N0 = vdivq_f32(_one, _hc_N0); |
| 440 | #else |
| 441 | float32x4_t _reciprocal_xc_R0 = vrecpeq_f32(_xc_R0); |
| 442 | float32x4_t _reciprocal_xc_U0 = vrecpeq_f32(_xc_U0); |
| 443 | float32x4_t _reciprocal_xc_N0 = vrecpeq_f32(_xc_N0); |
| 444 | _reciprocal_xc_R0 = vmulq_f32(vrecpsq_f32(_xc_R0, _reciprocal_xc_R0), _reciprocal_xc_R0); |
| 445 | _reciprocal_xc_U0 = vmulq_f32(vrecpsq_f32(_xc_U0, _reciprocal_xc_U0), _reciprocal_xc_U0); |
| 446 | _reciprocal_xc_N0 = vmulq_f32(vrecpsq_f32(_xc_N0, _reciprocal_xc_N0), _reciprocal_xc_N0); |
| 447 | _reciprocal_xc_R0 = vmulq_f32(vrecpsq_f32(_xc_R0, _reciprocal_xc_R0), _reciprocal_xc_R0); |
| 448 | _reciprocal_xc_U0 = vmulq_f32(vrecpsq_f32(_xc_U0, _reciprocal_xc_U0), _reciprocal_xc_U0); |
| 449 | _reciprocal_xc_N0 = vmulq_f32(vrecpsq_f32(_xc_N0, _reciprocal_xc_N0), _reciprocal_xc_N0); |
| 450 | float32x4_t _reciprocal_hc_R0 = vrecpeq_f32(_hc_R0); |
| 451 | float32x4_t _reciprocal_hc_U0 = vrecpeq_f32(_hc_U0); |
| 452 | float32x4_t _reciprocal_hc_N0 = vrecpeq_f32(_hc_N0); |
| 453 | _reciprocal_hc_R0 = vmulq_f32(vrecpsq_f32(_hc_R0, _reciprocal_hc_R0), _reciprocal_hc_R0); |
| 454 | _reciprocal_hc_U0 = vmulq_f32(vrecpsq_f32(_hc_U0, _reciprocal_hc_U0), _reciprocal_hc_U0); |
| 455 | _reciprocal_hc_N0 = vmulq_f32(vrecpsq_f32(_hc_N0, _reciprocal_hc_N0), _reciprocal_hc_N0); |
| 456 | _reciprocal_hc_R0 = vmulq_f32(vrecpsq_f32(_hc_R0, _reciprocal_hc_R0), _reciprocal_hc_R0); |
| 457 | _reciprocal_hc_U0 = vmulq_f32(vrecpsq_f32(_hc_U0, _reciprocal_hc_U0), _reciprocal_hc_U0); |
| 458 | _reciprocal_hc_N0 = vmulq_f32(vrecpsq_f32(_hc_N0, _reciprocal_hc_N0), _reciprocal_hc_N0); |
| 459 | #endif |
| 460 | |
| 461 | vst1q_f32(descales_ptr, _reciprocal_xc_R0); |
| 462 | vst1q_f32(descales_ptr + 4, _reciprocal_xc_U0); |
| 463 | vst1q_f32(descales_ptr + 8, _reciprocal_hc_R0); |
| 464 | vst1q_f32(descales_ptr + 12, _reciprocal_hc_U0); |
| 465 | vst1q_f32(descales_ptr + 16, _reciprocal_hc_N0); |
| 466 | vst1q_f32(descales_ptr + 20, _reciprocal_xc_N0); |
| 467 | } |
| 468 | #endif // __ARM_NEON |
| 469 | for (; q < num_output; q++) |
| 470 | { |
| 471 | bias_c_RUBNWN[0] = bias_c_R[q]; |
| 472 | bias_c_RUBNWN[1] = bias_c_U[q]; |
| 473 | bias_c_RUBNWN[2] = bias_c_BN[q]; |
| 474 | bias_c_RUBNWN[3] = bias_c_WN[q]; |
| 475 | |
| 476 | bias_c_RUBNWN += 4; |
| 477 | |
| 478 | const signed char* weight_xc_R = weight_xc_dr.row<const signed char>(num_output * 0 + q); |
| 479 | const signed char* weight_xc_U = weight_xc_dr.row<const signed char>(num_output * 1 + q); |
| 480 | const signed char* weight_xc_N = weight_xc_dr.row<const signed char>(num_output * 2 + q); |
| 481 | |
| 482 | const signed char* weight_hc_R = weight_hc_dr.row<const signed char>(num_output * 0 + q); |
| 483 | const signed char* weight_hc_U = weight_hc_dr.row<const signed char>(num_output * 1 + q); |
| 484 | const signed char* weight_hc_N = weight_hc_dr.row<const signed char>(num_output * 2 + q); |
| 485 | |
| 486 | #if __ARM_NEON |
| 487 | signed char* kptr = weight_data_tm_dr.row<signed char>(q / 4 + q % 4); |
| 488 | float* descales_ptr = weight_data_tm_int8_descales_dr.row(q / 4 + q % 4); |
| 489 | #else |
| 490 | signed char* kptr = weight_data_tm_dr.row<signed char>(q); |
| 491 | float* descales_ptr = weight_data_tm_int8_descales_dr.row(q); |
| 492 | #endif // __ARM_NEON |
| 493 | |
| 494 | for (int i = 0; i < size; i++) |
| 495 | { |
| 496 | kptr[0] = weight_xc_R[i]; |
| 497 | kptr[1] = weight_xc_U[i]; |
| 498 | kptr += 2; |
| 499 | } |
| 500 | |
| 501 | for (int i = 0; i < num_output; i++) |
| 502 | { |
| 503 | kptr[0] = weight_hc_R[i]; |
| 504 | kptr[1] = weight_hc_U[i]; |
| 505 | kptr += 2; |
| 506 | } |
| 507 | |
| 508 | for (int i = 0; i < num_output; i++) |
| 509 | { |
| 510 | kptr[0] = weight_hc_N[i]; |
| 511 | kptr += 1; |
| 512 | } |
| 513 | |
| 514 | for (int i = 0; i < size; i++) |
| 515 | { |
| 516 | kptr[0] = weight_xc_N[i]; |
| 517 | kptr += 1; |
| 518 | } |
| 519 | |
| 520 | descales_ptr[0] = 1.f / weight_xc_int8_scales_ptr[num_output * 0 + q]; |
| 521 | descales_ptr[1] = 1.f / weight_xc_int8_scales_ptr[num_output * 1 + q]; |
| 522 | descales_ptr[2] = 1.f / weight_hc_int8_scales_ptr[num_output * 0 + q]; |
| 523 | descales_ptr[3] = 1.f / weight_hc_int8_scales_ptr[num_output * 1 + q]; |
| 524 | descales_ptr[4] = 1.f / weight_hc_int8_scales_ptr[num_output * 2 + q]; |
| 525 | descales_ptr[5] = 1.f / weight_xc_int8_scales_ptr[num_output * 2 + q]; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static void gru_int8_gate_output(const Mat& gates, Mat& hidden_state, Mat& top_blob, int ti, int elemtype, const Option& opt) |
| 531 | { |
| 532 | #if NCNN_RUNTIME_CPU && NCNN_VFPV4 && __ARM_NEON && !(__ARM_FP & 2) |
| 533 | if (ncnn::cpu_support_arm_vfpv4()) |
| 534 | { |
| 535 | gru_int8_gate_output_vfpv4(gates, hidden_state, top_blob, ti, elemtype, opt); |
| 536 | return; |
| 537 | } |
| 538 | #endif |
| 539 | |
| 540 | const int num_output = top_blob.w; |
| 541 | |
| 542 | // h_t := (1 - update) .* new + update .* h_{t-1} |
| 543 | float* output_data = top_blob.row(ti); |
| 544 | |
| 545 | float* hidden_ptr = hidden_state; |
| 546 | |
| 547 | int remain_num_output_start = 0; |
| 548 | #if __ARM_NEON |
| 549 | int nn_num_output = num_output >> 2; |
| 550 | #pragma omp parallel for num_threads(opt.num_threads) |
| 551 | for (int qq = 0; qq < nn_num_output; qq++) |
| 552 | { |
| 553 | int q = qq * 4; |
| 554 | |
| 555 | const float* gates_data = gates.row(q / 4); |
| 556 | |
| 557 | float32x4_t _gru_U0 = vld1q_f32(gates_data); |
| 558 | float32x4_t _gru_N0 = vld1q_f32(gates_data + 4); |
| 559 | |
| 560 | float32x4_t _gru_H0 = vaddq_f32(vmulq_f32(vsubq_f32(vdupq_n_f32(1.f), _gru_U0), _gru_N0), vmulq_f32(_gru_U0, vld1q_f32(hidden_ptr + q))); |
| 561 | |
| 562 | vst1q_f32(hidden_ptr + q, _gru_H0); |
| 563 | |
| 564 | if (elemtype == 1) |
| 565 | { |
| 566 | // fp32 |
| 567 | vst1q_f32(output_data + q, _gru_H0); |
| 568 | } |
| 569 | if (elemtype == 2) |
| 570 | { |
| 571 | // fp16 |
| 572 | unsigned short* outptr = (unsigned short*)output_data + q; |
| 573 | #if (__ARM_FP & 2) |
| 574 | #if NCNN_GNU_INLINE_ASM |
| 575 | #if __aarch64__ |
| 576 | asm volatile( |
| 577 | "fcvtn v0.4h, %2.4s \n" |
| 578 | "st1 {v0.4h}, [%0] \n" |
| 579 | : "=r"(outptr) // %0 |
| 580 | : "0"(outptr), |
| 581 | "w"(_gru_H0) |
| 582 | : "memory", "v0"); |
| 583 | #else // __aarch64__ |
| 584 | asm volatile( |
| 585 | "vcvt.f16.f32 d0, %q2 \n" |
| 586 | "vst1.u16 {d0}, [%0] \n" |
| 587 | : "=r"(outptr) // %0 |
| 588 | : "0"(outptr), |
| 589 | "w"(_gru_H0) |
| 590 | : "memory", "q0"); |
| 591 | #endif // __aarch64__ |
| 592 | #else // NCNN_GNU_INLINE_ASM |
| 593 | vst1_u16(outptr, (uint16x4_t)vcvt_f16_f32(_gru_H0)); |
| 594 | #endif // NCNN_GNU_INLINE_ASM |
| 595 | #else |
| 596 | outptr[q] = float32_to_float16(hidden_ptr[q]); |
| 597 | outptr[q + 1] = float32_to_float16(hidden_ptr[q + 1]); |
| 598 | outptr[q + 2] = float32_to_float16(hidden_ptr[q + 2]); |
| 599 | outptr[q + 3] = float32_to_float16(hidden_ptr[q + 3]); |
| 600 | #endif // (__ARM_FP & 2) |
| 601 | } |
| 602 | if (elemtype == 4) |
| 603 | { |
| 604 | // bf16 |
| 605 | vst1_u16((unsigned short*)output_data + q, float2bfloat(_gru_H0)); |
| 606 | } |
| 607 | } |
| 608 | remain_num_output_start += nn_num_output << 2; |
| 609 | #endif // __ARM_NEON |
| 610 | #pragma omp parallel for num_threads(opt.num_threads) |
| 611 | for (int q = remain_num_output_start; q < num_output; q++) |
| 612 | { |
| 613 | #if __ARM_NEON |
| 614 | const float* gates_data = gates.row(q / 4 + q % 4); |
| 615 | #else |
| 616 | const float* gates_data = gates.row(q); |
| 617 | #endif |
| 618 | |
| 619 | float U = gates_data[0]; |
| 620 | float N = gates_data[1]; |
| 621 | |
| 622 | float H = (1 - U) * N + U * hidden_ptr[q]; |
| 623 | |
| 624 | hidden_ptr[q] = H; |
| 625 | |
| 626 | if (elemtype == 1) |
| 627 | { |
| 628 | output_data[q] = H; |
| 629 | } |
| 630 | if (elemtype == 2) |
| 631 | { |
| 632 | ((unsigned short*)output_data)[q] = float32_to_float16(H); |
| 633 | } |
| 634 | if (elemtype == 4) |
| 635 | { |
| 636 | ((unsigned short*)output_data)[q] = float32_to_bfloat16(H); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static void gru_int8(const Mat& bottom_blob_int8, const Mat& bottom_blob_int8_descales, Mat& top_blob, int elemtype, int reverse, const Mat& weight_data_tm, const Mat& weight_data_tm_int8_descales, const Mat& bias_c, Mat& hidden_state, const Option& opt) |
| 642 | { |
| 643 | #if NCNN_RUNTIME_CPU && NCNN_ARM82DOT && __aarch64__ && !__ARM_FEATURE_DOTPROD |
| 644 | if (ncnn::cpu_support_arm_asimddp()) |
| 645 | { |
| 646 | gru_int8_asimddp(bottom_blob_int8, bottom_blob_int8_descales, top_blob, elemtype, reverse, weight_data_tm, weight_data_tm_int8_descales, bias_c, hidden_state, opt); |
| 647 | return; |
| 648 | } |
| 649 | #endif |
| 650 | |
| 651 | int size = bottom_blob_int8.w; |
| 652 | int T = bottom_blob_int8.h; |
| 653 | |
| 654 | int num_output = top_blob.w; |
| 655 | |
| 656 | // 2 x num_output |
| 657 | #if __ARM_NEON |
| 658 | Mat gates(4 * 2, num_output / 4 + num_output % 4, 4u, opt.workspace_allocator); |
| 659 | #else |
| 660 | Mat gates(2, num_output, 4u, opt.workspace_allocator); |
| 661 | #endif |
| 662 | |
| 663 | Mat hidden_state_int8(num_output, (size_t)1u, 1, opt.workspace_allocator); |
| 664 | float hidden_state_int8_scale = 1.f; |
| 665 | float hidden_state_int8_descale = 1.f; |
| 666 | |
| 667 | // unroll |
| 668 | for (int t = 0; t < T; t++) |
| 669 | { |
| 670 | int ti = reverse ? T - 1 - t : t; |
| 671 | |
| 672 | // dynamic quantize hidden_state |
| 673 | { |
| 674 | float absmax = 0.f; |
| 675 | for (int i = 0; i < num_output; i++) |
| 676 | { |
| 677 | absmax = std::max(absmax, (float)fabs(hidden_state[i])); |
| 678 | } |
| 679 | |
| 680 | if (absmax == 0.f) |
| 681 | { |
| 682 | hidden_state_int8.fill<signed char>(0); |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | hidden_state_int8_scale = 127.f / absmax; |
| 687 | hidden_state_int8_descale = absmax / 127.f; |
| 688 | |
| 689 | signed char* hs = hidden_state_int8; |
| 690 | for (int i = 0; i < num_output; i++) |
| 691 | { |
| 692 | hs[i] = float2int8(hidden_state[i] * hidden_state_int8_scale); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | int remain_num_output_start = 0; |
| 698 | #if __ARM_NEON |
| 699 | int nn_num_output = num_output >> 2; |
| 700 | remain_num_output_start = nn_num_output << 2; |
| 701 | |
| 702 | #pragma omp parallel for num_threads(opt.num_threads) |
| 703 | for (int qq = 0; qq < nn_num_output; qq++) |
| 704 | { |
| 705 | int q = qq * 4; |
| 706 | |
| 707 | const signed char* x = bottom_blob_int8.row<const signed char>(ti); |
| 708 | const signed char* hs = hidden_state_int8; |
| 709 | const float descale_x = bottom_blob_int8_descales[ti]; |
| 710 | const float descale_h = hidden_state_int8_descale; |
| 711 | |
| 712 | // gate reset update |
| 713 | const float* bias_c_RUBNWN = (const float*)bias_c + q * 4; |
| 714 | |
| 715 | const signed char* kptr = weight_data_tm.row<const signed char>(q / 4); |
| 716 | |
| 717 | const float* descales_ptr = weight_data_tm_int8_descales.row(q / 4); |
| 718 | |
| 719 | int32x4_t _gru_Rx0 = vdupq_n_s32(0); |
| 720 | int32x4_t _gru_Ux0 = vdupq_n_s32(0); |
| 721 | int i = 0; |
| 722 | #if __ARM_FEATURE_DOTPROD |
| 723 | int32x4_t _sum1 = vdupq_n_s32(0); |
| 724 | int32x4_t _sum2 = vdupq_n_s32(0); |
| 725 | for (; i + 7 < size; i += 8) |
| 726 | { |
| 727 | int8x8_t _xi = vld1_s8(x + i); |
| 728 | int8x16_t _w0 = vld1q_s8(kptr); |
| 729 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 730 | int8x16_t _w2 = vld1q_s8(kptr + 32); |
| 731 | int8x16_t _w3 = vld1q_s8(kptr + 48); |
| 732 | _gru_Rx0 = vdotq_lane_s32(_gru_Rx0, _w0, _xi, 0); |
| 733 | _gru_Ux0 = vdotq_lane_s32(_gru_Ux0, _w1, _xi, 0); |
| 734 | _sum1 = vdotq_lane_s32(_sum1, _w2, _xi, 1); |
| 735 | _sum2 = vdotq_lane_s32(_sum2, _w3, _xi, 1); |
| 736 | |
| 737 | kptr += 64; |
| 738 | } |
| 739 | _gru_Rx0 = vaddq_s32(_gru_Rx0, _sum1); |
| 740 | _gru_Ux0 = vaddq_s32(_gru_Ux0, _sum2); |
| 741 | #else |
| 742 | int32x4_t _sum0 = vdupq_n_s32(0); |
| 743 | int32x4_t _sum1 = vdupq_n_s32(0); |
| 744 | int32x4_t _sum2 = vdupq_n_s32(0); |
| 745 | int32x4_t _sum3 = vdupq_n_s32(0); |
| 746 | for (; i + 7 < size; i += 8) |
| 747 | { |
| 748 | #if NCNN_GNU_INLINE_ASM && !__aarch64__ |
| 749 | const signed char* xptr = x + i; |
| 750 | |
| 751 | asm volatile( |
| 752 | "vldm %1!, {d0-d7} \n" |
| 753 | "vld1.s8 {d16}, [%0] \n" |
| 754 | "vdup.32 d17, d16[0] \n" |
| 755 | "vdup.32 d16, d16[1] \n" |
| 756 | "vmull.s8 q4, d0, d17 \n" |
| 757 | "vmull.s8 q5, d1, d17 \n" |
| 758 | "vmull.s8 q6, d2, d17 \n" |
| 759 | "vmull.s8 q7, d3, d17 \n" |
| 760 | "vmlal.s8 q4, d4, d16 \n" |
| 761 | "vmlal.s8 q5, d5, d16 \n" |
| 762 | "vmlal.s8 q6, d6, d16 \n" |
| 763 | "vmlal.s8 q7, d7, d16 \n" |
| 764 | "vpadal.s16 %q2, q4 \n" |
| 765 | "vpadal.s16 %q3, q5 \n" |
| 766 | "vpadal.s16 %q4, q6 \n" |
| 767 | "vpadal.s16 %q5, q7 \n" |
| 768 | : "=r"(xptr), "=r"(kptr), "=w"(_sum0), "=w"(_sum1), "=w"(_sum2), "=w"(_sum3) |
| 769 | : "0"(xptr), "1"(kptr), "2"(_sum0), "3"(_sum1), "4"(_sum2), "5"(_sum3) |
| 770 | : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8"); |
| 771 | #else |
| 772 | int32x2_t _xi01 = vreinterpret_s32_s8(vld1_s8(x + i)); |
| 773 | int8x8_t _xi0 = vreinterpret_s8_s32(vdup_lane_s32(_xi01, 0)); |
| 774 | int8x8_t _xi1 = vreinterpret_s8_s32(vdup_lane_s32(_xi01, 1)); |
| 775 | int8x16_t _w0 = vld1q_s8(kptr); |
| 776 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 777 | int8x16_t _w2 = vld1q_s8(kptr + 32); |
| 778 | int8x16_t _w3 = vld1q_s8(kptr + 48); |
| 779 | |
| 780 | int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), _xi0); |
| 781 | int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), _xi0); |
| 782 | int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), _xi0); |
| 783 | int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), _xi0); |
| 784 | _s0 = vmlal_s8(_s0, vget_low_s8(_w2), _xi1); |
| 785 | _s1 = vmlal_s8(_s1, vget_high_s8(_w2), _xi1); |
| 786 | _s2 = vmlal_s8(_s2, vget_low_s8(_w3), _xi1); |
| 787 | _s3 = vmlal_s8(_s3, vget_high_s8(_w3), _xi1); |
| 788 | |
| 789 | _sum0 = vpadalq_s16(_sum0, _s0); |
| 790 | _sum1 = vpadalq_s16(_sum1, _s1); |
| 791 | _sum2 = vpadalq_s16(_sum2, _s2); |
| 792 | _sum3 = vpadalq_s16(_sum3, _s3); |
| 793 | |
| 794 | kptr += 64; |
| 795 | #endif |
| 796 | } |
| 797 | { |
| 798 | int32x2_t _s0 = vpadd_s32(vget_low_s32(_sum0), vget_high_s32(_sum0)); |
| 799 | int32x2_t _s1 = vpadd_s32(vget_low_s32(_sum1), vget_high_s32(_sum1)); |
| 800 | int32x2_t _s2 = vpadd_s32(vget_low_s32(_sum2), vget_high_s32(_sum2)); |
| 801 | int32x2_t _s3 = vpadd_s32(vget_low_s32(_sum3), vget_high_s32(_sum3)); |
| 802 | _gru_Rx0 = vaddq_s32(_gru_Rx0, vcombine_s32(_s0, _s1)); |
| 803 | _gru_Ux0 = vaddq_s32(_gru_Ux0, vcombine_s32(_s2, _s3)); |
| 804 | } |
| 805 | #endif // __ARM_FEATURE_DOTPROD |
| 806 | for (; i + 3 < size; i += 4) |
| 807 | { |
| 808 | #if __ARM_FEATURE_DOTPROD |
| 809 | int8x8_t _xi = vld1_s8(x + i); |
| 810 | int8x16_t _w0 = vld1q_s8(kptr); |
| 811 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 812 | _gru_Rx0 = vdotq_lane_s32(_gru_Rx0, _w0, _xi, 0); |
| 813 | _gru_Ux0 = vdotq_lane_s32(_gru_Ux0, _w1, _xi, 0); |
| 814 | #else |
| 815 | int16x4_t _xi01 = vreinterpret_s16_s8(vld1_s8(x + i)); |
| 816 | int8x8_t _xi0 = vreinterpret_s8_s16(vdup_lane_s16(_xi01, 0)); |
| 817 | int8x8_t _xi1 = vreinterpret_s8_s16(vdup_lane_s16(_xi01, 1)); |
| 818 | int8x16_t _weight_xc_RU0 = vld1q_s8(kptr); |
| 819 | int8x16_t _weight_xc_RU1 = vld1q_s8(kptr + 16); |
| 820 | |
| 821 | int16x8_t _gru_Rx = vmull_s8(vget_low_s8(_weight_xc_RU0), _xi0); |
| 822 | int16x8_t _gru_Ux = vmull_s8(vget_high_s8(_weight_xc_RU0), _xi0); |
| 823 | _gru_Rx = vmlal_s8(_gru_Rx, vget_low_s8(_weight_xc_RU1), _xi1); |
| 824 | _gru_Ux = vmlal_s8(_gru_Ux, vget_high_s8(_weight_xc_RU1), _xi1); |
| 825 | |
| 826 | _gru_Rx0 = vpadalq_s16(_gru_Rx0, _gru_Rx); |
| 827 | _gru_Ux0 = vpadalq_s16(_gru_Ux0, _gru_Ux); |
| 828 | #endif // __ARM_FEATURE_DOTPROD |
| 829 | |
| 830 | kptr += 32; |
| 831 | } |
| 832 | for (; i + 1 < size; i += 2) |
| 833 | { |
| 834 | int8x8_t _xi = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(vld1_s8(x + i)), 0)); |
| 835 | int8x16_t _weight_xc_RU = vld1q_s8(kptr); |
| 836 | |
| 837 | int16x8_t _gru_Rx = vmull_s8(vget_low_s8(_weight_xc_RU), _xi); |
| 838 | int16x8_t _gru_Ux = vmull_s8(vget_high_s8(_weight_xc_RU), _xi); |
| 839 | |
| 840 | _gru_Rx0 = vpadalq_s16(_gru_Rx0, _gru_Rx); |
| 841 | _gru_Ux0 = vpadalq_s16(_gru_Ux0, _gru_Ux); |
| 842 | |
| 843 | kptr += 16; |
| 844 | } |
| 845 | for (; i < size; i++) |
| 846 | { |
| 847 | int8x8_t _xi = vdup_n_s8(x[i]); |
| 848 | int8x8_t _weight_xc_RU = vld1_s8(kptr); |
| 849 | |
| 850 | int16x8_t _gru_RxUx = vmull_s8(_weight_xc_RU, _xi); |
| 851 | _gru_Rx0 = vaddw_s16(_gru_Rx0, vget_low_s16(_gru_RxUx)); |
| 852 | _gru_Ux0 = vaddw_s16(_gru_Ux0, vget_high_s16(_gru_RxUx)); |
| 853 | |
| 854 | kptr += 8; |
| 855 | } |
| 856 | |
| 857 | int32x4_t _gru_Rh0 = vdupq_n_s32(0); |
| 858 | int32x4_t _gru_Uh0 = vdupq_n_s32(0); |
| 859 | i = 0; |
| 860 | #if __ARM_FEATURE_DOTPROD |
| 861 | _sum1 = vdupq_n_s32(0); |
| 862 | _sum2 = vdupq_n_s32(0); |
| 863 | for (; i + 7 < num_output; i += 8) |
| 864 | { |
| 865 | int8x8_t _h_cont = vld1_s8(hs + i); |
| 866 | int8x16_t _w0 = vld1q_s8(kptr); |
| 867 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 868 | int8x16_t _w2 = vld1q_s8(kptr + 32); |
| 869 | int8x16_t _w3 = vld1q_s8(kptr + 48); |
| 870 | _gru_Rh0 = vdotq_lane_s32(_gru_Rh0, _w0, _h_cont, 0); |
| 871 | _gru_Uh0 = vdotq_lane_s32(_gru_Uh0, _w1, _h_cont, 0); |
| 872 | _sum1 = vdotq_lane_s32(_sum1, _w2, _h_cont, 1); |
| 873 | _sum2 = vdotq_lane_s32(_sum2, _w3, _h_cont, 1); |
| 874 | |
| 875 | kptr += 64; |
| 876 | } |
| 877 | _gru_Rh0 = vaddq_s32(_gru_Rh0, _sum1); |
| 878 | _gru_Uh0 = vaddq_s32(_gru_Uh0, _sum2); |
| 879 | #else |
| 880 | _sum0 = vdupq_n_s32(0); |
| 881 | _sum1 = vdupq_n_s32(0); |
| 882 | _sum2 = vdupq_n_s32(0); |
| 883 | _sum3 = vdupq_n_s32(0); |
| 884 | for (; i + 7 < num_output; i += 8) |
| 885 | { |
| 886 | #if NCNN_GNU_INLINE_ASM && !__aarch64__ |
| 887 | const signed char* hsptr = hs + i; |
| 888 | |
| 889 | asm volatile( |
| 890 | "vldm %1!, {d0-d7} \n" |
| 891 | "vld1.s8 {d16}, [%0] \n" |
| 892 | "vdup.32 d17, d16[0] \n" |
| 893 | "vdup.32 d16, d16[1] \n" |
| 894 | "vmull.s8 q4, d0, d17 \n" |
| 895 | "vmull.s8 q5, d1, d17 \n" |
| 896 | "vmull.s8 q6, d2, d17 \n" |
| 897 | "vmull.s8 q7, d3, d17 \n" |
| 898 | "vmlal.s8 q4, d4, d16 \n" |
| 899 | "vmlal.s8 q5, d5, d16 \n" |
| 900 | "vmlal.s8 q6, d6, d16 \n" |
| 901 | "vmlal.s8 q7, d7, d16 \n" |
| 902 | "vpadal.s16 %q2, q4 \n" |
| 903 | "vpadal.s16 %q3, q5 \n" |
| 904 | "vpadal.s16 %q4, q6 \n" |
| 905 | "vpadal.s16 %q5, q7 \n" |
| 906 | : "=r"(hsptr), "=r"(kptr), "=w"(_sum0), "=w"(_sum1), "=w"(_sum2), "=w"(_sum3) |
| 907 | : "0"(hsptr), "1"(kptr), "2"(_sum0), "3"(_sum1), "4"(_sum2), "5"(_sum3) |
| 908 | : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8"); |
| 909 | #else |
| 910 | int32x2_t _h_cont01 = vreinterpret_s32_s8(vld1_s8(hs + i)); |
| 911 | int8x8_t _h_cont0 = vreinterpret_s8_s32(vdup_lane_s32(_h_cont01, 0)); |
| 912 | int8x8_t _h_cont1 = vreinterpret_s8_s32(vdup_lane_s32(_h_cont01, 1)); |
| 913 | int8x16_t _w0 = vld1q_s8(kptr); |
| 914 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 915 | int8x16_t _w2 = vld1q_s8(kptr + 32); |
| 916 | int8x16_t _w3 = vld1q_s8(kptr + 48); |
| 917 | |
| 918 | int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), _h_cont0); |
| 919 | int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), _h_cont0); |
| 920 | int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), _h_cont0); |
| 921 | int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), _h_cont0); |
| 922 | _s0 = vmlal_s8(_s0, vget_low_s8(_w2), _h_cont1); |
| 923 | _s1 = vmlal_s8(_s1, vget_high_s8(_w2), _h_cont1); |
| 924 | _s2 = vmlal_s8(_s2, vget_low_s8(_w3), _h_cont1); |
| 925 | _s3 = vmlal_s8(_s3, vget_high_s8(_w3), _h_cont1); |
| 926 | |
| 927 | _sum0 = vpadalq_s16(_sum0, _s0); |
| 928 | _sum1 = vpadalq_s16(_sum1, _s1); |
| 929 | _sum2 = vpadalq_s16(_sum2, _s2); |
| 930 | _sum3 = vpadalq_s16(_sum3, _s3); |
| 931 | |
| 932 | kptr += 64; |
| 933 | #endif |
| 934 | } |
| 935 | { |
| 936 | int32x2_t _s0 = vpadd_s32(vget_low_s32(_sum0), vget_high_s32(_sum0)); |
| 937 | int32x2_t _s1 = vpadd_s32(vget_low_s32(_sum1), vget_high_s32(_sum1)); |
| 938 | int32x2_t _s2 = vpadd_s32(vget_low_s32(_sum2), vget_high_s32(_sum2)); |
| 939 | int32x2_t _s3 = vpadd_s32(vget_low_s32(_sum3), vget_high_s32(_sum3)); |
| 940 | _gru_Rh0 = vaddq_s32(_gru_Rh0, vcombine_s32(_s0, _s1)); |
| 941 | _gru_Uh0 = vaddq_s32(_gru_Uh0, vcombine_s32(_s2, _s3)); |
| 942 | } |
| 943 | #endif // __ARM_FEATURE_DOTPROD |
| 944 | for (; i + 3 < num_output; i += 4) |
| 945 | { |
| 946 | #if __ARM_FEATURE_DOTPROD |
| 947 | int8x8_t _h_cont = vld1_s8(hs + i); |
| 948 | int8x16_t _w0 = vld1q_s8(kptr); |
| 949 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 950 | _gru_Rh0 = vdotq_lane_s32(_gru_Rh0, _w0, _h_cont, 0); |
| 951 | _gru_Uh0 = vdotq_lane_s32(_gru_Uh0, _w1, _h_cont, 0); |
| 952 | #else |
| 953 | int16x4_t _h_cont01 = vreinterpret_s16_s8(vld1_s8(hs + i)); |
| 954 | int8x8_t _h_cont0 = vreinterpret_s8_s16(vdup_lane_s16(_h_cont01, 0)); |
| 955 | int8x8_t _h_cont1 = vreinterpret_s8_s16(vdup_lane_s16(_h_cont01, 1)); |
| 956 | int8x16_t _weight_hc_RU0 = vld1q_s8(kptr); |
| 957 | int8x16_t _weight_hc_RU1 = vld1q_s8(kptr + 16); |
| 958 | |
| 959 | int16x8_t _gru_Rh = vmull_s8(vget_low_s8(_weight_hc_RU0), _h_cont0); |
| 960 | int16x8_t _gru_Uh = vmull_s8(vget_high_s8(_weight_hc_RU0), _h_cont0); |
| 961 | _gru_Rh = vmlal_s8(_gru_Rh, vget_low_s8(_weight_hc_RU1), _h_cont1); |
| 962 | _gru_Uh = vmlal_s8(_gru_Uh, vget_high_s8(_weight_hc_RU1), _h_cont1); |
| 963 | |
| 964 | _gru_Rh0 = vpadalq_s16(_gru_Rh0, _gru_Rh); |
| 965 | _gru_Uh0 = vpadalq_s16(_gru_Uh0, _gru_Uh); |
| 966 | #endif // __ARM_FEATURE_DOTPROD |
| 967 | |
| 968 | kptr += 32; |
| 969 | } |
| 970 | for (; i + 1 < num_output; i += 2) |
| 971 | { |
| 972 | int8x8_t _h_cont = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(vld1_s8(hs + i)), 0)); |
| 973 | int8x16_t _weight_hc_RU = vld1q_s8(kptr); |
| 974 | |
| 975 | int16x8_t _gru_Rh = vmull_s8(vget_low_s8(_weight_hc_RU), _h_cont); |
| 976 | int16x8_t _gru_Uh = vmull_s8(vget_high_s8(_weight_hc_RU), _h_cont); |
| 977 | |
| 978 | _gru_Rh0 = vpadalq_s16(_gru_Rh0, _gru_Rh); |
| 979 | _gru_Uh0 = vpadalq_s16(_gru_Uh0, _gru_Uh); |
| 980 | |
| 981 | kptr += 16; |
| 982 | } |
| 983 | for (; i < num_output; i++) |
| 984 | { |
| 985 | int8x8_t _h_cont = vdup_n_s8(hs[i]); |
| 986 | int8x8_t _weight_hc_RU = vld1_s8(kptr); |
| 987 | |
| 988 | int16x8_t _gru_RhUh = vmull_s8(_weight_hc_RU, _h_cont); |
| 989 | _gru_Rh0 = vaddw_s16(_gru_Rh0, vget_low_s16(_gru_RhUh)); |
| 990 | _gru_Uh0 = vaddw_s16(_gru_Uh0, vget_high_s16(_gru_RhUh)); |
| 991 | |
| 992 | kptr += 8; |
| 993 | } |
| 994 | |
| 995 | float32x4_t _descale_x = vdupq_n_f32(descale_x); |
| 996 | float32x4_t _descale_h = vdupq_n_f32(descale_h); |
| 997 | |
| 998 | float32x4_t _gru_R0 = vld1q_f32(bias_c_RUBNWN); |
| 999 | float32x4_t _gru_U0 = vld1q_f32(bias_c_RUBNWN + 4); |
| 1000 | |
| 1001 | float32x4_t _descale_xc_R0 = vld1q_f32(descales_ptr); |
| 1002 | float32x4_t _descale_xc_U0 = vld1q_f32(descales_ptr + 4); |
| 1003 | |
| 1004 | _gru_R0 = vmlaq_f32(_gru_R0, vcvtq_f32_s32(_gru_Rx0), vmulq_f32(_descale_x, _descale_xc_R0)); |
| 1005 | _gru_U0 = vmlaq_f32(_gru_U0, vcvtq_f32_s32(_gru_Ux0), vmulq_f32(_descale_x, _descale_xc_U0)); |
| 1006 | |
| 1007 | float32x4_t _descale_hc_R0 = vld1q_f32(descales_ptr + 8); |
| 1008 | float32x4_t _descale_hc_U0 = vld1q_f32(descales_ptr + 12); |
| 1009 | |
| 1010 | _gru_R0 = vmlaq_f32(_gru_R0, vcvtq_f32_s32(_gru_Rh0), vmulq_f32(_descale_h, _descale_hc_R0)); |
| 1011 | _gru_U0 = vmlaq_f32(_gru_U0, vcvtq_f32_s32(_gru_Uh0), vmulq_f32(_descale_h, _descale_hc_U0)); |
| 1012 | |
| 1013 | // sigmoid(R) |
| 1014 | // sigmoid(U) |
| 1015 | _gru_R0 = sigmoid_ps(_gru_R0); |
| 1016 | _gru_U0 = sigmoid_ps(_gru_U0); |
| 1017 | |
| 1018 | // gate new |
| 1019 | |
| 1020 | int32x4_t _gru_Nh0 = vdupq_n_s32(0); |
| 1021 | i = 0; |
| 1022 | #if __ARM_FEATURE_DOTPROD |
| 1023 | _sum1 = vdupq_n_s32(0); |
| 1024 | for (; i + 7 < num_output; i += 8) |
| 1025 | { |
| 1026 | int8x8_t _h_cont = vld1_s8(hs + i); |
| 1027 | int8x16_t _w0 = vld1q_s8(kptr); |
| 1028 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 1029 | _gru_Nh0 = vdotq_lane_s32(_gru_Nh0, _w0, _h_cont, 0); |
| 1030 | _sum1 = vdotq_lane_s32(_sum1, _w1, _h_cont, 1); |
| 1031 | |
| 1032 | kptr += 32; |
| 1033 | } |
| 1034 | _gru_Nh0 = vaddq_s32(_gru_Nh0, _sum1); |
| 1035 | #else |
| 1036 | _sum0 = vdupq_n_s32(0); |
| 1037 | _sum1 = vdupq_n_s32(0); |
| 1038 | _sum2 = vdupq_n_s32(0); |
| 1039 | _sum3 = vdupq_n_s32(0); |
| 1040 | for (; i + 15 < num_output; i += 16) |
| 1041 | { |
| 1042 | #if NCNN_GNU_INLINE_ASM && !__aarch64__ |
| 1043 | const signed char* hsptr = hs + i; |
| 1044 | |
| 1045 | asm volatile( |
| 1046 | "vldm %1!, {d0-d7} \n" |
| 1047 | "vld1.s8 {d16-d17}, [%0] \n" |
| 1048 | "vmull.s8 q4, d0, d16 \n" |
| 1049 | "vmull.s8 q5, d1, d16 \n" |
| 1050 | "vmull.s8 q6, d2, d16 \n" |
| 1051 | "vmull.s8 q7, d3, d16 \n" |
| 1052 | "vmlal.s8 q4, d4, d17 \n" |
| 1053 | "vmlal.s8 q5, d5, d17 \n" |
| 1054 | "vmlal.s8 q6, d6, d17 \n" |
| 1055 | "vmlal.s8 q7, d7, d17 \n" |
| 1056 | "vpadal.s16 %q2, q4 \n" |
| 1057 | "vpadal.s16 %q3, q5 \n" |
| 1058 | "vpadal.s16 %q4, q6 \n" |
| 1059 | "vpadal.s16 %q5, q7 \n" |
| 1060 | : "=r"(hsptr), "=r"(kptr), "=w"(_sum0), "=w"(_sum1), "=w"(_sum2), "=w"(_sum3) |
| 1061 | : "0"(hsptr), "1"(kptr), "2"(_sum0), "3"(_sum1), "4"(_sum2), "5"(_sum3) |
| 1062 | : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8"); |
| 1063 | #else |
| 1064 | int8x16_t _h_cont = vld1q_s8(hs + i); |
| 1065 | int8x16_t _w0 = vld1q_s8(kptr); |
| 1066 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 1067 | int8x16_t _w2 = vld1q_s8(kptr + 32); |
| 1068 | int8x16_t _w3 = vld1q_s8(kptr + 48); |
| 1069 | |
| 1070 | int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), vget_low_s8(_h_cont)); |
| 1071 | int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), vget_low_s8(_h_cont)); |
| 1072 | int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), vget_low_s8(_h_cont)); |
| 1073 | int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), vget_low_s8(_h_cont)); |
| 1074 | _s0 = vmlal_s8(_s0, vget_low_s8(_w2), vget_high_s8(_h_cont)); |
| 1075 | _s1 = vmlal_s8(_s1, vget_high_s8(_w2), vget_high_s8(_h_cont)); |
| 1076 | _s2 = vmlal_s8(_s2, vget_low_s8(_w3), vget_high_s8(_h_cont)); |
| 1077 | _s3 = vmlal_s8(_s3, vget_high_s8(_w3), vget_high_s8(_h_cont)); |
| 1078 | _sum0 = vpadalq_s16(_sum0, _s0); |
| 1079 | _sum1 = vpadalq_s16(_sum1, _s1); |
| 1080 | _sum2 = vpadalq_s16(_sum2, _s2); |
| 1081 | _sum3 = vpadalq_s16(_sum3, _s3); |
| 1082 | |
| 1083 | kptr += 64; |
| 1084 | #endif |
| 1085 | } |
| 1086 | for (; i + 7 < num_output; i += 8) |
| 1087 | { |
| 1088 | int8x8_t _h_cont = vld1_s8(hs + i); |
| 1089 | int8x16_t _w0 = vld1q_s8(kptr); |
| 1090 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 1091 | |
| 1092 | int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), _h_cont); |
| 1093 | int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), _h_cont); |
| 1094 | int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), _h_cont); |
| 1095 | int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), _h_cont); |
| 1096 | _sum0 = vpadalq_s16(_sum0, _s0); |
| 1097 | _sum1 = vpadalq_s16(_sum1, _s1); |
| 1098 | _sum2 = vpadalq_s16(_sum2, _s2); |
| 1099 | _sum3 = vpadalq_s16(_sum3, _s3); |
| 1100 | |
| 1101 | kptr += 32; |
| 1102 | } |
| 1103 | { |
| 1104 | int32x4x2_t _tmp0 = vzipq_s32(_sum0, _sum1); |
| 1105 | int32x4x2_t _tmp1 = vzipq_s32(_sum2, _sum3); |
| 1106 | _sum0 = vcombine_s32(vget_low_s32(_tmp0.val[0]), vget_low_s32(_tmp1.val[0])); |
| 1107 | _sum1 = vcombine_s32(vget_high_s32(_tmp0.val[0]), vget_high_s32(_tmp1.val[0])); |
| 1108 | _sum2 = vcombine_s32(vget_low_s32(_tmp0.val[1]), vget_low_s32(_tmp1.val[1])); |
| 1109 | _sum3 = vcombine_s32(vget_high_s32(_tmp0.val[1]), vget_high_s32(_tmp1.val[1])); |
| 1110 | } |
| 1111 | _gru_Nh0 = vaddq_s32(_gru_Nh0, _sum0); |
| 1112 | _gru_Nh0 = vaddq_s32(_gru_Nh0, _sum1); |
| 1113 | _gru_Nh0 = vaddq_s32(_gru_Nh0, _sum2); |
| 1114 | _gru_Nh0 = vaddq_s32(_gru_Nh0, _sum3); |
| 1115 | #endif // __ARM_FEATURE_DOTPROD |
| 1116 | for (; i + 3 < num_output; i += 4) |
| 1117 | { |
| 1118 | #if __ARM_FEATURE_DOTPROD |
| 1119 | int8x8_t _h_cont = vld1_s8(hs + i); |
| 1120 | int8x16_t _w = vld1q_s8(kptr); |
| 1121 | _gru_Nh0 = vdotq_lane_s32(_gru_Nh0, _w, _h_cont, 0); |
| 1122 | #else |
| 1123 | int16x4_t _h_cont01 = vreinterpret_s16_s8(vld1_s8(hs + i)); |
| 1124 | int8x8_t _h_cont0 = vreinterpret_s8_s16(vdup_lane_s16(_h_cont01, 0)); |
| 1125 | int8x8_t _h_cont1 = vreinterpret_s8_s16(vdup_lane_s16(_h_cont01, 1)); |
| 1126 | int8x16_t _w01 = vld1q_s8(kptr); |
| 1127 | |
| 1128 | int16x8_t _gru_Nh = vmull_s8(vget_low_s8(_w01), _h_cont0); |
| 1129 | _gru_Nh = vmlal_s8(_gru_Nh, vget_high_s8(_w01), _h_cont1); |
| 1130 | _gru_Nh0 = vpadalq_s16(_gru_Nh0, _gru_Nh); |
| 1131 | #endif // __ARM_FEATURE_DOTPROD |
| 1132 | |
| 1133 | kptr += 16; |
| 1134 | } |
| 1135 | for (; i + 1 < num_output; i += 2) |
| 1136 | { |
| 1137 | int8x8_t _h_cont = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(vld1_s8(hs + i)), 0)); |
| 1138 | int8x8_t _w = vld1_s8(kptr); |
| 1139 | |
| 1140 | int16x8_t _gru_Nh = vmull_s8(_w, _h_cont); |
| 1141 | _gru_Nh0 = vpadalq_s16(_gru_Nh0, _gru_Nh); |
| 1142 | |
| 1143 | kptr += 8; |
| 1144 | } |
| 1145 | for (; i < num_output; i++) |
| 1146 | { |
| 1147 | int8x8_t _h_cont = vdup_n_s8(hs[i]); |
| 1148 | int8x8_t _w = vld1_s8(kptr); |
| 1149 | |
| 1150 | int16x8_t _gru_Nh = vmull_s8(_w, _h_cont); |
| 1151 | _gru_Nh0 = vaddw_s16(_gru_Nh0, vget_low_s16(_gru_Nh)); |
| 1152 | |
| 1153 | kptr += 4; |
| 1154 | } |
| 1155 | |
| 1156 | int32x4_t _gru_Nx0 = vdupq_n_s32(0); |
| 1157 | i = 0; |
| 1158 | #if __ARM_FEATURE_DOTPROD |
| 1159 | _sum1 = vdupq_n_s32(0); |
| 1160 | for (; i + 7 < size; i += 8) |
| 1161 | { |
| 1162 | int8x8_t _xi = vld1_s8(x + i); |
| 1163 | int8x16_t _w0 = vld1q_s8(kptr); |
| 1164 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 1165 | _gru_Nx0 = vdotq_lane_s32(_gru_Nx0, _w0, _xi, 0); |
| 1166 | _sum1 = vdotq_lane_s32(_sum1, _w1, _xi, 1); |
| 1167 | |
| 1168 | kptr += 32; |
| 1169 | } |
| 1170 | _gru_Nx0 = vaddq_s32(_gru_Nx0, _sum1); |
| 1171 | #else |
| 1172 | _sum0 = vdupq_n_s32(0); |
| 1173 | _sum1 = vdupq_n_s32(0); |
| 1174 | _sum2 = vdupq_n_s32(0); |
| 1175 | _sum3 = vdupq_n_s32(0); |
| 1176 | for (; i + 15 < size; i += 16) |
| 1177 | { |
| 1178 | #if NCNN_GNU_INLINE_ASM && !__aarch64__ |
| 1179 | const signed char* xptr = x + i; |
| 1180 | |
| 1181 | asm volatile( |
| 1182 | "vldm %1!, {d0-d7} \n" |
| 1183 | "vld1.s8 {d16-d17}, [%0] \n" |
| 1184 | "vmull.s8 q4, d0, d16 \n" |
| 1185 | "vmull.s8 q5, d1, d16 \n" |
| 1186 | "vmull.s8 q6, d2, d16 \n" |
| 1187 | "vmull.s8 q7, d3, d16 \n" |
| 1188 | "vmlal.s8 q4, d4, d17 \n" |
| 1189 | "vmlal.s8 q5, d5, d17 \n" |
| 1190 | "vmlal.s8 q6, d6, d17 \n" |
| 1191 | "vmlal.s8 q7, d7, d17 \n" |
| 1192 | "vpadal.s16 %q2, q4 \n" |
| 1193 | "vpadal.s16 %q3, q5 \n" |
| 1194 | "vpadal.s16 %q4, q6 \n" |
| 1195 | "vpadal.s16 %q5, q7 \n" |
| 1196 | : "=r"(xptr), "=r"(kptr), "=w"(_sum0), "=w"(_sum1), "=w"(_sum2), "=w"(_sum3) |
| 1197 | : "0"(xptr), "1"(kptr), "2"(_sum0), "3"(_sum1), "4"(_sum2), "5"(_sum3) |
| 1198 | : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8"); |
| 1199 | #else |
| 1200 | int8x16_t _xi = vld1q_s8(x + i); |
| 1201 | int8x16_t _w0 = vld1q_s8(kptr); |
| 1202 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 1203 | int8x16_t _w2 = vld1q_s8(kptr + 32); |
| 1204 | int8x16_t _w3 = vld1q_s8(kptr + 48); |
| 1205 | |
| 1206 | int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), vget_low_s8(_xi)); |
| 1207 | int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), vget_low_s8(_xi)); |
| 1208 | int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), vget_low_s8(_xi)); |
| 1209 | int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), vget_low_s8(_xi)); |
| 1210 | _s0 = vmlal_s8(_s0, vget_low_s8(_w2), vget_high_s8(_xi)); |
| 1211 | _s1 = vmlal_s8(_s1, vget_high_s8(_w2), vget_high_s8(_xi)); |
| 1212 | _s2 = vmlal_s8(_s2, vget_low_s8(_w3), vget_high_s8(_xi)); |
| 1213 | _s3 = vmlal_s8(_s3, vget_high_s8(_w3), vget_high_s8(_xi)); |
| 1214 | _sum0 = vpadalq_s16(_sum0, _s0); |
| 1215 | _sum1 = vpadalq_s16(_sum1, _s1); |
| 1216 | _sum2 = vpadalq_s16(_sum2, _s2); |
| 1217 | _sum3 = vpadalq_s16(_sum3, _s3); |
| 1218 | |
| 1219 | kptr += 64; |
| 1220 | #endif |
| 1221 | } |
| 1222 | for (; i + 7 < size; i += 8) |
| 1223 | { |
| 1224 | int8x8_t _xi = vld1_s8(x + i); |
| 1225 | int8x16_t _w0 = vld1q_s8(kptr); |
| 1226 | int8x16_t _w1 = vld1q_s8(kptr + 16); |
| 1227 | |
| 1228 | int16x8_t _s0 = vmull_s8(vget_low_s8(_w0), _xi); |
| 1229 | int16x8_t _s1 = vmull_s8(vget_high_s8(_w0), _xi); |
| 1230 | int16x8_t _s2 = vmull_s8(vget_low_s8(_w1), _xi); |
| 1231 | int16x8_t _s3 = vmull_s8(vget_high_s8(_w1), _xi); |
| 1232 | _sum0 = vpadalq_s16(_sum0, _s0); |
| 1233 | _sum1 = vpadalq_s16(_sum1, _s1); |
| 1234 | _sum2 = vpadalq_s16(_sum2, _s2); |
| 1235 | _sum3 = vpadalq_s16(_sum3, _s3); |
| 1236 | |
| 1237 | kptr += 32; |
| 1238 | } |
| 1239 | { |
| 1240 | int32x4x2_t _tmp0 = vzipq_s32(_sum0, _sum1); |
| 1241 | int32x4x2_t _tmp1 = vzipq_s32(_sum2, _sum3); |
| 1242 | _sum0 = vcombine_s32(vget_low_s32(_tmp0.val[0]), vget_low_s32(_tmp1.val[0])); |
| 1243 | _sum1 = vcombine_s32(vget_high_s32(_tmp0.val[0]), vget_high_s32(_tmp1.val[0])); |
| 1244 | _sum2 = vcombine_s32(vget_low_s32(_tmp0.val[1]), vget_low_s32(_tmp1.val[1])); |
| 1245 | _sum3 = vcombine_s32(vget_high_s32(_tmp0.val[1]), vget_high_s32(_tmp1.val[1])); |
| 1246 | } |
| 1247 | _gru_Nx0 = vaddq_s32(_gru_Nx0, _sum0); |
| 1248 | _gru_Nx0 = vaddq_s32(_gru_Nx0, _sum1); |
| 1249 | _gru_Nx0 = vaddq_s32(_gru_Nx0, _sum2); |
| 1250 | _gru_Nx0 = vaddq_s32(_gru_Nx0, _sum3); |
| 1251 | #endif // __ARM_FEATURE_DOTPROD |
| 1252 | for (; i + 3 < size; i += 4) |
| 1253 | { |
| 1254 | #if __ARM_FEATURE_DOTPROD |
| 1255 | int8x8_t _xi = vld1_s8(x + i); |
| 1256 | int8x16_t _w = vld1q_s8(kptr); |
| 1257 | _gru_Nx0 = vdotq_lane_s32(_gru_Nx0, _w, _xi, 0); |
| 1258 | #else |
| 1259 | int16x4_t _xi01 = vreinterpret_s16_s8(vld1_s8(x + i)); |
| 1260 | int8x8_t _xi0 = vreinterpret_s8_s16(vdup_lane_s16(_xi01, 0)); |
| 1261 | int8x8_t _xi1 = vreinterpret_s8_s16(vdup_lane_s16(_xi01, 1)); |
| 1262 | int8x16_t _w01 = vld1q_s8(kptr); |
| 1263 | |
| 1264 | int16x8_t _gru_Nx = vmull_s8(vget_low_s8(_w01), _xi0); |
| 1265 | _gru_Nx = vmlal_s8(_gru_Nx, vget_high_s8(_w01), _xi1); |
| 1266 | _gru_Nx0 = vpadalq_s16(_gru_Nx0, _gru_Nx); |
| 1267 | #endif // __ARM_FEATURE_DOTPROD |
| 1268 | |
| 1269 | kptr += 16; |
| 1270 | } |
| 1271 | for (; i + 1 < size; i += 2) |
| 1272 | { |
| 1273 | int8x8_t _xi = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(vld1_s8(x + i)), 0)); |
| 1274 | int8x8_t _w = vld1_s8(kptr); |
| 1275 | |
| 1276 | int16x8_t _gru_Nx = vmull_s8(_w, _xi); |
| 1277 | _gru_Nx0 = vpadalq_s16(_gru_Nx0, _gru_Nx); |
| 1278 | |
| 1279 | kptr += 8; |
| 1280 | } |
| 1281 | for (; i < size; i++) |
| 1282 | { |
| 1283 | int8x8_t _xi = vdup_n_s8(x[i]); |
| 1284 | int8x8_t _w = vld1_s8(kptr); |
| 1285 | |
| 1286 | int16x8_t _gru_Nx = vmull_s8(_w, _xi); |
| 1287 | _gru_Nx0 = vaddw_s16(_gru_Nx0, vget_low_s16(_gru_Nx)); |
| 1288 | |
| 1289 | kptr += 4; |
| 1290 | } |
| 1291 | |
| 1292 | float32x4_t _gru_N0 = vld1q_f32(bias_c_RUBNWN + 8); |
| 1293 | |
| 1294 | float32x4_t _descale_hc_N0 = vld1q_f32(descales_ptr + 16); |
| 1295 | |
| 1296 | _gru_N0 = vmlaq_f32(_gru_N0, vcvtq_f32_s32(_gru_Nh0), vmulq_f32(_descale_h, _descale_hc_N0)); |
| 1297 | |
| 1298 | _gru_N0 = vmlaq_f32(vld1q_f32(bias_c_RUBNWN + 12), _gru_R0, _gru_N0); |
| 1299 | |
| 1300 | float32x4_t _descale_xc_N0 = vld1q_f32(descales_ptr + 20); |
| 1301 | |
| 1302 | _gru_N0 = vmlaq_f32(_gru_N0, vcvtq_f32_s32(_gru_Nx0), vmulq_f32(_descale_x, _descale_xc_N0)); |
| 1303 | |
| 1304 | // tanh(N) |
| 1305 | _gru_N0 = tanh_ps(_gru_N0); |
| 1306 | |
| 1307 | float* gates_data = gates.row(q / 4); |
| 1308 | |
| 1309 | vst1q_f32(gates_data, _gru_U0); |
| 1310 | vst1q_f32(gates_data + 4, _gru_N0); |
| 1311 | } |
| 1312 | #endif // __ARM_NEON |
| 1313 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1314 | for (int q = remain_num_output_start; q < num_output; q++) |
| 1315 | { |
| 1316 | const signed char* x = bottom_blob_int8.row<const signed char>(ti); |
| 1317 | const signed char* hs = hidden_state_int8; |
| 1318 | const float descale_x = bottom_blob_int8_descales[ti]; |
| 1319 | const float descale_h = hidden_state_int8_descale; |
| 1320 | |
| 1321 | // gate reset update |
| 1322 | const float* bias_c_RUBNWN = (const float*)bias_c + q * 4; |
| 1323 | |
| 1324 | #if __ARM_NEON |
| 1325 | const signed char* kptr = weight_data_tm.row<const signed char>(q / 4 + q % 4); |
| 1326 | const float* descales_ptr = weight_data_tm_int8_descales.row(q / 4 + q % 4); |
| 1327 | #else |
| 1328 | const signed char* kptr = weight_data_tm.row<const signed char>(q); |
| 1329 | const float* descales_ptr = weight_data_tm_int8_descales.row(q); |
| 1330 | #endif |
| 1331 | |
| 1332 | const float descale_xc_R = descales_ptr[0]; |
| 1333 | const float descale_xc_U = descales_ptr[1]; |
| 1334 | const float descale_hc_R = descales_ptr[2]; |
| 1335 | const float descale_hc_U = descales_ptr[3]; |
| 1336 | const float descale_hc_N = descales_ptr[4]; |
| 1337 | const float descale_xc_N = descales_ptr[5]; |
| 1338 | |
| 1339 | int Rx = 0; |
| 1340 | int Ux = 0; |
| 1341 | for (int i = 0; i < size; i++) |
| 1342 | { |
| 1343 | signed char xi = x[i]; |
| 1344 | |
| 1345 | Rx += kptr[0] * xi; |
| 1346 | Ux += kptr[1] * xi; |
| 1347 | |
| 1348 | kptr += 2; |
| 1349 | } |
| 1350 | |
| 1351 | int Rh = 0; |
| 1352 | int Uh = 0; |
| 1353 | for (int i = 0; i < num_output; i++) |
| 1354 | { |
| 1355 | signed char h_cont = hs[i]; |
| 1356 | |
| 1357 | Rh += kptr[0] * h_cont; |
| 1358 | Uh += kptr[1] * h_cont; |
| 1359 | |
| 1360 | kptr += 2; |
| 1361 | } |
| 1362 | |
| 1363 | float R = bias_c_RUBNWN[0] + Rx * (descale_x * descale_xc_R) + Rh * (descale_h * descale_hc_R); |
| 1364 | float U = bias_c_RUBNWN[1] + Ux * (descale_x * descale_xc_U) + Uh * (descale_h * descale_hc_U); |
| 1365 | |
| 1366 | // sigmoid(R) |
| 1367 | // sigmoid(U) |
| 1368 | R = 1.f / (1.f + expf(-R)); |
| 1369 | U = 1.f / (1.f + expf(-U)); |
| 1370 | |
| 1371 | // gate new |
| 1372 | |
| 1373 | int Nh = 0; |
| 1374 | for (int i = 0; i < num_output; i++) |
| 1375 | { |
| 1376 | Nh += kptr[0] * hs[i]; |
| 1377 | kptr += 1; |
| 1378 | } |
| 1379 | |
| 1380 | int Nx = 0; |
| 1381 | for (int i = 0; i < size; i++) |
| 1382 | { |
| 1383 | Nx += kptr[0] * x[i]; |
| 1384 | kptr += 1; |
| 1385 | } |
| 1386 | |
| 1387 | float N = bias_c_RUBNWN[2] + Nh * (descale_h * descale_hc_N); |
| 1388 | N = bias_c_RUBNWN[3] + R * N + Nx * (descale_x * descale_xc_N); |
| 1389 | |
| 1390 | // tanh(N) |
| 1391 | N = tanhf(N); |
| 1392 | |
| 1393 | #if __ARM_NEON |
| 1394 | float* gates_data = gates.row(q / 4 + q % 4); |
| 1395 | #else |
| 1396 | float* gates_data = gates.row(q); |
| 1397 | #endif |
| 1398 | |
| 1399 | gates_data[0] = U; |
| 1400 | gates_data[1] = N; |
| 1401 | } |
| 1402 | |
| 1403 | gru_int8_gate_output(gates, hidden_state, top_blob, ti, elemtype, opt); |
| 1404 | } |
| 1405 | } |