Skip to content

Commit 2f5bdd3

Browse files
add property and version modules (#109)
* add property and version modules Co-authored-by: Bradley Odell <[email protected]> * add doc for property --------- Co-authored-by: Bradley Odell <[email protected]>
1 parent 1aacdf9 commit 2f5bdd3

File tree

3 files changed

+165
-9
lines changed

3 files changed

+165
-9
lines changed

crates/openvino/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! Check the loaded version of OpenVINO:
88
//! ```
9-
//! assert!(openvino::version().starts_with("2"))
9+
//! assert!(openvino::version().build_number.starts_with("2"))
1010
//! ```
1111
//!
1212
//! Most interaction with OpenVINO begins with instantiating a [Core]:
@@ -35,12 +35,14 @@ mod model;
3535
mod node;
3636
mod partial_shape;
3737
pub mod prepostprocess;
38+
mod property;
3839
mod rank;
3940
mod request;
4041
mod resize_algorithm;
4142
mod shape;
4243
mod tensor;
4344
mod util;
45+
mod version;
4446

4547
pub use crate::core::Core;
4648
pub use device_type::DeviceType;
@@ -51,30 +53,28 @@ pub use layout::Layout;
5153
pub use model::{CompiledModel, Model};
5254
pub use node::Node;
5355
pub use partial_shape::PartialShape;
56+
pub use property::{PropertyKey, RwPropertyKey};
5457
pub use rank::Rank;
5558
pub use request::InferRequest;
5659
pub use resize_algorithm::ResizeAlgorithm;
5760
pub use shape::Shape;
5861
pub use tensor::Tensor;
62+
pub use version::Version;
5963

6064
/// Emit the version string of the OpenVINO C API backing this implementation.
6165
///
6266
/// # Panics
6367
///
6468
/// Panics if no OpenVINO library can be found.
65-
pub fn version() -> String {
66-
use std::ffi::CStr;
69+
pub fn version() -> Version {
6770
openvino_sys::load().expect("to have an OpenVINO shared library available");
6871
let mut ov_version = openvino_sys::ov_version_t {
69-
// Initialize the fields to default values
70-
description: std::ptr::null(),
7172
buildNumber: std::ptr::null(),
73+
description: std::ptr::null(),
7274
};
7375
let code = unsafe { openvino_sys::ov_get_openvino_version(&mut ov_version) };
7476
assert_eq!(code, 0);
75-
let version_ptr = { ov_version }.buildNumber;
76-
let c_str_version = unsafe { CStr::from_ptr(version_ptr) };
77-
let string_version = c_str_version.to_string_lossy().into_owned();
77+
let version = Version::from(&ov_version);
7878
unsafe { openvino_sys::ov_version_free(std::ptr::addr_of_mut!(ov_version)) };
79-
string_version
79+
version
8080
}

crates/openvino/src/property.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
use std::borrow::Cow;
2+
3+
/// See [`Property`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__property__c__api.html).
4+
/// `PropertyKey` represents valid configuration properties for a [crate::Core] instance.
5+
#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
6+
pub enum PropertyKey {
7+
/// A string list of supported read-only properties.
8+
SupportedProperties,
9+
/// A list of available device IDs.
10+
AvailableDevices,
11+
/// An unsigned integer value of optimal number of compiled model infer requests.
12+
OptimalNumberOfInferRequests,
13+
/// A hint for a range for number of async infer requests.
14+
/// If device supports streams, the metric provides range for number of IRs per stream.
15+
RangeForAsyncInferRequests,
16+
/// Information about a range for streams on platforms where streams are supported.
17+
RangeForStreams,
18+
/// A string value representing a full device name.
19+
DeviceFullName,
20+
/// A string list of capabilities options per device.
21+
DeviceCapabilities,
22+
/// The name of a model.
23+
ModelName,
24+
/// Information about optimal batch size for the given device and network.
25+
OptimalBatchSize,
26+
/// Maximum batch size which does not cause performance degradation due to memory swap impact.
27+
MaxBatchSize,
28+
/// Read-write property key.
29+
Rw(RwPropertyKey),
30+
/// Arbitrary string property key.
31+
Other(Cow<'static, str>),
32+
}
33+
34+
/// Read-write property keys.
35+
#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
36+
pub enum RwPropertyKey {
37+
/// The directory which will be used to store any data cached by plugins.
38+
CacheDir,
39+
/// The cache mode between optimize_size and optimize_speed.
40+
/// If optimize_size is selected, smaller cache files will be created.
41+
/// If optimize_speed is selected, loading time will decrease but the cache file size will increase.
42+
CacheMode,
43+
/// The number of executor logical partitions.
44+
NumStreams,
45+
/// CPU affinity per thread.
46+
Affinity,
47+
/// The maximum number of threads that can be used for inference tasks.
48+
InferenceNumThreads,
49+
/// High-level OpenVINO hint for using CPU pinning to bind CPU threads to processors during inference.
50+
HintEnableCpuPinning,
51+
/// High-level OpenVINO hint for using hyper threading processors during CPU inference.
52+
HintEnableHyperThreading,
53+
/// High-level OpenVINO Performance Hints.
54+
HintPerformanceMode,
55+
/// High-level OpenVINO Hints for the type of CPU core used during inference.
56+
HintSchedulingCoreType,
57+
/// Hint for device to use specified precision for inference.
58+
HintInferencePrecision,
59+
/// Backs the Performance Hints by giving
60+
/// additional information on how many inference requests the application will be
61+
/// keeping in flight usually this value comes from the actual use-case (e.g.
62+
/// number of video-cameras, or other sources of inputs)
63+
HintNumRequests,
64+
/// Desirable log level.
65+
LogLevel,
66+
/// High-level OpenVINO model priority hint.
67+
HintModelPriority,
68+
/// Performance counters.
69+
EnableProfiling,
70+
/// Device Priorities config option,
71+
/// with comma-separated devices listed in the desired priority.
72+
DevicePriorities,
73+
/// High-level OpenVINO Execution hint
74+
/// unlike low-level properties that are individual (per-device), the hints are something that every device accepts
75+
/// and turns into device-specific settings
76+
/// Execution mode hint controls preferred optimization targets (performance or accuracy) for given model
77+
///
78+
/// It can be set to be below value:
79+
/// - `"PERFORMANCE"`: Optimize for max performance
80+
/// - `"ACCURACY"`: Optimize for max accuracy
81+
HintExecutionMode,
82+
/// Whether to force terminate TBB when OV Core is destroyed.
83+
ForceTbbTerminate,
84+
/// Configure `mmap()` use for model read.
85+
EnableMmap,
86+
/// ?
87+
AutoBatchTimeout,
88+
/// Arbitrary string property key.
89+
Other(Cow<'static, str>),
90+
}
91+
92+
impl AsRef<str> for PropertyKey {
93+
fn as_ref(&self) -> &str {
94+
match self {
95+
PropertyKey::SupportedProperties => "SUPPORTED_PROPERTIES",
96+
PropertyKey::AvailableDevices => "AVAILABLE_DEVICES",
97+
PropertyKey::OptimalNumberOfInferRequests => "OPTIMAL_NUMBER_OF_INFER_REQUESTS",
98+
PropertyKey::RangeForAsyncInferRequests => "RANGE_FOR_ASYNC_INFER_REQUESTS",
99+
PropertyKey::RangeForStreams => "RANGE_FOR_STREAMS",
100+
PropertyKey::DeviceFullName => "FULL_DEVICE_NAME",
101+
PropertyKey::DeviceCapabilities => "OPTIMIZATION_CAPABILITIES",
102+
PropertyKey::ModelName => "NETWORK_NAME",
103+
PropertyKey::OptimalBatchSize => "OPTIMAL_BATCH_SIZE",
104+
PropertyKey::MaxBatchSize => "MAX_BATCH_SIZE",
105+
PropertyKey::Rw(rw) => rw.as_ref(),
106+
PropertyKey::Other(s) => s,
107+
}
108+
}
109+
}
110+
111+
impl AsRef<str> for RwPropertyKey {
112+
fn as_ref(&self) -> &str {
113+
match self {
114+
RwPropertyKey::CacheDir => "CACHE_DIR",
115+
RwPropertyKey::CacheMode => "CACHE_MODE",
116+
RwPropertyKey::NumStreams => "NUM_STREAMS",
117+
RwPropertyKey::Affinity => "AFFINITY",
118+
RwPropertyKey::InferenceNumThreads => "INFERENCE_NUM_THREADS",
119+
RwPropertyKey::HintEnableCpuPinning => "ENABLE_CPU_PINNING",
120+
RwPropertyKey::HintEnableHyperThreading => "ENABLE_HYPER_THREADING",
121+
RwPropertyKey::HintPerformanceMode => "PERFORMANCE_HINT",
122+
RwPropertyKey::HintSchedulingCoreType => "SCHEDULING_CORE_TYPE",
123+
RwPropertyKey::HintInferencePrecision => "INFERENCE_PRECISION_HINT",
124+
RwPropertyKey::HintNumRequests => "PERFORMANCE_HINT_NUM_REQUESTS",
125+
RwPropertyKey::LogLevel => "LOG_LEVEL",
126+
RwPropertyKey::HintModelPriority => "MODEL_PRIORITY",
127+
RwPropertyKey::EnableProfiling => "PERF_COUNT",
128+
RwPropertyKey::DevicePriorities => "MULTI_DEVICE_PRIORITIES",
129+
RwPropertyKey::HintExecutionMode => "EXECUTION_MODE_HINT",
130+
RwPropertyKey::ForceTbbTerminate => "FORCE_TBB_TERMINATE",
131+
RwPropertyKey::EnableMmap => "ENABLE_MMAP",
132+
RwPropertyKey::AutoBatchTimeout => "AUTO_BATCH_TIMEOUT",
133+
RwPropertyKey::Other(s) => s,
134+
}
135+
}
136+
}

crates/openvino/src/version.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use openvino_sys::ov_version_t;
2+
3+
/// Represents OpenVINO version information.
4+
pub struct Version {
5+
/// A string representing OpenVINO version.
6+
pub build_number: String,
7+
/// A string representing OpenVINO description.
8+
pub description: String,
9+
}
10+
11+
impl From<&ov_version_t> for Version {
12+
fn from(ov_version: &ov_version_t) -> Self {
13+
let c_str_version = unsafe { std::ffi::CStr::from_ptr(ov_version.buildNumber) };
14+
let c_str_description = unsafe { std::ffi::CStr::from_ptr(ov_version.description) };
15+
Self {
16+
build_number: c_str_version.to_string_lossy().into_owned(),
17+
description: c_str_description.to_string_lossy().into_owned(),
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)