提交 458183af 编写于 作者: Z zhangyang0701

support multiple feed for FPGA track

上级 ef972b7a
......@@ -92,6 +92,14 @@ Executor<Device, T>::Executor(const Program<Device> &program,
ops_list_.push_back(op_handler);
}
}
#ifdef PADDLE_MOBILE_FPGA
TalorFeedOp();
DLOG << "TalorFeed finished";
TalorFetchdOp();
DLOG << "TalorFetch finished";
program_.scope->print_vars();
#endif
}
template <typename T>
......@@ -443,11 +451,54 @@ std::shared_ptr<LoDTensor> Executor<Device, T>::GetOutput(
}
#ifdef PADDLE_MOBILE_FPGA
template <typename Device, typename T>
void Executor<Device, T>::TalorFeedOp() {
auto &ops = ops_of_block_[0];
int num = 0;
program_.scope->EraseVars(std::vector<string>{string("feed")});
for (auto op : ops) {
if (op->Type() == "feed") {
auto new_name = string("feed") + std::to_string(num++);
auto var = program_.scope->Var(new_name);
auto tensor = var->template GetMutable<LoDTensor>();
auto output_map = op->Outputs();
std::vector<std::string> out_keys = op->GetOutKeys();
PADDLE_MOBILE_ENFORCE(!out_keys.empty(), "this op contains no output");
auto output_tensor =
GetVarValue<LoDTensor>(out_keys[0], output_map, *(program_.scope));
tensor->Resize(output_tensor->dims());
tensor->init(typeid(float));
op->ChangeNameMap("X", std::vector<string>{new_name});
}
}
}
template <typename Device, typename T>
void Executor<Device, T>::TalorFetchdOp() {
auto &ops = ops_of_block_[0];
int num = 0;
program_.scope->EraseVars(std::vector<string>{string("fetch")});
for (auto op : ops) {
if (op->Type() == "fetch") {
auto new_name = string("fetch") + std::to_string(num++);
auto var = program_.scope->Var(new_name);
auto tensor = var->template GetMutable<LoDTensor>();
auto input_map = op->Inputs();
std::vector<std::string> in_keys = op->GetInputKeys();
PADDLE_MOBILE_ENFORCE(!in_keys.empty(), "this op contains no input");
auto input_tensor =
GetVarValue<LoDTensor>(in_keys[0], input_map, *(program_.scope));
tensor->Resize(input_tensor->dims());
tensor->init(typeid(float));
op->ChangeNameMap("Out", std::vector<string>{new_name});
}
}
}
template <typename Device, typename T>
void Executor<Device, T>::InjectVariable(const Tensor &t,
std::string var_name) {
Variable *g_feed_value = program_.scope->Var(var_name);
Tensor *feed_tensor = g_feed_value->GetMutable<LoDTensor>();
Tensor *feed_tensor = g_feed_value->template GetMutable<LoDTensor>();
feed_tensor->Resize(t.dims());
feed_tensor->ShareDataWith(t);
}
......@@ -457,6 +508,22 @@ void Executor<Device, T>::FeedData(const Tensor &t) {
InjectVariable(t, "feed");
}
template <typename Device, typename T>
void Executor<Device, T>::FeedData(const std::vector<Tensor> &v) {
auto input_size = v.size();
PADDLE_MOBILE_ENFORCE(input_size > 0, "Empty input");
int counter = 0;
auto vars = program_.scope->VarContain("feed");
for (auto var : vars) {
Tensor *feed_tensor = var->template GetMutable<LoDTensor>();
feed_tensor->Resize(v[counter].dims());
feed_tensor->ShareDataWith(v[counter]);
if (++counter > v.size()) {
return;
}
}
}
template <typename Device, typename T>
std::shared_ptr<Tensor> Executor<Device, T>::FetchResult(int id) {
auto &ops = ops_of_block_[0];
......
......@@ -50,8 +50,11 @@ class Executor {
std::shared_ptr<LoDTensor> GetOutput(const std::string &var_name);
#ifdef PADDLE_MOBILE_FPGA
void TalorFeedOp();
void TalorFetchdOp();
void InjectVariable(const Tensor &t, std::string var_name);
void FeedData(const Tensor &t);
void FeedData(const std::vector<Tensor> &v);
std::shared_ptr<Tensor> FetchResult(int id = -1);
void Predict_From_To(int start = 0, int end = -1);
void Predict_From(int start);
......
......@@ -131,6 +131,21 @@ void OperatorBase<GPU_CL>::Run() {
}
#endif
#ifdef PADDLE_MOBILE_FPGA
template <typename Dtype>
void OperatorBase<Dtype>::ChangeNameMap(string key, std::vector<string> value) {
auto it = inputs_.find(key);
if (it != inputs_.end()) {
inputs_[key] = value;
return;
}
it = outputs_.find(key);
if (it != outputs_.end()) {
inputs_[key] = value;
}
}
#endif
template class OperatorBase<CPU>;
template class OperatorBase<FPGA>;
template class OperatorBase<GPU_MALI>;
......
......@@ -78,7 +78,9 @@ class OperatorBase {
this->scope_->EraseVars(var_names);
}
}
#ifdef PADDLE_MOBILE_FPGA
void ChangeNameMap(string key, std::vector<string> value);
#endif
protected:
std::shared_ptr<Scope> scope_;
std::string type_;
......
......@@ -53,7 +53,8 @@ void ProgramDesc::Description(std::string header) {
}
}
for (auto &attr : op->GetAttrMap()) {
LOG(kLOG_DEBUG2) << "attr name:: " << attr.first;
if (attr.first == "op_callstack") continue;
LOG(kLOG_DEBUG2) << "attr name: " << attr.first;
LOG(kLOG_DEBUG3) << "argument - " << attr.second;
}
}
......
......@@ -111,5 +111,31 @@ Variable *Scope::FindVarLocally(const std::string &name) const {
return nullptr;
}
#ifdef PADDLE_MOBILE_FPGA
Variable *Scope::Var(const std::string &name, const int id) {
return Var(name + std::to_string(id));
}
std::vector<Variable *> Scope::VarContain(const std::string substring) {
std::vector<Variable *> v;
for (auto pair : vars_) {
if (pair.first.find(substring) == 0) {
v.push_back(pair.second);
}
}
return v;
}
void Scope::InsertVar(const std::string str, Variable *var) {}
void Scope::print_vars() {
DLOG << "====================start to print variables=================";
for (auto pair : vars_) {
DLOG << pair.first;
}
DLOG << "==================complete printing variables================";
}
#endif
} // namespace framework
} // namespace paddle_mobile
......@@ -83,6 +83,13 @@ class Scope {
Variable *FindVarLocally(const std::string &name) const;
#ifdef PADDLE_MOBILE_FPGA
Variable *Var(const std::string &name, const int id);
std::vector<Variable *> VarContain(const std::string substring);
void InsertVar(const std::string str, Variable *var);
void print_vars();
#endif
#ifdef PADDLE_MOBILE_CL
CLScope *GetCLScpoe() { return cl_scope_; }
#endif
......
......@@ -227,7 +227,11 @@ template <typename Device, typename T>
void PaddleMobile<Device, T>::FeedData(const framework::Tensor &t) {
executor_->FeedData(t);
}
template <typename Device, typename T>
void PaddleMobile<Device, T>::FeedData(
const std::vector<framework::Tensor> &v) {
executor_->FeedData(v);
};
template <typename Device, typename T>
std::shared_ptr<framework::Tensor> PaddleMobile<Device, T>::FetchResult(
int id) {
......
......@@ -90,6 +90,7 @@ class PaddleMobile {
#ifdef PADDLE_MOBILE_FPGA
void InjectVariable(const framework::Tensor &t, std::string var_name);
void FeedData(const framework::Tensor &t);
void FeedData(const std::vector<framework::Tensor> &v);
std::shared_ptr<framework::Tensor> FetchResult(int id = -1);
void Predict_From_To(int start = 0, int end = -1);
void Predict_From(int start);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册