Skip to content

Commit

Permalink
Add check for WinML availability.
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunz committed Feb 28, 2024
1 parent 64ddce9 commit 2de787c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 17 additions & 0 deletions crates/wasi-nn/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
//!
//! This module checks:
//! - that OpenVINO can be found in the environment
//! - that WinML is available
//! - that some ML model artifacts can be downloaded and cached.

use anyhow::{anyhow, Context, Result};
use std::{env, fs, path::Path, path::PathBuf, process::Command, sync::Mutex};
#[cfg(feature = "winml")]
use windows::AI::MachineLearning::{LearningModelDevice, LearningModelDeviceKind};

/// Return the directory in which the test artifacts are stored.
pub fn artifacts_dir() -> PathBuf {
Expand Down Expand Up @@ -41,6 +44,7 @@ pub fn check() -> Result<()> {
}
#[cfg(feature = "winml")]
{
check_winml_is_available()?;
check_winml_artifacts_are_available()?;
}
Ok(())
Expand All @@ -56,6 +60,19 @@ fn check_openvino_is_installed() -> Result<()> {
}
}

#[cfg(feature = "winml")]
fn check_winml_is_available() -> Result<()> {
match std::panic::catch_unwind(|| {
println!(
"> WinML learning device is available: {:?}",
LearningModelDevice::Create(LearningModelDeviceKind::Default)
)
}) {
Ok(_) => Ok(()),
Err(e) => Err(anyhow!("WinML learning device is not available: {:?}", e)),
}
}

/// Protect `check_openvino_artifacts_are_available` from concurrent access;
/// when running tests in parallel, we want to avoid two threads attempting to
/// create the same directory or download the same file.
Expand Down
7 changes: 5 additions & 2 deletions crates/wasi-nn/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ fn run(path: &str, preload_model: bool) -> Result<()> {
{
backends.push(Backend::from(backend::winml::WinMLBackend::default()));
}
for backend in backends{
let mut store = Store::new(&engine, Ctx::new(&testing::artifacts_dir(), preload_model,backend)?);
for backend in backends {
let mut store = Store::new(
&engine,
Ctx::new(&testing::artifacts_dir(), preload_model, backend)?,
);
let instance = linker.instantiate(&mut store, &module)?;
let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
start.call(&mut store, ())?;
Expand Down

0 comments on commit 2de787c

Please sign in to comment.