[Emul] Revise NPU emulation to use libmrpsim.so
authorDongju Chae <dongju.chae@samsung.com>
Fri, 30 Oct 2020 02:08:43 +0000 (11:08 +0900)
committer송욱/On-Device Lab(SR)/Staff Engineer/삼성전자 <wook16.song@samsung.com>
Tue, 10 Nov 2020 08:16:58 +0000 (17:16 +0900)
From now on, we don't use libnpu-engine-emul.so. Instead,
libmrpsim.so will do such work, and support profiling as well.
Also, the emulation on Tizen env. is now disabled.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
debian/control
src/core/ne-handler.cc
src/core/ne-model.h
src/core/npu/NPUdrvAPI_emul.cc
src/core/npu/meson.build
src/meson.build

index 71f8424..e900c56 100644 (file)
@@ -5,14 +5,14 @@ Maintainer: MyungJoo Ham <myungjoo.ham@samsung.com>
 Build-Depends: ninja-build, meson (>=0.50), debhelper (>=9),
  gcc-9 | gcc-8 | gcc-7 | gcc-6 | gcc-5, libgtest-dev, python,
  libiniparser-dev, pkg-config, cmake, libdrm-dev,
- linux-fvp-headers, npu-engine-emul, libtinyxml2-dev
+ linux-fvp-headers, libmrpsim-dev, libtinyxml2-dev
 Standards-Version: 3.8.2
 Homepage: https://research.samsung.com
 
 Package: npu-engine
 Architecture: amd64
 Multi-Arch: same
-Depends: npu-engine-emul, ${shlibs:Depends}, ${misc:Depends}
+Depends: libmrpsim, ${shlibs:Depends}, ${misc:Depends}
 Description: NPU Engine
  This provides in-NPU software control daemon, NPU-Engine.
 
index a1d3c93..ae3da99 100644 (file)
@@ -1503,6 +1503,11 @@ TrinityVision2::setModel (const generic_buffer *model_buf, Model ** model_ptr)
     config.program_size = hwmem_prog->getSize ();
     config.program_offset_addr = 0;
 
+    /** for metadata extra section */
+    config.metadata_dbuf_fd = model->getDmabuf ();
+    config.metadata_extra_addr = NPUBIN_META_SIZE;
+    config.metadata_extra_size = model->getMetadata()->getMetaExtraSize ();
+
     status = api_->registerModel (&config);
     if (status != 0)
       goto delete_exit;
index 9404f7e..9b2411e 100644 (file)
@@ -92,6 +92,7 @@ class Metadata {
     uint64_t getWeightSize () const { return meta_->weight_size; }
     uint64_t getBufferSize () const { return meta_->buffer_size; }
     uint32_t getMetaSize () const { return NPUBIN_META_TOTAL_SIZE(meta_->magiccode); }
+    uint32_t getMetaExtraSize () const { return NPUBIN_META_EXTRA_SIZE(meta_->magiccode); }
 
     int getVersion () const { return version_; }
 
index b9f1b52..063d84f 100644 (file)
@@ -17,7 +17,7 @@
 #include <atomic>
 #include <memory>
 
-#include <NPUemul.h>
+#include <mrpsim.h>
 #include <npubinfmt.h>
 #include <ne-conf.h>
 
@@ -27,9 +27,9 @@ class EmulTask {
   public:
     EmulTask () : stop_ (false) {}
 
-    void run_emul (char *prog, size_t prog_size, char **segt, size_t seg_num) {
+    void run_emul (char *prog, char **segt, char *metadata) {
       while (!stop_)
-        run_triv2_emul (prog, prog_size, segt, seg_num);
+        run_triv2_emul (prog, segt, metadata, MRPSIM_TRIV2_CMD_PATH);
 
       delete [] segt;
     }
@@ -409,15 +409,18 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const
   char * addr_model = static_cast<char *>(elem_model->getAddr ());
 
   model_config_t * model = elem_model->getModelConfig (input_config->model_id);
+  EmulElement *elem_metadata = elem_map_.find (model->metadata_dbuf_fd);
+  if (elem_metadata == nullptr || elem_metadata->getAddr () == nullptr)
+    return -EINVAL;
 
   /**
-   * call NPU C-emulation codes (AIP/NPU_SystemService_Emulator)
+   * call trinity simulator codes (https://github.sec.samsung.net/AIP/SIM_TrinityVision2)
    */
   int status = -EPERM;
 
   if ((dev_type_ & DEVICETYPE_MASK) == DEVICETYPE_TRIV) {
-    status = run_triv_emul (addr_model + model->program_offset_addr, model->program_size,
-        addr_input);
+    /* we don't check the functioanl correctness for TRIV */
+    return 0;
   } else if ((dev_type_ & DEVICETYPE_MASK) == DEVICETYPE_TRIV2) {
     if (input_config->num_segments <= 0)
       return -EINVAL;
@@ -442,9 +445,11 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const
       segment_table[i] = static_cast<char *>(elem->getAddr ()) + offset;
     }
 
+    /*
     set_triv2_debug (
         Conf::getInstance().getLogVerbose(),
         Conf::getInstance().getLogDir());
+    */
 
     if (input_config->input_mode == INPUT_HW) {
       int taskid = global_fd_.fetch_add (1);
@@ -457,12 +462,14 @@ TrinityEmulAPI::runInput (input_config_t *input_config) const
       }
 
       auto func = std::bind (&EmulTask::run_emul, task,
-          prog, prog_size, segment_table, num_segs);
+          prog, segment_table, static_cast <char*> (elem_metadata->getAddr ()));
 
       task->run (func);
       status = taskid;
     } else {
-      status = run_triv2_emul (prog, prog_size, segment_table, num_segs);
+      status = run_triv2_emul (prog, segment_table,
+          static_cast <char*> (elem_metadata->getAddr ()),
+          MRPSIM_TRIV2_CMD_PATH);
       delete [] segment_table;
     }
   }
@@ -480,6 +487,7 @@ TrinityEmulAPI::stop () const
   if (!initialized())
     return -EPERM;
 
+#if 0
   if ((dev_type_ & DEVICETYPE_MASK) == DEVICETYPE_TRIV) {
     return stop_triv_emul ();
   } else if ((dev_type_ & DEVICETYPE_MASK) == DEVICETYPE_TRIV2) {
@@ -487,6 +495,8 @@ TrinityEmulAPI::stop () const
   }
 
   return -EPERM;
+#endif
+  return 0;
 }
 
 int
index f04a199..5fc48c4 100644 (file)
@@ -7,17 +7,18 @@ ne_core_npu_src = [
 ]
 
 if get_option('enable_npu_emul')
-  ne_emul_dep = dependency(
-      'npu-engine-emul',
-      required: false
-  )
+  ne_emul_dep = dependency('libmrpsim', required: false)
   if not ne_emul_dep.found()
     warning('falling back to the hardcoded dependency on "npu-engine-emul"')
     ne_emul_dep = declare_dependency (
       compile_args : ['-I/opt/trinity/include/npu-engine'],
-      link_args: ['-L/opt/trinity/lib', '-lnpu-engine-emul'],
+      link_args: ['-L/opt/trinity/lib', '-lmrpsim -lrt -ldl'],
     )
   endif
+
+  cmd_path = '"/opt/trinity/share/mRPsim/triv2.cmd"'
+  add_project_arguments('-DMRPSIM_TRIV2_CMD_PATH=' + cmd_path, language: ['c', 'cpp'])
+
   ne_core_npu_dep = declare_dependency(
     sources : [ne_core_npu_src, 'NPUdrvAPI_emul.cc'],
     dependencies : [ne_core_utils_dep, ne_emul_dep],
index a67410b..09467aa 100644 (file)
@@ -11,12 +11,14 @@ ne_dependencies = [
 ne_library_static = static_library('npu-engine',
   include_directories : ne_common_inc,
   dependencies: ne_dependencies,
+  build_rpath : ne_libdir,
   install : true,
   install_rpath : ne_libdir,
   install_dir : ne_libdir)
 ne_library_shared = shared_library('npu-engine',
   include_directories : ne_common_inc,
   dependencies: ne_dependencies,
+  build_rpath : ne_libdir,
   install : true,
   install_rpath : ne_libdir,
   install_dir : ne_libdir)