From: Piotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics Date: Fri, 26 Feb 2021 06:09:19 +0000 (+0100) Subject: [ML][Single] Implemented setTimeout and close X-Git-Tag: submit/tizen/20210304.103045~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac135f7538c1e8740ed51499e44f387687a9aa7c;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [ML][Single] Implemented setTimeout and close [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 --- diff --git a/src/ml/js/ml_single.js b/src/ml/js/ml_single.js index 57fae3e6..45278c90 100755 --- a/src/ml/js/ml_single.js +++ b/src/ml/js/ml_single.js @@ -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; +}; diff --git a/src/ml/ml_instance.cc b/src/ml/ml_instance.cc index 1e188e11..85d96dd9 100644 --- a/src/ml/ml_instance.cc +++ b/src/ml/ml_instance.cc @@ -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(args.get(kId).get()); + auto timeout = static_cast(args.get(kTimeout).get()); + + 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(args.get(kId).get()); + + auto ret = single_manager_.Close(id); + if (!ret) { + ReportError(ret, &out); + return; + } + + ReportSuccess(out); +} // Single API end diff --git a/src/ml/ml_instance.h b/src/ml/ml_instance.h index 3366b3e5..0f1f92e2 100644 --- a/src/ml/ml_instance.h +++ b/src/ml/ml_instance.h @@ -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 diff --git a/src/ml/ml_single_manager.cc b/src/ml/ml_single_manager.cc index a460a14f..ef388258 100644 --- a/src/ml/ml_single_manager.cc +++ b/src/ml/ml_single_manager.cc @@ -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 diff --git a/src/ml/ml_single_manager.h b/src/ml/ml_single_manager.h index 18d5aa02..05fd040a 100644 --- a/src/ml/ml_single_manager.h +++ b/src/ml/ml_single_manager.h @@ -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_; diff --git a/src/ml/ml_singleshot.cc b/src/ml/ml_singleshot.cc index d7cc4d15..ad7b68f8 100644 --- a/src/ml/ml_singleshot.cc +++ b/src/ml/ml_singleshot.cc @@ -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 diff --git a/src/ml/ml_singleshot.h b/src/ml/ml_singleshot.h index 32a831e0..aebccab8 100644 --- a/src/ml/ml_singleshot.h +++ b/src/ml/ml_singleshot.h @@ -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);