From 30fa16c951ef6501e03b491e52587dc9a1e0f2ca Mon Sep 17 00:00:00 2001 From: Rahul Date: Tue, 28 May 2024 13:50:54 -0700 Subject: [PATCH] Add `as_mut_ptr` and inline hints (#116) * use as_mut_ptr and as_ptr * add inline hint * rename as_ptr and misc * update node::new to node::from_ptr --- crates/openvino/src/core.rs | 2 +- crates/openvino/src/dimension.rs | 3 +++ crates/openvino/src/layout.rs | 3 ++- crates/openvino/src/model.rs | 22 +++++++++++---------- crates/openvino/src/node.rs | 3 ++- crates/openvino/src/partial_shape.rs | 1 + crates/openvino/src/prepostprocess.rs | 16 +++++++-------- crates/openvino/src/rank.rs | 5 +++++ crates/openvino/src/request.rs | 1 + crates/openvino/src/shape.rs | 3 +++ crates/openvino/src/tensor.rs | 6 ++++-- crates/openvino/tests/classify-alexnet.rs | 4 ++-- crates/openvino/tests/classify-inception.rs | 4 ++-- crates/openvino/tests/classify-mobilenet.rs | 4 ++-- 14 files changed, 48 insertions(+), 29 deletions(-) diff --git a/crates/openvino/src/core.rs b/crates/openvino/src/core.rs index 7a8acb2..3dad9a0 100644 --- a/crates/openvino/src/core.rs +++ b/crates/openvino/src/core.rs @@ -218,7 +218,7 @@ impl Core { self.ptr, model_str.as_ptr().cast::(), 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)) diff --git a/crates/openvino/src/dimension.rs b/crates/openvino/src/dimension.rs index 71e8c2c..7cc5787 100644 --- a/crates/openvino/src/dimension.rs +++ b/crates/openvino/src/dimension.rs @@ -17,6 +17,7 @@ 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 }, @@ -24,11 +25,13 @@ impl Dimension { } /// 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 } diff --git a/crates/openvino/src/layout.rs b/crates/openvino/src/layout.rs index b2e93ef..a74df0b 100644 --- a/crates/openvino/src/layout.rs +++ b/crates/openvino/src/layout.rs @@ -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 } diff --git a/crates/openvino/src/model.rs b/crates/openvino/src/model.rs index 3359005..6f15f56 100644 --- a/crates/openvino/src/model.rs +++ b/crates/openvino/src/model.rs @@ -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 } @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/crates/openvino/src/node.rs b/crates/openvino/src/node.rs index 2371388..3bb3971 100644 --- a/crates/openvino/src/node.rs +++ b/crates/openvino/src/node.rs @@ -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 } } diff --git a/crates/openvino/src/partial_shape.rs b/crates/openvino/src/partial_shape.rs index c4f457b..b7d90cb 100644 --- a/crates/openvino/src/partial_shape.rs +++ b/crates/openvino/src/partial_shape.rs @@ -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 } } diff --git a/crates/openvino/src/prepostprocess.rs b/crates/openvino/src/prepostprocess.rs index 2ce34ee..257ad7b 100644 --- a/crates/openvino/src/prepostprocess.rs +++ b/crates/openvino/src/prepostprocess.rs @@ -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::{ @@ -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() )) } } @@ -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() )) } @@ -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(), )) } diff --git a/crates/openvino/src/rank.rs b/crates/openvino/src/rank.rs index 4031a85..2d5afb5 100644 --- a/crates/openvino/src/rank.rs +++ b/crates/openvino/src/rank.rs @@ -17,16 +17,19 @@ 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 }, @@ -34,11 +37,13 @@ impl Rank { } /// 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 } diff --git a/crates/openvino/src/request.rs b/crates/openvino/src/request.rs index ffbc1a4..873a50e 100644 --- a/crates/openvino/src/request.rs +++ b/crates/openvino/src/request.rs @@ -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 } } diff --git a/crates/openvino/src/shape.rs b/crates/openvino/src/shape.rs index f7f1fa1..81b71e6 100644 --- a/crates/openvino/src/shape.rs +++ b/crates/openvino/src/shape.rs @@ -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 } diff --git a/crates/openvino/src/tensor.rs b/crates/openvino/src/tensor.rs index d317534..ca9dc45 100644 --- a/crates/openvino/src/tensor.rs +++ b/crates/openvino/src/tensor.rs @@ -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 } } @@ -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 { + pub fn set_shape(&self, shape: Shape) -> Result { try_unsafe!(ov_tensor_set_shape(self.ptr, shape.as_c_struct()))?; Ok(Self { ptr: self.ptr }) } diff --git a/crates/openvino/tests/classify-alexnet.rs b/crates/openvino/tests/classify-alexnet.rs index e3b1dc1..9ef87e6 100644 --- a/crates/openvino/tests/classify-alexnet.rs +++ b/crates/openvino/tests/classify-alexnet.rs @@ -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)?; diff --git a/crates/openvino/tests/classify-inception.rs b/crates/openvino/tests/classify-inception.rs index c0bc7ce..30829a9 100644 --- a/crates/openvino/tests/classify-inception.rs +++ b/crates/openvino/tests/classify-inception.rs @@ -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. diff --git a/crates/openvino/tests/classify-mobilenet.rs b/crates/openvino/tests/classify-mobilenet.rs index 4810f71..ca812d7 100644 --- a/crates/openvino/tests/classify-mobilenet.rs +++ b/crates/openvino/tests/classify-mobilenet.rs @@ -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)?;