From 690ab8be4cad024e5c5372908c5f6d458829ec0c Mon Sep 17 00:00:00 2001 From: Jiaying Zhao Date: Mon, 26 Aug 2019 15:12:13 +0000 Subject: [PATCH] Catch mobile exceptions test=develop (#1867) * Catch mobile exceptions test=develop * code style format test=develop --- mobile/src/common/enforce.h | 2 - mobile/src/common/types.h | 3 +- mobile/src/framework/executor.cpp | 65 ++++++++++++++++++------------- mobile/src/framework/executor.h | 2 + mobile/src/io/paddle_mobile.cpp | 11 +++++- mobile/src/io/paddle_mobile.h | 1 + 6 files changed, 54 insertions(+), 30 deletions(-) diff --git a/mobile/src/common/enforce.h b/mobile/src/common/enforce.h index 1bacfb88d3..9cabee989b 100644 --- a/mobile/src/common/enforce.h +++ b/mobile/src/common/enforce.h @@ -43,7 +43,6 @@ struct PaddleMobileException : public std::exception { { \ char buffer[1000]; \ snprintf(buffer, sizeof(buffer), __VA_ARGS__); \ - std::string detail(buffer); \ throw paddle_mobile::PaddleMobileException("Custom Exception", buffer, \ __FILE__, __LINE__); \ } \ @@ -55,7 +54,6 @@ struct PaddleMobileException : public std::exception { } else { \ char buffer[1000]; \ snprintf(buffer, sizeof(buffer), __VA_ARGS__); \ - std::string detail(buffer); \ throw paddle_mobile::PaddleMobileException("paddle-mobile enforce", \ buffer, __FILE__, __LINE__); \ } \ diff --git a/mobile/src/common/types.h b/mobile/src/common/types.h index 744855e0fd..97db75e38a 100644 --- a/mobile/src/common/types.h +++ b/mobile/src/common/types.h @@ -83,7 +83,8 @@ enum PMStatus { PMOutOfAuthority = 0x05, /*!< Try to modified data not your own*/ PMOutOfMem = 0x06, /*!< OOM error*/ PMUnImplError = 0x07, /*!< Unimplement error. */ - PMWrongDevice = 0x08 /*!< un-correct device. */ + PMWrongDevice = 0x08, /*!< un-correct device. */ + PMException = 0x09 /*!< throw exception. */ }; enum RoundType { diff --git a/mobile/src/framework/executor.cpp b/mobile/src/framework/executor.cpp index d817b0c1e9..1bf9ba7e4b 100644 --- a/mobile/src/framework/executor.cpp +++ b/mobile/src/framework/executor.cpp @@ -480,44 +480,52 @@ const CLImage *Executor::GetOutputImage( template PMStatus Executor::Predict() { + try { #if _OPENMP - omp_set_num_threads(CPUContext::Context()->get_thread_num()); + omp_set_num_threads(CPUContext::Context()->get_thread_num()); #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()); + // 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 profile(ops_of_block0_.size()); - struct timespec ts; - int op_index = 0; + std::vector profile(ops_of_block0_.size()); + struct timespec ts; + int op_index = 0; #endif - for (int i = 0; i < ops_of_block0_.size(); ++i) { - auto &op_handler = ops_of_block0_[i]; + for (int i = 0; i < ops_of_block0_.size(); ++i) { + auto &op_handler = ops_of_block0_[i]; #ifdef PADDLE_MOBILE_PROFILE - clock_gettime(CLOCK_MONOTONIC, &ts); - profile[op_index].runBegin = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec; + clock_gettime(CLOCK_MONOTONIC, &ts); + profile[op_index].runBegin = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec; #endif - DLOG << i << "th, " - << "run op: " << op_handler->Type(); - if (lod_mode_ && input_dim_has_changed_) { - op_handler->InferShape(); - } - op_handler->Run(); + DLOG << i << "th, " + << "run op: " << op_handler->Type(); + if (lod_mode_ && input_dim_has_changed_) { + op_handler->InferShape(); + } + op_handler->Run(); #ifdef PADDLE_MOBILE_PROFILE - clock_gettime(CLOCK_MONOTONIC, &ts); - profile[op_index].runEnd = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec; - ++op_index; + clock_gettime(CLOCK_MONOTONIC, &ts); + profile[op_index].runEnd = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec; + ++op_index; #endif - } - if (feed_indices_.size() == 1) { - input_dim_has_changed_ = false; - } + } + if (feed_indices_.size() == 1) { + input_dim_has_changed_ = false; + } #ifdef PADDLE_MOBILE_PROFILE - PrintProfile(profile); + PrintProfile(profile); #endif - return PMSuccess; + return PMSuccess; + } catch (PaddleMobileException &e) { + exception_msg_ = e.what(); + return PMException; + } catch (std::exception &e) { + exception_msg_ = e.what(); + return PMException; + } } #ifdef PADDLE_MOBILE_PROFILE @@ -588,6 +596,11 @@ void Executor::GetTensorResults( } } +template +std::string Executor::GetExceptionMsg() { + return exception_msg_; +} + #ifdef PADDLE_MOBILE_FPGA template void Executor::InjectVariable(const Tensor &t, diff --git a/mobile/src/framework/executor.h b/mobile/src/framework/executor.h index d7dcb17620..4f108c993c 100644 --- a/mobile/src/framework/executor.h +++ b/mobile/src/framework/executor.h @@ -60,6 +60,7 @@ class Executor { void FeedTensorData(const std::vector &v); void GetTensorResults(std::vector *v); + std::string GetExceptionMsg(); #ifdef PADDLE_MOBILE_FPGA void InjectVariable(const Tensor &t, std::string var_name); @@ -100,6 +101,7 @@ class Executor { std::vector>> ops_of_block0_; std::unordered_map feed_indices_; std::unordered_map fetch_indices_; + std::string exception_msg_; // for super resoltion DDim input_dim_last_; diff --git a/mobile/src/io/paddle_mobile.cpp b/mobile/src/io/paddle_mobile.cpp index c08d801327..74cac25672 100644 --- a/mobile/src/io/paddle_mobile.cpp +++ b/mobile/src/io/paddle_mobile.cpp @@ -20,7 +20,7 @@ limitations under the License. */ #endif // _OPENMP #ifdef PADDLE_MOBILE_CL #include -#include +#include // NOLINT #include "framework/cl/cl_engine.h" #include "framework/cl/cl_tensor.h" #endif @@ -64,6 +64,7 @@ PMStatus PaddleMobile::Load(const std::string &model_path, loader_ = std::make_shared>(); } else { LOG(kLOG_INFO) << "loader inited"; + LOG(kLOG_INFO) << "loader inited"; } if (executor_.get() == nullptr) { @@ -187,6 +188,14 @@ void PaddleMobile::Clear() { template double PaddleMobile::GetPredictTime() {} +template +std::string PaddleMobile::GetExceptionMsg() { + if (executor_.get() != nullptr) { + return executor_->GetExceptionMsg(); + } + return ""; +} + #ifdef PADDLE_MOBILE_CPU template <> double PaddleMobile::GetPredictTime() { diff --git a/mobile/src/io/paddle_mobile.h b/mobile/src/io/paddle_mobile.h index a1b040df2c..e39d712447 100644 --- a/mobile/src/io/paddle_mobile.h +++ b/mobile/src/io/paddle_mobile.h @@ -90,6 +90,7 @@ class PaddleMobile { PowerMode power_mode = PERFORMANCE_PRIORITY); void Clear(); double GetPredictTime(); + std::string GetExceptionMsg(); #ifdef PADDLE_MOBILE_FPGA void InjectVariable(const framework::Tensor &t, std::string var_name); -- GitLab