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
use super::Factoid;
use tract_data::UndeterminedSymbol;
use crate::infer::*;
use std::fmt;
use tract_core::downcast_rs::Downcast;
tract_core::dyn_clone::clone_trait_object!(InferenceOp);
pub trait InferenceOp:
Op
+ fmt::Debug
+ tract_core::dyn_clone::DynClone
+ Send
+ Sync
+ 'static
+ Downcast
+ EvalOp
+ DynHash
{
fn infer(
&mut self,
inputs: TVec<&InferenceFact>,
outputs: TVec<&InferenceFact>,
observed: TVec<&InferenceFact>,
) -> TractResult<(TVec<InferenceFact>, TVec<InferenceFact>, TVec<InferenceFact>)> {
let (infered_inputs, infered_outputs, observed) =
self.infer_facts(inputs, outputs, observed).context("Infering facts")?;
if self.is_stateless() {
if infered_inputs.iter().all(|i| i.value.is_concrete()) {
let input_values = infered_inputs
.iter()
.map(|i| i.value.concretize().unwrap().clone().into())
.collect();
match self.eval(input_values) {
Ok(values) => {
let output_values =
values.into_iter().map(|t| t.into()).collect::<TVec<_>>();
return Ok((infered_inputs, output_values, observed));
}
Err(e) if e.source().map(|e| e.downcast_ref::<UndeterminedSymbol>()).is_some() => (),
Err(e) => return Err(e).context("Eager eval"),
}
}
}
return Ok((infered_inputs, infered_outputs, observed));
}
fn observe_outlets(
&self,
_model: &InferenceModel,
_node: &InferenceNode,
) -> TractResult<Vec<OutletId>> {
Ok(vec![])
}
fn infer_facts(
&mut self,
inputs: TVec<&InferenceFact>,
outputs: TVec<&InferenceFact>,
observed: TVec<&InferenceFact>,
) -> TractResult<(TVec<InferenceFact>, TVec<InferenceFact>, TVec<InferenceFact>)>;
#[allow(unused_variables)]
fn incorporate(
&self,
model: &InferenceModel,
node: &InferenceNode,
) -> TractResult<Option<InferenceModelPatch>> {
Ok(None)
}
fn nboutputs(&self) -> TractResult<usize> {
Ok(1)
}
fn as_op(&self) -> &dyn Op;
fn as_op_mut(&mut self) -> &mut dyn Op;
#[allow(unused_variables)]
fn to_typed(
&self,
source: &InferenceModel,
node: &InferenceNode,
target: &mut TypedModel,
mapping: &HashMap<OutletId, OutletId>,
) -> TractResult<TVec<OutletId>> {
bail!("Operator can not be made a TypedOp.")
}
}
impl Hash for Box<dyn InferenceOp> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(&self.type_id(), state);
self.dyn_hash(state)
}
}
impl std::fmt::Display for Box<dyn InferenceOp> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.name())
}
}
impl<O: InferenceOp> From<O> for Box<dyn InferenceOp> {
fn from(it: O) -> Box<dyn InferenceOp> {
Box::new(it)
}
}
impl AsRef<dyn Op> for dyn InferenceOp {
fn as_ref(&self) -> &dyn Op {
self.as_op()
}
}
impl AsRef<dyn Op> for Box<dyn InferenceOp> {
fn as_ref(&self) -> &dyn Op {
self.as_op()
}
}
impl AsMut<dyn Op> for dyn InferenceOp {
fn as_mut(&mut self) -> &mut dyn Op {
self.as_op_mut()
}
}
impl AsMut<dyn Op> for Box<dyn InferenceOp> {
fn as_mut(&mut self) -> &mut dyn Op {
self.as_op_mut()
}
}