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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
use super::*;
use crate::internal::*;
use crate::ops::Op;
use crate::prelude::*;
use std::fmt;
use std::hash::Hash;
use tract_data::internal::*;
use tract_itertools::Itertools;
pub trait SpecialOps<F, O> {
fn create_dummy(&self) -> O;
fn create_source(&self, fact: F) -> O;
fn is_source(op: &O) -> bool;
fn wire_node(
&mut self,
name: impl Into<String>,
op: impl Into<O>,
inputs: &[OutletId],
) -> TractResult<TVec<OutletId>>;
}
#[derive(Clone, Debug, Educe)]
#[educe(Hash)]
pub struct Graph<F, O>
where
F: Fact + Hash + Clone + 'static,
O: fmt::Debug + fmt::Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
{
pub nodes: Vec<Node<F, O>>,
pub inputs: Vec<OutletId>,
pub outputs: Vec<OutletId>,
#[educe(Hash(method = "hash_outlet_labels"))]
pub outlet_labels: HashMap<OutletId, String>,
#[educe(Hash(method = "hash_properties"))]
pub properties: HashMap<String, Arc<Tensor>>,
}
fn hash_outlet_labels<H: std::hash::Hasher>(it: &HashMap<OutletId, String>, state: &mut H) {
it.iter().sorted().for_each(|ol| ol.hash(state))
}
fn hash_properties<H: std::hash::Hasher>(it: &HashMap<String, Arc<Tensor>>, state: &mut H) {
it.iter().sorted_by_key(|(k, _)| k.to_owned()).for_each(|ol| ol.hash(state))
}
impl<F, O> DynHash for Graph<F, O>
where
F: Fact + Hash + Clone + 'static,
O: fmt::Debug + fmt::Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
{
fn dyn_hash(&self, hasher: &mut dyn std::hash::Hasher) {
dyn_hash(self, hasher)
}
}
impl<F, O> Default for Graph<F, O>
where
F: Fact + Hash + Clone + 'static,
O: fmt::Debug + fmt::Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
{
fn default() -> Graph<F, O> {
Graph {
nodes: vec![],
inputs: vec![],
outputs: vec![],
outlet_labels: HashMap::new(),
properties: HashMap::new(),
}
}
}
impl<F, O> Graph<F, O>
where
F: Fact + Hash + Clone + 'static,
O: fmt::Debug + fmt::Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
Graph<F, O>: SpecialOps<F, O>,
{
pub fn add_source(&mut self, name: impl Into<String>, fact: F) -> TractResult<OutletId> {
let source = self.create_source(fact.clone());
let id = self.add_node(name, source, tvec!(fact))?;
let id = OutletId::new(id, 0);
self.inputs.push(id);
Ok(id)
}
}
impl<F, O> Graph<F, O>
where
F: Fact + Hash + Clone + 'static,
O: fmt::Debug + fmt::Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
{
pub fn add_node(
&mut self,
name: impl Into<String>,
op: impl Into<O>,
output_facts: TVec<F>,
) -> TractResult<usize> {
let op = op.into();
let name = name.into();
let id = self.nodes.len();
let outputs =
output_facts.into_iter().map(|fact| Outlet { fact, successors: tvec!() }).collect();
let node = Node { id, name, op, inputs: vec![], outputs };
self.nodes.push(node);
Ok(id)
}
pub fn add_edge(&mut self, outlet: OutletId, inlet: InletId) -> TractResult<()> {
if let Some(previous) = self.nodes[inlet.node].inputs.get(inlet.slot).cloned() {
self.nodes[previous.node].outputs[previous.slot]
.successors
.retain(|&mut succ| succ != inlet);
}
{
let prec = &mut self.nodes[outlet.node];
prec.outputs[outlet.slot].successors.push(inlet);
}
let succ = &mut self.nodes[inlet.node];
if inlet.slot == succ.inputs.len() {
succ.inputs.push(outlet);
} else if inlet.slot < succ.inputs.len() {
succ.inputs[inlet.slot] = outlet;
} else {
bail!("Edges must be added in order and consecutive. Trying to connect input {:?} of node {:?} ", inlet.slot, succ)
}
Ok(())
}
pub fn input_outlets(&self) -> TractResult<&[OutletId]> {
Ok(&self.inputs)
}
pub fn set_input_outlets(&mut self, inputs: &[OutletId]) -> TractResult<()> {
self.inputs = inputs.to_vec();
Ok(())
}
pub fn with_input_outlets(mut self, inputs: &[OutletId]) -> TractResult<Self> {
self.set_input_outlets(inputs)?;
Ok(self)
}
pub fn set_input_names(
&mut self,
inputs: impl IntoIterator<Item = impl AsRef<str>>,
) -> TractResult<()> {
let mut ids = vec![];
for i in inputs.into_iter() {
let node = self.node_by_name(&i)?;
for o in 0..node.outputs.len() {
ids.push(OutletId::new(node.id, o))
}
}
self.inputs = ids;
Ok(())
}
pub fn with_input_names(
mut self,
inputs: impl IntoIterator<Item = impl AsRef<str>>,
) -> TractResult<Self> {
self.set_input_names(inputs)?;
Ok(self)
}
pub fn input_fact(&self, ix: usize) -> TractResult<&F> {
let input = self.input_outlets()?[ix];
self.outlet_fact(input)
}
pub fn input_fact_mut(&mut self, ix: usize) -> TractResult<&mut F> {
let input = self.input_outlets()?[ix];
self.outlet_fact_mut(input)
}
pub fn set_input_fact(&mut self, input: usize, fact: F) -> TractResult<()> {
let outlet = self.inputs[input];
self.set_outlet_fact(outlet, fact)
}
pub fn with_input_fact(mut self, input: usize, fact: F) -> TractResult<Self> {
self.set_input_fact(input, fact)?;
Ok(self)
}
pub fn output_outlets(&self) -> TractResult<&[OutletId]> {
Ok(&self.outputs)
}
pub fn auto_outputs(&mut self) -> TractResult<()> {
let outputs = self
.nodes
.iter()
.flat_map(|n| {
let id = n.id;
n.outputs.iter().enumerate().map(move |(ix, output_fact)| {
(OutletId::new(id, ix), output_fact.successors.len())
})
})
.filter(|(_f, succs)| *succs == 0)
.map(|(f, _)| f)
.collect();
self.outputs = outputs;
Ok(())
}
pub fn set_output_outlets(&mut self, outputs: &[OutletId]) -> TractResult<()> {
self.outputs = outputs.to_vec();
Ok(())
}
pub fn with_output_outlets(mut self, outputs: &[OutletId]) -> TractResult<Self> {
self.set_output_outlets(outputs)?;
Ok(self)
}
pub fn set_output_names(
&mut self,
outputs: impl IntoIterator<Item = impl AsRef<str>>,
) -> TractResult<()> {
let mut labels: HashMap<Cow<str>, OutletId> =
self.outlet_labels.iter().map(|(o, s)| (Cow::Borrowed(&**s), *o)).collect();
for n in self.nodes() {
for ix in 0..n.outputs.len() {
labels.insert(Cow::Owned(format!("{}:{}", &n.name, ix)), OutletId::new(n.id, ix));
}
}
let ids: Vec<OutletId> = outputs
.into_iter()
.map(|s| {
let s = s.as_ref();
labels
.get(s)
.cloned()
.or_else(|| self.nodes.iter().find(|n| n.name == s).map(|n| n.id.into()))
.ok_or_else(|| format_err!("Node {} not found", s))
})
.collect::<TractResult<_>>()?;
self.outputs = ids;
Ok(())
}
pub fn with_output_names(
mut self,
outputs: impl IntoIterator<Item = impl AsRef<str>>,
) -> TractResult<Self> {
self.set_output_names(outputs)?;
Ok(self)
}
pub fn output_fact(&self, ix: usize) -> TractResult<&F> {
let output = self.output_outlets()?[ix];
self.outlet_fact(output)
}
pub fn output_fact_mut(&mut self, ix: usize) -> TractResult<&mut F> {
let output = self.output_outlets()?[ix];
self.outlet_fact_mut(output)
}
pub fn set_output_fact(&mut self, output: usize, fact: F) -> TractResult<()> {
let outlet = self.outputs[output];
self.set_outlet_fact(outlet, fact)
}
pub fn with_output_fact(mut self, output: usize, fact: F) -> TractResult<Self> {
self.set_output_fact(output, fact)?;
Ok(self)
}
pub fn node_names(&self) -> impl Iterator<Item = &str> {
self.nodes.iter().map(|s| &*s.name)
}
pub fn node_id_by_name(&self, name: &str) -> TractResult<usize> {
self.nodes
.iter()
.find(|n| n.name == name)
.map(|n| n.id)
.ok_or_else(|| format_err!("No node found for name: \"{}\"", name).into())
}
pub fn node_by_name(&self, name: impl AsRef<str>) -> TractResult<&Node<F, O>> {
let id: usize = self.node_id_by_name(name.as_ref())?;
Ok(&self.nodes[id])
}
pub fn node_by_name_mut(&mut self, name: impl AsRef<str>) -> TractResult<&mut Node<F, O>> {
let id: usize = self.node_id_by_name(name.as_ref())?;
Ok(&mut self.nodes[id])
}
pub fn rename_node(&mut self, id: usize, name: &str) -> TractResult<()> {
self.node_mut(id).name = name.to_string();
Ok(())
}
pub fn node(&self, id: usize) -> &Node<F, O> {
&self.nodes[id]
}
pub fn node_mut(&mut self, id: usize) -> &mut Node<F, O> {
&mut self.nodes[id]
}
pub fn nodes(&self) -> &[Node<F, O>] {
&*self.nodes
}
pub fn nodes_mut(&mut self) -> &mut [Node<F, O>] {
&mut *self.nodes
}
pub fn node_facts(&self, id: usize) -> TractResult<(TVec<&F>, TVec<&F>)> {
Ok((self.node_input_facts(id)?, self.node_output_facts(id)?))
}
pub fn node_input_facts(&self, node_id: usize) -> TractResult<TVec<&F>> {
self.nodes[node_id].inputs.iter().map(|o| self.outlet_fact(*o)).collect()
}
pub fn node_output_facts(&self, node_id: usize) -> TractResult<TVec<&F>> {
Ok(self.nodes[node_id].outputs.iter().map(|o| &o.fact).collect())
}
pub fn outlet_fact(&self, outlet: OutletId) -> TractResult<&F> {
anyhow::ensure!(outlet.node < self.nodes.len(), "Invalid outlet for graph");
let outlets = &self.nodes[outlet.node].outputs;
outlets
.get(outlet.slot)
.map(|o| &o.fact)
.ok_or_else(|| format_err!("Invalid outlet reference: {:?}", outlet).into())
}
pub fn outlet_fact_mut(&mut self, outlet: OutletId) -> TractResult<&mut F> {
let outlets = &mut self.nodes[outlet.node].outputs;
outlets
.get_mut(outlet.slot)
.map(|o| &mut o.fact)
.ok_or_else(|| format_err!("Invalid outlet reference: {:?}", outlet).into())
}
pub fn outlets_fact_mut(&mut self, outlets: &[OutletId]) -> TractResult<TVec<&mut F>> {
assert!(outlets.iter().tuple_combinations().all(|(a, b)| a != b));
Ok(unsafe {
outlets
.iter()
.map(|o| &mut *(&self.nodes[o.node].outputs[o.slot].fact as *const F as *mut F))
.collect()
})
}
pub fn set_outlet_fact(&mut self, outlet: OutletId, fact: F) -> TractResult<()> {
let outlets = &mut self.nodes[outlet.node].outputs;
if outlets.len() <= outlet.slot {
bail!("Invalid outlet refererence: {:?}", outlet)
}
outlets[outlet.slot].fact = fact;
Ok(())
}
pub fn with_outlet_fact(mut self, outlet: OutletId, fact: F) -> TractResult<Self> {
self.set_outlet_fact(outlet, fact)?;
Ok(self)
}
pub fn outlet_label(&self, outlet: OutletId) -> Option<&str> {
self.outlet_labels.get(&outlet).map(|s| &**s)
}
pub fn set_outlet_label(&mut self, outlet: OutletId, label: String) -> TractResult<()> {
self.outlet_labels.insert(outlet, label);
Ok(())
}
pub fn with_outlet_label(mut self, outlet: OutletId, label: String) -> TractResult<Self> {
self.set_outlet_label(outlet, label)?;
Ok(self)
}
pub fn find_outlet_label(&self, label: &str) -> Option<OutletId> {
self.outlet_labels.iter().find(|(_k, v)| &**v == label).map(|(k, _v)| *k)
}
pub fn eval_order(&self) -> TractResult<Vec<usize>> {
eval_order(&self)
}
pub fn check_edges(&self) -> TractResult<()> {
for node_id in self.eval_order()? {
let node = &self.nodes[node_id];
for (ix, input) in node.inputs.iter().enumerate() {
let prec = &self.nodes[input.node];
if !prec.outputs[input.slot].successors.contains(&InletId::new(node.id, ix)) {
bail!(
"Mismatched oncoming edge, node:{} input:{} to {:?} not reciprocated",
node.id,
ix,
prec
)
}
}
for (ix, output) in node.outputs.iter().enumerate() {
for succ in &output.successors {
if self.nodes[succ.node].inputs[succ.slot] != OutletId::new(node.id, ix) {
bail!(
"Mismatched outgoing edge, node:{} output:{} to {:?} not reciprocated",
node.id,
ix,
succ
)
}
}
}
}
Ok(())
}
pub fn into_runnable(self) -> TractResult<RunnableModel<F, O, Self>> {
crate::plan::SimplePlan::new(self)
}
pub fn single_prec(&self, id: usize) -> TractResult<Option<&Node<F, O>>> {
let node = &self.nodes()[id];
if node.inputs.len() != 1 {
return Ok(None);
}
let prec = &self.nodes()[node.inputs[0].node];
if prec.outputs.iter().map(|of| of.successors.len()).sum::<usize>() != 1 {
return Ok(None);
}
Ok(Some(prec))
}
pub fn single_prec_at(&self, id: usize, count: usize) -> TractResult<Option<&Node<F, O>>> {
let mut node = self.node(id);
for _ in 0..count {
if let Some(next) = self.single_prec(node.id)? {
node = next
} else {
return Ok(None);
}
}
Ok(Some(node))
}
pub fn single_succ_at(&self, id: usize, count: usize) -> TractResult<Option<&Node<F, O>>> {
let mut node = self.node(id);
for _ in 0..count {
if let Some(next) = self.single_succ(node.id)? {
node = next
} else {
return Ok(None);
}
}
Ok(Some(node))
}
pub fn single_succ(&self, id: usize) -> TractResult<Option<&Node<F, O>>> {
let node = &self.nodes()[id];
if node.outputs.iter().map(|of| of.successors.len()).sum::<usize>() != 1 {
return Ok(None);
}
let succ = node.outputs[0].successors[0];
let succ = &self.nodes()[succ.node];
if succ.inputs.len() != 1 {
return Ok(None);
}
Ok(Some(succ))
}
pub fn outlet_successors(&self, outlet: OutletId) -> &[InletId] {
&self.nodes[outlet.node].outputs[outlet.slot].successors
}
}
impl<F: Fact + Clone + 'static, O> Graph<F, O>
where
F: Fact + Clone + 'static + From<std::sync::Arc<Tensor>> + Hash,
O: fmt::Debug
+ fmt::Display
+ From<crate::ops::konst::Const>
+ AsRef<dyn Op>
+ AsMut<dyn Op>
+ Clone
+ Hash
+ 'static,
{
pub fn add_const(
&mut self,
name: impl Into<String>,
v: impl IntoArcTensor,
) -> TractResult<OutletId> {
let v = v.into_arc_tensor();
let fact = F::from(v.clone());
let name = name.into();
self.add_node(name, crate::ops::konst::Const::new(v), tvec!(fact)).map(|id| id.into())
}
}
impl<F, O> fmt::Display for Graph<F, O>
where
F: Fact + Hash + Clone + 'static,
O: fmt::Debug + fmt::Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
for i in 0..self.nodes.len() {
let input_1 =
self.nodes[i].inputs.get(0).map(|o| format!("{:?}", o)).unwrap_or("".to_string());
let input_2 =
self.nodes[i].inputs.get(1).map(|o| format!("{:?}", o)).unwrap_or("".to_string());
let output_1 = self
.outlet_successors(OutletId::new(i, 0))
.get(0)
.map(|o| format!("{:?}", o))
.unwrap_or("".to_string());
let output_2 = self
.outlet_successors(OutletId::new(i, 0))
.get(1)
.map(|o| format!("{:?}", o))
.unwrap_or("".to_string());
writeln!(
fmt,
"{:5} | {:8} {:8} -> {:8} {:8} | {:25} {:50} {:?} => {:?}",
i,
input_1,
input_2,
output_1,
output_2,
self.nodes[i].op().name(),
self.nodes[i].name,
self.node_input_facts(i).unwrap(),
self.node_output_facts(i).unwrap(),
)?;
if self.nodes[i].inputs.len() > 2 {
writeln!(
fmt,
" | * inputs: {}",
self.nodes[i].inputs.iter().map(|s| format!("{:?}", s)).join(", ")
)?;
}
if self.nodes[i].outputs.len() > 1
|| self.outlet_successors((i, 0).into()).len() > 2
|| (self.outlet_label(i.into()).is_some()
&& self.outlet_label(i.into()).unwrap() != self.nodes[i].name)
{
for o in 0..self.nodes[i].outputs.len() {
if self.outlet_successors((i, o).into()).len() > 0 {
writeln!(
fmt,
" | * output #{}: {} {}",
o,
self.outlet_label((i, o).into()).unwrap_or(""),
self.outlet_successors((i, o).into())
.iter()
.map(|s| format!("{:?}", s))
.join(", "),
)?;
}
}
}
}
writeln!(fmt, "outputs: {}", self.outputs.iter().map(|o| format!("{:?}", o)).join(", "))?;
Ok(())
}
}
impl<F, O> Graph<F, O>
where
F: Fact + Clone + 'static + std::hash::Hash + for<'a> std::convert::From<&'a F>,
O: std::fmt::Display
+ std::fmt::Debug
+ Clone
+ AsRef<dyn Op>
+ AsMut<dyn Op>
+ Clone
+ 'static
+ std::hash::Hash
+ for<'a> std::convert::From<&'a O>,
Graph<F, O>: SpecialOps<F, O>,
{
#[cfg(debug_assertions)]
pub fn check_compact(&self) -> TractResult<()> {
let order = self.eval_order()?;
let useless_sources = self
.input_outlets()?
.iter()
.filter(|io| {
self.outlet_successors(**io).len() == 0
&& !self.output_outlets().unwrap().contains(io)
})
.count();
if order.len() + useless_sources != self.nodes.len() {
bail!(
"Eval order is {} long, nodes are {}, including {} unused sources",
order.len(),
self.nodes.len(),
useless_sources
);
}
if (0..order.len()).any(|ix| order[ix] != ix) {
bail!("eval order is not trivial");
}
let mut seen = std::collections::HashSet::new();
for (ix, n) in self.nodes.iter().enumerate() {
if ix != n.id {
bail!("Invalid node id: position is {}, node is {}", ix, n);
}
if seen.contains(&n.name) {
eprintln!("{}", self);
bail!("duplicate name {}", n.name);
}
seen.insert(&n.name);
}
Ok(())
}
pub fn compact(&self) -> TractResult<Self> {
use crate::model::translator::Translate;
let result = crate::model::translator::IntoTranslator.translate_model(self)?;
#[cfg(debug_assertions)]
{
result.check_compact()?;
}
Ok(result)
}
}
#[cfg(test)]
mod test {
use crate::internal::*;
#[test]
fn hashable() {
let mut model = TypedModel::default();
let _s =
model.add_source("source", TypedFact::dt_shape(DatumType::F32, &[1, 2, 3])).unwrap();
let mut hasher = std::collections::hash_map::DefaultHasher::default();
model.hash(&mut hasher);
}
}