[ML][single] SingleShot.setValue and SingleShot.getValue 69/253569/4
authorRafal Walczyna <r.walczyna@samsung.com>
Mon, 15 Feb 2021 14:02:14 +0000 (15:02 +0100)
committerRafal Walczyna <r.walczyna@samsung.com>
Tue, 16 Feb 2021 14:46:48 +0000 (15:46 +0100)
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 <r.walczyna@samsung.com>
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 2fdf571..503d1b1 100755 (executable)
@@ -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()
index 20c6ec8..cf9d01b 100644 (file)
@@ -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<int>(args.get(kId).get<double>());
+  const auto& name = args.get(kName).get<std::string>();
+  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<int>(args.get(kId).get<double>());
+  const auto& name = args.get(kName).get<std::string>();
+  const auto& value = args.get(kValue).get<std::string>();
+
+  auto ret = single_manager_.SetValue(id, name, value);
+  if (!ret) {
+    ReportError(ret, &out);
+    return;
+  }
 
-// SingleShot::setValue()
+  ReportSuccess(out);
+}
 
 // SingleShot::setTimeout()
 
index 36ff883..3d68464 100644 (file)
@@ -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
index 5f7438d..e21eaf2 100644 (file)
@@ -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()
 
index 2edbb8e..3482c3e 100644 (file)
@@ -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()
 
index 9a4c96c..7459242 100644 (file)
@@ -13,6 +13,7 @@
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
+#include <glib.h>
 #include <tizen.h>
 
 #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()
 
index 35c18b4..e456baf 100644 (file)
@@ -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()