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
use crate::{NNSplitLogic, NNSplitOptions};
use ndarray::prelude::*;
use std::error::Error;
use tract_onnx::prelude::*;
struct TractBackend {
model: TypedModel,
n_outputs: usize,
length_divisor: usize,
}
impl TractBackend {
fn new(model: TypedModel, length_divisor: usize) -> TractResult<Self> {
let n_outputs = if let TDim::Val(value) = model.outlet_fact(model.outputs[0])?.shape[2] {
value as usize
} else {
0
};
Ok(TractBackend {
model,
n_outputs,
length_divisor,
})
}
fn predict(
&self,
input: Array2<u8>,
_batch_size: usize,
) -> Result<Array3<f32>, Box<dyn Error>> {
let input_shape = input.shape();
let opt_model = self
.model
.concretize_dims(&SymbolValues::default().with(
's'.into(),
input_shape[1] as i64 / self.length_divisor as i64,
))?
.optimize()?
.into_runnable()?;
let mut preds = Array3::<f32>::zeros((input_shape[0], input_shape[1], self.n_outputs));
for i in 0..input_shape[0] {
let batch_inputs: Tensor = input.slice(s![i..(i + 1), ..]).to_owned().into();
let batch_preds = opt_model.run(tvec![batch_inputs])?.remove(0);
let mut batch_preds: ArrayD<f32> = (*batch_preds).clone().into_array()?;
batch_preds.mapv_inplace(|x| 1f32 / (1f32 + (-x).exp()));
preds.slice_mut(s![i..(i + 1), .., ..]).assign(&batch_preds);
}
Ok(preds)
}
}
pub struct NNSplit {
backend: TractBackend,
logic: NNSplitLogic,
}
impl NNSplit {
fn type_model(model: InferenceModel, length_divisor: usize) -> TractResult<TypedModel> {
model
.with_input_fact(
0,
InferenceFact::dt_shape(
u8::datum_type(),
tvec!(1.into(), TDim::from(Symbol::from('s')) * length_divisor),
),
)?
.into_typed()?
.declutter()
}
fn from_model(
model_proto: tract_onnx::pb::ModelProto,
options: NNSplitOptions,
) -> Result<Self, Box<dyn Error>> {
let model = NNSplit::type_model(
onnx().model_for_proto_model(&model_proto)?,
options.length_divisor,
)?;
let split_sequence_string = model_proto
.metadata_props
.into_iter()
.find_map(|x| {
if x.key == "split_sequence" {
Some(x.value)
} else {
None
}
})
.ok_or("Model must contain `split_sequence` metadata key")?;
let backend = TractBackend::new(model, options.length_divisor)?;
Ok(NNSplit {
backend,
logic: NNSplitLogic::new(options, serde_json::from_str(&split_sequence_string)?),
})
}
pub fn new<P: AsRef<std::path::Path>>(
model_path: P,
options: NNSplitOptions,
) -> Result<Self, Box<dyn Error>> {
let model_proto = onnx().proto_model_for_path(model_path)?;
NNSplit::from_model(model_proto, options)
}
#[cfg(feature = "model-loader")]
pub fn load(model_name: &str, options: NNSplitOptions) -> Result<Self, Box<dyn Error>> {
let mut model_data = crate::model_loader::get_resource(model_name, "model.onnx")?.0;
let model_proto = onnx().proto_model_for_read(&mut model_data)?;
NNSplit::from_model(model_proto, options)
}
pub fn split<'a>(&self, texts: &[&'a str]) -> Vec<crate::Split<'a>> {
let (inputs, indices) = self.logic.get_inputs_and_indices(texts);
let slice_preds = self
.backend
.predict(inputs, self.logic.options().batch_size)
.expect("model failure.");
self.logic.split(texts, slice_preds, indices)
}
pub fn logic(&self) -> &NNSplitLogic {
&self.logic
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Level;
#[test]
fn splitter_model_works() {
let splitter = NNSplit::new(
concat!(env!("CARGO_MANIFEST_DIR"), "/../models/de/model.onnx"),
NNSplitOptions::default(),
)
.unwrap();
let splits = &splitter.split(&["Das ist ein Test Das ist noch ein Test."])[0];
assert_eq!(
splits.flatten(0),
vec!["Das ist ein Test ", "Das ist noch ein Test."]
);
}
#[test]
fn splitter_model_works_on_long_texts() {
let splitter = NNSplit::new(
concat!(env!("CARGO_MANIFEST_DIR"), "/../models/de/model.onnx"),
NNSplitOptions::default(),
)
.unwrap();
let text =
"Eine Vernetzung von Neuronen im Nervensystem eines Lebewesens darstellen. ".repeat(20);
let splits = &splitter.split(&[&text])[0];
assert_eq!(
splits.flatten(0),
vec!["Eine Vernetzung von Neuronen im Nervensystem eines Lebewesens darstellen. "; 20]
);
}
#[test]
fn getting_levels_works() {
let splitter = NNSplit::new(
concat!(env!("CARGO_MANIFEST_DIR"), "/../models/de/model.onnx"),
NNSplitOptions::default(),
)
.unwrap();
assert_eq!(
splitter.logic().split_sequence().get_levels(),
vec![
&Level("Sentence".into()),
&Level("Token".into()),
&Level("_Whitespace".into()),
&Level("Compound constituent".into())
]
);
}
}