From 3c916a9472bb5000dd8db6e96a2c52502aec2dbf Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 26 Apr 2021 11:05:05 -0700 Subject: [PATCH 1/3] Add `--features runtime-linking` to openvino crate This makes it easier to use the runtime linking feature directly from an `openvino` dependency: ``` openvino = { version = "...", features = ["runtime-linking"] } ``` --- crates/openvino/Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) 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"] From 88a8ceaa7b4bcfeebcbf8e2490453d499c3d0d8f Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 26 Apr 2021 11:07:58 -0700 Subject: [PATCH 2/3] Return error when loading fails instead of panicking This refactoring changes the public API of the `openvino` crate to add a new error type, `LoadingError`, as well as a wrapper type, `SetupError`, to disambiguate between that and `InferenceError`. The `Core::new` function changes to return the wrapper, `SetupError`, since it could fail while loading the OpenVINO shared libraries. --- crates/openvino/src/core.rs | 16 ++++++++++------ crates/openvino/src/error.rs | 30 ++++++++++++++++++++++++------ crates/openvino/src/lib.rs | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) 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`). From aa388fa03890bdf92cc041d2975566832fbbc230 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 26 Apr 2021 11:18:33 -0700 Subject: [PATCH 3/3] Document openvino-finder further in README --- crates/openvino-finder/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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.