Migrate model class to Tensor

This commit is contained in:
2025-11-22 22:40:38 +01:00
parent ca44ea4436
commit 51bcee01ab
11 changed files with 207 additions and 254 deletions

View File

@@ -1,8 +1,29 @@
#pragma once
#include <cstdio>
#include "backend.hpp"
#include "tensor.hpp"
#ifndef BLOCK_SIZE
#define BLOCK_SIZE 128
#endif // BLOCK_SIZE
/**
* @brief CUDA error checking macro
*
*/
#define CUDA_CHECK(call) \
do { \
cudaError_t result = call; \
if (result != cudaSuccess) { \
fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", \
__FILE__, __LINE__, static_cast<unsigned int>(result), \
cudaGetErrorString(result), #call); \
exit(EXIT_FAILURE); \
} \
} while (0)
namespace CUDANet::Backend {
class CUDA : public Backend {

55
include/model.hpp Normal file
View File

@@ -0,0 +1,55 @@
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include "layer.hpp"
#include "module.hpp"
namespace CUDANet {
enum TensorType {
WEIGHT,
BIAS,
RUNNING_MEAN,
RUNNING_VAR
};
struct TensorInfo {
std::string name;
TensorType type;
int size;
int offset;
};
class Model {
public:
Model(const CUDANet::Shape input_shape, const CUDANet::Shape output_shape);
~Model();
virtual CUDANet::Tensor& predict(CUDANet::Tensor& input);
CUDANet::Layer* get_layer(const std::string& name);
void register_layer(const std::string& name, Layer* layer);
void register_module(Module& module);
void load_weights(const std::string& path);
bool validate();
void print_summary();
protected:
CUDANet::Shape in_shape;
CUDANet::Shape out_shape;
CUDANet::Tensor output;
std::vector<std::pair<std::string, Layer*>> layers;
std::unordered_map<std::string, Layer*> layer_map;
};
} // namespace CUDANet

View File

@@ -1,61 +0,0 @@
#ifndef CUDANET_MODEL_H
#define CUDANET_MODEL_H
#include <string>
#include <unordered_map>
#include <vector>
#include "input.hpp"
#include "layer.hpp"
#include "module.hpp"
#include "output.hpp"
namespace CUDANet {
enum TensorType {
WEIGHT,
BIAS,
RUNNING_MEAN,
RUNNING_VAR
};
struct TensorInfo {
std::string name;
TensorType type;
int size;
int offset;
};
class Model {
public:
Model(const shape2d inputSize, const int inputChannels, const int outputSize);
Model(const Model& other);
~Model();
virtual float* predict(const float* input);
void addLayer(const std::string& name, Layers::SequentialLayer* layer);
Layers::SequentialLayer* getLayer(const std::string& name);
void loadWeights(const std::string& path);
bool validate();
void printSummary();
protected:
Layers::Input* inputLayer;
Layers::Output* outputLayer;
shape2d inputSize;
int inputChannels;
int outputSize;
std::vector<std::pair<std::string, Layers::SequentialLayer*>> layers;
std::unordered_map<std::string, Layers::SequentialLayer*> layerMap;
};
} // namespace CUDANet
#endif // CUDANET_MODEL_H

View File

@@ -19,14 +19,14 @@ class Module {
size_t output_size();
void register_layer(const std::string& name, Layer& layer);
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;
const std::vector<std::pair<std::string, Layer*>>& get_layers() const;
protected:
std::vector<std::pair<std::string, Layer&>> layers;
std::vector<std::pair<std::string, Layer*>> layers;
CUDANet::Shape in_shape;
CUDANet::Shape out_shape;

View File

@@ -1,11 +1,21 @@
#pragma once
#include <format>
#include <vector>
namespace CUDANet {
typedef std::vector<size_t> Shape;
std::string format_shape(const Shape& shape) {
std::string result;
for (size_t i = 0; i < shape.size(); ++i) {
if (i > 0) result += ", ";
result += std::to_string(shape[i]);
}
return result;
}
class InvalidShapeException : public std::runtime_error {
public:
InvalidShapeException(
@@ -35,16 +45,6 @@ class InvalidShapeException : public std::runtime_error {
format_shape(shape_b)
)
) {}
private:
static std::string format_shape(const Shape& shape) {
std::string result;
for (size_t i = 0; i < shape.size(); ++i) {
if (i > 0) result += ", ";
result += std::to_string(shape[i]);
}
return result;
}
};
} // namespace CUDANet

View File

@@ -1,26 +0,0 @@
#ifndef CUDANET_HELPER_H
#define CUDANET_HELPER_H
#include <cuda_runtime.h>
#include <cstdio>
#ifndef BLOCK_SIZE
#define BLOCK_SIZE 128
#endif // BLOCK_SIZE
/**
* @brief CUDA error checking macro
*
*/
#define CUDA_CHECK(call) \
do { \
cudaError_t result = call; \
if (result != cudaSuccess) { \
fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", \
__FILE__, __LINE__, static_cast<unsigned int>(result), \
cudaGetErrorString(result), #call); \
exit(EXIT_FAILURE); \
} \
} while (0)
#endif // CUDANET_HELPER_H