1 /** 2 * Copyright 2019-2022 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_INCLUDE_COMMON_DEBUG_ANF_IR_DUMP_H_ 17 #define MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_ANF_IR_DUMP_H_ 18 19 #include <string> 20 #include <memory> 21 #include <vector> 22 #include <unordered_map> 23 #include "ir/dtype/type.h" 24 #include "ir/anf.h" 25 #include "include/common/debug/common.h" 26 #include "utils/hash_set.h" 27 #include "include/common/visible.h" 28 29 namespace mindspore { 30 enum LocDumpMode : int { kOff = 0, kTopStack = 1, kWholeStack = 2, kInValid = 3 }; 31 auto constexpr kDumpConfigLineLevel0 = "LINE_LEVEL0"; 32 auto constexpr kDumpConfigLineLevel1 = "LINE_LEVEL1"; 33 auto constexpr kDumpConfigLineLevel2 = "LINE_LEVEL2"; 34 auto constexpr kDumpConfigDisableBackend = "DISABLE_BACKEND"; 35 auto constexpr kDumpConfigEnablePassIR = "ENABLE_PASS_IR"; 36 struct DumpConfig { 37 LocDumpMode dump_line_level = kInValid; 38 bool disable_backend_dump = false; 39 bool enable_dump_pass_ir = false; 40 }; 41 42 struct SubGraphIRInfo { 43 int32_t local_var; 44 std::ostringstream buffer; 45 OrderedMap<AnfNodePtr, int32_t> local_var_map; 46 int32_t format_level; 47 int32_t cnode_num = 0; 48 }; 49 COMMON_EXPORT void DumpCNode(const CNodePtr &node, const FuncGraphPtr &sub_graph, 50 const OrderedMap<AnfNodePtr, int32_t> ¶_map, 51 const std::shared_ptr<SubGraphIRInfo> &gsub, bool dump_full_name = false, 52 LocDumpMode dump_location = kWholeStack); 53 COMMON_EXPORT int32_t DumpParams(const FuncGraphPtr &graph, std::ostringstream &buffer, 54 OrderedMap<AnfNodePtr, int32_t> *para_map); 55 COMMON_EXPORT void OutputOrderList(const FuncGraphPtr &sub_graph, std::ostringstream &oss); 56 constexpr char PARALLEL_STRATEGY[] = "strategy"; 57 COMMON_EXPORT void DumpIRHead(const FuncGraphPtr &graph, std::ostringstream &buffer); 58 COMMON_EXPORT void SaveIRFile(const std::string &filename, const std::string &str, const std::string &target_file); 59 COMMON_EXPORT void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name = false, 60 LocDumpMode dump_location = kWholeStack, const std::string &target_file = ""); 61 COMMON_EXPORT void DumpIR(std::ostringstream &graph_buffer, const FuncGraphPtr &graph, bool dump_full_name = false, 62 int dump_location = kWholeStack, bool avoid_circle = false); 63 COMMON_EXPORT void DumpParallelJson(const std::string &filename, const FuncGraphPtr &graph, 64 const int64_t global_rank_id, 65 const std::unordered_map<std::string, std::vector<uint32_t>> &group_map); 66 COMMON_EXPORT void GatherInputAndOutputInferType(std::ostringstream &buffer, const AnfNodePtr &node); 67 68 COMMON_EXPORT void DumpIRForRDR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name = false, 69 LocDumpMode dump_location = kOff); 70 COMMON_EXPORT DumpConfig GetDumpConfig(); 71 std::string GetValueText(const ValuePtr &value, const std::shared_ptr<SubGraphIRInfo> &gsub); 72 73 COMMON_EXPORT void DumpOperator(const AnfNodePtr &node, const std::shared_ptr<SubGraphIRInfo> &gsub); 74 75 COMMON_EXPORT void DumpOperands(const AnfNodePtr &node, const OrderedMap<AnfNodePtr, int32_t> ¶_map, 76 const std::shared_ptr<SubGraphIRInfo> &gsub); 77 78 COMMON_EXPORT void DumpOperateAttrs(const AnfNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub); 79 80 COMMON_EXPORT void DumpCNodeAttrs(const CNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub); 81 82 COMMON_EXPORT void DumpCNodePrimalAttrs(const CNodePtr &op, const std::shared_ptr<SubGraphIRInfo> &gsub); 83 84 COMMON_EXPORT void DumpParallelInfo(const CNodePtr &node, const std::shared_ptr<SubGraphIRInfo> &gsub); 85 86 COMMON_EXPORT int32_t DumpParams(const FuncGraphPtr &graph, std::ostringstream &buffer, 87 OrderedMap<AnfNodePtr, int32_t> *para_map); 88 89 COMMON_EXPORT void DumpIRInSubgraph(const std::vector<AnfNodePtr> &nodes, OrderedMap<AnfNodePtr, int32_t> *para_map, 90 OrderedMap<FuncGraphPtr, std::shared_ptr<SubGraphIRInfo>> *const sub_graphs, 91 int32_t total_para, bool dump_full_name = false, 92 LocDumpMode dump_location = kWholeStack); 93 94 COMMON_EXPORT void DumpGlobalInfoEntry(const FuncGraphPtr &graph, std::ostringstream &buffer, size_t sub_graphs_size); 95 96 struct ParamPtrEqual { operatorParamPtrEqual97 bool operator()(AnfNodePtr const &t1, AnfNodePtr const &t2) const { 98 const ParameterPtr param1 = dyn_cast<Parameter>(t1); 99 const ParameterPtr param2 = dyn_cast<Parameter>(t2); 100 101 if (param1 == nullptr || param2 == nullptr) { 102 return false; 103 } 104 105 return *param1 == *param2; 106 } 107 }; 108 109 struct ParamPtrHasher { operatorParamPtrHasher110 std::size_t operator()(AnfNodePtr const ¶m) const { 111 const ParameterPtr parameter = dyn_cast<Parameter>(param); 112 if (parameter == nullptr) { 113 return 0; 114 } 115 std::size_t hash = std::hash<std::string>()(parameter->name()); 116 return hash; 117 } 118 }; 119 120 using ParamIndexMap = OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual, true>; 121 122 class AnfExporter { 123 public: 124 explicit AnfExporter(bool export_used = true, bool check_integrity = false) 125 : param_index_(1), export_used_(export_used), check_integrity_(check_integrity) { 126 func_graph_set_.clear(); 127 exported_.clear(); 128 } ~AnfExporter()129 virtual ~AnfExporter() {} 130 131 void ExportFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph); 132 133 protected: 134 void OuputIrStyleCNodes(const FuncGraphPtr &func_graph, const std::vector<AnfNodePtr> &nodes, int32_t total_para, 135 std::ostringstream &oss, OrderedMap<AnfNodePtr, int32_t> *para_map); 136 137 virtual void ExportOneFuncGraph(const FuncGraphPtr &func_graph, const TaggedNodeMap &tagged_cnodes_map, 138 std::ostringstream &oss, int32_t total_para = 0, 139 OrderedMap<AnfNodePtr, int32_t> *para_map = nullptr); 140 141 OrderedMap<FuncGraphPtr, ParamIndexMap> exported_; 142 bool is_top_graph_; 143 144 private: 145 int param_index_; 146 OrderedSet<FuncGraphPtr> func_graph_set_{}; 147 bool export_used_ = true; // whether export function graphs used in current exporting function graph 148 bool check_integrity_ = false; // whether check integrity or not, when dumping ir for loading, must set it to true 149 }; 150 151 COMMON_EXPORT void ExportIR(const std::string &filename, const FuncGraphPtr &func_graph); 152 } // namespace mindspore 153 154 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_ANF_IR_DUMP_H_ 155