tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Wasm-simd-threaded backend stuck

Open lcycoding opened this issue 6 years ago • 18 comments

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): Not yet
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS 10.15.6
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow.js installed from (npm or script link):
  • TensorFlow.js version (use command below):
  • Browser version: Chrome 85.0.4183.121 / Canary 88.0.4283.0
  • Tensorflow.js Converter Version:

Describe the current behavior Hi tfjs developers, I've been trying wasm simd threaded backend on https://tfjs-wasm-simd-demo.netlify.app/ But under my environment (Macbook pro + Google Chrome), the demo will stuck after few seconds only when simd enabled. Since we're trying to build some feature based on wasm backend instead of webgl, is there any advice to stop the hanging problem?

Describe the expected behavior Wasm backend + SIMD + threaded mode work properly

lcycoding avatar Oct 06 '20 03:10 lcycoding

@lcycoding thanks for reporting , @annxingyuan I was able to reproduce the same behavior.

rthadur avatar Oct 06 '20 04:10 rthadur

Hi @lcycoding - are you saying that Wasm + SIMD + threaded works, but Wasm + SIMD + non-threaded does not? Also, does non-SIMD work for you?

Basically I'm trying to figure out which of the permutations of SIMD / threaded support is failing.

annxingyuan avatar Oct 06 '20 11:10 annxingyuan

@annxingyuan OK, here's the combination that I've tried

Macbook Pro + Google Chrome 85 Wasm + SIMD + Threaded -> Fail, hanged after few seconds(or minutes) Wasm + Non SIMD + Threaded -> Stable Wasm + Non SIMD + Non Threaded -> Stable

Windows PC + Google Chrome 85 Wasm + SIMD + Threaded -> Live longer than macbook, but will hang eventually Wasm + Non SIMD + Threaded -> Stable Wasm + Non SIMD + Non Threaded -> Stable

Linux PC + Google Chrome 85 Wasm + SIMD + Threaded -> Live longer than macbook, but will hang eventually Wasm + Non SIMD + Threaded -> Stable Wasm + Non SIMD + Non Threaded -> Stable

According to the above situation, I believe there might be something wrong with SIMD...

lcycoding avatar Oct 06 '20 12:10 lcycoding

Thanks so much @lcycoding - would you mind trying one more configuration: Wasm + SIMD + non-threaded?

annxingyuan avatar Oct 06 '20 12:10 annxingyuan

Hi @annxingyuan , Sorry didn't bring my macbook pro home, but I tried on Macbook Air Here's the result Wasm + SIMD + Threaded -> Fails immediately Wasm + SIMD + Non Threaded -> Seems stable, just tried few minutes Wasm + Non SIMD + Threaded -> Seems stable, just tried few minutes Wasm + Non SIMD + Non Threaded -> Seems stable, just tried few minutes

Thanks for your prompt reply!

lcycoding avatar Oct 06 '20 13:10 lcycoding

Hi @lcycoding - thanks again for reporting this - it may take us a bit to get to the bottom of the issue. In the meantime, I'm curious how you are toggling SIMD / thread support? Through Chrome flags?

Also, you may want to try our benchmarking page to test instead, it is a more stripped down environment.

annxingyuan avatar Oct 06 '20 13:10 annxingyuan

Yes, I'm toggling the switch through chrome flags. Will try the benchmark page, thanks!

lcycoding avatar Oct 06 '20 13:10 lcycoding

@annxingyuan Sorry for bothering, the benchmark page works fine. After finishing performance evaluation, we were trying to adopt wasm + SIMD + threaded on our side. But it comes out that the loading process of tfjs-backend-wasm-threaded-simd config will make our webapp hang. Keep receiving the following error callback

3292d53d-3d24-486e-9d64-eb899590fc44:1 ReferenceError: t is not defined at WasmBackendModuleThreadedSimd (5cf07358-5ca8-4459-a8dc-936e498a13ac:71) at onmessage (3292d53d-3d24-486e-9d64-eb899590fc44:1)

Any thought on this?

lcycoding avatar Oct 19 '20 07:10 lcycoding

Hi @lcycoding - hmm, it's tough to know where that error message is coming from - but given that the benchmark page works for you, I'm hoping this is an issue with setup. One thing to be aware of is that our WASM backend fetches a binary file after it's been loaded, and by default it will assume that the binary file is located in the same directory. So if you are serving the WASM backend yourself, you'll need to make sure to also serve the binary files. There's more information on this issue here and here.

Also curious whether wasm + SIMD + non-threaded has the same configuration issue?

annxingyuan avatar Oct 20 '20 11:10 annxingyuan

Hi @annxingyuan, thanks for the reply. Non-threaded configuration works well. Also, I've been setting the wasmPath to my own directory. I'll check with the information you given.

lcycoding avatar Oct 20 '20 13:10 lcycoding

I can replicate the issue as per the OP on the Netlify app running Chrome 86 on macOS 10.15.7 (19H15) - this crashes the tab completely.

I'm also experiencing a tab crash here: https://tensorflow.github.io/tfjs/e2e/benchmarks/local-benchmark/index.html

I've found that I have a similar problem on an app of my own using Blazeface 0.0.5 and TFJS 2.7.0, where I get another "o is not defined" error.

I'm also toggling threads and simd using Chrome flags.

Not sure whether it's a problem with Chrome, TFJS, the fact I'm running Blazeface which doesn't officially support 2.7.0 (so I assume it hasn't been tested with simd + threads, although "vanilla" WASM works great.)

willmorgan avatar Nov 18 '20 17:11 willmorgan

Hi,

We're having the same problem with our MobileNetV3 based model. We found that it's a lot more stable if we disable wasm threads support.

Is there any way to detect if the threads support is enabled in the code. Or better yet, disable it programmatically?

haowang1013 avatar Dec 16 '20 13:12 haowang1013

Hi @haowang1013 - you can check whether we've detected thread support by calling tf.env().get('WASM_HAS_MULTITHREAD_SUPPORT') (after the WASM backend has successfully initialized).

You could disable multithreading by setting WASM_HAS_MULTITHREAD_SUPPORT to false via tf.env().set('WASM_HAS_MULTITHREAD_SUPPORT', false). In general I would recommend not touching TFJS's environment flags, but I think since this is a somewhat experimental feature to begin with it makes sense to try this approach.

annxingyuan avatar Dec 16 '20 15:12 annxingyuan

I did some test with WASM_HAS_MULTITHREAD_SUPPORT and here are what I found:

Enable Experimental WebAssembly, WebAssembly SIMD support, WebAssembly threads support in chrome://flags:
  • stuck in tfjs-backend-wasm-threaded-simd.wasm without code change
  • set WASM_HAS_MULTITHREAD_SUPPORT to false after WASM backend successfully initialized, still stuck in tfjs-backend-wasm-threaded-simd.wasm
  • set 'WASM_HAS_MULTITHREAD_SUPPORT` to false before initialize WASM backend, works fine
Enable Experimental WebAssembly, set WebAssembly SIMD support and WebAssembly threads support to default:
  • WASM flag can be changed in code before initializing WASM and proper wasm backend will be loaded and works fine
Disable Experimental WebAssembly, set WebAssembly SIMD support and WebAssembly threads support to default:
  • change WASM flag in code before backend initialization will raise warnings and errors like this
wasm streaming compile failed: CompileError: WebAssembly.instantiateStreaming(): Compiling function #53 failed: invalid value type 's128', enable with --experimental-wasm-simd @+16497
(anonymous) @ tfjs-backend-wasm.js:9
tfjs-backend-wasm.js:9 falling back to ArrayBuffer instantiation
(anonymous) @ tfjs-backend-wasm.js:9
tfjs-backend-wasm.js:9 failed to asynchronously prepare wasm: CompileError: WebAssembly.instantiate(): Compiling function #53 failed: invalid value type 's128', enable with --experimental-wasm-simd @+16497
(anonymous) @ tfjs-backend-wasm.js:9
tfjs-backend-wasm.js:9 CompileError: WebAssembly.instantiate(): Compiling function #53 failed: invalid value type 's128', enable with --experimental-wasm-simd @+16497
tfjs-backend-wasm.js:9 CompileError: WebAssembly.instantiate(): Compiling function #53 failed: invalid value type 's128', enable with --experimental-wasm-simd @+16497
abort @ tfjs-backend-wasm.js:9
tfjs:17 Initialization of backend wasm failed
(anonymous) @ tfjs:17
tfjs:17 Make sure the server can serve the `.wasm` file relative to the bundled js file. For more details see https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/README.md#using-bundlers
(anonymous) @ tfjs:17

...

tfjs-backend-wasm.js:9 Uncaught (in promise) RuntimeError: abort(CompileError: WebAssembly.instantiate(): Compiling function #53 failed: invalid value type 's128', enable with --experimental-wasm-simd @+16497). Build with -s ASSERTIONS=1 for more info.
    at abort (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.js:4237:9407)
    at https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.js:4237:11161

wshuang808 avatar Dec 17 '20 04:12 wshuang808

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.

google-ml-butler[bot] avatar May 20 '22 16:05 google-ml-butler[bot]

Closing as stale. Please @mention us if this needs more attention.

google-ml-butler[bot] avatar May 27 '22 16:05 google-ml-butler[bot]

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar May 27 '22 16:05 google-ml-butler[bot]

tf.env().set('WASM_HAS_MULTITHREAD_SUPPORT', false) should set before set backend

vacing avatar Sep 28 '22 09:09 vacing