emscripten-core / emsdk

Emscripten SDK
http://emscripten.org
Other
2.98k stars 677 forks source link

wasm_cc_binary with tensorflow lite minimal example failed #1424

Open Yongle-Fu opened 1 month ago

Yongle-Fu commented 1 month ago

tensorflow lite minimal example

wasm_cc_binary(
    name = "wasm-minimal",
    cc_target = ":minimal",
)

tensorflow/tensorflow/lite/examples/minimal/BUILD:13:10: Linking tensorflow/lite/examples/minimal/minimal failed: (Exit 1): emcc_link.sh failed: error executing command (from target //tensorflow/lite/examples/minimal:minimal) external/emsdk/emscripten_toolchain/emcc_link.sh @bazel-out/wasm-opt-ST-b4dbd4a2bfc4/bin/tensorflow/lite/examples/minimal/minimal-2.params wasm-ld: error: --shared-memory is disallowed by bazel-out/wasm-opt-ST-b4dbd4a2bfc4/bin/tensorflow/lite/examples/minimal/_objs/minimal/minimal.o because it was not compiled with 'atomics' or 'bulk-memory' features. emcc: error: '/private/var/tmp/_bazel_yongle/fd2da45fc7b2786266a27e6e6c87cfd0/execroot/org_tensorflow/external/emscripten_bin_mac/bin/wasm-ld @/var/folders/sh/vmbfsw6x5fv2mn91h60r1jj00000gn/T/emscripten_qe8ad8zr.rsp.utf-8' failed (returned 1) Target //tensorflow/lite/examples/minimal:wasm-minimal failed to build

walkingeyerobot commented 1 month ago

The error message is complaining about atomics, so maybe it needs to be built with threads. Try adding threads = "emscripten", to your wasm_cc_binary

Yongle-Fu commented 1 month ago

Thank you very much. Build completed successfully now

Yongle-Fu commented 1 month ago

The error message is complaining about atomics, so maybe it needs to be built with threads. Try adding threads = "emscripten", to your wasm_cc_binary

I am using WebAssembly (Wasm) in a WeChat Mini Program environment, but it does not support Web Workers.

worker=new Worker(pthreadMainJs,workerOptions)

Would you like additional information or suggestions on how to handle WebAssembly in WeChat Mini Programs without Web Workers? @walkingeyerobot

like without option threads = "emscripten" in wasm_cc_binary,

thank you so much !~

walkingeyerobot commented 1 month ago

Are you able to get tensorflow lite to compile without threads outside of bazel and run in your environment? If yes, I'm happy to help dig into this further. If no, I think that's a question for hte tensorflow lite folks.

Yongle-Fu commented 1 month ago

Thank you very much for your help.

According to the advice from a TensorFlow Lite team member, I tried remove the -pthread lines in the emlink params file and ran emcc_link.sh. It was successful, although the generated wasm-loader.js is larger than usual. Are there any unfinished tasks that need to be completed after running emcc_link.sh?

generated wasm-loader.js file tensorflow isuse help which build wasm_cc_binary without pthread

mattsoulanille commented 1 month ago

Are you able to get tensorflow lite to compile without threads outside of bazel and run in your environment? If yes, I'm happy to help dig into this further. If no, I think that's a question for hte tensorflow lite folks.

This works internally in google3 (you can ping me in chat for a link) for building @tensorflow/tfjs-tflite, so I think the Bazel wasm tooling probably supports it. My guess is there's something misconfigured in the oss build, possibly before anything gets passed to wasm_cc_binary, that's causing it to use threads even when threads are not requested. If that's the case, then this is a TFLite issue.

Yongle-Fu commented 1 month ago

The error message is complaining about atomics, so maybe it needs to be built with threads. Try adding threads = "emscripten", to your wasm_cc_binary

I found a solution to fix the emcc_link issue by threads = "off" and adding -s USE_PTHREADS=0