FirstVersion

This commit is contained in:
2026-05-03 21:02:34 +07:00
parent 05875d8aa2
commit 0d6b240099
12 changed files with 632 additions and 566 deletions
+63 -41
View File
@@ -3,51 +3,46 @@
#include "typedef.hpp"
#include <vector>
#include <cmath>
#include <cstdlib>
#include <omp.h>
#include <string>
#include <vulkan/vulkan.hpp>
#include <iostream>
#include <fstream>
#include <functional>
struct TrainStatus {
int currentEpoch;
int totalEpochs;
int currentToken;
int totalTokens;
double currentLoss;
double epochLoss;
double lastEpochLoss;
double speed;
double eta;
float percentage;
long totalParams;
};
class Tokenizer;
class Embedder;
class NeuralNetwork {
private:
// Параметры нейросети
int numLayers;
std::vector<int> sizes;
std::vector<std::vector<std::vector<double>>> weights;
std::vector<std::vector<double>> biases;
std::vector<std::vector<double>> outputs;
std::vector<float> h_weights, h_biases, h_outputs, h_errors;
std::vector<uint32_t> wOff, bOff, oOff;
// Объекты Vulkan
bool useVulkan; // Сохраняем выбор пользователя
bool useVulkan;
vk::Instance instance;
vk::PhysicalDevice physDev;
vk::Device device;
vk::Queue queue;
vk::CommandPool cmdPool;
uint32_t computeQueueFamilyIndex; // Индекс очереди для команд
uint32_t computeQueueFamilyIndex;
// Вспомогательные методы Vulkan
uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
std::vector<char> readFile(const std::string& filename);
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;
// Математика CPU
double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }
double sigmoidDeriv(double x) { return x * (1.0 - x); }
struct TrainParams {
uint32_t prevLayerSize;
uint32_t nextLayerSize;
uint32_t weightOffset;
uint32_t biasOffset;
uint32_t outOffset;
uint32_t errOffset;
float lr;
};
vk::Buffer gpuW, gpuB, gpuO, gpuE;
vk::DeviceMemory memW, memB, memO, memE;
vk::DescriptorPool descriptorPool;
vk::DescriptorSet descriptorSet;
vk::DescriptorSetLayout dsLayout;
@@ -55,25 +50,52 @@ private:
vk::Pipeline pipeline;
vk::ShaderModule shaderModule;
bool vulkanResourcesInitialized = false;
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;
};
void initVulkanResources(); // Метод для разовой инициализации
void cleanupVulkanResources();
void initVulkan();
void initVulkanResources();
uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
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 = 1;
// Конструктор
int cpu_count = 4;
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan = false);
// Деструктор (ВАЖНО для очистки Vulkan)
~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);
double trainVulkan(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;
}
};
#endif