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
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
6 changes: 1 addition & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ jobs:
apt: [false]
# We also spot-check that things work when installing from APT by adding to the matrix: see
# https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs#expanding-or-adding-matrix-configurations
# APT install and check oldest supported version 2023.2.0
include:
- os: ubuntu-22.04
version: 2022.3.0
apt: false
# APT install and check latest supported version 2024.1.0
include:
- os: ubuntu-22.04
version: 2024.1.0
apt: true
Expand Down
19 changes: 15 additions & 4 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 @@ -53,15 +55,15 @@ impl Core {
/// Read model with model and weights loaded in memory.
pub fn read_model_from_buffer(
&mut self,
model_str: &str,
weights_buffer: &Tensor,
model_str: &[u8],
weights_buffer: Option<&Tensor>,
) -> 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.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 +87,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