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