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.

convolutiondepthwise_arm.cpp 40 kB

7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 "convolutiondepthwise_arm.h"
  15. #include "layer_type.h"
  16. #if __ARM_NEON
  17. #include <arm_neon.h>
  18. #include "neon_mathfun.h"
  19. #endif // __ARM_NEON
  20. namespace ncnn {
  21. #include "convolutiondepthwise_3x3.h"
  22. #include "convolutiondepthwise_5x5.h"
  23. #include "convolutiondepthwise_3x3_int8.h"
  24. DEFINE_LAYER_CREATOR(ConvolutionDepthWise_arm)
  25. ConvolutionDepthWise_arm::ConvolutionDepthWise_arm()
  26. {
  27. #if __ARM_NEON
  28. support_packing = true;
  29. #endif // __ARM_NEON
  30. activation = 0;
  31. }
  32. int ConvolutionDepthWise_arm::create_pipeline(const Option& opt)
  33. {
  34. Option opt_cpu = opt;
  35. opt_cpu.use_vulkan_compute = false;
  36. if (activation_type == 1)
  37. {
  38. activation = ncnn::create_layer(ncnn::LayerType::ReLU);
  39. ncnn::ParamDict pd;
  40. activation->load_param(pd);
  41. }
  42. else if (activation_type == 2)
  43. {
  44. activation = ncnn::create_layer(ncnn::LayerType::ReLU);
  45. ncnn::ParamDict pd;
  46. pd.set(0, activation_params[0]);// slope
  47. activation->load_param(pd);
  48. }
  49. else if (activation_type == 3)
  50. {
  51. activation = ncnn::create_layer(ncnn::LayerType::Clip);
  52. ncnn::ParamDict pd;
  53. pd.set(0, activation_params[0]);// min
  54. pd.set(1, activation_params[1]);// max
  55. activation->load_param(pd);
  56. }
  57. else if (activation_type == 4)
  58. {
  59. activation = ncnn::create_layer(ncnn::LayerType::Sigmoid);
  60. ncnn::ParamDict pd;
  61. activation->load_param(pd);
  62. }
  63. if (activation)
  64. {
  65. activation->create_pipeline(opt_cpu);
  66. }
  67. // create Convolution op for each group
  68. const int maxk = kernel_w * kernel_h;
  69. int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
  70. #if __ARM_NEON
  71. if (opt.use_packing_layout)
  72. {
  73. // depth-wise
  74. if (channels == group && group == num_output)
  75. {
  76. // pack4
  77. if (num_output % 4 == 0)
  78. {
  79. Mat weight_data_r2 = weight_data.reshape(maxk, group);
  80. convert_packing(weight_data_r2, weight_data_pack4, 4);
  81. }
  82. }
  83. // group convolution
  84. const int channels_g = channels / group;
  85. const int num_output_g = num_output / group;
  86. // pack4
  87. if (channels_g % 4 == 0 && num_output_g % 4 == 0)
  88. {
  89. // src = kw-kh-inch-outch
  90. // dst = 4a-4b-kw-kh-inch/4a-outch/4b
  91. {
  92. Mat weight_data_r2_groups = weight_data.reshape(maxk, channels_g, num_output_g * group);
  93. weight_data_pack4_groups.create(maxk, channels_g/4, num_output_g/4 * group, (size_t)4*16, 16);
  94. for (int g=0; g<group; g++)
  95. {
  96. const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
  97. Mat weight_data_pack4_g = weight_data_pack4_groups.channel_range(num_output_g/4 * g, num_output_g/4);
  98. for (int q=0; q+3<num_output_g; q+=4)
  99. {
  100. const Mat k0 = weight_data_r2.channel(q);
  101. const Mat k1 = weight_data_r2.channel(q+1);
  102. const Mat k2 = weight_data_r2.channel(q+2);
  103. const Mat k3 = weight_data_r2.channel(q+3);
  104. Mat g0 = weight_data_pack4_g.channel(q/4);
  105. for (int p=0; p+3<channels_g; p+=4)
  106. {
  107. const float* k00 = k0.row(p);
  108. const float* k01 = k0.row(p+1);
  109. const float* k02 = k0.row(p+2);
  110. const float* k03 = k0.row(p+3);
  111. const float* k10 = k1.row(p);
  112. const float* k11 = k1.row(p+1);
  113. const float* k12 = k1.row(p+2);
  114. const float* k13 = k1.row(p+3);
  115. const float* k20 = k2.row(p);
  116. const float* k21 = k2.row(p+1);
  117. const float* k22 = k2.row(p+2);
  118. const float* k23 = k2.row(p+3);
  119. const float* k30 = k3.row(p);
  120. const float* k31 = k3.row(p+1);
  121. const float* k32 = k3.row(p+2);
  122. const float* k33 = k3.row(p+3);
  123. float* g00 = g0.row(p/4);
  124. for (int k=0; k<maxk; k++)
  125. {
  126. g00[0] = k00[k];
  127. g00[1] = k10[k];
  128. g00[2] = k20[k];
  129. g00[3] = k30[k];
  130. g00[4] = k01[k];
  131. g00[5] = k11[k];
  132. g00[6] = k21[k];
  133. g00[7] = k31[k];
  134. g00[8] = k02[k];
  135. g00[9] = k12[k];
  136. g00[10] = k22[k];
  137. g00[11] = k32[k];
  138. g00[12] = k03[k];
  139. g00[13] = k13[k];
  140. g00[14] = k23[k];
  141. g00[15] = k33[k];
  142. g00 += 16;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. // pack1to4
  150. if (channels_g % 4 != 0 && num_output_g % 4 == 0)
  151. {
  152. // src = kw-kh-inch-outch
  153. // dst = 4b-kw-kh-inch-outch/4b
  154. {
  155. Mat weight_data_r2_groups = weight_data.reshape(maxk, channels_g, num_output_g * group);
  156. weight_data_pack1to4_groups.create(maxk, channels_g, num_output_g/4 * group, (size_t)4*4, 4);
  157. for (int g=0; g<group; g++)
  158. {
  159. const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
  160. Mat weight_data_pack1to4_g = weight_data_pack1to4_groups.channel_range(num_output_g/4 * g, num_output_g/4);
  161. for (int q=0; q+3<num_output_g; q+=4)
  162. {
  163. const Mat k0 = weight_data_r2.channel(q);
  164. const Mat k1 = weight_data_r2.channel(q+1);
  165. const Mat k2 = weight_data_r2.channel(q+2);
  166. const Mat k3 = weight_data_r2.channel(q+3);
  167. Mat g0 = weight_data_pack1to4_g.channel(q/4);
  168. for (int p=0; p<channels_g; p++)
  169. {
  170. const float* k00 = k0.row(p);
  171. const float* k10 = k1.row(p);
  172. const float* k20 = k2.row(p);
  173. const float* k30 = k3.row(p);
  174. float* g00 = g0.row(p);
  175. for (int k=0; k<maxk; k++)
  176. {
  177. g00[0] = k00[k];
  178. g00[1] = k10[k];
  179. g00[2] = k20[k];
  180. g00[3] = k30[k];
  181. g00 += 4;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. // pack4to1
  189. if (channels_g % 4 == 0 && num_output_g % 4 != 0)
  190. {
  191. // src = kw-kh-inch-outch
  192. // dst = 4a-kw-kh-inch/4a-outch
  193. {
  194. Mat weight_data_r2_groups = weight_data.reshape(maxk, channels_g, num_output_g * group);
  195. weight_data_pack4to1_groups.create(maxk, channels_g/4, num_output_g * group, (size_t)4*4, 4);
  196. for (int g=0; g<group; g++)
  197. {
  198. const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
  199. Mat weight_data_pack4to1_g = weight_data_pack4to1_groups.channel_range(num_output_g * g, num_output_g);
  200. for (int q=0; q<num_output_g; q++)
  201. {
  202. const Mat k0 = weight_data_r2.channel(q);
  203. Mat g0 = weight_data_pack4to1_g.channel(q);
  204. for (int p=0; p+3<channels_g; p+=4)
  205. {
  206. const float* k00 = k0.row(p);
  207. const float* k01 = k0.row(p+1);
  208. const float* k02 = k0.row(p+2);
  209. const float* k03 = k0.row(p+3);
  210. float* g00 = g0.row(p/4);
  211. for (int k=0; k<maxk; k++)
  212. {
  213. g00[0] = k00[k];
  214. g00[1] = k01[k];
  215. g00[2] = k02[k];
  216. g00[3] = k03[k];
  217. g00 += 4;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. } // opt.use_packing_layout
  225. #endif // __ARM_NEON
  226. for (int i=0; i<(int)group_ops.size(); i++)
  227. delete group_ops[i];
  228. group_ops.clear();
  229. if (channels == group && group == num_output)
  230. {
  231. // depth-wise specific
  232. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1)
  233. {
  234. if ((stride_w == 1 && stride_h == 1) || (stride_w == 2 && stride_h == 2))
  235. {
  236. return 0;
  237. }
  238. }
  239. if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && use_int8_inference == false)
  240. {
  241. if ((stride_w == 1 && stride_h == 1) || (stride_w == 2 && stride_h == 2))
  242. {
  243. return 0;
  244. }
  245. }
  246. }
  247. const int channels_g = channels / group;
  248. const int num_output_g = num_output / group;
  249. group_ops.resize(group);
  250. for (int g=0; g<group; g++)
  251. {
  252. Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g);
  253. Mat bias_data_g;
  254. if (bias_term)
  255. bias_data_g = bias_data.range(num_output_g * g, num_output_g);
  256. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Convolution);
  257. // set param
  258. ncnn::ParamDict pd;
  259. pd.set(0, num_output_g);// num_output
  260. pd.set(1, kernel_w);
  261. pd.set(11, kernel_h);
  262. pd.set(2, dilation_w);
  263. pd.set(12, dilation_h);
  264. pd.set(3, stride_w);
  265. pd.set(13, stride_h);
  266. pd.set(4, 0);// pad_w
  267. pd.set(14, 0);// pad_h
  268. pd.set(5, bias_term);
  269. pd.set(6, maxk * channels_g * num_output_g);// weight_data_size
  270. pd.set(8, int8_scale_term);
  271. op->load_param(pd);
  272. // set weights
  273. if (bias_term)
  274. {
  275. ncnn::Mat weights[4];
  276. weights[0] = weight_data_g;
  277. weights[1] = bias_data_g;
  278. if (int8_scale_term)
  279. {
  280. weights[2] = weight_data_int8_scales.range(g, 1);
  281. weights[3] = bottom_blob_int8_scales.range(g, 1);
  282. }
  283. op->load_model(ModelBinFromMatArray(weights));
  284. }
  285. else
  286. {
  287. ncnn::Mat weights[3];
  288. weights[0] = weight_data_g;
  289. if (int8_scale_term)
  290. {
  291. weights[1] = weight_data_int8_scales.range(g, 1);
  292. weights[2] = bottom_blob_int8_scales.range(g, 1);
  293. }
  294. op->load_model(ModelBinFromMatArray(weights));
  295. }
  296. op->create_pipeline(opt_cpu);
  297. group_ops[g] = op;
  298. }
  299. return 0;
  300. }
  301. int ConvolutionDepthWise_arm::destroy_pipeline(const Option& opt)
  302. {
  303. Option opt_cpu = opt;
  304. opt_cpu.use_vulkan_compute = false;
  305. if (activation)
  306. {
  307. activation->destroy_pipeline(opt_cpu);
  308. delete activation;
  309. activation = 0;
  310. }
  311. for (int i=0; i<(int)group_ops.size(); i++)
  312. {
  313. group_ops[i]->destroy_pipeline(opt_cpu);
  314. delete group_ops[i];
  315. }
  316. group_ops.clear();
  317. return 0;
  318. }
  319. int ConvolutionDepthWise_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  320. {
  321. // convolv with NxN kernel
  322. // value = value + bias
  323. int w = bottom_blob.w;
  324. int h = bottom_blob.h;
  325. int channels = bottom_blob.c;
  326. size_t elemsize = bottom_blob.elemsize;
  327. int elempack = bottom_blob.elempack;
  328. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  329. const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
  330. Mat bottom_blob_unbordered = bottom_blob;
  331. if (use_int8_inference && elemsize != 1)
  332. {
  333. Mat bottom_blob_int8;
  334. bottom_blob_int8.create(w, h, channels, (size_t)1u, opt.workspace_allocator);
  335. if (bottom_blob_int8.empty())
  336. return -100;
  337. const int channels_g = channels / group;
  338. // quantize, scale and round to nearest
  339. #pragma omp parallel for num_threads(opt.num_threads)
  340. for (int g=0; g<group; g++)
  341. {
  342. ncnn::Option opt_g = opt;
  343. opt_g.num_threads = 1;
  344. opt_g.blob_allocator = bottom_blob_int8.allocator;
  345. const Mat bottom_blob_g = bottom_blob.channel_range(channels_g * g, channels_g);
  346. Mat bottom_blob_int8_g = bottom_blob_int8.channel_range(channels_g * g, channels_g);
  347. quantize_ops[g]->forward(bottom_blob_g, bottom_blob_int8_g, opt_g);
  348. }
  349. bottom_blob_unbordered = bottom_blob_int8;
  350. }
  351. Mat bottom_blob_bordered = bottom_blob_unbordered;
  352. if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
  353. {
  354. Option opt_b = opt;
  355. opt_b.blob_allocator = opt.workspace_allocator;
  356. copy_make_border(bottom_blob_unbordered, bottom_blob_bordered, pad_top, pad_bottom, pad_left, pad_right, BORDER_CONSTANT, 0.f, opt_b);
  357. if (bottom_blob_bordered.empty())
  358. return -100;
  359. w = bottom_blob_bordered.w;
  360. h = bottom_blob_bordered.h;
  361. }
  362. else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233)
  363. {
  364. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  365. int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
  366. if (wpad > 0 || hpad > 0)
  367. {
  368. Option opt_b = opt;
  369. opt_b.blob_allocator = opt.workspace_allocator;
  370. copy_make_border(bottom_blob_unbordered, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, 0.f, opt_b);
  371. if (bottom_blob_bordered.empty())
  372. return -100;
  373. }
  374. w = bottom_blob_bordered.w;
  375. h = bottom_blob_bordered.h;
  376. }
  377. int outw = (w - kernel_extent_w) / stride_w + 1;
  378. int outh = (h - kernel_extent_h) / stride_h + 1;
  379. int out_elempack = num_output % 4 == 0 ? 4 : 1;
  380. size_t out_elemsize = elemsize / elempack * out_elempack;
  381. #if __ARM_NEON
  382. if (opt.use_packing_layout)
  383. {
  384. const int maxk = kernel_w * kernel_h;
  385. // kernel offsets
  386. std::vector<int> _space_ofs(maxk);
  387. int* space_ofs = &_space_ofs[0];
  388. {
  389. int p1 = 0;
  390. int p2 = 0;
  391. int gap = w * dilation_h - kernel_w * dilation_w;
  392. for (int i = 0; i < kernel_h; i++)
  393. {
  394. for (int j = 0; j < kernel_w; j++)
  395. {
  396. space_ofs[p1] = p2;
  397. p1++;
  398. p2 += dilation_w;
  399. }
  400. p2 += gap;
  401. }
  402. }
  403. top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  404. if (top_blob.empty())
  405. return -100;
  406. // depth-wise
  407. if (channels == group / elempack && group / elempack == num_output / elempack)
  408. {
  409. if (elempack == 4)
  410. {
  411. #pragma omp parallel for num_threads(opt.num_threads)
  412. for (int g=0; g<group / elempack; g++)
  413. {
  414. float* outptr = top_blob.channel(g);
  415. const float* kptr = (const float*)weight_data_pack4 + maxk * g * 4;
  416. const Mat m = bottom_blob_bordered.channel(g);
  417. for (int i = 0; i < outh; i++)
  418. {
  419. for (int j = 0; j < outw; j++)
  420. {
  421. float32x4_t _sum = vdupq_n_f32(0.f);
  422. if (bias_term)
  423. {
  424. _sum = vld1q_f32(((const float*)bias_data) + g * 4);
  425. }
  426. const float* sptr = m.row(i*stride_h) + j*stride_w * 4;
  427. for (int k = 0; k < maxk; k++)
  428. {
  429. float32x4_t _val = vld1q_f32( sptr + space_ofs[k] * 4 );
  430. float32x4_t _w = vld1q_f32( kptr + k * 4 );
  431. _sum = vmlaq_f32(_sum, _val, _w);
  432. }
  433. if (activation_type == 1)
  434. {
  435. float32x4_t _zero = vdupq_n_f32(0.f);
  436. _sum = vmaxq_f32(_sum, _zero);
  437. }
  438. else if (activation_type == 2)
  439. {
  440. float32x4_t _zero = vdupq_n_f32(0.f);
  441. float32x4_t _slope = vdupq_n_f32(activation_params[0]);
  442. uint32x4_t _lemask = vcleq_f32(_sum, _zero);
  443. float32x4_t _ps = vmulq_f32(_sum, _slope);
  444. _sum = vbslq_f32(_lemask, _ps, _sum);
  445. }
  446. else if (activation_type == 3)
  447. {
  448. float32x4_t _min = vdupq_n_f32(activation_params[0]);
  449. float32x4_t _max = vdupq_n_f32(activation_params[1]);
  450. _sum = vmaxq_f32(_sum, _min);
  451. _sum = vminq_f32(_sum, _max);
  452. }
  453. else if (activation_type == 4)
  454. {
  455. float32x4_t _one = vdupq_n_f32(1.f);
  456. _sum = vnegq_f32(_sum);
  457. _sum = exp_ps(_sum);
  458. _sum = vaddq_f32(_sum, _one);
  459. float32x4_t _outp = vrecpeq_f32(_sum);
  460. _outp = vmulq_f32(vrecpsq_f32(_sum, _outp), _outp);
  461. // _outp = vmulq_f32(vrecpsq_f32(_sum, _outp), _outp);
  462. _sum = _outp;
  463. }
  464. vst1q_f32(outptr + j * 4, _sum);
  465. }
  466. outptr += outw * 4;
  467. }
  468. }
  469. return 0;
  470. }
  471. }
  472. const int channels_g = channels * elempack / group;
  473. const int num_output_g = num_output / group;
  474. // unpacking
  475. Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
  476. if (elempack == 4 && channels_g % 4 != 0)
  477. {
  478. Option opt_p = opt;
  479. opt_p.blob_allocator = opt.workspace_allocator;
  480. convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, 1, opt_p);
  481. }
  482. Mat top_blob_unpacked = top_blob;
  483. if (num_output_g % 4 != 0 && out_elempack == 4)
  484. {
  485. top_blob_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
  486. if (top_blob_unpacked.empty())
  487. return -100;
  488. }
  489. if (channels_g % 4 == 0 && num_output_g % 4 == 0)
  490. {
  491. #ifdef _WIN32
  492. #pragma omp parallel for num_threads(opt.num_threads)
  493. #else // _WIN32
  494. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  495. #endif // _WIN32
  496. for (int g=0; g<group; g++)
  497. {
  498. for (int p=0; p<num_output_g / 4; p++)
  499. {
  500. float* outptr = top_blob_unpacked.channel(g * num_output_g / 4 + p);
  501. const float* weight_data_ptr = (const float*)weight_data_pack4_groups + maxk * channels_g / 4 * num_output_g / 4 * g * 16;
  502. for (int i = 0; i < outh; i++)
  503. {
  504. for (int j = 0; j < outw; j++)
  505. {
  506. float32x4_t _sum = vdupq_n_f32(0.f);
  507. if (bias_term)
  508. {
  509. _sum = vld1q_f32(((const float*)bias_data) + num_output_g * g + p * 4);
  510. }
  511. const float* kptr = weight_data_ptr + maxk * channels_g / 4 * p * 16;
  512. // channels_g
  513. for (int q=0; q<channels_g / 4; q++)
  514. {
  515. const Mat m = bottom_blob_bordered.channel(channels_g / 4 * g + q);
  516. const float* sptr = m.row(i*stride_h) + j*stride_w * 4;
  517. for (int k = 0; k < maxk; k++)
  518. {
  519. float32x4_t _val = vld1q_f32( sptr + space_ofs[k] * 4 );
  520. float32x4_t _w0 = vld1q_f32( kptr );
  521. float32x4_t _w1 = vld1q_f32( kptr + 4 );
  522. float32x4_t _w2 = vld1q_f32( kptr + 8 );
  523. float32x4_t _w3 = vld1q_f32( kptr + 12 );
  524. #if __aarch64__
  525. _sum = vmlaq_laneq_f32(_sum, _w0, _val, 0);
  526. _sum = vmlaq_laneq_f32(_sum, _w1, _val, 1);
  527. _sum = vmlaq_laneq_f32(_sum, _w2, _val, 2);
  528. _sum = vmlaq_laneq_f32(_sum, _w3, _val, 3);
  529. #else
  530. _sum = vmlaq_lane_f32(_sum, _w0, vget_low_f32(_val), 0);
  531. _sum = vmlaq_lane_f32(_sum, _w1, vget_low_f32(_val), 1);
  532. _sum = vmlaq_lane_f32(_sum, _w2, vget_high_f32(_val), 0);
  533. _sum = vmlaq_lane_f32(_sum, _w3, vget_high_f32(_val), 1);
  534. #endif
  535. kptr += 16;
  536. }
  537. }
  538. if (activation_type == 1)
  539. {
  540. float32x4_t _zero = vdupq_n_f32(0.f);
  541. _sum = vmaxq_f32(_sum, _zero);
  542. }
  543. else if (activation_type == 2)
  544. {
  545. float32x4_t _zero = vdupq_n_f32(0.f);
  546. float32x4_t _slope = vdupq_n_f32(activation_params[0]);
  547. uint32x4_t _lemask = vcleq_f32(_sum, _zero);
  548. float32x4_t _ps = vmulq_f32(_sum, _slope);
  549. _sum = vbslq_f32(_lemask, _ps, _sum);
  550. }
  551. else if (activation_type == 3)
  552. {
  553. float32x4_t _min = vdupq_n_f32(activation_params[0]);
  554. float32x4_t _max = vdupq_n_f32(activation_params[1]);
  555. _sum = vmaxq_f32(_sum, _min);
  556. _sum = vminq_f32(_sum, _max);
  557. }
  558. else if (activation_type == 4)
  559. {
  560. float32x4_t _one = vdupq_n_f32(1.f);
  561. _sum = vnegq_f32(_sum);
  562. _sum = exp_ps(_sum);
  563. _sum = vaddq_f32(_sum, _one);
  564. float32x4_t _outp = vrecpeq_f32(_sum);
  565. _outp = vmulq_f32(vrecpsq_f32(_sum, _outp), _outp);
  566. // _outp = vmulq_f32(vrecpsq_f32(_sum, _outp), _outp);
  567. _sum = _outp;
  568. }
  569. vst1q_f32(outptr + j * 4, _sum);
  570. }
  571. outptr += outw * 4;
  572. }
  573. }
  574. }
  575. }
  576. if (channels_g % 4 != 0 && num_output_g % 4 == 0)
  577. {
  578. #ifdef _WIN32
  579. #pragma omp parallel for num_threads(opt.num_threads)
  580. #else // _WIN32
  581. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  582. #endif // _WIN32
  583. for (int g=0; g<group; g++)
  584. {
  585. for (int p=0; p<num_output_g / 4; p++)
  586. {
  587. float* outptr = top_blob_unpacked.channel(g * num_output_g / 4 + p);
  588. const float* weight_data_ptr = (const float*)weight_data_pack1to4_groups + maxk * channels_g * num_output_g / 4 * g * 4;
  589. for (int i = 0; i < outh; i++)
  590. {
  591. for (int j = 0; j < outw; j++)
  592. {
  593. float32x4_t _sum = vdupq_n_f32(0.f);
  594. if (bias_term)
  595. {
  596. _sum = vld1q_f32(((const float*)bias_data) + (num_output_g / 4 * g + p) * 4);
  597. }
  598. const float* kptr = weight_data_ptr + maxk * channels_g * p * 4;
  599. // channels_g
  600. for (int q=0; q<channels_g; q++)
  601. {
  602. const Mat m = bottom_blob_bordered.channel(channels_g * g + q);
  603. const float* sptr = m.row(i*stride_h) + j*stride_w;
  604. for (int k = 0; k < maxk; k++)
  605. {
  606. float32x4_t _val = vdupq_n_f32( sptr[ space_ofs[k] ] );
  607. float32x4_t _w = vld1q_f32( kptr );
  608. _sum = vmlaq_f32(_sum, _val, _w);
  609. kptr += 4;
  610. }
  611. }
  612. if (activation_type == 1)
  613. {
  614. float32x4_t _zero = vdupq_n_f32(0.f);
  615. _sum = vmaxq_f32(_sum, _zero);
  616. }
  617. else if (activation_type == 2)
  618. {
  619. float32x4_t _zero = vdupq_n_f32(0.f);
  620. float32x4_t _slope = vdupq_n_f32(activation_params[0]);
  621. uint32x4_t _lemask = vcleq_f32(_sum, _zero);
  622. float32x4_t _ps = vmulq_f32(_sum, _slope);
  623. _sum = vbslq_f32(_lemask, _ps, _sum);
  624. }
  625. else if (activation_type == 3)
  626. {
  627. float32x4_t _min = vdupq_n_f32(activation_params[0]);
  628. float32x4_t _max = vdupq_n_f32(activation_params[1]);
  629. _sum = vmaxq_f32(_sum, _min);
  630. _sum = vminq_f32(_sum, _max);
  631. }
  632. else if (activation_type == 4)
  633. {
  634. float32x4_t _one = vdupq_n_f32(1.f);
  635. _sum = vnegq_f32(_sum);
  636. _sum = exp_ps(_sum);
  637. _sum = vaddq_f32(_sum, _one);
  638. float32x4_t _outp = vrecpeq_f32(_sum);
  639. _outp = vmulq_f32(vrecpsq_f32(_sum, _outp), _outp);
  640. // _outp = vmulq_f32(vrecpsq_f32(_sum, _outp), _outp);
  641. _sum = _outp;
  642. }
  643. vst1q_f32(outptr + j * 4, _sum);
  644. }
  645. outptr += outw * 4;
  646. }
  647. }
  648. }
  649. }
  650. if (channels_g % 4 == 0 && num_output_g % 4 != 0)
  651. {
  652. #ifdef _WIN32
  653. #pragma omp parallel for num_threads(opt.num_threads)
  654. #else // _WIN32
  655. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  656. #endif // _WIN32
  657. for (int g=0; g<group; g++)
  658. {
  659. for (int p=0; p<num_output_g; p++)
  660. {
  661. float* outptr = top_blob_unpacked.channel(g * num_output_g + p);
  662. const float* weight_data_ptr = (const float*)weight_data_pack4to1_groups + maxk * channels_g / 4 * num_output_g * g * 4;
  663. for (int i = 0; i < outh; i++)
  664. {
  665. for (int j = 0; j < outw; j++)
  666. {
  667. float sum = 0.f;
  668. if (bias_term)
  669. sum = bias_data[num_output_g * g + p];
  670. const float* kptr = weight_data_ptr + maxk * channels_g / 4 * p * 4;
  671. // channels_g
  672. for (int q=0; q<channels_g / 4; q++)
  673. {
  674. const Mat m = bottom_blob_bordered.channel(channels_g / 4 * g + q);
  675. const float* sptr = m.row(i*stride_h) + j*stride_w * 4;
  676. for (int k = 0; k < maxk; k++)
  677. {
  678. float32x4_t _val = vld1q_f32( sptr + space_ofs[k] * 4 );
  679. float32x4_t _w = vld1q_f32( kptr );
  680. float32x4_t _s4 = vmulq_f32(_val, _w);
  681. #if __aarch64__
  682. sum += vaddvq_f32(_s4); // dot
  683. #else
  684. float32x2_t _ss = vadd_f32(vget_low_f32(_s4), vget_high_f32(_s4));
  685. _ss = vpadd_f32(_ss, _ss);
  686. sum += vget_lane_f32(_ss, 0);
  687. #endif
  688. kptr += 4;
  689. }
  690. }
  691. if (activation_type == 1)
  692. {
  693. sum = std::max(sum, 0.f);
  694. }
  695. else if (activation_type == 2)
  696. {
  697. float slope = activation_params[0];
  698. sum = sum > 0.f ? sum : sum * slope;
  699. }
  700. else if (activation_type == 3)
  701. {
  702. float min = activation_params[0];
  703. float max = activation_params[1];
  704. if (sum < min)
  705. sum = min;
  706. if (sum > max)
  707. sum = max;
  708. }
  709. else if (activation_type == 4)
  710. {
  711. sum = 1.f / (1.f + exp(-sum));
  712. }
  713. outptr[j] = sum;
  714. }
  715. outptr += outw;
  716. }
  717. }
  718. }
  719. }
  720. // packing
  721. if (num_output_g % 4 != 0 && out_elempack == 4)
  722. {
  723. convert_packing(top_blob_unpacked, top_blob, 4, opt);
  724. }
  725. else
  726. {
  727. top_blob = top_blob_unpacked;
  728. }
  729. return 0;
  730. } // opt.use_packing_layout
  731. #endif // __ARM_NEON
  732. // int8
  733. if (use_int8_inference)
  734. {
  735. if (use_int8_requantize)
  736. {
  737. Mat top_blob_tm;
  738. top_blob_tm.create(outw, outh, num_output, (size_t)4u, opt.workspace_allocator);
  739. if (top_blob_tm.empty())
  740. return -100;
  741. top_blob.create(outw, outh, num_output, (size_t)1u, opt.blob_allocator);
  742. if (top_blob.empty())
  743. return -100;
  744. // depth-wise
  745. if (channels == group && group == num_output)
  746. {
  747. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1)
  748. {
  749. if ((stride_w == 1 && stride_h == 1) || (stride_w == 2 && stride_h == 2))
  750. {
  751. if (stride_w == 1 && stride_h == 1)
  752. {
  753. convdw3x3s1_int8_requant_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, requantize_scales, opt);
  754. }
  755. else if (stride_w == 2 && stride_h == 2)
  756. {
  757. convdw3x3s2_int8_requant_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, requantize_scales, opt);
  758. }
  759. if (activation)
  760. {
  761. activation->forward_inplace(top_blob, opt);
  762. }
  763. return 0;
  764. }
  765. }
  766. #pragma omp parallel for num_threads(opt.num_threads)
  767. for (int g=0; g<group; g++)
  768. {
  769. const Mat bottom_blob_bordered_g = bottom_blob_bordered.channel_range(g, 1);
  770. Mat top_blob_tm_g = top_blob_tm.channel_range(g, 1);
  771. const ncnn::Layer* op = group_ops[g];
  772. ncnn::Option opt_g = opt;
  773. opt_g.num_threads = 1;
  774. opt_g.blob_allocator = top_blob.allocator;
  775. // forward
  776. op->forward(bottom_blob_bordered_g, top_blob_tm_g, opt_g);
  777. }
  778. if (activation)
  779. {
  780. activation->forward_inplace(top_blob, opt);
  781. }
  782. return 0;
  783. }
  784. const int channels_g = channels / group;
  785. const int num_output_g = num_output / group;
  786. #pragma omp parallel for num_threads(opt.num_threads)
  787. for (int g=0; g<group; g++)
  788. {
  789. const Mat bottom_blob_bordered_g = bottom_blob_bordered.channel_range(channels_g * g, channels_g);
  790. Mat top_blob_tm_g = top_blob_tm.channel_range(num_output_g * g, num_output_g);
  791. const ncnn::Layer* op = group_ops[g];
  792. ncnn::Option opt_g = opt;
  793. opt_g.blob_allocator = top_blob.allocator;
  794. // forward
  795. op->forward(bottom_blob_bordered_g, top_blob_tm_g, opt_g);
  796. }
  797. }
  798. else
  799. {
  800. top_blob.create(outw, outh, num_output, (size_t)4u, opt.blob_allocator);
  801. if (top_blob.empty())
  802. return -100;
  803. // depth-wise
  804. if (channels == group && group == num_output)
  805. {
  806. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1)
  807. {
  808. if ((stride_w == 1 && stride_h == 1) || (stride_w == 2 && stride_h == 2))
  809. {
  810. if (stride_w == 1 && stride_h == 1)
  811. {
  812. convdw3x3s1_int8_neon(bottom_blob_bordered, top_blob, weight_data, opt);
  813. }
  814. else if (stride_w == 2 && stride_h == 2)
  815. {
  816. convdw3x3s2_int8_neon(bottom_blob_bordered, top_blob, weight_data, opt);
  817. }
  818. // dequantize, reverse scale inplace
  819. #pragma omp parallel for num_threads(opt.num_threads)
  820. for (int g=0; g<group; g++)
  821. {
  822. ncnn::Option opt_g = opt;
  823. opt_g.num_threads = 1;
  824. opt_g.blob_allocator = top_blob.allocator;
  825. Mat top_blob_g = top_blob.channel(g);
  826. dequantize_ops[g]->forward_inplace(top_blob_g, opt_g);
  827. }
  828. if (activation)
  829. {
  830. activation->forward_inplace(top_blob, opt);
  831. }
  832. return 0;
  833. }
  834. }
  835. #pragma omp parallel for num_threads(opt.num_threads)
  836. for (int g=0; g<group; g++)
  837. {
  838. const Mat bottom_blob_bordered_g = bottom_blob_bordered.channel_range(g, 1);
  839. Mat top_blob_g = top_blob.channel_range(g, 1);
  840. const ncnn::Layer* op = group_ops[g];
  841. ncnn::Option opt_g = opt;
  842. opt_g.num_threads = 1;
  843. opt_g.blob_allocator = top_blob.allocator;
  844. // forward
  845. op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
  846. }
  847. if (activation)
  848. {
  849. activation->forward_inplace(top_blob, opt);
  850. }
  851. return 0;
  852. }
  853. const int channels_g = channels / group;
  854. const int num_output_g = num_output / group;
  855. #pragma omp parallel for num_threads(opt.num_threads)
  856. for (int g=0; g<group; g++)
  857. {
  858. const Mat bottom_blob_bordered_g = bottom_blob_bordered.channel_range(channels_g * g, channels_g);
  859. Mat top_blob_g = top_blob.channel_range(num_output_g * g, num_output_g);
  860. const ncnn::Layer* op = group_ops[g];
  861. ncnn::Option opt_g = opt;
  862. opt_g.blob_allocator = top_blob.allocator;
  863. // forward
  864. op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
  865. }
  866. }
  867. if (activation)
  868. {
  869. activation->forward_inplace(top_blob, opt);
  870. }
  871. return 0;
  872. }
  873. // float32
  874. top_blob.create(outw, outh, num_output, elemsize, opt.blob_allocator);
  875. if (top_blob.empty())
  876. return -100;
  877. // depth-wise
  878. if (channels == group && group == num_output)
  879. {
  880. if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1)
  881. {
  882. if (stride_w == 1 && stride_h == 1)
  883. {
  884. convdw3x3s1_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt);
  885. }
  886. else if (stride_w == 2 && stride_h == 2)
  887. {
  888. convdw3x3s2_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt);
  889. }
  890. if (activation)
  891. {
  892. activation->forward_inplace(top_blob, opt);
  893. }
  894. return 0;
  895. }
  896. if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1)
  897. {
  898. if (stride_w == 1 && stride_h == 1)
  899. {
  900. convdw5x5s1_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt);
  901. }
  902. else if (stride_w == 2 && stride_h == 2)
  903. {
  904. convdw5x5s2_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt);
  905. }
  906. if (activation)
  907. {
  908. activation->forward_inplace(top_blob, opt);
  909. }
  910. return 0;
  911. }
  912. #pragma omp parallel for num_threads(opt.num_threads)
  913. for (int g=0; g<group; g++)
  914. {
  915. const Mat bottom_blob_bordered_g = bottom_blob_bordered.channel_range(g, 1);
  916. Mat top_blob_g = top_blob.channel_range(g, 1);
  917. const ncnn::Layer* op = group_ops[g];
  918. ncnn::Option opt_g = opt;
  919. opt_g.num_threads = 1;
  920. opt_g.blob_allocator = top_blob.allocator;
  921. // forward
  922. op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
  923. }
  924. if (activation)
  925. {
  926. activation->forward_inplace(top_blob, opt);
  927. }
  928. return 0;
  929. }
  930. const int channels_g = channels / group;
  931. const int num_output_g = num_output / group;
  932. for (int g=0; g<group; g++)
  933. {
  934. const Mat bottom_blob_bordered_g = bottom_blob_bordered.channel_range(channels_g * g, channels_g);
  935. Mat top_blob_g = top_blob.channel_range(num_output_g * g, num_output_g);
  936. const ncnn::Layer* op = group_ops[g];
  937. ncnn::Option opt_g = opt;
  938. opt_g.blob_allocator = top_blob.allocator;
  939. // forward
  940. op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
  941. }
  942. if (activation)
  943. {
  944. activation->forward_inplace(top_blob, opt);
  945. }
  946. return 0;
  947. }
  948. } // namespace ncnn