generated from embedded-dev-research/cpp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Layers #54
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
Open
Lepsps
wants to merge
41
commits into
embedded-dev-research:main
Choose a base branch
from
Lepsps:layers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Layers #54
Changes from 37 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
ac67f77
added a graph class
5f36230
gtest dir fix
4867937
CMake fix 1.0
fb57554
CMake fix 2.0
f2afeea
CMake fix 2.0
82f0dc4
clang-tidy, clang-format and ubuntu-build fix 1.0
2f1141a
clang-tidy, clang-format, ubuntu-build and cmake fix 2.0
fdfe528
CMake, builts, clang format/tidy fix 3.0
116cf4a
tensor add 1.0
55a9ee8
graph pr fix
b3e62d4
tensor class v0.1
f65be87
tensor v0.2
7e9a389
tensor v0.3
f2547da
tensor v0.3
a89b0ba
tensor v0.4
9dcf39d
tensor built fix
fbf472e
tensor fix
ad52dd0
codecov fix
2fc6832
macos-clang-build
0efcb04
clang-format
4d68495
clang-format 2
2c2d708
clang-tidy
7d84878
errors fix
d07c3d8
errors fix 2
96b403c
error fix 3
7be04f0
error fix 4
aaa5d80
errors fix 5
0ca0331
errors fix 7
ff1a8f8
add layer, change graph
abd51c9
clang-tidy fix
e901675
clang-tidy and clang-format fix
f379e51
clang-format and clang-tidy fix 2
b185542
clang-format fix
5ae7ad8
clang-format fix 2
e1350f0
tensor fix
3155cd8
clang-tidy fix
9de22dc
add layer's mocks, change graph
Lepsps d64ad06
throws fix
7bba704
build and clang fix, add test with grath branching
9ee9fe3
clang-tidy fix 1.0
5009b33
clang-tidy fix 1.01
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
Member
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. why |
||
| 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 "") | ||
|
Comment on lines
+23
to
+30
Member
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 this needed? |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #ifndef GRAPH_H | ||
| #define GRAPH_H | ||
|
|
||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "./layer/layer.h" | ||
| #include "./tensor/tensor.h" | ||
|
|
||
| class Network { | ||
| 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: | ||
| Network(); | ||
|
|
||
| bool addLayer(Layer& lay, const std::vector<int>& inputs ={}, const std::vector<int>& outputs = {}); | ||
| void addEdge(Layer& layPrev, Layer& layNext); | ||
| void removeEdge(Layer& layPrev, Layer& layNext); | ||
| void removeLayer(Layer& lay); | ||
| int getLayers() const; | ||
| int getEdges() const; | ||
| bool isEmpty() const; | ||
| bool hasPath(Layer& layPrev, Layer& layNext) const; | ||
| std::vector<int> inference(int start) const; | ||
| void setInput(Layer& lay, Tensor<double>& vec); | ||
| void setOutput(Layer& lay, Tensor<double>& vec); | ||
| void run(); | ||
| std::vector<std::string> getLayersTypeVector() const; | ||
| ~Network(); | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #ifndef LAYER_H | ||
| #define LAYER_H | ||
|
|
||
| #include <list> | ||
|
|
||
| #include "./tensor/tensor.h" | ||
|
|
||
| struct LayerAttributes { | ||
| int id = -1; | ||
| }; | ||
|
|
||
| class Layer { | ||
| protected: | ||
| int id_; | ||
|
|
||
| public: | ||
| Layer() = default; | ||
| explicit Layer(const LayerAttributes& attrs) : id_(attrs.id) {} | ||
| virtual ~Layer() = default; | ||
| void setID(int id) { id_ = id; } | ||
| int getID() const { return id_; } | ||
| virtual std::string getInfoString() const; | ||
| virtual void exec(const Tensor<double>& input, Tensor<double>& output) = 0; | ||
| virtual Shape get_output_shape() = 0; | ||
|
|
||
| virtual std::string get_type_name() const = 0; | ||
| void addNeighbor(Layer* neighbor); | ||
| void removeNeighbor(Layer* neighbor); | ||
| std::list<Layer*> neighbors_; | ||
| }; | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #ifndef TENSOR_H | ||
| #define TENSOR_H | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| struct Shape { | ||
| std::vector<size_t> dimensions; | ||
| size_t total_elements; | ||
|
|
||
| Shape() : dimensions(), total_elements(0) {} | ||
| 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() : shape(), layout(Layout::kNd), 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]) { | ||
| std::string error_msg = "Index out of range for dimension "); | ||
| throw std::out_of_range(error_msg); | ||
| } | ||
| } | ||
|
|
||
| size_t linear_index = 0; | ||
| size_t N = shape.get_rank(); | ||
|
|
||
| if (N == 0) { | ||
| if (shape.total_elements == 1 && indices.empty()) | ||
| return 0; | ||
| if (shape.total_elements == 0 && indices.empty()) | ||
| return 0; | ||
| throw std::logic_error("Invalid access to rank-0 tensor or empty tensor."); | ||
| } | ||
|
|
||
| if (N == 4 && layout == Layout::kNhwc) { | ||
| if (shape.dimensions.size() != 4) { | ||
| throw std::logic_error( | ||
| "kNhwc layout is specified for a tensor not of rank 4."); | ||
| } | ||
|
|
||
| size_t C_dim = shape.dimensions[1]; | ||
| size_t H_dim = shape.dimensions[2]; | ||
| size_t W_dim = shape.dimensions[3]; | ||
|
|
||
| linear_index = indices[0] * (H_dim * W_dim * C_dim) + | ||
| indices[2] * (W_dim * C_dim) + indices[3] * (C_dim) + | ||
| indices[1]; | ||
| } | ||
| else | ||
| { | ||
| for (size_t i = 0; i < N; ++i) { | ||
| size_t term_stride = 1; | ||
| for (size_t j = i + 1; j < N; ++j) { | ||
| term_stride *= shape.dimensions[j]; | ||
| } | ||
| linear_index += indices[i] * term_stride; | ||
| } | ||
| } | ||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
C++11 is too low. If you need to specify the standard then it should be at least 17.