Revise frontend model object creation and finish (#4412)
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 14 Feb 2019 02:12:03 +0000 (11:12 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Thu, 14 Feb 2019 02:12:03 +0000 (11:12 +0900)
* Revise frontend model object creation and finish

- Add log message for fail
- Add noexcept keyword to wrapper method
- Move duplicate finish call check from wrapper to NNAPI implementation

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
* Return false if exception catched

runtimes/neurun/src/frontend/model.cc
runtimes/neurun/src/frontend/wrapper/model.cc
runtimes/neurun/src/frontend/wrapper/model.h

index 4f69305..4c30979 100644 (file)
 #include "frontend/wrapper/memory.h"
 #include "model/operation/Node.Include.h"
 #include "util/TypeConvert.h"
+#include "util/logging.h"
 
 int ANeuralNetworksModel_create(ANeuralNetworksModel **model)
 {
   if (model == nullptr)
   {
+    VERBOSE(NNAPI::Model) << "create: Incorrect null pointer parameter" << std::endl;
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
   *model = new (std::nothrow) ANeuralNetworksModel{};
   if (*model == nullptr)
   {
+    VERBOSE(NNAPI::Model) << "create: Fail to create model object" << std::endl;
     return ANEURALNETWORKS_OUT_OF_MEMORY;
   }
 
@@ -482,8 +485,21 @@ int ANeuralNetworksModel_finish(ANeuralNetworksModel *model)
 {
   if (model == nullptr)
   {
+    VERBOSE(NNAPI::Model) << "finish: Incorrect null pointer parameter" << std::endl;
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
-  return model->finish();
+  if (model->isFinished())
+  {
+    VERBOSE(NNAPI::Model) << "finish: Already finished" << std::endl;
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
+  if (!model->finish())
+  {
+    VERBOSE(NNAPI::Model) << "finish: Fail to generate internal graph" << std::endl;
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
+  return ANEURALNETWORKS_NO_ERROR;
 }
index a7a9275..424f436 100644 (file)
 #include "model.h"
 
 #include "graph/Graph.h"
+#include "util/logging.h"
 
 //
 // ANeuralNetworksModel
 //
-ANeuralNetworksModel::ANeuralNetworksModel()
+ANeuralNetworksModel::ANeuralNetworksModel() noexcept
     : _model{new neurun::graph::Graph}, _optional_operands{}
 {
   // DO NOTHING
 }
 
-ResultCode ANeuralNetworksModel::finish()
+bool ANeuralNetworksModel::finish() noexcept
 {
-  // This function must only be called once for a given model
-  if (isFinished())
+  try
   {
-    return ANEURALNETWORKS_BAD_STATE;
-  }
+    fillOptionalOperand();
 
-  fillOptionalOperand();
+    _model->finishBuilding();
+  }
+  catch (const std::exception &e)
+  {
+    VERBOSE(EXCEPTION) << e.what() << '\n';
 
-  _model->finishBuilding();
+    return false;
+  }
 
-  return ANEURALNETWORKS_NO_ERROR;
+  return true;
 }
 
 void ANeuralNetworksModel::fillOptionalOperand(void)
index 6491b63..fa25777 100644 (file)
 struct ANeuralNetworksModel
 {
 public:
-  ANeuralNetworksModel();
+  ANeuralNetworksModel() noexcept;
 
 public:
   neurun::graph::Graph &deref(void) { return *_model; }
-  ResultCode finish();
+  bool finish() noexcept;
   bool isFinished() { return !_model->isBuildingPhase(); }
   void release(std::shared_ptr<neurun::graph::Graph> &model) { model = _model; }
   void setOptionalOperand(const neurun::model::operand::Index idx)