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
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
submodules: recursive
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y cmake ninja-build ccache scons
- name: ccache
uses: hendrikmuhs/[email protected]
Expand All @@ -37,6 +38,7 @@ jobs:
submodules: recursive
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y cmake ninja-build ccache scons
- name: ccache
uses: hendrikmuhs/[email protected]
Expand Down Expand Up @@ -81,6 +83,7 @@ jobs:
submodules: true
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y cmake ninja-build ccache gcovr lcov scons
- uses: actions/checkout@v4
with:
Expand All @@ -102,7 +105,7 @@ jobs:
cmake --build build --parallel
- name: Test
run: |
build/bin/run_tests
build/test/run_test
env:
CTEST_OUTPUT_ON_FAILURE: 1
- name: Generate lcov Coverage Data
Expand Down
19 changes: 13 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 11)

project(cpp_template)

include(cmake/configure.cmake)
set(ProjectName "itlab")
project(${ProjectName})

include_directories(include)

enable_testing()

add_subdirectory(3rdparty)
add_subdirectory(app)
add_subdirectory(include)

add_subdirectory(3rdparty/googletest)
add_subdirectory(src)
add_subdirectory(test)

# REPORT
message( STATUS "")
message( STATUS "General configuration for ${PROJECT_NAME}")
message( STATUS "======================================")
message( STATUS "")
message( STATUS " Configuration: ${CMAKE_BUILD_TYPE}")
message( STATUS "")
38 changes: 38 additions & 0 deletions include/graph/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef GRAPH_H
#define GRAPH_H

#include <unordered_map>
#include <vector>

#include "./layer/layer.h"
#include "./tensor/tensor.h"

class Graph {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class Graph {
class Network {

or Model

private:
std::unordered_map<int, Layer*> layers_;
Tensor<double> inputTensor_;
Tensor<double>* outputTensor_;
int start_ = -1;
int end_ = -1;
bool bfs_helper(int start, int vert, bool flag,
std::vector<int>* v_ord) const;

public:
Graph();

void addLayer(Layer& lay);
void addEdge(Layer& layPrev, Layer& layNext);
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void addLayer(Layer& lay);
void addEdge(Layer& layPrev, Layer& layNext);
bool addLayer(Layer& lay, std::vector<LayerID>& InputsLayers, std::vector<LayerID>& OutputLayers);

bool for check - Layer is added or not
addEdge is redudant

void removeEdge(Layer& layPrev, Layer& layNext);
void removeLayer(Layer& lay);
int getLayers() const;
int getEdges() const;
bool empty() const;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
bool empty() const;
bool isEmpty() const;

bool hasPath(Layer& layPrev, Layer& layNext) const;
std::vector<int> BFS(int start);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
std::vector<int> BFS(int start);
std::vector<int> inference(int start);

void setInput(Layer& lay, Tensor<double>& vec);
void setOutput(Layer& lay, Tensor<double>& vec);
void inference();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void inference();
void run();

~Graph();
};

#endif
24 changes: 24 additions & 0 deletions include/layer/layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef LAYER_H
#define LAYER_H

#include <list>

#include "./tensor/tensor.h"

class Layer {
protected:
int id_;

public:
Layer() = default;
Copy link
Member

Choose a reason for hiding this comment

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

please create constructor with abstract class of attributes, @aobolensk what do you think?

virtual ~Layer() = default;
void setID(int id) { id_ = id; }
Copy link
Member

@allnes allnes Apr 15, 2025

Choose a reason for hiding this comment

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

i think it would be string form with integer ID

int getID() const { return id_; }
virtual void run(const Tensor<double>& input, Tensor<double>& output) = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
virtual void run(const Tensor<double>& input, Tensor<double>& output) = 0;
virtual void exec(const Tensor<double>& input, Tensor<double>& output) = 0;

virtual Shape get_output_shape() = 0;

void addNeighbor(Layer* neighbor);
void removeNeighbor(Layer* neighbor);
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

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

is it important? I think its part of graph

std::list<Layer*> neighbors_;
};
#endif
98 changes: 98 additions & 0 deletions include/tensor/tensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef TENSOR_H
#define TENSOR_H

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <utility>
#include <vector>

struct Shape {
std::vector<size_t> dimensions;
size_t total_elements;

Shape(std::vector<size_t> dims);

size_t get_rank() const;
};

enum Layout : std::uint8_t { kNchw, kNhwc, kNd };

template <typename T>
class Tensor {
public:
Shape shape;
Layout layout;
std::vector<T> data;

Tensor(const Shape &sh, Layout l = Layout::kNd);
Tensor(std::vector<size_t> dims, Layout l = Layout::kNd);

size_t get_linear_index(const std::vector<size_t> &indices) const;

T &at(const std::vector<size_t> &indices);
const T &at(const std::vector<size_t> &indices) const;
};

template <typename T>
Tensor<T>::Tensor(const Shape &sh, Layout l)
: shape(sh), layout(l), data(sh.total_elements) {}

template <typename T>
Tensor<T>::Tensor(std::vector<size_t> dims, Layout l)
: Tensor(Shape(std::move(dims)), l) {}

template <typename T>
size_t Tensor<T>::get_linear_index(const std::vector<size_t> &indices) const {
if (indices.size() != shape.get_rank()) {
throw std::runtime_error("Incorrect number of indices provided.");
}
for (size_t i = 0; i < indices.size(); ++i) {
if (indices[i] >= shape.dimensions[i]) {
throw std::out_of_range("Index out of range for dimension");
}
}

size_t linear_index = 0;
size_t stride = 1;

if (shape.get_rank() == 4) {
if (layout == Layout::kNchw) {
linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] *
shape.dimensions[3]) +
indices[1] * (shape.dimensions[2] * shape.dimensions[3]) +
indices[2] * shape.dimensions[3] + indices[3];
} else if (layout == Layout::kNhwc) {
linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] *
shape.dimensions[3]) +
indices[1] * (shape.dimensions[2] * shape.dimensions[3]) +
indices[2] * shape.dimensions[3] + indices[3];
} else {
linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] *
shape.dimensions[3]) +
indices[1] * (shape.dimensions[2] * shape.dimensions[3]) +
indices[2] * shape.dimensions[3] + indices[3];
}
} else {
std::vector<size_t> reversed_dims = shape.dimensions;
std::reverse(reversed_dims.begin(), reversed_dims.end());
for (int i = static_cast<int>(reversed_dims.size()) - 1; i >= 0; --i) {
linear_index += indices[i] * stride;
stride *= reversed_dims[i];
}
}

return linear_index;
}

template <typename T>
T &Tensor<T>::at(const std::vector<size_t> &indices) {
return data[get_linear_index(indices)];
}

template <typename T>
const T &Tensor<T>::at(const std::vector<size_t> &indices) const {
return data[get_linear_index(indices)];
}
#endif
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp")

add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES})
target_sources(${ProjectName} PRIVATE ${HEADER_FILES})

target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src)
Loading
Loading