Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ logs/
.vscode
*.out
*.dot
*.png
*.proto
.DS_Store
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# ***Tensor Compiler***
>*PROJ_FRONT, Tensor Compiler course 26*
>*FRONTEND*

A frontend for a tensor compiler that parses neural network models in ONNX format and converts them into an internal graph representation. The graph can be visualized using Graphviz.

<p align="center">
<img width="200px" src="./readme_images/graph.png" alt="qr"/>
</p>

## Frontend features

- Reads **ONNX models** (`.onnx`) using Protocol Buffers;
- Builds a computational graph with tensors and operations;
- Supports operations: *Add*, *Mul*, *Conv*, *Relu*, *MatMul*, *Gemm*;
- Visualizes the graph as a DOT file and renders it to PNG (requires **Graphviz**);
- Logging system with configurable log file (can be found in `./logs`);
- Modular design with clear separation of concerns (parser, converters, graph, visualization).

## Requirements

Expand All @@ -20,6 +24,7 @@ A frontend for a tensor compiler that parses neural network models in ONNX forma
- Python 3 with `onnx` and `numpy` (optional, for generating test models)

## Installation

```bash
git clone https://github.com/a-palonskaa/TensorCompiler
cd TensorCompiler
Expand All @@ -35,17 +40,20 @@ cmake --build build --parallel
## Run

```bash
./build/run [name].onnx
./build/run [name].onnx [optional -d]
```
`-d` is a flag for debug vesion with debug text dump
Name from `./models`.
Image of graph and a dot file are located in `./images`.

### Notes

To get absl:
```bash
git clone https://github.com/abseil/abseil-cpp.git
```

## Authors:

- *Palonskaya Alina, B01-405 DREC*
- *Makarskaya Alexandra, B01-401 DREC*
- *Makarskaya Alexandra, B01-401 DREC*
29 changes: 17 additions & 12 deletions common/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,35 @@ class Logger {
<< "[time: " << timestamp << "] " << "[" << file << ":" << line
<< "] " << ": " << message << "\n";

if (logFile_.is_open()) {
logFile_ << logEntry.str();
logFile_.flush();
if (fileStream_.is_open()) {
fileStream_ << logEntry.str();
fileStream_.flush();
}
}

bool setlogFile(const string& filename) {
if (logFile_.is_open()) {
logFile_.close();
bool setLogFile(const std::string& filename) {
if (fileStream_.is_open()) {
fileStream_.close();
}

logFile_.open(filename, ios::app);
if (!logFile_.is_open()) {
cerr << "Error opening new log file: " << filename << endl;
fileStream_.open(filename, std::ios::app);

if (fileStream_.is_open()) {
output_ = &fileStream_;
return true;
} else {
output_ = &std::cerr;
std::cerr << "[ERROR] Cannot open log file: " << filename << "\n";
return false;
}
return true;
}

private:
Logger() = default;
~Logger() {
if (logFile_.is_open()) logFile_.close();
if (fileStream_.is_open()) fileStream_.close();
}

ofstream logFile_;
std::ofstream fileStream_;
std::ostream* output_ = &std::cerr;
};
56 changes: 37 additions & 19 deletions frontend/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,61 @@ std::string replace_extension(const std::string& filename,

} // namespace

namespace Config {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

добавить пустую строку после namespace

constexpr char LOGS_DIR[] = "./logs/";
constexpr char IMG_DIR[] = "./images/";
constexpr char MODELS[] = "./models/";

constexpr char DOT_EXT[] = ".dot";
constexpr char PNG_EXT[] = ".png";
constexpr char LOG_EXT[] = ".log";

constexpr char LOGFILE[] = "frontend.log";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

добавитть пустую строку после 31

} // namespace Config

int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <model.onnx>" << '\n';
return 1;
if ((argc < 2) || (argc > 3) ||
(argc == 3 && argv[2] && strcmp(argv[2], "-d") != 0)) {
std::cerr << "Usage: " << argv[0] << " <model.onnx>" << "[optional -d]"
<< '\n';
return 0;
}

std::filesystem::create_directories("./logs");
std::filesystem::create_directories("./images");

if (!Logger::getInstance().setlogFile("./logs/frontend.log")) {
std::cout << "Failed to open log file, logs will be printd out to "
"std::cerr only \n";
if (!Logger::getInstance().setLogFile(std::string(Config::LOGS_DIR) +
std::string(Config::LOGFILE))) {
LOG(INFO, "logs will be printed to std::cerr\n");
}

TensorCompiler::ONNXParser parser;
std::filesystem::create_directories(Config::LOGS_DIR);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не логичнее ли поставить их до строки 42?

std::filesystem::create_directories(Config::IMG_DIR);

std::string path = std::string("./models/") + argv[1];
std::string dot_filename =
Config::IMG_DIR + replace_extension(argv[1], Config::DOT_EXT);
std::string png_filename =
Config::IMG_DIR + replace_extension(argv[1], Config::PNG_EXT);

TensorCompiler::ONNXParser parser;
std::string path = std::string(Config::MODELS) + argv[1];
if (!parser.load(path)) {
std::cerr << "Failed to parse ONNX model\n";
LOG(ERROR, "Failed to parse ONNX model\n");
return 1;
}

const auto& graph = parser.ParseGraph();
std::string dot_filename = "./images/" + replace_extension(argv[1], ".dot");
std::string png_filename = "./images/" + replace_extension(argv[1], ".png");

std::cout << "\nParsed graph with " << graph.nodes().size() << " nodes and "
<< graph.tensors().size() << " tensors.\n\n";
parser.dump();
<< graph.tensors().size() << " tensors.\n";

if (argc == 3 && strcmp(argv[2], "-d") == 0) {
parser.dump();
}

graph.ToGraphViz(dot_filename);

std::string cmd = "dot -Tpng " + dot_filename + " -o " + png_filename;
int ret = std::system(cmd.c_str());
if (ret != 0) {
std::cerr << "Error: failed to render PNG (dot returned " << ret
<< ").\n";
LOG(WARNING, "Failed to render PNG (dot returned " +
std::to_string(ret) + ").\n");
} else {
std::cout << "\nGraph rendered to " << png_filename << '\n';
}
Expand Down
Binary file added readme_images/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.