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
use std::fs; mod google { mod protobuf { include!(concat!(env!("OUT_DIR"), "/prost/google.protobuf.rs")); } } pub mod tensorflow { include!(concat!(env!("OUT_DIR"), "/prost/tensorflow.rs")); } use self::tensorflow::attr_value::ListValue; use self::tensorflow::attr_value::Value; use self::tensorflow::{AttrValue, DataType, GraphDef, NodeDef, TensorProto, TensorShapeProto}; use std::convert::TryInto; use tract_core::internal::*; pub fn graph() -> GraphDef { GraphDef { library: None, node: vec![], version: 0, versions: None } } pub fn node() -> NodeDef { NodeDef { name: String::new(), op: String::new(), input: vec![], device: String::new(), attr: HashMap::new(), } } impl GraphDef { pub fn node(mut self, n: NodeDef) -> Self { self.node.push(n); self } pub fn write_to_bytes(&self) -> TractResult<Vec<u8>> { use prost::Message; let mut buf = vec![]; self.encode(&mut buf).map_err(|e| format!("Prost/Protobuf encoding error : {:?}", e))?; Ok(buf) } pub fn save_to<P: AsRef<::std::path::Path>>(self, p: P) -> TractResult<()> { let buf = self.write_to_bytes()?; fs::write(p, &buf)?; Ok(()) } } impl NodeDef { pub fn name<S: ToString>(mut self, n: S) -> NodeDef { self.name = n.to_string(); self } pub fn op<S: ToString>(mut self, n: S) -> NodeDef { self.op = n.to_string(); self } pub fn input<S: ToString>(mut self, n: S) -> NodeDef { self.input.push(n.to_string()); self } pub fn attr<S: ToString, V: Into<AttrValue>>(mut self, n: S, v: V) -> NodeDef { self.attr.insert(n.to_string(), v.into()); self } } impl NodeDef { pub fn get_attr_raw_str(&self, name: &str) -> TractResult<&[u8]> { Ok(self.get_attr_opt_raw_str(name)?.ok_or_else(|| { format!("Node {} ({}) expected string attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_raw_str(&self, name: &str) -> TractResult<Option<&[u8]>> { if let Some(a) = self.attr.get(name) { if let Value::S(bytes) = a.value.as_ref().unwrap() { return Ok(Some(&bytes)); } }; Ok(None) } pub fn get_attr_str(&self, name: &str) -> TractResult<String> { Ok(self.get_attr_opt_str(name)?.ok_or_else(|| { format!("Node {} ({}) expected UTF-8 string attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_str(&self, name: &str) -> TractResult<Option<String>> { if let Some(s) = self.get_attr_opt_raw_str(name)? { Ok(Some(String::from_utf8(s.to_vec()).map_err(|_| { format!( "Node {} ({}) expected an UTF-8 string for attribute '{}'", self.name, self.op, name ) })?)) } else { Ok(None) } } pub fn get_attr_bool(&self, name: &str) -> TractResult<bool> { Ok(self.get_attr_opt_bool(name)?.ok_or_else(|| { format!("Node {} ({}) expected bool attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_bool(&self, name: &str) -> TractResult<Option<bool>> { if let Some(a) = self.attr.get(name) { if let Value::B(v) = a.value.as_ref().unwrap() { return Ok(Some(*v)); } }; Ok(None) } pub fn get_attr_datum_type(&self, name: &str) -> TractResult<DatumType> { Ok(self.get_attr_opt_datum_type(name)?.ok_or_else(|| { format!("Node {} ({}) expected datum_type attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_datum_type(&self, name: &str) -> TractResult<Option<DatumType>> { if let Some(a) = self.attr.get(name) { if let Value::Type(v) = a.value.as_ref().unwrap() { return Ok(Some(DataType::from_i32(*v).unwrap().try_into()?)); } }; Ok(None) } pub fn get_attr_shape(&self, name: &str) -> TractResult<TVec<isize>> { Ok(self.get_attr_opt_shape(name)?.ok_or_else(|| { format!("Node {} ({}) expected shape attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_shape(&self, name: &str) -> TractResult<Option<TVec<isize>>> { if let Some(a) = self.attr.get(name) { if let Value::Shape(ref shape) = a.value.as_ref().unwrap() { return Ok(Some(shape.try_into()?)); } }; Ok(None) } pub fn get_attr_tensor(&self, name: &str) -> TractResult<tract_core::internal::Tensor> { Ok(self.get_attr_opt_tensor(name)?.ok_or_else(|| { format!("Node {} ({}) expected tensor attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_tensor( &self, name: &str, ) -> TractResult<Option<tract_core::internal::Tensor>> { if let Some(a) = self.attr.get(name) { if let Value::Tensor(ref t) = a.value.as_ref().unwrap() { return Ok(Some(t.try_into()?)); } }; Ok(None) } pub fn get_attr_int<T: ::num_traits::FromPrimitive>(&self, name: &str) -> TractResult<T> { Ok(self.get_attr_opt_int(name)?.ok_or_else(|| { format!("Node {} ({}) expected int attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_int<T: ::num_traits::FromPrimitive>( &self, name: &str, ) -> TractResult<Option<T>> { if let Some(a) = self.attr.get(name) { if let Value::I(i) = a.value.as_ref().unwrap() { return Ok(Some(T::from_i64(*i).unwrap())); } }; Ok(None) } pub fn get_attr_float<T: ::num_traits::FromPrimitive>(&self, name: &str) -> TractResult<T> { Ok(self.get_attr_opt_float(name)?.ok_or_else(|| { format!("Node {} ({}) expected int attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_float<T: ::num_traits::FromPrimitive>( &self, name: &str, ) -> TractResult<Option<T>> { if let Some(a) = self.attr.get(name) { if let Value::F(i) = a.value.as_ref().unwrap() { return Ok(Some(T::from_f32(*i).unwrap())); } }; Ok(None) } pub fn get_attr_list_int<T: ::num_traits::FromPrimitive>( &self, name: &str, ) -> TractResult<Vec<T>> { Ok(self.get_attr_opt_list_int(name)?.ok_or_else(|| { format!("Node {} ({}) expected list<int> attribute '{}'", self.name, self.op, name) })?) } pub fn get_attr_opt_list_int<T: ::num_traits::FromPrimitive>( &self, name: &str, ) -> TractResult<Option<Vec<T>>> { if let Some(a) = self.attr.get(name) { if let Value::List(list) = a.value.as_ref().unwrap() { return Ok(Some(list.i.iter().map(|&i| T::from_i64(i).unwrap()).collect())); } }; Ok(None) } } impl From<DataType> for AttrValue { fn from(t: DataType) -> AttrValue { AttrValue { value: Some(Value::Type(t.into())) } } } impl<'a> From<&'a str> for AttrValue { fn from(t: &'a str) -> AttrValue { AttrValue { value: Some(Value::S(t.as_bytes().to_vec())) } } } impl From<i32> for AttrValue { fn from(t: i32) -> AttrValue { AttrValue::from(t as i64) } } impl From<i64> for AttrValue { fn from(t: i64) -> AttrValue { AttrValue { value: Some(Value::I(t)) } } } impl From<f32> for AttrValue { fn from(t: f32) -> AttrValue { AttrValue { value: Some(Value::F(t)) } } } impl From<Vec<i64>> for AttrValue { fn from(t: Vec<i64>) -> AttrValue { AttrValue { value: Some(Value::List(ListValue { s: vec![], i: t, f: vec![], b: vec![], r#type: vec![], shape: vec![], tensor: vec![], func: vec![], })), } } } impl From<TensorProto> for AttrValue { fn from(t: TensorProto) -> AttrValue { AttrValue { value: Some(Value::Tensor(t.into())) } } } impl From<TensorShapeProto> for AttrValue { fn from(t: TensorShapeProto) -> AttrValue { AttrValue { value: Some(Value::Shape(t.into())) } } }