From f1940e9c2223d9997edda38b629ba104fb0544ef Mon Sep 17 00:00:00 2001 From: Jianjun Zhu Date: Tue, 14 May 2024 15:57:14 +0800 Subject: [PATCH] Add a benchmark for wasi-nn ONNX models. --- .../rust-benchmark/Cargo.lock | 143 ++++++------------ .../rust-benchmark/Cargo.toml | 10 +- .../rust-benchmark/src/onnx.rs | 55 +++++++ .../src/{main.rs => openvino.rs} | 0 benchmarks/image-classification/setup.sh | 22 ++- 5 files changed, 124 insertions(+), 106 deletions(-) create mode 100644 benchmarks/image-classification/rust-benchmark/src/onnx.rs rename benchmarks/image-classification/rust-benchmark/src/{main.rs => openvino.rs} (100%) diff --git a/benchmarks/image-classification/rust-benchmark/Cargo.lock b/benchmarks/image-classification/rust-benchmark/Cargo.lock index 7dfb30b8..236bac94 100644 --- a/benchmarks/image-classification/rust-benchmark/Cargo.lock +++ b/benchmarks/image-classification/rust-benchmark/Cargo.lock @@ -2,30 +2,12 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "adler32" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" - [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" -[[package]] -name = "bitflags" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" - [[package]] name = "bytemuck" version = "1.5.1" @@ -38,47 +20,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" -[[package]] -name = "crc32fast" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "deflate" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" -dependencies = [ - "adler32", - "byteorder", -] - -[[package]] -name = "gif" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a668f699973d0f573d15749b7002a9ac9e1f9c6b220e7b165601334c173d8de" -dependencies = [ - "color_quant", - "weezl", -] - [[package]] name = "image" version = "0.23.14" @@ -88,14 +35,10 @@ dependencies = [ "bytemuck", "byteorder", "color_quant", - "gif", "jpeg-decoder", "num-iter", "num-rational", "num-traits", - "png", - "scoped_threadpool", - "tiff", ] [[package]] @@ -113,25 +56,6 @@ version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2" -[[package]] -name = "miniz_oxide" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" -dependencies = [ - "adler32", -] - -[[package]] -name = "miniz_oxide" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" -dependencies = [ - "adler", - "autocfg", -] - [[package]] name = "num-integer" version = "0.1.44" @@ -174,22 +98,22 @@ dependencies = [ ] [[package]] -name = "png" -version = "0.16.8" +name = "proc-macro2" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" dependencies = [ - "bitflags", - "crc32fast", - "deflate", - "miniz_oxide 0.3.7", + "unicode-ident", ] [[package]] -name = "scoped_threadpool" -version = "0.1.9" +name = "quote" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] [[package]] name = "sightglass-api" @@ -198,24 +122,47 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3807ba2ea8ba8ed7d5510915850ef220ab09a1042d181aca0619df10a3d9bf15" [[package]] -name = "tiff" -version = "0.6.1" +name = "syn" +version = "2.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" dependencies = [ - "jpeg-decoder", - "miniz_oxide 0.4.4", - "weezl", + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] -name = "wasi-nn" -version = "0.2.1" +name = "thiserror" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80715609d32850b55b000f7061a0b796d374bd37aa261db94eae0a6c2b35ccf8" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] -name = "weezl" -version = "0.1.5" +name = "wasi-nn" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e" +checksum = "03d01b90f0cca3f19682e90e1bc3f5e3e441031e19e56ce7dbf034f3b3597552" +dependencies = [ + "thiserror", +] diff --git a/benchmarks/image-classification/rust-benchmark/Cargo.toml b/benchmarks/image-classification/rust-benchmark/Cargo.toml index a4775e4f..f5dedeb3 100644 --- a/benchmarks/image-classification/rust-benchmark/Cargo.toml +++ b/benchmarks/image-classification/rust-benchmark/Cargo.toml @@ -8,7 +8,15 @@ publish = false [dependencies] image = { version = "0.23.14", default-features = false, features = ["jpeg"] } -wasi-nn = "0.2.1" +wasi-nn = "0.5.0" sightglass-api = "0.1" [workspace] + +[[bin]] +name = "image-classification-benchmark-onnx" +path = "src/onnx.rs" + +[[bin]] +name = "image-classification-benchmark-openvino" +path = "src/openvino.rs" diff --git a/benchmarks/image-classification/rust-benchmark/src/onnx.rs b/benchmarks/image-classification/rust-benchmark/src/onnx.rs new file mode 100644 index 00000000..908cd443 --- /dev/null +++ b/benchmarks/image-classification/rust-benchmark/src/onnx.rs @@ -0,0 +1,55 @@ +use image::io::Reader; +use image::DynamicImage; +use sightglass_api as bench; +use std::convert::TryInto; +use std::fs; +use wasi_nn; +mod imagenet_classes; + +pub fn main() { + // Convert image to tensor data. + let tensor_data = fs::read("./kitten.rgb").unwrap(); + + // Load model from a file. + let graph = + wasi_nn::GraphBuilder::new(wasi_nn::GraphEncoding::Onnx, wasi_nn::ExecutionTarget::CPU) + .build_from_files(["./mobilenet.onnx"]) + .unwrap(); + + let mut context = graph.init_execution_context().unwrap(); + context + .set_input(0, wasi_nn::TensorType::F32, &[1, 3, 224, 224], &tensor_data) + .unwrap(); + + bench::start(); + + // Execute the inference. + context.compute().unwrap(); + + bench::end(); + + + // Retrieve the output. + let mut output_buffer = vec![0f32; 1000]; + context.get_output(0, &mut output_buffer[..]).unwrap(); + + let result = sort_results(&output_buffer); +} + +// Sort the buffer of probabilities. The graph places the match probability for each class at the +// index for that class (e.g. the probability of class 42 is placed at buffer[42]). Here we convert +// to a wrapping InferenceResult and sort the results. +fn sort_results(buffer: &[f32]) -> Vec { + let mut results: Vec = buffer + .iter() + .skip(1) + .enumerate() + .map(|(c, p)| InferenceResult(c, *p)) + .collect(); + results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); + results +} + +// A wrapper for class ID and match probabilities. +#[derive(Debug, PartialEq)] +struct InferenceResult(usize, f32); diff --git a/benchmarks/image-classification/rust-benchmark/src/main.rs b/benchmarks/image-classification/rust-benchmark/src/openvino.rs similarity index 100% rename from benchmarks/image-classification/rust-benchmark/src/main.rs rename to benchmarks/image-classification/rust-benchmark/src/openvino.rs diff --git a/benchmarks/image-classification/setup.sh b/benchmarks/image-classification/setup.sh index 74b6f400..cadeb7e2 100755 --- a/benchmarks/image-classification/setup.sh +++ b/benchmarks/image-classification/setup.sh @@ -4,10 +4,18 @@ # cargo run -- benchmark benchmarks/image-classification/image-classification-benchmark.wasm --engine-flags="--wasi nn" --engine engines/wasmtime/libengine.so WASI_NN_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) -FILENAME=l_openvino_toolkit_ubuntu20_2022.2.0.7713.af16ea1d79a_x86_64 -MODEL=https://github.com/intel/openvino-rs/raw/main/crates/openvino/tests/fixtures/mobilenet -[ ! -d ${WASI_NN_DIR}/mobilenet.xml ] && wget -nc ${MODEL}/mobilenet.xml -O ${WASI_NN_DIR}/mobilenet.xml -[ ! -d ${WASI_NN_DIR}/mobilenet.bin ] && wget -nc -q --no-check-certificate ${MODEL}/mobilenet.bin -O ${WASI_NN_DIR}/mobilenet.bin -[ ! -d ${WASI_NN_DIR}/openvino ] && wget -nc -q --no-check-certificate https://storage.openvinotoolkit.org/repositories/openvino/packages/2022.2/linux/${FILENAME}.tgz -O ${WASI_NN_DIR}/${FILENAME}.tgz -[ ! -d ${WASI_NN_DIR}/openvino ] && wget -nc -q --no-check-certificate https://storage.openvinotoolkit.org/repositories/openvino/packages/2022.2/linux/${FILENAME}.tgz -O ${WASI_NN_DIR}/${FILENAME}.tgz -[ ! -d ${WASI_NN_DIR}/openvino ] && tar -C ${WASI_NN_DIR} -zxf ${WASI_NN_DIR}/${FILENAME}.tgz && mv ${WASI_NN_DIR}/${FILENAME} ${WASI_NN_DIR}/openvino || echo "OpenVINO is already there, skipping..." + +OpenVINO +OPENVINO_FILENAME=l_openvino_toolkit_ubuntu20_2022.2.0.7713.af16ea1d79a_x86_64 +OPENVINO_MODEL=https://github.com/intel/openvino-rs/raw/main/crates/openvino/tests/fixtures/mobilenet +[ ! -d ${WASI_NN_DIR}/mobilenet.xml ] && wget -nc ${OPENVINO_MODEL}/mobilenet.xml -O ${WASI_NN_DIR}/mobilenet.xml +[ ! -d ${WASI_NN_DIR}/mobilenet.bin ] && wget -nc -q --no-check-certificate ${OPENVINO_MODEL}/mobilenet.bin -O ${WASI_NN_DIR}/mobilenet.bin +[ ! -d ${WASI_NN_DIR}/openvino ] && wget -nc -q --no-check-certificate https://storage.openvinotoolkit.org/repositories/openvino/packages/2022.2/linux/${OPENVINO_FILENAME}.tgz -O ${WASI_NN_DIR}/${OPENVINO_FILENAME}.tgz +[ ! -d ${WASI_NN_DIR}/openvino ] && wget -nc -q --no-check-certificate https://storage.openvinotoolkit.org/repositories/openvino/packages/2022.2/linux/${OPENVINO_FILENAME}.tgz -O ${WASI_NN_DIR}/${OPENVINO_FILENAME}.tgz +[ ! -d ${WASI_NN_DIR}/openvino ] && tar -C ${WASI_NN_DIR} -zxf ${WASI_NN_DIR}/${OPENVINO_FILENAME}.tgz && mv ${WASI_NN_DIR}/${OPENVINO_FILENAME} ${WASI_NN_DIR}/openvino || echo "OpenVINO is already there, skipping..." + +# ONNX +ONNX_MODEL=https://github.com/onnx/models/raw/bec48b6a70e5e9042c0badbaafefe4454e072d08/validated/vision/classification/mobilenet/model/mobilenetv2-7.onnx?download= +[ ! -d ${WASI_NN_DIR}/mobilenet.onnx ] && wget -nc ${ONNX_MODEL} -O ${WASI_NN_DIR}/mobilenet.onnx +ONNX_IMAGE_RGB=https://github.com/bytecodealliance/wasmtime/raw/v20.0.2/crates/wasi-nn/tests/fixtures/kitten.rgb +[ ! -d ${WASI_NN_DIR}/kitten.rgb ] && wget -nc ${ONNX_IMAGE_RGB} -O ${WASI_NN_DIR}/kitten.rgb