[Handler/API] Add libnpuhost APIs to get driver API level
authorDongju Chae <dongju.chae@samsung.com>
Fri, 13 Nov 2020 10:36:59 +0000 (19:36 +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 driver API levle.

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

index 8f03adf..276ea98 100644 (file)
@@ -103,6 +103,14 @@ typedef struct {
 typedef void *npudev_h;
 
 /**
+ * @brief Get npu-engine libraray version
+ * @param[out] major major version
+ * @param[out] minor minor version
+ * @param[out] extra extra version
+ */
+void getVersion (uint32_t *major, uint32_t *minor, uint32_t *extra);
+
+/**
  * @brief Returns the number of available NPU devices.
  * @return @c The number of available NPU devices.
  * @note this number indicates the range of device IDs in getNPUdeviceByType ().
@@ -125,6 +133,22 @@ int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id);
 void putNPUdevice (npudev_h dev);
 
 /**
+ * @brief Get the driver API level that npu-engine library assumes
+ * @param[out] level driver API level
+ * @note when this API level is lower than actual driver's one,
+ *       some operations might be not working.
+ */
+void getDriverAPILevel (uint32_t *level);
+
+/**
+ * @brief Get the driver API level of opened NPU device
+ * @param[in] dev the NPU device handle
+ * @param[out] level driver API level
+ * @return 0 if no error, otherwise a negative errno
+ */
+int getNPU_driverAPILevel (npudev_h dev, uint32_t *level);
+
+/**
  * @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 24977fe..f08570f 100644 (file)
@@ -42,6 +42,22 @@ npudev_h HostHandler::latest_dev_ = nullptr;
 /** implement libnpuhost APIs */
 
 /**
+ * @brief Get npu-engine libraray version
+ * @param[out] major major version
+ * @param[out] minor minor version
+ * @param[out] extra extra version
+ */
+void getVersion (uint32_t *major, uint32_t *minor, uint32_t *extra)
+{
+  if (major != nullptr)
+    *major = VER_NE_MAJOR;
+  if (minor != nullptr)
+    *minor = VER_NE_MINOR;
+  if (extra != nullptr)
+    *extra = VER_NE_EXTRA;
+}
+
+/**
  * @brief Returns the number of available NPU devices.
  * @return @c The number of NPU devices.
  * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
@@ -75,6 +91,34 @@ void putNPUdevice (npudev_h dev)
 }
 
 /**
+ * @brief Get the driver API level that npu-engine library assumes (compiled)
+ * @param[out] level driver API level
+ * @note when this API level is lower than actual driver's one,
+ *       some operations might be not working.
+ */
+void getDriverAPILevel (uint32_t *level)
+{
+  if (level != nullptr)
+    *level = DriverAPI::getCompiledAPILevel ();
+}
+
+/**
+ * @brief Get the driver API level of opened NPU device
+ * @param[in] dev the NPU device handle
+ * @param[out] level driver API level
+ * @return 0 if no error, otherwise a negative errno
+ */
+int getNPU_driverAPILevel (npudev_h dev, uint32_t *level)
+{
+  INIT_HOST_HANDLER (host_handler, dev);
+
+  if (level == nullptr)
+    return -EINVAL;
+
+  return host_handler->getAPILevel (level);
+}
+
+/**
  * @brief Get the profile information from NPU
  * @param[in] dev The NPU device handle
  * @param[in] task_id The identifier for each inference
@@ -579,6 +623,20 @@ HostHandler::getProfile (int task_id, npu_profile *profile)
 }
 
 /**
+ * @brief Get the driver API level of opened NPU device
+ * @param[out] level driver API level
+ * @return 0 if no error, otherwise a negative errno
+ */
+int
+HostHandler::getAPILevel (uint32_t *level)
+{
+  const DriverAPI * api = device_->getDriverAPI ();
+  assert (api != nullptr);
+
+  return api->getAPILevel (level);
+}
+
+/**
  * @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 a29aa28..5437cd2 100644 (file)
@@ -36,6 +36,7 @@ class HostHandler {
     int unregisterModels ();
 
     int getProfile (int run_id, npu_profile *profile);
+    int getAPILevel (uint32_t *level);
 
     int setDataInfo (uint32_t modelid, tensors_data_info *in, tensors_data_info *out);
     int setConstraint (uint32_t modelid, npuConstraint constraint);
index f41fbc5..367ffb1 100644 (file)
@@ -19,6 +19,8 @@
 #include "NPUdrvAPI.h"
 #include <assert.h>
 
+uint32_t DriverAPI::api_level_ = TRINITY_API_LEVEL;
+
 /**
  * @brief DriverAPI constructor
  */
index 37d2964..82a5c0e 100644 (file)
@@ -59,13 +59,14 @@ class DriverAPI {
 
     static int getNumDevices (dev_type type);
     static std::unique_ptr<DriverAPI> createDriverAPI (dev_type type, int device_id);
-
+    static uint32_t getCompiledAPILevel () { return api_level_; }
     int getDeviceID () const { return dev_id_; }
     int getDeviceFD () const { return dev_fd_; }
     bool initialized () const { return initialized_; }
 
     /** @brief open the device and set device fd */
     virtual int open () { return -ENODEV; }
+    virtual int getAPILevel (uint32_t *level) const { return -EPERM; }
 
     virtual int checkSanity () { return 0; }
 
@@ -116,6 +117,8 @@ class DriverAPI {
     /** @brief initialize device API. open the device here */
     int init ();
     bool initialized_;
+
+    static uint32_t api_level_; /**< Trinity API level */
 };
 
 /** @brief Driver APIs for TRIV */
@@ -127,6 +130,7 @@ class TrinityVisionAPI : public DriverAPI {
 
     int open ();
     int checkSanity ();
+    int getAPILevel (uint32_t *level) const;
 
     device_state_t isReady () const;
     uint32_t numRequests () const;
@@ -161,6 +165,7 @@ class TrinityVision2API : public DriverAPI {
 
     int open ();
     int checkSanity ();
+    int getAPILevel (uint32_t *level) const;
 
     device_state_t isReady () const;
     uint32_t numRequests () const;
index 47ebf5d..f171705 100644 (file)
@@ -366,3 +366,18 @@ TrinityVisionAPI::getDrvVersion () const
 
   return ver;
 }
+
+int
+TrinityVisionAPI::getAPILevel (uint32_t *level) const
+{
+ int err;
+
+  if (!this->initialized())
+    return -EPERM;
+
+  err = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_API_LEVEL, level);
+  if (err != 0)
+    return -errno;
+
+  return 0;
+}
index ab18bbf..36d12ab 100644 (file)
@@ -470,3 +470,18 @@ ioctl_fail:
   free (profile.buf);
   return -errno;
 }
+
+int
+TrinityVision2API::getAPILevel (uint32_t *level) const
+{
+ int err;
+
+  if (!this->initialized())
+    return -EPERM;
+
+  err = ioctl (this->getDeviceFD (), TRINITY_IOCTL_GET_API_LEVEL, level);
+  if (err != 0)
+    return -errno;
+
+  return 0;
+}