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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 "debug/draw.h"
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <iterator>
  20. #include <vector>
  21. #include <string>
  22. #include "ir/meta_func_graph.h"
  23. #include "ir/param_info.h"
  24. #include "ir/primitive.h"
  25. #include "ir/graph_utils.h"
  26. #include "utils/utils.h"
  27. #include "frontend/operator/composite/composite.h"
  28. #include "pipeline/jit/parse/resolve.h"
  29. #include "ir/tensor.h"
  30. #include "pipeline/jit/base.h"
  31. #include "debug/common.h"
  32. namespace mindspore {
  33. // namespace to support debug utils
  34. namespace draw {
  35. namespace {
  36. // Only for ValueNode
  37. std::string ValueType(const ValueNodePtr &node) {
  38. if (node == nullptr) {
  39. return "";
  40. }
  41. auto v = node->value();
  42. return v->type_name();
  43. }
  44. std::string ReplaceSpecialChar(const std::string &str) {
  45. std::ostringstream oss;
  46. for (size_t i = 0; i < str.size(); i++) {
  47. if (str[i] == '<') {
  48. oss << "「";
  49. } else if (str[i] == '>') {
  50. oss << "」";
  51. } else {
  52. oss << str[i];
  53. }
  54. }
  55. return oss.str();
  56. }
  57. } // namespace
  58. // API of debug utils
  59. void DrawNodes(const std::vector<AnfNodePtr> &nodes, OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> *sub_graphs,
  60. bool is_user) {
  61. if (sub_graphs == nullptr) {
  62. return;
  63. }
  64. for (auto &nd : nodes) {
  65. MS_EXCEPTION_IF_NULL(nd);
  66. auto sub_graph = nd->func_graph();
  67. if (sub_graph != nullptr) {
  68. auto gsub = (*sub_graphs)[sub_graph];
  69. if (gsub == nullptr) {
  70. if (is_user) {
  71. gsub = std::make_shared<ModelDigraph>(sub_graph->ToString());
  72. } else {
  73. gsub = std::make_shared<Digraph>(sub_graph->ToString());
  74. }
  75. (*sub_graphs)[sub_graph] = gsub;
  76. }
  77. if (!nd->isa<Parameter>()) {
  78. gsub->Node(nd);
  79. }
  80. }
  81. }
  82. }
  83. void DrawValueNodes(const std::vector<AnfNodePtr> &nodes,
  84. OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> *sub_graphs) {
  85. if (sub_graphs == nullptr) {
  86. return;
  87. }
  88. int dup_idx = 0;
  89. for (auto &nd : nodes) {
  90. for (auto &t : GetInputs(nd)) {
  91. MS_EXCEPTION_IF_NULL(t);
  92. MS_EXCEPTION_IF_NULL(nd);
  93. if (t->isa<ValueNode>() && (*sub_graphs).find(nd->func_graph()) != (*sub_graphs).end()) {
  94. (*sub_graphs)[nd->func_graph()]->Node(t, dup_idx);
  95. dup_idx++;
  96. } else if (t->isa<Parameter>() && (*sub_graphs).find(t->func_graph()) != (*sub_graphs).end()) {
  97. (*sub_graphs)[t->func_graph()]->Node(t, dup_idx);
  98. dup_idx++;
  99. }
  100. }
  101. }
  102. }
  103. void DrawEdges(const std::vector<AnfNodePtr> &nodes, const std::shared_ptr<BaseDigraph> &digraph, bool is_user) {
  104. if (digraph == nullptr) {
  105. return;
  106. }
  107. int dup_idx = 0;
  108. int offset = 0;
  109. if (is_user) {
  110. offset = 1;
  111. }
  112. // Draw edge
  113. for (auto &nd : nodes) {
  114. auto &succs = GetInputs(nd);
  115. auto num = succs.size();
  116. for (size_t i = 0; i < num; i++) {
  117. auto &t = succs.at(i);
  118. MS_EXCEPTION_IF_NULL(t);
  119. if (t->isa<ValueNode>() || t->isa<Parameter>()) {
  120. if ((!is_user) || (i != 0)) {
  121. // `SizeToInt(i) - offset` is just for printing as text
  122. digraph->Edge(t, nd, SizeToInt(i) - offset, dup_idx);
  123. }
  124. if (IsValueNode<FuncGraph>(t)) {
  125. auto const_graph = GetValueNode<FuncGraphPtr>(t);
  126. digraph->Edge(t, const_graph, dup_idx);
  127. }
  128. dup_idx++;
  129. } else {
  130. digraph->Edge(t, nd, SizeToInt(i) - offset);
  131. }
  132. }
  133. }
  134. }
  135. void DrawByOpt(const std::string &filename, const FuncGraphPtr &func_graph, bool is_user) {
  136. if (func_graph == nullptr) {
  137. return;
  138. }
  139. auto ret = func_graph->get_return();
  140. auto nodes = DeepScopedGraphSearch(ret);
  141. std::shared_ptr<BaseDigraph> digraph;
  142. OrderedMap<FuncGraphPtr, std::shared_ptr<BaseDigraph>> sub_graphs;
  143. ChangeFileMode(filename, S_IWUSR);
  144. if (is_user) {
  145. digraph = std::make_shared<ModelDigraph>("mindspore", filename);
  146. } else {
  147. digraph = std::make_shared<Digraph>("mindspore", filename);
  148. }
  149. MS_EXCEPTION_IF_NULL(digraph);
  150. digraph->Start();
  151. // Draw nodes
  152. DrawNodes(nodes, &sub_graphs, is_user);
  153. // Draw ValueNodes on CNodes
  154. DrawValueNodes(nodes, &sub_graphs);
  155. // Draw subgraph
  156. for (const auto &gsub : sub_graphs) {
  157. digraph->SubGraph(gsub.first, gsub.second);
  158. }
  159. // Draw edge
  160. DrawEdges(nodes, digraph, is_user);
  161. digraph->End();
  162. // set file mode to read only by user
  163. ChangeFileMode(filename, S_IRUSR);
  164. }
  165. #ifdef ENABLE_DUMP_IR
  166. void Draw(const std::string &filename, const FuncGraphPtr &func_graph) {
  167. const std::string dot_suffix = ".dot";
  168. const std::string filename_with_suffix =
  169. (filename.rfind(dot_suffix) != (filename.size() - dot_suffix.size())) ? (filename + dot_suffix) : filename;
  170. const std::string filepath = pipeline::GetSaveGraphsPathName(Common::AddId(filename_with_suffix, dot_suffix));
  171. auto real_filepath = Common::GetRealPath(filepath);
  172. if (!real_filepath.has_value()) {
  173. MS_LOG(EXCEPTION) << "The export ir path: " << filepath << " is not illegal.";
  174. }
  175. DrawByOpt(real_filepath.value(), func_graph, false);
  176. }
  177. void DrawUserFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph) {
  178. const std::string dot_suffix = ".dot";
  179. const std::string filepath = pipeline::GetSaveGraphsPathName(Common::AddId(filename, dot_suffix));
  180. auto real_filepath = Common::GetRealPath(filepath);
  181. if (!real_filepath.has_value()) {
  182. MS_LOG(EXCEPTION) << "The export ir path: " << filepath << " is not illegal.";
  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(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(AnfNodePtr start, 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. start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
  340. }
  341. return str;
  342. }
  343. static void DrawValueNode(Graphviz *const graph_obj, const ValueNodePtr &node) {
  344. MS_EXCEPTION_IF_NULL(graph_obj);
  345. graph_obj->buffer() << "label=<<table port='core' cellborder='0' cellspacing='2' bgcolor='" << graph_obj->Color(node)
  346. << "'>";
  347. graph_obj->buffer() << "<tr><td bgcolor='white'>" << ValueType(node) << "</td></tr>"
  348. << "<tr><td>";
  349. if (IsValueNode<MetaFuncGraph>(node)) {
  350. graph_obj->buffer() << node->value()->cast<MetaFuncGraphPtr>()->name();
  351. } else if (IsValueNode<parse::NameSpace>(node)) {
  352. graph_obj->buffer() << node->value()->cast<parse::NameSpacePtr>()->name();
  353. } else if (IsValueNode<parse::Symbol>(node)) {
  354. graph_obj->buffer() << ReplaceSpecialChar(node->value()->cast<parse::SymbolPtr>()->name());
  355. } else {
  356. std::ostringstream ss;
  357. ss << node->value()->ToString();
  358. std::string s = ReplaceAll(ss.str(), ", ", "<br/>");
  359. graph_obj->buffer() << s;
  360. ValuePtr value = node->value();
  361. if (value->isa<Primitive>()) {
  362. PrimitivePtr primitive = value->cast<PrimitivePtr>();
  363. graph_obj->buffer() << "</td></tr>"
  364. << "<tr><td align='left'>";
  365. if (!primitive->instance_name().empty()) {
  366. graph_obj->buffer() << "instance name:"
  367. << " " << primitive->instance_name() << "<br/>";
  368. }
  369. auto attrs = primitive->attrs();
  370. if (attrs.size() > 0) {
  371. graph_obj->buffer() << "</td></tr>"
  372. << "<tr><td align='left'>";
  373. int i = 0;
  374. for (const auto &attr : attrs) {
  375. if (i != 0) {
  376. graph_obj->buffer() << "<br/>";
  377. }
  378. graph_obj->buffer() << attr.first << " ";
  379. if (attr.second == nullptr) {
  380. graph_obj->buffer() << " ";
  381. } else {
  382. graph_obj->buffer() << attr.second->ToString();
  383. }
  384. i++;
  385. }
  386. }
  387. }
  388. }
  389. graph_obj->buffer() << "</td></tr>"
  390. << "</table>>,";
  391. }
  392. static void DrawParallelInfo(Graphviz *const graph_obj, const CNodePtr &node) {
  393. if (graph_obj == nullptr || node == nullptr) {
  394. return;
  395. }
  396. auto distributed_operation_info = node->user_data<parallel::OperatorInfo>();
  397. if (distributed_operation_info != nullptr) {
  398. auto strategyPtr = distributed_operation_info->strategy();
  399. if (strategyPtr != nullptr) {
  400. auto num = node->inputs().size();
  401. graph_obj->buffer() << "<tr><td colspan='" << num << "' ";
  402. graph_obj->buffer() << "bgcolor='" << graph_obj->Color(node) << "'>";
  403. std::vector<ValuePtr> temp = {MakeValue(strategyPtr->GetInputStage()), MakeValue(strategyPtr->GetInputDim())};
  404. ValueTuplePtr strategy_tuple = std::make_shared<ValueTuple>(temp);
  405. graph_obj->buffer() << "Strategy " << strategy_tuple->ToString();
  406. graph_obj->buffer() << "</td></tr>" << std::endl;
  407. }
  408. }
  409. }
  410. static void DrawCNode(Graphviz *const graph_obj, const CNodePtr &node) {
  411. if (graph_obj == nullptr || node == nullptr || node->size() == 0) {
  412. return;
  413. }
  414. auto num = node->size();
  415. bool is_modelgraph = false;
  416. if (typeid(*graph_obj) == typeid(ModelDigraph)) {
  417. is_modelgraph = true;
  418. num -= 1;
  419. }
  420. graph_obj->buffer() << "label=<<table port='core'>" << std::endl;
  421. // Draw ports for CNode
  422. if (num > 0) {
  423. graph_obj->buffer() << "<tr>";
  424. for (size_t i = 0; i < num; i++) {
  425. graph_obj->buffer() << "<td port='" << i << "'>" << i << "</td>";
  426. }
  427. graph_obj->buffer() << "</tr>" << std::endl;
  428. }
  429. // Draw op name
  430. graph_obj->buffer() << "<tr><td";
  431. if (num > 0) {
  432. graph_obj->buffer() << " colspan='" << num << "'";
  433. }
  434. graph_obj->buffer() << " bgcolor='" << graph_obj->Color(node) << "'>";
  435. if (IsValueNode<Primitive>(node->input(0)) && is_modelgraph) {
  436. auto primitive = GetValueNode<PrimitivePtr>(node->input(0));
  437. graph_obj->buffer() << ReplaceAll(primitive->ToString(), ", ", "<br/>");
  438. auto attrs = primitive->attrs();
  439. if (attrs.size() > 0) {
  440. graph_obj->buffer() << "</td></tr>" << std::endl << "<tr><td";
  441. // Draw attrs
  442. if (num > 0) {
  443. graph_obj->buffer() << " colspan='" << num << "'";
  444. }
  445. graph_obj->buffer() << ">";
  446. int i = 0;
  447. for (auto &attr : attrs) {
  448. if (i != 0) {
  449. graph_obj->buffer() << "<br/>";
  450. }
  451. graph_obj->buffer() << attr.first << " " << attr.second->ToString();
  452. i++;
  453. }
  454. }
  455. graph_obj->buffer() << "CNode";
  456. } else {
  457. graph_obj->buffer() << "CNode(" << node->ToString() << ")";
  458. }
  459. graph_obj->buffer() << "</td></tr>" << std::endl;
  460. DrawParallelInfo(graph_obj, node);
  461. graph_obj->buffer() << "</table>>,";
  462. }
  463. void Digraph::Node(AnfNodePtr node, int id) {
  464. if (node == nullptr) {
  465. return;
  466. }
  467. buffer_ << "node" << node << "_" << id;
  468. buffer_ << "[";
  469. // set fontname
  470. buffer_ << "fontname=\"Courier New\",";
  471. // set label and shape
  472. buffer_ << "shape=" << Shape(node) << ",";
  473. if (node->isa<CNode>()) {
  474. DrawCNode(this, node->cast<CNodePtr>());
  475. } else if (node->isa<ValueNode>() && !IsValueNode<FuncGraph>(node)) {
  476. DrawValueNode(this, node->cast<ValueNodePtr>());
  477. } else {
  478. buffer_ << "label=\"" << node->ToString();
  479. if (IsValueNode<FuncGraph>(node)) {
  480. FuncGraphPtr nextNet = GetValueNode<FuncGraphPtr>(node);
  481. std::string nextNetName = nextNet->debug_info()->get_full_name();
  482. if (!nextNetName.empty()) {
  483. buffer_ << "[" << nextNet->debug_info()->get_full_name().c_str() << "]";
  484. }
  485. }
  486. buffer_ << "\","
  487. << "style=filled,fillcolor=" << Color(node) << ",";
  488. }
  489. // Set URL for func graph
  490. if (IsValueNode<FuncGraph>(node)) {
  491. buffer_ << "URL=\"#cluster_" << GetValueNode(node) << "\",";
  492. }
  493. buffer_ << "]" << std::endl;
  494. }
  495. void Digraph::Edge(AnfNodePtr start, AnfNodePtr end, int idx, int id_start) {
  496. if (start == nullptr || end == nullptr) {
  497. return;
  498. }
  499. Head(start, id_start);
  500. buffer_ << "->";
  501. Tail(end, idx);
  502. buffer_ << "[arrowhead=vee,";
  503. // check how many inputs for end
  504. if (end->isa<CNode>()) {
  505. auto cnode = end->cast<CNodePtr>();
  506. MS_EXCEPTION_IF_NULL(cnode);
  507. auto num = cnode->inputs().size();
  508. if (idx == 0 && num > 1) {
  509. buffer_ << "style=dashed";
  510. }
  511. }
  512. buffer_ << "]" << std::endl;
  513. }
  514. ModelDigraph::~ModelDigraph() {
  515. try {
  516. if (fout_.is_open()) {
  517. fout_.close();
  518. }
  519. } catch (const std::exception &e) {
  520. MS_LOG(ERROR) << "exception when closing file " << filename_;
  521. }
  522. }
  523. std::string ModelDigraph::Shape(AnfNodePtr node) {
  524. if (node == nullptr) {
  525. return "";
  526. }
  527. if (node->isa<CNode>()) {
  528. return "plaintext";
  529. }
  530. if (node->isa<Parameter>()) {
  531. return "ellipse";
  532. }
  533. if (IsValueNode<FuncGraph>(node)) {
  534. return "oval";
  535. }
  536. return "plaintext";
  537. }
  538. void ModelDigraph::Node(AnfNodePtr node, int id) {
  539. if (node == nullptr) {
  540. return;
  541. }
  542. if (IsValueNode<Primitive>(node)) {
  543. return;
  544. }
  545. buffer_ << "node" << node << "_" << id;
  546. buffer_ << "[";
  547. // set fontname
  548. buffer_ << "fontname=\"Courier New\",";
  549. // set label and shape
  550. buffer_ << "shape=" << Shape(node) << ",";
  551. if (node->isa<CNode>()) {
  552. DrawCNode(this, node->cast<CNodePtr>());
  553. } else if (node->isa<ValueNode>() && !IsValueNode<FuncGraph>(node)) {
  554. DrawValueNode(this, node->cast<ValueNodePtr>());
  555. } else {
  556. buffer_ << "label=\"" << node->ToString() << "\",";
  557. buffer_ << "style=filled,fillcolor=" << Color(node) << ",";
  558. }
  559. // Set URL for func graph
  560. if (IsValueNode<FuncGraph>(node)) {
  561. buffer_ << "URL=\"#cluster_" << GetValueNode(node) << "\",";
  562. }
  563. buffer_ << "]" << std::endl;
  564. }
  565. void ModelDigraph::Edge(AnfNodePtr start, AnfNodePtr end, int idx, int id_start) {
  566. if (start == nullptr || end == nullptr) {
  567. return;
  568. }
  569. Head(start, id_start);
  570. buffer_ << "->";
  571. Tail(end, idx);
  572. buffer_ << "[arrowhead=vee,";
  573. buffer_ << "]" << std::endl;
  574. }
  575. struct DrawerRegister {
  576. DrawerRegister() {
  577. FuncGraph::set_drawer(
  578. [](const std::string &filename, const FuncGraphPtr &func_graph) { Draw(filename, func_graph); });
  579. }
  580. ~DrawerRegister() = default;
  581. } drawer_regsiter;
  582. } // namespace draw
  583. } // namespace mindspore