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.

mlir2ncnn.cpp 59 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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. #include <stdio.h>
  15. #include <map>
  16. #include <set>
  17. #include <mlir/Dialect/StandardOps/IR/Ops.h>
  18. #include <mlir/IR/PatternMatch.h>
  19. #include <mlir/Parser.h>
  20. #include <mlir/Pass/Pass.h>
  21. #include <mlir/Pass/PassManager.h>
  22. #include <mlir/Transforms/Passes.h>
  23. #include "tf_dialect.h"
  24. #include "ncnn_dialect.h"
  25. static std::string get_mlir_value_uniq_id(const mlir::Value& value)
  26. {
  27. if (value.getLoc().isa<mlir::FileLineColLoc>())
  28. {
  29. mlir::FileLineColLoc floc = value.getLoc().cast<mlir::FileLineColLoc>();
  30. return std::to_string(floc.getLine()) + ":" + std::to_string(floc.getColumn());
  31. }
  32. if (value.getLoc().isa<mlir::FusedLoc>())
  33. {
  34. mlir::FileLineColLoc floc = value.getLoc().cast<mlir::FusedLoc>().getLocations().front().cast<mlir::FileLineColLoc>();
  35. return std::to_string(floc.getLine()) + ":" + std::to_string(floc.getColumn());
  36. }
  37. fprintf(stderr, "unhandled get_mlir_value_uniq_id\n");
  38. return std::string();
  39. }
  40. static std::string get_attr_s(const mlir::Attribute& attr)
  41. {
  42. std::string s;
  43. if (attr.isa<mlir::StringAttr>())
  44. {
  45. mlir::StringAttr a = attr.cast<mlir::StringAttr>();
  46. s = a.getValue().str();
  47. }
  48. return s;
  49. }
  50. static int get_attr_b(const mlir::Attribute& attr)
  51. {
  52. int i;
  53. if (attr.isa<mlir::BoolAttr>())
  54. {
  55. mlir::BoolAttr a = attr.cast<mlir::BoolAttr>();
  56. i = a.getValue() ? 1 : 0;
  57. }
  58. else
  59. {
  60. fprintf(stderr, "not BoolAttr\n");
  61. }
  62. return i;
  63. }
  64. static int get_attr_i(const mlir::Attribute& attr)
  65. {
  66. int i;
  67. if (attr.isa<mlir::IntegerAttr>())
  68. {
  69. mlir::IntegerAttr a = attr.cast<mlir::IntegerAttr>();
  70. i = (int)a.getInt();
  71. }
  72. else
  73. {
  74. fprintf(stderr, "not IntegerAttr\n");
  75. }
  76. return i;
  77. }
  78. static float get_attr_f(const mlir::Attribute& attr)
  79. {
  80. float f;
  81. if (attr.isa<mlir::FloatAttr>())
  82. {
  83. mlir::FloatAttr a = attr.cast<mlir::FloatAttr>();
  84. f = (float)a.getValueAsDouble();
  85. }
  86. else
  87. {
  88. fprintf(stderr, "not FloatAttr\n");
  89. }
  90. return f;
  91. }
  92. static std::vector<int> get_attr_ai(const mlir::Attribute& attr)
  93. {
  94. std::vector<int> v;
  95. if (attr.isa<mlir::ArrayAttr>())
  96. {
  97. mlir::ArrayAttr a = attr.cast<mlir::ArrayAttr>();
  98. const int array_size = a.getValue().size();
  99. v.resize(array_size);
  100. for (int j = 0; j < array_size; j++)
  101. {
  102. if (a[j].isa<mlir::IntegerAttr>())
  103. {
  104. int64_t ii = a[j].cast<mlir::IntegerAttr>().getInt();
  105. v[j] = std::max(std::min(ii, (int64_t)INT_MAX), (int64_t)INT_MIN);
  106. }
  107. }
  108. }
  109. else if (attr.isa<mlir::DenseIntElementsAttr>())
  110. {
  111. mlir::DenseIntElementsAttr ai = attr.cast<mlir::DenseIntElementsAttr>();
  112. for (auto ii : ai.getIntValues())
  113. {
  114. v.push_back(ii.getSExtValue());
  115. }
  116. }
  117. else
  118. {
  119. fprintf(stderr, "not ArrayAttr or DenseIntElementsAttr\n");
  120. }
  121. return v;
  122. }
  123. static std::vector<float> get_attr_af(const mlir::Attribute& attr)
  124. {
  125. std::vector<float> v;
  126. if (attr.isa<mlir::ArrayAttr>())
  127. {
  128. mlir::ArrayAttr a = attr.cast<mlir::ArrayAttr>();
  129. const int array_size = a.getValue().size();
  130. v.resize(array_size);
  131. for (int j = 0; j < array_size; j++)
  132. {
  133. if (a[j].isa<mlir::FloatAttr>())
  134. {
  135. double ff = a[j].cast<mlir::FloatAttr>().getValueAsDouble();
  136. v[j] = ff;
  137. }
  138. }
  139. }
  140. else if (attr.isa<mlir::DenseFPElementsAttr>())
  141. {
  142. mlir::DenseFPElementsAttr af = attr.cast<mlir::DenseFPElementsAttr>();
  143. for (auto ff : af.getFloatValues())
  144. {
  145. v.push_back(ff.convertToFloat());
  146. }
  147. }
  148. else
  149. {
  150. fprintf(stderr, "not ArrayAttr or DenseFPElementsAttr\n");
  151. }
  152. return v;
  153. }
  154. static std::string get_operation_attr_s(const mlir::Operation& _operation, const char* key)
  155. {
  156. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  157. mlir::Attribute attr = operation.getAttr(key);
  158. return get_attr_s(attr);
  159. }
  160. static int get_operation_attr_b(const mlir::Operation& _operation, const char* key)
  161. {
  162. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  163. mlir::Attribute attr = operation.getAttr(key);
  164. return get_attr_b(attr);
  165. }
  166. static int get_operation_attr_i(const mlir::Operation& _operation, const char* key)
  167. {
  168. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  169. mlir::Attribute attr = operation.getAttr(key);
  170. return get_attr_i(attr);
  171. }
  172. static float get_operation_attr_f(const mlir::Operation& _operation, const char* key)
  173. {
  174. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  175. mlir::Attribute attr = operation.getAttr(key);
  176. return get_attr_f(attr);
  177. }
  178. static std::vector<int> get_operation_attr_ai(const mlir::Operation& _operation, const char* key)
  179. {
  180. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  181. mlir::Attribute attr = operation.getAttr(key);
  182. return get_attr_ai(attr);
  183. }
  184. static std::vector<float> get_operation_attr_af(const mlir::Operation& _operation, const char* key)
  185. {
  186. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  187. mlir::Attribute attr = operation.getAttr(key);
  188. return get_attr_af(attr);
  189. }
  190. int main(int argc, char** argv)
  191. {
  192. if (!(argc == 2 || argc == 4))
  193. {
  194. fprintf(stderr, "Usage: %s [mlir] [ncnnparam] [ncnnbin]\n", argv[0]);
  195. return -1;
  196. }
  197. const char* mlirpath = argv[1];
  198. const char* ncnn_prototxt = argc == 4 ? argv[2] : "ncnn.param";
  199. const char* ncnn_modelbin = argc == 4 ? argv[3] : "ncnn.bin";
  200. mlir::MLIRContext context;
  201. context.getOrLoadDialect<mlir::StandardOpsDialect>();
  202. context.getOrLoadDialect<mlir::TF::TensorFlowDialect>();
  203. context.getOrLoadDialect<mlir::ncnn::NCNNDialect>();
  204. mlir::OwningModuleRef m = mlir::parseSourceFile(mlirpath, &context);
  205. mlir::PassManager pm(&context);
  206. pm.addNestedPass<mlir::FuncOp>(mlir::ncnn::createNCNNOptimizePass());
  207. if (pm.run(*m).failed())
  208. {
  209. fprintf(stderr, "canonicalizer pass failed\n");
  210. return -1;
  211. }
  212. // m->dump();
  213. mlir::FuncOp main_fn = m->lookupSymbol<mlir::FuncOp>("main");
  214. auto& bb = main_fn.getBlocks().front();
  215. // bb.dump();
  216. FILE* pp = fopen(ncnn_prototxt, "wb");
  217. FILE* bp = fopen(ncnn_modelbin, "wb");
  218. // node reference
  219. std::map<std::string, int> node_reference;
  220. // weight node and weight reshape node
  221. std::map<std::string, mlir::Attribute> weights;
  222. fprintf(pp, "7767517\n");
  223. const mlir::Block::OpListType& operations = bb.getOperations();
  224. int node_count = operations.size();
  225. // global definition line
  226. // [layer count] [blob count]
  227. std::set<std::string> blob_names;
  228. for (const mlir::Operation& _operation : operations)
  229. {
  230. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  231. std::string op = operation.getName().getStringRef().str();
  232. int num_input = (int)operation.getNumOperands();
  233. int num_output = (int)operation.getNumResults();
  234. if (op == "tf.Const")
  235. {
  236. // weight
  237. std::string output_name = get_mlir_value_uniq_id(operation.getResult(0));
  238. weights[output_name] = operation.getAttr("value");
  239. }
  240. for (int j = 0; j < num_input; j++)
  241. {
  242. std::string input_name = get_mlir_value_uniq_id(operation.getOperand(j));
  243. blob_names.insert(input_name);
  244. if (node_reference.find(input_name) == node_reference.end())
  245. {
  246. node_reference[input_name] = 1;
  247. }
  248. else
  249. {
  250. node_reference[input_name] = node_reference[input_name] + 1;
  251. }
  252. }
  253. for (int j = 0; j < num_output; j++)
  254. {
  255. std::string output_name = get_mlir_value_uniq_id(operation.getResult(j));
  256. blob_names.insert(output_name);
  257. node_reference[output_name] = 0;
  258. }
  259. }
  260. // reduce common const weight node_reference
  261. for (const mlir::Operation& _operation : operations)
  262. {
  263. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  264. std::string op = operation.getName().getStringRef().str();
  265. if (op == "ncnn.KerasConv2D")
  266. {
  267. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  268. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  269. node_reference[weight_name] -= 1;
  270. node_reference[bias_name] -= 1;
  271. }
  272. else if (op == "ncnn.KerasDense")
  273. {
  274. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  275. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  276. node_reference[weight_name] -= 1;
  277. node_reference[bias_name] -= 1;
  278. }
  279. else if (op == "ncnn.KerasBatchNorm")
  280. {
  281. std::string gamma_name = get_mlir_value_uniq_id(operation.getOperand(1));
  282. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  283. node_reference[gamma_name] -= 1;
  284. node_reference[bias_name] -= 1;
  285. }
  286. else if (op == "ncnn.InstanceNormAffine")
  287. {
  288. std::string gamma_name = get_mlir_value_uniq_id(operation.getOperand(1));
  289. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  290. node_reference[gamma_name] -= 1;
  291. node_reference[bias_name] -= 1;
  292. }
  293. else if (op == "tf.ConcatV2")
  294. {
  295. std::string axis_name = get_mlir_value_uniq_id(operation.getOperand(operation.getNumOperands() - 1));
  296. node_reference[axis_name] -= 1;
  297. }
  298. else if (op == "tf.Conv2D")
  299. {
  300. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  301. node_reference[weight_name] -= 1;
  302. }
  303. else if (op == "tf.Conv2DBackpropInput")
  304. {
  305. std::string output_shape_name = get_mlir_value_uniq_id(operation.getOperand(0));
  306. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  307. node_reference[output_shape_name] -= 1;
  308. node_reference[weight_name] -= 1;
  309. }
  310. else if (op == "tf.DepthwiseConv2dNative")
  311. {
  312. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  313. node_reference[weight_name] -= 1;
  314. }
  315. else if (op == "tf.MatMul")
  316. {
  317. int transpose_a = get_operation_attr_b(operation, "transpose_a");
  318. int transpose_b = get_operation_attr_b(operation, "transpose_b");
  319. if (transpose_a == 0 && transpose_b == 1)
  320. {
  321. // InnerProduct-like A * B + C
  322. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  323. node_reference[weight_name] -= 1;
  324. }
  325. }
  326. else if (op == "tf.Mean")
  327. {
  328. std::string reduction_indices_name = get_mlir_value_uniq_id(operation.getOperand(1));
  329. node_reference[reduction_indices_name] -= 1;
  330. }
  331. else if (op == "tf.Pad")
  332. {
  333. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  334. node_reference[weight_name] -= 1;
  335. }
  336. else if (op == "tf.Reshape")
  337. {
  338. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  339. node_reference[weight_name] -= 1;
  340. }
  341. else if (op == "tf.ResizeBilinear")
  342. {
  343. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  344. node_reference[weight_name] -= 1;
  345. }
  346. else if (op == "tf.ResizeNearestNeighbor")
  347. {
  348. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  349. node_reference[weight_name] -= 1;
  350. }
  351. else if (op == "tf.StridedSlice")
  352. {
  353. std::string begin_name = get_mlir_value_uniq_id(operation.getOperand(1));
  354. std::string end_name = get_mlir_value_uniq_id(operation.getOperand(2));
  355. std::string strides_name = get_mlir_value_uniq_id(operation.getOperand(3));
  356. node_reference[begin_name] -= 1;
  357. node_reference[end_name] -= 1;
  358. node_reference[strides_name] -= 1;
  359. }
  360. }
  361. // count all weight node with zero reference
  362. int zero_reference_weight_node_count = 0;
  363. for (std::map<std::string, mlir::Attribute>::iterator it = weights.begin(); it != weights.end(); it++)
  364. {
  365. const std::string& input_name = it->first;
  366. int refcount = node_reference[input_name];
  367. if (refcount == 0)
  368. zero_reference_weight_node_count++;
  369. }
  370. // remove node_reference entry with reference equals to one
  371. int split_layer_count = 0;
  372. int splitncnn_blob_count = 0;
  373. // split node reference
  374. std::map<std::string, int> split_node_reference;
  375. for (std::map<std::string, int>::iterator it = node_reference.begin(); it != node_reference.end(); it++)
  376. {
  377. if (it->second > 1)
  378. {
  379. split_layer_count++;
  380. splitncnn_blob_count += it->second;
  381. split_node_reference[it->first] = it->second;
  382. }
  383. }
  384. fprintf(pp, "%lu %lu\n", node_count - zero_reference_weight_node_count + split_layer_count, blob_names.size() - zero_reference_weight_node_count + splitncnn_blob_count);
  385. int internal_split = 0;
  386. // place MemoryData next
  387. for (std::map<std::string, mlir::Attribute>::iterator weight_it = weights.begin(); weight_it != weights.end(); weight_it++)
  388. {
  389. const std::string& input_name = weight_it->first;
  390. int refcount = node_reference[input_name];
  391. if (refcount == 0)
  392. {
  393. continue;
  394. }
  395. fprintf(pp, "%-16s %-24s 0 1 %s", "MemoryData", input_name.c_str(), input_name.c_str());
  396. const mlir::Attribute& M = weights[input_name];
  397. llvm::ArrayRef<int64_t> shape = M.getType().cast<mlir::RankedTensorType>().getShape();
  398. // c wc hwc
  399. if (shape.size() == 0)
  400. {
  401. // scalar
  402. fprintf(pp, " 0=1");
  403. }
  404. else if (shape.size() == 1)
  405. {
  406. fprintf(pp, " 0=%d", (int)shape[0]);
  407. }
  408. else if (shape.size() == 2)
  409. {
  410. fprintf(pp, " 0=%d", (int)shape[1]);
  411. fprintf(pp, " 1=%d", (int)shape[0]);
  412. }
  413. else if (shape.size() == 3)
  414. {
  415. fprintf(pp, " 0=%d", (int)shape[1]);
  416. fprintf(pp, " 1=%d", (int)shape[0]);
  417. fprintf(pp, " 2=%d", (int)shape[2]);
  418. }
  419. fprintf(pp, "\n");
  420. std::vector<float> v = get_attr_af(M);
  421. if (shape.size() != 3)
  422. {
  423. fwrite(v.data(), sizeof(float), v.size(), bp);
  424. }
  425. else
  426. {
  427. int w = (int)shape[1];
  428. int h = (int)shape[0];
  429. int c = (int)shape[2];
  430. float tmp;
  431. // h-w-c to c-h-w
  432. for (int p = 0; p < c; p++)
  433. {
  434. for (int i = 0; i < h; i++)
  435. {
  436. for (int j = 0; j < w; j++)
  437. {
  438. tmp = v[i * w * c + j * c + p];
  439. fwrite(&tmp, sizeof(float), 1, bp);
  440. }
  441. }
  442. }
  443. }
  444. if (refcount <= 1)
  445. {
  446. continue;
  447. }
  448. char splitname[256];
  449. sprintf(splitname, "splitncnn_%d", internal_split);
  450. fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
  451. fprintf(pp, " %s", input_name.c_str());
  452. for (int k = 0; k < refcount; k++)
  453. {
  454. fprintf(pp, " %s_splitncnn_%d", input_name.c_str(), k);
  455. }
  456. fprintf(pp, "\n");
  457. internal_split++;
  458. }
  459. // model op
  460. int g_opid = 0;
  461. for (const mlir::Operation& _operation : operations)
  462. {
  463. mlir::Operation& operation = const_cast<mlir::Operation&>(_operation);
  464. std::string op = operation.getName().getStringRef().str();
  465. int opid = g_opid++;
  466. int num_input = (int)operation.getNumOperands();
  467. int num_output = (int)operation.getNumResults();
  468. for (int i = 0; i < (int)operation.getNumOperands(); i++)
  469. {
  470. std::string input_name = get_mlir_value_uniq_id(operation.getOperand(i));
  471. // check weight
  472. if (weights.find(input_name) != weights.end() && node_reference[input_name] == 0)
  473. {
  474. num_input--;
  475. }
  476. }
  477. if (op == "std.return")
  478. {
  479. fprintf(pp, "%-16s", "Noop");
  480. }
  481. else if (op == "ncnn.BinaryOp")
  482. {
  483. fprintf(pp, "%-16s", "BinaryOp");
  484. }
  485. else if (op == "ncnn.KerasConv2D")
  486. {
  487. fprintf(pp, "%-16s", "Convolution");
  488. }
  489. else if (op == "ncnn.KerasDense")
  490. {
  491. fprintf(pp, "%-16s", "InnerProduct");
  492. }
  493. else if (op == "ncnn.KerasBatchNorm")
  494. {
  495. fprintf(pp, "%-16s", "BatchNorm");
  496. }
  497. else if (op == "ncnn.InstanceNorm")
  498. {
  499. fprintf(pp, "%-16s", "InstanceNorm");
  500. }
  501. else if (op == "ncnn.InstanceNormAffine")
  502. {
  503. fprintf(pp, "%-16s", "InstanceNorm");
  504. }
  505. else if (op == "ncnn.Swish")
  506. {
  507. fprintf(pp, "%-16s", "Swish");
  508. }
  509. else if (op == "tf.AddN")
  510. {
  511. fprintf(pp, "%-16s", "Eltwise");
  512. }
  513. else if (op == "tf.AddV2")
  514. {
  515. fprintf(pp, "%-16s", "BinaryOp");
  516. }
  517. else if (op == "tf.AvgPool")
  518. {
  519. fprintf(pp, "%-16s", "Pooling");
  520. }
  521. else if (op == "tf.BiasAdd")
  522. {
  523. fprintf(pp, "%-16s", "BinaryOp");
  524. }
  525. else if (op == "tf.ConcatV2")
  526. {
  527. fprintf(pp, "%-16s", "Concat");
  528. }
  529. else if (op == "tf.Const")
  530. {
  531. continue;
  532. }
  533. else if (op == "tf.Conv2D")
  534. {
  535. fprintf(pp, "%-16s", "Convolution");
  536. }
  537. else if (op == "tf.Conv2DBackpropInput")
  538. {
  539. fprintf(pp, "%-16s", "Deconvolution");
  540. }
  541. else if (op == "tf.DepthToSpace")
  542. {
  543. fprintf(pp, "%-16s", "PixelShuffle");
  544. }
  545. else if (op == "tf.DepthwiseConv2dNative")
  546. {
  547. fprintf(pp, "%-16s", "ConvolutionDepthWise");
  548. }
  549. else if (op == "tf.Identity")
  550. {
  551. fprintf(pp, "%-16s", "Noop");
  552. }
  553. else if (op == "tf.LeakyRelu")
  554. {
  555. fprintf(pp, "%-16s", "ReLU");
  556. }
  557. else if (op == "tf.MatMul")
  558. {
  559. int transpose_a = get_operation_attr_b(operation, "transpose_a");
  560. int transpose_b = get_operation_attr_b(operation, "transpose_b");
  561. if (transpose_a == 0 && transpose_b == 1)
  562. {
  563. // InnerProduct-like A * B + C
  564. fprintf(pp, "%-16s", "InnerProduct");
  565. }
  566. else
  567. {
  568. fprintf(pp, "%-16s", "Gemm");
  569. }
  570. }
  571. else if (op == "tf.Maximum")
  572. {
  573. fprintf(pp, "%-16s", "BinaryOp");
  574. }
  575. else if (op == "tf.MaxPool")
  576. {
  577. fprintf(pp, "%-16s", "Pooling");
  578. }
  579. else if (op == "tf.Mean")
  580. {
  581. std::string reduction_indices_name = get_mlir_value_uniq_id(operation.getOperand(1));
  582. const mlir::Attribute& R = weights[reduction_indices_name];
  583. std::vector<int> v = get_attr_ai(R);
  584. int keep_dims = get_operation_attr_b(operation, "keep_dims");
  585. if (keep_dims == 0 && v.size() == 2 && v[0] == 1 && v[1] == 2)
  586. {
  587. // global avg pooling style nhwc -> nc
  588. fprintf(pp, "%-16s", "Pooling");
  589. }
  590. else
  591. {
  592. fprintf(pp, "%-16s", "Reduction");
  593. }
  594. }
  595. else if (op == "tf.Minimum")
  596. {
  597. fprintf(pp, "%-16s", "BinaryOp");
  598. }
  599. else if (op == "tf.Mul")
  600. {
  601. fprintf(pp, "%-16s", "BinaryOp");
  602. }
  603. else if (op == "tf.Pad")
  604. {
  605. fprintf(pp, "%-16s", "Padding");
  606. }
  607. else if (op == "tf.Placeholder")
  608. {
  609. fprintf(pp, "%-16s", "Input");
  610. }
  611. else if (op == "tf.Relu")
  612. {
  613. fprintf(pp, "%-16s", "ReLU");
  614. }
  615. else if (op == "tf.Relu6")
  616. {
  617. fprintf(pp, "%-16s", "Clip");
  618. }
  619. else if (op == "tf.Reshape")
  620. {
  621. fprintf(pp, "%-16s", "Reshape");
  622. }
  623. else if (op == "tf.ResizeBilinear")
  624. {
  625. fprintf(pp, "%-16s", "Interp");
  626. }
  627. else if (op == "tf.ResizeNearestNeighbor")
  628. {
  629. fprintf(pp, "%-16s", "Interp");
  630. }
  631. else if (op == "tf.Sigmoid")
  632. {
  633. fprintf(pp, "%-16s", "Sigmoid");
  634. }
  635. else if (op == "tf.Softmax")
  636. {
  637. fprintf(pp, "%-16s", "Softmax");
  638. }
  639. else if (op == "tf.SpaceToDepth")
  640. {
  641. fprintf(pp, "%-16s", "Reorg");
  642. }
  643. else if (op == "tf.StridedSlice")
  644. {
  645. fprintf(pp, "%-16s", "Crop");
  646. }
  647. else if (op == "tf.Sub")
  648. {
  649. fprintf(pp, "%-16s", "BinaryOp");
  650. }
  651. else if (op == "tf.Tanh")
  652. {
  653. fprintf(pp, "%-16s", "TanH");
  654. }
  655. else
  656. {
  657. // TODO
  658. fprintf(stderr, "%s not supported yet!\n", op.c_str());
  659. fprintf(pp, "%-16s", op.c_str());
  660. }
  661. char opid_name[64];
  662. sprintf(opid_name, "op_%d", opid);
  663. fprintf(pp, " %-24s %d %d", opid_name, num_input, num_output);
  664. for (int i = 0; i < (int)operation.getNumOperands(); i++)
  665. {
  666. std::string input_name = get_mlir_value_uniq_id(operation.getOperand(i));
  667. // check weight
  668. if (weights.find(input_name) != weights.end() && node_reference[input_name] == 0)
  669. {
  670. continue;
  671. }
  672. if (split_node_reference.find(input_name) != split_node_reference.end())
  673. {
  674. int refidx = split_node_reference[input_name] - 1;
  675. split_node_reference[input_name] = refidx;
  676. char splitsuffix[256];
  677. sprintf(splitsuffix, "_splitncnn_%d", refidx);
  678. input_name = input_name + splitsuffix;
  679. }
  680. fprintf(pp, " %s", input_name.c_str());
  681. }
  682. for (int i = 0; i < num_output; i++)
  683. {
  684. std::string output_name = get_mlir_value_uniq_id(operation.getResult(i));
  685. fprintf(pp, " %s", output_name.c_str());
  686. }
  687. if (op == "std.return")
  688. {
  689. }
  690. else if (op == "ncnn.BinaryOp")
  691. {
  692. int op_type = get_operation_attr_i(operation, "op_type");
  693. int with_scalar = get_operation_attr_i(operation, "with_scalar");
  694. float b = get_operation_attr_f(operation, "b");
  695. fprintf(pp, " 0=%d", op_type);
  696. fprintf(pp, " 1=%d", with_scalar);
  697. fprintf(pp, " 2=%e", b);
  698. }
  699. else if (op == "ncnn.KerasConv2D")
  700. {
  701. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  702. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  703. const mlir::Attribute& W = weights[weight_name];
  704. const mlir::Attribute& B = weights[bias_name];
  705. llvm::ArrayRef<int64_t> shape = W.getType().cast<mlir::RankedTensorType>().getShape();
  706. // assert(shape.size() == 4)
  707. // kh-kw-inch-outch
  708. int kernel_size_h = shape[0];
  709. int kernel_size_w = shape[1];
  710. int num_input = shape[2];
  711. int num_output = shape[3];
  712. int weight_data_size = kernel_size_h * kernel_size_w * num_input * num_output;
  713. fprintf(pp, " 0=%d", num_output);
  714. fprintf(pp, " 1=%d", kernel_size_w);
  715. fprintf(pp, " 11=%d", kernel_size_h);
  716. fprintf(pp, " 6=%d", weight_data_size);
  717. std::vector<int> dilations = get_operation_attr_ai(operation, "dilations");
  718. std::vector<int> strides = get_operation_attr_ai(operation, "strides");
  719. std::string padding = get_operation_attr_s(operation, "padding");
  720. if (dilations.size() == 4)
  721. {
  722. fprintf(pp, " 2=%d", dilations[2]);
  723. fprintf(pp, " 12=%d", dilations[1]);
  724. }
  725. if (strides.size() == 4)
  726. {
  727. fprintf(pp, " 3=%d", strides[2]);
  728. fprintf(pp, " 13=%d", strides[1]);
  729. }
  730. if (padding == "EXPLICIT")
  731. {
  732. // nhwc = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
  733. std::vector<int> explicit_paddings = get_operation_attr_ai(operation, "explicit_paddings");
  734. fprintf(pp, " 4=%d", explicit_paddings[4]);
  735. fprintf(pp, " 15=%d", explicit_paddings[5]);
  736. fprintf(pp, " 14=%d", explicit_paddings[2]);
  737. fprintf(pp, " 16=%d", explicit_paddings[3]);
  738. }
  739. else if (padding == "VALID")
  740. {
  741. fprintf(pp, " 4=%d", 0);
  742. }
  743. else if (padding == "SAME")
  744. {
  745. fprintf(pp, " 4=%d", -233);
  746. }
  747. fprintf(pp, " 5=1"); // bias_term
  748. std::vector<float> v = get_attr_af(W);
  749. std::vector<float> bv = get_attr_af(B);
  750. // reorder h-w-i-o to o-i-h-w
  751. {
  752. int quantize_tag = 0;
  753. fwrite(&quantize_tag, sizeof(int), 1, bp);
  754. float tmp;
  755. for (int p = 0; p < num_output; p++)
  756. {
  757. for (int q = 0; q < num_input; q++)
  758. {
  759. for (int i = 0; i < kernel_size_h; i++)
  760. {
  761. for (int j = 0; j < kernel_size_w; j++)
  762. {
  763. tmp = v[i * kernel_size_w * num_input * num_output + j * num_input * num_output + q * num_output + p];
  764. fwrite(&tmp, sizeof(float), 1, bp);
  765. }
  766. }
  767. }
  768. }
  769. }
  770. fwrite(bv.data(), sizeof(float), bv.size(), bp);
  771. }
  772. else if (op == "ncnn.KerasDense")
  773. {
  774. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  775. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  776. const mlir::Attribute& W = weights[weight_name];
  777. const mlir::Attribute& B = weights[bias_name];
  778. llvm::ArrayRef<int64_t> shape = W.getType().cast<mlir::RankedTensorType>().getShape();
  779. // assert(shape.size() == 2)
  780. // inch-outch
  781. int num_input = shape[0];
  782. int num_output = shape[1];
  783. int weight_data_size = shape[0] * shape[1];
  784. fprintf(pp, " 0=%d", num_output);
  785. fprintf(pp, " 1=1"); // bias_term
  786. fprintf(pp, " 2=%d", weight_data_size);
  787. std::vector<float> v = get_attr_af(W);
  788. std::vector<float> bv = get_attr_af(B);
  789. // reorder i-o to o-i
  790. {
  791. int quantize_tag = 0;
  792. fwrite(&quantize_tag, sizeof(int), 1, bp);
  793. float tmp;
  794. for (int p = 0; p < num_output; p++)
  795. {
  796. for (int q = 0; q < num_input; q++)
  797. {
  798. tmp = v[q * num_output + p];
  799. fwrite(&tmp, sizeof(float), 1, bp);
  800. }
  801. }
  802. }
  803. fwrite(bv.data(), sizeof(float), bv.size(), bp);
  804. }
  805. else if (op == "ncnn.KerasBatchNorm")
  806. {
  807. std::string gamma_name = get_mlir_value_uniq_id(operation.getOperand(1));
  808. std::string bias_name = get_mlir_value_uniq_id(operation.getOperand(2));
  809. const mlir::Attribute& W = weights[gamma_name];
  810. const mlir::Attribute& B = weights[bias_name];
  811. std::vector<float> v = get_attr_af(W);
  812. std::vector<float> bv = get_attr_af(B);
  813. int channels = v.size();
  814. fprintf(pp, " 0=%d", channels);
  815. std::vector<float> mean(channels, 0.f);
  816. std::vector<float> var(channels, 1.f);
  817. fwrite(v.data(), sizeof(float), channels, bp);
  818. fwrite(mean.data(), sizeof(float), channels, bp);
  819. fwrite(var.data(), sizeof(float), channels, bp);
  820. fwrite(bv.data(), sizeof(float), channels, bp);
  821. }
  822. else if (op == "ncnn.InstanceNorm")
  823. {
  824. float eps = get_operation_attr_f(operation, "epsilon");
  825. fprintf(pp, " 0=0"); // channels
  826. fprintf(pp, " 1=%e", eps);
  827. fprintf(pp, " 2=0"); // affine
  828. }
  829. else if (op == "ncnn.InstanceNormAffine")
  830. {
  831. float eps = get_operation_attr_f(operation, "epsilon");
  832. std::string gamma_name = get_mlir_value_uniq_id(operation.getOperand(1));
  833. std::string beta_name = get_mlir_value_uniq_id(operation.getOperand(2));
  834. const mlir::Attribute& G = weights[gamma_name];
  835. const mlir::Attribute& B = weights[beta_name];
  836. std::vector<float> gv = get_attr_af(G);
  837. std::vector<float> bv = get_attr_af(B);
  838. int channels = gv.size();
  839. fprintf(pp, " 0=%d", channels);
  840. fprintf(pp, " 1=%e", eps);
  841. fprintf(pp, " 2=1"); // affine
  842. fwrite(gv.data(), sizeof(float), gv.size(), bp);
  843. fwrite(bv.data(), sizeof(float), bv.size(), bp);
  844. }
  845. else if (op == "ncnn.Swish")
  846. {
  847. // no param
  848. }
  849. else if (op == "tf.AddN")
  850. {
  851. int op_type = 1;
  852. fprintf(pp, " 0=%d", op_type);
  853. }
  854. else if (op == "tf.AddV2")
  855. {
  856. int op_type = 0;
  857. fprintf(pp, " 0=%d", op_type);
  858. }
  859. else if (op == "tf.AvgPool")
  860. {
  861. std::vector<int> ksize = get_operation_attr_ai(operation, "ksize");
  862. std::vector<int> strides = get_operation_attr_ai(operation, "strides");
  863. std::string padding = get_operation_attr_s(operation, "padding");
  864. fprintf(pp, " 0=1"); // avg pool
  865. if (ksize.size() == 4)
  866. {
  867. fprintf(pp, " 1=%d", ksize[2]);
  868. fprintf(pp, " 11=%d", ksize[1]);
  869. }
  870. if (strides.size() == 4)
  871. {
  872. fprintf(pp, " 2=%d", strides[2]);
  873. fprintf(pp, " 12=%d", strides[1]);
  874. }
  875. int pad_mode = 1;
  876. if (padding == "VALID")
  877. {
  878. pad_mode = 1;
  879. }
  880. else if (padding == "SAME")
  881. {
  882. pad_mode = 2;
  883. }
  884. fprintf(pp, " 5=%d", pad_mode);
  885. }
  886. else if (op == "tf.ConcatV2")
  887. {
  888. std::string axis_name = get_mlir_value_uniq_id(operation.getOperand(operation.getNumOperands() - 1));
  889. const mlir::Attribute& A = weights[axis_name];
  890. int axis = get_attr_ai(A)[0];
  891. // axis nhc to nhw
  892. // axis nhwc to nchw
  893. int dims = operation.getOperand(0).getType().cast<mlir::RankedTensorType>().getShape().size();
  894. if (dims == 2 && axis == 1)
  895. {
  896. axis = 0;
  897. }
  898. if (dims == 3 && axis == 1)
  899. {
  900. axis = 1;
  901. }
  902. if (dims == 3 && axis == 2)
  903. {
  904. axis = 0;
  905. }
  906. if (dims == 4 && axis == 1)
  907. {
  908. axis = 1;
  909. }
  910. if (dims == 4 && axis == 2)
  911. {
  912. axis = 2;
  913. }
  914. if (dims == 4 && axis == 3)
  915. {
  916. axis = 0;
  917. }
  918. fprintf(pp, " 0=%d", axis);
  919. }
  920. else if (op == "tf.Const")
  921. {
  922. // never reach here
  923. }
  924. else if (op == "tf.Conv2D")
  925. {
  926. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  927. const mlir::Attribute& W = weights[weight_name];
  928. llvm::ArrayRef<int64_t> shape = W.getType().cast<mlir::RankedTensorType>().getShape();
  929. // assert(shape.size() == 4)
  930. // kh-kw-inch-outch
  931. int kernel_size_h = shape[0];
  932. int kernel_size_w = shape[1];
  933. int num_input = shape[2];
  934. int num_output = shape[3];
  935. int weight_data_size = kernel_size_h * kernel_size_w * num_input * num_output;
  936. fprintf(pp, " 0=%d", num_output);
  937. fprintf(pp, " 1=%d", kernel_size_w);
  938. fprintf(pp, " 11=%d", kernel_size_h);
  939. fprintf(pp, " 6=%d", weight_data_size);
  940. std::vector<int> dilations = get_operation_attr_ai(operation, "dilations");
  941. std::vector<int> strides = get_operation_attr_ai(operation, "strides");
  942. std::string padding = get_operation_attr_s(operation, "padding");
  943. if (dilations.size() == 4)
  944. {
  945. fprintf(pp, " 2=%d", dilations[2]);
  946. fprintf(pp, " 12=%d", dilations[1]);
  947. }
  948. if (strides.size() == 4)
  949. {
  950. fprintf(pp, " 3=%d", strides[2]);
  951. fprintf(pp, " 13=%d", strides[1]);
  952. }
  953. if (padding == "EXPLICIT")
  954. {
  955. // nhwc = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
  956. std::vector<int> explicit_paddings = get_operation_attr_ai(operation, "explicit_paddings");
  957. fprintf(pp, " 4=%d", explicit_paddings[4]);
  958. fprintf(pp, " 15=%d", explicit_paddings[5]);
  959. fprintf(pp, " 14=%d", explicit_paddings[2]);
  960. fprintf(pp, " 16=%d", explicit_paddings[3]);
  961. }
  962. else if (padding == "VALID")
  963. {
  964. fprintf(pp, " 4=%d", 0);
  965. }
  966. else if (padding == "SAME")
  967. {
  968. fprintf(pp, " 4=%d", -233);
  969. }
  970. std::vector<float> v = get_attr_af(W);
  971. // reorder h-w-i-o to o-i-h-w
  972. {
  973. int quantize_tag = 0;
  974. fwrite(&quantize_tag, sizeof(int), 1, bp);
  975. float tmp;
  976. for (int p = 0; p < num_output; p++)
  977. {
  978. for (int q = 0; q < num_input; q++)
  979. {
  980. for (int i = 0; i < kernel_size_h; i++)
  981. {
  982. for (int j = 0; j < kernel_size_w; j++)
  983. {
  984. tmp = v[i * kernel_size_w * num_input * num_output + j * num_input * num_output + q * num_output + p];
  985. fwrite(&tmp, sizeof(float), 1, bp);
  986. }
  987. }
  988. }
  989. }
  990. }
  991. }
  992. else if (op == "tf.Conv2DBackpropInput")
  993. {
  994. std::string output_shape_name = get_mlir_value_uniq_id(operation.getOperand(0));
  995. const std::vector<int> output_shape = get_attr_ai(weights[output_shape_name]);
  996. // assert(output_shape.size() == 4)
  997. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  998. const mlir::Attribute& W = weights[weight_name];
  999. llvm::ArrayRef<int64_t> shape = W.getType().cast<mlir::RankedTensorType>().getShape();
  1000. // assert(shape.size() == 4)
  1001. // kh-kw-outch-inch
  1002. int kernel_size_h = shape[0];
  1003. int kernel_size_w = shape[1];
  1004. int num_output = shape[2];
  1005. int num_input = shape[3];
  1006. int weight_data_size = kernel_size_h * kernel_size_w * num_input * num_output;
  1007. fprintf(pp, " 0=%d", num_output);
  1008. fprintf(pp, " 1=%d", kernel_size_w);
  1009. fprintf(pp, " 11=%d", kernel_size_h);
  1010. fprintf(pp, " 6=%d", weight_data_size);
  1011. std::vector<int> dilations = get_operation_attr_ai(operation, "dilations");
  1012. std::vector<int> strides = get_operation_attr_ai(operation, "strides");
  1013. std::string padding = get_operation_attr_s(operation, "padding");
  1014. if (dilations.size() == 4)
  1015. {
  1016. fprintf(pp, " 2=%d", dilations[2]);
  1017. fprintf(pp, " 12=%d", dilations[1]);
  1018. }
  1019. if (strides.size() == 4)
  1020. {
  1021. fprintf(pp, " 3=%d", strides[2]);
  1022. fprintf(pp, " 13=%d", strides[1]);
  1023. }
  1024. if (padding == "EXPLICIT")
  1025. {
  1026. // nhwc = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
  1027. std::vector<int> explicit_paddings = get_operation_attr_ai(operation, "explicit_paddings");
  1028. fprintf(pp, " 4=%d", explicit_paddings[4]);
  1029. fprintf(pp, " 15=%d", explicit_paddings[5]);
  1030. fprintf(pp, " 14=%d", explicit_paddings[2]);
  1031. fprintf(pp, " 16=%d", explicit_paddings[3]);
  1032. }
  1033. else if (padding == "VALID")
  1034. {
  1035. fprintf(pp, " 4=%d", 0);
  1036. }
  1037. else if (padding == "SAME")
  1038. {
  1039. fprintf(pp, " 4=%d", -233);
  1040. fprintf(pp, " 20=%d", output_shape[2]);
  1041. fprintf(pp, " 21=%d", output_shape[1]);
  1042. }
  1043. std::vector<float> v = get_attr_af(W);
  1044. // reorder h-w-o-i to o-i-h-w
  1045. {
  1046. int quantize_tag = 0;
  1047. fwrite(&quantize_tag, sizeof(int), 1, bp);
  1048. float tmp;
  1049. for (int p = 0; p < num_output; p++)
  1050. {
  1051. for (int q = 0; q < num_input; q++)
  1052. {
  1053. for (int i = 0; i < kernel_size_h; i++)
  1054. {
  1055. for (int j = 0; j < kernel_size_w; j++)
  1056. {
  1057. tmp = v[i * kernel_size_w * num_output * num_input + j * num_output * num_input + p * num_input + q];
  1058. fwrite(&tmp, sizeof(float), 1, bp);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. else if (op == "tf.DepthToSpace")
  1066. {
  1067. int block_size = get_operation_attr_i(operation, "block_size");
  1068. fprintf(pp, " 0=%d", block_size);
  1069. fprintf(pp, " 1=1"); // mode
  1070. }
  1071. else if (op == "tf.DepthwiseConv2dNative")
  1072. {
  1073. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1074. const mlir::Attribute& W = weights[weight_name];
  1075. llvm::ArrayRef<int64_t> shape = W.getType().cast<mlir::RankedTensorType>().getShape();
  1076. // assert(shape.size() == 4)
  1077. // kh-kw-inch-cm
  1078. int kernel_size_h = shape[0];
  1079. int kernel_size_w = shape[1];
  1080. int num_input = shape[2];
  1081. int channel_multiplier = shape[3];
  1082. int num_output = num_input * channel_multiplier;
  1083. int group = num_input;
  1084. int weight_data_size = kernel_size_h * kernel_size_w * num_input * channel_multiplier;
  1085. fprintf(pp, " 0=%d", num_output);
  1086. fprintf(pp, " 1=%d", kernel_size_w);
  1087. fprintf(pp, " 11=%d", kernel_size_h);
  1088. fprintf(pp, " 6=%d", weight_data_size);
  1089. fprintf(pp, " 7=%d", group);
  1090. std::vector<int> dilations = get_operation_attr_ai(operation, "dilations");
  1091. std::vector<int> strides = get_operation_attr_ai(operation, "strides");
  1092. std::string padding = get_operation_attr_s(operation, "padding");
  1093. if (dilations.size() == 4)
  1094. {
  1095. fprintf(pp, " 2=%d", dilations[2]);
  1096. fprintf(pp, " 12=%d", dilations[1]);
  1097. }
  1098. if (strides.size() == 4)
  1099. {
  1100. fprintf(pp, " 3=%d", strides[2]);
  1101. fprintf(pp, " 13=%d", strides[1]);
  1102. }
  1103. if (padding == "EXPLICIT")
  1104. {
  1105. // nhwc = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
  1106. std::vector<int> explicit_paddings = get_operation_attr_ai(operation, "explicit_paddings");
  1107. fprintf(pp, " 4=%d", explicit_paddings[4]);
  1108. fprintf(pp, " 15=%d", explicit_paddings[5]);
  1109. fprintf(pp, " 14=%d", explicit_paddings[2]);
  1110. fprintf(pp, " 16=%d", explicit_paddings[3]);
  1111. }
  1112. else if (padding == "VALID")
  1113. {
  1114. fprintf(pp, " 4=%d", 0);
  1115. }
  1116. else if (padding == "SAME")
  1117. {
  1118. fprintf(pp, " 4=%d", -233);
  1119. }
  1120. std::vector<float> v = get_attr_af(W);
  1121. // reorder h-w-i-cm to i-cm-h-w
  1122. {
  1123. int quantize_tag = 0;
  1124. fwrite(&quantize_tag, sizeof(int), 1, bp);
  1125. float tmp;
  1126. for (int p = 0; p < num_input; p++)
  1127. {
  1128. for (int q = 0; q < channel_multiplier; q++)
  1129. {
  1130. for (int i = 0; i < kernel_size_h; i++)
  1131. {
  1132. for (int j = 0; j < kernel_size_w; j++)
  1133. {
  1134. tmp = v[i * kernel_size_w * channel_multiplier * num_input + j * channel_multiplier * num_input + p * channel_multiplier + q];
  1135. fwrite(&tmp, sizeof(float), 1, bp);
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. else if (op == "tf.Identity")
  1143. {
  1144. }
  1145. else if (op == "tf.LeakyRelu")
  1146. {
  1147. float alpha = get_operation_attr_f(operation, "alpha");
  1148. fprintf(pp, " 0=%e", alpha);
  1149. }
  1150. else if (op == "tf.MatMul")
  1151. {
  1152. int transpose_a = get_operation_attr_b(operation, "transpose_a");
  1153. int transpose_b = get_operation_attr_b(operation, "transpose_b");
  1154. if (transpose_a == 0 && transpose_b == 1)
  1155. {
  1156. // InnerProduct-like A * B + C
  1157. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1158. const mlir::Attribute& W = weights[weight_name];
  1159. llvm::ArrayRef<int64_t> shape = W.getType().cast<mlir::RankedTensorType>().getShape();
  1160. // assert(shape.size() == 2)
  1161. // inch-outch
  1162. int num_input = shape[0];
  1163. int num_output = shape[1];
  1164. int weight_data_size = shape[0] * shape[1];
  1165. fprintf(pp, " 0=%d", num_output);
  1166. fprintf(pp, " 2=%d", weight_data_size);
  1167. std::vector<float> v = get_attr_af(W);
  1168. // reorder i-o to o-i
  1169. {
  1170. int quantize_tag = 0;
  1171. fwrite(&quantize_tag, sizeof(int), 1, bp);
  1172. float tmp;
  1173. for (int p = 0; p < num_output; p++)
  1174. {
  1175. for (int q = 0; q < num_input; q++)
  1176. {
  1177. tmp = v[q * num_output + p];
  1178. fwrite(&tmp, sizeof(float), 1, bp);
  1179. }
  1180. }
  1181. }
  1182. }
  1183. else
  1184. {
  1185. // gemm
  1186. fprintf(pp, " 0=1.0"); // alpha
  1187. fprintf(pp, " 1=1.0"); // beta
  1188. fprintf(pp, " 2=%d", transpose_a);
  1189. fprintf(pp, " 3=%d", transpose_b);
  1190. }
  1191. }
  1192. else if (op == "tf.Maximum")
  1193. {
  1194. int op_type = 4;
  1195. fprintf(pp, " 0=%d", op_type);
  1196. }
  1197. else if (op == "tf.MaxPool")
  1198. {
  1199. std::vector<int> ksize = get_operation_attr_ai(operation, "ksize");
  1200. std::vector<int> strides = get_operation_attr_ai(operation, "strides");
  1201. std::string padding = get_operation_attr_s(operation, "padding");
  1202. fprintf(pp, " 0=0"); // max pool
  1203. if (ksize.size() == 4)
  1204. {
  1205. fprintf(pp, " 1=%d", ksize[2]);
  1206. fprintf(pp, " 11=%d", ksize[1]);
  1207. }
  1208. if (strides.size() == 4)
  1209. {
  1210. fprintf(pp, " 2=%d", strides[2]);
  1211. fprintf(pp, " 12=%d", strides[1]);
  1212. }
  1213. int pad_mode = 1;
  1214. if (padding == "VALID")
  1215. {
  1216. pad_mode = 1;
  1217. }
  1218. else if (padding == "SAME")
  1219. {
  1220. pad_mode = 2;
  1221. }
  1222. fprintf(pp, " 5=%d", pad_mode);
  1223. }
  1224. else if (op == "tf.Mean")
  1225. {
  1226. std::string reduction_indices_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1227. const mlir::Attribute& R = weights[reduction_indices_name];
  1228. std::vector<int> v = get_attr_ai(R);
  1229. int keep_dims = get_operation_attr_b(operation, "keep_dims");
  1230. if (keep_dims == 0 && v.size() == 2 && v[0] == 1 && v[1] == 2)
  1231. {
  1232. // global avg pooling style nhwc -> nc
  1233. int pool = 1;
  1234. int global_pool = 1;
  1235. fprintf(pp, " 0=%d", pool);
  1236. fprintf(pp, " 4=%d", global_pool);
  1237. }
  1238. else
  1239. {
  1240. // Reduction mean
  1241. fprintf(pp, " 0=3");
  1242. fprintf(pp, " 1=0"); // reduce_all
  1243. fprintf(pp, " -23303=%d", (int)v.size());
  1244. for (int i = 0; i < (int)v.size(); i++)
  1245. {
  1246. if (v[i] == 1)
  1247. fprintf(pp, ",1");
  1248. if (v[i] == 2)
  1249. fprintf(pp, ",2");
  1250. if (v[i] == 3)
  1251. fprintf(pp, ",0");
  1252. }
  1253. fprintf(pp, " 4=%d", keep_dims);
  1254. fprintf(pp, " 5=1");
  1255. }
  1256. }
  1257. else if (op == "tf.Minimum")
  1258. {
  1259. int op_type = 5;
  1260. fprintf(pp, " 0=%d", op_type);
  1261. }
  1262. else if (op == "tf.Mul")
  1263. {
  1264. int op_type = 2;
  1265. fprintf(pp, " 0=%d", op_type);
  1266. }
  1267. else if (op == "tf.Pad")
  1268. {
  1269. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1270. const mlir::Attribute& P = weights[weight_name];
  1271. std::vector<int> v = get_attr_ai(P);
  1272. // nhwc = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
  1273. fprintf(pp, " 0=%d", v[2]);
  1274. fprintf(pp, " 1=%d", v[3]);
  1275. fprintf(pp, " 2=%d", v[4]);
  1276. fprintf(pp, " 3=%d", v[5]);
  1277. }
  1278. else if (op == "tf.Placeholder")
  1279. {
  1280. }
  1281. else if (op == "tf.Relu")
  1282. {
  1283. }
  1284. else if (op == "tf.Relu6")
  1285. {
  1286. float min = 0.f;
  1287. float max = 6.f;
  1288. fprintf(pp, " 0=%e", min);
  1289. fprintf(pp, " 1=%e", max);
  1290. }
  1291. else if (op == "tf.Reshape")
  1292. {
  1293. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1294. const mlir::Attribute& S = weights[weight_name];
  1295. std::vector<int> v = get_attr_ai(S);
  1296. int size = v.size();
  1297. // n h w c
  1298. // n h c
  1299. // n c
  1300. if (size == 4)
  1301. {
  1302. fprintf(pp, " 0=%d 1=%d 2=%d", v[2], v[1], v[3]);
  1303. }
  1304. if (size == 3)
  1305. {
  1306. fprintf(pp, " 0=%d 1=%d 2=-233", v[1], v[2]);
  1307. }
  1308. if (size == 2)
  1309. {
  1310. fprintf(pp, " 0=%d 1=-233 2=-233", v[1]);
  1311. }
  1312. // FIXME may not always be the case
  1313. fprintf(pp, " 3=1");
  1314. }
  1315. else if (op == "tf.ResizeBilinear")
  1316. {
  1317. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1318. const mlir::Attribute& P = weights[weight_name];
  1319. std::vector<int> size = get_attr_ai(P);
  1320. int align_corners = get_operation_attr_b(operation, "align_corners");
  1321. int half_pixel_centers = get_operation_attr_b(operation, "half_pixel_centers");
  1322. if (!(align_corners == 0 && half_pixel_centers == 1))
  1323. {
  1324. fprintf(stderr, "Unsupported ResizeBilinear align_corners %d half_pixel_centers %d !\n", align_corners, half_pixel_centers);
  1325. }
  1326. fprintf(pp, " 0=2"); // bilinear
  1327. fprintf(pp, " 3=%d 4=%d", size[1], size[0]);
  1328. }
  1329. else if (op == "tf.ResizeNearestNeighbor")
  1330. {
  1331. std::string weight_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1332. const mlir::Attribute& P = weights[weight_name];
  1333. std::vector<int> size = get_attr_ai(P);
  1334. int align_corners = get_operation_attr_b(operation, "align_corners");
  1335. int half_pixel_centers = get_operation_attr_b(operation, "half_pixel_centers");
  1336. if (!(align_corners == 0 && half_pixel_centers == 1))
  1337. {
  1338. fprintf(stderr, "Unsupported ResizeNearestNeighbor align_corners %d half_pixel_centers %d !\n", align_corners, half_pixel_centers);
  1339. }
  1340. fprintf(pp, " 0=1"); // nearest
  1341. fprintf(pp, " 3=%d 4=%d", size[1], size[0]);
  1342. }
  1343. else if (op == "tf.Sigmoid")
  1344. {
  1345. }
  1346. else if (op == "tf.Softmax")
  1347. {
  1348. }
  1349. else if (op == "tf.SpaceToDepth")
  1350. {
  1351. int block_size = get_operation_attr_i(operation, "block_size");
  1352. fprintf(pp, " 0=%d", block_size);
  1353. fprintf(pp, " 1=1"); // mode
  1354. }
  1355. else if (op == "tf.StridedSlice")
  1356. {
  1357. std::string begin_name = get_mlir_value_uniq_id(operation.getOperand(1));
  1358. std::string end_name = get_mlir_value_uniq_id(operation.getOperand(2));
  1359. std::string strides_name = get_mlir_value_uniq_id(operation.getOperand(3));
  1360. const mlir::Attribute& B = weights[begin_name];
  1361. const mlir::Attribute& E = weights[end_name];
  1362. const mlir::Attribute& S = weights[strides_name];
  1363. std::vector<int> begin = get_attr_ai(B);
  1364. std::vector<int> end = get_attr_ai(E);
  1365. std::vector<int> strides = get_attr_ai(S);
  1366. int begin_mask = get_operation_attr_i(operation, "begin_mask");
  1367. int end_mask = get_operation_attr_i(operation, "end_mask");
  1368. int ellipsis_mask = get_operation_attr_i(operation, "ellipsis_mask");
  1369. int new_axis_mask = get_operation_attr_i(operation, "new_axis_mask");
  1370. int shrink_axis_mask = get_operation_attr_i(operation, "shrink_axis_mask");
  1371. int dims = strides.size();
  1372. // assert strides == 1
  1373. for (int i = 0; i < dims; i++)
  1374. {
  1375. if (strides[i] != 1)
  1376. fprintf(stderr, "Unsupported StridedSlice strides !\n");
  1377. }
  1378. for (int i = 0; i < dims; i++)
  1379. {
  1380. // TODO strides[i] < 0
  1381. if (begin_mask & (1 << i))
  1382. {
  1383. begin[i] = 0;
  1384. }
  1385. if (end_mask & (1 << i))
  1386. {
  1387. end[i] = -233;
  1388. }
  1389. if (ellipsis_mask & (1 << i))
  1390. {
  1391. begin[i] = 0;
  1392. end[i] = -233;
  1393. }
  1394. }
  1395. if (new_axis_mask)
  1396. {
  1397. fprintf(stderr, "Unsupported StridedSlice new_axis_mask !\n");
  1398. }
  1399. if (shrink_axis_mask)
  1400. {
  1401. fprintf(stderr, "Unsupported StridedSlice shrink_axis_mask !\n");
  1402. }
  1403. // n h w c
  1404. // n h c
  1405. // n c
  1406. if (dims == 4)
  1407. {
  1408. fprintf(pp, " -23309=3,%d,%d,%d", begin[3], begin[1], begin[2]);
  1409. fprintf(pp, " -23310=3,%d,%d,%d", end[3], end[1], end[2]);
  1410. }
  1411. if (dims == 3)
  1412. {
  1413. fprintf(pp, " -23309=2,%d,%d", begin[2], begin[1]);
  1414. fprintf(pp, " -23310=2,%d,%d", end[2], end[1]);
  1415. }
  1416. if (dims == 2)
  1417. {
  1418. fprintf(pp, " -23309=1,%d", begin[1]);
  1419. fprintf(pp, " -23310=1,%d", end[1]);
  1420. }
  1421. }
  1422. else if (op == "tf.Sub")
  1423. {
  1424. int op_type = 1;
  1425. fprintf(pp, " 0=%d", op_type);
  1426. }
  1427. else if (op == "tf.Tanh")
  1428. {
  1429. }
  1430. #if 0
  1431. for (const mlir::NamedAttribute& attr : operation.getAttrs())
  1432. {
  1433. const mlir::Identifier& identifier = attr.first;
  1434. const mlir::Attribute& attr = attr.second;
  1435. fprintf(pp, " %s=", identifier.c_str());
  1436. if (attr.isa<mlir::AffineMapAttr>())
  1437. {
  1438. fprintf(pp, "AffineMap");
  1439. }
  1440. if (attr.isa<mlir::ArrayAttr>())
  1441. {
  1442. // fprintf(pp, "Array");
  1443. mlir::ArrayAttr a = attr.cast<mlir::ArrayAttr>();
  1444. int array_size = a.getValue().size();
  1445. for (int t=0; t<array_size; t++)
  1446. {
  1447. if (a[t].isa<mlir::IntegerAttr>())
  1448. {
  1449. int64_t ii = a[t].cast<mlir::IntegerAttr>().getInt();
  1450. fprintf(pp, "%lld,", ii);
  1451. }
  1452. }
  1453. }
  1454. if (attr.isa<mlir::BoolAttr>())
  1455. {
  1456. // fprintf(pp, "Bool");
  1457. mlir::BoolAttr a = attr.cast<mlir::BoolAttr>();
  1458. fprintf(pp, "%d", a.getValue() ? 1 : 0);
  1459. }
  1460. if (attr.isa<mlir::DictionaryAttr>())
  1461. {
  1462. fprintf(pp, "Dictionary");
  1463. }
  1464. if (attr.isa<mlir::FloatAttr>())
  1465. {
  1466. fprintf(pp, "Float");
  1467. }
  1468. if (attr.isa<mlir::IntegerAttr>())
  1469. {
  1470. fprintf(pp, "Integer");
  1471. }
  1472. if (attr.isa<mlir::IntegerSetAttr>())
  1473. {
  1474. fprintf(pp, "IntegerSet");
  1475. }
  1476. if (attr.isa<mlir::OpaqueAttr>())
  1477. {
  1478. fprintf(pp, "Opaque");
  1479. }
  1480. if (attr.isa<mlir::StringAttr>())
  1481. {
  1482. // fprintf(pp, "String");
  1483. mlir::StringAttr s = attr.cast<mlir::StringAttr>();
  1484. fprintf(pp, "%s", s.getValue().empty() ? "" : s.getValue().data());
  1485. }
  1486. if (attr.isa<mlir::SymbolRefAttr>())
  1487. {
  1488. fprintf(pp, "SymbolRef");
  1489. }
  1490. if (attr.isa<mlir::FlatSymbolRefAttr>())
  1491. {
  1492. fprintf(pp, "FlatSymbolRef");
  1493. }
  1494. if (attr.isa<mlir::TypeAttr>())
  1495. {
  1496. fprintf(pp, "Type");
  1497. }
  1498. if (attr.isa<mlir::UnitAttr>())
  1499. {
  1500. fprintf(pp, "Unit");
  1501. }
  1502. if (attr.isa<mlir::ElementsAttr>())
  1503. {
  1504. fprintf(pp, "Elements");
  1505. }
  1506. if (attr.isa<mlir::DenseElementsAttr>())
  1507. {
  1508. fprintf(pp, "DenseElements");
  1509. }
  1510. if (attr.isa<mlir::DenseFPElementsAttr>())
  1511. {
  1512. fprintf(pp, "DenseFPElements");
  1513. }
  1514. if (attr.isa<mlir::DenseIntElementsAttr>())
  1515. {
  1516. fprintf(pp, "DenseIntElements");
  1517. }
  1518. if (attr.isa<mlir::OpaqueElementsAttr>())
  1519. {
  1520. fprintf(pp, "OpaqueElements");
  1521. }
  1522. if (attr.isa<mlir::SparseElementsAttr>())
  1523. {
  1524. fprintf(pp, "SparseElements");
  1525. }
  1526. if (attr.isa<mlir::SplatElementsAttr>())
  1527. {
  1528. fprintf(pp, "SplatElements");
  1529. }
  1530. }
  1531. #endif
  1532. fprintf(pp, "\n");
  1533. for (int j = 0; j < num_output; j++)
  1534. {
  1535. std::string output_name = get_mlir_value_uniq_id(operation.getResult(j));
  1536. if (node_reference.find(output_name) != node_reference.end())
  1537. {
  1538. int refcount = node_reference[output_name];
  1539. if (refcount > 1)
  1540. {
  1541. char splitname[256];
  1542. sprintf(splitname, "splitncnn_%d", internal_split);
  1543. fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
  1544. fprintf(pp, " %s", output_name.c_str());
  1545. for (int k = 0; k < refcount; k++)
  1546. {
  1547. fprintf(pp, " %s_splitncnn_%d", output_name.c_str(), k);
  1548. }
  1549. fprintf(pp, "\n");
  1550. internal_split++;
  1551. }
  1552. }
  1553. }
  1554. }
  1555. fclose(pp);
  1556. fclose(bp);
  1557. return 0;
  1558. }