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 #ifndef MINDSPORE_CCSRC_DEBUG_ANF_IR_UTILS_H_ 17 #define MINDSPORE_CCSRC_DEBUG_ANF_IR_UTILS_H_ 18 19 #include <string> 20 #include <vector> 21 #include <fstream> 22 #include <map> 23 #include <memory> 24 #include <unordered_map> 25 #include <unordered_set> 26 #include <algorithm> 27 28 #include "ir/anf.h" 29 #include "ir/func_graph.h" 30 #include "ir/meta_func_graph.h" 31 #include "pipeline/jit/parse/python_adapter.h" 32 #include "pipeline/jit/parse/resolve.h" 33 #include "frontend/operator/composite/composite.h" 34 #include "utils/symbolic.h" 35 #include "utils/ordered_map.h" 36 #include "utils/ordered_set.h" 37 #include "utils/utils.h" 38 39 namespace mindspore { 40 41 struct ParamPtrEqual { operatorParamPtrEqual42 bool operator()(AnfNodePtr const &t1, AnfNodePtr const &t2) const { 43 const ParameterPtr param1 = dyn_cast<Parameter>(t1); 44 const ParameterPtr param2 = dyn_cast<Parameter>(t2); 45 46 if (param1 == nullptr || param2 == nullptr) { 47 return false; 48 } 49 50 return *param1 == *param2; 51 } 52 }; 53 54 struct ParamPtrHasher { operatorParamPtrHasher55 std::size_t operator()(AnfNodePtr const ¶m) const { 56 const ParameterPtr parameter = dyn_cast<Parameter>(param); 57 if (parameter == nullptr) { 58 return 0; 59 } 60 std::size_t hash = std::hash<std::string>()(parameter->name()); 61 return hash; 62 } 63 }; 64 65 class AnfExporter { 66 public: 67 explicit AnfExporter(bool export_used = true, bool check_integrity = false) 68 : param_index(1), export_used_(export_used), check_integrity_(check_integrity) { 69 func_graph_set.clear(); 70 exported.clear(); 71 } ~AnfExporter()72 virtual ~AnfExporter() {} 73 74 void ExportFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph); 75 76 protected: 77 virtual std::string GetNodeType(const AnfNodePtr &nd); 78 int GetParamIndex(const FuncGraphPtr &func_graph, const AnfNodePtr ¶m, bool throw_excp = true); 79 int GetParamIndexFromExported(const AnfNodePtr ¶m); 80 std::string GetValueNodeText(const FuncGraphPtr &func_graph, const ValueNodePtr &node); 81 std::string GetMultitypeFuncGraphText(const prim::MultitypeFuncGraphPtr &mt_func_graph); 82 std::string GetSymbolicKeyInstanceText(const FuncGraphPtr &func_graph, const SymbolicKeyInstancePtr &sym_inst); 83 std::string GetSequenceText(const FuncGraphPtr &func_graph, const ValuePtr &value); 84 std::string GetValueText(const FuncGraphPtr &func_graph, const ValuePtr &value); 85 std::string GetOtherValueText(const FuncGraphPtr &func_graph, const ValuePtr &value); 86 std::string GetPrimitiveText(const PrimitivePtr &prim); 87 std::string GetDictText(const FuncGraphPtr &func_graph, const ValuePtr &value); 88 std::string GetNameSpaceText(const parse::NameSpacePtr &ns); 89 std::string GetMetaFuncGraphText(const MetaFuncGraphPtr &meta_func_graph); 90 std::string GetAnfNodeText(const FuncGraphPtr &func_graph, const AnfNodePtr &node, 91 const std::map<AnfNodePtr, int> &apply_map); 92 void OutputParameters(std::ofstream &ofs, const std::vector<AnfNodePtr> ¶meters, 93 OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual> *param_map); 94 95 void OutputStatementComment(std::ofstream &ofs, const CNodePtr &node); 96 void OutputOrderList(std::ofstream &ofs, const FuncGraphPtr &func_graph); 97 98 void OutputCNodeText(std::ofstream &ofs, const CNodePtr &cnode, const FuncGraphPtr &func_graph, int *idx, 99 std::map<AnfNodePtr, int> *const apply_map); 100 virtual void OutputCNode(std::ofstream &ofs, const CNodePtr &cnode, const FuncGraphPtr &func_graph, int *idx, 101 std::map<AnfNodePtr, int> *const apply_map); 102 void ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph, const TaggedNodeMap &tagged_cnodes_map); 103 104 OrderedMap<FuncGraphPtr, OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual>> exported; 105 106 private: 107 void OutputCNodes(std::ofstream &ofs, const std::vector<AnfNodePtr> &nodes, const FuncGraphPtr &func_graph, 108 const TaggedNodeMap &tagged_cnodes_map); 109 110 int param_index; 111 OrderedSet<FuncGraphPtr> func_graph_set{}; 112 bool export_used_ = true; // whether export function graphs used in current exporting function graph 113 bool check_integrity_ = false; // whether check integrity or not, when dumping ir for loading, must set it to true 114 }; 115 116 void ExportIR(const std::string &filename, const FuncGraphPtr &func_graph); 117 118 std::string GetKernelNodeName(const AnfNodePtr &anf_node); 119 } // namespace mindspore 120 121 #endif // MINDSPORE_CCSRC_DEBUG_ANF_IR_UTILS_H_ 122