diff --git a/crates/openvino-finder/README.md b/crates/openvino-finder/README.md index 3379ad3..3d62ec7 100644 --- a/crates/openvino-finder/README.md +++ b/crates/openvino-finder/README.md @@ -1,7 +1,15 @@ openvino-finder =============== -A utility for locating OpenVINO™ libraries on a host system. +A utility for locating OpenVINO™ libraries on a host system. It will attempt to find the OpenVINO +shared libraries in: +- the `OPENVINO_INSTALL_DIR` environment variable (pointed at the top-level OpenVINO installation, + e.g. `/opt/intel/openvino`) +- the `INTEL_OPENVINO_DIR` environment variable (same as above; this is set by OpenVINO setup + scripts) +- the environment's library path (e.g., `LD_LIBRARY_PATH` in Linux; this is also set by the OpenVINO + setup scripts) +- OpenVINO's default installation paths for the OS (a best effort attempt) > #### WARNING > This crate is currently experimental--its API surface is subject to change. diff --git a/crates/openvino/Cargo.toml b/crates/openvino/Cargo.toml index a15b38e..be92542 100644 --- a/crates/openvino/Cargo.toml +++ b/crates/openvino/Cargo.toml @@ -18,3 +18,6 @@ thiserror = "1.0.20" [dev-dependencies] float-cmp = "0.8" + +[features] +runtime-linking = ["openvino-sys/runtime-linking"] diff --git a/crates/openvino/src/core.rs b/crates/openvino/src/core.rs index b868a0a..53dbe51 100644 --- a/crates/openvino/src/core.rs +++ b/crates/openvino/src/core.rs @@ -2,9 +2,12 @@ //! [API](https://docs.openvinotoolkit.org/latest/ie_c_api/modules.html). use crate::blob::Blob; -use crate::network::{CNNNetwork, ExecutableNetwork}; use crate::tensor_desc::TensorDesc; use crate::{cstr, drop_using_function, try_unsafe, util::Result}; +use crate::{ + error::{LoadingError, SetupError}, + network::{CNNNetwork, ExecutableNetwork}, +}; use crate::{Layout, Precision}; use openvino_sys::{ self, ie_config_t, ie_core_create, ie_core_free, ie_core_load_network, ie_core_read_network, @@ -19,17 +22,18 @@ drop_using_function!(Core, ie_core_free); impl Core { /// Construct a new OpenVINO [Core]--this is the primary entrypoint for constructing and using - /// inference networks. - pub fn new(xml_config_file: Option<&str>) -> Result { - openvino_sys::library::load().expect("unable to load shared library"); + /// inference networks. Because this function may load OpenVINO's shared libraries at runtime, + /// there are more ways than usual that this function can fail (e.g., [LoadingError]s). + pub fn new(xml_config_file: Option<&str>) -> std::result::Result { + openvino_sys::library::load().or_else(|e| Err(LoadingError::SystemFailure(e)))?; let file = match xml_config_file { None => format!( "{}/plugins.xml", openvino_sys::library::find() - .expect("unable to find path to OpenVINO libraries") + .ok_or(LoadingError::CannotFindPath)? .parent() - .expect("unable to get the parent of the linked OpenVINO library") + .ok_or(LoadingError::NoParentDirectory)? .display() ), Some(f) => f.to_string(), diff --git a/crates/openvino/src/error.rs b/crates/openvino/src/error.rs index 87c69cd..e626cf5 100644 --- a/crates/openvino/src/error.rs +++ b/crates/openvino/src/error.rs @@ -1,12 +1,8 @@ use thiserror::Error; -/// See +/// Enumerate errors returned by the OpenVINO implementation. See /// [IEStatusCode](https://docs.openvinotoolkit.org/latest/ie_c_api/ie__c__api_8h.html#a391683b1e8e26df8b58d7033edd9ee83). -/// -/// TODO Replace this in bindgen with -/// [newtype_enum](https://docs.rs/bindgen/0.54.1/bindgen/struct.Builder.html#method.newtype_enum) -/// or -/// [rustified_enum](https://docs.rs/bindgen/0.54.1/bindgen/struct.Builder.html#method.rustified_enum). +/// TODO This could be auto-generated (https://github.com/intel/openvino-rs/issues/20). #[derive(Debug, Error)] pub enum InferenceError { #[error("general error")] @@ -58,3 +54,25 @@ impl InferenceError { } } } + +/// Enumberate setup failures: in some cases, this library calls library loading code that may fail +/// in a different way (i.e., [LoadingError]) than the calls in to the OpenVINO libraries (i.e., +/// [InferenceError]). +#[derive(Debug, Error)] +pub enum SetupError { + #[error("inference error")] + Inference(#[from] InferenceError), + #[error("library loading error")] + Loading(#[from] LoadingError), +} + +/// Enumerate the ways that library loading can fail. +#[derive(Debug, Error)] +pub enum LoadingError { + #[error("system failed to load shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder): {0}")] + SystemFailure(String), + #[error("cannot find path to shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder)")] + CannotFindPath, + #[error("no parent directory found for shared library path")] + NoParentDirectory, +} diff --git a/crates/openvino/src/lib.rs b/crates/openvino/src/lib.rs index e36cba9..9f5949e 100644 --- a/crates/openvino/src/lib.rs +++ b/crates/openvino/src/lib.rs @@ -8,7 +8,7 @@ mod util; pub use crate::core::Core; pub use blob::Blob; -pub use error::InferenceError; +pub use error::{InferenceError, LoadingError, SetupError}; pub use network::{CNNNetwork, ExecutableNetwork}; // Re-publish some OpenVINO enums with a conventional Rust naming (see // `crates/openvino-sys/build.rs`).