microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.12k stars 2.84k forks source link

[Web] Previously used .wasm binaries missing in 1.19.0 (using web bundler with CopyPlugin) #21811

Open e-simpson opened 3 weeks ago

e-simpson commented 3 weeks ago

Describe the issue

Using a onnxruntime-web with a web bundler like CopyPlugin I am now getting Package path ./webgpu is not exported from package. When I manually try to bundle the .wasm's I get Unable to locate '..node_modules/onnxruntime-web/dist/ort-wasm.wasm' glob.

I assume the names changed with this update as I see the web bundling changed. I see now bundling was introduced with https://github.com/microsoft/onnxruntime/commit/dd805ff77d896e9ff5457818c49d2103d6ba6248. I looked for some migration docs but couldn't find any.

To reproduce

Can follow the current quick-start https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js/quick-start_onnxruntime-web-bundler

or

  1. Use next.js or similar web bundler/pack framework
  2. Use webpack copy plugin to load onnxruntime-web. Example for next.js:
    
    /** @type {import('next').NextConfig} */
    import CopyPlugin from "copy-webpack-plugin";

const nextConfig = { webpack: (config) => { config.resolve.extensions.push(".ts", ".tsx"); config.resolve.fallback = { fs: false };

config.plugins.push(
  new CopyPlugin({
    patterns: [
      {
        from: './node_modules/onnxruntime-web/dist/ort-wasm.wasm',
        to: 'static/chunks/pages',
      },
      {
        from: './node_modules/onnxruntime-web/dist/ort-wasm-simd.wasm',
        to: 'static/chunks/pages',
      },
      { 
        from: './node_modules/onnxruntime-web/dist/ort-wasm-simd.jsep.wasm',
        to: 'static/chunks/pages',
      },
      ],
    }),
);

return config;

} }

export default nextConfig;

3. Import onnxruntime-web/webgpu with `import * as ort from 'onnxruntime-web/webgpu';`
4. Run the dev server, example: `bun run dev`

I get the error: 

Module not found: Package path ./webgpu is not exported from package .../node_modules/onnxruntime-web



### Urgency

Not urgent, rolled back to 1.18.0 for now.

### ONNX Runtime Installation

Released Package

### ONNX Runtime Version or Commit ID

1.19.0

### Execution Provider

'webgpu' (WebGPU)
gotomypc commented 3 weeks ago

The solution to fix this, can be npm install onnxruntime-web@1.14.0, all fix the package.json, lock the onnxruntime-web to 1.14.0 version.

e-simpson commented 3 weeks ago

@gotomypc I use 1.18.0 currently which works great. I was just looking to use some new features from 1.19.0😊

fs-eire commented 2 weeks ago

There are multiple changes for onnxruntime-web exports.

a TL;DR version:

Just 2 .wasm files. copy the one you uses. See this link to check out options for importing.

Let me know if you have further questions

fs-eire commented 2 weeks ago

work item: examples should be updated.

e-simpson commented 2 weeks ago

@fs-eire thanks for the reply!

I copied the ort-wasm-simd-threaded.jsep.wasm into my static files, made sure I'm using 1.19.0 and importing with import * as ort from 'onnxruntime-web/webgpu'; I get the error:

Module not found: Package path ./webgpu is not exported from package 
.../node_modules/onnxruntime-web (see exports field in .../node_modules/onnxruntime-web/package.json)
> 1 | import * as ort from 'onnxruntime-web/webgpu';
    | ^
  2 |

Strange. I tried to copy the ort-wasm-simd-threaded.wasm and use the base non-gpu version and nothing seems to be working. If I revert back to 1.18.0 everything seems to work.

fs-eire commented 2 weeks ago

@fs-eire thanks for the reply!

I copied the ort-wasm-simd-threaded.jsep.wasm into my static files, made sure I'm using 1.19.0 and importing with import * as ort from 'onnxruntime-web/webgpu'; I get the error:

Module not found: Package path ./webgpu is not exported from package 
.../node_modules/onnxruntime-web (see exports field in .../node_modules/onnxruntime-web/package.json)
> 1 | import * as ort from 'onnxruntime-web/webgpu';
    | ^
  2 |

Strange. I tried to copy the ort-wasm-simd-threaded.wasm and use the base non-gpu version and nothing seems to be working. If I revert back to 1.18.0 everything seems to work.

What bundler (and version) do you use? It looks like that your bundler is not reading from the "export" field of onnxruntime-web's package.json

e-simpson commented 2 weeks ago

@fs-eire Bundling with webpack 5.93.0 (what the latest Next.js comes with)

Yeah, and I see the 1.19.0 package exports for web-gpu

    "./webgpu": {
      "node": null,
      "import": "./dist/ort.webgpu.bundle.min.mjs",
      "require": "./dist/ort.webgpu.min.js",
      "types": "./types.d.ts"
    },

For reference the web-gpu exports for 1.18.0

    "./webgpu": {
      "import": "./dist/esm/ort.webgpu.min.js",
      "require": "./dist/cjs/ort.webgpu.min.js",
      "types": "./types.d.ts",
      "default": "./dist/ort.webgpu.min.js"
    },

Let me give turbopack a try

fs-eire commented 2 weeks ago

Since it's Next.js, make sure not using SSR (because importing 'onnxruntime-web/webgpu' from Node.js is explicitly disabled)