ssa_graph_printer.h 1.8 KB
Newer Older
Y
yuyang18 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

#pragma once

X
Xin Pan 已提交
17
#include <fstream>
Y
yuyang18 已提交
18
#include <iosfwd>
X
Xin Pan 已提交
19
#include <ostream>
Y
yi.wu 已提交
20
#include <string>
Y
yuyang18 已提交
21 22 23 24 25
#include "paddle/fluid/framework/details/ssa_graph_builder.h"

namespace paddle {
namespace framework {
namespace details {
X
Xin Pan 已提交
26

Y
yuyang18 已提交
27 28 29
class SSAGraphPrinter {
 public:
  virtual ~SSAGraphPrinter() {}
X
Xin Pan 已提交
30
  virtual void Print(const ir::Graph& graph, std::ostream& sout) const = 0;
Y
yuyang18 已提交
31 32 33 34
};

class GraphvizSSAGraphPrinter : public SSAGraphPrinter {
 public:
X
Xin Pan 已提交
35
  void Print(const ir::Graph& graph, std::ostream& sout) const override;
Y
yuyang18 已提交
36 37 38 39
};

class SSAGraghBuilderWithPrinter : public SSAGraphBuilder {
 public:
X
Xin Pan 已提交
40 41
  std::unique_ptr<ir::Graph> Apply(
      std::unique_ptr<ir::Graph> graph) const override {
X
Xin Pan 已提交
42 43 44 45 46 47 48
    auto new_graph =
        Get<SSAGraphBuilder>("previous_pass").Apply(std::move(graph));

    std::unique_ptr<std::ostream> fout(
        new std::ofstream(Get<std::string>("debug_graphviz_path")));
    PADDLE_ENFORCE(fout->good());
    Get<GraphvizSSAGraphPrinter>("graph_printer").Print(*new_graph, *fout);
Q
qiaolongfei 已提交
49
    return new_graph;
Y
yuyang18 已提交
50 51
  }

Y
yi.wu 已提交
52
  int GetVarDeviceID(const std::string& var_name) const override {
X
Xin Pan 已提交
53
    return Get<SSAGraphBuilder>("previous_pass").GetVarDeviceID(var_name);
Y
yi.wu 已提交
54
  }
Y
yuyang18 已提交
55 56 57 58 59
};

}  // namespace details
}  // namespace framework
}  // namespace paddle