-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[Web] How to use JSEP and WebGPU in static library (missing jsepAlloc or jsepInit) #23072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
JSEP does not work in ONNX Runtime WebAssembly static lib, because it depends on the inter-op between JS and C++ specifically defined for the JavaScript library. We are currently working on a C++ implemented WebGPU EP, which is technically able to work in wasm static lib. It is still being worked on. |
Thanks! |
Is there an alternative, for example if I use webgpu without JSEP? But this involves another component called "dawn" |
if you are building C++ targeting web (wasm), then your application should not depend the native implementation of dawn. instead, you should use the dawn's fork of Emscripten in order to compile the code that uses WebGPU API. However, as I explained above, we've not get to there yet. We will work on it but since that involves quite code and build script changes it may take a while. |
Thanks for the clear explanation @fs-eire, do you happen to have a rough estimate of when this is expected to be completed? |
it's about 3-4 months or so |
Looking forward for WebGPU being available as execution runner in the Emscripten environment! May I ask how is it possible that WebGPU is already available in PS: My running assumption is that |
Is this available now? #23364 (comment) |
Please track #23697 |
Not to be a bother, but it looks like this got more complicated than originally anticipated (judging by the PRs made that are relevant to WebGPU/CXX API). I'm wondering if I should instead focus on re-thinking my design to be able to use ORT's own javascript wrapper (with webgpu support) vs waiting for the ability to do a custom CXX WASM+WebGPU compilation. |
There are a lot of changes ongoing, including upstreams (dawn/emsdk). If you can use JavaScript, it would not be a bad idea to use ORT JavaScript API because even if we replace JSEP by WebGPU EP in future the JavaScript API is expected to still work. If you want to use WebGPU EP in WASM, it is functional working today in ORT web. However there are a few performance issues and I am working on it for a while. If you want to try I can help. |
Latest main branch should work for WebGPU EP or do I need a PR branch? Let me test it. |
Using this latest commit: d58ff6b I can compile static onnxruntime with these flags:
Result:
Then, I compile my CXX code which links against this library:
My cmake target:
|
you also need to include the following files:
Eventually these should be appended to ONNX Runtime Cmake file, but for now please add them as a workaround. I will do this change when I got some time |
With the additional complication of needing to append struct_info and enum_table to the js file, your instructions seemed to work:
Now, I do this:
There is no abort or exception or anything thrown, but the code silently returns after
So the part that prints "WebGPU is experimental on this platform" is this part of the js file of my wasm module, line 5850:
So, I'm not sure what's happening inside I've compiled it currently with |
Ah I see. You need to enable either Asyncify or JSPI settings in emscripten, because some webgpu APIs are async. (eg. requestAdapter) See https://emscripten.org/docs/porting/asyncify.html |
Update: false alarm, old: The results are totally mangled, so I have to dig deeper into host/device tensors etc. (my old code was written 100% around the CPU provider), but this is huge progress 🥳 Thanks for your help! |
Error is here:
Assert fails, follow the chain here:
To the assert higher up in the js wrapper:
If I print the webgpu tracked js objects:
Also, in the beginning, it seems many of the operations, layers, or kernels are not supported in WebGPU ep:
|
could you share the reproduce step? |
I will try to distill a MWE. Does it matter to you to have access to the ONNX format, or is ORT enough? I could create a small repo with:
If you consent, I would make it a private repo and invite you to it |
the ONNX format model is better for debugging. If the error is in If you still find it difficult to make a public sharable repo, you can make a private repo and invite me. |
Describe the issue
Hello,
I have C++ code that uses the ONNXRuntime CXX API which I compile against static onnxruntime wasm (
--build_wasm_static_lib
- full build commands are shown at the bottom). I link against the static lib in my CMakeLists.txt file like so:When building with onnxruntime only using simd, my code works fine. When building with webgpu support through jsep
and using the js execution provider in the C++ code:
This is the javascript webworker code which imports the wasm module:
I get an error when using my code, saying
Module.jsepAlloc is not a function
. This is printed by my wasm module in the Chrome developer console:I believe it's because I'm using the static lib which is missing the
jsepInit
step we find in the fileort-wasm-simd.jsep.mjs
is built with the non-static--build_wasm
. I have also read this guide https://gist.github.com/fs-eire/a55b2c7e10a6864b9602c279b8b75dceI tried to include
pre-jsep.js
in my link flags:It's probably missing other ts or js files. Any advice? @fs-eire sorry to ping you directly but you seem to be the WebGPU/JSEP implementor
Build commands:
Simd no webgpu:
Webgpu jsep:
To reproduce
Write custom C++ code using ORT CXX API, build onnxruntime WASM static lib with jsep and webgpu support, you might get a
Module.jsepAlloc is not a function
Urgency
No response
ONNX Runtime Installation
Built from Source
ONNX Runtime Version or Commit ID
deee480
Execution Provider
'webgpu' (WebGPU)
The text was updated successfully, but these errors were encountered: