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

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

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