Because ort-web operates across two separate WebAssembly contexts, memory is not shared. You must manually synchronize data when moving it between the Rust context and the ONNX Runtime context.
Input Tensors
Do not use Tensor::new for inputs, as it allocates on the ONNX Runtime side and requires an unnecessary synchronization of empty data. Instead, use:
Tensor::from_arrayTensorRef::from_array_view
These methods create tensors that do not require synchronization.
Output Tensors
Session outputs are not synchronized automatically. To use output data in Rust, you must sync them. You can sync all outputs at once using ort_web::sync_outputs, or sync individual tensors using .sync(SyncDirection::Rust).await?.
Sync Directions:
SyncDirection::Rust: Synchronizes data from the ONNX Runtime context to the Rust context. Use this after running a session to read outputs.SyncDirection::Runtime: Synchronizes data from the Rust context to the ONNX Runtime context. Use this if you have modified a tensor in Rust and want the changes to be visible to the runtime.
use ort_web::{TensorExt, SyncDirection};
// ... after session.run_async ...
let mut bounding_boxes = outputs.remove("bounding_boxes").unwrap();
bounding_boxes.sync(SyncDirection::Rust).await?;
// now we can use the data
let data = bounding_boxes.try_extract_tensor::<f32>()?;