tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.35k stars 1.92k forks source link

Cannot read properties of undefined (reading '_malloc') #8153

Open pkesseli opened 7 months ago

pkesseli commented 7 months ago

System information

I encountered this issue https://github.com/tensorflow/tfjs/issues/7893 when using @tensorflow/tfjs-tflite@0.0.1-alpha.10. So I regressed to @tensorflow/tfjs-tflite@0.0.1-alpha.9, as suggested in the issue. As a consequence of this downgrade, I had to use @tensorflow/tfjs@3.20.0, as illustrated in https://github.com/tensorflow/tfjs/issues/7789.

With this setup, I tried to load a .tfilte model as follows:

const modelContent: ArrayBuffer = await modelDownloadWithProgress.arrayBuffer();
console.log(`features: ${JSON.stringify(await getWasmFeatures())}`);
const model: TFLiteModel = await loadTFLiteModel(modelContent)
console.log(`model: ${model}`);

The output is the following:

features: {"simd":true,"multiThreading":false}
tflite_web_api_client.js:2710 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_malloc')
    at $jscomp.generator.Engine_.eval [as program_] (tflite_web_api_client.js:2710:245)
    at $jscomp.generator.Engine_.nextStep_ (tflite_web_api_client.js:31:105)
    at $jscomp.generator.Engine_.next_ (tflite_web_api_client.js:27:234)
    at $jscomp.generator.Generator_.next (tflite_web_api_client.js:32:73)
    at b (tflite_web_api_client.js:33:73)

The referenced line is the following:

module$exports$google3$third_party$tensorflow_lite_support$web$tflite_web_api_client.TFLiteWebModelRunner.create = function(a, b) {
    b = void 0 === b ? {} : b;
    var c, d, e, f, g, h, k, l, m, n, p, q, r;
    return $jscomp.asyncExecutePromiseGeneratorProgram(function(t) {
        if (1 == t.nextAddress)
            return c = module$exports$google3$third_party$tensorflow_lite_support$web$task$codegen$common$emscripten_module_loader.EmscriptenModuleLoader.getInstance(module$contents$google3$third_party$tensorflow_lite_support$web$tflite_web_api_client_wasmModulesPath, "tflite_web_api", '"use strict";var Module={};var initializedJS=false;var pendingNotifiedProxyingQueues=[];function threadPrintErr(){var text=Array.prototype.slice.call(arguments).join(" ");console.error(text)}function threadAlert(){var text=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:text,threadId:Module["_pthread_self"]()})}var err=threadPrintErr;self.alert=threadAlert;Module["instantiateWasm"]=(info,receiveInstance)=>{var instance=new WebAssembly.Instance(Module["wasmModule"],info);receiveInstance(instance);Module["wasmModule"]=null;return instance.exports};self.onunhandledrejection=e=>{throw e.reason??e};self.onmessage=e=>{try{if(e.data.cmd==="load"){Module["wasmModule"]=e.data.wasmModule;Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob=="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}tflite_web_api_ModuleFactory(Module).then(function(instance){Module=instance})}else if(e.data.cmd==="run"){Module["__performance_now_clock_drift"]=performance.now()-e.data.time;Module["__emscripten_thread_init"](e.data.pthread_ptr,0,0,1);Module["establishStackSpace"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInitTLS();if(!initializedJS){Module["__embind_initialize_bindings"]();pendingNotifiedProxyingQueues.forEach(queue=>{Module["executeNotifiedProxyingQueue"](queue)});pendingNotifiedProxyingQueues=[];initializedJS=true}try{Module["invokeEntryPoint"](e.data.start_routine,e.data.arg)}catch(ex){if(ex!="unwind"){if(ex instanceof Module["ExitStatus"]){if(Module["keepRuntimeAlive"]()){}else{Module["__emscripten_thread_exit"](ex.status)}}else{throw ex}}}}else if(e.data.cmd==="cancel"){if(Module["_pthread_self"]()){Module["__emscripten_thread_exit"](-1)}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="processProxyingQueue"){if(initializedJS){Module["executeNotifiedProxyingQueue"](e.data.queue)}else{pendingNotifiedProxyingQueues.push(e.data.queue)}}else{err("worker.js received unknown command "+e.data.cmd);err(e.data)}}catch(ex){if(Module["__emscripten_thread_crashed"]){Module["__emscripten_thread_crashed"]()}throw ex}};\n'),
            d = $jscomp,
            e = d.makeIterator,
            t.yield(Promise.all([c.load(), "string" === typeof a ? fetch(a).then(function(u) {
                return u.arrayBuffer()
            }) : Promise.resolve(a)]), 2);
        f = e.call(d, t.yieldResult);
        g = f.next().value;
        h = f.next().value;
        k = g;
        l = h;
        m = new Uint8Array(l);
        n = k._malloc(m.length); // <== k is undefined
        k.HEAPU8.set(m, n);

Perhaps this is a result of using a woefully outdated TensorFlow.js version, so my question would be:

Thanks for any suggestions you can provide!

gaikwadrahul8 commented 7 months ago

Hi, @pkesseli

I apologize for the delayed response and could you please help us with your Github repo or sample code snippet (along with TensorFlow Lite model (.tflite file)) with complete steps to replicate the same behavior from our end ? Thank you for your cooperation and patience.

pkesseli commented 7 months ago

Hi @gaikwadrahul8,

Thanks for your response. I created a minimal repro that you can download here: https://1drv.ms/f/s!AgTGrw38VMRagvcARXTu4TVM12H8BQ?e=pGg5TE

Steps to reproduce:

npm install
npm run dev

If you browse http://localhost:3000 and look at the development console, you should see the following output:

Starting model creation...
model.ts:17 features: {"simd":true,"multiThreading":false}
tflite_web_api_client.js:2710 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_malloc')
    at $jscomp.generator.Engine_.eval [as program_] (tflite_web_api_client.js:2710:245)
    at $jscomp.generator.Engine_.nextStep_ (tflite_web_api_client.js:31:105)
    at $jscomp.generator.Engine_.next_ (tflite_web_api_client.js:27:234)
    at $jscomp.generator.Generator_.next (tflite_web_api_client.js:32:73)
    at b (tflite_web_api_client.js:33:73)
eval @ tflite_web_api_client.js:2710
$jscomp.generator.Engine_.nextStep_ @ tflite_web_api_client.js:31
$jscomp.generator.Engine_.next_ @ tflite_web_api_client.js:27
next @ tflite_web_api_client.js:32
b @ tflite_web_api_client.js:33
pkesseli commented 7 months ago

Apologies, the link expired today. Renewed it by another two weeks.

pkesseli commented 6 months ago

Created a link for direct download that will last a year: https://1drv.ms/u/s!AgTGrw38VMRagvcBOf-wIlvCf49Y1A

gaikwadrahul8 commented 6 months ago

Hi, @pkesseli

I sincerely apologize for the delayed response and thank you for sharing the workable link and now I'm able to replicate the same behaviour from my end also so we'll have to dig more into this issue and will update you soon. thank you for bringing this issue to our attention.

image

I also tried to replicate the same issue with latest version @tensorflow/tfjs-tflite@0.0.1-alpha.10 with peer dependency @tensorflow/tfjs@4.9.0 but I'm getting this error message Module not found: Can't resolve './tflite_web_api_client'

Thank you for your understanding and patience.

mittalmohit commented 3 months ago

Hello everyone i am using tensor flow lite version @tensorflow/tfjs-tflite@0.0.1-alpha.9 with peer dependency @tensorflow/tfjs@4.9.0.

also i am trying to load model like: const tfliteModel = await tflite.loadTFLiteModel('assets/models/litemodal.tflite'); console.log(tfliteModel)

But getting error:

Cannot read properties of undefined (reading '_malloc')

please help

sb2702 commented 3 months ago

I had the exact same issue as @pkesseli, to the line. I was loading the libraries from a worker module environment (though I think the module environment was the key here)

When trying to run tflite.loadModel, I observed no network requests to load the tflite_web_api_cc.js modules, and other files in the remote wasm folder, nor when manually specifying tflite.setWasmPath(), neither specifying the remote path nor specifying a local path with the same files hosted seem to work.

I know the model works because I use the same exact model with the same exact library files, to the version, when loaded directly from cdn in a script tag. I'm guessing the libraries by default assume loading from CDN as I've had other errors in the past working with tfjs-lite from npm, but I have never had issues using tflite-tfjs when loading from cdn.

An unelegant workaround that I found was to create a worker that can load the scripts directly from CDN, that seems to work

Parent:

 const worker = new Worker(new URL(`@/my_path/tfjs_worker.js', import.meta.url));

 worker.postMessage('Ping from parent');

Worker

importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core');
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu');
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite@0.0.1-alpha.10')

self.onmessage = async function(event) {

    const url = 'http://localhost:port/self/hosted/path';

    tflite.setWasmPath('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite@0.0.1-alpha.10/wasm/')

    const model = await tflite.loadTFLiteModel(url)

    console.log(model)

};

The above seems to work for me. It doesn't work if you don't specify the remote setWasmPath

If it's helpful for debugging, I was using a vite build system for a vue application, and the tfjs versions were:

I also tried @tensorflow/tfjs-tflite: 0.0.1-alpha.9 and that also had the same issue

mittalmohit commented 3 months ago

Thank you @sb2702 . I am able to successfully load model after setWasmPath

tflite.setWasmPath('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tflite@0.0.1-alpha.10/wasm/')

pkesseli commented 2 months ago

Thanks @mittalmohit, that does resolve my issue. However, as mentioned above, due to issue https://github.com/tensorflow/tfjs/issues/7893 I was forced to use @tensorflow/tfjs-tflite@0.0.1-alpha.9. Did you actually get @tensorflow/tfjs-tflite@0.0.1-alpha.10 to work? If so, how did you fix https://github.com/tensorflow/tfjs/issues/7893?