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

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/pybind/protobuf.h"
Y
Yu Yang 已提交
16
#include <deque>
Y
Yu Yang 已提交
17
#include <iostream>
F
fengjiayi 已提交
18 19 20 21
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
#include "paddle/framework/program_desc.h"
#include "paddle/framework/var_desc.h"
22

Y
Yu Yang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Cast boost::variant for PyBind.
// Copy from
// https://github.com/pybind/pybind11/issues/576#issuecomment-269563199
namespace pybind11 {
namespace detail {

// Can be replaced by a generic lambda in C++14
struct variant_caster_visitor : public boost::static_visitor<handle> {
  return_value_policy policy;
  handle parent;

  variant_caster_visitor(return_value_policy policy, handle parent)
      : policy(policy), parent(parent) {}

  template <class T>
  handle operator()(T const &src) const {
    return make_caster<T>::cast(src, policy, parent);
  }
};

template <class Variant>
struct variant_caster;

template <template <class...> class V, class... Ts>
struct variant_caster<V<Ts...>> {
  using Type = V<Ts...>;

Y
Yu Yang 已提交
50 51
  template <typename T>
  typename std::enable_if<
Y
Yu Yang 已提交
52
      !std::is_same<T, boost::detail::variant::void_>::value, bool>::type
Y
Yu Yang 已提交
53
  try_load(handle src, bool convert) {
Y
Yu Yang 已提交
54 55 56 57 58 59 60 61 62
    auto caster = make_caster<T>();
    if (!load_success_ && caster.load(src, convert)) {
      load_success_ = true;
      value = cast_op<T>(caster);
      return true;
    }
    return false;
  }

Y
Yu Yang 已提交
63 64 65 66 67 68 69
  template <typename T>
  typename std::enable_if<std::is_same<T, boost::detail::variant::void_>::value,
                          bool>::type
  try_load(handle src, bool convert) {
    return false;
  }

Y
Yu Yang 已提交
70 71 72 73 74 75
  bool load(handle src, bool convert) {
    auto unused = {false, try_load<Ts>(src, convert)...};
    (void)(unused);
    return load_success_;
  }

Y
Yu Yang 已提交
76
  static handle cast(Type const &src, return_value_policy policy,
Y
Yu Yang 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                     handle parent) {
    variant_caster_visitor visitor(policy, parent);
    return boost::apply_visitor(visitor, src);
  }

  PYBIND11_TYPE_CASTER(Type, _("Variant"));
  bool load_success_{false};
};

// Add specialization for concrete variant type
template <class... Args>
struct type_caster<boost::variant<Args...>>
    : variant_caster<boost::variant<Args...>> {};

}  // namespace detail
}  // namespace pybind11

94
namespace paddle {
95
namespace pybind {
96

Y
Yu Yang 已提交
97
// Bind Methods
F
fengjiayi 已提交
98
void BindProgramDesc(py::module &m) {
F
fengjiayi 已提交
99 100 101 102 103 104 105
  py::class_<framework::ProgramDescBind>(m, "ProgramDesc", "")
      .def_static(
          "instance",
          []() -> framework::ProgramDescBind * {
            return &framework::ProgramDescBind::Instance(&GetProgramDesc());
          },
          py::return_value_policy::reference)
106
      .def_static("__create_program_desc__",
F
fengjiayi 已提交
107
                  []() -> framework::ProgramDescBind * {
108 109 110 111 112
                    // Only used for unit-test
                    auto *prog_desc = new ProgramDesc;
                    auto *block = prog_desc->mutable_blocks()->Add();
                    block->set_idx(0);
                    block->set_parent_idx(-1);
F
fengjiayi 已提交
113
                    return &framework::ProgramDescBind::Instance(prog_desc);
Y
Yu Yang 已提交
114 115
                  },
                  py::return_value_policy::reference)
F
fengjiayi 已提交
116
      .def("append_block", &framework::ProgramDescBind::AppendBlock,
117
           py::return_value_policy::reference)
F
fengjiayi 已提交
118 119 120 121
      .def("block", &framework::ProgramDescBind::Block,
           py::return_value_policy::reference)
      .def("__str__", &framework::ProgramDescBind::DebugString)
      .def("num_blocks", &framework::ProgramDescBind::Size);
122 123
}

F
fengjiayi 已提交
124
void BindBlockDesc(py::module &m) {
F
fengjiayi 已提交
125 126 127 128 129 130
  py::class_<framework::BlockDescBind>(m, "BlockDesc", "")
      .def_property_readonly("id", &framework::BlockDescBind::ID)
      .def_property_readonly("parent", &framework::BlockDescBind::Parent)
      .def("append_op", &framework::BlockDescBind::AppendOp,
           py::return_value_policy::reference)
      .def("prepend_op", &framework::BlockDescBind::PrependOp,
131
           py::return_value_policy::reference)
F
fengjiayi 已提交
132 133 134 135 136
      .def("new_var",
           [](framework::BlockDescBind &self, py::bytes byte_name) {
             std::string name = byte_name;
             return self.NewVar(name);
           },
Y
Yu Yang 已提交
137
           py::return_value_policy::reference)
F
fengjiayi 已提交
138 139 140 141 142
      .def("var",
           [](framework::BlockDescBind &self, py::bytes byte_name) {
             std::string name = byte_name;
             return self.Var(name);
           },
F
fengjiayi 已提交
143
           py::return_value_policy::reference)
F
fengjiayi 已提交
144
      .def("all_vars", &framework::BlockDescBind::AllVars,
F
fengjiayi 已提交
145
           py::return_value_policy::reference)
F
fengjiayi 已提交
146
      .def("all_ops", &framework::BlockDescBind::AllOps,
147 148 149
           py::return_value_policy::reference);
}

F
fengjiayi 已提交
150
void BindVarDsec(py::module &m) {
F
Fix bug  
fengjiayi 已提交
151 152 153 154 155 156 157 158 159
  py::enum_<framework::DataType>(m, "DataType", "")
      .value("BOOL", DataType::BOOL)
      .value("INT16", DataType::INT16)
      .value("INT32", DataType::INT32)
      .value("INT64", DataType::INT64)
      .value("FP16", DataType::FP16)
      .value("FP32", DataType::FP32)
      .value("FP64", DataType::FP64);

F
fengjiayi 已提交
160 161 162 163 164 165 166 167 168 169 170 171
  py::class_<framework::VarDescBind>(m, "VarDesc", "")
      .def("name",
           [](const framework::framework::VarDescBind &self) {
             py::bytes name = self.Name();
             return name;
           },
           py::return_value_policy::reference)
      .def("set_shape", &framework::VarDescBind::SetShape)
      .def("set_data_type", &framework::VarDescBind::SetDataType)
      .def("shape", &framework::VarDescBind::Shape,
           py::return_value_policy::reference)
      .def("data_type", &framework::VarDescBind::DataType);
172 173
}

F
fengjiayi 已提交
174
void BindOpDesc(py::module &m) {
Y
Yu Yang 已提交
175 176 177 178 179 180 181 182 183 184 185
  py::enum_<framework::AttrType>(m, "AttrType", "")
      .value("INT", AttrType::INT)
      .value("INTS", AttrType::INTS)
      .value("FLOAT", AttrType::FLOAT)
      .value("FLOATS", AttrType::FLOATS)
      .value("STRING", AttrType::STRING)
      .value("STRINGS", AttrType::STRINGS)
      .value("BOOL", AttrType::BOOLEAN)
      .value("BOOLS", AttrType::BOOLEANS)
      .value("BLOCK", AttrType::BLOCK);

F
fengjiayi 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  py::class_<framework::OpDescBind> op_desc(m, "OpDesc", "");
  op_desc.def("type", &framework::OpDescBind::Type)
      .def("set_type", &framework::OpDescBind::SetType)
      .def("input", &framework::OpDescBind::Input)
      .def("input_names", &framework::OpDescBind::InputNames)
      .def("set_input", &framework::OpDescBind::SetInput)
      .def("output", &framework::OpDescBind::Output)
      .def("output_names", &framework::OpDescBind::OutputNames)
      .def("set_output", &framework::OpDescBind::SetOutput)
      .def("__str__", &framework::OpDescBind::DebugString)
      .def("__repr__", &framework::OpDescBind::DebugString)
      .def("has_attr", &framework::OpDescBind::HasAttr)
      .def("attr_type", &framework::OpDescBind::GetAttrType)
      .def("attr_names", &framework::OpDescBind::AttrNames)
      .def("set_attr", &framework::OpDescBind::SetAttr)
      .def("attr", &framework::OpDescBind::GetAttr)
      .def("set_block_attr", &framework::OpDescBind::SetBlockAttr)
      .def("get_block_attr", &framework::OpDescBind::GetBlockAttr);
204
}
Y
Yu Yang 已提交
205

206
}  // namespace pybind
207
}  // namespace paddle