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
use std::ffi::CString;
use std::path::Path;
use interoptopus::patterns::slice::FFISlice;
use interoptopus::patterns::string::AsciiPointer;
use interoptopus::{ffi_service, ffi_service_ctor, ffi_service_method, ffi_type};
use crate::error::Result;
use crate::ffi::{
error::FFIError, DeviceFFI, EnvContainer, GraphOptimizationLevelFFI, StringBatch,
};
use crate::{ClassPrediction, TaggedString, TokenClassPrediction, TokenClassificationPipeline};
#[repr(C)]
#[ffi_type]
pub struct TaggedStringFFI<'a> {
pub input_string: AsciiPointer<'a>,
pub tags: FFISlice<'a, TokenClassPredictionFFI<'a>>,
}
impl<'a> TaggedStringFFI<'a> {
pub fn from_tagged_string(
tagged_string: &'a TaggedString,
tags_ffi_buf: &'a Vec<TokenClassPredictionFFI<'a>>,
) -> Self {
let input_string = AsciiPointer::from_slice_with_nul(
CString::new(tagged_string.input_string.as_bytes())
.unwrap()
.as_bytes_with_nul(),
)
.expect("Failed to convert input string to AsciiPointer");
let tags = FFISlice::from_slice(tags_ffi_buf.as_slice());
Self { input_string, tags }
}
}
impl Default for TaggedStringFFI<'_> {
fn default() -> Self {
Self {
input_string: AsciiPointer::from_slice_with_nul(b"").unwrap(),
tags: FFISlice::from_slice(&[]),
}
}
}
#[repr(C)]
#[ffi_type]
pub struct TokenClassPredictionFFI<'a> {
pub best: ClassPredictionFFI<'a>,
pub all: FFISlice<'a, ClassPredictionFFI<'a>>,
pub start: u32,
pub end: u32,
}
impl<'a> TokenClassPredictionFFI<'a> {
pub fn from_token_class_prediction(
token_class_prediction: &'a TokenClassPrediction,
prediction_all_ffi: &'a Vec<ClassPredictionFFI<'a>>,
) -> Self {
let best = ClassPredictionFFI::from(&token_class_prediction.best);
let all = FFISlice::from_slice(prediction_all_ffi.as_slice());
let start = token_class_prediction.start;
let end = token_class_prediction.end;
Self {
best,
all,
start: start as u32,
end: end as u32,
}
}
}
#[repr(C)]
#[ffi_type]
pub struct ClassPredictionFFI<'a> {
pub label: AsciiPointer<'a>,
pub score: f32,
}
impl<'a> From<&ClassPrediction> for ClassPredictionFFI<'a> {
fn from(class_prediction: &ClassPrediction) -> Self {
let v = Vec::from(
CString::new(class_prediction.label.as_str())
.unwrap()
.to_bytes_with_nul(),
);
Self {
label: AsciiPointer::from_slice_with_nul(v.as_ref())
.expect("Failed to convert CString to AsciiPointer"),
score: class_prediction.score,
}
}
}
#[ffi_type(opaque)]
pub struct TokenClassificationPipelineFFI<'a> {
pub model: TokenClassificationPipeline<'a>,
output_buf: Vec<TaggedString>,
output_buf_ffi: Vec<TaggedStringFFI<'a>>,
tags_buf_ffi: Vec<Vec<TokenClassPredictionFFI<'a>>>,
prediction_all_buf_ffi: Vec<Vec<Vec<ClassPredictionFFI<'a>>>>,
}
#[ffi_service(error = "FFIError", prefix = "onnx_token_classification_")]
impl<'a> TokenClassificationPipelineFFI<'a> {
#[ffi_service_ctor]
pub fn from_pretrained(
env: &'a EnvContainer,
model_id: AsciiPointer<'a>,
device: DeviceFFI,
optimization: GraphOptimizationLevelFFI,
) -> Result<Self> {
let model = TokenClassificationPipeline::from_pretrained(
env.env.clone(),
model_id.as_c_str().unwrap().to_string_lossy().to_string(),
device.into(),
optimization.into(),
)?;
Ok(Self {
model,
output_buf: Vec::new(),
output_buf_ffi: Vec::new(),
tags_buf_ffi: Vec::new(),
prediction_all_buf_ffi: Vec::new(),
})
}
#[ffi_service_ctor]
pub fn create_from_files(
env: &'a EnvContainer,
model_path: AsciiPointer<'a>,
tokenizer_config_path: AsciiPointer<'a>,
special_tokens_map_path: AsciiPointer<'a>,
device: DeviceFFI,
optimization: GraphOptimizationLevelFFI,
) -> Result<Self> {
let model = TokenClassificationPipeline::new_from_files(
env.env.clone(),
Path::new(model_path.as_str().unwrap()).to_path_buf(),
Path::new(tokenizer_config_path.as_str().unwrap()).to_path_buf(),
Path::new(special_tokens_map_path.as_str().unwrap()).to_path_buf(),
device.into(),
optimization.into(),
None,
)?;
Ok(Self {
model,
output_buf: Vec::new(),
output_buf_ffi: Vec::new(),
tags_buf_ffi: Vec::new(),
prediction_all_buf_ffi: Vec::new(),
})
}
#[ffi_service_method(on_panic = "return_default")]
pub fn tag(
s: &'a mut TokenClassificationPipelineFFI<'a>,
input: AsciiPointer,
) -> TaggedStringFFI<'a> {
let input_str = input.as_c_str().unwrap().to_string_lossy().to_string();
let output = s.model.tag(&input_str).unwrap();
s.output_buf = vec![output];
s.prediction_all_buf_ffi = vec![s.output_buf[0]
.tags
.iter()
.map(|x| x.all.iter().map(|x| ClassPredictionFFI::from(x)).collect())
.collect()];
s.tags_buf_ffi = vec![s.output_buf[0]
.tags
.iter()
.zip(s.prediction_all_buf_ffi.last().unwrap().iter())
.map(|(x, y)| TokenClassPredictionFFI::from_token_class_prediction(x, y))
.collect()];
TaggedStringFFI::from_tagged_string(&s.output_buf[0], s.tags_buf_ffi.last().unwrap())
}
#[ffi_service_method(on_panic = "return_default")]
pub fn tag_batch(
s: &'a mut TokenClassificationPipelineFFI<'a>,
input: StringBatch,
) -> FFISlice<'a, TaggedStringFFI<'a>> {
let output = s.model.tag_batch(input.batch).unwrap();
s.output_buf = output;
s.prediction_all_buf_ffi = s
.output_buf
.iter()
.map(|x| {
x.tags
.iter()
.map(|x| x.all.iter().map(|x| x.into()).collect())
.collect()
})
.collect();
s.tags_buf_ffi = s
.output_buf
.iter()
.zip(s.prediction_all_buf_ffi.iter())
.map(|(x, y)| {
x.tags
.iter()
.zip(y.iter())
.map(|(x, y)| TokenClassPredictionFFI::from_token_class_prediction(x, y))
.collect()
})
.collect();
s.output_buf_ffi = s
.output_buf
.iter()
.zip(s.tags_buf_ffi.iter())
.map(|(x, y)| TaggedStringFFI::from_tagged_string(x, y))
.collect();
FFISlice::from_slice(s.output_buf_ffi.as_slice())
}
}