Skip to content

Commit 1aacdf9

Browse files
authored
Support loading non-string models from memory (#107)
* 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
1 parent 8aa9a52 commit 1aacdf9

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ jobs:
2525
apt: [false]
2626
# We also spot-check that things work when installing from APT by adding to the matrix: see
2727
# https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs#expanding-or-adding-matrix-configurations
28-
# APT install and check oldest supported version 2023.2.0
29-
include:
30-
- os: ubuntu-22.04
31-
version: 2022.3.0
32-
apt: false
3328
# APT install and check latest supported version 2024.1.0
29+
include:
3430
- os: ubuntu-22.04
3531
version: 2024.1.0
3632
apt: true

crates/openvino/src/core.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use openvino_sys::{
1111
};
1212
use std::ffi::CString;
1313

14+
use std::os::raw::c_char;
15+
1416
/// See [`Core`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__core__c__api.html).
1517
pub struct Core {
1618
ptr: *mut ov_core_t,
@@ -54,15 +56,15 @@ impl Core {
5456
/// Read model with model and weights loaded in memory.
5557
pub fn read_model_from_buffer(
5658
&mut self,
57-
model_str: &str,
58-
weights_buffer: &Tensor,
59+
model_str: &[u8],
60+
weights_buffer: Option<&Tensor>,
5961
) -> Result<Model> {
6062
let mut ptr = std::ptr::null_mut();
6163
try_unsafe!(ov_core_read_model_from_memory_buffer(
6264
self.ptr,
63-
cstr!(model_str),
65+
model_str.as_ptr().cast::<c_char>(),
6466
model_str.len(),
65-
weights_buffer.as_ptr(),
67+
weights_buffer.map_or(std::ptr::null(), |tensor| tensor.as_ptr().cast_const()),
6668
std::ptr::addr_of_mut!(ptr)
6769
))?;
6870
Ok(Model::from_ptr(ptr))
@@ -87,9 +89,18 @@ impl Core {
8789
#[cfg(test)]
8890
mod core_tests {
8991
use super::*;
92+
9093
#[test]
9194
fn test_new() {
9295
let core = Core::new();
9396
assert!(core.is_ok());
9497
}
98+
99+
#[test]
100+
fn test_load_onnx_from_buffer() {
101+
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";
102+
let mut core = Core::new().unwrap();
103+
let model = core.read_model_from_buffer(model, None);
104+
assert!(model.is_ok());
105+
}
95106
}

0 commit comments

Comments
 (0)