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