graph_viz_pass.cc 3.4 KB
Newer Older
X
Xin Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */

#include <algorithm>
#include <unordered_set>

#include "paddle/fluid/framework/ir/graph_viz_pass.h"
19
#include "paddle/fluid/inference/analysis/dot.h"
X
Xin Pan 已提交
20 21 22 23

namespace paddle {
namespace framework {
namespace ir {
X
Xin Pan 已提交
24
static const char kGraphVizPath[] = "graph_viz_path";
25
using inference::analysis::Dot;
X
Xin Pan 已提交
26

X
Xin Pan 已提交
27
std::unique_ptr<ir::Graph> GraphVizPass::ApplyImpl(
X
Xin Pan 已提交
28
    std::unique_ptr<ir::Graph> graph) const {
X
Xin Pan 已提交
29
  const std::string graph_viz_path = Get<std::string>(kGraphVizPath);
30
  VLOG(3) << "draw IR graph viz to " << graph_viz_path;
X
Xin Pan 已提交
31
  std::unique_ptr<std::ostream> fout(new std::ofstream(graph_viz_path));
X
Xin Pan 已提交
32 33 34
  PADDLE_ENFORCE(fout->good());
  std::ostream& sout = *fout;

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  std::unordered_map<const ir::Node*, std::string> node2dot;

  Dot dot;

  std::vector<Dot::Attr> op_attrs({Dot::Attr("style", "filled"),
                                   Dot::Attr("shape", "box"),
                                   Dot::Attr("fillcolor", "red")});
  std::vector<Dot::Attr> var_attrs({Dot::Attr("style", "filled,rounded"),
                                    // Dot::Attr("shape", "diamond"),
                                    Dot::Attr("fillcolor", "yellow")});

  std::vector<Dot::Attr> marked_op_attrs({Dot::Attr("style", "filled"),
                                          Dot::Attr("shape", "box"),
                                          Dot::Attr("fillcolor", "lightgray")});
  std::vector<Dot::Attr> marked_var_attrs(
      {Dot::Attr("style", "filled,rounded"),
       // Dot::Attr("shape", "diamond"),
       Dot::Attr("fillcolor", "lightgray")});

  auto marked_nodes = ConsumeMarkedNodes(graph.get());
  // Create nodes
  for (const Node* n : graph->Nodes()) {
    std::string node_id = n->Name() + "(" + std::to_string(n->id()) + ")";
    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()) {
      decltype(op_attrs) attr =
          marked_nodes.count(n) ? marked_var_attrs : var_attrs;
      dot.AddNode(node_id, attr, node_id);
X
Xin Pan 已提交
66
    }
67 68 69 70 71 72 73 74
    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 已提交
75 76 77
    }
  }

78 79
  sout << dot.Build();

X
Xin Pan 已提交
80 81
  return graph;
}
X
Xin Pan 已提交
82

83 84 85 86 87 88 89 90 91 92 93
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 已提交
94 95 96
}  // namespace ir
}  // namespace framework
}  // namespace paddle
X
Xin Pan 已提交
97

X
Xin Pan 已提交
98 99
REGISTER_PASS(graph_viz_pass, paddle::framework::ir::GraphVizPass)
    .RequirePassAttr(paddle::framework::ir::kGraphVizPath);