-
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
[js/web] optimize module export and deployment #20165
Conversation
@xenova, any feedback or insight is greatly appreciated |
It seems that In webpack we can use
EDIT (2024-04-28): Used a post-process script to workaround this. |
6a6383f
to
20c54f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving for admin
Is there a workaround for applications that do not produce esm modules for the browser? even if we disable the evaluation of |
If your project is not using ESM, webpack should be picking up the Please let us know if you see anything unexpected and share the reproduce step so we can try to debug. |
I think the problem is the project uses ESM as input format but it does not produce ESM as an output for the browser. The I forced it to use the I think it would be better if I can continue using the |
Note the
|
Setting importMeta: false is used to exactly fix this problem. |
Yes, my observation was about not being able able to specify wasmBinaries for it because it is not the .wasm file the problem. RecapA bit of a summary of what the issue is for me.
I will delay the update to the latest version until I have more time to look into the webpack configuration. I think some options are:
Again, thanks for the help! |
Webpack looks at a few different places to determine whether to load a library as UMD or ESM:
You may figure out that webpack may look at more places because there are various of use scenarios. Anyway, in your case, it decides to load onnxruntime-web as ESM. It's good so far.
Yes this is expected. It's good so far.
This is the problem. So far, the information does not tell whether the webpack is trying to create a UMD bundle or ESM bundle. I guess webpack tries to emit a ESM bundle. If so, when browser complain about that error message, you should make sure that your web app uses the bundle in ESM. Specifically, your HTML should include the bundle like this: <script src="your.bundle.min.js" type="module"></script> Please note the If Webpack tries to load dependency as ESM but output UMD.... This does not work for onnxruntime-web. It's not common that webpack uses different format for loading dependencies and output bundle.
Check the file change of
I am pretty sure Webpack can do both of them. The problem is the webpack documentation is amazing and it might take a while before you find the correct way to write the config. Good luck! |
Description
This PR make numbers of optimizations to onnxruntime-web's module export and deployment.
See each section below for more details.
Preview
Breaking changes
There is no code change required, but there are a few differences regarding code import, flags, bundler config and deployment steps.
Importing:
Import table is changed. See following for details.
Current import table:
ort
(default)onnxruntime-web
ort.all
onnxruntime-web/experimental
ort.node
onnxruntime-web
ort.training
onnxruntime-web/training
ort.wasm
onnxruntime-web/wasm
ort.wasm-core
onnxruntime-web/wasm-core
ort.webgl
onnxruntime-web/webgl
ort.webgpu
onnxruntime-web/webgpu
Proposed update:
ort
(default)onnxruntime-web
ort.all
onnxruntime-web/experimental
onnxruntime-web/all
ort.node
onnxruntime-web
ort.training
onnxruntime-web/training
ort.wasm
onnxruntime-web/wasm
ort.wasm-core
onnxruntime-web/wasm-core
❌❌✔️❌❌ort.webgl
onnxruntime-web/webgl
✔️❌ort.webgpu
onnxruntime-web/webgpu
Flags:
The following flags are deprecated:
env.wasm.simd
(boolean): will be ignored. SIMD is always enabled in build.The following flags changed their type:
env.wasm.wasmPaths
: When using this flag as a string ( for the URL prefix ), nothing is changed. When using this flag as an object ( for per-file path override ), the type changed:Bundler compatibility:
Config changes are need for bundlers. See usage example in /js/web/test/e2e/ for Webpack, parcel and rollup.
Deployment:
ort-*.wasm
andort-*.mjs
files (totally 6 files) in the dist folder. (previously only need to copyort-*.wasm
files.)Problems
There are a few problems with the current module export and deployment:
function.toString()
, which is unstable and error-prone.Goals
<script>
tag (IIFE format, exporting to global variableort
)<script type="module">
tag (ESM)NOTE: keeping single JavaScript file (the all-in-one bundle) is no longer a goal. This is because technically there is a conflict with the other requirements.
Important Design Decisions
Drop support of single JavaScript output.
Do not run parser/postprocess on Emscripten generated JavaScript.
_scriptDir
)Drop support for non-SIMD and non-MultiThread. This helps to reduce the number of artifacts in distribution.
Use ESM output for Emscripten generated JavaScript.
fetch()
andeval()
. Howevereval
is strongly suggested to be avoid because there is a great perf hit.import()
call. Considering ESM is widely supported in modern browsers and Node.js this is the better option.Add Blob based solution as a fallback for cross-origin workers.
fetch()
+Blob
to create a same-origin Blob URL.Distribution File Manifest
The distribution folder contains the following files:
WebAssembly artifacts. These files are the result of compiling the ONNX Runtime C++ code to WebAssembly by Emscripten.
ort-wasm-simd-threaded.wasm
--enable_wasm_simd
--enable_wasm_threads
ort-training-wasm-simd-threaded.wasm
--enable_training_apis
--enable_wasm_simd
--enable_wasm_threads
ort-wasm-simd-threaded.jsep.wasm
--enable_wasm_simd
--enable_wasm_threads
--use_jsep
--use_webnn
onnxruntime-web JavaScript artifacts. These files are generated by ESBuild as the entry point for onnxruntime-web.
There are multiple build targets for different use cases:
ort
onnxruntime-web
ort.all
onnxruntime-web/all
ort.node
onnxruntime-web
ort.training
onnxruntime-web/training
ort.wasm
onnxruntime-web/wasm
ort.webgl
onnxruntime-web/webgl
For each target, there are multiple files generated:
[target].min.js.map
[target].min.mjs.map
[target].proxy.min.mjs.map
Dynamic Import Explained
Local Served | No Proxy:
Local Served | Proxy:
Cross Origin | No Proxy:
Cross Origin | Proxy