This commit is contained in:
2026-05-15 18:45:50 +07:00
parent 9a296a404c
commit ea467f8298
36 changed files with 2175 additions and 615 deletions
+93 -72
View File
@@ -1,101 +1,122 @@
#ifndef CORE_H
#define CORE_H
#ifndef CORE_HPP
#define CORE_HPP
#include "typedef.hpp"
#include <vector>
#include <string>
#include <vulkan/vulkan.hpp>
#include <functional>
#include <map>
#include <mutex>
struct TrainStatus {
int currentEpoch;
int totalEpochs;
int currentToken;
int totalTokens;
double currentLoss;
double epochLoss;
double lastEpochLoss;
double speed;
double eta;
float percentage;
long totalParams;
// Структура для описания слоя
struct LayerStructure_t {
int size; // Количество нейронов в слое
std::vector<int> sources; // Индексы слоев, которые являются входными для данного
std::vector<int> sourceBranches; // Ветка для каждого источника (-1 если не разделен, 0 или 1)
int branch; // -1 если не разделен, 0 или 1 если слой работает с конкретной веткой
bool isSplit; // true если слой разделяет выход на две ветки
LayerStructure_t() : size(0), branch(-1), isSplit(false) {}
LayerStructure_t(int s, const std::vector<int>& src = {})
: size(s), sources(src), branch(-1), isSplit(false) {
sourceBranches.resize(src.size(), -1);
}
};
class Tokenizer;
class Embedder;
// Параметры для передачи в шейдер через Push Constants
struct TrainParams {
uint32_t mode; // 0: FF, 1: OutError, 2: BackProp, 3: Update
uint32_t prevSize; // Суммарный размер входов
uint32_t nextSize; // Размер текущего слоя
uint32_t wOff; // Смещение весов в буфере W
uint32_t bOff; // Смещение смещений в буфере B
uint32_t oOff; // Смещение входов в буфере O
uint32_t nextOOff; // Смещение выходов в буфере O
float lr; // Скорость обучения
};
// Структура для обратной связи о процессе обучения
struct TrainStatus {
int epoch = 0;
int totalEpochs = 0;
int step = 0;
int totalSteps = 0;
double loss = 0.0;
double totalLoss = 0.0;
double lastEpochLoss = 0.0;
double speed = 0.0;
double eta = 0.0;
float progress = 0.0f;
long long params = 0;
};
class NeuralNetwork {
private:
int numLayers;
std::vector<int> sizes;
std::vector<float> h_weights, h_biases, h_outputs, h_errors;
std::vector<uint32_t> wOff, bOff, oOff;
public:
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkanParam);
~NeuralNetwork();
double train(const std::map<int, std::vector<double>>& inputs,
const std::vector<double>& target, double lr);
std::vector<double> feedForward(const std::map<int, std::vector<double>>& inputs);
void syncToCPU();
void syncToGPU();
// Геттеры для UI
const std::vector<int>& getSizes() const { return sizes; }
const std::vector<std::vector<int>>& getLayerSources() const { return layerSources; }
const std::vector<std::vector<int>>& getSourceBranches() const { return layerSourceBranches; }
const std::vector<int>& getBranches() const { return branches; }
const std::vector<bool>& getSplits() const { return splits; }
private:
bool useVulkan;
int numLayers;
// Метаданные архитектуры
std::vector<int> sizes;
std::vector<uint32_t> wOff;
std::vector<uint32_t> bOff;
std::vector<uint32_t> oOff;
std::vector<std::vector<int>> layerSources;
std::vector<std::vector<int>> layerSourceBranches; // Ветка для каждого источника
std::vector<int> branches; // Ветка каждого слоя (-1, 0, 1)
std::vector<bool> splits; // Является ли слой split
// Данные на стороне CPU
std::vector<float> h_weights;
std::vector<float> h_biases;
std::vector<float> h_outputs;
std::vector<float> h_errors;
// Vulkan объекты
vk::Instance instance;
vk::PhysicalDevice physDev;
vk::Device device;
vk::Queue queue;
vk::CommandPool cmdPool;
uint32_t computeQueueFamilyIndex;
vk::Buffer gpuW, gpuB, gpuO, gpuE, gpuT;
vk::DeviceMemory memW, memB, memO, memE, memT;
void *pW = nullptr, *pB = nullptr, *pO = nullptr, *pE = nullptr, *pT = nullptr;
vk::DescriptorSetLayout dsLayout;
vk::DescriptorPool descriptorPool;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout dsLayout;
vk::PipelineLayout pipeLayout;
vk::Pipeline pipeline;
vk::ShaderModule shaderModule;
struct TrainParams {
uint32_t mode;
uint32_t prevSize;
uint32_t nextSize;
uint32_t wOff;
uint32_t bOff;
uint32_t oOff;
uint32_t nextOOff;
float lr;
};
vk::Buffer gpuW, gpuB, gpuO, gpuE, gpuT;
vk::DeviceMemory memW, memB, memO, memE, memT;
void *pW = nullptr, *pB = nullptr, *pO = nullptr, *pE = nullptr, *pT = nullptr;
// Command buffer pooling
std::vector<vk::CommandBuffer> cmdBuffers;
uint32_t currentCmdBuffer = 0;
void initVulkan();
void initVulkanResources();
uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
uint32_t findMemoryType(uint32_t f, vk::MemoryPropertyFlags p);
std::vector<char> readFile(const std::string& filename);
double runTrainCPU(const std::vector<double>& input, const std::vector<double>& target, double lr);
public:
int cpu_count = 4;
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan = false);
~NeuralNetwork();
void syncToCPU();
void syncToGPU();
std::vector<double> feedForward(const std::vector<double>& input);
double train(const std::vector<double>& input, const std::vector<double>& target, double lr);
void trainOnSequence(
Tokenizer& tok,
Embedder& emb,
const std::string& dataset,
int epochs,
double lr,
std::function<std::vector<double>(const std::vector<int>&, Embedder&)> buildInput,
std::function<void(const TrainStatus&)> onProgress = nullptr
);
long long getTotalParameters() {
long long total = 0;
for (int i = 0; i < numLayers - 1; i++) {
total += (long long)sizes[i] * sizes[i+1];
total += (long long)sizes[i+1];
}
return total;
}
int calculateTotalInputSize(int layerIdx) const;
};
#endif
#endif // CORE_HPP