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.

anf_ir_utils.cc 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /**
  2. * Copyright 2019-2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "debug/anf_ir_utils.h"
  17. #include <fstream>
  18. #include <map>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <algorithm>
  22. #include <iomanip>
  23. #include "ir/graph_utils.h"
  24. #include "utils/symbolic.h"
  25. #include "ir/meta_func_graph.h"
  26. #include "ir/param_info.h"
  27. #include "pybind_api/ir/tensor_py.h"
  28. #include "pipeline/jit/parse/python_adapter.h"
  29. #include "pipeline/jit/parse/resolve.h"
  30. #include "frontend/operator/composite/composite.h"
  31. #include "frontend/operator/composite/map.h"
  32. #include "utils/ordered_map.h"
  33. #include "utils/ordered_set.h"
  34. #include "utils/utils.h"
  35. #include "utils/shape_utils.h"
  36. #include "debug/trace.h"
  37. #include "utils/label.h"
  38. #include "utils/ms_context.h"
  39. #include "frontend/operator/ops.h"
  40. #include "pipeline/jit/base.h"
  41. #include "debug/common.h"
  42. using mindspore::tensor::TensorPy;
  43. namespace mindspore {
  44. std::string GetKernelNodeName(const AnfNodePtr &anf_node) {
  45. std::string kernel_name = anf_node->fullname_with_scope();
  46. if (kernel_name.empty()) {
  47. kernel_name = anf_node->ToString();
  48. }
  49. MS_LOG(DEBUG) << "Full scope kernel name is " << kernel_name << ".";
  50. return kernel_name;
  51. }
  52. // ============================================= MindSpore IR Exporter =============================================
  53. std::string AnfExporter::GetNodeType(const AnfNodePtr &nd) {
  54. ValuePtr tensor_value = nullptr;
  55. auto abstract = nd->abstract();
  56. if (abstract != nullptr && abstract->isa<abstract::AbstractTensor>()) {
  57. tensor_value = abstract->BuildValue();
  58. }
  59. abstract::ShapePtr shape = nd->Shape() == nullptr ? nullptr : dyn_cast<abstract::Shape>(nd->Shape());
  60. TypePtr type = dyn_cast<Type>(nd->Type());
  61. std::ostringstream oss;
  62. if ((shape != nullptr) && (type != nullptr)) {
  63. oss << type->DumpText() << shape->DumpText();
  64. if (tensor_value != nullptr && tensor_value != kAnyValue) {
  65. oss << "(...)";
  66. }
  67. } else if (type != nullptr) {
  68. oss << type->DumpText();
  69. if (tensor_value != nullptr && tensor_value != kAnyValue) {
  70. oss << "(...)";
  71. }
  72. } else {
  73. oss << "Undefined";
  74. }
  75. return oss.str();
  76. }
  77. int AnfExporter::GetParamIndex(const FuncGraphPtr &func_graph, const AnfNodePtr &param, bool throw_excp) {
  78. if (func_graph == nullptr || param == nullptr) {
  79. return -1;
  80. }
  81. FuncGraphPtr fg = func_graph;
  82. while (fg != nullptr) {
  83. if (exported.find(fg) == exported.end()) {
  84. if (!check_integrity_) {
  85. break;
  86. }
  87. MS_LOG(EXCEPTION) << "Can not find func graph '" << fg->DumpText() << "'";
  88. }
  89. auto param_map = exported[fg];
  90. if (param_map.find(param) != param_map.end()) {
  91. return param_map[param];
  92. }
  93. fg = fg->parent();
  94. }
  95. if (throw_excp) {
  96. MS_LOG(EXCEPTION) << "Can not find index for param '" << param->DumpText() << "' for func graph '"
  97. << func_graph->DumpText() << "'";
  98. }
  99. return -1;
  100. }
  101. // Try to find index of parameter for SymbolicKeyInstance from all exported graphs
  102. // NOTICE: Suppose name of all parameters in SymbolicKeyInstance are different
  103. int AnfExporter::GetParamIndexFromExported(const AnfNodePtr &param) {
  104. if (param == nullptr) {
  105. return -1;
  106. }
  107. int ret = -1;
  108. for (const auto &item : exported) {
  109. auto pram_iter = item.second.find(param);
  110. if (pram_iter != item.second.end()) {
  111. return pram_iter->second;
  112. }
  113. }
  114. return ret;
  115. }
  116. std::string AnfExporter::GetValueNodeText(const FuncGraphPtr &fg, const ValueNodePtr &node) {
  117. MS_EXCEPTION_IF_NULL(node);
  118. return GetValueText(fg, node->value());
  119. }
  120. std::string AnfExporter::GetMultitypeFuncGraphText(const prim::MultitypeFuncGraphPtr &mt_func_graph) {
  121. auto py_funcs = mt_func_graph->GetPyFunctions();
  122. if (py_funcs.empty()) {
  123. return "";
  124. }
  125. std::ostringstream oss;
  126. oss << "{";
  127. bool is_first = true;
  128. for (const auto &py_func : py_funcs) {
  129. if (is_first) {
  130. is_first = false;
  131. } else {
  132. oss << ", ";
  133. }
  134. oss << "(";
  135. for (size_t i = 0; i < py_func.first.size(); ++i) {
  136. if (i > 0) {
  137. oss << ", ";
  138. }
  139. oss << py_func.first[i]->DumpText();
  140. }
  141. oss << ")";
  142. }
  143. oss << "}";
  144. return oss.str();
  145. }
  146. inline bool Skip(const MetaFuncGraphPtr &meta_func_graph) {
  147. return meta_func_graph->isa<prim::Tail>() || meta_func_graph->isa<prim::MakeTupleGradient>() ||
  148. meta_func_graph->isa<prim::MakeListGradient>() || meta_func_graph->isa<prim::TupleAdd>() ||
  149. meta_func_graph->isa<prim::TupleSlice>() || meta_func_graph->isa<prim::UnpackCall>() ||
  150. meta_func_graph->isa<prim::ZipOperation>() || meta_func_graph->isa<prim::ListAppend>() ||
  151. meta_func_graph->isa<prim::DoSignatureMetaFuncGraph>();
  152. }
  153. /* inherit relation of MetaFuncGraph
  154. *
  155. * MetaGraph
  156. * ├── MultitypeGraph
  157. * ├── HyperMap
  158. * │ └── HyperMapPy
  159. * ├── Map
  160. * │ └── MapPy
  161. * ├── Tail
  162. * ├── MakeTupleGradient
  163. * ├── MakeListGradient
  164. * ├── GradOperation
  165. * └── TupleAdd
  166. */
  167. std::string AnfExporter::GetMetaFuncGraphText(const MetaFuncGraphPtr &meta_func_graph) {
  168. if (meta_func_graph == nullptr) {
  169. return "";
  170. }
  171. std::ostringstream oss;
  172. oss << meta_func_graph->type_name() << "::" << meta_func_graph->name();
  173. if (meta_func_graph->isa<prim::MultitypeFuncGraph>()) {
  174. prim::MultitypeFuncGraphPtr mt_func_graph = meta_func_graph->cast<prim::MultitypeFuncGraphPtr>();
  175. oss << GetMultitypeFuncGraphText(mt_func_graph);
  176. } else if (meta_func_graph
  177. ->isa<prim::HyperMapPy>()) { // This statement must before 'meta_graph->isa<prim::HyperMap>()'
  178. auto hyper_map = meta_func_graph->cast<prim::HyperMapPyPtr>();
  179. if (hyper_map->GetFnLeaf() != nullptr) {
  180. oss << "{fn_leaf=" << GetMetaFuncGraphText(hyper_map->GetFnLeaf()) << "}";
  181. }
  182. } else if (meta_func_graph->isa<prim::HyperMap>()) {
  183. auto hyper_map = meta_func_graph->cast<prim::HyperMapPtr>();
  184. if (hyper_map->GetFnLeaf() != nullptr) {
  185. oss << "{fn_leaf=" << GetMetaFuncGraphText(hyper_map->GetFnLeaf()) << "}";
  186. }
  187. } else if (meta_func_graph->isa<prim::MapPy>()) { // This statement must before 'meta_graph->isa<prim::Map>()'
  188. auto map = meta_func_graph->cast<prim::MapPyPtr>();
  189. if (map->GetFnLeaf() != nullptr) {
  190. oss << "{fn_leaf=" << GetMetaFuncGraphText(map->GetFnLeaf()) << "}";
  191. }
  192. } else if (meta_func_graph->isa<prim::Map>()) {
  193. auto map = meta_func_graph->cast<prim::MapPtr>();
  194. if (map->GetFnLeaf() != nullptr) {
  195. oss << "{fn_leaf=" << GetMetaFuncGraphText(map->GetFnLeaf()) << "}";
  196. }
  197. } else if (meta_func_graph->isa<prim::GradOperation>()) {
  198. prim::GradOperationPtr grad_op = meta_func_graph->cast<prim::GradOperationPtr>();
  199. oss << "{get_all=" << grad_op->get_all_ << ", get_by_list=" << grad_op->get_by_list_
  200. << ", sens_param=" << grad_op->sens_param_ << "}";
  201. } else if (Skip(meta_func_graph)) {
  202. // Do nothing
  203. } else {
  204. MS_LOG(EXCEPTION) << "Unknown MetaFuncGraph type " << meta_func_graph->type_name();
  205. }
  206. return oss.str();
  207. }
  208. std::string AnfExporter::GetPrimitiveText(const PrimitivePtr &prim) {
  209. std::ostringstream oss;
  210. if (prim == nullptr) {
  211. return oss.str();
  212. }
  213. oss << prim->type_name() << "::" << prim->name();
  214. // Output primitive type
  215. oss << "{prim_type=" << static_cast<int>(prim->prim_type()) << "}";
  216. // Output primitive attributes
  217. oss << prim->GetAttrsText();
  218. if (prim->isa<prim::DoSignaturePrimitive>()) {
  219. auto do_signature = dyn_cast<prim::DoSignaturePrimitive>(prim);
  220. auto &func = do_signature->function();
  221. if (func->isa<Primitive>()) {
  222. auto sig_prim = dyn_cast<Primitive>(func);
  223. oss << sig_prim->GetAttrsText();
  224. }
  225. }
  226. return oss.str();
  227. }
  228. std::string AnfExporter::GetNameSpaceText(const parse::NameSpacePtr &ns) {
  229. std::ostringstream oss;
  230. if (ns == nullptr) {
  231. return oss.str();
  232. }
  233. // Dump related module information in Namespace
  234. oss << ns->type_name() << "::" << ns->module();
  235. return oss.str();
  236. }
  237. std::string AnfExporter::GetSymbolicKeyInstanceText(const FuncGraphPtr &func_graph,
  238. const SymbolicKeyInstancePtr &sym_inst) {
  239. MS_EXCEPTION_IF_NULL(func_graph);
  240. MS_EXCEPTION_IF_NULL(sym_inst);
  241. AnfNodePtr sym_node = sym_inst->node();
  242. MS_EXCEPTION_IF_NULL(sym_node);
  243. std::ostringstream oss;
  244. if (sym_node->isa<Parameter>()) {
  245. int idx = GetParamIndex(func_graph, sym_node, false);
  246. // If can not find SymbolicKeyInstance related parameter from ancestors,
  247. // try to find from all exported graphs
  248. if (idx < 0) {
  249. idx = GetParamIndexFromExported(sym_node);
  250. }
  251. if (idx < 0) {
  252. ParameterPtr p = dyn_cast<Parameter>(sym_node);
  253. if (p == nullptr) {
  254. MS_LOG(EXCEPTION) << "Sym_inst's node could not cast to parameter";
  255. }
  256. MS_LOG(WARNING) << "Can not find SymbolicKeyInstance: " << p->name();
  257. }
  258. oss << "SymInst(%para" << idx << ")";
  259. } else {
  260. MS_LOG(WARNING) << "SymbolicKeyInstance does not embed a parameter: " << sym_node->ToString();
  261. oss << "SymInst(cnode_" << sym_node->ToString() << ")";
  262. }
  263. return oss.str();
  264. }
  265. std::string AnfExporter::GetSequenceText(const FuncGraphPtr &func_graph, const ValuePtr &value) {
  266. std::ostringstream oss;
  267. // Output ValueList, ValueTuple
  268. ValueSequeuePtr seq = dyn_cast<ValueSequeue>(value);
  269. MS_EXCEPTION_IF_NULL(seq);
  270. MS_EXCEPTION_IF_NULL(value);
  271. bool is_tuple = value->isa<ValueTuple>();
  272. oss << (is_tuple ? "(" : "[");
  273. bool first_flag = true;
  274. for (auto elem : seq->value()) {
  275. if (first_flag) {
  276. first_flag = false;
  277. } else {
  278. oss << ", ";
  279. }
  280. oss << GetValueText(func_graph, elem);
  281. }
  282. oss << (is_tuple ? ")" : "]");
  283. return oss.str();
  284. }
  285. std::string AnfExporter::GetDictText(const FuncGraphPtr &func_graph, const ValuePtr &value) {
  286. std::ostringstream oss;
  287. ValueDictionaryPtr dict = value->cast<ValueDictionaryPtr>();
  288. oss << "{";
  289. bool first_flag = true;
  290. for (const auto &elem : dict->value()) {
  291. if (first_flag) {
  292. first_flag = false;
  293. } else {
  294. oss << ", ";
  295. }
  296. oss << "\"" << elem.first << "\": " << GetValueText(func_graph, elem.second);
  297. }
  298. oss << "}";
  299. return oss.str();
  300. }
  301. std::string AnfExporter::GetOtherValueText(const FuncGraphPtr &, const ValuePtr &value) {
  302. std::ostringstream oss;
  303. if (check_integrity_) {
  304. MS_LOG(EXCEPTION) << "Need to process type: " << value->type_name() << ", dump text: " << value->DumpText();
  305. }
  306. oss << value->type_name() << "[" << value->DumpText() << "]";
  307. return oss.str();
  308. }
  309. std::string AnfExporter::GetValueText(const FuncGraphPtr &func_graph, const ValuePtr &value) {
  310. std::ostringstream oss;
  311. bool is_null_ptr = (func_graph == nullptr || value == nullptr);
  312. if (is_null_ptr) {
  313. return oss.str();
  314. }
  315. if (value->isa<Primitive>()) {
  316. oss << GetPrimitiveText(value->cast<PrimitivePtr>());
  317. } else if (value->isa<MetaFuncGraph>()) {
  318. MetaFuncGraphPtr meta_func_graph = value->cast<MetaFuncGraphPtr>();
  319. oss << GetMetaFuncGraphText(meta_func_graph);
  320. } else if (value->isa<SymbolicKeyInstance>()) {
  321. oss << GetSymbolicKeyInstanceText(func_graph, value->cast<SymbolicKeyInstancePtr>());
  322. } else if (value->isa<RefKey>()) {
  323. oss << value->DumpText();
  324. } else if (value->isa<Scalar>() || value->isa<StringImm>()) {
  325. oss << value->DumpText();
  326. } else if (value->isa<tensor::Tensor>()) {
  327. oss << value->DumpText();
  328. } else if (value->isa<parse::Symbol>() || value->isa<None>() || value->isa<Null>()) {
  329. oss << value->DumpText();
  330. } else if (value->isa<ValueSequeue>()) {
  331. oss << GetSequenceText(func_graph, value);
  332. } else if (value->isa<ValueDictionary>()) {
  333. oss << GetDictText(func_graph, value);
  334. } else if (value->isa<ValueSlice>()) {
  335. ValueSlicePtr slice = value->cast<ValueSlicePtr>();
  336. oss << slice->DumpText();
  337. } else if (value->isa<Type>()) {
  338. oss << value->DumpText();
  339. } else if (value->isa<parse::NameSpace>()) {
  340. oss << GetNameSpaceText(value->cast<parse::NameSpacePtr>());
  341. } else if (value->isa<parse::PyObjectWrapper>()) {
  342. oss << value->type_name();
  343. } else if (value->isa<KeywordArg>()) {
  344. KeywordArgPtr keyword_arg = value->cast<KeywordArgPtr>();
  345. oss << keyword_arg->DumpText();
  346. } else {
  347. return GetOtherValueText(func_graph, value);
  348. }
  349. return oss.str();
  350. }
  351. // This function is used to output node in CNode's inputs
  352. std::string AnfExporter::GetAnfNodeText(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
  353. const std::map<AnfNodePtr, int> &apply_map) {
  354. std::ostringstream oss;
  355. if (func_graph == nullptr || node == nullptr) {
  356. return oss.str();
  357. }
  358. if (node->isa<CNode>()) {
  359. auto iter = apply_map.find(node);
  360. if (iter == apply_map.end()) {
  361. MS_LOG(EXCEPTION) << "Can not find node '" << node->DumpText() << "' in apply_map";
  362. }
  363. oss << "%" << iter->second;
  364. } else if (node->isa<Parameter>()) {
  365. // Parameter maybe a free variable, so check it in its own funcgraph.
  366. oss << "%para" << GetParamIndex(node->func_graph(), node, check_integrity_);
  367. } else if (IsValueNode<FuncGraph>(node)) {
  368. FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(node);
  369. oss << fg->type_name() << "::fg_" << fg->debug_info()->get_id();
  370. if (!func_graph_set.contains(fg) && exported.find(fg) == exported.end() && export_used_) {
  371. func_graph_set.add(fg);
  372. }
  373. } else if (node->isa<ValueNode>()) {
  374. oss << GetValueNodeText(func_graph, node->cast<ValueNodePtr>());
  375. } else {
  376. MS_LOG(EXCEPTION) << "Unknown node '" << node->DumpText() << "'";
  377. }
  378. return oss.str();
  379. }
  380. void AnfExporter::OutputParameters(std::ofstream &ofs, const std::vector<AnfNodePtr> &parameters,
  381. OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual> *param_map) {
  382. bool first_flag = true;
  383. for (const AnfNodePtr &param : parameters) {
  384. if (first_flag) {
  385. first_flag = false;
  386. ofs << " ";
  387. } else {
  388. ofs << " , ";
  389. }
  390. (*param_map)[param] = param_index;
  391. std::string type_info = GetNodeType(param);
  392. // Output parameter and type
  393. if (type_info == "Undefined") {
  394. ofs << "%para" << param_index;
  395. } else {
  396. ofs << "%para" << param_index << " : " << type_info;
  397. }
  398. // Output comment
  399. ofs << " # " << param->DumpText() << "\n";
  400. param_index += 1;
  401. }
  402. }
  403. void AnfExporter::OutputStatementComment(std::ofstream &ofs, const CNodePtr &node) {
  404. if (node == nullptr) {
  405. return;
  406. }
  407. // Output type of each input argument
  408. auto &inputs = node->inputs();
  409. if (inputs.size() > 1) {
  410. ofs << " #(";
  411. for (size_t i = 1; i < inputs.size(); ++i) {
  412. if (i != 1) {
  413. ofs << ", ";
  414. }
  415. AnfNodePtr arg = inputs[i];
  416. ofs << GetNodeType(arg);
  417. }
  418. ofs << ")";
  419. }
  420. // Output other comment, map the graph name to original representation(containing unicode character)
  421. std::ostringstream comment;
  422. comment << " #";
  423. bool has_comment = false;
  424. for (size_t i = 0; i < inputs.size(); ++i) {
  425. AnfNodePtr arg = inputs[i];
  426. if (!IsValueNode<FuncGraph>(arg)) {
  427. continue;
  428. }
  429. if (!has_comment) {
  430. has_comment = true;
  431. } else {
  432. comment << ",";
  433. }
  434. FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(arg);
  435. std::string func_graph_id = fg->debug_info()->get_id();
  436. comment << " fg_" << func_graph_id << "=" << fg->ToString();
  437. }
  438. if (has_comment) {
  439. ofs << comment.str();
  440. }
  441. ofs << " #scope: " << node->scope()->name();
  442. }
  443. void AnfExporter::OutputCNodeText(std::ofstream &ofs, const CNodePtr &cnode, const FuncGraphPtr &func_graph, int *idx,
  444. std::map<AnfNodePtr, int> *const apply_map) {
  445. auto &inputs = cnode->inputs();
  446. std::string op_text = GetAnfNodeText(func_graph, inputs[0], *apply_map);
  447. std::string fv_text = (cnode->func_graph() != func_graph) ? ("$(" + cnode->func_graph()->ToString() + "):") : "";
  448. // Non-return node
  449. if (cnode != func_graph->get_return()) {
  450. int apply_idx = (*idx)++;
  451. (*apply_map)[cnode] = apply_idx;
  452. std::string type_info = GetNodeType(cnode);
  453. if (type_info == "Undefined") {
  454. ofs << " %" << apply_idx << " = " << fv_text << op_text << "(";
  455. } else {
  456. ofs << " %" << apply_idx << " : " << fv_text << type_info << " = " << op_text << "(";
  457. }
  458. } else {
  459. ofs << " " << fv_text << op_text << "(";
  460. }
  461. for (size_t i = 1; i < inputs.size(); ++i) {
  462. if (i != 1) {
  463. ofs << ", ";
  464. }
  465. AnfNodePtr arg = inputs[i];
  466. ofs << GetAnfNodeText(func_graph, arg, *apply_map);
  467. }
  468. ofs << ")";
  469. }
  470. void AnfExporter::OutputCNode(std::ofstream &ofs, const CNodePtr &cnode, const FuncGraphPtr &func_graph, int *idx,
  471. std::map<AnfNodePtr, int> *const apply_map) {
  472. OutputCNodeText(ofs, cnode, func_graph, idx, apply_map);
  473. // Output comment
  474. OutputStatementComment(ofs, cnode);
  475. ofs << "\n";
  476. }
  477. void AnfExporter::OutputCNodes(std::ofstream &ofs, const std::vector<AnfNodePtr> &nodes, const FuncGraphPtr &func_graph,
  478. const TaggedNodeMap &tagged_cnodes_map) {
  479. if (func_graph == nullptr) {
  480. return;
  481. }
  482. int idx = 1;
  483. std::map<AnfNodePtr, int> apply_map;
  484. for (const AnfNodePtr &node : nodes) {
  485. MS_EXCEPTION_IF_NULL(node);
  486. if (!node->isa<CNode>()) {
  487. continue;
  488. }
  489. if (!tagged_cnodes_map.empty()) {
  490. auto iter = tagged_cnodes_map.find(node);
  491. if (iter != tagged_cnodes_map.end()) {
  492. ofs << "\n#------------------------> " << iter->second << "\n";
  493. }
  494. }
  495. auto cnode = node->cast<CNodePtr>();
  496. OutputCNode(ofs, cnode, func_graph, &idx, &apply_map);
  497. if (label_manage::GetGlobalTraceLabelType() == label_manage::TraceLabelType::kWithUniqueId) {
  498. ofs << trace::GetDebugInfo(cnode->debug_info(), " # ", kSourceLineTipDiscard) << "#"
  499. << label_manage::Label(cnode->debug_info()) << "\n";
  500. } else {
  501. ofs << trace::GetDebugInfo(cnode->debug_info(), " # ", kSourceLineTipDiscard) << "#" << cnode->ToString()
  502. << "\n";
  503. }
  504. }
  505. }
  506. void AnfExporter::OutputOrderList(std::ofstream &ofs, const FuncGraphPtr &func_graph) {
  507. auto &order_list = func_graph->order_list();
  508. if (order_list.empty()) {
  509. return;
  510. }
  511. constexpr int width = 4;
  512. ofs << "# order:\n";
  513. int i = 1;
  514. for (auto &node : order_list) {
  515. ofs << '#' << std::setw(width) << i << ": " << node->DebugString() << '\n';
  516. ++i;
  517. }
  518. }
  519. void AnfExporter::ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph,
  520. const TaggedNodeMap &tagged_cnodes_map) {
  521. if (func_graph == nullptr) {
  522. return;
  523. }
  524. std::vector<AnfNodePtr> nodes = TopoSort(func_graph->get_return(), SuccIncoming, AlwaysInclude);
  525. std::vector<AnfNodePtr> parameters = func_graph->parameters();
  526. OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual> param_map;
  527. if (*(func_graph->switch_input())) {
  528. ofs << "switch_input: " << *(func_graph->switch_input()) << "\n";
  529. }
  530. if (*(func_graph->switch_layer_input())) {
  531. ofs << "switch_layer_input: " << *(func_graph->switch_layer_input()) << "\n";
  532. }
  533. ofs << "# [No." << (exported.size() + 1) << "] " << func_graph->DumpText() << "\n";
  534. if (label_manage::GetGlobalTraceLabelType() == label_manage::TraceLabelType::kWithUniqueId) {
  535. ofs << trace::GetDebugInfo(func_graph->debug_info(), "# ", kSourceLineTipDiscard) << "#"
  536. << label_manage::Label(func_graph->debug_info()) << "\n";
  537. } else {
  538. ofs << trace::GetDebugInfo(func_graph->debug_info(), "# ", kSourceLineTipDiscard) << "\n";
  539. }
  540. ofs << "funcgraph fg_" << func_graph->debug_info()->get_id();
  541. // Output name of parent of graph if exists
  542. if (func_graph->parent() != nullptr) {
  543. ofs << "[fg_" << func_graph->parent()->debug_info()->get_id() << "]";
  544. }
  545. ofs << "(\n";
  546. OutputParameters(ofs, parameters, &param_map);
  547. exported[func_graph] = param_map;
  548. ofs << (!parameters.empty() ? " " : "") << ") {\n";
  549. OutputCNodes(ofs, nodes, func_graph, tagged_cnodes_map);
  550. ofs << "}\n";
  551. OutputOrderList(ofs, func_graph);
  552. }
  553. void AnfExporter::ExportFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph) {
  554. if (func_graph == nullptr) {
  555. return;
  556. }
  557. std::ofstream ofs(filename);
  558. if (!ofs.is_open()) {
  559. MS_LOG(ERROR) << "Open file '" << filename << "' failed!"
  560. << " Errno:" << errno << " ErrInfo:" << strerror(errno);
  561. return;
  562. }
  563. param_index = 1;
  564. TaggedNodeMap tagged_cnodes_map;
  565. func_graph_set.add(func_graph);
  566. while (!func_graph_set.empty()) {
  567. FuncGraphPtr fg = *func_graph_set.begin();
  568. ExportOneFuncGraph(ofs, fg, tagged_cnodes_map);
  569. ofs << "\n\n";
  570. (void)func_graph_set.erase(fg);
  571. }
  572. ofs << "# num of total function graphs: " << exported.size();
  573. ofs.close();
  574. }
  575. #ifdef ENABLE_DUMP_IR
  576. void ExportIR(const std::string &filename, const FuncGraphPtr &func_graph) {
  577. if (func_graph == nullptr) {
  578. return;
  579. }
  580. auto filepath = GetSaveGraphsPathName(Common::AddId(filename, ".dat"));
  581. auto real_filepath = Common::CreatePrefixPath(filepath);
  582. if (!real_filepath.has_value()) {
  583. MS_LOG(ERROR) << "The export ir path: " << filepath << " is not illegal.";
  584. return;
  585. }
  586. ChangeFileMode(real_filepath.value(), S_IWUSR);
  587. AnfExporter exporter;
  588. exporter.ExportFuncGraph(real_filepath.value(), func_graph);
  589. // Set file mode to read only by user
  590. ChangeFileMode(real_filepath.value(), S_IRUSR);
  591. }
  592. #else
  593. void ExportIR(const std::string &, const FuncGraphPtr &) {
  594. static bool already_printed = false;
  595. if (already_printed) {
  596. return;
  597. }
  598. already_printed = true;
  599. MS_LOG(WARNING) << "The functionality of dumping function graph IR is disabled, "
  600. << "please recompile source to enable it. See help of building script.";
  601. }
  602. void ExportIR(const std::string &filename, const std::vector<TaggedGraph> &graphs) {
  603. static bool already_printed = false;
  604. if (already_printed) {
  605. return;
  606. }
  607. already_printed = true;
  608. MS_LOG(WARNING) << "The functionality of dumping function graph IR is disabled, "
  609. << "please recompile source to enable it. See help of building script.";
  610. }
  611. #endif
  612. } // namespace mindspore