onnxruntime: [Web] ORT format model not working on WebGPU EP + Wasm Static lib
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:
set(ONNX_RUNTIME_WASM_LIB_WEBGPU ${CMAKE_SOURCE_DIR}/../build/build-ort-wasm/Release/libonnxruntime_webassembly.a)
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:
// set provider to WebGPU via "JsExecutionProvider"
session_options.AppendExecutionProvider("JS", {});
This is the javascript webworker code which imports the wasm module:
function loadWASMModule() {
// Check if WebGPU is supported
const webGPUSupported = isWebGPUSupported();
let scriptToImport = "";
//const scriptToImport = webGPUSupported ? 'demucs_onnx_webgpu.js' : 'demucs_onnx_simd.js';
if (webGPUSupported) {
console.log("WebGPU is supported, using WebGPU module");
scriptToImport = "demucs_onnx_webgpu.js";
} else {
console.log("WebGPU is not supported, using SIMD module");
scriptToImport = "demucs_onnx_simd.js";
}
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:
WebGPU is supported, using WebGPU module
demucs_onnx_webgpu.js:9 Using JsExecutionProvider
demucs_onnx_webgpu.js:9 JsExecutionProvider
demucs_onnx_webgpu.js:9 Uncaught (in promise) TypeError: Module.jsepAlloc is not a function
at 738632 (demucs_onnx_webgpu.js:9:11657)
at runEmAsmFunction (demucs_onnx_webgpu.js:9:89035)
at _emscripten_asm_const_ptr (demucs_onnx_webgpu.js:9:89275)
at demucs_onnx_webgpu.wasm:0x2877e6
I believe it’s because I’m using the static lib which is missing the jsepInit step we find in the file ort-wasm-simd.jsep.mjs is built with the non-static --build_wasm. I have also read this guide https://gist.github.com/fs-eire/a55b2c7e10a6864b9602c279b8b75dce
I tried to include pre-jsep.js in my link flags:
LINK_FLAGS "${COMMON_LINK_FLAGS} -s USE_WEBGPU=1 \
-s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=2048mb -s MAXIMUM_MEMORY=4gb \
--pre-js \"${ONNXRUNTIME_ROOT}/wasm/pre-jsep.js\""
)
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:
python ./vendor/onnxruntime/tools/ci_build/build.py \
--build_dir="./build/build-ort-wasm-simd" \
--config=MinSizeRel \
--build_wasm_static_lib \
--parallel \
--minimal_build \
--disable_ml_ops \
--disable_rtti \
--use_preinstalled_eigen \
--eigen_path=$(realpath "./vendor/eigen") \
--skip_tests \
--skip_onnx_tests \
--enable_wasm_simd \
--enable_wasm_exception_throwing_override \
--disable_exceptions \
--include_ops_by_config="./onnx-models/required_operators_and_types.config" \
--enable_reduced_operator_type_support
;;
Webgpu jsep:
python ./vendor/onnxruntime/tools/ci_build/build.py \
--build_dir="./build/build-ort-wasm-webgpu" \
--config=Release \
--build_wasm_static_lib \
--parallel \
--use_preinstalled_eigen \
--eigen_path=$(realpath "./vendor/eigen") \
--skip_tests \
--skip_onnx_tests \
--enable_wasm_simd \
--use_jsep \
--include_ops_by_config="./onnx-models/required_operators_and_types.config" \
--enable_reduced_operator_type_support
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)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 34 (32 by maintainers)
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.
it’s about 3-4 months or so
the ONNX format model is better for debugging.
If the error is in
_wgpuDeviceCreateBindGroup, it’s probably not even need the full model to reproduce the issue. So, if possible, please use public repo because it will help for tracking and reference in future. (maybe you can use a public model to reproduce the issue?)If you still find it difficult to make a public sharable repo, you can make a private repo and invite me.
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.
Please track #23697
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.
If ONNX format models works for you, I think it is good for now.
I guess dynamic quantization and ORT format may be not fully compatible with Webgpu now
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
you also need to include the following files:
--js-library <path-to-library_webgpu.js>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
Using this latest commit: https://github.com/microsoft/onnxruntime/commit/d58ff6b9db9ec21db1be51874b4a5ba4787a6071
I can compile static onnxruntime with these flags:
Result:
Then, I compile my CXX code which links against this library:
My cmake target:
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.
Is this available now? https://github.com/microsoft/onnxruntime/pull/23364#issuecomment-2594348897