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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727
  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. }
  2107. else
  2108. optimizer.load_model(inbin);
  2109. if (optimizer.set_cutparam(cutstartname, cutendname) < 0)
  2110. {
  2111. return -1;
  2112. }
  2113. optimizer.fuse_batchnorm_scale();
  2114. optimizer.fuse_convolution_batchnorm();
  2115. optimizer.fuse_convolution_mul();
  2116. optimizer.fuse_convolution_add();
  2117. optimizer.fuse_convolutiondepthwise_batchnorm();
  2118. optimizer.fuse_convolutiondepthwise_mul();
  2119. optimizer.fuse_convolutiondepthwise_add();
  2120. optimizer.fuse_deconvolution_batchnorm();
  2121. optimizer.fuse_deconvolution_mul();
  2122. optimizer.fuse_deconvolution_add();
  2123. optimizer.fuse_deconvolutiondepthwise_batchnorm();
  2124. optimizer.fuse_innerproduct_batchnorm();
  2125. optimizer.fuse_innerproduct_add();
  2126. optimizer.fuse_innerproduct_dropout();
  2127. optimizer.replace_reduction_with_global_pooling();
  2128. optimizer.replace_prelu_with_leaky_relu();
  2129. optimizer.fuse_convolution_activation();
  2130. optimizer.fuse_convolutiondepthwise_activation();
  2131. optimizer.fuse_deconvolution_activation();
  2132. optimizer.fuse_deconvolutiondepthwise_activation();
  2133. optimizer.fuse_innerproduct_activation();
  2134. optimizer.fuse_memorydata_binaryop();
  2135. optimizer.fuse_binaryop_eltwise();
  2136. optimizer.eliminate_dropout();
  2137. optimizer.eliminate_pooling1x1();
  2138. optimizer.eliminate_noop();
  2139. optimizer.eliminate_split();
  2140. optimizer.eliminate_flatten_after_global_pooling();
  2141. optimizer.eliminate_reshape_after_global_pooling();
  2142. optimizer.eliminate_reshape_before_binaryop();
  2143. optimizer.replace_convolution_with_innerproduct_after_global_pooling();
  2144. optimizer.replace_convolution_with_innerproduct_after_innerproduct();
  2145. optimizer.eliminate_flatten_after_innerproduct();
  2146. optimizer.eliminate_orphaned_memorydata();
  2147. optimizer.shape_inference();
  2148. optimizer.estimate_memory_footprint();
  2149. optimizer.save(outparam, outbin);
  2150. return 0;
  2151. }