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 86 kB

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