onnx_pb/
lib.rs

1mod attrs;
2mod util;
3
4pub use self::attrs::*;
5pub use self::util::*;
6
7include!(concat!(env!("OUT_DIR"), "/onnx.rs"));
8
9use self::{
10    tensor_proto::DataType,
11    tensor_shape_proto::{dimension, Dimension},
12    type_proto::{Tensor, Value},
13};
14
15impl From<i64> for dimension::Value {
16    fn from(v: i64) -> Self {
17        dimension::Value::DimValue(v)
18    }
19}
20
21impl From<String> for dimension::Value {
22    fn from(v: String) -> Self {
23        dimension::Value::DimParam(v)
24    }
25}
26
27impl<T: Into<Dimension>> From<Vec<T>> for TensorShapeProto {
28    fn from(v: Vec<T>) -> Self {
29        TensorShapeProto {
30            dim: v.into_iter().map(Into::into).collect(),
31        }
32    }
33}
34
35impl<V: Into<dimension::Value>> From<V> for Dimension {
36    fn from(v: V) -> Self {
37        tensor_shape_proto::Dimension {
38            denotation: String::default(),
39            value: Some(v.into()),
40        }
41    }
42}
43
44impl<K: Into<String>, V: Into<String>> From<(K, V)> for StringStringEntryProto {
45    fn from((k, v): (K, V)) -> Self {
46        StringStringEntryProto {
47            key: k.into(),
48            value: v.into(),
49        }
50    }
51}
52
53impl<T: Into<String>> From<T> for ValueInfoProto {
54    fn from(name: T) -> Self {
55        ValueInfoProto {
56            name: name.into(),
57            ..ValueInfoProto::default()
58        }
59    }
60}
61
62impl From<f32> for TensorProto {
63    fn from(data: f32) -> TensorProto {
64        TensorProto {
65            dims: vec![1],
66            float_data: vec![data],
67            data_type: DataType::Float as i32,
68            ..TensorProto::default()
69        }
70    }
71}
72
73impl From<Vec<f32>> for TensorProto {
74    fn from(data: Vec<f32>) -> TensorProto {
75        TensorProto {
76            dims: vec![data.len() as i64],
77            float_data: data,
78            data_type: DataType::Float as i32,
79            ..TensorProto::default()
80        }
81    }
82}
83
84impl From<i32> for TensorProto {
85    fn from(data: i32) -> TensorProto {
86        TensorProto {
87            dims: vec![1],
88            int32_data: vec![data],
89            data_type: DataType::Int32 as i32,
90            ..TensorProto::default()
91        }
92    }
93}
94
95impl From<Vec<i32>> for TensorProto {
96    fn from(data: Vec<i32>) -> TensorProto {
97        TensorProto {
98            dims: vec![data.len() as i64],
99            int32_data: data,
100            data_type: DataType::Int32 as i32,
101            ..TensorProto::default()
102        }
103    }
104}
105
106impl From<i64> for TensorProto {
107    fn from(data: i64) -> TensorProto {
108        TensorProto {
109            dims: vec![1],
110            int64_data: vec![data],
111            data_type: DataType::Int64 as i32,
112            ..TensorProto::default()
113        }
114    }
115}
116
117impl From<Vec<i64>> for TensorProto {
118    fn from(data: Vec<i64>) -> TensorProto {
119        TensorProto {
120            dims: vec![data.len() as i64],
121            int64_data: data,
122            data_type: DataType::Int64 as i32,
123            ..TensorProto::default()
124        }
125    }
126}
127
128impl From<f64> for TensorProto {
129    fn from(data: f64) -> TensorProto {
130        TensorProto {
131            dims: vec![1],
132            double_data: vec![data],
133            data_type: DataType::Double as i32,
134            ..TensorProto::default()
135        }
136    }
137}
138
139impl From<Vec<f64>> for TensorProto {
140    fn from(data: Vec<f64>) -> TensorProto {
141        TensorProto {
142            dims: vec![data.len() as i64],
143            double_data: data,
144            data_type: DataType::Double as i32,
145            ..TensorProto::default()
146        }
147    }
148}
149
150impl From<u64> for TensorProto {
151    fn from(data: u64) -> TensorProto {
152        TensorProto {
153            dims: vec![1],
154            uint64_data: vec![data],
155            data_type: DataType::Uint64 as i32,
156            ..TensorProto::default()
157        }
158    }
159}
160
161impl From<Vec<u64>> for TensorProto {
162    fn from(data: Vec<u64>) -> TensorProto {
163        TensorProto {
164            dims: vec![data.len() as i64],
165            uint64_data: data,
166            data_type: DataType::Uint64 as i32,
167            ..TensorProto::default()
168        }
169    }
170}
171
172impl From<DataType> for TypeProto {
173    fn from(typ: DataType) -> TypeProto {
174        TypeProto {
175            denotation: "".to_owned(),
176            value: Some(Value::TensorType(Tensor {
177                elem_type: typ as i32,
178                shape: None,
179            })),
180        }
181    }
182}