Skip to content

Commit 1dfd624

Browse files
committed
total time per layer
1 parent 8fdd518 commit 1dfd624

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

app/Graph/build.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ void print_time_stats(Graph& graph) {
11741174
int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0);
11751175
std::cout << "Elapsed inference time:" << sum << '\n';
11761176
std::cout << "!INFERENCE TIME INFO END!" << '\n';
1177+
graph.printLayerStats();
11771178
#else
11781179
(void)graph;
11791180
#endif

include/graph/graph.hpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
#include "runtime_options.hpp"
1515

1616
namespace 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

1825
struct BranchState {
1926
int ind_layer;
@@ -24,6 +31,8 @@ struct BranchState {
2431
};
2532

2633
class 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_);

src/Weights_Reader/reader_weights.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <nlohmann/json.hpp>
66
#include <stdexcept>
77
#include <vector>
8+
89
#ifdef _WIN32
910
#include <windows.h>
1011
#else
@@ -20,7 +21,6 @@ using json = nlohmann::json;
2021

2122
json read_json(const std::string& filename) {
2223
#ifdef _WIN32
23-
// Windows implementation
2424
HANDLE file = CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ,
2525
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2626
HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
@@ -33,7 +33,6 @@ json read_json(const std::string& filename) {
3333
CloseHandle(mapping);
3434
CloseHandle(file);
3535
#else
36-
// Unix implementation
3736
int fd = open(filename.c_str(), O_RDONLY);
3837
struct stat sb;
3938
fstat(fd, &sb);

0 commit comments

Comments
 (0)