feat(onnxruntime-web): Allow the WASM backend to import the emscripten Module via a user-land defined loader #21430
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!