From: Rafal Walczyna Date: Mon, 15 Feb 2021 14:02:14 +0000 (+0100) Subject: [ML][single] SingleShot.setValue and SingleShot.getValue X-Git-Tag: submit/tizen/20210217.032056~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2461f44f70c3d7a5db14f6308cfc3f062e42bfbb;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [ML][single] SingleShot.setValue and SingleShot.getValue ACR: TWDAPI-273 Test code: var m = tizen.ml.single.openModel('documents/mobilenet_v1_1.0_224_quant.tflite') var arr = ["input", "inputtype", "inputlayout", "output", "outputtype", "outputlayout", "accelerator", "is-updatable", "inputname", "outputname", "custom" ] arr.forEach((v) => { try { console.log(v, ':', m.getValue(v)); } catch (error) { console.log(v, ':', error.message) } }); m.setValue("input", "3:244:244:4"); console.log("input: " + m.getValue("input")) m.setValue("is-updatable", "true"); console.log("is-updatable: " + m.getValue("is-updatable")) [Verification] Built successful. Tested in Chrome Dev console. Change-Id: Id911ed1717fb7b96c7ac75350ea27c78786cc5d9 Signed-off-by: Rafal Walczyna --- diff --git a/src/ml/js/ml_single.js b/src/ml/js/ml_single.js index 2fdf5712..503d1b17 100755 --- a/src/ml/js/ml_single.js +++ b/src/ml/js/ml_single.js @@ -172,10 +172,79 @@ var SingleShot = function(id) { // SingleShot::invoke() -// SingleShot::getValue() +var GetSetValueValidExceptions = [ + 'AbortError', + 'InvalidValuesError', + 'NotSupportedError' +]; + +SingleShot.prototype.getValue = function() { + var args = validator_.validateArgs(arguments, [ + { + name: 'name', + type: types_.STRING + } + ]); + + if (!args.has.name) { + throw new WebAPIException( + WebAPIException.INVALID_VALUES_ERR, + 'Invalid parameter: name is undefined' + ); + } -// SingleShot::setValue() + var callArgs = { + id: this._id, + name: args.name + }; + + var result = native_.callSync('MLSingleShotGetValue', callArgs); + if (native_.isFailure(result)) { + throw native_.getErrorObjectAndValidate( + result, + GetSetValueValidExceptions, + AbortError + ); + } + return native_.getResultObject(result); +}; + +SingleShot.prototype.setValue = function() { + var args = validator_.validateArgs(arguments, [ + { + name: 'name', + type: types_.STRING + }, + { + name: 'value', + type: types_.STRING + } + ]); + + if (!args.has.name || !args.has.value) { + throw new WebAPIException( + WebAPIException.INVALID_VALUES_ERR, + 'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined' + ); + } + + var callArgs = { + id: this._id, + name: args.name, + value: args.value + }; + + var result = native_.callSync('MLSingleShotSetValue', callArgs); + + if (native_.isFailure(result)) { + throw native_.getErrorObjectAndValidate( + result, + GetSetValueValidExceptions, + AbortError + ); + } +}; // SingleShot::setTimeout() // SingleShot::close() diff --git a/src/ml/ml_instance.cc b/src/ml/ml_instance.cc index 20c6ec82..cf9d01b2 100644 --- a/src/ml/ml_instance.cc +++ b/src/ml/ml_instance.cc @@ -121,8 +121,8 @@ MlInstance::MlInstance() REGISTER_METHOD(MLSingleShotGetTensorsInfo); REGISTER_METHOD(MLSingleShotSetInputInfo); // SingleShot::invoke() - // SingleShot::getValue() - // SingleShot::setValue() + REGISTER_METHOD(MLSingleShotGetValue); + REGISTER_METHOD(MLSingleShotSetValue); // SingleShot::setTimeout() // SingleShot::close() @@ -726,9 +726,11 @@ const std::string kOutTensorsInfo = "outTensorsInfo"; const std::string kFwType = "fwType"; const std::string kHwType = "hwType"; const std::string kIsDynamicMode = "isDynamicMode"; +const std::string kValue = "value"; const int kNoId = -1; } // namespace + void MlInstance::MLSingleManagerOpenModel(const picojson::value& args, picojson::object& out) { ScopeLogger("args: %s", args.serialize().c_str()); CHECK_ARGS(args, kModelPath, std::string, out); @@ -852,9 +854,42 @@ void MlInstance::MLSingleShotSetInputInfo(const picojson::value& args, picojson: // SingleShot::invoke() -// SingleShot::getValue() +void MlInstance::MLSingleShotGetValue(const picojson::value& args, picojson::object& out) { + ScopeLogger("args: %s", args.serialize().c_str()); + CHECK_ARGS(args, kId, double, out); + CHECK_ARGS(args, kName, std::string, out); + + auto id = static_cast(args.get(kId).get()); + const auto& name = args.get(kName).get(); + std::string value; + auto ret = single_manager_.GetValue(id, name, value); + if (!ret) { + ReportError(ret, &out); + return; + } + + picojson::value val = picojson::value{value}; + ReportSuccess(val, out); +} + +void MlInstance::MLSingleShotSetValue(const picojson::value& args, picojson::object& out) { + ScopeLogger("args: %s", args.serialize().c_str()); + CHECK_ARGS(args, kId, double, out); + CHECK_ARGS(args, kName, std::string, out); + CHECK_ARGS(args, kValue, std::string, out); + + auto id = static_cast(args.get(kId).get()); + const auto& name = args.get(kName).get(); + const auto& value = args.get(kValue).get(); + + auto ret = single_manager_.SetValue(id, name, value); + if (!ret) { + ReportError(ret, &out); + return; + } -// SingleShot::setValue() + ReportSuccess(out); +} // SingleShot::setTimeout() diff --git a/src/ml/ml_instance.h b/src/ml/ml_instance.h index 36ff8834..3d684640 100644 --- a/src/ml/ml_instance.h +++ b/src/ml/ml_instance.h @@ -72,8 +72,8 @@ class MlInstance : public common::ParsedInstance { void MLSingleShotGetTensorsInfo(const picojson::value& args, picojson::object& out); void MLSingleShotSetInputInfo(const picojson::value& args, picojson::object& out); // SingleShot::invoke() - // SingleShot::getValue() - // SingleShot::setValue() + void MLSingleShotGetValue(const picojson::value& args, picojson::object& out); + void MLSingleShotSetValue(const picojson::value& args, picojson::object& out); // SingleShot::setTimeout() // SingleShot::close() // Single API end diff --git a/src/ml/ml_single_manager.cc b/src/ml/ml_single_manager.cc index 5f7438d3..e21eaf2e 100644 --- a/src/ml/ml_single_manager.cc +++ b/src/ml/ml_single_manager.cc @@ -107,8 +107,30 @@ PlatformResult SingleManager::SetNativeInputInfo(int id, TensorsInfo* inTensorsI } // SingleShot::invoke() -// SingleShot::getValue() -// SingleShot::setValue() +PlatformResult SingleManager::GetValue(int id, const std::string& name, std::string& value) { + ScopeLogger(); + + SingleShot* single = GetSingleShot(id); + if (!single) { + LoggerE("Could not find SingleShot handle"); + return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error"); + } + + return single->GetValue(name, value); +} + +PlatformResult SingleManager::SetValue(int id, const std::string& name, const std::string& value) { + ScopeLogger(); + + SingleShot* single = GetSingleShot(id); + if (!single) { + LoggerE("Could not find SingleShot handle"); + return PlatformResult(ErrorCode::ABORT_ERR, "Internal SingleShot error"); + } + + return single->SetValue(name, value); +} + // SingleShot::setTimeout() // SingleShot::close() diff --git a/src/ml/ml_single_manager.h b/src/ml/ml_single_manager.h index 2edbb8e7..3482c3e6 100644 --- a/src/ml/ml_single_manager.h +++ b/src/ml/ml_single_manager.h @@ -46,8 +46,8 @@ class SingleManager { PlatformResult GetNativeTensorsInfo(int id, bool get_input_mode, int* res_id); PlatformResult SetNativeInputInfo(int id, TensorsInfo* inTensorsInfo); // SingleShot::invoke() - // SingleShot::getValue() - // SingleShot::setValue() + 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() diff --git a/src/ml/ml_singleshot.cc b/src/ml/ml_singleshot.cc index 9a4c96cc..7459242c 100644 --- a/src/ml/ml_singleshot.cc +++ b/src/ml/ml_singleshot.cc @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include "common/logger.h" @@ -76,9 +77,38 @@ PlatformResult SingleShot::SetInputInfo(ml_tensors_info_h in_info) { // SingleShot::invoke() -// SingleShot::getValue() +PlatformResult SingleShot::GetValue(const std::string& name, std::string& value) { + ScopeLogger(); + + gchar* out_value = nullptr; + int ret = ml_single_get_property(handle_, name.c_str(), &out_value); + if (ML_ERROR_NONE != ret) { + LoggerE("ml_single_get_property failed: %d (%s)", ret, get_error_message(ret)); + return util::ToPlatformResult(ret, "Failed to get value"); + } + + if (nullptr == out_value) { + LoggerW("ml_single_get_property returned nullptr, returning empty string instead"); + // set value to an empty string as ml_single_get_property returned ML_ERROR_NONE + value = std::string{}; + } else { + value = std::string{out_value}; + g_free(out_value); + } + + return PlatformResult{}; +} + +PlatformResult SingleShot::SetValue(const std::string& name, const std::string& value) { + ScopeLogger(); + int ret = ml_single_set_property(handle_, name.c_str(), value.c_str()); + if (ML_ERROR_NONE != ret) { + LoggerE("ml_single_set_property failed: %d (%s)", ret, get_error_message(ret)); + return util::ToPlatformResult(ret, "Failed to set value"); + } -// SingleShot::setValue() + return PlatformResult{}; +} // SingleShot::setTimeout() diff --git a/src/ml/ml_singleshot.h b/src/ml/ml_singleshot.h index 35c18b4f..e456baf8 100644 --- a/src/ml/ml_singleshot.h +++ b/src/ml/ml_singleshot.h @@ -45,9 +45,8 @@ class SingleShot { PlatformResult SetInputInfo(ml_tensors_info_h in_info); // SingleShot::invoke() - // SingleShot::getValue() - - // SingleShot::setValue() + PlatformResult GetValue(const std::string& name, std::string& value); + PlatformResult SetValue(const std::string& name, const std::string& value); // SingleShot::setTimeout()