loretoparisi / fasttext.js

FastText for Node.js
MIT License
192 stars 28 forks source link

Fail to load WASM error #35

Open brendantwy opened 1 year ago

brendantwy commented 1 year ago

Heya, I am currently facing this error when calling FastText.loadWASM():

TypeError: Failed to parse URL from /home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.wasm
/home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.js:230
      throw ex;
      ^

RuntimeError: abort(TypeError: Failed to parse URL from /home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.wasm) at Error
    at jsStackTrace (/home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.js:1937:19)
    at stackTrace (/home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.js:1954:16)
    at process.abort (/home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.js:1653:44)
    at process.emit (node:events:513:28)
    at emit (node:internal/process/promises:149:20)
    at processPromiseRejections (node:internal/process/promises:283:27)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)
    at process.abort (/home/brendan/internal-sourcing-tool/node_modules/fasttext.js/lib/wasm/fasttext_wasm.js:1659:11)
    at process.emit (node:events:513:28)
    at emit (node:internal/process/promises:149:20)
    at processPromiseRejections (node:internal/process/promises:283:27)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)

Node.js v18.15.0

Below is my code snippet, following the example given in this issue:

async function main() {
    const modelPath = path.resolve(__dirname, "../model");
    console.log(modelPath + `/300-dim-10-epoch.bin`);
    let FastText = require("fasttext.js");
    const ft =  new FastText({
        loadModel: modelPath + `/300-dim-10-epoch.bin`
    })
    try {
        await ft.loadWASM();
        const vec = ft.getWordVector("hello");
        console.log(vec);
    }catch(err){
        console.log(err);
    }
}

main();

There is no issues with my model directory and I tried running the snippet via the npm install package and git clone installation method.

brendantwy commented 1 year ago

Update

So apparently changing the current node version from 18.15.0 to 16.19.1 fixed it. No idea how node versions affect the lib.

Edit

Found the cause. Node 18 contains a new experimental fetch API. Disabling it with --no-experimental-fetch flag works as well

Sidenote

If your .bin model file is > 2Gb using the native fasttext wasm functions to load said model will fail. Very unfortunate :(

loretoparisi commented 1 year ago

amazing finding, thank you! I actually had the same error when released the last update few days ago, while the problem of 2GB is well-known due to Wasm 32bit architecture. Wasm 64bit is a work in progress and it should come in 2023. I will investigate a bit more node 18 and WASM and update the README.md

sneljo1 commented 1 year ago

Also having this issue regardless of node version. Also, the model used is very small so should also not be an issue. Seems to complain just when even only importing the module

loretoparisi commented 1 year ago

@sneljo1 do you have the error log from wasm module init?

nemphys commented 2 months ago

Downgrading to Node 16/18 in 2024 is not a viable option anymore, and the error is still there with Node 20. Is there a change that a proper solution is found for this issue?

loretoparisi commented 2 months ago

Downgrading to Node 16/18 in 2024 is not a viable option anymore, and the error is still there with Node 20.

Is there a change that a proper solution is found for this issue?

agreed, it should work on node ETS. I will investigate a bit more. There were some underlying changes in the WASM ABI and wasm modules support since Node 18 that requires some work.

nemphys commented 2 months ago

After commenting out

FastTextWasm = require('./wasm/fasttext')

everything seems to work fine, since wasm is not needed in my case, maybe it should be initialized only when needed.