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
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use ndarray::{Array, Array2, Array3, IxDyn};
use ort::environment::Environment;
use ort::tensor::{FromArray, InputTensor};
use ort::{GraphOptimizationLevel, SessionBuilder};
use crate::common::Device;
use crate::common::{apply_device, match_to_inputs};
use crate::error::Result;
use crate::{try_extract_to_f32, ORTSession};
pub struct Seq2SeqEncoderModel<'a> {
model_session: ORTSession<'a>,
token_type_support: bool,
}
impl<'a> Seq2SeqEncoderModel<'a> {
pub fn new_from_memory(
env: Arc<Environment>,
model_bytes: &'a [u8],
device: Device,
optimization_level: GraphOptimizationLevel,
) -> Result<Self> {
let mut session_builder = SessionBuilder::new(&env)?;
session_builder = apply_device(session_builder, device)?;
let session = session_builder
.with_optimization_level(optimization_level)?
.with_model_from_memory(model_bytes)?;
let token_type_support = session.inputs.len() == 3
&& session
.inputs
.iter()
.filter(|i| i.name == "token_type_ids")
.count()
> 0;
Ok(Self {
model_session: ORTSession::InMemory(session),
token_type_support,
})
}
pub fn new_from_file(
env: Arc<Environment>,
model_path: PathBuf,
device: Device,
optimization_level: GraphOptimizationLevel,
) -> Result<Self> {
let session_builder = SessionBuilder::new(&env)?;
let session_builder = apply_device(session_builder, device)?;
let session = session_builder
.with_optimization_level(optimization_level)?
.with_model_from_file(model_path)?;
let token_type_support = session.inputs.len() == 3
&& session
.inputs
.iter()
.filter(|i| i.name == "token_type_ids")
.count()
> 0;
Ok(Self {
model_session: ORTSession::Owned(session),
token_type_support,
})
}
pub fn get_token_type_support(&self) -> bool {
self.token_type_support
}
pub fn forward(
&self,
input_ids: Array2<u32>,
attention_mask: Option<Array2<u32>>,
token_type_ids: Option<Array2<u32>>,
) -> Result<Array3<f32>> {
let input_map = self.prepare_input_map(input_ids, attention_mask, token_type_ids)?;
let model = match &self.model_session {
ORTSession::Owned(m) => m,
ORTSession::InMemory(m) => m,
};
let input_tensor = match_to_inputs(&model.inputs, input_map)?;
let output_names = model
.outputs
.iter()
.map(|o| o.name.clone())
.collect::<Vec<_>>();
let outputs_tensors = model.run(input_tensor)?;
let output_map: HashMap<String, Array<f32, IxDyn>> = output_names
.iter()
.map(|name| name.to_string())
.zip(
outputs_tensors
.into_iter()
.map(|tensor| try_extract_to_f32(tensor).unwrap().view().to_owned()),
)
.collect();
Ok(output_map
.get("last_hidden_state")
.expect("Model must have last_hidden_state output")
.to_owned()
.into_dimensionality()?)
}
fn prepare_input_map(
&self,
input_ids: Array2<u32>,
attention_mask: Option<Array2<u32>>,
token_type_ids: Option<Array2<u32>>,
) -> Result<HashMap<String, InputTensor>> {
let mut input_map = HashMap::<String, InputTensor>::new();
let attention_mask = if attention_mask.is_none() {
Array::ones((input_ids.shape()[0], input_ids.shape()[1]))
} else {
attention_mask.unwrap()
};
if self.token_type_support {
if let Some(token_types_array) = token_type_ids {
input_map.insert(
"token_type_ids".to_string(),
InputTensor::from_array(token_types_array.into_dimensionality()?),
);
} else {
input_map.insert(
"token_type_ids".to_string(),
InputTensor::from_array(
Array2::<u32>::zeros((input_ids.nrows(), input_ids.ncols()))
.into_dimensionality()?,
),
);
}
}
input_map.insert(
"input_ids".to_string(),
InputTensor::from_array(input_ids.into_dimensionality()?),
);
input_map.insert(
"attention_mask".to_string(),
InputTensor::from_array(attention_mask.into_dimensionality()?),
);
Ok(input_map)
}
}
#[cfg(test)]
mod tests {
use crate::hf_hub::hf_hub_download;
use super::*;
#[test]
fn test_encoder() -> Result<()> {
let env = Environment::builder().build().unwrap();
let model = Seq2SeqEncoderModel::new_from_file(
env.into_arc(),
hf_hub_download("optimum/t5-small", "encoder_model.onnx", None, None).unwrap(),
Device::CPU,
GraphOptimizationLevel::Disable,
)
.unwrap();
let input_ids1 =
Array2::from_shape_vec((1, 8), vec![101, 2000, 1037, 1037, 1037, 1037, 1037, 102])
.unwrap();
let attention_mask = Array2::from_shape_vec((1, 8), vec![1, 1, 1, 1, 1, 1, 1, 1]).unwrap();
let embeddings = model
.forward(input_ids1, Some(attention_mask), None)
.unwrap();
assert_eq!(*embeddings.shape().get(0).unwrap(), 1 as usize);
Ok(())
}
}