generated from embedded-dev-research/cpp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Tensor class #32
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
32
commits into
embedded-dev-research:main
Choose a base branch
from
Lepsps:tensor-class
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
Tensor class #32
Changes from 28 commits
Commits
Show all changes
32 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
7b1d6bc
graph fix
2c2f81a
tensor fix
6d5d4a9
clang-tidy and install dependencies
213e4c7
install dependencies fix
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
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) | ||
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 "") |
Empty file.
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,43 @@ | ||
#ifndef GRAPH_H | ||
#define GRAPH_H | ||
|
||
#include <list> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
class Vertex { | ||
private: | ||
int id_; | ||
std::list<int> neighbors_; | ||
|
||
public: | ||
Vertex(int id_); | ||
void addNeighbor(int neighbor); | ||
void removeNeighbor(int neighbor); | ||
void print() const; | ||
int getId() const; | ||
const std::list<int>& getNeighbors() const; | ||
}; | ||
|
||
class Graph { | ||
private: | ||
std::unordered_map<int, Vertex*> vertices_; | ||
|
||
public: | ||
Graph(); | ||
void addVertex(int id_); | ||
void getVertex() const; | ||
void addEdge(int u, int v); | ||
void removeEdge(int u, int v); | ||
void removeVertex(int id_); | ||
int vertexCount() const; | ||
int edgeCount() const; | ||
bool empty() const; | ||
void printGraph() const; | ||
bool bfs_helper(int start, int vert, bool flag, std::vector<int>* v_ord); | ||
bool hasPath(int u, int v); | ||
std::vector<int> BFS(int start); | ||
~Graph(); | ||
}; | ||
|
||
#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,35 @@ | ||
#ifndef TENSOR_H | ||
#define TENSOR_H | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#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; | ||
}; | ||
|
||
#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) |
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,141 @@ | ||
#include "./graph/graph.h" | ||
|
||
#include <iostream> | ||
#include <list> | ||
#include <queue> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
Vertex::Vertex(int id_) : id_(id_) {} | ||
|
||
void Vertex::addNeighbor(int neighbor) { | ||
if (neighbor != id_) { | ||
neighbors_.push_back(neighbor); | ||
} | ||
} | ||
|
||
void Vertex::removeNeighbor(int neighbor) { neighbors_.remove(neighbor); } | ||
|
||
void Vertex::print() const { | ||
std::cout << id_ << ": "; | ||
for (const int& neighbor : neighbors_) { | ||
std::cout << neighbor << " "; | ||
} | ||
std::cout << '\n'; | ||
} | ||
|
||
int Vertex::getId() const { return id_; } | ||
|
||
const std::list<int>& Vertex::getNeighbors() const { return neighbors_; } | ||
|
||
Graph::Graph() = default; | ||
|
||
void Graph::addVertex(int id_) { | ||
if (vertices_.find(id_) == vertices_.end()) { | ||
vertices_[id_] = new Vertex(id_); | ||
} | ||
} | ||
|
||
void Graph::getVertex() const { | ||
if (!this->empty()) { | ||
for (const auto& vertice : vertices_) { | ||
std::cout << vertice.first << " "; | ||
} | ||
std::cout << '\n'; | ||
} | ||
} | ||
|
||
void Graph::addEdge(int u, int v) { | ||
if (vertices_.find(u) == vertices_.end()) { | ||
addVertex(u); | ||
} | ||
if (vertices_.find(v) == vertices_.end()) { | ||
addVertex(v); | ||
} | ||
vertices_[u]->addNeighbor(v); | ||
} | ||
|
||
void Graph::removeEdge(int u, int v) { | ||
if (vertices_.find(u) != vertices_.end()) { | ||
vertices_[u]->removeNeighbor(v); | ||
} | ||
} | ||
|
||
void Graph::removeVertex(int id_) { | ||
for (auto& pair : vertices_) { | ||
pair.second->removeNeighbor(id_); | ||
} | ||
auto it = vertices_.find(id_); | ||
if (it != vertices_.end()) { | ||
delete it->second; | ||
vertices_.erase(it); | ||
} | ||
} | ||
|
||
int Graph::vertexCount() const { return static_cast<int>(vertices_.size()); } | ||
|
||
int Graph::edgeCount() const { | ||
int count = 0; | ||
for (const auto& vertice : vertices_) { | ||
count += (vertice.second->getNeighbors()).size(); | ||
} | ||
return count; | ||
} | ||
|
||
bool Graph::empty() const { return vertices_.empty(); } | ||
|
||
void Graph::printGraph() const { | ||
for (const auto& pair : vertices_) { | ||
pair.second->print(); | ||
} | ||
} | ||
|
||
bool Graph::bfs_helper(int start, int vert, bool flag, | ||
std::vector<int>* v_ord) { | ||
std::unordered_map<int, bool> visited; | ||
std::queue<int> queue; | ||
queue.push(start); | ||
visited[start] = true; | ||
|
||
while (!queue.empty()) { | ||
int current = queue.front(); | ||
queue.pop(); | ||
|
||
if (flag && current == vert) { | ||
return true; | ||
} | ||
if (v_ord != nullptr) { | ||
v_ord->push_back(current); | ||
} | ||
|
||
if (vertices_.find(current) != vertices_.end()) { | ||
for (const int& neighbor : vertices_[current]->getNeighbors()) { | ||
if (!visited[neighbor]) { | ||
visited[neighbor] = true; | ||
queue.push(neighbor); | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool Graph::hasPath(int u, int v) { | ||
if (vertices_.find(u) == vertices_.end() || | ||
vertices_.find(v) == vertices_.end()) { | ||
return false; | ||
} | ||
return bfs_helper(u, v, true, nullptr); | ||
} | ||
|
||
std::vector<int> Graph::BFS(int start) { | ||
std::vector<int> v_ord; | ||
bfs_helper(start, -1, false, &v_ord); | ||
return v_ord; | ||
} | ||
|
||
Graph::~Graph() { | ||
for (auto& pair : vertices_) { | ||
delete pair.second; | ||
} | ||
} |
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,84 @@ | ||
#include "./tensor/tensor.h" | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <iostream> | ||
#include <numeric> | ||
#include <stdexcept> | ||
#include <utility> | ||
#include <vector> | ||
|
||
Shape::Shape(std::vector<size_t> dims) : dimensions(std::move(dims)) { | ||
total_elements = std::accumulate(dimensions.begin(), dimensions.end(), | ||
static_cast<size_t>(1), | ||
[](size_t a, size_t b) { return a * b; }); | ||
} | ||
|
||
size_t Shape::get_rank() const { return dimensions.size(); } | ||
|
||
template <typename T> | ||
Tensor<T>::Tensor(const Shape &sh, Layout l) | ||
: shape(sh), layout(l), data(sh.total_elements) { | ||
if (sh.get_rank() == 4 && l == Layout::kNd) { | ||
std::cerr << "4D Tensor created with kNd layout." << '\n'; | ||
} | ||
} | ||
|
||
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)]; | ||
} | ||
|
||
template class Tensor<double>; |
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,6 +1,11 @@ | ||
file(GLOB_RECURSE TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) | ||
|
||
add_executable(run_tests ${TEST_SRC_FILES}) | ||
target_link_libraries(run_tests PUBLIC | ||
gtest_main | ||
) | ||
file(GLOB_RECURSE TEST_FILES ./*.cpp) | ||
|
||
set(TestsName "run_test") | ||
|
||
add_executable(${TestsName} ${TEST_FILES}) | ||
|
||
target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest) | ||
|
||
enable_testing() | ||
add_test(NAME ${TestsName} COMMAND ${TestsName}) |
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.
Please, move template implementation to the header, otherwise it will not work for other data types specified by the user