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.cpp 18 kB

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