未验证 提交 04c139b9 编写于 作者: H Houjiang Chen 提交者: GitHub

Clear no persistable tensor array before predicting, fix crash when predicting...

Clear no persistable tensor array before predicting, fix crash when predicting with gpu debugging mode (#1548)

* Clear no persistable tensor array before predicting, fix crash when predicting with gpu debugging mode

* Fix code style
上级 71c8718b
......@@ -36,16 +36,16 @@ static const char *ANDROID_LOG_TAG =
#define ANDROIDLOGI(...) \
__android_log_print(ANDROID_LOG_INFO, ANDROID_LOG_TAG, __VA_ARGS__); \
printf(__VA_ARGS__)
printf("%s\n", __VA_ARGS__);
#define ANDROIDLOGW(...) \
__android_log_print(ANDROID_LOG_WARNING, ANDROID_LOG_TAG, __VA_ARGS__); \
printf(__VA_ARGS__)
printf("%s\n", __VA_ARGS__);
#define ANDROIDLOGD(...) \
__android_log_print(ANDROID_LOG_DEBUG, ANDROID_LOG_TAG, __VA_ARGS__); \
printf(__VA_ARGS__)
printf("%s\n", __VA_ARGS__)
#define ANDROIDLOGE(...) \
__android_log_print(ANDROID_LOG_ERROR, ANDROID_LOG_TAG, __VA_ARGS__); \
printf(__VA_ARGS__)
printf("%s\n", __VA_ARGS__)
#else
#define ANDROIDLOGI(...)
#define ANDROIDLOGW(...)
......@@ -88,9 +88,17 @@ struct Print {
void print(LogLevel level) {
// buffer_ << std::endl;
if (level == kLOG_ERROR) {
#ifdef ANDROID
ANDROIDLOGE(buffer_.str().c_str());
#else
std::cerr << buffer_.str() << std::endl;
#endif
} else {
#ifdef ANDROID
ANDROIDLOGI(buffer_.str().c_str());
#else
std::cout << buffer_.str() << std::endl;
#endif
}
}
std::ostringstream buffer_;
......
......@@ -228,6 +228,20 @@ void Executor<Device, T>::InitMemory() {
}
}
static void ClearNoPersistableTensorArray(const framework::ProgramDesc *program,
framework::Scope *scope) {
for (const auto &block : program->Blocks()) {
for (const auto &var_desc : block->Vars()) {
if (!var_desc->Persistable() &&
var_desc->Type() == VARTYPE_TYPE_STEP_LOD_TENSOR_ARRAY) {
auto var = scope->Var(var_desc->Name());
auto array = var->template GetMutable<framework::LoDTensorArray>();
array->resize(1);
}
}
}
}
template <typename Device, typename T>
void Executor<Device, T>::InitCombineMemory() {
char *origin_data = nullptr;
......@@ -421,6 +435,10 @@ PMStatus Executor<Device, T>::Predict() {
#if _OPENMP
omp_set_num_threads(get_global_num_threads());
#endif
// clear all no persistable tensor array since write_to_array
// is always push back a new tensor in the array
ClearNoPersistableTensorArray(program_desc_.get(), program_.scope.get());
#ifdef PADDLE_MOBILE_PROFILE
std::vector<ProfInfo> profile(ops_of_block0_.size());
struct timespec ts;
......
......@@ -102,16 +102,11 @@ void OperatorBase<GPU_CL>::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 = scope_->FindVar(var_vec_in[i]);
if (vari->IsInitialized()) {
if (type_ == "feed") {
const Tensor *tensor = vari->template Get<framework::LoDTensor>();
if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor;
} else {
const CLImage *cl_image = vari->template Get<framework::CLImage>();
if (cl_image) {
DLOG << type_ << " input- " << key << "=" << *cl_image;
}
auto var = scope_->FindVar(var_vec_in[i]);
if (var->IsInitialized() && var->template IsType<framework::CLImage>()) {
const CLImage *cl_image = var->template Get<framework::CLImage>();
if (cl_image) {
DLOG << type_ << " input- " << key << "=" << *cl_image;
}
}
}
......@@ -119,18 +114,11 @@ void OperatorBase<GPU_CL>::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()) {
if (type_ == "fetch") {
const Tensor *tensor = vari->template Get<framework::LoDTensor>();
if (tensor) {
DLOG << type_ << " output- " << key << "=" << *tensor;
}
} else {
const CLImage *cl_image = vari->template Get<framework::CLImage>();
if (cl_image) {
DLOG << type_ << " output- " << key << "=" << *cl_image;
}
auto var = scope_->FindVar(var_vec_out[i]);
if (var->IsInitialized() && var->template IsType<framework::CLImage>()) {
const CLImage *cl_image = var->template Get<framework::CLImage>();
if (cl_image) {
DLOG << type_ << " output- " << key << "=" << *cl_image;
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册