Skip to content
This repository was archived by the owner on Jul 22, 2026. It is now read-only.

Commit d4d7bd0

Browse files
author
NeiroYT
committed
Tests
1 parent e27d175 commit d4d7bd0

3 files changed

Lines changed: 310 additions & 43 deletions

File tree

app/Graph/onnx_subgraphs.cpp

Lines changed: 112 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,83 @@ void alexnet_inf_careless(Graph& graph, const RuntimeOptions& options,
2626
graph.setInput(graph.getLayerFromID(0), *i);
2727
}
2828

29-
void alexnet_comparison() {
30-
std::vector<size_t> counts = {979, 1134, 1031, 1009, 981,
31-
891, 957, 1027, 973, 1008};
32-
size_t sum = std::accumulate(counts.begin(), counts.end(), size_t{0});
33-
int count_pic = static_cast<int>(sum) + 10;
34-
std::vector<float> res(count_pic * 28 * 28, 1.0F);
35-
Tensor input;
36-
Shape sh1({1, 5, 5, 3});
37-
std::vector<float> vec;
38-
vec.reserve(75);
39-
for (int i = 0; i < 75; ++i) {
40-
vec.push_back(3);
41-
}
42-
Tensor output = make_tensor(vec, sh1);
29+
void alexnet_comparison(int type = 3) {
30+
if (type == 2) {
31+
Tensor input = make_tensor(std::vector<float>(3 * 224 * 224, 200.0F),
32+
Shape({1, 3, 224, 224}));
33+
Tensor output = make_tensor(std::vector<int>({0}));
34+
Tensor input_c = input;
35+
Tensor output_c = make_tensor(std::vector<int>({0}));
36+
RuntimeOptions options;
37+
Graph graph;
38+
Graph graph2;
39+
build_graph(graph, input, output, MODEL_PATH_GOOGLENET_ONNX, options,
40+
true);
41+
Graph subgraph;
42+
std::shared_ptr<Layer> layer_0 = std::make_shared<ConvolutionalLayer>();
43+
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");
44+
subgraph.setInput(layer_0, input);
45+
subgraph.makeConnection(layer_0, layer_1);
46+
std::shared_ptr<Layer> layer_to = std::make_shared<ConvReluLayer>(
47+
std::dynamic_pointer_cast<ConvolutionalLayer>(layer_0));
48+
changed_subgraphs(graph, subgraph, layer_to, graph2, input, options);
49+
auto time1 = elapsed_time_avg<double, std::milli>(
50+
10, alexnet_inf_careless, graph, options, input_c, output_c);
51+
print_time_stats(graph);
52+
auto time2 = elapsed_time_avg<double, std::milli>(
53+
10, alexnet_inf_careless, graph2, options, input_c, output_c);
54+
print_time_stats(graph2);
55+
std::cout << time1 << " for unchanged graph\n";
56+
std::cout << time2 << " for convrelu graph\n";
57+
} else if (type == 3) {
58+
std::vector<size_t> counts = {979, 1134, 1031, 1009, 981,
59+
891, 957, 1027, 973, 1008};
60+
size_t sum = std::accumulate(counts.begin(), counts.end(), size_t{0});
61+
int count_pic = static_cast<int>(sum) + 10;
62+
std::vector<float> res(count_pic * 28 * 28, 1.0F);
63+
Tensor input;
64+
Shape sh1({1, 5, 5, 3});
65+
std::vector<float> vec;
66+
vec.reserve(75);
67+
for (int i = 0; i < 75; ++i) {
68+
vec.push_back(3);
69+
}
70+
Tensor output = make_tensor(vec, sh1);
4371

44-
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
45-
Tensor t = make_tensor<float>(res, sh);
46-
input = t;
72+
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
73+
Tensor t = make_tensor<float>(res, sh);
74+
input = t;
4775

48-
RuntimeOptions options;
49-
Graph graph;
50-
Graph graph2;
51-
build_graph_linear(graph, input, output, options, true, false);
52-
Graph subgraph;
53-
std::shared_ptr<Layer> layer_0 = std::make_shared<ConvolutionalLayer>();
54-
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");
55-
subgraph.setInput(layer_0, input);
56-
subgraph.makeConnection(layer_0, layer_1);
57-
std::shared_ptr<Layer> layer_to = std::make_shared<ConvReluLayer>(
58-
std::dynamic_pointer_cast<ConvolutionalLayer>(layer_0));
59-
changed_subgraphs(graph, subgraph, layer_to, graph2, input, options);
60-
Tensor input_c = input;
61-
Tensor output_c = output;
62-
auto time1 = elapsed_time_avg<double, std::milli>(
63-
4, alexnet_inf_careless, graph, options, input_c, output_c);
64-
print_time_stats(graph);
65-
auto time2 = elapsed_time_avg<double, std::milli>(
66-
4, alexnet_inf_careless, graph2, options, input_c, output_c);
67-
print_time_stats(graph2);
68-
std::cout << time1 << " for unchanged graph\n";
69-
std::cout << time2 << " for convrelu graph\n";
76+
RuntimeOptions options;
77+
Graph graph;
78+
Graph graph2;
79+
build_graph_linear(graph, input, output, options, true, false);
80+
Graph subgraph;
81+
std::shared_ptr<Layer> layer_0 = std::make_shared<ConvolutionalLayer>();
82+
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");
83+
subgraph.setInput(layer_0, input);
84+
subgraph.makeConnection(layer_0, layer_1);
85+
std::shared_ptr<Layer> layer_to = std::make_shared<ConvReluLayer>(
86+
std::dynamic_pointer_cast<ConvolutionalLayer>(layer_0));
87+
changed_subgraphs(graph, subgraph, layer_to, graph2, input, options);
88+
Tensor input_c = input;
89+
Tensor output_c = output;
90+
auto time1 = elapsed_time_avg<double, std::milli>(
91+
2, alexnet_inf_careless, graph, options, input_c, output_c);
92+
print_time_stats(graph);
93+
auto time2 = elapsed_time_avg<double, std::milli>(
94+
2, alexnet_inf_careless, graph2, options, input_c, output_c);
95+
print_time_stats(graph2);
96+
std::cout << time1 << " for unchanged graph\n";
97+
std::cout << time2 << " for convrelu graph\n";
98+
}
7099
}
71100

72-
int main() {
73-
int type = 2;
101+
int main(int argc, char* argv[]) {
102+
int type = (argc > 1) ? (int)(argv[1][0]-'0') : 0;
74103
Tensor input = make_tensor(std::vector<int>({0}));
75104
RuntimeOptions options;
76-
alexnet_comparison();
105+
//alexnet_comparison(type);
77106
if (type == 0) {
78107
Graph graph1;
79108
build_graph(graph1, input, input, MODEL_PATH_DENSENET_ONNX, options, false);
@@ -104,6 +133,27 @@ int main() {
104133

105134
auto vec = find_subgraphs(graph1, subgraph);
106135
auto vec2 = find_subgraphs(graph1, subgraph2);
136+
137+
auto time = elapsed_time_avg<double, std::milli>(10, find_subgraphs, graph1,
138+
subgraph);
139+
auto time2 = elapsed_time_avg<double, std::milli>(10, find_subgraphs,
140+
graph1, subgraph2);
141+
142+
for (auto& i : vec) {
143+
for (int j : i) {
144+
std::cerr << j << ' ';
145+
}
146+
std::cerr << '\n';
147+
}
148+
std::cerr << "Time for path5:" << time << '\n';
149+
150+
for (auto& i : vec2) {
151+
for (int j : i) {
152+
std::cerr << j << ' ';
153+
}
154+
std::cerr << '\n';
155+
}
156+
std::cerr << "Time for concat:" << time2 << '\n';
107157
} else if (type == 1) {
108158
Graph graph1;
109159
build_graph(graph1, input, input, MODEL_PATH_RESNET_ONNX, options, false);
@@ -121,6 +171,16 @@ int main() {
121171
subgraph.makeConnection(layer_3, layer_4);
122172

123173
auto vec = find_subgraphs(graph1, subgraph);
174+
175+
auto time = elapsed_time_avg<double, std::milli>(10, find_subgraphs, graph1,
176+
subgraph);
177+
for (auto& i : vec) {
178+
for (int j : i) {
179+
std::cerr << j << ' ';
180+
}
181+
std::cerr << '\n';
182+
}
183+
std::cerr << "Time for path5:" << time << '\n';
124184
} else if (type == 2) {
125185
Graph graph1;
126186
build_graph(graph1, input, input, MODEL_PATH_GOOGLENET_ONNX, options,
@@ -145,6 +205,16 @@ int main() {
145205
subgraph.makeConnection(layer_5, layer_3);
146206

147207
auto vec = find_subgraphs(graph1, subgraph);
208+
209+
auto time = elapsed_time_avg<double, std::milli>(10, find_subgraphs, graph1,
210+
subgraph);
211+
for (auto& i : vec) {
212+
for (int j : i) {
213+
std::cerr << j << ' ';
214+
}
215+
std::cerr << '\n';
216+
}
217+
std::cerr << "Time for concat:" << time << '\n';
148218
}
149219
return 0;
150220
}

include/graph_transformations/graph_transformations.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "layers/EWLayer.hpp"
77
#include "layers/Layer.hpp"
88
#include "layers_fused/ConvRelu.hpp"
9+
#include "perf/benchmarking.hpp"
910

1011
namespace it_lab_ai {
1112

@@ -29,6 +30,16 @@ std::vector<std::vector<int>> find_subgraphs(const Graph& graph,
2930
bool has_edge(const Graph& graph, int id_from, int id_to);
3031
bool is_root(const Graph& graph, int id);
3132
bool is_leaf(const Graph& graph, int id);
33+
void fill_possible_assignments(
34+
const Graph& graph, const Graph& subgraph,
35+
std::vector<std::vector<int>>& possible_assignments);
36+
bool update_refinement(const Graph& graph, const Graph& subgraph,
37+
std::vector<std::vector<int>>& possible_assignments);
38+
bool run_search_refined(const Graph& graph, const Graph& subgraph,
39+
std::vector<int>& assignments,
40+
std::vector<std::vector<int>>& results,
41+
std::vector<std::vector<int>>& possible_assignments);
42+
3243
bool run_search(const Graph& graph, const Graph& subgraph,
3344
std::vector<int>& assignments,
3445
std::vector<std::vector<int>>& results);

0 commit comments

Comments
 (0)