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
use crate::internal::*; pub use super::{InletId, Model, Node, OutletId}; pub trait ModelDsl<TI: TensorInfo> { fn single_prec(&self, id: usize) -> TractResult<Option<&Node<TI>>>; fn single_prec_at(&self, id: usize, count: usize) -> TractResult<Option<&Node<TI>>>; fn single_succ(&self, id: usize) -> TractResult<Option<&Node<TI>>>; fn single_succ_at(&self, id: usize, count: usize) -> TractResult<Option<&Node<TI>>>; fn add_source(&mut self, name: impl Into<String>, fact: TI) -> TractResult<usize>; fn chain( &mut self, name: impl Into<String>, op: impl Into<Box<Op>>, facts: TVec<TI>, ) -> TractResult<usize>; fn chain_after( &mut self, tap: OutletId, name: impl Into<String>, op: impl Into<Box<Op>>, facts: TVec<TI>, ) -> TractResult<usize>; } impl<TI: TensorInfo> ModelDsl<TI> for Model<TI> { fn add_source(&mut self, name: impl Into<String>, fact: TI) -> TractResult<usize> { let id = self.add_node( name, crate::ops::source::Source::new(), tvec!(fact), )?; Ok(id) } fn chain( &mut self, name: impl Into<String>, op: impl Into<Box<Op>>, facts: TVec<TI>, ) -> TractResult<usize> { let previous_id = self.nodes().len() - 1; self.chain_after(OutletId::new(previous_id, 0), name, op.into(), facts) } fn single_prec(&self, id: usize) -> TractResult<Option<&Node<TI>>> { 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)) } fn single_prec_at(&self, id: usize, count: usize) -> TractResult<Option<&Node<TI>>> { 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)) } fn single_succ_at(&self, id: usize, count: usize) -> TractResult<Option<&Node<TI>>> { 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)) } fn single_succ(&self, id: usize) -> TractResult<Option<&Node<TI>>> { 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)) } fn chain_after( &mut self, tap: OutletId, name: impl Into<String>, op: impl Into<Box<Op>>, facts: TVec<TI>, ) -> TractResult<usize> { let id = self.add_node(name, op, facts)?; self.add_edge(tap, InletId::new(id, 0))?; Ok(id) } } pub trait ModelDslConst { fn add_const(&mut self, name: impl Into<String>, v: SharedTensor) -> TractResult<usize>; fn plug_const( &mut self, inlet: InletId, name: impl Into<String>, v: SharedTensor, ) -> TractResult<()>; } impl ModelDslConst for super::InferenceModel { fn add_const(&mut self, name: impl Into<String>, v: SharedTensor) -> TractResult<usize> { let facts = tvec!(v.clone().into()); self.add_node(name, crate::ops::konst::Const::new(v), facts) } fn plug_const( &mut self, inlet: InletId, name: impl Into<String>, v: SharedTensor, ) -> TractResult<()> { let cst = self.add_const(name, v)?; self.add_edge(OutletId::new(cst, 0), inlet)?; Ok(()) } } impl ModelDslConst for super::TypedModel { fn add_const(&mut self, name: impl Into<String>, v: SharedTensor) -> TractResult<usize> { let facts = tvec!(v.clone().into()); self.add_node(name, crate::ops::konst::Const::new(v), facts) } fn plug_const( &mut self, inlet: InletId, name: impl Into<String>, v: SharedTensor, ) -> TractResult<()> { let cst = self.add_const(name, v)?; self.add_edge(OutletId::new(cst, 0), inlet)?; Ok(()) } } pub trait ModelDslInfer { fn add_source_default(&mut self, name: impl Into<String>) -> TractResult<usize>; fn add_node_default( &mut self, name: impl Into<String>, op: impl Into<Box<Op>>, ) -> TractResult<usize>; fn chain_default( &mut self, name: impl Into<String>, op: impl Into<Box<Op>>, ) -> TractResult<usize>; } impl ModelDslInfer for super::InferenceModel { fn add_source_default(&mut self, name: impl Into<String>) -> TractResult<usize> { self.add_source(name, TensorFact::default()) } fn add_node_default( &mut self, name: impl Into<String>, op: impl Into<Box<Op>>, ) -> TractResult<usize> { self.add_node(name, op, tvec!(TensorFact::default())) } fn chain_default( &mut self, name: impl Into<String>, op: impl Into<Box<Op>>, ) -> TractResult<usize> { self.chain(name, op, tvec!(TensorFact::default())) } }