提交 9d499955 编写于 作者: H hjchen2

update

上级 f79ad065
......@@ -170,20 +170,19 @@ void Executor<Device, T>::InitMemory() {
for (const auto &block : program_desc_->Blocks()) {
for (const auto &var_desc : block->Vars()) {
auto var = program_.scope->Var(var_desc->Name());
auto tensor = var->template GetMutable<LoDTensor>();
if (var_desc->Persistable()) {
if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") {
var->template GetMutable<framework::LoDTensorArray>();
continue;
}
char *origin_data =
ReadFileToBuff(program_.model_path + "/" + var_desc->Name());
char *data = origin_data;
auto tensor = var->template GetMutable<LoDTensor>();
LoadMemory(reinterpret_cast<void **>(&data), var_desc, tensor);
delete[] origin_data;
} else {
if (var_desc->Type() == VARTYPE_TYPE_LOD_TENSOR) {
varInputMemory(var_desc, var, tensor);
}
varInputMemory(var_desc, var);
}
}
}
......@@ -205,23 +204,18 @@ void Executor<Device, T>::InitCombineMemory() {
for (const auto &block : program_desc_->Blocks()) {
for (const auto &var_desc : block->Vars()) {
auto var = program_.scope->Var(var_desc->Name());
auto tensor = var->template GetMutable<LoDTensor>();
if (var_desc->Persistable()) {
if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") {
var->template GetMutable<framework::LoDTensorArray>();
continue;
}
DLOG << " init combine memory persistable: " << var_desc->Name();
auto tensor = var->template GetMutable<LoDTensor>();
LoadMemory(reinterpret_cast<void **>(&data), var_desc, tensor);
} else {
if (var_desc->Type() == VARTYPE_TYPE_LOD_TENSOR) {
DLOG << " init combine memory no persistable in lod: "
<< var_desc->Name();
varInputMemory(var_desc, var, tensor);
} else {
DLOG << " init combine memory no persistable: " << var_desc->Name();
}
DLOG << " init combine memory no persistable: " << var_desc->Name();
varInputMemory(var_desc, var);
}
}
}
......@@ -239,6 +233,7 @@ void Executor<Device, T>::InitNoPersistableMemory(const Tensor &input_tensor) {
auto tensor = var->template GetMutable<LoDTensor>();
if (var_desc->Persistable()) {
if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") {
var->template GetMutable<framework::LoDTensorArray>();
continue;
}
} else {
......@@ -249,6 +244,9 @@ void Executor<Device, T>::InitNoPersistableMemory(const Tensor &input_tensor) {
input_tensor.dims()[3]});
tensor->Resize(new_dim);
tensor->template mutable_data<T>();
} else {
PADDLE_MOBILE_THROW_EXCEPTION("Unsupported var type `%d`",
var_desc->Type());
}
}
}
......@@ -261,34 +259,43 @@ void Executor<Device, T>::InitNoPersistableMemory(const Tensor &input_tensor) {
template <typename Device, typename T>
bool Executor<Device, T>::varInputMemory(
const std::shared_ptr<VarDesc> &var_desc, Variable *var,
LoDTensor *tensor) const {
const std::shared_ptr<VarDesc> &var_desc, Variable *var) const {
#ifdef PADDLE_MOBILE_FPGA
tensor->init(typeid(float));
return true;
#endif
auto type = var_desc->Tensor_desc().DataType();
switch (type) {
case VARTYPE_TYPE_FP32:
tensor->mutable_data<float>();
break;
case VARTYPE_TYPE_INT8:
tensor->mutable_data<int8_t>();
break;
case VARTYPE_TYPE_INT32:
tensor->mutable_data<int32_t>();
break;
case VARTYPE_TYPE_INT64:
tensor->mutable_data<int64_t>();
break;
default:
break;
auto TypeId = [](const VarType_Type &type) -> std::type_index {
switch (type) {
case VARTYPE_TYPE_BOOL:
return typeid(bool);
case VARTYPE_TYPE_FP32:
return typeid(float);
case VARTYPE_TYPE_INT8:
return typeid(int8_t);
case VARTYPE_TYPE_INT32:
return typeid(int);
case VARTYPE_TYPE_INT64:
return typeid(int64_t);
default:
PADDLE_MOBILE_THROW_EXCEPTION("got unhandled var type `%d`", type);
}
};
auto type = var_desc->Type();
if (type == VARTYPE_TYPE_LOD_TENSOR) {
auto data_type = var_desc->Tensor_desc().DataType();
framework::LoDTensor *tensor = var->template GetMutable<LoDTensor>();
tensor->mutable_data(TypeId(data_type));
} else if (type == VARTYPE_TYPE_STEP_SCOPES) {
std::vector<framework::Scope *> *step_scopes =
var->template GetMutable<std::vector<framework::Scope *>>();
} else if (type == VARTYPE_TYPE_STEP_LOD_TENSOR_ARRAY) {
framework::LoDTensorArray *tensor_array =
var->template GetMutable<framework::LoDTensorArray>();
} else {
PADDLE_MOBILE_THROW_EXCEPTION("got unhandled var type `%d`", type);
}
bool is_mute_match =
(type == VARTYPE_TYPE_FP32) || (type == VARTYPE_TYPE_INT8) ||
(type == VARTYPE_TYPE_INT32) || (type == VARTYPE_TYPE_INT64);
PADDLE_MOBILE_ENFORCE(is_mute_match, "got unhandled data type : %d", type);
return is_mute_match;
return true;
}
template <typename Device, typename T>
......
......@@ -61,8 +61,8 @@ class Executor {
protected:
Executor() = default;
bool varInputMemory(const std::shared_ptr<VarDesc> &var_desc, Variable *var,
LoDTensor *tensor) const;
bool varInputMemory(const std::shared_ptr<VarDesc> &var_desc,
Variable *var) const;
void InitMemory();
void InitCombineMemory();
void InitNoPersistableMemory(const Tensor &input_tensor);
......
......@@ -324,3 +324,6 @@ LOAD_OP1(psroi_pool, CPU);
#ifdef ROI_PERSPECTIVE_OP
LOAD_OP1(roi_perspective_transform, CPU);
#endif
#ifdef BEAM_SEARCH_DECODE_OP
LOAD_OP1(beam_search_decode, CPU);
#endif
......@@ -64,9 +64,10 @@ void OperatorBase<Dtype>::Run() {
for (const auto key : input_keys) {
auto var_vec_in = inputs_.at(key);
for (int i = 0; i < var_vec_in.size(); ++i) {
auto vari = this->scope_->FindVar(var_vec_in[i]);
if (vari->IsInitialized()) {
const Tensor *tensor = vari->template Get<framework::LoDTensor>();
auto var = this->scope_->FindVar(var_vec_in[i]);
if (var->IsInitialized() &&
var->template IsType<framework::LoDTensor>()) {
const Tensor *tensor = var->template Get<framework::LoDTensor>();
if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor;
}
}
......@@ -74,9 +75,10 @@ void OperatorBase<Dtype>::Run() {
for (const auto key : GetOutKeys()) {
auto var_vec_out = outputs_.at(key);
for (int i = 0; i < var_vec_out.size(); ++i) {
auto vari = scope_->FindVar(var_vec_out[i]);
if (vari->IsInitialized()) {
const Tensor *tensor = vari->template Get<framework::LoDTensor>();
auto var = scope_->FindVar(var_vec_out[i]);
if (var->IsInitialized() &&
var->template IsType<framework::LoDTensor>()) {
const Tensor *tensor = var->template Get<framework::LoDTensor>();
if (tensor) DLOG << type_ << " output- " << key << "=" << *tensor;
}
}
......
......@@ -78,9 +78,8 @@ void ProgramDesc::Description(std::string header) {
}
for (const auto &var_desc : block->Vars()) {
LOG(kLOG_DEBUG1) << "var name: " << var_desc->Name();
if (var_desc->Type() == VARTYPE_TYPE_LOD_TENSOR) {
LOG(kLOG_DEBUG1) << "var name: " << var_desc->Name();
const TensorDesc &tensor_desc = var_desc->Tensor_desc();
LOG(kLOG_DEBUG2) << "in var tensor desc dims size: "
......
......@@ -152,14 +152,14 @@ PMStatus PaddleMobile<Device, T>::Predict() {
}
template <typename Device, typename T>
void PaddleMobile<Device, T>::Feed(const framework::Tensor &input,
const std::string &var_name) {
void PaddleMobile<Device, T>::Feed(const std::string &var_name,
const framework::Tensor &input) {
executor_->SetInput(input, var_name);
}
template <typename Device, typename T>
void PaddleMobile<Device, T>::Feed(const framework::LoDTensor &input,
const std::string &var_name) {
void PaddleMobile<Device, T>::Feed(const std::string &var_name,
const framework::LoDTensor &input) {
executor_->SetInput(input, var_name);
}
......
......@@ -33,7 +33,7 @@ namespace paddle_mobile {
template <typename Device, typename T = float>
class PaddleMobile {
public:
PaddleMobile(PaddleMobileConfigInternal config) : config_(config) {
explicit PaddleMobile(PaddleMobileConfigInternal config) : config_(config) {
#ifndef PADDLE_MOBILE_CL
bool is_gpu = std::is_same<DeviceType<kGPU_CL>, Device>::value;
PADDLE_MOBILE_ENFORCE(!is_gpu, "Please recompile with GPU_CL is on");
......@@ -69,8 +69,8 @@ class PaddleMobile {
const std::vector<int64_t> &dims);
PMStatus Predict();
void Feed(const framework::LoDTensor &input, const std::string &var_name);
void Feed(const framework::Tensor &input, const std::string &var_name);
void Feed(const std::string &var_name, const framework::LoDTensor &input);
void Feed(const std::string &var_name, const framework::Tensor &input);
typedef std::shared_ptr<framework::LoDTensor> LoDTensorPtr;
LoDTensorPtr Fetch(const std::string &var_name);
......
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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. */
#ifdef BEAM_SEARCH_DECODE_OP
#pragma once
#include "operators/beam_search_decode_op.h"
namespace paddle_mobile {
namespace operators {
template <typename Dtype, typename T>
void BeamSearchDecodeOp<Dtype, T>::InferShape() const {}
} // namespace operators
} // namespace paddle_mobile
namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(beam_search_decode, ops::BeamSearchDecodeOp);
#endif
namespace ops = paddle_mobile::operators;
#endif // BEAM_SEARCH_DECODE_OP
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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. */
#ifdef BEAM_SEARCH_DECODE_OP
#pragma once
#include <string>
#include "framework/operator.h"
#include "operators/kernel/beam_search_decode_kernel.h"
namespace paddle_mobile {
namespace operators {
DECLARE_OPERATOR(BeamSearchDecode, BeamSearchDecodeParam,
BeamSearchDecodeKernel);
} // namespace operators
} // namespace paddle_mobile
#endif // BEAM_SEARCH_DECODE_OP
......@@ -21,7 +21,8 @@ template <typename DeviceType, typename T>
void FeedOp<DeviceType, T>::InferShape() const {
auto out_dims = this->param_.Out()->dims();
out_dims[0] = this->param_.BatchSize();
auto input_dims = this->param_.InputX()->dims();
int col = this->param_.Col();
auto input_dims = this->param_.InputX()->at(col).dims();
if (input_dims.size() == 4) {
this->param_.Out()->Resize(input_dims);
} else {
......
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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. */
#ifdef BEAM_SEARCH_DECODE_OP
#include "operators/kernel/beam_search_decode_kernel.h"
namespace paddle_mobile {
namespace operators {
template <>
bool BeamSearchDecodeKernel<CPU, float>::Init(
BeamSearchDecodeParam<CPU> *param) {
return true;
}
template <>
void BeamSearchDecodeKernel<CPU, float>::Compute(
const BeamSearchDecodeParam<CPU> &param) {
// TODO(hjchen2)
}
} // namespace operators
} // namespace paddle_mobile
#endif
......@@ -24,8 +24,9 @@ bool FeedKernel<CPU, float>::Init(FeedParam<CPU> *param) {
template <>
void FeedKernel<CPU, float>::Compute(const FeedParam<CPU> &param) {
param.Out()->ShareDataWith(*(param.InputX()));
param.Out()->set_lod(param.InputX()->lod());
int col = param.Col();
param.Out()->ShareDataWith(param.InputX()->at(col));
param.Out()->set_lod(param.InputX()->at(col).lod());
}
template class FeedKernel<CPU, float>;
......
......@@ -28,8 +28,9 @@ void WriteToArrayKernel<CPU, float>::Compute(
const WriteToArrayParam<CPU> &param) {
int64_t offset = param.index_->data<int64_t>()[0];
if (offset >= param.output_->size()) {
param.output_->resize(offset);
param.output_->resize(offset + 1);
}
framework::LoDTensor *out_tensor = &(param.output_->at(offset));
out_tensor->set_lod(param.input_->lod());
if (param.input_->memory_size() > 0) {
......
......@@ -12,12 +12,46 @@ 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. */
#ifdef WHILE_OP
#include "operators/kernel/while_kernel.h"
#include "framework/op_registry.h"
#include "framework/operator.h"
namespace paddle_mobile {
namespace operators {
#ifdef WHILE_OP
class StepExecutor {
typedef std::shared_ptr<framework::OperatorBase<CPU>> OperatorPtr;
public:
StepExecutor(const framework::BlockDesc *block, framework::Scope *scope)
: scope_(std::shared_ptr<framework::Scope>(scope)) {
std::vector<std::shared_ptr<framework::OpDesc>> ops = block->Ops();
ops_of_block_.resize(ops.size());
for (int i = 0; i < ops.size(); ++i) {
std::shared_ptr<framework::OpDesc> op_desc = ops[i];
auto op_handler = framework::OpRegistry<CPU>::CreateOp(
op_desc->Type(), op_desc->GetInputs(), op_desc->GetOutputs(),
op_desc->GetAttrMap(), scope_);
ops_of_block_[i] = op_handler;
}
}
void Run() {
for (auto &op_handler : ops_of_block_) {
DLOG << "run op: " << op_handler->Type();
op_handler->InferShape();
op_handler->Run();
DLOG << "run op finish";
}
}
private:
std::shared_ptr<framework::Scope> scope_;
std::vector<OperatorPtr> ops_of_block_;
};
template <>
bool WhileKernel<CPU, float>::Init(WhileParam<CPU> *param) {
return true;
......@@ -26,8 +60,15 @@ bool WhileKernel<CPU, float>::Init(WhileParam<CPU> *param) {
template <>
void WhileKernel<CPU, float>::Compute(const WhileParam<CPU> &param) {
// TODO(hjchen2)
auto &current_scope = param.scope_->NewScope();
StepExecutor executor(param.sub_block_, &current_scope);
while (param.cond_->data<bool>()[0]) {
executor.Run();
}
param.scope_->DeleteScope(&current_scope);
}
#endif // WHILE_OP
} // namespace operators
} // namespace paddle_mobile
#endif // WHILE_OP
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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. */
#ifdef BEAM_SEARCH_DECODE_OP
#pragma once
#include "framework/operator.h"
#include "operators/op_param.h"
namespace paddle_mobile {
namespace operators {
template <typename Dtype>
class BeamSearchDecodeParam : public OpParam {
public:
BeamSearchDecodeParam(const VariableNameMap &inputs,
const VariableNameMap &outputs,
const AttributeMap &attrs, const Scope &scope) {
ids_ =
OpParam::GetVarValue<framework::LoDTensorArray>("Ids", inputs, scope);
scores_ = OpParam::GetVarValue<framework::LoDTensorArray>("Scores", inputs,
scope);
sentence_ids_ = OpParam::GetVarValue<framework::LoDTensor>("SentenceIds",
outputs, scope);
sentence_scores_ = OpParam::GetVarValue<framework::LoDTensor>(
"SentenceScores", outputs, scope);
beam_size_ = OpParam::GetAttr<int>("beam_size", attrs);
end_id_ = OpParam::GetAttr<int>("end_id", attrs);
}
public:
framework::LoDTensorArray *ids_;
framework::LoDTensorArray *scores_;
framework::LoDTensor *sentence_ids_;
framework::LoDTensor *sentence_scores_;
int beam_size_;
int end_id_;
};
DECLARE_KERNEL(BeamSearchDecode, BeamSearchDecodeParam);
} // namespace operators
} // namespace paddle_mobile
#endif // BEAM_SEARCH_DECODE_OP
......@@ -26,18 +26,16 @@ class WhileParam : public OpParam {
public:
WhileParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
const AttributeMap &attrs, const Scope &scope)
: inputs_(inputs), outputs_(outputs), scope_(scope) {
: scope_(&scope) {
cond_ =
OpParam::GetVarValue<framework::LoDTensor>("Condition", inputs, scope);
sub_block_ = OpParam::GetAttr<framework::BlockDesc *>("sub_block", attrs);
}
public:
const Scope *scope_;
framework::LoDTensor *cond_;
const framework::BlockDesc *sub_block_;
const VariableNameMap inputs_;
const VariableNameMap outputs_;
const Scope scope_;
framework::BlockDesc *sub_block_;
};
DECLARE_KERNEL(While, WhileParam);
......
......@@ -1172,18 +1172,21 @@ class FeedParam : public OpParam {
public:
FeedParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
const AttributeMap &attrs, const Scope &scope) {
input_x_ = InputXFrom<LoDTensor>(inputs, scope);
input_x_ = InputXFrom<framework::LoDTensorArray>(inputs, scope);
out_ = OutFrom<GType>(outputs, scope);
col_ = GetAttr<int>("col", attrs);
auto var = scope.FindVar("batch_size");
batch_size = var->GetValue<int>();
}
const LoDTensor *InputX() const { return input_x_; }
const framework::LoDTensorArray *InputX() const { return input_x_; }
GType *Out() const { return out_; }
const int Col() const { return col_; }
const int BatchSize() const { return batch_size; }
private:
LoDTensor *input_x_;
framework::LoDTensorArray *input_x_;
GType *out_;
int col_;
int batch_size;
};
......@@ -3008,24 +3011,6 @@ class LogicalUnaryParam : public OpParam {
};
#endif // LOGICAL_NOT_OP
// #ifdef WHILE_OP
// template <typename Dtype>
// class WhileParam : public OpParam {
// public:
// WhileParam(const VariableNameMap &inputs,
// const VariableNameMap &outputs, const AttributeMap &attrs,
// const Scope &scope) {
// cond_ = OpParam::GetVarValue<framework::LoDTensor>("Condition", inputs,
// scope); block_desc_ = OpParam::GetAttr<framework::BlockDesc
// *>("sub_block", attrs);
// }
//
// public:
// framework::LoDTensor *cond_;
// const framework::BlockDesc *block_desc_;
// };
// #endif // WHILE_OP
#ifdef WRITE_TO_ARRAY_OP
template <typename Dtype>
class WriteToArrayParam : public OpParam {
......@@ -3099,17 +3084,17 @@ class IncrementParam : public OpParam {
const AttributeMap &attrs, const Scope &scope) {
input_x_ = InputXFrom<GType>(inputs, scope);
output_ = OutFrom<GType>(outputs, scope);
step_ = OpParam::GetAttr<int>("step", attrs);
step_ = OpParam::GetAttr<float>("step", attrs);
}
const GType *InputX() const { return input_x_; }
GType *Out() const { return output_; }
int Step() const { return step_; }
float Step() const { return step_; }
public:
GType *input_x_;
GType *output_;
int step_;
float step_;
};
#endif // INCREMENT_OP
......
......@@ -294,6 +294,7 @@ if(NOT FOUND_MATCH)
set(PROPOSAL_OP ON)
set(PSROI_POOL_OP ON)
set(ROI_PERSPECTIVE_OP ON)
set(BEAM_SEARCH_DECODE_OP ON)
endif()
# option(BATCHNORM_OP "" ON)
......@@ -597,3 +598,6 @@ endif()
if (ROI_PERSPECTIVE_OP)
add_definitions(-DROI_PERSPECTIVE_OP)
endif()
if (BEAM_SEARCH_DECODE_OP)
add_definitions(-DBEAM_SEARCH_DECODE_OP)
endif()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册