WIP Migrate Dense layer

This commit is contained in:
2025-11-18 21:12:47 +01:00
parent 64eac7050b
commit 7f203b8947
14 changed files with 116 additions and 221 deletions

61
src/tensor.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include <stdexcept>
#include "tensor.hpp"
using namespace CUDANet;
Tensor::Tensor(Shape shape, DType dtype, Backend* backend)
: shape(shape), dtype(dtype), backend(backend), d_ptr(nullptr) {
// Count total elements
size_t count = 1;
for (const auto& dim : shape) {
count *= dim;
}
total_elms = count;
// Compute total size (bytes)
size_t type_size = 0;
switch (dtype) {
case DType::FLOAT32:
type_size = 4;
break;
default:
throw std::runtime_error("Unsupported data type");
}
total_size = total_elms * type_size;
// Allocate memory on backend
d_ptr = backend->allocate(total_size);
}
Tensor::~Tensor() {
backend->deallocate(d_ptr);
d_ptr = nullptr;
}
size_t Tensor::numel() const {
return total_elms;
}
size_t Tensor::size() const {
return total_size;
}
template <typename T>
const T* Tensor::data() const {
return static_cast<T*>(d_ptr);
}
template <typename T>
T* Tensor::data() {
return static_cast<T*>(d_ptr);
}
void Tensor::zero() {
backend->zero(*this);
}
template <typename T>
void Tensor::set_data(T *data) {
backend->copy_to_device(*this, data, total_size)
}