ssa_graph_printer.h 1.6 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
};

class SSAGraghBuilderWithPrinter : public SSAGraphBuilder {
X
Xin Pan 已提交
39 40
 protected:
  std::unique_ptr<ir::Graph> ApplyImpl(
X
Xin Pan 已提交
41
      std::unique_ptr<ir::Graph> graph) const override {
X
Xin Pan 已提交
42
    std::unique_ptr<std::ostream> fout(
X
Xin Pan 已提交
43
        new std::ofstream(Get<const std::string>("debug_graphviz_path")));
X
Xin Pan 已提交
44
    PADDLE_ENFORCE(fout->good());
X
Xin Pan 已提交
45 46
    Get<GraphvizSSAGraphPrinter>("graph_printer").Print(*graph, *fout);
    return graph;
Y
yuyang18 已提交
47 48 49 50 51 52
  }
};

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