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
use crate::internal::*;
use crate::ops::array::Slice;
use crate::optim::OptimizerSession;
#[derive(Clone, Debug)]
pub struct PushSliceUp;
impl super::TypedPass for PushSliceUp {
fn reset(&mut self) -> TractResult<()> {
Ok(())
}
fn next(
&mut self,
_session: &mut OptimizerSession,
model: &TypedModel,
) -> TractResult<Option<TypedModelPatch>> {
for n in model.eval_order()? {
let (ifacts, ofacts) = model.node_facts(n)?;
if ofacts.len() != 1 {
continue;
}
let node = model.node(n);
let invariants = node.op.invariants(&ifacts, &ofacts)?;
'axis: for axis in 0..ofacts[0].rank() {
if let Some(boundaries) = should_slice_output(model, node, axis)? {
let mut splits = tvec!();
let mut patch = TypedModelPatch::new("push slice up");
let inputs = node
.inputs
.iter()
.map(|i| patch.tap_model(model, *i))
.collect::<TractResult<TVec<OutletId>>>()?;
let mut start = 0;
let axis_info = invariants.track_output_axis(0, axis);
for end in &boundaries {
let mut wires = tvec!();
for input_ix in 0..inputs.len() {
let mut wire = inputs[input_ix];
if let Some(input_axis) = axis_info.and_then(|it| it.inputs[input_ix]) {
wire = patch.wire_node(
format!(
"{}.split-{}-over-{}.{}..{}.slice",
&node.name, input_ix, input_axis, start, end
),
Slice {
axis: input_axis,
start: start.to_dim(),
end: end.to_dim(),
},
&[wire],
)?[0];
}
wires.push(wire);
}
let Some(wire) = node.op.slice(
&mut patch,
&format!(
"{}.split-over-{}.{}..{}",
&node.name, axis, start, end
),
&wires,
axis,
start,
*end,
)? else {
continue 'axis };
splits.push(wire[0]);
start = *end;
}
rewire_sliced_outputs(model, node, axis, &mut patch, &boundaries, &splits)?;
return Ok(Some(patch));
}
}
}
Ok(None)
}
}
pub fn should_slice_output(
model: &TypedModel,
node: &TypedNode,
axis: usize,
) -> TractResult<Option<TVec<usize>>> {
let Some(slice) = node.outputs[0].successors.iter().find_map(|inlet| {
model.node(inlet.node).op_as::<Slice>().filter(|slice| slice.axis == axis).map(|_| inlet.node)
}) else {
return Ok(None)
};
let slice_op = model.node(slice).op_as::<Slice>().unwrap();
let axis = slice_op.axis;
let mut boundaries = tvec!();
for succ in &node.outputs[0].successors {
if let Some(slice) = model.node(succ.node).op_as::<Slice>() {
if slice.axis == axis {
boundaries.push(slice.start.clone());
boundaries.push(slice.end.clone());
}
}
}
let mut boundaries: TVec<usize> = if let Ok(boundaries) =
boundaries.iter().map(|x| x.to_usize()).collect::<TractResult<TVec<_>>>()
{
boundaries
} else {
return Ok(None);
};
let end = if let Ok(x) = node.outputs[0].fact.shape[axis].to_usize() {
x
} else {
return Ok(None);
};
boundaries.push(end);
boundaries.retain(|x| *x > 0);
boundaries.sort();
boundaries.dedup();
Ok(Some(boundaries))
}
pub fn rewire_sliced_outputs(
model: &TypedModel,
node: &TypedNode,
axis: usize,
patch: &mut TypedModelPatch,
boundaries: &[usize],
splits: &[OutletId],
) -> TractResult<()> {
let full = patch.wire_node(
format!("{}.concat-{}", node.name, axis),
crate::ops::array::TypedConcat::new(axis),
splits,
)?[0];
patch.shunt_outside(model, node.id.into(), full)?;
for (ix, succ) in node.outputs[0].successors.iter().enumerate() {
if let Some(slice) =
model.node(succ.node).op_as::<Slice>().filter(|slice| slice.axis == axis)
{
let slices: TVec<OutletId> = boundaries
.iter()
.zip(splits.iter())
.filter_map(|(up, split)| {
if *up > slice.start.to_usize().unwrap() && *up <= slice.end.to_usize().unwrap()
{
Some(*split)
} else {
None
}
})
.collect();
let wire = if slices.len() > 1 {
patch.wire_node(
format!("{}.concat-m{}..{}..{}", node.name, ix, slice.start, slice.end),
crate::ops::array::TypedConcat::new(axis),
&slices,
)?[0]
} else {
slices[0]
};
patch.shunt_outside(model, succ.node.into(), wire)?;
}
}
Ok(())
}