protobuf.cc 11.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13

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. */
Y
Yi Wang 已提交
14
#include "paddle/fluid/pybind/protobuf.h"
15

Y
Yu Yang 已提交
16
#include <deque>
Y
Yu Yang 已提交
17
#include <iostream>
L
Luo Tao 已提交
18 19
#include <string>
#include <tuple>
20

Y
Yi Wang 已提交
21 22 23 24
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/var_desc.h"
25
#include "paddle/fluid/framework/version.h"
26

27
#include "paddle/fluid/pybind/pybind_boost_headers.h"
Y
Yu Yang 已提交
28

29
namespace paddle {
30
namespace pybind {
31

32
namespace pd = paddle::framework;
F
fengjiayi 已提交
33

Y
Yu Yang 已提交
34
template <typename T>
35 36
static pybind11::bytes SerializeMessage(
    T &self) {  // NOLINT due to pybind11 convention.
Y
Yu Yang 已提交
37 38
  // Check IsInitialized in Python
  std::string retv;
39 40 41
  PADDLE_ENFORCE_EQ(self.Proto()->SerializePartialToString(&retv), true,
                    platform::errors::InvalidArgument(
                        "Failed to serialize input Desc to string."));
Y
Yu Yang 已提交
42 43 44
  return retv;
}

Y
Yu Yang 已提交
45
// Bind Methods
46 47 48
void BindProgramDesc(pybind11::module *m) {
  pybind11::class_<pd::ProgramDesc>(*m, "ProgramDesc", "")
      .def(pybind11::init<>())
Y
Yu Yang 已提交
49
      .def("__init__",
50 51
           [](pd::ProgramDesc &self, const pd::ProgramDesc &other) {
             new (&self) pd::ProgramDesc(other);
Y
Yu Yang 已提交
52
           })
53
      .def("__init__",
54
           [](pd::ProgramDesc &self, const pybind11::bytes &binary_str) {
55
             std::string str(binary_str);
56
             new (&self) pd::ProgramDesc(str);
57
           })
58 59 60 61 62
      .def("append_block", &pd::ProgramDesc::AppendBlock,
           pybind11::return_value_policy::reference)
      .def("block", &pd::ProgramDesc::MutableBlock,
           pybind11::return_value_policy::reference)
      .def("num_blocks", &pd::ProgramDesc::Size)
63
      .def("flush", &pd::ProgramDesc::Flush)
64 65
      .def("get_feed_target_names", &pd::ProgramDesc::GetFeedTargetNames)
      .def("get_fetch_target_names", &pd::ProgramDesc::GetFetchTargetNames)
66
      .def("serialize_to_string", SerializeMessage<pd::ProgramDesc>)
67
      .def("parse_from_string",
68 69
           [](pd::ProgramDesc &program_desc, const std::string &data) {
             pd::proto::ProgramDesc *desc = program_desc.Proto();
70 71 72 73
             PADDLE_ENFORCE_EQ(
                 desc->ParseFromString(data), true,
                 platform::errors::InvalidArgument(
                     "Failed to parse ProgramDesc from binary string."));
X
version  
Xin Pan 已提交
74
           })
75 76 77 78 79 80 81
      .def("_set_version",
           [](pd::ProgramDesc &self, int64_t version) {
             return self.SetVersion(version);
           },
           pybind11::arg("version") = pd::kCurProgramVersion)
      .def("_version",
           [](pd::ProgramDesc &self) -> int64_t { return self.Version(); });
82 83
}

84 85 86 87 88
void BindBlockDesc(pybind11::module *m) {
  pybind11::class_<pd::BlockDesc>(*m, "BlockDesc", "")
      .def_property_readonly("id", &pd::BlockDesc::ID)
      .def_property_readonly("parent", &pd::BlockDesc::Parent)
      .def("get_forward_block_idx", &pd::BlockDesc::ForwardBlockID)
W
Wu Yi 已提交
89
      .def("_set_forward_block_idx", &pd::BlockDesc::SetForwardBlockID)
90 91
      .def("append_op", &pd::BlockDesc::AppendOp,
           pybind11::return_value_policy::reference)
W
Wu Yi 已提交
92
      .def("_prepend_op", &pd::BlockDesc::PrependOp,
93
           pybind11::return_value_policy::reference)
W
Wu Yi 已提交
94
      .def("_insert_op", &pd::BlockDesc::InsertOp,
95
           pybind11::return_value_policy::reference)
W
Wu Yi 已提交
96
      .def("_remove_op", &pd::BlockDesc::RemoveOp)
D
dongzhihong 已提交
97
      .def("var",
98
           [](pd::BlockDesc &self, pybind11::bytes byte_name) {
F
fengjiayi 已提交
99
             std::string name = byte_name;
D
dongzhihong 已提交
100
             return self.Var(name);
F
fengjiayi 已提交
101
           },
102
           pybind11::return_value_policy::reference)
Q
Qiao Longfei 已提交
103
      .def("has_var",
104
           [](pd::BlockDesc &self, pybind11::bytes byte_name) {
Q
Qiao Longfei 已提交
105 106
             std::string name = byte_name;
             return self.HasVar(name);
T
wip  
typhoonzero 已提交
107
           },
108
           pybind11::return_value_policy::reference)
W
Wu Yi 已提交
109
      .def("_rename_var",
110 111
           [](pd::BlockDesc &self, const pybind11::bytes &byte_name,
              const pybind11::bytes &byte_name_new) {
T
typhoonzero 已提交
112 113
             std::string name = byte_name;
             std::string new_name = byte_name_new;
T
wip  
typhoonzero 已提交
114
             self.RenameVar(name, new_name);
Q
Qiao Longfei 已提交
115
           })
F
fengjiayi 已提交
116
      .def("has_var_recursive",
117
           [](pd::BlockDesc &self, pybind11::bytes byte_name) {
F
fengjiayi 已提交
118 119 120
             std::string name = byte_name;
             return self.HasVarRecursive(name);
           })
D
Dong Zhihong 已提交
121
      .def("find_var",
122
           [](pd::BlockDesc &self, pybind11::bytes byte_name) {
F
fengjiayi 已提交
123
             std::string name = byte_name;
D
Dong Zhihong 已提交
124
             return self.FindVar(name);
F
fengjiayi 已提交
125
           },
126
           pybind11::return_value_policy::reference)
F
fengjiayi 已提交
127
      .def("find_var_recursive",
128
           [](pd::BlockDesc &self, pybind11::bytes byte_name) {
F
fengjiayi 已提交
129 130 131
             std::string name = byte_name;
             return self.FindVarRecursive(name);
           },
132
           pybind11::return_value_policy::reference)
W
Wu Yi 已提交
133
      .def("_remove_var",
134
           [](pd::BlockDesc &self, pybind11::bytes byte_name) {
L
Luo Tao 已提交
135 136 137
             std::string name = byte_name;
             return self.RemoveVar(name);
           },
138 139 140 141 142 143
           pybind11::return_value_policy::reference)
      .def("all_vars", &pd::BlockDesc::AllVars,
           pybind11::return_value_policy::reference)
      .def("op_size", &pd::BlockDesc::OpSize)
      .def("op", &pd::BlockDesc::Op, pybind11::return_value_policy::reference)
      .def("serialize_to_string", SerializeMessage<pd::BlockDesc>);
144 145
}

146 147
void BindVarDsec(pybind11::module *m) {
  pybind11::class_<pd::VarDesc> var_desc(*m, "VarDesc", "");
W
WangZhen 已提交
148
  var_desc.def(pybind11::init<const std::string &>())
M
minqiyang 已提交
149
      .def("name", &pd::VarDesc::Name, pybind11::return_value_policy::reference)
150 151 152
      .def("set_name", &pd::VarDesc::SetName)
      .def("set_shape", &pd::VarDesc::SetShape)
      .def("set_shapes", &pd::VarDesc::SetShapes)
H
hong 已提交
153
      .def("get_shape", &pd::VarDesc::GetShape)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      .def("set_dtype", &pd::VarDesc::SetDataType)
      .def("set_dtypes", &pd::VarDesc::SetDataTypes)
      .def("shape", &pd::VarDesc::GetShape,
           pybind11::return_value_policy::reference)
      .def("shapes", &pd::VarDesc::GetShapes,
           pybind11::return_value_policy::reference)
      .def("dtype", &pd::VarDesc::GetDataType,
           pybind11::return_value_policy::reference)
      .def("dtypes", &pd::VarDesc::GetDataTypes,
           pybind11::return_value_policy::reference)
      .def("lod_level", &pd::VarDesc::GetLoDLevel)
      .def("lod_levels", &pd::VarDesc::GetLoDLevels,
           pybind11::return_value_policy::reference)
      .def("set_lod_level", &pd::VarDesc::SetLoDLevel)
      .def("set_lod_levels", &pd::VarDesc::SetLoDLevels)
      .def("type", &pd::VarDesc::GetType)
      .def("set_type", &pd::VarDesc::SetType)
      .def("serialize_to_string", SerializeMessage<pd::VarDesc>)
      .def("persistable", &pd::VarDesc::Persistable)
H
Huihuang Zheng 已提交
173 174 175
      .def("set_persistable", &pd::VarDesc::SetPersistable)
      .def("need_check_feed", &pd::VarDesc::NeedCheckFeed)
      .def("set_need_check_feed", &pd::VarDesc::SetNeedCheckFeed);
Y
Yu Yang 已提交
176

177 178
  pybind11::enum_<pd::proto::VarType::Type>(var_desc, "VarType", "")
      .value("BOOL", pd::proto::VarType::BOOL)
179
      .value("UINT8", pd::proto::VarType::UINT8)
Q
qingqing01 已提交
180
      .value("INT8", pd::proto::VarType::INT8)
181 182 183 184 185 186
      .value("INT16", pd::proto::VarType::INT16)
      .value("INT32", pd::proto::VarType::INT32)
      .value("INT64", pd::proto::VarType::INT64)
      .value("FP16", pd::proto::VarType::FP16)
      .value("FP32", pd::proto::VarType::FP32)
      .value("FP64", pd::proto::VarType::FP64)
187
      .value("BF16", pd::proto::VarType::BF16)
188 189
      .value("COMPLEX64", pd::proto::VarType::COMPLEX64)
      .value("COMPLEX128", pd::proto::VarType::COMPLEX128)
190 191 192 193 194 195 196 197 198 199
      .value("LOD_TENSOR", pd::proto::VarType::LOD_TENSOR)
      .value("SELECTED_ROWS", pd::proto::VarType::SELECTED_ROWS)
      .value("FEED_MINIBATCH", pd::proto::VarType::FEED_MINIBATCH)
      .value("FETCH_LIST", pd::proto::VarType::FETCH_LIST)
      .value("STEP_SCOPES", pd::proto::VarType::STEP_SCOPES)
      .value("LOD_RANK_TABLE", pd::proto::VarType::LOD_RANK_TABLE)
      .value("LOD_TENSOR_ARRAY", pd::proto::VarType::LOD_TENSOR_ARRAY)
      .value("PLACE_LIST", pd::proto::VarType::PLACE_LIST)
      .value("READER", pd::proto::VarType::READER)
      .value("RAW", pd::proto::VarType::RAW);
200 201
}

202 203 204 205
void BindOpDesc(pybind11::module *m) {
  pybind11::enum_<pd::proto::AttrType>(*m, "AttrType", "")
      .value("INT", pd::proto::AttrType::INT)
      .value("INTS", pd::proto::AttrType::INTS)
T
tangwei12 已提交
206 207
      .value("LONG", pd::proto::AttrType::LONG)
      .value("LONGS", pd::proto::AttrType::LONGS)
208 209 210 211 212 213
      .value("FLOAT", pd::proto::AttrType::FLOAT)
      .value("FLOATS", pd::proto::AttrType::FLOATS)
      .value("STRING", pd::proto::AttrType::STRING)
      .value("STRINGS", pd::proto::AttrType::STRINGS)
      .value("BOOL", pd::proto::AttrType::BOOLEAN)
      .value("BOOLS", pd::proto::AttrType::BOOLEANS)
Y
Yancey1989 已提交
214 215
      .value("BLOCK", pd::proto::AttrType::BLOCK)
      .value("BLOCKS", pd::proto::AttrType::BLOCKS);
Y
Yu Yang 已提交
216

217
  pybind11::class_<pd::OpDesc> op_desc(*m, "OpDesc", "");
F
fengjiayi 已提交
218
  op_desc
219 220 221 222 223 224 225 226 227
      .def("__init__", [](pd::OpDesc &self) { new (&self) pd::OpDesc(); },
           pybind11::return_value_policy::reference)
      .def("copy_from", &pd::OpDesc::CopyFrom)
      .def("type", &pd::OpDesc::Type)
      .def("set_type", &pd::OpDesc::SetType)
      .def("input", &pd::OpDesc::Input)
      .def("input_names", &pd::OpDesc::InputNames)
      .def("output", &pd::OpDesc::Output)
      .def("output_names", &pd::OpDesc::OutputNames)
H
hong 已提交
228 229 230 231 232 233 234 235 236 237
      .def("set_input",
           [](pd::OpDesc &self, const std::string &name,
              const std::vector<std::string> &vec_var_name) {
             self.SetInput(name, vec_var_name);
           })
      .def("set_output",
           [](pd::OpDesc &self, const std::string &name,
              const std::vector<std::string> &vec_var_name) {
             self.SetOutput(name, vec_var_name);
           })
238
      .def("remove_output", &pd::OpDesc::RemoveOutput)
239 240
      .def("input_arg_names", &pd::OpDesc::InputArgumentNames)
      .def("output_arg_names", &pd::OpDesc::OutputArgumentNames)
W
Wu Yi 已提交
241 242
      .def("_rename_input", &pd::OpDesc::RenameInput)
      .def("_rename_output", &pd::OpDesc::RenameOutput)
243 244 245
      .def("has_attr", &pd::OpDesc::HasAttr)
      .def("attr_type", &pd::OpDesc::GetAttrType)
      .def("attr_names", &pd::OpDesc::AttrNames)
W
Wu Yi 已提交
246
      .def("_set_attr", &pd::OpDesc::SetAttr)
247
      .def("remove_attr", &pd::OpDesc::RemoveAttr)
248 249
      .def("attr", &pd::OpDesc::GetAttr)
      .def("set_block_attr", &pd::OpDesc::SetBlockAttr)
250
      .def("set_blocks_attr", &pd::OpDesc::SetBlocksAttr)
T
typhoonzero 已提交
251
      .def("set_serialized_attr",
252 253
           [](pd::OpDesc &self, const std::string &name,
              const pybind11::bytes &seriralized) {
T
typhoonzero 已提交
254 255 256
             std::string ser(seriralized);
             self.SetAttr(name, ser);
           })
W
Wu Yi 已提交
257 258
      .def("_block_attr_id", &pd::OpDesc::GetBlockAttrId)
      .def("_blocks_attr_ids", &pd::OpDesc::GetBlocksAttrIds)
259 260 261
      .def("check_attrs", &pd::OpDesc::CheckAttrs)
      .def("infer_shape", &pd::OpDesc::InferShape)
      .def("infer_var_type", &pd::OpDesc::InferVarType)
262
      .def("set_is_target", &pd::OpDesc::SetIsTarget)
263
      .def("serialize_to_string", SerializeMessage<pd::OpDesc>)
S
sneaxiy 已提交
264
      .def("block", [](pd::OpDesc &self) { return self.Block(); },
265 266 267
           pybind11::return_value_policy::reference)
      .def("inputs", &pd::OpDesc::Inputs)
      .def("outputs", &pd::OpDesc::Outputs);
268
}
Y
Yu Yang 已提交
269

270
}  // namespace pybind
271
}  // namespace paddle