-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathonnx_subgraphs.cpp
More file actions
150 lines (136 loc) · 5.7 KB
/
Copy pathonnx_subgraphs.cpp
File metadata and controls
150 lines (136 loc) · 5.7 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
#include <algorithm>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <opencv2/opencv.hpp>
#include <sstream>
#include <unordered_map>
#include "build.hpp"
#include "graph_transformations/graph_transformations.hpp"
#include "layers_fused/ConvRelu.hpp"
#include "perf/benchmarking.hpp"
using namespace it_lab_ai;
void alexnet_inf_careless(Graph& graph, const RuntimeOptions& options,
Tensor& input, Tensor& output) {
auto* o = new Tensor(output);
auto* i = new Tensor(input);
graph.inference(options);
if (graph.getLayersCount() == 0) {
throw std::runtime_error("No layers");
}
graph.setOutput(graph.getLayerFromID(graph.getLayersCount() - 1), *o);
graph.setInput(graph.getLayerFromID(0), *i);
}
void alexnet_comparison() {
std::vector<size_t> counts = {979, 1134, 1031, 1009, 981,
891, 957, 1027, 973, 1008};
size_t sum = std::accumulate(counts.begin(), counts.end(), size_t{0});
int count_pic = static_cast<int>(sum) + 10;
std::vector<float> res(count_pic * 28 * 28, 1.0F);
Tensor input;
Shape sh1({1, 5, 5, 3});
std::vector<float> vec;
vec.reserve(75);
for (int i = 0; i < 75; ++i) {
vec.push_back(3);
}
Tensor output = make_tensor(vec, sh1);
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
Tensor t = make_tensor<float>(res, sh);
input = t;
RuntimeOptions options;
Graph graph;
Graph graph2;
build_graph_linear(graph, input, output, options, true, false);
Graph subgraph;
std::shared_ptr<Layer> layer_0 = std::make_shared<ConvolutionalLayer>();
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");
subgraph.setInput(layer_0, input);
subgraph.makeConnection(layer_0, layer_1);
std::shared_ptr<Layer> layer_to = std::make_shared<ConvReluLayer>(
std::dynamic_pointer_cast<ConvolutionalLayer>(layer_0));
changed_subgraphs(graph, subgraph, layer_to, graph2, input, options);
Tensor input_c = input;
Tensor output_c = output;
auto time1 = elapsed_time_avg<double, std::milli>(
4, alexnet_inf_careless, graph, options, input_c, output_c);
print_time_stats(graph);
auto time2 = elapsed_time_avg<double, std::milli>(
4, alexnet_inf_careless, graph2, options, input_c, output_c);
print_time_stats(graph2);
std::cout << time1 << " for unchanged graph\n";
std::cout << time2 << " for convrelu graph\n";
}
int main() {
int type = 2;
Tensor input = make_tensor(std::vector<int>({0}));
RuntimeOptions options;
alexnet_comparison();
if (type == 0) {
Graph graph1;
build_graph(graph1, input, input, MODEL_PATH_DENSENET_ONNX, options, false);
Graph subgraph;
Tensor scale = make_tensor(std::vector<float>({1.0F}));
std::shared_ptr<Layer> layer_0 =
std::make_shared<BatchNormalizationLayer>(scale, scale, scale, scale);
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");
std::shared_ptr<Layer> layer_2 = std::make_shared<ConvolutionalLayer>();
std::shared_ptr<Layer> layer_3 = std::make_shared<EWLayer>("relu");
std::shared_ptr<Layer> layer_4 = std::make_shared<ConvolutionalLayer>();
subgraph.setInput(layer_0, input);
subgraph.makeConnection(layer_0, layer_1);
subgraph.makeConnection(layer_1, layer_2);
subgraph.makeConnection(layer_2, layer_3);
subgraph.makeConnection(layer_3, layer_4);
Graph subgraph2;
std::shared_ptr<Layer> layer_5 = std::make_shared<ConcatLayer>();
std::shared_ptr<Layer> layer_6 =
std::make_shared<PoolingLayer>(Shape({1, 1, 1}), "max");
std::shared_ptr<Layer> layer_7 = std::make_shared<ConvolutionalLayer>();
subgraph2.setInput(layer_6, input);
subgraph2.makeConnection(layer_6, layer_5);
subgraph2.addSingleLayer(layer_7);
subgraph2.makeConnection(layer_7, layer_5);
auto vec = find_subgraphs(graph1, subgraph);
auto vec2 = find_subgraphs(graph1, subgraph2);
} else if (type == 1) {
Graph graph1;
build_graph(graph1, input, input, MODEL_PATH_RESNET_ONNX, options, false);
Graph subgraph;
std::shared_ptr<Layer> layer_0 = std::make_shared<TransposeLayer>();
std::shared_ptr<Layer> layer_1 = std::make_shared<SoftmaxLayer>();
std::shared_ptr<Layer> layer_2 = std::make_shared<ReshapeLayer>();
std::shared_ptr<Layer> layer_3 = std::make_shared<ReshapeLayer>();
std::shared_ptr<Layer> layer_4 = std::make_shared<ReshapeLayer>();
subgraph.setInput(layer_0, input);
subgraph.makeConnection(layer_0, layer_1);
subgraph.makeConnection(layer_1, layer_2);
subgraph.makeConnection(layer_2, layer_3);
subgraph.makeConnection(layer_3, layer_4);
auto vec = find_subgraphs(graph1, subgraph);
} else if (type == 2) {
Graph graph1;
build_graph(graph1, input, input, MODEL_PATH_GOOGLENET_ONNX, options,
false);
Graph subgraph;
Shape shape(2);
std::shared_ptr<Layer> layer_0 = std::make_shared<ConcatLayer>();
std::shared_ptr<Layer> layer_1 = std::make_shared<ConvolutionalLayer>();
std::shared_ptr<Layer> layer_2 = std::make_shared<EWLayer>("relu");
std::shared_ptr<Layer> layer_3 = std::make_shared<EWLayer>("relu");
std::shared_ptr<Layer> layer_4 = std::make_shared<ConvolutionalLayer>();
std::shared_ptr<Layer> layer_5 = std::make_shared<ConvolutionalLayer>();
std::shared_ptr<Layer> layer_6 =
std::make_shared<PoolingLayer>(shape, "max");
subgraph.setInput(layer_0, input);
subgraph.makeConnection(layer_0, layer_1);
subgraph.makeConnection(layer_0, layer_4);
subgraph.makeConnection(layer_0, layer_5);
subgraph.makeConnection(layer_0, layer_6);
subgraph.makeConnection(layer_4, layer_2);
subgraph.makeConnection(layer_5, layer_3);
auto vec = find_subgraphs(graph1, subgraph);
}
return 0;
}