[ML][Common] Add TensorsData.dispose method 02/252002/4
authorRafal Walczyna <r.walczyna@samsung.com>
Wed, 20 Jan 2021 13:19:15 +0000 (14:19 +0100)
committerRafal Walczyna <r.walczyna@samsung.com>
Tue, 26 Jan 2021 08:08:35 +0000 (09:08 +0100)
ACR: TWDAPI-273

Test code:
var ti = new tizen.ml.TensorsInfo();
ti.addTensorInfo("tensor1", "UINT8", [1, 1])
ti.addTensorInfo("tensor2", "UINT8", [2, 4])
var td = ti.getTensorsData();
console.log(td.count)
td.dispose();
console.log(td.count)

[Verification] Built successful. Tested in Chrome Dev console.

Change-Id: I9997467b36908d6e031473e58e2b3defbf253e00
Signed-off-by: Rafal Walczyna <r.walczyna@samsung.com>
src/ml/js/ml_common.js
src/ml/ml_instance.cc
src/ml/ml_instance.h
src/ml/ml_tensors_data_manager.cc
src/ml/ml_tensors_data_manager.h

index 8d8d19c..1c11756 100755 (executable)
@@ -65,6 +65,14 @@ var TensorType = {
 
 // TensorsData
 
+var _ValidTensorsDataIds = new Set();
+
+function _CheckIfTensorsDataNotDisposed(id) {
+    if (false == _ValidTensorsDataIds.has(id)) {
+        throw new WebAPIException(WebAPIException.ABORT_ERR, 'TensorsData is disposed');
+    }
+}
+
 var TensorsData = function(id, tensorsInfoId) {
     Object.defineProperties(this, {
         count: {
@@ -76,6 +84,7 @@ var TensorsData = function(id, tensorsInfoId) {
         tensorsInfo: {
             enumerable: true,
             get: function() {
+                _CheckIfTensorsDataNotDisposed(this._id);
                 return this._tensorsInfo.clone();
             }
         },
@@ -86,18 +95,36 @@ var TensorsData = function(id, tensorsInfoId) {
             enumerable: false
         }
     });
+    _ValidTensorsDataIds.add(id);
 };
 
 TensorsData.prototype.getTensorRawData = function() {
+    _CheckIfTensorsDataNotDisposed();
     throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
 };
 
 TensorsData.prototype.setTensorData = function() {
+    _CheckIfTensorsDataNotDisposed();
     throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
 };
 
 TensorsData.prototype.dispose = function() {
-    throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
+    if (false == _ValidTensorsDataIds.has(this._id)) {
+        privUtils_.log('TensorsData already disposed');
+        return;
+    }
+    var callArgs = {
+        tensorsDataId: this._id
+    };
+
+    var result = native_.callSync('MLTensorsDataDispose', callArgs);
+
+    if (native_.isFailure(result)) {
+        return;
+    }
+    _ValidTensorsDataIds['delete'](this._id);
+    // underlying tensorsInfo_ is also invalid
+    _ValidTensorsInfoIds['delete'](this._tensorsInfo._id);
 };
 
 // TensorsInfo
index bb6a0e0..a60a0f0 100644 (file)
@@ -95,6 +95,8 @@ MlInstance::MlInstance()
   REGISTER_METHOD(MLTensorsInfoEquals);
   REGISTER_METHOD(MLTensorsInfoDispose);
 
+  REGISTER_METHOD(MLTensorsDataDispose);
+
   // Single API begin
   REGISTER_METHOD(MLSingleManagerOpenModel);
   // MachineLearningSingle::openModelAsync()
@@ -568,6 +570,32 @@ void MlInstance::MLTensorsInfoDispose(const picojson::value& args, picojson::obj
   }
   ReportSuccess(out);
 }
+
+void MlInstance::MLTensorsDataDispose(const picojson::value& args, picojson::object& out) {
+  ScopeLogger("args: %s", args.serialize().c_str());
+  CHECK_ARGS(args, kTensorsDataId, double, out);
+  int tensors_data_id = static_cast<int>(args.get(kTensorsDataId).get<double>());
+
+  TensorsData* tensors_data = GetTensorsDataManager().GetTensorsData(tensors_data_id);
+  if (nullptr == tensors_data) {
+    LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Internal TensorsData error"), &out,
+                      ("Could not find TensorsData handle with given id: %d", tensors_data_id));
+    return;
+  }
+  // Dispose underlying tensorsInfo
+  PlatformResult result = GetTensorsInfoManager().DisposeTensorsInfo(tensors_data->TensorsInfoId());
+  if (!result) {
+    LogAndReportError(result, &out);
+    return;
+  }
+
+  result = GetTensorsDataManager().DisposeTensorsData(tensors_data_id);
+  if (!result) {
+    LogAndReportError(result, &out);
+    return;
+  }
+  ReportSuccess(out);
+}
 // Common ML API end
 
 // Single API begin
index f626751..499e89a 100644 (file)
@@ -54,6 +54,9 @@ class MlInstance : public common::ParsedInstance {
   void MLTensorsInfoClone(const picojson::value& args, picojson::object& out);
   void MLTensorsInfoEquals(const picojson::value& args, picojson::object& out);
   void MLTensorsInfoDispose(const picojson::value& args, picojson::object& out);
+
+  void MLTensorsDataDispose(const picojson::value& args, picojson::object& out);
+
   TensorsInfoManager tensors_info_manager_;
   TensorsDataManager tensors_data_manager_;
   // Common ML API end
index e3d2439..225e289 100644 (file)
@@ -102,5 +102,26 @@ TensorsData* TensorsDataManager::GetTensorsData(int id) {
   return nullptr;
 }
 
+PlatformResult TensorsDataManager::DisposeTensorsData(int id) {
+  ScopeLogger("id: %d", id);
+
+  TensorsData* t = GetTensorsData(id);
+
+  return DisposeTensorsData(t);
+}
+
+PlatformResult TensorsDataManager::DisposeTensorsData(TensorsData* t) {
+  ScopeLogger();
+
+  if (nullptr == t) {
+    LoggerE("Could not find tensor");
+    return PlatformResult(ErrorCode::ABORT_ERR);
+  }
+
+  map_.erase(t->Id());
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
 }  // ml
 }  // extension
index 012a2c3..39811df 100644 (file)
@@ -60,6 +60,9 @@ class TensorsDataManager {
   TensorsData* CreateTensorsData(TensorsInfo* tensors_info);
   TensorsData* GetTensorsData(int id);
 
+  PlatformResult DisposeTensorsData(int id);
+  PlatformResult DisposeTensorsData(TensorsData* t);
+
  private:
   TensorsDataManager(TensorsDataManager const&) = delete;
   TensorsDataManager& operator=(TensorsDataManager const&) = delete;