From: Pawel Wasowski Date: Wed, 10 Mar 2021 12:38:22 +0000 (+0100) Subject: [ML][Pipeline] Invalidate CustomFilter arguments after return X-Git-Tag: submit/tizen/20210315.104011~3^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=94a5155b3bbe650b717b6c963364de8e48f683c1;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [ML][Pipeline] Invalidate CustomFilter arguments after return ACR: TWDAPI-274 This commit makes input and output of a JS CustomFilter act as if they were disposed after the callback's return. [Verification] Change was tested in Chrome DevTools with the snippets below. It works fine. var inputTI = new tizen.ml.TensorsInfo(); inputTI.addTensorInfo('ti1', 'UINT8', [4, 20, 15, 1]); var outputTI = new tizen.ml.TensorsInfo(); outputTI.addTensorInfo('ti1', 'UINT8', [1200]); var flattenAndSet123 = function(input, output) { console.log("Custom filter called"); var rawOutputData = new Uint8Array(1200); for (var i = 0; i < rawOutputData.length; ++i) { rawOutputData[i] = 123; } output.setTensorRawData(0, rawOutputData); console.log(input); return 0; } tizen.ml.pipeline.registerCustomFilter('testfilter2', flattenAndSet123, inputTI, outputTI, function errorCallback(error) { console.warn('custom filter error:') ; console.warn(error); }); var pipeline_def = "videotestsrc num-buffers=3 " + "! video/x-raw,width=20,height=15,format=BGRA " + "! tensor_converter " + "! tensor_filter framework=custom-easy model=testfilter2 " + "! appsink name=mysink"; var pipeline = tizen.ml.pipeline.createPipeline(pipeline_def, state => {console.log(state);}) pipeline.registerSinkListener('mysink', function(sinkName, data) { console.log('SinkListener for "' + sinkName + '" sink called'); console.log(data); }) // READY // Custom filter called // TensorsData {_id: 0, _tensorsInfo: TensorsInfo, _disposable: false} // PAUSED pipeline.start() // CustomFilter is called 3 times inputFromCustomFilter.getTensorRawData(0) inputFromCustomFilter.tensorsInfo outputFromCustomFilter.getTensorRawData(0) outputFromCustomFilter.tensorsInfo // The 4 lines above result in the following exception // WebAPIException {name: "AbortError", message: "TensorsData is disposed"} // as expected ////////////////////////////////////////////////////////////////////////// var inputTI = new tizen.ml.TensorsInfo(); inputTI.addTensorInfo('ti1', 'UINT8', [4, 20, 15, 1]); var inputTD = inputTI.getTensorsData() inputTD.dispose() inputTD.tensorsInfo inputTD.getTensorRawData(0) // The 2 lines above result in the following exception // WebAPIException {name: "AbortError", message: "TensorsData is disposed"} // as expected - the behavior of TensorsData.dispose() did not change Change-Id: Ib6c6afc4bb09ad2ce04bb85650b79868e9923b7b Signed-off-by: Pawel Wasowski --- diff --git a/src/ml/js/ml_common.js b/src/ml/js/ml_common.js index cb863c8f..649a214e 100755 --- a/src/ml/js/ml_common.js +++ b/src/ml/js/ml_common.js @@ -113,6 +113,18 @@ function _CheckIfTensorsDataNotDisposed(id) { } } +/* + * TensorsData objects invalidated by this function cannot be used + * in JS. However, this function does not release any resources allocated + * in C++ layer - if any action is needed in C++, another piece of + * code should do it (e.g. TensorsData::dispose()). + */ +function _InvalidateTensorsData(data) { + _ValidTensorsDataIds['delete'](data._id); + // underlying tensorsInfo_ is also invalid + _ValidTensorsInfoIds['delete'](data._tensorsInfo._id); +} + var TensorsData = function(id, tensorsInfoId, disposable) { Object.defineProperties(this, { count: { @@ -308,9 +320,8 @@ TensorsData.prototype.dispose = function() { if (native_.isFailure(result)) { return; } - _ValidTensorsDataIds['delete'](this._id); - // underlying tensorsInfo_ is also invalid - _ValidTensorsInfoIds['delete'](this._tensorsInfo._id); + + _InvalidateTensorsData(this); }; // TensorsInfo diff --git a/src/ml/js/ml_pipeline.js b/src/ml/js/ml_pipeline.js index f9a285fc..7673d813 100755 --- a/src/ml/js/ml_pipeline.js +++ b/src/ml/js/ml_pipeline.js @@ -838,6 +838,10 @@ MachineLearningPipeline.prototype.registerCustomFilter = function() { jsResponse.status = -1; } + // Ensuring that these TensorsData cannot be used outside of the callback + _InvalidateTensorsData(inputData); + _InvalidateTensorsData(outputData); + /* * Entering stage 3. */