You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convolution_arm_asimdhp.cpp 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. #include "convolution_arm.h"
  15. #include "cpu.h"
  16. #if __ARM_NEON
  17. #include <arm_neon.h>
  18. #endif // __ARM_NEON
  19. #include "arm_activation.h"
  20. #include "arm_usability.h"
  21. namespace ncnn {
  22. #if __ARM_NEON
  23. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  24. #include "convolution_packed_fp16s.h"
  25. #include "convolution_3x3_winograd_fp16s.h"
  26. #include "convolution_im2col_gemm_bf16s_fp16s.h"
  27. #include "convolution_im2col_gemm_fp16s.h"
  28. #if NCNN_GNU_INLINE_ASM
  29. #include "convolution_3x3_pack4_fp16s.h"
  30. #include "convolution_3x3_pack1to8_fp16s.h"
  31. #include "convolution_3x3_pack1to4_fp16s.h"
  32. #include "convolution_3x3_pack8_fp16s.h"
  33. #include "convolution_5x5_pack8_fp16s.h"
  34. #include "convolution_7x7_pack1to8_fp16s.h"
  35. #endif // NCNN_GNU_INLINE_ASM
  36. #endif
  37. #endif // __ARM_NEON
  38. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  39. 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)
  40. {
  41. const int maxk = kernel_w * kernel_h;
  42. // src = kw-kh-inch-outch
  43. // dst = pb-pa-kw-kh-inch/pa-outch/pb
  44. {
  45. Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
  46. weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)2u * elempack * out_elempack, elempack * out_elempack);
  47. for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
  48. {
  49. __fp16* g00 = weight_data_tm.channel(q / out_elempack);
  50. for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
  51. {
  52. for (int k = 0; k < maxk; k++)
  53. {
  54. for (int i = 0; i < elempack; i++)
  55. {
  56. for (int j = 0; j < out_elempack; j++)
  57. {
  58. const float* k00 = weight_data_r2.channel(q + j).row(p + i);
  59. g00[0] = (__fp16)k00[k];
  60. g00++;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. int Convolution_arm::create_pipeline_fp16s(const Option& opt)
  69. {
  70. const int maxk = kernel_w * kernel_h;
  71. const int num_input = weight_data_size / maxk / num_output;
  72. int elempack = 1;
  73. int out_elempack = 1;
  74. if (opt.use_packing_layout)
  75. {
  76. elempack = opt.use_fp16_arithmetic && num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1;
  77. out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
  78. }
  79. bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 16 || num_output >= 16);
  80. 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)
  81. {
  82. // dynamic shape
  83. if (opt.use_winograd63_convolution && (num_input <= 128 && num_output <= 128))
  84. conv3x3s1_winograd63_transform_kernel_fp16sa(weight_data, weight_winograd63_data, num_input, num_output, opt);
  85. else if (opt.use_winograd43_convolution && (num_input >= 16 && num_output >= 16))
  86. conv3x3s1_winograd43_transform_kernel_fp16sa(weight_data, weight_winograd43_data, num_input, num_output, opt);
  87. else
  88. conv3x3s1_winograd23_transform_kernel_fp16sa(weight_data, weight_winograd23_data, num_input, num_output, opt);
  89. if (opt.lightmode)
  90. {
  91. weight_data.release();
  92. }
  93. if (opt.use_fp16_arithmetic)
  94. {
  95. ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt);
  96. }
  97. return 0;
  98. }
  99. int l2_cache_size_fp16 = get_cpu_level2_cache_size() / sizeof(unsigned short);
  100. 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);
  101. #if NCNN_GNU_INLINE_ASM
  102. if (elempack == 8 && out_elempack == 8)
  103. {
  104. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  105. {
  106. prefer_sgemm = false;
  107. }
  108. 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))
  109. {
  110. prefer_sgemm = false;
  111. }
  112. if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  113. {
  114. prefer_sgemm = false;
  115. }
  116. 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))
  117. {
  118. prefer_sgemm = false;
  119. }
  120. }
  121. if (elempack == 1 && out_elempack == 8)
  122. {
  123. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  124. {
  125. prefer_sgemm = false;
  126. }
  127. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  128. {
  129. prefer_sgemm = false;
  130. }
  131. if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  132. {
  133. prefer_sgemm = false;
  134. }
  135. }
  136. if (elempack == 4 && out_elempack == 4)
  137. {
  138. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  139. {
  140. prefer_sgemm = false;
  141. }
  142. }
  143. if (elempack == 1 && out_elempack == 4)
  144. {
  145. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  146. {
  147. prefer_sgemm = false;
  148. }
  149. else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  150. {
  151. prefer_sgemm = false;
  152. }
  153. else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  154. {
  155. prefer_sgemm = false;
  156. }
  157. }
  158. #endif // NCNN_GNU_INLINE_ASM
  159. if (opt.use_fp16_arithmetic && ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1)))
  160. {
  161. convolution_im2col_gemm_transform_kernel_fp16sa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h, opt);
  162. ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt);
  163. if (opt.lightmode)
  164. {
  165. weight_data.release();
  166. }
  167. return 0;
  168. }
  169. #if NCNN_GNU_INLINE_ASM
  170. if ((elempack == 8 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  171. || (elempack == 8 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  172. || (elempack == 8 && out_elempack == 8 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  173. || (elempack == 8 && out_elempack == 8 && kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  174. || (elempack == 1 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  175. || (elempack == 1 && out_elempack == 8 && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  176. || (elempack == 1 && out_elempack == 8 && kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  177. || (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)
  178. || (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)
  179. || (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))
  180. {
  181. convolution_transform_kernel_packed_fp16s_neon(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  182. }
  183. else
  184. #endif // NCNN_GNU_INLINE_ASM
  185. {
  186. convolution_transform_kernel_packed_fp16s(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h);
  187. }
  188. if (opt.use_fp16_arithmetic)
  189. {
  190. ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt);
  191. }
  192. if (opt.lightmode)
  193. {
  194. weight_data.release();
  195. }
  196. return 0;
  197. }
  198. int Convolution_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  199. {
  200. int w = bottom_blob.w;
  201. int h = bottom_blob.h;
  202. size_t elemsize = bottom_blob.elemsize;
  203. int elempack = bottom_blob.elempack;
  204. // 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);
  205. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  206. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  207. Mat bottom_blob_bordered;
  208. make_padding(bottom_blob, bottom_blob_bordered, opt);
  209. if (bottom_blob_bordered.empty())
  210. return -100;
  211. w = bottom_blob_bordered.w;
  212. h = bottom_blob_bordered.h;
  213. int outw = (w - kernel_extent_w) / stride_w + 1;
  214. int outh = (h - kernel_extent_h) / stride_h + 1;
  215. int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1;
  216. size_t out_elemsize = elemsize / elempack * out_elempack;
  217. top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  218. if (top_blob.empty())
  219. return -100;
  220. // TODO dilated conv for bf16s
  221. // if ((!support_packing || !opt.use_packing_layout) && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1)
  222. // {
  223. // return forwardDilation_arm(bottom_blob_bordered, top_blob, opt);
  224. // }
  225. if (elempack == 4 && out_elempack == 4)
  226. {
  227. 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);
  228. }
  229. if (elempack == 1 && out_elempack == 4)
  230. {
  231. 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);
  232. }
  233. if (elempack == 4 && out_elempack == 1)
  234. {
  235. 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);
  236. }
  237. if (elempack == 1 && out_elempack == 1)
  238. {
  239. 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);
  240. }
  241. return 0;
  242. }
  243. int Convolution_arm::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  244. {
  245. int w = bottom_blob.w;
  246. int h = bottom_blob.h;
  247. int channels = bottom_blob.c;
  248. size_t elemsize = bottom_blob.elemsize;
  249. int elempack = bottom_blob.elempack;
  250. // 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);
  251. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  252. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  253. Mat bottom_blob_bordered;
  254. make_padding(bottom_blob, bottom_blob_bordered, opt);
  255. if (bottom_blob_bordered.empty())
  256. return -100;
  257. w = bottom_blob_bordered.w;
  258. h = bottom_blob_bordered.h;
  259. int outw = (w - kernel_extent_w) / stride_w + 1;
  260. int outh = (h - kernel_extent_h) / stride_h + 1;
  261. int out_elempack = 1;
  262. if (opt.use_packing_layout)
  263. {
  264. out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
  265. }
  266. size_t out_elemsize = elemsize / elempack * out_elempack;
  267. top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  268. if (top_blob.empty())
  269. return -100;
  270. // TODO dilated conv for bf16s
  271. // if ((!support_packing || !opt.use_packing_layout) && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1)
  272. // {
  273. // return forwardDilation_arm(bottom_blob_bordered, top_blob, opt);
  274. // }
  275. const int num_input = channels * elempack;
  276. bool prefer_winograd = (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && (num_input >= 16 || num_output >= 16);
  277. 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)
  278. {
  279. bool prefer_winograd63 = false;
  280. bool prefer_winograd23 = false;
  281. bool prefer_winograd43 = !prefer_winograd63 && !prefer_winograd23;
  282. if (prefer_winograd23 && (!opt.use_winograd23_convolution || weight_winograd23_data.empty()))
  283. {
  284. // f23 fallback to f43
  285. prefer_winograd23 = false;
  286. prefer_winograd43 = true;
  287. }
  288. if (prefer_winograd63 && (!opt.use_winograd63_convolution || weight_winograd63_data.empty()))
  289. {
  290. // f63 fallback to f43
  291. prefer_winograd63 = false;
  292. prefer_winograd43 = true;
  293. }
  294. if (prefer_winograd43 && (!opt.use_winograd43_convolution || weight_winograd43_data.empty()))
  295. {
  296. // f43 fallback to f63 or f23
  297. prefer_winograd43 = false;
  298. if (opt.use_winograd63_convolution && !weight_winograd63_data.empty())
  299. {
  300. prefer_winograd63 = true;
  301. }
  302. else
  303. {
  304. prefer_winograd23 = true;
  305. }
  306. }
  307. // NCNN_LOGE("prefer_winograd %d %d %d", prefer_winograd23, prefer_winograd43, prefer_winograd63);
  308. int _nT = nT ? nT : opt.num_threads;
  309. if (nT != 0 && opt.num_threads != nT)
  310. {
  311. // force num_threads the same as in create_pipeline
  312. // so we could use pre-packed A/B from the same tile config
  313. NCNN_LOGE("opt.num_threads %d changed, convolution winograd will use load-time value %d", opt.num_threads, nT);
  314. }
  315. if (prefer_winograd23)
  316. {
  317. conv3x3s1_winograd23_fp16sa(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data_fp16, _nT, opt);
  318. }
  319. else if (prefer_winograd43)
  320. {
  321. conv3x3s1_winograd43_fp16sa(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data_fp16, _nT, opt);
  322. }
  323. else if (prefer_winograd63)
  324. {
  325. conv3x3s1_winograd63_fp16sa(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data_fp16, _nT, opt);
  326. }
  327. else
  328. {
  329. // should never reach here
  330. }
  331. if (activation)
  332. {
  333. activation->forward_inplace(top_blob, opt);
  334. }
  335. return 0;
  336. }
  337. int l2_cache_size_fp16 = get_cpu_level2_cache_size() / sizeof(unsigned short);
  338. 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);
  339. #if NCNN_GNU_INLINE_ASM
  340. if (elempack == 8 && out_elempack == 8)
  341. {
  342. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  343. {
  344. prefer_sgemm = false;
  345. }
  346. 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))
  347. {
  348. prefer_sgemm = false;
  349. }
  350. if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  351. {
  352. prefer_sgemm = false;
  353. }
  354. 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))
  355. {
  356. prefer_sgemm = false;
  357. }
  358. }
  359. if (elempack == 1 && out_elempack == 8)
  360. {
  361. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  362. {
  363. prefer_sgemm = false;
  364. }
  365. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  366. {
  367. prefer_sgemm = false;
  368. }
  369. if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  370. {
  371. prefer_sgemm = false;
  372. }
  373. }
  374. if (elempack == 4 && out_elempack == 4)
  375. {
  376. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  377. {
  378. prefer_sgemm = false;
  379. }
  380. }
  381. if (elempack == 1 && out_elempack == 4)
  382. {
  383. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  384. {
  385. prefer_sgemm = false;
  386. }
  387. else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  388. {
  389. prefer_sgemm = false;
  390. }
  391. else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  392. {
  393. prefer_sgemm = false;
  394. }
  395. }
  396. #endif // NCNN_GNU_INLINE_ASM
  397. if ((opt.use_sgemm_convolution && prefer_sgemm) || (kernel_w == 1 && kernel_h == 1))
  398. {
  399. int _nT = nT ? nT : opt.num_threads;
  400. if (nT != 0 && opt.num_threads != nT)
  401. {
  402. // force num_threads the same as in create_pipeline
  403. // so we could use pre-packed A/B from the same tile config
  404. NCNN_LOGE("opt.num_threads %d changed, convolution gemm will use load-time value %d", opt.num_threads, nT);
  405. }
  406. 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);
  407. if (activation)
  408. {
  409. activation->forward_inplace(top_blob, opt);
  410. }
  411. return 0;
  412. }
  413. #if NCNN_GNU_INLINE_ASM
  414. if (elempack == 8 && out_elempack == 8)
  415. {
  416. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  417. {
  418. conv3x3s1_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  419. if (activation)
  420. {
  421. activation->forward_inplace(top_blob, opt);
  422. }
  423. }
  424. else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  425. {
  426. conv3x3s2_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  427. if (activation)
  428. {
  429. activation->forward_inplace(top_blob, opt);
  430. }
  431. }
  432. else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  433. {
  434. conv5x5s1_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  435. if (activation)
  436. {
  437. activation->forward_inplace(top_blob, opt);
  438. }
  439. }
  440. else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  441. {
  442. conv5x5s2_pack8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  443. if (activation)
  444. {
  445. activation->forward_inplace(top_blob, opt);
  446. }
  447. }
  448. else
  449. {
  450. 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);
  451. }
  452. }
  453. if (elempack == 1 && out_elempack == 8)
  454. {
  455. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  456. {
  457. conv3x3s1_pack1to8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  458. if (activation)
  459. {
  460. activation->forward_inplace(top_blob, opt);
  461. }
  462. }
  463. else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  464. {
  465. conv3x3s2_pack1to8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  466. if (activation)
  467. {
  468. activation->forward_inplace(top_blob, opt);
  469. }
  470. }
  471. else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  472. {
  473. conv7x7s2_pack1to8_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  474. if (activation)
  475. {
  476. activation->forward_inplace(top_blob, opt);
  477. }
  478. }
  479. else
  480. {
  481. 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);
  482. }
  483. }
  484. if (elempack == 4 && out_elempack == 8)
  485. {
  486. {
  487. 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);
  488. }
  489. }
  490. if (elempack == 8 && out_elempack == 1)
  491. {
  492. {
  493. 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);
  494. }
  495. }
  496. if (elempack == 8 && out_elempack == 4)
  497. {
  498. {
  499. 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);
  500. }
  501. }
  502. if (elempack == 4 && out_elempack == 4)
  503. {
  504. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  505. {
  506. conv3x3s1_pack4_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  507. if (activation)
  508. {
  509. activation->forward_inplace(top_blob, opt);
  510. }
  511. }
  512. else
  513. {
  514. 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);
  515. }
  516. }
  517. if (elempack == 1 && out_elempack == 4)
  518. {
  519. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  520. {
  521. conv3x3s1_pack1to4_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  522. if (activation)
  523. {
  524. activation->forward_inplace(top_blob, opt);
  525. }
  526. }
  527. else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  528. {
  529. conv3x3s2_pack1to4_fp16sa_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data_fp16, opt);
  530. if (activation)
  531. {
  532. activation->forward_inplace(top_blob, opt);
  533. }
  534. }
  535. else
  536. {
  537. 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);
  538. }
  539. }
  540. if (elempack == 4 && out_elempack == 1)
  541. {
  542. {
  543. 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);
  544. }
  545. }
  546. if (elempack == 1 && out_elempack == 1)
  547. {
  548. {
  549. 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);
  550. }
  551. }
  552. #else // NCNN_GNU_INLINE_ASM
  553. {
  554. 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);
  555. }
  556. #endif // NCNN_GNU_INLINE_ASM
  557. return 0;
  558. }
  559. #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  560. } // namespace ncnn