protobuf.cc 17.2 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>
Y
Yu Yang 已提交
18
#include "paddle/framework/attribute.h"
19

Y
Yu Yang 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// 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 已提交
47 48 49 50 51
  template <typename T>
  typename std::enable_if<
      !std::is_same<T, boost::detail::variant::void_>::value,
      bool>::type
  try_load(handle src, bool convert) {
Y
Yu Yang 已提交
52 53 54 55 56 57 58 59 60
    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 已提交
61 62 63 64 65 66 67
  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 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  bool load(handle src, bool convert) {
    auto unused = {false, try_load<Ts>(src, convert)...};
    (void)(unused);
    return load_success_;
  }

  static handle cast(Type const &src,
                     return_value_policy policy,
                     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

93
namespace paddle {
94
namespace pybind {
95

Y
Yu Yang 已提交
96 97
using namespace paddle::framework;  // NOLINT

Y
Yu Yang 已提交
98
// convert between std::vector and protobuf repeated.
Y
Yu Yang 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
template <typename T>
inline std::vector<T> RepeatedToVector(
    const google::protobuf::RepeatedField<T> &repeated_field) {
  std::vector<T> ret;
  ret.reserve(repeated_field.size());
  std::copy(
      repeated_field.begin(), repeated_field.end(), std::back_inserter(ret));
  return ret;
}

template <typename T, typename RepeatedField>
inline void VectorToRepeated(const std::vector<T> &vec,
                             RepeatedField *repeated_field) {
  repeated_field->Reserve(vec.size());
F
fengjiayi 已提交
113
  for (const auto &elem : vec) {
Y
Yu Yang 已提交
114 115 116 117
    *repeated_field->Add() = elem;
  }
}

Y
Yu Yang 已提交
118
// Specialize vector<bool>.
Y
Yu Yang 已提交
119 120 121 122 123 124 125 126 127
template <typename RepeatedField>
inline void VectorToRepeated(const std::vector<bool> &vec,
                             RepeatedField *repeated_field) {
  repeated_field->Reserve(vec.size());
  for (auto elem : vec) {
    *repeated_field->Add() = elem;
  }
}

Y
Yu Yang 已提交
128 129 130
class ProgramDescBind;
class OpDescBind;
class BlockDescBind;
Y
Fix bug  
Yu Yang 已提交
131
class VarDescBind;
Y
Yu Yang 已提交
132

Y
Yu Yang 已提交
133 134 135
// Each Protobuf Message, we provide a XXXBind class. In that class, we optimize
// read/write speed. Only when we want the protobuf message, the local changes
// will be synchronized (by `Sync` method).
Y
Fix bug  
Yu Yang 已提交
136
class VarDescBind {
Y
Yu Yang 已提交
137
public:
F
fengjiayi 已提交
138
  explicit VarDescBind(const std::string &name) { desc_.set_name(name); }
Y
Fix bug  
Yu Yang 已提交
139

F
fengjiayi 已提交
140 141
  VarDesc *Proto() { return &desc_; }

Y
Yu Yang 已提交
142
  py::bytes Name() const { return desc_.name(); }
F
fengjiayi 已提交
143

F
fengjiayi 已提交
144
  void SetShape(const std::vector<int64_t> &dims) {
F
fengjiayi 已提交
145 146 147
    VectorToRepeated(dims, desc_.mutable_lod_tensor()->mutable_dims());
  }

F
Fix bug  
fengjiayi 已提交
148 149
  void SetDataType(framework::DataType data_type) {
    desc_.mutable_lod_tensor()->set_data_type(data_type);
F
fengjiayi 已提交
150 151
  }

Y
Yu Yang 已提交
152
  std::vector<int64_t> Shape() const {
F
fengjiayi 已提交
153 154
    return RepeatedToVector(desc_.lod_tensor().dims());
  }
Y
Fix bug  
Yu Yang 已提交
155

Y
Yu Yang 已提交
156 157 158
  framework::DataType DataType() const {
    return desc_.lod_tensor().data_type();
  }
F
fengjiayi 已提交
159

Y
Fix bug  
Yu Yang 已提交
160
private:
F
fengjiayi 已提交
161
  VarDesc desc_;
Y
Fix bug  
Yu Yang 已提交
162
};
Y
Yu Yang 已提交
163

Y
Fix bug  
Yu Yang 已提交
164 165
class OpDescBind {
public:
Y
Yu Yang 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  OpDesc *Proto() {
    Sync();
    return &op_desc_;
  }

  std::string Type() const { return op_desc_.type(); }

  void SetType(const std::string &type) { op_desc_.set_type(type); }

  const std::vector<std::string> &Input(const std::string &name) const {
    auto it = inputs_.find(name);
    PADDLE_ENFORCE(
        it != inputs_.end(), "Input %s cannot be found in Op %s", name, Type());
    return it->second;
  }

  std::vector<std::string> InputNames() const {
    std::vector<std::string> retv;
    retv.reserve(this->inputs_.size());
    for (auto &ipt : this->inputs_) {
      retv.push_back(ipt.first);
    }
    return retv;
  }

  void SetInput(const std::string &param_name,
                const std::vector<std::string> &args) {
    need_update_ = true;
    inputs_[param_name] = args;
  }

  const std::vector<std::string> &Output(const std::string &name) const {
    auto it = outputs_.find(name);
    PADDLE_ENFORCE(it != outputs_.end(),
                   "Output %s cannot be found in Op %s",
                   name,
                   Type());
    return it->second;
  }

  std::vector<std::string> OutputNames() const {
    std::vector<std::string> retv;
    retv.reserve(this->outputs_.size());
    for (auto &ipt : this->outputs_) {
      retv.push_back(ipt.first);
    }
    return retv;
  }

  void SetOutput(const std::string &param_name,
                 const std::vector<std::string> &args) {
    need_update_ = true;
    this->outputs_[param_name] = args;
  }

  std::string DebugString() { return this->Proto()->DebugString(); }

Y
Yu Yang 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  bool HasAttr(const std::string &name) const {
    return attrs_.find(name) != attrs_.end();
  }

  framework::AttrType GetAttrType(const std::string &name) const {
    auto it = attrs_.find(name);
    PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name);
    return static_cast<framework::AttrType>(it->second.which() - 1);
  }

  std::vector<std::string> AttrNames() const {
    std::vector<std::string> retv;
    retv.reserve(attrs_.size());
    for (auto &attr : attrs_) {
      retv.push_back(attr.first);
    }
    return retv;
  }

  void SetAttr(const std::string &name, const Attribute &v) {
    this->attrs_[name] = v;
    need_update_ = true;
  }

  void SetBlockAttr(const std::string &name, BlockDescBind &block);

  Attribute GetAttr(const std::string &name) const {
    auto it = attrs_.find(name);
    PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name);
    return it->second;
  }

  int GetBlockAttr(const std::string &name) const {
    auto it = attrs_.find(name);
    PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name);
    return boost::get<BlockDesc *>(it->second)->idx();
  }

private:
Y
Stash  
Yu Yang 已提交
262 263
  struct SetAttrDescVisitor : public boost::static_visitor<void> {
    explicit SetAttrDescVisitor(OpDesc::Attr *attr) : attr_(attr) {}
Y
Yu Yang 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    mutable OpDesc::Attr *attr_;
    void operator()(int v) const { attr_->set_i(v); }
    void operator()(float v) const { attr_->set_f(v); }
    void operator()(const std::string &v) const { attr_->set_s(v); }
    void operator()(bool b) const { attr_->set_b(b); }

    void operator()(const std::vector<int> &v) const {
      VectorToRepeated(v, attr_->mutable_ints());
    }
    void operator()(const std::vector<float> &v) const {
      VectorToRepeated(v, attr_->mutable_floats());
    }
    void operator()(const std::vector<std::string> &v) const {
      VectorToRepeated(v, attr_->mutable_strings());
    }
    void operator()(const std::vector<bool> &v) const {
      VectorToRepeated(v, attr_->mutable_bools());
    }
Y
Yu Yang 已提交
282 283 284 285
    void operator()(BlockDesc *desc) const {
      attr_->set_block_idx(desc->idx());
    }
    void operator()(boost::blank) const { PADDLE_THROW("Unexpected branch"); }
Y
Stash  
Yu Yang 已提交
286 287
  };

Y
Yu Yang 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  void Sync() {
    if (need_update_) {
      this->op_desc_.mutable_inputs()->Clear();
      for (auto &ipt : inputs_) {
        auto *input = op_desc_.add_inputs();
        input->set_parameter(ipt.first);
        VectorToRepeated(ipt.second, input->mutable_arguments());
      }

      this->op_desc_.mutable_outputs()->Clear();
      for (auto &opt : outputs_) {
        auto *output = op_desc_.add_outputs();
        output->set_parameter(opt.first);
        VectorToRepeated(opt.second, output->mutable_arguments());
      }

Y
Stash  
Yu Yang 已提交
304 305 306 307
      this->op_desc_.mutable_attrs()->Clear();
      for (auto &attr : attrs_) {
        auto *attr_desc = op_desc_.add_attrs();
        attr_desc->set_name(attr.first);
Y
Yu Yang 已提交
308 309 310
        attr_desc->set_type(
            static_cast<framework::AttrType>(attr.second.which() - 1));
        boost::apply_visitor(SetAttrDescVisitor(attr_desc), attr.second);
Y
Stash  
Yu Yang 已提交
311 312
      }

Y
Yu Yang 已提交
313 314 315
      need_update_ = false;
    }
  }
Y
Yu Yang 已提交
316 317

  OpDesc op_desc_;
Y
Yu Yang 已提交
318 319 320 321
  std::unordered_map<std::string, std::vector<std::string>> inputs_;
  std::unordered_map<std::string, std::vector<std::string>> outputs_;
  std::unordered_map<std::string, Attribute> attrs_;

Y
Yu Yang 已提交
322 323
  // need_update_ indicate there some local changes not be synchronized. If
  // local changes should be synchronized, need_update_ should be set to true.
Y
Yu Yang 已提交
324
  bool need_update_{false};
Y
Yu Yang 已提交
325 326 327 328 329 330 331
};

class BlockDescBind {
public:
  BlockDescBind(ProgramDescBind *prog, BlockDesc *desc)
      : prog_(prog), desc_(desc), need_update_(false) {}

Y
Fix bug  
Yu Yang 已提交
332 333 334
  BlockDescBind(const BlockDescBind &o) = delete;
  BlockDescBind &operator=(const BlockDescBind &o) = delete;

Y
Yu Yang 已提交
335
  int32_t ID() const { return desc_->idx(); }
Y
Yu Yang 已提交
336 337 338

  int32_t Parent() const { return desc_->parent_idx(); }

F
fengjiayi 已提交
339 340
  VarDescBind *NewVar(py::bytes name_bytes) {
    std::string name = name_bytes;
Y
Fix bug  
Yu Yang 已提交
341 342 343 344 345 346 347 348
    need_update_ = true;
    auto it = vars_.find(name);
    PADDLE_ENFORCE(it == vars_.end(), "Duplicated variable %s", name);
    auto var = new VarDescBind(name);
    vars_[name].reset(var);
    return var;
  }

F
fengjiayi 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  VarDescBind *Var(py::bytes name_bytes) const {
    std::string name = name_bytes;
    auto it = vars_.find(name);
    PADDLE_ENFORCE(
        it != vars_.end(), "Can not find variable %s in current block.", name);
    return it->second.get();
  }

  std::vector<VarDescBind *> AllVars() const {
    std::vector<VarDescBind *> res;
    for (const auto &p : vars_) {
      res.push_back(p.second.get());
    }
    return res;
  }

Y
Fix bug  
Yu Yang 已提交
365 366
  BlockDescBind *ParentBlock() const;

Y
Yu Yang 已提交
367 368
  OpDescBind *AppendOp() {
    need_update_ = true;
Y
Fix bug  
Yu Yang 已提交
369 370
    ops_.emplace_back(new OpDescBind());
    return ops_.back().get();
Y
Yu Yang 已提交
371 372
  }

F
fengjiayi 已提交
373 374 375 376 377 378
  OpDescBind *PrependOp() {
    need_update_ = true;
    ops_.emplace_front(new OpDescBind());
    return ops_.front().get();
  }

F
fengjiayi 已提交
379 380 381 382 383 384 385 386
  std::vector<OpDescBind *> AllOps() const {
    std::vector<OpDescBind *> res;
    for (const auto &op : ops_) {
      res.push_back(op.get());
    }
    return res;
  }

Y
Yu Yang 已提交
387 388 389 390 391 392
  void Sync() {
    if (need_update_) {
      auto &op_field = *this->desc_->mutable_ops();
      op_field.Clear();
      op_field.Reserve(static_cast<int>(ops_.size()));
      for (auto &op_desc : ops_) {
Y
Fix bug  
Yu Yang 已提交
393
        op_field.AddAllocated(op_desc->Proto());
Y
Yu Yang 已提交
394
      }
Y
Fix bug  
Yu Yang 已提交
395
      need_update_ = false;
Y
Yu Yang 已提交
396 397 398
    }
  }

Y
Yu Yang 已提交
399 400
  BlockDesc *RawPtr() { return desc_; }

Y
Yu Yang 已提交
401 402 403 404 405
private:
  ProgramDescBind *prog_;  // not_own
  BlockDesc *desc_;        // not_own
  bool need_update_;

Y
Fix bug  
Yu Yang 已提交
406 407
  std::deque<std::unique_ptr<OpDescBind>> ops_;
  std::unordered_map<std::string, std::unique_ptr<VarDescBind>> vars_;
Y
Yu Yang 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
};

using ProgDescMap =
    std::unordered_map<ProgramDesc *, std::unique_ptr<ProgramDescBind>>;
static ProgDescMap *g_bind_map = nullptr;

class ProgramDescBind {
public:
  static ProgramDescBind &Instance(ProgramDesc *prog) {
    if (g_bind_map == nullptr) {
      g_bind_map = new ProgDescMap();
    }
    auto &map = *g_bind_map;
    auto &ptr = map[prog];

    if (ptr == nullptr) {
      ptr.reset(new ProgramDescBind(prog));
    }
    return *ptr;
  }
Y
Fix bug  
Yu Yang 已提交
428 429
  ProgramDescBind(const ProgramDescBind &o) = delete;
  ProgramDescBind &operator=(const ProgramDescBind &o) = delete;
Y
Yu Yang 已提交
430

Y
Yu Yang 已提交
431
  BlockDescBind *AppendBlock(const BlockDescBind &parent) {
Y
Yu Yang 已提交
432
    auto *b = prog_->add_blocks();
Y
Yu Yang 已提交
433
    b->set_parent_idx(parent.ID());
Y
Yu Yang 已提交
434
    b->set_idx(prog_->blocks_size() - 1);
Y
Fix bug  
Yu Yang 已提交
435 436
    blocks_.emplace_back(new BlockDescBind(this, b));
    return blocks_.back().get();
Y
Yu Yang 已提交
437 438
  }

Y
Fix bug  
Yu Yang 已提交
439
  BlockDescBind *Block(size_t idx) { return blocks_[idx].get(); }
Y
Yu Yang 已提交
440 441 442 443 444 445 446

  std::string DebugString() { return Proto()->DebugString(); }

  size_t Size() const { return blocks_.size(); }

  ProgramDesc *Proto() {
    for (auto &block : blocks_) {
Y
Fix bug  
Yu Yang 已提交
447
      block->Sync();
Y
Yu Yang 已提交
448 449 450 451 452 453 454
    }
    return prog_;
  }

private:
  explicit ProgramDescBind(ProgramDesc *prog) : prog_(prog) {
    for (auto &block : *prog->mutable_blocks()) {
Y
Fix bug  
Yu Yang 已提交
455
      blocks_.emplace_back(new BlockDescBind(this, &block));
Y
Yu Yang 已提交
456 457 458 459 460 461
    }
  }

  // Not owned
  ProgramDesc *prog_;

Y
Fix bug  
Yu Yang 已提交
462
  std::vector<std::unique_ptr<BlockDescBind>> blocks_;
Y
Yu Yang 已提交
463 464
};

Y
Fix bug  
Yu Yang 已提交
465 466 467 468 469 470 471
BlockDescBind *BlockDescBind::ParentBlock() const {
  if (this->desc_->parent_idx() == -1) {
    return nullptr;
  }
  return prog_->Block(static_cast<size_t>(this->desc_->parent_idx()));
}

Y
Yu Yang 已提交
472 473 474 475 476
void OpDescBind::SetBlockAttr(const std::string &name, BlockDescBind &block) {
  BlockDesc *desc = block.RawPtr();
  this->attrs_[name] = desc;
}

Y
Yu Yang 已提交
477
// Bind Methods
F
fengjiayi 已提交
478
void BindProgramDesc(py::module &m) {
Y
Yu Yang 已提交
479
  py::class_<ProgramDescBind>(m, "ProgramDesc", "")
480
      .def_static("instance",
Y
Yu Yang 已提交
481 482 483
                  []() -> ProgramDescBind * {
                    return &ProgramDescBind::Instance(&GetProgramDesc());
                  },
484 485
                  py::return_value_policy::reference)
      .def_static("__create_program_desc__",
Y
Yu Yang 已提交
486
                  []() -> ProgramDescBind * {
487 488 489 490 491
                    // 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);
Y
Yu Yang 已提交
492 493 494
                    return &ProgramDescBind::Instance(prog_desc);
                  },
                  py::return_value_policy::reference)
495
      .def("append_block",
Y
Yu Yang 已提交
496
           &ProgramDescBind::AppendBlock,
497
           py::return_value_policy::reference)
Y
Yu Yang 已提交
498 499 500
      .def("block", &ProgramDescBind::Block, py::return_value_policy::reference)
      .def("__str__", &ProgramDescBind::DebugString)
      .def("num_blocks", &ProgramDescBind::Size);
501 502
}

F
fengjiayi 已提交
503
void BindBlockDesc(py::module &m) {
Y
Yu Yang 已提交
504
  py::class_<BlockDescBind>(m, "BlockDesc", "")
Y
Yu Yang 已提交
505
      .def_property_readonly("id", &BlockDescBind::ID)
Y
Yu Yang 已提交
506
      .def_property_readonly("parent", &BlockDescBind::Parent)
507
      .def("append_op",
Y
Yu Yang 已提交
508
           &BlockDescBind::AppendOp,
509
           py::return_value_policy::reference)
F
fengjiayi 已提交
510 511 512
      .def("prepend_op",
           &BlockDescBind::PrependOp,
           py::return_value_policy::reference)
F
fengjiayi 已提交
513 514 515 516 517 518 519 520
      .def(
          "new_var", &BlockDescBind::NewVar, py::return_value_policy::reference)
      .def("var", &BlockDescBind::Var, py::return_value_policy::reference)
      .def("all_vars",
           &BlockDescBind::AllVars,
           py::return_value_policy::reference)
      .def("all_ops",
           &BlockDescBind::AllOps,
521 522 523
           py::return_value_policy::reference);
}

F
fengjiayi 已提交
524
void BindVarDsec(py::module &m) {
F
Fix bug  
fengjiayi 已提交
525 526 527 528 529 530 531 532 533
  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 已提交
534
  py::class_<VarDescBind>(m, "VarDesc", "")
F
fengjiayi 已提交
535
      .def("name", &VarDescBind::Name, py::return_value_policy::reference)
F
fengjiayi 已提交
536 537
      .def("set_shape", &VarDescBind::SetShape)
      .def("set_data_type", &VarDescBind::SetDataType)
F
fengjiayi 已提交
538
      .def("shape", &VarDescBind::Shape, py::return_value_policy::reference)
F
Fix bug  
fengjiayi 已提交
539
      .def("data_type", &VarDescBind::DataType);
540 541
}

F
fengjiayi 已提交
542
void BindOpDesc(py::module &m) {
Y
Yu Yang 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555
  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);

  py::class_<OpDescBind> op_desc(m, "OpDesc", "");
  op_desc.def("type", &OpDescBind::Type)
Y
Yu Yang 已提交
556 557 558 559 560 561 562 563
      .def("set_type", &OpDescBind::SetType)
      .def("input", &OpDescBind::Input)
      .def("input_names", &OpDescBind::InputNames)
      .def("set_input", &OpDescBind::SetInput)
      .def("output", &OpDescBind::Output)
      .def("output_names", &OpDescBind::OutputNames)
      .def("set_output", &OpDescBind::SetOutput)
      .def("__str__", &OpDescBind::DebugString)
Y
Yu Yang 已提交
564 565 566 567 568 569 570 571
      .def("__repr__", &OpDescBind::DebugString)
      .def("has_attr", &OpDescBind::HasAttr)
      .def("attr_type", &OpDescBind::GetAttrType)
      .def("attr_names", &OpDescBind::AttrNames)
      .def("set_attr", &OpDescBind::SetAttr)
      .def("attr", &OpDescBind::GetAttr)
      .def("set_block_attr", &OpDescBind::SetBlockAttr)
      .def("get_block_attr", &OpDescBind::GetBlockAttr);
572
}
Y
Yu Yang 已提交
573

574
}  // namespace pybind
575
}  // namespace paddle