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