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

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