Skip to content

Commit 4f33d75

Browse files
authored
Merge branch 'main' into Semyon1104/Acc_Parametrized
2 parents 1107f98 + dd595d7 commit 4f33d75

94 files changed

Lines changed: 2750 additions & 1024 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-format

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
Language: Cpp
2+
Standard: c++20
23
BasedOnStyle: Google
4+
UseTab: Never
5+
AllowShortFunctionsOnASingleLine: Empty
6+
IndentPPDirectives: AfterHash
7+
SortIncludes: true
8+
FixNamespaceComments: true
9+
InsertBraces: true
10+
QualifierAlignment: Left
11+
PointerAlignment: Right
12+
ReferenceAlignment: Right
13+
SortUsingDeclarations: LexicographicNumeric
14+
InsertNewlineAtEOF: true
15+
LambdaBodyIndentation: OuterScope
16+
MaxEmptyLinesToKeep: 1
17+
LineEnding: LF

.clang-tidy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
Checks: >
22
bugprone-*,
3+
google-*,
4+
cppcoreguidelines-*,
35
modernize-*,
46
misc-*,
57
performance-*,
68
portability-*,
79
readability-*,
10+
-google-build-using-namespace,
11+
-cppcoreguidelines-avoid-magic-numbers,
12+
-cppcoreguidelines-avoid-non-const-global-variables,
13+
-cppcoreguidelines-narrowing-conversions,
14+
-cppcoreguidelines-non-private-member-variables-in-classes,
15+
-cppcoreguidelines-prefer-member-initializer,
16+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
17+
-cppcoreguidelines-pro-type-const-cast,
18+
-cppcoreguidelines-pro-type-reinterpret-cast,
19+
-cppcoreguidelines-pro-type-member-init,
20+
-cppcoreguidelines-special-member-functions,
821
-google-readability-braces-around-statements,
922
-google-readability-namespace-comments,
1023
-google-runtime-references,

app/Converters/reader_weights_sample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ int main() {
2525
}
2626

2727
return 0;
28-
}
28+
}

app/Converters/reader_weights_sample_onnx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ int main() {
7474
}
7575

7676
return 0;
77-
}
77+
}

app/Graph/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ target_link_libraries(BuildGraph PUBLIC ${OpenCV_LIBS})
1111
target_link_libraries(BuildGraph PUBLIC reader_lib)
1212
target_link_libraries(BuildGraph PUBLIC TBB_unified)
1313
target_link_libraries(BuildGraph PUBLIC layers_lib)
14+
target_link_libraries(BuildGraph PUBLIC layers_fused_lib)
1415
target_link_libraries(BuildGraph PUBLIC layers_oneDNN_lib)
1516
target_link_libraries(BuildGraph PUBLIC gtest_main)
1617

@@ -25,6 +26,7 @@ target_link_libraries(ACC BuildGraph)
2526
add_executable(onnx_subgraphs onnx_subgraphs.cpp)
2627
target_link_libraries(onnx_subgraphs BuildGraph)
2728
target_link_libraries(onnx_subgraphs OpenMP::OpenMP_CXX)
29+
target_link_libraries(onnx_subgraphs ${OpenCV_LIBS})
2830
target_link_libraries(onnx_subgraphs graphT_lib)
2931

3032
file(DOWNLOAD

app/Graph/acc_check.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ int main(int argc, char* argv[]) {
120120
for (int i = 0; i < 28; ++i) {
121121
for (int j = 0; j < 28; ++j) {
122122
size_t a = ind;
123-
for (size_t n = 0; n < name; n++) a += counts[n] + 1;
123+
for (size_t n = 0; n < name; n++) {
124+
a += counts[n] + 1;
125+
}
124126
res[(a) * 28 * 28 + i * 28 + j] = channels[0].at<uchar>(j, i);
125127
}
126128
}
@@ -147,8 +149,12 @@ int main(int argc, char* argv[]) {
147149
for (size_t name = 0; name < 10; name++) {
148150
for (size_t ind = 0; ind < counts[name] + 1; ind++) {
149151
size_t a = ind;
150-
for (size_t n = 0; n < name; n++) a += counts[n] + 1;
151-
if (name == indices[a]) stat++;
152+
for (size_t n = 0; n < name; n++) {
153+
a += counts[n] + 1;
154+
}
155+
if (name == indices[a]) {
156+
stat++;
157+
}
152158
}
153159
}
154160
double percentage =
@@ -219,7 +225,9 @@ int main(int argc, char* argv[]) {
219225
folder_oss << std::setw(5) << std::setfill('0') << class_id;
220226
std::string class_folder_path = dataset_path + "/" + folder_oss.str();
221227

222-
if (!fs::exists(class_folder_path)) continue;
228+
if (!fs::exists(class_folder_path)) {
229+
continue;
230+
}
223231

224232
std::vector<std::string> class_images;
225233
for (const auto& entry : fs::directory_iterator(class_folder_path)) {
@@ -433,4 +441,4 @@ int main(int argc, char* argv[]) {
433441
<< final_accuracy_top5 << "%" << '\n';
434442

435443
return 0;
436-
}
444+
}

app/Graph/build.cpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ std::unordered_map<std::string, std::string> model_paths = {
1313

1414
void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
1515
it_lab_ai::Tensor& output, RuntimeOptions options,
16-
bool comments) {
16+
bool comments, bool enable_postops) {
1717
if (comments) {
1818
for (size_t i = 0; i < input.get_shape().dims(); i++) {
1919
std::cout << input.get_shape()[i] << ' ';
@@ -25,7 +25,9 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
2525
std::string json_file = MODEL_PATH_H5;
2626
it_lab_ai::json model_data = it_lab_ai::read_json(json_file);
2727

28-
if (comments) std::cout << "Loaded model data from JSON." << '\n';
28+
if (comments) {
29+
std::cout << "Loaded model data from JSON." << '\n';
30+
}
2931

3032
for (const auto& layer_data : model_data) {
3133
std::string layer_type = layer_data["type"];
@@ -70,12 +72,14 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
7072
options, 1, pads, 1, tmp_values, tmp_bias, 1, true);
7173
layers.push_back(conv_layer);
7274
layerpostop.push_back(false);
73-
if (comments) std::cout << "ConvLayer added to layers." << '\n';
75+
if (comments) {
76+
std::cout << "ConvLayer added to layers." << '\n';
77+
}
7478
}
7579
if (layer_type.find("relu") != std::string::npos) {
7680
auto ew_layer = LayerFactory::createEwLayer("relu", options);
7781
layers.push_back(ew_layer);
78-
layerpostop.push_back(true);
82+
layerpostop.push_back(enable_postops);
7983
if (comments) {
8084
std::cout << "Element wise (relu) added to layers" << '\n';
8185
}
@@ -85,7 +89,9 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
8589
auto fc_layer = std::make_shared<it_lab_ai::FCLayer>(tensor, tmp_bias);
8690
layers.push_back(fc_layer);
8791
layerpostop.push_back(false);
88-
if (comments) std::cout << "DenseLayer added to layers." << '\n';
92+
if (comments) {
93+
std::cout << "DenseLayer added to layers." << '\n';
94+
}
8995
}
9096

9197
if (layer_type.find("Pool") != std::string::npos) {
@@ -105,15 +111,19 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
105111

106112
layers.push_back(pool_layer);
107113
layerpostop.push_back(false);
108-
if (comments) std::cout << "PoolingLayer added to layers." << '\n';
114+
if (comments) {
115+
std::cout << "PoolingLayer added to layers." << '\n';
116+
}
109117
}
110118

111119
if (layer_type.find("Flatten") != std::string::npos) {
112120
auto flatten_layer = std::make_shared<it_lab_ai::FlattenLayer>(
113121
std::vector<size_t>({0, 3, 2, 1}));
114122
layers.push_back(flatten_layer);
115123
layerpostop.push_back(false);
116-
if (comments) std::cout << "FlattenLayer added to layers." << '\n';
124+
if (comments) {
125+
std::cout << "FlattenLayer added to layers." << '\n';
126+
}
117127
}
118128

119129
if (layer_type.find("Dropout") != std::string::npos) {
@@ -128,26 +138,34 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
128138
}
129139
}
130140
}
131-
if (comments) std::cout << "number of layers - " << layers.size() + 1 << '\n';
141+
if (comments) {
142+
std::cout << "number of layers - " << layers.size() + 1 << '\n';
143+
}
132144
auto a1 = std::make_shared<it_lab_ai::InputLayer>(it_lab_ai::kNchw,
133145
it_lab_ai::kNchw);
134146

135-
if (comments) std::cout << "InputLayer created." << '\n';
147+
if (comments) {
148+
std::cout << "InputLayer created." << '\n';
149+
}
136150

137151
graph.setInput(a1, input);
138-
if (comments) std::cout << "Input set in graph." << '\n';
152+
if (comments) {
153+
std::cout << "Input set in graph." << '\n';
154+
}
139155

140156
graph.makeConnection(a1, layers[0]);
141-
if (comments)
157+
if (comments) {
142158
std::cout << "Connection made between InputLayer and first layer." << '\n';
159+
}
143160

144161
for (size_t i = 0; i < layers.size() - 1; ++i) {
145162
if (layerpostop[i]) {
146163
layers[i - 1]->postops.layers.push_back(layers[i]);
147164
layers[i - 1]->postops.count++;
148165
graph.makeConnection(layers[i - 1], layers[i + 1]);
149-
} else if (!layerpostop[i + 1])
166+
} else if (!layerpostop[i + 1]) {
150167
graph.makeConnection(layers[i], layers[i + 1]);
168+
}
151169
}
152170

153171
graph.setOutput(layers.back(), output);
@@ -197,13 +215,12 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
197215
try {
198216
std::sort(connection_list.begin(), connection_list.end(),
199217
[&](const auto& a, const auto& b) {
200-
if (!name_to_layer.contains(a.first) ||
201-
!name_to_layer.contains(b.first)) {
202-
return false;
203-
}
204-
return name_to_layer[a.first]->getID() <
205-
name_to_layer[b.first]->getID();
206-
});
218+
if (!name_to_layer.contains(a.first) ||
219+
!name_to_layer.contains(b.first)) {
220+
return false;
221+
}
222+
return name_to_layer[a.first]->getID() < name_to_layer[b.first]->getID();
223+
});
207224

208225
} catch (const std::exception& e) {
209226
std::cerr << "ERROR during sorting: " << e.what() << '\n';
@@ -296,7 +313,9 @@ ParseResult parse_json_model(RuntimeOptions options,
296313
}
297314
}
298315

299-
if (comments) std::cout << "Loaded model data from JSON." << '\n';
316+
if (comments) {
317+
std::cout << "Loaded model data from JSON." << '\n';
318+
}
300319

301320
auto input_layer = std::make_shared<it_lab_ai::InputLayer>(it_lab_ai::kNchw,
302321
it_lab_ai::kNchw);
@@ -309,7 +328,9 @@ ParseResult parse_json_model(RuntimeOptions options,
309328
try {
310329
std::string layer_type = layer_data["type"];
311330

312-
if (layer_type == "InputLayer") continue;
331+
if (layer_type == "InputLayer") {
332+
continue;
333+
}
313334
std::string layer_name = layer_data["name"];
314335
int layer_index = layer_data["index"];
315336
if (comments) {
@@ -703,7 +724,9 @@ ParseResult parse_json_model(RuntimeOptions options,
703724
std::cout << "TransposeLayer added with perm: [";
704725
for (size_t i = 0; i < perm.size(); ++i) {
705726
std::cout << perm[i];
706-
if (i < perm.size() - 1) std::cout << ", ";
727+
if (i < perm.size() - 1) {
728+
std::cout << ", ";
729+
}
707730
}
708731
std::cout << "]" << '\n';
709732
}

app/Graph/build.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
6161
it_lab_ai::RuntimeOptions options, bool comments);
6262
void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input,
6363
it_lab_ai::Tensor& output,
64-
it_lab_ai::RuntimeOptions options, bool comments);
64+
it_lab_ai::RuntimeOptions options, bool comments,
65+
bool enable_postops = true);
6566
std::unordered_map<int, std::string> load_class_names(
6667
const std::string& filename);
6768

app/Graph/graph_build.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,4 @@ int main(int argc, char* argv[]) {
181181
}
182182
}
183183
return 0;
184-
}
184+
}

app/Graph/onnx_subgraphs.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,83 @@
11
#include <algorithm>
22
#include <filesystem>
33
#include <iomanip>
4+
#include <iostream>
45
#include <numeric>
6+
#include <opencv2/opencv.hpp>
57
#include <sstream>
68
#include <unordered_map>
79

810
#include "build.hpp"
911
#include "graph_transformations/graph_transformations.hpp"
12+
#include "layers_fused/ConvRelu.hpp"
1013
#include "perf/benchmarking.hpp"
1114

1215
using namespace it_lab_ai;
1316

17+
void alexnet_inf_careless(Graph& graph, const RuntimeOptions& options,
18+
Tensor& input, Tensor& output) {
19+
if (graph.getLayersCount() == 0) {
20+
throw std::runtime_error("No layers");
21+
}
22+
graph.setInput(graph.getLayerFromID(0), input);
23+
graph.setOutput(graph.getLayerFromID(graph.getLayersCount() - 1), output);
24+
graph.inference(options);
25+
}
26+
27+
void alexnet_comparison() {
28+
std::vector<size_t> counts = {979, 1134, 1031, 1009, 981,
29+
891, 957, 1027, 973, 1008};
30+
size_t sum = std::accumulate(counts.begin(), counts.end(), size_t{0});
31+
int count_pic = static_cast<int>(sum) + 10;
32+
std::vector<float> res(count_pic * 28 * 28, 1.0F);
33+
Tensor input;
34+
Shape sh1({1, 5, 5, 3});
35+
std::vector<float> vec;
36+
vec.reserve(75);
37+
for (int i = 0; i < 75; ++i) {
38+
vec.push_back(3);
39+
}
40+
Tensor output = make_tensor(vec, sh1);
41+
42+
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
43+
Tensor t = make_tensor<float>(res, sh);
44+
input = t;
45+
46+
RuntimeOptions options;
47+
Graph graph;
48+
Graph graph2;
49+
build_graph_linear(graph, input, output, options, true, false);
50+
Graph subgraph;
51+
std::shared_ptr<Layer> layer_0 = std::make_shared<ConvolutionalLayer>();
52+
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");
53+
subgraph.setInput(layer_0, input);
54+
subgraph.makeConnection(layer_0, layer_1);
55+
std::shared_ptr<Layer> layer_to = std::make_shared<ConvReluLayer>(
56+
std::dynamic_pointer_cast<ConvolutionalLayer>(layer_0));
57+
changed_subgraphs(graph, subgraph, layer_to, graph2, input, options);
58+
Tensor input_c = input;
59+
Tensor output_c = output;
60+
auto time1 = elapsed_time_avg<double, std::milli>(
61+
4, alexnet_inf_careless, graph, options, input_c, output_c);
62+
print_time_stats(graph);
63+
auto time2 = elapsed_time_avg<double, std::milli>(
64+
4, alexnet_inf_careless, graph2, options, input_c, output_c);
65+
print_time_stats(graph2);
66+
std::cout << time1 << " for unchanged graph\n";
67+
std::cout << time2 << " for convrelu graph\n";
68+
}
69+
1470
int main() {
1571
int type = 2;
1672
Tensor input = make_tensor(std::vector<int>({0}));
1773
RuntimeOptions options;
74+
alexnet_comparison();
1875
if (type == 0) {
1976
Graph graph1;
2077
build_graph(graph1, input, input, MODEL_PATH_DENSENET_ONNX, options, false);
2178

2279
Graph subgraph;
23-
Tensor scale = make_tensor(std::vector<float>({1.0}));
80+
Tensor scale = make_tensor(std::vector<float>({1.0F}));
2481
std::shared_ptr<Layer> layer_0 =
2582
std::make_shared<BatchNormalizationLayer>(scale, scale, scale, scale);
2683
std::shared_ptr<Layer> layer_1 = std::make_shared<EWLayer>("relu");

0 commit comments

Comments
 (0)