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
use crate::internal::*;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Store {
    pub id: String,
}

impl Store {
    pub fn new(id: &str) -> Store {
        Store { id: id.to_string() }
    }
}

impl Op for Store {
    fn name(&self) -> Cow<str> {
        "Store".into()
    }

    fn info(&self) -> TractResult<Vec<String>> {
        Ok(vec![format!("id: {:?}", self.id)])
    }

    impl_op_same_as!();
    op_as_typed_op!();
}

impl EvalOp for Store {
    fn is_stateless(&self) -> bool {
        false
    }

    fn state(
        &self,
        _session: &mut SessionState,
        _node_id: usize,
    ) -> TractResult<Option<Box<dyn OpState>>> {
        Ok(Some(Box::new(self.clone())))
    }
}

impl TypedOp for Store {
    as_op!();

    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
        ensure!(
            inputs.len() == 2,
            "Expected two inputs (input to propagate and state to store) for Store op"
        );
        Ok(tvec![inputs[0].clone()])
    }
}

impl OpState for Store {
    fn eval(
        &mut self,
        session: &mut SessionState,
        _op: &dyn Op,
        inputs: TVec<TValue>,
    ) -> TractResult<TVec<TValue>> {
        let (input, state) = args_2!(inputs);
        session.tensors.insert(self.id.clone(), state.into_tensor());
        Ok(tvec![input])
    }
}

trivial_op_state_freeeze!(Store);