facebookresearch / fastText

Library for fast text representation and classification.
https://fasttext.cc/
MIT License
25.87k stars 4.71k forks source link

Run WASM in web worker #1346

Open Sejmou opened 1 year ago

Sejmou commented 1 year ago

Hello,

I was wondering: should it be possible to run the web assembly version of fasttext in a web worker?

I tried using it inside a web worker myself, but got errors like this (logs copied from the developer console of my machine):

fasttext_wasm.js:2594 Compiled code throwing an exception, 5289056,30532,27
___cxa_throw @ fasttext_wasm.js:2594
$dynCall_viii @ fasttext_wasm.wasm:0xe5378
(anonymous) @ fasttext_wasm.js:2200
invoke_viii @ fasttext_wasm.js:9247
$func2482 @ fasttext_wasm.wasm:0x661dc
$dynCall_vii @ fasttext_wasm.wasm:0xe536a
(anonymous) @ fasttext_wasm.js:2200
invoke_vii @ fasttext_wasm.js:9269
$func4119 @ fasttext_wasm.wasm:0xa821b
$dynCall_viii @ fasttext_wasm.wasm:0xe5378
(anonymous) @ fasttext_wasm.js:2200
dynCall_viii_586 @ VM62:4
FastText$loadModel @ VM237:9
(anonymous) @ fasttext.js:79
Promise.then (async)
(anonymous) @ fasttext.js:78
loadModel @ fasttext.js:68
initModel @ index.ts:53
await in initModel (async)
predict @ index.ts:106
callback @ comlink.ts:329
Show 1 more frame
Show less
fasttext_wasm.js:2503 can_catch on 5289056
fasttext_wasm.js:2631 Resuming exception 5289056,5289056
fasttext_wasm.js:2503 can_catch on 5289056
fasttext_wasm.js:2631 

For reference: I built the wasm module as outlined here under the 'Fasttext WebAssembly integration' heading.

The main reason I even considered running fasttext in a web worker is that while I managed to load and run inference on a model on the main thread, the loadModel() function blocks (and hence freezes the UI) for around 2 to 3 seconds. I would really appreciate any workaround that would solve this issue.

Best regards, Samo

yunsii commented 9 months ago

Maybe my project https://github.com/yunsii/fasttext.wasm.js will help, it works in background script (worker) of browser extension.