// 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,
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 */
// MachineLearningSingle::openModelAsync()
// OpenModelSuccessCallback
REGISTER_METHOD(MLSingleShotGetTensorsInfo);
+ REGISTER_METHOD(MLSingleShotSetInputInfo);
// SingleShot::invoke()
// SingleShot::getValue()
// SingleShot::setValue()
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<int>(args.get(kId).get<double>());
+ auto inTensorId = static_cast<int>(args.get(kInTensorsInfo).get<double>());
+
+ 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()
// 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()
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()
// 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()
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()
~SingleShot();
PlatformResult GetTensorsInfo(bool get_input_mode, ml_tensors_info_h* result);
-
+ PlatformResult SetInputInfo(ml_tensors_info_h in_info);
// SingleShot::invoke()
// SingleShot::getValue()