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