1414#include " runtime_options.hpp"
1515
1616namespace it_lab_ai {
17+ struct LayerTimeStats {
18+ std::string layer_name;
19+ double total_time = 0.0 ;
20+ int call_count = 0 ;
21+ double min_time = std::numeric_limits<double >::max();
22+ double max_time = 0.0 ;
23+ };
1724
1825struct BranchState {
1926 int ind_layer;
@@ -24,6 +31,8 @@ struct BranchState {
2431};
2532
2633class Graph {
34+ std::map<std::string, LayerTimeStats> layer_stats_;
35+ bool collect_layer_stats_ = true ;
2736 int BiggestSize_;
2837 int V_; // amount of ids
2938 std::vector<std::shared_ptr<Layer>> layers_;
@@ -372,8 +381,52 @@ class Graph {
372381 auto end = std::chrono::high_resolution_clock::now ();
373382 auto elapsed =
374383 std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
375- time_.push_back (static_cast <int >(elapsed.count ()));
376- time_layer_.push_back (layers_[current_layer]->getName ());
384+ int elapsed_ms = static_cast <int >(elapsed.count ());
385+ time_.push_back (elapsed_ms);
386+
387+ LayerType layer_type = layers_[current_layer]->getName ();
388+ time_layer_.push_back (layer_type);
389+
390+ if (collect_layer_stats_) {
391+ // Èñïîëüçóåì òó æå label_map äëÿ êîíâåðòàöèè â ñòðîêó
392+ static const std::unordered_map<LayerType, std::string> label_map = {
393+ {kInput , " Input" },
394+ {kPooling , " Pooling" },
395+ {kElementWise , " Element-wise" },
396+ {kConvolution , " Convolution" },
397+ {kFullyConnected , " Dense" },
398+ {kFlatten , " Flatten" },
399+ {kConcat , " Concat" },
400+ {kDropout , " Dropout" },
401+ {kSplit , " Split" },
402+ {kBinaryOp , " BinaryOp" },
403+ {kTranspose , " Transpose" },
404+ {kMatmul , " MatMul" },
405+ {kReshape , " Reshape" },
406+ {kSoftmax , " Softmax" },
407+ {kReduce , " Reduce" },
408+ {kBatchNormalization , " Normalization" }};
409+
410+ auto it = label_map.find (layer_type);
411+ std::string layer_name_str =
412+ (it != label_map.end ()) ? it->second : " Unknown" ;
413+
414+ auto & stats = layer_stats_[layer_name_str];
415+
416+ // Îáíîâëÿåì ñòàòèñòèêó (ïðåäïîëàãàåì, ÷òî stats.total_time - ýòî int
417+ // èëè double)
418+ stats.total_time += elapsed_ms;
419+ stats.call_count ++;
420+
421+ // Îáíîâëÿåì min/max
422+ if (stats.call_count == 1 ) {
423+ stats.min_time = elapsed_ms;
424+ stats.max_time = elapsed_ms;
425+ } else {
426+ if (elapsed_ms < stats.min_time ) stats.min_time = elapsed_ms;
427+ if (elapsed_ms > stats.max_time ) stats.max_time = elapsed_ms;
428+ }
429+ }
377430#endif
378431 }
379432 }
@@ -449,7 +502,22 @@ class Graph {
449502
450503 return result;
451504 }
452-
505+ void printLayerStats () {
506+ std::cout << " \n ========== LAYER PERFORMANCE STATISTICS ==========\n " ;
507+ std::cout << std::left << std::setw (20 ) << " Layer Type" << std::right
508+ << std::setw (15 ) << " Total (ms)" << std::setw (12 ) << " Calls"
509+ << std::setw (15 ) << " Avg (ms)" << std::setw (15 ) << " Min (ms)"
510+ << std::setw (15 ) << " Max (ms)" << std::endl;
511+
512+ for (const auto & [name, stats] : layer_stats_) {
513+ double avg = stats.total_time / stats.call_count ;
514+ std::cout << std::left << std::setw (20 ) << name << std::right
515+ << std::fixed << std::setprecision (3 ) << std::setw (15 )
516+ << stats.total_time << std::setw (12 ) << stats.call_count
517+ << std::setw (15 ) << avg << std::setw (15 ) << stats.min_time
518+ << std::setw (15 ) << stats.max_time << std::endl;
519+ }
520+ }
453521 [[nodiscard]] std::vector<int > getTraversalOrder () const {
454522 auto in_out_degrees = getInOutDegrees ();
455523 std::vector<int > in_degree (V_);
0 commit comments