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.

draw.cc 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #include "debug/draw.h"
  17. #include <iostream>
  18. #include <iterator>
  19. #include <vector>
  20. #include <string>
  21. #include "ir/meta_func_graph.h"
  22. #include "ir/param_info.h"
  23. #include "ir/primitive.h"
  24. #include "ir/graph_utils.h"
  25. #include "utils/utils.h"
  26. #include "frontend/operator/composite/composite.h"
  27. #include "pipeline/jit/parse/resolve.h"
  28. #include "ir/tensor.h"
  29. namespace mindspore {
  30. // namespace to support debug utils
  31. namespace draw {
  32. namespace {
  33. // Only for ValueNode
  34. std::string ValueType(const ValueNodePtr &node) {
  35. if (node == nullptr) {
  36. return "";
  37. }
  38. auto v = node->value();
  39. MS_EXCEPTION_IF_NULL(v);
  40. return v->type_name();
  41. }
  42. std::string ReplaceSpecialChar(const std::string &str) {
  43. std::ostringstream oss;
  44. for (size_t i = 0; i < str.size(); i++) {
  45. if (str[i] == '<') {
  46. oss << "「";
  47. } else if (str[i] == '>') {
  48. oss << "」";
  49. } else {
  50. oss << str[i];
  51. }
  52. }
  53. return oss.str();
  54. }
  55. } // namespace
  56. // API of debug utils
  57. void DrawNodes(const std::vector<AnfNodePtr> &nodes, OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> *sub_graphs,
  58. bool is_user) {
  59. if (sub_graphs == nullptr) {
  60. return;
  61. }
  62. for (auto &nd : nodes) {
  63. MS_EXCEPTION_IF_NULL(nd);
  64. auto sub_graph = nd->func_graph();
  65. if (sub_graph != nullptr) {
  66. auto gsub = (*sub_graphs)[sub_graph];
  67. if (gsub == nullptr) {
  68. if (is_user) {
  69. gsub = std::make_shared<ModelDigraph>(sub_graph->ToString());
  70. } else {
  71. gsub = std::make_shared<Digraph>(sub_graph->ToString());
  72. }
  73. (*sub_graphs)[sub_graph] = gsub;
  74. }
  75. if (!nd->isa<Parameter>()) {
  76. gsub->Node(nd);
  77. }
  78. }
  79. }
  80. }
  81. void DrawValueNodes(const std::vector<AnfNodePtr> &nodes,
  82. OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> *sub_graphs) {
  83. if (sub_graphs == nullptr) {
  84. return;
  85. }
  86. int dup_idx = 0;
  87. for (auto &nd : nodes) {
  88. for (auto &t : GetInputs(nd)) {
  89. MS_EXCEPTION_IF_NULL(t);
  90. MS_EXCEPTION_IF_NULL(nd);
  91. if (t->isa<ValueNode>() && (*sub_graphs).find(nd->func_graph()) != (*sub_graphs).end()) {
  92. (*sub_graphs)[nd->func_graph()]->Node(t, dup_idx);
  93. dup_idx++;
  94. } else if (t->isa<Parameter>() && (*sub_graphs).find(t->func_graph()) != (*sub_graphs).end()) {
  95. (*sub_graphs)[t->func_graph()]->Node(t, dup_idx);
  96. dup_idx++;
  97. }
  98. }
  99. }
  100. }
  101. void DrawEdges(const std::vector<AnfNodePtr> &nodes, const std::shared_ptr<BaseDigraph> &digraph, bool is_user) {
  102. if (digraph == nullptr) {
  103. return;
  104. }
  105. int dup_idx = 0;
  106. int offset = 0;
  107. if (is_user) {
  108. offset = 1;
  109. }
  110. // Draw edge
  111. for (auto &nd : nodes) {
  112. auto &succs = GetInputs(nd);
  113. auto num = succs.size();
  114. for (size_t i = 0; i < num; i++) {
  115. auto &t = succs.at(i);
  116. MS_EXCEPTION_IF_NULL(t);
  117. if (t->isa<ValueNode>() || t->isa<Parameter>()) {
  118. if ((!is_user) || (i != 0)) {
  119. // `SizeToInt(i) - offset` is just for printing as text
  120. digraph->Edge(t, nd, SizeToInt(i) - offset, dup_idx);
  121. }
  122. if (IsValueNode<FuncGraph>(t)) {
  123. auto const_graph = GetValueNode<FuncGraphPtr>(t);
  124. digraph->Edge(t, const_graph, dup_idx);
  125. }
  126. dup_idx++;
  127. } else {
  128. digraph->Edge(t, nd, SizeToInt(i) - offset);
  129. }
  130. }
  131. }
  132. }
  133. void DrawByOpt(const std::string &filename, const FuncGraphPtr &func_graph, bool is_user) {
  134. if (func_graph == nullptr) {
  135. return;
  136. }
  137. auto ret = func_graph->get_return();
  138. auto nodes = DeepScopedGraphSearch(ret);
  139. std::shared_ptr<BaseDigraph> digraph;
  140. OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> sub_graphs;
  141. ChangeFileMode(filename, S_IWUSR);
  142. if (is_user) {
  143. digraph = std::make_shared<ModelDigraph>("mindspore", filename);
  144. } else {
  145. digraph = std::make_shared<Digraph>("mindspore", filename);
  146. }
  147. MS_EXCEPTION_IF_NULL(digraph);
  148. digraph->Start();
  149. // Draw nodes
  150. DrawNodes(nodes, &sub_graphs, is_user);
  151. // Draw ValueNodes on CNodes
  152. DrawValueNodes(nodes, &sub_graphs);
  153. // Draw subgraph
  154. for (const auto &gsub : sub_graphs) {
  155. digraph->SubGraph(gsub.first, gsub.second);
  156. }
  157. // Draw edge
  158. DrawEdges(nodes, digraph, is_user);
  159. digraph->End();
  160. // set file mode to read only by user
  161. ChangeFileMode(filename, S_IRUSR);
  162. }
  163. #ifdef ENABLE_DUMP_IR
  164. void Draw(const std::string &filename, const FuncGraphPtr &func_graph) {
  165. const std::string dot_suffix = ".dot";
  166. const std::string filename_with_suffix =
  167. (filename.rfind(dot_suffix) != (filename.size() - dot_suffix.size())) ? (filename + dot_suffix) : filename;
  168. const std::string filepath = GetSaveGraphsPathName(Common::AddId(filename_with_suffix, dot_suffix));
  169. auto real_filepath = Common::CreatePrefixPath(filepath);
  170. if (!real_filepath.has_value()) {
  171. MS_LOG(ERROR) << "The export ir path: " << filepath << " is illegal.";
  172. return;
  173. }
  174. DrawByOpt(real_filepath.value(), func_graph, false);
  175. }
  176. void DrawUserFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph) {
  177. const std::string dot_suffix = ".dot";
  178. const std::string filepath = GetSaveGraphsPathName(Common::AddId(filename, dot_suffix));
  179. auto real_filepath = Common::CreatePrefixPath(filepath);
  180. if (!real_filepath.has_value()) {
  181. MS_LOG(ERROR) << "The export ir path: " << filepath << " is illegal.";
  182. return;
  183. }
  184. DrawByOpt(real_filepath.value(), func_graph, true);
  185. }
  186. #else
  187. void Draw(const std::string &, const FuncGraphPtr &) {
  188. static bool already_printed = false;
  189. if (already_printed) {
  190. return;
  191. }
  192. already_printed = true;
  193. MS_LOG(WARNING) << "The functionality of dumping function graph IR in graphviz dot format is disabled, "
  194. << "please recompile source to enable it. See help of building script.";
  195. }
  196. void DrawUserFuncGraph(const std::string &, const FuncGraphPtr &) {
  197. static bool already_printed = false;
  198. if (already_printed) {
  199. return;
  200. }
  201. already_printed = true;
  202. MS_LOG(WARNING) << "The functionality of dumping function graph IR in graphviz dot format is disabled, "
  203. << "please recompile source to enable it. See help of building script.";
  204. }
  205. #endif
  206. std::string Graphviz::Shape(const AnfNodePtr &node) {
  207. if (node == nullptr) {
  208. return "";
  209. }
  210. if (node->isa<CNode>()) {
  211. return "plaintext";
  212. }
  213. if (node->isa<Parameter>()) {
  214. return "octagon";
  215. }
  216. if (IsValueNode<FuncGraph>(node)) {
  217. return "oval";
  218. }
  219. return "plaintext";
  220. }
  221. std::string Graphviz::Color(const AnfNodePtr &node) {
  222. if (node == nullptr) {
  223. return "";
  224. }
  225. if (node->isa<CNode>()) {
  226. return "cornsilk";
  227. }
  228. if (node->isa<Parameter>()) {
  229. return "paleturquoise";
  230. }
  231. if (IsValueNode<FuncGraph>(node)) {
  232. return "palegreen";
  233. }
  234. return "lavender";
  235. }
  236. void BaseDigraph::Start() {
  237. buffer_ << "digraph " << name_ << " {" << std::endl;
  238. buffer_ << "compound=true" << std::endl;
  239. }
  240. void BaseDigraph::Head(const AnfNodePtr &node, int id) {
  241. if (node == nullptr) {
  242. return;
  243. }
  244. buffer_ << "node" << node << "_" << id;
  245. if (node->isa<CNode>() || (node->isa<ValueNode>() && !IsValueNode<FuncGraph>(node))) {
  246. buffer_ << ":core";
  247. }
  248. }
  249. void BaseDigraph::Tail(const AnfNodePtr &node, int idx, int id) {
  250. if (node == nullptr) {
  251. return;
  252. }
  253. buffer_ << "node" << node << "_" << id;
  254. buffer_ << ":" << idx;
  255. }
  256. void BaseDigraph::Tail(const FuncGraphPtr &func_graph) {
  257. if (func_graph == nullptr) {
  258. return;
  259. }
  260. buffer_ << "node" << func_graph->get_return() << "_" << 0;
  261. }
  262. void BaseDigraph::Edge(const AnfNodePtr &start, const FuncGraphPtr &end, int id_start) {
  263. Head(start, id_start);
  264. buffer_ << "->";
  265. Tail(end);
  266. buffer_ << "[lhead=cluster_" << end;
  267. buffer_ << ",dir=both,arrowhead=dot,style=filled,color=blue]";
  268. buffer_ << std::endl;
  269. }
  270. void BaseDigraph::End() {
  271. buffer_ << "}" << std::endl;
  272. if (fout_.is_open()) {
  273. fout_ << buffer_.str();
  274. }
  275. }
  276. void BaseDigraph::FuncGraphParameters(const FuncGraphPtr &key) {
  277. buffer_ << "parameters_" << key << "[shape=plaintext ";
  278. buffer_ << "label=<<table bgcolor='paleturquoise' cellspacing='0' cellborder='1' border='0'>";
  279. buffer_ << "<tr><td>parameters</td></tr>";
  280. int count = 0;
  281. for (auto &parameter : key->parameters()) {
  282. MS_EXCEPTION_IF_NULL(parameter);
  283. buffer_ << "<tr><td>";
  284. buffer_ << parameter->ToString();
  285. auto param = parameter->cast<ParameterPtr>();
  286. if (param && param->has_default()) {
  287. auto tensor_v = param->default_param();
  288. if (tensor_v && tensor_v->isa<tensor::Tensor>()) {
  289. auto tensor = tensor_v->cast<tensor::TensorPtr>();
  290. auto &shape = tensor->shape();
  291. std::ostringstream shape_str;
  292. std::copy(shape.begin(), shape.end(), std::ostream_iterator<int>(shape_str, ","));
  293. buffer_ << "[" << shape_str.str() << "]";
  294. }
  295. }
  296. buffer_ << "</td></tr>";
  297. count++;
  298. // Wrap the text.
  299. if (count % 10 == 0) {
  300. buffer_ << "\n";
  301. }
  302. }
  303. buffer_ << "</table>>,];";
  304. }
  305. void BaseDigraph::SubGraph(const FuncGraphPtr &key, const std::shared_ptr<BaseDigraph> &gsub) {
  306. if (key == nullptr || gsub == nullptr) {
  307. return;
  308. }
  309. std::string label = key->debug_info()->get_full_name();
  310. if (label.empty()) {
  311. label = gsub->name();
  312. }
  313. std::string label_managed = "[managed]";
  314. if (key->manager() == nullptr) {
  315. label_managed = "[not managed]";
  316. }
  317. label += label_managed;
  318. gsub->FuncGraphParameters(key);
  319. buffer_ << "subgraph cluster_" << key << "{" << std::endl;
  320. buffer_ << "id=cluster_" << key << std::endl;
  321. buffer_ << "label=\"" << label << "\"" << std::endl;
  322. buffer_ << "fontname=\"Courier New\"" << std::endl;
  323. buffer_ << gsub->buffer().str();
  324. buffer_ << "}" << std::endl;
  325. }
  326. Digraph::~Digraph() {
  327. try {
  328. if (fout_.is_open()) {
  329. fout_.close();
  330. }
  331. } catch (const std::exception &e) {
  332. MS_LOG(ERROR) << "Exception when closing file " << filename_;
  333. }
  334. }
  335. static std::string ReplaceAll(std::string str, const std::string &from, const std::string &to) {
  336. size_t start_pos = 0;
  337. while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
  338. (void)str.replace(start_pos, from.length(), to);
  339. // Handles case where 'to' is a substring of 'from'
  340. start_pos += to.length();
  341. }
  342. return str;
  343. }
  344. static void DrawValueNode(Graphviz *const graph_obj, const ValueNodePtr &node) {
  345. MS_EXCEPTION_IF_NULL(graph_obj);
  346. graph_obj->buffer() << "label=<<table port='core' cellborder='0' cellspacing='2' bgcolor='" << graph_obj->Color(node)
  347. << "'>";
  348. graph_obj->buffer() << "<tr><td bgcolor='white'>" << ValueType(node) << "</td></tr>"
  349. << "<tr><td>";
  350. if (IsValueNode<MetaFuncGraph>(node)) {
  351. graph_obj->buffer() << node->value()->cast<MetaFuncGraphPtr>()->name();
  352. } else if (IsValueNode<parse::NameSpace>(node)) {
  353. graph_obj->buffer() << node->value()->cast<parse::NameSpacePtr>()->name();
  354. } else if (IsValueNode<parse::Symbol>(node)) {
  355. graph_obj->buffer() << ReplaceSpecialChar(node->value()->cast<parse::SymbolPtr>()->name());
  356. } else {
  357. std::ostringstream ss;
  358. ss << node->value()->ToString();
  359. std::string s = ReplaceAll(ss.str(), ", ", "<br/>");
  360. graph_obj->buffer() << s;
  361. ValuePtr value = node->value();
  362. if (value->isa<Primitive>()) {
  363. PrimitivePtr primitive = value->cast<PrimitivePtr>();
  364. graph_obj->buffer() << "</td></tr>"
  365. << "<tr><td align='left'>";
  366. if (!primitive->instance_name().empty()) {
  367. graph_obj->buffer() << "instance name:"
  368. << " " << primitive->instance_name() << "<br/>";
  369. }
  370. auto attrs = primitive->attrs();
  371. if (attrs.size() > 0) {
  372. graph_obj->buffer() << "</td></tr>"
  373. << "<tr><td align='left'>";
  374. int i = 0;
  375. for (const auto &attr : attrs) {
  376. if (i != 0) {
  377. graph_obj->buffer() << "<br/>";
  378. }
  379. graph_obj->buffer() << attr.first << " ";
  380. if (attr.second == nullptr) {
  381. graph_obj->buffer() << " ";
  382. } else {
  383. graph_obj->buffer() << attr.second->ToString();
  384. }
  385. i++;
  386. }
  387. }
  388. }
  389. }
  390. graph_obj->buffer() << "</td></tr>"
  391. << "</table>>,";
  392. }
  393. static void DrawParallelInfo(Graphviz *const graph_obj, const CNodePtr &node) {
  394. if (graph_obj == nullptr || node == nullptr) {
  395. return;
  396. }
  397. auto distributed_operation_info = node->user_data<parallel::OperatorInfo>();
  398. if (distributed_operation_info != nullptr) {
  399. auto strategyPtr = distributed_operation_info->strategy();
  400. if (strategyPtr != nullptr) {
  401. auto num = node->inputs().size();
  402. graph_obj->buffer() << "<tr><td colspan='" << num << "' ";
  403. graph_obj->buffer() << "bgcolor='" << graph_obj->Color(node) << "'>";
  404. std::vector<ValuePtr> temp = {MakeValue(strategyPtr->GetInputStage()), MakeValue(strategyPtr->GetInputDim())};
  405. ValueTuplePtr strategy_tuple = std::make_shared<ValueTuple>(temp);
  406. graph_obj->buffer() << "Strategy " << strategy_tuple->ToString();
  407. graph_obj->buffer() << "</td></tr>" << std::endl;
  408. }
  409. }
  410. }
  411. static void DrawCNode(Graphviz *const graph_obj, const CNodePtr &node) {
  412. if (graph_obj == nullptr || node == nullptr || node->size() == 0) {
  413. return;
  414. }
  415. auto num = node->size();
  416. bool is_modelgraph = false;
  417. if (typeid(*graph_obj) == typeid(ModelDigraph)) {
  418. is_modelgraph = true;
  419. num -= 1;
  420. }
  421. graph_obj->buffer() << "label=<<table port='core'>" << std::endl;
  422. // Draw ports for CNode
  423. if (num > 0) {
  424. graph_obj->buffer() << "<tr>";
  425. for (size_t i = 0; i < num; i++) {
  426. graph_obj->buffer() << "<td port='" << i << "'>" << i << "</td>";
  427. }
  428. graph_obj->buffer() << "</tr>" << std::endl;
  429. }
  430. // Draw op name
  431. graph_obj->buffer() << "<tr><td";
  432. if (num > 0) {
  433. graph_obj->buffer() << " colspan='" << num << "'";
  434. }
  435. graph_obj->buffer() << " bgcolor='" << graph_obj->Color(node) << "'>";
  436. if (IsValueNode<Primitive>(node->input(0)) && is_modelgraph) {
  437. auto primitive = GetValueNode<PrimitivePtr>(node->input(0));
  438. graph_obj->buffer() << ReplaceAll(primitive->ToString(), ", ", "<br/>");
  439. auto attrs = primitive->attrs();
  440. if (attrs.size() > 0) {
  441. graph_obj->buffer() << "</td></tr>" << std::endl << "<tr><td";
  442. // Draw attrs
  443. if (num > 0) {
  444. graph_obj->buffer() << " colspan='" << num << "'";
  445. }
  446. graph_obj->buffer() << ">";
  447. int i = 0;
  448. for (auto &attr : attrs) {
  449. if (i != 0) {
  450. graph_obj->buffer() << "<br/>";
  451. }
  452. graph_obj->buffer() << attr.first << " " << attr.second->ToString();
  453. i++;
  454. }
  455. }
  456. graph_obj->buffer() << "CNode";
  457. } else {
  458. graph_obj->buffer() << "CNode(" << node->ToString() << ")";
  459. }
  460. graph_obj->buffer() << "</td></tr>" << std::endl;
  461. DrawParallelInfo(graph_obj, node);
  462. graph_obj->buffer() << "</table>>,";
  463. }
  464. void Digraph::Node(const AnfNodePtr &node, int id) {
  465. if (node == nullptr) {
  466. return;
  467. }
  468. buffer_ << "node" << node << "_" << id;
  469. buffer_ << "[";
  470. // Set fontname
  471. buffer_ << "fontname=\"Courier New\",";
  472. // Set label and shape
  473. buffer_ << "shape=" << Shape(node) << ",";
  474. if (node->isa<CNode>()) {
  475. DrawCNode(this, node->cast<CNodePtr>());
  476. } else if (node->isa<ValueNode>() && !IsValueNode<FuncGraph>(node)) {
  477. DrawValueNode(this, node->cast<ValueNodePtr>());
  478. } else {
  479. buffer_ << "label=\"" << node->ToString();
  480. if (IsValueNode<FuncGraph>(node)) {
  481. FuncGraphPtr nextNet = GetValueNode<FuncGraphPtr>(node);
  482. std::string nextNetName = nextNet->debug_info()->get_full_name();
  483. if (!nextNetName.empty()) {
  484. buffer_ << "[" << nextNet->debug_info()->get_full_name().c_str() << "]";
  485. }
  486. }
  487. buffer_ << "\","
  488. << "style=filled,fillcolor=" << Color(node) << ",";
  489. }
  490. // Set URL for func graph
  491. if (IsValueNode<FuncGraph>(node)) {
  492. buffer_ << "URL=\"#cluster_" << GetValueNode(node) << "\",";
  493. }
  494. buffer_ << "]" << std::endl;
  495. }
  496. void Digraph::Edge(const AnfNodePtr &start, const AnfNodePtr &end, int idx, int id_start) {
  497. if (start == nullptr || end == nullptr) {
  498. return;
  499. }
  500. Head(start, id_start);
  501. buffer_ << "->";
  502. Tail(end, idx);
  503. buffer_ << "[arrowhead=vee,";
  504. // Check how many inputs for end
  505. if (end->isa<CNode>()) {
  506. auto cnode = end->cast<CNodePtr>();
  507. MS_EXCEPTION_IF_NULL(cnode);
  508. auto num = cnode->inputs().size();
  509. if (idx == 0 && num > 1) {
  510. buffer_ << "style=dashed";
  511. }
  512. }
  513. buffer_ << "]" << std::endl;
  514. }
  515. ModelDigraph::~ModelDigraph() {
  516. try {
  517. if (fout_.is_open()) {
  518. fout_.close();
  519. }
  520. } catch (const std::exception &e) {
  521. MS_LOG(ERROR) << "exception when closing file " << filename_;
  522. }
  523. }
  524. std::string ModelDigraph::Shape(const AnfNodePtr &node) {
  525. if (node == nullptr) {
  526. return "";
  527. }
  528. if (node->isa<CNode>()) {
  529. return "plaintext";
  530. }
  531. if (node->isa<Parameter>()) {
  532. return "ellipse";
  533. }
  534. if (IsValueNode<FuncGraph>(node)) {
  535. return "oval";
  536. }
  537. return "plaintext";
  538. }
  539. void ModelDigraph::Node(const AnfNodePtr &node, int id) {
  540. if (node == nullptr) {
  541. return;
  542. }
  543. if (IsValueNode<Primitive>(node)) {
  544. return;
  545. }
  546. buffer_ << "node" << node << "_" << id;
  547. buffer_ << "[";
  548. // Set fontname
  549. buffer_ << "fontname=\"Courier New\",";
  550. // Set label and shape
  551. buffer_ << "shape=" << Shape(node) << ",";
  552. if (node->isa<CNode>()) {
  553. DrawCNode(this, node->cast<CNodePtr>());
  554. } else if (node->isa<ValueNode>() && !IsValueNode<FuncGraph>(node)) {
  555. DrawValueNode(this, node->cast<ValueNodePtr>());
  556. } else {
  557. buffer_ << "label=\"" << node->ToString() << "\",";
  558. buffer_ << "style=filled,fillcolor=" << Color(node) << ",";
  559. }
  560. // Set URL for func graph
  561. if (IsValueNode<FuncGraph>(node)) {
  562. buffer_ << "URL=\"#cluster_" << GetValueNode(node) << "\",";
  563. }
  564. buffer_ << "]" << std::endl;
  565. }
  566. void ModelDigraph::Edge(const AnfNodePtr &start, const AnfNodePtr &end, int idx, int id_start) {
  567. if (start == nullptr || end == nullptr) {
  568. return;
  569. }
  570. Head(start, id_start);
  571. buffer_ << "->";
  572. Tail(end, idx);
  573. buffer_ << "[arrowhead=vee,";
  574. buffer_ << "]" << std::endl;
  575. }
  576. struct DrawerRegister {
  577. DrawerRegister() {
  578. FuncGraph::set_drawer(
  579. [](const std::string &filename, const FuncGraphPtr &func_graph) { Draw(filename, func_graph); });
  580. }
  581. ~DrawerRegister() = default;
  582. } drawer_regsiter;
  583. } // namespace draw
  584. } // namespace mindspore