1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
//! Run the wasi-nn tests in `crates/test-programs`.
//!
//! It may be difficult to run to run all tests on all platforms; we check the
//! pre-requisites for each test dynamically (see [`check`]). Using
//! `libtest-mimic` allows us then to dynamically ignore tests that cannot run
//! on the current machine.
//!
//! There are two modes these tests run in:
//! - "ignore if unavailable" mode: if the checks for a test fail (e.g., the
//! backend is not installed, test artifacts cannot download, we're on the
//! wrong platform), the test is ignored.
//! - "fail if unavailable" mode: when the `CI` or `FORCE_WASINN_TEST_CHECK`
//! environment variables are set, any checks that fail cause the test to fail
//! early.
mod check;
mod exec;
use anyhow::Result;
use libtest_mimic::{Arguments, Trial};
use std::{borrow::Cow, env};
use test_programs_artifacts::*;
use wasmtime_wasi_nn::{backend, Backend};
fn main() -> Result<()> {
tracing_subscriber::fmt::init();
if cfg!(miri) {
return Ok(());
}
// Gather a list of the test-program names.
let mut programs = Vec::new();
macro_rules! add_to_list {
($name:ident) => {
programs.push(stringify!($name));
};
}
foreach_nn!(add_to_list);
// Make ignored tests turn into failures.
let error_on_failed_check =
env::var_os("CI").is_some() || env::var_os("FORCE_WASINN_TEST_CHECK").is_some();
// Inform `libtest-mimic` how to run each test program.
let arguments = Arguments::from_args();
let mut trials = Vec::new();
for program in programs {
// Either ignore the test if it cannot run (i.e., downgrade `Fail` to
// `Ignore`) or preemptively fail it if `error_on_failed_check` is set.
let (run_test, mut check) = check_test_program(program);
if !error_on_failed_check {
check = check.downgrade_failure(); // Downgrade `Fail` to `Ignore`.
}
let should_ignore = check.is_ignore();
if arguments.nocapture && should_ignore {
println!("> ignoring {program}: {}", check.reason());
}
let trial = Trial::test(program, move || {
run_test().map_err(|e| format!("{e:?}").into())
})
.with_ignored_flag(should_ignore);
trials.push(trial);
}
// Run the tests.
libtest_mimic::run(&arguments, trials).exit()
}
/// Return the test program to run and a check that must pass for the test to
/// run.
fn check_test_program(name: &str) -> (fn() -> Result<()>, IgnoreCheck) {
match name {
// Legacy WITX-based tests:
"nn_witx_image_classification_openvino" => (
nn_witx_image_classification_openvino,
IgnoreCheck::for_openvino(),
),
"nn_witx_image_classification_openvino_named" => (
nn_witx_image_classification_openvino_named,
IgnoreCheck::for_openvino(),
),
"nn_witx_image_classification_onnx" => {
(nn_witx_image_classification_onnx, IgnoreCheck::for_onnx())
}
"nn_witx_image_classification_winml_named" => (
nn_witx_image_classification_winml_named,
IgnoreCheck::for_winml(),
),
// WIT-based tests:
"nn_wit_image_classification_openvino" => (
nn_wit_image_classification_openvino,
IgnoreCheck::for_openvino(),
),
"nn_wit_image_classification_openvino_named" => (
nn_wit_image_classification_openvino_named,
IgnoreCheck::for_openvino(),
),
"nn_wit_image_classification_onnx" => {
(nn_wit_image_classification_onnx, IgnoreCheck::for_onnx())
}
"nn_wit_image_classification_winml_named" => (
nn_wit_image_classification_winml_named,
IgnoreCheck::for_winml(),
),
_ => panic!("unknown test program: {name} (add to this `match`)"),
}
}
fn nn_witx_image_classification_openvino() -> Result<()> {
check::openvino::is_installed()?;
check::openvino::are_artifacts_available()?;
let backend = Backend::from(backend::openvino::OpenvinoBackend::default());
exec::witx::run(NN_WITX_IMAGE_CLASSIFICATION_OPENVINO, backend, false)
}
fn nn_witx_image_classification_openvino_named() -> Result<()> {
check::openvino::is_installed()?;
check::openvino::are_artifacts_available()?;
let backend = Backend::from(backend::openvino::OpenvinoBackend::default());
exec::witx::run(NN_WITX_IMAGE_CLASSIFICATION_OPENVINO_NAMED, backend, true)
}
#[cfg(feature = "onnx")]
fn nn_witx_image_classification_onnx() -> Result<()> {
check::onnx::are_artifacts_available()?;
let backend = Backend::from(backend::onnx::OnnxBackend::default());
exec::witx::run(NN_WITX_IMAGE_CLASSIFICATION_ONNX, backend, false)
}
#[cfg(not(feature = "onnx"))]
fn nn_witx_image_classification_onnx() -> Result<()> {
anyhow::bail!("this test requires the `onnx` feature")
}
#[cfg(all(feature = "winml", target_os = "windows"))]
fn nn_witx_image_classification_winml_named() -> Result<()> {
check::winml::is_available()?;
check::onnx::are_artifacts_available()?;
let backend = Backend::from(backend::winml::WinMLBackend::default());
exec::witx::run(NN_WITX_IMAGE_CLASSIFICATION_ONNX, backend, false)
}
#[cfg(not(all(feature = "winml", target_os = "windows")))]
fn nn_witx_image_classification_winml_named() -> Result<()> {
anyhow::bail!("this test requires the `winml` feature and only runs on windows")
}
fn nn_wit_image_classification_openvino() -> Result<()> {
check::openvino::is_installed()?;
check::openvino::are_artifacts_available()?;
let backend = Backend::from(backend::openvino::OpenvinoBackend::default());
exec::wit::run(
NN_WIT_IMAGE_CLASSIFICATION_OPENVINO_COMPONENT,
backend,
false,
)
}
fn nn_wit_image_classification_openvino_named() -> Result<()> {
check::openvino::is_installed()?;
check::openvino::are_artifacts_available()?;
let backend = Backend::from(backend::openvino::OpenvinoBackend::default());
exec::wit::run(
NN_WIT_IMAGE_CLASSIFICATION_OPENVINO_NAMED_COMPONENT,
backend,
true,
)
}
#[cfg(feature = "onnx")]
fn nn_wit_image_classification_onnx() -> Result<()> {
check::onnx::are_artifacts_available()?;
let backend = Backend::from(backend::onnx::OnnxBackend::default());
exec::wit::run(NN_WIT_IMAGE_CLASSIFICATION_ONNX_COMPONENT, backend, false)
}
#[cfg(not(feature = "onnx"))]
fn nn_wit_image_classification_onnx() -> Result<()> {
anyhow::bail!("this test requires the `onnx` feature")
}
#[cfg(all(feature = "winml", target_os = "windows"))]
fn nn_wit_image_classification_winml_named() -> Result<()> {
check::winml::is_available()?;
check::onnx::are_artifacts_available()?;
let backend = Backend::from(backend::winml::WinMLBackend::default());
exec::wit::run(NN_WIT_IMAGE_CLASSIFICATION_ONNX_COMPONENT, backend, false)
}
#[cfg(not(all(feature = "winml", target_os = "windows")))]
fn nn_wit_image_classification_winml_named() -> Result<()> {
anyhow::bail!("this test requires the `winml` feature and only runs on windows")
}
/// Helper for keeping track of what tests should do when pre-test checks fail.
#[derive(Clone)]
enum IgnoreCheck {
Run,
Ignore(Cow<'static, str>),
Fail(Cow<'static, str>),
}
impl IgnoreCheck {
fn reason(&self) -> &str {
match self {
IgnoreCheck::Run => panic!("cannot get reason for `Run`"),
IgnoreCheck::Ignore(reason) => reason,
IgnoreCheck::Fail(reason) => reason,
}
}
fn downgrade_failure(self) -> Self {
if let IgnoreCheck::Fail(reason) = self {
IgnoreCheck::Ignore(reason)
} else {
self
}
}
fn is_ignore(&self) -> bool {
matches!(self, IgnoreCheck::Ignore(_))
}
}
/// Some pre-test checks for various backends.
impl IgnoreCheck {
fn for_openvino() -> IgnoreCheck {
use IgnoreCheck::*;
if !cfg!(target_arch = "x86_64") {
Fail("requires x86_64".into())
} else if !cfg!(target_os = "linux") && !cfg!(target_os = "windows") {
Fail("requires linux or windows or macos".into())
} else if let Err(e) = check::openvino::is_installed() {
Fail(e.to_string().into())
} else {
Run
}
}
fn for_onnx() -> Self {
use IgnoreCheck::*;
#[cfg(feature = "onnx")]
if !cfg!(target_arch = "x86_64") && !cfg!(target_arch = "aarch64") {
Fail("requires x86_64 or aarch64".into())
} else if !cfg!(target_os = "linux")
&& !cfg!(target_os = "windows")
&& !cfg!(target_os = "macos")
{
Fail("requires linux, windows, or macos".into())
} else {
Run
}
#[cfg(not(feature = "onnx"))]
Ignore("requires the `onnx` feature".into())
}
fn for_winml() -> IgnoreCheck {
use IgnoreCheck::*;
#[cfg(all(feature = "winml", target_os = "windows"))]
if !cfg!(target_arch = "x86_64") {
Fail("requires x86_64".into())
} else if !cfg!(target_os = "windows") {
Fail("requires windows".into())
} else if let Err(e) = check::winml::is_available() {
Fail(e.to_string().into())
} else {
Run
}
#[cfg(not(all(feature = "winml", target_os = "windows")))]
Ignore("requires the `winml` feature on windows".into())
}
}
|