[neurun] Model finish flag (#2176)
author오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Tue, 7 Aug 2018 01:36:45 +0000 (10:36 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 7 Aug 2018 01:36:45 +0000 (10:36 +0900)
Introduce model finish flag to check duplicated finish call & modification after finished

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/neurun/src/frontend/model.cc
runtimes/neurun/src/model.cc
runtimes/neurun/src/model.h

index 8debb4c..fc6c852 100644 (file)
@@ -36,6 +36,11 @@ int ANeuralNetworksModel_addOperand(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   // ASSUME A tensor operand always consists of fp32 values
   // NOTE We do not care about scala operands.
   assert(!(type->dimensionCount > 1) || (type->type == 3 /* ANEURALNETWORKS_TENSOR_FLOAT32 */));
@@ -66,6 +71,11 @@ int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel *model, int32_t in
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   const internal::tflite::operand::Index ind{index};
   auto &obj = model->deref().operands().at(ind);
 
@@ -85,6 +95,11 @@ int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   const internal::tflite::operand::Index ind{index};
   auto &obj = model->deref().operands().at(ind);
 
@@ -106,6 +121,11 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   switch (type)
   {
     case ANEURALNETWORKS_CONV_2D:
@@ -249,6 +269,11 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   switch (type)
   {
     default:
@@ -265,6 +290,11 @@ int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, u
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (model->isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   // NOTE ::internal::tflite::operand::Index uses int as its underlying type as various NNAPI
   //      functions such as ANeuralNetworksModel_setOperandValue use int to represent operand index
   //
@@ -294,5 +324,5 @@ int ANeuralNetworksModel_finish(ANeuralNetworksModel *model)
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
-  return ANEURALNETWORKS_NO_ERROR;
+  return model->finish();
 }
index 088803b..32c16c0 100644 (file)
@@ -1,12 +1,22 @@
-#include <NeuralNetworks.h>
-#include <NeuralNetworksEx.h>
-
 #include "model.h"
 
 //
 // ANeuralNetworksModel
 //
-ANeuralNetworksModel::ANeuralNetworksModel() : _model{new internal::tflite::Model}
+ANeuralNetworksModel::ANeuralNetworksModel() : _model{new internal::tflite::Model}, _finished(false)
 {
   // DO NOTHING
 }
+
+ResultCode ANeuralNetworksModel::finish()
+{
+  // This function must only be called once for a given model
+  if (isFinished())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
+  _finished = true;
+
+  return ANEURALNETWORKS_NO_ERROR;
+}
index 8e27199..e9c1009 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef __MODEL_H__
 #define __MODEL_H__
 
+#include <NeuralNetworks.h>
+
 #include "internal/Model.h"
 
 struct ANeuralNetworksModel
@@ -10,12 +12,15 @@ public:
 
 public:
   internal::tflite::Model &deref(void) { return *_model; }
+  ResultCode finish();
+  bool isFinished() { return _finished; }
 
 public:
   void release(std::shared_ptr<internal::tflite::Model> &model) { model = _model; }
 
 private:
   std::shared_ptr<internal::tflite::Model> _model;
+  bool _finished;
 };
 
 #endif // __MODEL_H__