1use crate::internal::*;
2use downcast_rs::Downcast;
3use std::fmt;
4
5pub trait ElementWiseMiniOp:
6 fmt::Debug + dyn_clone::DynClone + Send + Sync + 'static + Downcast
7{
8 fn name(&self) -> String;
9 fn prefix(&self) -> &'static str {
10 ""
11 }
12 fn validation(&self) -> Validation {
13 Validation::Accurate
14 }
15 #[allow(unused_variables)]
16 fn output_type(&self, input_type: DatumType) -> Option<DatumType> {
17 None
18 }
19 #[allow(unused_variables)]
20 fn eval_in_place(&self, t: &mut Tensor, out_dt: Option<DatumType>) -> TractResult<()> {
21 bail!("Element wise eval in-place not defined");
22 }
23 #[allow(unused_variables)]
24 fn eval_out_of_place(&self, t: &Tensor, out_dt: Option<DatumType>) -> TractResult<Tensor> {
25 bail!("Element wise eval out-of-place place not defined");
26 }
27 #[allow(unused_variables)]
28 fn cost_per_element(&self, dt: DatumType) -> TVec<(Cost, usize)> {
29 tvec!()
30 }
31 #[allow(unused_variables)]
32 fn operating_datum_type(&self, dt: DatumType) -> DatumType {
33 dt
34 }
35 #[allow(unused_variables)]
36 fn declutter(
37 &self,
38 model: &TypedModel,
39 node: &TypedNode,
40 ) -> TractResult<Option<TypedModelPatch>> {
41 Ok(None)
42 }
43
44 #[allow(unused_variables)]
45 fn quantize(
46 &self,
47 dt: DatumType,
48 scale: f32,
49 zero_point: i32,
50 ) -> TractResult<Option<Box<dyn ElementWiseMiniOp>>> {
51 Ok(None)
52 }
53 #[allow(unused_variables)]
54 fn info(&self) -> TractResult<Vec<String>> {
55 Ok(vec![])
56 }
57
58 #[allow(unused_variables)]
59 fn same_as(&self, other: &dyn ElementWiseMiniOp) -> bool {
60 false
61 }
62}
63
64dyn_clone::clone_trait_object!(ElementWiseMiniOp);
65downcast_rs::impl_downcast!(ElementWiseMiniOp);
66
67#[derive(Debug, Clone)]
68pub struct ElementWiseOp(pub Box<dyn ElementWiseMiniOp>, pub Option<DatumType>);
69
70impl ElementWiseOp {
71 fn output_datum_type(&self, input_dt: DatumType) -> DatumType {
72 self.1.unwrap_or(self.0.operating_datum_type(input_dt))
73 }
74}
75
76impl Op for ElementWiseOp {
77 fn name(&self) -> Cow<str> {
78 self.0.name().into()
79 }
80
81 fn info(&self) -> TractResult<Vec<String>> {
82 self.0.info()
83 }
84
85 fn validation(&self) -> Validation {
86 self.0.validation()
87 }
88
89 fn same_as(&self, other: &dyn Op) -> bool {
90 let Some(other) = other.downcast_ref::<ElementWiseOp>() else { return false };
91 self.1 == other.1 && self.0.same_as(&*other.0)
92 }
93
94 op_as_typed_op!();
95}
96
97impl EvalOp for ElementWiseOp {
98 fn is_stateless(&self) -> bool {
99 true
100 }
101
102 fn eval(&self, mut inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
103 if let Some(_dt) = self.0.output_type(inputs[0].datum_type()) {
104 Ok(tvec!(self.0.eval_out_of_place(&inputs[0], self.1)?.into_tvalue()))
105 } else {
106 let mut m = inputs.remove(0).into_tensor();
107 self.0.eval_in_place(&mut m, self.1)?;
108 Ok(tvec!(m.into()))
109 }
110 }
111}
112
113impl TypedOp for ElementWiseOp {
114 fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
115 let mut fact = inputs[0].clone().without_value();
116 let dt = self.output_datum_type(fact.datum_type);
117 if let Some(dt) = self.1 {
118 fact.datum_type = dt;
119 } else if let Some(dt) = self.0.output_type(dt) {
120 fact.datum_type = dt;
121 }
122 Ok(tvec!(fact))
123 }
124
125 fn change_axes(
126 &self,
127 model: &TypedModel,
128 node: &TypedNode,
129 _io: InOut,
130 change: &AxisOp,
131 ) -> TractResult<Option<AxisChangeConsequence>> {
132 Ok(Some(AxisChangeConsequence::new(model, node, None, change)))
133 }
134
135 fn declutter(
136 &self,
137 model: &TypedModel,
138 node: &TypedNode,
139 ) -> TractResult<Option<TypedModelPatch>> {
140 if let Some(prec) = model.single_prec(node.id)? {
141 if prec.op_is::<AxisOp>() || prec.op_is::<IntoShape>() {
142 let mut patch = TypedModelPatch::default();
143 let mut wire = tvec!(patch.tap_model(model, prec.inputs[0])?);
144 wire = patch.wire_node(&node.name, &node.op, &wire)?;
145 wire = patch.wire_node(&prec.name, &prec.op, &wire)?;
146 patch.shunt_outside(model, node.id.into(), wire[0])?;
147 return Ok(Some(patch))
148 }
149 }
150 self.0.declutter(model, node)
151 }
152
153 fn axes_mapping(
154 &self,
155 inputs: &[&TypedFact],
156 outputs: &[&TypedFact],
157 ) -> TractResult<AxesMapping> {
158 AxesMapping::natural(inputs, outputs)
159 }
160
161 fn cost(&self, inputs: &[&TypedFact]) -> TractResult<TVec<(Cost, TDim)>> {
162 let count: TDim = inputs[0].shape.iter().product();
163 Ok(self
164 .0
165 .cost_per_element(inputs[0].datum_type)
166 .into_iter()
167 .map(|(c, n)| (c, count.clone() * n))
168 .collect())
169 }
170
171 fn quantize(
172 &self,
173 _model: &TypedModel,
174 _node: &TypedNode,
175 dt: DatumType,
176 scale: f32,
177 zero_point: i32,
178 ) -> TractResult<Option<Box<dyn TypedOp>>> {
179 if let Some(mini) = self.0.quantize(dt, scale, zero_point)? {
180 Ok(Some(Box::new(ElementWiseOp(mini, self.1))))
181 } else {
182 Ok(None)
183 }
184 }
185
186 as_op!();
187}
188
189#[macro_export]
190macro_rules! element_wise {
191 ($func:ident, $Op:ident $({$( $(#[$meta: meta])? $var: ident : $var_typ: path),*})?,
192 $([$($typ:ident),*] => $f:expr ),*
193 $(; q: $( [$($typ_dt:ident),*] => $f_f32:expr),*)?
194 $(; cost: $cost:expr )?
195 $(; declutter: $declutter:expr )?
196 $(; operating_datum_type: $operating_datum_type:expr )?
197 $(; prefix: $prefix:expr )?
198 $(; quantize: $quantize:expr )?
199 $(; validation: $validation:expr )?
200 ) => {
201 #[derive(Debug, Clone)]
202 pub struct $Op { $( $( $(#[$meta])? pub $var: $var_typ),* )? }
203 impl $crate::ops::element_wise::ElementWiseMiniOp for $Op {
204 fn name(&self) -> String {
205 format!("{}{}", self.prefix(), stringify!($Op))
206 }
207 #[allow(unused_variables)]
208 fn same_as(&self, other: &dyn ElementWiseMiniOp) -> bool {
209 let Some(other) = other.downcast_ref::<$Op>() else { return false };
210 $( $( if self.$var != other.$var { return false; })* )?
211 true
212 }
213 fn eval_in_place(&self, t: &mut Tensor, out_dt: Option<DatumType>) -> TractResult<()> {
214 $(
215 $(if out_dt.unwrap_or(t.datum_type()) == $typ::datum_type() {
216 let t: &mut[$typ] = t.as_slice_mut::<$typ>()?;
217 let f: fn(&Self, &mut[$typ]) -> TractResult<()> = $f;
218 f(self, t)?;
219 return Ok(())
220 }
221 )*
222 )*
223 $(
224 $(
225 $(
226 let mut input_dt = t.datum_type();
227 let sout_dt = out_dt.unwrap_or(input_dt);
228 if sout_dt.unquantized() == <$typ_dt>::datum_type().unquantized() {
229 if input_dt.unquantized() != sout_dt.unquantized() {
230 *t = match input_dt.unquantized() {
232 DatumType::U8 => t.clone().into_arc_tensor().offset_u8_as_i8(),
233 DatumType::I8 => t.clone().into_arc_tensor().offset_i8_as_u8(),
234 unknown_dt => bail!("unexpected quantization input dt {:?}", unknown_dt)
235 }.into_tensor();
236 input_dt = t.datum_type(); }
238 unsafe { t.set_datum_type(sout_dt) } let t: &mut[$typ_dt] = t.as_slice_mut::<$typ_dt>()?;
240 let f: fn(&Self, &mut[$typ_dt], DatumType, DatumType) -> TractResult<()> = |_, xs, input_dt, out_dt| {
241 let (izp, iscale) = input_dt.zp_scale();
242 let (ozp, oscale) = out_dt.zp_scale();
243 xs.iter_mut().for_each(|x| {
244 let x_f32 = (*x as f32 - izp as f32) * iscale;
245 *x = (($f_f32(x_f32) / oscale) + ozp as f32).as_()
246 });
247 Ok(())
248 };
249 f(self, t, input_dt, sout_dt)?;
250 return Ok(())
251 }
252 )*
253 )*
254 )?
255 bail!("{} does not support {:?}", self.name(), out_dt.unwrap_or(t.datum_type()));
256 }
257 $(
258 fn cost_per_element(&self, dt: DatumType) -> TVec<(Cost, usize)> {
259 $cost(dt)
260 }
261 )?
262 $(
263 fn declutter(
264 &self,
265 model: &TypedModel,
266 node: &TypedNode,
267 ) -> TractResult<Option<TypedModelPatch>> {
268 $declutter(model, node)
269 }
270 )?
271 $(
272 fn prefix(&self) -> &'static str {
273 $prefix
274 }
275 )?
276 $(
277 fn quantize(
278 &self,
279 dt: DatumType,
280 scale: f32,
281 zero_point: i32) -> TractResult<Option<Box<dyn ElementWiseMiniOp>>> {
282 $quantize(&self, dt, scale, zero_point)
283 }
284 )?
285 $(
286 fn validation(&self) -> Validation {
287 $validation
288 }
289 )?
290 $(
291 fn operating_datum_type(&self, dt: DatumType) -> DatumType {
292 ($operating_datum_type)(dt)
293 }
294 )?
295 }
296 pub fn $func($( $($var: $var_typ),* )?) -> $crate::ops::element_wise::ElementWiseOp {
297 $crate::ops::element_wise::ElementWiseOp(Box::new($Op { $( $($var),* )? }), None)
298 }
299 }
300}
301
302#[macro_export]
303macro_rules! element_wise_oop {
304 ($(#[$fmeta:meta])* $func:ident, $Op:ident $({$( $(#[$meta: meta])? $var: ident : $var_typ: path),*})?,
305 $( [$($typ:ident),*] => $typ_dst:ident $f:expr ),*
306 $(; cost: $cost:expr )?
307 $(; info: $info:expr )?
308 $(; operating_datum_type: $operating_datum_type:expr )?
309 $(; prefix: $prefix:expr )?
310 $(; quantize: $quantize:expr )?
311 $(; validation: $validation:expr )?
312 ) => {
313 #[derive(Debug, Clone)]
314 pub struct $Op { $( $($(#[$meta])? pub $var: $var_typ),* )? }
315 impl $crate::ops::element_wise::ElementWiseMiniOp for $Op {
316 fn name(&self) -> String {
317 format!("{}{}", self.prefix(), stringify!($Op))
318 }
319 fn output_type(&self, input_type: DatumType) -> Option<DatumType> {
320 $(
321 $(if input_type == $typ::datum_type() {
322 return Some(<$typ_dst>::datum_type())
323 }
324 )*
325 )*
326 None
327 }
328 fn eval_out_of_place(&self, t: &Tensor, _out_dt: Option<DatumType>) -> TractResult<Tensor> {
329 $(
330 let mut dst = unsafe { Tensor::uninitialized_dt(<$typ_dst>::datum_type(), &t.shape())? };
331 $(if t.datum_type() == $typ::datum_type() {
332 let f: fn(&Self, &[$typ], &mut[$typ_dst]) -> TractResult<()> = $f;
333 f(self, t.as_slice::<$typ>()?, dst.as_slice_mut::<$typ_dst>()?)?;
334 return Ok(dst)
335 }
336 )*
337 )*
338 bail!("{} does not support {:?}", self.name(), t.datum_type());
339 }
340 $(
341 fn cost_per_element(&self, dt: DatumType) -> TVec<(Cost, usize)> {
342 $cost(dt)
343 }
344 )?
345 $(
346 fn info(&self) -> TractResult<Vec<String>> {
347 $info(self)
348 }
349 )?
350 $(
351 fn prefix(&self) -> &'static str {
352 $prefix
353 }
354 )?
355 $(
356 fn quantize(
357 &self,
358 dt: DatumType,
359 scale: f32,
360 zero_point: i32) -> TractResult<Option<Box<dyn ElementWiseMiniOp>>> {
361 $quantize(ft, scale, zero_point)
362 }
363 )?
364 $(
365 fn validation(&self) -> Validation {
366 $validation
367 }
368 )?
369 $(
370 fn operating_datum_type(&self, dt: DatumType) -> DatumType {
371 ($operating_datum_type)(dt)
372 }
373 )?
374 }
375 $(#[$fmeta])*
376 pub fn $func($( $($var: $var_typ),* )?) -> $crate::ops::element_wise::ElementWiseOp {
377 $crate::ops::element_wise::ElementWiseOp(Box::new($Op { $( $($var),* )? }), None)
378 }
379 }
380}