tract_core/axes/
model.rs

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::internal::*;

#[derive(Debug, Clone, Default)]
pub struct OutletMap<T>(Vec<TVec<Option<T>>>);

impl<T: Clone> OutletMap<T> {
    fn insert(&mut self, outlet: OutletId, t: T) {
        if outlet.node >= self.0.len() {
            self.0.resize_with(outlet.node + 1, || tvec!());
        }
        let node = &mut self.0[outlet.node];
        if outlet.slot >= node.len() {
            node.resize(outlet.slot + 1, None);
        }
        node[outlet.slot] = Some(t)
    }
}

impl<T> OutletMap<T> {
    fn remove(&mut self, outlet: &OutletId) -> Option<T> {
        if let Some(node) = self.0.get_mut(outlet.node) {
            if let Some(slot) = node.get_mut(outlet.slot) {
                return slot.take();
            }
        }
        None
    }

    pub fn get(&self, outlet: &OutletId) -> Option<&T> {
        if let Some(node) = self.0.get(outlet.node) {
            if let Some(slot) = node.get(outlet.slot) {
                return slot.as_ref();
            }
        }
        None
    }

    pub fn keys(&self) -> OutletMapKeysIter<T> {
        OutletMapKeysIter(self, (0, 0).into())
    }
}

impl<'a, T: Clone> std::ops::Index<&'a OutletId> for OutletMap<T> {
    type Output = T;
    fn index(&self, index: &'a OutletId) -> &Self::Output {
        self.get(index).unwrap()
    }
}

pub struct OutletMapKeysIter<'a, T>(&'a OutletMap<T>, OutletId);

impl<T> std::iter::Iterator for OutletMapKeysIter<'_, T> {
    type Item = OutletId;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.1.node >= (self.0).0.len() {
                return None;
            }
            if self.1.slot >= (self.0).0[self.1.node].len() {
                self.1.slot = 0;
                self.1.node += 1;
                continue;
            }
            let current = self.1;
            self.1.slot += 1;
            if self.0.get(&current).is_some() {
                return Some(current);
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct AxisTracking {
    pub creators: TVec<OutletId>,
    pub destructors: TVec<InletId>,
    pub outlets: OutletMap<usize>,
}

impl AxisTracking {
    pub fn for_outlet_and_axis(
        model: &TypedModel,
        outlet: OutletId,
        axis: usize,
    ) -> TractResult<Option<AxisTracking>> {
        let mut mapped_outlets = OutletMap::default();
        let mut todo = OutletMap::default();
        let mut creators = tvec!();
        let mut destructors = tvec!();
        mapped_outlets.insert(outlet, axis);
        todo.insert(outlet, ());
        while let Some(wire) = todo.keys().next() {
            todo.remove(&wire);
            let axis = mapped_outlets[&wire];
            let emiter_node = model.node(wire.node);
            let mut nodes = vec![];
            let (input_facts, output_facts) = model.node_facts(emiter_node.id)?;
            let map = emiter_node
                .op
                .axes_mapping(&input_facts, &output_facts)
                .with_context(|| format!("Computing axes mapping for {emiter_node}"))?;
            let info = map.axis((InOut::Out(wire.slot), axis)).with_context(|| {
                format!(
                    "Axes mapping for {} is {map}, need output axis {:?} from slot {}",
                    emiter_node, axis, wire.slot,
                )
            })?;

            if info.inputs.iter().any(|i| i.len() > 0) {
                nodes.push((wire.node, info.clone()));
            } else {
                creators.push(wire);
            };
            for succ in &emiter_node.outputs[wire.slot].successors {
                let succ_node = model.node(succ.node);
                let (input_facts, output_facts) = model.node_facts(succ_node.id)?;
                let map = succ_node.op.axes_mapping(&input_facts, &output_facts)?;
                let info = map.axis((InOut::In(succ.slot), axis)).with_context(|| {
                    format!(
                        "Axes mapping for {succ_node} is {map}, need input axis {:?} from slot {}",
                        axis, succ.slot,
                    )
                })?;
                if info.outputs.iter().any(|o| o.len() > 0) {
                    nodes.push((succ_node.id, info.clone()));
                } else {
                    destructors.push(*succ);
                };
            }
            let mut new_outlets = vec![];
            for (n, axes) in nodes {
                let node = model.node(n);
                for slot in 0..node.outputs.len() {
                    if let &[axis] = &*axes.outputs[slot] {
                        new_outlets.push((OutletId::new(n, slot), axis));
                    }
                }
                for slot in 0..node.inputs.len() {
                    if let &[axis] = &*axes.inputs[slot] {
                        new_outlets.push((node.inputs[slot], axis));
                    }
                }
            }
            for (outlet, axis) in new_outlets {
                if let Some(prev) = mapped_outlets.get(&outlet) {
                    if *prev != axis {
                        return Ok(None);
                    }
                } else {
                    mapped_outlets.insert(outlet, axis);
                    todo.insert(outlet, ());
                }
            }
        }
        Ok(Some(AxisTracking { creators, destructors, outlets: mapped_outlets }))
    }
}

pub fn full_axis_tracking(model: &TypedModel) -> TractResult<Vec<AxisTracking>> {
    let mut axes: Vec<AxisTracking> = vec![];
    for node in model.eval_order()? {
        for slot in 0..model.node(node).outputs.len() {
            let outlet = OutletId::new(node, slot);
            let input_fact = model.outlet_fact(outlet)?;
            'axis: for axis in 0..input_fact.rank() {
                if axes.iter().any(|tracking| tracking.outlets.get(&outlet) == Some(&axis)) {
                    continue 'axis;
                }
                if let Some(tracker) = AxisTracking::for_outlet_and_axis(model, outlet, axis)? {
                    axes.push(tracker);
                }
            }
        }
    }
    Ok(axes)
}

pub fn for_model(model: &TypedModel) -> TractResult<AxesMapping> {
    let input_ranks = model
        .input_outlets()?
        .iter()
        .map(|io| model.outlet_fact(*io).map(|f| f.rank()))
        .collect::<TractResult<TVec<usize>>>()?;
    let output_ranks = model
        .output_outlets()?
        .iter()
        .map(|io| model.outlet_fact(*io).map(|f| f.rank()))
        .collect::<TractResult<TVec<usize>>>()?;
    let mut result = AxesMapping::disconnected_for_ranks(&input_ranks, &output_ranks)?;
    for tracking in full_axis_tracking(model)? {
        let mut reprs: Vec<char> = vec![];
        for (ix, outlet) in model.input_outlets()?.iter().enumerate() {
            if let Some(appearance) = tracking.outlets.get(outlet) {
                reprs.push(result.axis((InOut::In(ix), *appearance)).unwrap().repr);
            }
        }
        for (ix, outlet) in model.output_outlets()?.iter().enumerate() {
            if let Some(appearance) = tracking.outlets.get(outlet) {
                reprs.push(result.axis((InOut::Out(ix), *appearance)).unwrap().repr);
            }
        }
        if reprs.len() > 1 {
            for other in &reprs[1..] {
                result = result.linking(reprs[0], *other)?;
            }
        }
    }
    result.relabel()
}