Refactor Backend and Layer interfaces

This commit is contained in:
2025-11-18 18:27:57 +01:00
parent 25670f90c4
commit 6340b27055
23 changed files with 154 additions and 201 deletions

View File

@@ -2,6 +2,7 @@
#include "backend/tensor.hpp"
#include "backend/backend.hpp"
#include "layers/layer.hpp"
namespace CUDANet::Layers {
@@ -19,7 +20,7 @@ enum ActivationType { SIGMOID, RELU, SOFTMAX, NONE };
* @brief Utility class that performs activation
*
*/
class Activation {
class Activation : Layer {
public:
Activation() = default;

View File

@@ -6,7 +6,7 @@
namespace CUDANet::Layers {
class AvgPooling2d : public SequentialLayer, public TwoDLayer {
class AvgPooling2d : public Layer, public TwoDLayer {
public:
AvgPooling2d(
shape2d inputSize,
@@ -25,7 +25,7 @@ class AvgPooling2d : public SequentialLayer, public TwoDLayer {
*
* @return int output size
*/
int getOutputSize();
int get_output_size();
/**
* @brief Get input size

View File

@@ -9,7 +9,7 @@ namespace CUDANet::Layers {
* @brief Input layer, just copies the input to the device
*
*/
class Input : public SequentialLayer {
class Input : public Layer {
public:
/**
* @brief Create a new Input layer
@@ -38,7 +38,7 @@ class Input : public SequentialLayer {
*
* @return int output size
*/
int getOutputSize();
int get_output_size();
/**
* @brief Get input size

View File

@@ -1,124 +0,0 @@
#ifndef CUDANET_I_LAYER_H
#define CUDANET_I_LAYER_H
#include <vector>
#define CUDANET_SAME_PADDING(inputSize, kernelSize, stride) \
((stride - 1) * inputSize - stride + kernelSize) / 2;
typedef std::pair<int, int> shape2d;
namespace CUDANet::Layers {
class TwoDLayer {
public:
virtual shape2d getOutputDims() = 0;
};
/**
* @brief Basic Sequential Layer
*
*/
class SequentialLayer {
public:
/**
* @brief Destroy the Sequential Layer
*
*/
virtual ~SequentialLayer(){};
/**
* @brief Forward propagation virtual function
*
* @param input Device pointer to the input
* @return float* Device pointer to the output
*/
virtual float* forward(const float* input) = 0;
/**
* @brief Get output size
*
* @return int output size
*/
virtual int getOutputSize() = 0;
/**
* @brief Get input size
*
* @return int input size
*/
virtual int getInputSize() = 0;
};
/**
* @brief Base class for layers with weights and biases
*/
class WeightedLayer : public SequentialLayer {
public:
/**
* @brief Destroy the ILayer object
*
*/
virtual ~WeightedLayer(){};
/**
* @brief Virtual function for forward pass
*
* @param input (Device) Pointer to the input
* @return float* Device pointer to the output
*/
virtual float* forward(const float* input) = 0;
/**
* @brief Virtual function for setting weights
*
* @param weights Pointer to the weights
*/
virtual void setWeights(const float* weights) = 0;
/**
* @brief Virtual function for getting weights
*
*/
virtual std::vector<float> getWeights() = 0;
/**
* @brief Virtual function for setting biases
*
* @param biases Pointer to the biases
*/
virtual void setBiases(const float* biases) = 0;
/**
* @brief Virtual function for getting biases
*
*/
virtual std::vector<float> getBiases() = 0;
private:
/**
* @brief Initialize the weights
*/
virtual void initializeWeights() = 0;
/**
* @brief Initialize the biases
*/
virtual void initializeBiases() = 0;
#ifdef USE_CUDA
/**
* @brief Copy the weights and biases to the device
*/
virtual void toCuda() = 0;
#endif
};
} // namespace CUDANet::Layers
#endif // CUDANET_I_LAYERH

View File

@@ -6,7 +6,7 @@
namespace CUDANet::Layers {
class MaxPooling2d : public SequentialLayer, public TwoDLayer {
class MaxPooling2d : public Layer, public TwoDLayer {
public:
MaxPooling2d(
shape2d inputSize,
@@ -25,7 +25,7 @@ class MaxPooling2d : public SequentialLayer, public TwoDLayer {
*
* @return int output size
*/
int getOutputSize();
int get_output_size();
/**
* @brief Get input size

View File

@@ -5,7 +5,7 @@
namespace CUDANet::Layers {
class Output : public SequentialLayer {
class Output : public Layer {
public:
/**
* @brief Create a new Output layer
@@ -34,7 +34,7 @@ class Output : public SequentialLayer {
*
* @return int output size
*/
int getOutputSize();
int get_output_size();
/**
* @brief Get input size