-
Notifications
You must be signed in to change notification settings - Fork 5
Graph with layer #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Graph with layer #38
Changes from all commits
ac67f77
5f36230
4867937
fb57554
f2afeea
82f0dc4
2f1141a
fdfe528
116cf4a
55a9ee8
b3e62d4
f65be87
7e9a389
f2547da
a89b0ba
9dcf39d
fbf472e
ad52dd0
2fc6832
0efcb04
4d68495
2c2d708
7d84878
d07c3d8
96b403c
7be04f0
aaa5d80
0ca0331
ff1a8f8
abd51c9
e901675
f379e51
b185542
5ae7ad8
e1350f0
3155cd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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] | ||
|
@@ -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: | ||
|
@@ -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 | ||
|
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 "") |
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 { | ||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
bool for check - Layer is added or not |
||||||||
void removeEdge(Layer& layPrev, Layer& layNext); | ||||||||
void removeLayer(Layer& lay); | ||||||||
int getLayers() const; | ||||||||
int getEdges() const; | ||||||||
bool empty() const; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
bool hasPath(Layer& layPrev, Layer& layNext) const; | ||||||||
std::vector<int> BFS(int start); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
void setInput(Layer& lay, Tensor<double>& vec); | ||||||||
void setOutput(Layer& lay, Tensor<double>& vec); | ||||||||
void inference(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
~Graph(); | ||||||||
}; | ||||||||
|
||||||||
#endif |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
virtual Shape get_output_shape() = 0; | ||||||
|
||||||
void addNeighbor(Layer* neighbor); | ||||||
void removeNeighbor(Layer* neighbor); | ||||||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or
Model