44
55using 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
193165std::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