added accumulator reset (not working properly yet)

This commit is contained in:
rektdie committed 2025-04-08 19:59:14 +02:00
1 parent 67e9ebdda3
commit b7e0b37ca8
7 files changed
+68 -18

No files matched your search

+1
View File
@@ -1,5 +1,6 @@
#include "accumulator.h" #include "accumulator.h"
#include "types.h" #include "types.h"
#include "bitboard.h"
namespace ACC { namespace ACC {
+1 -1
View File
@@ -4,7 +4,7 @@
namespace ACC { namespace ACC {
struct Accumulator { struct Accumulator {
int16_t values[NNUE::HL_SIZE]; std::array<int16_t, NNUE::HL_SIZE> values;
void Add(const NNUE::Network* net, int index); void Add(const NNUE::Network* net, int index);
void Sub(const NNUE::Network* net, int index); void Sub(const NNUE::Network* net, int index);
+61 -2
View File
@@ -16,11 +16,11 @@ void Board::Reset() {
sideToMove = White; sideToMove = White;
occupied = 0ULL; occupied = 0ULL;
accPair = ACC::AccumulatorPair();
pieces = std::array<Bitboard, 6>(); pieces = std::array<Bitboard, 6>();
colors = std::array<Bitboard, 2>(); colors = std::array<Bitboard, 2>();
accPair = ACC::AccumulatorPair();
pieceThreats = std::array<Bitboard, 6>(); pieceThreats = std::array<Bitboard, 6>();
colorThreats = std::array<Bitboard, 2>(); colorThreats = std::array<Bitboard, 2>();
@@ -81,6 +81,7 @@ void Board::SetByFen(std::string_view fen) {
hashKey = UTILS::GetHashKey(*this); hashKey = UTILS::GetHashKey(*this);
MOVEGEN::GenThreatMaps(*this); MOVEGEN::GenThreatMaps(*this);
MOVEGEN::GenerateMoves<All>(*this); MOVEGEN::GenerateMoves<All>(*this);
ResetAccPair();
} }
void Board::PrintBoard() { void Board::PrintBoard() {
@@ -135,6 +136,28 @@ void Board::PrintBoard() {
std::cout << " Hashkey: 0x" << std::hex << hashKey << std::dec << std::endl; 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) { void Board::AddMove(Move move) {
moveList[currentMoveIndex] = move; moveList[currentMoveIndex] = move;
currentMoveIndex++; currentMoveIndex++;
@@ -380,3 +403,39 @@ bool Board::InPossibleZug() {
return !toCheck; 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);
}
}
+4 -1
View File
@@ -42,15 +42,18 @@ public:
U64 hashKey = 0ULL; U64 hashKey = 0ULL;
void Init();
void Reset(); void Reset();
void SetByFen(std::string_view fen); void SetByFen(std::string_view fen);
void PrintBoard(); void PrintBoard();
void PrintNNUE();
void AddMove(Move move); void AddMove(Move move);
void ResetMoves(); void ResetMoves();
void ListMoves(); void ListMoves();
void ResetAccPair();
int GetPieceType(int square); int GetPieceType(int square);
int GetPieceColor(int square); int GetPieceColor(int square);
-12
View File
@@ -33,18 +33,6 @@ void Network::Load(const std::string& path) {
file.read(reinterpret_cast<char*>(&output_bias), sizeof(int16_t)); 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;
-1
View File
@@ -19,7 +19,6 @@ struct Network {
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; inline Network net;
+1 -1
View File
@@ -195,7 +195,7 @@ void UCILoop(Board &board) {
} }
if (input.find("nnue") != std::string::npos) { if (input.find("nnue") != std::string::npos) {
NNUE::net.Print(); board.PrintNNUE();
} }
} }
} }