Skip to content

Commit c7ddbdb

Browse files
committed
refactor: let ONNX Runtime manage operator library handles
This removes the `libc` & `winapi` dependencies (though they were gated behind `operator-libraries`, so not really any impact...)
1 parent 60f6eca commit c7ddbdb

File tree

4 files changed

+9
-77
lines changed

4 files changed

+9
-77
lines changed

Cargo.toml

-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ default = [ "ndarray", "half", "download-binaries", "copy-dylibs" ]
5656

5757
training = [ "ort-sys/training" ]
5858

59-
operator-libraries = [ "libc", "winapi" ]
60-
6159
fetch-models = [ "ureq", "sha2" ]
6260
download-binaries = [ "ort-sys/download-binaries" ]
6361
load-dynamic = [ "libloading", "ort-sys/load-dynamic" ]
@@ -91,12 +89,6 @@ sha2 = { version = "0.10", optional = true }
9189
tracing = { version = "0.1", default-features = false, features = [ "std" ] }
9290
half = { version = "2.1", optional = true }
9391

94-
[target.'cfg(unix)'.dependencies]
95-
libc = { version = "0.2", optional = true }
96-
97-
[target.'cfg(windows)'.dependencies]
98-
winapi = { version = "0.3", optional = true, features = [ "std", "libloaderapi" ] }
99-
10092
[dev-dependencies]
10193
anyhow = "1.0"
10294
ureq = "2.1"

src/session/builder/impl_commit.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ impl SessionBuilder {
103103
.map(|i| dangerous::extract_output(session_ptr, &allocator, i))
104104
.collect::<Result<Vec<Output>>>()?;
105105

106-
let extras = self.operator_domains.drain(..).map(|d| Box::new(d) as Box<dyn Any>);
107-
#[cfg(feature = "operator-libraries")]
108-
let extras = extras.chain(self.custom_runtime_handles.drain(..).map(|d| Box::new(d) as Box<dyn Any>));
109-
let extras: Vec<Box<dyn Any>> = extras.collect();
106+
let extras = self.operator_domains.drain(..).map(|d| Box::new(d) as Box<dyn Any>).collect();
110107

111108
Ok(Session {
112109
inner: Arc::new(SharedSessionInner {
@@ -175,10 +172,7 @@ impl SessionBuilder {
175172
.map(|i| dangerous::extract_output(session_ptr, &allocator, i))
176173
.collect::<Result<Vec<Output>>>()?;
177174

178-
let extras = self.operator_domains.drain(..).map(|d| Box::new(d) as Box<dyn Any>);
179-
#[cfg(feature = "operator-libraries")]
180-
let extras = extras.chain(self.custom_runtime_handles.drain(..).map(|d| Box::new(d) as Box<dyn Any>));
181-
let extras: Vec<Box<dyn Any>> = extras.collect();
175+
let extras = self.operator_domains.drain(..).map(|d| Box::new(d) as Box<dyn Any>).collect();
182176

183177
let session = Session {
184178
inner: Arc::new(SharedSessionInner {

src/session/builder/impl_options.rs

+7-53
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#[cfg(any(feature = "operator-libraries", not(windows)))]
2-
use std::ffi::CString;
3-
use std::{rc::Rc, sync::Arc};
1+
use std::{path::Path, rc::Rc, sync::Arc};
42

53
use super::SessionBuilder;
64
use crate::{
75
error::Result,
86
execution_providers::{apply_execution_providers, ExecutionProviderDispatch},
9-
ortsys, MemoryInfo, OperatorDomain
7+
ortsys,
8+
util::path_to_os_char,
9+
MemoryInfo, OperatorDomain
1010
};
1111

1212
impl SessionBuilder {
@@ -116,31 +116,9 @@ impl SessionBuilder {
116116
}
117117

118118
/// Registers a custom operator library at the given library path.
119-
#[cfg(feature = "operator-libraries")]
120-
#[cfg_attr(docsrs, doc(cfg(feature = "operator-libraries")))]
121-
pub fn with_operator_library(mut self, lib_path: impl AsRef<str>) -> Result<Self> {
122-
use crate::error::status_to_result;
123-
124-
let path_cstr = CString::new(lib_path.as_ref())?;
125-
126-
let mut handle: *mut ::std::os::raw::c_void = std::ptr::null_mut();
127-
128-
let status = ortsys![unsafe RegisterCustomOpsLibrary(self.session_options_ptr.as_ptr(), path_cstr.as_ptr(), &mut handle)];
129-
130-
let handle = LibHandle(handle);
131-
// per RegisterCustomOpsLibrary docs, release handle if there was an error and the handle
132-
// is non-null
133-
if let Err(e) = status_to_result(status) {
134-
if !handle.is_null() {
135-
// handle was written to, should release it
136-
drop(handle);
137-
}
138-
139-
return Err(e);
140-
}
141-
142-
self.custom_runtime_handles.push(Arc::new(handle));
143-
119+
pub fn with_operator_library(self, lib_path: impl AsRef<Path>) -> Result<Self> {
120+
let path_cstr = path_to_os_char(lib_path);
121+
ortsys![unsafe RegisterCustomOpsLibrary_V2(self.session_options_ptr.as_ptr(), path_cstr.as_ptr())?];
144122
Ok(self)
145123
}
146124

@@ -248,27 +226,3 @@ impl From<GraphOptimizationLevel> for ort_sys::GraphOptimizationLevel {
248226
}
249227
}
250228
}
251-
252-
#[cfg(feature = "operator-libraries")]
253-
pub(super) struct LibHandle(*mut std::os::raw::c_void);
254-
255-
#[cfg(feature = "operator-libraries")]
256-
impl LibHandle {
257-
pub(self) fn is_null(&self) -> bool {
258-
self.0.is_null()
259-
}
260-
}
261-
262-
#[cfg(feature = "operator-libraries")]
263-
impl Drop for LibHandle {
264-
fn drop(&mut self) {
265-
#[cfg(unix)]
266-
unsafe {
267-
libc::dlclose(self.0)
268-
};
269-
#[cfg(windows)]
270-
unsafe {
271-
winapi::um::libloaderapi::FreeLibrary(self.0 as winapi::shared::minwindef::HINSTANCE)
272-
};
273-
}
274-
}

src/session/builder/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ mod impl_config_keys;
1717
mod impl_options;
1818

1919
pub use self::impl_options::GraphOptimizationLevel;
20-
#[cfg(feature = "operator-libraries")]
21-
use self::impl_options::LibHandle;
2220

2321
/// Creates a session using the builder pattern.
2422
///
@@ -38,8 +36,6 @@ use self::impl_options::LibHandle;
3836
pub struct SessionBuilder {
3937
pub(crate) session_options_ptr: NonNull<ort_sys::OrtSessionOptions>,
4038
memory_info: Option<Rc<MemoryInfo>>,
41-
#[cfg(feature = "operator-libraries")]
42-
custom_runtime_handles: Vec<Arc<LibHandle>>,
4339
operator_domains: Vec<Arc<OperatorDomain>>
4440
}
4541

@@ -52,8 +48,6 @@ impl Clone for SessionBuilder {
5248
Self {
5349
session_options_ptr: unsafe { NonNull::new_unchecked(session_options_ptr) },
5450
memory_info: self.memory_info.clone(),
55-
#[cfg(feature = "operator-libraries")]
56-
custom_runtime_handles: self.custom_runtime_handles.clone(),
5751
operator_domains: self.operator_domains.clone()
5852
}
5953
}
@@ -85,8 +79,6 @@ impl SessionBuilder {
8579
Ok(Self {
8680
session_options_ptr: unsafe { NonNull::new_unchecked(session_options_ptr) },
8781
memory_info: None,
88-
#[cfg(feature = "operator-libraries")]
89-
custom_runtime_handles: Vec::new(),
9082
operator_domains: Vec::new()
9183
})
9284
}

0 commit comments

Comments
 (0)