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_mips.cpp 38 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "convolution_mips.h"
  15. #include "benchmark.h"
  16. #include "cpu.h"
  17. #include "layer_type.h"
  18. #if __mips_msa
  19. #include <msa.h>
  20. #endif // __mips_msa
  21. #include "mips_activation.h"
  22. #include "mips_usability.h"
  23. #include "cpu.h"
  24. namespace ncnn {
  25. #include "convolution_sgemm.h"
  26. #include "convolution_winograd_transform.h"
  27. #include "convolution_winograd_dot.h"
  28. #include "convolution_1x1.h"
  29. #include "convolution_3x3.h"
  30. #if NCNN_INT8
  31. #include "convolution_sgemm_int8.h"
  32. #include "convolution_winograd_transform_int8.h"
  33. #include "convolution_winograd_dot_int8.h"
  34. #include "convolution_1x1_int8.h"
  35. #include "convolution_3x3_int8.h"
  36. #include "convolution_int8.h"
  37. #endif // NCNN_INT8
  38. #if __mips_msa
  39. #include "convolution_pack4.h"
  40. #include "convolution_pack1to4.h"
  41. #include "convolution_pack4to1.h"
  42. #include "convolution_sgemm_pack4.h"
  43. #include "convolution_sgemm_pack4to1.h"
  44. #include "convolution_winograd_transform_pack4.h"
  45. #include "convolution_winograd_dot_pack4.h"
  46. #include "convolution_1x1_pack4.h"
  47. #include "convolution_1x1_pack4to1.h"
  48. #include "convolution_3x3_pack4.h"
  49. #include "convolution_3x3_pack1to4.h"
  50. #include "convolution_7x7_pack1to4.h"
  51. #if NCNN_INT8
  52. #include "convolution_pack8to4_int8.h"
  53. #include "convolution_pack1to4_int8.h"
  54. #include "convolution_pack8to1_int8.h"
  55. #include "convolution_sgemm_pack8to4_int8.h"
  56. #include "convolution_sgemm_pack1to4_int8.h"
  57. #include "convolution_sgemm_pack8to1_int8.h"
  58. #include "convolution_winograd_transform_pack4_int8.h"
  59. #include "convolution_winograd_transform_pack8_int8.h"
  60. #include "convolution_winograd_dot_pack8to4_int8.h"
  61. #include "convolution_winograd_dot_pack8to1_int8.h"
  62. #include "convolution_1x1_pack8to4_int8.h"
  63. #include "convolution_1x1_pack1to4_int8.h"
  64. #include "convolution_1x1_pack8to1_int8.h"
  65. #include "convolution_3x3_pack8to4_int8.h"
  66. #include "convolution_3x3_pack8to1_int8.h"
  67. #endif // NCNN_INT8
  68. #endif // __mips_msa
  69. Convolution_mips::Convolution_mips()
  70. {
  71. #if __mips_msa
  72. support_packing = true;
  73. #endif // __mips_msa
  74. activation = 0;
  75. }
  76. static void convolution_transform_kernel_packed_msa(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)
  77. {
  78. const int maxk = kernel_w * kernel_h;
  79. // src = kw-kh-inch-outch
  80. // dst = pb-pa-kw-kh-inch/pa-outch/pb
  81. {
  82. Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
  83. weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack);
  84. for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
  85. {
  86. float* g00 = weight_data_tm.channel(q / out_elempack);
  87. for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
  88. {
  89. for (int k = 0; k < maxk; k++)
  90. {
  91. for (int i = 0; i < elempack; i++)
  92. {
  93. for (int j = 0; j < out_elempack; j++)
  94. {
  95. const float* k00 = weight_data_r2.channel(q + j).row(p + i);
  96. g00[0] = k00[k];
  97. g00++;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. int Convolution_mips::create_pipeline(const Option& opt)
  106. {
  107. if (dynamic_weight)
  108. return 0;
  109. activation = create_activation_layer(activation_type, activation_params, opt);
  110. #if NCNN_INT8
  111. if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
  112. {
  113. return create_pipeline_int8_mips(opt);
  114. }
  115. #endif
  116. const int maxk = kernel_w * kernel_h;
  117. const int num_input = weight_data_size / maxk / num_output;
  118. int elempack = 1;
  119. int out_elempack = 1;
  120. #if __mips_msa
  121. if (opt.use_packing_layout)
  122. {
  123. elempack = num_input % 4 == 0 ? 4 : 1;
  124. out_elempack = num_output % 4 == 0 ? 4 : 1;
  125. }
  126. #endif
  127. #if __mips_msa
  128. // pack4
  129. if (elempack == 4 && out_elempack == 4)
  130. {
  131. if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  132. {
  133. if ((opt.use_winograd63_convolution && num_input >= 8 && num_output >= 8 && num_input <= 64 && num_output <= 64) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
  134. conv3x3s1_winograd63_transform_kernel_pack4_msa(weight_data, weight_winograd63_data, num_input, num_output, opt);
  135. else if ((opt.use_winograd43_convolution && num_input >= 8 && num_output >= 8) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
  136. conv3x3s1_winograd43_transform_kernel_pack4_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
  137. else // if (opt.use_winograd23_convolution)
  138. conv3x3s1_winograd23_transform_kernel_pack4_msa(weight_data, weight_winograd23_data, num_input, num_output, opt);
  139. }
  140. else
  141. {
  142. convolution_transform_kernel_packed_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  143. }
  144. }
  145. // pack1ton
  146. if (elempack == 1 && out_elempack == 4)
  147. {
  148. convolution_transform_kernel_packed_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  149. }
  150. // pack4to1
  151. if (elempack == 4 && out_elempack == 1)
  152. {
  153. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  154. {
  155. convolution_im2col_sgemm_transform_kernel_pack4to1_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  156. }
  157. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  158. {
  159. convolution_im2col_sgemm_transform_kernel_pack4to1_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  160. }
  161. else if (opt.use_sgemm_convolution)
  162. {
  163. convolution_im2col_sgemm_transform_kernel_pack4to1_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  164. }
  165. else
  166. {
  167. convolution_transform_kernel_packed_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  168. }
  169. }
  170. #endif // __mips_msa
  171. // pack1
  172. if (elempack == 1 && out_elempack == 1)
  173. {
  174. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  175. {
  176. convolution_im2col_sgemm_transform_kernel_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  177. }
  178. if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  179. {
  180. if ((opt.use_winograd43_convolution && num_input >= 16 && num_output >= 16) || !opt.use_winograd23_convolution)
  181. {
  182. conv3x3s1_winograd43_transform_kernel_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
  183. }
  184. else if (opt.use_winograd23_convolution)
  185. {
  186. conv3x3s1_winograd23_transform_kernel_msa(weight_data, weight_winograd23_data, num_input, num_output, opt);
  187. }
  188. }
  189. else if (opt.use_sgemm_convolution)
  190. {
  191. convolution_im2col_sgemm_transform_kernel_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  192. }
  193. else
  194. {
  195. weight_data_tm = weight_data;
  196. }
  197. }
  198. if (opt.lightmode)
  199. weight_data.release();
  200. return 0;
  201. }
  202. int Convolution_mips::destroy_pipeline(const Option& opt)
  203. {
  204. if (activation)
  205. {
  206. activation->destroy_pipeline(opt);
  207. delete activation;
  208. activation = 0;
  209. }
  210. return 0;
  211. }
  212. int Convolution_mips::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  213. {
  214. #if NCNN_INT8
  215. if (opt.use_int8_inference && int8_scale_term)
  216. {
  217. return forward_int8_mips(bottom_blob, top_blob, opt);
  218. }
  219. #endif
  220. // flattened blob, implement as InnerProduct
  221. if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1)
  222. {
  223. Mat bottom_blob_3d;
  224. if (bottom_blob.elemsize % 16 == 0)
  225. {
  226. bottom_blob_3d = bottom_blob;
  227. bottom_blob_3d.dims = 3;
  228. bottom_blob_3d.w = 1;
  229. bottom_blob_3d.h = 1;
  230. bottom_blob_3d.c = bottom_blob.w;
  231. bottom_blob_3d.cstep = 1;
  232. }
  233. else
  234. {
  235. bottom_blob_3d = bottom_blob.reshape(1, 1, bottom_blob.w, opt.workspace_allocator);
  236. }
  237. Mat top_blob_3d;
  238. int ret = forward(bottom_blob_3d, top_blob_3d, opt);
  239. if (ret != 0)
  240. return ret;
  241. if (top_blob_3d.elemsize % 16 == 0)
  242. {
  243. top_blob = top_blob_3d;
  244. top_blob.dims = 1;
  245. top_blob.w = top_blob_3d.c;
  246. top_blob.h = 1;
  247. top_blob.c = 1;
  248. bottom_blob_3d.cstep = top_blob_3d.c;
  249. }
  250. else
  251. {
  252. top_blob = top_blob_3d.reshape(top_blob_3d.c, opt.blob_allocator);
  253. }
  254. return 0;
  255. }
  256. int w = bottom_blob.w;
  257. int h = bottom_blob.h;
  258. int channels = bottom_blob.c;
  259. size_t elemsize = bottom_blob.elemsize;
  260. int elempack = bottom_blob.elempack;
  261. // 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);
  262. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  263. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  264. Mat bottom_blob_bordered;
  265. make_padding(bottom_blob, bottom_blob_bordered, opt);
  266. if (bottom_blob_bordered.empty())
  267. return -100;
  268. w = bottom_blob_bordered.w;
  269. h = bottom_blob_bordered.h;
  270. int outw = (w - kernel_extent_w) / stride_w + 1;
  271. int outh = (h - kernel_extent_h) / stride_h + 1;
  272. int out_elempack = 1;
  273. #if __mips_msa
  274. if (opt.use_packing_layout)
  275. {
  276. out_elempack = num_output % 4 == 0 ? 4 : 1;
  277. }
  278. #endif
  279. size_t out_elemsize = elemsize / elempack * out_elempack;
  280. top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  281. if (top_blob.empty())
  282. return -100;
  283. const int num_input = channels * elempack;
  284. #if __mips_msa
  285. if (elempack == 4 && out_elempack == 4)
  286. {
  287. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  288. {
  289. conv1x1s1_sgemm_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
  290. if (activation)
  291. {
  292. activation->forward_inplace(top_blob, opt);
  293. }
  294. }
  295. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  296. {
  297. conv1x1s2_sgemm_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
  298. if (activation)
  299. {
  300. activation->forward_inplace(top_blob, opt);
  301. }
  302. }
  303. else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution || opt.use_winograd63_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  304. {
  305. if ((opt.use_winograd63_convolution && num_input >= 8 && num_output >= 8 && num_input <= 64 && num_output <= 64) || (!opt.use_winograd43_convolution && !opt.use_winograd23_convolution))
  306. conv3x3s1_winograd63_pack4_msa(bottom_blob_bordered, top_blob, weight_winograd63_data, bias_data, opt);
  307. else if ((opt.use_winograd43_convolution && num_input >= 8 && num_output >= 8) || (!opt.use_winograd63_convolution && !opt.use_winograd23_convolution))
  308. conv3x3s1_winograd43_pack4_msa(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, opt);
  309. else // if (opt.use_winograd23_convolution)
  310. conv3x3s1_winograd23_pack4_msa(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, opt);
  311. if (activation)
  312. {
  313. activation->forward_inplace(top_blob, opt);
  314. }
  315. }
  316. else if (opt.use_sgemm_convolution)
  317. {
  318. convolution_im2col_sgemm_pack4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  319. if (activation)
  320. {
  321. activation->forward_inplace(top_blob, opt);
  322. }
  323. }
  324. else
  325. {
  326. convolution_pack4_msa(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);
  327. }
  328. }
  329. if (elempack == 1 && out_elempack == 4)
  330. {
  331. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  332. {
  333. conv3x3s1_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
  334. if (activation)
  335. {
  336. activation->forward_inplace(top_blob, opt);
  337. }
  338. }
  339. else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  340. {
  341. conv3x3s2_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
  342. if (activation)
  343. {
  344. activation->forward_inplace(top_blob, opt);
  345. }
  346. }
  347. else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  348. {
  349. conv7x7s2_pack1to4_msa(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
  350. if (activation)
  351. {
  352. activation->forward_inplace(top_blob, opt);
  353. }
  354. }
  355. else
  356. {
  357. convolution_pack1to4_msa(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);
  358. }
  359. }
  360. if (elempack == 4 && out_elempack == 1)
  361. {
  362. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  363. {
  364. conv1x1s1_sgemm_pack4to1_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, opt);
  365. if (activation)
  366. {
  367. activation->forward_inplace(top_blob, opt);
  368. }
  369. }
  370. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  371. {
  372. conv1x1s2_sgemm_pack4to1_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, opt);
  373. if (activation)
  374. {
  375. activation->forward_inplace(top_blob, opt);
  376. }
  377. }
  378. else if (opt.use_sgemm_convolution)
  379. {
  380. convolution_im2col_sgemm_pack4to1_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  381. if (activation)
  382. {
  383. activation->forward_inplace(top_blob, opt);
  384. }
  385. }
  386. else
  387. {
  388. convolution_pack4to1_msa(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);
  389. }
  390. }
  391. #endif // __mips_msa
  392. if (elempack == 1 && out_elempack == 1)
  393. {
  394. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  395. {
  396. conv1x1s1_sgemm_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, opt);
  397. if (activation)
  398. {
  399. activation->forward_inplace(top_blob, opt);
  400. }
  401. }
  402. else if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  403. {
  404. if ((opt.use_winograd43_convolution && num_input >= 16 && num_output >= 16) || !opt.use_winograd23_convolution)
  405. {
  406. conv3x3s1_winograd43_msa(bottom_blob_bordered, top_blob, weight_winograd43_data, bias_data, opt);
  407. }
  408. else if (opt.use_winograd23_convolution)
  409. {
  410. conv3x3s1_winograd23_msa(bottom_blob_bordered, top_blob, weight_winograd23_data, bias_data, opt);
  411. }
  412. if (activation)
  413. {
  414. activation->forward_inplace(top_blob, opt);
  415. }
  416. }
  417. else if (opt.use_sgemm_convolution)
  418. {
  419. convolution_im2col_sgemm_msa(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  420. if (activation)
  421. {
  422. activation->forward_inplace(top_blob, opt);
  423. }
  424. }
  425. else
  426. {
  427. const int maxk = kernel_w * kernel_h;
  428. // kernel offsets
  429. std::vector<int> _space_ofs(maxk);
  430. int* space_ofs = &_space_ofs[0];
  431. {
  432. int p1 = 0;
  433. int p2 = 0;
  434. int gap = w * dilation_h - kernel_w * dilation_w;
  435. for (int i = 0; i < kernel_h; i++)
  436. {
  437. for (int j = 0; j < kernel_w; j++)
  438. {
  439. space_ofs[p1] = p2;
  440. p1++;
  441. p2 += dilation_w;
  442. }
  443. p2 += gap;
  444. }
  445. }
  446. // num_output
  447. #pragma omp parallel for num_threads(opt.num_threads)
  448. for (int p = 0; p < num_output; p++)
  449. {
  450. float* outptr = top_blob.channel(p);
  451. for (int i = 0; i < outh; i++)
  452. {
  453. for (int j = 0; j < outw; j++)
  454. {
  455. float sum = 0.f;
  456. if (bias_term)
  457. {
  458. sum = bias_data[p];
  459. }
  460. const float* kptr = (const float*)weight_data_tm + maxk * channels * p;
  461. // channels
  462. for (int q = 0; q < channels; q++)
  463. {
  464. const Mat m = bottom_blob_bordered.channel(q);
  465. const float* sptr = m.row(i * stride_h) + j * stride_w;
  466. for (int k = 0; k < maxk; k++)
  467. {
  468. float val = sptr[space_ofs[k]];
  469. float wt = kptr[k];
  470. sum += val * wt;
  471. }
  472. kptr += maxk;
  473. }
  474. sum = activation_ss(sum, activation_type, activation_params);
  475. outptr[j] = sum;
  476. }
  477. outptr += outw;
  478. }
  479. }
  480. }
  481. }
  482. return 0;
  483. }
  484. int Convolution_mips::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  485. {
  486. const Mat& bottom_blob = bottom_blobs[0];
  487. const Mat& _weight_data = bottom_blobs[1];
  488. Mat& top_blob = top_blobs[0];
  489. const int _kernel_w = _weight_data.w;
  490. const int _kernel_h = _weight_data.h;
  491. const int _num_output = _weight_data.c * _weight_data.elempack;
  492. Mat weight_data_flattened;
  493. flatten(_weight_data, weight_data_flattened, opt);
  494. if (weight_data_flattened.empty())
  495. return -100;
  496. // weight_data_flattened as pack1
  497. weight_data_flattened.w *= weight_data_flattened.elempack;
  498. weight_data_flattened.elemsize /= weight_data_flattened.elempack;
  499. weight_data_flattened.elempack = 1;
  500. Mat bias_data_flattened;
  501. if (bias_term)
  502. {
  503. const Mat& _bias_data = bottom_blobs[2];
  504. flatten(_bias_data, bias_data_flattened, opt);
  505. if (bias_data_flattened.empty())
  506. return -100;
  507. // bias_data_flattened as pack1
  508. bias_data_flattened.w *= bias_data_flattened.elempack;
  509. bias_data_flattened.elemsize /= bias_data_flattened.elempack;
  510. bias_data_flattened.elempack = 1;
  511. }
  512. ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution);
  513. ncnn::ParamDict pd;
  514. pd.set(0, _num_output);
  515. pd.set(1, _kernel_w);
  516. pd.set(11, _kernel_h);
  517. pd.set(2, dilation_w);
  518. pd.set(12, dilation_h);
  519. pd.set(3, stride_w);
  520. pd.set(13, stride_h);
  521. pd.set(4, pad_left);
  522. pd.set(15, pad_right);
  523. pd.set(14, pad_top);
  524. pd.set(16, pad_bottom);
  525. pd.set(18, pad_value);
  526. pd.set(5, bias_term);
  527. pd.set(6, weight_data_flattened.w);
  528. pd.set(8, int8_scale_term);
  529. pd.set(9, activation_type);
  530. pd.set(10, activation_params);
  531. op->load_param(pd);
  532. ncnn::Mat weights[2];
  533. weights[0] = weight_data_flattened;
  534. weights[1] = bias_data_flattened;
  535. op->load_model(ncnn::ModelBinFromMatArray(weights));
  536. op->create_pipeline(opt);
  537. op->forward(bottom_blob, top_blob, opt);
  538. op->destroy_pipeline(opt);
  539. delete op;
  540. return 0;
  541. }
  542. #if NCNN_INT8
  543. static void convolution_transform_kernel_packed_int8_msa(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)
  544. {
  545. const int maxk = kernel_w * kernel_h;
  546. // src = kw-kh-inch-outch
  547. // dst = pa-pb-kw-kh-inch/pa-outch/pb
  548. {
  549. Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
  550. weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)elempack * out_elempack, elempack * out_elempack);
  551. for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
  552. {
  553. signed char* g00 = weight_data_tm.channel(q / out_elempack);
  554. for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
  555. {
  556. for (int k = 0; k < maxk; k++)
  557. {
  558. for (int i = 0; i < out_elempack; i++)
  559. {
  560. for (int j = 0; j < elempack; j++)
  561. {
  562. const signed char* k00 = weight_data_r2.channel(q + i).row<const signed char>(p + j);
  563. g00[0] = k00[k];
  564. g00++;
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. int Convolution_mips::create_pipeline_int8_mips(const Option& opt)
  573. {
  574. const int maxk = kernel_w * kernel_h;
  575. const int num_input = weight_data_size / maxk / num_output;
  576. int elempack = 1;
  577. int out_elempack = 1;
  578. #if __mips_msa
  579. if (opt.use_packing_layout)
  580. {
  581. elempack = num_input % 8 == 0 ? 8 : 1;
  582. out_elempack = num_output % 4 == 0 ? 4 : 1;
  583. }
  584. #endif // __mips_msa
  585. #if __mips_msa
  586. if (elempack == 8 && out_elempack == 4)
  587. {
  588. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  589. {
  590. convolution_im2col_sgemm_transform_kernel_pack8to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  591. }
  592. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  593. {
  594. convolution_im2col_sgemm_transform_kernel_pack8to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  595. }
  596. else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  597. {
  598. conv3x3s1_winograd43_transform_kernel_pack8to4_int8_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
  599. }
  600. else if (opt.use_sgemm_convolution)
  601. {
  602. convolution_im2col_sgemm_transform_kernel_pack8to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  603. }
  604. else
  605. {
  606. convolution_transform_kernel_packed_int8_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  607. }
  608. }
  609. if (elempack == 1 && out_elempack == 4)
  610. {
  611. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  612. {
  613. convolution_im2col_sgemm_transform_kernel_pack1to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  614. }
  615. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  616. {
  617. convolution_im2col_sgemm_transform_kernel_pack1to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  618. }
  619. else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
  620. {
  621. convolution_im2col_sgemm_transform_kernel_pack1to4_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  622. }
  623. else
  624. {
  625. convolution_transform_kernel_packed_int8_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  626. }
  627. }
  628. if (elempack == 8 && out_elempack == 1)
  629. {
  630. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  631. {
  632. convolution_im2col_sgemm_transform_kernel_pack8to1_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  633. }
  634. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  635. {
  636. convolution_im2col_sgemm_transform_kernel_pack8to1_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  637. }
  638. else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  639. {
  640. conv3x3s1_winograd43_transform_kernel_pack8to1_int8_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
  641. }
  642. else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
  643. {
  644. convolution_im2col_sgemm_transform_kernel_pack8to1_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  645. }
  646. else
  647. {
  648. convolution_transform_kernel_packed_int8_msa(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack);
  649. }
  650. }
  651. #endif // __mips_msa
  652. if (elempack == 1 && out_elempack == 1)
  653. {
  654. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  655. {
  656. convolution_im2col_sgemm_transform_kernel_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  657. }
  658. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  659. {
  660. convolution_im2col_sgemm_transform_kernel_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  661. }
  662. else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  663. {
  664. conv3x3s1_winograd43_transform_kernel_int8_msa(weight_data, weight_winograd43_data, num_input, num_output, opt);
  665. }
  666. else if (opt.use_sgemm_convolution)
  667. {
  668. convolution_im2col_sgemm_transform_kernel_int8_msa(weight_data, weight_sgemm_data, num_input, num_output, kernel_w, kernel_h);
  669. }
  670. else
  671. {
  672. weight_data_tm = weight_data;
  673. }
  674. }
  675. scale_in_data.create(num_output);
  676. for (int p = 0; p < num_output; p++)
  677. {
  678. // requantize and relu
  679. float scale_in;
  680. if (weight_data_int8_scales[p] == 0)
  681. scale_in = 0;
  682. else
  683. scale_in = 1.f / (bottom_blob_int8_scales[0] * weight_data_int8_scales[p]);
  684. scale_in_data[p] = scale_in;
  685. }
  686. if (opt.lightmode)
  687. weight_data.release();
  688. return 0;
  689. }
  690. int Convolution_mips::forward_int8_mips(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  691. {
  692. int elembits = bottom_blob.elembits();
  693. Mat bottom_blob_int8 = bottom_blob;
  694. if (elembits != 8)
  695. {
  696. Option opt_q = opt;
  697. opt_q.blob_allocator = opt.workspace_allocator;
  698. quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_q);
  699. }
  700. Mat bottom_blob_bordered;
  701. make_padding(bottom_blob_int8, bottom_blob_bordered, opt);
  702. if (bottom_blob_bordered.empty())
  703. return -100;
  704. int w = bottom_blob_bordered.w;
  705. int h = bottom_blob_bordered.h;
  706. int channels = bottom_blob_bordered.c;
  707. int elempack = bottom_blob_bordered.elempack;
  708. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  709. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  710. int outw = (w - kernel_extent_w) / stride_w + 1;
  711. int outh = (h - kernel_extent_h) / stride_h + 1;
  712. bool use_int8_requantize = int8_scale_term > 100;
  713. int out_elempack = 1;
  714. #if __mips_msa
  715. if (opt.use_packing_layout)
  716. {
  717. if (use_int8_requantize)
  718. out_elempack = num_output % 8 == 0 ? 8 : 1;
  719. else
  720. out_elempack = num_output % 4 == 0 ? 4 : 1;
  721. }
  722. #endif // __mips_msa
  723. size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack;
  724. top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  725. if (top_blob.empty())
  726. return -100;
  727. const int num_input = channels * elempack;
  728. int out_elempack_int32 = 1;
  729. #if __mips_msa
  730. if (opt.use_packing_layout)
  731. {
  732. out_elempack_int32 = num_output % 4 == 0 ? 4 : 1;
  733. }
  734. #endif // __mips_msa
  735. Mat top_blob_int32;
  736. top_blob_int32.create(outw, outh, num_output / out_elempack_int32, (size_t)(4u * out_elempack_int32), out_elempack_int32, opt.workspace_allocator);
  737. if (top_blob_int32.empty())
  738. return -100;
  739. #if __mips_msa
  740. if (elempack == 8 && out_elempack_int32 == 4)
  741. {
  742. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  743. {
  744. conv1x1s1_sgemm_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  745. }
  746. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  747. {
  748. conv1x1s2_sgemm_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  749. }
  750. else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  751. {
  752. conv3x3s1_winograd43_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, opt);
  753. }
  754. else if (opt.use_sgemm_convolution)
  755. {
  756. convolution_im2col_sgemm_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  757. }
  758. else
  759. {
  760. convolution_pack8to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  761. }
  762. }
  763. if (elempack == 1 && out_elempack_int32 == 4)
  764. {
  765. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  766. {
  767. conv1x1s1_sgemm_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  768. }
  769. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  770. {
  771. conv1x1s2_sgemm_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  772. }
  773. else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
  774. {
  775. convolution_im2col_sgemm_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  776. }
  777. else
  778. {
  779. convolution_pack1to4_int8_msa(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  780. }
  781. }
  782. if (elempack == 8 && out_elempack_int32 == 1)
  783. {
  784. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  785. {
  786. conv1x1s1_sgemm_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  787. }
  788. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  789. {
  790. conv1x1s2_sgemm_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  791. }
  792. else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  793. {
  794. conv3x3s1_winograd43_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, opt);
  795. }
  796. else if (opt.use_sgemm_convolution) // TODO better condition && num_input >= 8 && num_output >= 8)
  797. {
  798. convolution_im2col_sgemm_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  799. }
  800. else
  801. {
  802. convolution_pack8to1_int8_msa(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  803. }
  804. }
  805. #endif // __mips_msa
  806. if (elempack == 1 && out_elempack_int32 == 1)
  807. {
  808. if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  809. {
  810. conv1x1s1_sgemm_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  811. }
  812. else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
  813. {
  814. conv1x1s2_sgemm_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, opt);
  815. }
  816. else if (opt.use_winograd_convolution && opt.use_winograd43_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
  817. {
  818. conv3x3s1_winograd43_int8_msa(bottom_blob_bordered, top_blob_int32, weight_winograd43_data, opt);
  819. }
  820. else if (opt.use_sgemm_convolution)
  821. {
  822. convolution_im2col_sgemm_int8_msa(bottom_blob_bordered, top_blob_int32, weight_sgemm_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  823. }
  824. else
  825. {
  826. convolution_int8(bottom_blob_bordered, top_blob_int32, weight_data_tm, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, opt);
  827. }
  828. }
  829. if (use_int8_requantize)
  830. {
  831. requantize_from_int32_to_int8(top_blob_int32, top_blob, scale_in_data, top_blob_int8_scales, bias_data, activation_type, activation_params, opt);
  832. }
  833. else
  834. {
  835. dequantize_from_int32(top_blob_int32, top_blob, scale_in_data, bias_data, opt);
  836. if (activation)
  837. {
  838. activation->forward_inplace(top_blob, opt);
  839. }
  840. }
  841. return 0;
  842. }
  843. #endif // NCNN_INT8
  844. } // namespace ncnn