Skip to content

Commit 163c6a8

Browse files
committed
update about onnx runtime's fallback behavior
1 parent c919981 commit 163c6a8

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wasi-nn/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ onnx = ["dep:ort"]
7272
onnx-download = ["onnx", "ort/download-binaries"]
7373
# CUDA execution provider for NVIDIA GPU support (requires CUDA toolkit)
7474
onnx-cuda = ["onnx", "ort/cuda"]
75+
# Enable tracing for ONNX Runtime
76+
ort-tracing = ["onnx", "ort/tracing"]
7577
# WinML is only available on Windows 10 1809 and later.
7678
winml = ["dep:windows"]
7779
# PyTorch is available on all platforms; requires Libtorch to be installed

crates/wasi-nn/examples/classification-component-onnx/README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ GPU execution target only supports Nvidia CUDA (onnx-cuda) as execution provider
1212

1313
In this directory, run the following command to build the WebAssembly component:
1414
```console
15+
# build component for target wasm32-wasip1
1516
cargo component build
17+
18+
# build component for target wasm32-wasip2
19+
cargo component build --target wasm32-wasip2
1620
```
1721

1822
## Running the Example
@@ -44,7 +48,7 @@ Arguments:
4448
./target/debug/wasmtime run \
4549
-Snn \
4650
--dir ./crates/wasi-nn/examples/classification-component-onnx/fixture/::fixture \
47-
./crates/wasi-nn/examples/classification-component-onnx/target/wasm32-wasip1/debug/classification-component-onnx.wasm
51+
./crates/wasi-nn/examples/classification-component-onnx/target/wasm32-wasip2/debug/classification-component-onnx.wasm
4852
```
4953

5054
#### GPU (CUDA) Execution:
@@ -55,7 +59,7 @@ export LD_LIBRARY_PATH={wasmtime_workspace}/target/debug
5559
./target/debug/wasmtime run \
5660
-Snn \
5761
--dir ./crates/wasi-nn/examples/classification-component-onnx/fixture/::fixture \
58-
./crates/wasi-nn/examples/classification-component-onnx/target/wasm32-wasip1/debug/classification-component-onnx.wasm \
62+
./crates/wasi-nn/examples/classification-component-onnx/target/wasm32-wasip2/debug/classification-component-onnx.wasm \
5963
gpu
6064

6165
```
@@ -79,7 +83,31 @@ Index: n02102318 cocker spaniel, English cocker spaniel, cocker - Probability: 0
7983
When using GPU target, the first line will indicate the selected execution target.
8084
You can monitor GPU usage using cmd `watch -n 1 nvidia-smi`.
8185

86+
To see trace logs from `wasmtime_wasi_nn` or `ort`, run Wasmtime with `WASMTIME_LOG` enabled, e.g.,
87+
88+
```sh
89+
WASMTIME_LOG=wasmtime_wasi_nn=warn ./target/debug/wasmtime run ...
90+
WASMTIME_LOG=ort=warn ./target/debug/wasmtime run ...
91+
```
92+
8293
## Prerequisites for GPU(CUDA) Support
8394
- NVIDIA GPU with CUDA support
8495
- CUDA Toolkit 12.x with cuDNN 9.x
8596
- Build wasmtime with `wasmtime-wasi-nn/onnx-cuda` feature
97+
98+
## ONNX Runtime's Fallback Behavior
99+
100+
If the GPU execution provider is requested (by passing `gpu`) but the device does not have a GPU or the necessary CUDA drivers are missing, ONNX Runtime will **silently fall back** to the CPU execution provider. The application will continue to run, but inference will happen on the CPU.
101+
102+
To verify if fallback is happening, you can enable ONNX Runtime logging:
103+
104+
1. Build Wasmtime with the additional `wasmtime-wasi-nn/ort-tracing` feature:
105+
```sh
106+
cargo build --features component-model,wasi-nn,wasmtime-wasi-nn/onnx-cuda,wasmtime-wasi-nn/ort-tracing
107+
```
108+
109+
2. Run Wasmtime with `WASMTIME_LOG` enabled to see `ort` warnings:
110+
```sh
111+
WASMTIME_LOG=ort=warn ./target/debug/wasmtime run ...
112+
```
113+
You should see a warning like: `No execution providers from session options registered successfully; may fall back to CPU.`

crates/wasi-nn/src/backend/onnx.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ fn configure_execution_providers(
6868
#[cfg(feature = "onnx-cuda")]
6969
{
7070
// Use CUDA execution provider for GPU acceleration
71-
tracing::debug!("Configuring ONNX Nvidia CUDA execution provider for GPU target");
71+
tracing::debug!("Using ONNX runtime's Nvidia GPU CUDA execution provider");
7272
Ok(vec![CUDAExecutionProvider::default().build()])
7373
}
7474
#[cfg(not(feature = "onnx-cuda"))]
7575
{
76-
Err(BackendError::BackendAccess(wasmtime::format_err!(
77-
"GPU execution target is requested, but 'onnx-cuda' feature is not enabled"
78-
)))
76+
tracing::warn!("ONNX runtime GPU CUDA execution provider is not enabled, falling back to CPU");
77+
Ok(vec![CPUExecutionProvider::default().build()])
7978
}
8079
}
8180
ExecutionTarget::Tpu => {
82-
unimplemented!("TPU execution target is not supported for ONNX backend yet");
81+
tracing::warn!("TPU execution target is not supported for ONNX backend yet, falling back to CPU");
82+
Ok(vec![CPUExecutionProvider::default().build()])
8383
}
8484
}
8585
}

0 commit comments

Comments
 (0)