mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Initialize conv2d layer
This commit is contained in:
@@ -13,6 +13,7 @@ set(LIBRARY_SOURCES
|
|||||||
src/kernels/activations.cu
|
src/kernels/activations.cu
|
||||||
src/kernels/padding.cu
|
src/kernels/padding.cu
|
||||||
src/layers/dense.cu
|
src/layers/dense.cu
|
||||||
|
src/layers/conv2d.cu
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CMAKE_CUDA_ARCHITECTURES 75)
|
set(CMAKE_CUDA_ARCHITECTURES 75)
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
#ifndef CONV_LAYER_H
|
|
||||||
#define CONV_LAYER_H
|
|
||||||
|
|
||||||
#include <cublas_v2.h>
|
|
||||||
|
|
||||||
namespace Layers {
|
|
||||||
|
|
||||||
class Conv {
|
|
||||||
public:
|
|
||||||
Conv(
|
|
||||||
int inputSize,
|
|
||||||
int outputSize,
|
|
||||||
int kernelSize,
|
|
||||||
cublasHandle_t cublasHandle
|
|
||||||
);
|
|
||||||
~Conv();
|
|
||||||
|
|
||||||
void forward(const float* input, float* output);
|
|
||||||
|
|
||||||
private:
|
|
||||||
int inputSize;
|
|
||||||
int outputSize;
|
|
||||||
int kernelSize;
|
|
||||||
cublasHandle_t cublasHandle;
|
|
||||||
float* d_weights;
|
|
||||||
float* d_biases;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Layers
|
|
||||||
|
|
||||||
#endif // CONV_LAYER_H
|
|
||||||
60
include/layers/conv2d.cuh
Normal file
60
include/layers/conv2d.cuh
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef CONV_LAYER_H
|
||||||
|
#define CONV_LAYER_H
|
||||||
|
|
||||||
|
#include <cublas_v2.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "activations.cuh"
|
||||||
|
|
||||||
|
namespace Layers {
|
||||||
|
|
||||||
|
class Conv2d {
|
||||||
|
public:
|
||||||
|
Conv2d(
|
||||||
|
int inputSize,
|
||||||
|
int inputChannels,
|
||||||
|
int kernelSize,
|
||||||
|
int stride,
|
||||||
|
std::string padding,
|
||||||
|
int numFilters,
|
||||||
|
Activation activation,
|
||||||
|
cublasHandle_t cublasHandle
|
||||||
|
);
|
||||||
|
~Conv2d();
|
||||||
|
|
||||||
|
void forward(const float* d_input, float* d_output);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Inputs
|
||||||
|
int inputSize;
|
||||||
|
int inputChannels;
|
||||||
|
|
||||||
|
// Kernel
|
||||||
|
int kernelSize;
|
||||||
|
int stride;
|
||||||
|
int paddingSize;
|
||||||
|
int numFilters;
|
||||||
|
|
||||||
|
// Outputs
|
||||||
|
int outputSize;
|
||||||
|
|
||||||
|
// Kernels
|
||||||
|
std::vector<float> kernels;
|
||||||
|
|
||||||
|
// Cuda
|
||||||
|
cublasHandle_t cublasHandle;
|
||||||
|
float* d_kernels;
|
||||||
|
float* d_padded;
|
||||||
|
|
||||||
|
// Kernels
|
||||||
|
Activation activation;
|
||||||
|
|
||||||
|
void initializeKernels();
|
||||||
|
void toCuda();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Layers
|
||||||
|
|
||||||
|
#endif // CONV_LAYER_H
|
||||||
@@ -21,7 +21,7 @@ class Dense : public ILayer {
|
|||||||
);
|
);
|
||||||
~Dense();
|
~Dense();
|
||||||
|
|
||||||
void forward(const float* input, float* output);
|
void forward(const float* d_input, float* d_output);
|
||||||
void setWeights(const std::vector<std::vector<float>>& weights);
|
void setWeights(const std::vector<std::vector<float>>& weights);
|
||||||
void setBiases(const std::vector<float>& biases);
|
void setBiases(const std::vector<float>& biases);
|
||||||
|
|
||||||
|
|||||||
86
src/layers/conv2d.cu
Normal file
86
src/layers/conv2d.cu
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#include <cublas_v2.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "activations.cuh"
|
||||||
|
#include "conv2d.cuh"
|
||||||
|
#include "cuda_helper.cuh"
|
||||||
|
#include "padding.cuh"
|
||||||
|
|
||||||
|
Layers::Conv2d::Conv2d(
|
||||||
|
int inputSize,
|
||||||
|
int inputChannels,
|
||||||
|
int kernelSize,
|
||||||
|
int stride,
|
||||||
|
std::string padding,
|
||||||
|
int numFilters,
|
||||||
|
Activation activation,
|
||||||
|
cublasHandle_t cublasHandle
|
||||||
|
)
|
||||||
|
: inputSize(inputSize),
|
||||||
|
inputChannels(inputChannels),
|
||||||
|
kernelSize(kernelSize),
|
||||||
|
stride(stride),
|
||||||
|
numFilters(numFilters),
|
||||||
|
cublasHandle(cublasHandle),
|
||||||
|
activation(activation) {
|
||||||
|
// Allocate memory for kernels
|
||||||
|
|
||||||
|
if (padding == "SAME") {
|
||||||
|
outputSize = inputSize;
|
||||||
|
paddingSize = ((stride - 1) * inputSize - stride + kernelSize) / 2;
|
||||||
|
} else if (padding == "VALID") {
|
||||||
|
paddingSize = 0;
|
||||||
|
outputSize = (inputSize - kernelSize) / stride + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
kernels.resize(kernelSize * kernelSize);
|
||||||
|
initializeKernels();
|
||||||
|
|
||||||
|
d_kernels = nullptr;
|
||||||
|
|
||||||
|
CUDA_CHECK(
|
||||||
|
cudaMalloc((void**)&d_kernels, sizeof(float) * kernelSize * kernelSize)
|
||||||
|
);
|
||||||
|
toCuda();
|
||||||
|
|
||||||
|
d_padded = nullptr;
|
||||||
|
|
||||||
|
if (paddingSize > 0) {
|
||||||
|
CUDA_CHECK(
|
||||||
|
cudaMalloc((void**)&d_padded,
|
||||||
|
sizeof(float) * (inputSize + 2 * paddingSize) *
|
||||||
|
(inputSize + 2 * paddingSize) * inputChannels)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Layers::Conv2d::~Conv2d() {
|
||||||
|
cudaFree(d_kernels);
|
||||||
|
cudaFree(d_padded);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layers::Conv2d::initializeKernels() {
|
||||||
|
std::fill(kernels.begin(), kernels.end(), 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layers::Conv2d::toCuda() {
|
||||||
|
CUDA_CHECK(cudaMemcpy(
|
||||||
|
d_kernels, kernels.data(), sizeof(float) * kernelSize * kernelSize,
|
||||||
|
cudaMemcpyHostToDevice
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layers::Conv2d::forward(const float* d_input, float* d_output) {
|
||||||
|
|
||||||
|
// Padd input
|
||||||
|
int THREADS_PER_BLOCK = 256;
|
||||||
|
int BLOCKS = (outputSize * outputSize * inputChannels) / THREADS_PER_BLOCK + 1;
|
||||||
|
|
||||||
|
pad_matrix_kernel<<<BLOCKS, THREADS_PER_BLOCK>>>(
|
||||||
|
d_input, d_padded, inputSize, inputSize, inputChannels, paddingSize
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: Implement 2D convolution
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user