Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
84a7b8b
first commit
dredstone1 Aug 5, 2025
313c342
new commit
dredstone1 Aug 5, 2025
2294ccd
new commit
dredstone1 Aug 5, 2025
590e9cc
new commit
dredstone1 Aug 5, 2025
b1a9c53
new commit
dredstone1 Aug 5, 2025
6aecfb5
new commit
dredstone1 Aug 5, 2025
2d8637c
new commit
dredstone1 Aug 5, 2025
228b6d6
new commit
dredstone1 Aug 6, 2025
7c96a3c
new commit
dredstone1 Aug 6, 2025
038136f
new commit
dredstone1 Aug 6, 2025
2785823
new commit
dredstone1 Aug 6, 2025
d50e875
bug fix
dredstone1 Aug 6, 2025
5c37bfb
bug fixes
dredstone1 Aug 6, 2025
882810c
new commit
dredstone1 Aug 6, 2025
4c83f40
more bug fixes
dredstone1 Aug 6, 2025
8062331
bug fix
dredstone1 Aug 6, 2025
413770b
new commit
dredstone1 Aug 6, 2025
efe75cb
bug fixes, i simplify the data structure
dredstone1 Aug 6, 2025
ace20ca
bug fixes
dredstone1 Aug 7, 2025
26c607a
bug fix
dredstone1 Aug 7, 2025
a157c5e
new commit
dredstone1 Aug 7, 2025
696092a
performance improvment
dredstone1 Aug 7, 2025
2f64b7e
small change
dredstone1 Aug 7, 2025
7890c8f
bug fix, improve performance
dredstone1 Aug 8, 2025
d313393
improved performance
dredstone1 Aug 8, 2025
0ef2270
small change
dredstone1 Aug 8, 2025
0857251
improved performance
dredstone1 Aug 8, 2025
c5530d1
Update cmake-multi-platform.yml for cuda
dredstone1 Aug 8, 2025
7f8d49c
small change - restoring
dredstone1 Aug 8, 2025
fd16a3b
improved performance
dredstone1 Aug 8, 2025
09174e4
small changes
dredstone1 Aug 8, 2025
a3825f8
small formating changes
dredstone1 Aug 8, 2025
586dc3f
Update cmake-multi-platform.yml
dredstone1 Aug 8, 2025
4ddf94d
improved code
dredstone1 Aug 9, 2025
369b2ab
bug fixes
dredstone1 Aug 9, 2025
c271d62
now it is possible to show only status on visual
dredstone1 Aug 9, 2025
60a4cb1
small bug fix
dredstone1 Aug 9, 2025
0c6228e
gpu mode is fully work now!!!
dredstone1 Aug 9, 2025
4f77d9e
bug fixes
dredstone1 Aug 9, 2025
c2b92e7
now it is possible to change to gpu mode from the user code
dredstone1 Aug 9, 2025
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
8 changes: 8 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ jobs:
libxi-dev \
libfreetype6-dev

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-ubuntu2404.pin
sudo mv cuda-ubuntu2404.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/13.0.0/local_installers/cuda-repo-ubuntu2404-13-0-local_13.0.0-580.65.06-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2404-13-0-local_13.0.0-580.65.06-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2404-13-0-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get -y install cuda-toolkit-13-0

- name: Checkout code
uses: actions/checkout@v4

Expand All @@ -36,3 +43,4 @@ jobs:

- name: Build
run: cmake --build build --config Release

31 changes: 17 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
cmake_minimum_required(VERSION 3.28)
project(NeuralNetwork LANGUAGES CXX)
set(CMAKE_CUDA_ARCHITECTURES 86) # For RTX 3060
project(NeuralNetwork LANGUAGES CXX CUDA) # Add CUDA here

# ------------------------------------------------------------------
# Configuration
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_STANDARD 17) # Add CUDA standard
set(CMAKE_CUDA_STANDARD_REQUIRED ON) # Enforce it
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

enable_language(CUDA)

# Default to Debug build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
Expand Down Expand Up @@ -39,22 +44,23 @@ FetchContent_Declare(nlohmann_json

FetchContent_MakeAvailable(SFML nlohmann_json)

# ------------------------------------------------------------------
# Function: Apply sanitizers
function(apply_sanitizers target)
target_compile_options(${target} PRIVATE -fsanitize=address -fno-omit-frame-pointer -g)
target_link_libraries(${target} PRIVATE -fsanitize=address)
endfunction()

# ------------------------------------------------------------------
# Main library

# Add both C++ and CUDA sources
file(GLOB_RECURSE NN_SOURCES CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu" # Include CUDA source files
)

add_library(NeuralNetwork STATIC ${NN_SOURCES})
set_target_properties(NeuralNetwork PROPERTIES POSITION_INDEPENDENT_CODE ON)

# Enable separable compilation for CUDA files
set_target_properties(NeuralNetwork PROPERTIES
CUDA_SEPARABLE_COMPILATION ON
)

target_include_directories(NeuralNetwork
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand All @@ -71,21 +77,19 @@ target_link_libraries(NeuralNetwork
SFML::Window
SFML::System
nlohmann_json::nlohmann_json
cuda
)

target_compile_options(NeuralNetwork PRIVATE -Wall -Wextra -Wpedantic)

# ------------------------------------------------------------------
# Tests (with sanitizers)
# Tests
option(BUILD_NN_TESTS "Build NeuralNetwork tests" OFF)

if(BUILD_NN_TESTS)
enable_testing()
include(CTest)

# Apply sanitizers only for test builds
apply_sanitizers(NeuralNetwork)

file(GLOB TEST_SOURCES CONFIGURE_DEPENDS tests/*.cpp)

if(TEST_SOURCES)
Expand All @@ -96,8 +100,6 @@ if(BUILD_NN_TESTS)
target_link_libraries(${test_name} PRIVATE NeuralNetwork)
target_include_directories(${test_name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")

apply_sanitizers(${test_name})

add_test(NAME ${test_name} COMMAND ${test_name})
endforeach()
else()
Expand All @@ -109,3 +111,4 @@ endif()
# Install
install(TARGETS NeuralNetwork ARCHIVE DESTINATION lib)
install(DIRECTORY include/ DESTINATION include)

34 changes: 20 additions & 14 deletions include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "../src/model/dataBase.hpp"
#include "../src/model/optimizers.hpp"
#include "../src/visualizer/VisualizerController.hpp"
#include "Globals.hpp"
#include "tensor.hpp"
#include <cstddef>
#include <network/INetwork.hpp>

namespace nn::visualizer {
Expand Down Expand Up @@ -65,7 +68,7 @@ class Model {
global::ValueType runBackPropagation(
const Batch &batch,
const bool updateWeights,
global::Transformation transformation = dt);
global::Transformation transformation = nullptr);

void printTrainingResult(
const std::chrono::high_resolution_clock::time_point &start,
Expand All @@ -81,12 +84,12 @@ class Model {
DataBase &dataBase,
const bool cancleOnError = false,
const bool showProgressbar = true,
global::Transformation transformation = dt);
global::Transformation transformation = nullptr);
void trainModel(
DataBase &trainedDataBase,
DataBase &evaluateDataBase,
global::Transformation transformationB = dt,
global::Transformation transformationE = dt);
global::Transformation transformationB = nullptr,
global::Transformation transformationE = nullptr);

size_t outputSize() const;
size_t inputSize() const;
Expand All @@ -103,10 +106,13 @@ class Model {

void autoSave(const int i);

void addFNN(const std::uint32_t width, ISubNetworkConfig &_config);
void addCNN(const std::uint32_t width, ISubNetworkConfig &_config);
void addFNN(const std::uint32_t width, ISubNetworkConfig &_config);
void addCNN(const std::uint32_t width, ISubNetworkConfig &_config);

std::uint32_t calculateSubNetWidth() const;
std::uint32_t calculateSubNetWidth() const;

void runModel(const global::Tensor &input,
global::Transformation transformation);

public:
Model(const std::string &config_filepath);
Expand All @@ -115,19 +121,19 @@ class Model {
void runModel(const global::Tensor &input);
void train(
const std::string &db_filename,
global::Transformation transformationB = dt,
global::Transformation transformationE = dt);
global::Transformation transformationB = nullptr,
global::Transformation transformationE = nullptr);
void train(
const std::vector<std::string> &db_filename,
global::Transformation transformationB = dt,
global::Transformation transformationE = dt);
global::Transformation transformationB = nullptr,
global::Transformation transformationE = nullptr);
modelResult evaluateModel(
const std::string &db_filename,
const bool cancleOnError = false,
global::Transformation transformation = dt);
global::Transformation transformation = nullptr);

void save(const std::string &file);
void load(const std::string &file);
void save(const std::string &file, bool print = true);
void load(const std::string &file, bool print = true);

global::Prediction getPrediction() const;
};
Expand Down
2 changes: 1 addition & 1 deletion include/network/INetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class INetwork {
virtual ~INetwork() = default;

virtual void forward(const global::Tensor &input) = 0;
virtual void backward(const global::Tensor &outputDeltas) = 0;
virtual void backward(global::Tensor **outputDeltas) = 0;
virtual void updateWeights(IOptimizer &optimizer) = 0;
virtual void resetGradient() = 0;

Expand Down
3 changes: 1 addition & 2 deletions include/network/IvisualNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

#include "../../src/visualizer/panel.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
#include <Globals.hpp>

namespace nn::visualizer {
constexpr std::uint32_t MODEL_HEIGHT = 770u;
Expand Down
74 changes: 37 additions & 37 deletions include/tensor.hpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
#ifndef TENSOR
#define TENSOR

#include <cstddef>
#include "../src/model/tensor_gpu.hpp"
#include <vector>

namespace nn::model {
class Activation;
void enableGpuMode();
} // namespace nn::model

namespace nn::global {
using ValueType = float;

class Tensor {
private:
std::vector<ValueType> data;
std::vector<ValueType> cpu_data;
std::vector<size_t> shape;
std::vector<size_t> strides;

ValueType *gpu_data = nullptr;
std::size_t gpu_data_size;

static bool isGpu;
static size_t tensorCount;

void computeStrides();
inline size_t flattenIndex(const std::vector<size_t> &indices) const;

friend model::Activation;

public:
// Constructors
Tensor(const std::vector<size_t> &shape, float init = 0.0f);
Tensor(const Tensor &other)
: data(other.data),
shape(other.shape),
strides(other.strides) {}
Tensor(const std::vector<size_t> &shape, ValueType init = 0.0f);
Tensor(const Tensor &other);

Tensor &operator=(const Tensor &other);
~Tensor();

// Element access
ValueType &operator()(const std::vector<size_t> &indices);
ValueType operator()(const std::vector<size_t> &indices) const;
inline ValueType &operator[](size_t i) { return data[i]; }
inline const ValueType &operator[](size_t i) const { return data[i]; }
Tensor &operator=(const Tensor &other);
Tensor &operator=(const std::vector<ValueType> &other);

// Iterators (for range-based loops)
auto begin() noexcept { return data.begin(); }
auto end() noexcept { return data.end(); }
auto begin() const noexcept { return data.begin(); }
auto end() const noexcept { return data.end(); }
ValueType getValue(const std::vector<size_t> &newShape) const;
void setValue(const std::vector<size_t> &newShape, const ValueType value);
void insertRange(const Tensor &other, const size_t startO,
const size_t startT, const size_t length);

// Shape and size
inline const std::vector<size_t> &getShape() const { return shape; }
inline size_t numElements() const { return data.size(); }
inline const std::vector<ValueType> &getData() const { return data; }
inline void fill(const ValueType &value) { std::fill(begin(), end(), value); }

// Arithmetic operations
Tensor operator+(const Tensor &other) const;
Tensor operator*(const Tensor &other) const;
Tensor operator-(const Tensor &other) const;
Tensor operator/(const Tensor &other) const;

Tensor operator*(ValueType scalar) const;
Tensor operator+(ValueType scalar) const;
Tensor operator/(ValueType scalar) const;
Tensor operator-(ValueType scalar) const;
size_t numElements() const;
const std::vector<size_t> &getShape() const { return shape; }
const std::vector<size_t> &getStrides() const { return strides; }
void getData(std::vector<ValueType> &dest) const;
void fill(const ValueType &value);
void zero();

Tensor &operator+=(const Tensor &other);
Tensor &operator-=(const Tensor &other);
Expand All @@ -65,10 +61,14 @@ class Tensor {
Tensor &operator+=(ValueType scalar);
Tensor &operator-=(ValueType scalar);

Tensor matmul(const Tensor &other) const;
static Tensor outer(const Tensor &a, const Tensor &b);
Tensor matmulT(const Tensor &vec) const;
void matmul(const Tensor &other, Tensor &result) const;
static void outer(const Tensor &a, const Tensor &b, Tensor &result);
void matmulT(const Tensor &vec, Tensor &result) const;

static void toGpu();
static void toCpu();
};

} // namespace nn::global

#endif // TENSOR
Loading
Loading