[Triv2/Model] Add the model's finalize() to apply user configurations
authorDongju Chae <dongju.chae@samsung.com>
Mon, 22 Jun 2020 04:49:59 +0000 (13:49 +0900)
committer송욱/On-Device Lab(SR)/Staff Engineer/삼성전자 <wook16.song@samsung.com>
Wed, 24 Jun 2020 07:17:02 +0000 (16:17 +0900)
This patch adds the model's finalize() to apply user configurations
before running inference.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
include/common/typedef.h
src/core/ne-handler.cc
src/core/ne-model.cc
src/core/ne-model.h

index 6bf8bdf..3b43703 100644 (file)
@@ -61,6 +61,7 @@ typedef enum {
   DATA_LAYOUT_SRNPU = DATA_LAYOUT_TRIV,
                         /**< alias for backward-compatibility */
   DATA_LAYOUT_TRIV2,    /**< customized layout for TRIV2 (based on NHWC) */
+  DATA_LAYOUT_MODEL,    /**< use the same data layout specified in model metadata */
 } data_layout;
 
 /**
@@ -84,6 +85,8 @@ typedef enum {
   DATA_TYPE_INT64,
   DATA_TYPE_UINT64,
   DATA_TYPE_FLOAT64,
+  /* unknown */
+  DATA_TYPE_MODEL,      /**< use the same data type specified in model metadata */
 } data_type;
 
 /**
index d7954ba..a93b0e4 100644 (file)
@@ -581,6 +581,12 @@ HostHandler::runAsync (uint32_t modelid, const input_buffers *input,
       return -ENOENT;
   }
 
+  /* check the given model before running */
+  if (!model->finalize ()) {
+    logerr (TAG, "Failed to finalize the model. Please see the log messages\n");
+    return -EINVAL;
+  }
+
   device_->setAsyncMode (mode);
   return device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
 }
index d5de732..bcd4a81 100644 (file)
@@ -434,6 +434,61 @@ Model::getOutputDataInfo (uint32_t idx) const {
 }
 
 /**
+ * @brief finalize the model instance with user-provided configurations
+ * @return true if no error. otherwise false
+ */
+bool
+Model::finalize () {
+  uint32_t input_num = getInputTensorNum ();
+  uint32_t output_num = getOutputTensorNum ();
+
+  /** check tensors info */
+  if (input_num != in_.num_info) {
+    logerr (TAG, "The number of input tensors is different. Please set setNPU_dataInfo()\n");
+    return false;
+  }
+
+  if (output_num != out_.num_info) {
+    logerr (TAG, "The number of output tensors is different. Please set setNPU_dataInfo()\n");
+    return false;
+  }
+
+  /** evaluate data layout/type if required */
+  int version = getMetadata ()->getVersion ();
+  for (uint32_t idx = 0; idx < input_num; idx++) {
+    if (in_.info[idx].layout == DATA_LAYOUT_MODEL) {
+      if (version >= 3)
+        in_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      else
+        in_.info[idx].layout = DATA_LAYOUT_SRNPU;
+    }
+    if (in_.info[idx].type == DATA_TYPE_MODEL) {
+      if (version >= 3)
+        in_.info[idx].type = getMetadata ()->getInputQuantType (idx);
+      else
+        in_.info[idx].type = DATA_TYPE_SRNPU;
+    }
+  }
+
+  for (uint32_t idx = 0; idx < output_num; idx++) {
+    if (out_.info[idx].layout == DATA_LAYOUT_MODEL) {
+      if (version >= 3)
+        out_.info[idx].layout = DATA_LAYOUT_TRIV2;
+      else
+        out_.info[idx].layout = DATA_LAYOUT_SRNPU;
+    }
+    if (out_.info[idx].type == DATA_TYPE_MODEL) {
+      if (version >= 3)
+        out_.info[idx].type = getMetadata ()->getOutputQuantType (idx);
+      else
+        out_.info[idx].type = DATA_TYPE_SRNPU;
+    }
+  }
+
+  return true;
+}
+
+/**
  * @brief get the size of data type
  * @return the data size
  */
index b16ba79..e0ee6e3 100644 (file)
@@ -313,6 +313,8 @@ class Model : public HWmem {
     const tensor_data_info * getInputDataInfo (uint32_t idx) const;
     const tensor_data_info * getOutputDataInfo (uint32_t idx) const;
 
+    bool finalize ();
+
   private:
     static std::atomic<uint32_t> global_model_id_;  /**< global model id */
     uint32_t model_id_;         /**< model id */