Skip to content

Commit 0045461

Browse files
Fix core get/set properties (#124)
* Remove property functions which allow empty device string * Add tests * remove loglevel property, incompatible with ov 2023.3/2 * remove lifetime specifier, add From trait
1 parent 5d4f47a commit 0045461

File tree

2 files changed

+92
-56
lines changed

2 files changed

+92
-56
lines changed

crates/openvino/src/core.rs

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use std::os::raw::c_char;
1616
use std::slice;
1717
use std::str::FromStr;
1818

19-
const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
20-
2119
/// See [`Core`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__core__c__api.html).
2220
pub struct Core {
2321
ptr: *mut ov_core_t,
@@ -103,52 +101,10 @@ impl Core {
103101
Ok(devices)
104102
}
105103

106-
/// Gets properties related to this Core.
107-
/// The method extracts information that can be set via the [set_property] method.
108-
pub fn get_property(&self, key: PropertyKey) -> Result<String> {
109-
let ov_prop_key = cstr!(key.as_ref());
110-
let mut ov_prop_value = std::ptr::null_mut();
111-
try_unsafe!(ov_core_get_property(
112-
self.ptr,
113-
EMPTY_C_STR.as_ptr(),
114-
ov_prop_key.as_ptr(),
115-
std::ptr::addr_of_mut!(ov_prop_value)
116-
))?;
117-
let rust_prop = unsafe { CStr::from_ptr(ov_prop_value) }
118-
.to_str()
119-
.unwrap()
120-
.to_owned();
121-
Ok(rust_prop)
122-
}
123-
124-
/// Sets a property for this Core instance.
125-
pub fn set_property(&mut self, key: RwPropertyKey, value: &str) -> Result<()> {
126-
let ov_prop_key = cstr!(key.as_ref());
127-
let ov_prop_value = cstr!(value);
128-
try_unsafe!(ov_core_set_property(
129-
self.ptr,
130-
EMPTY_C_STR.as_ptr(),
131-
ov_prop_key.as_ptr(),
132-
ov_prop_value.as_ptr(),
133-
))?;
134-
Ok(())
135-
}
136-
137-
/// Sets properties for this Core instance.
138-
pub fn set_properties<'a>(
139-
&mut self,
140-
properties: impl IntoIterator<Item = (RwPropertyKey, &'a str)>,
141-
) -> Result<()> {
142-
for (prop_key, prop_value) in properties {
143-
self.set_property(prop_key, prop_value)?;
144-
}
145-
Ok(())
146-
}
147-
148-
/// Gets properties related to device behaviour.
149-
/// The method extracts information that can be set via the [set_device_property] method.
150-
pub fn get_device_property(&self, device_name: &str, key: PropertyKey) -> Result<String> {
151-
let ov_device_name = cstr!(device_name);
104+
/// Gets properties related to device behaviour for this core.
105+
/// The method extracts information that can be set via the [`set_property`] method.
106+
pub fn get_property(&self, device_name: &DeviceType, key: &PropertyKey) -> Result<String> {
107+
let ov_device_name = cstr!(device_name.as_ref());
152108
let ov_prop_key = cstr!(key.as_ref());
153109
let mut ov_prop_value = std::ptr::null_mut();
154110
try_unsafe!(ov_core_get_property(
@@ -165,13 +121,13 @@ impl Core {
165121
}
166122

167123
/// Sets a property for a device.
168-
pub fn set_device_property(
124+
pub fn set_property(
169125
&mut self,
170-
device_name: &str,
171-
key: RwPropertyKey,
126+
device_name: &DeviceType,
127+
key: &RwPropertyKey,
172128
value: &str,
173129
) -> Result<()> {
174-
let ov_device_name = cstr!(device_name);
130+
let ov_device_name = cstr!(device_name.as_ref());
175131
let ov_prop_key = cstr!(key.as_ref());
176132
let ov_prop_value = cstr!(value);
177133
try_unsafe!(ov_core_set_property(
@@ -184,13 +140,13 @@ impl Core {
184140
}
185141

186142
/// Sets properties for a device.
187-
pub fn set_device_properties<'a>(
143+
pub fn set_properties<'a>(
188144
&mut self,
189-
device_name: &str,
145+
device_name: DeviceType,
190146
properties: impl IntoIterator<Item = (RwPropertyKey, &'a str)>,
191147
) -> Result<()> {
192148
for (prop_key, prop_value) in properties {
193-
self.set_device_property(device_name, prop_key, prop_value)?;
149+
self.set_property(&device_name, &prop_key, prop_value)?;
194150
}
195151
Ok(())
196152
}
@@ -246,6 +202,8 @@ impl Core {
246202
#[cfg(test)]
247203
mod core_tests {
248204
use super::*;
205+
use PropertyKey::*;
206+
use RwPropertyKey::*;
249207

250208
#[test]
251209
fn test_new() {
@@ -260,4 +218,76 @@ mod core_tests {
260218
let model = core.read_model_from_buffer(model, None);
261219
assert!(model.is_ok());
262220
}
221+
222+
#[test]
223+
fn test_get_core_properties_supported() {
224+
let core = Core::new().unwrap();
225+
let supported_keys = vec![
226+
SupportedProperties,
227+
AvailableDevices,
228+
OptimalNumberOfInferRequests,
229+
RangeForAsyncInferRequests,
230+
RangeForStreams,
231+
DeviceFullName,
232+
DeviceCapabilities,
233+
];
234+
for key in supported_keys {
235+
let supported_properties = core.get_property(&DeviceType::CPU, &key);
236+
assert!(
237+
supported_properties.is_ok(),
238+
"Failed on supported key: {:?}",
239+
&key
240+
);
241+
}
242+
}
243+
244+
#[test]
245+
fn test_get_core_properties_rw() {
246+
let core = Core::new().unwrap();
247+
let rw_keys = vec![
248+
CacheDir,
249+
NumStreams,
250+
Affinity,
251+
InferenceNumThreads,
252+
HintEnableCpuPinning,
253+
HintEnableHyperThreading,
254+
HintPerformanceMode,
255+
HintSchedulingCoreType,
256+
HintInferencePrecision,
257+
HintNumRequests,
258+
EnableProfiling,
259+
HintExecutionMode,
260+
];
261+
for key in rw_keys {
262+
let key_clone = key.clone();
263+
let supported_properties = core.get_property(&DeviceType::CPU, &key.into());
264+
assert!(
265+
supported_properties.is_ok(),
266+
"Failed on rw key: {:?}",
267+
&PropertyKey::Rw(key_clone)
268+
);
269+
}
270+
}
271+
272+
#[test]
273+
fn test_get_core_properties_unsupported() {
274+
let core = Core::new().unwrap();
275+
let unsupported_keys = vec![
276+
HintModelPriority,
277+
DevicePriorities,
278+
CacheMode,
279+
ForceTbbTerminate,
280+
EnableMmap,
281+
AutoBatchTimeout,
282+
];
283+
for key in unsupported_keys {
284+
let key_clone = key.clone();
285+
let supported_properties = core.get_property(&DeviceType::CPU, &key.into());
286+
assert!(
287+
supported_properties.is_err(),
288+
"Failed on unsupported key: {:?}",
289+
&key_clone
290+
);
291+
}
292+
}
263293
}

crates/openvino/src/property.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub enum PropertyKey {
3232
}
3333

3434
/// Read-write property keys.
35-
#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
35+
#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Clone)]
3636
pub enum RwPropertyKey {
3737
/// The directory which will be used to store any data cached by plugins.
3838
CacheDir,
@@ -134,3 +134,9 @@ impl AsRef<str> for RwPropertyKey {
134134
}
135135
}
136136
}
137+
138+
impl From<RwPropertyKey> for PropertyKey {
139+
fn from(key: RwPropertyKey) -> Self {
140+
PropertyKey::Rw(key)
141+
}
142+
}

0 commit comments

Comments
 (0)