Skip to content

Commit 0d93626

Browse files
rahulchaphalkarBTOdellabrown
authored
Add compiled model functions (#115)
* Add compiled model functions Co-authored-by: Bradley Odell <[email protected]> * renamed with get_ and misc * Rewrite comment Co-authored-by: Andrew Brown <[email protected]> * Rewrite comment Co-authored-by: Andrew Brown <[email protected]> --------- Co-authored-by: Bradley Odell <[email protected]> Co-authored-by: Andrew Brown <[email protected]>
1 parent a3731ac commit 0d93626

File tree

1 file changed

+124
-3
lines changed

1 file changed

+124
-3
lines changed

crates/openvino/src/model.rs

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
55
use crate::node::Node;
66
use crate::request::InferRequest;
7-
use crate::{drop_using_function, try_unsafe, util::Result};
7+
use crate::{cstr, drop_using_function, try_unsafe, util::Result, PropertyKey, RwPropertyKey};
88
use openvino_sys::{
9-
ov_compiled_model_create_infer_request, ov_compiled_model_free, ov_compiled_model_t,
9+
ov_compiled_model_create_infer_request, ov_compiled_model_free, ov_compiled_model_get_property,
10+
ov_compiled_model_get_runtime_model, ov_compiled_model_input, ov_compiled_model_input_by_index,
11+
ov_compiled_model_input_by_name, ov_compiled_model_inputs_size, ov_compiled_model_output,
12+
ov_compiled_model_output_by_index, ov_compiled_model_output_by_name,
13+
ov_compiled_model_outputs_size, ov_compiled_model_set_property, ov_compiled_model_t,
1014
ov_model_const_input_by_index, ov_model_const_output_by_index, ov_model_free,
1115
ov_model_inputs_size, ov_model_is_dynamic, ov_model_outputs_size, ov_model_t,
1216
};
17+
use std::borrow::Cow;
18+
use std::ffi::CStr;
1319

14-
/// See [`Model`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__model__c__api.html).
20+
/// See [`Model`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__model__c__api.html).
1521
pub struct Model {
1622
ptr: *mut ov_model_t,
1723
}
@@ -107,4 +113,119 @@ impl CompiledModel {
107113
))?;
108114
Ok(InferRequest::from_ptr(infer_request))
109115
}
116+
117+
/// Get the number of inputs of the compiled model.
118+
pub fn get_input_size(&self) -> Result<usize> {
119+
let mut input_size: usize = 0;
120+
try_unsafe!(ov_compiled_model_inputs_size(self.ptr, &mut input_size))?;
121+
Ok(input_size)
122+
}
123+
124+
/// Get the single input port of the compiled model, which we expect to have only one input.
125+
pub fn get_input(&self) -> Result<Node> {
126+
let mut port = std::ptr::null_mut();
127+
try_unsafe!(ov_compiled_model_input(
128+
self.ptr,
129+
std::ptr::addr_of_mut!(port)
130+
))?;
131+
Ok(Node::new(port))
132+
}
133+
134+
/// Get an input port of the compiled model by port index.
135+
pub fn get_input_by_index(&self, index: usize) -> Result<Node> {
136+
let mut port = std::ptr::null_mut();
137+
try_unsafe!(ov_compiled_model_input_by_index(
138+
self.ptr,
139+
index,
140+
std::ptr::addr_of_mut!(port)
141+
))?;
142+
Ok(Node::new(port))
143+
}
144+
145+
/// Get an input port of the compiled model by name.
146+
pub fn get_input_by_name(&self, name: &str) -> Result<Node> {
147+
let name = cstr!(name);
148+
let mut port = std::ptr::null_mut();
149+
try_unsafe!(ov_compiled_model_input_by_name(
150+
self.ptr,
151+
name,
152+
std::ptr::addr_of_mut!(port)
153+
))?;
154+
Ok(Node::new(port))
155+
}
156+
157+
/// Get the number of outputs of the compiled model.
158+
pub fn get_output_size(&self) -> Result<usize> {
159+
let mut output_size: usize = 0;
160+
try_unsafe!(ov_compiled_model_outputs_size(self.ptr, &mut output_size))?;
161+
Ok(output_size)
162+
}
163+
164+
/// Get the single output port of the compiled model, which we expect to have a single output.
165+
pub fn get_output(&self) -> Result<Node> {
166+
let mut port = std::ptr::null_mut();
167+
try_unsafe!(ov_compiled_model_output(
168+
self.ptr,
169+
std::ptr::addr_of_mut!(port)
170+
))?;
171+
Ok(Node::new(port))
172+
}
173+
174+
/// Get an output port of the compiled model by port index.
175+
pub fn get_output_by_index(&self, index: usize) -> Result<Node> {
176+
let mut port = std::ptr::null_mut();
177+
try_unsafe!(ov_compiled_model_output_by_index(
178+
self.ptr,
179+
index,
180+
std::ptr::addr_of_mut!(port)
181+
))?;
182+
Ok(Node::new(port))
183+
}
184+
185+
/// Get an output port of the compiled model by name.
186+
pub fn get_output_by_name(&self, name: &str) -> Result<Node> {
187+
let name = cstr!(name);
188+
let mut port = std::ptr::null_mut();
189+
try_unsafe!(ov_compiled_model_output_by_name(
190+
self.ptr,
191+
name,
192+
std::ptr::addr_of_mut!(port)
193+
))?;
194+
Ok(Node::new(port))
195+
}
196+
197+
/// Gets runtime model information from a device.
198+
pub fn get_runtime_model(&self) -> Result<Model> {
199+
let mut ptr = std::ptr::null_mut();
200+
try_unsafe!(ov_compiled_model_get_runtime_model(
201+
self.ptr,
202+
std::ptr::addr_of_mut!(ptr)
203+
))?;
204+
Ok(Model { ptr })
205+
}
206+
207+
/// Gets a property for the compiled model.
208+
pub fn get_property(&self, key: PropertyKey) -> Result<Cow<str>> {
209+
let ov_prop_key = cstr!(key.as_ref());
210+
let mut ov_prop_value = std::ptr::null_mut();
211+
try_unsafe!(ov_compiled_model_get_property(
212+
self.ptr,
213+
ov_prop_key,
214+
std::ptr::addr_of_mut!(ov_prop_value)
215+
))?;
216+
let rust_prop = unsafe { CStr::from_ptr(ov_prop_value) }.to_string_lossy();
217+
Ok(rust_prop)
218+
}
219+
220+
/// Sets a property for the compiled model.
221+
pub fn set_property(&mut self, key: RwPropertyKey, value: &str) -> Result<()> {
222+
let ov_prop_key = cstr!(key.as_ref());
223+
let ov_prop_value = cstr!(value);
224+
try_unsafe!(ov_compiled_model_set_property(
225+
self.ptr,
226+
ov_prop_key,
227+
ov_prop_value,
228+
))?;
229+
Ok(())
230+
}
110231
}

0 commit comments

Comments
 (0)