ir.cc 6.7 KB
Newer Older
F
flame 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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 "paddle/fluid/pybind/ir.h"
W
WangZhen 已提交
16
#include <algorithm>
17
#include <memory>
F
flame 已提交
18 19
#include <string>
#include <unordered_map>
W
WangZhen 已提交
20
#include <unordered_set>
21
#include <utility>
F
flame 已提交
22
#include "paddle/fluid/framework/ir/graph.h"
W
WangZhen 已提交
23
#include "paddle/fluid/framework/ir/graph_helper.h"
W
WangZhen 已提交
24
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
F
flame 已提交
25 26 27 28 29 30 31 32
#include "paddle/fluid/framework/ir/node.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/var_desc.h"
#include "pybind11/stl.h"

namespace py = pybind11;
using paddle::framework::ir::Graph;
using paddle::framework::ir::Node;
W
WangZhen 已提交
33
using paddle::framework::ir::GraphSafeRemoveNodes;
W
WangZhen 已提交
34 35 36 37
using paddle::framework::ir::HasCircle;
using paddle::framework::ir::GraphNum;
using paddle::framework::ir::TopologySortOperations;
using paddle::framework::ir::BuildOperationAdjList;
F
flame 已提交
38 39 40 41 42 43 44 45
using paddle::framework::OpDesc;
using paddle::framework::ProgramDesc;
using paddle::framework::VarDesc;
using pybind11::return_value_policy;

namespace paddle {
namespace pybind {
void BindGraph(py::module *m) {
W
WangZhen 已提交
46
  m->def("graph_safe_remove_nodes", GraphSafeRemoveNodes);
W
WangZhen 已提交
47 48 49 50 51 52
  m->def("has_circle", HasCircle);
  m->def("graph_num", GraphNum);
  m->def("topology_sort", TopologySortOperations,
         return_value_policy::reference);
  m->def("build_adjacency_list", BuildOperationAdjList,
         return_value_policy::reference);
F
flame 已提交
53 54 55 56 57
  py::class_<Graph, std::shared_ptr<Graph>>(
      *m, "Graph",
      "The graph is a Directed Acyclic Single Static Assignment Graph, see "
      "`paddle::ir::Graph` for details.")
      .def(py::init<const ProgramDesc &>())
58
      .def("clone", &Graph::Clone)
F
flame 已提交
59 60 61 62 63
      .def("has", &Graph::Has)
      .def("get_int", &Graph::Get<int>)
      .def("get_float", &Graph::Get<float>)
      .def("get_double", &Graph::Get<double>)
      .def("get_string", &Graph::Get<std::string>)
64 65
      .def("get_marked_nodes", &Graph::Get<std::unordered_set<const Node *>>,
           return_value_policy::reference)
F
flame 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      .def("set", [](Graph &self, const std::string &attr_name,
                     int attr) { return self.Set(attr_name, new int(attr)); })
      .def("set",
           [](Graph &self, const std::string &attr_name,
              const std::string &attr) {
             return self.Set(attr_name, new std::string(attr));
           })
      .def("set",
           [](Graph &self, const std::string &attr_name, float attr) {
             return self.Set(attr_name, new float(attr));
           })
      .def("set",
           [](Graph &self, const std::string &attr_name, double attr) {
             return self.Set(attr_name, new double(attr));
           })
W
WangZhen 已提交
81 82 83 84 85 86
      .def("set",
           [](Graph &self, const std::string &attr_name,
              const std::unordered_set<const Node *> &attr) {
             return self.Set(attr_name,
                             new std::unordered_set<const Node *>(attr));
           })
F
flame 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      .def("erase", &Graph::Erase)
      .def("nodes", &Graph::Nodes, return_value_policy::reference)
      .def("create_var_node",
           [](Graph &self, VarDesc &var_desc) {
             return self.CreateVarNode(&var_desc);
           },
           return_value_policy::reference)
      .def("create_op_node",
           [](Graph &self, OpDesc &op_desc) {
             return self.CreateOpNode(&op_desc);
           },
           return_value_policy::reference)
      .def("create_control_dep_var", &Graph::CreateControlDepVar,
           return_value_policy::reference)
      .def("create_empty_node", &Graph::CreateEmptyNode,
           return_value_policy::reference)
      .def("release_nodes", &Graph::ReleaseNodes)
      .def("remove_node",
           [](Graph &self, Node &node) { return self.RemoveNode(&node); })
      .def("retrieve_node", &Graph::RetrieveNode,
           return_value_policy::reference)
X
Xin Pan 已提交
108
      .def("resolve_hazard", &Graph::ResolveHazard)
109 110
      .def("origin_program_desc", &Graph::OriginProgram,
           return_value_policy::reference);
F
flame 已提交
111 112 113 114 115 116
}

void BindNode(py::module *m) {
  py::class_<Node> node(*m, "Node");
  node.def("name", &Node::Name)
      .def("node_type", &Node::NodeType)
W
WangZhen 已提交
117 118
      .def("var", &Node::Var, return_value_policy::reference)
      .def("op", &Node::Op, return_value_policy::reference)
F
flame 已提交
119 120 121 122
      .def("id", &Node::id)
      .def("is_op", &Node::IsOp)
      .def("is_var", &Node::IsVar)
      .def("is_ctrl_var", &Node::IsCtrlVar)
W
WangZhen 已提交
123
      .def("clear_inputs", [](Node &self) { self.inputs.clear(); })
124
      .def("remove_input",
W
WangZhen 已提交
125
           [](Node &self, int node_id) {
W
WangZhen 已提交
126 127 128 129 130
             auto pos = std::find_if(
                 self.inputs.begin(), self.inputs.end(),
                 [&node_id](const Node *n) { return n->id() == node_id; });
             if (pos != self.inputs.end()) {
               self.inputs.erase(pos);
W
WangZhen 已提交
131 132
             }
           })
133
      .def("remove_input",
W
WangZhen 已提交
134
           [](Node &self, Node &node) {
W
WangZhen 已提交
135 136 137 138
             auto pos =
                 std::find(self.inputs.begin(), self.inputs.end(), &node);
             if (pos != self.inputs.end()) {
               self.inputs.erase(pos);
W
WangZhen 已提交
139 140
             }
           })
141
      .def("append_input",
W
WangZhen 已提交
142
           [](Node &self, Node &node) { self.inputs.push_back(&node); })
W
WangZhen 已提交
143
      .def("clear_outputs", [](Node &self) { self.outputs.clear(); })
144
      .def("remove_output",
W
WangZhen 已提交
145
           [](Node &self, int node_id) {
W
WangZhen 已提交
146 147 148 149 150
             auto pos = std::find_if(
                 self.outputs.begin(), self.outputs.end(),
                 [&node_id](const Node *n) { return n->id() == node_id; });
             if (pos != self.outputs.end()) {
               self.outputs.erase(pos);
W
WangZhen 已提交
151 152
             }
           })
153
      .def("remove_output",
W
WangZhen 已提交
154
           [](Node &self, Node &node) {
W
WangZhen 已提交
155 156 157 158
             auto pos =
                 std::find(self.outputs.begin(), self.outputs.end(), &node);
             if (pos != self.outputs.end()) {
               self.outputs.erase(pos);
W
WangZhen 已提交
159 160
             }
           })
161
      .def("append_output",
W
WangZhen 已提交
162
           [](Node &self, Node &node) { self.outputs.push_back(&node); })
163 164
      .def_readwrite("inputs", &Node::inputs)
      .def_readwrite("outputs", &Node::outputs);
F
flame 已提交
165 166 167 168 169 170 171 172

  py::enum_<Node::Type>(node, "Type")
      .value("Operation", Node::Type::kOperation)
      .value("Variable", Node::Type::kVariable)
      .export_values();
}
}  // namespace pybind
}  // namespace paddle