parent
d2edbe5709
commit
54ef4cdae5
@ -0,0 +1,90 @@
|
|||||||
|
/* 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/framework/block_desc.h"
|
||||||
|
#include "paddle/framework/op_desc.h"
|
||||||
|
#include "paddle/framework/var_desc.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
VarDescBind *BlockDescBind::NewVar(const std::string &name) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
VarDescBind *BlockDescBind::Var(const std::string &name) const {
|
||||||
|
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 *> BlockDescBind::AllVars() const {
|
||||||
|
std::vector<VarDescBind *> res;
|
||||||
|
for (const auto &p : vars_) {
|
||||||
|
res.push_back(p.second.get());
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
OpDescBind *BlockDescBind::AppendOp() {
|
||||||
|
need_update_ = true;
|
||||||
|
ops_.emplace_back(new OpDescBind());
|
||||||
|
return ops_.back().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
OpDescBind *BlockDescBind::PrependOp() {
|
||||||
|
need_update_ = true;
|
||||||
|
ops_.emplace_front(new OpDescBind());
|
||||||
|
return ops_.front().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<OpDescBind *> BlockDescBind::AllOps() const {
|
||||||
|
std::vector<OpDescBind *> res;
|
||||||
|
for (const auto &op : ops_) {
|
||||||
|
res.push_back(op.get());
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BlockDescBind::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_) {
|
||||||
|
op_field.AddAllocated(op_desc->Proto());
|
||||||
|
}
|
||||||
|
need_update_ = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockDescBind *BlockDescBind::ParentBlock() const {
|
||||||
|
if (this->desc_->parent_idx() == -1) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return prog_->Block(static_cast<size_t>(this->desc_->parent_idx()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpDescBind::SetBlockAttr(const std::string &name, BlockDescBind &block) {
|
||||||
|
BlockDesc *desc = block.RawPtr();
|
||||||
|
this->attrs_[name] = desc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include "paddle/framework/framework.pb.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
class ProgramDescBind;
|
||||||
|
class OpDescBind;
|
||||||
|
class VarDescBind;
|
||||||
|
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
class BlockDescBind {
|
||||||
|
public:
|
||||||
|
BlockDescBind(ProgramDescBind *prog, BlockDesc *desc)
|
||||||
|
: prog_(prog), desc_(desc), need_update_(false) {}
|
||||||
|
|
||||||
|
BlockDescBind(const BlockDescBind &o) = delete;
|
||||||
|
BlockDescBind &operator=(const BlockDescBind &o) = delete;
|
||||||
|
|
||||||
|
int32_t ID() const { return desc_->idx(); }
|
||||||
|
|
||||||
|
int32_t Parent() const { return desc_->parent_idx(); }
|
||||||
|
|
||||||
|
VarDescBind *NewVar(const std::string &name_bytes);
|
||||||
|
|
||||||
|
VarDescBind *Var(const std::string &name_bytes) const;
|
||||||
|
|
||||||
|
std::vector<VarDescBind *> AllVars() const;
|
||||||
|
|
||||||
|
BlockDescBind *ParentBlock() const;
|
||||||
|
|
||||||
|
OpDescBind *AppendOp();
|
||||||
|
|
||||||
|
OpDescBind *PrependOp();
|
||||||
|
|
||||||
|
std::vector<OpDescBind *> AllOps() const;
|
||||||
|
|
||||||
|
void Sync();
|
||||||
|
|
||||||
|
BlockDesc *RawPtr() { return desc_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
ProgramDescBind *prog_; // not_own
|
||||||
|
BlockDesc *desc_; // not_own
|
||||||
|
bool need_update_;
|
||||||
|
|
||||||
|
std::deque<std::unique_ptr<OpDescBind>> ops_;
|
||||||
|
std::unordered_map<std::string, std::unique_ptr<VarDescBind>> vars_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
/* 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/framework/op_desc.h"
|
||||||
|
#include "paddle/frameword/block_desc.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
OpDesc *OpDescBind::Proto() {
|
||||||
|
Sync();
|
||||||
|
return &op_desc_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<std::string> &OpDescBind::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> OpDescBind::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 OpDescBind::SetInput(const std::string ¶m_name,
|
||||||
|
const std::vector<std::string> &args) {
|
||||||
|
need_update_ = true;
|
||||||
|
inputs_[param_name] = args;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<std::string> &OpDescBind::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> OpDescBind::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 OpDescBind::SetOutput(const std::string ¶m_name,
|
||||||
|
const std::vector<std::string> &args) {
|
||||||
|
need_update_ = true;
|
||||||
|
this->outputs_[param_name] = args;
|
||||||
|
}
|
||||||
|
|
||||||
|
AttrType OpDescBind::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<AttrType>(it->second.which() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> OpDescBind::AttrNames() const {
|
||||||
|
std::vector<std::string> retv;
|
||||||
|
retv.reserve(attrs_.size());
|
||||||
|
for (auto &attr : attrs_) {
|
||||||
|
retv.push_back(attr.first);
|
||||||
|
}
|
||||||
|
return retv;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpDescBind::SetAttr(const std::string &name, const Attribute &v) {
|
||||||
|
this->attrs_[name] = v;
|
||||||
|
need_update_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Attribute OpDescBind::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 OpDescBind::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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpDescBind::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());
|
||||||
|
}
|
||||||
|
|
||||||
|
this->op_desc_.mutable_attrs()->Clear();
|
||||||
|
for (auto &attr : attrs_) {
|
||||||
|
auto *attr_desc = op_desc_.add_attrs();
|
||||||
|
attr_desc->set_name(attr.first);
|
||||||
|
attr_desc->set_type(
|
||||||
|
static_cast<framework::AttrType>(attr.second.which() - 1));
|
||||||
|
boost::apply_visitor(SetAttrDescVisitor(attr_desc), attr.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
need_update_ = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include "paddle/framework/attribute.h"
|
||||||
|
#include "paddle/framework/var_desc.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
class BlockDescBind;
|
||||||
|
|
||||||
|
class OpDescBind {
|
||||||
|
public:
|
||||||
|
OpDesc *Proto();
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
std::vector<std::string> InputNames() const;
|
||||||
|
|
||||||
|
void SetInput(const std::string ¶m_name,
|
||||||
|
const std::vector<std::string> &args);
|
||||||
|
|
||||||
|
const std::vector<std::string> &Output(const std::string &name) const;
|
||||||
|
|
||||||
|
std::vector<std::string> OutputNames() const;
|
||||||
|
|
||||||
|
void SetOutput(const std::string ¶m_name,
|
||||||
|
const std::vector<std::string> &args);
|
||||||
|
|
||||||
|
std::string DebugString() { return this->Proto()->DebugString(); }
|
||||||
|
|
||||||
|
bool HasAttr(const std::string &name) const {
|
||||||
|
return attrs_.find(name) != attrs_.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
AttrType GetAttrType(const std::string &name) const;
|
||||||
|
|
||||||
|
std::vector<std::string> AttrNames() const;
|
||||||
|
|
||||||
|
void SetAttr(const std::string &name, const Attribute &v);
|
||||||
|
|
||||||
|
void SetBlockAttr(const std::string &name, BlockDescBind &block);
|
||||||
|
|
||||||
|
Attribute GetAttr(const std::string &name) const;
|
||||||
|
|
||||||
|
int GetBlockAttr(const std::string &name) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct SetAttrDescVisitor : public boost::static_visitor<void> {
|
||||||
|
explicit SetAttrDescVisitor(OpDesc::Attr *attr) : attr_(attr) {}
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
void operator()(BlockDesc *desc) const {
|
||||||
|
attr_->set_block_idx(desc->idx());
|
||||||
|
}
|
||||||
|
void operator()(boost::blank) const { PADDLE_THROW("Unexpected branch"); }
|
||||||
|
};
|
||||||
|
|
||||||
|
void Sync();
|
||||||
|
|
||||||
|
OpDesc op_desc_;
|
||||||
|
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_;
|
||||||
|
|
||||||
|
// need_update_ indicate there some local changes not be synchronized. If
|
||||||
|
// local changes should be synchronized, need_update_ should be set to true.
|
||||||
|
bool need_update_{false};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/* 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/farmework/block_desc.h"
|
||||||
|
#include "paddle/framework/programe_desc.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
using ProgDescMap =
|
||||||
|
std::unordered_map<ProgramDesc *, std::unique_ptr<ProgramDescBind>>;
|
||||||
|
static ProgDescMap *g_bind_map = nullptr;
|
||||||
|
|
||||||
|
ProgramDescBind &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;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockDescBind *ProgramDescBind::AppendBlock(const BlockDescBind &parent) {
|
||||||
|
auto *b = prog_->add_blocks();
|
||||||
|
b->set_parent_idx(parent.ID());
|
||||||
|
b->set_idx(prog_->blocks_size() - 1);
|
||||||
|
blocks_.emplace_back(new BlockDescBind(this, b));
|
||||||
|
return blocks_.back().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramDesc *ProgramDescBind::Proto() {
|
||||||
|
for (auto &block : blocks_) {
|
||||||
|
block->Sync();
|
||||||
|
}
|
||||||
|
return prog_;
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramDescBind::ProgramDescBind(ProgramDesc *prog) {
|
||||||
|
prog_ = prog;
|
||||||
|
for (auto &block : *prog->mutable_blocks()) {
|
||||||
|
blocks_.emplace_back(new BlockDescBind(this, &block));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "paddle/framework/framework.pb.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
class BlockDescBind;
|
||||||
|
|
||||||
|
class ProgramDescBind {
|
||||||
|
public:
|
||||||
|
static ProgramDescBind &Instance(ProgramDesc *prog);
|
||||||
|
|
||||||
|
ProgramDescBind(const ProgramDescBind &o) = delete;
|
||||||
|
ProgramDescBind &operator=(const ProgramDescBind &o) = delete;
|
||||||
|
|
||||||
|
BlockDescBind *AppendBlock(const BlockDescBind &parent);
|
||||||
|
|
||||||
|
BlockDescBind *Block(size_t idx) { return blocks_[idx].get(); }
|
||||||
|
|
||||||
|
std::string DebugString() { return Proto()->DebugString(); }
|
||||||
|
|
||||||
|
size_t Size() const { return blocks_.size(); }
|
||||||
|
|
||||||
|
ProgramDesc *Proto();
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit ProgramDescBind(ProgramDesc *prog);
|
||||||
|
|
||||||
|
// Not owned
|
||||||
|
ProgramDesc *prog_;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<BlockDescBind>> blocks_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/* 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/framework/var_desc.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
void VarDescBind::SetShape(const std::vector<int64_t> &dims) {
|
||||||
|
VectorToRepeated(dims, desc_.mutable_lod_tensor()->mutable_dims());
|
||||||
|
}
|
||||||
|
|
||||||
|
void VarDescBind::SetDataType(enum DataType data_type) {
|
||||||
|
desc_.mutable_lod_tensor()->set_data_type(data_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int64_t> VarDescBind::Shape() const {
|
||||||
|
return RepeatedToVector(desc_.lod_tensor().dims());
|
||||||
|
}
|
||||||
|
|
||||||
|
DataType VarDescBind::DataType() const {
|
||||||
|
return desc_.lod_tensor().data_type();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/* 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. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "paddle/framework/framework.pb.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace framework {
|
||||||
|
|
||||||
|
// convert between std::vector and protobuf repeated.
|
||||||
|
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());
|
||||||
|
for (const auto &elem : vec) {
|
||||||
|
*repeated_field->Add() = elem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Specialize vector<bool>.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class VarDescBind {
|
||||||
|
public:
|
||||||
|
explicit VarDescBind(const std::string &name) { desc_.set_name(name); }
|
||||||
|
|
||||||
|
VarDesc *Proto() { return &desc_; }
|
||||||
|
|
||||||
|
std::string Name() const { return desc_.name(); }
|
||||||
|
|
||||||
|
void SetShape(const std::vector<int64_t> &dims);
|
||||||
|
|
||||||
|
void SetDataType(DataType data_type);
|
||||||
|
|
||||||
|
std::vector<int64_t> Shape() const;
|
||||||
|
|
||||||
|
DataType DataType() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
VarDesc desc_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue