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.

deconvolutiondepthwise_vulkan.cpp 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 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 "deconvolutiondepthwise_vulkan.h"
  15. #include <algorithm>
  16. #include "layer_type.h"
  17. namespace ncnn {
  18. DEFINE_LAYER_CREATOR(DeconvolutionDepthWise_vulkan)
  19. DeconvolutionDepthWise_vulkan::DeconvolutionDepthWise_vulkan()
  20. {
  21. support_vulkan = true;
  22. crop = 0;
  23. packing_pack1 = 0;
  24. packing_pack4 = 0;
  25. pipeline_deconvolutiondepthwise = 0;
  26. pipeline_deconvolutiondepthwise_pack4 = 0;
  27. pipeline_deconvolutiondepthwise_group = 0;
  28. pipeline_deconvolutiondepthwise_group_pack4 = 0;
  29. pipeline_deconvolutiondepthwise_group_pack1to4 = 0;
  30. pipeline_deconvolutiondepthwise_group_pack4to1 = 0;
  31. }
  32. int DeconvolutionDepthWise_vulkan::create_pipeline(const Option& opt)
  33. {
  34. {
  35. crop = ncnn::create_layer(ncnn::LayerType::Crop);
  36. crop->vkdev = vkdev;
  37. ncnn::ParamDict pd;
  38. pd.set(0, pad_w);
  39. pd.set(1, pad_h);
  40. pd.set(2, 0);
  41. crop->load_param(pd);
  42. crop->create_pipeline(opt);
  43. }
  44. std::vector<vk_specialization_type> specializations(11);
  45. specializations[0].i = kernel_w;
  46. specializations[1].i = kernel_h;
  47. specializations[2].i = dilation_w;
  48. specializations[3].i = dilation_h;
  49. specializations[4].i = stride_w;
  50. specializations[5].i = stride_h;
  51. specializations[6].i = bias_term;
  52. specializations[7].i = group;
  53. specializations[8].i = activation_type;
  54. specializations[9].f = activation_params.w == 1 ? activation_params[0] : 0.f;
  55. specializations[10].f = activation_params.w == 2 ? activation_params[1] : 0.f;
  56. const int maxk = kernel_w * kernel_h;
  57. int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
  58. // depth-wise
  59. if (channels == group && group == num_output)
  60. {
  61. // pack1
  62. if (num_output % 4 != 0)
  63. {
  64. pipeline_deconvolutiondepthwise = new Pipeline(vkdev);
  65. pipeline_deconvolutiondepthwise->set_optimal_local_size_xyz(32, 32, num_output);
  66. pipeline_deconvolutiondepthwise->create("deconvolutiondepthwise", opt, specializations, 4, 10);
  67. }
  68. // pack4
  69. if (num_output % 4 == 0)
  70. {
  71. pipeline_deconvolutiondepthwise_pack4 = new Pipeline(vkdev);
  72. pipeline_deconvolutiondepthwise_pack4->set_optimal_local_size_xyz(32, 32, std::max(1, num_output / 4));
  73. pipeline_deconvolutiondepthwise_pack4->create("deconvolutiondepthwise_pack4", opt, specializations, 4, 10);
  74. }
  75. return 0;
  76. }
  77. // group deconvolution
  78. const int channels_g = channels / group;
  79. const int num_output_g = num_output / group;
  80. // pack1
  81. if (channels_g % 4 != 0 && num_output_g % 4 != 0)
  82. {
  83. pipeline_deconvolutiondepthwise_group = new Pipeline(vkdev);
  84. pipeline_deconvolutiondepthwise_group->set_optimal_local_size_xyz(32, 32, std::max(1, num_output / 8));
  85. pipeline_deconvolutiondepthwise_group->create("deconvolutiondepthwise_group", opt, specializations, 4, 10);
  86. }
  87. // pack4
  88. if (channels_g % 4 == 0 && num_output_g % 4 == 0)
  89. {
  90. pipeline_deconvolutiondepthwise_group_pack4 = new Pipeline(vkdev);
  91. pipeline_deconvolutiondepthwise_group_pack4->set_optimal_local_size_xyz(32, 32, std::max(1, num_output / 8));
  92. pipeline_deconvolutiondepthwise_group_pack4->create("deconvolutiondepthwise_group_pack4", opt, specializations, 4, 10);
  93. }
  94. // pack1to4
  95. if (channels_g % 4 != 0 && num_output_g % 4 == 0)
  96. {
  97. pipeline_deconvolutiondepthwise_group_pack1to4 = new Pipeline(vkdev);
  98. pipeline_deconvolutiondepthwise_group_pack1to4->set_optimal_local_size_xyz(32, 32, std::max(1, num_output / 8));
  99. pipeline_deconvolutiondepthwise_group_pack1to4->create("deconvolutiondepthwise_group_pack1to4", opt, specializations, 4, 10);
  100. }
  101. // pack4to1
  102. if (channels_g % 4 == 0 && num_output_g % 4 != 0)
  103. {
  104. pipeline_deconvolutiondepthwise_group_pack4to1 = new Pipeline(vkdev);
  105. pipeline_deconvolutiondepthwise_group_pack4to1->set_optimal_local_size_xyz(32, 32, std::max(1, num_output / 8));
  106. pipeline_deconvolutiondepthwise_group_pack4to1->create("deconvolutiondepthwise_group_pack4to1", opt, specializations, 4, 10);
  107. }
  108. if (channels % 4 == 0 && channels_g % 4 != 0)
  109. {
  110. packing_pack1 = ncnn::create_layer(ncnn::LayerType::Packing);
  111. packing_pack1->vkdev = vkdev;
  112. ncnn::ParamDict pd;
  113. pd.set(0, 1);
  114. packing_pack1->load_param(pd);
  115. packing_pack1->create_pipeline(opt);
  116. }
  117. if (num_output_g % 4 != 0 && num_output % 4 == 0)
  118. {
  119. packing_pack4 = ncnn::create_layer(ncnn::LayerType::Packing);
  120. packing_pack4->vkdev = vkdev;
  121. ncnn::ParamDict pd;
  122. pd.set(0, 4);
  123. packing_pack4->load_param(pd);
  124. packing_pack4->create_pipeline(opt);
  125. }
  126. return 0;
  127. }
  128. int DeconvolutionDepthWise_vulkan::destroy_pipeline(const Option& opt)
  129. {
  130. if (crop)
  131. {
  132. crop->destroy_pipeline(opt);
  133. delete crop;
  134. crop = 0;
  135. }
  136. if (packing_pack1)
  137. {
  138. packing_pack1->destroy_pipeline(opt);
  139. delete packing_pack1;
  140. packing_pack1 = 0;
  141. }
  142. if (packing_pack4)
  143. {
  144. packing_pack4->destroy_pipeline(opt);
  145. delete packing_pack4;
  146. packing_pack4 = 0;
  147. }
  148. delete pipeline_deconvolutiondepthwise;
  149. pipeline_deconvolutiondepthwise = 0;
  150. delete pipeline_deconvolutiondepthwise_pack4;
  151. pipeline_deconvolutiondepthwise_pack4 = 0;
  152. delete pipeline_deconvolutiondepthwise_group;
  153. pipeline_deconvolutiondepthwise_group = 0;
  154. delete pipeline_deconvolutiondepthwise_group_pack4;
  155. pipeline_deconvolutiondepthwise_group_pack4 = 0;
  156. delete pipeline_deconvolutiondepthwise_group_pack1to4;
  157. pipeline_deconvolutiondepthwise_group_pack1to4 = 0;
  158. delete pipeline_deconvolutiondepthwise_group_pack4to1;
  159. pipeline_deconvolutiondepthwise_group_pack4to1 = 0;
  160. return 0;
  161. }
  162. int DeconvolutionDepthWise_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
  163. {
  164. const int maxk = kernel_w * kernel_h;
  165. int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
  166. Mat weight_data_transposed(weight_data.w);
  167. {
  168. float* pt = weight_data_transposed;
  169. const float* p = weight_data;
  170. for (int i=0; i<(channels/group)*(num_output/group)*group; i++)
  171. {
  172. for (int k=0; k<maxk; k++)
  173. {
  174. pt[maxk-1 - k] = p[k];
  175. }
  176. p += maxk;
  177. pt += maxk;
  178. }
  179. }
  180. // depth-wise
  181. if (channels == group && group == num_output)
  182. {
  183. // pack1
  184. if (num_output % 4 != 0)
  185. {
  186. cmd.record_upload(weight_data_transposed, weight_data_gpu, opt);
  187. }
  188. // pack4
  189. if (num_output % 4 == 0)
  190. {
  191. const int maxk = kernel_w * kernel_h;
  192. Mat weight_data_pack4;
  193. Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group);
  194. convert_packing(weight_data_r2, weight_data_pack4, 4);
  195. weight_data_pack4 = weight_data_pack4.reshape(maxk * (group/4));
  196. cmd.record_upload(weight_data_pack4, weight_data_gpu_pack4, opt);
  197. }
  198. if (bias_term)
  199. {
  200. if (num_output % 4 != 0)
  201. {
  202. cmd.record_upload(bias_data, bias_data_gpu, opt);
  203. }
  204. if (num_output % 4 == 0)
  205. {
  206. Mat bias_data_pack4;
  207. convert_packing(bias_data, bias_data_pack4, 4);
  208. cmd.record_upload(bias_data_pack4, bias_data_gpu_pack4, opt);
  209. }
  210. }
  211. return 0;
  212. }
  213. // group deconvolution
  214. const int channels_g = channels / group;
  215. const int num_output_g = num_output / group;
  216. // pack1
  217. if (channels_g % 4 != 0 && num_output_g % 4 != 0)
  218. {
  219. cmd.record_upload(weight_data_transposed, weight_data_gpu, opt);
  220. }
  221. // pack4
  222. if (channels_g % 4 == 0 && num_output_g % 4 == 0)
  223. {
  224. // src = kw-kh-inch-outch
  225. // dst = 4a-4b-kw-kh-inch/4a-outch/4b
  226. Mat weight_data_pack4_groups;
  227. {
  228. Mat weight_data_r2_groups = weight_data_transposed.reshape(maxk, channels_g, num_output_g * group);
  229. weight_data_pack4_groups.create(maxk, channels_g/4, num_output_g/4 * group, (size_t)4*16, 16);
  230. for (int g=0; g<group; g++)
  231. {
  232. const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
  233. Mat weight_data_pack4 = weight_data_pack4_groups.channel_range(num_output_g/4 * g, num_output_g/4);
  234. for (int q=0; q+3<num_output_g; q+=4)
  235. {
  236. const Mat k0 = weight_data_r2.channel(q);
  237. const Mat k1 = weight_data_r2.channel(q+1);
  238. const Mat k2 = weight_data_r2.channel(q+2);
  239. const Mat k3 = weight_data_r2.channel(q+3);
  240. Mat g0 = weight_data_pack4.channel(q/4);
  241. for (int p=0; p+3<channels_g; p+=4)
  242. {
  243. const float* k00 = k0.row(p);
  244. const float* k01 = k0.row(p+1);
  245. const float* k02 = k0.row(p+2);
  246. const float* k03 = k0.row(p+3);
  247. const float* k10 = k1.row(p);
  248. const float* k11 = k1.row(p+1);
  249. const float* k12 = k1.row(p+2);
  250. const float* k13 = k1.row(p+3);
  251. const float* k20 = k2.row(p);
  252. const float* k21 = k2.row(p+1);
  253. const float* k22 = k2.row(p+2);
  254. const float* k23 = k2.row(p+3);
  255. const float* k30 = k3.row(p);
  256. const float* k31 = k3.row(p+1);
  257. const float* k32 = k3.row(p+2);
  258. const float* k33 = k3.row(p+3);
  259. float* g00 = g0.row(p/4);
  260. for (int k=0; k<maxk; k++)
  261. {
  262. g00[0] = k00[k];
  263. g00[1] = k01[k];
  264. g00[2] = k02[k];
  265. g00[3] = k03[k];
  266. g00[4] = k10[k];
  267. g00[5] = k11[k];
  268. g00[6] = k12[k];
  269. g00[7] = k13[k];
  270. g00[8] = k20[k];
  271. g00[9] = k21[k];
  272. g00[10] = k22[k];
  273. g00[11] = k23[k];
  274. g00[12] = k30[k];
  275. g00[13] = k31[k];
  276. g00[14] = k32[k];
  277. g00[15] = k33[k];
  278. g00 += 16;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. cmd.record_upload(weight_data_pack4_groups, weight_data_gpu_pack4, opt);
  285. }
  286. // pack1to4
  287. if (channels_g % 4 != 0 && num_output_g % 4 == 0)
  288. {
  289. // src = kw-kh-inch-outch
  290. // dst = 4b-kw-kh-inch-outch/4b
  291. Mat weight_data_pack1to4_groups;
  292. {
  293. Mat weight_data_r2_groups = weight_data_transposed.reshape(maxk, channels_g, num_output_g * group);
  294. weight_data_pack1to4_groups.create(maxk, channels_g, num_output_g/4 * group, (size_t)4*4, 4);
  295. for (int g=0; g<group; g++)
  296. {
  297. const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
  298. Mat weight_data_pack1to4 = weight_data_pack1to4_groups.channel_range(num_output_g/4 * g, num_output_g/4);
  299. for (int q=0; q+3<num_output_g; q+=4)
  300. {
  301. const Mat k0 = weight_data_r2.channel(q);
  302. const Mat k1 = weight_data_r2.channel(q+1);
  303. const Mat k2 = weight_data_r2.channel(q+2);
  304. const Mat k3 = weight_data_r2.channel(q+3);
  305. Mat g0 = weight_data_pack1to4.channel(q/4);
  306. for (int p=0; p<channels_g; p++)
  307. {
  308. const float* k00 = k0.row(p);
  309. const float* k10 = k1.row(p);
  310. const float* k20 = k2.row(p);
  311. const float* k30 = k3.row(p);
  312. float* g00 = g0.row(p);
  313. for (int k=0; k<maxk; k++)
  314. {
  315. g00[0] = k00[k];
  316. g00[1] = k10[k];
  317. g00[2] = k20[k];
  318. g00[3] = k30[k];
  319. g00 += 4;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. cmd.record_upload(weight_data_pack1to4_groups, weight_data_gpu_pack1to4, opt);
  326. }
  327. // pack4to1
  328. if (channels_g % 4 == 0 && num_output_g % 4 != 0)
  329. {
  330. // src = kw-kh-inch-outch
  331. // dst = 4a-kw-kh-inch/4a-outch
  332. Mat weight_data_pack4to1_groups;
  333. {
  334. Mat weight_data_r2_groups = weight_data_transposed.reshape(maxk, channels_g, num_output_g * group);
  335. weight_data_pack4to1_groups.create(maxk, channels_g/4, num_output_g * group, (size_t)4*4, 4);
  336. for (int g=0; g<group; g++)
  337. {
  338. const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
  339. Mat weight_data_pack4to1 = weight_data_pack4to1_groups.channel_range(num_output_g * g, num_output_g);
  340. for (int q=0; q<num_output_g; q++)
  341. {
  342. const Mat k0 = weight_data_r2.channel(q);
  343. Mat g0 = weight_data_pack4to1.channel(q);
  344. for (int p=0; p+3<channels_g; p+=4)
  345. {
  346. const float* k00 = k0.row(p);
  347. const float* k01 = k0.row(p+1);
  348. const float* k02 = k0.row(p+2);
  349. const float* k03 = k0.row(p+3);
  350. float* g00 = g0.row(p/4);
  351. for (int k=0; k<maxk; k++)
  352. {
  353. g00[0] = k00[k];
  354. g00[1] = k01[k];
  355. g00[2] = k02[k];
  356. g00[3] = k03[k];
  357. g00 += 4;
  358. }
  359. }
  360. }
  361. }
  362. }
  363. cmd.record_upload(weight_data_pack4to1_groups, weight_data_gpu_pack4to1, opt);
  364. }
  365. if (bias_term)
  366. {
  367. if (num_output_g % 4 != 0)
  368. {
  369. cmd.record_upload(bias_data, bias_data_gpu, opt);
  370. }
  371. if (num_output_g % 4 == 0)
  372. {
  373. Mat bias_data_pack4;
  374. convert_packing(bias_data, bias_data_pack4, 4);
  375. cmd.record_upload(bias_data_pack4, bias_data_gpu_pack4, opt);
  376. }
  377. }
  378. return 0;
  379. }
  380. int DeconvolutionDepthWise_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
  381. {
  382. int w = bottom_blob.w;
  383. int h = bottom_blob.h;
  384. int channels = bottom_blob.c;
  385. size_t elemsize = bottom_blob.elemsize;
  386. int packing = bottom_blob.packing;
  387. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  388. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  389. int outw = (w - 1) * stride_w + kernel_extent_w;
  390. int outh = (h - 1) * stride_h + kernel_extent_h;
  391. int out_packing = num_output % 4 == 0 ? 4 : 1;
  392. size_t out_elemsize = elemsize / packing * out_packing;
  393. VkMat top_blob_bordered;
  394. if (pad_w > 0 || pad_h > 0)
  395. {
  396. top_blob_bordered.create(outw, outh, num_output / out_packing, out_elemsize, out_packing, opt.workspace_vkallocator, opt.staging_vkallocator);
  397. if (top_blob_bordered.empty())
  398. return -100;
  399. }
  400. else
  401. {
  402. top_blob_bordered.create(outw, outh, num_output / out_packing, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
  403. if (top_blob_bordered.empty())
  404. return -100;
  405. }
  406. // depth-wise
  407. if (channels == group / packing && group / packing == num_output / packing)
  408. {
  409. std::vector<VkMat> bindings(4);
  410. bindings[0] = bottom_blob;
  411. bindings[1] = top_blob_bordered;
  412. bindings[2] = packing == 4 ? weight_data_gpu_pack4 : weight_data_gpu;
  413. bindings[3] = bias_term ? (packing == 4 ? bias_data_gpu_pack4 : bias_data_gpu) : bindings[2];// TODO use dummy buffer
  414. std::vector<vk_constant_type> constants(10);
  415. constants[0].i = bottom_blob.dims;
  416. constants[1].i = bottom_blob.w;
  417. constants[2].i = bottom_blob.h;
  418. constants[3].i = bottom_blob.c;
  419. constants[4].i = bottom_blob.cstep;
  420. constants[5].i = top_blob_bordered.dims;
  421. constants[6].i = top_blob_bordered.w;
  422. constants[7].i = top_blob_bordered.h;
  423. constants[8].i = top_blob_bordered.c;
  424. constants[9].i = top_blob_bordered.cstep;
  425. const Pipeline* pipeline = packing == 4 ? pipeline_deconvolutiondepthwise_pack4 : pipeline_deconvolutiondepthwise;
  426. // record
  427. cmd.record_pipeline(pipeline, bindings, constants, top_blob_bordered);
  428. if (pad_w > 0 || pad_h > 0)
  429. {
  430. VkMat reference_blob;
  431. reference_blob.dims = 2;
  432. reference_blob.w = top_blob_bordered.w - pad_w - pad_w;
  433. reference_blob.h = top_blob_bordered.h - pad_h - pad_h;
  434. std::vector<VkMat> crop_bottom_blobs(2);
  435. crop_bottom_blobs[0] = top_blob_bordered;
  436. crop_bottom_blobs[1] = reference_blob;
  437. std::vector<VkMat> crop_top_blobs(1);
  438. crop->forward(crop_bottom_blobs, crop_top_blobs, cmd, opt);
  439. top_blob = crop_top_blobs[0];
  440. outw = top_blob.w;
  441. outh = top_blob.h;
  442. }
  443. else
  444. {
  445. top_blob = top_blob_bordered;
  446. }
  447. return 0;
  448. }
  449. const int channels_g = channels * packing / group;
  450. const int num_output_g = num_output / group;
  451. // unpacking
  452. VkMat bottom_blob_unpacked = bottom_blob;
  453. if (packing == 4 && channels_g % 4 != 0)
  454. {
  455. ncnn::Option opt_pack1 = opt;
  456. opt_pack1.blob_vkallocator = opt.workspace_vkallocator;
  457. packing_pack1->forward(bottom_blob, bottom_blob_unpacked, cmd, opt_pack1);
  458. }
  459. VkMat top_blob_unpacked = top_blob_bordered;
  460. if (num_output_g % 4 != 0 && out_packing == 4)
  461. {
  462. top_blob_unpacked.create(outw, outh, num_output, elemsize / packing, 1, opt.workspace_vkallocator, opt.staging_vkallocator);
  463. if (top_blob_unpacked.empty())
  464. return -100;
  465. }
  466. std::vector<VkMat> bindings(4);
  467. bindings[0] = bottom_blob_unpacked;
  468. bindings[1] = top_blob_unpacked;
  469. if (channels_g % 4 != 0 && num_output_g % 4 != 0)
  470. {
  471. bindings[2] = weight_data_gpu;
  472. bindings[3] = bias_term ? bias_data_gpu : bindings[2];// TODO use dummy buffer
  473. }
  474. else if (channels_g % 4 == 0 && num_output_g % 4 == 0)
  475. {
  476. bindings[2] = weight_data_gpu_pack4;
  477. bindings[3] = bias_term ? bias_data_gpu_pack4 : bindings[2];// TODO use dummy buffer
  478. }
  479. else if (channels_g % 4 != 0 && num_output_g % 4 == 0)
  480. {
  481. bindings[2] = weight_data_gpu_pack1to4;
  482. bindings[3] = bias_term ? bias_data_gpu_pack4 : bindings[2];// TODO use dummy buffer
  483. }
  484. else if (channels_g % 4 == 0 && num_output_g % 4 != 0)
  485. {
  486. bindings[2] = weight_data_gpu_pack4to1;
  487. bindings[3] = bias_term ? bias_data_gpu : bindings[2];// TODO use dummy buffer
  488. }
  489. std::vector<vk_constant_type> constants(10);
  490. constants[0].i = bottom_blob_unpacked.dims;
  491. constants[1].i = bottom_blob_unpacked.w;
  492. constants[2].i = bottom_blob_unpacked.h;
  493. constants[3].i = bottom_blob_unpacked.c;
  494. constants[4].i = bottom_blob_unpacked.cstep;
  495. constants[5].i = top_blob_unpacked.dims;
  496. constants[6].i = top_blob_unpacked.w;
  497. constants[7].i = top_blob_unpacked.h;
  498. constants[8].i = top_blob_unpacked.c;
  499. constants[9].i = top_blob_unpacked.cstep;
  500. const Pipeline* pipeline = 0;
  501. if (channels_g % 4 != 0 && num_output_g % 4 != 0)
  502. {
  503. pipeline = pipeline_deconvolutiondepthwise_group;
  504. }
  505. else if (channels_g % 4 == 0 && num_output_g % 4 == 0)
  506. {
  507. pipeline = pipeline_deconvolutiondepthwise_group_pack4;
  508. }
  509. else if (channels_g % 4 != 0 && num_output_g % 4 == 0)
  510. {
  511. pipeline = pipeline_deconvolutiondepthwise_group_pack1to4;
  512. }
  513. else if (channels_g % 4 == 0 && num_output_g % 4 != 0)
  514. {
  515. pipeline = pipeline_deconvolutiondepthwise_group_pack4to1;
  516. }
  517. cmd.record_pipeline(pipeline, bindings, constants, top_blob_unpacked);
  518. // packing
  519. if (num_output_g % 4 != 0 && out_packing == 4)
  520. {
  521. packing_pack4->forward(top_blob_unpacked, top_blob_bordered, cmd, opt);
  522. }
  523. else
  524. {
  525. top_blob_bordered = top_blob_unpacked;
  526. }
  527. if (pad_w > 0 || pad_h > 0)
  528. {
  529. VkMat reference_blob;
  530. reference_blob.dims = 2;
  531. reference_blob.w = top_blob_bordered.w - pad_w - pad_w;
  532. reference_blob.h = top_blob_bordered.h - pad_h - pad_h;
  533. std::vector<VkMat> crop_bottom_blobs(2);
  534. crop_bottom_blobs[0] = top_blob_bordered;
  535. crop_bottom_blobs[1] = reference_blob;
  536. std::vector<VkMat> crop_top_blobs(1);
  537. crop->forward(crop_bottom_blobs, crop_top_blobs, cmd, opt);
  538. top_blob = crop_top_blobs[0];
  539. outw = top_blob.w;
  540. outh = top_blob.h;
  541. }
  542. else
  543. {
  544. top_blob = top_blob_bordered;
  545. }
  546. return 0;
  547. }
  548. } // namespace ncnn