summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>2019-02-18 09:12:34 +0900
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>2019-02-18 09:12:34 +0900
commit15079bc44357cecc3d9b025bae6155b486843562 (patch)
tree7bdf9b6dddfa9704dc427f76d82bb01fdf751b0f
parent3794cd2fc9bd66a5b447e1196250585f6a1e4805 (diff)
downloadnnfw-15079bc44357cecc3d9b025bae6155b486843562.tar.gz
nnfw-15079bc44357cecc3d9b025bae6155b486843562.tar.bz2
nnfw-15079bc44357cecc3d9b025bae6155b486843562.zip
Revise frontend set operand value (#4432)
- Introduce wrapper method to set operand value - Introduce wrapper methods to check correctness - Add log message for fail - Move method implementation into .cc from header - Change setOptionalOperand as private Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
-rw-r--r--runtimes/neurun/src/frontend/model.cc45
-rw-r--r--runtimes/neurun/src/frontend/wrapper/model.cc63
-rw-r--r--runtimes/neurun/src/frontend/wrapper/model.h12
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: