mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Remove cublas dependency
This commit is contained in:
@@ -7,12 +7,6 @@ add_executable(test_main
|
||||
kernels/test_padding.cu
|
||||
)
|
||||
|
||||
add_library(test_utils
|
||||
test_utils/test_cublas_fixture.cu
|
||||
)
|
||||
|
||||
target_include_directories(test_utils PUBLIC test_utils)
|
||||
|
||||
target_link_libraries(test_main ${GTEST_BOTH_LIBRARIES} CUDANet test_utils)
|
||||
target_link_libraries(test_main ${GTEST_BOTH_LIBRARIES} CUDANet)
|
||||
|
||||
add_test(NAME TestMain COMMAND test_main)
|
||||
@@ -4,15 +4,11 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "activations.cuh"
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
class ActivationsTest : public CublasTestFixture {
|
||||
protected:
|
||||
cudaError_t cudaStatus;
|
||||
cublasStatus_t cublasStatus;
|
||||
};
|
||||
TEST(ActivationsTest, SigmoidSanityCheck) {
|
||||
|
||||
cudaError_t cudaStatus;
|
||||
|
||||
TEST_F(ActivationsTest, SigmoidSanityCheck) {
|
||||
float input[3] = {-100.0f, 0.0f, 100.0f};
|
||||
|
||||
std::vector<float> expected_output = {0.0f, 0.5f, 1.0f};
|
||||
@@ -26,8 +22,8 @@ TEST_F(ActivationsTest, SigmoidSanityCheck) {
|
||||
cudaStatus = cudaMalloc((void**)&d_output, sizeof(float) * 3);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
cublasStatus = cublasSetVector(3, sizeof(float), input, 1, d_input, 1);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
cudaStatus = cudaMemcpy(d_input, input, sizeof(float) * 3, cudaMemcpyHostToDevice);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
sigmoid_kernel<<<1, 3>>>(d_input, d_output, 3);
|
||||
cudaStatus = cudaDeviceSynchronize();
|
||||
@@ -35,9 +31,8 @@ TEST_F(ActivationsTest, SigmoidSanityCheck) {
|
||||
|
||||
std::vector<float> output(3);
|
||||
|
||||
cublasStatus =
|
||||
cublasGetVector(3, sizeof(float), d_output, 1, output.data(), 1);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
cudaStatus = cudaMemcpy(output.data(), d_output, sizeof(float) * 3, cudaMemcpyDeviceToHost);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
EXPECT_NEAR(expected_output[i], output[i], 1e-5);
|
||||
|
||||
@@ -4,15 +4,10 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "padding.cuh"
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
class PaddingTest : public CublasTestFixture {
|
||||
protected:
|
||||
cudaError_t cudaStatus;
|
||||
cublasStatus_t cublasStatus;
|
||||
};
|
||||
TEST(PaddingTest, SimplePaddingTest) {
|
||||
cudaError_t cudaStatus;
|
||||
|
||||
TEST_F(PaddingTest, SimplePaddingTest) {
|
||||
int w = 2;
|
||||
int h = 3;
|
||||
int n = 2;
|
||||
@@ -48,9 +43,10 @@ TEST_F(PaddingTest, SimplePaddingTest) {
|
||||
std::vector<float> input = {0.0f, 2.0f, 4.0f, 1.0f, 3.0f, 5.0f,
|
||||
6.0f, 8.0f, 10.0f, 7.0f, 9.0f, 11.0f};
|
||||
|
||||
cublasStatus =
|
||||
cublasSetVector(inputSize, sizeof(float), input.data(), 1, d_input, 1);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
cudaStatus = cudaMemcpy(
|
||||
d_input, input.data(), sizeof(float) * inputSize, cudaMemcpyHostToDevice
|
||||
);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
int THREADS_PER_BLOCK = 64;
|
||||
int BLOCKS = paddedSize / THREADS_PER_BLOCK + 1;
|
||||
@@ -69,9 +65,12 @@ TEST_F(PaddingTest, SimplePaddingTest) {
|
||||
};
|
||||
|
||||
std::vector<float> output(paddedSize);
|
||||
cublasStatus = cublasGetVector(
|
||||
paddedSize, sizeof(float), d_padded, 1, output.data(), 1
|
||||
|
||||
cudaStatus = cudaMemcpy(
|
||||
output.data(), d_padded, sizeof(float) * paddedSize,
|
||||
cudaMemcpyDeviceToHost
|
||||
);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
for (int i = 0; i < paddedSize; i++) {
|
||||
EXPECT_NEAR(expectedOutput[i], output[i], 1e-5);
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
#include "activations.cuh"
|
||||
#include "dense.cuh"
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
class DenseLayerTest : public CublasTestFixture {
|
||||
|
||||
class DenseLayerTest : public::testing::Test {
|
||||
protected:
|
||||
Layers::Dense commonTestSetup(
|
||||
int inputSize,
|
||||
@@ -21,7 +21,7 @@ class DenseLayerTest : public CublasTestFixture {
|
||||
) {
|
||||
// Create Dense layer
|
||||
Layers::Dense denseLayer(
|
||||
inputSize, outputSize, activation, cublasHandle
|
||||
inputSize, outputSize, activation
|
||||
);
|
||||
|
||||
// Set weights and biases
|
||||
@@ -36,10 +36,11 @@ class DenseLayerTest : public CublasTestFixture {
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
// Copy input to device
|
||||
cublasStatus = cublasSetVector(
|
||||
input.size(), sizeof(float), input.data(), 1, d_input, 1
|
||||
cudaStatus = cudaMemcpy(
|
||||
d_input, input.data(), sizeof(float) * input.size(), cudaMemcpyHostToDevice
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
|
||||
return denseLayer;
|
||||
}
|
||||
@@ -51,7 +52,6 @@ class DenseLayerTest : public CublasTestFixture {
|
||||
}
|
||||
|
||||
cudaError_t cudaStatus;
|
||||
cublasStatus_t cublasStatus;
|
||||
};
|
||||
|
||||
TEST_F(DenseLayerTest, Init) {
|
||||
@@ -60,10 +60,8 @@ TEST_F(DenseLayerTest, Init) {
|
||||
int inputSize = i;
|
||||
int outputSize = j;
|
||||
|
||||
// std::cout << "Dense layer: input size = " << inputSize << ",
|
||||
// output size = " << outputSize << std::endl;
|
||||
Layers::Dense denseLayer(
|
||||
inputSize, outputSize, SIGMOID, cublasHandle
|
||||
inputSize, outputSize, SIGMOID
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -81,7 +79,7 @@ TEST_F(DenseLayerTest, setWeights) {
|
||||
{1.3f, 0.5f, 0.0f, 1.7f}
|
||||
};
|
||||
|
||||
Layers::Dense denseLayer(inputSize, outputSize, SIGMOID, cublasHandle);
|
||||
Layers::Dense denseLayer(inputSize, outputSize, SIGMOID);
|
||||
|
||||
denseLayer.setWeights(weights);
|
||||
}
|
||||
@@ -113,10 +111,10 @@ TEST_F(DenseLayerTest, ForwardUnitWeightMatrixLinear) {
|
||||
denseLayer.forward(d_input, d_output);
|
||||
|
||||
std::vector<float> output(outputSize);
|
||||
cublasStatus = cublasGetVector(
|
||||
outputSize, sizeof(float), d_output, 1, output.data(), 1
|
||||
cudaStatus = cudaMemcpy(
|
||||
output.data(), d_output, sizeof(float) * outputSize, cudaMemcpyDeviceToHost
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
// Check if the output is a zero vector
|
||||
EXPECT_FLOAT_EQ(output[0], 2.0f);
|
||||
@@ -150,10 +148,10 @@ TEST_F(DenseLayerTest, ForwardRandomWeightMatrixRelu) {
|
||||
denseLayer.forward(d_input, d_output);
|
||||
|
||||
std::vector<float> output(outputSize);
|
||||
cublasStatus = cublasGetVector(
|
||||
outputSize, sizeof(float), d_output, 1, output.data(), 1
|
||||
cudaStatus = cudaMemcpy(
|
||||
output.data(), d_output, sizeof(float) * outputSize, cudaMemcpyDeviceToHost
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
// weights * inputs = 0.1, 12.5, 8.3, -2.2
|
||||
// + biases = 0.3, 13, 9, -3.3
|
||||
@@ -193,10 +191,10 @@ TEST_F(DenseLayerTest, ForwardRandomWeightMatrixSigmoid) {
|
||||
denseLayer.forward(d_input, d_output);
|
||||
|
||||
std::vector<float> output(outputSize);
|
||||
cublasStatus = cublasGetVector(
|
||||
outputSize, sizeof(float), d_output, 1, output.data(), 1
|
||||
cudaStatus = cudaMemcpy(
|
||||
output.data(), d_output, sizeof(float) * outputSize, cudaMemcpyDeviceToHost
|
||||
);
|
||||
EXPECT_EQ(cublasStatus, CUBLAS_STATUS_SUCCESS);
|
||||
EXPECT_EQ(cudaStatus, cudaSuccess);
|
||||
|
||||
// weights * input = 0.95, 0.43, 0.45, 0.93
|
||||
// + biases = 1.05, 0.63, 0.75, 1.33
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
#include <cublas_v2.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "test_cublas_fixture.cuh"
|
||||
|
||||
cublasHandle_t CublasTestFixture::cublasHandle;
|
||||
|
||||
void CublasTestFixture::SetUpTestSuite() {
|
||||
cublasCreate(&cublasHandle);
|
||||
}
|
||||
|
||||
void CublasTestFixture::TearDownTestSuite() {
|
||||
cublasDestroy(cublasHandle);
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#include <cublas_v2.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class CublasTestFixture : public ::testing::Test {
|
||||
protected:
|
||||
static cublasHandle_t cublasHandle;
|
||||
|
||||
static void SetUpTestSuite();
|
||||
static void TearDownTestSuite();
|
||||
};
|
||||
Reference in New Issue
Block a user