Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 62 additions & 55 deletions crates/openvino/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String> {
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<Item = (RwPropertyKey, &'a str)>,
) -> 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<String> {
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<String> {
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(
Expand All @@ -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(
Expand All @@ -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<Item = (RwPropertyKey, &'a str)>,
) -> 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(())
}
Expand Down Expand Up @@ -246,6 +202,8 @@ impl Core {
#[cfg(test)]
mod core_tests {
use super::*;
use PropertyKey::*;
use RwPropertyKey::*;

#[test]
fn test_new() {
Expand All @@ -260,4 +218,53 @@ 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 supported_properties = core.get_property(&DeviceType::CPU, &PropertyKey::Rw(&key));
assert!(
supported_properties.is_ok(),
"Failed on rw key: {:?}",
&PropertyKey::Rw(&key)
);
}
}
}
6 changes: 3 additions & 3 deletions crates/openvino/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
/// See [`Property`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__property__c__api.html).
/// `PropertyKey` represents valid configuration properties for a [crate::Core] instance.
#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum PropertyKey {
pub enum PropertyKey<'a> {
/// A string list of supported read-only properties.
SupportedProperties,
/// A list of available device IDs.
Expand All @@ -26,7 +26,7 @@ pub enum PropertyKey {
/// Maximum batch size which does not cause performance degradation due to memory swap impact.
MaxBatchSize,
/// Read-write property key.
Rw(RwPropertyKey),
Rw(&'a RwPropertyKey),
/// Arbitrary string property key.
Other(Cow<'static, str>),
}
Expand Down Expand Up @@ -89,7 +89,7 @@ pub enum RwPropertyKey {
Other(Cow<'static, str>),
}

impl AsRef<str> for PropertyKey {
impl AsRef<str> for PropertyKey<'_> {
fn as_ref(&self) -> &str {
match self {
PropertyKey::SupportedProperties => "SUPPORTED_PROPERTIES",
Expand Down