[Handler/API] Add libnpuhost APIs to get NPU device's TOPS
authorDongju Chae <dongju.chae@samsung.com>
Wed, 18 Nov 2020 04:17:07 +0000 (13:17 +0900)
committer우상정/On-Device Lab(SR)/Staff Engineer/삼성전자 <sangjung.woo@samsung.com>
Wed, 18 Nov 2020 10:57:29 +0000 (19:57 +0900)
This patch adds libnpuhost APIs to get NPU device's TOPS

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
include/common/typedef.h
include/host/libnpuhost.h
src/core/ne-handler.cc
src/core/ne-handler.h
src/core/npu/NPUdrvAPI.h
src/core/npu/NPUdrvAPI_emul.cc
src/core/npu/NPUdrvAPI_triv.cc
src/core/npu/NPUdrvAPI_triv2.cc

index 3b43703..8553bb8 100644 (file)
@@ -23,7 +23,7 @@
 /** alias */
 #define DEVICETYPE_TRIV DEVICETYPE_NPU
 #define DEVICETYPE_TRIA DEVICETYPE_ASR
-#define DEVICETYPE_TRIV2 (0x30000)      /** VD/SR-NPU 2020 */
+#define DEVICETYPE_TRIV2 (0x40000)      /** VD/SR-NPU 2020 */
 
 #define DEVICETYPE_MASK (0xFFFF0000)
 
index 276ea98..693b333 100644 (file)
@@ -149,6 +149,15 @@ void getDriverAPILevel (uint32_t *level);
 int getNPU_driverAPILevel (npudev_h dev, uint32_t *level);
 
 /**
+ * @brief Get the TOPS of the opened NPU device
+ * @param[in] dev the NPU device handle
+ * @param[out] tops npu tops
+ * @return 0 if no error, otherwise a negative errno
+ * @note this does not support for emulated devices
+ */
+int getNPU_tops (npudev_h dev, uint32_t *tops);
+
+/**
  * @brief Get metadata for NPU model
  * @param[in] model The path of model binary file
  * @param[in] need_extra whether you want to extract the extra data in metadata
index f08570f..655302f 100644 (file)
@@ -119,6 +119,23 @@ int getNPU_driverAPILevel (npudev_h dev, uint32_t *level)
 }
 
 /**
+ * @brief Get the TOPS of the opened NPU device
+ * @param[in] dev the NPU device handle
+ * @param[out] tops npu tops
+ * @return 0 if no error, otherwise a negative errno
+ * @note this does not support for emulated devices
+ */
+int getNPU_tops (npudev_h dev, uint32_t *tops)
+{
+  INIT_HOST_HANDLER (host_handler, dev);
+
+  if (tops == nullptr)
+    return -EINVAL;
+
+  return host_handler->getTops (tops);
+}
+
+/**
  * @brief Get the profile information from NPU
  * @param[in] dev The NPU device handle
  * @param[in] task_id The identifier for each inference
@@ -637,6 +654,22 @@ HostHandler::getAPILevel (uint32_t *level)
 }
 
 /**
+ * @brief Get the TOPS of the opened NPU device
+ * @param[in] dev the NPU device handle
+ * @param[out] tops npu tops
+ * @return 0 if no error, otherwise a negative errno
+ * @note this does not support for emulated devices
+ */
+int
+HostHandler::getTops (uint32_t *tops)
+{
+  const DriverAPI * api = device_->getDriverAPI ();
+  assert (api != nullptr);
+
+  return api->getTops (tops);
+}
+
+/**
  * @brief Set the data layout for input/output tensors
  * @param[in] modelid The ID of model whose layouts are set
  * @param[in] in the layout/type info for input tensors
index 5437cd2..630dbb1 100644 (file)
@@ -37,6 +37,7 @@ class HostHandler {
 
     int getProfile (int run_id, npu_profile *profile);
     int getAPILevel (uint32_t *level);
+    int getTops (uint32_t *tops);
 
     int setDataInfo (uint32_t modelid, tensors_data_info *in, tensors_data_info *out);
     int setConstraint (uint32_t modelid, npuConstraint constraint);
index 82a5c0e..3e38f1b 100644 (file)
@@ -67,6 +67,7 @@ class DriverAPI {
     /** @brief open the device and set device fd */
     virtual int open () { return -ENODEV; }
     virtual int getAPILevel (uint32_t *level) const { return -EPERM; }
+    virtual int getTops (uint32_t *tops) const { return -EPERM; }
 
     virtual int checkSanity () { return 0; }
 
@@ -131,6 +132,7 @@ class TrinityVisionAPI : public DriverAPI {
     int open ();
     int checkSanity ();
     int getAPILevel (uint32_t *level) const;
+    int getTops (uint32_t *tops) const;
 
     device_state_t isReady () const;
     uint32_t numRequests () const;
@@ -166,6 +168,7 @@ class TrinityVision2API : public DriverAPI {
     int open ();
     int checkSanity ();
     int getAPILevel (uint32_t *level) const;
+    int getTops (uint32_t *tops) const;
 
     device_state_t isReady () const;
     uint32_t numRequests () const;
index 778c1b1..c9aee2e 100644 (file)
@@ -21,7 +21,7 @@
 #include <npubinfmt.h>
 #include <ne-conf.h>
 
-#define MAX_EMUL_DEVICES (100)
+#define MAX_EMUL_DEVICES (3)
 #define ENV_PREFIX_SHARE "NE_PREFIX_SHARE"
 #define DEFAULT_PREFIX_SHARE "/opt/trinity/share"
 
index f171705..762df1c 100644 (file)
@@ -381,3 +381,23 @@ TrinityVisionAPI::getAPILevel (uint32_t *level) const
 
   return 0;
 }
+
+/**
+ * @brief get TOPS from the driver
+ * @param[out] tops npu tops
+ * @return 0 if no error. otherwise a negative errno
+ */
+int
+TrinityVisionAPI::getTops (uint32_t *tops) const
+{
+  int ret;
+
+  if (!this->initialized())
+    return -EPERM;
+
+  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_TOPS, tops);
+  if (ret != 0)
+    return -errno;
+
+  return ret;
+}
index 36d12ab..bded66e 100644 (file)
@@ -291,10 +291,9 @@ TrinityVision2API::registerModel (model_config_t *model_config,
 
   model_tops = NPU_VERSION_TOPS(npu_version);
   if (model_tops != 0) {
-    ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_TOPS,
-        &device_tops);
+    ret = getTops (&device_tops);
     if (ret != 0)
-      return -errno;
+      return ret;
 
     if (model_tops != device_tops)
       return -EINVAL;
@@ -485,3 +484,23 @@ TrinityVision2API::getAPILevel (uint32_t *level) const
 
   return 0;
 }
+
+/**
+ * @brief get TOPS from the driver
+ * @param[out] tops npu tops
+ * @return 0 if no error. otherwise a negative errno
+ */
+int
+TrinityVision2API::getTops (uint32_t *tops) const
+{
+  int ret;
+
+  if (!this->initialized())
+    return -EPERM;
+
+  ret = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_TOPS, tops);
+  if (ret != 0)
+    return -errno;
+
+  return ret;
+}