WIP Migrate Activation to Tensor

This commit is contained in:
2025-11-16 22:17:46 +01:00
parent 64bf9197ff
commit 6133fb20af
7 changed files with 160 additions and 41 deletions

View File

@@ -13,9 +13,10 @@ public:
virtual void* allocate(size_t bytes) = 0;
virtual void deallocate(void* ptr) = 0;
virtual void copyToDevice(void* devicePtr, const void* hostPtr, size_t bytes) = 0;
virtual void copyToHost(void* hostPtr, const void* devicePtr, size_t bytes) = 0;
// Layer operations
virtual void relu(CUDANet::Backend::Tensor &tensor) = 0;
virtual void sigmoid(CUDANet::Backend::Tensor &tensor) = 0;
virtual void softmax(CUDANet::Backend::Tensor &tensor, CUDANet::Backend::Tensor &temp_max, CUDANet::Backend::Tensor &temp_sum) = 0;
};
} // namespace CUDANet::Backend

View File

@@ -0,0 +1,23 @@
#pragma once
#include "backend/backend.hpp"
#include "backend/tensor.hpp"
namespace CUDANet::Backend {
class CUDABackend : public IBackend {
public:
// Memory management
void* allocate(size_t bytes) override;
void deallocate(void* ptr) override;
// Layer operations
void relu(CUDANet::Backend::Tensor &tensor) override;
void sigmoid(CUDANet::Backend::Tensor &tensor) override;
void softmax(CUDANet::Backend::Tensor &tensor, CUDANet::Backend::Tensor &temp_max, CUDANet::Backend::Tensor &temp_sum) override;
private:
static constexpr int BLOCK_SIZE = 256;
};
} // namespace CUDANet::Backend

View File

@@ -18,6 +18,8 @@ typedef std::vector<size_t> Shape;
class Tensor
{
public:
Tensor() = default;
Tensor(Shape shape, DType dtype, IBackend* backend);
~Tensor();
@@ -27,6 +29,10 @@ public:
void toDevice(const void* hostPtr);
void toHost(void* hostPtr);
size_t size() const;
size_t numel() const;
void* data() const;
private:
Shape shape;
DType dtype;

View File

@@ -1,5 +1,7 @@
#ifndef CUDANET_ACTIVATION_H
#define CUDANET_ACTIVATION_H
#pragma once
#include "backend/tensor.hpp"
#include "backend/backend.hpp"
namespace CUDANet::Layers {
@@ -41,29 +43,16 @@ class Activation {
*
* @param d_input Pointer to the input vector on the device
*/
void activate(float* d_input);
void activate(CUDANet::Backend::Tensor input);
private:
CUDANet::Backend::IBackend* backend;
ActivationType activationType;
int length;
void activateCPU(float* input);
#ifdef USE_CUDA
int gridSize;
float* d_softmax_sum;
float* d_max;
void activateCUDA(float* d_input);
void initCUDA();
void delCUDA();
#endif
CUDANet::Backend::Tensor softmax_sum;
CUDANet::Backend::Tensor tensor_max;
};
} // namespace CUDANet::Layers
#endif // CUDANET_ACTIVATION_H