[ML][Common] Add support for creating TensorsInfo from native handle 96/251896/2
authorRafal Walczyna <r.walczyna@samsung.com>
Wed, 20 Jan 2021 11:49:15 +0000 (12:49 +0100)
committerRafal Walczyna <r.walczyna@samsung.com>
Wed, 20 Jan 2021 13:43:15 +0000 (13:43 +0000)
ACR: TWDAPI-273

Some of the ml native functions require not initialized handle.
TensorsInfo::CreateTensorsInfo() uses ml_tensors_info_create which allocates
memory for tensors info - but i.e. ml_single_get_input_info allocates memory
again without freeing old handle.

[Verification] Builts successfully, TensorsInfo.count tested in Chrome dev console.

Change-Id: I6b2e9c60dbed23a2d1962e68cc9408a8faecb383
Signed-off-by: Rafal Walczyna <r.walczyna@samsung.com>
src/ml/ml_tensors_info_manager.cc
src/ml/ml_tensors_info_manager.h

index 33af06f64cdeec8bde41338bfe8d918f16058171..8947aa677ec4e9b2ac2d69f5b8f4d95aeac3d298 100644 (file)
@@ -24,7 +24,7 @@ using common::PlatformResult;
 namespace extension {
 namespace ml {
 
-TensorsInfo::TensorsInfo(ml_tensors_info_h handle, int id) : handle_(handle), id_(id), count_(0) {
+TensorsInfo::TensorsInfo(ml_tensors_info_h handle, int id) : handle_(handle), id_(id), count_(-1) {
   ScopeLogger();
 }
 
@@ -42,6 +42,15 @@ int TensorsInfo::Id() {
 }
 
 int TensorsInfo::Count() {
+  if (-1 == count_) {
+    LoggerD("Lazy initialization of count_ property.");
+    unsigned int c;
+    if (NativeGetCount(&c)) {
+      count_ = c;
+    } else {
+      LoggerE("Could not fetch TensorsInfo count from native layer");
+    }
+  }
   return this->count_;
 }
 
@@ -98,7 +107,6 @@ std::shared_ptr<TensorsInfo> TensorsInfo::CreateClone(int cloneId) {
     return nullptr;
   }
   auto t = std::make_shared<TensorsInfo>(clone_h, cloneId);
-  t->count_ = this->Count();
   return t;
 }
 
@@ -301,6 +309,17 @@ TensorsInfo* TensorsInfoManager::CreateTensorsInfo() {
   return t.get();
 };
 
+TensorsInfo* TensorsInfoManager::CreateTensorsInfo(ml_tensors_info_h handle) {
+  ScopeLogger();
+
+  int id = nextId_++;
+  auto t = std::make_shared<TensorsInfo>(handle, id);
+  map_by_id_[id] = t;
+  map_by_handle_[handle] = t;
+
+  return t.get();
+};
+
 TensorsInfo* TensorsInfoManager::CloneTensorsInfo(TensorsInfo* src) {
   ScopeLogger();
   if (nullptr == src) {
index d1d8b3c563df465dcc8e35087750d5d1dcd0b057..6320216e25c580a80d72a71122f5de627afa6617 100644 (file)
@@ -69,6 +69,8 @@ class TensorsInfoManager {
   TensorsInfoManager();
   ~TensorsInfoManager();
   TensorsInfo* CreateTensorsInfo();
+  // handle will be destroyed on TensorsInfo object destruction
+  TensorsInfo* CreateTensorsInfo(ml_tensors_info_h handle);
   TensorsInfo* CloneTensorsInfo(TensorsInfo* src);
 
   TensorsInfo* GetTensorsInfo(int id);