Skip to content

Commit

Permalink
Regard review by @decahedron1.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelmenges committed Feb 7, 2025
1 parent 6da5ea8 commit cbf5276
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/wasm-emscripten/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
libonnxruntime.a
models
yolov8m.onnx
emsdk
1 change: 0 additions & 1 deletion examples/wasm-emscripten/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
[dependencies]
ort = { path = "../../" }
ndarray = "0.16"
rust-embed = { version = "8", features = ["debug-embed"] } # Embed in debug to debug WASM.
image = "0.25"

[build-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions examples/wasm-emscripten/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn copy_dir_all(src: impl AsRef<std::path::Path>, dst: impl AsRef<std::path::Pat

fn main() {
use std::{
fs::{File, create_dir_all},
fs::File,
io::{Cursor, Read, Write}
};

Expand Down Expand Up @@ -60,8 +60,7 @@ fn main() {
let mut request = get("https://parcel.pyke.io/v2/cdn/assetdelivery/ortrsv2/ex_models/yolov8m.onnx").expect("Cannot request model.");
let mut buf = Vec::<u8>::new();
request.read_to_end(&mut buf).expect("Cannot read model.");
create_dir_all("models").expect("Cannot create models directory.");
let mut file = File::create("models/yolov8m.onnx").expect("Cannot create model file.");
let mut file = File::create("./yolov8m.onnx").expect("Cannot create model file.");
file.write_all(&buf).expect("Cannot store model.");
}

Expand Down
2 changes: 1 addition & 1 deletion examples/wasm-emscripten/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Threading requires to compile the standard library with "atomics" and "bulk-memory" features.
# Threading requires to compile the standard library with "+atomics,+bulk-memory,+mutable-globals".
# See: https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html#building-the-demo
[toolchain]
channel = "nightly"
Expand Down
24 changes: 9 additions & 15 deletions examples/wasm-emscripten/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![no_main]

// Embed models into the .wasm file.
#[derive(rust_embed::RustEmbed)]
#[folder = "models/"]
pub struct Models;

// Below YoloV8 implementation partly copied from the "yolov8" example.
#[derive(Debug, Clone, Copy)]
struct BoundingBox {
Expand All @@ -27,17 +22,18 @@ static YOLOV8_CLASS_LABELS: [&str; 80] = [
];

#[no_mangle]
pub extern "C" fn alloc(capacity: usize) -> *mut std::os::raw::c_void {
let mut buf = Vec::with_capacity(capacity);
let ptr = buf.as_mut_ptr();
std::mem::forget(buf);
return ptr as *mut std::os::raw::c_void;
pub extern "C" fn alloc(size: usize) -> *mut std::os::raw::c_void {
unsafe {
let layout = std::alloc::Layout::from_size_align(size, std::mem::align_of::<u8>()).expect("Cannot create memory layout.");
return std::alloc::alloc(layout) as *mut std::os::raw::c_void;
}
}

#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut std::os::raw::c_void, capacity: usize) {
pub extern "C" fn dealloc(ptr: *mut std::os::raw::c_void, size: usize) {
unsafe {
let _buf = Vec::from_raw_parts(ptr, 0, capacity);
let layout = std::alloc::Layout::from_size_align(size, std::mem::align_of::<u8>()).expect("Cannot create memory layout.");
std::alloc::dealloc(ptr as *mut u8, layout);
}
}

Expand All @@ -49,8 +45,6 @@ pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) {
.commit()
.expect("Cannot initialize ort.");

let model = Models::get("yolov8m.onnx").expect("Cannot find model.").data.to_vec();

let session = ort::session::Session::builder()
.expect("Cannot create Session builder.")
.with_optimization_level(ort::session::builder::GraphOptimizationLevel::Level3)
Expand All @@ -61,7 +55,7 @@ pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) {
.expect("Cannot set intra thread count.")
.with_inter_threads(1)
.expect("Cannot set inter thread count.")
.commit_from_memory(&model)
.commit_from_memory(include_bytes!("../yolov8m.onnx"))
.expect("Cannot commit model.");

let image_data = unsafe { std::slice::from_raw_parts(ptr, (width * height * 4) as usize).to_vec() }; // Copy via .to_vec might be not necessary as memory lives long enough.
Expand Down

0 comments on commit cbf5276

Please sign in to comment.