[ML][Single] Fix invokeAsync 28/262228/1
authorPawel Wasowski <p.wasowski2@samsung.com>
Wed, 4 Aug 2021 14:49:28 +0000 (16:49 +0200)
committerPawel Wasowski <p.wasowski2@samsung.com>
Wed, 4 Aug 2021 14:49:28 +0000 (16:49 +0200)
invokeAsync implementation did not should copy the input TensorsData
object, as it was supposed to. This commit adds copying

[Verification] Tested with the snippet below several times - the outputs
of invoke and invokeAsync were always identical:

var tensorsData;
var model;

function errorCallback(error)
{
    console.log("Error during invokeAsync: " + error.message);
}

function isSameArray(a, b) {
    if(a.length != b.length) {
        return false;
    }
    for(var i=0 ; i<a.length ; i++) {
        if(a[i] != b[i]) {
            return false;
        }
    }
    return true;
}

function successCallback(tensorsDataOut)
{
    var asyncResult = tensorsDataOut.getTensorRawData(0).data;
    var syncResultTD = model.invoke(tensorsData);
    var syncResult = syncResultTD.getTensorRawData(0).data;

    if (isSameArray(syncResult, asyncResult)) {
        console.log('Sync and async versions of invoke returned the same output');
    } else {
        console.error('Outputs of sync and async invoke versions differ!');
    }
}

function run_test () {
    model = tizen.ml.single.openModel("wgt-package/models/mobilenet_v1_1.0_224_quant.tflite", null, null, "TENSORFLOW_LITE", "ANY");

    var tensorsInfo = new tizen.ml.TensorsInfo();
    tensorsInfo.addTensorInfo("tensor", "UINT8", [224, 224, 3]);
    tensorsData = tensorsInfo.getTensorsData();
    var randomData = Array.from({length: 224 * 224 * 3}, () => Math.floor(Math.random() * 255));
    var rgb = new Uint8Array(randomData);
    tensorsData.setTensorRawData(0, rgb);
    model.invokeAsync(tensorsData, successCallback, errorCallback);
};

Change-Id: Iff0d39ca6be9e1c5d2ff3c9f9fc3d061d49377c5

src/ml/ml_instance.cc

index 5cd456c..27f74b0 100644 (file)
@@ -896,7 +896,8 @@ void MlInstance::MLSingleShotInvoke(const picojson::value& args, picojson::objec
   if (async && in_tensors_data) {
     // in case of async flow need to prevent destroying entry data during invoke
     // from JS, creation of a copy
-    in_tensors_data = GetTensorsInfoManager().CreateTensorsData(in_tensors_data->GetTensorsInfo());
+    in_tensors_data = GetTensorsInfoManager().CloneNativeTensorWithData(
+        in_tensors_data->GetTensorsInfo()->Handle(), in_tensors_data->Handle());
   }
   if (nullptr == in_tensors_data) {
     LogAndReportError(PlatformResult(ErrorCode::ABORT_ERR, "Internal TensorsData error"), &out,