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.3 kB

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