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.

graph_debug.cc 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Copyright 2020 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 "graph/debug/graph_debug.h"
  17. #include <algorithm>
  18. #include <unordered_set>
  19. #include <vector>
  20. #include "debug/ge_util.h"
  21. #include "framework/common/debug/ge_log.h"
  22. using namespace std;
  23. #define TAB " "
  24. #define STR_FMT(str) (" \"" + std::string(str) + "\" ")
  25. #define INPUT_ANCHOR_PORT(name) ("__input__" + (name))
  26. #define OUTPUT_ANCHOR_PORT(name) ("__output__" + (name))
  27. namespace ge {
  28. std::unordered_set<std::string> control_anchor;
  29. std::vector<string> types = {
  30. "DT_FLOAT", "DT_FLOAT16", "DT_INT8", "DT_INT32", "DT_UINT8", "",
  31. "DT_INT16", "DT_UINT16", "DT_UINT32", "DT_INT64", "DT_UINT64", "DT_DOUBLE",
  32. "DT_BOOL", "DT_DUAL", "DT_DUAL_SUB_INT8", "DT_DUAL_SUB_UINT8", "DT_UNDEFINED"};
  33. std::vector<string> formats = {"FORMAT_NCHW",
  34. "FORMAT_NHWC",
  35. "FORMAT_ND",
  36. "FORMAT_NC1HWC0",
  37. "FORMAT_FRACTAL_Z",
  38. "FORMAT_NC1C0HWPAD",
  39. "FORMAT_NHWC1C0",
  40. "FORMAT_FSR_NCHW",
  41. "FORMAT_FRACTAL_DECONV",
  42. "FORMAT_C1HWNC0",
  43. "FORMAT_FRACTAL_DECONV_TRANSPOSE",
  44. "FORMAT_FRACTAL_DECONV_SP_STRIDE_TRANS",
  45. "FORMAT_NC1HWC0_C04",
  46. "FORMAT_FRACTAL_Z_C04",
  47. "FORMAT_CHWN",
  48. "FORMAT_FRACTAL_DECONV_SP_STRIDE8_TRANS",
  49. "FORMAT_HWCN",
  50. "FORMAT_NC1KHKWHWC0",
  51. "FORMAT_BN_WEIGHT",
  52. "FORMAT_FILTER_HWCK",
  53. "FORMAT_HASHTABLE_LOOKUP_LOOKUPS",
  54. "FORMAT_HASHTABLE_LOOKUP_KEYS",
  55. "FORMAT_HASHTABLE_LOOKUP_VALUE",
  56. "FORMAT_HASHTABLE_LOOKUP_OUTPUT",
  57. "FORMAT_HASHTABLE_LOOKUP_HITS",
  58. "FORMAT_RESERVED"};
  59. std::vector<string> data_nodes = {"Const", "Data"};
  60. void GraphDebugPrinter::DumpNodeToDot(const NodePtr node, std::ostringstream &out_) {
  61. if (node == nullptr) {
  62. GELOGI("Some nodes are null.");
  63. return;
  64. }
  65. bool in_control = false;
  66. auto name = node->GetName();
  67. out_ << TAB << STR_FMT(name);
  68. auto input_cnt = std::max(static_cast<size_t>(1), node->GetAllInDataAnchors().size());
  69. auto output_cnt = std::max(static_cast<size_t>(1), node->GetAllOutDataAnchors().size());
  70. if (control_anchor.find(node->GetName()) != control_anchor.end()) {
  71. input_cnt++;
  72. in_control = true;
  73. }
  74. auto max_col = input_cnt * output_cnt;
  75. out_ << "[\n";
  76. if (find(data_nodes.begin(), data_nodes.end(), node->GetType()) != data_nodes.end()) {
  77. out_ << TAB << TAB << "shape=plaintext, color=goldenrod\n";
  78. } else {
  79. out_ << TAB << TAB << "shape=plaintext, color=deepskyblue\n";
  80. }
  81. out_ << TAB << TAB << "label=<\n";
  82. out_ << TAB << TAB << R"(<table border="0" cellborder="1" align="center")"
  83. << ">" << std::endl;
  84. auto input_anchors = node->GetAllInDataAnchors();
  85. auto op_desc = node->GetOpDesc();
  86. GE_CHECK_NOTNULL_EXEC(op_desc, return);
  87. if (!input_anchors.empty()) {
  88. out_ << TAB << TAB << "<tr>";
  89. }
  90. for (const auto &anchor : input_anchors) {
  91. string anchor_text = op_desc->GetInputNameByIndex(anchor->GetIdx());
  92. out_ << "<td port = " << STR_FMT(INPUT_ANCHOR_PORT(anchor_text)) << " colspan='" << output_cnt << "'>"
  93. << anchor_text << "</td>";
  94. }
  95. if (in_control) {
  96. string anchor_text = "ctrl";
  97. out_ << "<td port = " << STR_FMT(INPUT_ANCHOR_PORT(anchor_text)) << " colspan='" << output_cnt << "'>"
  98. << anchor_text << "</td>";
  99. }
  100. if (!input_anchors.empty()) {
  101. out_ << "</tr>\n";
  102. }
  103. // Node type
  104. out_ << TAB << TAB << "<tr><td colspan='" << max_col << "'>"
  105. << "<b>" << node->GetType() << "</b></td></tr>\n";
  106. // Output
  107. auto output_anchors = node->GetAllOutDataAnchors();
  108. if (!output_anchors.empty()) {
  109. out_ << TAB << TAB << "<tr>";
  110. }
  111. for (const auto &anchor : output_anchors) {
  112. string anchor_text = op_desc->GetOutputNameByIndex(anchor->GetIdx());
  113. out_ << "<td port = " << STR_FMT(OUTPUT_ANCHOR_PORT(anchor_text)) << " colspan='" << input_cnt << "'>"
  114. << anchor_text << "</td>";
  115. }
  116. if (!output_anchors.empty()) {
  117. out_ << "</tr>\n";
  118. }
  119. out_ << TAB << TAB << "</table>\n" << TAB << ">];\n";
  120. }
  121. void GraphDebugPrinter::DumpEdgeToDot(const NodePtr node, std::ostringstream &out_, uint32_t flag) {
  122. if (node == nullptr) {
  123. GELOGI("Some nodes are null.");
  124. return;
  125. }
  126. auto all_out_anchor = node->GetAllOutDataAnchors();
  127. auto op_desc = node->GetOpDesc();
  128. GE_CHECK_NOTNULL_EXEC(op_desc, return);
  129. for (const auto &anchor : all_out_anchor) {
  130. auto src_anchor = anchor;
  131. auto src_node_name = node->GetName();
  132. auto src_anchor_index = op_desc->GetOutputNameByIndex(static_cast<uint32_t>(src_anchor->GetIdx()));
  133. auto des_anchors = anchor->GetPeerAnchors();
  134. for (const auto &peer_in_anchor : des_anchors) {
  135. auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(peer_in_anchor);
  136. std::string dst_node_name;
  137. out_ << TAB << STR_FMT(src_node_name);
  138. out_ << ":" << OUTPUT_ANCHOR_PORT(src_anchor_index);
  139. auto op = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  140. GE_CHECK_NOTNULL_EXEC(op, continue);
  141. if (in_data_anchor != nullptr) {
  142. dst_node_name = in_data_anchor->GetOwnerNode()->GetName();
  143. string des_anchor_index = op->GetInputNameByIndex(static_cast<uint32_t>(in_data_anchor->GetIdx()));
  144. out_ << " -> " << STR_FMT(dst_node_name);
  145. out_ << ":" << INPUT_ANCHOR_PORT(des_anchor_index);
  146. out_ << "[";
  147. }
  148. auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(peer_in_anchor);
  149. if (in_control_anchor != nullptr) {
  150. dst_node_name = in_control_anchor->GetOwnerNode()->GetName();
  151. string des_anchor_index = "ctrl";
  152. out_ << " -> " << STR_FMT(dst_node_name);
  153. out_ << ":" << INPUT_ANCHOR_PORT(des_anchor_index);
  154. out_ << "[";
  155. out_ << " style=dashed ";
  156. }
  157. if (flag != DOT_NOT_SHOW_EDGE_LABEL && in_data_anchor) {
  158. string label;
  159. auto src_ops = src_anchor->GetOwnerNode()->GetOpDesc();
  160. GE_CHECK_NOTNULL_EXEC(src_ops, return);
  161. auto src_shape = src_ops->GetOutputDesc(src_anchor->GetIdx()).GetShape();
  162. auto dim = src_shape.GetDims();
  163. std::ostringstream tensor_info;
  164. if (dim.size() > 0) {
  165. for (size_t i = 0; i < dim.size(); i++) {
  166. if (i != dim.size() - 1) {
  167. tensor_info << dim[i] << "x";
  168. } else {
  169. tensor_info << dim[i];
  170. }
  171. }
  172. } else {
  173. tensor_info << "?";
  174. }
  175. auto src_tensor_desc = src_ops->GetOutputDescPtr(src_anchor->GetIdx());
  176. GE_CHECK_NOTNULL_EXEC(src_tensor_desc, return);
  177. auto format = src_tensor_desc->GetFormat();
  178. auto datatype = src_tensor_desc->GetDataType();
  179. tensor_info << " : " << formats[format] << " : " << types[datatype];
  180. label = tensor_info.str();
  181. out_ << "label=" << STR_FMT(label);
  182. }
  183. out_ << "]" << std::endl;
  184. }
  185. }
  186. }
  187. graphStatus GraphDebugPrinter::DumpGraphDotFile(const Graph &graph, const std::string &output_dot_file_name,
  188. uint32_t flag) {
  189. auto compute_graph = GraphUtils::GetComputeGraph(graph);
  190. if (compute_graph == nullptr) {
  191. GELOGI("Compute graph is NULL .");
  192. return GRAPH_SUCCESS;
  193. }
  194. return DumpGraphDotFile(compute_graph, output_dot_file_name, flag);
  195. }
  196. graphStatus GraphDebugPrinter::DumpGraphDotFile(const ComputeGraphPtr graph, const std::string &output_dot_file_name,
  197. uint32_t flag) {
  198. if (graph == nullptr) {
  199. GELOGI("graph is null.");
  200. return GRAPH_SUCCESS;
  201. }
  202. std::ostringstream out_;
  203. out_ << "digraph G{\n";
  204. out_ << TAB << R"(ratio=compress;size="8, 100")" << std::endl;
  205. out_ << TAB << R"(node[fontname="Consolas"])" << std::endl;
  206. out_ << TAB << R"(edge[fontsize = "8" fontname = "Consolas" color="dimgray" ])" << std::endl;
  207. auto all_nodes = graph->GetAllNodes();
  208. for (const auto &node : all_nodes) {
  209. for (const auto &temp : node->GetAllOutDataAnchors()) {
  210. for (const auto &peer : temp->GetPeerAnchors()) {
  211. auto temp_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(peer);
  212. if (temp_control_anchor) {
  213. (void)control_anchor.insert(peer->GetOwnerNode()->GetName());
  214. }
  215. }
  216. }
  217. }
  218. for (const auto &node : all_nodes) {
  219. DumpNodeToDot(node, out_);
  220. }
  221. for (const auto &node : all_nodes) {
  222. DumpEdgeToDot(node, out_, flag);
  223. }
  224. out_ << "}";
  225. std::ofstream output_file(output_dot_file_name);
  226. if (output_file.is_open()) {
  227. output_file << out_.str();
  228. } else {
  229. GELOGW("%s open error.", output_dot_file_name.c_str());
  230. }
  231. return GRAPH_SUCCESS;
  232. }
  233. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示