From 8e1c240cdb626d3dfd3a3e5da3a08a78cadf6633 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 21 Feb 2019 09:50:42 +0900 Subject: [PATCH] Revise frontend execution (#4450) - Change return type of wrapper methods - Change order of method declarations in wrapper class - Add noexcept keyword - Add log message for fail - Exception handle on wrapper methods Signed-off-by: Hyeongseok Oh --- runtimes/neurun/src/frontend/execution.cc | 27 +++++++- runtimes/neurun/src/frontend/wrapper/execution.cc | 80 +++++++++++++++++------ runtimes/neurun/src/frontend/wrapper/execution.h | 23 +++---- 3 files changed, 97 insertions(+), 33 deletions(-) diff --git a/runtimes/neurun/src/frontend/execution.cc b/runtimes/neurun/src/frontend/execution.cc index 4b1a98c..de7dfa3 100644 --- a/runtimes/neurun/src/frontend/execution.cc +++ b/runtimes/neurun/src/frontend/execution.cc @@ -31,12 +31,14 @@ int ANeuralNetworksExecution_create(ANeuralNetworksCompilation *compilation, { if ((compilation == nullptr) || (execution == nullptr)) { + VERBOSE(NNAPI::Execution) << "create: Incorrect null pointer parameter(s)" << std::endl; return ANEURALNETWORKS_UNEXPECTED_NULL; } // Can handle compiled state only if (compilation->state() != neurun::compiler::State::COMPILED) { + VERBOSE(NNAPI::Execution) << "create: Not compiled yet" << std::endl; return ANEURALNETWORKS_BAD_STATE; } @@ -47,6 +49,7 @@ int ANeuralNetworksExecution_create(ANeuralNetworksCompilation *compilation, *execution = new (std::nothrow) ANeuralNetworksExecution{plan}; if (*execution == nullptr) { + VERBOSE(NNAPI::Execution) << "create: Fail to create execution object" << std::endl; return ANEURALNETWORKS_OUT_OF_MEMORY; } @@ -120,7 +123,11 @@ int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution *execution, int32 } } - execution->setInput(index, type, buffer, length); + if (!execution->setInput(index, type, buffer, length)) + { + VERBOSE(NNAPI::Execution) << "setInput: Fail to set input" << std::endl; + return ANEURALNETWORKS_BAD_DATA; + } return ANEURALNETWORKS_NO_ERROR; } @@ -190,7 +197,11 @@ int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution *execution, int3 } } - execution->setOutput(index, type, buffer, length); + if (!execution->setOutput(index, type, buffer, length)) + { + VERBOSE(NNAPI::Execution) << "setOutput: Fail to set output" << std::endl; + return ANEURALNETWORKS_BAD_DATA; + } return ANEURALNETWORKS_NO_ERROR; } @@ -200,6 +211,7 @@ int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, { if ((execution == nullptr) || (event == nullptr)) { + VERBOSE(NNAPI::Execution) << "startCompute: Incorrect null pointer parameter(s)" << std::endl; return ANEURALNETWORKS_UNEXPECTED_NULL; } @@ -207,10 +219,15 @@ int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution, *event = new (std::nothrow) ANeuralNetworksEvent{}; if (*event == nullptr) { + VERBOSE(NNAPI::Execution) << "startCompute: Fail to create event" << std::endl; return ANEURALNETWORKS_OUT_OF_MEMORY; } - execution->execute(); + if (!execution->execute()) + { + VERBOSE(NNAPI::Execution) << "startCompute: Fail to start execution" << std::endl; + return ANEURALNETWORKS_BAD_STATE; + } return ANEURALNETWORKS_NO_ERROR; } @@ -225,6 +242,8 @@ int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution *execut { if ((execution == nullptr) || (memory == nullptr)) { + VERBOSE(NNAPI::Execution) << "setInputFromMemory: Incorrect null pointer parameter(s)" + << std::endl; return ANEURALNETWORKS_UNEXPECTED_NULL; } @@ -241,6 +260,8 @@ int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution *execu { if ((execution == nullptr) || (memory == nullptr)) { + VERBOSE(NNAPI::Execution) << "setOutputFromMemory: Incorrect null pointer parameter(s)" + << std::endl; return ANEURALNETWORKS_UNEXPECTED_NULL; } diff --git a/runtimes/neurun/src/frontend/wrapper/execution.cc b/runtimes/neurun/src/frontend/wrapper/execution.cc index a6606e2..64d8e90 100644 --- a/runtimes/neurun/src/frontend/wrapper/execution.cc +++ b/runtimes/neurun/src/frontend/wrapper/execution.cc @@ -1,13 +1,16 @@ #include "execution.h" #include "util/NNAPIConvert.h" +#include "util/logging.h" -const neurun::model::operand::Index ANeuralNetworksExecution::getInputOperandIndex(uint32_t index) +const neurun::model::operand::Index +ANeuralNetworksExecution::getInputOperandIndex(uint32_t index) noexcept { neurun::model::operand::IO::Index input_index{index}; const auto operand_index = _executor->plan().model().getInputs().at(input_index); return operand_index; } -const neurun::model::operand::Index ANeuralNetworksExecution::getOutputOperandIndex(uint32_t index) +const neurun::model::operand::Index +ANeuralNetworksExecution::getOutputOperandIndex(uint32_t index) noexcept { neurun::model::operand::IO::Index output_index{index}; const auto operand_index = _executor->plan().model().getInputs().at(output_index); @@ -15,7 +18,7 @@ const neurun::model::operand::Index ANeuralNetworksExecution::getOutputOperandIn } bool ANeuralNetworksExecution::compareDataType(const ANeuralNetworksOperandType *type, - const neurun::model::operand::Index index) + const neurun::model::operand::Index index) noexcept { const auto operand_type = _executor->plan().model().operands().at(index).typeInfo(); const auto typeInfo = ::neurun::util::getTypeInfo(type); @@ -30,7 +33,7 @@ bool ANeuralNetworksExecution::compareDataType(const ANeuralNetworksOperandType } bool ANeuralNetworksExecution::compareShape(const ANeuralNetworksOperandType *type, - const neurun::model::operand::Index index) + const neurun::model::operand::Index index) noexcept { const auto operand_shape = _executor->plan().model().operands().at(index).shape(); if (operand_shape.rank() != type->dimensionCount) @@ -56,32 +59,71 @@ bool ANeuralNetworksExecution::compareShape(const ANeuralNetworksOperandType *ty return true; } -bool ANeuralNetworksExecution::haveUnspecifiedDims(const neurun::model::operand::Index index) +bool ANeuralNetworksExecution::haveUnspecifiedDims( + const neurun::model::operand::Index index) noexcept { const auto operand_shape = _executor->plan().model().operands().at(index).shape(); return ((operand_shape.element_nums() == 0) ? true : false); } -void ANeuralNetworksExecution::setInput(uint32_t index, const ANeuralNetworksOperandType * /*type*/, - const void *buffer, size_t length) +bool ANeuralNetworksExecution::setInput(uint32_t index, const ANeuralNetworksOperandType * /*type*/, + const void *buffer, size_t length) noexcept { - neurun::model::operand::IO::Index input_index{index}; - const auto operand_index = getInputOperandIndex(index); - const auto type_info = _executor->plan().model().operands().at(operand_index).typeInfo(); - const auto shape = _executor->plan().model().operands().at(operand_index).shape(); + try + { + neurun::model::operand::IO::Index input_index{index}; + const auto operand_index = getInputOperandIndex(index); + const auto type_info = _executor->plan().model().operands().at(operand_index).typeInfo(); + const auto shape = _executor->plan().model().operands().at(operand_index).shape(); + + _executor->setInput(input_index, type_info, shape, buffer, length); + } + catch (const std::exception &e) + { + VERBOSE(EXCEPTION) << e.what() << std::endl; + + return false; + } - _executor->setInput(input_index, type_info, shape, buffer, length); + return true; } -void ANeuralNetworksExecution::setOutput(uint32_t index, +bool ANeuralNetworksExecution::setOutput(uint32_t index, const ANeuralNetworksOperandType * /*type*/, void *buffer, - size_t length) + size_t length) noexcept { - neurun::model::operand::IO::Index output_index{index}; - const auto operand_index = getOutputOperandIndex(index); - const auto type_info = _executor->plan().model().operands().at(operand_index).typeInfo(); - const auto shape = _executor->plan().model().operands().at(operand_index).shape(); + try + { + neurun::model::operand::IO::Index output_index{index}; + const auto operand_index = getOutputOperandIndex(index); + const auto type_info = _executor->plan().model().operands().at(operand_index).typeInfo(); + const auto shape = _executor->plan().model().operands().at(operand_index).shape(); - _executor->setOutput(output_index, type_info, shape, buffer, length); + _executor->setOutput(output_index, type_info, shape, buffer, length); + } + catch (const std::exception &e) + { + VERBOSE(EXCEPTION) << e.what() << std::endl; + + return false; + } + + return true; +} + +bool ANeuralNetworksExecution::execute(void) noexcept +{ + try + { + _executor->execute(); + } + catch (const std::exception &e) + { + VERBOSE(EXCEPTION) << e.what() << std::endl; + + return false; + } + + return true; } diff --git a/runtimes/neurun/src/frontend/wrapper/execution.h b/runtimes/neurun/src/frontend/wrapper/execution.h index 684b009..8479ec5 100644 --- a/runtimes/neurun/src/frontend/wrapper/execution.h +++ b/runtimes/neurun/src/frontend/wrapper/execution.h @@ -24,25 +24,26 @@ struct ANeuralNetworksExecution { public: - ANeuralNetworksExecution(const std::shared_ptr &plan) + ANeuralNetworksExecution(const std::shared_ptr &plan) noexcept : _executor{new neurun::exec::Executor{plan}} { // DO NOTHING } public: - const neurun::model::operand::Index getInputOperandIndex(uint32_t index); - const neurun::model::operand::Index getOutputOperandIndex(uint32_t index); + bool setInput(uint32_t index, const ANeuralNetworksOperandType *type, const void *buffer, + size_t length) noexcept; + bool setOutput(uint32_t index, const ANeuralNetworksOperandType *type, void *buffer, + size_t length) noexcept; + bool execute(void) noexcept; + + const neurun::model::operand::Index getInputOperandIndex(uint32_t index) noexcept; + const neurun::model::operand::Index getOutputOperandIndex(uint32_t index) noexcept; bool compareDataType(const ANeuralNetworksOperandType *type, - const neurun::model::operand::Index index); + const neurun::model::operand::Index index) noexcept; bool compareShape(const ANeuralNetworksOperandType *type, - const neurun::model::operand::Index index); - bool haveUnspecifiedDims(const neurun::model::operand::Index index); - void setInput(uint32_t index, const ANeuralNetworksOperandType *type, const void *buffer, - size_t length); - void setOutput(uint32_t index, const ANeuralNetworksOperandType *type, void *buffer, - size_t length); - void execute(void) { _executor->execute(); } + const neurun::model::operand::Index index) noexcept; + bool haveUnspecifiedDims(const neurun::model::operand::Index index) noexcept; private: std::shared_ptr _executor; -- 2.7.4