tract_core/ops/
identity.rs

1use crate::internal::*;
2
3#[derive(Debug, Clone, Default, Hash)]
4pub struct Identity;
5
6impl Op for Identity {
7    fn name(&self) -> Cow<str> {
8        "Identity".into()
9    }
10
11    op_as_typed_op!();
12}
13
14impl EvalOp for Identity {
15    fn is_stateless(&self) -> bool {
16        true
17    }
18
19    /// Evaluates the operation given the input tensors.
20    fn eval(&self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
21        Ok(inputs)
22    }
23}
24
25impl TypedOp for Identity {
26    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
27        Ok(tvec!(inputs[0].clone()))
28    }
29
30    fn declutter(
31        &self,
32        model: &TypedModel,
33        node: &TypedNode,
34    ) -> TractResult<Option<TypedModelPatch>> {
35        TypedModelPatch::shunt_one_op(model, node)
36    }
37
38    fn fuse(&self, model: &TypedModel, node: &TypedNode) -> TractResult<Option<TypedModelPatch>> {
39        TypedModelPatch::shunt_one_op(model, node)
40    }
41
42    fn axes_mapping(
43        &self,
44        inputs: &[&TypedFact],
45        outputs: &[&TypedFact],
46    ) -> TractResult<AxesMapping> {
47        AxesMapping::natural(inputs, outputs)
48    }
49
50    as_op!();
51}
52
53#[derive(Debug, Clone, Default, Hash)]
54pub struct PinConst;
55
56impl Op for PinConst {
57    fn name(&self) -> Cow<str> {
58        "PinConst".into()
59    }
60
61    op_as_typed_op!();
62}
63
64impl EvalOp for PinConst {
65    fn is_stateless(&self) -> bool {
66        false
67    }
68
69    /// Evaluates the operation given the input tensors.
70    fn eval(&self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
71        Ok(inputs)
72    }
73
74    fn state(
75        &self,
76        _session: &mut SessionState,
77        _node_id: usize,
78    ) -> TractResult<Option<Box<dyn OpState>>> {
79        Ok(Some(Box::new(self.clone())))
80    }
81}
82
83impl OpState for PinConst {
84    fn eval(
85            &mut self,
86            _session: &mut SessionState,
87            _op: &dyn Op,
88            inputs: TVec<TValue>,
89        ) -> TractResult<TVec<TValue>> {
90        Ok(inputs)
91    }
92}
93
94impl TypedOp for PinConst {
95    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
96        Ok(tvec!(inputs[0].without_value()))
97    }
98
99    as_op!();
100}
101
102trivial_op_state_freeeze!(PinConst);