WIP Migrate vector utils to Tesnor

This commit is contained in:
2025-11-17 22:15:19 +01:00
parent 6133fb20af
commit 6744c8964f
8 changed files with 96 additions and 190 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include <cstddef>
#include "backend/tensor.hpp"
namespace CUDANet::Backend
{
@@ -13,7 +14,13 @@ public:
virtual void* allocate(size_t bytes) = 0;
virtual void deallocate(void* ptr) = 0;
// Layer operations
// Tensor ops
virtual void print(const CUDANet::Backend::Tensor &input) = 0;
virtual void clear(CUDANet::Backend::Tensor &input) = 0;
virtual void sum(const CUDANet::Backend::Tensor &input, CUDANet::Backend::Tensor &sum) = 0;
virtual void max(const CUDANet::Backend::Tensor &input, CUDANet::Backend::Tensor &max) = 0;
// Layer ops
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;

View File

@@ -11,7 +11,13 @@ public:
void* allocate(size_t bytes) override;
void deallocate(void* ptr) override;
// Layer operations
// Tensor ops
void print(const CUDANet::Backend::Tensor &input) override;
void clear(CUDANet::Backend::Tensor &input) override;
void sum(const CUDANet::Backend::Tensor &input, CUDANet::Backend::Tensor &sum) override;
void max(const CUDANet::Backend::Tensor &input, CUDANet::Backend::Tensor &max) override;
// Layer ops
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;

View File

@@ -26,19 +26,20 @@ public:
void* allocate();
void deallocate();
void toDevice(const void* hostPtr);
void toHost(void* hostPtr);
size_t size() const;
size_t numel() const;
void* data() const;
template <typename T>
const T* data() const;
template <typename T>
T* data();
private:
Shape shape;
DType dtype;
IBackend* backend;
void* devicePtr;
void* hostPtr;
void* d_ptr;
};
} // namespace CUDANet::Backend

View File

@@ -1,63 +0,0 @@
#ifndef CUDANET_VECTOR_H
#define CUDANET_VECTOR_H
namespace CUDANet::Utils {
/**
* @brief Utility function that prints a vector
*
* @param d_vec Pointer to the vector on device
* @param length Length of the vector
*/
void print_vec(const float *d_vec, const unsigned int length);
/**
* @brief Utility function that clears a vector
*
* @param d_vector Pointer to the vector on device
* @param len Length of the vector
*/
void clear(float *d_vector, const unsigned int len);
/**
* @brief Utility function that returns the sum of a vector
*
* @param d_vec Pointer to the vector
* @param length Length of the vector
*/
void sum(const float *d_vec, float *d_sum, const unsigned int length);
/**
* @brief Get the max of a vector
*
* @param d_vec Pointer to the vector
* @param length Length of the vector
*/
void max(const float *d_vec, float *d_max, const unsigned int length);
/**
* @brief Compute the mean of the vector
*
* @param d_vec Device pointer to the vector
* @param d_mean Device pointer to the mean
* @param d_length Device pointer to the length
* @param length Length of the vector
*/
void mean(const float *d_vec, float *d_mean, float *d_length, int length);
/**
* @brief Compute the variance of a vector
*
* @param d_vec
* @param d_var
* @param length
*/
void var(float *d_vec, float *d_var, float *d_length, const unsigned int length);
} // namespace CUDANet::Utils
#endif // CUDANET_VECTOR_H