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.h 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright 2019 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. #ifndef MINDSPORE_CCSRC_DEBUG_DRAW_H_
  17. #define MINDSPORE_CCSRC_DEBUG_DRAW_H_
  18. #include <fstream>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "ir/anf.h"
  23. #include "utils/any.h"
  24. #include "pipeline/parse/resolve.h"
  25. namespace mindspore {
  26. namespace draw {
  27. namespace parse = mindspore::parse;
  28. class Graphviz {
  29. public:
  30. Graphviz(const std::string &name, const std::string &filename) : name_(name), filename_(filename), fout_(filename_) {}
  31. explicit Graphviz(const std::string &name) : name_(name) {}
  32. virtual ~Graphviz() {}
  33. virtual void Start() {}
  34. virtual void End() {}
  35. virtual std::string Shape(AnfNodePtr node);
  36. std::string Color(const AnfNodePtr &node);
  37. std::ostringstream &buffer() { return buffer_; }
  38. std::ostringstream buffer_;
  39. protected:
  40. std::string name_;
  41. std::string filename_;
  42. std::ofstream fout_;
  43. };
  44. class BaseDigraph : public Graphviz {
  45. public:
  46. BaseDigraph(const std::string &name, const std::string &filename) : Graphviz(name, filename) {}
  47. explicit BaseDigraph(const std::string &name) : Graphviz(name) {}
  48. ~BaseDigraph() override = default;
  49. virtual void Node(AnfNodePtr node, int id = 0) = 0;
  50. virtual void Edge(AnfNodePtr start, AnfNodePtr end, int idx, int idx_start = 0) = 0;
  51. void Start() override;
  52. void End() override;
  53. virtual void Edge(AnfNodePtr start, FuncGraphPtr end, int id_start);
  54. void FuncGraphParameters(const FuncGraphPtr &key);
  55. void SubGraph(const FuncGraphPtr &key, const std::shared_ptr<BaseDigraph> &gsub);
  56. const std::string &name() const { return name_; }
  57. protected:
  58. void Head(const AnfNodePtr &node, int id = 0);
  59. void Tail(const AnfNodePtr &node, int idx, int id = 0);
  60. void Tail(const FuncGraphPtr &func_graph);
  61. };
  62. class Digraph : public BaseDigraph {
  63. public:
  64. Digraph(const std::string &name, const std::string &filename) : BaseDigraph(name, filename) {}
  65. explicit Digraph(const std::string &name) : BaseDigraph(name) {}
  66. ~Digraph() override;
  67. void Node(AnfNodePtr node, int id = 0) override;
  68. void Edge(AnfNodePtr start, AnfNodePtr end, int idx, int idx_start = 0) override;
  69. };
  70. class ModelDigraph : public BaseDigraph {
  71. public:
  72. ModelDigraph(const std::string &name, const std::string &filename) : BaseDigraph(name, filename) {}
  73. explicit ModelDigraph(const std::string &name) : BaseDigraph(name) {}
  74. ~ModelDigraph() override;
  75. std::string Shape(AnfNodePtr node) override;
  76. void Node(AnfNodePtr node, int id = 0) override;
  77. void Edge(AnfNodePtr start, AnfNodePtr end, int idx, int idx_start = 0) override;
  78. };
  79. // API to draw
  80. void Draw(const std::string &filename, const FuncGraphPtr &func_graph);
  81. void DrawUserFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph);
  82. } // namespace draw
  83. } // namespace mindspore
  84. #endif // MINDSPORE_CCSRC_DEBUG_DRAW_H_