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

remove tensor new_from_host_ptr function #125

Merged
merged 1 commit into from
Jun 25, 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
4 changes: 3 additions & 1 deletion crates/openvino/src/prepostprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
//! # ).expect("to read the model from file");
//! # let data = fs::read("tests/fixtures/inception/tensor-1x3x299x299-f32.bgr").expect("to read the tensor from file");
//! # let input_shape = Shape::new(&vec![1, 299, 299, 3]).expect("to create a new shape");
//! # let tensor = Tensor::new_from_host_ptr(ElementType::F32, &input_shape, &data).expect("to create a new tensor from host pointer");
//! # let mut tensor = Tensor::new(ElementType::F32, &input_shape).expect("to create a new tensor");
//! # let buffer = tensor.buffer_mut().unwrap();
//! # buffer.copy_from_slice(&data);
//! // Insantiate a new core, read in a model, and set up a tensor with input data before performing pre/post processing
//! // Pre-process the input by:
//! // - converting NHWC to NCHW
Expand Down
30 changes: 3 additions & 27 deletions crates/openvino/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::element_type::ElementType;
use crate::shape::Shape;
use crate::{drop_using_function, try_unsafe, util::Result};
use openvino_sys::{
self, ov_shape_t, ov_tensor_create, ov_tensor_create_from_host_ptr, ov_tensor_data,
ov_tensor_free, ov_tensor_get_byte_size, ov_tensor_get_element_type, ov_tensor_get_shape,
ov_tensor_get_size, ov_tensor_set_shape, ov_tensor_t,
self, ov_shape_t, ov_tensor_create, ov_tensor_data, ov_tensor_free, ov_tensor_get_byte_size,
ov_tensor_get_element_type, ov_tensor_get_shape, ov_tensor_get_size, ov_tensor_set_shape,
ov_tensor_t,
};

/// See [`Tensor`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__tensor__c__api.html).
Expand All @@ -34,30 +34,6 @@ impl Tensor {
Self { ptr }
}

/// Create a new [`Tensor`] from a host pointer.
///
/// # Arguments
///
/// * `data_type` - The data type of the tensor.
/// * `shape` - The shape of the tensor.
/// * `data` - The data buffer.
///
/// # Returns
///
/// A new `Tensor` object.
pub fn new_from_host_ptr(data_type: ElementType, shape: &Shape, data: &[u8]) -> Result<Self> {
let mut tensor: *mut ov_tensor_t = std::ptr::null_mut();
let element_type: u32 = data_type as u32;
let buffer = data.as_ptr() as *mut std::os::raw::c_void;
try_unsafe!(ov_tensor_create_from_host_ptr(
element_type,
shape.as_c_struct(),
buffer,
std::ptr::addr_of_mut!(tensor)
))?;
Ok(Self { ptr: tensor })
}

/// Get the pointer to the underlying OpenVINO tensor.
#[inline]
pub(crate) fn as_ptr(&self) -> *const ov_tensor_t {
Expand Down
4 changes: 3 additions & 1 deletion crates/openvino/tests/classify-alexnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fn classify_alexnet() -> anyhow::Result<()> {
let data = fs::read(Fixture::tensor())?;
let input_shape = Shape::new(&vec![1, 227, 227, 3])?;
let element_type = ElementType::F32;
let tensor = Tensor::new_from_host_ptr(element_type, &input_shape, &data)?;
let mut tensor = Tensor::new(element_type, &input_shape)?;
let buffer = tensor.buffer_mut()?;
buffer.copy_from_slice(&data);

// Pre-process the input by:
// - converting NHWC to NCHW
Expand Down
4 changes: 3 additions & 1 deletion crates/openvino/tests/classify-inception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fn classify_inception() -> anyhow::Result<()> {
let data = fs::read(Fixture::tensor())?;
let input_shape = Shape::new(&vec![1, 299, 299, 3])?;
let element_type = ElementType::F32;
let tensor = Tensor::new_from_host_ptr(element_type, &input_shape, &data)?;
let mut tensor = Tensor::new(element_type, &input_shape)?;
let buffer = tensor.buffer_mut()?;
buffer.copy_from_slice(&data);

// Pre-process the input by:
// - converting NHWC to NCHW
Expand Down
4 changes: 3 additions & 1 deletion crates/openvino/tests/classify-mobilenet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fn classify_mobilenet() -> anyhow::Result<()> {
let data = fs::read(Fixture::tensor())?;
let input_shape = Shape::new(&vec![1, 224, 224, 3])?;
let element_type = ElementType::F32;
let tensor = Tensor::new_from_host_ptr(element_type, &input_shape, &data)?;
let mut tensor = Tensor::new(element_type, &input_shape)?;
let buffer = tensor.buffer_mut()?;
buffer.copy_from_slice(&data);

// Pre-process the input by:
// - converting NHWC to NCHW
Expand Down
Loading