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_arm.cpp 46 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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_arm.h"
  15. #include "layer_type.h"
  16. #if __ARM_NEON
  17. #include <arm_neon.h>
  18. #endif // __ARM_NEON
  19. #include "arm_activation.h"
  20. #include "arm_usability.h"
  21. #include "cpu.h"
  22. namespace ncnn {
  23. #include "deconvolution_3x3.h"
  24. #include "deconvolution_4x4.h"
  25. Deconvolution_arm::Deconvolution_arm()
  26. {
  27. #if __ARM_NEON
  28. support_packing = true;
  29. #if NCNN_ARM82
  30. support_fp16_storage = cpu_support_arm_asimdhp();
  31. #endif
  32. #endif // __ARM_NEON
  33. #if NCNN_BF16
  34. support_bf16_storage = true;
  35. #endif
  36. activation = 0;
  37. gemm = 0;
  38. }
  39. int Deconvolution_arm::create_pipeline(const Option& opt)
  40. {
  41. if (dynamic_weight)
  42. return 0;
  43. activation = create_activation_layer(activation_type, activation_params, opt);
  44. #if NCNN_ARM82
  45. if (support_fp16_storage && opt.use_fp16_storage)
  46. {
  47. return create_pipeline_fp16s(opt);
  48. }
  49. #endif
  50. #if NCNN_BF16
  51. if (opt.use_bf16_storage)
  52. {
  53. return create_pipeline_bf16s(opt);
  54. }
  55. #endif
  56. const int maxk = kernel_w * kernel_h;
  57. int num_input = weight_data_size / maxk / num_output;
  58. int elempack = 1;
  59. int out_elempack = 1;
  60. #if __ARM_NEON
  61. if (opt.use_packing_layout)
  62. {
  63. elempack = num_input % 4 == 0 ? 4 : 1;
  64. out_elempack = num_output % 4 == 0 ? 4 : 1;
  65. }
  66. #endif
  67. if (opt.use_sgemm_convolution)
  68. {
  69. const int maxk = kernel_w * kernel_h;
  70. gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
  71. ncnn::ParamDict pd;
  72. pd.set(2, 1); // transA
  73. pd.set(3, 0); // transB
  74. pd.set(4, 1); // constantA
  75. pd.set(5, 0); // constantB
  76. pd.set(6, 1); // constantC
  77. pd.set(7, maxk * num_output); // M = maxk*num_output
  78. pd.set(8, 0); // N = size
  79. pd.set(9, num_input); // K = inch
  80. pd.set(10, -1); // constant_broadcast_type_C = null
  81. pd.set(11, 0); // output_N1M
  82. pd.set(12, out_elempack);
  83. gemm->load_param(pd);
  84. // maxk-inch-outch to pa-maxk-outch/pa-inch
  85. Mat tmp;
  86. {
  87. Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output);
  88. tmp.create(maxk * num_output, num_input);
  89. for (int p = 0; p < num_input; p += 1)
  90. {
  91. float* g00 = tmp.row(p);
  92. for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
  93. {
  94. for (int k = 0; k < maxk; k++)
  95. {
  96. for (int i = 0; i < out_elempack; i++)
  97. {
  98. const float* k00 = weight_data_r2.channel(q + i).row(p);
  99. g00[0] = k00[k];
  100. g00++;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. ncnn::Mat weights[1];
  107. weights[0] = tmp;
  108. gemm->load_model(ModelBinFromMatArray(weights));
  109. Option opt1 = opt;
  110. opt1.use_fp16_storage = false;
  111. gemm->create_pipeline(opt1);
  112. }
  113. else
  114. {
  115. Mat weight_data_transposed(weight_data.w);
  116. {
  117. float* pt = weight_data_transposed;
  118. const float* p = weight_data;
  119. for (int i = 0; i < num_input * num_output; i++)
  120. {
  121. for (int k = 0; k < maxk; k++)
  122. {
  123. pt[maxk - 1 - k] = p[k];
  124. }
  125. p += maxk;
  126. pt += maxk;
  127. }
  128. }
  129. // src = kw-kh-inch-outch
  130. // dst = pb-pa-kw-kh-inch/pa-outch/pb
  131. Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output);
  132. weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack);
  133. for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
  134. {
  135. float* g00 = weight_data_tm.channel(q / out_elempack);
  136. for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
  137. {
  138. for (int k = 0; k < maxk; k++)
  139. {
  140. for (int i = 0; i < elempack; i++)
  141. {
  142. for (int j = 0; j < out_elempack; j++)
  143. {
  144. const float* k00 = weight_data_r2.channel(q + j).row(p + i);
  145. g00[0] = k00[k];
  146. g00++;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. // pack1
  153. if (elempack == 1 && out_elempack == 1)
  154. {
  155. if (kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1)
  156. {
  157. weight_data_tm = weight_data;
  158. }
  159. else if (kernel_w == 3 && kernel_h == 3 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1)
  160. {
  161. weight_data_tm = weight_data;
  162. }
  163. else if (kernel_w == 4 && kernel_h == 4 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1)
  164. {
  165. weight_data_tm = weight_data;
  166. }
  167. else if (kernel_w == 4 && kernel_h == 4 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1)
  168. {
  169. weight_data_tm = weight_data;
  170. }
  171. else
  172. {
  173. weight_data_tm = weight_data_transposed;
  174. }
  175. }
  176. }
  177. if (opt.lightmode)
  178. weight_data.release();
  179. return 0;
  180. }
  181. int Deconvolution_arm::destroy_pipeline(const Option& opt)
  182. {
  183. if (activation)
  184. {
  185. activation->destroy_pipeline(opt);
  186. delete activation;
  187. activation = 0;
  188. }
  189. if (gemm)
  190. {
  191. gemm->destroy_pipeline(opt);
  192. delete gemm;
  193. gemm = 0;
  194. }
  195. return 0;
  196. }
  197. int Deconvolution_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  198. {
  199. int elembits = bottom_blob.elembits();
  200. #if NCNN_ARM82
  201. if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
  202. {
  203. if (opt.use_fp16_arithmetic)
  204. return forward_fp16sa(bottom_blob, top_blob, opt);
  205. else
  206. return forward_fp16s(bottom_blob, top_blob, opt);
  207. }
  208. #endif
  209. #if NCNN_BF16
  210. if (opt.use_bf16_storage && elembits == 16)
  211. return forward_bf16s(bottom_blob, top_blob, opt);
  212. #endif
  213. int w = bottom_blob.w;
  214. int h = bottom_blob.h;
  215. int channels = bottom_blob.c;
  216. size_t elemsize = bottom_blob.elemsize;
  217. int elempack = bottom_blob.elempack;
  218. // 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);
  219. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  220. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  221. int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
  222. int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
  223. int out_elempack = 1;
  224. #if __ARM_NEON
  225. if (opt.use_packing_layout)
  226. {
  227. out_elempack = num_output % 4 == 0 ? 4 : 1;
  228. }
  229. #endif
  230. size_t out_elemsize = elemsize / elempack * out_elempack;
  231. int out_channels = num_output / out_elempack;
  232. Mat top_blob_bordered;
  233. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
  234. {
  235. top_blob_bordered.create(outw, outh, out_channels, out_elemsize, out_elempack, opt.workspace_allocator);
  236. }
  237. else
  238. {
  239. top_blob_bordered = top_blob;
  240. top_blob_bordered.create(outw, outh, out_channels, out_elemsize, out_elempack, opt.blob_allocator);
  241. }
  242. if (top_blob_bordered.empty())
  243. return -100;
  244. const int maxk = kernel_w * kernel_h;
  245. if (opt.use_sgemm_convolution)
  246. {
  247. // sgemm
  248. Mat bottom_blob_2 = bottom_blob;
  249. {
  250. bottom_blob_2.w = bottom_blob.w * bottom_blob.h;
  251. bottom_blob_2.h = 1;
  252. }
  253. Mat top_col2im;
  254. Option opt_b = opt;
  255. opt_b.blob_allocator = top_blob_bordered.allocator;
  256. gemm->forward(bottom_blob_2, top_col2im, opt_b);
  257. {
  258. // col2im
  259. const int gap = (outw * stride_h - w * stride_w) * out_elempack;
  260. #if __ARM_NEON
  261. if (out_elempack == 4)
  262. {
  263. #pragma omp parallel for num_threads(opt.num_threads)
  264. for (int p = 0; p < out_channels; p++)
  265. {
  266. const float* sptr = top_col2im.row(p * maxk);
  267. Mat outm = top_blob_bordered.channel(p);
  268. if (bias_data.empty())
  269. {
  270. outm.fill(vdupq_n_f32(0.f));
  271. }
  272. else
  273. {
  274. outm.fill(vld1q_f32((const float*)bias_data + p * 4));
  275. }
  276. for (int u = 0; u < kernel_h; u++)
  277. {
  278. for (int v = 0; v < kernel_w; v++)
  279. {
  280. float* ptr = outm.row(dilation_h * u) + dilation_w * v * 4;
  281. for (int i = 0; i < h; i++)
  282. {
  283. for (int j = 0; j < w; j++)
  284. {
  285. float32x4_t _val = vld1q_f32(ptr);
  286. float32x4_t _s = vld1q_f32(sptr);
  287. _val = vaddq_f32(_val, _s);
  288. vst1q_f32(ptr, _val);
  289. ptr += stride_w * 4;
  290. sptr += 4;
  291. }
  292. ptr += gap;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. #endif // __ARM_NEON
  299. if (out_elempack == 1)
  300. {
  301. #pragma omp parallel for num_threads(opt.num_threads)
  302. for (int p = 0; p < out_channels; p++)
  303. {
  304. const float* sptr = top_col2im.row(p * maxk);
  305. Mat outm = top_blob_bordered.channel(p);
  306. const float bias = bias_data.empty() ? 0.f : bias_data[p];
  307. outm.fill(bias);
  308. for (int u = 0; u < kernel_h; u++)
  309. {
  310. for (int v = 0; v < kernel_w; v++)
  311. {
  312. float* ptr = outm.row(dilation_h * u) + dilation_w * v;
  313. for (int i = 0; i < h; i++)
  314. {
  315. for (int j = 0; j < w; j++)
  316. {
  317. ptr[0] += sptr[0];
  318. ptr += stride_w;
  319. sptr += 1;
  320. }
  321. ptr += gap;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. if (activation)
  329. {
  330. activation->forward_inplace(top_blob_bordered, opt);
  331. }
  332. }
  333. else
  334. {
  335. #if __ARM_NEON
  336. if (elempack == 4 && out_elempack == 4)
  337. {
  338. // num_output
  339. #pragma omp parallel for num_threads(opt.num_threads)
  340. for (int p = 0; p < out_channels; p++)
  341. {
  342. float* outptr = top_blob_bordered.channel(p);
  343. for (int i = 0; i < outh; i++)
  344. {
  345. for (int j = 0; j < outw; j++)
  346. {
  347. float32x4_t _sum = vdupq_n_f32(0.f);
  348. if (bias_term)
  349. {
  350. _sum = vld1q_f32(((const float*)bias_data) + p * 4);
  351. }
  352. const float* kptr = weight_data_tm.channel(p);
  353. // channels
  354. for (int q = 0; q < channels; q++)
  355. {
  356. const Mat m = bottom_blob.channel(q);
  357. for (int y = 0; y < kernel_h; y++)
  358. {
  359. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  360. if (sys < 0 || sys % stride_h != 0)
  361. continue;
  362. int sy = sys / stride_h;
  363. if (sy >= h)
  364. continue;
  365. for (int x = 0; x < kernel_w; x++)
  366. {
  367. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  368. if (sxs < 0 || sxs % stride_w != 0)
  369. continue;
  370. int sx = sxs / stride_w;
  371. if (sx >= w)
  372. continue;
  373. const float* sptr = m.row(sy) + sx * 4;
  374. float32x4_t _val = vld1q_f32(sptr);
  375. int k = y * kernel_w + x;
  376. float32x4_t _w0 = vld1q_f32(kptr + k * 16);
  377. float32x4_t _w1 = vld1q_f32(kptr + k * 16 + 4);
  378. float32x4_t _w2 = vld1q_f32(kptr + k * 16 + 8);
  379. float32x4_t _w3 = vld1q_f32(kptr + k * 16 + 12);
  380. #if __aarch64__
  381. _sum = vmlaq_laneq_f32(_sum, _w0, _val, 0);
  382. _sum = vmlaq_laneq_f32(_sum, _w1, _val, 1);
  383. _sum = vmlaq_laneq_f32(_sum, _w2, _val, 2);
  384. _sum = vmlaq_laneq_f32(_sum, _w3, _val, 3);
  385. #else
  386. _sum = vmlaq_lane_f32(_sum, _w0, vget_low_f32(_val), 0);
  387. _sum = vmlaq_lane_f32(_sum, _w1, vget_low_f32(_val), 1);
  388. _sum = vmlaq_lane_f32(_sum, _w2, vget_high_f32(_val), 0);
  389. _sum = vmlaq_lane_f32(_sum, _w3, vget_high_f32(_val), 1);
  390. #endif
  391. }
  392. }
  393. kptr += maxk * 16;
  394. }
  395. _sum = activation_ps(_sum, activation_type, activation_params);
  396. vst1q_f32(outptr + j * 4, _sum);
  397. }
  398. outptr += outw * 4;
  399. }
  400. }
  401. }
  402. if (elempack == 1 && out_elempack == 4)
  403. {
  404. // num_output
  405. #pragma omp parallel for num_threads(opt.num_threads)
  406. for (int p = 0; p < out_channels; p++)
  407. {
  408. float* outptr = top_blob_bordered.channel(p);
  409. for (int i = 0; i < outh; i++)
  410. {
  411. for (int j = 0; j < outw; j++)
  412. {
  413. float32x4_t _sum = vdupq_n_f32(0.f);
  414. if (bias_term)
  415. {
  416. _sum = vld1q_f32(((const float*)bias_data) + p * 4);
  417. }
  418. const float* kptr = weight_data_tm.channel(p);
  419. // channels
  420. for (int q = 0; q < channels; q++)
  421. {
  422. const Mat m = bottom_blob.channel(q);
  423. for (int y = 0; y < kernel_h; y++)
  424. {
  425. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  426. if (sys < 0 || sys % stride_h != 0)
  427. continue;
  428. int sy = sys / stride_h;
  429. if (sy >= h)
  430. continue;
  431. const float* sptr = m.row(sy);
  432. for (int x = 0; x < kernel_w; x++)
  433. {
  434. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  435. if (sxs < 0 || sxs % stride_w != 0)
  436. continue;
  437. int sx = sxs / stride_w;
  438. if (sx >= w)
  439. continue;
  440. float32x4_t _val = vdupq_n_f32(sptr[sx]);
  441. int k = y * kernel_w + x;
  442. float32x4_t _w = vld1q_f32(kptr + k * 4);
  443. _sum = vmlaq_f32(_sum, _val, _w);
  444. }
  445. }
  446. kptr += maxk * 4;
  447. }
  448. _sum = activation_ps(_sum, activation_type, activation_params);
  449. vst1q_f32(outptr + j * 4, _sum);
  450. }
  451. outptr += outw * 4;
  452. }
  453. }
  454. }
  455. if (elempack == 4 && out_elempack == 1)
  456. {
  457. // num_output
  458. #pragma omp parallel for num_threads(opt.num_threads)
  459. for (int p = 0; p < num_output; p++)
  460. {
  461. float* outptr = top_blob_bordered.channel(p);
  462. for (int i = 0; i < outh; i++)
  463. {
  464. for (int j = 0; j < outw; j++)
  465. {
  466. float sum = 0.f;
  467. if (bias_term)
  468. {
  469. sum = bias_data[p];
  470. }
  471. const float* kptr = weight_data_tm.channel(p);
  472. // channels
  473. for (int q = 0; q < channels; q++)
  474. {
  475. const Mat m = bottom_blob.channel(q);
  476. for (int y = 0; y < kernel_h; y++)
  477. {
  478. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  479. if (sys < 0 || sys % stride_h != 0)
  480. continue;
  481. int sy = sys / stride_h;
  482. if (sy >= h)
  483. continue;
  484. for (int x = 0; x < kernel_w; x++)
  485. {
  486. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  487. if (sxs < 0 || sxs % stride_w != 0)
  488. continue;
  489. int sx = sxs / stride_w;
  490. if (sx >= w)
  491. continue;
  492. const float* sptr = m.row(sy) + sx * 4;
  493. float32x4_t _val = vld1q_f32(sptr);
  494. int k = y * kernel_w + x;
  495. float32x4_t _w = vld1q_f32(kptr + k * 4);
  496. float32x4_t _s4 = vmulq_f32(_val, _w);
  497. #if __aarch64__
  498. sum += vaddvq_f32(_s4); // dot
  499. #else
  500. float32x2_t _ss = vadd_f32(vget_low_f32(_s4), vget_high_f32(_s4));
  501. _ss = vpadd_f32(_ss, _ss);
  502. sum += vget_lane_f32(_ss, 0);
  503. #endif
  504. }
  505. }
  506. kptr += maxk * 4;
  507. }
  508. sum = activation_ss(sum, activation_type, activation_params);
  509. outptr[j] = sum;
  510. }
  511. outptr += outw;
  512. }
  513. }
  514. }
  515. #endif // __ARM_NEON
  516. if (elempack == 1 && out_elempack == 1)
  517. {
  518. if (kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1)
  519. {
  520. deconv3x3s1_neon(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, opt);
  521. if (activation)
  522. {
  523. activation->forward_inplace(top_blob_bordered, opt);
  524. }
  525. }
  526. else if (kernel_w == 3 && kernel_h == 3 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1)
  527. {
  528. deconv3x3s2_neon(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, opt);
  529. if (activation)
  530. {
  531. activation->forward_inplace(top_blob_bordered, opt);
  532. }
  533. }
  534. else if (kernel_w == 4 && kernel_h == 4 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1)
  535. {
  536. deconv4x4s1_neon(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, opt);
  537. if (activation)
  538. {
  539. activation->forward_inplace(top_blob_bordered, opt);
  540. }
  541. }
  542. else if (kernel_w == 4 && kernel_h == 4 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1)
  543. {
  544. deconv4x4s2_neon(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, opt);
  545. if (activation)
  546. {
  547. activation->forward_inplace(top_blob_bordered, opt);
  548. }
  549. }
  550. else
  551. {
  552. // num_output
  553. #pragma omp parallel for num_threads(opt.num_threads)
  554. for (int p = 0; p < num_output; p++)
  555. {
  556. float* outptr = top_blob_bordered.channel(p);
  557. for (int i = 0; i < outh; i++)
  558. {
  559. for (int j = 0; j < outw; j++)
  560. {
  561. float sum = 0.f;
  562. if (bias_term)
  563. {
  564. sum = bias_data[p];
  565. }
  566. const float* kptr = (const float*)weight_data_tm + maxk * channels * p;
  567. // channels
  568. for (int q = 0; q < channels; q++)
  569. {
  570. const Mat m = bottom_blob.channel(q);
  571. for (int y = 0; y < kernel_h; y++)
  572. {
  573. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  574. if (sys < 0 || sys % stride_h != 0)
  575. continue;
  576. int sy = sys / stride_h;
  577. if (sy >= h)
  578. continue;
  579. const float* sptr = m.row(sy);
  580. for (int x = 0; x < kernel_w; x++)
  581. {
  582. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  583. if (sxs < 0 || sxs % stride_w != 0)
  584. continue;
  585. int sx = sxs / stride_w;
  586. if (sx >= w)
  587. continue;
  588. float val = sptr[sx];
  589. int k = y * kernel_w + x;
  590. float w = kptr[k];
  591. sum += val * w;
  592. }
  593. }
  594. kptr += maxk;
  595. }
  596. sum = activation_ss(sum, activation_type, activation_params);
  597. outptr[j] = sum;
  598. }
  599. outptr += outw;
  600. }
  601. }
  602. }
  603. }
  604. }
  605. cut_padding(top_blob_bordered, top_blob, opt);
  606. if (top_blob.empty())
  607. return -100;
  608. return 0;
  609. }
  610. int Deconvolution_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  611. {
  612. const Mat& bottom_blob = bottom_blobs[0];
  613. const Mat& _weight_data = bottom_blobs[1];
  614. Mat& top_blob = top_blobs[0];
  615. const int _num_input = bottom_blob.c * bottom_blob.elempack;
  616. const int _kernel_w = _weight_data.w;
  617. const int _kernel_h = _weight_data.h;
  618. const int _num_output = _weight_data.d * 1;
  619. Mat weight_data_flattened;
  620. flatten(_weight_data, weight_data_flattened, opt);
  621. if (weight_data_flattened.empty())
  622. return -100;
  623. #if NCNN_ARM82
  624. if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16)
  625. {
  626. Mat weight_data_flattened_fp32;
  627. cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  628. weight_data_flattened = weight_data_flattened_fp32;
  629. }
  630. #endif // NCNN_ARM82
  631. #if NCNN_BF16
  632. if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16)
  633. {
  634. Mat weight_data_flattened_fp32;
  635. cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  636. weight_data_flattened = weight_data_flattened_fp32;
  637. }
  638. #endif // NCNN_BF16
  639. // weight_data_flattened as pack1
  640. weight_data_flattened.w *= weight_data_flattened.elempack;
  641. weight_data_flattened.elemsize /= weight_data_flattened.elempack;
  642. weight_data_flattened.elempack = 1;
  643. // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
  644. Mat weight_data_transposed;
  645. {
  646. weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / 1, 4u, opt.workspace_allocator);
  647. if (weight_data_transposed.empty())
  648. return -100;
  649. const int outch_g = _num_output / 1;
  650. const int inch_g = _num_input / 1;
  651. const int maxk = _kernel_h * _kernel_w;
  652. for (int g = 0; g < 1; g++)
  653. {
  654. // reorder weight from inch-outch to outch-inch
  655. float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk;
  656. const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk;
  657. for (int i = 0; i < outch_g; i++)
  658. {
  659. for (int j = 0; j < inch_g; j++)
  660. {
  661. for (int k = 0; k < maxk; k++)
  662. {
  663. wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
  664. }
  665. }
  666. }
  667. }
  668. }
  669. Mat bias_data_flattened;
  670. if (bias_term)
  671. {
  672. const Mat& _bias_data = bottom_blobs[2];
  673. flatten(_bias_data, bias_data_flattened, opt);
  674. if (bias_data_flattened.empty())
  675. return -100;
  676. #if NCNN_ARM82
  677. if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16)
  678. {
  679. Mat bias_data_flattened_fp32;
  680. cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  681. bias_data_flattened = bias_data_flattened_fp32;
  682. }
  683. #endif // NCNN_ARM82
  684. #if NCNN_BF16
  685. if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16)
  686. {
  687. Mat bias_data_flattened_fp32;
  688. cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  689. bias_data_flattened = bias_data_flattened_fp32;
  690. }
  691. #endif // NCNN_BF16
  692. // bias_data_flattened as pack1
  693. bias_data_flattened.w *= bias_data_flattened.elempack;
  694. bias_data_flattened.elemsize /= bias_data_flattened.elempack;
  695. bias_data_flattened.elempack = 1;
  696. }
  697. ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution);
  698. ncnn::ParamDict pd;
  699. pd.set(0, _num_output);
  700. pd.set(1, _kernel_w);
  701. pd.set(11, _kernel_h);
  702. pd.set(2, dilation_w);
  703. pd.set(12, dilation_h);
  704. pd.set(3, stride_w);
  705. pd.set(13, stride_h);
  706. pd.set(4, pad_left);
  707. pd.set(15, pad_right);
  708. pd.set(14, pad_top);
  709. pd.set(16, pad_bottom);
  710. pd.set(18, output_pad_right);
  711. pd.set(19, output_pad_bottom);
  712. pd.set(20, output_w);
  713. pd.set(21, output_h);
  714. pd.set(5, bias_term);
  715. pd.set(6, weight_data_transposed.w);
  716. pd.set(9, activation_type);
  717. pd.set(10, activation_params);
  718. op->load_param(pd);
  719. ncnn::Mat weights[2];
  720. weights[0] = weight_data_transposed;
  721. weights[1] = bias_data_flattened;
  722. op->load_model(ncnn::ModelBinFromMatArray(weights));
  723. op->create_pipeline(opt);
  724. op->forward(bottom_blob, top_blob, opt);
  725. op->destroy_pipeline(opt);
  726. delete op;
  727. return 0;
  728. }
  729. #if NCNN_BF16
  730. int Deconvolution_arm::create_pipeline_bf16s(const Option& opt)
  731. {
  732. const int maxk = kernel_w * kernel_h;
  733. const int num_input = weight_data_size / maxk / num_output;
  734. int elempack = 1;
  735. int out_elempack = 1;
  736. #if __ARM_NEON
  737. if (opt.use_packing_layout)
  738. {
  739. elempack = num_input % 4 == 0 ? 4 : 1;
  740. out_elempack = num_output % 4 == 0 ? 4 : 1;
  741. }
  742. #endif
  743. Mat weight_data_transposed(weight_data.w);
  744. {
  745. float* pt = weight_data_transposed;
  746. const float* p = weight_data;
  747. for (int i = 0; i < num_input * num_output; i++)
  748. {
  749. for (int k = 0; k < maxk; k++)
  750. {
  751. pt[maxk - 1 - k] = p[k];
  752. }
  753. p += maxk;
  754. pt += maxk;
  755. }
  756. }
  757. // src = kw-kh-inch-outch
  758. // dst = pb-pa-kw-kh-inch/pa-outch/pb
  759. {
  760. Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output);
  761. weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)2u * elempack * out_elempack, elempack * out_elempack);
  762. for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack)
  763. {
  764. unsigned short* g00 = weight_data_tm.channel(q / out_elempack);
  765. for (int p = 0; p + (elempack - 1) < num_input; p += elempack)
  766. {
  767. for (int k = 0; k < maxk; k++)
  768. {
  769. for (int i = 0; i < elempack; i++)
  770. {
  771. for (int j = 0; j < out_elempack; j++)
  772. {
  773. const float* k00 = weight_data_r2.channel(q + j).row(p + i);
  774. g00[0] = float32_to_bfloat16(k00[k]);
  775. g00++;
  776. }
  777. }
  778. }
  779. }
  780. }
  781. }
  782. if (opt.lightmode)
  783. weight_data.release();
  784. return 0;
  785. }
  786. int Deconvolution_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  787. {
  788. // deconvolv with NxN kernel
  789. // value = value + bias
  790. int w = bottom_blob.w;
  791. int h = bottom_blob.h;
  792. int channels = bottom_blob.c;
  793. size_t elemsize = bottom_blob.elemsize;
  794. int elempack = bottom_blob.elempack;
  795. // 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);
  796. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  797. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  798. int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
  799. int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
  800. int out_elempack = 1;
  801. #if __ARM_NEON
  802. if (opt.use_packing_layout)
  803. {
  804. out_elempack = num_output % 4 == 0 ? 4 : 1;
  805. }
  806. #endif
  807. size_t out_elemsize = elemsize / elempack * out_elempack;
  808. Mat top_blob_bordered;
  809. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
  810. {
  811. top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
  812. }
  813. else
  814. {
  815. top_blob_bordered = top_blob;
  816. top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  817. }
  818. if (top_blob_bordered.empty())
  819. return -100;
  820. const int maxk = kernel_w * kernel_h;
  821. #if __ARM_NEON
  822. if (elempack == 4 && out_elempack == 4)
  823. {
  824. {
  825. // num_output
  826. #pragma omp parallel for num_threads(opt.num_threads)
  827. for (int p = 0; p < num_output / out_elempack; p++)
  828. {
  829. unsigned short* outptr = top_blob_bordered.channel(p);
  830. for (int i = 0; i < outh; i++)
  831. {
  832. for (int j = 0; j < outw; j++)
  833. {
  834. float32x4_t _sum = vdupq_n_f32(0.f);
  835. if (bias_term)
  836. {
  837. _sum = vld1q_f32(((const float*)bias_data) + p * 4);
  838. }
  839. const unsigned short* kptr = weight_data_tm.channel(p);
  840. // channels
  841. for (int q = 0; q < channels; q++)
  842. {
  843. const Mat m = bottom_blob.channel(q);
  844. for (int y = 0; y < kernel_h; y++)
  845. {
  846. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  847. if (sys < 0 || sys % stride_h != 0)
  848. continue;
  849. int sy = sys / stride_h;
  850. if (sy >= h)
  851. continue;
  852. for (int x = 0; x < kernel_w; x++)
  853. {
  854. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  855. if (sxs < 0 || sxs % stride_w != 0)
  856. continue;
  857. int sx = sxs / stride_w;
  858. if (sx >= w)
  859. continue;
  860. const unsigned short* sptr = m.row<const unsigned short>(sy) + sx * 4;
  861. float32x4_t _val = bfloat2float(vld1_u16(sptr));
  862. int k = y * kernel_w + x;
  863. float32x4_t _w0 = bfloat2float(vld1_u16(kptr + k * 16));
  864. float32x4_t _w1 = bfloat2float(vld1_u16(kptr + k * 16 + 4));
  865. float32x4_t _w2 = bfloat2float(vld1_u16(kptr + k * 16 + 8));
  866. float32x4_t _w3 = bfloat2float(vld1_u16(kptr + k * 16 + 12));
  867. #if __aarch64__
  868. _sum = vmlaq_laneq_f32(_sum, _w0, _val, 0);
  869. _sum = vmlaq_laneq_f32(_sum, _w1, _val, 1);
  870. _sum = vmlaq_laneq_f32(_sum, _w2, _val, 2);
  871. _sum = vmlaq_laneq_f32(_sum, _w3, _val, 3);
  872. #else
  873. _sum = vmlaq_lane_f32(_sum, _w0, vget_low_f32(_val), 0);
  874. _sum = vmlaq_lane_f32(_sum, _w1, vget_low_f32(_val), 1);
  875. _sum = vmlaq_lane_f32(_sum, _w2, vget_high_f32(_val), 0);
  876. _sum = vmlaq_lane_f32(_sum, _w3, vget_high_f32(_val), 1);
  877. #endif
  878. }
  879. }
  880. kptr += maxk * 16;
  881. }
  882. _sum = activation_ps(_sum, activation_type, activation_params);
  883. vst1_u16(outptr + j * 4, float2bfloat(_sum));
  884. }
  885. outptr += outw * 4;
  886. }
  887. }
  888. }
  889. }
  890. if (elempack == 1 && out_elempack == 4)
  891. {
  892. {
  893. // num_output
  894. #pragma omp parallel for num_threads(opt.num_threads)
  895. for (int p = 0; p < num_output / out_elempack; p++)
  896. {
  897. unsigned short* outptr = top_blob_bordered.channel(p);
  898. for (int i = 0; i < outh; i++)
  899. {
  900. for (int j = 0; j < outw; j++)
  901. {
  902. float32x4_t _sum = vdupq_n_f32(0.f);
  903. if (bias_term)
  904. {
  905. _sum = vld1q_f32(((const float*)bias_data) + p * 4);
  906. }
  907. const unsigned short* kptr = weight_data_tm.channel(p);
  908. // channels
  909. for (int q = 0; q < channels; q++)
  910. {
  911. const Mat m = bottom_blob.channel(q);
  912. for (int y = 0; y < kernel_h; y++)
  913. {
  914. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  915. if (sys < 0 || sys % stride_h != 0)
  916. continue;
  917. int sy = sys / stride_h;
  918. if (sy >= h)
  919. continue;
  920. const unsigned short* sptr = m.row<const unsigned short>(sy);
  921. for (int x = 0; x < kernel_w; x++)
  922. {
  923. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  924. if (sxs < 0 || sxs % stride_w != 0)
  925. continue;
  926. int sx = sxs / stride_w;
  927. if (sx >= w)
  928. continue;
  929. float32x4_t _val = vdupq_n_f32(bfloat16_to_float32(sptr[sx]));
  930. int k = y * kernel_w + x;
  931. float32x4_t _w = bfloat2float(vld1_u16(kptr + k * 4));
  932. _sum = vmlaq_f32(_sum, _val, _w);
  933. }
  934. }
  935. kptr += maxk * 4;
  936. }
  937. _sum = activation_ps(_sum, activation_type, activation_params);
  938. vst1_u16(outptr + j * 4, float2bfloat(_sum));
  939. }
  940. outptr += outw * 4;
  941. }
  942. }
  943. }
  944. }
  945. if (elempack == 4 && out_elempack == 1)
  946. {
  947. {
  948. // num_output
  949. #pragma omp parallel for num_threads(opt.num_threads)
  950. for (int p = 0; p < num_output / out_elempack; p++)
  951. {
  952. unsigned short* outptr = top_blob_bordered.channel(p);
  953. for (int i = 0; i < outh; i++)
  954. {
  955. for (int j = 0; j < outw; j++)
  956. {
  957. float sum = 0.f;
  958. if (bias_term)
  959. {
  960. sum = bias_data[p];
  961. }
  962. const unsigned short* kptr = weight_data_tm.channel(p);
  963. // channels
  964. for (int q = 0; q < channels; q++)
  965. {
  966. const Mat m = bottom_blob.channel(q);
  967. for (int y = 0; y < kernel_h; y++)
  968. {
  969. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  970. if (sys < 0 || sys % stride_h != 0)
  971. continue;
  972. int sy = sys / stride_h;
  973. if (sy >= h)
  974. continue;
  975. for (int x = 0; x < kernel_w; x++)
  976. {
  977. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  978. if (sxs < 0 || sxs % stride_w != 0)
  979. continue;
  980. int sx = sxs / stride_w;
  981. if (sx >= w)
  982. continue;
  983. const unsigned short* sptr = m.row<const unsigned short>(sy) + sx * 4;
  984. float32x4_t _val = bfloat2float(vld1_u16(sptr));
  985. int k = y * kernel_w + x;
  986. float32x4_t _w = bfloat2float(vld1_u16(kptr + k * 4));
  987. float32x4_t _s4 = vmulq_f32(_val, _w);
  988. #if __aarch64__
  989. sum += vaddvq_f32(_s4); // dot
  990. #else
  991. float32x2_t _ss = vadd_f32(vget_low_f32(_s4), vget_high_f32(_s4));
  992. _ss = vpadd_f32(_ss, _ss);
  993. sum += vget_lane_f32(_ss, 0);
  994. #endif
  995. }
  996. }
  997. kptr += maxk * 4;
  998. }
  999. sum = activation_ss(sum, activation_type, activation_params);
  1000. outptr[j] = float32_to_bfloat16(sum);
  1001. }
  1002. outptr += outw;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. #endif // __ARM_NEON
  1008. if (elempack == 1 && out_elempack == 1)
  1009. {
  1010. {
  1011. // num_output
  1012. #pragma omp parallel for num_threads(opt.num_threads)
  1013. for (int p = 0; p < num_output; p++)
  1014. {
  1015. unsigned short* outptr = top_blob_bordered.channel(p);
  1016. for (int i = 0; i < outh; i++)
  1017. {
  1018. for (int j = 0; j < outw; j++)
  1019. {
  1020. float sum = 0.f;
  1021. if (bias_term)
  1022. {
  1023. sum = bias_data[p];
  1024. }
  1025. const unsigned short* kptr = weight_data_tm.channel(p);
  1026. // channels
  1027. for (int q = 0; q < channels; q++)
  1028. {
  1029. const Mat m = bottom_blob.channel(q);
  1030. for (int y = 0; y < kernel_h; y++)
  1031. {
  1032. int sys = (i + y * dilation_h - (kernel_extent_h - 1));
  1033. if (sys < 0 || sys % stride_h != 0)
  1034. continue;
  1035. int sy = sys / stride_h;
  1036. if (sy >= h)
  1037. continue;
  1038. const unsigned short* sptr = m.row<const unsigned short>(sy);
  1039. for (int x = 0; x < kernel_w; x++)
  1040. {
  1041. int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
  1042. if (sxs < 0 || sxs % stride_w != 0)
  1043. continue;
  1044. int sx = sxs / stride_w;
  1045. if (sx >= w)
  1046. continue;
  1047. float val = bfloat16_to_float32(sptr[sx]);
  1048. int k = y * kernel_w + x;
  1049. float w = bfloat16_to_float32(kptr[k]);
  1050. sum += val * w;
  1051. }
  1052. }
  1053. kptr += maxk;
  1054. }
  1055. sum = activation_ss(sum, activation_type, activation_params);
  1056. outptr[j] = float32_to_bfloat16(sum);
  1057. }
  1058. outptr += outw;
  1059. }
  1060. }
  1061. }
  1062. }
  1063. cut_padding(top_blob_bordered, top_blob, opt);
  1064. if (top_blob.empty())
  1065. return -100;
  1066. return 0;
  1067. }
  1068. #endif // NCNN_BF16
  1069. } // namespace ncnn