diff options
-rw-r--r-- | runtimes/neurun/src/frontend/model.cc | 45 | ||||
-rw-r--r-- | runtimes/neurun/src/frontend/wrapper/model.cc | 63 | ||||
-rw-r--r-- | runtimes/neurun/src/frontend/wrapper/model.h | 12 |
3 files changed, 94 insertions, 26 deletions
diff --git a/runtimes/neurun/src/frontend/model.cc b/runtimes/neurun/src/frontend/model.cc index c42d45f31..3efeee431 100644 --- a/runtimes/neurun/src/frontend/model.cc +++ b/runtimes/neurun/src/frontend/model.cc @@ -116,63 +116,66 @@ int ANeuralNetworksModel_addOperand(ANeuralNetworksModel *model, int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel *model, int32_t index, const void *buffer, size_t length) { - const bool isOptional = ((buffer == nullptr) && (length == 0)); + const bool optional_operand = ((buffer == nullptr) && (length == 0)); if ((model == nullptr) || ((buffer == nullptr) && (length != 0))) { + VERBOSE(NNAPI::Model) << "setOperandValue: Incorrect null pointer parameter(s)" << std::endl; return ANEURALNETWORKS_UNEXPECTED_NULL; } if (model->isFinished()) { + VERBOSE(NNAPI::Model) << "setOperandValue: Already finished" << std::endl; return ANEURALNETWORKS_BAD_STATE; } // Negative index value is not allowed if (index < 0) { + VERBOSE(NNAPI::Model) << "setOperandValue: Invalid index value (negative)" << std::endl; return ANEURALNETWORKS_BAD_DATA; } - const neurun::model::operand::Index ind{static_cast<uint32_t>(index)}; + // NOTE ::neurun::model::operand::Index uses uint32_t as its underlying type as various NNAPI + // functions such as ANeuralNetworksModel_addOperation use uint32_t to represent operand + // index + // ANeuralNetworksModel_setOperandValue, however, uses int32_t to represent operand index. + // + // Below, static_cast<uint32_t>(...) is introduced to eliminate compiler warning. + uint32_t ind = static_cast<uint32_t>(index); - if (!model->deref().operands().exist(ind)) + if (!model->isExistOperand(ind)) { + VERBOSE(NNAPI::Model) << "setOperandValue: Invalid index value (not exist)" << std::endl; return ANEURALNETWORKS_BAD_DATA; } - auto &obj = model->deref().operands().at(ind); - if ((obj.operandSize() != length) && !isOptional) + if (!optional_operand && (model->operandSize(ind) != length)) { + VERBOSE(NNAPI::Model) << "setOperandValue: Invalid data length" << std::endl; return ANEURALNETWORKS_BAD_DATA; } - if (!obj.usage(neurun::model::operand::Usage::CONSTANT)) - { - return ANEURALNETWORKS_BAD_DATA; - } - - using ::neurun::model::operand::CachedData; - using ::neurun::model::operand::ExternalData; - // Remain operands.at(ind).data()->base() as nullptr for optional operand - // This will be filled when model finished - if (isOptional) + if (model->isUsageSet(ind)) { - model->setOptionalOperand(ind); + VERBOSE(NNAPI::Model) << "setOperandValue: Already set operand" << std::endl; + return ANEURALNETWORKS_BAD_DATA; } // NNAPI spec in NeuralNetworks.h // For values of length greater than ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES, // the application is responsible for not changing the content of this region // until all executions using this model have completed + bool copy_value = false; if (length <= ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES) { - model->deref().setOperandValue(ind, nnfw::cpp14::make_unique<CachedData>( - reinterpret_cast<const uint8_t *>(buffer), length)); + copy_value = true; } - else + + if (!model->setOperandValue(ind, buffer, length, optional_operand, copy_value)) { - model->deref().setOperandValue(ind, nnfw::cpp14::make_unique<ExternalData>( - reinterpret_cast<const uint8_t *>(buffer), length)); + VERBOSE(NNAPI::Model) << "setOperandValue: Fail to set operand value" << std::endl; + return ANEURALNETWORKS_BAD_DATA; } return ANEURALNETWORKS_NO_ERROR; diff --git a/runtimes/neurun/src/frontend/wrapper/model.cc b/runtimes/neurun/src/frontend/wrapper/model.cc index c9276a551..d38e6708f 100644 --- a/runtimes/neurun/src/frontend/wrapper/model.cc +++ b/runtimes/neurun/src/frontend/wrapper/model.cc @@ -20,6 +20,8 @@ #include "util/logging.h" #include "util/NNAPIConvert.h" +#include "cpp14/memory.h" + // // ANeuralNetworksModel // @@ -52,6 +54,45 @@ bool ANeuralNetworksModel::addOperand(const ANeuralNetworksOperandType *type) no return true; } +bool ANeuralNetworksModel::setOperandValue(uint32_t index, const void *buffer, size_t length, + bool optional, bool copy) noexcept +{ + const neurun::model::operand::Index ind{index}; + + try + { + _model->operands().at(ind).usage(neurun::model::operand::Usage::CONSTANT); + + // Remain operands.at(ind).data()->base() as nullptr for optional operand + // This will be filled when model finished + if (optional) + { + setOptionalOperand(ind); + } + + using ::neurun::model::operand::CachedData; + using ::neurun::model::operand::ExternalData; + if (copy) + { + _model->setOperandValue(ind, nnfw::cpp14::make_unique<CachedData>( + reinterpret_cast<const uint8_t *>(buffer), length)); + } + else + { + _model->setOperandValue(ind, nnfw::cpp14::make_unique<ExternalData>( + reinterpret_cast<const uint8_t *>(buffer), length)); + } + } + catch (const std::exception &e) + { + VERBOSE(EXCEPTION) << e.what() << std::endl; + + return false; + } + + return true; +} + bool ANeuralNetworksModel::finish() noexcept { try @@ -70,6 +111,28 @@ bool ANeuralNetworksModel::finish() noexcept return true; } +bool ANeuralNetworksModel::isFinished() noexcept { return !_model->isBuildingPhase(); } + +bool ANeuralNetworksModel::isExistOperand(uint32_t index) noexcept +{ + return _model->operands().exist(neurun::model::operand::Index{index}); +} + +size_t ANeuralNetworksModel::operandSize(uint32_t index) noexcept +{ + return _model->operands().at(neurun::model::operand::Index{index}).operandSize(); +} + +bool ANeuralNetworksModel::isUsageSet(uint32_t index) noexcept +{ + return _model->operands().at(neurun::model::operand::Index{index}).usageIsDefined(); +} + +void ANeuralNetworksModel::setOptionalOperand(const neurun::model::operand::Index idx) +{ + _optional_operands.insert(idx); +} + void ANeuralNetworksModel::fillOptionalOperand(void) { _model->operations().iterate( diff --git a/runtimes/neurun/src/frontend/wrapper/model.h b/runtimes/neurun/src/frontend/wrapper/model.h index c6c9277a7..77ed5aab1 100644 --- a/runtimes/neurun/src/frontend/wrapper/model.h +++ b/runtimes/neurun/src/frontend/wrapper/model.h @@ -29,17 +29,19 @@ public: public: bool addOperand(const ANeuralNetworksOperandType *type) noexcept; + bool setOperandValue(uint32_t index, const void *buffer, size_t length, bool optional = false, + bool copy = false) noexcept; bool finish() noexcept; neurun::graph::Graph &deref(void) { return *_model; } - bool isFinished() { return !_model->isBuildingPhase(); } + bool isFinished() noexcept; + bool isExistOperand(uint32_t index) noexcept; + size_t operandSize(uint32_t index) noexcept; + bool isUsageSet(uint32_t index) noexcept; void release(std::shared_ptr<neurun::graph::Graph> &model) { model = _model; } - void setOptionalOperand(const neurun::model::operand::Index idx) - { - _optional_operands.insert(idx); - } private: + void setOptionalOperand(const neurun::model::operand::Index idx); void fillOptionalOperand(void); private: |