From eac9c6193b2e59706f7c03cefb87d667b4f6f677 Mon Sep 17 00:00:00 2001 From: "Piotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics" Date: Mon, 25 Jan 2021 14:14:43 +0100 Subject: [PATCH] [ML][Single] Implemented SingleShot.input member - set MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * added set feature for SingleShot.input * added single gathering of SingleShot.input and output (wihout checking native layer every time) [ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-273 [Verification] Code compiles without errors. Checked in Chrome console: var model = tizen.ml.single.openModel("documents/mobilenet_v1_1.0_224_quant.tflite") model.input > TensorsInfo {_previouslyConstructed: true, _id: 1} var newTI = new tizen.ml.TensorsInfo() newTI.addTensorInfo("input", "UINT8", [3,224,224,1]) model.input = newTI; model.input > TensorsInfo {_previouslyConstructed: true, _id: 2} /// calling getter multiple times return always the same TensorsInfo Change-Id: I16c33a0ba515aba1efe2f5d93d0d3225b410aced --- src/ml/js/ml_single.js | 66 ++++++++++++++++++++++++++----------- src/ml/ml_instance.cc | 25 ++++++++++++++ src/ml/ml_instance.h | 1 + src/ml/ml_single_manager.cc | 19 +++++++++++ src/ml/ml_single_manager.h | 1 + src/ml/ml_singleshot.cc | 11 +++++++ src/ml/ml_singleshot.h | 2 +- 7 files changed, 104 insertions(+), 21 deletions(-) diff --git a/src/ml/js/ml_single.js b/src/ml/js/ml_single.js index 40a00de3..2fdf5712 100755 --- a/src/ml/js/ml_single.js +++ b/src/ml/js/ml_single.js @@ -99,13 +99,41 @@ MachineLearningSingle.prototype.openModel = function() { // SingleShot interface (input & output) var ValidInputExceptions = ['TypeMismatchError', 'AbortError']; var SingleShot = function(id) { + var _input, _output; Object.defineProperties(this, { input: { get: function() { - var result = native_.callSync('MLSingleShotGetTensorsInfo', { + if (!_input) { + var result = native_.callSync('MLSingleShotGetTensorsInfo', { + id: this._id, + getInputMode: true // true means gathering input information + }); + if (native_.isFailure(result)) { + throw native_.getErrorObjectAndValidate( + result, + ValidInputExceptions, + AbortError + ); + } + _input = new TensorsInfo(result.id); + } + return _input; + }, + set: function() { + var args = validator_.validateArgs(arguments, [ + { + name: 'inTensorsInfo', + type: types_.PLATFORM_OBJECT, + values: TensorsInfo + } + ]); + + var nativeArgs = { id: this._id, - getInputMode: true // true means gathering input information - }); + inTensorsInfo: args.inTensorsInfo._id + }; + + var result = native_.callSync('MLSingleShotSetInputInfo', nativeArgs); if (native_.isFailure(result)) { throw native_.getErrorObjectAndValidate( result, @@ -113,28 +141,26 @@ var SingleShot = function(id) { AbortError ); } - - return new TensorsInfo(result.id); - }, - set: function(v) { - /* TODO*/ + _input = args.inTensorsInfo; } }, output: { get: function() { - var result = native_.callSync('MLSingleShotGetTensorsInfo', { - id: this._id, - getInputMode: false // false means gathering output information - }); - if (native_.isFailure(result)) { - throw native_.getErrorObjectAndValidate( - result, - ValidInputExceptions, - AbortError - ); + if (!_output) { + var result = native_.callSync('MLSingleShotGetTensorsInfo', { + id: this._id, + getInputMode: false // false means gathering output information + }); + if (native_.isFailure(result)) { + throw native_.getErrorObjectAndValidate( + result, + ValidInputExceptions, + AbortError + ); + } + _output = new TensorsInfo(result.id); } - - return new TensorsInfo(result.id); + return _output.clone(); // return new copy to make it 'readonly' }, set: function(v) { /* readonly */ diff --git a/src/ml/ml_instance.cc b/src/ml/ml_instance.cc index f9fc8685..e4c78333 100644 --- a/src/ml/ml_instance.cc +++ b/src/ml/ml_instance.cc @@ -106,6 +106,7 @@ MlInstance::MlInstance() // MachineLearningSingle::openModelAsync() // OpenModelSuccessCallback REGISTER_METHOD(MLSingleShotGetTensorsInfo); + REGISTER_METHOD(MLSingleShotSetInputInfo); // SingleShot::invoke() // SingleShot::getValue() // SingleShot::setValue() @@ -713,6 +714,30 @@ void MlInstance::MLSingleShotGetTensorsInfo(const picojson::value& args, picojso ReportSuccess(out); } +void MlInstance::MLSingleShotSetInputInfo(const picojson::value& args, picojson::object& out) { + ScopeLogger("args: %s", args.serialize().c_str()); + CHECK_ARGS(args, kId, double, out); + CHECK_ARGS(args, kInTensorsInfo, double, out); + + auto id = static_cast(args.get(kId).get()); + auto inTensorId = static_cast(args.get(kInTensorsInfo).get()); + + TensorsInfo* in_tensors_info = GetTensorsInfoManager().GetTensorsInfo(inTensorId); + if (nullptr == in_tensors_info) { + LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Internal TensorsInfo error"), &out, + ("Could not find TensorsInfo handle with given id: %d", inTensorId)); + return; + } + + auto ret = single_manager_.SetNativeInputInfo(id, in_tensors_info); + if (!ret) { + ReportError(ret, &out); + return; + } + + ReportSuccess(out); +} + // SingleShot::invoke() // SingleShot::getValue() diff --git a/src/ml/ml_instance.h b/src/ml/ml_instance.h index 5e9dd634..95125fb5 100644 --- a/src/ml/ml_instance.h +++ b/src/ml/ml_instance.h @@ -67,6 +67,7 @@ class MlInstance : public common::ParsedInstance { // MachineLearningSingle::openModelAsync() // OpenModelSuccessCallback void MLSingleShotGetTensorsInfo(const picojson::value& args, picojson::object& out); + void MLSingleShotSetInputInfo(const picojson::value& args, picojson::object& out); // SingleShot::invoke() // SingleShot::getValue() // SingleShot::setValue() diff --git a/src/ml/ml_single_manager.cc b/src/ml/ml_single_manager.cc index 4bc3d1a7..5f7438d3 100644 --- a/src/ml/ml_single_manager.cc +++ b/src/ml/ml_single_manager.cc @@ -87,6 +87,25 @@ PlatformResult SingleManager::GetNativeTensorsInfo(int id, bool get_input_mode, return PlatformResult{}; } +PlatformResult SingleManager::SetNativeInputInfo(int id, TensorsInfo* inTensorsInfo) { + ScopeLogger(); + + SingleShot* single = GetSingleShot(id); + if (!single) { + LoggerE("Could not find singleShot handle"); + return PlatformResult(ErrorCode::ABORT_ERR); + } + + ml_tensors_info_h in_info = inTensorsInfo ? inTensorsInfo->Handle() : nullptr; + + PlatformResult ret = single->SetInputInfo(in_info); + if (!ret) { + return ret; + } + + return PlatformResult{}; +} + // SingleShot::invoke() // SingleShot::getValue() // SingleShot::setValue() diff --git a/src/ml/ml_single_manager.h b/src/ml/ml_single_manager.h index bf983739..2edbb8e7 100644 --- a/src/ml/ml_single_manager.h +++ b/src/ml/ml_single_manager.h @@ -44,6 +44,7 @@ class SingleManager { // MachineLearningSingle::openModelAsync() // OpenModelSuccessCallback PlatformResult GetNativeTensorsInfo(int id, bool get_input_mode, int* res_id); + PlatformResult SetNativeInputInfo(int id, TensorsInfo* inTensorsInfo); // SingleShot::invoke() // SingleShot::getValue() // SingleShot::setValue() diff --git a/src/ml/ml_singleshot.cc b/src/ml/ml_singleshot.cc index beff9f1e..9a4c96cc 100644 --- a/src/ml/ml_singleshot.cc +++ b/src/ml/ml_singleshot.cc @@ -63,6 +63,17 @@ PlatformResult SingleShot::GetTensorsInfo(bool get_input_mode, ml_tensors_info_h return PlatformResult{}; } +PlatformResult SingleShot::SetInputInfo(ml_tensors_info_h in_info) { + ScopeLogger(); + int ret = ml_single_set_input_info(handle_, in_info); + if (ML_ERROR_NONE != ret) { + LoggerE("ml_single_set_input_info failed: %d (%s)", ret, get_error_message(ret)); + return util::ToPlatformResult(ret, "Failed to set input info"); + } + + return PlatformResult{}; +} + // SingleShot::invoke() // SingleShot::getValue() diff --git a/src/ml/ml_singleshot.h b/src/ml/ml_singleshot.h index a78d955d..35c18b4f 100644 --- a/src/ml/ml_singleshot.h +++ b/src/ml/ml_singleshot.h @@ -42,7 +42,7 @@ class SingleShot { ~SingleShot(); PlatformResult GetTensorsInfo(bool get_input_mode, ml_tensors_info_h* result); - + PlatformResult SetInputInfo(ml_tensors_info_h in_info); // SingleShot::invoke() // SingleShot::getValue() -- 2.34.1