forked from pykeio/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_providers.rs
More file actions
237 lines (223 loc) · 9.35 KB
/
execution_providers.rs
File metadata and controls
237 lines (223 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#![allow(unused_imports)]
use std::{collections::HashMap, ffi::CString, os::raw::c_char};
use crate::{error::status_to_result, ortsys, sys, OrtApiError, OrtResult};
extern "C" {
pub(crate) fn OrtSessionOptionsAppendExecutionProvider_CPU(options: *mut sys::OrtSessionOptions, use_arena: std::os::raw::c_int) -> sys::OrtStatusPtr;
#[cfg(feature = "acl")]
pub(crate) fn OrtSessionOptionsAppendExecutionProvider_ACL(options: *mut sys::OrtSessionOptions, use_arena: std::os::raw::c_int) -> sys::OrtStatusPtr;
#[cfg(feature = "onednn")]
pub(crate) fn OrtSessionOptionsAppendExecutionProvider_Dnnl(options: *mut sys::OrtSessionOptions, use_arena: std::os::raw::c_int) -> sys::OrtStatusPtr;
#[cfg(feature = "coreml")]
pub(crate) fn OrtSessionOptionsAppendExecutionProvider_CoreML(options: *mut sys::OrtSessionOptions, flags: u32) -> sys::OrtStatusPtr;
#[cfg(feature = "directml")]
pub(crate) fn OrtSessionOptionsAppendExecutionProvider_DML(options: *mut sys::OrtSessionOptions, device_id: std::os::raw::c_int) -> sys::OrtStatusPtr;
}
/// Execution provider container. See [the ONNX Runtime docs](https://onnxruntime.ai/docs/execution-providers/) for more
/// info on execution providers. Execution providers are actually registered via the `with_execution_providers()`
/// functions [`crate::SessionBuilder`] (per-session) or [`crate::EnvBuilder`] (default for all sessions in an
/// environment).
#[derive(Debug, Clone)]
pub struct ExecutionProvider {
provider: String,
options: HashMap<String, String>
}
macro_rules! ep_providers {
($($fn_name:ident = $name:expr),*) => {
$(
/// Creates a new `
#[doc = $name]
#[doc = "` configuration object."]
pub fn $fn_name() -> Self {
Self::new($name)
}
)*
}
}
macro_rules! ep_options {
($(
$(#[$meta:meta])*
pub fn $fn_name:ident($opt_type:ty) = $option_name:ident;
)*) => {
$(
$(#[$meta])*
pub fn $fn_name(mut self, v: $opt_type) -> Self {
self = self.with(stringify!($option_name), v.to_string());
self
}
)*
}
}
impl ExecutionProvider {
/// Creates an `ExecutionProvider` for the given execution provider name.
///
/// You probably want the dedicated methods instead, e.g. [`ExecutionProvider::cuda`].
pub fn new(provider: impl Into<String>) -> Self {
Self {
provider: provider.into(),
options: HashMap::new()
}
}
ep_providers! {
cpu = "CPUExecutionProvider",
cuda = "CUDAExecutionProvider",
tensorrt = "TensorRTExecutionProvider",
acl = "AclExecutionProvider",
dnnl = "DnnlExecutionProvider",
onednn = "DnnlExecutionProvider",
coreml = "CoreMLExecutionProvider",
directml = "DmlExecutionProvider",
rocm = "ROCmExecutionProvider"
}
/// Returns `true` if this execution provider is available, `false` otherwise.
/// The CPU execution provider will always be available.
pub fn is_available(&self) -> bool {
let mut providers: *mut *mut c_char = std::ptr::null_mut();
let mut num_providers = 0;
if status_to_result(ortsys![unsafe GetAvailableProviders(&mut providers, &mut num_providers)]).is_err() {
return false;
}
for i in 0..num_providers {
let avail = unsafe { std::ffi::CStr::from_ptr(*providers.offset(i as isize)) }
.to_string_lossy()
.into_owned();
if self.provider == avail {
let _ = ortsys![unsafe ReleaseAvailableProviders(providers, num_providers)];
return true;
}
}
let _ = ortsys![unsafe ReleaseAvailableProviders(providers, num_providers)];
false
}
/// Configure this execution provider with the given option name and value
pub fn with(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.options.insert(name.into(), value.into());
self
}
ep_options! {
/// Whether or not to use the arena allocator.
///
/// Supported backends: CPU, ACL, oneDNN
pub fn with_use_arena(bool) = use_arena;
/// The device ID to initialize the execution provider on.
///
/// Supported backends: DirectML
pub fn with_device_id(i32) = device_id;
}
}
#[tracing::instrument(skip_all)]
pub(crate) fn apply_execution_providers(options: *mut sys::OrtSessionOptions, execution_providers: impl AsRef<[ExecutionProvider]>) {
let status_to_result_and_log = |ep: &'static str, status: *mut sys::OrtStatus| {
let result = status_to_result(status);
tracing::debug!("{ep} execution provider registration {result:?}");
result
};
for ep in execution_providers.as_ref() {
let init_args = ep.options.clone();
match ep.provider.as_str() {
"CPUExecutionProvider" => {
let use_arena = init_args.get("use_arena").map_or(false, |s| s.parse::<bool>().unwrap_or(false));
let status = unsafe { OrtSessionOptionsAppendExecutionProvider_CPU(options, use_arena.into()) };
if status_to_result_and_log("CPU", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "cuda")]
"CUDAExecutionProvider" => {
let mut cuda_options: *mut sys::OrtCUDAProviderOptionsV2 = std::ptr::null_mut();
if status_to_result_and_log("CUDA", ortsys![unsafe CreateCUDAProviderOptions(&mut cuda_options)]).is_err() {
continue; // next EP
}
let keys: Vec<CString> = init_args.keys().map(|k| CString::new(k.as_str()).unwrap()).collect();
let values: Vec<CString> = init_args.values().map(|v| CString::new(v.as_str()).unwrap()).collect();
assert_eq!(keys.len(), values.len()); // sanity check
let key_ptrs: Vec<*const c_char> = keys.iter().map(|k| k.as_ptr()).collect();
let value_ptrs: Vec<*const c_char> = values.iter().map(|v| v.as_ptr()).collect();
let status = ortsys![unsafe UpdateCUDAProviderOptions(cuda_options, key_ptrs.as_ptr(), value_ptrs.as_ptr(), keys.len())];
if status_to_result_and_log("CUDA", status).is_err() {
ortsys![unsafe ReleaseCUDAProviderOptions(cuda_options)];
continue; // next EP
}
let status = ortsys![unsafe SessionOptionsAppendExecutionProvider_CUDA_V2(options, cuda_options)];
ortsys![unsafe ReleaseCUDAProviderOptions(cuda_options)];
if status_to_result_and_log("CUDA", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "tensorrt")]
"TensorRTExecutionProvider" => {
let mut tensorrt_options: *mut sys::OrtTensorRTProviderOptionsV2 = std::ptr::null_mut();
if status_to_result_and_log("TensorRT", ortsys![unsafe CreateTensorRTProviderOptions(&mut tensorrt_options)]).is_err() {
continue; // next EP
}
let keys: Vec<CString> = init_args.keys().map(|k| CString::new(k.as_str()).unwrap()).collect();
let values: Vec<CString> = init_args.values().map(|v| CString::new(v.as_str()).unwrap()).collect();
assert_eq!(keys.len(), values.len()); // sanity check
let key_ptrs: Vec<*const c_char> = keys.iter().map(|k| k.as_ptr()).collect();
let value_ptrs: Vec<*const c_char> = values.iter().map(|v| v.as_ptr()).collect();
let status = ortsys![unsafe UpdateTensorRTProviderOptions(tensorrt_options, key_ptrs.as_ptr(), value_ptrs.as_ptr(), keys.len())];
if status_to_result_and_log("TensorRT", status).is_err() {
ortsys![unsafe ReleaseTensorRTProviderOptions(tensorrt_options)];
continue; // next EP
}
let status = ortsys![unsafe SessionOptionsAppendExecutionProvider_TensorRT_V2(options, tensorrt_options)];
ortsys![unsafe ReleaseTensorRTProviderOptions(tensorrt_options)];
if status_to_result_and_log("TensorRT", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "acl")]
"AclExecutionProvider" => {
let use_arena = init_args.get("use_arena").map_or(false, |s| s.parse::<bool>().unwrap_or(false));
let status = unsafe { OrtSessionOptionsAppendExecutionProvider_ACL(options, use_arena.into()) };
if status_to_result_and_log("ACL", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "onednn")]
"DnnlExecutionProvider" => {
let use_arena = init_args.get("use_arena").map_or(false, |s| s.parse::<bool>().unwrap_or(false));
let status = unsafe { OrtSessionOptionsAppendExecutionProvider_Dnnl(options, use_arena.into()) };
if status_to_result_and_log("oneDNN", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "coreml")]
"CoreMLExecutionProvider" => {
// TODO: Support additional CoreML flags
// https://onnxruntime.ai/docs/execution-providers/CoreML-ExecutionProvider.html#available-options
let status = unsafe { OrtSessionOptionsAppendExecutionProvider_CoreML(options, 0) };
if status_to_result_and_log("CoreML", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "directml")]
"DmlExecutionProvider" => {
let device_id = init_args.get("device_id").map_or(0, |s| s.parse::<i32>().unwrap_or(0));
// TODO: extended options with OrtSessionOptionsAppendExecutionProviderEx_DML
let status = unsafe { OrtSessionOptionsAppendExecutionProvider_DML(options, device_id) };
if status_to_result_and_log("DirectML", status).is_ok() {
return; // EP found
}
}
#[cfg(feature = "rocm")]
"ROCmExecutionProvider" => {
let rocm_options = sys::OrtROCMProviderOptions {
device_id: 0,
miopen_conv_exhaustive_search: 0,
gpu_mem_limit: usize::MAX,
arena_extend_strategy: 0,
do_copy_in_default_stream: 1,
has_user_compute_stream: 0,
user_compute_stream: std::ptr::null_mut(),
default_memory_arena_cfg: std::ptr::null_mut(),
tunable_op_enabled: 0
};
let status = ortsys![unsafe SessionOptionsAppendExecutionProvider_ROCM(options, &rocm_options)];
if status_to_result_and_log("ROCm", status).is_ok() {
return; // EP found
}
}
_ => {}
};
}
}