[API/Profile] Add profile data path to the profile data struct
authorDongju Chae <dongju.chae@samsung.com>
Tue, 20 Apr 2021 08:11:07 +0000 (17:11 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Wed, 21 Apr 2021 09:58:23 +0000 (18:58 +0900)
This patch adds profile data path (.rec) to the profile data struct.
Also, the prefix directory can be set using envvar (NE_PREFIX_PROFILE).

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/ne-profiler.cc
src/core/npu/NPUdrvAPI.h
src/core/npu/NPUdrvAPI_emul.cc
src/host/ne-host.cc
tests/apptests/tvn_triv2_profile.cc
tests/utils/ne_test_utils.cc
tests/utils/ne_test_utils.h

index c1c095f..2e58d2e 100644 (file)
@@ -352,6 +352,7 @@ typedef struct {
 } npu_profile_layer;
 
 typedef struct {
+  char *prof_path;               /**< Profile data path (.rec file, if exists) */
   uint32_t num_layers;
 
   int64_t total_system_cycles;
index fc2d01f..d80463f 100644 (file)
@@ -375,6 +375,7 @@ int cleanNPU_genericBuffers (npudev_h dev, generic_buffers * buffers);
  * @param[in] req_id Identifier for each inference (obtained by runNPU_*)
  * @param[out] profile Profile instance
  * @return 0 if no error, otherwise a negative errno.
+ * @note Internal data of npu_profile is valid until putNPU_profile is called
  */
 int getNPU_profile (npudev_h dev, int req_id, npu_profile *profile);
 
index f01002e..bddbb54 100644 (file)
@@ -137,6 +137,7 @@ HostHandler::getProfile (int req_id, npu_profile *profile)
 
   profile->num_layers = 0;
   profile->layers = nullptr;
+  profile->prof_path = nullptr;
 
   return profiler_->getProfile (req_id, profile);
 }
index 91a809d..7fbc804 100644 (file)
@@ -35,7 +35,7 @@ class HostHandler {
     int unregisterModel (uint32_t modelid);
     int unregisterModels ();
 
-    int getProfile (int run_id, npu_profile *profile);
+    int getProfile (int req_id, npu_profile *profile);
     int getAPILevel (uint32_t *level);
     int getTops (uint32_t *tops);
     int getDspmSize (uint32_t *dspm);
index 3c76413..9c08e98 100644 (file)
@@ -50,7 +50,6 @@ ModelProfiler::getProfile (int req_id, npu_profile *profile)
     manipulateProfile (extended, profile);
 
   profile_map_.remove (req_id);
-
   return 0;
 }
 
@@ -179,5 +178,3 @@ ModelProfiler::manipulateProfile (HWmem * extended, npu_profile *profile)
     delete [] new_layers;
   }
 }
-
-
index 9b3fec4..6ee02ef 100644 (file)
@@ -96,8 +96,7 @@ class DriverAPI {
 #endif
 
     /** @brief get profile data for vISA instructions */
-    virtual int getProfile (int task_id, npu_profile *profile) const { return -EPERM; }
-
+    virtual int getProfile (int req_id, npu_profile *profile) const { return -EPERM; }
     virtual int getStatApps (npu_stat_apps *stat) const { return -EPERM; }
     virtual int getStatReqs (int appid, npu_stat_reqs *stat) const { return -EPERM; }
 
@@ -216,6 +215,7 @@ class TrinityEmulAPI : public DriverAPI {
     dev_type dev_type_; /**< emulated device type */
 
     char *prefix_share_;  /**< prefix of share directory */
+    char *prefix_profile_;  /**< prefix of profile directory */
 };
 
 #endif
index dc1c5aa..e6c3860 100644 (file)
 
 #define MAX_EMUL_DEVICES (3)
 #define ENV_PREFIX_SHARE "NE_PREFIX_SHARE"
+#define ENV_PREFIX_PROFILE "NE_PREFIX_PROFILE"
 #define DEFAULT_PREFIX_SHARE NE_PREFIX "/share"
-#define DEFAULT_PROFILE_PATH "/tmp"
+#define DEFAULT_PREFIX_PROFILE "/tmp"
 
 class EmulReq {
   public:
     EmulReq (int req_id) :
       req_id_ (req_id), stop_ (false), first_run_ (false) {}
 
+    const char * get_profile_path () const { return prof_path_.c_str (); }
+
     void run_emul (char *prog, char **segt, char *metadata,
         std::string cmd_path, std::string prof_path) {
       first_run_ = true;
 
+      prof_path_ = prof_path + ".rec";
       while (!stop_ || first_run_) {
         run_triv2_emul (prog, segt, metadata, cmd_path.c_str (), prof_path.c_str ());
         first_run_ = false;
@@ -57,12 +61,15 @@ class EmulReq {
     }
 
     bool get_profile (npu_profile *profile) {
-      std::string path (DEFAULT_PROFILE_PATH);
-      path += "/ne_profile." + std::to_string (req_id_) + ".rec";
-
-      std::ifstream ifs (path, std::ios::binary);
+      std::ifstream ifs (prof_path_, std::ios::binary);
       if (!ifs.good ()) {
-        std::cerr << "Failed to find the profile data " << path << "\n";
+        std::cerr << "Failed to find the profile data " << prof_path_ << "\n";
+        return false;
+      }
+
+      profile->prof_path = strdup (prof_path_.c_str ());
+      if (!profile->prof_path) {
+        std::cerr << "Unable to duplicate the profile path " << prof_path_ << "\n";
         return false;
       }
 
@@ -175,6 +182,7 @@ class EmulReq {
     bool stop_;
     bool first_run_;
     std::thread req_;
+    std::string prof_path_;
 };
 
 /**
@@ -290,12 +298,19 @@ static std::mutex global_lock;
 TrinityEmulAPI::TrinityEmulAPI (int dev_id, dev_type type)
   : DriverAPI (dev_id), dev_type_ (type)
 {
-  static char default_prefix[] = DEFAULT_PREFIX_SHARE;
+  static char default_prefix_share[] = DEFAULT_PREFIX_SHARE;
+  static char default_prefix_profile[] = DEFAULT_PREFIX_PROFILE;
 
   prefix_share_ = getenv(ENV_PREFIX_SHARE);
   if (prefix_share_ == nullptr) {
-    prefix_share_ = default_prefix;
-    setenv(ENV_PREFIX_SHARE, default_prefix, 1);
+    prefix_share_ = default_prefix_share;
+    setenv(ENV_PREFIX_SHARE, default_prefix_share, 1);
+  }
+
+  prefix_profile_ = getenv(ENV_PREFIX_PROFILE);
+  if (prefix_profile_ == nullptr) {
+    prefix_profile_ = default_prefix_profile;
+    setenv(ENV_PREFIX_PROFILE, default_prefix_profile, 1);
   }
 }
 
@@ -616,7 +631,7 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const
       return status;
     }
 
-    std::string prof_path (DEFAULT_PROFILE_PATH);
+    std::string prof_path (prefix_profile_);
 
     prof_path += "/ne_profile." + std::to_string (req_id);
 
@@ -686,6 +701,5 @@ TrinityEmulAPI::getProfile (int req_id, npu_profile *profile) const
     return -EINVAL;
 
   req_map_.remove (req_id);
-
   return 0;
 }
index fb1b5c2..b7d98b1 100644 (file)
@@ -198,6 +198,9 @@ void putNPU_profile (npu_profile *profile)
   if (profile != nullptr) {
     delete [] profile->layers;
 
+    if (profile->prof_path != nullptr)
+      free (profile->prof_path);
+
     memset (profile, '\x00', sizeof (npu_profile));
   }
 }
index 175d241..ac421c1 100644 (file)
@@ -56,6 +56,8 @@ class Tester : public UtilTRIV2
       npu_profile profile;
       int status = UtilTRIV2::getProfile (task_id, &profile);
       if (status == 0) {
+        if (profile.prof_path != nullptr)
+          cerr << "Profile Path : " << profile.prof_path << "\n";
         cerr << "Total System Cycles : " << profile.total_system_cycles << "\n";
         if (profile.dram_input_footprint > 0)
           cerr << "DRAM Input Footprint (KB) : " << (profile.dram_input_footprint >> 10) << "\n";
index fc08343..94c4e3e 100644 (file)
@@ -678,15 +678,15 @@ int UtilTrinity::runInternal (uint32_t model_id, std::string dev_path)
 }
 
 /** @brief stop inference from run_internal */
-int UtilTrinity::stopInternal (int task_id)
+int UtilTrinity::stopInternal (int req_id)
 {
-  return stopNPU_internalInput (dev_, task_id);
+  return stopNPU_internalInput (dev_, req_id);
 }
 
 /** @brief get profile information from NPU */
-int UtilTrinity::getProfile (int task_id, npu_profile *profile)
+int UtilTrinity::getProfile (int req_id, npu_profile *profile)
 {
-  return getNPU_profile (dev_, task_id, profile);
+  return getNPU_profile (dev_, req_id, profile);
 }
 
 /** @brief configure constraint for the model */
index 158fa5f..a26af6b 100644 (file)
@@ -107,9 +107,9 @@ class UtilTrinity {
     int runAll (bool sync);
 
     int runInternal (uint32_t model_id, std::string dev_path);
-    int stopInternal (int task_id);
+    int stopInternal (int req_id);
 
-    int getProfile (int task_id, npu_profile *profile);
+    int getProfile (int req_id, npu_profile *profile);
 
     UtilModel *findModel (uint32_t model_id);