Nnue test #48

Merged
rektdie merged 14 commits from nnue_test into main 2025-04-09 03:23:41 +02:00
8 changed files with 108 additions and 43 deletions
Showing only changes of commit 2ea6bd1c64 - Show all commits

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+64 -7
View File
@@ -4,7 +4,7 @@
namespace ACC { namespace ACC {
int CalculateIndex(bool perspective, int square, int pieceType, bool side) { int CalculateIndex(bool perspective, bool side, int pieceType, int square) {
if (perspective == Black) if (perspective == Black)
{ {
side = 1 - side; // Flip side side = 1 - side; // Flip side
@@ -14,15 +14,72 @@ int CalculateIndex(bool perspective, int square, int pieceType, bool side) {
return side * 64 * 6 + pieceType * 64 + square; return side * 64 * 6 + pieceType * 64 + square;
} }
void Accumulator::Add(const NNUE::Network* net, int index) { // All friendly, for quiets
for (int i = 0; i < NNUE::HL_SIZE; i ++) { void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) {
values[i] += net->accumulator_weights[index * NNUE::INPUT_SIZE + i]; int addW = CalculateIndex(White, stm, addPT, add);
int addB = CalculateIndex(Black, stm, addPT, add);
int subW = CalculateIndex(White, stm, subPT, sub);
int subB = CalculateIndex(Black, stm, subPT, sub);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i];
} }
} }
void Accumulator::Sub(const NNUE::Network* net, int index) { // Captures
for (int i = 0; i < NNUE::HL_SIZE; i ++) { void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2) {
values[i] -= net->accumulator_weights[index * NNUE::INPUT_SIZE + i]; int addW = CalculateIndex(White, stm, addPT, add);
int addB = CalculateIndex(Black, stm, addPT, add);
int subW1 = CalculateIndex(White, stm, subPT1, sub1);
int subB1 = CalculateIndex(Black, stm, subPT1, sub1);
int subW2 = CalculateIndex(White, ~stm, subPT2, sub2);
int subB2 = CalculateIndex(Black, ~stm, subPT2, sub2);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
}
}
// Castling
void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2) {
int addW1 = CalculateIndex(White, stm, addPT1, add1);
int addB1 = CalculateIndex(Black, stm, addPT1, add1);
int addW2 = CalculateIndex(White, stm, addPT2, add2);
int addB2 = CalculateIndex(Black, stm, addPT2, add2);
int subW1 = CalculateIndex(White, stm, subPT1, sub1);
int subB1 = CalculateIndex(Black, stm, subPT1, sub1);
int subW2 = CalculateIndex(White, stm, subPT2, sub2);
int subB2 = CalculateIndex(Black, stm, subPT2, sub2);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
white.values[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i];
white.values[i] += NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
} }
} }
+6 -4
View File
@@ -5,16 +5,18 @@ namespace ACC {
struct Accumulator { struct Accumulator {
std::array<int16_t, NNUE::HL_SIZE> values; std::array<int16_t, NNUE::HL_SIZE> values;
void Add(const NNUE::Network* net, int index);
void Sub(const NNUE::Network* net, int index);
}; };
struct AccumulatorPair { struct AccumulatorPair {
Accumulator white; Accumulator white;
Accumulator black; Accumulator black;
// yoinked from Quinniboi - Prelude
void addSub(bool stm, int add, int addPT, int sub, int subPT);
void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2);
void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2);
}; };
int CalculateIndex(bool perspective, int square, int pieceType, bool side); int CalculateIndex(bool perspective, bool side, int pieceType, int square);
} }
+31 -29
View File
@@ -278,9 +278,33 @@ bool Board::MakeMove(Move move) {
int targetPiece = GetPieceType(move.MoveTo()); int targetPiece = GetPieceType(move.MoveTo());
int direction = attackerColor ? south : north; int direction = attackerColor ? south : north;
int endPiece = attackerPiece;
// Removing attacker piece from old position // Removing attacker piece from old position
RemovePiece(attackerPiece, move.MoveFrom(), attackerColor); RemovePiece(attackerPiece, move.MoveFrom(), attackerColor);
if (move.IsPromo()) {
if (move.IsCapture()) {
endPiece = move.GetFlags() - 9;
Promote(move.MoveTo(), endPiece, sideToMove, true);
} else {
endPiece = move.GetFlags() - 5;
Promote(move.MoveTo(), endPiece, sideToMove, false);
}
}
if (move.IsCapture()) {
if (move.GetFlags() != epCapture) {
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece);
} else {
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, enPassantTarget, Pawn);
}
} else {
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece);
}
}
switch (move.GetFlags()) switch (move.GetFlags())
{ {
case quiet: case quiet:
@@ -312,6 +336,8 @@ bool Board::MakeMove(Move move) {
SetPiece(Rook, rookSquare - 2, attackerColor); SetPiece(Rook, rookSquare - 2, attackerColor);
SetPiece(attackerPiece, move.MoveTo(), attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor);
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook);
break; break;
} }
case queenCastle: case queenCastle:
@@ -325,32 +351,9 @@ bool Board::MakeMove(Move move) {
SetPiece(Rook, rookSquare + 3, attackerColor); SetPiece(Rook, rookSquare + 3, attackerColor);
SetPiece(attackerPiece, move.MoveTo(), attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor);
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook);
break; break;
} }
case knightPromotion:
Promote(move.MoveTo(), Knight, attackerColor, false);
break;
case bishopPromotion:
Promote(move.MoveTo(), Bishop, attackerColor, false);
break;
case rookPromotion:
Promote(move.MoveTo(), Rook, attackerColor, false);
break;
case queenPromotion:
Promote(move.MoveTo(), Queen, attackerColor, false);
break;
case knightPromoCapture:
Promote(move.MoveTo(), Knight, attackerColor, true);
break;
case bishopPromoCapture:
Promote(move.MoveTo(), Bishop, attackerColor, true);
break;
case rookPromoCapture:
Promote(move.MoveTo(), Rook, attackerColor, true);
break;
case queenPromoCapture:
Promote(move.MoveTo(), Queen, attackerColor, true);
break;
default: default:
break; break;
} }
@@ -371,7 +374,6 @@ bool Board::MakeMove(Move move) {
enPassantTarget = newEpTarget; enPassantTarget = newEpTarget;
MOVEGEN::GenThreatMaps(*this); MOVEGEN::GenThreatMaps(*this);
sideToMove = !sideToMove; sideToMove = !sideToMove;
@@ -416,8 +418,8 @@ void Board::ResetAccPair() {
while (whitePieces) { while (whitePieces) {
int square = whitePieces.getLS1BIndex(); int square = whitePieces.getLS1BIndex();
int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), White); int wInput = ACC::CalculateIndex(White, White, GetPieceType(square), square);
int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), White); int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
@@ -430,8 +432,8 @@ void Board::ResetAccPair() {
while (blackPieces) { while (blackPieces) {
int square = blackPieces.getLS1BIndex(); int square = blackPieces.getLS1BIndex();
int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), Black); int wInput = ACC::CalculateIndex(White, Black, GetPieceType(square), square);
int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), Black); int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
+1 -1
View File
@@ -13,7 +13,7 @@ int main(int argc, char* argv[]) {
UTILS::InitZobrist(); UTILS::InitZobrist();
SEARCH::InitLMRTable(); SEARCH::InitLMRTable();
NNUE::net.Load("beans.bin"); NNUE::net.Load("nnue.bin");
Board board; Board board;
+4
View File
@@ -60,6 +60,10 @@ public:
|| GetFlags() == queenCastle || GetFlags() == doublePawnPush); || GetFlags() == queenCastle || GetFlags() == doublePawnPush);
} }
bool IsPromo() {
return (GetFlags() >= knightPromotion);
}
void PrintMove() { void PrintMove() {
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()]; std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
+2 -2
View File
@@ -156,7 +156,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
} }
} }
int bestScore = HCE::Evaluate(board); int bestScore = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
TTEntry *entry = TT.GetRawEntry(board.hashKey); TTEntry *entry = TT.GetRawEntry(board.hashKey);
if (entry->hashKey == board.hashKey) { if (entry->hashKey == board.hashKey) {
@@ -239,7 +239,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply); if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply);
const int staticEval = HCE::Evaluate(board); const int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
if (!board.InCheck()) { if (!board.InCheck()) {
if (ply) { if (ply) {