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
use std::collections::HashMap;
use std::str;
use std::borrow::Cow;
use itertools::Itertools;
pub mod compact;
pub mod dsl;
mod fact;
mod model;
mod node;
pub mod order;
mod patch;
pub mod translator;
pub use self::dsl::*;
pub use self::fact::*;
pub use self::model::*;
pub use self::node::*;
pub use self::order::eval_order;
pub use self::patch::ModelPatch;
pub use crate::ops::{Op, TypedOp};
use crate::ops::invariants;
use crate::plan::{SimplePlan, SimpleState};
use crate::TractResult;
use tract_linalg::hash::DynHash;
pub trait Model: downcast_rs::Downcast + std::fmt::Debug + dyn_clone::DynClone {
fn model_label(&self) -> Option<&str>;
fn node_id_by_name(&self, name: &str) -> TractResult<usize>;
fn node_name(&self, id: usize) -> &str;
fn node_op(&self, id: usize) -> &dyn Op;
fn node_inputs(&self, id: usize) -> &[OutletId];
fn node_output_count(&self, id: usize) -> usize;
fn nodes_len(&self) -> usize;
fn node_display(&self, id: usize) -> String;
fn node_debug(&self, id: usize) -> String;
fn eval_order(&self) -> TractResult<Vec<usize>>;
fn eval_order_for_io(&self, inputs: &[usize], outputs: &[usize]) -> TractResult<Vec<usize>>;
fn input_outlets(&self) -> &[OutletId];
fn output_outlets(&self) -> &[OutletId];
fn outlet_typedfact(&self, outlet: OutletId) -> TractResult<TypedFact>;
fn outlet_fact_format(&self, outlet: OutletId) -> String;
fn outlet_label(&self, id: OutletId) -> Option<&str>;
fn outlet_successors(&self, outlet: OutletId) -> &[InletId];
fn nested_models(&self, node: usize) -> Vec<(Cow<str>, &dyn Model, Vec<String>, Vec<String>)>;
}
impl_downcast!(Model);
pub type TypedModel = ModelImpl<TypedFact, Box<dyn TypedOp>>;
pub type TypedNode = BaseNode<TypedFact, Box<dyn TypedOp>>;
pub type TypedModelPatch = ModelPatch<TypedFact, Box<dyn TypedOp>>;
pub type TypedSimplePlan<M> = SimplePlan<TypedFact, Box<dyn TypedOp>, M>;
pub type TypedRunnableModel<M> = RunnableModel<TypedFact, Box<dyn TypedOp>, M>;
pub type TypedSimpleState<M, P> = SimpleState<TypedFact, Box<dyn TypedOp>, M, P>;
pub type RunnableModel<F, O, M> = SimplePlan<F, O, M>;
impl TypedModel {
pub fn signature(&self) -> u64 {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
self.hash(&mut hasher);
hasher.finish()
}
pub fn declutter(self) -> TractResult<TypedModel> {
let started = self.signature();
let mut model = self;
let model_inputs = model.input_outlets()?.len();
let model_outputs = model.output_outlets()?.len();
loop {
let mut done_something = false;
for p in crate::optim::declutter() {
debug!("done_something {:?} before {:?}", done_something, p);
done_something = done_something || p.pass(&mut model)?;
debug!("done_something {:?} after {:?}", done_something, p);
if cfg!(debug_assertions) {
model.check_edges()?;
assert_eq!(model.input_outlets()?.len(), model_inputs);
assert_eq!(model.output_outlets()?.len(), model_outputs);
}
}
if !done_something || model.signature() == started {
break;
}
model = compact::compact(&model)?;
}
compact::compact(&model)
}
pub fn optimize(self) -> TractResult<TypedModel> {
let mut model = self;
loop {
let mut done_something = false;
for p in crate::optim::codegen() {
done_something = done_something || p.pass(&mut model)?;
if cfg!(debug_assertions) {
model.check_edges()?;
}
}
if !done_something {
break;
}
model = compact::compact(&model)?;
}
Ok(model)
}
pub fn invariants(&self) -> TractResult<invariants::Invariants> {
invariants::for_model(self)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test() {
fn is_sync<T: Sync>() {}
is_sync::<TypedModel>();
}
}