mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Rename files to .cu and fix IDX2C usage
This commit is contained in:
@@ -9,8 +9,8 @@ include_directories(${CUDAToolkit_INCLUDE_DIRS})
|
||||
|
||||
# Add project source files for the library
|
||||
set(LIBRARY_SOURCES
|
||||
src/utils/cuda_helper.cpp
|
||||
src/layers/dense.cpp
|
||||
src/utils/cuda_helper.cu
|
||||
src/layers/dense.cu
|
||||
)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75)
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
// fully_connected_layer.h
|
||||
|
||||
#ifndef DENSE_LAYER_H
|
||||
#define DENSE_LAYER_H
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ do { \
|
||||
cublasStatus_t result = call; \
|
||||
if (result != CUBLAS_STATUS_SUCCESS) { \
|
||||
fprintf(stderr, "cuBLAS error at %s:%d code=%d\n", \
|
||||
__FILE__, __LINE__, static_cast<int>(result)); \
|
||||
__FILE__, __LINE__, static_cast<unsigned int>(result)); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
set(LAYER_SOURCES layers/dense.cpp)
|
||||
set(LAYER_SOURCES layers/dense.cu)
|
||||
|
||||
add_library(CUDANet
|
||||
utils/cuda_helper.cpp
|
||||
utils/cuda_helper.cu
|
||||
${LAYER_SOURCES}
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cuda_runtime.h>
|
||||
#include <cublas_v2.h>
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
|
||||
Layers::Dense::Dense(int inputSize, int outputSize, cublasHandle_t cublasHandle)
|
||||
: inputSize(inputSize), outputSize(outputSize), cublasHandle(cublasHandle) {
|
||||
@@ -16,9 +16,12 @@ Layers::Dense::Dense(int inputSize, int outputSize, cublasHandle_t cublasHandle)
|
||||
initializeWeights();
|
||||
initializeBiases();
|
||||
|
||||
d_weights = nullptr;
|
||||
d_biases = nullptr;
|
||||
|
||||
// Allocate GPU memory for weights and biases
|
||||
CUDA_CHECK(cudaMalloc((void**)&d_weights, sizeof(float) * inputSize * outputSize));
|
||||
CUDA_CHECK(cudaMalloc((void**)&d_biases, sizeof(float) * biases.size()));
|
||||
CUDA_CHECK(cudaMalloc((void**)&d_biases, sizeof(float) * outputSize));
|
||||
|
||||
toCuda();
|
||||
}
|
||||
@@ -30,43 +33,43 @@ Layers::Dense::~Dense() {
|
||||
}
|
||||
|
||||
void Layers::Dense::initializeWeights() {
|
||||
int numWeights = inputSize * outputSize;
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::normal_distribution<float> dist(0.0f, 0.01f); // Xavier initialization
|
||||
|
||||
for (int i = 0; i < outputSize; ++i) {
|
||||
for (int j = 0; j < inputSize; ++j) {
|
||||
int idx = IDX2C(i, j, inputSize);
|
||||
weights[idx] = dist(gen);
|
||||
for (int j = 0; j < inputSize; ++j) {
|
||||
for (int i = 0; i < outputSize; ++i) {
|
||||
int idx = IDX2C(i, j, outputSize);
|
||||
weights[idx] = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Layers::Dense::initializeBiases() {
|
||||
std::fill(biases.begin(), biases.end(), 0.1f);
|
||||
std::fill(biases.begin(), biases.end(), 0.0f);
|
||||
}
|
||||
|
||||
void Layers::Dense::forward(const float* d_input, float* d_output) {
|
||||
const float alpha = 1.0f;
|
||||
const float beta = 1.0f;
|
||||
|
||||
cublasSgemv(cublasHandle, CUBLAS_OP_N, inputSize, outputSize, &alpha, d_weights, inputSize, d_input, 1, &beta, d_output, 1);
|
||||
cublasSaxpy(cublasHandle, outputSize, &alpha, d_biases, 1, d_output, 1);
|
||||
CUBLAS_CHECK(cublasSgemv(cublasHandle, CUBLAS_OP_N, inputSize, outputSize, &alpha, d_weights, inputSize, d_input, 1, &beta, d_output, 1));
|
||||
CUBLAS_CHECK(cublasSaxpy(cublasHandle, outputSize, &alpha, d_biases, 1, d_output, 1));
|
||||
}
|
||||
|
||||
void Layers::Dense::toCuda() {
|
||||
CUBLAS_CHECK(cublasSetMatrix(outputSize, inputSize, sizeof(float), weights.data(), inputSize, d_weights, outputSize));
|
||||
CUBLAS_CHECK(cublasSetMatrix(outputSize, inputSize, sizeof(float), weights.data(), outputSize, d_weights, outputSize));
|
||||
CUBLAS_CHECK(cublasSetVector(biases.size(), sizeof(float), biases.data(), 1, d_biases, 1));
|
||||
}
|
||||
|
||||
void Layers::Dense::setWeights(const std::vector<std::vector<float>>& weights_input) {
|
||||
int numWeights = inputSize * outputSize;
|
||||
|
||||
for (int i = 0; i < outputSize; ++i) {
|
||||
for (int j = 0; j < inputSize; ++j) {
|
||||
int idx = IDX2C(i, j, inputSize);
|
||||
if (weights.size() != numWeights) {
|
||||
std::cerr << "Invalid number of weights" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int j = 0; j < inputSize; ++j) {
|
||||
for (int i = 0; i < outputSize; ++i) {
|
||||
int idx = IDX2C(i, j, outputSize);
|
||||
weights[idx] = weights_input[i][j];
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
find_package(GTest REQUIRED)
|
||||
include_directories(${GTEST_INCLUDE_DIRS})
|
||||
|
||||
add_executable(test_dense layers/test_dense.cpp)
|
||||
add_executable(test_dense layers/test_dense.cu)
|
||||
|
||||
add_library(test_utils
|
||||
test_utils/test_cublas_fixture.cpp
|
||||
test_utils/test_cublas_fixture.cu
|
||||
)
|
||||
|
||||
target_include_directories(test_utils PUBLIC test_utils)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <driver_types.h>
|
||||
#include <iostream>
|
||||
#include "dense.h"
|
||||
#include "test_cublas_fixture.h"
|
||||
|
||||
@@ -38,6 +39,40 @@ protected:
|
||||
cublasStatus_t cublasStatus;
|
||||
};
|
||||
|
||||
TEST_F(DenseLayerTest, Init) {
|
||||
|
||||
for (int i = 1; i < 100; ++i) {
|
||||
for (int j = 1; j < 100; ++j) {
|
||||
|
||||
int inputSize = i;
|
||||
int outputSize = j;
|
||||
|
||||
// std::cout << "Dense layer: input size = " << inputSize << ", output size = " << outputSize << std::endl;
|
||||
Layers::Dense denseLayer(inputSize, outputSize, cublasHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DenseLayerTest, setWeights) {
|
||||
|
||||
|
||||
int inputSize = 4;
|
||||
int outputSize = 5;
|
||||
|
||||
std::vector<std::vector<float>> weights = {
|
||||
{0.5f, 1.0f, 0.2f, 0.8f},
|
||||
{1.2f, 0.3f, 1.5f, 0.4f},
|
||||
{0.7f, 1.8f, 0.9f, 0.1f},
|
||||
{0.4f, 2.0f, 0.6f, 1.1f},
|
||||
{1.3f, 0.5f, 0.0f, 1.7f}
|
||||
};
|
||||
|
||||
Layers::Dense denseLayer(inputSize, outputSize, cublasHandle);
|
||||
|
||||
denseLayer.setWeights(weights);
|
||||
|
||||
}
|
||||
|
||||
TEST_F(DenseLayerTest, ForwardUnitWeightMatrix) {
|
||||
|
||||
int inputSize = 3;
|
||||
@@ -80,11 +115,10 @@ TEST_F(DenseLayerTest, ForwardRandomWeightMatrix) {
|
||||
std::vector<float> input = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
|
||||
|
||||
std::vector<std::vector<float>> weights = {
|
||||
{0.5f, 1.0f, 0.2f, 0.8f},
|
||||
{1.2f, 0.3f, 1.5f, 0.4f},
|
||||
{0.7f, 1.8f, 0.9f, 0.1f},
|
||||
{0.4f, 2.0f, 0.6f, 1.1f},
|
||||
{1.3f, 0.5f, 0.0f, 1.7f}
|
||||
{0.5f, 1.2f, 0.7f, 0.4f, 1.3f},
|
||||
{1.0f, 0.3f, 1.8f, 2.0f, 0.5f},
|
||||
{0.2f, 1.5f, 0.9f, 0.6f, 0.0f},
|
||||
{0.8f, 0.4f, 0.1f, 1.1f, 1.7f}
|
||||
};
|
||||
std::vector<float> biases = {0.2f, 0.5f, 0.7f, 1.1f};
|
||||
|
||||
@@ -92,13 +126,14 @@ TEST_F(DenseLayerTest, ForwardRandomWeightMatrix) {
|
||||
float* d_output;
|
||||
|
||||
Layers::Dense denseLayer = commonTestSetup(inputSize, outputSize, input, weights, biases, d_input, d_output);
|
||||
|
||||
denseLayer.forward(d_input, d_output);
|
||||
|
||||
std::vector<float> output(outputSize);
|
||||
cublasStatus = cublasGetVector(outputSize, sizeof(float), d_output, 1, output.data(), 1);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
|
||||
std::vector<float> expectedOutput = {3.4f, 4.4f, 5.6f, 7.4f};
|
||||
std::vector<float> expectedOutput = {10.4f, 13.0f, 8.9f, 9.3f};
|
||||
for (int i = 0; i < outputSize; ++i) {
|
||||
EXPECT_NEAR(output[i], expectedOutput[i], 1e-4); // Allow small tolerance for floating-point comparison
|
||||
}
|
||||
Reference in New Issue
Block a user