-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for
wasm32-unknown-unknown
target (#184)
- Loading branch information
1 parent
cedeb55
commit b12c43c
Showing
11 changed files
with
360 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
publish = false | ||
name = "example-webassembly" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "ortwasm" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
ort = { path = "../../" } | ||
ndarray = "0.15" | ||
wasm-bindgen = "0.2.92" | ||
web-sys = "0.3" | ||
tracing = "0.1" | ||
tracing-subscriber = "0.3" | ||
tracing-subscriber-wasm = "0.1" | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.3" | ||
console_error_panic_hook = "0.1" | ||
|
||
[features] | ||
load-dynamic = [ "ort/load-dynamic" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use ndarray::{Array4, ArrayViewD}; | ||
use ort::Session; | ||
use wasm_bindgen::prelude::*; | ||
|
||
static MODEL_BYTES: &[u8] = include_bytes!("upsample.ort"); | ||
|
||
pub fn upsample_inner() -> ort::Result<()> { | ||
let session = Session::builder()? | ||
.commit_from_memory_directly(MODEL_BYTES) | ||
.expect("Could not read model from memory"); | ||
|
||
let array = Array4::<f32>::zeros((1, 224, 224, 3)); | ||
|
||
let outputs = session.run(ort::inputs![array]?)?; | ||
|
||
assert_eq!(outputs.len(), 1); | ||
let output: ArrayViewD<f32> = outputs[0].try_extract_tensor()?; | ||
|
||
assert_eq!(output.shape(), [1, 448, 448, 3]); | ||
|
||
Ok(()) | ||
} | ||
|
||
macro_rules! console_log { | ||
($($t:tt)*) => (web_sys::console::log_1(&format_args!($($t)*).to_string().into())) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn upsample() { | ||
if let Err(e) = upsample_inner() { | ||
console_log!("Error occurred while performing inference: {e:?}"); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[wasm_bindgen_test::wasm_bindgen_test] | ||
fn run_test() { | ||
use tracing::Level; | ||
use tracing_subscriber::fmt; | ||
use tracing_subscriber_wasm::MakeConsoleWriter; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
ort::wasm::initialize(); | ||
|
||
fmt() | ||
.with_ansi(false) | ||
.with_max_level(Level::DEBUG) | ||
.with_writer(MakeConsoleWriter::default().map_trace_level_to(Level::DEBUG)) | ||
.without_time() | ||
.init(); | ||
|
||
std::panic::set_hook(Box::new(console_error_panic_hook::hook)); | ||
|
||
upsample(); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.