diff --git a/crates/openvino/src/core.rs b/crates/openvino/src/core.rs index 34dfb17..91a0c02 100644 --- a/crates/openvino/src/core.rs +++ b/crates/openvino/src/core.rs @@ -16,8 +16,6 @@ use std::os::raw::c_char; use std::slice; use std::str::FromStr; -const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") }; - /// See [`Core`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__core__c__api.html). pub struct Core { ptr: *mut ov_core_t, @@ -103,52 +101,10 @@ impl Core { Ok(devices) } - /// Gets properties related to this Core. - /// The method extracts information that can be set via the [set_property] method. - pub fn get_property(&self, key: PropertyKey) -> Result { - let ov_prop_key = cstr!(key.as_ref()); - let mut ov_prop_value = std::ptr::null_mut(); - try_unsafe!(ov_core_get_property( - self.ptr, - EMPTY_C_STR.as_ptr(), - ov_prop_key.as_ptr(), - std::ptr::addr_of_mut!(ov_prop_value) - ))?; - let rust_prop = unsafe { CStr::from_ptr(ov_prop_value) } - .to_str() - .unwrap() - .to_owned(); - Ok(rust_prop) - } - - /// Sets a property for this Core instance. - pub fn set_property(&mut self, key: RwPropertyKey, value: &str) -> Result<()> { - let ov_prop_key = cstr!(key.as_ref()); - let ov_prop_value = cstr!(value); - try_unsafe!(ov_core_set_property( - self.ptr, - EMPTY_C_STR.as_ptr(), - ov_prop_key.as_ptr(), - ov_prop_value.as_ptr(), - ))?; - Ok(()) - } - - /// Sets properties for this Core instance. - pub fn set_properties<'a>( - &mut self, - properties: impl IntoIterator, - ) -> Result<()> { - for (prop_key, prop_value) in properties { - self.set_property(prop_key, prop_value)?; - } - Ok(()) - } - - /// Gets properties related to device behaviour. - /// The method extracts information that can be set via the [set_device_property] method. - pub fn get_device_property(&self, device_name: &str, key: PropertyKey) -> Result { - let ov_device_name = cstr!(device_name); + /// Gets properties related to device behaviour for this core. + /// The method extracts information that can be set via the [`set_property`] method. + pub fn get_property(&self, device_name: &DeviceType, key: &PropertyKey) -> Result { + let ov_device_name = cstr!(device_name.as_ref()); let ov_prop_key = cstr!(key.as_ref()); let mut ov_prop_value = std::ptr::null_mut(); try_unsafe!(ov_core_get_property( @@ -165,13 +121,13 @@ impl Core { } /// Sets a property for a device. - pub fn set_device_property( + pub fn set_property( &mut self, - device_name: &str, - key: RwPropertyKey, + device_name: &DeviceType, + key: &RwPropertyKey, value: &str, ) -> Result<()> { - let ov_device_name = cstr!(device_name); + let ov_device_name = cstr!(device_name.as_ref()); let ov_prop_key = cstr!(key.as_ref()); let ov_prop_value = cstr!(value); try_unsafe!(ov_core_set_property( @@ -184,13 +140,13 @@ impl Core { } /// Sets properties for a device. - pub fn set_device_properties<'a>( + pub fn set_properties<'a>( &mut self, - device_name: &str, + device_name: DeviceType, properties: impl IntoIterator, ) -> Result<()> { for (prop_key, prop_value) in properties { - self.set_device_property(device_name, prop_key, prop_value)?; + self.set_property(&device_name, &prop_key, prop_value)?; } Ok(()) } @@ -246,6 +202,8 @@ impl Core { #[cfg(test)] mod core_tests { use super::*; + use PropertyKey::*; + use RwPropertyKey::*; #[test] fn test_new() { @@ -260,4 +218,76 @@ mod core_tests { let model = core.read_model_from_buffer(model, None); assert!(model.is_ok()); } + + #[test] + fn test_get_core_properties_supported() { + let core = Core::new().unwrap(); + let supported_keys = vec![ + SupportedProperties, + AvailableDevices, + OptimalNumberOfInferRequests, + RangeForAsyncInferRequests, + RangeForStreams, + DeviceFullName, + DeviceCapabilities, + ]; + for key in supported_keys { + let supported_properties = core.get_property(&DeviceType::CPU, &key); + assert!( + supported_properties.is_ok(), + "Failed on supported key: {:?}", + &key + ); + } + } + + #[test] + fn test_get_core_properties_rw() { + let core = Core::new().unwrap(); + let rw_keys = vec![ + CacheDir, + NumStreams, + Affinity, + InferenceNumThreads, + HintEnableCpuPinning, + HintEnableHyperThreading, + HintPerformanceMode, + HintSchedulingCoreType, + HintInferencePrecision, + HintNumRequests, + EnableProfiling, + HintExecutionMode, + ]; + for key in rw_keys { + let key_clone = key.clone(); + let supported_properties = core.get_property(&DeviceType::CPU, &key.into()); + assert!( + supported_properties.is_ok(), + "Failed on rw key: {:?}", + &PropertyKey::Rw(key_clone) + ); + } + } + + #[test] + fn test_get_core_properties_unsupported() { + let core = Core::new().unwrap(); + let unsupported_keys = vec![ + HintModelPriority, + DevicePriorities, + CacheMode, + ForceTbbTerminate, + EnableMmap, + AutoBatchTimeout, + ]; + for key in unsupported_keys { + let key_clone = key.clone(); + let supported_properties = core.get_property(&DeviceType::CPU, &key.into()); + assert!( + supported_properties.is_err(), + "Failed on unsupported key: {:?}", + &key_clone + ); + } + } } diff --git a/crates/openvino/src/property.rs b/crates/openvino/src/property.rs index f04bd0d..24d2ff8 100644 --- a/crates/openvino/src/property.rs +++ b/crates/openvino/src/property.rs @@ -32,7 +32,7 @@ pub enum PropertyKey { } /// Read-write property keys. -#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] +#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Clone)] pub enum RwPropertyKey { /// The directory which will be used to store any data cached by plugins. CacheDir, @@ -134,3 +134,9 @@ impl AsRef for RwPropertyKey { } } } + +impl From for PropertyKey { + fn from(key: RwPropertyKey) -> Self { + PropertyKey::Rw(key) + } +}