the beginning of the implementation of Vulkan
This commit is contained in:
+45
-11
@@ -5,41 +5,51 @@
|
|||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <fstream>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
NeuralNetwork::NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan) : numLayers(count) {
|
NeuralNetwork::NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan) : numLayers(count) {
|
||||||
|
|
||||||
if (useVulkan) {
|
if (useVulkan) {
|
||||||
|
|
||||||
vk::ApplicationInfo appInfo{"Xenith", 1, nullptr, 0, VK_API_VERSION_1_1};
|
vk::ApplicationInfo appInfo{"Xenith", 1, nullptr, 0, VK_API_VERSION_1_1};
|
||||||
instance = vk::createInstance({{}, &appInfo});
|
instance = vk::createInstance({{}, &appInfo});
|
||||||
|
|
||||||
|
// 2. Выбор видеокарты
|
||||||
auto physicalDevices = instance.enumeratePhysicalDevices();
|
auto physicalDevices = instance.enumeratePhysicalDevices();
|
||||||
|
if (physicalDevices.empty()) throw std::runtime_error("GPU с поддержкой Vulkan не найдены!");
|
||||||
physDev = physicalDevices[0];
|
physDev = physicalDevices[0];
|
||||||
auto props = physDev.getProperties();
|
|
||||||
std::cout << "Используем GPU: " << props.deviceName << std::endl;
|
std::cout << "Используем GPU: " << physDev.getProperties().deviceName << std::endl;
|
||||||
|
|
||||||
// 3. Поиск очереди для вычислений
|
// 3. Поиск очереди для вычислений (Compute)
|
||||||
auto queueProps = physDev.getQueueFamilyProperties();
|
auto queueProps = physDev.getQueueFamilyProperties();
|
||||||
int computeFamily = -1;
|
int computeFamily = -1;
|
||||||
for (int i = 0; i < queueProps.size(); i++) {
|
for (int i = 0; i < queueProps.size(); i++) {
|
||||||
if (queueProps[i].queueFlags & vk::QueueFlagBits::eCompute) {
|
if (queueProps[i].queueFlags & vk::QueueFlagBits::eCompute) {
|
||||||
computeFamily = i; break;
|
computeFamily = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (computeFamily == -1) throw std::runtime_error("GPU не поддерживает Compute");
|
|
||||||
|
if (computeFamily == -1) throw std::runtime_error("GPU не поддерживает вычисления (Compute)");
|
||||||
|
|
||||||
|
// ВАЖНО: Сохраняем индекс в переменную класса, чтобы использовать её везде
|
||||||
|
this->computeQueueFamilyIndex = (uint32_t)computeFamily;
|
||||||
|
|
||||||
// 4. Логическое устройство
|
// 4. Создание логического устройства
|
||||||
float priority = 1.0f;
|
float priority = 1.0f;
|
||||||
vk::DeviceQueueCreateInfo queueInfo({}, (uint32_t)computeFamily, 1, &priority);
|
vk::DeviceQueueCreateInfo queueInfo({}, computeQueueFamilyIndex, 1, &priority);
|
||||||
vk::DeviceCreateInfo deviceCreateInfo({}, 1, &queueInfo);
|
vk::DeviceCreateInfo deviceCreateInfo({}, 1, &queueInfo);
|
||||||
device = physDev.createDevice(deviceCreateInfo);
|
device = physDev.createDevice(deviceCreateInfo);
|
||||||
|
|
||||||
queue = device.getQueue(computeFamily, 0);
|
// 5. Получаем саму очередь
|
||||||
|
queue = device.getQueue(computeQueueFamilyIndex, 0);
|
||||||
|
|
||||||
|
// 6. Создаем пул команд (теперь используем правильный индекс)
|
||||||
|
vk::CommandPoolCreateInfo poolInfo({}, computeQueueFamilyIndex);
|
||||||
|
cmdPool = device.createCommandPool(poolInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) sizes.push_back(layers[i].size);
|
for (int i = 0; i < count; i++) sizes.push_back(layers[i].size);
|
||||||
@@ -132,6 +142,25 @@ uint32_t NeuralNetwork::findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Внутри класса NeuralNetwork в секции private:
|
||||||
|
std::vector<char> NeuralNetwork::readFile(const std::string& filename) {
|
||||||
|
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
throw std::runtime_error("Не удалось открыть файл шейдера: " + filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fileSize = (size_t)file.tellg();
|
||||||
|
std::vector<char> buffer(fileSize);
|
||||||
|
|
||||||
|
file.seekg(0);
|
||||||
|
file.read(buffer.data(), fileSize);
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
double NeuralNetwork::trainVulkan() {
|
double NeuralNetwork::trainVulkan() {
|
||||||
// 1. Создание буферов
|
// 1. Создание буферов
|
||||||
vk::Buffer inputBuffer = device.createBuffer({{}, sizeof(float) * 2, vk::BufferUsageFlagBits::eStorageBuffer});
|
vk::Buffer inputBuffer = device.createBuffer({{}, sizeof(float) * 2, vk::BufferUsageFlagBits::eStorageBuffer});
|
||||||
@@ -218,4 +247,9 @@ double NeuralNetwork::trainVulkan() {
|
|||||||
device.destroyBuffer(outputBuffer); device.freeMemory(outputMemory);
|
device.destroyBuffer(outputBuffer); device.freeMemory(outputMemory);
|
||||||
|
|
||||||
return (double)result;
|
return (double)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NeuralNetwork::~NeuralNetwork() {
|
||||||
|
// Здесь позже мы добавим удаление vkInstance, vkDevice и прочего,
|
||||||
|
// чтобы не было утечек памяти на видеокарте.
|
||||||
|
}
|
||||||
|
|||||||
+17
-2
@@ -4,7 +4,6 @@
|
|||||||
#include "typedef.hpp"
|
#include "typedef.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "core.hpp"
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
#include <vulkan/vulkan.hpp>
|
#include <vulkan/vulkan.hpp>
|
||||||
@@ -13,28 +12,44 @@
|
|||||||
|
|
||||||
class NeuralNetwork {
|
class NeuralNetwork {
|
||||||
private:
|
private:
|
||||||
|
// Параметры нейросети
|
||||||
int numLayers;
|
int numLayers;
|
||||||
std::vector<int> sizes;
|
std::vector<int> sizes;
|
||||||
std::vector<std::vector<std::vector<double>>> weights;
|
std::vector<std::vector<std::vector<double>>> weights;
|
||||||
std::vector<std::vector<double>> biases;
|
std::vector<std::vector<double>> biases;
|
||||||
std::vector<std::vector<double>> outputs;
|
std::vector<std::vector<double>> outputs;
|
||||||
|
|
||||||
|
// Объекты Vulkan
|
||||||
|
bool useVulkan; // Сохраняем выбор пользователя
|
||||||
vk::Instance instance;
|
vk::Instance instance;
|
||||||
vk::PhysicalDevice physDev;
|
vk::PhysicalDevice physDev;
|
||||||
vk::Device device;
|
vk::Device device;
|
||||||
vk::Queue queue;
|
vk::Queue queue;
|
||||||
vk::CommandPool cmdPool;
|
vk::CommandPool cmdPool;
|
||||||
|
uint32_t computeQueueFamilyIndex; // Индекс очереди для команд
|
||||||
|
|
||||||
uint32_t NeuralNetwork::findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
|
// Вспомогательные методы Vulkan
|
||||||
|
uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties);
|
||||||
|
std::vector<char> readFile(const std::string& filename);
|
||||||
|
|
||||||
|
// Математика CPU
|
||||||
double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }
|
double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }
|
||||||
double sigmoidDeriv(double x) { return x * (1.0 - x); }
|
double sigmoidDeriv(double x) { return x * (1.0 - x); }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int cpu_count = 1;
|
int cpu_count = 1;
|
||||||
|
|
||||||
|
// Конструктор
|
||||||
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan = false);
|
NeuralNetwork(LayerStructure_t layers[], int count, bool useVulkan = false);
|
||||||
|
|
||||||
|
// Деструктор (ВАЖНО для очистки Vulkan)
|
||||||
|
~NeuralNetwork();
|
||||||
|
|
||||||
|
// Методы работы
|
||||||
std::vector<double> feedForward(const std::vector<double>& input);
|
std::vector<double> feedForward(const std::vector<double>& input);
|
||||||
double train(const std::vector<double>& input, const std::vector<double>& target, double lr);
|
double train(const std::vector<double>& input, const std::vector<double>& target, double lr);
|
||||||
|
|
||||||
|
// Наш тест Vulkan
|
||||||
double trainVulkan();
|
double trainVulkan();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ int main() {
|
|||||||
std::cout << "System Prompt updated!" << std::endl;
|
std::cout << "System Prompt updated!" << std::endl;
|
||||||
|
|
||||||
} else if (cmdIn == "/trainVulkan") {
|
} else if (cmdIn == "/trainVulkan") {
|
||||||
nn.trainVulkan();
|
std::cout << nn.trainVulkan();
|
||||||
|
|
||||||
} else if (cmdIn == "/help") {
|
} else if (cmdIn == "/help") {
|
||||||
std::cout << "\n--- MENU ---" << std::endl;
|
std::cout << "\n--- MENU ---" << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user