Migrate module to tensors

This commit is contained in:
2025-11-22 18:02:42 +01:00
parent 104d6ea33d
commit ca44ea4436
3 changed files with 64 additions and 49 deletions

View File

@@ -1,32 +0,0 @@
#ifndef CUDANET_MODULE_H
#define CUDANET_MODULE_H
#include <string>
#include <unordered_map>
#include <vector>
#include "layer.hpp"
namespace CUDANet {
class Module : public Layers::SequentialLayer {
public:
virtual float* forward(const float* d_input) = 0;
int getOutputSize();
int getInputSize();
void addLayer(const std::string& name, Layers::SequentialLayer* layer);
const std::vector<std::pair<std::string, Layers::SequentialLayer*>>& getLayers() const;
protected:
std::vector<std::pair<std::string, Layers::SequentialLayer*>> layers;
int outputSize;
int inputSize;
};
} // namespace CUDANet
#endif

35
include/module.hpp Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "layer.hpp"
namespace CUDANet {
class Module {
public:
CUDANet::Shape input_shape();
CUDANet::Shape output_shape();
size_t input_size();
size_t output_size();
void register_layer(const std::string& name, Layer& layer);
void register_module(Module& module);
const std::vector<std::pair<std::string, Layer&>>& get_layers() const;
protected:
std::vector<std::pair<std::string, Layer&>> layers;
CUDANet::Shape in_shape;
CUDANet::Shape out_shape;
};
} // namespace CUDANet

View File

@@ -4,29 +4,41 @@
using namespace CUDANet; using namespace CUDANet;
void Module::addLayer(const std::string& name, Layers::SequentialLayer* layer) { CUDANet::Shape Module::input_shape() {
const Module* module = dynamic_cast<Module*>(layer); return in_shape;
}
if (module != nullptr) { CUDANet::Shape Module::output_shape() {
for (const auto& moduleLayer : module->getLayers()) { return out_shape;
layers.push_back({moduleLayer.first, moduleLayer.second}); }
size_t Module::input_size() {
size_t count = 1;
for (const auto& dim : in_shape) {
count *= dim;
} }
return sizeof(float) * count;
}
return; size_t Module::output_size() {
size_t count = 1;
for (const auto& dim : out_shape) {
count *= dim;
} }
return sizeof(float) * count;
}
void Module::register_layer(const std::string& name, Layer& layer) {
layers.push_back({name, layer}); layers.push_back({name, layer});
} }
const std::vector<std::pair<std::string, Layers::SequentialLayer*>>& void Module::register_module(Module& module) {
Module::getLayers() const { for (const auto& moduleLayer : module.get_layers()) {
layers.push_back({moduleLayer.first, moduleLayer.second});
}
}
const std::vector<std::pair<std::string, Layer&>>&
Module::get_layers() const {
return layers; return layers;
} }
int Module::getInputSize() {
return inputSize;
}
int Module::getOutputSize() {
return outputSize;
}