diff --git a/source/accumulator.cpp b/source/accumulator.cpp index d64f0ad..6301ee8 100644 --- a/source/accumulator.cpp +++ b/source/accumulator.cpp @@ -1,5 +1,6 @@ #include "accumulator.h" #include "types.h" +#include "bitboard.h" namespace ACC { diff --git a/source/accumulator.h b/source/accumulator.h index 24615e7..bbf650c 100644 --- a/source/accumulator.h +++ b/source/accumulator.h @@ -4,7 +4,7 @@ namespace ACC { struct Accumulator { - int16_t values[NNUE::HL_SIZE]; + std::array values; void Add(const NNUE::Network* net, int index); void Sub(const NNUE::Network* net, int index); diff --git a/source/board.cpp b/source/board.cpp index 574e9e0..b21974f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -16,11 +16,11 @@ void Board::Reset() { sideToMove = White; occupied = 0ULL; - accPair = ACC::AccumulatorPair(); - pieces = std::array(); colors = std::array(); + accPair = ACC::AccumulatorPair(); + pieceThreats = std::array(); colorThreats = std::array(); @@ -81,6 +81,7 @@ void Board::SetByFen(std::string_view fen) { hashKey = UTILS::GetHashKey(*this); MOVEGEN::GenThreatMaps(*this); MOVEGEN::GenerateMoves(*this); + ResetAccPair(); } void Board::PrintBoard() { @@ -135,6 +136,28 @@ void Board::PrintBoard() { std::cout << " Hashkey: 0x" << std::hex << hashKey << std::dec << std::endl; } +void Board::PrintNNUE() { + std::cout << "First 16 HL biases: "; + + for (int i = 0; i < 16; i++) { + std::cout << NNUE::net.accumulator_biases[i] << ' '; + } + + std::cout << std::endl; + + std::cout << "Output neuron bias: " << NNUE::net.output_bias << std::endl; + + std::cout << "Active indices (white)"; + + for (int i = 0; i < NNUE::HL_SIZE; i++) { + if (accPair.white.values[i] != 0) { + std::cout << accPair.white.values[i] << ' '; + } + } + + std::cout << std::endl; +} + void Board::AddMove(Move move) { moveList[currentMoveIndex] = move; currentMoveIndex++; @@ -380,3 +403,39 @@ bool Board::InPossibleZug() { return !toCheck; } + +void Board::ResetAccPair() { + Bitboard whitePieces = colors[White]; + Bitboard blackPieces = colors[Black]; + + accPair.white.values = NNUE::net.accumulator_biases; + accPair.black.values = NNUE::net.accumulator_biases; + + while (whitePieces) { + int square = whitePieces.getLS1BIndex(); + + int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), White); + int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), White); + + for (int i = 0; i < NNUE::HL_SIZE; i++) { + accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; + accPair.black.values[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; + } + + whitePieces.PopBit(square); + } + + while (blackPieces) { + int square = blackPieces.getLS1BIndex(); + + int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), Black); + int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), Black); + + for (int i = 0; i < NNUE::HL_SIZE; i++) { + accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; + accPair.black.values[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; + } + + blackPieces.PopBit(square); + } +} \ No newline at end of file diff --git a/source/board.h b/source/board.h index 658554e..9a95964 100644 --- a/source/board.h +++ b/source/board.h @@ -42,15 +42,18 @@ public: U64 hashKey = 0ULL; - void Init(); void Reset(); void SetByFen(std::string_view fen); void PrintBoard(); + void PrintNNUE(); + void AddMove(Move move); void ResetMoves(); void ListMoves(); + void ResetAccPair(); + int GetPieceType(int square); int GetPieceColor(int square); diff --git a/source/nnue.cpp b/source/nnue.cpp index 5b04667..05c7235 100644 --- a/source/nnue.cpp +++ b/source/nnue.cpp @@ -33,18 +33,6 @@ void Network::Load(const std::string& path) { 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 30a2da7..0f9c894 100644 --- a/source/nnue.h +++ b/source/nnue.h @@ -19,7 +19,6 @@ struct Network { 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 fdeb8a6..5055f65 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -195,7 +195,7 @@ void UCILoop(Board &board) { } if (input.find("nnue") != std::string::npos) { - NNUE::net.Print(); + board.PrintNNUE(); } } }