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.

deconvolution_riscv.cpp 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "deconvolution_riscv.h"
  15. #if __riscv_vector
  16. #include <riscv_vector.h>
  17. #endif // __riscv_vector
  18. #include "riscv_activation.h"
  19. #include "riscv_usability.h"
  20. #include "cpu.h"
  21. #include "layer_type.h"
  22. namespace ncnn {
  23. #if __riscv_vector
  24. #include "deconvolution_packn.h"
  25. #include "deconvolution_pack1ton.h"
  26. #include "deconvolution_packnto1.h"
  27. #endif // __riscv_vector
  28. Deconvolution_riscv::Deconvolution_riscv()
  29. {
  30. #if __riscv_vector
  31. support_packing = true;
  32. #endif // __riscv_vector
  33. #if NCNN_ZFH
  34. #if __riscv_vector
  35. support_fp16_storage = cpu_support_riscv_zvfh();
  36. #else
  37. support_fp16_storage = cpu_support_riscv_zfh();
  38. #endif
  39. #endif
  40. }
  41. int Deconvolution_riscv::create_pipeline(const Option& opt)
  42. {
  43. if (dynamic_weight)
  44. return 0;
  45. #if NCNN_ZFH
  46. if (support_fp16_storage && opt.use_fp16_storage)
  47. {
  48. return create_pipeline_fp16s(opt);
  49. }
  50. #endif
  51. #if __riscv_vector
  52. const int packn = csrr_vlenb() / 4;
  53. #endif
  54. const int maxk = kernel_w * kernel_h;
  55. int num_input = weight_data_size / maxk / num_output;
  56. Mat weight_data_transposed(weight_data.w);
  57. {
  58. float* pt = weight_data_transposed;
  59. const float* p = weight_data;
  60. for (int i = 0; i < num_input * num_output; i++)
  61. {
  62. for (int k = 0; k < maxk; k++)
  63. {
  64. pt[maxk - 1 - k] = p[k];
  65. }
  66. p += maxk;
  67. pt += maxk;
  68. }
  69. }
  70. int elempack = 1;
  71. int out_elempack = 1;
  72. #if __riscv_vector
  73. if (opt.use_packing_layout)
  74. {
  75. elempack = num_input % packn == 0 ? packn : 1;
  76. out_elempack = num_output % packn == 0 ? packn : 1;
  77. }
  78. #endif
  79. // src = kw-kh-inch-outch
  80. // dst = pb-pa-kw-kh-inch/pa-outch/pb
  81. {
  82. Mat weight_data_r2 = weight_data_transposed.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. #if __riscv_vector
  105. // packn
  106. if (elempack == packn && out_elempack == packn)
  107. {
  108. }
  109. // pack1ton
  110. if (elempack == 1 && out_elempack == packn)
  111. {
  112. }
  113. // packnto1
  114. if (elempack == packn && out_elempack == 1)
  115. {
  116. }
  117. #endif // __riscv_vector
  118. // pack1
  119. if (elempack == 1 && out_elempack == 1)
  120. {
  121. }
  122. if (opt.lightmode)
  123. weight_data.release();
  124. return 0;
  125. }
  126. int Deconvolution_riscv::destroy_pipeline(const Option& opt)
  127. {
  128. return 0;
  129. }
  130. int Deconvolution_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  131. {
  132. #if NCNN_ZFH
  133. int elembits = bottom_blob.elembits();
  134. if (opt.use_fp16_storage && elembits == 16)
  135. {
  136. if (opt.use_fp16_arithmetic)
  137. return forward_fp16sa(bottom_blob, top_blob, opt);
  138. else
  139. return forward_fp16s(bottom_blob, top_blob, opt);
  140. }
  141. #endif
  142. #if __riscv_vector
  143. const int packn = csrr_vlenb() / 4;
  144. #endif
  145. // deconvolv with NxN kernel
  146. // value = value + bias
  147. int w = bottom_blob.w;
  148. int h = bottom_blob.h;
  149. int channels = bottom_blob.c;
  150. size_t elemsize = bottom_blob.elemsize;
  151. int elempack = bottom_blob.elempack;
  152. // NCNN_LOGE("Deconvolution 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);
  153. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  154. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  155. int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
  156. int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
  157. int out_elempack = 1;
  158. #if __riscv_vector
  159. if (opt.use_packing_layout)
  160. {
  161. out_elempack = num_output % packn == 0 ? packn : 1;
  162. }
  163. #endif
  164. size_t out_elemsize = elemsize / elempack * out_elempack;
  165. Mat top_blob_bordered;
  166. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
  167. {
  168. top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
  169. }
  170. else
  171. {
  172. top_blob_bordered = top_blob;
  173. top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  174. }
  175. if (top_blob_bordered.empty())
  176. return -100;
  177. const int maxk = kernel_w * kernel_h;
  178. #if __riscv_vector
  179. if (elempack == packn && out_elempack == packn)
  180. {
  181. {
  182. deconvolution_packn_rvv(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
  183. }
  184. }
  185. if (elempack == 1 && out_elempack == packn)
  186. {
  187. {
  188. deconvolution_pack1ton_rvv(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
  189. }
  190. }
  191. if (elempack == packn && out_elempack == 1)
  192. {
  193. {
  194. deconvolution_packnto1_rvv(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt);
  195. }
  196. }
  197. #endif // __riscv_vector
  198. if (elempack == 1 && out_elempack == 1)
  199. {
  200. {
  201. // num_output
  202. #pragma omp parallel for num_threads(opt.num_threads)
  203. for (int p = 0; p < num_output; p++)
  204. {
  205. float* outptr = top_blob_bordered.channel(p);
  206. for (int i = 0; i < outh; i++)
  207. {
  208. for (int j = 0; j < outw; j++)
  209. {
  210. float sum = 0.f;
  211. if (bias_term)
  212. {
  213. sum = bias_data[p];
  214. }
  215. const float* kptr = (const float*)weight_data_tm.channel(p);
  216. // channels
  217. for (int q = 0; q < channels; q++)
  218. {
  219. const Mat m = bottom_blob.channel(q);
  220. for (int y = 0; y < kernel_h; y++)
  221. {
  222. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  223. if (sys < 0 || sys % stride_h != 0)
  224. continue;
  225. int sy = sys / stride_h;
  226. if (sy >= h)
  227. continue;
  228. const float* sptr = m.row(sy);
  229. for (int x = 0; x < kernel_w; x++)
  230. {
  231. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  232. if (sxs < 0 || sxs % stride_w != 0)
  233. continue;
  234. int sx = sxs / stride_w;
  235. if (sx >= w)
  236. continue;
  237. float val = sptr[sx];
  238. int k = y * kernel_w + x;
  239. float w = kptr[k];
  240. sum += val * w;
  241. }
  242. }
  243. kptr += maxk;
  244. }
  245. sum = activation_ss(sum, activation_type, activation_params);
  246. outptr[j] = sum;
  247. }
  248. outptr += outw;
  249. }
  250. }
  251. }
  252. }
  253. cut_padding(top_blob_bordered, top_blob, opt);
  254. if (top_blob.empty())
  255. return -100;
  256. return 0;
  257. }
  258. int Deconvolution_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  259. {
  260. const Mat& bottom_blob = bottom_blobs[0];
  261. const Mat& _weight_data = bottom_blobs[1];
  262. Mat& top_blob = top_blobs[0];
  263. const int _num_input = bottom_blob.c * bottom_blob.elempack;
  264. const int _kernel_w = _weight_data.w;
  265. const int _kernel_h = _weight_data.h;
  266. const int _num_output = _weight_data.d * 1;
  267. Mat weight_data_flattened;
  268. flatten(_weight_data, weight_data_flattened, opt);
  269. if (weight_data_flattened.empty())
  270. return -100;
  271. #if NCNN_RVV
  272. if (opt.use_fp16_storage && cpu_support_riscv_zvfh() && weight_data_flattened.elembits() == 16)
  273. {
  274. Mat weight_data_flattened_fp32;
  275. cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  276. weight_data_flattened = weight_data_flattened_fp32;
  277. }
  278. #endif // NCNN_RVV
  279. // weight_data_flattened as pack1
  280. weight_data_flattened.w *= weight_data_flattened.elempack;
  281. weight_data_flattened.elemsize /= weight_data_flattened.elempack;
  282. weight_data_flattened.elempack = 1;
  283. // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
  284. Mat weight_data_transposed;
  285. {
  286. weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / 1, 4u, opt.workspace_allocator);
  287. if (weight_data_transposed.empty())
  288. return -100;
  289. const int outch_g = _num_output / 1;
  290. const int inch_g = _num_input / 1;
  291. const int maxk = _kernel_h * _kernel_w;
  292. for (int g = 0; g < 1; g++)
  293. {
  294. // reorder weight from inch-outch to outch-inch
  295. float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk;
  296. const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk;
  297. for (int i = 0; i < outch_g; i++)
  298. {
  299. for (int j = 0; j < inch_g; j++)
  300. {
  301. for (int k = 0; k < maxk; k++)
  302. {
  303. wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
  304. }
  305. }
  306. }
  307. }
  308. }
  309. Mat bias_data_flattened;
  310. if (bias_term)
  311. {
  312. const Mat& _bias_data = bottom_blobs[2];
  313. flatten(_bias_data, bias_data_flattened, opt);
  314. if (bias_data_flattened.empty())
  315. return -100;
  316. #if NCNN_RVV
  317. if (opt.use_fp16_storage && cpu_support_riscv_zvfh() && bias_data_flattened.elembits() == 16)
  318. {
  319. Mat bias_data_flattened_fp32;
  320. cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  321. bias_data_flattened = bias_data_flattened_fp32;
  322. }
  323. #endif // NCNN_RVV
  324. // bias_data_flattened as pack1
  325. bias_data_flattened.w *= bias_data_flattened.elempack;
  326. bias_data_flattened.elemsize /= bias_data_flattened.elempack;
  327. bias_data_flattened.elempack = 1;
  328. }
  329. ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution);
  330. ncnn::ParamDict pd;
  331. pd.set(0, _num_output);
  332. pd.set(1, _kernel_w);
  333. pd.set(11, _kernel_h);
  334. pd.set(2, dilation_w);
  335. pd.set(12, dilation_h);
  336. pd.set(3, stride_w);
  337. pd.set(13, stride_h);
  338. pd.set(4, pad_left);
  339. pd.set(15, pad_right);
  340. pd.set(14, pad_top);
  341. pd.set(16, pad_bottom);
  342. pd.set(18, output_pad_right);
  343. pd.set(19, output_pad_bottom);
  344. pd.set(20, output_w);
  345. pd.set(21, output_h);
  346. pd.set(5, bias_term);
  347. pd.set(6, weight_data_transposed.w);
  348. pd.set(9, activation_type);
  349. pd.set(10, activation_params);
  350. op->load_param(pd);
  351. ncnn::Mat weights[2];
  352. weights[0] = weight_data_transposed;
  353. weights[1] = bias_data_flattened;
  354. op->load_model(ncnn::ModelBinFromMatArray(weights));
  355. op->create_pipeline(opt);
  356. op->forward(bottom_blob, top_blob, opt);
  357. op->destroy_pipeline(opt);
  358. delete op;
  359. return 0;
  360. }
  361. } // namespace ncnn