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 84 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  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")
  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. int top_blob_index_final = activation->tops[0];
  1076. convolution->tops[0] = top_blob_index_final;
  1077. blobs[top_blob_index_final].producer = i;
  1078. activation->type = "ncnnfused";
  1079. }
  1080. for (size_t i = 0; i < layer_count; i++)
  1081. {
  1082. if (layers[i]->type != "Convolution1D")
  1083. continue;
  1084. // Convolution1D - Activation
  1085. int top_blob_index = layers[i]->tops[0];
  1086. size_t j = i + 1;
  1087. for (; j < layer_count; j++)
  1088. {
  1089. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish")
  1090. continue;
  1091. if (layers[j]->bottoms.size() != 1)
  1092. continue;
  1093. if (layers[j]->bottoms[0] == top_blob_index)
  1094. break;
  1095. }
  1096. if (j == layer_count)
  1097. continue;
  1098. // fuse Convolution1D - Activation to Convolution1D
  1099. ncnn::Convolution1D* convolution = (ncnn::Convolution1D*)layers[i];
  1100. ncnn::Layer* activation = layers[j];
  1101. fprintf(stderr, "fuse_convolution1d_activation %s %s\n", convolution->name.c_str(), activation->name.c_str());
  1102. if (activation->type == "ReLU")
  1103. {
  1104. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1105. if (relu->slope == 0.f)
  1106. {
  1107. convolution->activation_type = 1;
  1108. }
  1109. else
  1110. {
  1111. convolution->activation_type = 2;
  1112. convolution->activation_params = ncnn::Mat(1);
  1113. convolution->activation_params[0] = relu->slope;
  1114. }
  1115. }
  1116. else if (activation->type == "Clip")
  1117. {
  1118. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1119. convolution->activation_type = 3;
  1120. convolution->activation_params = ncnn::Mat(2);
  1121. convolution->activation_params[0] = clip->min;
  1122. convolution->activation_params[1] = clip->max;
  1123. }
  1124. else if (activation->type == "Sigmoid")
  1125. {
  1126. convolution->activation_type = 4;
  1127. }
  1128. else if (activation->type == "Mish")
  1129. {
  1130. convolution->activation_type = 5;
  1131. }
  1132. int top_blob_index_final = activation->tops[0];
  1133. convolution->tops[0] = top_blob_index_final;
  1134. blobs[top_blob_index_final].producer = i;
  1135. activation->type = "ncnnfused";
  1136. }
  1137. return 0;
  1138. }
  1139. int NetOptimize::fuse_convolutiondepthwise_activation()
  1140. {
  1141. const size_t layer_count = layers.size();
  1142. for (size_t i = 0; i < layer_count; i++)
  1143. {
  1144. if (layers[i]->type != "ConvolutionDepthWise")
  1145. continue;
  1146. // ConvolutionDepthWise - Activation
  1147. int top_blob_index = layers[i]->tops[0];
  1148. size_t j = i + 1;
  1149. for (; j < layer_count; j++)
  1150. {
  1151. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid" && layers[j]->type != "Mish")
  1152. continue;
  1153. if (layers[j]->bottoms.size() != 1)
  1154. continue;
  1155. if (layers[j]->bottoms[0] == top_blob_index)
  1156. break;
  1157. }
  1158. if (j == layer_count)
  1159. continue;
  1160. // fuse ConvolutionDepthWise - Activation to ConvolutionDepthWise
  1161. ncnn::ConvolutionDepthWise* convolutiondepthwise = (ncnn::ConvolutionDepthWise*)layers[i];
  1162. ncnn::Layer* activation = layers[j];
  1163. fprintf(stderr, "fuse_convolutiondepthwise_activation %s %s\n", convolutiondepthwise->name.c_str(), activation->name.c_str());
  1164. if (activation->type == "ReLU")
  1165. {
  1166. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1167. if (relu->slope == 0.f)
  1168. {
  1169. convolutiondepthwise->activation_type = 1;
  1170. }
  1171. else
  1172. {
  1173. convolutiondepthwise->activation_type = 2;
  1174. convolutiondepthwise->activation_params = ncnn::Mat(1);
  1175. convolutiondepthwise->activation_params[0] = relu->slope;
  1176. }
  1177. }
  1178. else if (activation->type == "Clip")
  1179. {
  1180. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1181. convolutiondepthwise->activation_type = 3;
  1182. convolutiondepthwise->activation_params = ncnn::Mat(2);
  1183. convolutiondepthwise->activation_params[0] = clip->min;
  1184. convolutiondepthwise->activation_params[1] = clip->max;
  1185. }
  1186. else if (activation->type == "Sigmoid")
  1187. {
  1188. convolutiondepthwise->activation_type = 4;
  1189. }
  1190. else if (activation->type == "Mish")
  1191. {
  1192. convolutiondepthwise->activation_type = 5;
  1193. }
  1194. int top_blob_index_final = activation->tops[0];
  1195. convolutiondepthwise->tops[0] = top_blob_index_final;
  1196. blobs[top_blob_index_final].producer = i;
  1197. activation->type = "ncnnfused";
  1198. }
  1199. return 0;
  1200. }
  1201. int NetOptimize::fuse_deconvolution_activation()
  1202. {
  1203. const size_t layer_count = layers.size();
  1204. for (size_t i = 0; i < layer_count; i++)
  1205. {
  1206. if (layers[i]->type != "Deconvolution")
  1207. continue;
  1208. // Deconvolution - Activation
  1209. int top_blob_index = layers[i]->tops[0];
  1210. size_t j = i + 1;
  1211. for (; j < layer_count; j++)
  1212. {
  1213. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1214. continue;
  1215. if (layers[j]->bottoms.size() != 1)
  1216. continue;
  1217. if (layers[j]->bottoms[0] == top_blob_index)
  1218. break;
  1219. }
  1220. if (j == layer_count)
  1221. continue;
  1222. // fuse Deconvolution - Activation to Deconvolution
  1223. ncnn::Deconvolution* deconvolution = (ncnn::Deconvolution*)layers[i];
  1224. ncnn::Layer* activation = layers[j];
  1225. fprintf(stderr, "fuse_deconvolution_activation %s %s\n", deconvolution->name.c_str(), activation->name.c_str());
  1226. if (activation->type == "ReLU")
  1227. {
  1228. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1229. if (relu->slope == 0.f)
  1230. {
  1231. deconvolution->activation_type = 1;
  1232. }
  1233. else
  1234. {
  1235. deconvolution->activation_type = 2;
  1236. deconvolution->activation_params = ncnn::Mat(1);
  1237. deconvolution->activation_params[0] = relu->slope;
  1238. }
  1239. }
  1240. else if (activation->type == "Clip")
  1241. {
  1242. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1243. deconvolution->activation_type = 3;
  1244. deconvolution->activation_params = ncnn::Mat(2);
  1245. deconvolution->activation_params[0] = clip->min;
  1246. deconvolution->activation_params[1] = clip->max;
  1247. }
  1248. else if (activation->type == "Sigmoid")
  1249. {
  1250. deconvolution->activation_type = 4;
  1251. }
  1252. int top_blob_index_final = activation->tops[0];
  1253. deconvolution->tops[0] = top_blob_index_final;
  1254. blobs[top_blob_index_final].producer = i;
  1255. activation->type = "ncnnfused";
  1256. }
  1257. return 0;
  1258. }
  1259. int NetOptimize::fuse_deconvolutiondepthwise_activation()
  1260. {
  1261. const size_t layer_count = layers.size();
  1262. for (size_t i = 0; i < layer_count; i++)
  1263. {
  1264. if (layers[i]->type != "DeconvolutionDepthWise")
  1265. continue;
  1266. // DeconvolutionDepthWise - Activation
  1267. int top_blob_index = layers[i]->tops[0];
  1268. size_t j = i + 1;
  1269. for (; j < layer_count; j++)
  1270. {
  1271. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1272. continue;
  1273. if (layers[j]->bottoms.size() != 1)
  1274. continue;
  1275. if (layers[j]->bottoms[0] == top_blob_index)
  1276. break;
  1277. }
  1278. if (j == layer_count)
  1279. continue;
  1280. // fuse DeconvolutionDepthWise - Activation to DeconvolutionDepthWise
  1281. ncnn::DeconvolutionDepthWise* deconvolutiondepthwise = (ncnn::DeconvolutionDepthWise*)layers[i];
  1282. ncnn::Layer* activation = layers[j];
  1283. fprintf(stderr, "fuse_deconvolutiondepthwise_activation %s %s\n", deconvolutiondepthwise->name.c_str(), activation->name.c_str());
  1284. if (activation->type == "ReLU")
  1285. {
  1286. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1287. if (relu->slope == 0.f)
  1288. {
  1289. deconvolutiondepthwise->activation_type = 1;
  1290. }
  1291. else
  1292. {
  1293. deconvolutiondepthwise->activation_type = 2;
  1294. deconvolutiondepthwise->activation_params = ncnn::Mat(1);
  1295. deconvolutiondepthwise->activation_params[0] = relu->slope;
  1296. }
  1297. }
  1298. else if (activation->type == "Clip")
  1299. {
  1300. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1301. deconvolutiondepthwise->activation_type = 3;
  1302. deconvolutiondepthwise->activation_params = ncnn::Mat(2);
  1303. deconvolutiondepthwise->activation_params[0] = clip->min;
  1304. deconvolutiondepthwise->activation_params[1] = clip->max;
  1305. }
  1306. else if (activation->type == "Sigmoid")
  1307. {
  1308. deconvolutiondepthwise->activation_type = 4;
  1309. }
  1310. int top_blob_index_final = activation->tops[0];
  1311. deconvolutiondepthwise->tops[0] = top_blob_index_final;
  1312. blobs[top_blob_index_final].producer = i;
  1313. activation->type = "ncnnfused";
  1314. }
  1315. return 0;
  1316. }
  1317. int NetOptimize::fuse_innerproduct_activation()
  1318. {
  1319. const size_t layer_count = layers.size();
  1320. for (size_t i = 0; i < layer_count; i++)
  1321. {
  1322. if (layers[i]->type != "InnerProduct")
  1323. continue;
  1324. // InnerProduct - Activation
  1325. int top_blob_index = layers[i]->tops[0];
  1326. size_t j = i + 1;
  1327. for (; j < layer_count; j++)
  1328. {
  1329. if (layers[j]->type != "ReLU" && layers[j]->type != "Clip" && layers[j]->type != "Sigmoid")
  1330. continue;
  1331. if (layers[j]->bottoms.size() != 1)
  1332. continue;
  1333. if (layers[j]->bottoms[0] == top_blob_index)
  1334. break;
  1335. }
  1336. if (j == layer_count)
  1337. continue;
  1338. // fuse InnerProduct - Activation to InnerProduct
  1339. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1340. ncnn::Layer* activation = layers[j];
  1341. fprintf(stderr, "fuse_innerproduct_activation %s %s\n", innerproduct->name.c_str(), activation->name.c_str());
  1342. if (activation->type == "ReLU")
  1343. {
  1344. ncnn::ReLU* relu = (ncnn::ReLU*)activation;
  1345. if (relu->slope == 0.f)
  1346. {
  1347. innerproduct->activation_type = 1;
  1348. }
  1349. else
  1350. {
  1351. innerproduct->activation_type = 2;
  1352. innerproduct->activation_params = ncnn::Mat(1);
  1353. innerproduct->activation_params[0] = relu->slope;
  1354. }
  1355. }
  1356. else if (activation->type == "Clip")
  1357. {
  1358. ncnn::Clip* clip = (ncnn::Clip*)activation;
  1359. innerproduct->activation_type = 3;
  1360. innerproduct->activation_params = ncnn::Mat(2);
  1361. innerproduct->activation_params[0] = clip->min;
  1362. innerproduct->activation_params[1] = clip->max;
  1363. }
  1364. else if (activation->type == "Sigmoid")
  1365. {
  1366. innerproduct->activation_type = 4;
  1367. }
  1368. int top_blob_index_final = activation->tops[0];
  1369. innerproduct->tops[0] = top_blob_index_final;
  1370. blobs[top_blob_index_final].producer = i;
  1371. activation->type = "ncnnfused";
  1372. }
  1373. return 0;
  1374. }
  1375. int NetOptimize::fuse_memorydata_binaryop()
  1376. {
  1377. const size_t layer_count = layers.size();
  1378. for (size_t i = 0; i < layer_count; i++)
  1379. {
  1380. if (layers[i]->type != "MemoryData")
  1381. continue;
  1382. // MemoryData - BinaryOp
  1383. int top_blob_index = layers[i]->tops[0];
  1384. size_t j = i + 1;
  1385. for (; j < layer_count; j++)
  1386. {
  1387. if (layers[j]->type != "BinaryOp")
  1388. continue;
  1389. if (layers[j]->bottoms.size() != 2)
  1390. continue;
  1391. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1392. break;
  1393. }
  1394. if (j == layer_count)
  1395. continue;
  1396. // fuse MemoryData - BinaryOp to BinaryOp
  1397. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[i];
  1398. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1399. if (memorydata->w != 1 || memorydata->h != 0 || memorydata->c != 0)
  1400. {
  1401. // not a scalar
  1402. continue;
  1403. }
  1404. int memorydata_index = 1;
  1405. if (binaryop->bottoms[0] == top_blob_index)
  1406. {
  1407. int op_type = binaryop->op_type;
  1408. if (op_type == ncnn::BinaryOp::Operation_ADD
  1409. || op_type == ncnn::BinaryOp::Operation_MUL
  1410. || op_type == ncnn::BinaryOp::Operation_MAX
  1411. || op_type == ncnn::BinaryOp::Operation_MIN)
  1412. {
  1413. memorydata_index = 0;
  1414. }
  1415. else if (op_type == ncnn::BinaryOp::Operation_SUB)
  1416. {
  1417. binaryop->op_type = ncnn::BinaryOp::Operation_RSUB;
  1418. memorydata_index = 0;
  1419. }
  1420. else if (op_type == ncnn::BinaryOp::Operation_DIV)
  1421. {
  1422. binaryop->op_type = ncnn::BinaryOp::Operation_RDIV;
  1423. memorydata_index = 0;
  1424. }
  1425. else
  1426. {
  1427. // non interchangeable binaryop
  1428. continue;
  1429. }
  1430. }
  1431. float scalar = memorydata->data[0];
  1432. binaryop->with_scalar = 1;
  1433. binaryop->b = scalar;
  1434. fprintf(stderr, "fuse_memorydata_binaryop %s %s\n", memorydata->name.c_str(), binaryop->name.c_str());
  1435. binaryop->bottoms.erase(binaryop->bottoms.begin() + memorydata_index);
  1436. memorydata->type = "ncnnfused";
  1437. }
  1438. for (size_t i = 0; i < layer_count; i++)
  1439. {
  1440. if (layers[i]->type != "MemoryData")
  1441. continue;
  1442. // MemoryData - Split - BinaryOp
  1443. int top_blob_index = layers[i]->tops[0];
  1444. size_t j0 = i + 1;
  1445. for (; j0 < layer_count; j0++)
  1446. {
  1447. if (layers[j0]->type != "Split")
  1448. continue;
  1449. if (layers[j0]->bottoms.size() != 1)
  1450. continue;
  1451. if (layers[j0]->bottoms[0] == top_blob_index)
  1452. break;
  1453. }
  1454. if (j0 == layer_count)
  1455. continue;
  1456. int split_top_blob_index = -1;
  1457. size_t j1 = j0 + 1;
  1458. for (; j1 < layer_count; j1++)
  1459. {
  1460. if (layers[j1]->type != "BinaryOp")
  1461. continue;
  1462. if (layers[j1]->bottoms.size() != 2)
  1463. continue;
  1464. for (int k = 0; k < (int)layers[j0]->tops.size(); k++)
  1465. {
  1466. if (layers[j1]->bottoms[0] == layers[j0]->tops[k] || layers[j1]->bottoms[1] == layers[j0]->tops[k])
  1467. {
  1468. split_top_blob_index = k;
  1469. break;
  1470. }
  1471. }
  1472. if (split_top_blob_index != -1)
  1473. break;
  1474. }
  1475. if (j1 == layer_count)
  1476. continue;
  1477. // fuse MemoryData - Split - BinaryOp to BinaryOp
  1478. ncnn::MemoryData* memorydata = (ncnn::MemoryData*)layers[i];
  1479. ncnn::Split* split = (ncnn::Split*)layers[j0];
  1480. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j1];
  1481. if (memorydata->w != 1 || memorydata->h != 0 || memorydata->c != 0)
  1482. {
  1483. // not a scalar
  1484. continue;
  1485. }
  1486. int memorydata_index = 1;
  1487. if (binaryop->bottoms[0] == split->tops[split_top_blob_index])
  1488. {
  1489. int op_type = binaryop->op_type;
  1490. if (op_type == ncnn::BinaryOp::Operation_ADD
  1491. || op_type == ncnn::BinaryOp::Operation_MUL
  1492. || op_type == ncnn::BinaryOp::Operation_MAX
  1493. || op_type == ncnn::BinaryOp::Operation_MIN)
  1494. {
  1495. memorydata_index = 0;
  1496. }
  1497. else if (op_type == ncnn::BinaryOp::Operation_SUB)
  1498. {
  1499. binaryop->op_type = ncnn::BinaryOp::Operation_RSUB;
  1500. memorydata_index = 0;
  1501. }
  1502. else if (op_type == ncnn::BinaryOp::Operation_DIV)
  1503. {
  1504. binaryop->op_type = ncnn::BinaryOp::Operation_RDIV;
  1505. memorydata_index = 0;
  1506. }
  1507. else
  1508. {
  1509. // non interchangeable binaryop
  1510. continue;
  1511. }
  1512. }
  1513. float scalar = memorydata->data[0];
  1514. binaryop->with_scalar = 1;
  1515. binaryop->b = scalar;
  1516. fprintf(stderr, "fuse_memorydata_binaryop %s %s\n", memorydata->name.c_str(), binaryop->name.c_str());
  1517. binaryop->bottoms.erase(binaryop->bottoms.begin() + memorydata_index);
  1518. split->tops.erase(split->tops.begin() + split_top_blob_index);
  1519. if (split->tops.empty())
  1520. {
  1521. split->type = "ncnnfused";
  1522. memorydata->type = "ncnnfused";
  1523. }
  1524. i--;
  1525. }
  1526. return 0;
  1527. }
  1528. int NetOptimize::fuse_binaryop_eltwise()
  1529. {
  1530. const size_t layer_count = layers.size();
  1531. for (size_t i = 0; i < layer_count; i++)
  1532. {
  1533. if (layers[i]->type != "BinaryOp")
  1534. continue;
  1535. if (layers[i]->bottoms.size() != 2)
  1536. continue;
  1537. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[i];
  1538. if (binaryop->op_type != ncnn::BinaryOp::Operation_ADD)
  1539. continue;
  1540. if (binaryop->with_scalar)
  1541. continue;
  1542. // BinaryOp - BinaryOp - BinaryOp
  1543. int bottom_blob_index_0 = binaryop->bottoms[0];
  1544. int bottom_blob_index_1 = binaryop->bottoms[1];
  1545. size_t j0 = 0;
  1546. for (; j0 < i; j0++)
  1547. {
  1548. if (layers[j0]->type != "BinaryOp")
  1549. continue;
  1550. if (layers[j0]->bottoms.size() != 1)
  1551. continue;
  1552. if (((ncnn::BinaryOp*)layers[j0])->op_type != ncnn::BinaryOp::Operation_MUL)
  1553. continue;
  1554. if (layers[j0]->tops[0] == bottom_blob_index_0)
  1555. break;
  1556. }
  1557. size_t j1 = 0;
  1558. for (; j1 < i; j1++)
  1559. {
  1560. if (layers[j1]->type != "BinaryOp")
  1561. continue;
  1562. if (layers[j1]->bottoms.size() != 1)
  1563. continue;
  1564. if (((ncnn::BinaryOp*)layers[j1])->op_type != ncnn::BinaryOp::Operation_MUL)
  1565. continue;
  1566. if (layers[j1]->tops[0] == bottom_blob_index_1)
  1567. break;
  1568. }
  1569. if (j0 == i && j1 == i)
  1570. continue;
  1571. ncnn::BinaryOp* binaryop0 = (ncnn::BinaryOp*)layers[j0];
  1572. ncnn::BinaryOp* binaryop1 = (ncnn::BinaryOp*)layers[j1];
  1573. fprintf(stderr, "fuse_binaryop_eltwise %s %s %s\n", binaryop0->name.c_str(), binaryop1->name.c_str(), binaryop->name.c_str());
  1574. ncnn::Eltwise* eltwise = (ncnn::Eltwise*)ncnn::create_layer("Eltwise");
  1575. eltwise->type = "Eltwise";
  1576. eltwise->name = binaryop->name;
  1577. eltwise->bottoms = binaryop->bottoms;
  1578. eltwise->tops = binaryop->tops;
  1579. ncnn::ParamDict pd;
  1580. eltwise->load_param(pd);
  1581. eltwise->op_type = ncnn::Eltwise::Operation_SUM;
  1582. eltwise->coeffs = ncnn::Mat(2);
  1583. if (j0 != i && j1 != i)
  1584. {
  1585. // fuse BinaryOp - BinaryOp - BinaryOp to Eltwise
  1586. eltwise->coeffs[0] = binaryop0->b;
  1587. eltwise->coeffs[1] = binaryop1->b;
  1588. eltwise->bottoms[0] = binaryop0->bottoms[0];
  1589. eltwise->bottoms[1] = binaryop1->bottoms[0];
  1590. binaryop0->type = "ncnnfused";
  1591. binaryop1->type = "ncnnfused";
  1592. }
  1593. if (j0 != i && j1 == i)
  1594. {
  1595. // fuse BinaryOp - X - BinaryOp to Eltwise
  1596. eltwise->coeffs[0] = binaryop0->b;
  1597. eltwise->coeffs[1] = 1.f;
  1598. eltwise->bottoms[0] = binaryop0->bottoms[0];
  1599. binaryop0->type = "ncnnfused";
  1600. }
  1601. if (j0 == i && j1 != i)
  1602. {
  1603. // fuse X - BinaryOp - BinaryOp to Eltwise
  1604. eltwise->coeffs[0] = 1.f;
  1605. eltwise->coeffs[1] = binaryop1->b;
  1606. eltwise->bottoms[1] = binaryop1->bottoms[0];
  1607. binaryop1->type = "ncnnfused";
  1608. }
  1609. layers[i] = eltwise;
  1610. delete binaryop;
  1611. }
  1612. return 0;
  1613. }
  1614. int NetOptimize::eliminate_dropout()
  1615. {
  1616. const size_t layer_count = layers.size();
  1617. for (size_t i = 0; i < layer_count; i++)
  1618. {
  1619. if (layers[i]->type != "Dropout")
  1620. continue;
  1621. ncnn::Dropout* dropout = (ncnn::Dropout*)layers[i];
  1622. if (dropout->scale != 1.f)
  1623. continue;
  1624. // Any - Dropout
  1625. int bottom_blob_index = layers[i]->bottoms[0];
  1626. int j = i - 1;
  1627. for (; j >= 0; j--)
  1628. {
  1629. if (layers[j]->type == "ncnnfused")
  1630. continue;
  1631. if (layers[j]->tops.size() != 1)
  1632. continue;
  1633. if (layers[j]->tops[0] == bottom_blob_index)
  1634. break;
  1635. }
  1636. if (j == -1)
  1637. continue;
  1638. ncnn::Layer* any = layers[j];
  1639. fprintf(stderr, "eliminate_dropout %s %s\n", any->name.c_str(), dropout->name.c_str());
  1640. int top_blob_index_final = dropout->tops[0];
  1641. any->tops[0] = top_blob_index_final;
  1642. blobs[top_blob_index_final].producer = j;
  1643. dropout->type = "ncnnfused";
  1644. }
  1645. return 0;
  1646. }
  1647. int NetOptimize::eliminate_pooling1x1()
  1648. {
  1649. const size_t layer_count = layers.size();
  1650. for (size_t i = 0; i < layer_count; i++)
  1651. {
  1652. if (layers[i]->type != "Pooling")
  1653. continue;
  1654. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1655. if (pooling->pad_left != 0 || pooling->pad_right != 0 || pooling->pad_top != 0 || pooling->pad_bottom != 0)
  1656. continue;
  1657. if (pooling->kernel_w != 1 || pooling->kernel_h != 1 || pooling->stride_w != 1 || pooling->stride_h != 1)
  1658. continue;
  1659. if (pooling->global_pooling != 0)
  1660. continue;
  1661. // Any - Pooling
  1662. int bottom_blob_index = layers[i]->bottoms[0];
  1663. int top_i = -1;
  1664. int j = i - 1;
  1665. for (; j >= 0; j--)
  1666. {
  1667. if (layers[j]->type == "ncnnfused")
  1668. continue;
  1669. for (size_t k = 0; k < layers[j]->tops.size(); k++)
  1670. {
  1671. if (layers[j]->tops[k] == bottom_blob_index)
  1672. {
  1673. top_i = k;
  1674. break;
  1675. }
  1676. }
  1677. if (top_i != -1)
  1678. break;
  1679. }
  1680. if (j == -1)
  1681. continue;
  1682. ncnn::Layer* any = layers[j];
  1683. fprintf(stderr, "eliminate_pooling1x1 %s %s\n", any->name.c_str(), pooling->name.c_str());
  1684. int top_blob_index_final = pooling->tops[0];
  1685. any->tops[top_i] = top_blob_index_final;
  1686. blobs[top_blob_index_final].producer = j;
  1687. pooling->type = "ncnnfused";
  1688. }
  1689. return 0;
  1690. }
  1691. int NetOptimize::eliminate_noop()
  1692. {
  1693. const size_t layer_count = layers.size();
  1694. for (size_t i = 0; i < layer_count; i++)
  1695. {
  1696. if (layers[i]->type != "Noop")
  1697. continue;
  1698. ncnn::Layer* noop = layers[i];
  1699. if (noop->bottoms.empty())
  1700. {
  1701. // Noop
  1702. fprintf(stderr, "eliminate_noop %s\n", noop->name.c_str());
  1703. size_t top_blob_count = noop->tops.size();
  1704. for (size_t j = 0; j < top_blob_count; j++)
  1705. {
  1706. int top_blob_index_final = noop->tops[j];
  1707. blobs[top_blob_index_final].producer = -1;
  1708. }
  1709. noop->type = "ncnnfused";
  1710. continue;
  1711. }
  1712. // Any - Noop
  1713. int bottom_blob_index = noop->bottoms[0];
  1714. int j = i - 1;
  1715. int any_k = -1;
  1716. for (; j >= 0; j--)
  1717. {
  1718. if (layers[j]->type == "ncnnfused")
  1719. continue;
  1720. bool link_noop = false;
  1721. size_t top_blob_count = layers[j]->tops.size();
  1722. for (size_t k = 0; k < top_blob_count; k++)
  1723. {
  1724. if (layers[j]->tops[k] == bottom_blob_index)
  1725. {
  1726. link_noop = true;
  1727. any_k = k;
  1728. break;
  1729. }
  1730. }
  1731. if (link_noop)
  1732. break;
  1733. }
  1734. if (j == -1 || any_k == -1)
  1735. continue;
  1736. ncnn::Layer* any = layers[j];
  1737. fprintf(stderr, "eliminate_noop %s %s\n", any->name.c_str(), noop->name.c_str());
  1738. int top_blob_index_final = noop->tops[0];
  1739. any->tops[any_k] = top_blob_index_final;
  1740. blobs[top_blob_index_final].producer = j;
  1741. noop->type = "ncnnfused";
  1742. }
  1743. return 0;
  1744. }
  1745. int NetOptimize::eliminate_split()
  1746. {
  1747. const size_t layer_count = layers.size();
  1748. for (size_t i = 0; i < layer_count; i++)
  1749. {
  1750. if (layers[i]->type != "Split")
  1751. continue;
  1752. ncnn::Layer* split = layers[i];
  1753. int real_split_output_count = 0;
  1754. int real_split_top_blob_index = -1;
  1755. size_t top_blob_count = split->tops.size();
  1756. for (size_t j = 0; j < top_blob_count; j++)
  1757. {
  1758. int top_blob_index_final = split->tops[j];
  1759. if (blobs[top_blob_index_final].consumer != -1)
  1760. {
  1761. real_split_output_count += 1;
  1762. real_split_top_blob_index = j;
  1763. }
  1764. }
  1765. if (real_split_output_count > 1)
  1766. continue;
  1767. // Any - Pooling
  1768. int bottom_blob_index = split->bottoms[0];
  1769. int top_i = -1;
  1770. int j = i - 1;
  1771. for (; j >= 0; j--)
  1772. {
  1773. if (layers[j]->type == "ncnnfused")
  1774. continue;
  1775. for (size_t k = 0; k < layers[j]->tops.size(); k++)
  1776. {
  1777. if (layers[j]->tops[k] == bottom_blob_index)
  1778. {
  1779. top_i = k;
  1780. break;
  1781. }
  1782. }
  1783. if (top_i != -1)
  1784. break;
  1785. }
  1786. if (j == -1)
  1787. continue;
  1788. ncnn::Layer* any = layers[j];
  1789. fprintf(stderr, "eliminate_split %s %s\n", any->name.c_str(), split->name.c_str());
  1790. int top_blob_index_final = split->tops[real_split_top_blob_index];
  1791. any->tops[top_i] = top_blob_index_final;
  1792. blobs[top_blob_index_final].producer = j;
  1793. split->type = "ncnnfused";
  1794. }
  1795. return 0;
  1796. }
  1797. int NetOptimize::eliminate_orphaned_memorydata()
  1798. {
  1799. const size_t layer_count = layers.size();
  1800. for (size_t i = 0; i < layer_count; i++)
  1801. {
  1802. if (layers[i]->type != "MemoryData")
  1803. continue;
  1804. // MemoryData - X
  1805. int top_blob_index = layers[i]->tops[0];
  1806. size_t j = i + 1;
  1807. for (; j < layer_count; j++)
  1808. {
  1809. if (layers[j]->type == "ncnnfused")
  1810. continue;
  1811. bool orphaned = true;
  1812. for (size_t k = 0; k < layers[j]->bottoms.size(); k++)
  1813. {
  1814. if (layers[j]->bottoms[k] == top_blob_index)
  1815. {
  1816. orphaned = false;
  1817. break;
  1818. }
  1819. }
  1820. if (!orphaned)
  1821. break;
  1822. }
  1823. if (j < layer_count)
  1824. continue;
  1825. // assert orphaned == true
  1826. fprintf(stderr, "eliminate_orphaned_memorydata %s\n", layers[i]->name.c_str());
  1827. layers[i]->type = "ncnnfused";
  1828. }
  1829. return 0;
  1830. }
  1831. int NetOptimize::eliminate_reshape_after_global_pooling()
  1832. {
  1833. const size_t layer_count = layers.size();
  1834. for (size_t i = 0; i < layer_count; i++)
  1835. {
  1836. if (layers[i]->type != "Pooling")
  1837. continue;
  1838. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1839. if (pooling->global_pooling == 0)
  1840. continue;
  1841. // Pooling - Reshape
  1842. int top_blob_index = layers[i]->tops[0];
  1843. size_t j = i + 1;
  1844. for (; j < layer_count; j++)
  1845. {
  1846. if (layers[j]->type != "Reshape")
  1847. continue;
  1848. if (layers[j]->bottoms.size() != 1)
  1849. continue;
  1850. if (layers[j]->bottoms[0] == top_blob_index)
  1851. break;
  1852. }
  1853. if (j == layer_count)
  1854. continue;
  1855. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[j];
  1856. if (reshape->h != -233 || reshape->c != -233 || reshape->permute != 0)
  1857. continue;
  1858. fprintf(stderr, "eliminate_reshape_after_global_pooling %s %s\n", pooling->name.c_str(), reshape->name.c_str());
  1859. int top_blob_index_final = reshape->tops[0];
  1860. pooling->tops[0] = top_blob_index_final;
  1861. blobs[top_blob_index_final].producer = i;
  1862. reshape->type = "ncnnfused";
  1863. }
  1864. return 0;
  1865. }
  1866. int NetOptimize::eliminate_flatten_after_global_pooling()
  1867. {
  1868. const size_t layer_count = layers.size();
  1869. for (size_t i = 0; i < layer_count; i++)
  1870. {
  1871. if (layers[i]->type != "Pooling")
  1872. continue;
  1873. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  1874. if (pooling->global_pooling == 0)
  1875. continue;
  1876. // Pooling - Flatten
  1877. int top_blob_index = layers[i]->tops[0];
  1878. size_t j = i + 1;
  1879. for (; j < layer_count; j++)
  1880. {
  1881. if (layers[j]->type != "Flatten")
  1882. continue;
  1883. if (layers[j]->bottoms.size() != 1)
  1884. continue;
  1885. if (layers[j]->bottoms[0] == top_blob_index)
  1886. break;
  1887. }
  1888. if (j == layer_count)
  1889. continue;
  1890. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1891. fprintf(stderr, "eliminate_flatten_after_global_pooling %s %s\n", pooling->name.c_str(), flatten->name.c_str());
  1892. int top_blob_index_final = flatten->tops[0];
  1893. pooling->tops[0] = top_blob_index_final;
  1894. blobs[top_blob_index_final].producer = i;
  1895. flatten->type = "ncnnfused";
  1896. }
  1897. return 0;
  1898. }
  1899. int NetOptimize::eliminate_flatten_after_innerproduct()
  1900. {
  1901. const size_t layer_count = layers.size();
  1902. for (size_t i = 0; i < layer_count; i++)
  1903. {
  1904. if (layers[i]->type != "InnerProduct")
  1905. continue;
  1906. // InnerProduct - 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::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  1921. ncnn::Flatten* flatten = (ncnn::Flatten*)layers[j];
  1922. fprintf(stderr, "eliminate_flatten_after_innerproduct %s %s\n", innerproduct->name.c_str(), flatten->name.c_str());
  1923. int top_blob_index_final = flatten->tops[0];
  1924. innerproduct->tops[0] = top_blob_index_final;
  1925. blobs[top_blob_index_final].producer = i;
  1926. flatten->type = "ncnnfused";
  1927. }
  1928. return 0;
  1929. }
  1930. int NetOptimize::eliminate_reshape_before_binaryop()
  1931. {
  1932. const size_t layer_count = layers.size();
  1933. for (size_t i = 0; i < layer_count; i++)
  1934. {
  1935. if (layers[i]->type != "Reshape")
  1936. continue;
  1937. ncnn::Reshape* reshape = (ncnn::Reshape*)layers[i];
  1938. if (reshape->w != 1 || reshape->h != 1 || reshape->permute != 0)
  1939. continue;
  1940. // Reshape - BinaryOp
  1941. int top_blob_index = layers[i]->tops[0];
  1942. size_t j = i + 1;
  1943. for (; j < layer_count; j++)
  1944. {
  1945. if (layers[j]->type != "BinaryOp")
  1946. continue;
  1947. if (layers[j]->bottoms.size() != 2)
  1948. continue;
  1949. if (layers[j]->bottoms[0] == top_blob_index || layers[j]->bottoms[1] == top_blob_index)
  1950. break;
  1951. }
  1952. if (j == layer_count)
  1953. continue;
  1954. ncnn::BinaryOp* binaryop = (ncnn::BinaryOp*)layers[j];
  1955. fprintf(stderr, "eliminate_reshape_before_binaryop %s %s\n", reshape->name.c_str(), binaryop->name.c_str());
  1956. int bottom_blob_index_final = reshape->bottoms[0];
  1957. if (layers[j]->bottoms[0] == top_blob_index)
  1958. binaryop->bottoms[0] = bottom_blob_index_final;
  1959. if (layers[j]->bottoms[1] == top_blob_index)
  1960. binaryop->bottoms[1] = bottom_blob_index_final;
  1961. blobs[bottom_blob_index_final].consumer = j;
  1962. reshape->type = "ncnnfused";
  1963. }
  1964. return 0;
  1965. }
  1966. int NetOptimize::replace_reduction_with_global_pooling()
  1967. {
  1968. const size_t layer_count = layers.size();
  1969. for (size_t i = 0; i < layer_count; i++)
  1970. {
  1971. if (layers[i]->type != "Reduction")
  1972. continue;
  1973. ncnn::Reduction* reduction1 = (ncnn::Reduction*)layers[i];
  1974. if (reduction1->operation != 3 || reduction1->reduce_all != 0 || reduction1->coeff != 1.f)
  1975. continue;
  1976. if (reduction1->axes.w != 1)
  1977. continue;
  1978. const int* axes_ptr = reduction1->axes;
  1979. if (axes_ptr[0] != 2 && axes_ptr[0] != 3)
  1980. continue;
  1981. // Reduction(2/3) - Reduction(2)
  1982. int top_blob_index = layers[i]->tops[0];
  1983. size_t j = i + 1;
  1984. for (; j < layer_count; j++)
  1985. {
  1986. if (layers[j]->type != "Reduction")
  1987. continue;
  1988. if (layers[j]->bottoms.size() != 1)
  1989. continue;
  1990. if (layers[j]->bottoms[0] == top_blob_index)
  1991. break;
  1992. }
  1993. if (j == layer_count)
  1994. continue;
  1995. ncnn::Reduction* reduction2 = (ncnn::Reduction*)layers[j];
  1996. if (reduction2->operation != 3 || reduction2->reduce_all != 0 || reduction2->coeff != 1.f)
  1997. continue;
  1998. if (reduction2->axes.w != 1)
  1999. continue;
  2000. const int* axes2_ptr = reduction2->axes;
  2001. if (axes2_ptr[0] != 2)
  2002. continue;
  2003. fprintf(stderr, "replace_reduction_with_global_pooling %s %s\n", reduction1->name.c_str(), reduction2->name.c_str());
  2004. ncnn::Pooling* pooling = (ncnn::Pooling*)ncnn::create_layer("Pooling");
  2005. pooling->type = "Pooling";
  2006. pooling->name = reduction2->name;
  2007. pooling->bottoms = reduction2->bottoms;
  2008. pooling->tops = reduction2->tops;
  2009. ncnn::ParamDict pd;
  2010. pooling->load_param(pd);
  2011. pooling->pooling_type = 1;
  2012. pooling->global_pooling = 1;
  2013. layers[j] = pooling;
  2014. delete reduction2;
  2015. int bottom_blob_index_final = reduction1->bottoms[0];
  2016. pooling->bottoms[0] = bottom_blob_index_final;
  2017. blobs[bottom_blob_index_final].consumer = j;
  2018. reduction1->type = "ncnnfused";
  2019. }
  2020. return 0;
  2021. }
  2022. int NetOptimize::replace_prelu_with_leaky_relu()
  2023. {
  2024. const size_t layer_count = layers.size();
  2025. for (size_t i = 0; i < layer_count; i++)
  2026. {
  2027. if (layers[i]->type != "PReLU")
  2028. continue;
  2029. ncnn::PReLU* prelu = (ncnn::PReLU*)layers[i];
  2030. if (prelu->num_slope != 1)
  2031. continue;
  2032. fprintf(stderr, "replace_prelu_with_leaky_relu %s\n", prelu->name.c_str());
  2033. ncnn::ReLU* relu = (ncnn::ReLU*)ncnn::create_layer("ReLU");
  2034. relu->type = "ReLU";
  2035. relu->name = prelu->name;
  2036. relu->bottoms = prelu->bottoms;
  2037. relu->tops = prelu->tops;
  2038. ncnn::ParamDict pd;
  2039. relu->load_param(pd);
  2040. relu->slope = prelu->slope_data[0];
  2041. layers[i] = relu;
  2042. delete prelu;
  2043. }
  2044. return 0;
  2045. }
  2046. int NetOptimize::replace_convolution_with_innerproduct_after_global_pooling()
  2047. {
  2048. const size_t layer_count = layers.size();
  2049. for (size_t i = 0; i < layer_count; i++)
  2050. {
  2051. if (layers[i]->type != "Pooling")
  2052. continue;
  2053. ncnn::Pooling* pooling = (ncnn::Pooling*)layers[i];
  2054. if (pooling->global_pooling == 0)
  2055. continue;
  2056. // Pooling - Convolution
  2057. int top_blob_index = layers[i]->tops[0];
  2058. size_t j = i + 1;
  2059. for (; j < layer_count; j++)
  2060. {
  2061. if (layers[j]->type != "Convolution")
  2062. continue;
  2063. if (layers[j]->bottoms.size() != 1)
  2064. continue;
  2065. if (layers[j]->bottoms[0] == top_blob_index)
  2066. break;
  2067. }
  2068. if (j == layer_count)
  2069. continue;
  2070. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  2071. fprintf(stderr, "replace_convolution_with_innerproduct_after_global_pooling %s %s\n", pooling->name.c_str(), convolution->name.c_str());
  2072. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)ncnn::create_layer("InnerProduct");
  2073. innerproduct->type = "InnerProduct";
  2074. innerproduct->name = convolution->name;
  2075. innerproduct->bottoms = convolution->bottoms;
  2076. innerproduct->tops = convolution->tops;
  2077. ncnn::ParamDict pd;
  2078. innerproduct->load_param(pd);
  2079. innerproduct->num_output = convolution->num_output;
  2080. innerproduct->bias_term = convolution->bias_term;
  2081. innerproduct->weight_data_size = convolution->weight_data_size;
  2082. innerproduct->int8_scale_term = convolution->int8_scale_term;
  2083. innerproduct->weight_data = convolution->weight_data;
  2084. innerproduct->bias_data = convolution->bias_data;
  2085. #if NCNN_INT8
  2086. innerproduct->weight_data_int8_scales = convolution->weight_data_int8_scales;
  2087. innerproduct->bottom_blob_int8_scales = convolution->bottom_blob_int8_scales;
  2088. #endif
  2089. innerproduct->activation_type = convolution->activation_type;
  2090. innerproduct->activation_params = convolution->activation_params;
  2091. layers[j] = innerproduct;
  2092. delete convolution;
  2093. }
  2094. return 0;
  2095. }
  2096. int NetOptimize::replace_convolution_with_innerproduct_after_innerproduct()
  2097. {
  2098. const size_t layer_count = layers.size();
  2099. for (;;)
  2100. {
  2101. bool replaced = false;
  2102. for (size_t i = 0; i < layer_count; i++)
  2103. {
  2104. if (layers[i]->type != "InnerProduct")
  2105. continue;
  2106. // InnerProduct - Convolution
  2107. int top_blob_index = layers[i]->tops[0];
  2108. size_t j = i + 1;
  2109. for (; j < layer_count; j++)
  2110. {
  2111. if (layers[j]->type != "Convolution")
  2112. continue;
  2113. if (layers[j]->bottoms.size() != 1)
  2114. continue;
  2115. if (layers[j]->bottoms[0] == top_blob_index)
  2116. break;
  2117. }
  2118. if (j == layer_count)
  2119. continue;
  2120. ncnn::InnerProduct* innerproduct = (ncnn::InnerProduct*)layers[i];
  2121. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[j];
  2122. fprintf(stderr, "replace_convolution_with_innerproduct_after_innerproduct %s %s\n", innerproduct->name.c_str(), convolution->name.c_str());
  2123. ncnn::InnerProduct* innerproduct2 = (ncnn::InnerProduct*)ncnn::create_layer("InnerProduct");
  2124. innerproduct2->type = "InnerProduct";
  2125. innerproduct2->name = convolution->name;
  2126. innerproduct2->bottoms = convolution->bottoms;
  2127. innerproduct2->tops = convolution->tops;
  2128. ncnn::ParamDict pd;
  2129. innerproduct2->load_param(pd);
  2130. innerproduct2->num_output = convolution->num_output;
  2131. innerproduct2->bias_term = convolution->bias_term;
  2132. innerproduct2->weight_data_size = convolution->weight_data_size;
  2133. innerproduct->int8_scale_term = convolution->int8_scale_term;
  2134. innerproduct2->weight_data = convolution->weight_data;
  2135. innerproduct2->bias_data = convolution->bias_data;
  2136. #if NCNN_INT8
  2137. innerproduct->weight_data_int8_scales = convolution->weight_data_int8_scales;
  2138. innerproduct->bottom_blob_int8_scales = convolution->bottom_blob_int8_scales;
  2139. #endif
  2140. innerproduct2->activation_type = convolution->activation_type;
  2141. innerproduct2->activation_params = convolution->activation_params;
  2142. layers[j] = innerproduct2;
  2143. delete convolution;
  2144. replaced = true;
  2145. }
  2146. if (!replaced)
  2147. break;
  2148. }
  2149. return 0;
  2150. }
  2151. int main(int argc, char** argv)
  2152. {
  2153. if (argc < 6)
  2154. {
  2155. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [flag] [cutstart] [cutend]\n", argv[0]);
  2156. return -1;
  2157. }
  2158. const char* inparam = argv[1];
  2159. const char* inbin = argv[2];
  2160. const char* outparam = argv[3];
  2161. const char* outbin = argv[4];
  2162. int flag = atoi(argv[5]);
  2163. const char* cutstartname = nullptr;
  2164. const char* cutendname = nullptr;
  2165. if (argc > 6)
  2166. {
  2167. cutstartname = argv[6];
  2168. }
  2169. if (argc > 7)
  2170. {
  2171. cutendname = argv[7];
  2172. }
  2173. NetOptimize optimizer;
  2174. if (flag == 65536 || flag == 1)
  2175. {
  2176. optimizer.storage_type = 1;
  2177. }
  2178. else
  2179. {
  2180. optimizer.storage_type = 0;
  2181. }
  2182. optimizer.load_param(inparam);
  2183. if (strcmp(inbin, "null") == 0)
  2184. {
  2185. DataReaderFromEmpty dr;
  2186. optimizer.load_model(dr);
  2187. optimizer.gen_random_weight = true;
  2188. }
  2189. else
  2190. optimizer.load_model(inbin);
  2191. if (optimizer.set_cutparam(cutstartname, cutendname) < 0)
  2192. {
  2193. return -1;
  2194. }
  2195. optimizer.fuse_batchnorm_scale();
  2196. optimizer.fuse_convolution_batchnorm();
  2197. optimizer.fuse_convolution_mul();
  2198. optimizer.fuse_convolution_add();
  2199. optimizer.fuse_convolutiondepthwise_batchnorm();
  2200. optimizer.fuse_convolutiondepthwise_mul();
  2201. optimizer.fuse_convolutiondepthwise_add();
  2202. optimizer.fuse_deconvolution_batchnorm();
  2203. optimizer.fuse_deconvolution_mul();
  2204. optimizer.fuse_deconvolution_add();
  2205. optimizer.fuse_deconvolutiondepthwise_batchnorm();
  2206. optimizer.fuse_innerproduct_batchnorm();
  2207. optimizer.fuse_innerproduct_add();
  2208. optimizer.fuse_innerproduct_dropout();
  2209. optimizer.replace_reduction_with_global_pooling();
  2210. optimizer.replace_prelu_with_leaky_relu();
  2211. optimizer.fuse_convolution_activation();
  2212. optimizer.fuse_convolutiondepthwise_activation();
  2213. optimizer.fuse_deconvolution_activation();
  2214. optimizer.fuse_deconvolutiondepthwise_activation();
  2215. optimizer.fuse_innerproduct_activation();
  2216. optimizer.fuse_memorydata_binaryop();
  2217. optimizer.fuse_binaryop_eltwise();
  2218. optimizer.eliminate_dropout();
  2219. optimizer.eliminate_pooling1x1();
  2220. optimizer.eliminate_noop();
  2221. optimizer.eliminate_split();
  2222. optimizer.eliminate_flatten_after_global_pooling();
  2223. optimizer.eliminate_reshape_after_global_pooling();
  2224. optimizer.eliminate_reshape_before_binaryop();
  2225. optimizer.replace_convolution_with_innerproduct_after_global_pooling();
  2226. optimizer.replace_convolution_with_innerproduct_after_innerproduct();
  2227. optimizer.eliminate_flatten_after_innerproduct();
  2228. optimizer.eliminate_orphaned_memorydata();
  2229. optimizer.shape_inference();
  2230. optimizer.estimate_memory_footprint();
  2231. optimizer.save(outparam, outbin);
  2232. return 0;
  2233. }