Skip to main content
ONNX Runtime Web brings high-performance machine learning inference to web browsers using WebAssembly and WebGPU.

Overview

ONNX Runtime Web supports multiple execution backends:
  • WebAssembly (WASM): CPU execution with SIMD support
  • WebGPU: GPU acceleration for modern browsers
  • WebGL: Legacy GPU support
  • WebNN: Web Neural Network API for hardware acceleration

Installation

Using NPM

Using CDN

Basic Usage

Creating an Inference Session

Loading Models

From URL

From ArrayBuffer

From Uint8Array

Execution Providers

WebAssembly (CPU)

Default CPU execution:

SIMD Support

Enable SIMD for better performance:

WebGPU

Modern GPU acceleration:

WebGPU Options

WebGL

Legacy GPU support:

WebGL Context Options

WebNN

Hardware acceleration via Web Neural Network API:

Session Options

Graph Optimization

Logging

Multi-Threading

Working with Tensors

Creating Tensors

Supported Data Types

  • float32, float64
  • int8, uint8, int16, uint16, int32, uint32
  • int64, uint64 (BigInt)
  • bool, string
  • float16 (Uint16Array)

Tensor from Image

Advanced Features

Pre-allocated Outputs

Model Metadata

Run Options

Performance Optimization

Model Optimization

  1. Use ORT format: Convert to .ort for faster loading
  2. Quantization: Use INT8 quantization
  3. Graph optimization: Enable ‘all’ optimization level

WASM Configuration

WebGPU Optimizations

Caching Sessions

Browser Compatibility

Feature Detection

Fallback Strategy

Build and Deployment

Webpack Configuration

Serving WASM Files

Ensure proper MIME types:

Cross-Origin Isolation

For multi-threading support:

Example: Image Classification

Troubleshooting

Common Issues

WASM files not loading:
  • Check file paths in ort.env.wasm.wasmPaths
  • Verify server MIME type for .wasm files
  • Check browser console for CORS errors
Memory errors:
  • Reduce model size or use quantization
  • Enable memory pattern optimization
  • Dispose sessions when not needed
WebGPU not available:
  • Check browser support (Chrome 94+, Edge 94+)
  • Ensure GPU is available
  • Fallback to WebGL or WASM

Resources