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.

ncnnoptimize.cpp 85 kB

6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  1. // Copyright 2019 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #ifdef _MSC_VER
  4. #define _CRT_SECURE_NO_DEPRECATE
  5. #endif
  6. #include <algorithm>
  7. #include <map>
  8. #include <set>
  9. #include <vector>
  10. // ncnn public header
  11. #include "datareader.h"
  12. #include "layer.h"
  13. #include "layer_type.h"
  14. #include "net.h"
  15. // ncnn private header
  16. #include "modelwriter.h"
  17. class DataReaderFromEmpty : public ncnn::DataReader
  18. {
  19. public:
  20. virtual int scan(const char* format, void* p) const
  21. {
  22. return 0;
  23. }
  24. virtual size_t read(void* buf, size_t size) const
  25. {
  26. memset(buf, 0, size);
  27. return size;
  28. }
  29. };
  30. class NetOptimize : public ModelWriter
  31. {
  32. public:
  33. NetOptimize();
  34. public:
  35. int fuse_batchnorm_scale();
  36. int fuse_convolution_batchnorm();
  37. int fuse_convolution_mul();
  38. int fuse_convolution_add();
  39. int fuse_convolutiondepthwise_batchnorm();
  40. int fuse_convolutiondepthwise_mul();
  41. int fuse_convolutiondepthwise_add();
  42. int fuse_deconvolution_batchnorm();
  43. int fuse_deconvolution_mul();
  44. int fuse_deconvolution_add();
  45. int fuse_deconvolutiondepthwise_batchnorm();
  46. int fuse_innerproduct_batchnorm();
  47. int fuse_innerproduct_add();
  48. int fuse_innerproduct_dropout();
  49. int fuse_convolution_activation();
  50. int fuse_convolutiondepthwise_activation();
  51. int fuse_deconvolution_activation();
  52. int fuse_deconvolutiondepthwise_activation();
  53. int fuse_innerproduct_activation();
  54. int fuse_memorydata_binaryop();
  55. int fuse_binaryop_eltwise();
  56. int eliminate_dropout();
  57. int eliminate_pooling1x1();
  58. int eliminate_noop();
  59. int eliminate_split();
  60. int eliminate_orphaned_memorydata();
  61. int eliminate_flatten_after_global_pooling();
  62. int eliminate_reshape_after_global_pooling();
  63. int eliminate_flatten_after_innerproduct();
  64. int eliminate_reshape_before_binaryop();
  65. int replace_reduction_with_global_pooling();
  66. int replace_prelu_with_leaky_relu();
  67. int replace_convolution_with_innerproduct_after_global_pooling();
  68. int replace_convolution_with_innerproduct_after_innerproduct();
  69. };
  70. NetOptimize::NetOptimize()
  71. : ModelWriter()
  72. {
  73. }
  74. int NetOptimize::fuse_batchnorm_scale()
  75. {
  76. const size_t layer_count = layers.size();
  77. for (size_t i = 0; i < layer_count; i++)
  78. {
  79. if (layers[i]->type != "BatchNorm")
  80. continue;
  81. // BatchNorm - Scale
  82. int top_blob_index = layers[i]->tops[0];
  83. size_t j = i + 1;
  84. for (; j < layer_count; j++)
  85. {
  86. if (layers[j]->type != "Scale")
  87. continue;
  88. if (layers[j]->bottoms.size() != 1)
  89. continue;
  90. if (layers[j]->bottoms[0] == top_blob_index)
  91. break;
  92. }
  93. if (j == layer_count)
  94. continue;
  95. // fuse BatchNorm - Scale to BatchNorm
  96. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[i];
  97. ncnn::Scale* scale = (ncnn::Scale*)layers[j];
  98. fprintf(stderr, "fuse_batchnorm_scale %s %s\n", batchnorm->name.c_str(), scale->name.c_str());
  99. {
  100. // v = ((v - mean) / sqrt(var + eps) * slope + bias) * s + b
  101. // = (v - mean) / sqrt(var + eps) * (slope * s) + (bias * s + b)
  102. int channels = batchnorm->channels;
  103. float* slope = batchnorm->slope_data;
  104. float* bias = batchnorm->bias_data;
  105. for (int q = 0; q < channels; q++)
  106. {
  107. slope[q] = slope[q] * scale->scale_data[q];
  108. if (scale->bias_term)
  109. bias[q] = bias[q] * scale->scale_data[q] + scale->bias_data[q];
  110. else
  111. bias[q] = bias[q] * scale->scale_data[q];
  112. }
  113. }
  114. int top_blob_index_final = scale->tops[0];
  115. batchnorm->tops[0] = top_blob_index_final;
  116. blobs[top_blob_index_final].producer = i;
  117. scale->type = "ncnnfused";
  118. }
  119. return 0;
  120. }
  121. int NetOptimize::fuse_convolution_batchnorm()
  122. {
  123. const size_t layer_count = layers.size();
  124. for (size_t i = 0; i < layer_count; i++)
  125. {
  126. if (layers[i]->type != "Convolution")
  127. continue;
  128. // Convolution - BatchNorm
  129. int top_blob_index = layers[i]->tops[0];
  130. size_t j = i + 1;
  131. for (; j < layer_count; j++)
  132. {
  133. if (layers[j]->type != "BatchNorm")
  134. continue;
  135. if (layers[j]->bottoms.size() != 1)
  136. continue;
  137. if (layers[j]->bottoms[0] == top_blob_index)
  138. break;
  139. }
  140. if (j == layer_count)
  141. continue;
  142. // fuse Convolution - BatchNorm to Convolution
  143. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  144. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  145. fprintf(stderr, "fuse_convolution_batchnorm %s %s\n", convolution->name.c_str(), batchnorm->name.c_str());
  146. {
  147. int channels = batchnorm->channels;
  148. float eps = batchnorm->eps;
  149. // a = bias - slope * mean / sqrt(var + eps)
  150. // b = slope / sqrt(var + eps)
  151. // value = value * b + a
  152. std::vector<float> a(channels);
  153. std::vector<float> b(channels);
  154. for (int i = 0; i < channels; i++)
  155. {
  156. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  157. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  158. b[i] = batchnorm->slope_data[i] / sqrt_var;
  159. }
  160. if (convolution->bias_term == 0)
  161. {
  162. // init bias as zero
  163. convolution->bias_term = 1;
  164. convolution->bias_data = ncnn::Mat(channels);
  165. convolution->bias_data.fill(0.f);
  166. }
  167. const int weight_per_outch = convolution->weight_data_size / channels;
  168. float* weight = convolution->weight_data;
  169. float* bias = convolution->bias_data;
  170. for (int i = 0; i < channels; i++)
  171. {
  172. float* conv_weight_outch = weight + weight_per_outch * i;
  173. for (int j = 0; j < weight_per_outch; j++)
  174. {
  175. conv_weight_outch[j] *= b[i];
  176. }
  177. bias[i] = bias[i] * b[i] + a[i];
  178. }
  179. }
  180. int top_blob_index_final = batchnorm->tops[0];
  181. convolution->tops[0] = top_blob_index_final;
  182. blobs[top_blob_index_final].producer = i;
  183. batchnorm->type = "ncnnfused";
  184. }
  185. return 0;
  186. }
  187. int NetOptimize::fuse_convolution_mul()
  188. {
  189. const size_t layer_count = layers.size();
  190. for (size_t i = 0; i < layer_count; i++)
  191. {
  192. if (layers[i]->type != "Convolution")
  193. continue;
  194. // Convolution - BinaryOp
  195. int top_blob_index = layers[i]->tops[0];
  196. size_t j = i + 1;
  197. for (; j < layer_count; j++)
  198. {
  199. if (layers[j]->type != "BinaryOp")
  200. continue;
  201. if (layers[j]->bottoms.size() != 2)
  202. continue;
  203. if (layers[j]->bottoms[0] == top_blob_index)
  204. break;
  205. }
  206. if (j == layer_count)
  207. continue;
  208. // fuse Convolution - BinaryOp to Convolution
  209. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  210. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  211. if (binaryop->op_type != 2 || binaryop->with_scalar)
  212. continue;
  213. // MemoryData - ..... - BinaryOp
  214. size_t k = 0;
  215. for (; k < j; k++)
  216. {
  217. if (layers[k]->type != "MemoryData")
  218. continue;
  219. if (layers[k]->tops[0] == binaryop->bottoms[1])
  220. break;
  221. }
  222. if (k == j)
  223. continue;
  224. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  225. int channels = convolution->num_output;
  226. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  227. {
  228. // not bias-like broadcasting type
  229. continue;
  230. }
  231. fprintf(stderr, "fuse_convolution_mul %s %s\n", convolution->name.c_str(), binaryop->name.c_str());
  232. {
  233. const int weight_per_outch = convolution->weight_data_size / channels;
  234. float* weight = convolution->weight_data;
  235. float* bias = convolution->bias_data;
  236. for (int i = 0; i < channels; i++)
  237. {
  238. float* conv_weight_outch = weight + weight_per_outch * i;
  239. for (int j = 0; j < weight_per_outch; j++)
  240. {
  241. conv_weight_outch[j] *= memorydata->data[i];
  242. }
  243. if (bias)
  244. {
  245. bias[i] = bias[i] * memorydata->data[i];
  246. }
  247. }
  248. }
  249. int top_blob_index_final = binaryop->tops[0];
  250. convolution->tops[0] = top_blob_index_final;
  251. blobs[top_blob_index_final].producer = i;
  252. binaryop->type = "ncnnfused";
  253. }
  254. return 0;
  255. }
  256. int NetOptimize::fuse_convolution_add()
  257. {
  258. const size_t layer_count = layers.size();
  259. for (size_t i = 0; i < layer_count; i++)
  260. {
  261. if (layers[i]->type != "Convolution")
  262. continue;
  263. // Convolution - BinaryOp
  264. int top_blob_index = layers[i]->tops[0];
  265. size_t j = i + 1;
  266. for (; j < layer_count; j++)
  267. {
  268. if (layers[j]->type != "BinaryOp")
  269. continue;
  270. if (layers[j]->bottoms.size() != 2)
  271. continue;
  272. if (layers[j]->bottoms[0] == top_blob_index)
  273. break;
  274. }
  275. if (j == layer_count)
  276. continue;
  277. // fuse Convolution - BinaryOp to Convolution
  278. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  279. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  280. if (binaryop->op_type != 0 || binaryop->with_scalar)
  281. continue;
  282. // MemoryData - ..... - BinaryOp
  283. size_t k = 0;
  284. for (; k < j; k++)
  285. {
  286. if (layers[k]->type != "MemoryData")
  287. continue;
  288. if (layers[k]->tops[0] == binaryop->bottoms[1])
  289. break;
  290. }
  291. if (k == j)
  292. continue;
  293. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  294. int channels = convolution->num_output;
  295. bool broadcasting_type_ok = false;
  296. if (memorydata->w == channels && memorydata->h == 0 && memorydata->c == 0)
  297. broadcasting_type_ok = true;
  298. if (memorydata->w == 1 && memorydata->h == 1 && memorydata->c == channels)
  299. broadcasting_type_ok = true;
  300. if (!broadcasting_type_ok)
  301. {
  302. // not bias-like broadcasting type
  303. continue;
  304. }
  305. fprintf(stderr, "fuse_convolution_add %s %s\n", convolution->name.c_str(), binaryop->name.c_str());
  306. ncnn::Mat bias_data = memorydata->data.reshape(channels);
  307. {
  308. if (convolution->bias_term == 0)
  309. {
  310. // init bias
  311. convolution->bias_term = 1;
  312. convolution->bias_data = bias_data;
  313. }
  314. else
  315. {
  316. float* bias = convolution->bias_data;
  317. for (int i = 0; i < channels; i++)
  318. {
  319. bias[i] = bias[i] + bias_data[i];
  320. }
  321. }
  322. }
  323. int top_blob_index_final = binaryop->tops[0];
  324. convolution->tops[0] = top_blob_index_final;
  325. blobs[top_blob_index_final].producer = i;
  326. binaryop->type = "ncnnfused";
  327. }
  328. return 0;
  329. }
  330. int NetOptimize::fuse_convolutiondepthwise_batchnorm()
  331. {
  332. const size_t layer_count = layers.size();
  333. for (size_t i = 0; i < layer_count; i++)
  334. {
  335. if (layers[i]->type != "ConvolutionDepthWise")
  336. continue;
  337. // ConvolutionDepthWise - BatchNorm
  338. int top_blob_index = layers[i]->tops[0];
  339. size_t j = i + 1;
  340. for (; j < layer_count; j++)
  341. {
  342. if (layers[j]->type != "BatchNorm")
  343. continue;
  344. if (layers[j]->bottoms.size() != 1)
  345. continue;
  346. if (layers[j]->bottoms[0] == top_blob_index)
  347. break;
  348. }
  349. if (j == layer_count)
  350. continue;
  351. // fuse ConvolutionDepthWise - BatchNorm to ConvolutionDepthWise
  352. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  353. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  354. fprintf(stderr, "fuse_convolutiondepthwise_batchnorm %s %s\n", convolutiondepthwise->name.c_str(), batchnorm->name.c_str());
  355. {
  356. int channels = batchnorm->channels;
  357. float eps = batchnorm->eps;
  358. // a = bias - slope * mean / sqrt(var + eps)
  359. // b = slope / sqrt(var + eps)
  360. // value = value * b + a
  361. std::vector<float> a(channels);
  362. std::vector<float> b(channels);
  363. for (int i = 0; i < channels; i++)
  364. {
  365. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  366. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  367. b[i] = batchnorm->slope_data[i] / sqrt_var;
  368. }
  369. if (convolutiondepthwise->bias_term == 0)
  370. {
  371. // init bias as zero
  372. convolutiondepthwise->bias_term = 1;
  373. convolutiondepthwise->bias_data = ncnn::Mat(channels);
  374. convolutiondepthwise->bias_data.fill(0.f);
  375. }
  376. const int weight_per_outch = convolutiondepthwise->weight_data_size / channels;
  377. float* weight = convolutiondepthwise->weight_data;
  378. float* bias = convolutiondepthwise->bias_data;
  379. for (int i = 0; i < channels; i++)
  380. {
  381. float* conv_weight_outch = weight + weight_per_outch * i;
  382. for (int j = 0; j < weight_per_outch; j++)
  383. {
  384. conv_weight_outch[j] *= b[i];
  385. }
  386. bias[i] = bias[i] * b[i] + a[i];
  387. }
  388. }
  389. int top_blob_index_final = batchnorm->tops[0];
  390. convolutiondepthwise->tops[0] = top_blob_index_final;
  391. blobs[top_blob_index_final].producer = i;
  392. batchnorm->type = "ncnnfused";
  393. }
  394. return 0;
  395. }
  396. int NetOptimize::fuse_convolutiondepthwise_mul()
  397. {
  398. const size_t layer_count = layers.size();
  399. for (size_t i = 0; i < layer_count; i++)
  400. {
  401. if (layers[i]->type != "ConvolutionDepthWise")
  402. continue;
  403. // ConvolutionDepthWise - BinaryOp
  404. int top_blob_index = layers[i]->tops[0];
  405. size_t j = i + 1;
  406. for (; j < layer_count; j++)
  407. {
  408. if (layers[j]->type != "BinaryOp")
  409. continue;
  410. if (layers[j]->bottoms.size() != 2)
  411. continue;
  412. if (layers[j]->bottoms[0] == top_blob_index)
  413. break;
  414. }
  415. if (j == layer_count)
  416. continue;
  417. // fuse ConvolutionDepthWise - BinaryOp to ConvolutionDepthWise
  418. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  419. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  420. if (binaryop->op_type != 2 || binaryop->with_scalar)
  421. continue;
  422. // MemoryData - ..... - BinaryOp
  423. size_t k = 0;
  424. for (; k < j; k++)
  425. {
  426. if (layers[k]->type != "MemoryData")
  427. continue;
  428. if (layers[k]->tops[0] == binaryop->bottoms[1])
  429. break;
  430. }
  431. if (k == j)
  432. continue;
  433. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  434. int channels = convolutiondepthwise->num_output;
  435. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  436. {
  437. // not bias-like broadcasting type
  438. continue;
  439. }
  440. fprintf(stderr, "fuse_convolutiondepthwise_mul %s %s\n", convolutiondepthwise->name.c_str(), binaryop->name.c_str());
  441. {
  442. const int weight_per_outch = convolutiondepthwise->weight_data_size / channels;
  443. float* weight = convolutiondepthwise->weight_data;
  444. float* bias = convolutiondepthwise->bias_data;
  445. for (int i = 0; i < channels; i++)
  446. {
  447. float* conv_weight_outch = weight + weight_per_outch * i;
  448. for (int j = 0; j < weight_per_outch; j++)
  449. {
  450. conv_weight_outch[j] *= memorydata->data[i];
  451. }
  452. if (bias)
  453. {
  454. bias[i] = bias[i] * memorydata->data[i];
  455. }
  456. }
  457. }
  458. int top_blob_index_final = binaryop->tops[0];
  459. convolutiondepthwise->tops[0] = top_blob_index_final;
  460. blobs[top_blob_index_final].producer = i;
  461. binaryop->type = "ncnnfused";
  462. }
  463. return 0;
  464. }
  465. int NetOptimize::fuse_convolutiondepthwise_add()
  466. {
  467. const size_t layer_count = layers.size();
  468. for (size_t i = 0; i < layer_count; i++)
  469. {
  470. if (layers[i]->type != "ConvolutionDepthWise")
  471. continue;
  472. // ConvolutionDepthWise - BinaryOp
  473. int top_blob_index = layers[i]->tops[0];
  474. size_t j = i + 1;
  475. for (; j < layer_count; j++)
  476. {
  477. if (layers[j]->type != "BinaryOp")
  478. continue;
  479. if (layers[j]->bottoms.size() != 2)
  480. continue;
  481. if (layers[j]->bottoms[0] == top_blob_index)
  482. break;
  483. }
  484. if (j == layer_count)
  485. continue;
  486. // fuse ConvolutionDepthWise - BinaryOp to ConvolutionDepthWise
  487. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  488. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  489. if (binaryop->op_type != 0 || binaryop->with_scalar)
  490. continue;
  491. // MemoryData - ..... - BinaryOp
  492. size_t k = 0;
  493. for (; k < j; k++)
  494. {
  495. if (layers[k]->type != "MemoryData")
  496. continue;
  497. if (layers[k]->tops[0] == binaryop->bottoms[1])
  498. break;
  499. }
  500. if (k == j)
  501. continue;
  502. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  503. int channels = convolutiondepthwise->num_output;
  504. bool broadcasting_type_ok = false;
  505. if (memorydata->w == channels && memorydata->h == 0 && memorydata->c == 0)
  506. broadcasting_type_ok = true;
  507. if (memorydata->w == 1 && memorydata->h == 1 && memorydata->c == channels)
  508. broadcasting_type_ok = true;
  509. if (!broadcasting_type_ok)
  510. {
  511. // not bias-like broadcasting type
  512. continue;
  513. }
  514. fprintf(stderr, "fuse_convolutiondepthwise_add %s %s\n", convolutiondepthwise->name.c_str(), binaryop->name.c_str());
  515. ncnn::Mat bias_data = memorydata->data.reshape(channels);
  516. {
  517. if (convolutiondepthwise->bias_term == 0)
  518. {
  519. // init bias
  520. convolutiondepthwise->bias_term = 1;
  521. convolutiondepthwise->bias_data = bias_data;
  522. }
  523. else
  524. {
  525. float* bias = convolutiondepthwise->bias_data;
  526. for (int i = 0; i < channels; i++)
  527. {
  528. bias[i] = bias[i] + bias_data[i];
  529. }
  530. }
  531. }
  532. int top_blob_index_final = binaryop->tops[0];
  533. convolutiondepthwise->tops[0] = top_blob_index_final;
  534. blobs[top_blob_index_final].producer = i;
  535. binaryop->type = "ncnnfused";
  536. }
  537. return 0;
  538. }
  539. int NetOptimize::fuse_deconvolution_batchnorm()
  540. {
  541. const size_t layer_count = layers.size();
  542. for (size_t i = 0; i < layer_count; i++)
  543. {
  544. if (layers[i]->type != "Deconvolution")
  545. continue;
  546. // Deconvolution - BatchNorm
  547. int top_blob_index = layers[i]->tops[0];
  548. size_t j = i + 1;
  549. for (; j < layer_count; j++)
  550. {
  551. if (layers[j]->type != "BatchNorm")
  552. continue;
  553. if (layers[j]->bottoms.size() != 1)
  554. continue;
  555. if (layers[j]->bottoms[0] == top_blob_index)
  556. break;
  557. }
  558. if (j == layer_count)
  559. continue;
  560. // fuse Deconvolution - BatchNorm to Deconvolution
  561. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  562. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  563. fprintf(stderr, "fuse_deconvolution_batchnorm %s %s\n", deconvolution->name.c_str(), batchnorm->name.c_str());
  564. {
  565. int channels = batchnorm->channels;
  566. float eps = batchnorm->eps;
  567. // a = bias - slope * mean / sqrt(var + eps)
  568. // b = slope / sqrt(var + eps)
  569. // value = value * b + a
  570. std::vector<float> a(channels);
  571. std::vector<float> b(channels);
  572. for (int i = 0; i < channels; i++)
  573. {
  574. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  575. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  576. b[i] = batchnorm->slope_data[i] / sqrt_var;
  577. }
  578. if (deconvolution->bias_term == 0)
  579. {
  580. // init bias as zero
  581. deconvolution->bias_term = 1;
  582. deconvolution->bias_data = ncnn::Mat(channels);
  583. deconvolution->bias_data.fill(0.f);
  584. }
  585. const int weight_per_outch = deconvolution->weight_data_size / channels;
  586. float* weight = deconvolution->weight_data;
  587. float* bias = deconvolution->bias_data;
  588. for (int i = 0; i < channels; i++)
  589. {
  590. float* conv_weight_outch = weight + weight_per_outch * i;
  591. for (int j = 0; j < weight_per_outch; j++)
  592. {
  593. conv_weight_outch[j] *= b[i];
  594. }
  595. bias[i] = bias[i] * b[i] + a[i];
  596. }
  597. }
  598. int top_blob_index_final = batchnorm->tops[0];
  599. deconvolution->tops[0] = top_blob_index_final;
  600. blobs[top_blob_index_final].producer = i;
  601. batchnorm->type = "ncnnfused";
  602. }
  603. return 0;
  604. }
  605. int NetOptimize::fuse_deconvolution_mul()
  606. {
  607. const size_t layer_count = layers.size();
  608. for (size_t i = 0; i < layer_count; i++)
  609. {
  610. if (layers[i]->type != "Deconvolution")
  611. continue;
  612. // Deconvolution - BinaryOp
  613. int top_blob_index = layers[i]->tops[0];
  614. size_t j = i + 1;
  615. for (; j < layer_count; j++)
  616. {
  617. if (layers[j]->type != "BinaryOp")
  618. continue;
  619. if (layers[j]->bottoms.size() != 2)
  620. continue;
  621. if (layers[j]->bottoms[0] == top_blob_index)
  622. break;
  623. }
  624. if (j == layer_count)
  625. continue;
  626. // fuse Deconvolution - BinaryOp to Deconvolution
  627. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  628. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  629. if (binaryop->op_type != 2 || binaryop->with_scalar)
  630. continue;
  631. // MemoryData - ..... - BinaryOp
  632. size_t k = 0;
  633. for (; k < j; k++)
  634. {
  635. if (layers[k]->type != "MemoryData")
  636. continue;
  637. if (layers[k]->tops[0] == binaryop->bottoms[1])
  638. break;
  639. }
  640. if (k == j)
  641. continue;
  642. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  643. int channels = deconvolution->num_output;
  644. if (memorydata->w != channels || memorydata->h != 0 || memorydata->c != 0)
  645. {
  646. // not bias-like broadcasting type
  647. continue;
  648. }
  649. fprintf(stderr, "fuse_deconvolution_mul %s %s\n", deconvolution->name.c_str(), binaryop->name.c_str());
  650. {
  651. const int weight_per_outch = deconvolution->weight_data_size / channels;
  652. float* weight = deconvolution->weight_data;
  653. float* bias = deconvolution->bias_data;
  654. for (int i = 0; i < channels; i++)
  655. {
  656. float* conv_weight_outch = weight + weight_per_outch * i;
  657. for (int j = 0; j < weight_per_outch; j++)
  658. {
  659. conv_weight_outch[j] *= memorydata->data[i];
  660. }
  661. if (bias)
  662. {
  663. bias[i] = bias[i] * memorydata->data[i];
  664. }
  665. }
  666. }
  667. int top_blob_index_final = binaryop->tops[0];
  668. deconvolution->tops[0] = top_blob_index_final;
  669. blobs[top_blob_index_final].producer = i;
  670. binaryop->type = "ncnnfused";
  671. }
  672. return 0;
  673. }
  674. int NetOptimize::fuse_deconvolution_add()
  675. {
  676. const size_t layer_count = layers.size();
  677. for (size_t i = 0; i < layer_count; i++)
  678. {
  679. if (layers[i]->type != "Deconvolution")
  680. continue;
  681. // Deconvolution - BinaryOp
  682. int top_blob_index = layers[i]->tops[0];
  683. size_t j = i + 1;
  684. for (; j < layer_count; j++)
  685. {
  686. if (layers[j]->type != "BinaryOp")
  687. continue;
  688. if (layers[j]->bottoms.size() != 2)
  689. continue;
  690. if (layers[j]->bottoms[0] == top_blob_index)
  691. break;
  692. }
  693. if (j == layer_count)
  694. continue;
  695. // fuse Deconvolution - BinaryOp to Deconvolution
  696. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  697. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  698. if (binaryop->op_type != 0 || binaryop->with_scalar)
  699. continue;
  700. // MemoryData - ..... - BinaryOp
  701. size_t k = 0;
  702. for (; k < j; k++)
  703. {
  704. if (layers[k]->type != "MemoryData")
  705. continue;
  706. if (layers[k]->tops[0] == binaryop->bottoms[1])
  707. break;
  708. }
  709. if (k == j)
  710. continue;
  711. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  712. int channels = deconvolution->num_output;
  713. bool broadcasting_type_ok = false;
  714. if (memorydata->w == channels && memorydata->h == 0 && memorydata->c == 0)
  715. broadcasting_type_ok = true;
  716. if (memorydata->w == 1 && memorydata->h == 1 && memorydata->c == channels)
  717. broadcasting_type_ok = true;
  718. if (!broadcasting_type_ok)
  719. {
  720. // not bias-like broadcasting type
  721. continue;
  722. }
  723. fprintf(stderr, "fuse_deconvolution_add %s %s\n", deconvolution->name.c_str(), binaryop->name.c_str());
  724. ncnn::Mat bias_data = memorydata->data.reshape(channels);
  725. {
  726. if (deconvolution->bias_term == 0)
  727. {
  728. // init bias
  729. deconvolution->bias_term = 1;
  730. deconvolution->bias_data = bias_data;
  731. }
  732. else
  733. {
  734. float* bias = deconvolution->bias_data;
  735. for (int i = 0; i < channels; i++)
  736. {
  737. bias[i] = bias[i] + bias_data[i];
  738. }
  739. }
  740. }
  741. int top_blob_index_final = binaryop->tops[0];
  742. deconvolution->tops[0] = top_blob_index_final;
  743. blobs[top_blob_index_final].producer = i;
  744. binaryop->type = "ncnnfused";
  745. }
  746. return 0;
  747. }
  748. int NetOptimize::fuse_deconvolutiondepthwise_batchnorm()
  749. {
  750. const size_t layer_count = layers.size();
  751. for (size_t i = 0; i < layer_count; i++)
  752. {
  753. if (layers[i]->type != "DeconvolutionDepthWise")
  754. continue;
  755. // DeconvolutionDepthWise - BatchNorm
  756. int top_blob_index = layers[i]->tops[0];
  757. size_t j = i + 1;
  758. for (; j < layer_count; j++)
  759. {
  760. if (layers[j]->type != "BatchNorm")
  761. continue;
  762. if (layers[j]->bottoms.size() != 1)
  763. continue;
  764. if (layers[j]->bottoms[0] == top_blob_index)
  765. break;
  766. }
  767. if (j == layer_count)
  768. continue;
  769. // fuse DeconvolutionDepthWise - BatchNorm to DeconvolutionDepthWise
  770. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  771. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  772. fprintf(stderr, "fuse_deconvolutiondepthwise_batchnorm %s %s\n", deconvolutiondepthwise->name.c_str(), batchnorm->name.c_str());
  773. {
  774. int channels = batchnorm->channels;
  775. float eps = batchnorm->eps;
  776. // a = bias - slope * mean / sqrt(var + eps)
  777. // b = slope / sqrt(var + eps)
  778. // value = value * b + a
  779. std::vector<float> a(channels);
  780. std::vector<float> b(channels);
  781. for (int i = 0; i < channels; i++)
  782. {
  783. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  784. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  785. b[i] = batchnorm->slope_data[i] / sqrt_var;
  786. }
  787. if (deconvolutiondepthwise->bias_term == 0)
  788. {
  789. // init bias as zero
  790. deconvolutiondepthwise->bias_term = 1;
  791. deconvolutiondepthwise->bias_data = ncnn::Mat(channels);
  792. deconvolutiondepthwise->bias_data.fill(0.f);
  793. }
  794. const int weight_per_outch = deconvolutiondepthwise->weight_data_size / channels;
  795. float* weight = deconvolutiondepthwise->weight_data;
  796. float* bias = deconvolutiondepthwise->bias_data;
  797. for (int i = 0; i < channels; i++)
  798. {
  799. float* conv_weight_outch = weight + weight_per_outch * i;
  800. for (int j = 0; j < weight_per_outch; j++)
  801. {
  802. conv_weight_outch[j] *= b[i];
  803. }
  804. bias[i] = bias[i] * b[i] + a[i];
  805. }
  806. }
  807. int top_blob_index_final = batchnorm->tops[0];
  808. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  809. blobs[top_blob_index_final].producer = i;
  810. batchnorm->type = "ncnnfused";
  811. }
  812. return 0;
  813. }
  814. int NetOptimize::fuse_innerproduct_batchnorm()
  815. {
  816. const size_t layer_count = layers.size();
  817. for (size_t i = 0; i < layer_count; i++)
  818. {
  819. if (layers[i]->type != "InnerProduct")
  820. continue;
  821. // InnerProduct - BatchNorm
  822. int top_blob_index = layers[i]->tops[0];
  823. size_t j = i + 1;
  824. for (; j < layer_count; j++)
  825. {
  826. if (layers[j]->type != "BatchNorm")
  827. continue;
  828. if (layers[j]->bottoms.size() != 1)
  829. continue;
  830. if (layers[j]->bottoms[0] == top_blob_index)
  831. break;
  832. }
  833. if (j == layer_count)
  834. continue;
  835. // fuse InnerProduct - BatchNorm to InnerProduct
  836. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  837. ncnn::BatchNorm* batchnorm = (ncnn::BatchNorm*)layers[j];
  838. fprintf(stderr, "fuse_innerproduct_batchnorm %s %s\n", innerproduct->name.c_str(), batchnorm->name.c_str());
  839. {
  840. int channels = batchnorm->channels;
  841. float eps = batchnorm->eps;
  842. // a = bias - slope * mean / sqrt(var + eps)
  843. // b = slope / sqrt(var + eps)
  844. // value = value * b + a
  845. std::vector<float> a(channels);
  846. std::vector<float> b(channels);
  847. for (int i = 0; i < channels; i++)
  848. {
  849. float sqrt_var = static_cast<float>(sqrt(batchnorm->var_data[i] + eps));
  850. a[i] = batchnorm->bias_data[i] - batchnorm->slope_data[i] * batchnorm->mean_data[i] / sqrt_var;
  851. b[i] = batchnorm->slope_data[i] / sqrt_var;
  852. }
  853. if (innerproduct->bias_term == 0)
  854. {
  855. // init bias as zero
  856. innerproduct->bias_term = 1;
  857. innerproduct->bias_data = ncnn::Mat(channels);
  858. innerproduct->bias_data.fill(0.f);
  859. }
  860. const int weight_per_outch = innerproduct->weight_data_size / channels;
  861. float* weight = innerproduct->weight_data;
  862. float* bias = innerproduct->bias_data;
  863. for (int i = 0; i < channels; i++)
  864. {
  865. float* conv_weight_outch = weight + weight_per_outch * i;
  866. for (int j = 0; j < weight_per_outch; j++)
  867. {
  868. conv_weight_outch[j] *= b[i];
  869. }
  870. bias[i] = bias[i] * b[i] + a[i];
  871. }
  872. }
  873. int top_blob_index_final = batchnorm->tops[0];
  874. innerproduct->tops[0] = top_blob_index_final;
  875. blobs[top_blob_index_final].producer = i;
  876. batchnorm->type = "ncnnfused";
  877. }
  878. return 0;
  879. }
  880. int NetOptimize::fuse_innerproduct_add()
  881. {
  882. const size_t layer_count = layers.size();
  883. for (size_t i = 0; i < layer_count; i++)
  884. {
  885. if (layers[i]->type != "InnerProduct")
  886. continue;
  887. // InnerProduct - BinaryOp
  888. int top_blob_index = layers[i]->tops[0];
  889. size_t j = i + 1;
  890. for (; j < layer_count; j++)
  891. {
  892. if (layers[j]->type != "BinaryOp")
  893. continue;
  894. if (layers[j]->bottoms.size() != 2)
  895. continue;
  896. if (layers[j]->bottoms[0] == top_blob_index)
  897. break;
  898. }
  899. if (j == layer_count)
  900. continue;
  901. // fuse InnerProduct - BinaryOp to InnerProduct
  902. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  903. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  904. if (binaryop->op_type != 0 || binaryop->with_scalar)
  905. continue;
  906. // MemoryData - ..... - BinaryOp
  907. size_t k = 0;
  908. for (; k < j; k++)
  909. {
  910. if (layers[k]->type != "MemoryData")
  911. continue;
  912. if (layers[k]->tops[0] == binaryop->bottoms[1])
  913. break;
  914. }
  915. if (k == j)
  916. continue;
  917. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[k];
  918. int channels = innerproduct->num_output;
  919. bool broadcasting_type_ok = false;
  920. if (memorydata->w == channels && memorydata->h == 0 && memorydata->c == 0)
  921. broadcasting_type_ok = true;
  922. if (memorydata->w == 1 && memorydata->h == 1 && memorydata->c == channels)
  923. broadcasting_type_ok = true;
  924. if (!broadcasting_type_ok)
  925. {
  926. // not bias-like broadcasting type
  927. continue;
  928. }
  929. fprintf(stderr, "fuse_innerproduct_add %s %s\n", innerproduct->name.c_str(), binaryop->name.c_str());
  930. ncnn::Mat bias_data = memorydata->data.reshape(channels);
  931. {
  932. if (innerproduct->bias_term == 0)
  933. {
  934. // init bias
  935. innerproduct->bias_term = 1;
  936. innerproduct->bias_data = bias_data;
  937. }
  938. else
  939. {
  940. float* bias = innerproduct->bias_data;
  941. for (int i = 0; i < channels; i++)
  942. {
  943. bias[i] = bias[i] + bias_data[i];
  944. }
  945. }
  946. }
  947. int top_blob_index_final = binaryop->tops[0];
  948. innerproduct->tops[0] = top_blob_index_final;
  949. blobs[top_blob_index_final].producer = i;
  950. binaryop->type = "ncnnfused";
  951. }
  952. return 0;
  953. }
  954. int NetOptimize::fuse_innerproduct_dropout()
  955. {
  956. const size_t layer_count = layers.size();
  957. for (size_t i = 0; i < layer_count; i++)
  958. {
  959. if (layers[i]->type != "InnerProduct")
  960. continue;
  961. // InnerProduct - Dropout
  962. int top_blob_index = layers[i]->tops[0];
  963. size_t j = i + 1;
  964. for (; j < layer_count; j++)
  965. {
  966. if (layers[j]->type != "Dropout")
  967. continue;
  968. if (layers[j]->bottoms.size() != 1)
  969. continue;
  970. if (layers[j]->bottoms[0] == top_blob_index)
  971. break;
  972. }
  973. if (j == layer_count)
  974. continue;
  975. // fuse InnerProduct - Dropout to InnerProduct
  976. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  977. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[j];
  978. fprintf(stderr, "fuse_innerproduct_dropout %s %s\n", innerproduct->name.c_str(), dropout->name.c_str());
  979. float scale = dropout->scale;
  980. if (scale != 1.f)
  981. {
  982. const int num_output = innerproduct->num_output;
  983. const int weight_per_outch = innerproduct->weight_data_size / num_output;
  984. float* weight = innerproduct->weight_data;
  985. for (int i = 0; i < num_output; i++)
  986. {
  987. float* conv_weight_outch = weight + weight_per_outch * i;
  988. for (int j = 0; j < weight_per_outch; j++)
  989. {
  990. conv_weight_outch[j] *= scale;
  991. }
  992. }
  993. if (innerproduct->bias_term)
  994. {
  995. float* bias = innerproduct->bias_data;
  996. for (int i = 0; i < num_output; i++)
  997. {
  998. bias[i] *= scale;
  999. }
  1000. }
  1001. }
  1002. int top_blob_index_final = dropout->tops[0];
  1003. innerproduct->tops[0] = top_blob_index_final;
  1004. blobs[top_blob_index_final].producer = i;
  1005. dropout->type = "ncnnfused";
  1006. }
  1007. return 0;
  1008. }
  1009. int NetOptimize::fuse_convolution_activation()
  1010. {
  1011. const size_t layer_count = layers.size();
  1012. for (size_t i = 0; i < layer_count; i++)
  1013. {
  1014. if (layers[i]->type != "Convolution")
  1015. continue;
  1016. // Convolution - Activation
  1017. int top_blob_index = layers[i]->tops[0];
  1018. size_t j = i + 1;
  1019. for (; j < layer_count; j++)
  1020. {
  1021. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish" && layers[j]->type != "HardSwish")
  1022. continue;
  1023. if (layers[j]->bottoms.size() != 1)
  1024. continue;
  1025. if (layers[j]->bottoms[0] == top_blob_index)
  1026. break;
  1027. }
  1028. if (j == layer_count)
  1029. continue;
  1030. // fuse Convolution - Activation to Convolution
  1031. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  1032. ncnn::Layer* activation = layers[j];
  1033. fprintf(stderr, "fuse_convolution_activation %s %s\n", convolution->name.c_str(), activation->name.c_str());
  1034. if (activation->type == "ReLU")
  1035. {
  1036. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1037. if (relu->slope == 0.f)
  1038. {
  1039. convolution->activation_type = 1;
  1040. }
  1041. else
  1042. {
  1043. convolution->activation_type = 2;
  1044. convolution->activation_params = ncnn::Mat(1);
  1045. convolution->activation_params[0] = relu->slope;
  1046. }
  1047. }
  1048. else if (activation->type == "Clip")
  1049. {
  1050. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1051. convolution->activation_type = 3;
  1052. convolution->activation_params = ncnn::Mat(2);
  1053. convolution->activation_params[0] = clip->min;
  1054. convolution->activation_params[1] = clip->max;
  1055. }
  1056. else if (activation->type == "Sigmoid")
  1057. {
  1058. convolution->activation_type = 4;
  1059. }
  1060. else if (activation->type == "Mish")
  1061. {
  1062. convolution->activation_type = 5;
  1063. }
  1064. else if (activation->type == "HardSwish")
  1065. {
  1066. ncnn::HardSwish* hardswish = (ncnn::HardSwish*)activation;
  1067. convolution->activation_type = 6;
  1068. convolution->activation_params = ncnn::Mat(2);
  1069. convolution->activation_params[0] = hardswish->alpha;
  1070. convolution->activation_params[1] = hardswish->beta;
  1071. }
  1072. int top_blob_index_final = activation->tops[0];
  1073. convolution->tops[0] = top_blob_index_final;
  1074. blobs[top_blob_index_final].producer = i;
  1075. activation->type = "ncnnfused";
  1076. }
  1077. for (size_t i = 0; i < layer_count; i++)
  1078. {
  1079. if (layers[i]->type != "Convolution1D")
  1080. continue;
  1081. // Convolution1D - Activation
  1082. int top_blob_index = layers[i]->tops[0];
  1083. size_t j = i + 1;
  1084. for (; j < layer_count; j++)
  1085. {
  1086. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish")
  1087. continue;
  1088. if (layers[j]->bottoms.size() != 1)
  1089. continue;
  1090. if (layers[j]->bottoms[0] == top_blob_index)
  1091. break;
  1092. }
  1093. if (j == layer_count)
  1094. continue;
  1095. // fuse Convolution1D - Activation to Convolution1D
  1096. ncnn::Convolution1D* convolution = (ncnn::Convolution1D*)layers[i];
  1097. ncnn::Layer* activation = layers[j];
  1098. fprintf(stderr, "fuse_convolution1d_activation %s %s\n", convolution->name.c_str(), activation->name.c_str());
  1099. if (activation->type == "ReLU")
  1100. {
  1101. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1102. if (relu->slope == 0.f)
  1103. {
  1104. convolution->activation_type = 1;
  1105. }
  1106. else
  1107. {
  1108. convolution->activation_type = 2;
  1109. convolution->activation_params = ncnn::Mat(1);
  1110. convolution->activation_params[0] = relu->slope;
  1111. }
  1112. }
  1113. else if (activation->type == "Clip")
  1114. {
  1115. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1116. convolution->activation_type = 3;
  1117. convolution->activation_params = ncnn::Mat(2);
  1118. convolution->activation_params[0] = clip->min;
  1119. convolution->activation_params[1] = clip->max;
  1120. }
  1121. else if (activation->type == "Sigmoid")
  1122. {
  1123. convolution->activation_type = 4;
  1124. }
  1125. else if (activation->type == "Mish")
  1126. {
  1127. convolution->activation_type = 5;
  1128. }
  1129. int top_blob_index_final = activation->tops[0];
  1130. convolution->tops[0] = top_blob_index_final;
  1131. blobs[top_blob_index_final].producer = i;
  1132. activation->type = "ncnnfused";
  1133. }
  1134. return 0;
  1135. }
  1136. int NetOptimize::fuse_convolutiondepthwise_activation()
  1137. {
  1138. const size_t layer_count = layers.size();
  1139. for (size_t i = 0; i < layer_count; i++)
  1140. {
  1141. if (layers[i]->type != "ConvolutionDepthWise")
  1142. continue;
  1143. // ConvolutionDepthWise - Activation
  1144. int top_blob_index = layers[i]->tops[0];
  1145. size_t j = i + 1;
  1146. for (; j < layer_count; j++)
  1147. {
  1148. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish" && layers[j]->type != "HardSwish")
  1149. continue;
  1150. if (layers[j]->bottoms.size() != 1)
  1151. continue;
  1152. if (layers[j]->bottoms[0] == top_blob_index)
  1153. break;
  1154. }
  1155. if (j == layer_count)
  1156. continue;
  1157. // fuse ConvolutionDepthWise - Activation to ConvolutionDepthWise
  1158. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  1159. ncnn::Layer* activation = layers[j];
  1160. fprintf(stderr, "fuse_convolutiondepthwise_activation %s %s\n", convolutiondepthwise->name.c_str(), activation->name.c_str());
  1161. if (activation->type == "ReLU")
  1162. {
  1163. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1164. if (relu->slope == 0.f)
  1165. {
  1166. convolutiondepthwise->activation_type = 1;
  1167. }
  1168. else
  1169. {
  1170. convolutiondepthwise->activation_type = 2;
  1171. convolutiondepthwise->activation_params = ncnn::Mat(1);
  1172. convolutiondepthwise->activation_params[0] = relu->slope;
  1173. }
  1174. }
  1175. else if (activation->type == "Clip")
  1176. {
  1177. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1178. convolutiondepthwise->activation_type = 3;
  1179. convolutiondepthwise->activation_params = ncnn::Mat(2);
  1180. convolutiondepthwise->activation_params[0] = clip->min;
  1181. convolutiondepthwise->activation_params[1] = clip->max;
  1182. }
  1183. else if (activation->type == "Sigmoid")
  1184. {
  1185. convolutiondepthwise->activation_type = 4;
  1186. }
  1187. else if (activation->type == "Mish")
  1188. {
  1189. convolutiondepthwise->activation_type = 5;
  1190. }
  1191. else if (activation->type == "HardSwish")
  1192. {
  1193. ncnn::HardSwish* hardswish = (ncnn::HardSwish*)activation;
  1194. convolutiondepthwise->activation_type = 6;
  1195. convolutiondepthwise->activation_params = ncnn::Mat(2);
  1196. convolutiondepthwise->activation_params[0] = hardswish->alpha;
  1197. convolutiondepthwise->activation_params[1] = hardswish->beta;
  1198. }
  1199. int top_blob_index_final = activation->tops[0];
  1200. convolutiondepthwise->tops[0] = top_blob_index_final;
  1201. blobs[top_blob_index_final].producer = i;
  1202. activation->type = "ncnnfused";
  1203. }
  1204. return 0;
  1205. }
  1206. int NetOptimize::fuse_deconvolution_activation()
  1207. {
  1208. const size_t layer_count = layers.size();
  1209. for (size_t i = 0; i < layer_count; i++)
  1210. {
  1211. if (layers[i]->type != "Deconvolution")
  1212. continue;
  1213. // Deconvolution - Activation
  1214. int top_blob_index = layers[i]->tops[0];
  1215. size_t j = i + 1;
  1216. for (; j < layer_count; j++)
  1217. {
  1218. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1219. continue;
  1220. if (layers[j]->bottoms.size() != 1)
  1221. continue;
  1222. if (layers[j]->bottoms[0] == top_blob_index)
  1223. break;
  1224. }
  1225. if (j == layer_count)
  1226. continue;
  1227. // fuse Deconvolution - Activation to Deconvolution
  1228. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  1229. ncnn::Layer* activation = layers[j];
  1230. fprintf(stderr, "fuse_deconvolution_activation %s %s\n", deconvolution->name.c_str(), activation->name.c_str());
  1231. if (activation->type == "ReLU")
  1232. {
  1233. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1234. if (relu->slope == 0.f)
  1235. {
  1236. deconvolution->activation_type = 1;
  1237. }
  1238. else
  1239. {
  1240. deconvolution->activation_type = 2;
  1241. deconvolution->activation_params = ncnn::Mat(1);
  1242. deconvolution->activation_params[0] = relu->slope;
  1243. }
  1244. }
  1245. else if (activation->type == "Clip")
  1246. {
  1247. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1248. deconvolution->activation_type = 3;
  1249. deconvolution->activation_params = ncnn::Mat(2);
  1250. deconvolution->activation_params[0] = clip->min;
  1251. deconvolution->activation_params[1] = clip->max;
  1252. }
  1253. else if (activation->type == "Sigmoid")
  1254. {
  1255. deconvolution->activation_type = 4;
  1256. }
  1257. int top_blob_index_final = activation->tops[0];
  1258. deconvolution->tops[0] = top_blob_index_final;
  1259. blobs[top_blob_index_final].producer = i;
  1260. activation->type = "ncnnfused";
  1261. }
  1262. return 0;
  1263. }
  1264. int NetOptimize::fuse_deconvolutiondepthwise_activation()
  1265. {
  1266. const size_t layer_count = layers.size();
  1267. for (size_t i = 0; i < layer_count; i++)
  1268. {
  1269. if (layers[i]->type != "DeconvolutionDepthWise")
  1270. continue;
  1271. // DeconvolutionDepthWise - Activation
  1272. int top_blob_index = layers[i]->tops[0];
  1273. size_t j = i + 1;
  1274. for (; j < layer_count; j++)
  1275. {
  1276. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1277. continue;
  1278. if (layers[j]->bottoms.size() != 1)
  1279. continue;
  1280. if (layers[j]->bottoms[0] == top_blob_index)
  1281. break;
  1282. }
  1283. if (j == layer_count)
  1284. continue;
  1285. // fuse DeconvolutionDepthWise - Activation to DeconvolutionDepthWise
  1286. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  1287. ncnn::Layer* activation = layers[j];
  1288. fprintf(stderr, "fuse_deconvolutiondepthwise_activation %s %s\n", deconvolutiondepthwise->name.c_str(), activation->name.c_str());
  1289. if (activation->type == "ReLU")
  1290. {
  1291. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1292. if (relu->slope == 0.f)
  1293. {
  1294. deconvolutiondepthwise->activation_type = 1;
  1295. }
  1296. else
  1297. {
  1298. deconvolutiondepthwise->activation_type = 2;
  1299. deconvolutiondepthwise->activation_params = ncnn::Mat(1);
  1300. deconvolutiondepthwise->activation_params[0] = relu->slope;
  1301. }
  1302. }
  1303. else if (activation->type == "Clip")
  1304. {
  1305. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1306. deconvolutiondepthwise->activation_type = 3;
  1307. deconvolutiondepthwise->activation_params = ncnn::Mat(2);
  1308. deconvolutiondepthwise->activation_params[0] = clip->min;
  1309. deconvolutiondepthwise->activation_params[1] = clip->max;
  1310. }
  1311. else if (activation->type == "Sigmoid")
  1312. {
  1313. deconvolutiondepthwise->activation_type = 4;
  1314. }
  1315. int top_blob_index_final = activation->tops[0];
  1316. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  1317. blobs[top_blob_index_final].producer = i;
  1318. activation->type = "ncnnfused";
  1319. }
  1320. return 0;
  1321. }
  1322. int NetOptimize::fuse_innerproduct_activation()
  1323. {
  1324. const size_t layer_count = layers.size();
  1325. for (size_t i = 0; i < layer_count; i++)
  1326. {
  1327. if (layers[i]->type != "InnerProduct")
  1328. continue;
  1329. // InnerProduct - Activation
  1330. int top_blob_index = layers[i]->tops[0];
  1331. size_t j = i + 1;
  1332. for (; j < layer_count; j++)
  1333. {
  1334. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish" && layers[j]->type != "HardSwish")
  1335. continue;
  1336. if (layers[j]->bottoms.size() != 1)
  1337. continue;
  1338. if (layers[j]->bottoms[0] == top_blob_index)
  1339. break;
  1340. }
  1341. if (j == layer_count)
  1342. continue;
  1343. // fuse InnerProduct - Activation to InnerProduct
  1344. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1345. ncnn::Layer* activation = layers[j];
  1346. fprintf(stderr, "fuse_innerproduct_activation %s %s\n", innerproduct->name.c_str(), activation->name.c_str());
  1347. if (activation->type == "ReLU")
  1348. {
  1349. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1350. if (relu->slope == 0.f)
  1351. {
  1352. innerproduct->activation_type = 1;
  1353. }
  1354. else
  1355. {
  1356. innerproduct->activation_type = 2;
  1357. innerproduct->activation_params = ncnn::Mat(1);
  1358. innerproduct->activation_params[0] = relu->slope;
  1359. }
  1360. }
  1361. else if (activation->type == "Clip")
  1362. {
  1363. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1364. innerproduct->activation_type = 3;
  1365. innerproduct->activation_params = ncnn::Mat(2);
  1366. innerproduct->activation_params[0] = clip->min;
  1367. innerproduct->activation_params[1] = clip->max;
  1368. }
  1369. else if (activation->type == "Sigmoid")
  1370. {
  1371. innerproduct->activation_type = 4;
  1372. }
  1373. else if (activation->type == "Mish")
  1374. {
  1375. innerproduct->activation_type = 5;
  1376. }
  1377. else if (activation->type == "HardSwish")
  1378. {
  1379. ncnn::HardSwish* hardswish = (ncnn::HardSwish*)activation;
  1380. innerproduct->activation_type = 6;
  1381. innerproduct->activation_params = ncnn::Mat(2);
  1382. innerproduct->activation_params[0] = hardswish->alpha;
  1383. innerproduct->activation_params[1] = hardswish->beta;
  1384. }
  1385. int top_blob_index_final = activation->tops[0];
  1386. innerproduct->tops[0] = top_blob_index_final;
  1387. blobs[top_blob_index_final].producer = i;
  1388. activation->type = "ncnnfused";
  1389. }
  1390. return 0;
  1391. }
  1392. int NetOptimize::fuse_memorydata_binaryop()
  1393. {
  1394. const size_t layer_count = layers.size();
  1395. for (size_t i = 0; i < layer_count; i++)
  1396. {
  1397. if (layers[i]->type != "MemoryData")
  1398. continue;
  1399. // MemoryData - BinaryOp
  1400. int top_blob_index = layers[i]->tops[0];
  1401. size_t j = i + 1;
  1402. for (; j < layer_count; j++)
  1403. {
  1404. if (layers[j]->type != "BinaryOp")
  1405. continue;
  1406. if (layers[j]->bottoms.size() != 2)
  1407. continue;
  1408. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1409. break;
  1410. }
  1411. if (j == layer_count)
  1412. continue;
  1413. // fuse MemoryData - BinaryOp to BinaryOp
  1414. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[i];
  1415. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1416. if (memorydata->w != 1 || memorydata->h != 0 || memorydata->c != 0)
  1417. {
  1418. // not a scalar
  1419. continue;
  1420. }
  1421. int memorydata_index = 1;
  1422. if (binaryop->bottoms[0] == top_blob_index)
  1423. {
  1424. int op_type = binaryop->op_type;
  1425. if (op_type == ncnn::BinaryOp::Operation_ADD
  1426. || op_type == ncnn::BinaryOp::Operation_MUL
  1427. || op_type == ncnn::BinaryOp::Operation_MAX
  1428. || op_type == ncnn::BinaryOp::Operation_MIN)
  1429. {
  1430. memorydata_index = 0;
  1431. }
  1432. else if (op_type == ncnn::BinaryOp::Operation_SUB)
  1433. {
  1434. binaryop->op_type = ncnn::BinaryOp::Operation_RSUB;
  1435. memorydata_index = 0;
  1436. }
  1437. else if (op_type == ncnn::BinaryOp::Operation_DIV)
  1438. {
  1439. binaryop->op_type = ncnn::BinaryOp::Operation_RDIV;
  1440. memorydata_index = 0;
  1441. }
  1442. else
  1443. {
  1444. // non interchangeable binaryop
  1445. continue;
  1446. }
  1447. }
  1448. float scalar = memorydata->data[0];
  1449. binaryop->with_scalar = 1;
  1450. binaryop->b = scalar;
  1451. fprintf(stderr, "fuse_memorydata_binaryop %s %s\n", memorydata->name.c_str(), binaryop->name.c_str());
  1452. binaryop->bottoms.erase(binaryop->bottoms.begin() + memorydata_index);
  1453. memorydata->type = "ncnnfused";
  1454. }
  1455. for (size_t i = 0; i < layer_count; i++)
  1456. {
  1457. if (layers[i]->type != "MemoryData")
  1458. continue;
  1459. // MemoryData - Split - BinaryOp
  1460. int top_blob_index = layers[i]->tops[0];
  1461. size_t j0 = i + 1;
  1462. for (; j0 < layer_count; j0++)
  1463. {
  1464. if (layers[j0]->type != "Split")
  1465. continue;
  1466. if (layers[j0]->bottoms.size() != 1)
  1467. continue;
  1468. if (layers[j0]->bottoms[0] == top_blob_index)
  1469. break;
  1470. }
  1471. if (j0 == layer_count)
  1472. continue;
  1473. int split_top_blob_index = -1;
  1474. size_t j1 = j0 + 1;
  1475. for (; j1 < layer_count; j1++)
  1476. {
  1477. if (layers[j1]->type != "BinaryOp")
  1478. continue;
  1479. if (layers[j1]->bottoms.size() != 2)
  1480. continue;
  1481. for (int k = 0; k < (int)layers[j0]->tops.size(); k++)
  1482. {
  1483. if (layers[j1]->bottoms[0] == layers[j0]->tops[k] || layers[j1]->bottoms[1] == layers[j0]->tops[k])
  1484. {
  1485. split_top_blob_index = k;
  1486. break;
  1487. }
  1488. }
  1489. if (split_top_blob_index != -1)
  1490. break;
  1491. }
  1492. if (j1 == layer_count)
  1493. continue;
  1494. // fuse MemoryData - Split - BinaryOp to BinaryOp
  1495. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[i];
  1496. ncnn::Split* split = (ncnn::Split*)layers[j0];
  1497. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j1];
  1498. if (memorydata->w != 1 || memorydata->h != 0 || memorydata->c != 0)
  1499. {
  1500. // not a scalar
  1501. continue;
  1502. }
  1503. int memorydata_index = 1;
  1504. if (binaryop->bottoms[0] == split->tops[split_top_blob_index])
  1505. {
  1506. int op_type = binaryop->op_type;
  1507. if (op_type == ncnn::BinaryOp::Operation_ADD
  1508. || op_type == ncnn::BinaryOp::Operation_MUL
  1509. || op_type == ncnn::BinaryOp::Operation_MAX
  1510. || op_type == ncnn::BinaryOp::Operation_MIN)
  1511. {
  1512. memorydata_index = 0;
  1513. }
  1514. else if (op_type == ncnn::BinaryOp::Operation_SUB)
  1515. {
  1516. binaryop->op_type = ncnn::BinaryOp::Operation_RSUB;
  1517. memorydata_index = 0;
  1518. }
  1519. else if (op_type == ncnn::BinaryOp::Operation_DIV)
  1520. {
  1521. binaryop->op_type = ncnn::BinaryOp::Operation_RDIV;
  1522. memorydata_index = 0;
  1523. }
  1524. else
  1525. {
  1526. // non interchangeable binaryop
  1527. continue;
  1528. }
  1529. }
  1530. float scalar = memorydata->data[0];
  1531. binaryop->with_scalar = 1;
  1532. binaryop->b = scalar;
  1533. fprintf(stderr, "fuse_memorydata_binaryop %s %s\n", memorydata->name.c_str(), binaryop->name.c_str());
  1534. binaryop->bottoms.erase(binaryop->bottoms.begin() + memorydata_index);
  1535. split->tops.erase(split->tops.begin() + split_top_blob_index);
  1536. if (split->tops.empty())
  1537. {
  1538. split->type = "ncnnfused";
  1539. memorydata->type = "ncnnfused";
  1540. }
  1541. i--;
  1542. }
  1543. return 0;
  1544. }
  1545. int NetOptimize::fuse_binaryop_eltwise()
  1546. {
  1547. const size_t layer_count = layers.size();
  1548. for (size_t i = 0; i < layer_count; i++)
  1549. {
  1550. if (layers[i]->type != "BinaryOp")
  1551. continue;
  1552. if (layers[i]->bottoms.size() != 2)
  1553. continue;
  1554. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[i];
  1555. if (binaryop->op_type != ncnn::BinaryOp::Operation_ADD)
  1556. continue;
  1557. if (binaryop->with_scalar)
  1558. continue;
  1559. // BinaryOp - BinaryOp - BinaryOp
  1560. int bottom_blob_index_0 = binaryop->bottoms[0];
  1561. int bottom_blob_index_1 = binaryop->bottoms[1];
  1562. size_t j0 = 0;
  1563. for (; j0 < i; j0++)
  1564. {
  1565. if (layers[j0]->type != "BinaryOp")
  1566. continue;
  1567. if (layers[j0]->bottoms.size() != 1)
  1568. continue;
  1569. if (((ncnn::BinaryOp*)layers[j0])->op_type != ncnn::BinaryOp::Operation_MUL)
  1570. continue;
  1571. if (layers[j0]->tops[0] == bottom_blob_index_0)
  1572. break;
  1573. }
  1574. size_t j1 = 0;
  1575. for (; j1 < i; j1++)
  1576. {
  1577. if (layers[j1]->type != "BinaryOp")
  1578. continue;
  1579. if (layers[j1]->bottoms.size() != 1)
  1580. continue;
  1581. if (((ncnn::BinaryOp*)layers[j1])->op_type != ncnn::BinaryOp::Operation_MUL)
  1582. continue;
  1583. if (layers[j1]->tops[0] == bottom_blob_index_1)
  1584. break;
  1585. }
  1586. if (j0 == i && j1 == i)
  1587. continue;
  1588. ncnn::BinaryOp* binaryop0 = (ncnn::BinaryOp*)layers[j0];
  1589. ncnn::BinaryOp* binaryop1 = (ncnn::BinaryOp*)layers[j1];
  1590. fprintf(stderr, "fuse_binaryop_eltwise %s %s %s\n", binaryop0->name.c_str(), binaryop1->name.c_str(), binaryop->name.c_str());
  1591. ncnn::Eltwise* eltwise = (ncnn::Eltwise*)ncnn::create_layer_cpu("Eltwise");
  1592. eltwise->type = "Eltwise";
  1593. eltwise->name = binaryop->name;
  1594. eltwise->bottoms = binaryop->bottoms;
  1595. eltwise->tops = binaryop->tops;
  1596. ncnn::ParamDict pd;
  1597. eltwise->load_param(pd);
  1598. eltwise->op_type = ncnn::Eltwise::Operation_SUM;
  1599. eltwise->coeffs = ncnn::Mat(2);
  1600. if (j0 != i && j1 != i)
  1601. {
  1602. // fuse BinaryOp - BinaryOp - BinaryOp to Eltwise
  1603. eltwise->coeffs[0] = binaryop0->b;
  1604. eltwise->coeffs[1] = binaryop1->b;
  1605. eltwise->bottoms[0] = binaryop0->bottoms[0];
  1606. eltwise->bottoms[1] = binaryop1->bottoms[0];
  1607. binaryop0->type = "ncnnfused";
  1608. binaryop1->type = "ncnnfused";
  1609. }
  1610. if (j0 != i && j1 == i)
  1611. {
  1612. // fuse BinaryOp - X - BinaryOp to Eltwise
  1613. eltwise->coeffs[0] = binaryop0->b;
  1614. eltwise->coeffs[1] = 1.f;
  1615. eltwise->bottoms[0] = binaryop0->bottoms[0];
  1616. binaryop0->type = "ncnnfused";
  1617. }
  1618. if (j0 == i && j1 != i)
  1619. {
  1620. // fuse X - BinaryOp - BinaryOp to Eltwise
  1621. eltwise->coeffs[0] = 1.f;
  1622. eltwise->coeffs[1] = binaryop1->b;
  1623. eltwise->bottoms[1] = binaryop1->bottoms[0];
  1624. binaryop1->type = "ncnnfused";
  1625. }
  1626. layers[i] = eltwise;
  1627. delete binaryop;
  1628. }
  1629. return 0;
  1630. }
  1631. int NetOptimize::eliminate_dropout()
  1632. {
  1633. const size_t layer_count = layers.size();
  1634. for (size_t i = 0; i < layer_count; i++)
  1635. {
  1636. if (layers[i]->type != "Dropout")
  1637. continue;
  1638. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[i];
  1639. if (dropout->scale != 1.f)
  1640. continue;
  1641. // Any - Dropout
  1642. int bottom_blob_index = layers[i]->bottoms[0];
  1643. int j = i - 1;
  1644. for (; j >= 0; j--)
  1645. {
  1646. if (layers[j]->type == "ncnnfused")
  1647. continue;
  1648. if (layers[j]->tops.size() != 1)
  1649. continue;
  1650. if (layers[j]->tops[0] == bottom_blob_index)
  1651. break;
  1652. }
  1653. if (j == -1)
  1654. continue;
  1655. ncnn::Layer* any = layers[j];
  1656. fprintf(stderr, "eliminate_dropout %s %s\n", any->name.c_str(), dropout->name.c_str());
  1657. int top_blob_index_final = dropout->tops[0];
  1658. any->tops[0] = top_blob_index_final;
  1659. blobs[top_blob_index_final].producer = j;
  1660. dropout->type = "ncnnfused";
  1661. }
  1662. return 0;
  1663. }
  1664. int NetOptimize::eliminate_pooling1x1()
  1665. {
  1666. const size_t layer_count = layers.size();
  1667. for (size_t i = 0; i < layer_count; i++)
  1668. {
  1669. if (layers[i]->type != "Pooling")
  1670. continue;
  1671. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1672. if (pooling->pad_left != 0 || pooling->pad_right != 0 || pooling->pad_top != 0 || pooling->pad_bottom != 0)
  1673. continue;
  1674. if (pooling->kernel_w != 1 || pooling->kernel_h != 1 || pooling->stride_w != 1 || pooling->stride_h != 1)
  1675. continue;
  1676. if (pooling->global_pooling != 0)
  1677. continue;
  1678. // Any - Pooling
  1679. int bottom_blob_index = layers[i]->bottoms[0];
  1680. int top_i = -1;
  1681. int j = i - 1;
  1682. for (; j >= 0; j--)
  1683. {
  1684. if (layers[j]->type == "ncnnfused")
  1685. continue;
  1686. for (size_t k = 0; k < layers[j]->tops.size(); k++)
  1687. {
  1688. if (layers[j]->tops[k] == bottom_blob_index)
  1689. {
  1690. top_i = k;
  1691. break;
  1692. }
  1693. }
  1694. if (top_i != -1)
  1695. break;
  1696. }
  1697. if (j == -1)
  1698. continue;
  1699. ncnn::Layer* any = layers[j];
  1700. fprintf(stderr, "eliminate_pooling1x1 %s %s\n", any->name.c_str(), pooling->name.c_str());
  1701. int top_blob_index_final = pooling->tops[0];
  1702. any->tops[top_i] = top_blob_index_final;
  1703. blobs[top_blob_index_final].producer = j;
  1704. pooling->type = "ncnnfused";
  1705. }
  1706. return 0;
  1707. }
  1708. int NetOptimize::eliminate_noop()
  1709. {
  1710. const size_t layer_count = layers.size();
  1711. for (size_t i = 0; i < layer_count; i++)
  1712. {
  1713. if (layers[i]->type != "Noop")
  1714. continue;
  1715. ncnn::Layer* noop = layers[i];
  1716. if (noop->bottoms.empty())
  1717. {
  1718. // Noop
  1719. fprintf(stderr, "eliminate_noop %s\n", noop->name.c_str());
  1720. size_t top_blob_count = noop->tops.size();
  1721. for (size_t j = 0; j < top_blob_count; j++)
  1722. {
  1723. int top_blob_index_final = noop->tops[j];
  1724. blobs[top_blob_index_final].producer = -1;
  1725. }
  1726. noop->type = "ncnnfused";
  1727. continue;
  1728. }
  1729. // Any - Noop
  1730. int bottom_blob_index = noop->bottoms[0];
  1731. int j = i - 1;
  1732. int any_k = -1;
  1733. for (; j >= 0; j--)
  1734. {
  1735. if (layers[j]->type == "ncnnfused")
  1736. continue;
  1737. bool link_noop = false;
  1738. size_t top_blob_count = layers[j]->tops.size();
  1739. for (size_t k = 0; k < top_blob_count; k++)
  1740. {
  1741. if (layers[j]->tops[k] == bottom_blob_index)
  1742. {
  1743. link_noop = true;
  1744. any_k = k;
  1745. break;
  1746. }
  1747. }
  1748. if (link_noop)
  1749. break;
  1750. }
  1751. if (j == -1 || any_k == -1)
  1752. continue;
  1753. ncnn::Layer* any = layers[j];
  1754. fprintf(stderr, "eliminate_noop %s %s\n", any->name.c_str(), noop->name.c_str());
  1755. int top_blob_index_final = noop->tops[0];
  1756. any->tops[any_k] = top_blob_index_final;
  1757. blobs[top_blob_index_final].producer = j;
  1758. noop->type = "ncnnfused";
  1759. }
  1760. return 0;
  1761. }
  1762. int NetOptimize::eliminate_split()
  1763. {
  1764. const size_t layer_count = layers.size();
  1765. for (size_t i = 0; i < layer_count; i++)
  1766. {
  1767. if (layers[i]->type != "Split")
  1768. continue;
  1769. ncnn::Layer* split = layers[i];
  1770. int real_split_output_count = 0;
  1771. int real_split_top_blob_index = -1;
  1772. size_t top_blob_count = split->tops.size();
  1773. for (size_t j = 0; j < top_blob_count; j++)
  1774. {
  1775. int top_blob_index_final = split->tops[j];
  1776. if (blobs[top_blob_index_final].consumer != -1)
  1777. {
  1778. real_split_output_count += 1;
  1779. real_split_top_blob_index = j;
  1780. }
  1781. }
  1782. if (real_split_output_count > 1)
  1783. continue;
  1784. // Any - Pooling
  1785. int bottom_blob_index = split->bottoms[0];
  1786. int top_i = -1;
  1787. int j = i - 1;
  1788. for (; j >= 0; j--)
  1789. {
  1790. if (layers[j]->type == "ncnnfused")
  1791. continue;
  1792. for (size_t k = 0; k < layers[j]->tops.size(); k++)
  1793. {
  1794. if (layers[j]->tops[k] == bottom_blob_index)
  1795. {
  1796. top_i = k;
  1797. break;
  1798. }
  1799. }
  1800. if (top_i != -1)
  1801. break;
  1802. }
  1803. if (j == -1)
  1804. continue;
  1805. ncnn::Layer* any = layers[j];
  1806. fprintf(stderr, "eliminate_split %s %s\n", any->name.c_str(), split->name.c_str());
  1807. int top_blob_index_final = split->tops[real_split_top_blob_index];
  1808. any->tops[top_i] = top_blob_index_final;
  1809. blobs[top_blob_index_final].producer = j;
  1810. split->type = "ncnnfused";
  1811. }
  1812. return 0;
  1813. }
  1814. int NetOptimize::eliminate_orphaned_memorydata()
  1815. {
  1816. const size_t layer_count = layers.size();
  1817. for (size_t i = 0; i < layer_count; i++)
  1818. {
  1819. if (layers[i]->type != "MemoryData")
  1820. continue;
  1821. // MemoryData - X
  1822. int top_blob_index = layers[i]->tops[0];
  1823. size_t j = i + 1;
  1824. for (; j < layer_count; j++)
  1825. {
  1826. if (layers[j]->type == "ncnnfused")
  1827. continue;
  1828. bool orphaned = true;
  1829. for (size_t k = 0; k < layers[j]->bottoms.size(); k++)
  1830. {
  1831. if (layers[j]->bottoms[k] == top_blob_index)
  1832. {
  1833. orphaned = false;
  1834. break;
  1835. }
  1836. }
  1837. if (!orphaned)
  1838. break;
  1839. }
  1840. if (j < layer_count)
  1841. continue;
  1842. // assert orphaned == true
  1843. fprintf(stderr, "eliminate_orphaned_memorydata %s\n", layers[i]->name.c_str());
  1844. layers[i]->type = "ncnnfused";
  1845. }
  1846. return 0;
  1847. }
  1848. int NetOptimize::eliminate_reshape_after_global_pooling()
  1849. {
  1850. const size_t layer_count = layers.size();
  1851. for (size_t i = 0; i < layer_count; i++)
  1852. {
  1853. if (layers[i]->type != "Pooling")
  1854. continue;
  1855. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1856. if (pooling->global_pooling == 0)
  1857. continue;
  1858. // Pooling - Reshape
  1859. int top_blob_index = layers[i]->tops[0];
  1860. size_t j = i + 1;
  1861. for (; j < layer_count; j++)
  1862. {
  1863. if (layers[j]->type != "Reshape")
  1864. continue;
  1865. if (layers[j]->bottoms.size() != 1)
  1866. continue;
  1867. if (layers[j]->bottoms[0] == top_blob_index)
  1868. break;
  1869. }
  1870. if (j == layer_count)
  1871. continue;
  1872. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[j];
  1873. if (reshape->h != -233 || reshape->c != -233)
  1874. continue;
  1875. if (!reshape->shape_expr.empty())
  1876. continue;
  1877. fprintf(stderr, "eliminate_reshape_after_global_pooling %s %s\n", pooling->name.c_str(), reshape->name.c_str());
  1878. int top_blob_index_final = reshape->tops[0];
  1879. pooling->tops[0] = top_blob_index_final;
  1880. blobs[top_blob_index_final].producer = i;
  1881. reshape->type = "ncnnfused";
  1882. }
  1883. return 0;
  1884. }
  1885. int NetOptimize::eliminate_flatten_after_global_pooling()
  1886. {
  1887. const size_t layer_count = layers.size();
  1888. for (size_t i = 0; i < layer_count; i++)
  1889. {
  1890. if (layers[i]->type != "Pooling")
  1891. continue;
  1892. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1893. if (pooling->global_pooling == 0)
  1894. continue;
  1895. // Pooling - Flatten
  1896. int top_blob_index = layers[i]->tops[0];
  1897. size_t j = i + 1;
  1898. for (; j < layer_count; j++)
  1899. {
  1900. if (layers[j]->type != "Flatten")
  1901. continue;
  1902. if (layers[j]->bottoms.size() != 1)
  1903. continue;
  1904. if (layers[j]->bottoms[0] == top_blob_index)
  1905. break;
  1906. }
  1907. if (j == layer_count)
  1908. continue;
  1909. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1910. fprintf(stderr, "eliminate_flatten_after_global_pooling %s %s\n", pooling->name.c_str(), flatten->name.c_str());
  1911. int top_blob_index_final = flatten->tops[0];
  1912. pooling->tops[0] = top_blob_index_final;
  1913. blobs[top_blob_index_final].producer = i;
  1914. flatten->type = "ncnnfused";
  1915. }
  1916. return 0;
  1917. }
  1918. int NetOptimize::eliminate_flatten_after_innerproduct()
  1919. {
  1920. const size_t layer_count = layers.size();
  1921. for (size_t i = 0; i < layer_count; i++)
  1922. {
  1923. if (layers[i]->type != "InnerProduct")
  1924. continue;
  1925. // InnerProduct - Flatten
  1926. int top_blob_index = layers[i]->tops[0];
  1927. size_t j = i + 1;
  1928. for (; j < layer_count; j++)
  1929. {
  1930. if (layers[j]->type != "Flatten")
  1931. continue;
  1932. if (layers[j]->bottoms.size() != 1)
  1933. continue;
  1934. if (layers[j]->bottoms[0] == top_blob_index)
  1935. break;
  1936. }
  1937. if (j == layer_count)
  1938. continue;
  1939. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1940. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1941. fprintf(stderr, "eliminate_flatten_after_innerproduct %s %s\n", innerproduct->name.c_str(), flatten->name.c_str());
  1942. int top_blob_index_final = flatten->tops[0];
  1943. innerproduct->tops[0] = top_blob_index_final;
  1944. blobs[top_blob_index_final].producer = i;
  1945. flatten->type = "ncnnfused";
  1946. }
  1947. return 0;
  1948. }
  1949. int NetOptimize::eliminate_reshape_before_binaryop()
  1950. {
  1951. const size_t layer_count = layers.size();
  1952. for (size_t i = 0; i < layer_count; i++)
  1953. {
  1954. if (layers[i]->type != "Reshape")
  1955. continue;
  1956. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[i];
  1957. if (reshape->w != 1 || reshape->h != 1)
  1958. continue;
  1959. if (!reshape->shape_expr.empty())
  1960. continue;
  1961. // Reshape - BinaryOp
  1962. int top_blob_index = layers[i]->tops[0];
  1963. size_t j = i + 1;
  1964. for (; j < layer_count; j++)
  1965. {
  1966. if (layers[j]->type != "BinaryOp")
  1967. continue;
  1968. if (layers[j]->bottoms.size() != 2)
  1969. continue;
  1970. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1971. break;
  1972. }
  1973. if (j == layer_count)
  1974. continue;
  1975. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1976. fprintf(stderr, "eliminate_reshape_before_binaryop %s %s\n", reshape->name.c_str(), binaryop->name.c_str());
  1977. int bottom_blob_index_final = reshape->bottoms[0];
  1978. if (layers[j]->bottoms[0] == top_blob_index)
  1979. binaryop->bottoms[0] = bottom_blob_index_final;
  1980. if (layers[j]->bottoms[1] == top_blob_index)
  1981. binaryop->bottoms[1] = bottom_blob_index_final;
  1982. blobs[bottom_blob_index_final].consumer = j;
  1983. reshape->type = "ncnnfused";
  1984. }
  1985. return 0;
  1986. }
  1987. int NetOptimize::replace_reduction_with_global_pooling()
  1988. {
  1989. const size_t layer_count = layers.size();
  1990. for (size_t i = 0; i < layer_count; i++)
  1991. {
  1992. if (layers[i]->type != "Reduction")
  1993. continue;
  1994. ncnn::Reduction* reduction1 = (ncnn::Reduction*)layers[i];
  1995. if (reduction1->operation != 3 || reduction1->reduce_all != 0 || reduction1->coeff != 1.f)
  1996. continue;
  1997. if (reduction1->axes.w != 1)
  1998. continue;
  1999. const int* axes_ptr = reduction1->axes;
  2000. if (axes_ptr[0] != 2 && axes_ptr[0] != 3)
  2001. continue;
  2002. // Reduction(2/3) - Reduction(2)
  2003. int top_blob_index = layers[i]->tops[0];
  2004. size_t j = i + 1;
  2005. for (; j < layer_count; j++)
  2006. {
  2007. if (layers[j]->type != "Reduction")
  2008. continue;
  2009. if (layers[j]->bottoms.size() != 1)
  2010. continue;
  2011. if (layers[j]->bottoms[0] == top_blob_index)
  2012. break;
  2013. }
  2014. if (j == layer_count)
  2015. continue;
  2016. ncnn::Reduction* reduction2 = (ncnn::Reduction*)layers[j];
  2017. if (reduction2->operation != 3 || reduction2->reduce_all != 0 || reduction2->coeff != 1.f)
  2018. continue;
  2019. if (reduction2->axes.w != 1)
  2020. continue;
  2021. const int* axes2_ptr = reduction2->axes;
  2022. if (axes2_ptr[0] != 2)
  2023. continue;
  2024. fprintf(stderr, "replace_reduction_with_global_pooling %s %s\n", reduction1->name.c_str(), reduction2->name.c_str());
  2025. ncnn::Pooling* pooling = (ncnn::Pooling*)ncnn::create_layer_cpu("Pooling");
  2026. pooling->type = "Pooling";
  2027. pooling->name = reduction2->name;
  2028. pooling->bottoms = reduction2->bottoms;
  2029. pooling->tops = reduction2->tops;
  2030. ncnn::ParamDict pd;
  2031. pooling->load_param(pd);
  2032. pooling->pooling_type = 1;
  2033. pooling->global_pooling = 1;
  2034. layers[j] = pooling;
  2035. delete reduction2;
  2036. int bottom_blob_index_final = reduction1->bottoms[0];
  2037. pooling->bottoms[0] = bottom_blob_index_final;
  2038. blobs[bottom_blob_index_final].consumer = j;
  2039. reduction1->type = "ncnnfused";
  2040. }
  2041. return 0;
  2042. }
  2043. int NetOptimize::replace_prelu_with_leaky_relu()
  2044. {
  2045. const size_t layer_count = layers.size();
  2046. for (size_t i = 0; i < layer_count; i++)
  2047. {
  2048. if (layers[i]->type != "PReLU")
  2049. continue;
  2050. ncnn::PReLU* prelu = (ncnn::PReLU*)layers[i];
  2051. if (prelu->num_slope != 1)
  2052. continue;
  2053. fprintf(stderr, "replace_prelu_with_leaky_relu %s\n", prelu->name.c_str());
  2054. ncnn::ReLU* relu = (ncnn::ReLU*)ncnn::create_layer_cpu("ReLU");
  2055. relu->type = "ReLU";
  2056. relu->name = prelu->name;
  2057. relu->bottoms = prelu->bottoms;
  2058. relu->tops = prelu->tops;
  2059. ncnn::ParamDict pd;
  2060. relu->load_param(pd);
  2061. relu->slope = prelu->slope_data[0];
  2062. layers[i] = relu;
  2063. delete prelu;
  2064. }
  2065. return 0;
  2066. }
  2067. int NetOptimize::replace_convolution_with_innerproduct_after_global_pooling()
  2068. {
  2069. const size_t layer_count = layers.size();
  2070. for (size_t i = 0; i < layer_count; i++)
  2071. {
  2072. if (layers[i]->type != "Pooling")
  2073. continue;
  2074. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  2075. if (pooling->global_pooling == 0)
  2076. continue;
  2077. // Pooling - Convolution
  2078. int top_blob_index = layers[i]->tops[0];
  2079. size_t j = i + 1;
  2080. for (; j < layer_count; j++)
  2081. {
  2082. if (layers[j]->type != "Convolution")
  2083. continue;
  2084. if (layers[j]->bottoms.size() != 1)
  2085. continue;
  2086. if (layers[j]->bottoms[0] == top_blob_index)
  2087. break;
  2088. }
  2089. if (j == layer_count)
  2090. continue;
  2091. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  2092. fprintf(stderr, "replace_convolution_with_innerproduct_after_global_pooling %s %s\n", pooling->name.c_str(), convolution->name.c_str());
  2093. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)ncnn::create_layer_cpu("InnerProduct");
  2094. innerproduct->type = "InnerProduct";
  2095. innerproduct->name = convolution->name;
  2096. innerproduct->bottoms = convolution->bottoms;
  2097. innerproduct->tops = convolution->tops;
  2098. ncnn::ParamDict pd;
  2099. innerproduct->load_param(pd);
  2100. innerproduct->num_output = convolution->num_output;
  2101. innerproduct->bias_term = convolution->bias_term;
  2102. innerproduct->weight_data_size = convolution->weight_data_size;
  2103. innerproduct->int8_scale_term = convolution->int8_scale_term;
  2104. innerproduct->weight_data = convolution->weight_data;
  2105. innerproduct->bias_data = convolution->bias_data;
  2106. #if NCNN_INT8
  2107. innerproduct->weight_data_int8_scales = convolution->weight_data_int8_scales;
  2108. innerproduct->bottom_blob_int8_scales = convolution->bottom_blob_int8_scales;
  2109. #endif
  2110. innerproduct->activation_type = convolution->activation_type;
  2111. innerproduct->activation_params = convolution->activation_params;
  2112. layers[j] = innerproduct;
  2113. delete convolution;
  2114. }
  2115. return 0;
  2116. }
  2117. int NetOptimize::replace_convolution_with_innerproduct_after_innerproduct()
  2118. {
  2119. const size_t layer_count = layers.size();
  2120. for (;;)
  2121. {
  2122. bool replaced = false;
  2123. for (size_t i = 0; i < layer_count; i++)
  2124. {
  2125. if (layers[i]->type != "InnerProduct")
  2126. continue;
  2127. // InnerProduct - Convolution
  2128. int top_blob_index = layers[i]->tops[0];
  2129. size_t j = i + 1;
  2130. for (; j < layer_count; j++)
  2131. {
  2132. if (layers[j]->type != "Convolution")
  2133. continue;
  2134. if (layers[j]->bottoms.size() != 1)
  2135. continue;
  2136. if (layers[j]->bottoms[0] == top_blob_index)
  2137. break;
  2138. }
  2139. if (j == layer_count)
  2140. continue;
  2141. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  2142. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  2143. fprintf(stderr, "replace_convolution_with_innerproduct_after_innerproduct %s %s\n", innerproduct->name.c_str(), convolution->name.c_str());
  2144. ncnn::InnerProduct* innerproduct2 = (ncnn::InnerProduct*)ncnn::create_layer_cpu("InnerProduct");
  2145. innerproduct2->type = "InnerProduct";
  2146. innerproduct2->name = convolution->name;
  2147. innerproduct2->bottoms = convolution->bottoms;
  2148. innerproduct2->tops = convolution->tops;
  2149. ncnn::ParamDict pd;
  2150. innerproduct2->load_param(pd);
  2151. innerproduct2->num_output = convolution->num_output;
  2152. innerproduct2->bias_term = convolution->bias_term;
  2153. innerproduct2->weight_data_size = convolution->weight_data_size;
  2154. innerproduct->int8_scale_term = convolution->int8_scale_term;
  2155. innerproduct2->weight_data = convolution->weight_data;
  2156. innerproduct2->bias_data = convolution->bias_data;
  2157. #if NCNN_INT8
  2158. innerproduct->weight_data_int8_scales = convolution->weight_data_int8_scales;
  2159. innerproduct->bottom_blob_int8_scales = convolution->bottom_blob_int8_scales;
  2160. #endif
  2161. innerproduct2->activation_type = convolution->activation_type;
  2162. innerproduct2->activation_params = convolution->activation_params;
  2163. layers[j] = innerproduct2;
  2164. delete convolution;
  2165. replaced = true;
  2166. }
  2167. if (!replaced)
  2168. break;
  2169. }
  2170. return 0;
  2171. }
  2172. int main(int argc, char** argv)
  2173. {
  2174. if (argc < 6)
  2175. {
  2176. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [flag] [cutstart] [cutend]\n", argv[0]);
  2177. return -1;
  2178. }
  2179. const char* inparam = argv[1];
  2180. const char* inbin = argv[2];
  2181. const char* outparam = argv[3];
  2182. const char* outbin = argv[4];
  2183. int flag = atoi(argv[5]);
  2184. const char* cutstartname = nullptr;
  2185. const char* cutendname = nullptr;
  2186. if (argc > 6)
  2187. {
  2188. cutstartname = argv[6];
  2189. }
  2190. if (argc > 7)
  2191. {
  2192. cutendname = argv[7];
  2193. }
  2194. NetOptimize optimizer;
  2195. if (flag == 65536 || flag == 1)
  2196. {
  2197. optimizer.storage_type = 1;
  2198. }
  2199. else
  2200. {
  2201. optimizer.storage_type = 0;
  2202. }
  2203. optimizer.load_param(inparam);
  2204. if (strcmp(inbin, "null") == 0)
  2205. {
  2206. DataReaderFromEmpty dr;
  2207. optimizer.load_model(dr);
  2208. optimizer.gen_random_weight = true;
  2209. }
  2210. else
  2211. optimizer.load_model(inbin);
  2212. if (optimizer.set_cutparam(cutstartname, cutendname) < 0)
  2213. {
  2214. return -1;
  2215. }
  2216. optimizer.fuse_batchnorm_scale();
  2217. optimizer.fuse_convolution_batchnorm();
  2218. optimizer.fuse_convolution_mul();
  2219. optimizer.fuse_convolution_add();
  2220. optimizer.fuse_convolutiondepthwise_batchnorm();
  2221. optimizer.fuse_convolutiondepthwise_mul();
  2222. optimizer.fuse_convolutiondepthwise_add();
  2223. optimizer.fuse_deconvolution_batchnorm();
  2224. optimizer.fuse_deconvolution_mul();
  2225. optimizer.fuse_deconvolution_add();
  2226. optimizer.fuse_deconvolutiondepthwise_batchnorm();
  2227. optimizer.fuse_innerproduct_batchnorm();
  2228. optimizer.fuse_innerproduct_add();
  2229. optimizer.fuse_innerproduct_dropout();
  2230. optimizer.replace_reduction_with_global_pooling();
  2231. optimizer.replace_prelu_with_leaky_relu();
  2232. optimizer.fuse_convolution_activation();
  2233. optimizer.fuse_convolutiondepthwise_activation();
  2234. optimizer.fuse_deconvolution_activation();
  2235. optimizer.fuse_deconvolutiondepthwise_activation();
  2236. optimizer.fuse_innerproduct_activation();
  2237. optimizer.fuse_memorydata_binaryop();
  2238. optimizer.fuse_binaryop_eltwise();
  2239. optimizer.eliminate_dropout();
  2240. optimizer.eliminate_pooling1x1();
  2241. optimizer.eliminate_noop();
  2242. optimizer.eliminate_split();
  2243. optimizer.eliminate_flatten_after_global_pooling();
  2244. optimizer.eliminate_reshape_after_global_pooling();
  2245. optimizer.eliminate_reshape_before_binaryop();
  2246. optimizer.replace_convolution_with_innerproduct_after_global_pooling();
  2247. optimizer.replace_convolution_with_innerproduct_after_innerproduct();
  2248. optimizer.eliminate_flatten_after_innerproduct();
  2249. optimizer.eliminate_orphaned_memorydata();
  2250. optimizer.shape_inference();
  2251. optimizer.estimate_memory_footprint();
  2252. optimizer.save(outparam, outbin);
  2253. return 0;
  2254. }