Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add as_mut_ptr and inline hints #116

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion crates/openvino/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl Core {
self.ptr,
model_str.as_ptr().cast::<c_char>(),
model_str.len(),
weights_buffer.map_or(std::ptr::null(), |tensor| tensor.as_ptr().cast_const()),
weights_buffer.map_or(std::ptr::null(), |tensor| tensor.as_ptr()),
std::ptr::addr_of_mut!(ptr)
))?;
Ok(Model::from_ptr(ptr))
Expand Down
3 changes: 3 additions & 0 deletions crates/openvino/src/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ impl Eq for Dimension {}

impl Dimension {
/// Creates a new Dimension with minimum and maximum values.
#[inline]
pub fn new(min: i64, max: i64) -> Self {
Self {
c_struct: ov_dimension_t { min, max },
}
}

/// Returns the minimum value.
#[inline]
pub fn get_min(&self) -> i64 {
self.c_struct.min
}

/// Returns the maximum value.
#[inline]
pub fn get_max(&self) -> i64 {
self.c_struct.max
}
Expand Down
3 changes: 2 additions & 1 deletion crates/openvino/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ drop_using_function!(Layout, ov_layout_free);

impl Layout {
/// Get a pointer to the [`ov_layout_t`].
pub(crate) fn as_ptr(&self) -> *mut ov_layout_t {
#[inline]
pub(crate) fn as_mut_ptr(&mut self) -> *mut ov_layout_t {
self.ptr
}

Expand Down
22 changes: 12 additions & 10 deletions crates/openvino/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ unsafe impl Sync for Model {}

impl Model {
/// Create a new [`Model`] from an internal pointer.
#[inline]
pub(crate) fn from_ptr(ptr: *mut ov_model_t) -> Self {
Self { ptr }
}

/// Get the pointer to the underlying [`ov_model_t`].
pub(crate) fn as_ptr(&self) -> *mut ov_model_t {
#[inline]
pub(crate) fn as_ptr(&self) -> *const ov_model_t {
self.ptr
}

Expand All @@ -59,7 +61,7 @@ impl Model {
index,
std::ptr::addr_of_mut!(node)
))?;
Ok(Node::new(node))
Ok(Node::from_ptr(node))
}

/// Retrieve the output node by index.
Expand All @@ -70,7 +72,7 @@ impl Model {
index,
std::ptr::addr_of_mut!(node)
))?;
Ok(Node::new(node))
Ok(Node::from_ptr(node))
}

/// Retrieve the constant output node by index.
Expand All @@ -81,7 +83,7 @@ impl Model {
index,
std::ptr::addr_of_mut!(node)
))?;
Ok(Node::new(node))
Ok(Node::from_ptr(node))
}

/// Returns `true` if the model contains dynamic shapes.
Expand Down Expand Up @@ -128,7 +130,7 @@ impl CompiledModel {
self.ptr,
std::ptr::addr_of_mut!(port)
))?;
Ok(Node::new(port))
Ok(Node::from_ptr(port))
}

/// Get an input port of the compiled model by port index.
Expand All @@ -139,7 +141,7 @@ impl CompiledModel {
index,
std::ptr::addr_of_mut!(port)
))?;
Ok(Node::new(port))
Ok(Node::from_ptr(port))
}

/// Get an input port of the compiled model by name.
Expand All @@ -151,7 +153,7 @@ impl CompiledModel {
name,
std::ptr::addr_of_mut!(port)
))?;
Ok(Node::new(port))
Ok(Node::from_ptr(port))
}

/// Get the number of outputs of the compiled model.
Expand All @@ -168,7 +170,7 @@ impl CompiledModel {
self.ptr,
std::ptr::addr_of_mut!(port)
))?;
Ok(Node::new(port))
Ok(Node::from_ptr(port))
}

/// Get an output port of the compiled model by port index.
Expand All @@ -179,7 +181,7 @@ impl CompiledModel {
index,
std::ptr::addr_of_mut!(port)
))?;
Ok(Node::new(port))
Ok(Node::from_ptr(port))
}

/// Get an output port of the compiled model by name.
Expand All @@ -191,7 +193,7 @@ impl CompiledModel {
name,
std::ptr::addr_of_mut!(port)
))?;
Ok(Node::new(port))
Ok(Node::from_ptr(port))
}

/// Gets runtime model information from a device.
Expand Down
3 changes: 2 additions & 1 deletion crates/openvino/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub struct Node {

impl Node {
/// Create a new [`Port`] from [`ov_output_const_port_t`].
pub(crate) fn new(ptr: *mut ov_output_const_port_t) -> Self {
#[inline]
pub(crate) fn from_ptr(ptr: *mut ov_output_const_port_t) -> Self {
Self { ptr }
}

Expand Down
1 change: 1 addition & 0 deletions crates/openvino/src/partial_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl Drop for PartialShape {

impl PartialShape {
/// Create a new partial shape object from `ov_partial_shape_t`.
#[inline]
pub(crate) fn from_c_struct(c_struct: ov_partial_shape_t) -> Self {
Self { c_struct }
}
Expand Down
16 changes: 8 additions & 8 deletions crates/openvino/src/prepostprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
//! let input_info = pipeline.get_input_info_by_name("input").expect("to get input info by name");
//! let mut input_tensor_info = input_info.get_tensor_info().expect("to get tensor info");
//! input_tensor_info.set_from(&tensor).expect("to set tensor from");
//! input_tensor_info.set_layout(&Layout::new("NHWC").expect("to create a new layout")).expect("to set layout");
//! input_tensor_info.set_layout(Layout::new("NHWC").expect("to create a new layout")).expect("to set layout");
//! let mut preprocess_steps = input_info.get_steps().expect("to get preprocess steps");
//! preprocess_steps.resize(ResizeAlgorithm::Linear).expect("to resize");
//! let mut model_info = input_info.get_model_info().expect("to get model info");
//! model_info.set_layout(&Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
//! model_info.set_layout(Layout::new("NCHW").expect("to create a new layout")).expect("to set layout");
//! let new_model = pipeline.build_new_model().expect("to build new model with above prepostprocess steps");
//! ```
use crate::{
Expand Down Expand Up @@ -205,10 +205,10 @@ pub struct InputModelInfo {
drop_using_function!(InputModelInfo, ov_preprocess_input_model_info_free);
impl InputModelInfo {
/// Sets the layout for the model information obj.
pub fn set_layout(&mut self, layout: &Layout) -> Result<()> {
pub fn set_layout(&mut self, mut layout: Layout) -> Result<()> {
try_unsafe!(ov_preprocess_input_model_info_set_layout(
self.ptr,
layout.as_ptr()
layout.as_mut_ptr()
))
}
}
Expand All @@ -220,10 +220,10 @@ pub struct InputTensorInfo {
drop_using_function!(InputTensorInfo, ov_preprocess_input_tensor_info_free);
impl InputTensorInfo {
/// Sets the [`Layout`] for the input tensor.
pub fn set_layout(&mut self, layout: &Layout) -> Result<()> {
pub fn set_layout(&mut self, mut layout: Layout) -> Result<()> {
try_unsafe!(ov_preprocess_input_tensor_info_set_layout(
self.ptr,
layout.as_ptr()
layout.as_mut_ptr()
))
}

Expand Down Expand Up @@ -266,10 +266,10 @@ impl Steps {
}

/// Converts the [`Layout`] of the data in a [`Tensor`].
pub fn convert_layout(&mut self, new_layout: &Layout) -> Result<()> {
pub fn convert_layout(&mut self, mut new_layout: Layout) -> Result<()> {
try_unsafe!(ov_preprocess_preprocess_steps_convert_layout(
self.ptr,
new_layout.as_ptr(),
new_layout.as_mut_ptr(),
))
}

Expand Down
5 changes: 5 additions & 0 deletions crates/openvino/src/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ impl Eq for Rank {}

impl Rank {
/// Get the pointer to the underlying OpenVINO rank.
#[inline]
pub(crate) fn as_c_struct(&self) -> ov_rank_t {
self.c_struct
}

/// Create a new rank object from `ov_rank_t`.
#[inline]
pub(crate) fn from_c_struct(ptr: ov_rank_t) -> Self {
Self { c_struct: ptr }
}

/// Creates a new Rank with minimum and maximum values.
#[inline]
pub fn new(min: i64, max: i64) -> Self {
Self {
c_struct: ov_rank_t { min, max },
}
}

/// Returns the minimum value.
#[inline]
pub fn get_min(&self) -> i64 {
self.c_struct.min
}

/// Returns the maximum value.
#[inline]
pub fn get_max(&self) -> i64 {
self.c_struct.max
}
Expand Down
1 change: 1 addition & 0 deletions crates/openvino/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ unsafe impl Sync for InferRequest {}

impl InferRequest {
/// Create a new [`InferRequest`] from [`ov_infer_request_t`].
#[inline]
pub(crate) fn from_ptr(ptr: *mut ov_infer_request_t) -> Self {
Self { ptr }
}
Expand Down
3 changes: 3 additions & 0 deletions crates/openvino/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ impl Shape {
}

/// Create a new shape object from `ov_shape_t`.
#[inline]
pub(crate) fn from_c_struct(ptr: ov_shape_t) -> Self {
Self { c_struct: ptr }
}

/// Get the pointer to the underlying OpenVINO shape.
#[inline]
pub(crate) fn as_c_struct(&self) -> ov_shape_t {
self.c_struct
}

/// Returns the rank of the shape.
#[inline]
pub fn get_rank(&self) -> i64 {
self.c_struct.rank
}
Expand Down
6 changes: 4 additions & 2 deletions crates/openvino/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Tensor {
}

/// Create a new [`Tensor`] from a pointer.
#[inline]
pub(crate) fn from_ptr(ptr: *mut ov_tensor_t) -> Self {
Self { ptr }
}
Expand Down Expand Up @@ -58,12 +59,13 @@ impl Tensor {
}

/// Get the pointer to the underlying OpenVINO tensor.
pub(crate) fn as_ptr(&self) -> *mut ov_tensor_t {
#[inline]
pub(crate) fn as_ptr(&self) -> *const ov_tensor_t {
self.ptr
}

/// (Re)Set the shape of the tensor to a new shape.
pub fn set_shape(&self, shape: &Shape) -> Result<Self> {
pub fn set_shape(&self, shape: Shape) -> Result<Self> {
try_unsafe!(ov_tensor_set_shape(self.ptr, shape.as_c_struct()))?;
Ok(Self { ptr: self.ptr })
}
Expand Down
4 changes: 2 additions & 2 deletions crates/openvino/tests/classify-alexnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ fn classify_alexnet() -> anyhow::Result<()> {
let input_info = pre_post_process.get_input_info_by_name("data")?;
let mut input_tensor_info = input_info.get_tensor_info()?;
input_tensor_info.set_from(&tensor)?;
input_tensor_info.set_layout(&Layout::new("NHWC")?)?;
input_tensor_info.set_layout(Layout::new("NHWC")?)?;
let mut steps = input_info.get_steps()?;
steps.resize(ResizeAlgorithm::Linear)?;
let mut model_info = input_info.get_model_info()?;
model_info.set_layout(&Layout::new("NCHW")?)?;
model_info.set_layout(Layout::new("NCHW")?)?;
let output_info = pre_post_process.get_output_info_by_index(0)?;
let mut output_tensor_info = output_info.get_tensor_info()?;
output_tensor_info.set_element_type(ElementType::F32)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/openvino/tests/classify-inception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ fn classify_inception() -> anyhow::Result<()> {
let input_info = pre_post_process.get_input_info_by_name("input")?;
let mut input_tensor_info = input_info.get_tensor_info()?;
input_tensor_info.set_from(&tensor)?;
input_tensor_info.set_layout(&Layout::new("NHWC")?)?;
input_tensor_info.set_layout(Layout::new("NHWC")?)?;
let mut steps = input_info.get_steps()?;
steps.resize(ResizeAlgorithm::Linear)?;
let mut model_info = input_info.get_model_info()?;
model_info.set_layout(&Layout::new("NCHW")?)?;
model_info.set_layout(Layout::new("NCHW")?)?;
let new_model = pre_post_process.build_new_model()?;

// Compile the model and infer the results.
Expand Down
4 changes: 2 additions & 2 deletions crates/openvino/tests/classify-mobilenet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ fn classify_mobilenet() -> anyhow::Result<()> {
let input_info = pre_post_process.get_input_info_by_name("input")?;
let mut input_tensor_info = input_info.get_tensor_info()?;
input_tensor_info.set_from(&tensor)?;
input_tensor_info.set_layout(&Layout::new("NHWC")?)?;
input_tensor_info.set_layout(Layout::new("NHWC")?)?;
let mut steps = input_info.get_steps()?;
steps.resize(ResizeAlgorithm::Linear)?;
let mut model_info = input_info.get_model_info()?;
model_info.set_layout(&Layout::new("NCHW")?)?;
model_info.set_layout(Layout::new("NCHW")?)?;
let output_info = pre_post_process.get_output_info_by_index(0)?;
let mut output_tensor_info = output_info.get_tensor_info()?;
output_tensor_info.set_element_type(ElementType::F32)?;
Expand Down
Loading