WebAssembly support in ort is currently experimental. If you experience any issues using ort in WebAssembly, please open an issue.

Development of WASM support is done in a separate branch for now, so you’ll have to add ort as a Git dependency:

Cargo.toml
[dependencies]
ort = { git = "https://github.com/pykeio/ort.git", branch = "wasm32-unknown-unknown" }

By nature, some features of ONNX Runtime are not available in the web. These include:

  • Support for .onnx models. You instead need to convert .onnx models to the .ort format.
  • Runtime graph optimizations, aka SessionBuilder::with_optimization_level. You can statically optimize the graph using the .ort conversion tool, though.
  • Loading models with commit_from_file/commit_from_url. You can create models from a slice of bytes in memory with SessionBuilder::commit_from_memory or SessionBuilder::commit_from_memory_directly.

Additionally, you’ll need to call ort::wasm::initialize() at the earliest possible point in your code, before you use any ort APIs:

main.rs
use ort::Session;

static MODEL_BYTES: &[u8] = include_bytes!("../model.ort");

fn main() -> anyhow::Result<()> {
    #[cfg(target_arch = "wasm32")]
    ort::wasm::initialize();

    let session = Session::builder()?.commit_from_memory_directly(MODEL_BYTES)?;
    ...
}