Skip to main content

tract_core/ops/
identity.rs

1use crate::internal::*;
2
3#[derive(Debug, Clone, Default, Hash, PartialEq, Eq)]
4pub struct Identity;
5
6impl Op for Identity {
7    fn name(&self) -> StaticName {
8        "Identity".into()
9    }
10
11    op_as_typed_op!();
12}
13
14impl EvalOp for Identity {
15    op_out_of_plan!();
16
17    /// Evaluates the operation given the input tensors.
18    fn eval(&self, _ctx: &EvalContext, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
19        Ok(inputs)
20    }
21}
22
23impl TypedOp for Identity {
24    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
25        Ok(tvec!(inputs[0].clone()))
26    }
27
28    fn input_roi(
29        &self,
30        model: &TypedModel,
31        node: &TypedNode,
32    ) -> TractResult<Option<TVec<Option<TDim>>>> {
33        crate::optim::propagate_roi::bubble_roi(model, node)
34    }
35
36    fn declutter(
37        &self,
38        model: &TypedModel,
39        node: &TypedNode,
40    ) -> TractResult<Option<TypedModelPatch>> {
41        TypedModelPatch::shunt_one_op(model, node)
42    }
43
44    fn fuse(&self, model: &TypedModel, node: &TypedNode) -> TractResult<Option<TypedModelPatch>> {
45        TypedModelPatch::shunt_one_op(model, node)
46    }
47
48    fn axes_mapping(
49        &self,
50        inputs: &[&TypedFact],
51        outputs: &[&TypedFact],
52    ) -> TractResult<AxesMapping> {
53        AxesMapping::natural(inputs, outputs)
54    }
55
56    as_op!();
57}