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

Support loading non-string models from memory #107

Merged
merged 7 commits into from
May 17, 2024
Merged
Changes from 5 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
23 changes: 18 additions & 5 deletions crates/openvino/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use openvino_sys::{
ov_core_read_model, ov_core_read_model_from_memory_buffer, ov_core_t,
};

use std::os::raw::c_char;

/// See [`Core`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__core__c__api.html).
pub struct Core {
ptr: *mut ov_core_t,
Expand Down Expand Up @@ -51,17 +53,19 @@ impl Core {
}

/// Read model with model and weights loaded in memory.
pub fn read_model_from_buffer(
pub fn read_model_from_buffer<'a, T: Into<Option<&'a Tensor>>>(
&mut self,
model_str: &str,
weights_buffer: &Tensor,
model_str: &[u8],
weights_buffer: T,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose we move back to the Option<&Tensor> like you had; it's a bit more straightforward and avoids potential concerns with code size, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

) -> Result<Model> {
let mut ptr = std::ptr::null_mut();
try_unsafe!(ov_core_read_model_from_memory_buffer(
self.ptr,
cstr!(model_str),
model_str.as_ptr().cast::<c_char>(),
model_str.len(),
weights_buffer.as_ptr(),
weights_buffer
.into()
.map_or(std::ptr::null(), |tensor| tensor.as_ptr().cast_const()),
std::ptr::addr_of_mut!(ptr)
))?;
Ok(Model::from_ptr(ptr))
Expand All @@ -85,9 +89,18 @@ impl Core {
#[cfg(test)]
mod core_tests {
use super::*;

#[test]
fn test_new() {
let core = Core::new();
assert!(core.is_ok());
}

#[test]
fn test_load_onnx_from_buffer() {
let model = b"\x08\x07\x12\nonnx-wally:j\n*\n\x06inputs\x12\x07outputs\x1a\ridentity_node\"\x08Identity\x12\x0bno-op-modelZ\x16\n\x06inputs\x12\x0c\n\n\x08\x01\x12\x06\n\x00\n\x02\x08\x02b\x17\n\x07outputs\x12\x0c\n\n\x08\x01\x12\x06\n\x00\n\x02\x08\x02B\x02\x10\x0c";
let mut core = Core::new().unwrap();
let model = core.read_model_from_buffer(model, None);
assert!(model.is_ok());
}
}
Loading