Skip to content

Commit

Permalink
Support loading non-string models from memory (#107)
Browse files Browse the repository at this point in the history
* Expose setting input/getting output tensors by index

* Support loading non-string models from memory

* Improve ergonomics

* Use explicit Option arg

* Remove 2022.3.0 from matrix
  • Loading branch information
pnehrer committed May 17, 2024
1 parent 8aa9a52 commit 1aacdf9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
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 @@ -11,6 +11,8 @@ use openvino_sys::{
};
use std::ffi::CString;

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 @@ -54,15 +56,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 @@ -87,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());
}
}

0 comments on commit 1aacdf9

Please sign in to comment.