| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "convolution_arm.h" |
| 16 | |
| 17 | #include "cpu.h" |
| 18 | |
| 19 | #if __ARM_NEON |
| 20 | #include <arm_neon.h> |
| 21 | #endif // __ARM_NEON |
| 22 | |
| 23 | #include "arm_activation.h" |
| 24 | #include "arm_usability.h" |
| 25 | |
| 26 | namespace ncnn { |
| 27 | |
| 28 | #if __ARM_NEON |
| 29 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 30 | #include "convolution_packed_fp16s.h" |
| 31 | |
| 32 | #include "convolution_3x3_winograd_fp16s.h" |
| 33 | |
| 34 | #include "convolution_im2col_gemm_bf16s_fp16s.h" |
| 35 | #include "convolution_im2col_gemm_fp16s.h" |
| 36 | |
| 37 | #if NCNN_GNU_INLINE_ASM |
| 38 | #include "convolution_3x3_pack4_fp16s.h" |
| 39 | #include "convolution_3x3_pack1to8_fp16s.h" |
| 40 | #include "convolution_3x3_pack1to4_fp16s.h" |
| 41 | #include "convolution_3x3_pack8_fp16s.h" |
| 42 | #include "convolution_5x5_pack8_fp16s.h" |
| 43 | #include "convolution_7x7_pack1to8_fp16s.h" |
| 44 | #endif // NCNN_GNU_INLINE_ASM |
| 45 | #endif |
| 46 | #endif // __ARM_NEON |
| 47 | |
| 48 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 49 | static void convolution_transform_kernel_packed_fp16s_neon(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack) |
| 50 | { |
| 51 | const int maxk = kernel_w * kernel_h; |
| 52 | |
| 53 | // src = kw-kh-inch-outch |
| 54 | // dst = pb-pa-kw-kh-inch/pa-outch/pb |
| 55 | { |
| 56 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 57 | |
| 58 | weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)2u * elempack * out_elempack, elempack * out_elempack); |
| 59 | |
| 60 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 61 | { |
| 62 | __fp16* g00 = weight_data_tm.channel(q / out_elempack); |
| 63 | |
| 64 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 65 | { |
| 66 | for (int k = 0; k < maxk; k++) |
| 67 | { |
| 68 | for (int i = 0; i < elempack; i++) |
| 69 | { |
| 70 | for (int j = 0; j < out_elempack; j++) |
| 71 | { |
| 72 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 73 | |
| 74 | g00[0] = (__fp16)k00[k]; |
| 75 | |
| 76 | g00++; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | int Convolution_arm::create_pipeline_fp16s(const Option& opt) |
| 86 | { |
| 87 | const int maxk = kernel_w * kernel_h; |
| 88 | const int num_input = weight_data_size / maxk / num_output; |
| 89 | |
| 90 | int elempack = 1; |
| 91 | int out_elempack = 1; |
| 92 | |
| 93 | if (opt.use_packing_layout) |
| 94 | { |
| 95 | elempack = opt.use_fp16_arithmetic && num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 96 | out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 97 | } |
| 98 | |
| 99 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 16 || num_output >= 16); |
| 100 | |
| 101 | if (opt.use_fp16_arithmetic && opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 102 | { |
| 103 | // dynamic shape |
| 104 | if (opt.use_winograd63_convolution && (num_input <= 128 && num_output <= 128)) |
| 105 | conv3x3s1_winograd63_transform_kernel_fp16sa(weight_data, weight_winograd63_data, num_input, num_output, opt); |
| 106 | else if (opt.use_winograd43_convolution && (num_input >= 16 && num_output >= 16)) |
| 107 | conv3x3s1_winograd43_transform_kernel_fp16sa(weight_data, weight_winograd43_data, num_input, num_output, opt); |
| 108 | else |
| 109 | conv3x3s1_winograd23_transform_kernel_fp16sa(weight_data, weight_winograd23_data, num_input, num_output, opt); |
| 110 | |
| 111 | if (opt.lightmode) |
| 112 | weight_data.release(); |
| 113 | |
| 114 | if (opt.use_fp16_arithmetic) |
| 115 | { |
| 116 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | int l2_cache_size_fp16 = get_cpu_level2_cache_size() / sizeof(unsigned short); |
| 123 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * 2 > l2_cache_size_fp16 || (num_input > 16 || num_output > 16); |
| 124 | |
| 125 | #if NCNN_GNU_INLINE_ASM |
| 126 | if (elempack == 8 && out_elempack == 8) |
| 127 | { |
| 128 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 129 | { |
| 130 | prefer_sgemm = false; |
| 131 | } |
| 132 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 64 || num_output < 128)) |
| 133 | { |
| 134 | prefer_sgemm = false; |
| 135 | } |
| 136 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 137 | { |
| 138 | prefer_sgemm = false; |
| 139 | } |
| 140 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 16 || num_output < 88)) |
| 141 | { |
| 142 | prefer_sgemm = false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (elempack == 1 && out_elempack == 8) |
| 147 | { |
| 148 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 149 | { |
| 150 | prefer_sgemm = false; |
| 151 | } |
| 152 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 153 | { |
| 154 | prefer_sgemm = false; |
| 155 | } |
| 156 | if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 157 | { |
| 158 | prefer_sgemm = false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (elempack == 4 && out_elempack == 4) |
| 163 | { |
| 164 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 165 | { |
| 166 | prefer_sgemm = false; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (elempack == 1 && out_elempack == 4) |
| 171 | { |
| 172 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 173 | { |
| 174 | prefer_sgemm = false; |
| 175 | } |
| 176 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 177 | { |
| 178 | prefer_sgemm = false; |
| 179 | } |
| 180 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 181 | { |
| 182 | prefer_sgemm = false; |
| 183 | } |
| 184 | } |
| 185 | #endif // NCNN_GNU_INLINE_ASM |
| 186 | |
| 187 | if (opt.use_fp16_arithmetic && ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1))) |
| 188 | { |
| 189 | convolution_im2col_gemm_transform_kernel_fp16sa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt); |
| 190 | |
| 191 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 192 | |
| 193 | if (opt.lightmode) |
| 194 | weight_data.release(); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | #if NCNN_GNU_INLINE_ASM |
| 200 | if ((elempack == 8 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 201 | || (elempack == 8 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 202 | || (elempack == 8 && out_elempack == 8 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 203 | || (elempack == 8 && out_elempack == 8 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 204 | || (elempack == 1 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 205 | || (elempack == 1 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 206 | || (elempack == 1 && out_elempack == 8 && kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 207 | || (opt.use_fp16_arithmetic && elempack == 4 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 208 | || (opt.use_fp16_arithmetic && elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 209 | || (opt.use_fp16_arithmetic && elempack == 1 && out_elempack == 4 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)) |
| 210 | { |
| 211 | convolution_transform_kernel_packed_fp16s_neon(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack); |
| 212 | } |
| 213 | else |
| 214 | #endif // NCNN_GNU_INLINE_ASM |
| 215 | { |
| 216 | convolution_transform_kernel_packed_fp16s(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h); |
| 217 | } |
| 218 | |
| 219 | if (opt.use_fp16_arithmetic) |
| 220 | { |
| 221 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 222 | } |
| 223 | |
| 224 | if (opt.lightmode) |
| 225 | weight_data.release(); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int Convolution_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 231 | { |
| 232 | int w = bottom_blob.w; |
| 233 | int h = bottom_blob.h; |
| 234 | size_t elemsize = bottom_blob.elemsize; |
| 235 | int elempack = bottom_blob.elempack; |
| 236 | |
| 237 | // NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h); |
| 238 | |
| 239 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 240 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 241 | |
| 242 | Mat bottom_blob_bordered; |
| 243 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 244 | if (bottom_blob_bordered.empty()) |
| 245 | return -100; |
| 246 | |
| 247 | w = bottom_blob_bordered.w; |
| 248 | h = bottom_blob_bordered.h; |
| 249 | |
| 250 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 251 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 252 | int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1; |
| 253 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 254 | |
| 255 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 256 | if (top_blob.empty()) |
| 257 | return -100; |
| 258 | |
| 259 | // TODO dilated conv for bf16s |
| 260 | // if ((!support_packing || !opt.use_packing_layout) && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 261 | // { |
| 262 | // return forwardDilation_arm(bottom_blob_bordered, top_blob, opt); |
| 263 | // } |
| 264 | |
| 265 | if (elempack == 4 && out_elempack == 4) |
| 266 | { |
| 267 | convolution_packed_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 268 | } |
| 269 | |
| 270 | if (elempack == 1 && out_elempack == 4) |
| 271 | { |
| 272 | convolution_packed_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 273 | } |
| 274 | |
| 275 | if (elempack == 4 && out_elempack == 1) |
| 276 | { |
| 277 | convolution_packed_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 278 | } |
| 279 | |
| 280 | if (elempack == 1 && out_elempack == 1) |
| 281 | { |
| 282 | convolution_packed_fp16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | int Convolution_arm::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 289 | { |
| 290 | int w = bottom_blob.w; |
| 291 | int h = bottom_blob.h; |
| 292 | int channels = bottom_blob.c; |
| 293 | size_t elemsize = bottom_blob.elemsize; |
| 294 | int elempack = bottom_blob.elempack; |
| 295 | |
| 296 | // NCNN_LOGE("Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h); |
| 297 | |
| 298 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 299 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 300 | |
| 301 | Mat bottom_blob_bordered; |
| 302 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 303 | if (bottom_blob_bordered.empty()) |
| 304 | return -100; |
| 305 | |
| 306 | w = bottom_blob_bordered.w; |
| 307 | h = bottom_blob_bordered.h; |
| 308 | |
| 309 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 310 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 311 | int out_elempack = 1; |
| 312 | if (opt.use_packing_layout) |
| 313 | { |
| 314 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 315 | } |
| 316 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 317 | |
| 318 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 319 | if (top_blob.empty()) |
| 320 | return -100; |
| 321 | |
| 322 | // TODO dilated conv for bf16s |
| 323 | // if ((!support_packing || !opt.use_packing_layout) && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) |
| 324 | // { |
| 325 | // return forwardDilation_arm(bottom_blob_bordered, top_blob, opt); |
| 326 | // } |
| 327 | |
| 328 | const int num_input = channels * elempack; |
| 329 | |
| 330 | bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 16 || num_output >= 16); |
| 331 | |
| 332 | if (opt.use_winograd_convolution && prefer_winograd && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 333 | { |
| 334 | bool prefer_winograd63 = false; |
| 335 | bool prefer_winograd23 = false; |
| 336 | bool prefer_winograd43 = !prefer_winograd63 && !prefer_winograd23; |
| 337 | |
| 338 | if (prefer_winograd23 && (!opt.use_winograd23_convolution || weight_winograd23_data.empty())) |
| 339 | { |
| 340 | // f23 fallback to f43 |
| 341 | prefer_winograd23 = false; |
| 342 | prefer_winograd43 = true; |
| 343 | } |
| 344 | |
| 345 | if (prefer_winograd63 && (!opt.use_winograd63_convolution || weight_winograd63_data.empty())) |
| 346 | { |
| 347 | // f63 fallback to f43 |
| 348 | prefer_winograd63 = false; |
| 349 | prefer_winograd43 = true; |
| 350 | } |
| 351 | |
| 352 | if (prefer_winograd43 && (!opt.use_winograd43_convolution || weight_winograd43_data.empty())) |
| 353 | { |
| 354 | // f43 fallback to f63 or f23 |
| 355 | prefer_winograd43 = false; |
| 356 | if (opt.use_winograd63_convolution && !weight_winograd63_data.empty()) |
| 357 | { |
| 358 | prefer_winograd63 = true; |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | prefer_winograd23 = true; |
| 363 | } |
| 364 | } |
| 365 | // NCNN_LOGE("prefer_winograd %d %d %d", prefer_winograd23, prefer_winograd43, prefer_winograd63); |
| 366 | |
| 367 | int _nT = nT ? nT : opt.num_threads; |
| 368 | if (nT != 0 && opt.num_threads != nT) |
| 369 | { |
| 370 | // force num_threads the same as in create_pipeline |
| 371 | // so we could use pre-packed A/B from the same tile config |
| 372 | NCNN_LOGE("opt.num_threads %d changed, convolution winograd will use load-time value %d", opt.num_threads, nT); |
| 373 | } |
| 374 | |
| 375 | int ret = 0; |
| 376 | if (prefer_winograd23) |
| 377 | { |
| 378 | ret = conv3x3s1_winograd23_fp16sa(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data_fp16, _nT, opt); |
| 379 | } |
| 380 | else if (prefer_winograd43) |
| 381 | { |
| 382 | ret = conv3x3s1_winograd43_fp16sa(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data_fp16, _nT, opt); |
| 383 | } |
| 384 | else if (prefer_winograd63) |
| 385 | { |
| 386 | ret = conv3x3s1_winograd63_fp16sa(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data_fp16, _nT, opt); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | // should never reach here |
| 391 | } |
| 392 | if (ret != 0) |
| 393 | return ret; |
| 394 | |
| 395 | if (activation) |
| 396 | { |
| 397 | activation->forward_inplace(top_blob, opt); |
| 398 | } |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | int l2_cache_size_fp16 = get_cpu_level2_cache_size() / sizeof(unsigned short); |
| 403 | bool prefer_sgemm = num_input * num_output * kernel_w * kernel_h * dilation_w * dilation_h * stride_w * stride_h * 2 > l2_cache_size_fp16 || (num_input > 16 || num_output > 16); |
| 404 | |
| 405 | #if NCNN_GNU_INLINE_ASM |
| 406 | if (elempack == 8 && out_elempack == 8) |
| 407 | { |
| 408 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 409 | { |
| 410 | prefer_sgemm = false; |
| 411 | } |
| 412 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 64 || num_output < 128)) |
| 413 | { |
| 414 | prefer_sgemm = false; |
| 415 | } |
| 416 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 417 | { |
| 418 | prefer_sgemm = false; |
| 419 | } |
| 420 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (num_input < 16 || num_output < 88)) |
| 421 | { |
| 422 | prefer_sgemm = false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if (elempack == 1 && out_elempack == 8) |
| 427 | { |
| 428 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 429 | { |
| 430 | prefer_sgemm = false; |
| 431 | } |
| 432 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 433 | { |
| 434 | prefer_sgemm = false; |
| 435 | } |
| 436 | if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 437 | { |
| 438 | prefer_sgemm = false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (elempack == 4 && out_elempack == 4) |
| 443 | { |
| 444 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 445 | { |
| 446 | prefer_sgemm = false; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (elempack == 1 && out_elempack == 4) |
| 451 | { |
| 452 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 453 | { |
| 454 | prefer_sgemm = false; |
| 455 | } |
| 456 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 457 | { |
| 458 | prefer_sgemm = false; |
| 459 | } |
| 460 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 461 | { |
| 462 | prefer_sgemm = false; |
| 463 | } |
| 464 | } |
| 465 | #endif // NCNN_GNU_INLINE_ASM |
| 466 | |
| 467 | if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)) |
| 468 | { |
| 469 | int _nT = nT ? nT : opt.num_threads; |
| 470 | if (nT != 0 && opt.num_threads != nT) |
| 471 | { |
| 472 | // force num_threads the same as in create_pipeline |
| 473 | // so we could use pre-packed A/B from the same tile config |
| 474 | NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT); |
| 475 | } |
| 476 | |
| 477 | int ret = convolution_im2col_gemm_fp16sa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, _nT, opt); |
| 478 | if (ret != 0) |
| 479 | return ret; |
| 480 | |
| 481 | if (activation) |
| 482 | { |
| 483 | activation->forward_inplace(top_blob, opt); |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | #if NCNN_GNU_INLINE_ASM |
| 489 | if (elempack == 8 && out_elempack == 8) |
| 490 | { |
| 491 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 492 | { |
| 493 | conv3x3s1_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 494 | |
| 495 | if (activation) |
| 496 | { |
| 497 | activation->forward_inplace(top_blob, opt); |
| 498 | } |
| 499 | } |
| 500 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 501 | { |
| 502 | conv3x3s2_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 503 | |
| 504 | if (activation) |
| 505 | { |
| 506 | activation->forward_inplace(top_blob, opt); |
| 507 | } |
| 508 | } |
| 509 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 510 | { |
| 511 | conv5x5s1_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 512 | |
| 513 | if (activation) |
| 514 | { |
| 515 | activation->forward_inplace(top_blob, opt); |
| 516 | } |
| 517 | } |
| 518 | else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 519 | { |
| 520 | conv5x5s2_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 521 | |
| 522 | if (activation) |
| 523 | { |
| 524 | activation->forward_inplace(top_blob, opt); |
| 525 | } |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if (elempack == 1 && out_elempack == 8) |
| 534 | { |
| 535 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 536 | { |
| 537 | conv3x3s1_pack1to8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 538 | |
| 539 | if (activation) |
| 540 | { |
| 541 | activation->forward_inplace(top_blob, opt); |
| 542 | } |
| 543 | } |
| 544 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 545 | { |
| 546 | conv3x3s2_pack1to8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 547 | |
| 548 | if (activation) |
| 549 | { |
| 550 | activation->forward_inplace(top_blob, opt); |
| 551 | } |
| 552 | } |
| 553 | else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 554 | { |
| 555 | conv7x7s2_pack1to8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 556 | |
| 557 | if (activation) |
| 558 | { |
| 559 | activation->forward_inplace(top_blob, opt); |
| 560 | } |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (elempack == 4 && out_elempack == 8) |
| 569 | { |
| 570 | { |
| 571 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (elempack == 8 && out_elempack == 1) |
| 576 | { |
| 577 | { |
| 578 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | if (elempack == 8 && out_elempack == 4) |
| 583 | { |
| 584 | { |
| 585 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (elempack == 4 && out_elempack == 4) |
| 590 | { |
| 591 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 592 | { |
| 593 | conv3x3s1_pack4_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 594 | |
| 595 | if (activation) |
| 596 | { |
| 597 | activation->forward_inplace(top_blob, opt); |
| 598 | } |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (elempack == 1 && out_elempack == 4) |
| 607 | { |
| 608 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 609 | { |
| 610 | conv3x3s1_pack1to4_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 611 | |
| 612 | if (activation) |
| 613 | { |
| 614 | activation->forward_inplace(top_blob, opt); |
| 615 | } |
| 616 | } |
| 617 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 618 | { |
| 619 | conv3x3s2_pack1to4_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt); |
| 620 | |
| 621 | if (activation) |
| 622 | { |
| 623 | activation->forward_inplace(top_blob, opt); |
| 624 | } |
| 625 | } |
| 626 | else |
| 627 | { |
| 628 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (elempack == 4 && out_elempack == 1) |
| 633 | { |
| 634 | { |
| 635 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (elempack == 1 && out_elempack == 1) |
| 640 | { |
| 641 | { |
| 642 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 643 | } |
| 644 | } |
| 645 | #else // NCNN_GNU_INLINE_ASM |
| 646 | { |
| 647 | convolution_packed_fp16sa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 648 | } |
| 649 | #endif // NCNN_GNU_INLINE_ASM |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 654 | |
| 655 | } // namespace ncnn |