|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/framework/op_desc.h" |
| 16 | +#include "paddle/framework/block_desc.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace framework { |
| 20 | + |
| 21 | +OpDesc *OpDescBind::Proto() { |
| 22 | + Sync(); |
| 23 | + return &op_desc_; |
| 24 | +} |
| 25 | + |
| 26 | +const std::vector<std::string> &OpDescBind::Input( |
| 27 | + const std::string &name) const { |
| 28 | + auto it = inputs_.find(name); |
| 29 | + PADDLE_ENFORCE(it != inputs_.end(), "Input %s cannot be found in Op %s", name, |
| 30 | + Type()); |
| 31 | + return it->second; |
| 32 | +} |
| 33 | + |
| 34 | +std::vector<std::string> OpDescBind::InputNames() const { |
| 35 | + std::vector<std::string> retv; |
| 36 | + retv.reserve(this->inputs_.size()); |
| 37 | + for (auto &ipt : this->inputs_) { |
| 38 | + retv.push_back(ipt.first); |
| 39 | + } |
| 40 | + return retv; |
| 41 | +} |
| 42 | + |
| 43 | +void OpDescBind::SetInput(const std::string ¶m_name, |
| 44 | + const std::vector<std::string> &args) { |
| 45 | + need_update_ = true; |
| 46 | + inputs_[param_name] = args; |
| 47 | +} |
| 48 | + |
| 49 | +const std::vector<std::string> &OpDescBind::Output( |
| 50 | + const std::string &name) const { |
| 51 | + auto it = outputs_.find(name); |
| 52 | + PADDLE_ENFORCE(it != outputs_.end(), "Output %s cannot be found in Op %s", |
| 53 | + name, Type()); |
| 54 | + return it->second; |
| 55 | +} |
| 56 | + |
| 57 | +std::vector<std::string> OpDescBind::OutputNames() const { |
| 58 | + std::vector<std::string> retv; |
| 59 | + retv.reserve(this->outputs_.size()); |
| 60 | + for (auto &ipt : this->outputs_) { |
| 61 | + retv.push_back(ipt.first); |
| 62 | + } |
| 63 | + return retv; |
| 64 | +} |
| 65 | + |
| 66 | +void OpDescBind::SetOutput(const std::string ¶m_name, |
| 67 | + const std::vector<std::string> &args) { |
| 68 | + need_update_ = true; |
| 69 | + this->outputs_[param_name] = args; |
| 70 | +} |
| 71 | + |
| 72 | +AttrType OpDescBind::GetAttrType(const std::string &name) const { |
| 73 | + auto it = attrs_.find(name); |
| 74 | + PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name); |
| 75 | + return static_cast<AttrType>(it->second.which() - 1); |
| 76 | +} |
| 77 | + |
| 78 | +std::vector<std::string> OpDescBind::AttrNames() const { |
| 79 | + std::vector<std::string> retv; |
| 80 | + retv.reserve(attrs_.size()); |
| 81 | + for (auto &attr : attrs_) { |
| 82 | + retv.push_back(attr.first); |
| 83 | + } |
| 84 | + return retv; |
| 85 | +} |
| 86 | + |
| 87 | +void OpDescBind::SetAttr(const std::string &name, const Attribute &v) { |
| 88 | + this->attrs_[name] = v; |
| 89 | + need_update_ = true; |
| 90 | +} |
| 91 | + |
| 92 | +Attribute OpDescBind::GetAttr(const std::string &name) const { |
| 93 | + auto it = attrs_.find(name); |
| 94 | + PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name); |
| 95 | + return it->second; |
| 96 | +} |
| 97 | + |
| 98 | +int OpDescBind::GetBlockAttr(const std::string &name) const { |
| 99 | + auto it = attrs_.find(name); |
| 100 | + PADDLE_ENFORCE(it != attrs_.end(), "Attribute %s is not found", name); |
| 101 | + return boost::get<BlockDesc *>(it->second)->idx(); |
| 102 | +} |
| 103 | + |
| 104 | +void OpDescBind::Sync() { |
| 105 | + if (need_update_) { |
| 106 | + this->op_desc_.mutable_inputs()->Clear(); |
| 107 | + for (auto &ipt : inputs_) { |
| 108 | + auto *input = op_desc_.add_inputs(); |
| 109 | + input->set_parameter(ipt.first); |
| 110 | + VectorToRepeated(ipt.second, input->mutable_arguments()); |
| 111 | + } |
| 112 | + |
| 113 | + this->op_desc_.mutable_outputs()->Clear(); |
| 114 | + for (auto &opt : outputs_) { |
| 115 | + auto *output = op_desc_.add_outputs(); |
| 116 | + output->set_parameter(opt.first); |
| 117 | + VectorToRepeated(opt.second, output->mutable_arguments()); |
| 118 | + } |
| 119 | + |
| 120 | + this->op_desc_.mutable_attrs()->Clear(); |
| 121 | + for (auto &attr : attrs_) { |
| 122 | + auto *attr_desc = op_desc_.add_attrs(); |
| 123 | + attr_desc->set_name(attr.first); |
| 124 | + attr_desc->set_type( |
| 125 | + static_cast<framework::AttrType>(attr.second.which() - 1)); |
| 126 | + boost::apply_visitor(SetAttrDescVisitor(attr_desc), attr.second); |
| 127 | + } |
| 128 | + |
| 129 | + need_update_ = false; |
| 130 | + } |
| 131 | +} |
| 132 | +} // namespace framework |
| 133 | +} // namespace paddle |
0 commit comments