-
Notifications
You must be signed in to change notification settings - Fork 215
/
analyser.rs
170 lines (159 loc) · 7.21 KB
/
analyser.rs
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
use super::InferenceModel;
use super::*;
use crate::prelude::*;
use std::borrow::BorrowMut;
use std::collections::{BTreeSet, HashMap};
/// A graph analyser, along with its current state.
#[derive(new)]
pub struct Analyser<M: BorrowMut<InferenceModel>> {
model: M,
}
impl<M: BorrowMut<InferenceModel>> Analyser<M> {
/// Runs the entire analysis at once. Will not stop on error if obstinate is
/// true.
pub fn analyse_obstinate(&mut self, obstinate: bool) -> TractResult<bool> {
let mut nodes_to_visit: BTreeSet<usize> =
self.model.borrow().eval_order()?.iter().cloned().collect();
let mut observed_outlets: HashMap<usize, Vec<OutletId>> = HashMap::new();
let mut observers: HashMap<OutletId, TVec<usize>> = HashMap::new();
for node in self.model.borrow().nodes() {
if !nodes_to_visit.contains(&node.id) {
nodes_to_visit.insert(node.id);
}
let observed = node.op.observe_outlets(self.model.borrow(), node)?;
for outlet in &observed {
observers.entry(*outlet).or_insert(tvec!()).push(node.id);
}
observed_outlets.insert(node.id, observed);
}
let mut first_error = None;
let mut did_something = false;
while let Some(&node) = nodes_to_visit.iter().next() {
trace!("Remaining nodes {}, visiting {}", nodes_to_visit.len(), node);
match self.analyse_one(node) {
Ok(changed_edges) => {
for (edge, _fact) in changed_edges {
did_something = true;
trace!("Changed edge: {:?}", edge);
for dst in self.model.borrow().nodes()[edge.node].outputs[edge.slot]
.successors
.iter()
{
if dst.node != edge.node {
trace!("Inserting node dn {:?}", dst.node);
nodes_to_visit.insert(dst.node);
}
}
if edge.node != node {
trace!("Inserting node up {}", edge.node);
nodes_to_visit.insert(edge.node);
}
if let Some(observers) = observers.get(&edge) {
for observer in observers {
nodes_to_visit.insert(*observer);
}
}
}
}
Err(e) => {
let e = e.context(format!(
"Failed analyse for node {}",
self.model.borrow().node(node)
));
if !obstinate {
return Err(e);
}
debug!("{:?}", e);
if first_error.is_none() {
first_error = Some(e);
}
}
}
nodes_to_visit.remove(&node);
}
trace!("analyse done");
if let Some(e) = first_error {
Err(e)?
}
Ok(did_something)
}
/// Tries to run a single step of the analysis, and returns whether
/// there was any additional information gained during the step.
pub fn analyse_one(&mut self, node: usize) -> TractResult<Vec<(OutletId, InferenceFact)>> {
let mut changed_edges = vec![];
{
trace!("Starting step for {}", self.model.borrow().node(node));
let observed_outlets: Vec<OutletId> = {
let model = self.model.borrow();
let node = model.node(node);
node.op.observe_outlets(model, node)?
};
let inferred = {
let (inputs, outputs) = self.model.borrow().node_facts(node)?;
if outputs.len() != self.model.borrow().node(node).op.nboutputs().unwrap() {
bail!(
"Wrong nnumber of outputs. Op says {}, node says {}.",
self.model.borrow().node(node).op.nboutputs().unwrap(),
outputs.len(),
)
}
let inputs: TVec<InferenceFact> = inputs.into_iter().cloned().collect();
let outputs: TVec<InferenceFact> = outputs.into_iter().cloned().collect();
let observed: TVec<(OutletId, InferenceFact)> = {
let model = self.model.borrow();
let node = model.node(node);
node.op
.observe_outlets(model, node)?
.iter()
.map(|o| model.outlet_fact(*o).map(|f| (*o, f.clone())))
.collect::<TractResult<_>>()?
};
if log_enabled!(log::Level::Trace) {
for (ix, i) in inputs.iter().enumerate() {
trace!(" Input #{}: {:?}", ix, i);
}
for (ix, o) in outputs.iter().enumerate() {
trace!(" Output #{}: {:?}", ix, o);
}
}
let inputs: TVec<&InferenceFact> = inputs.iter().collect();
let outputs: TVec<&InferenceFact> = outputs.iter().collect();
let observed: TVec<&InferenceFact> = observed.iter().map(|p| &p.1).collect();
self.model.borrow_mut().node_mut(node).op.infer(inputs, outputs, observed)?
};
let node = self.model.borrow().node(node);
for (ix, &outlet) in node.inputs.iter().enumerate() {
let inferred_fact = &inferred.0[ix];
let old_fact = self.model.borrow().outlet_fact(outlet)?;
let unified = inferred_fact
.unify(old_fact)
.with_context(|| format!("while unifying inputs of {node}"))?;
if &unified != old_fact {
debug!(" Refined {:?}: {:?} -> {:?}", outlet, old_fact, unified);
changed_edges.push((outlet, unified));
}
}
for (ix, inferred_fact) in inferred.1.iter().enumerate() {
let old_fact = self.model.borrow().outlet_fact(OutletId::new(node.id, ix))?;
let unified = old_fact.unify(inferred_fact)?;
if &unified != old_fact {
let outlet = OutletId::new(node.id, ix);
debug!(" Refined {:?}: {:?} -> {:?}", outlet, old_fact, unified);
changed_edges.push((outlet, unified));
}
}
for (ix, &outlet) in observed_outlets.iter().enumerate() {
let old_fact = self.model.borrow().outlet_fact(outlet)?;
let new_fact = &inferred.2[ix];
let unified = old_fact.unify(new_fact)?;
if &unified != old_fact {
changed_edges.push((outlet, unified));
}
}
}
for (outlet, fact) in &changed_edges {
self.model.borrow_mut().set_outlet_fact(*outlet, fact.clone())?;
}
Ok(changed_edges)
}
}