-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
feat(onnxruntime-web): Allow the WASM backend to import the emscripten Module via a user-land defined loader #21430
base: main
Are you sure you want to change the base?
Conversation
@microsoft-github-policy-service agree |
looking |
Alongside this PR and solution, I see potential to improve on the complexities that come with the current WASM runtime code: The idea would be, to, as part of another, new PR, enhance the current build system for the emscripten based runtime files. I'm imagining additional runtime files that bundle the WASM binary using inline data URI's, so that there is no need to dynamically load these binary files at all, reducing complexities with runtime environments and bundlers downstream. The file size of these additional runtime file variants would roughly be the size of the current JS runtime + (WASM runtime size * ~1,33). So the initial file size increases, but as the runtime JS files are usually dynamically loaded, this shouldn't be an issue. Additional benefits of this approach would be, that all code regarding WASM file loading can be removed entirely from the emscripten-generated JS runtime. All the imports from Finally, the top-level await issue downstream libraries and users suffer from, is only relevant for Node.js environments. Why not build dedicated runtimes for the web platform and server-side JS? I demonstrate a quite "hacky" and pragmatic approach here: https://github.com/kyr0/fast-dotproduct/blob/main/scripts/build.ts#L51 Proposal (for another PR):
One con of those new runtime variants would be that |
The story of wasm loading for onnxruntime-web is a little bit complicated. Let's go to the conclusion (the current status) first: For
For
========================= Now answers to the questions: Q: Using a huge data URL for onnxruntime webassembly binary to avoid dynamic loading of
Q: Why not build dedicated runtimes for the web platform and server-side JS?
|
Description
This minimal change will allow defining a custom loader for the WASM backend to be passed via
flags.importWasmModule
(akaenv
), allowing for Inversion of Control (IoC) and increased flexibility in loading the WASM backend. The change is minimally invasive.Motivation and Context
As described at length in #20876, and confirmed by other users as well, specifically @asadm the current WASM backend implementation suffers from the inflexibility of not being able to load the WASM module in user-land code. This leads to issues in contexts like Web Extensions, specifically their background scripts, where dynamic loading isn't possible, as it is prohibited by W3C standards. The issue expands to other areas where complexities in downstream build processes lead to the loader code not being able to load the Module and, in effect, to the WASM backend not being available.
Considerations
Regarding this implementation, I'm concerned that the type import from
onnxruntime-web
toonnxruntime-common
(for theEnv
interface) which imports the typeOrtWasmModule
fromonnxruntime-web
leads to a cyclic dependency and a package boundary issue. This isn't an issue at runtime, as it only affects the type system, but it's clearly not what I'd wish to see in my projects. Maybe it would be a good idea to move the specificEnv
-extending type definitions of the WASM backend implementation into theonnxruntime-web
package, or theOrtWasmModule
intocommon
? Duplicate type definitions aren't a good idea either, nor isany
a good idea.As this is my first PR to this repository and as I'm not familiar with the processes, I'm kindly asking for advice. Please guide me in finishing this PR. Thank you in advance!