graph_viz_pass.cc 4.6 KB
Newer Older
X
Xin Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

15
#include "paddle/fluid/framework/ir/graph_viz_pass.h"
X
Xin Pan 已提交
16
#include <algorithm>
17
#include <unordered_map>
X
Xin Pan 已提交
18
#include <unordered_set>
C
chengduo 已提交
19
#include "paddle/fluid/framework/ir/graph_printer.h"
20
#include "paddle/fluid/framework/op_proto_maker.h"
X
Xin Pan 已提交
21
#include "paddle/fluid/inference/analysis/dot.h"
22
#include "paddle/fluid/string/printf.h"
X
Xin Pan 已提交
23 24 25 26

namespace paddle {
namespace framework {
namespace ir {
27
using inference::analysis::Dot;
28
namespace {
X
fix  
Xin Pan 已提交
29 30 31 32
std::string FormatName(const Node* node) {
  if (!node->IsOp() || !node->Op() ||
      !node->Op()->HasAttr(OpProtoAndCheckerMaker::OpNamescopeAttrName())) {
    return node->Name();
33 34
  }
  const std::string full_scope = boost::get<std::string>(
X
fix  
Xin Pan 已提交
35 36
      node->Op()->GetAttr(OpProtoAndCheckerMaker::OpNamescopeAttrName()));
  return string::Sprintf("%s%s", full_scope.c_str(), node->Name().c_str());
37 38
}
}  // namespace
X
Xin Pan 已提交
39

40
void GraphVizPass::ApplyImpl(ir::Graph* graph) const {
C
chengduo 已提交
41
  const std::string& graph_viz_path = Get<std::string>(kGraphvizPath);
M
minqiyang 已提交
42
  VLOG(3) << "draw IR graph viz to " << graph_viz_path;
X
Xin Pan 已提交
43
  std::unique_ptr<std::ostream> fout(new std::ofstream(graph_viz_path));
X
Xin Pan 已提交
44 45 46
  PADDLE_ENFORCE(fout->good());
  std::ostream& sout = *fout;

47 48 49 50
  std::unordered_map<const ir::Node*, std::string> node2dot;

  Dot dot;

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  const std::vector<Dot::Attr> op_attrs({
      Dot::Attr("style", "rounded,filled,bold"),  //
      Dot::Attr("shape", "box"),                  //
      Dot::Attr("color", "#303A3A"),              //
      Dot::Attr("fontcolor", "#ffffff"),          //
      Dot::Attr("width", "1.3"),                  //
      Dot::Attr("height", "0.84"),                //
      Dot::Attr("fontname", "Arial"),             //
  });
  const std::vector<Dot::Attr> arg_attrs({
      Dot::Attr("shape", "box"),                  //
      Dot::Attr("style", "rounded,filled,bold"),  //
      Dot::Attr("fontname", "Arial"),             //
      Dot::Attr("fillcolor", "#999999"),          //
      Dot::Attr("color", "#dddddd"),              //
  });

  const std::vector<Dot::Attr> param_attrs({
      Dot::Attr("shape", "box"),                  //
      Dot::Attr("style", "rounded,filled,bold"),  //
      Dot::Attr("fontname", "Arial"),             //
      Dot::Attr("color", "#148b97"),              //
      Dot::Attr("fontcolor", "#ffffff"),          //
  });

  const std::vector<Dot::Attr> marked_op_attrs(
      {Dot::Attr("style", "rounded,filled,bold"), Dot::Attr("shape", "box"),
       Dot::Attr("fillcolor", "yellow")});
  const std::vector<Dot::Attr> marked_var_attrs(
      {Dot::Attr("style", "filled,rounded"), Dot::Attr("shape", "box"),
       Dot::Attr("fillcolor", "yellow")});
82

83
  auto marked_nodes = ConsumeMarkedNodes(graph);
84 85
  // Create nodes
  for (const Node* n : graph->Nodes()) {
86
    std::string node_id = FormatName(n) + "(" + std::to_string(n->id()) + ")";
87 88 89 90 91
    if (n->IsOp()) {
      decltype(op_attrs) attr =
          marked_nodes.count(n) ? marked_op_attrs : op_attrs;
      dot.AddNode(node_id, attr, node_id);
    } else if (n->IsVar()) {
92 93 94 95 96 97 98 99 100 101 102
      decltype(op_attrs)* attr;
      if (marked_nodes.count(n)) {
        attr = &marked_var_attrs;
      } else if (const_cast<Node*>(n)->Var() &&
                 const_cast<Node*>(n)->Var()->Persistable()) {
        attr = &param_attrs;
      } else {
        attr = &arg_attrs;
      }

      dot.AddNode(node_id, *attr, node_id);
X
Xin Pan 已提交
103
    }
104 105 106 107 108 109 110 111
    node2dot[n] = node_id;
  }
  // Create edges
  for (const Node* n : graph->Nodes()) {
    const auto& src_id = node2dot.at(n);
    for (auto* out : n->outputs) {
      const auto& trg_id = node2dot.at(out);
      dot.AddEdge(src_id, trg_id, {});
X
Xin Pan 已提交
112 113 114
    }
  }

115
  sout << dot.Build();
X
Xin Pan 已提交
116
}
X
Xin Pan 已提交
117

118 119 120 121 122 123 124 125 126 127 128
GraphVizPass::marked_nodes_t GraphVizPass::ConsumeMarkedNodes(
    Graph* graph) const {
  marked_nodes_t res;
  if (graph->Has(kGraphvizMarkedNodeAttr)) {
    auto& attr = graph->Get<marked_nodes_t>(kGraphvizMarkedNodeAttr);
    res = attr;
    attr.clear();
  }
  return res;
}

X
Xin Pan 已提交
129 130 131
}  // namespace ir
}  // namespace framework
}  // namespace paddle
X
Xin Pan 已提交
132

X
Xin Pan 已提交
133
REGISTER_PASS(graph_viz_pass, paddle::framework::ir::GraphVizPass)
C
chengduo 已提交
134
    .RequirePassAttr(paddle::framework::ir::kGraphvizPath);