forked from embedded-dev-research/ITLabAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_transformations.cpp
More file actions
381 lines (365 loc) · 13.8 KB
/
Copy pathgraph_transformations.cpp
File metadata and controls
381 lines (365 loc) · 13.8 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "graph_transformations/graph_transformations.hpp"
namespace it_lab_ai {
namespace {
bool layer_conditions(const std::shared_ptr<Layer>& layer,
const std::shared_ptr<Layer>& layer_sub) {
return layer->getName() == layer_sub->getName();
}
} // namespace
std::vector<std::vector<int>> find_subgraphs(const Graph& graph,
const Graph& subgraph) {
// requirements for subgraph:
// one or multiple inputs, one or multiple outputs
// requirements for graph:
// can't be connected from outside, except IO for input and O for output
std::vector<int> assignments; // cur assumption for graph
std::vector<std::vector<int>> results;
run_search(graph, subgraph, assignments, results);
return results;
}
bool has_edge(const Graph& graph, int id_from, int id_to) {
std::vector<int> outs = graph.getOutLayers(id_from);
auto it = std::find(outs.begin(), outs.end(), id_to);
return it != outs.end();
}
bool is_root(const Graph& graph, int id) {
return graph.getInputsSize(id) == 0;
}
bool is_leaf(const Graph& graph, int id) {
return graph.getOutputsSize(id) == 0;
}
bool run_search(const Graph& graph, const Graph& subgraph,
std::vector<int>& assignments,
std::vector<std::vector<int>>& results) {
size_t cur_size = assignments.size();
for (int prev_id = 0; prev_id < static_cast<int>(cur_size); prev_id++) {
size_t amount_connected_s = subgraph.getOutputsSize(prev_id);
for (size_t j = 0; j < amount_connected_s; j++) {
int next_id = subgraph.getOutLayers(prev_id)[j];
if (next_id < static_cast<int>(cur_size)) {
if (!has_edge(graph, assignments[prev_id], assignments[next_id])) {
return false;
}
std::vector<int> ids = {prev_id, next_id};
for (int k = 0; k < 2; k++) {
if (!layer_conditions(subgraph.getLayerFromID(ids[k]),
graph.getLayerFromID(assignments[ids[k]]))) {
return false;
}
// input node shouldn't be checked for it's inputs
if (!is_root(subgraph, ids[k]) &&
subgraph.getInputsSize(ids[k]) !=
graph.getInputsSize(assignments[ids[k]])) {
return false;
}
// input & output node shouldn't be checked for it's outputs
if (!is_leaf(subgraph, ids[k]) && !is_root(subgraph, ids[k])) {
size_t amount_connected_s1 = subgraph.getOutputsSize(ids[k]);
size_t amount_connected_1 =
graph.getOutputsSize(assignments[ids[k]]);
if (amount_connected_1 != amount_connected_s1) {
return false;
}
}
}
}
}
}
// assumption is good -> return true
if (static_cast<int>(cur_size) == subgraph.getLayersCount()) {
// special root->root case
std::vector<int> roots;
for (int v = 0; v < subgraph.getLayersCount(); v++) {
if (is_root(subgraph, v)) {
roots.push_back(assignments[v]);
}
}
for (int root : roots) {
std::vector<int> outs = graph.getOutLayers(root);
for (int out : outs) {
auto it = std::find(roots.begin(), roots.end(), out);
if (it != roots.end()) {
return false;
}
}
}
//
return true;
}
// add new nodes for assumption and try recursion
for (int id = 0; id < graph.getLayersCount(); id++) {
auto it = std::find(assignments.begin(), assignments.end(), id);
if (it == assignments.end()) {
assignments.push_back(id);
if (run_search(graph, subgraph, assignments, results)) {
results.emplace_back(assignments);
}
assignments.pop_back();
}
}
return false;
}
void change_ids(std::vector<std::vector<int>>& vec, int id) {
for (auto& i : vec) {
std::transform(i.begin(), i.end(), i.begin(),
[&](int elem) { return elem > id ? elem - 1 : elem; });
}
}
bool does_intersect(const std::vector<int>& vec1,
const std::vector<int>& vec2) {
// exists elem in vec1 which is found in vec2
return std::any_of(vec1.begin(), vec1.end(), [&](int elem) {
return std::find(vec2.begin(), vec2.end(), elem) != vec2.end();
});
}
void changed_subgraphs(const Graph& graph, const Graph& subgraph_from,
const std::shared_ptr<Layer>& layer_to, Graph& new_graph,
Tensor& out, const RuntimeOptions& options) {
graph.clone(new_graph, out, options);
std::vector<std::vector<int>> subs = find_subgraphs(graph, subgraph_from);
std::vector<std::vector<int>> subs_c = subs;
std::vector<bool> sub_used(subs.size(), true);
std::vector<int> roots;
std::vector<int> leaves;
std::vector<int> roots_inps_final;
std::vector<int> leaves_outs_final;
for (int v = 0; v < subgraph_from.getLayersCount(); v++) {
if (is_root(subgraph_from, v)) {
roots.push_back(v);
}
if (is_leaf(subgraph_from, v)) {
leaves.push_back(v);
}
}
for (size_t i = 0; i < subs.size(); i++) {
bool flag = false;
// don't change already changed subgraph
for (size_t j = 0; j < i; j++) {
if (sub_used[j] && does_intersect(subs_c[j], subs_c[i])) {
flag = true;
break;
}
}
if (flag) {
sub_used[i] = false;
continue;
}
std::shared_ptr<Layer> layer = layer_based_shared_copy(layer_to, options);
std::vector<bool> is_root_special(roots.size(), false);
roots_inps_final.clear();
leaves_outs_final.clear();
for (size_t j = 0; j < roots.size(); j++) {
std::vector<int> root_inps = new_graph.getInLayers(subs[i][roots[j]]);
// want subgraph -> single node
for (int root_inp : root_inps) {
auto it = std::find(roots_inps_final.begin(), roots_inps_final.end(),
root_inp);
if (it == roots_inps_final.end()) {
roots_inps_final.push_back(root_inp);
}
}
// recognize transformations we can apply with roots
const size_t amount_connected =
new_graph.getOutputsSize(subs[i][roots[j]]);
const size_t amount_connected_s = subgraph_from.getOutputsSize(roots[j]);
if (amount_connected == amount_connected_s) {
continue;
}
for (size_t k = 0; k < amount_connected; k++) {
int id = new_graph.getOutLayers(subs[i][roots[j]])[k];
auto it = std::find(subs[i].begin(), subs[i].end(), id);
if (it == subs[i].end()) {
is_root_special[j] = true;
}
}
}
for (int leaf : leaves) {
const size_t amount_connected = new_graph.getOutputsSize(subs[i][leaf]);
for (size_t k = 0; k < amount_connected; k++) {
int id = new_graph.getOutLayers(subs[i][leaf])[k];
auto it =
std::find(leaves_outs_final.begin(), leaves_outs_final.end(), id);
if (it == leaves_outs_final.end()) {
leaves_outs_final.push_back(id);
}
}
}
for (size_t j = 0; j < subs[i].size(); j++) {
auto it = std::find(roots.begin(), roots.end(), j);
size_t index_for_root = std::distance(roots.begin(), it);
// remove all nodes that isn't special roots
if (it == roots.end() ||
(it != roots.end() && !is_root_special[index_for_root])) {
new_graph.removeSingleLayer(subs[i][j]);
change_ids(subs, subs[i][j]);
std::transform(roots_inps_final.begin(), roots_inps_final.end(),
roots_inps_final.begin(), [&](int elem) {
return elem > subs[i][j] ? elem - 1 : elem;
});
std::transform(leaves_outs_final.begin(), leaves_outs_final.end(),
leaves_outs_final.begin(), [&](int elem) {
return elem > subs[i][j] ? elem - 1 : elem;
});
}
}
for (int j : roots_inps_final) {
new_graph.makeConnection(new_graph.getLayerFromID(j), layer);
}
if (roots_inps_final.empty()) {
new_graph.addSingleLayer(layer);
}
for (int j : leaves_outs_final) {
new_graph.makeConnection(layer, new_graph.getLayerFromID(j));
}
}
}
void changed_subgraphs(const Graph& graph, const Graph& subgraph_from,
const Graph& subgraph_to, Graph& new_graph, Tensor& out,
const RuntimeOptions& options, IOOrder order) {
graph.clone(new_graph, out, options);
std::vector<std::vector<int>> subs = find_subgraphs(graph, subgraph_from);
std::vector<std::vector<int>> subs_c = subs;
std::vector<bool> sub_used(subs.size(), true);
std::vector<int> roots_from;
std::vector<int> leaves_from;
std::vector<int> roots_to;
std::vector<int> leaves_to;
std::vector<std::vector<int>> roots_inps_final;
std::vector<std::vector<int>> leaves_outs_final;
for (int v = 0; v < subgraph_from.getLayersCount(); v++) {
if (is_root(subgraph_from, v)) {
roots_from.push_back(v);
}
if (is_leaf(subgraph_from, v)) {
leaves_from.push_back(v);
}
}
for (int v = 0; v < subgraph_to.getLayersCount(); v++) {
if (is_root(subgraph_to, v)) {
roots_to.push_back(v);
}
if (is_leaf(subgraph_to, v)) {
leaves_to.push_back(v);
}
}
if (roots_to.size() != roots_from.size()) {
throw std::invalid_argument(
"Subgraph_to and Subgraph_from roots amounts aren't same.");
}
if (leaves_to.size() != leaves_from.size()) {
throw std::invalid_argument(
"Subgraph_to and Subgraph_from leaves amounts aren't same.");
}
order.fill_empty(roots_from.size(), leaves_from.size());
if (order.in_order.size() != roots_from.size()) {
throw std::invalid_argument("Order for roots isn't complete");
}
if (order.out_order.size() != leaves_from.size()) {
throw std::invalid_argument("Order for leaves isn't complete");
}
for (size_t i = 0; i < subs.size(); i++) {
bool flag = false;
// don't change already changed subgraph
for (size_t j = 0; j < i; j++) {
if (sub_used[j] && does_intersect(subs_c[j], subs_c[i])) {
flag = true;
break;
}
}
if (flag) {
sub_used[i] = false;
continue;
}
std::vector<bool> is_root_special(roots_from.size(), false);
roots_inps_final =
std::vector<std::vector<int>>(roots_from.size(), std::vector<int>());
leaves_outs_final =
std::vector<std::vector<int>>(leaves_from.size(), std::vector<int>());
for (size_t j = 0; j < roots_from.size(); j++) {
roots_inps_final[j] = new_graph.getInLayers(subs[i][roots_from[j]]);
// recognize transformations we can apply with roots
const size_t amount_connected =
new_graph.getOutputsSize(subs[i][roots_from[j]]);
const size_t amount_connected_s =
subgraph_from.getOutputsSize(roots_from[j]);
if (amount_connected == amount_connected_s) {
continue;
}
for (size_t k = 0; k < amount_connected; k++) {
int id = new_graph.getOutLayers(subs[i][roots_from[j]])[k];
auto it = std::find(subs[i].begin(), subs[i].end(), id);
if (it == subs[i].end()) {
is_root_special[j] = true;
}
}
}
for (size_t j = 0; j < leaves_from.size(); j++) {
const size_t amount_connected =
new_graph.getOutputsSize(subs[i][leaves_from[j]]);
for (size_t k = 0; k < amount_connected; k++) {
int id = new_graph.getOutLayers(subs[i][leaves_from[j]])[k];
leaves_outs_final[j].push_back(id);
}
}
for (size_t j = 0; j < subs[i].size(); j++) {
auto it = std::find(roots_from.begin(), roots_from.end(), j);
size_t index_for_root = std::distance(roots_from.begin(), it);
// remove all nodes that isn't special roots
if (it == roots_from.end() ||
(it != roots_from.end() && !is_root_special[index_for_root])) {
new_graph.removeSingleLayer(subs[i][j]);
change_ids(subs, subs[i][j]);
for (auto& k : roots_inps_final) {
std::transform(k.begin(), k.end(), k.begin(), [&](int elem) {
return elem > subs[i][j] ? elem - 1 : elem;
});
}
for (auto& k : leaves_outs_final) {
std::transform(k.begin(), k.end(), k.begin(), [&](int elem) {
return elem > subs[i][j] ? elem - 1 : elem;
});
}
}
}
std::vector<int> roots_to_c = roots_to;
std::vector<int> leaves_to_c = leaves_to;
std::vector<std::shared_ptr<Layer>> layers;
for (int j = 0; j < subgraph_to.getLayersCount(); j++) {
std::shared_ptr<Layer> layer =
layer_based_shared_copy(subgraph_to.getLayerFromID(j), options);
layers.push_back(layer);
new_graph.addSingleLayer(layer);
auto it = std::find(roots_to_c.begin(), roots_to_c.end(), j);
if (it != roots_to_c.end()) {
size_t index_for_root = std::distance(roots_to_c.begin(), it);
roots_to[index_for_root] = layer->getID();
}
it = std::find(leaves_to_c.begin(), leaves_to_c.end(), j);
if (it != leaves_to_c.end()) {
size_t index_for_leaf = std::distance(leaves_to_c.begin(), it);
leaves_to[index_for_leaf] = layer->getID();
}
}
for (int j = 0; j < subgraph_to.getLayersCount(); j++) {
std::vector<int> cur_outs = subgraph_to.getOutLayers(j);
for (int cur_out : cur_outs) {
new_graph.makeConnection(layers[j], layers[cur_out]);
}
}
for (size_t j = 0; j < roots_inps_final.size(); j++) {
for (size_t k = 0; k < roots_inps_final[j].size(); k++) {
new_graph.makeConnection(
new_graph.getLayerFromID(roots_inps_final[j][k]),
new_graph.getLayerFromID(roots_to[order.in_order[j]]));
}
}
for (size_t j = 0; j < leaves_outs_final.size(); j++) {
for (size_t k = 0; k < leaves_outs_final[j].size(); k++) {
new_graph.makeConnection(
new_graph.getLayerFromID(leaves_to[order.out_order[j]]),
new_graph.getLayerFromID(leaves_outs_final[j][k]));
}
}
}
}
} // namespace it_lab_ai