Skip to content

Commit 4220c19

Browse files
authored
Get rid of "parallel" and "oneDNN" parameters in "build_graph" (#223)
#218
1 parent 2a2b9df commit 4220c19

9 files changed

Lines changed: 366 additions & 399 deletions

File tree

app/Accuracy/accuracy_check.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ int main() {
4343
}
4444
Tensor input = t;
4545
Tensor output = make_tensor(vec, sh1);
46-
InputLayer a1(kNhwc, kNchw, 1, 2);
46+
auto a1 = std::make_shared<InputLayer>(kNchw, kNchw, 1, 2);
4747
std::vector<float> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
4848
Shape sh2({3, 3});
4949
Tensor kernel = make_tensor(kernelvec, sh2);
50-
ConvolutionalLayer a2(1, 0, 0, kernel);
50+
auto a2 = std::make_shared<ConvolutionalLayer>(1, 0, 0, kernel);
5151
Shape poolshape = {2, 2};
52-
EWLayer a3("linear", 2.0F, 3.0F);
53-
PoolingLayer a4(poolshape, "average");
54-
FCLayer a6;
55-
OutputLayer a5;
52+
auto a3 = std::make_shared<EWLayer>("linear", 2.0F, 3.0F);
53+
auto a4 = std::make_shared<PoolingLayer>(poolshape, "average");
54+
auto a5 = std::make_shared<OutputLayer>();
55+
auto a6 = std::make_shared<FCLayer>();
5656
graph.setInput(a1, input);
5757
graph.makeConnection(a1, a2);
5858
graph.makeConnection(a2, a3);

app/AccuracyImgNet/accimgnet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ void check_accuracy(const std::string& neural_network_path,
118118
Graph a1 = open_network(neural_network_path);
119119
Tensor input;
120120
Tensor output;
121-
InputLayer inlayer;
122-
OutputLayer outlayer;
121+
auto inlayer = std::make_shared<InputLayer>();
122+
auto outlayer = std::make_shared<OutputLayer>();
123123
// ?? warning from linux
124-
outlayer.setID(1);
125-
inlayer.setID(0);
124+
outlayer->setID(1);
125+
inlayer->setID(0);
126126
//
127127
size_t k = 5;
128128
for (size_t i = 0; i < imgs_size; i++) {

app/Graph/acc_check.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ using namespace it_lab_ai;
1313

1414
int main(int argc, char* argv[]) {
1515
std::string model_name = "alexnet_mnist";
16-
bool parallel = false;
1716
bool onednn = false;
1817
for (int i = 1; i < argc; ++i) {
19-
if (std::string(argv[i]) == "--parallel") {
20-
parallel = true;
21-
} else if (std::string(argv[i]) == "--model" && i + 1 < argc) {
18+
if (std::string(argv[i]) == "--model" && i + 1 < argc) {
2219
model_name = argv[++i];
2320
} else if (std::string(argv[i]) == "--onednn") {
2421
onednn = true;
2522
}
2623
}
27-
24+
it_lab_ai::LayerFactory::configure(onednn);
2825
std::string dataset_path;
2926
if (model_name == "alexnet_mnist") {
3027
dataset_path = MNIST_PATH;
@@ -80,7 +77,9 @@ int main(int argc, char* argv[]) {
8077
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
8178
Tensor t = make_tensor<float>(res, sh);
8279
input = t;
83-
build_graph_linear(input, output, false, parallel, onednn);
80+
auto graph = build_graph_linear(input, output, false);
81+
graph.inference();
82+
print_time_stats(graph);
8483
std::vector<std::vector<float>> tmp_output =
8584
softmax<float>(*output.as<float>(), 10);
8685
std::vector<size_t> indices;
@@ -187,7 +186,9 @@ int main(int argc, char* argv[]) {
187186
it_lab_ai::Tensor output =
188187
it_lab_ai::Tensor(output_shape, it_lab_ai::Type::kFloat);
189188

190-
build_graph(input, output, json_path, false, parallel, onednn);
189+
auto graph = build_graph(input, output, json_path, false);
190+
graph.inference();
191+
print_time_stats(graph);
191192
std::vector<std::vector<float>> processed_outputs;
192193
const std::vector<float>& raw_output = *output.as<float>();
193194

app/Graph/build.cpp

Lines changed: 50 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
using namespace it_lab_ai;
66

7-
void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
8-
bool comments, bool parallel, bool onednn) {
7+
Graph build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
8+
bool comments) {
99
if (comments) {
1010
for (size_t i = 0; i < input.get_shape().dims(); i++) {
1111
std::cout << input.get_shape()[i] << ' ';
@@ -25,8 +25,6 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
2525
std::cout << std::endl << std::endl;
2626
}
2727
}
28-
it_lab_ai::ImplType impl1 = parallel ? it_lab_ai::kTBB : it_lab_ai::kDefault;
29-
it_lab_ai::ImplType impl2 = parallel ? it_lab_ai::kSTL : it_lab_ai::kDefault;
3028
std::vector<std::shared_ptr<it_lab_ai::Layer>> layers;
3129
std::vector<bool> layerpostop;
3230

@@ -74,18 +72,13 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
7472
it_lab_ai::Tensor tmp_values = tensor;
7573
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
7674
auto conv_layer = std::make_shared<it_lab_ai::ConvolutionalLayer>(
77-
1, pads, 1, tmp_values, tmp_bias, impl2, 1, true);
75+
1, pads, 1, tmp_values, tmp_bias, kDefault, 1, true);
7876
layers.push_back(conv_layer);
7977
layerpostop.push_back(false);
8078
if (comments) std::cout << "ConvLayer added to layers." << std::endl;
8179
}
8280
if (layer_type.find("relu") != std::string::npos) {
83-
std::shared_ptr<it_lab_ai::Layer> ew_layer;
84-
if (onednn) {
85-
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>("relu");
86-
} else {
87-
ew_layer = std::make_shared<it_lab_ai::EWLayer>("relu");
88-
}
81+
auto ew_layer = LayerFactory::createEwLayer("relu");
8982
layers.push_back(ew_layer);
9083
layerpostop.push_back(true);
9184
if (comments)
@@ -111,7 +104,7 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
111104
std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1]
112105
<< std::endl;
113106
auto pool_layer =
114-
std::make_shared<it_lab_ai::PoolingLayer>(shape, pooltype, impl1);
107+
std::make_shared<it_lab_ai::PoolingLayer>(shape, pooltype, kDefault);
115108
layers.push_back(pool_layer);
116109
layerpostop.push_back(false);
117110
if (comments) std::cout << "PoolingLayer added to layers." << std::endl;
@@ -139,14 +132,15 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
139132
if (comments)
140133
std::cout << "number of layers - " << layers.size() + 1 << std::endl;
141134
it_lab_ai::Graph graph(static_cast<int>(layers.size()));
142-
it_lab_ai::InputLayer a1(it_lab_ai::kNchw, it_lab_ai::kNchw);
135+
auto a1 = std::make_shared<it_lab_ai::InputLayer>(it_lab_ai::kNchw,
136+
it_lab_ai::kNchw);
143137

144138
if (comments) std::cout << "InputLayer created." << std::endl;
145139

146140
graph.setInput(a1, input);
147141
if (comments) std::cout << "Input set in graph." << std::endl;
148142

149-
graph.makeConnection(a1, *layers[0]);
143+
graph.makeConnection(a1, layers[0]);
150144
if (comments)
151145
std::cout << "Connection made between InputLayer and first layer."
152146
<< std::endl;
@@ -155,39 +149,17 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
155149
if (layerpostop[i]) {
156150
layers[i - 1]->postops.layers.push_back(layers[i].get());
157151
layers[i - 1]->postops.count++;
158-
graph.makeConnection(*layers[i - 1], *layers[i + 1]);
152+
graph.makeConnection(layers[i - 1], layers[i + 1]);
159153
} else if (!layerpostop[i + 1])
160-
graph.makeConnection(*layers[i], *layers[i + 1]);
154+
graph.makeConnection(layers[i], layers[i + 1]);
161155
}
162156

163-
graph.setOutput(*layers.back(), output);
164-
if (comments) std::cout << "Output set in graph." << std::endl;
157+
graph.setOutput(layers.back(), output);
165158

166-
if (comments) std::cout << "Starting inference..." << std::endl;
167-
graph.inference();
168-
#ifdef ENABLE_STATISTIC_TIME
169-
std::vector<std::string> times = graph.getTimeInfo();
170-
std::cout << "!INFERENCE TIME INFO START!" << std::endl;
171-
for (size_t i = 0; i < times.size(); i++) {
172-
std::cout << times[i] << std::endl;
173-
}
174-
std::vector<int> elps_time = graph.getTime();
175-
int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0);
176-
std::cout << "Elapsed inference time:" << sum << std::endl;
177-
std::cout << "!INFERENCE TIME INFO END!" << std::endl;
178-
#endif
179-
if (comments) std::cout << "Inference completed." << std::endl;
180-
if (comments) {
181-
std::vector<float> tmp_output =
182-
it_lab_ai::softmax<float>(*output.as<float>());
183-
for (size_t i = 0; i < tmp_output.size(); i++) {
184-
if (tmp_output[i] < 1e-6) {
185-
std::cout << i << ": 0" << std::endl;
186-
} else {
187-
std::cout << i << ": " << tmp_output[i] << std::endl;
188-
}
189-
}
159+
for (auto& layer : layers) {
160+
graph.addOwnedLayer(layer);
190161
}
162+
return graph;
191163
}
192164

193165
std::string get_base_layer_name(const std::string& tensor_name) {
@@ -234,9 +206,8 @@ std::string layerTypeToString(it_lab_ai::LayerType type) {
234206
}
235207
}
236208

237-
void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
238-
const std::string& json_path, bool comments, bool parallel,
239-
bool onednn) {
209+
Graph build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
210+
const std::string& json_path, bool comments) {
240211
if (comments) {
241212
for (size_t i = 0; i < input.get_shape().dims(); i++) {
242213
std::cout << input.get_shape()[i] << ' ';
@@ -257,7 +228,7 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
257228
}
258229
}
259230

260-
auto parse_result = parse_json_model(json_path, comments, parallel, onednn);
231+
auto parse_result = parse_json_model(json_path, comments);
261232

262233
auto& layers = parse_result.layers;
263234
auto& name_to_layer = parse_result.name_to_layer;
@@ -275,7 +246,7 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
275246
[](const auto& layer) { return layer->getName() == it_lab_ai::kInput; });
276247

277248
if (input_layer_it != layers.end()) {
278-
graph.setInput(**input_layer_it, input);
249+
graph.setInput(*input_layer_it, input);
279250
}
280251

281252
std::vector<std::pair<std::string, std::string>> connection_list;
@@ -331,8 +302,8 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
331302
}
332303

333304
try {
334-
graph.makeConnection(*name_to_layer[source_name],
335-
*name_to_layer[target_name]);
305+
graph.makeConnection(name_to_layer[source_name],
306+
name_to_layer[target_name]);
336307

337308
} catch (const std::exception& e) {
338309
std::cerr << "Failed: " << source_name << " -> " << target_name << " : "
@@ -353,31 +324,15 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
353324
}
354325
graph.setSplitDistribution(split_distribution);
355326
auto output_layer = layers.back();
356-
graph.setOutput(*output_layer, output);
357-
358-
if (comments) std::cout << "Starting inference..." << std::endl;
359-
try {
360-
graph.inference();
361-
if (comments) std::cout << "Inference completed successfully." << std::endl;
362-
} catch (const std::exception& e) {
363-
std::cerr << "ERROR during inference: " << e.what() << std::endl;
327+
graph.setOutput(output_layer, output);
328+
for (auto& layer : layers) {
329+
graph.addOwnedLayer(layer);
364330
}
365331

366-
#ifdef ENABLE_STATISTIC_TIME
367-
std::vector<std::string> times = graph.getTimeInfo();
368-
std::cout << "!INFERENCE TIME INFO START!" << std::endl;
369-
for (size_t i = 0; i < times.size(); i++) {
370-
std::cout << times[i] << std::endl;
371-
}
372-
std::vector<int> elps_time = graph.getTime();
373-
int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0);
374-
std::cout << "Elapsed inference time:" << sum << std::endl;
375-
std::cout << "!INFERENCE TIME INFO END!" << std::endl;
376-
#endif
332+
return graph;
377333
}
378334

379-
ParseResult parse_json_model(const std::string& json_path, bool comments,
380-
bool parallel, bool onednn) {
335+
ParseResult parse_json_model(const std::string& json_path, bool comments) {
381336
ParseResult result;
382337

383338
auto& layers = result.layers;
@@ -390,9 +345,6 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
390345
auto& split_distribution = result.split_distribution;
391346
auto& original_ids = result.original_ids;
392347

393-
it_lab_ai::ImplType impl1 = parallel ? it_lab_ai::kTBB : it_lab_ai::kDefault;
394-
it_lab_ai::ImplType impl2 = parallel ? it_lab_ai::kSTL : it_lab_ai::kDefault;
395-
396348
std::unordered_map<std::string, std::vector<int64_t>> layer_parameters;
397349
std::unordered_map<std::string, float> float_parameters;
398350
std::string last_constant_name;
@@ -490,25 +442,13 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
490442
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
491443

492444
auto conv_layer = std::make_shared<it_lab_ai::ConvolutionalLayer>(
493-
stride, pads, dilations, tmp_tensor, tmp_bias, impl2, group);
445+
stride, pads, dilations, tmp_tensor, tmp_bias, kDefault, group);
494446
layer = conv_layer;
495447
} else if (layer_type.find("Relu") != std::string::npos ||
496448
layer_type.find("relu") != std::string::npos) {
497-
std::shared_ptr<it_lab_ai::Layer> ew_layer;
498-
if (onednn) {
499-
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>("relu");
500-
} else {
501-
ew_layer = std::make_shared<it_lab_ai::EWLayer>("relu");
502-
}
503-
layer = ew_layer;
449+
layer = LayerFactory::createEwLayer("relu");
504450
} else if (layer_type.find("Sigmoid") != std::string::npos) {
505-
std::shared_ptr<it_lab_ai::Layer> ew_layer;
506-
if (onednn) {
507-
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>("sigmoid");
508-
} else {
509-
ew_layer = std::make_shared<it_lab_ai::EWLayer>("sigmoid");
510-
}
511-
layer = ew_layer;
451+
layer = LayerFactory::createEwLayer("sigmoid");
512452
} else if (layer_type.find("Dense") != std::string::npos ||
513453
layer_type.find("FullyConnected") != std::string::npos) {
514454
it_lab_ai::Tensor tensor = it_lab_ai::create_tensor_from_json(
@@ -538,7 +478,7 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
538478
<< std::endl;
539479
} else if (layer_type == "GlobalAveragePool") {
540480
auto pool_layer = std::make_shared<it_lab_ai::PoolingLayer>(
541-
it_lab_ai::Shape({0, 0}), "average", impl1);
481+
it_lab_ai::Shape({0, 0}), "average", kDefault);
542482
layer = pool_layer;
543483
if (comments) {
544484
std::cout << "GlobalAveragePool layer added (will use input spatial "
@@ -599,8 +539,8 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
599539
}
600540
}
601541

602-
auto pool_layer =
603-
std::make_shared<it_lab_ai::PoolingLayer>(shape, pooltype, impl1);
542+
auto pool_layer = std::make_shared<it_lab_ai::PoolingLayer>(
543+
shape, pooltype, kDefault);
604544

605545
try {
606546
if (strides[0] != 2 || strides[1] != 2) {
@@ -732,39 +672,13 @@ ParseResult parse_json_model(const std::string& json_path, bool comments,
732672

733673
if (layer_type == "Mul") {
734674
ew_operation = "linear";
735-
std::shared_ptr<it_lab_ai::Layer> ew_layer;
736-
if (onednn) {
737-
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>(
738-
ew_operation, value, 0.0F);
739-
} else {
740-
ew_layer = std::make_shared<it_lab_ai::EWLayer>(ew_operation,
741-
value, 0.0F);
742-
}
743-
layer = ew_layer;
675+
layer = LayerFactory::createEwLayer(ew_operation, value, 0.0F);
744676
} else if (layer_type == "Add") {
745677
ew_operation = "linear";
746-
std::shared_ptr<it_lab_ai::Layer> ew_layer;
747-
if (onednn &&
748-
it_lab_ai::EwLayerOneDnn::is_function_supported("linear")) {
749-
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>(
750-
ew_operation, 1.0F, value);
751-
} else {
752-
ew_layer = std::make_shared<it_lab_ai::EWLayer>(ew_operation,
753-
1.0F, value);
754-
}
755-
layer = ew_layer;
678+
layer = LayerFactory::createEwLayer(ew_operation, 1.0F, value);
756679
} else if (layer_type == "Sub") {
757680
ew_operation = "linear";
758-
std::shared_ptr<it_lab_ai::Layer> ew_layer;
759-
if (onednn &&
760-
it_lab_ai::EwLayerOneDnn::is_function_supported("linear")) {
761-
ew_layer = std::make_shared<it_lab_ai::EwLayerOneDnn>(
762-
ew_operation, 1.0F, -value);
763-
} else {
764-
ew_layer = std::make_shared<it_lab_ai::EWLayer>(ew_operation,
765-
1.0F, -value);
766-
}
767-
layer = ew_layer;
681+
layer = LayerFactory::createEwLayer(ew_operation, 1.0F, -value);
768682
} else {
769683
continue;
770684
}
@@ -1292,4 +1206,20 @@ it_lab_ai::Tensor prepare_mnist_image(const cv::Mat& image) {
12921206

12931207
Shape sh({1, 1, 28, 28});
12941208
return it_lab_ai::make_tensor(res, sh);
1209+
}
1210+
1211+
void print_time_stats(Graph& graph) {
1212+
#ifdef ENABLE_STATISTIC_TIME
1213+
std::vector<std::string> times = graph.getTimeInfo();
1214+
std::cout << "!INFERENCE TIME INFO START!" << std::endl;
1215+
for (size_t i = 0; i < times.size(); i++) {
1216+
std::cout << times[i] << std::endl;
1217+
}
1218+
std::vector<int> elps_time = graph.getTime();
1219+
int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0);
1220+
std::cout << "Elapsed inference time:" << sum << std::endl;
1221+
std::cout << "!INFERENCE TIME INFO END!" << std::endl;
1222+
#else
1223+
(void)graph;
1224+
#endif
12951225
}

0 commit comments

Comments
 (0)