microsoft/onnxruntime

[Web] use web worker importScripts load ort.min.js from CDN, but load local ort-wasm-simd.wasm file.

daassh opened this issue · 3 comments

daassh commented

Describe the issue

// worker.js
importScripts("https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js");

self.addEventListener("message", async (event) => {
  const session = await ort.InferenceSession.create('./model.onnx');
  // ...
});

importScripts with web worker in a website. It can't load corrent ort-wasm-simd.wasm

Expected loading

https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort-wasm-simd.wasm

Actually loading

http://localhost:3000/ort-wasm-simd.wasm

I'm new to onnx so I'm not sure if it's a bug.
I don't want to save ort-wasm-simd.wasm to the local public directory because of its large size. So I want load it from a public CDN. then I got this issue.

To reproduce

I just screate a repo to reproduce the question. https://github.com/daassh/test_webworker_onnx_cdn, this is a next.js project. to run it:

git clone https://github.com/daassh/test_webworker_onnx_cdn.git
cd test_webworker_onnx_cdn
npm install
npm run dev

open http://localhost:3000/ then into Developer Tools -> Network
refresh and then you can see load

Request URL: http://localhost:3000/ort-wasm-simd.wasm
Request Method: GET
Status Code: 404 Not Found

Urgency

No response

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js

Execution Provider

'wasm'/'cpu' (WebAssembly CPU)

looks like when ort.min.js being imported via importScripts(), it missed where it comes from. self.location.href not showing the correct URL. so you need to do this:

ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/";

before creating the first session in file test.worker.js.

If you know how to get its own URL via importScript() for worker, please let me know and I can do a change to onnxruntime-web so that no longer need the wasm path override

daassh commented

If you know how to get its own URL via importScript() for worker, please let me know and I can do a change to onnxruntime-web so that no longer need the wasm path override

I have checked the importScripts document. It only receive URLs as parameter. Then I have no idea for it.
Thank you for the solution, it solved my problem.