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
use std::collections::HashMap;
use super::factoid::Factoid;
use super::{InferenceFact, InferenceModel, InferenceNode, InferenceOp};
use crate::internal::*;
use crate::prelude::TVec;
use tract_core::model::compact;
use tract_core::model::translator::Translate;
pub trait InferenceModelExt {
fn analyse(&mut self, obstinate: bool) -> TractResult<bool>;
fn incorporate(self) -> TractResult<InferenceModel>;
fn missing_type_shape(&self) -> TractResult<Vec<OutletId>>;
fn eliminate_dead_branches(self) -> TractResult<InferenceModel>;
fn into_typed(self) -> TractResult<TypedModel>;
fn into_normalized(self) -> TractResult<NormalizedModel>;
fn into_optimized(self) -> TractResult<TypedModel>;
}
impl InferenceModelExt for InferenceModel {
fn analyse(&mut self, obstinate: bool) -> TractResult<bool> {
super::analyser::Analyser::new(self).analyse_obstinate(obstinate)
}
fn incorporate(self) -> TractResult<InferenceModel> {
let mut model = self;
loop {
let mut done_something = false;
for p in crate::infer::optim::incorporate() {
done_something = done_something || p.pass(&mut model)?;
if cfg!(debug_assertions) {
model.check_edges()?;
}
}
if !done_something {
break;
}
}
model = compact::compact(&model)?;
model.analyse(false)?;
Ok(model)
}
fn missing_type_shape(&self) -> TractResult<Vec<OutletId>> {
Ok(self
.eval_order()?
.iter()
.flat_map(|&node| {
self.nodes()[node]
.outputs
.iter()
.enumerate()
.map(move |(ix, outlet)| (OutletId::new(node, ix), outlet))
})
.filter(|(_, o)| !o.fact.datum_type.is_concrete() || !o.fact.shape.is_concrete())
.map(|(id, _)| id)
.collect())
}
fn eliminate_dead_branches(mut self) -> TractResult<InferenceModel> {
compact::compact(&mut self)
}
fn into_typed(mut self) -> TractResult<TypedModel> {
self.analyse(false)?;
let m = self.incorporate()?;
#[derive(Debug)]
struct ToTypedTranslator;
impl Translate<InferenceFact, Box<dyn InferenceOp>, TypedFact, Box<dyn TypedOp>>
for ToTypedTranslator
{
fn translate_node(
&self,
source: &InferenceModel,
node: &InferenceNode,
target: &mut TypedModel,
mapping: &HashMap<OutletId, OutletId>,
) -> TractResult<TVec<OutletId>> {
node.op.to_typed(source, node, target, mapping)
}
}
ToTypedTranslator.translate_model(&m)
}
fn into_normalized(self) -> TractResult<NormalizedModel> {
self.into_typed()?.declutter()?.into_normalized()
}
fn into_optimized(self) -> TractResult<TypedModel> {
self.into_typed()?.into_optimized()
}
}
impl ModelSpecialOps<InferenceFact, Box<dyn InferenceOp>> for InferenceModel {
fn add_source(
&mut self,
name: impl Into<String>,
fact: InferenceFact,
) -> TractResult<OutletId> {
let id = self.add_node(name, crate::ops::source::Source::new(), tvec!(fact))?;
let id = OutletId::new(id, 0);
self.inputs.push(id);
Ok(id)
}
fn is_source(op: &dyn Op) -> bool {
op.downcast_ref::<crate::ops::source::Source>().is_some()
}
fn create_dummy(&self) -> Box<dyn InferenceOp> {
Box::new(tract_core::ops::dummy::Dummy::new())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test() {
fn is_sync<T: Sync>() {}
is_sync::<InferenceModel>();
}
}