Skip to content

Instantly share code, notes, and snippets.

@gabrielgrant
Last active May 24, 2024 18:51
Show Gist options
  • Save gabrielgrant/cb3e072dec5a416b4fc24f18ae902fb7 to your computer and use it in GitHub Desktop.
Save gabrielgrant/cb3e072dec5a416b4fc24f18ae902fb7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<header>
<title>ONNX Runtime JavaScript example with WebGPU</title>
</header>
<body>
<script type="module">
// see also advanced usage of importing ONNX Runtime Web:
// https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js/importing_onnxruntime-web
// import ONNXRuntime Web from CDN
import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/esm/ort.webgpu.min.js";
// set wasm path override (not sure why this is still needed for webgpu backend? as fallback for missing kernels?)
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/";
// use an async context to call onnxruntime functions.
async function main() {
try {
// create a new session and load the model.
const modelPath = 'https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/onnx/vae_encoder/model.onnx?download=true'
const session = await ort.InferenceSession.create(modelPath, {executionProviders: ['webgpu']});
document.write(`loaded model successfully`);
} catch (e) {
document.write(`failed to load ONNX model: ${e}.`);
}
}
main();
</script>
</body>
</html>
@fs-eire
Copy link

fs-eire commented May 24, 2024

microsoft/onnxruntime#20165 makes some optimization to the package export/import and deployment. Now you can use latest dev build:

//import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/esm/ort.webgpu.min.js";
import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@dev/dist/ort.webgpu.min.mjs";

// by using the new URL, you no longer need to explicitly set `ort.env.wasm.wasmPaths`

(not sure why this is still needed for webgpu backend? as fallback for missing kernels?)

As explained in microsoft/onnxruntime#15869, ORT Web always uses WebAssembly, regardless what EP it is using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment