tract_core/ops/array/
reshape.rs

1use crate::internal::*;
2use tract_itertools::Itertools;
3
4#[derive(Debug, Clone, new, Default, Hash)]
5pub struct FiniteReshape {
6    pub shape: TVec<usize>,
7}
8
9impl Op for FiniteReshape {
10    fn name(&self) -> Cow<str> {
11        "Reshape".into()
12    }
13
14    fn info(&self) -> TractResult<Vec<String>> {
15        Ok(vec![format!("to shape: {}", self.shape.iter().join(","))])
16    }
17
18    op_as_typed_op!();
19}
20
21
22
23impl EvalOp for FiniteReshape {
24    fn is_stateless(&self) -> bool {
25        true
26    }
27
28    fn eval(&self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
29        let input = args_1!(inputs);
30        let mut tensor = input.into_tensor();
31        unsafe {
32            tensor.set_shape_unchecked(&self.shape);
33        }
34        Ok(tvec!(tensor.into_tvalue()))
35    }
36}
37
38impl TypedOp for FiniteReshape {
39    fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
40        Ok(tvec!(inputs[0].datum_type.fact(&self.shape)))
41    }
42
43    as_op!();
44}