Nnue test #48

Merged
rektdie merged 14 commits from nnue_test into main 2025-04-09 03:23:41 +02:00
6 changed files with 55 additions and 6 deletions
Showing only changes of commit 67e9ebdda3 - Show all commits

No files matched your search

BIN
View File
Binary file not shown.
+2 -3
View File
@@ -15,15 +15,14 @@ int CalculateIndex(bool perspective, int square, int pieceType, bool side) {
void Accumulator::Add(const NNUE::Network* net, int index) { void Accumulator::Add(const NNUE::Network* net, int index) {
for (int i = 0; i < NNUE::HL_SIZE; i ++) { for (int i = 0; i < NNUE::HL_SIZE; i ++) {
values[i] += net->accumulator_weights[index][i]; values[i] += net->accumulator_weights[index * NNUE::INPUT_SIZE + i];
} }
} }
void Accumulator::Sub(const NNUE::Network* net, int index) { void Accumulator::Sub(const NNUE::Network* net, int index) {
for (int i = 0; i < NNUE::HL_SIZE; i ++) { for (int i = 0; i < NNUE::HL_SIZE; i ++) {
values[i] -= net->accumulator_weights[index][i]; values[i] -= net->accumulator_weights[index * NNUE::INPUT_SIZE + i];
} }
} }
} }
+3
View File
@@ -5,6 +5,7 @@
#include "benchmark.h" #include "benchmark.h"
#include "search.h" #include "search.h"
#include "datagen.h" #include "datagen.h"
#include "nnue.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
MOVEGEN::initLeaperAttacks(); MOVEGEN::initLeaperAttacks();
@@ -12,6 +13,8 @@ int main(int argc, char* argv[]) {
UTILS::InitZobrist(); UTILS::InitZobrist();
SEARCH::InitLMRTable(); SEARCH::InitLMRTable();
NNUE::net.Load("beans.bin");
Board board; Board board;
if (argc > 1) { if (argc > 1) {
+39
View File
@@ -1,4 +1,6 @@
#include <immintrin.h> #include <immintrin.h>
#include <fstream>
#include "nnue.h" #include "nnue.h"
#include "accumulator.h" #include "accumulator.h"
#include "board.h" #include "board.h"
@@ -6,6 +8,43 @@
namespace NNUE { namespace NNUE {
void Network::Load(const std::string& path) {
std::ifstream file(path, std::ios::binary);
if (!file.is_open()){
std::cerr << "Failed to open file " + path << std::endl;
}
// HL weights
for (int i = 0; i < accumulator_weights.size(); i++) {
file.read(reinterpret_cast<char*>(&accumulator_weights[i]), sizeof(int16_t));
}
// HL biases
for (int i = 0; i < accumulator_biases.size(); i++) {
file.read(reinterpret_cast<char*>(&accumulator_biases[i]), sizeof(int16_t));
}
// Output weights
for (int i = 0; i < output_weights.size(); i++) {
file.read(reinterpret_cast<char*>(&output_weights[i]), sizeof(int16_t));
}
// Output bias
file.read(reinterpret_cast<char*>(&output_bias), sizeof(int16_t));
}
void Network::Print() {
std::cout << "First 16 HL biases: ";
for (int i = 0; i < 16; i++) {
std::cout << accumulator_biases[i] << ' ';
}
std::cout << std::endl;
std::cout << "Output neuron bias: " << output_bias << std::endl;
}
static int32_t Forward(const Board& board, static int32_t Forward(const Board& board,
const Network* net) { const Network* net) {
const ACC::Accumulator& stmAcc = board.sideToMove == White ? board.accPair.white : board.accPair.black; const ACC::Accumulator& stmAcc = board.sideToMove == White ? board.accPair.white : board.accPair.black;
+7 -3
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <array>
namespace NNUE { namespace NNUE {
@@ -12,12 +13,15 @@ constexpr int QA = 255;
constexpr int QB = 64; constexpr int QB = 64;
struct Network { struct Network {
int16_t accumulator_weights[INPUT_SIZE][HL_SIZE]; std::array<int16_t, HL_SIZE * INPUT_SIZE> accumulator_weights;
int16_t accumulator_biases[HL_SIZE]; std::array<int16_t, HL_SIZE> accumulator_biases;
int16_t output_weights[2 * HL_SIZE]; std::array<int16_t, 2 * HL_SIZE> output_weights;
int16_t output_bias; int16_t output_bias;
void Load(const std::string& path); void Load(const std::string& path);
void Print();
}; };
inline Network net;
} }
+4
View File
@@ -193,5 +193,9 @@ void UCILoop(Board &board) {
board.ListMoves(); board.ListMoves();
continue; continue;
} }
if (input.find("nnue") != std::string::npos) {
NNUE::net.Print();
}
} }
} }