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
use onnx_pb::{
tensor_proto::DataType,
tensor_shape_proto::Dimension,
type_proto::{self, Tensor},
TensorShapeProto, TypeProto, ValueInfoProto,
};
use crate::{
builder::{Bag, Marker, Node},
nodes,
};
#[derive(Default, Clone)]
pub struct Value {
name: String,
elem_type: DataType,
shape: Vec<Dimension>,
doc_string: Option<String>,
pub(crate) bag: Option<Bag>,
pub(crate) marker: Option<Marker>,
}
impl Value {
#[inline]
pub fn new<S: Into<String>>(name: S) -> Self {
Value {
name: name.into(),
..Value::default()
}
}
#[inline]
pub fn name<S: Into<String>>(mut self, name: S) -> Self {
self.name = name.into();
self
}
#[inline]
pub fn typed<T: Into<DataType>>(mut self, elem_type: T) -> Self {
self.elem_type = elem_type.into();
self
}
#[inline]
pub fn shape<D: Into<Dimension>>(mut self, shape: Vec<D>) -> Self {
self.shape = shape.into_iter().map(|dim| dim.into()).collect();
self
}
#[inline]
pub fn dim<D: Into<Dimension>>(mut self, dim: D) -> Self {
self.shape.push(dim.into());
self
}
#[inline]
pub fn node(self) -> nodes::Node {
let mut node = Node::named(self.name.clone()).build();
node.bag = self.bag.clone();
let marker = self.marker.as_ref().unwrap().clone();
let mut bag: Bag = self.bag.as_ref().unwrap().clone();
let value = self.build();
bag.value(value, marker);
node
}
#[inline]
pub fn build(self) -> ValueInfoProto {
ValueInfoProto {
name: self.name,
r#type: Some(TypeProto {
denotation: String::default(),
value: Some(type_proto::Value::TensorType(Tensor {
shape: Some(TensorShapeProto { dim: self.shape }),
elem_type: self.elem_type as i32,
})),
}),
doc_string: self.doc_string.unwrap_or_default(),
}
}
}
impl Into<ValueInfoProto> for Value {
fn into(self) -> ValueInfoProto {
self.build()
}
}