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