diff --git a/beans.bin b/beans.bin new file mode 100644 index 0000000..219a711 Binary files /dev/null and b/beans.bin differ diff --git a/source/accumulator.cpp b/source/accumulator.cpp index 15537d6..d64f0ad 100644 --- a/source/accumulator.cpp +++ b/source/accumulator.cpp @@ -15,15 +15,14 @@ int CalculateIndex(bool perspective, int square, int pieceType, bool side) { void Accumulator::Add(const NNUE::Network* net, int index) { 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) { 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]; } } - } diff --git a/source/main.cpp b/source/main.cpp index 486cee2..069cfac 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -5,6 +5,7 @@ #include "benchmark.h" #include "search.h" #include "datagen.h" +#include "nnue.h" int main(int argc, char* argv[]) { MOVEGEN::initLeaperAttacks(); @@ -12,6 +13,8 @@ int main(int argc, char* argv[]) { UTILS::InitZobrist(); SEARCH::InitLMRTable(); + NNUE::net.Load("beans.bin"); + Board board; if (argc > 1) { diff --git a/source/nnue.cpp b/source/nnue.cpp index af487e1..5b04667 100644 --- a/source/nnue.cpp +++ b/source/nnue.cpp @@ -1,4 +1,6 @@ #include +#include + #include "nnue.h" #include "accumulator.h" #include "board.h" @@ -6,6 +8,43 @@ 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(&accumulator_weights[i]), sizeof(int16_t)); + } + + // HL biases + for (int i = 0; i < accumulator_biases.size(); i++) { + file.read(reinterpret_cast(&accumulator_biases[i]), sizeof(int16_t)); + } + + // Output weights + for (int i = 0; i < output_weights.size(); i++) { + file.read(reinterpret_cast(&output_weights[i]), sizeof(int16_t)); + } + + // Output bias + file.read(reinterpret_cast(&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, const Network* net) { const ACC::Accumulator& stmAcc = board.sideToMove == White ? board.accPair.white : board.accPair.black; diff --git a/source/nnue.h b/source/nnue.h index 787fea0..30a2da7 100644 --- a/source/nnue.h +++ b/source/nnue.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace NNUE { @@ -12,12 +13,15 @@ constexpr int QA = 255; constexpr int QB = 64; struct Network { - int16_t accumulator_weights[INPUT_SIZE][HL_SIZE]; - int16_t accumulator_biases[HL_SIZE]; - int16_t output_weights[2 * HL_SIZE]; + std::array accumulator_weights; + std::array accumulator_biases; + std::array output_weights; int16_t output_bias; void Load(const std::string& path); + void Print(); }; +inline Network net; + } diff --git a/source/uci.cpp b/source/uci.cpp index 53d8121..fdeb8a6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -193,5 +193,9 @@ void UCILoop(Board &board) { board.ListMoves(); continue; } + + if (input.find("nnue") != std::string::npos) { + NNUE::net.Print(); + } } }