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_asimdhp.cpp 52 kB

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