[core] apply npu version check logic to both emul and triv2 api
authorYelin Jeong <yelini.jeong@samsung.com>
Tue, 26 Sep 2023 09:20:52 +0000 (18:20 +0900)
committer추지호/SoC Architecture팀(SR)/삼성전자 <jiho.chu@samsung.com>
Thu, 12 Oct 2023 04:24:47 +0000 (13:24 +0900)
This patch applies npu version check logic to both api.
checkVersion is moved to handler from emul API.

Signed-off-by: Yelin Jeong <yelini.jeong@samsung.com>
src/core/ne-handler.cc
src/core/ne-handler.h
src/core/npu/NPUdrvAPI.h
src/core/npu/NPUdrvAPI_emul.cc
tests/unittests/ne_core_handler_test.cc

index e2bf0bf4d0a4e84b25b173b01df6516585f2f35d..5b0274835fbe5207740931ab216b6408cc3f3c95 100644 (file)
@@ -926,6 +926,40 @@ TrinityVision2::checkDspmSize (const Model *model) {
   return 0;
 }
 
+/**
+ * @brief check if npu_version matches dev_type
+ * @param[in] npu_version the npu version of model
+ * @return 0 if no error, otherwise a negative errno
+ */
+int
+TrinityVision2::checkVersion (uint64_t npu_version) const {
+  uint32_t major, minor;
+  bool match = false;
+
+  major = NPU_VERSION_MAJOR (npu_version);
+  minor = NPU_VERSION_MINOR (npu_version);
+
+  if (major == 2) {
+    if (type_ == dev_type::NPUCOND_TRIV2_CONN_UNKNOWN ||
+        type_ == dev_type::NPUCOND_TRIV2_CONN_SOCIP) {
+      match = true;
+    } else if (minor == 3 && (type_ == NPUCOND_TRIV23_CONN_SOCIP ||
+                              type_ == NPUCOND_TRIV23_CONN_UNKNOWN)) {
+      match = true;
+    } else if (minor == 4 && (type_ == NPUCOND_TRIV24_CONN_SOCIP ||
+                              type_ == NPUCOND_TRIV24_CONN_UNKNOWN)) {
+      match = true;
+    }
+  }
+
+  if (!match) {
+    logerr (TAG, "npu version(0x%llx), dev_type(0x%x)", npu_version, type_);
+    return -EINVAL;
+  }
+
+  return 0;
+}
+
 /**
  * @brief implementation of TRIV2's setModel ()
  * @param[in] model_buf the model generic buffer
@@ -980,6 +1014,10 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model **model_ptr) {
   if (status != 0)
     goto delete_exit;
 
+  status = checkVersion (model->getMetadata ()->getNPUVersion ());
+  if (status != 0)
+    goto delete_exit;
+
   /** allocate program (optional; NOP) */
   if (model->getMetadata ()->getProgramSize () > 0) {
     HWmem *hwmem_prog = new HWmem (new HWmemDevice);
index 44396c8b04e082a96cf4c4656014c50c7b0cbb3f..b8ef0a0f7fa9340be90086a16096cbdf5b6a7ecf 100644 (file)
@@ -194,6 +194,7 @@ class TrinityVision2 : public Device {
 
   int getTensorSize (const Model *model, bool input, uint32_t index, uint32_t *size);
   int checkDspmSize (const Model *model);
+  int checkVersion (uint64_t npu_version) const;
 
   int run (npu_input_opmode opmode, const Model *model, const input_buffers *input,
            output_buffers *output = nullptr, npuOutputNotify cb = nullptr, void *cb_data = nullptr);
index cf5f6e664a870c01b7db440dbfff983619733566..e32cb26f750adf49942ba9742dc0adf6bd82baea 100644 (file)
@@ -209,7 +209,6 @@ class TrinityEmulAPI : public DriverAPI {
 
   int getProfile (int req_id, npu_profile *profile, const std::string path = "") const;
   void manipulateProfile (EmulReq *req, npu_profile *profile) const;
-  int checkVersion (uint64_t npu_version) const;
 
  private:
   static std::atomic<int> global_fd_;
index afb8fc925de2a04231947321415cc0d1086cc572..affe5a1179c15c7bd60a64a2e63e990eb0b2de64 100644 (file)
@@ -630,35 +630,6 @@ TrinityEmulAPI::munmap (void *addr, size_t size) const {
   return 0;
 }
 
-int
-TrinityEmulAPI::checkVersion (uint64_t npu_version) const {
-  uint32_t major, minor;
-  bool match = false;
-
-  major = NPU_VERSION_MAJOR (npu_version);
-  minor = NPU_VERSION_MINOR (npu_version);
-
-  if (major == 2) {
-    if (dev_type_ == dev_type::NPUCOND_TRIV2_CONN_UNKNOWN ||
-        dev_type_ == dev_type::NPUCOND_TRIV2_CONN_SOCIP) {
-      match = true;
-    } else if (minor == 3 && (dev_type_ == NPUCOND_TRIV23_CONN_SOCIP ||
-                              dev_type_ == NPUCOND_TRIV23_CONN_UNKNOWN)) {
-      match = true;
-    } else if (minor == 4 && (dev_type_ == NPUCOND_TRIV24_CONN_SOCIP ||
-                              dev_type_ == NPUCOND_TRIV24_CONN_UNKNOWN)) {
-      match = true;
-    }
-  }
-
-  if (!match) {
-    logerr (TAG, "npu version(0x%lx), dev_type(0x%x)", npu_version, dev_type_);
-    return -1;
-  }
-
-  return 0;
-}
-
 /**
  * @brief register model config to the driver
  * @param[in] model_config model configuration to be registered
@@ -675,11 +646,6 @@ TrinityEmulAPI::registerModel (model_config_t *model_config) const {
   if (model_config == nullptr || model_config->dbuf_fd < 0)
     return -EINVAL;
 
-  if (checkVersion (model_config->npu_version)) {
-    logerr (TAG, "model npu version is not matched to device");
-    return -EINVAL;
-  }
-
   EmulElement *elem = elem_map_.find (model_config->dbuf_fd);
   if (elem == nullptr)
     return -ENOENT;
index fc5bb2aa4cbf03322f853dba56c12927c15ba1e1..944a3d4beeb67005194d086eb4b7c58a0673c51b 100644 (file)
@@ -168,6 +168,8 @@ create_model_buffer (generic_buffer &model_buf) {
   meta.output_seg_dims[0][2] = 64;
   meta.output_seg_dims[0][3] = 64;
 
+  meta.npu_version = testenv.get_version ();
+
   model_buf.type = BUFFER_MAPPED;
   model_buf.size = meta.size;
   model_buf.addr = new char[meta.size];