[API] add an API to calculate a tensor size of model
authorDongju Chae <dongju.chae@samsung.com>
Mon, 24 May 2021 11:25:32 +0000 (20:25 +0900)
committer문지중/On-Device Lab(SR)/Principal Engineer/삼성전자 <jijoong.moon@samsung.com>
Tue, 25 May 2021 04:34:50 +0000 (13:34 +0900)
This patch adds an API to calculate a tensor size which
the target model assumes.

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

index 27b7eee..7e31a27 100644 (file)
@@ -155,6 +155,18 @@ int unregisterNPUmodel (npudev_h dev, uint32_t model_id);
 int unregisterNPUmodel_all (npudev_h dev);
 
 /**
+ * @brief Get tensor size that the target model assumes
+ * @param[in] dev The NPU device handle
+ * @param[in] model_id The target model id
+ * @param[in] input true if it's input tensor
+ * @param[in] index tensor index
+ * @param[out] size tensor size
+ * @return 0 if no error. otherwise a negative error value
+ */
+int getNPUmodel_tensorSize (npudev_h dev, uint32_t model_id, bool input,
+                            uint32_t index, uint32_t *size);
+
+/**
  * @brief [OPTIONAL] Set the data layout for input/output tensors
  * @param[in] dev The NPU device handle
  * @param[in] model_id The ID of model whose layouts are set
index 2d0c909..3b5fb72 100644 (file)
@@ -116,6 +116,24 @@ HostHandler::unregisterModels () {
 }
 
 /**
+ * @brief Get tensor size that the target model assumes
+ * @param[in] model_id The target model id
+ * @param[in] input true if it's input tensor
+ * @param[in] index tensor index
+ * @param[out] size tensor size
+ * @return 0 if no error. otherwise a negative error value
+ */
+int
+HostHandler::getTensorSize (uint32_t modelid, bool input, uint32_t index,
+                            uint32_t *size) {
+  Model *model = models_.find (modelid);
+  if (model == nullptr)
+    return -ENOENT;
+
+  return device_->getTensorSize (model, input, index, size);
+}
+
+/**
  * @brief Get the profile information from NPU
  * @param[in] req_id The identifier for each inference
  * @param[in] opt Profile options
@@ -924,6 +942,29 @@ TrinityVision2::unsetModel (Model *model) {
   return 0;
 }
 
+/**
+ * @brief Get tensor size that the target model assumes
+ * @param[in] model_id The target model id
+ * @param[in] input true if it's input tensor
+ * @param[in] index tensor index
+ * @param[out] size tensor size
+ * @return 0 if no error. otherwise a negative error value
+ */
+int
+TrinityVision2::getTensorSize (const Model *model, bool input, uint32_t index,
+                               uint32_t *size) {
+  const Metadata *meta = model->getMetadata ();
+  if (meta == nullptr)
+    return -EINVAL;
+
+  if (input)
+    *size = meta->getInputTensorSize (index, DATA_LAYOUT_TRIV2);
+  else
+    *size = meta->getOutputTensorSize (index, DATA_LAYOUT_TRIV2);
+
+  return 0;
+}
+
 /** @brief implementation of TRIV2's run() */
 int
 TrinityVision2::run (npu_input_opmode opmode, const Model *model,
index a5736b2..4efe8d3 100644 (file)
@@ -36,6 +36,9 @@ class HostHandler {
   int unregisterModel (uint32_t modelid);
   int unregisterModels ();
 
+  int getTensorSize (uint32_t modelid, bool input, uint32_t index,
+                     uint32_t *size);
+
   int getProfile (int req_id, const npu_profile_opt &opt, npu_profile *profile);
   int getAPILevel (uint32_t *level);
   int getTops (uint32_t *tops);
@@ -124,6 +127,11 @@ class Device {
   }
   virtual int unsetModel (Model *model) { return -EPERM; }
 
+  virtual int getTensorSize (const Model *model, bool input, uint32_t index,
+                             uint32_t *size) {
+    return -EPERM;
+  }
+
   virtual int run (npu_input_opmode opmode, const Model *model,
                    const input_buffers *input, npuOutputNotify cb = nullptr,
                    void *cb_data = nullptr, uint64_t *sequence = nullptr) = 0;
@@ -173,6 +181,10 @@ class TrinityVision2 : public Device {
 
   int setModel (const generic_buffer *model, Model **model_ptr);
   int unsetModel (Model *model);
+
+  int getTensorSize (const Model *model, bool input, uint32_t index,
+                     uint32_t *size);
+
   int run (npu_input_opmode opmode, const Model *model,
            const input_buffers *input, npuOutputNotify cb = nullptr,
            void *cb_data = nullptr, uint64_t *sequence = nullptr);
index 533eb33..3d9b058 100644 (file)
@@ -350,6 +350,23 @@ unregisterNPUmodel_all (npudev_h dev) {
 }
 
 /**
+ * @brief Get tensor size that the target model assumes
+ * @param[in] dev The NPU device handle
+ * @param[in] model_id The target model id
+ * @param[in] input true if it's input tensor
+ * @param[in] index tensor index
+ * @param[out] size tensor size
+ * @return 0 if no error. otherwise a negative error value
+ */
+int
+getNPUmodel_tensorSize (npudev_h dev, uint32_t model_id, bool input,
+                        uint32_t index, uint32_t *size) {
+  INIT_HOST_HANDLER (host_handler, dev);
+
+  return host_handler->getTensorSize (model_id, input, index, size);
+}
+
+/**
  * @brief [OPTIONAL] Set the data layout for input/output tensors
  * @param[in] dev The NPU device handle
  * @param[in] modelid The ID of model whose layouts are set