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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::internal::*;
use itertools::Itertools;
#[derive(Debug, Clone, new, Default)]
pub struct Reshape {}
impl Reshape {
fn compute_shape<D: DimLike>(&self, input: &[D], shape: &[isize]) -> TractResult<TVec<D>> {
if shape.iter().all(|d| *d > 0) {
return Ok(shape.iter().map(|&d| D::from(d as usize)).collect());
}
let mut result: TVec<D> = shape
.iter()
.zip(input.iter().chain(std::iter::repeat(&D::from(1))))
.map(|(&shape, input)| if shape > 0 { D::from(shape as usize) } else { input.clone() })
.collect();
if let Some(minus_one) = shape.iter().position(|d| *d == -1) {
let prod_input: usize =
input.iter().try_fold(1, |acc, dim| dim.to_integer().map(|a| a as usize * acc))?;
let prod_shape: usize = result
.iter()
.enumerate()
.filter(|(ix, _)| *ix != minus_one)
.try_fold(1, |acc, (_, dim)| dim.to_integer().map(|a| a as usize * acc))?;
result[minus_one] = D::from(prod_input / prod_shape);
}
Ok(result)
}
}
impl Op for Reshape {
fn name(&self) -> Cow<str> {
"Reshape".into()
}
not_a_typed_op!();
not_a_pulsed_op!();
}
impl StatelessOp for Reshape {
fn eval(&self, mut inputs: TVec<Arc<Tensor>>) -> TractResult<TVec<Arc<Tensor>>> {
let (input, shape) = args_2!(inputs);
let shape: Vec<isize> =
shape.cast_to::<i64>()?.to_array_view::<i64>()?.iter().map(|&i| i as isize).collect();
let oshape = self.compute_shape(input.shape(), &shape)?;
unsafe { Ok(tvec![input.into_tensor().into_shape(&*oshape)?.into_arc_tensor()]) }
}
}
impl InferenceRulesOp for Reshape {
fn rules<'r, 'p: 'r, 's: 'r>(
&'s self,
s: &mut Solver<'r>,
inputs: &'p [TensorProxy],
outputs: &'p [TensorProxy],
) -> InferenceResult {
s.equals(&outputs[0].datum_type, &inputs[0].datum_type)?;
s.given_2(&inputs[0].shape, &inputs[1].value, move |s, ishape, shape| {
let shape: Vec<isize> = shape
.cast_to::<i64>()?
.to_array_view::<i64>()?
.iter()
.map(|&i| i as isize)
.collect();
let shape = self.compute_shape(&ishape, &shape)?;
s.equals(&outputs[0].shape, ShapeFact::from(shape))
})
}
fn to_typed(
&self,
_source: &InferenceModel,
node: &InferenceNode,
target: &mut TypedModel,
mapping: &HashMap<OutletId, OutletId>,
) -> TractResult<TVec<OutletId>> {
if let Some(ref shape) = target.outlet_fact(mapping[&node.inputs[1]])?.konst {
let input_shape: TVec<TDim> =
target.outlet_fact(mapping[&node.inputs[0]])?.shape.to_tvec();
let shape_spec: TVec<isize> =
shape.cast_to::<i64>()?.as_slice::<i64>()?.iter().map(|&i| i as isize).collect();
let shape = self.compute_shape(&input_shape, &shape_spec)?;
let op = TypedReshape::new(shape);
return target.wire_node(&*node.name, op, [mapping[&node.inputs[0]]].as_ref());
}
bail!("shape input is variable")
}
inference_op_as_op!();
}
#[derive(Debug, Clone, new, Default)]
pub struct TypedReshape {
shape: TVec<TDim>,
}
impl Op for TypedReshape {
fn name(&self) -> Cow<str> {
"TypedReshape".into()
}
fn info(&self) -> TractResult<Vec<String>> {
Ok(vec![format!("to shape: {}", self.shape.iter().map(|d| format!("{:?}", d)).join("x"))])
}
op_as_typed_op!();
not_a_pulsed_op!();
}
impl StatelessOp for TypedReshape {
fn eval(&self, mut inputs: TVec<Arc<Tensor>>) -> TractResult<TVec<Arc<Tensor>>> {
let input = args_1!(inputs);
let shape: TVec<usize> =
self.shape.iter().map(|d| Ok(d.to_integer()? as usize)).collect::<TractResult<_>>()?;
let o = unsafe { input.into_tensor().into_shape(&*shape)?.into_arc_tensor() };
Ok(tvec!(o))
}
}
impl TypedOp for TypedReshape {
typed_op_as_op!();
fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
Ok(tvec!(TypedFact::dt_shape(inputs[0].datum_type, &*self.shape)?))
}
fn codegen(
&self,
model: &TypedModel,
node: &TypedNode,
) -> TractResult<Option<TypedModelPatch>> {
let input_fact = model.outlet_fact(node.inputs[0])?;
if input_fact.shape.to_tvec() == self.shape {
return Ok(Some(TypedModelPatch::shunt_one_op(model, node)?));
} else if let Ok(shape) =
self.shape.iter().map(|d| Ok(d.to_integer()? as usize)).collect::<TractResult<_>>()
{
return Ok(Some(TypedModelPatch::single_unary_op(
model,
node,
FiniteReshape::new(shape),
)?));
}
Ok(None)
}
}
#[derive(Debug, Clone, new, Default)]
pub struct FiniteReshape {
shape: TVec<usize>,
}
impl Op for FiniteReshape {
fn name(&self) -> Cow<str> {
"FiniteReshape".into()
}
fn info(&self) -> TractResult<Vec<String>> {
Ok(vec![format!("to shape: {}", self.shape.iter().join("x"))])
}
op_as_typed_op!();
not_a_pulsed_op!();
}
impl StatelessOp for FiniteReshape {
fn eval(&self, mut inputs: TVec<Arc<Tensor>>) -> TractResult<TVec<Arc<Tensor>>> {
let input = args_1!(inputs);
let o = unsafe { input.into_tensor().into_shape(&*self.shape)?.into_arc_tensor() };
Ok(tvec!(o))
}
}
impl TypedOp for FiniteReshape {
fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
Ok(tvec!(TypedFact::dt_shape(inputs[0].datum_type, &*self.shape)?))
}
typed_op_as_op!();
}