-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
onnxruntime.rs
149 lines (127 loc) · 4.76 KB
/
onnxruntime.rs
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
//! Implements a `wasi-nn` [`BackendInner`] using ONNX via ort.
use super::{BackendError, BackendExecutionContext, BackendFromDir, BackendGraph, BackendInner};
use crate::backend::read;
use crate::wit::types::{ExecutionTarget, GraphEncoding, Tensor, TensorType};
use crate::{ExecutionContext, Graph};
use ort::{inputs, GraphOptimizationLevel, Session};
use std::path::Path;
use std::sync::{Arc, Mutex};
#[derive(Default)]
pub struct OnnxBackend();
unsafe impl Send for OnnxBackend {}
unsafe impl Sync for OnnxBackend {}
impl BackendInner for OnnxBackend {
fn encoding(&self) -> GraphEncoding {
GraphEncoding::Onnx
}
fn load(&mut self, builders: &[&[u8]], target: ExecutionTarget) -> Result<Graph, BackendError> {
if builders.len() != 1 {
return Err(BackendError::InvalidNumberOfBuilders(1, builders.len()).into());
}
let session = Session::builder()?
.with_optimization_level(GraphOptimizationLevel::Level3)?
.with_model_from_memory(builders[0])?;
let box_: Box<dyn BackendGraph> =
Box::new(ONNXGraph(Arc::new(Mutex::new(session)), target));
Ok(box_.into())
}
fn as_dir_loadable<'a>(&'a mut self) -> Option<&'a mut dyn BackendFromDir> {
Some(self)
}
}
impl BackendFromDir for OnnxBackend {
fn load_from_dir(
&mut self,
path: &Path,
target: ExecutionTarget,
) -> Result<Graph, BackendError> {
let model = read(&path.join("model.onnx"))?;
self.load(&[&model], target)
}
}
struct ONNXGraph(Arc<Mutex<Session>>, #[allow(dead_code)] ExecutionTarget);
unsafe impl Send for ONNXGraph {}
unsafe impl Sync for ONNXGraph {}
impl BackendGraph for ONNXGraph {
fn init_execution_context(&self) -> Result<ExecutionContext, BackendError> {
let session = self.0.lock().unwrap();
let inputs = session.inputs.iter().map(|_| None).collect::<Vec<_>>();
let outputs = session.outputs.iter().map(|_| None).collect::<Vec<_>>();
let box_: Box<dyn BackendExecutionContext> = Box::new(ONNXExecutionContext {
session: self.0.clone(),
inputs,
outputs,
});
Ok(box_.into())
}
}
struct ONNXExecutionContext {
session: Arc<Mutex<Session>>,
inputs: Vec<Option<Tensor>>,
outputs: Vec<Option<Vec<u8>>>,
}
unsafe impl Send for ONNXExecutionContext {}
unsafe impl Sync for ONNXExecutionContext {}
impl BackendExecutionContext for ONNXExecutionContext {
fn set_input(&mut self, index: u32, tensor: &Tensor) -> Result<(), BackendError> {
self.inputs[index as usize].replace(tensor.clone());
Ok(())
}
fn compute(&mut self) -> Result<(), BackendError> {
let shaped_inputs: Vec<_> = self
.inputs
.iter()
.enumerate()
.map(|(i, _o)| {
let input = self.inputs[i].as_ref().unwrap();
let dims = input
.dimensions
.as_slice()
.iter()
.map(|d| *d as i64)
.collect::<Vec<_>>();
match input.tensor_type {
TensorType::Fp32 => {
let data = bytes_to_f32_vec(input.data.to_vec());
inputs![(dims, Arc::new(data.into_boxed_slice()))].unwrap()
}
_ => {
unimplemented!("{:?} not supported by ONNX", input.tensor_type);
}
}
})
.flatten()
.collect();
let session = self.session.lock().unwrap();
let res = session.run(shaped_inputs.as_slice())?;
for i in 0..self.outputs.len() {
let raw: (Vec<i64>, &[f32]) = res[i].extract_raw_tensor()?;
let f32s = raw.1.to_vec();
self.outputs[i].replace(f32_vec_to_bytes(f32s));
}
Ok(())
}
fn get_output(&mut self, index: u32, destination: &mut [u8]) -> Result<u32, BackendError> {
let output = self.outputs[index as usize].as_ref().unwrap();
destination[..output.len()].copy_from_slice(output);
Ok(output.len() as u32)
}
}
impl From<ort::Error> for BackendError {
fn from(e: ort::Error) -> Self {
BackendError::BackendAccess(e.into())
}
}
pub fn f32_vec_to_bytes(data: Vec<f32>) -> Vec<u8> {
let chunks: Vec<[u8; 4]> = data.into_iter().map(|f| f.to_le_bytes()).collect();
let result: Vec<u8> = chunks.iter().flatten().copied().collect();
result
}
pub fn bytes_to_f32_vec(data: Vec<u8>) -> Vec<f32> {
let chunks: Vec<&[u8]> = data.chunks(4).collect();
let v: Vec<f32> = chunks
.into_iter()
.map(|c| f32::from_le_bytes(c.try_into().unwrap()))
.collect();
v.into_iter().collect()
}