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
use onnx_pb::{AttributeProto, NodeProto};
use crate::{
attrs::{make_attribute, Attribute},
builder::Bag,
nodes,
};
#[derive(Default, Clone)]
pub struct Node {
op_type: String,
inputs: Vec<String>,
outputs: Vec<String>,
name: Option<String>,
doc_string: Option<String>,
domain: Option<String>,
attributes: Vec<AttributeProto>,
pub(crate) bag: Option<Bag>,
}
impl Node {
#[inline]
pub fn new<S: Into<String>>(op_type: S) -> Self {
Node {
op_type: op_type.into(),
..Node::default()
}
}
#[inline]
pub fn named<S: Into<String>>(name: S) -> Self {
Node {
name: Some(name.into()),
..Node::default()
}
}
#[inline]
pub fn name<S: Into<String>>(mut self, name: S) -> Self {
self.name = Some(name.into());
self
}
#[inline]
pub fn doc_string<S: Into<String>>(mut self, doc_string: S) -> Self {
self.doc_string = Some(doc_string.into());
self
}
#[inline]
pub fn domain<S: Into<String>>(mut self, domain: S) -> Self {
self.domain = Some(domain.into());
self
}
#[inline]
pub fn input<S: Into<String>>(mut self, input: S) -> Self {
self.inputs.push(input.into());
self
}
#[inline]
pub fn output<S: Into<String>>(mut self, output: S) -> Self {
self.outputs.push(output.into());
self
}
#[inline]
pub fn attribute<S: Into<String>, A: Into<Attribute>>(mut self, name: S, attribute: A) -> Self {
self.attributes.push(make_attribute(name, attribute));
self
}
#[inline]
pub fn build(self) -> nodes::Node {
let name = if let Some(name) = self.name {
name
} else {
if self.inputs.len() == 2 {
format!(
"{}_{}_{}",
self.inputs.get(0).unwrap(),
self.op_type,
self.inputs.get(1).unwrap()
)
} else {
format!(
"S{}_{}_{}E",
self.op_type,
self.inputs.join("_"),
self.op_type
)
}
};
let output = if self.outputs.len() > 0 {
self.outputs
} else {
vec![format!("{}O", name)]
};
let proto = NodeProto {
name,
domain: self.domain.unwrap_or_default(),
op_type: self.op_type,
doc_string: self.doc_string.unwrap_or_default(),
input: self.inputs,
output: output,
attribute: self.attributes,
};
let mut node = nodes::Node::from_proto(proto);
nodes::maybe_bag_node(self.bag.clone(), &mut node);
node
}
}
impl Into<nodes::Node> for Node {
fn into(self) -> nodes::Node {
self.build()
}
}
impl Into<NodeProto> for Node {
fn into(self) -> NodeProto {
self.build().into()
}
}