mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 09:44:28 +00:00
Format source code using clang-format
This commit is contained in:
@@ -1,14 +1,24 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <driver_types.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "activations.cuh"
|
||||
#include "dense.cuh"
|
||||
#include "gtest/gtest.h"
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
class DenseLayerTest : public CublasTestFixture {
|
||||
protected:
|
||||
Layers::Dense commonTestSetup(int inputSize, int outputSize, std::vector<float>& input, std::vector<std::vector<float>>& weights, std::vector<float>& biases, float*& d_input, float*& d_output) {
|
||||
protected:
|
||||
Layers::Dense commonTestSetup(
|
||||
int inputSize,
|
||||
int outputSize,
|
||||
std::vector<float>& input,
|
||||
std::vector<std::vector<float>>& weights,
|
||||
std::vector<float>& biases,
|
||||
float*& d_input,
|
||||
float*& d_output
|
||||
) {
|
||||
// Create Dense layer
|
||||
Layers::Dense denseLayer(inputSize, outputSize, "linear", cublasHandle);
|
||||
|
||||
@@ -24,7 +34,9 @@ protected:
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
// Copy input to device
|
||||
cublasStatus = cublasSetVector(input.size(), sizeof(float), input.data(), 1, d_input, 1);
|
||||
cublasStatus = cublasSetVector(
|
||||
input.size(), sizeof(float), input.data(), 1, d_input, 1
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
|
||||
return denseLayer;
|
||||
@@ -36,28 +48,27 @@ protected:
|
||||
cudaFree(d_output);
|
||||
}
|
||||
|
||||
cudaError_t cudaStatus;
|
||||
cudaError_t cudaStatus;
|
||||
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 inputSize = i;
|
||||
int outputSize = j;
|
||||
|
||||
// std::cout << "Dense layer: input size = " << inputSize << ", output size = " << outputSize << std::endl;
|
||||
Layers::Dense denseLayer(inputSize, outputSize, "linear", cublasHandle);
|
||||
}
|
||||
// std::cout << "Dense layer: input size = " << inputSize << ",
|
||||
// output size = " << outputSize << std::endl;
|
||||
Layers::Dense denseLayer(
|
||||
inputSize, outputSize, "linear", cublasHandle
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DenseLayerTest, setWeights) {
|
||||
|
||||
|
||||
int inputSize = 4;
|
||||
int inputSize = 4;
|
||||
int outputSize = 5;
|
||||
|
||||
std::vector<std::vector<float>> weights = {
|
||||
@@ -71,17 +82,17 @@ TEST_F(DenseLayerTest, setWeights) {
|
||||
Layers::Dense denseLayer(inputSize, outputSize, "linear", cublasHandle);
|
||||
|
||||
denseLayer.setWeights(weights);
|
||||
|
||||
}
|
||||
|
||||
TEST_F(DenseLayerTest, ForwardUnitWeightMatrix) {
|
||||
|
||||
int inputSize = 3;
|
||||
int inputSize = 3;
|
||||
int outputSize = 3;
|
||||
|
||||
std::vector<float> input = {1.0f, 2.0f, 3.0f};
|
||||
|
||||
std::vector<std::vector<float>> weights(inputSize, std::vector<float>(outputSize, 0.0f));
|
||||
std::vector<std::vector<float>> weights(
|
||||
inputSize, std::vector<float>(outputSize, 0.0f)
|
||||
);
|
||||
for (int i = 0; i < inputSize; ++i) {
|
||||
for (int j = 0; j < outputSize; ++j) {
|
||||
if (i == j) {
|
||||
@@ -94,11 +105,15 @@ TEST_F(DenseLayerTest, ForwardUnitWeightMatrix) {
|
||||
float* d_input;
|
||||
float* d_output;
|
||||
|
||||
Layers::Dense denseLayer = commonTestSetup(inputSize, outputSize, input, weights, biases, d_input, 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);
|
||||
cublasStatus = cublasGetVector(
|
||||
outputSize, sizeof(float), d_output, 1, output.data(), 1
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
|
||||
// Check if the output is a zero vector
|
||||
@@ -110,7 +125,7 @@ TEST_F(DenseLayerTest, ForwardUnitWeightMatrix) {
|
||||
}
|
||||
|
||||
TEST_F(DenseLayerTest, ForwardRandomWeightMatrix) {
|
||||
int inputSize = 5;
|
||||
int inputSize = 5;
|
||||
int outputSize = 4;
|
||||
|
||||
std::vector<float> input = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
|
||||
@@ -120,23 +135,29 @@ TEST_F(DenseLayerTest, ForwardRandomWeightMatrix) {
|
||||
{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};
|
||||
|
||||
float* d_input;
|
||||
float* d_output;
|
||||
float* d_output;
|
||||
|
||||
Layers::Dense denseLayer = commonTestSetup(
|
||||
inputSize, outputSize, input, weights, biases, d_input, 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);
|
||||
cublasStatus = cublasGetVector(
|
||||
outputSize, sizeof(float), d_output, 1, output.data(), 1
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
|
||||
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
|
||||
EXPECT_NEAR(
|
||||
output[i], expectedOutput[i], 1e-4
|
||||
); // Allow small tolerance for floating-point comparison
|
||||
}
|
||||
|
||||
commonTestTeardown(d_input, d_output);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "cublas_v2.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
cublasHandle_t CublasTestFixture::cublasHandle;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "cublas_v2.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class CublasTestFixture : public ::testing::Test {
|
||||
protected:
|
||||
protected:
|
||||
static cublasHandle_t cublasHandle;
|
||||
|
||||
static void SetUpTestSuite();
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include <cuda_runtime_api.h>
|
||||
#include <driver_types.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "functions.cuh"
|
||||
#include "gtest/gtest.h"
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
class FunctionsTest : public CublasTestFixture {
|
||||
protected:
|
||||
cudaError_t cudaStatus;
|
||||
protected:
|
||||
cudaError_t cudaStatus;
|
||||
cublasStatus_t cublasStatus;
|
||||
};
|
||||
|
||||
TEST_F(FunctionsTest, sigmoid) {
|
||||
|
||||
}
|
||||
TEST_F(FunctionsTest, sigmoid) {}
|
||||
Reference in New Issue
Block a user