[ML][Single] Implemented setTimeout and close 22/254322/5
authorPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Fri, 26 Feb 2021 06:09:19 +0000 (07:09 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 2 Mar 2021 13:36:10 +0000 (13:36 +0000)
[ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-273

[Verification] Code compiles. Verified in chrome console:

var m = tizen.ml.single.openModel("/opt/usr/home/owner/media/Documents/mobilenet_v1_1.0_224_quant.tflite")   // success
// timeout check
// check if invoke works 'normally'
var ti = new tizen.ml.TensorsInfo();
ti.addTensorInfo("tensor", "UINT8", [3, 224, 224])
var td = ti.getTensorsData();
var tdout = m.invoke(td)  // success

// check if timeout will change behaviour of invoke, while short timeout is set
m.setTimeout(5)    // success
var tdout = m.invoke(td)  // TimeoutError

// check empty arguments
m.setTimeout()     // InvalidValuesError

// close check
m.close()    // success
// Abort error: "SingleShot object was closed and using it is no longer possible."
// for following calls:
m.close()
m.input
m.output
m.invoke()
m.getValue()
m.setValue()
m.setTimeout()

Change-Id: I3ff7b899fc1527f62aea24387cadf4d0f0db3f05

src/ml/js/ml_single.js
src/ml/ml_instance.cc
src/ml/ml_instance.h
src/ml/ml_single_manager.cc
src/ml/ml_single_manager.h
src/ml/ml_singleshot.cc
src/ml/ml_singleshot.h

index 57fae3e..45278c9 100755 (executable)
@@ -197,12 +197,22 @@ MachineLearningSingle.prototype.openModelAsync = function() {
 };
 
 // SingleShot interface (input & output)
+function checkSingleShotNotClosed(singleshot) {
+    if (singleshot._id === undefined) {
+        throw new WebAPIException(
+            WebAPIException.ABORT_ERR,
+            'SingleShot object was closed and using it is no longer possible.'
+        );
+    }
+}
+
 var ValidInputExceptions = ['TypeMismatchError', 'AbortError'];
 var SingleShot = function(id) {
     var _input, _output;
     Object.defineProperties(this, {
         input: {
             get: function() {
+                checkSingleShotNotClosed(this);
                 if (!_input) {
                     var result = native_.callSync('MLSingleShotGetTensorsInfo', {
                         id: this._id,
@@ -220,6 +230,7 @@ var SingleShot = function(id) {
                 return _input;
             },
             set: function() {
+                checkSingleShotNotClosed(this);
                 var args = validator_.validateArgs(arguments, [
                     {
                         name: 'inTensorsInfo',
@@ -246,6 +257,7 @@ var SingleShot = function(id) {
         },
         output: {
             get: function() {
+                checkSingleShotNotClosed(this);
                 if (!_output) {
                     var result = native_.callSync('MLSingleShotGetTensorsInfo', {
                         id: this._id,
@@ -266,7 +278,7 @@ var SingleShot = function(id) {
                 /* readonly */
             }
         },
-        _id: { value: id, writable: false, enumerable: false }
+        _id: { value: id, writable: true, enumerable: false }
     });
 };
 
@@ -278,6 +290,7 @@ var ValidInvokeExceptions = [
 ];
 
 SingleShot.prototype.invoke = function() {
+    checkSingleShotNotClosed(this);
     var args = validator_.validateArgs(arguments, [
         {
             name: 'inTensorsData',
@@ -311,6 +324,7 @@ var GetSetValueValidExceptions = [
 ];
 
 SingleShot.prototype.getValue = function() {
+    checkSingleShotNotClosed(this);
     var args = validator_.validateArgs(arguments, [
         {
             name: 'name',
@@ -343,6 +357,7 @@ SingleShot.prototype.getValue = function() {
 };
 
 SingleShot.prototype.setValue = function() {
+    checkSingleShotNotClosed(this);
     var args = validator_.validateArgs(arguments, [
         {
             name: 'name',
@@ -377,6 +392,58 @@ SingleShot.prototype.setValue = function() {
         );
     }
 };
-// SingleShot::setTimeout()
 
-// SingleShot::close()
+var SetTimeoutValidExceptions = [
+    'NotSupportedError',
+    'AbortError'
+];
+SingleShot.prototype.setTimeout = function() {
+    checkSingleShotNotClosed(this);
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'timeout',
+            type: types_.UNSIGNED_LONG
+        }
+    ]);
+
+    if (!args.has.timeout) {
+        throw new WebAPIException(
+            WebAPIException.INVALID_VALUES_ERR,
+            'Invalid parameter: timeout is undefined'
+        );
+    }
+
+    var callArgs = {
+        id: this._id,
+        timeout: args.timeout
+    };
+
+    var result = native_.callSync('MLSingleShotSetTimeout', callArgs);
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObjectAndValidate(
+            result,
+            SetTimeoutValidExceptions,
+            AbortError
+        );
+    }
+};
+
+var CloseValidExceptions = [
+    'AbortError'
+];
+SingleShot.prototype.close = function() {
+    checkSingleShotNotClosed(this);
+    var callArgs = {
+        id: this._id
+    };
+
+    var result = native_.callSync('MLSingleShotClose', callArgs);
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObjectAndValidate(
+            result,
+            CloseValidExceptions,
+            AbortError
+        );
+    }
+    this._id = undefined;
+};
index 1e188e1..85d96dd 100644 (file)
@@ -128,8 +128,8 @@ MlInstance::MlInstance()
   REGISTER_METHOD(MLSingleShotInvoke);
   REGISTER_METHOD(MLSingleShotGetValue);
   REGISTER_METHOD(MLSingleShotSetValue);
-  // SingleShot::setTimeout()
-  // SingleShot::close()
+  REGISTER_METHOD(MLSingleShotSetTimeout);
+  REGISTER_METHOD(MLSingleShotClose);
 
   // Single API end
 
@@ -731,6 +731,7 @@ void MlInstance::MLTensorsDataSetTensorRawData(const picojson::value& args, pico
 
 // TODO move to the up section with field names
 namespace {
+const std::string kTimeout = "timeout";
 const std::string kModelPath = "modelPath";
 const std::string kInTensorsInfo = "inTensorsInfo";
 const std::string kOutTensorsInfo = "outTensorsInfo";
@@ -952,9 +953,37 @@ void MlInstance::MLSingleShotSetValue(const picojson::value& args, picojson::obj
   ReportSuccess(out);
 }
 
-// SingleShot::setTimeout()
+void MlInstance::MLSingleShotSetTimeout(const picojson::value& args, picojson::object& out) {
+  ScopeLogger("args: %s", args.serialize().c_str());
+  CHECK_ARGS(args, kId, double, out);
+  CHECK_ARGS(args, kTimeout, double, out);
+
+  auto id = static_cast<int>(args.get(kId).get<double>());
+  auto timeout = static_cast<unsigned long>(args.get(kTimeout).get<double>());
+
+  auto ret = single_manager_.SetTimeout(id, timeout);
+  if (!ret) {
+    ReportError(ret, &out);
+    return;
+  }
+
+  ReportSuccess(out);
+}
 
-// SingleShot::close()
+void MlInstance::MLSingleShotClose(const picojson::value& args, picojson::object& out) {
+  ScopeLogger("args: %s", args.serialize().c_str());
+  CHECK_ARGS(args, kId, double, out);
+
+  auto id = static_cast<int>(args.get(kId).get<double>());
+
+  auto ret = single_manager_.Close(id);
+  if (!ret) {
+    ReportError(ret, &out);
+    return;
+  }
+
+  ReportSuccess(out);
+}
 
 // Single API end
 
index 3366b3e..0f1f92e 100644 (file)
@@ -78,8 +78,8 @@ class MlInstance : public common::ParsedInstance {
   void MLSingleShotInvoke(const picojson::value& args, picojson::object& out);
   void MLSingleShotGetValue(const picojson::value& args, picojson::object& out);
   void MLSingleShotSetValue(const picojson::value& args, picojson::object& out);
-  // SingleShot::setTimeout()
-  // SingleShot::close()
+  void MLSingleShotSetTimeout(const picojson::value& args, picojson::object& out);
+  void MLSingleShotClose(const picojson::value& args, picojson::object& out);
   // Single API end
 
   // Pipeline API begin
index a460a14..ef38825 100644 (file)
@@ -167,8 +167,29 @@ PlatformResult SingleManager::SetValue(int id, const std::string& name, const st
   return single->SetValue(name, value);
 }
 
-// SingleShot::setTimeout()
-// SingleShot::close()
+PlatformResult SingleManager::SetTimeout(int id, unsigned long timeout) {
+  ScopeLogger();
+
+  SingleShot* single = GetSingleShot(id);
+  if (!single) {
+    LoggerE("Could not find SingleShot handle");
+    return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
+  }
+
+  return single->SetTimeout(timeout);
+}
+
+PlatformResult SingleManager::Close(int id) {
+  ScopeLogger();
+
+  SingleShot* single = GetSingleShot(id);
+  if (!single) {
+    LoggerE("Could not find SingleShot handle");
+    return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error");
+  }
+
+  return single->Close();
+}
 
 }  // namespace ml
 }  // namespace extension
index 18d5aa0..05fd040 100644 (file)
@@ -50,8 +50,8 @@ class SingleManager {
   PlatformResult Invoke(int id, TensorsData* in_tensors_data, TensorsData** out_tensors_data);
   PlatformResult GetValue(int id, const std::string& name, std::string& value);
   PlatformResult SetValue(int id, const std::string& name, const std::string& value);
-  // SingleShot::setTimeout()
-  // SingleShot::close()
+  PlatformResult SetTimeout(int id, unsigned long timeout);
+  PlatformResult Close(int id);
 
  private:
   int nextId_;
index d7cc4d1..ad7b68f 100644 (file)
@@ -222,9 +222,27 @@ PlatformResult SingleShot::SetValue(const std::string& name, const std::string&
   return PlatformResult{};
 }
 
-// SingleShot::setTimeout()
+PlatformResult SingleShot::SetTimeout(unsigned long timeout) {
+  ScopeLogger();
+  int ret = ml_single_set_timeout(handle_, timeout);
+  if (ML_ERROR_NONE != ret) {
+    LoggerE("ml_single_set_timeout failed: %d (%s)", ret, get_error_message(ret));
+    return util::ToPlatformResult(ret, "Failed to set timeout");
+  }
 
-// SingleShot::close()
+  return PlatformResult{};
+}
+
+PlatformResult SingleShot::Close() {
+  ScopeLogger();
+  int ret = ml_single_close(handle_);
+  if (ML_ERROR_NONE != ret) {
+    LoggerE("ml_single_close failed: %d (%s)", ret, get_error_message(ret));
+    return util::ToPlatformResult(ret, "Failed to close");
+  }
+
+  return PlatformResult{};
+}
 
 }  // namespace extension
 }  // namespace ml
index 32a831e..aebccab 100644 (file)
@@ -49,10 +49,8 @@ class SingleShot {
   PlatformResult CleanUpAfterInvoke();
   PlatformResult GetValue(const std::string& name, std::string& value);
   PlatformResult SetValue(const std::string& name, const std::string& value);
-
-  // SingleShot::setTimeout()
-
-  // SingleShot::close()
+  PlatformResult SetTimeout(unsigned long timeout);
+  PlatformResult Close();
 
  private:
   PlatformResult InvokeInternal(ml_tensors_data_h in_data, ml_tensors_info_h in_info);