Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Graph/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ void print_time_stats(Graph& graph) {
int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0);
std::cout << "Elapsed inference time:" << sum << '\n';
std::cout << "!INFERENCE TIME INFO END!" << '\n';
graph.printLayerStats();
#else
(void)graph;
#endif
Expand Down
2 changes: 2 additions & 0 deletions app/Graph/graph_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ int main(int argc, char* argv[]) {
options.par_backend = ParBackend::kThreads;
} else if (backend_str == "omp") {
options.par_backend = ParBackend::kOmp;
} else if (backend_str == "kokkos") {
options.par_backend = ParBackend::kKokkos;
} else {
std::cerr << "Unknown parallel backend: " << backend_str
<< ". Using default (Threads)." << '\n';
Expand Down
86 changes: 65 additions & 21 deletions include/graph/graph.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <memory>
#include <queue>
#include <stdexcept>
Expand All @@ -14,6 +15,31 @@
#include "runtime_options.hpp"

namespace it_lab_ai {
static std::unordered_map<LayerType, std::string> label_map = {
{kInput, "Input"},
{kPooling, "Pooling"},
{kElementWise, "Element-wise"},
{kConvolution, "Convolution"},
{kFullyConnected, "Dense"},
{kFlatten, "Flatten"},
{kConcat, "Concat"},
{kDropout, "Dropout"},
{kSplit, "Split"},
{kBinaryOp, "BinaryOp"},
{kTranspose, "Transpose"},
{kMatmul, "MatMul"},
{kReshape, "Reshape"},
{kSoftmax, "Softmax"},
{kReduce, "Reduce"},
{kBatchNormalization, "Normalization"}};

struct LayerTimeStats {
std::string layer_name;
double total_time = 0.0;
int call_count = 0;
double min_time = std::numeric_limits<double>::max();
double max_time = 0.0;
};

struct BranchState {
int ind_layer;
Expand All @@ -27,6 +53,7 @@ std::shared_ptr<Layer> layer_based_shared_copy(
const std::shared_ptr<Layer>& layer, const RuntimeOptions& options);

class Graph {
std::map<std::string, LayerTimeStats> layer_stats_;
int BiggestSize_;
int V_; // amount of ids
std::vector<std::shared_ptr<Layer>> layers_;
Expand Down Expand Up @@ -378,8 +405,27 @@ class Graph {
auto end = std::chrono::high_resolution_clock::now();
auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
time_.push_back(static_cast<int>(elapsed.count()));
time_layer_.push_back(layers_[current_layer]->getName());
int elapsed_ms = static_cast<int>(elapsed.count());
time_.push_back(elapsed_ms);

LayerType layer_type = layers_[current_layer]->getName();
time_layer_.push_back(layer_type);

auto it = label_map.find(layer_type);
std::string layer_name_str =
(it != label_map.end()) ? it->second : "Unknown";

auto& stats = layer_stats_[layer_name_str];
stats.total_time += elapsed_ms;
stats.call_count++;

if (stats.call_count == 1) {
stats.min_time = elapsed_ms;
stats.max_time = elapsed_ms;
} else {
if (elapsed_ms < stats.min_time) stats.min_time = elapsed_ms;
if (elapsed_ms > stats.max_time) stats.max_time = elapsed_ms;
}
#endif
}
}
Expand All @@ -403,25 +449,6 @@ class Graph {
#ifdef ENABLE_STATISTIC_TIME
std::vector<std::string> getTimeInfo() {
std::vector<std::string> res;

std::unordered_map<LayerType, std::string> label_map = {
{kInput, "Input"},
{kPooling, "Pooling"},
{kElementWise, "Element-wise"},
{kConvolution, "Convolution"},
{kFullyConnected, "Dense"},
{kFlatten, "Flatten"},
{kConcat, "Concat"},
{kDropout, "Dropout"},
{kSplit, "Split"},
{kBinaryOp, "BinaryOp"},
{kTranspose, "Transpose"},
{kMatmul, "MatMul"},
{kReshape, "Reshape"},
{kSoftmax, "Softmax"},
{kReduce, "Reduce"},
{kBatchNormalization, "Normalization"}};

for (size_t i = 0; i < time_.size(); i++) {
auto it = label_map.find(time_layer_[i]);
std::string layer_name = (it != label_map.end()) ? it->second : "Unknown";
Expand Down Expand Up @@ -456,6 +483,23 @@ class Graph {
return result;
}

void printLayerStats() {
std::cout << "\n========== LAYER PERFORMANCE STATISTICS ==========\n";
std::cout << std::left << std::setw(20) << "Layer Type" << std::right
<< std::setw(15) << "Total (ms)" << std::setw(12) << "Calls"
<< std::setw(15) << "Avg (ms)" << std::setw(15) << "Min (ms)"
<< std::setw(15) << "Max (ms)" << '\n';

for (const auto& [name, stats] : layer_stats_) {
double avg = stats.total_time / stats.call_count;
std::cout << std::left << std::setw(20) << name << std::right
<< std::fixed << std::setprecision(3) << std::setw(15)
<< stats.total_time << std::setw(12) << stats.call_count
<< std::setw(15) << avg << std::setw(15) << stats.min_time
<< std::setw(15) << stats.max_time << '\n';
}
}

[[nodiscard]] std::vector<int> getTraversalOrder() const {
auto in_out_degrees = getInOutDegrees();
std::vector<int> in_degree(V_);
Expand Down
58 changes: 49 additions & 9 deletions src/Weights_Reader/reader_weights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,64 @@
#include <stdexcept>
#include <vector>

#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

namespace it_lab_ai {

using json = nlohmann::json;

json read_json(const std::string& filename) {
std::ifstream ifs(filename);
if (!ifs.is_open()) {
throw std::runtime_error("Failed to open JSON file: " + filename);
#ifdef _WIN32
HANDLE file = CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
throw std::runtime_error("Cannot open file: " + filename);
}

DWORD size = GetFileSize(file, NULL);
if (size == 0) {
CloseHandle(file);
return json{};
}

HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
char* data = (char*)MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);

json result = json::parse(data, data + size);

UnmapViewOfFile(data);
CloseHandle(mapping);
CloseHandle(file);
return result;

#else
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
throw std::runtime_error("Cannot open file: " + filename);
}

json model_data;
try {
ifs >> model_data;
} catch (const json::parse_error& e) {
throw std::runtime_error("JSON parse error: " + std::string(e.what()));
struct stat sb;
fstat(fd, &sb);

if (sb.st_size == 0) {
close(fd);
return json{};
}

return model_data;
char* data = (char*)mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
json result = json::parse(data, data + sb.st_size);

munmap(data, sb.st_size);
close(fd);
return result;
#endif
}

void extract_values_from_json(const json& j, std::vector<float>& values) {
Expand Down
Loading